blob: fdb9b5655414e77dc6506cdda13642ef16115e27 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Nithin Nayak Sujirb681b652013-01-06 12:51:10 +00007 * Copyright (C) 2005-2013 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000047#include <linux/ssb/ssb_driver_gige.h>
Michael Chanaed93e02012-07-16 16:24:02 +000048#include <linux/hwmon.h>
49#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030052#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000056#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Matt Carlsonbe947302012-12-03 19:36:57 +000058#include <uapi/linux/net_tstamp.h>
59#include <linux/ptp_clock_kernel.h>
60
David S. Miller49b6e95f2007-03-29 01:38:42 -070061#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070063#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
65
Matt Carlson63532392008-11-03 16:49:57 -080066#define BAR_0 0
67#define BAR_2 2
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include "tg3.h"
70
Joe Perches63c3a662011-04-26 08:12:10 +000071/* Functions & macros to verify TG3_FLAGS types */
72
73static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 return test_bit(flag, bits);
76}
77
78static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 set_bit(flag, bits);
81}
82
83static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
84{
85 clear_bit(flag, bits);
86}
87
88#define tg3_flag(tp, flag) \
89 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
90#define tg3_flag_set(tp, flag) \
91 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
92#define tg3_flag_clear(tp, flag) \
93 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000096#define TG3_MAJ_NUM 3
Michael Chand8871992013-02-14 12:13:42 +000097#define TG3_MIN_NUM 130
Matt Carlson6867c842010-07-11 09:31:44 +000098#define DRV_MODULE_VERSION \
99 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Michael Chand8871992013-02-14 12:13:42 +0000100#define DRV_MODULE_RELDATE "February 14, 2013"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000102#define RESET_KIND_SHUTDOWN 0
103#define RESET_KIND_INIT 1
104#define RESET_KIND_SUSPEND 2
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_MODE 0
107#define TG3_DEF_TX_MODE 0
108#define TG3_DEF_MSG_ENABLE \
109 (NETIF_MSG_DRV | \
110 NETIF_MSG_PROBE | \
111 NETIF_MSG_LINK | \
112 NETIF_MSG_TIMER | \
113 NETIF_MSG_IFDOWN | \
114 NETIF_MSG_IFUP | \
115 NETIF_MSG_RX_ERR | \
116 NETIF_MSG_TX_ERR)
117
Matt Carlson520b2752011-06-13 13:39:02 +0000118#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* length of time before we decide the hardware is borked,
121 * and dev->tx_timeout() should be called to fix the problem
122 */
Joe Perches63c3a662011-04-26 08:12:10 +0000123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TG3_TX_TIMEOUT (5 * HZ)
125
126/* hardware minimum and maximum for a single frame's data payload */
127#define TG3_MIN_MTU 60
128#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000129 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/* These numbers seem to be hard coded in the NIC firmware somehow.
132 * You can't change the ring sizes, but you can change where you place
133 * them in the NIC onboard memory.
134 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000135#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000136 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000137 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000139#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000140 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000141 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define TG3_DEF_RX_JUMBO_RING_PENDING 100
143
144/* Do not place this n-ring entries value into the tp struct itself,
145 * we really want to expose these constants to GCC so that modulo et
146 * al. operations are done with shifts and masks instead of with
147 * hw multiply/modulo instructions. Another solution would be to
148 * replace things like '% foo' with '& (foo - 1)'.
149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define TG3_TX_RING_SIZE 512
152#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
153
Matt Carlson2c49a442010-09-30 10:34:35 +0000154#define TG3_RX_STD_RING_BYTES(tp) \
155 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
156#define TG3_RX_JMB_RING_BYTES(tp) \
157 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
158#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000159 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
161 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
163
Matt Carlson287be122009-08-28 13:58:46 +0000164#define TG3_DMA_BYTE_ENAB 64
165
166#define TG3_RX_STD_DMA_SZ 1536
167#define TG3_RX_JMB_DMA_SZ 9046
168
169#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
170
171#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
172#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Matt Carlson2c49a442010-09-30 10:34:35 +0000174#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
175 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000176
Matt Carlson2c49a442010-09-30 10:34:35 +0000177#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
178 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000179
Matt Carlsond2757fc2010-04-12 06:58:27 +0000180/* Due to a hardware bug, the 5701 can only DMA to memory addresses
181 * that are at least dword aligned when used in PCIX mode. The driver
182 * works around this bug by double copying the packet. This workaround
183 * is built into the normal double copy length check for efficiency.
184 *
185 * However, the double copy is only necessary on those architectures
186 * where unaligned memory accesses are inefficient. For those architectures
187 * where unaligned memory accesses incur little penalty, we can reintegrate
188 * the 5701 in the normal rx path. Doing so saves a device structure
189 * dereference by hardcoding the double copy threshold in place.
190 */
191#define TG3_RX_COPY_THRESHOLD 256
192#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
193 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
194#else
195 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
196#endif
197
Matt Carlson81389f52011-08-31 11:44:49 +0000198#if (NET_IP_ALIGN != 0)
199#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
200#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000201#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000202#endif
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000205#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000206#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000207#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Matt Carlsonad829262008-11-21 17:16:16 -0800209#define TG3_RAW_IP_ALIGN 2
210
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000211#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000212#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000213
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800214#define FIRMWARE_TG3 "tigon/tg3.bin"
215#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
216#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
217
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500218static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000219 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
222MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
223MODULE_LICENSE("GPL");
224MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800225MODULE_FIRMWARE(FIRMWARE_TG3);
226MODULE_FIRMWARE(FIRMWARE_TG3TSO);
227MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
230module_param(tg3_debug, int, 0);
231MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
232
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000233#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
234#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
235
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000236static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
256 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
257 TG3_DRV_DATA_FLAG_5705_10_100},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
259 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
260 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
263 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
264 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
271 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
277 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000285 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
286 PCI_VENDOR_ID_LENOVO,
287 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
288 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
291 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000310 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
311 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
312 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
313 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
314 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
315 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800316 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
317 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000318 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
319 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000320 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
329 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
330 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
331 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000332 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000333 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000335 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000336 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
338 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700339 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
340 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
341 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
342 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
343 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
344 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
345 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000346 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700347 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348};
349
350MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
351
Andreas Mohr50da8592006-08-14 23:54:30 -0700352static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000354} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 { "rx_octets" },
356 { "rx_fragments" },
357 { "rx_ucast_packets" },
358 { "rx_mcast_packets" },
359 { "rx_bcast_packets" },
360 { "rx_fcs_errors" },
361 { "rx_align_errors" },
362 { "rx_xon_pause_rcvd" },
363 { "rx_xoff_pause_rcvd" },
364 { "rx_mac_ctrl_rcvd" },
365 { "rx_xoff_entered" },
366 { "rx_frame_too_long_errors" },
367 { "rx_jabbers" },
368 { "rx_undersize_packets" },
369 { "rx_in_length_errors" },
370 { "rx_out_length_errors" },
371 { "rx_64_or_less_octet_packets" },
372 { "rx_65_to_127_octet_packets" },
373 { "rx_128_to_255_octet_packets" },
374 { "rx_256_to_511_octet_packets" },
375 { "rx_512_to_1023_octet_packets" },
376 { "rx_1024_to_1522_octet_packets" },
377 { "rx_1523_to_2047_octet_packets" },
378 { "rx_2048_to_4095_octet_packets" },
379 { "rx_4096_to_8191_octet_packets" },
380 { "rx_8192_to_9022_octet_packets" },
381
382 { "tx_octets" },
383 { "tx_collisions" },
384
385 { "tx_xon_sent" },
386 { "tx_xoff_sent" },
387 { "tx_flow_control" },
388 { "tx_mac_errors" },
389 { "tx_single_collisions" },
390 { "tx_mult_collisions" },
391 { "tx_deferred" },
392 { "tx_excessive_collisions" },
393 { "tx_late_collisions" },
394 { "tx_collide_2times" },
395 { "tx_collide_3times" },
396 { "tx_collide_4times" },
397 { "tx_collide_5times" },
398 { "tx_collide_6times" },
399 { "tx_collide_7times" },
400 { "tx_collide_8times" },
401 { "tx_collide_9times" },
402 { "tx_collide_10times" },
403 { "tx_collide_11times" },
404 { "tx_collide_12times" },
405 { "tx_collide_13times" },
406 { "tx_collide_14times" },
407 { "tx_collide_15times" },
408 { "tx_ucast_packets" },
409 { "tx_mcast_packets" },
410 { "tx_bcast_packets" },
411 { "tx_carrier_sense_errors" },
412 { "tx_discards" },
413 { "tx_errors" },
414
415 { "dma_writeq_full" },
416 { "dma_write_prioq_full" },
417 { "rxbds_empty" },
418 { "rx_discards" },
419 { "rx_errors" },
420 { "rx_threshold_hit" },
421
422 { "dma_readq_full" },
423 { "dma_read_prioq_full" },
424 { "tx_comp_queue_full" },
425
426 { "ring_set_send_prod_index" },
427 { "ring_status_update" },
428 { "nic_irqs" },
429 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000430 { "nic_tx_threshold_hit" },
431
432 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433};
434
Matt Carlson48fa55a2011-04-13 11:05:06 +0000435#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000436#define TG3_NVRAM_TEST 0
437#define TG3_LINK_TEST 1
438#define TG3_REGISTER_TEST 2
439#define TG3_MEMORY_TEST 3
440#define TG3_MAC_LOOPB_TEST 4
441#define TG3_PHY_LOOPB_TEST 5
442#define TG3_EXT_LOOPB_TEST 6
443#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000444
445
Andreas Mohr50da8592006-08-14 23:54:30 -0700446static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700447 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000448} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000449 [TG3_NVRAM_TEST] = { "nvram test (online) " },
450 [TG3_LINK_TEST] = { "link test (online) " },
451 [TG3_REGISTER_TEST] = { "register test (offline)" },
452 [TG3_MEMORY_TEST] = { "memory test (offline)" },
453 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
454 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
455 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
456 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700457};
458
Matt Carlson48fa55a2011-04-13 11:05:06 +0000459#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
460
461
Michael Chanb401e9e2005-12-19 16:27:04 -0800462static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
463{
464 writel(val, tp->regs + off);
465}
466
467static u32 tg3_read32(struct tg3 *tp, u32 off)
468{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000469 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800470}
471
Matt Carlson0d3031d2007-10-10 18:02:43 -0700472static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
473{
474 writel(val, tp->aperegs + off);
475}
476
477static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
478{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000479 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
483{
Michael Chan68929142005-08-09 20:17:14 -0700484 unsigned long flags;
485
486 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700487 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
488 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700489 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700490}
491
492static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
493{
494 writel(val, tp->regs + off);
495 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Michael Chan68929142005-08-09 20:17:14 -0700498static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
499{
500 unsigned long flags;
501 u32 val;
502
503 spin_lock_irqsave(&tp->indirect_lock, flags);
504 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
505 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
506 spin_unlock_irqrestore(&tp->indirect_lock, flags);
507 return val;
508}
509
510static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
511{
512 unsigned long flags;
513
514 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
515 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
516 TG3_64BIT_REG_LOW, val);
517 return;
518 }
Matt Carlson66711e62009-11-13 13:03:49 +0000519 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700520 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
521 TG3_64BIT_REG_LOW, val);
522 return;
523 }
524
525 spin_lock_irqsave(&tp->indirect_lock, flags);
526 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
527 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
528 spin_unlock_irqrestore(&tp->indirect_lock, flags);
529
530 /* In indirect mode when disabling interrupts, we also need
531 * to clear the interrupt bit in the GRC local ctrl register.
532 */
533 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
534 (val == 0x1)) {
535 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
536 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
537 }
538}
539
540static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
541{
542 unsigned long flags;
543 u32 val;
544
545 spin_lock_irqsave(&tp->indirect_lock, flags);
546 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
547 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
548 spin_unlock_irqrestore(&tp->indirect_lock, flags);
549 return val;
550}
551
Michael Chanb401e9e2005-12-19 16:27:04 -0800552/* usec_wait specifies the wait time in usec when writing to certain registers
553 * where it is unsafe to read back the register without some delay.
554 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
555 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
556 */
557static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Joe Perches63c3a662011-04-26 08:12:10 +0000559 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800560 /* Non-posted methods */
561 tp->write32(tp, off, val);
562 else {
563 /* Posted method */
564 tg3_write32(tp, off, val);
565 if (usec_wait)
566 udelay(usec_wait);
567 tp->read32(tp, off);
568 }
569 /* Wait again after the read for the posted method to guarantee that
570 * the wait time is met.
571 */
572 if (usec_wait)
573 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Michael Chan09ee9292005-08-09 20:17:00 -0700576static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
577{
578 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000579 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
580 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
581 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700582 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700583}
584
Michael Chan20094932005-08-09 20:16:32 -0700585static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 void __iomem *mbox = tp->regs + off;
588 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000589 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000591 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
592 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 readl(mbox);
594}
595
Michael Chanb5d37722006-09-27 16:06:21 -0700596static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
597{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000598 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700599}
600
601static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
602{
603 writel(val, tp->regs + off + GRCMBOX_BASE);
604}
605
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000606#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700607#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000608#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
609#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
610#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700611
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000612#define tw32(reg, val) tp->write32(tp, reg, val)
613#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
614#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
615#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
618{
Michael Chan68929142005-08-09 20:17:14 -0700619 unsigned long flags;
620
Joe Perches41535772013-02-16 11:20:04 +0000621 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700622 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
623 return;
624
Michael Chan68929142005-08-09 20:17:14 -0700625 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000626 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700627 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
628 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Michael Chanbbadf502006-04-06 21:46:34 -0700630 /* Always leave this as zero. */
631 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
632 } else {
633 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
634 tw32_f(TG3PCI_MEM_WIN_DATA, val);
635
636 /* Always leave this as zero. */
637 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
638 }
Michael Chan68929142005-08-09 20:17:14 -0700639 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
643{
Michael Chan68929142005-08-09 20:17:14 -0700644 unsigned long flags;
645
Joe Perches41535772013-02-16 11:20:04 +0000646 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700647 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
648 *val = 0;
649 return;
650 }
651
Michael Chan68929142005-08-09 20:17:14 -0700652 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000653 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700654 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
655 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Michael Chanbbadf502006-04-06 21:46:34 -0700657 /* Always leave this as zero. */
658 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
659 } else {
660 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
661 *val = tr32(TG3PCI_MEM_WIN_DATA);
662
663 /* Always leave this as zero. */
664 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
665 }
Michael Chan68929142005-08-09 20:17:14 -0700666 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Matt Carlson0d3031d2007-10-10 18:02:43 -0700669static void tg3_ape_lock_init(struct tg3 *tp)
670{
671 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000672 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000673
Joe Perches41535772013-02-16 11:20:04 +0000674 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000675 regbase = TG3_APE_LOCK_GRANT;
676 else
677 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700678
679 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000680 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
681 switch (i) {
682 case TG3_APE_LOCK_PHY0:
683 case TG3_APE_LOCK_PHY1:
684 case TG3_APE_LOCK_PHY2:
685 case TG3_APE_LOCK_PHY3:
686 bit = APE_LOCK_GRANT_DRIVER;
687 break;
688 default:
689 if (!tp->pci_fn)
690 bit = APE_LOCK_GRANT_DRIVER;
691 else
692 bit = 1 << tp->pci_fn;
693 }
694 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000695 }
696
Matt Carlson0d3031d2007-10-10 18:02:43 -0700697}
698
699static int tg3_ape_lock(struct tg3 *tp, int locknum)
700{
701 int i, off;
702 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000703 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700704
Joe Perches63c3a662011-04-26 08:12:10 +0000705 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706 return 0;
707
708 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000709 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000710 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000711 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000712 case TG3_APE_LOCK_GRC:
713 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000714 if (!tp->pci_fn)
715 bit = APE_LOCK_REQ_DRIVER;
716 else
717 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000718 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000719 case TG3_APE_LOCK_PHY0:
720 case TG3_APE_LOCK_PHY1:
721 case TG3_APE_LOCK_PHY2:
722 case TG3_APE_LOCK_PHY3:
723 bit = APE_LOCK_REQ_DRIVER;
724 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000725 default:
726 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700727 }
728
Joe Perches41535772013-02-16 11:20:04 +0000729 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000730 req = TG3_APE_LOCK_REQ;
731 gnt = TG3_APE_LOCK_GRANT;
732 } else {
733 req = TG3_APE_PER_LOCK_REQ;
734 gnt = TG3_APE_PER_LOCK_GRANT;
735 }
736
Matt Carlson0d3031d2007-10-10 18:02:43 -0700737 off = 4 * locknum;
738
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000739 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700740
741 /* Wait for up to 1 millisecond to acquire lock. */
742 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000743 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000744 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700745 break;
746 udelay(10);
747 }
748
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000749 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700750 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000751 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700752 ret = -EBUSY;
753 }
754
755 return ret;
756}
757
758static void tg3_ape_unlock(struct tg3 *tp, int locknum)
759{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000760 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700761
Joe Perches63c3a662011-04-26 08:12:10 +0000762 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700763 return;
764
765 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000766 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000767 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000768 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000769 case TG3_APE_LOCK_GRC:
770 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000771 if (!tp->pci_fn)
772 bit = APE_LOCK_GRANT_DRIVER;
773 else
774 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000775 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000776 case TG3_APE_LOCK_PHY0:
777 case TG3_APE_LOCK_PHY1:
778 case TG3_APE_LOCK_PHY2:
779 case TG3_APE_LOCK_PHY3:
780 bit = APE_LOCK_GRANT_DRIVER;
781 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000782 default:
783 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700784 }
785
Joe Perches41535772013-02-16 11:20:04 +0000786 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000787 gnt = TG3_APE_LOCK_GRANT;
788 else
789 gnt = TG3_APE_PER_LOCK_GRANT;
790
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000791 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700792}
793
Matt Carlsonb65a3722012-07-16 16:24:00 +0000794static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000795{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000796 u32 apedata;
797
Matt Carlsonb65a3722012-07-16 16:24:00 +0000798 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000799 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000800 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000801
802 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000803 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
804 break;
805
Matt Carlsonb65a3722012-07-16 16:24:00 +0000806 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
807
808 udelay(10);
809 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000810 }
811
Matt Carlsonb65a3722012-07-16 16:24:00 +0000812 return timeout_us ? 0 : -EBUSY;
813}
814
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000815static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
816{
817 u32 i, apedata;
818
819 for (i = 0; i < timeout_us / 10; i++) {
820 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
821
822 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
823 break;
824
825 udelay(10);
826 }
827
828 return i == timeout_us / 10;
829}
830
Michael Chan86449942012-10-02 20:31:14 -0700831static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
832 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000833{
834 int err;
835 u32 i, bufoff, msgoff, maxlen, apedata;
836
837 if (!tg3_flag(tp, APE_HAS_NCSI))
838 return 0;
839
840 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
841 if (apedata != APE_SEG_SIG_MAGIC)
842 return -ENODEV;
843
844 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
845 if (!(apedata & APE_FW_STATUS_READY))
846 return -EAGAIN;
847
848 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
849 TG3_APE_SHMEM_BASE;
850 msgoff = bufoff + 2 * sizeof(u32);
851 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
852
853 while (len) {
854 u32 length;
855
856 /* Cap xfer sizes to scratchpad limits. */
857 length = (len > maxlen) ? maxlen : len;
858 len -= length;
859
860 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
861 if (!(apedata & APE_FW_STATUS_READY))
862 return -EAGAIN;
863
864 /* Wait for up to 1 msec for APE to service previous event. */
865 err = tg3_ape_event_lock(tp, 1000);
866 if (err)
867 return err;
868
869 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
870 APE_EVENT_STATUS_SCRTCHPD_READ |
871 APE_EVENT_STATUS_EVENT_PENDING;
872 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
873
874 tg3_ape_write32(tp, bufoff, base_off);
875 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
876
877 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
878 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
879
880 base_off += length;
881
882 if (tg3_ape_wait_for_event(tp, 30000))
883 return -EAGAIN;
884
885 for (i = 0; length; i += 4, length -= 4) {
886 u32 val = tg3_ape_read32(tp, msgoff + i);
887 memcpy(data, &val, sizeof(u32));
888 data++;
889 }
890 }
891
892 return 0;
893}
894
Matt Carlsonb65a3722012-07-16 16:24:00 +0000895static int tg3_ape_send_event(struct tg3 *tp, u32 event)
896{
897 int err;
898 u32 apedata;
899
900 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
901 if (apedata != APE_SEG_SIG_MAGIC)
902 return -EAGAIN;
903
904 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
905 if (!(apedata & APE_FW_STATUS_READY))
906 return -EAGAIN;
907
908 /* Wait for up to 1 millisecond for APE to service previous event. */
909 err = tg3_ape_event_lock(tp, 1000);
910 if (err)
911 return err;
912
913 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
914 event | APE_EVENT_STATUS_EVENT_PENDING);
915
916 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
917 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
918
919 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000920}
921
922static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
923{
924 u32 event;
925 u32 apedata;
926
927 if (!tg3_flag(tp, ENABLE_APE))
928 return;
929
930 switch (kind) {
931 case RESET_KIND_INIT:
932 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
933 APE_HOST_SEG_SIG_MAGIC);
934 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
935 APE_HOST_SEG_LEN_MAGIC);
936 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
937 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
938 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
939 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
940 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
941 APE_HOST_BEHAV_NO_PHYLOCK);
942 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
943 TG3_APE_HOST_DRVR_STATE_START);
944
945 event = APE_EVENT_STATUS_STATE_START;
946 break;
947 case RESET_KIND_SHUTDOWN:
948 /* With the interface we are currently using,
949 * APE does not track driver state. Wiping
950 * out the HOST SEGMENT SIGNATURE forces
951 * the APE to assume OS absent status.
952 */
953 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
954
955 if (device_may_wakeup(&tp->pdev->dev) &&
956 tg3_flag(tp, WOL_ENABLE)) {
957 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
958 TG3_APE_HOST_WOL_SPEED_AUTO);
959 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
960 } else
961 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
962
963 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
964
965 event = APE_EVENT_STATUS_STATE_UNLOAD;
966 break;
967 case RESET_KIND_SUSPEND:
968 event = APE_EVENT_STATUS_STATE_SUSPEND;
969 break;
970 default:
971 return;
972 }
973
974 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
975
976 tg3_ape_send_event(tp, event);
977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979static void tg3_disable_ints(struct tg3 *tp)
980{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000981 int i;
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 tw32(TG3PCI_MISC_HOST_CTRL,
984 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000985 for (i = 0; i < tp->irq_max; i++)
986 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989static void tg3_enable_ints(struct tg3 *tp)
990{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000991 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000992
Michael Chanbbe832c2005-06-24 20:20:04 -0700993 tp->irq_sync = 0;
994 wmb();
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 tw32(TG3PCI_MISC_HOST_CTRL,
997 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000998
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000999 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001000 for (i = 0; i < tp->irq_cnt; i++) {
1001 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001002
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001003 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001004 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001005 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1006
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001007 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001008 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001009
1010 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001011 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001012 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1013 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1014 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001015 tw32(HOSTCC_MODE, tp->coal_now);
1016
1017 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Matt Carlson17375d22009-08-28 14:02:18 +00001020static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001021{
Matt Carlson17375d22009-08-28 14:02:18 +00001022 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001023 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001024 unsigned int work_exists = 0;
1025
1026 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001027 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001028 if (sblk->status & SD_STATUS_LINK_CHG)
1029 work_exists = 1;
1030 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001031
1032 /* check for TX work to do */
1033 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1034 work_exists = 1;
1035
1036 /* check for RX work to do */
1037 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001038 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001039 work_exists = 1;
1040
1041 return work_exists;
1042}
1043
Matt Carlson17375d22009-08-28 14:02:18 +00001044/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001045 * similar to tg3_enable_ints, but it accurately determines whether there
1046 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001047 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
Matt Carlson17375d22009-08-28 14:02:18 +00001049static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Matt Carlson17375d22009-08-28 14:02:18 +00001051 struct tg3 *tp = tnapi->tp;
1052
Matt Carlson898a56f2009-08-28 14:02:40 +00001053 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 mmiowb();
1055
David S. Millerfac9b832005-05-18 22:46:34 -07001056 /* When doing tagged status, this work check is unnecessary.
1057 * The last_tag we write above tells the chip which piece of
1058 * work we've completed.
1059 */
Joe Perches63c3a662011-04-26 08:12:10 +00001060 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001061 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001062 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065static void tg3_switch_clocks(struct tg3 *tp)
1066{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001067 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 u32 orig_clock_ctrl;
1069
Joe Perches63c3a662011-04-26 08:12:10 +00001070 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001071 return;
1072
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001073 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 orig_clock_ctrl = clock_ctrl;
1076 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1077 CLOCK_CTRL_CLKRUN_OENABLE |
1078 0x1f);
1079 tp->pci_clock_ctrl = clock_ctrl;
1080
Joe Perches63c3a662011-04-26 08:12:10 +00001081 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001083 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1084 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001087 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1088 clock_ctrl |
1089 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1090 40);
1091 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1092 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1093 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001095 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
1098#define PHY_BUSY_LOOPS 5000
1099
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001100static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1101 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 u32 frame_val;
1104 unsigned int loops;
1105 int ret;
1106
1107 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1108 tw32_f(MAC_MI_MODE,
1109 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1110 udelay(80);
1111 }
1112
Michael Chan8151ad52012-07-29 19:15:41 +00001113 tg3_ape_lock(tp, tp->phy_ape_lock);
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 *val = 0x0;
1116
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001117 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 MI_COM_PHY_ADDR_MASK);
1119 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1120 MI_COM_REG_ADDR_MASK);
1121 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 tw32_f(MAC_MI_COM, frame_val);
1124
1125 loops = PHY_BUSY_LOOPS;
1126 while (loops != 0) {
1127 udelay(10);
1128 frame_val = tr32(MAC_MI_COM);
1129
1130 if ((frame_val & MI_COM_BUSY) == 0) {
1131 udelay(5);
1132 frame_val = tr32(MAC_MI_COM);
1133 break;
1134 }
1135 loops -= 1;
1136 }
1137
1138 ret = -EBUSY;
1139 if (loops != 0) {
1140 *val = frame_val & MI_COM_DATA_MASK;
1141 ret = 0;
1142 }
1143
1144 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1145 tw32_f(MAC_MI_MODE, tp->mi_mode);
1146 udelay(80);
1147 }
1148
Michael Chan8151ad52012-07-29 19:15:41 +00001149 tg3_ape_unlock(tp, tp->phy_ape_lock);
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return ret;
1152}
1153
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001154static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1155{
1156 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1157}
1158
1159static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1160 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 u32 frame_val;
1163 unsigned int loops;
1164 int ret;
1165
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001166 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001167 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001168 return 0;
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1171 tw32_f(MAC_MI_MODE,
1172 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1173 udelay(80);
1174 }
1175
Michael Chan8151ad52012-07-29 19:15:41 +00001176 tg3_ape_lock(tp, tp->phy_ape_lock);
1177
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001178 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 MI_COM_PHY_ADDR_MASK);
1180 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1181 MI_COM_REG_ADDR_MASK);
1182 frame_val |= (val & MI_COM_DATA_MASK);
1183 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 tw32_f(MAC_MI_COM, frame_val);
1186
1187 loops = PHY_BUSY_LOOPS;
1188 while (loops != 0) {
1189 udelay(10);
1190 frame_val = tr32(MAC_MI_COM);
1191 if ((frame_val & MI_COM_BUSY) == 0) {
1192 udelay(5);
1193 frame_val = tr32(MAC_MI_COM);
1194 break;
1195 }
1196 loops -= 1;
1197 }
1198
1199 ret = -EBUSY;
1200 if (loops != 0)
1201 ret = 0;
1202
1203 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1204 tw32_f(MAC_MI_MODE, tp->mi_mode);
1205 udelay(80);
1206 }
1207
Michael Chan8151ad52012-07-29 19:15:41 +00001208 tg3_ape_unlock(tp, tp->phy_ape_lock);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return ret;
1211}
1212
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001213static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1214{
1215 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1216}
1217
Matt Carlsonb0988c12011-04-20 07:57:39 +00001218static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1219{
1220 int err;
1221
1222 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1223 if (err)
1224 goto done;
1225
1226 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1227 if (err)
1228 goto done;
1229
1230 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1231 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1232 if (err)
1233 goto done;
1234
1235 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1236
1237done:
1238 return err;
1239}
1240
1241static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1242{
1243 int err;
1244
1245 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1246 if (err)
1247 goto done;
1248
1249 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1250 if (err)
1251 goto done;
1252
1253 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1254 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1255 if (err)
1256 goto done;
1257
1258 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1259
1260done:
1261 return err;
1262}
1263
1264static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1265{
1266 int err;
1267
1268 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1269 if (!err)
1270 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1271
1272 return err;
1273}
1274
1275static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1276{
1277 int err;
1278
1279 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1280 if (!err)
1281 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1282
1283 return err;
1284}
1285
Matt Carlson15ee95c2011-04-20 07:57:40 +00001286static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1287{
1288 int err;
1289
1290 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1291 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1292 MII_TG3_AUXCTL_SHDWSEL_MISC);
1293 if (!err)
1294 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1295
1296 return err;
1297}
1298
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001299static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1300{
1301 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1302 set |= MII_TG3_AUXCTL_MISC_WREN;
1303
1304 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1305}
1306
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001307static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1308{
1309 u32 val;
1310 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001311
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001312 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1313
1314 if (err)
1315 return err;
1316 if (enable)
1317
1318 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1319 else
1320 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1321
1322 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1323 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1324
1325 return err;
1326}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001327
Matt Carlson95e28692008-05-25 23:44:14 -07001328static int tg3_bmcr_reset(struct tg3 *tp)
1329{
1330 u32 phy_control;
1331 int limit, err;
1332
1333 /* OK, reset it, and poll the BMCR_RESET bit until it
1334 * clears or we time out.
1335 */
1336 phy_control = BMCR_RESET;
1337 err = tg3_writephy(tp, MII_BMCR, phy_control);
1338 if (err != 0)
1339 return -EBUSY;
1340
1341 limit = 5000;
1342 while (limit--) {
1343 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1344 if (err != 0)
1345 return -EBUSY;
1346
1347 if ((phy_control & BMCR_RESET) == 0) {
1348 udelay(40);
1349 break;
1350 }
1351 udelay(10);
1352 }
Roel Kluind4675b52009-02-12 16:33:27 -08001353 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001354 return -EBUSY;
1355
1356 return 0;
1357}
1358
Matt Carlson158d7ab2008-05-29 01:37:54 -07001359static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1360{
Francois Romieu3d165432009-01-19 16:56:50 -08001361 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001362 u32 val;
1363
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001364 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001365
1366 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001367 val = -EIO;
1368
1369 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001370
1371 return val;
1372}
1373
1374static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1375{
Francois Romieu3d165432009-01-19 16:56:50 -08001376 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001377 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001378
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001379 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001380
1381 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001382 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001383
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001384 spin_unlock_bh(&tp->lock);
1385
1386 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001387}
1388
1389static int tg3_mdio_reset(struct mii_bus *bp)
1390{
1391 return 0;
1392}
1393
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001394static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001395{
1396 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001397 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001398
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001399 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001400 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001401 case PHY_ID_BCM50610:
1402 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001403 val = MAC_PHYCFG2_50610_LED_MODES;
1404 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001405 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001406 val = MAC_PHYCFG2_AC131_LED_MODES;
1407 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001408 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001409 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1410 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001411 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001412 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1413 break;
1414 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001415 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001416 }
1417
1418 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1419 tw32(MAC_PHYCFG2, val);
1420
1421 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001422 val &= ~(MAC_PHYCFG1_RGMII_INT |
1423 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1424 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001425 tw32(MAC_PHYCFG1, val);
1426
1427 return;
1428 }
1429
Joe Perches63c3a662011-04-26 08:12:10 +00001430 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001431 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1432 MAC_PHYCFG2_FMODE_MASK_MASK |
1433 MAC_PHYCFG2_GMODE_MASK_MASK |
1434 MAC_PHYCFG2_ACT_MASK_MASK |
1435 MAC_PHYCFG2_QUAL_MASK_MASK |
1436 MAC_PHYCFG2_INBAND_ENABLE;
1437
1438 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001439
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001440 val = tr32(MAC_PHYCFG1);
1441 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1442 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001443 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1444 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001445 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001446 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001447 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1448 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001449 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1450 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1451 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001452
Matt Carlsona9daf362008-05-25 23:49:44 -07001453 val = tr32(MAC_EXT_RGMII_MODE);
1454 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1455 MAC_RGMII_MODE_RX_QUALITY |
1456 MAC_RGMII_MODE_RX_ACTIVITY |
1457 MAC_RGMII_MODE_RX_ENG_DET |
1458 MAC_RGMII_MODE_TX_ENABLE |
1459 MAC_RGMII_MODE_TX_LOWPWR |
1460 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001461 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1462 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001463 val |= MAC_RGMII_MODE_RX_INT_B |
1464 MAC_RGMII_MODE_RX_QUALITY |
1465 MAC_RGMII_MODE_RX_ACTIVITY |
1466 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001467 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001468 val |= MAC_RGMII_MODE_TX_ENABLE |
1469 MAC_RGMII_MODE_TX_LOWPWR |
1470 MAC_RGMII_MODE_TX_RESET;
1471 }
1472 tw32(MAC_EXT_RGMII_MODE, val);
1473}
1474
Matt Carlson158d7ab2008-05-29 01:37:54 -07001475static void tg3_mdio_start(struct tg3 *tp)
1476{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001477 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1478 tw32_f(MAC_MI_MODE, tp->mi_mode);
1479 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001480
Joe Perches63c3a662011-04-26 08:12:10 +00001481 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001482 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001483 tg3_mdio_config_5785(tp);
1484}
1485
1486static int tg3_mdio_init(struct tg3 *tp)
1487{
1488 int i;
1489 u32 reg;
1490 struct phy_device *phydev;
1491
Joe Perches63c3a662011-04-26 08:12:10 +00001492 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001493 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001494
Matt Carlson69f11c92011-07-13 09:27:30 +00001495 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001496
Joe Perches41535772013-02-16 11:20:04 +00001497 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001498 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1499 else
1500 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1501 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001502 if (is_serdes)
1503 tp->phy_addr += 7;
1504 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001505 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001506
Matt Carlson158d7ab2008-05-29 01:37:54 -07001507 tg3_mdio_start(tp);
1508
Joe Perches63c3a662011-04-26 08:12:10 +00001509 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001510 return 0;
1511
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001512 tp->mdio_bus = mdiobus_alloc();
1513 if (tp->mdio_bus == NULL)
1514 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001515
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001516 tp->mdio_bus->name = "tg3 mdio bus";
1517 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001518 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001519 tp->mdio_bus->priv = tp;
1520 tp->mdio_bus->parent = &tp->pdev->dev;
1521 tp->mdio_bus->read = &tg3_mdio_read;
1522 tp->mdio_bus->write = &tg3_mdio_write;
1523 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001524 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001525 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001526
1527 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001528 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001529
1530 /* The bus registration will look for all the PHYs on the mdio bus.
1531 * Unfortunately, it does not ensure the PHY is powered up before
1532 * accessing the PHY ID registers. A chip reset is the
1533 * quickest way to bring the device back to an operational state..
1534 */
1535 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1536 tg3_bmcr_reset(tp);
1537
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001538 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001539 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001540 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001541 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001542 return i;
1543 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001544
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001545 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001546
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001547 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001548 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001549 mdiobus_unregister(tp->mdio_bus);
1550 mdiobus_free(tp->mdio_bus);
1551 return -ENODEV;
1552 }
1553
1554 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001555 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001556 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001557 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001558 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001559 case PHY_ID_BCM50610:
1560 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001561 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001562 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001563 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001564 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001565 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001566 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001567 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001568 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001569 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001570 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001571 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001572 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001573 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001574 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001575 case PHY_ID_RTL8201E:
1576 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001577 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001578 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001579 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001580 break;
1581 }
1582
Joe Perches63c3a662011-04-26 08:12:10 +00001583 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001584
Joe Perches41535772013-02-16 11:20:04 +00001585 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001586 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001587
1588 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001589}
1590
1591static void tg3_mdio_fini(struct tg3 *tp)
1592{
Joe Perches63c3a662011-04-26 08:12:10 +00001593 if (tg3_flag(tp, MDIOBUS_INITED)) {
1594 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001595 mdiobus_unregister(tp->mdio_bus);
1596 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001597 }
1598}
1599
Matt Carlson95e28692008-05-25 23:44:14 -07001600/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001601static inline void tg3_generate_fw_event(struct tg3 *tp)
1602{
1603 u32 val;
1604
1605 val = tr32(GRC_RX_CPU_EVENT);
1606 val |= GRC_RX_CPU_DRIVER_EVENT;
1607 tw32_f(GRC_RX_CPU_EVENT, val);
1608
1609 tp->last_event_jiffies = jiffies;
1610}
1611
1612#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1613
1614/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001615static void tg3_wait_for_event_ack(struct tg3 *tp)
1616{
1617 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001618 unsigned int delay_cnt;
1619 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001620
Matt Carlson4ba526c2008-08-15 14:10:04 -07001621 /* If enough time has passed, no wait is necessary. */
1622 time_remain = (long)(tp->last_event_jiffies + 1 +
1623 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1624 (long)jiffies;
1625 if (time_remain < 0)
1626 return;
1627
1628 /* Check if we can shorten the wait time. */
1629 delay_cnt = jiffies_to_usecs(time_remain);
1630 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1631 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1632 delay_cnt = (delay_cnt >> 3) + 1;
1633
1634 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001635 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1636 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001637 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001638 }
1639}
1640
1641/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001642static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001643{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001644 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001645
1646 val = 0;
1647 if (!tg3_readphy(tp, MII_BMCR, &reg))
1648 val = reg << 16;
1649 if (!tg3_readphy(tp, MII_BMSR, &reg))
1650 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001651 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001652
1653 val = 0;
1654 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1655 val = reg << 16;
1656 if (!tg3_readphy(tp, MII_LPA, &reg))
1657 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001658 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001659
1660 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001661 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001662 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1663 val = reg << 16;
1664 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1665 val |= (reg & 0xffff);
1666 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001667 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001668
1669 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1670 val = reg << 16;
1671 else
1672 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001673 *data++ = val;
1674}
1675
1676/* tp->lock is held. */
1677static void tg3_ump_link_report(struct tg3 *tp)
1678{
1679 u32 data[4];
1680
1681 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1682 return;
1683
1684 tg3_phy_gather_ump_data(tp, data);
1685
1686 tg3_wait_for_event_ack(tp);
1687
1688 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1689 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1690 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1691 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1692 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1693 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001694
Matt Carlson4ba526c2008-08-15 14:10:04 -07001695 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001696}
1697
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001698/* tp->lock is held. */
1699static void tg3_stop_fw(struct tg3 *tp)
1700{
1701 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1702 /* Wait for RX cpu to ACK the previous event. */
1703 tg3_wait_for_event_ack(tp);
1704
1705 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1706
1707 tg3_generate_fw_event(tp);
1708
1709 /* Wait for RX cpu to ACK this event. */
1710 tg3_wait_for_event_ack(tp);
1711 }
1712}
1713
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001714/* tp->lock is held. */
1715static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1716{
1717 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1718 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1719
1720 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1721 switch (kind) {
1722 case RESET_KIND_INIT:
1723 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1724 DRV_STATE_START);
1725 break;
1726
1727 case RESET_KIND_SHUTDOWN:
1728 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1729 DRV_STATE_UNLOAD);
1730 break;
1731
1732 case RESET_KIND_SUSPEND:
1733 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1734 DRV_STATE_SUSPEND);
1735 break;
1736
1737 default:
1738 break;
1739 }
1740 }
1741
1742 if (kind == RESET_KIND_INIT ||
1743 kind == RESET_KIND_SUSPEND)
1744 tg3_ape_driver_state_change(tp, kind);
1745}
1746
1747/* tp->lock is held. */
1748static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1749{
1750 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1751 switch (kind) {
1752 case RESET_KIND_INIT:
1753 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1754 DRV_STATE_START_DONE);
1755 break;
1756
1757 case RESET_KIND_SHUTDOWN:
1758 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1759 DRV_STATE_UNLOAD_DONE);
1760 break;
1761
1762 default:
1763 break;
1764 }
1765 }
1766
1767 if (kind == RESET_KIND_SHUTDOWN)
1768 tg3_ape_driver_state_change(tp, kind);
1769}
1770
1771/* tp->lock is held. */
1772static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1773{
1774 if (tg3_flag(tp, ENABLE_ASF)) {
1775 switch (kind) {
1776 case RESET_KIND_INIT:
1777 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1778 DRV_STATE_START);
1779 break;
1780
1781 case RESET_KIND_SHUTDOWN:
1782 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1783 DRV_STATE_UNLOAD);
1784 break;
1785
1786 case RESET_KIND_SUSPEND:
1787 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1788 DRV_STATE_SUSPEND);
1789 break;
1790
1791 default:
1792 break;
1793 }
1794 }
1795}
1796
1797static int tg3_poll_fw(struct tg3 *tp)
1798{
1799 int i;
1800 u32 val;
1801
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001802 if (tg3_flag(tp, IS_SSB_CORE)) {
1803 /* We don't use firmware. */
1804 return 0;
1805 }
1806
Joe Perches41535772013-02-16 11:20:04 +00001807 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001808 /* Wait up to 20ms for init done. */
1809 for (i = 0; i < 200; i++) {
1810 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1811 return 0;
1812 udelay(100);
1813 }
1814 return -ENODEV;
1815 }
1816
1817 /* Wait for firmware initialization to complete. */
1818 for (i = 0; i < 100000; i++) {
1819 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1820 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1821 break;
1822 udelay(10);
1823 }
1824
1825 /* Chip might not be fitted with firmware. Some Sun onboard
1826 * parts are configured like that. So don't signal the timeout
1827 * of the above loop as an error, but do report the lack of
1828 * running firmware once.
1829 */
1830 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1831 tg3_flag_set(tp, NO_FWARE_REPORTED);
1832
1833 netdev_info(tp->dev, "No firmware running\n");
1834 }
1835
Joe Perches41535772013-02-16 11:20:04 +00001836 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001837 /* The 57765 A0 needs a little more
1838 * time to do some important work.
1839 */
1840 mdelay(10);
1841 }
1842
1843 return 0;
1844}
1845
Matt Carlson95e28692008-05-25 23:44:14 -07001846static void tg3_link_report(struct tg3 *tp)
1847{
1848 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001849 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001850 tg3_ump_link_report(tp);
1851 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001852 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1853 (tp->link_config.active_speed == SPEED_1000 ?
1854 1000 :
1855 (tp->link_config.active_speed == SPEED_100 ?
1856 100 : 10)),
1857 (tp->link_config.active_duplex == DUPLEX_FULL ?
1858 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001859
Joe Perches05dbe002010-02-17 19:44:19 +00001860 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1861 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1862 "on" : "off",
1863 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1864 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001865
1866 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1867 netdev_info(tp->dev, "EEE is %s\n",
1868 tp->setlpicnt ? "enabled" : "disabled");
1869
Matt Carlson95e28692008-05-25 23:44:14 -07001870 tg3_ump_link_report(tp);
1871 }
1872}
1873
Matt Carlson95e28692008-05-25 23:44:14 -07001874static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1875{
1876 u16 miireg;
1877
Steve Glendinninge18ce342008-12-16 02:00:00 -08001878 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001879 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001880 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001881 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001882 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001883 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1884 else
1885 miireg = 0;
1886
1887 return miireg;
1888}
1889
Matt Carlson95e28692008-05-25 23:44:14 -07001890static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1891{
1892 u8 cap = 0;
1893
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001894 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1895 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1896 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1897 if (lcladv & ADVERTISE_1000XPAUSE)
1898 cap = FLOW_CTRL_RX;
1899 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001900 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001901 }
1902
1903 return cap;
1904}
1905
Matt Carlsonf51f3562008-05-25 23:45:08 -07001906static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001907{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001908 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001909 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001910 u32 old_rx_mode = tp->rx_mode;
1911 u32 old_tx_mode = tp->tx_mode;
1912
Joe Perches63c3a662011-04-26 08:12:10 +00001913 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001914 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001915 else
1916 autoneg = tp->link_config.autoneg;
1917
Joe Perches63c3a662011-04-26 08:12:10 +00001918 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001919 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001920 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001921 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001922 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001923 } else
1924 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001925
Matt Carlsonf51f3562008-05-25 23:45:08 -07001926 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001927
Steve Glendinninge18ce342008-12-16 02:00:00 -08001928 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001929 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1930 else
1931 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1932
Matt Carlsonf51f3562008-05-25 23:45:08 -07001933 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001934 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001935
Steve Glendinninge18ce342008-12-16 02:00:00 -08001936 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001937 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1938 else
1939 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1940
Matt Carlsonf51f3562008-05-25 23:45:08 -07001941 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001942 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001943}
1944
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001945static void tg3_adjust_link(struct net_device *dev)
1946{
1947 u8 oldflowctrl, linkmesg = 0;
1948 u32 mac_mode, lcl_adv, rmt_adv;
1949 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001950 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001951
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001952 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001953
1954 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1955 MAC_MODE_HALF_DUPLEX);
1956
1957 oldflowctrl = tp->link_config.active_flowctrl;
1958
1959 if (phydev->link) {
1960 lcl_adv = 0;
1961 rmt_adv = 0;
1962
1963 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1964 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001965 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00001966 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001967 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001968 else
1969 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001970
1971 if (phydev->duplex == DUPLEX_HALF)
1972 mac_mode |= MAC_MODE_HALF_DUPLEX;
1973 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001974 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001975 tp->link_config.flowctrl);
1976
1977 if (phydev->pause)
1978 rmt_adv = LPA_PAUSE_CAP;
1979 if (phydev->asym_pause)
1980 rmt_adv |= LPA_PAUSE_ASYM;
1981 }
1982
1983 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1984 } else
1985 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1986
1987 if (mac_mode != tp->mac_mode) {
1988 tp->mac_mode = mac_mode;
1989 tw32_f(MAC_MODE, tp->mac_mode);
1990 udelay(40);
1991 }
1992
Joe Perches41535772013-02-16 11:20:04 +00001993 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001994 if (phydev->speed == SPEED_10)
1995 tw32(MAC_MI_STAT,
1996 MAC_MI_STAT_10MBPS_MODE |
1997 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1998 else
1999 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2000 }
2001
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002002 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2003 tw32(MAC_TX_LENGTHS,
2004 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2005 (6 << TX_LENGTHS_IPG_SHIFT) |
2006 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2007 else
2008 tw32(MAC_TX_LENGTHS,
2009 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2010 (6 << TX_LENGTHS_IPG_SHIFT) |
2011 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2012
Matt Carlson34655ad2012-02-22 12:35:18 +00002013 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002014 phydev->speed != tp->link_config.active_speed ||
2015 phydev->duplex != tp->link_config.active_duplex ||
2016 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002017 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002018
Matt Carlson34655ad2012-02-22 12:35:18 +00002019 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002020 tp->link_config.active_speed = phydev->speed;
2021 tp->link_config.active_duplex = phydev->duplex;
2022
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002023 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002024
2025 if (linkmesg)
2026 tg3_link_report(tp);
2027}
2028
2029static int tg3_phy_init(struct tg3 *tp)
2030{
2031 struct phy_device *phydev;
2032
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002033 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002034 return 0;
2035
2036 /* Bring the PHY back to a known state. */
2037 tg3_bmcr_reset(tp);
2038
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002039 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002040
2041 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002042 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2043 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002044 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002045 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002046 return PTR_ERR(phydev);
2047 }
2048
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002049 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002050 switch (phydev->interface) {
2051 case PHY_INTERFACE_MODE_GMII:
2052 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002053 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002054 phydev->supported &= (PHY_GBIT_FEATURES |
2055 SUPPORTED_Pause |
2056 SUPPORTED_Asym_Pause);
2057 break;
2058 }
2059 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002060 case PHY_INTERFACE_MODE_MII:
2061 phydev->supported &= (PHY_BASIC_FEATURES |
2062 SUPPORTED_Pause |
2063 SUPPORTED_Asym_Pause);
2064 break;
2065 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002066 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002067 return -EINVAL;
2068 }
2069
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002070 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002071
2072 phydev->advertising = phydev->supported;
2073
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002074 return 0;
2075}
2076
2077static void tg3_phy_start(struct tg3 *tp)
2078{
2079 struct phy_device *phydev;
2080
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002081 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002082 return;
2083
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002084 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002085
Matt Carlson80096062010-08-02 11:26:06 +00002086 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2087 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002088 phydev->speed = tp->link_config.speed;
2089 phydev->duplex = tp->link_config.duplex;
2090 phydev->autoneg = tp->link_config.autoneg;
2091 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002092 }
2093
2094 phy_start(phydev);
2095
2096 phy_start_aneg(phydev);
2097}
2098
2099static void tg3_phy_stop(struct tg3 *tp)
2100{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002101 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002102 return;
2103
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002104 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002105}
2106
2107static void tg3_phy_fini(struct tg3 *tp)
2108{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002109 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002110 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002111 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002112 }
2113}
2114
Matt Carlson941ec902011-08-19 13:58:23 +00002115static int tg3_phy_set_extloopbk(struct tg3 *tp)
2116{
2117 int err;
2118 u32 val;
2119
2120 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2121 return 0;
2122
2123 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2124 /* Cannot do read-modify-write on 5401 */
2125 err = tg3_phy_auxctl_write(tp,
2126 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2127 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2128 0x4c20);
2129 goto done;
2130 }
2131
2132 err = tg3_phy_auxctl_read(tp,
2133 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2134 if (err)
2135 return err;
2136
2137 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2138 err = tg3_phy_auxctl_write(tp,
2139 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2140
2141done:
2142 return err;
2143}
2144
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002145static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2146{
2147 u32 phytest;
2148
2149 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2150 u32 phy;
2151
2152 tg3_writephy(tp, MII_TG3_FET_TEST,
2153 phytest | MII_TG3_FET_SHADOW_EN);
2154 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2155 if (enable)
2156 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2157 else
2158 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2159 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2160 }
2161 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2162 }
2163}
2164
Matt Carlson6833c042008-11-21 17:18:59 -08002165static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2166{
2167 u32 reg;
2168
Joe Perches63c3a662011-04-26 08:12:10 +00002169 if (!tg3_flag(tp, 5705_PLUS) ||
2170 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002171 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002172 return;
2173
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002174 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002175 tg3_phy_fet_toggle_apd(tp, enable);
2176 return;
2177 }
2178
Matt Carlson6833c042008-11-21 17:18:59 -08002179 reg = MII_TG3_MISC_SHDW_WREN |
2180 MII_TG3_MISC_SHDW_SCR5_SEL |
2181 MII_TG3_MISC_SHDW_SCR5_LPED |
2182 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2183 MII_TG3_MISC_SHDW_SCR5_SDTL |
2184 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002185 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002186 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2187
2188 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2189
2190
2191 reg = MII_TG3_MISC_SHDW_WREN |
2192 MII_TG3_MISC_SHDW_APD_SEL |
2193 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2194 if (enable)
2195 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2196
2197 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2198}
2199
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002200static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2201{
2202 u32 phy;
2203
Joe Perches63c3a662011-04-26 08:12:10 +00002204 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002205 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002206 return;
2207
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002208 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002209 u32 ephy;
2210
Matt Carlson535ef6e2009-08-25 10:09:36 +00002211 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2212 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2213
2214 tg3_writephy(tp, MII_TG3_FET_TEST,
2215 ephy | MII_TG3_FET_SHADOW_EN);
2216 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002217 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002218 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002219 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002220 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2221 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002222 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002223 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002224 }
2225 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002226 int ret;
2227
2228 ret = tg3_phy_auxctl_read(tp,
2229 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2230 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002231 if (enable)
2232 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2233 else
2234 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002235 tg3_phy_auxctl_write(tp,
2236 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002237 }
2238 }
2239}
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241static void tg3_phy_set_wirespeed(struct tg3 *tp)
2242{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002243 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 u32 val;
2245
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002246 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return;
2248
Matt Carlson15ee95c2011-04-20 07:57:40 +00002249 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2250 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002251 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2252 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
2254
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002255static void tg3_phy_apply_otp(struct tg3 *tp)
2256{
2257 u32 otp, phy;
2258
2259 if (!tp->phy_otp)
2260 return;
2261
2262 otp = tp->phy_otp;
2263
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002264 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002265 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002266
2267 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2268 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2269 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2270
2271 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2272 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2273 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2274
2275 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2276 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2277 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2278
2279 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2280 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2281
2282 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2283 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2284
2285 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2286 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2287 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2288
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002289 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002290}
2291
Matt Carlson52b02d02010-10-14 10:37:41 +00002292static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2293{
2294 u32 val;
2295
2296 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2297 return;
2298
2299 tp->setlpicnt = 0;
2300
2301 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2302 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002303 tp->link_config.active_duplex == DUPLEX_FULL &&
2304 (tp->link_config.active_speed == SPEED_100 ||
2305 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002306 u32 eeectl;
2307
2308 if (tp->link_config.active_speed == SPEED_1000)
2309 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2310 else
2311 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2312
2313 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2314
Matt Carlson3110f5f52010-12-06 08:28:50 +00002315 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2316 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002317
Matt Carlsonb0c59432011-05-19 12:12:48 +00002318 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2319 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002320 tp->setlpicnt = 2;
2321 }
2322
2323 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002324 if (current_link_up == 1 &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002325 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002326 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002327 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002328 }
2329
Matt Carlson52b02d02010-10-14 10:37:41 +00002330 val = tr32(TG3_CPMU_EEE_MODE);
2331 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2332 }
2333}
2334
Matt Carlsonb0c59432011-05-19 12:12:48 +00002335static void tg3_phy_eee_enable(struct tg3 *tp)
2336{
2337 u32 val;
2338
2339 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002340 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2341 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002342 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002343 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002344 val = MII_TG3_DSP_TAP26_ALNOKO |
2345 MII_TG3_DSP_TAP26_RMRXSTO;
2346 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002347 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002348 }
2349
2350 val = tr32(TG3_CPMU_EEE_MODE);
2351 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2352}
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354static int tg3_wait_macro_done(struct tg3 *tp)
2355{
2356 int limit = 100;
2357
2358 while (limit--) {
2359 u32 tmp32;
2360
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002361 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if ((tmp32 & 0x1000) == 0)
2363 break;
2364 }
2365 }
Roel Kluind4675b52009-02-12 16:33:27 -08002366 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return -EBUSY;
2368
2369 return 0;
2370}
2371
2372static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2373{
2374 static const u32 test_pat[4][6] = {
2375 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2376 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2377 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2378 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2379 };
2380 int chan;
2381
2382 for (chan = 0; chan < 4; chan++) {
2383 int i;
2384
2385 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2386 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002387 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 for (i = 0; i < 6; i++)
2390 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2391 test_pat[chan][i]);
2392
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002393 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (tg3_wait_macro_done(tp)) {
2395 *resetp = 1;
2396 return -EBUSY;
2397 }
2398
2399 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2400 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002401 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (tg3_wait_macro_done(tp)) {
2403 *resetp = 1;
2404 return -EBUSY;
2405 }
2406
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002407 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (tg3_wait_macro_done(tp)) {
2409 *resetp = 1;
2410 return -EBUSY;
2411 }
2412
2413 for (i = 0; i < 6; i += 2) {
2414 u32 low, high;
2415
2416 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2417 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2418 tg3_wait_macro_done(tp)) {
2419 *resetp = 1;
2420 return -EBUSY;
2421 }
2422 low &= 0x7fff;
2423 high &= 0x000f;
2424 if (low != test_pat[chan][i] ||
2425 high != test_pat[chan][i+1]) {
2426 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2427 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2428 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2429
2430 return -EBUSY;
2431 }
2432 }
2433 }
2434
2435 return 0;
2436}
2437
2438static int tg3_phy_reset_chanpat(struct tg3 *tp)
2439{
2440 int chan;
2441
2442 for (chan = 0; chan < 4; chan++) {
2443 int i;
2444
2445 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2446 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002447 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 for (i = 0; i < 6; i++)
2449 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002450 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (tg3_wait_macro_done(tp))
2452 return -EBUSY;
2453 }
2454
2455 return 0;
2456}
2457
2458static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2459{
2460 u32 reg32, phy9_orig;
2461 int retries, do_phy_reset, err;
2462
2463 retries = 10;
2464 do_phy_reset = 1;
2465 do {
2466 if (do_phy_reset) {
2467 err = tg3_bmcr_reset(tp);
2468 if (err)
2469 return err;
2470 do_phy_reset = 0;
2471 }
2472
2473 /* Disable transmitter and interrupt. */
2474 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2475 continue;
2476
2477 reg32 |= 0x3000;
2478 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2479
2480 /* Set full-duplex, 1000 mbps. */
2481 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002482 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002485 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 continue;
2487
Matt Carlson221c5632011-06-13 13:39:01 +00002488 tg3_writephy(tp, MII_CTRL1000,
2489 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002491 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002492 if (err)
2493 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002496 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2499 if (!err)
2500 break;
2501 } while (--retries);
2502
2503 err = tg3_phy_reset_chanpat(tp);
2504 if (err)
2505 return err;
2506
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002507 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002510 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002512 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Matt Carlson221c5632011-06-13 13:39:01 +00002514 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2517 reg32 &= ~0x3000;
2518 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2519 } else if (!err)
2520 err = -EBUSY;
2521
2522 return err;
2523}
2524
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002525static void tg3_carrier_on(struct tg3 *tp)
2526{
2527 netif_carrier_on(tp->dev);
2528 tp->link_up = true;
2529}
2530
2531static void tg3_carrier_off(struct tg3 *tp)
2532{
2533 netif_carrier_off(tp->dev);
2534 tp->link_up = false;
2535}
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537/* This will reset the tigon3 PHY if there is no valid
2538 * link unless the FORCE argument is non-zero.
2539 */
2540static int tg3_phy_reset(struct tg3 *tp)
2541{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002542 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 int err;
2544
Joe Perches41535772013-02-16 11:20:04 +00002545 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002546 val = tr32(GRC_MISC_CFG);
2547 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2548 udelay(40);
2549 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002550 err = tg3_readphy(tp, MII_BMSR, &val);
2551 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (err != 0)
2553 return -EBUSY;
2554
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002555 if (netif_running(tp->dev) && tp->link_up) {
2556 tg3_carrier_off(tp);
Michael Chanc8e1e822006-04-29 18:55:17 -07002557 tg3_link_report(tp);
2558 }
2559
Joe Perches41535772013-02-16 11:20:04 +00002560 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2561 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2562 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 err = tg3_phy_reset_5703_4_5(tp);
2564 if (err)
2565 return err;
2566 goto out;
2567 }
2568
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002569 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002570 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2571 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002572 cpmuctrl = tr32(TG3_CPMU_CTRL);
2573 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2574 tw32(TG3_CPMU_CTRL,
2575 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2576 }
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 err = tg3_bmcr_reset(tp);
2579 if (err)
2580 return err;
2581
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002582 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002583 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2584 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002585
2586 tw32(TG3_CPMU_CTRL, cpmuctrl);
2587 }
2588
Joe Perches41535772013-02-16 11:20:04 +00002589 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2590 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002591 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2592 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2593 CPMU_LSPD_1000MB_MACCLK_12_5) {
2594 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2595 udelay(40);
2596 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2597 }
2598 }
2599
Joe Perches63c3a662011-04-26 08:12:10 +00002600 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002601 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002602 return 0;
2603
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002604 tg3_phy_apply_otp(tp);
2605
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002606 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002607 tg3_phy_toggle_apd(tp, true);
2608 else
2609 tg3_phy_toggle_apd(tp, false);
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002612 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002613 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002614 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2615 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002616 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002618
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002619 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002620 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2621 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002623
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002624 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002625 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002626 tg3_phydsp_write(tp, 0x000a, 0x310b);
2627 tg3_phydsp_write(tp, 0x201f, 0x9506);
2628 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002629 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002630 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002631 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002632 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002633 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2634 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2635 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2636 tg3_writephy(tp, MII_TG3_TEST1,
2637 MII_TG3_TEST1_TRIM_EN | 0x4);
2638 } else
2639 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2640
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002641 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002642 }
Michael Chanc424cb22006-04-29 18:56:34 -07002643 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 /* Set Extended packet length bit (bit 14) on all chips that */
2646 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002647 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002649 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002650 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002652 err = tg3_phy_auxctl_read(tp,
2653 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2654 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002655 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2656 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658
2659 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2660 * jumbo frames transmission.
2661 */
Joe Perches63c3a662011-04-26 08:12:10 +00002662 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002663 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002664 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002665 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
2667
Joe Perches41535772013-02-16 11:20:04 +00002668 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002669 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002670 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002671 }
2672
Joe Perches41535772013-02-16 11:20:04 +00002673 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002674 tg3_phydsp_write(tp, 0xffb, 0x4000);
2675
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002676 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 tg3_phy_set_wirespeed(tp);
2678 return 0;
2679}
2680
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002681#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2682#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2683#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2684 TG3_GPIO_MSG_NEED_VAUX)
2685#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2686 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2687 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2688 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2689 (TG3_GPIO_MSG_DRVR_PRES << 12))
2690
2691#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2692 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2693 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2694 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2695 (TG3_GPIO_MSG_NEED_VAUX << 12))
2696
2697static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2698{
2699 u32 status, shift;
2700
Joe Perches41535772013-02-16 11:20:04 +00002701 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2702 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002703 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2704 else
2705 status = tr32(TG3_CPMU_DRV_STATUS);
2706
2707 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2708 status &= ~(TG3_GPIO_MSG_MASK << shift);
2709 status |= (newstat << shift);
2710
Joe Perches41535772013-02-16 11:20:04 +00002711 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2712 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002713 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2714 else
2715 tw32(TG3_CPMU_DRV_STATUS, status);
2716
2717 return status >> TG3_APE_GPIO_MSG_SHIFT;
2718}
2719
Matt Carlson520b2752011-06-13 13:39:02 +00002720static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2721{
2722 if (!tg3_flag(tp, IS_NIC))
2723 return 0;
2724
Joe Perches41535772013-02-16 11:20:04 +00002725 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2726 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2727 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002728 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2729 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002730
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002731 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2732
2733 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2734 TG3_GRC_LCLCTL_PWRSW_DELAY);
2735
2736 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2737 } else {
2738 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2739 TG3_GRC_LCLCTL_PWRSW_DELAY);
2740 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002741
Matt Carlson520b2752011-06-13 13:39:02 +00002742 return 0;
2743}
2744
2745static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2746{
2747 u32 grc_local_ctrl;
2748
2749 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002750 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2751 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002752 return;
2753
2754 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2755
2756 tw32_wait_f(GRC_LOCAL_CTRL,
2757 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2758 TG3_GRC_LCLCTL_PWRSW_DELAY);
2759
2760 tw32_wait_f(GRC_LOCAL_CTRL,
2761 grc_local_ctrl,
2762 TG3_GRC_LCLCTL_PWRSW_DELAY);
2763
2764 tw32_wait_f(GRC_LOCAL_CTRL,
2765 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2766 TG3_GRC_LCLCTL_PWRSW_DELAY);
2767}
2768
2769static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2770{
2771 if (!tg3_flag(tp, IS_NIC))
2772 return;
2773
Joe Perches41535772013-02-16 11:20:04 +00002774 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2775 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002776 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2777 (GRC_LCLCTRL_GPIO_OE0 |
2778 GRC_LCLCTRL_GPIO_OE1 |
2779 GRC_LCLCTRL_GPIO_OE2 |
2780 GRC_LCLCTRL_GPIO_OUTPUT0 |
2781 GRC_LCLCTRL_GPIO_OUTPUT1),
2782 TG3_GRC_LCLCTL_PWRSW_DELAY);
2783 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2784 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2785 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2786 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2787 GRC_LCLCTRL_GPIO_OE1 |
2788 GRC_LCLCTRL_GPIO_OE2 |
2789 GRC_LCLCTRL_GPIO_OUTPUT0 |
2790 GRC_LCLCTRL_GPIO_OUTPUT1 |
2791 tp->grc_local_ctrl;
2792 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2793 TG3_GRC_LCLCTL_PWRSW_DELAY);
2794
2795 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2796 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2797 TG3_GRC_LCLCTL_PWRSW_DELAY);
2798
2799 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2800 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2801 TG3_GRC_LCLCTL_PWRSW_DELAY);
2802 } else {
2803 u32 no_gpio2;
2804 u32 grc_local_ctrl = 0;
2805
2806 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002807 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002808 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2809 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2810 grc_local_ctrl,
2811 TG3_GRC_LCLCTL_PWRSW_DELAY);
2812 }
2813
2814 /* On 5753 and variants, GPIO2 cannot be used. */
2815 no_gpio2 = tp->nic_sram_data_cfg &
2816 NIC_SRAM_DATA_CFG_NO_GPIO2;
2817
2818 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2819 GRC_LCLCTRL_GPIO_OE1 |
2820 GRC_LCLCTRL_GPIO_OE2 |
2821 GRC_LCLCTRL_GPIO_OUTPUT1 |
2822 GRC_LCLCTRL_GPIO_OUTPUT2;
2823 if (no_gpio2) {
2824 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2825 GRC_LCLCTRL_GPIO_OUTPUT2);
2826 }
2827 tw32_wait_f(GRC_LOCAL_CTRL,
2828 tp->grc_local_ctrl | grc_local_ctrl,
2829 TG3_GRC_LCLCTL_PWRSW_DELAY);
2830
2831 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2832
2833 tw32_wait_f(GRC_LOCAL_CTRL,
2834 tp->grc_local_ctrl | grc_local_ctrl,
2835 TG3_GRC_LCLCTL_PWRSW_DELAY);
2836
2837 if (!no_gpio2) {
2838 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2839 tw32_wait_f(GRC_LOCAL_CTRL,
2840 tp->grc_local_ctrl | grc_local_ctrl,
2841 TG3_GRC_LCLCTL_PWRSW_DELAY);
2842 }
2843 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002844}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002845
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002846static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002847{
2848 u32 msg = 0;
2849
2850 /* Serialize power state transitions */
2851 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2852 return;
2853
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002854 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002855 msg = TG3_GPIO_MSG_NEED_VAUX;
2856
2857 msg = tg3_set_function_status(tp, msg);
2858
2859 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2860 goto done;
2861
2862 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2863 tg3_pwrsrc_switch_to_vaux(tp);
2864 else
2865 tg3_pwrsrc_die_with_vmain(tp);
2866
2867done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002868 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002869}
2870
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002871static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Matt Carlson683644b2011-03-09 16:58:23 +00002873 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Matt Carlson334355a2010-01-20 16:58:10 +00002875 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002876 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return;
2878
Joe Perches41535772013-02-16 11:20:04 +00002879 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2880 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2881 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002882 tg3_frob_aux_power_5717(tp, include_wol ?
2883 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002884 return;
2885 }
2886
2887 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002888 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002890 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002891
Michael Chanbc1c7562006-03-20 17:48:03 -08002892 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002893 if (dev_peer) {
2894 struct tg3 *tp_peer = netdev_priv(dev_peer);
2895
Joe Perches63c3a662011-04-26 08:12:10 +00002896 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002897 return;
2898
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002899 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002900 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002901 need_vaux = true;
2902 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002905 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2906 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002907 need_vaux = true;
2908
Matt Carlson520b2752011-06-13 13:39:02 +00002909 if (need_vaux)
2910 tg3_pwrsrc_switch_to_vaux(tp);
2911 else
2912 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002915static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2916{
2917 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2918 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002919 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002920 if (speed != SPEED_10)
2921 return 1;
2922 } else if (speed == SPEED_10)
2923 return 1;
2924
2925 return 0;
2926}
2927
Matt Carlson0a459aa2008-11-03 16:54:15 -08002928static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002929{
Matt Carlsonce057f02007-11-12 21:08:03 -08002930 u32 val;
2931
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002932 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00002933 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08002934 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2935 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2936
2937 sg_dig_ctrl |=
2938 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2939 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2940 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2941 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002942 return;
Michael Chan51297242007-02-13 12:17:57 -08002943 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002944
Joe Perches41535772013-02-16 11:20:04 +00002945 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002946 tg3_bmcr_reset(tp);
2947 val = tr32(GRC_MISC_CFG);
2948 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2949 udelay(40);
2950 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002951 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002952 u32 phytest;
2953 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2954 u32 phy;
2955
2956 tg3_writephy(tp, MII_ADVERTISE, 0);
2957 tg3_writephy(tp, MII_BMCR,
2958 BMCR_ANENABLE | BMCR_ANRESTART);
2959
2960 tg3_writephy(tp, MII_TG3_FET_TEST,
2961 phytest | MII_TG3_FET_SHADOW_EN);
2962 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2963 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2964 tg3_writephy(tp,
2965 MII_TG3_FET_SHDW_AUXMODE4,
2966 phy);
2967 }
2968 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2969 }
2970 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002971 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002972 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2973 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002974
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002975 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2976 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2977 MII_TG3_AUXCTL_PCTL_VREG_11V;
2978 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002979 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002980
Michael Chan15c3b692006-03-22 01:06:52 -08002981 /* The PHY should not be powered down on some chips because
2982 * of bugs.
2983 */
Joe Perches41535772013-02-16 11:20:04 +00002984 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2985 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2986 (tg3_asic_rev(tp) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002987 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
Joe Perches41535772013-02-16 11:20:04 +00002988 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002989 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002990 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002991
Joe Perches41535772013-02-16 11:20:04 +00002992 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2993 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002994 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2995 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2996 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2997 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2998 }
2999
Michael Chan15c3b692006-03-22 01:06:52 -08003000 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3001}
3002
Matt Carlson3f007892008-11-03 16:51:36 -08003003/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003004static int tg3_nvram_lock(struct tg3 *tp)
3005{
Joe Perches63c3a662011-04-26 08:12:10 +00003006 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003007 int i;
3008
3009 if (tp->nvram_lock_cnt == 0) {
3010 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3011 for (i = 0; i < 8000; i++) {
3012 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3013 break;
3014 udelay(20);
3015 }
3016 if (i == 8000) {
3017 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3018 return -ENODEV;
3019 }
3020 }
3021 tp->nvram_lock_cnt++;
3022 }
3023 return 0;
3024}
3025
3026/* tp->lock is held. */
3027static void tg3_nvram_unlock(struct tg3 *tp)
3028{
Joe Perches63c3a662011-04-26 08:12:10 +00003029 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003030 if (tp->nvram_lock_cnt > 0)
3031 tp->nvram_lock_cnt--;
3032 if (tp->nvram_lock_cnt == 0)
3033 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3034 }
3035}
3036
3037/* tp->lock is held. */
3038static void tg3_enable_nvram_access(struct tg3 *tp)
3039{
Joe Perches63c3a662011-04-26 08:12:10 +00003040 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003041 u32 nvaccess = tr32(NVRAM_ACCESS);
3042
3043 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3044 }
3045}
3046
3047/* tp->lock is held. */
3048static void tg3_disable_nvram_access(struct tg3 *tp)
3049{
Joe Perches63c3a662011-04-26 08:12:10 +00003050 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003051 u32 nvaccess = tr32(NVRAM_ACCESS);
3052
3053 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3054 }
3055}
3056
3057static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3058 u32 offset, u32 *val)
3059{
3060 u32 tmp;
3061 int i;
3062
3063 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3064 return -EINVAL;
3065
3066 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3067 EEPROM_ADDR_DEVID_MASK |
3068 EEPROM_ADDR_READ);
3069 tw32(GRC_EEPROM_ADDR,
3070 tmp |
3071 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3072 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3073 EEPROM_ADDR_ADDR_MASK) |
3074 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3075
3076 for (i = 0; i < 1000; i++) {
3077 tmp = tr32(GRC_EEPROM_ADDR);
3078
3079 if (tmp & EEPROM_ADDR_COMPLETE)
3080 break;
3081 msleep(1);
3082 }
3083 if (!(tmp & EEPROM_ADDR_COMPLETE))
3084 return -EBUSY;
3085
Matt Carlson62cedd12009-04-20 14:52:29 -07003086 tmp = tr32(GRC_EEPROM_DATA);
3087
3088 /*
3089 * The data will always be opposite the native endian
3090 * format. Perform a blind byteswap to compensate.
3091 */
3092 *val = swab32(tmp);
3093
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003094 return 0;
3095}
3096
3097#define NVRAM_CMD_TIMEOUT 10000
3098
3099static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3100{
3101 int i;
3102
3103 tw32(NVRAM_CMD, nvram_cmd);
3104 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3105 udelay(10);
3106 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3107 udelay(10);
3108 break;
3109 }
3110 }
3111
3112 if (i == NVRAM_CMD_TIMEOUT)
3113 return -EBUSY;
3114
3115 return 0;
3116}
3117
3118static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3119{
Joe Perches63c3a662011-04-26 08:12:10 +00003120 if (tg3_flag(tp, NVRAM) &&
3121 tg3_flag(tp, NVRAM_BUFFERED) &&
3122 tg3_flag(tp, FLASH) &&
3123 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003124 (tp->nvram_jedecnum == JEDEC_ATMEL))
3125
3126 addr = ((addr / tp->nvram_pagesize) <<
3127 ATMEL_AT45DB0X1B_PAGE_POS) +
3128 (addr % tp->nvram_pagesize);
3129
3130 return addr;
3131}
3132
3133static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3134{
Joe Perches63c3a662011-04-26 08:12:10 +00003135 if (tg3_flag(tp, NVRAM) &&
3136 tg3_flag(tp, NVRAM_BUFFERED) &&
3137 tg3_flag(tp, FLASH) &&
3138 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003139 (tp->nvram_jedecnum == JEDEC_ATMEL))
3140
3141 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3142 tp->nvram_pagesize) +
3143 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3144
3145 return addr;
3146}
3147
Matt Carlsone4f34112009-02-25 14:25:00 +00003148/* NOTE: Data read in from NVRAM is byteswapped according to
3149 * the byteswapping settings for all other register accesses.
3150 * tg3 devices are BE devices, so on a BE machine, the data
3151 * returned will be exactly as it is seen in NVRAM. On a LE
3152 * machine, the 32-bit value will be byteswapped.
3153 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003154static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3155{
3156 int ret;
3157
Joe Perches63c3a662011-04-26 08:12:10 +00003158 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003159 return tg3_nvram_read_using_eeprom(tp, offset, val);
3160
3161 offset = tg3_nvram_phys_addr(tp, offset);
3162
3163 if (offset > NVRAM_ADDR_MSK)
3164 return -EINVAL;
3165
3166 ret = tg3_nvram_lock(tp);
3167 if (ret)
3168 return ret;
3169
3170 tg3_enable_nvram_access(tp);
3171
3172 tw32(NVRAM_ADDR, offset);
3173 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3174 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3175
3176 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003177 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003178
3179 tg3_disable_nvram_access(tp);
3180
3181 tg3_nvram_unlock(tp);
3182
3183 return ret;
3184}
3185
Matt Carlsona9dc5292009-02-25 14:25:30 +00003186/* Ensures NVRAM data is in bytestream format. */
3187static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003188{
3189 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003190 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003191 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003192 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003193 return res;
3194}
3195
Matt Carlsondbe9b922012-02-13 10:20:09 +00003196static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3197 u32 offset, u32 len, u8 *buf)
3198{
3199 int i, j, rc = 0;
3200 u32 val;
3201
3202 for (i = 0; i < len; i += 4) {
3203 u32 addr;
3204 __be32 data;
3205
3206 addr = offset + i;
3207
3208 memcpy(&data, buf + i, 4);
3209
3210 /*
3211 * The SEEPROM interface expects the data to always be opposite
3212 * the native endian format. We accomplish this by reversing
3213 * all the operations that would have been performed on the
3214 * data from a call to tg3_nvram_read_be32().
3215 */
3216 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3217
3218 val = tr32(GRC_EEPROM_ADDR);
3219 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3220
3221 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3222 EEPROM_ADDR_READ);
3223 tw32(GRC_EEPROM_ADDR, val |
3224 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3225 (addr & EEPROM_ADDR_ADDR_MASK) |
3226 EEPROM_ADDR_START |
3227 EEPROM_ADDR_WRITE);
3228
3229 for (j = 0; j < 1000; j++) {
3230 val = tr32(GRC_EEPROM_ADDR);
3231
3232 if (val & EEPROM_ADDR_COMPLETE)
3233 break;
3234 msleep(1);
3235 }
3236 if (!(val & EEPROM_ADDR_COMPLETE)) {
3237 rc = -EBUSY;
3238 break;
3239 }
3240 }
3241
3242 return rc;
3243}
3244
3245/* offset and length are dword aligned */
3246static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3247 u8 *buf)
3248{
3249 int ret = 0;
3250 u32 pagesize = tp->nvram_pagesize;
3251 u32 pagemask = pagesize - 1;
3252 u32 nvram_cmd;
3253 u8 *tmp;
3254
3255 tmp = kmalloc(pagesize, GFP_KERNEL);
3256 if (tmp == NULL)
3257 return -ENOMEM;
3258
3259 while (len) {
3260 int j;
3261 u32 phy_addr, page_off, size;
3262
3263 phy_addr = offset & ~pagemask;
3264
3265 for (j = 0; j < pagesize; j += 4) {
3266 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3267 (__be32 *) (tmp + j));
3268 if (ret)
3269 break;
3270 }
3271 if (ret)
3272 break;
3273
3274 page_off = offset & pagemask;
3275 size = pagesize;
3276 if (len < size)
3277 size = len;
3278
3279 len -= size;
3280
3281 memcpy(tmp + page_off, buf, size);
3282
3283 offset = offset + (pagesize - page_off);
3284
3285 tg3_enable_nvram_access(tp);
3286
3287 /*
3288 * Before we can erase the flash page, we need
3289 * to issue a special "write enable" command.
3290 */
3291 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3292
3293 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3294 break;
3295
3296 /* Erase the target page */
3297 tw32(NVRAM_ADDR, phy_addr);
3298
3299 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3300 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3301
3302 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3303 break;
3304
3305 /* Issue another write enable to start the write. */
3306 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3307
3308 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3309 break;
3310
3311 for (j = 0; j < pagesize; j += 4) {
3312 __be32 data;
3313
3314 data = *((__be32 *) (tmp + j));
3315
3316 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3317
3318 tw32(NVRAM_ADDR, phy_addr + j);
3319
3320 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3321 NVRAM_CMD_WR;
3322
3323 if (j == 0)
3324 nvram_cmd |= NVRAM_CMD_FIRST;
3325 else if (j == (pagesize - 4))
3326 nvram_cmd |= NVRAM_CMD_LAST;
3327
3328 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3329 if (ret)
3330 break;
3331 }
3332 if (ret)
3333 break;
3334 }
3335
3336 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3337 tg3_nvram_exec_cmd(tp, nvram_cmd);
3338
3339 kfree(tmp);
3340
3341 return ret;
3342}
3343
3344/* offset and length are dword aligned */
3345static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3346 u8 *buf)
3347{
3348 int i, ret = 0;
3349
3350 for (i = 0; i < len; i += 4, offset += 4) {
3351 u32 page_off, phy_addr, nvram_cmd;
3352 __be32 data;
3353
3354 memcpy(&data, buf + i, 4);
3355 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3356
3357 page_off = offset % tp->nvram_pagesize;
3358
3359 phy_addr = tg3_nvram_phys_addr(tp, offset);
3360
Matt Carlsondbe9b922012-02-13 10:20:09 +00003361 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3362
3363 if (page_off == 0 || i == 0)
3364 nvram_cmd |= NVRAM_CMD_FIRST;
3365 if (page_off == (tp->nvram_pagesize - 4))
3366 nvram_cmd |= NVRAM_CMD_LAST;
3367
3368 if (i == (len - 4))
3369 nvram_cmd |= NVRAM_CMD_LAST;
3370
Matt Carlson42278222012-02-13 15:20:11 +00003371 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3372 !tg3_flag(tp, FLASH) ||
3373 !tg3_flag(tp, 57765_PLUS))
3374 tw32(NVRAM_ADDR, phy_addr);
3375
Joe Perches41535772013-02-16 11:20:04 +00003376 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003377 !tg3_flag(tp, 5755_PLUS) &&
3378 (tp->nvram_jedecnum == JEDEC_ST) &&
3379 (nvram_cmd & NVRAM_CMD_FIRST)) {
3380 u32 cmd;
3381
3382 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3383 ret = tg3_nvram_exec_cmd(tp, cmd);
3384 if (ret)
3385 break;
3386 }
3387 if (!tg3_flag(tp, FLASH)) {
3388 /* We always do complete word writes to eeprom. */
3389 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3390 }
3391
3392 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3393 if (ret)
3394 break;
3395 }
3396 return ret;
3397}
3398
3399/* offset and length are dword aligned */
3400static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3401{
3402 int ret;
3403
3404 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3405 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3406 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3407 udelay(40);
3408 }
3409
3410 if (!tg3_flag(tp, NVRAM)) {
3411 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3412 } else {
3413 u32 grc_mode;
3414
3415 ret = tg3_nvram_lock(tp);
3416 if (ret)
3417 return ret;
3418
3419 tg3_enable_nvram_access(tp);
3420 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3421 tw32(NVRAM_WRITE1, 0x406);
3422
3423 grc_mode = tr32(GRC_MODE);
3424 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3425
3426 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3427 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3428 buf);
3429 } else {
3430 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3431 buf);
3432 }
3433
3434 grc_mode = tr32(GRC_MODE);
3435 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3436
3437 tg3_disable_nvram_access(tp);
3438 tg3_nvram_unlock(tp);
3439 }
3440
3441 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3442 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3443 udelay(40);
3444 }
3445
3446 return ret;
3447}
3448
Matt Carlson997b4f12011-08-31 11:44:53 +00003449#define RX_CPU_SCRATCH_BASE 0x30000
3450#define RX_CPU_SCRATCH_SIZE 0x04000
3451#define TX_CPU_SCRATCH_BASE 0x34000
3452#define TX_CPU_SCRATCH_SIZE 0x04000
3453
3454/* tp->lock is held. */
3455static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3456{
3457 int i;
3458
3459 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3460
Joe Perches41535772013-02-16 11:20:04 +00003461 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003462 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3463
3464 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3465 return 0;
3466 }
3467 if (offset == RX_CPU_BASE) {
3468 for (i = 0; i < 10000; i++) {
3469 tw32(offset + CPU_STATE, 0xffffffff);
3470 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3471 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3472 break;
3473 }
3474
3475 tw32(offset + CPU_STATE, 0xffffffff);
3476 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3477 udelay(10);
3478 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003479 /*
3480 * There is only an Rx CPU for the 5750 derivative in the
3481 * BCM4785.
3482 */
3483 if (tg3_flag(tp, IS_SSB_CORE))
3484 return 0;
3485
Matt Carlson997b4f12011-08-31 11:44:53 +00003486 for (i = 0; i < 10000; i++) {
3487 tw32(offset + CPU_STATE, 0xffffffff);
3488 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3489 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3490 break;
3491 }
3492 }
3493
3494 if (i >= 10000) {
3495 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3496 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3497 return -ENODEV;
3498 }
3499
3500 /* Clear firmware's nvram arbitration. */
3501 if (tg3_flag(tp, NVRAM))
3502 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3503 return 0;
3504}
3505
3506struct fw_info {
3507 unsigned int fw_base;
3508 unsigned int fw_len;
3509 const __be32 *fw_data;
3510};
3511
3512/* tp->lock is held. */
3513static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3514 u32 cpu_scratch_base, int cpu_scratch_size,
3515 struct fw_info *info)
3516{
3517 int err, lock_err, i;
3518 void (*write_op)(struct tg3 *, u32, u32);
3519
3520 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3521 netdev_err(tp->dev,
3522 "%s: Trying to load TX cpu firmware which is 5705\n",
3523 __func__);
3524 return -EINVAL;
3525 }
3526
3527 if (tg3_flag(tp, 5705_PLUS))
3528 write_op = tg3_write_mem;
3529 else
3530 write_op = tg3_write_indirect_reg32;
3531
3532 /* It is possible that bootcode is still loading at this point.
3533 * Get the nvram lock first before halting the cpu.
3534 */
3535 lock_err = tg3_nvram_lock(tp);
3536 err = tg3_halt_cpu(tp, cpu_base);
3537 if (!lock_err)
3538 tg3_nvram_unlock(tp);
3539 if (err)
3540 goto out;
3541
3542 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3543 write_op(tp, cpu_scratch_base + i, 0);
3544 tw32(cpu_base + CPU_STATE, 0xffffffff);
3545 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3546 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3547 write_op(tp, (cpu_scratch_base +
3548 (info->fw_base & 0xffff) +
3549 (i * sizeof(u32))),
3550 be32_to_cpu(info->fw_data[i]));
3551
3552 err = 0;
3553
3554out:
3555 return err;
3556}
3557
3558/* tp->lock is held. */
3559static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3560{
3561 struct fw_info info;
3562 const __be32 *fw_data;
3563 int err, i;
3564
3565 fw_data = (void *)tp->fw->data;
3566
3567 /* Firmware blob starts with version numbers, followed by
3568 start address and length. We are setting complete length.
3569 length = end_address_of_bss - start_address_of_text.
3570 Remainder is the blob to be loaded contiguously
3571 from start address. */
3572
3573 info.fw_base = be32_to_cpu(fw_data[1]);
3574 info.fw_len = tp->fw->size - 12;
3575 info.fw_data = &fw_data[3];
3576
3577 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3578 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3579 &info);
3580 if (err)
3581 return err;
3582
3583 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3584 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3585 &info);
3586 if (err)
3587 return err;
3588
3589 /* Now startup only the RX cpu. */
3590 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3591 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3592
3593 for (i = 0; i < 5; i++) {
3594 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3595 break;
3596 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3597 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3598 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3599 udelay(1000);
3600 }
3601 if (i >= 5) {
3602 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3603 "should be %08x\n", __func__,
3604 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3605 return -ENODEV;
3606 }
3607 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3608 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3609
3610 return 0;
3611}
3612
3613/* tp->lock is held. */
3614static int tg3_load_tso_firmware(struct tg3 *tp)
3615{
3616 struct fw_info info;
3617 const __be32 *fw_data;
3618 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3619 int err, i;
3620
3621 if (tg3_flag(tp, HW_TSO_1) ||
3622 tg3_flag(tp, HW_TSO_2) ||
3623 tg3_flag(tp, HW_TSO_3))
3624 return 0;
3625
3626 fw_data = (void *)tp->fw->data;
3627
3628 /* Firmware blob starts with version numbers, followed by
3629 start address and length. We are setting complete length.
3630 length = end_address_of_bss - start_address_of_text.
3631 Remainder is the blob to be loaded contiguously
3632 from start address. */
3633
3634 info.fw_base = be32_to_cpu(fw_data[1]);
3635 cpu_scratch_size = tp->fw_len;
3636 info.fw_len = tp->fw->size - 12;
3637 info.fw_data = &fw_data[3];
3638
Joe Perches41535772013-02-16 11:20:04 +00003639 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003640 cpu_base = RX_CPU_BASE;
3641 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3642 } else {
3643 cpu_base = TX_CPU_BASE;
3644 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3645 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3646 }
3647
3648 err = tg3_load_firmware_cpu(tp, cpu_base,
3649 cpu_scratch_base, cpu_scratch_size,
3650 &info);
3651 if (err)
3652 return err;
3653
3654 /* Now startup the cpu. */
3655 tw32(cpu_base + CPU_STATE, 0xffffffff);
3656 tw32_f(cpu_base + CPU_PC, info.fw_base);
3657
3658 for (i = 0; i < 5; i++) {
3659 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3660 break;
3661 tw32(cpu_base + CPU_STATE, 0xffffffff);
3662 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3663 tw32_f(cpu_base + CPU_PC, info.fw_base);
3664 udelay(1000);
3665 }
3666 if (i >= 5) {
3667 netdev_err(tp->dev,
3668 "%s fails to set CPU PC, is %08x should be %08x\n",
3669 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3670 return -ENODEV;
3671 }
3672 tw32(cpu_base + CPU_STATE, 0xffffffff);
3673 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3674 return 0;
3675}
3676
3677
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003678/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003679static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3680{
3681 u32 addr_high, addr_low;
3682 int i;
3683
3684 addr_high = ((tp->dev->dev_addr[0] << 8) |
3685 tp->dev->dev_addr[1]);
3686 addr_low = ((tp->dev->dev_addr[2] << 24) |
3687 (tp->dev->dev_addr[3] << 16) |
3688 (tp->dev->dev_addr[4] << 8) |
3689 (tp->dev->dev_addr[5] << 0));
3690 for (i = 0; i < 4; i++) {
3691 if (i == 1 && skip_mac_1)
3692 continue;
3693 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3694 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3695 }
3696
Joe Perches41535772013-02-16 11:20:04 +00003697 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3698 tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson3f007892008-11-03 16:51:36 -08003699 for (i = 0; i < 12; i++) {
3700 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3701 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3702 }
3703 }
3704
3705 addr_high = (tp->dev->dev_addr[0] +
3706 tp->dev->dev_addr[1] +
3707 tp->dev->dev_addr[2] +
3708 tp->dev->dev_addr[3] +
3709 tp->dev->dev_addr[4] +
3710 tp->dev->dev_addr[5]) &
3711 TX_BACKOFF_SEED_MASK;
3712 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3713}
3714
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003715static void tg3_enable_register_access(struct tg3 *tp)
3716{
3717 /*
3718 * Make sure register accesses (indirect or otherwise) will function
3719 * correctly.
3720 */
3721 pci_write_config_dword(tp->pdev,
3722 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3723}
3724
3725static int tg3_power_up(struct tg3 *tp)
3726{
Matt Carlsonbed98292011-07-13 09:27:29 +00003727 int err;
3728
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003729 tg3_enable_register_access(tp);
3730
Matt Carlsonbed98292011-07-13 09:27:29 +00003731 err = pci_set_power_state(tp->pdev, PCI_D0);
3732 if (!err) {
3733 /* Switch out of Vaux if it is a NIC */
3734 tg3_pwrsrc_switch_to_vmain(tp);
3735 } else {
3736 netdev_err(tp->dev, "Transition to D0 failed\n");
3737 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003738
Matt Carlsonbed98292011-07-13 09:27:29 +00003739 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003740}
3741
Matt Carlson4b409522012-02-13 10:20:11 +00003742static int tg3_setup_phy(struct tg3 *, int);
3743
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003744static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
3746 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003747 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003749 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003750
3751 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003752 if (tg3_flag(tp, CLKREQ_BUG))
3753 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3754 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003755
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3757 tw32(TG3PCI_MISC_HOST_CTRL,
3758 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3759
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003760 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003761 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003762
Joe Perches63c3a662011-04-26 08:12:10 +00003763 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003764 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003765 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003766 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003767 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003768 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003769
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003770 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003771
Matt Carlson80096062010-08-02 11:26:06 +00003772 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003773
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003774 tp->link_config.speed = phydev->speed;
3775 tp->link_config.duplex = phydev->duplex;
3776 tp->link_config.autoneg = phydev->autoneg;
3777 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003778
3779 advertising = ADVERTISED_TP |
3780 ADVERTISED_Pause |
3781 ADVERTISED_Autoneg |
3782 ADVERTISED_10baseT_Half;
3783
Joe Perches63c3a662011-04-26 08:12:10 +00003784 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3785 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003786 advertising |=
3787 ADVERTISED_100baseT_Half |
3788 ADVERTISED_100baseT_Full |
3789 ADVERTISED_10baseT_Full;
3790 else
3791 advertising |= ADVERTISED_10baseT_Full;
3792 }
3793
3794 phydev->advertising = advertising;
3795
3796 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003797
3798 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003799 if (phyid != PHY_ID_BCMAC131) {
3800 phyid &= PHY_BCM_OUI_MASK;
3801 if (phyid == PHY_BCM_OUI_1 ||
3802 phyid == PHY_BCM_OUI_2 ||
3803 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003804 do_low_power = true;
3805 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003806 }
Matt Carlsondd477002008-05-25 23:45:58 -07003807 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003808 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003809
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003810 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003811 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Matt Carlson2855b9f2012-02-13 15:20:14 +00003813 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003814 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 }
3816
Joe Perches41535772013-02-16 11:20:04 +00003817 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07003818 u32 val;
3819
3820 val = tr32(GRC_VCPU_EXT_CTRL);
3821 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003822 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003823 int i;
3824 u32 val;
3825
3826 for (i = 0; i < 200; i++) {
3827 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3828 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3829 break;
3830 msleep(1);
3831 }
3832 }
Joe Perches63c3a662011-04-26 08:12:10 +00003833 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003834 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3835 WOL_DRV_STATE_SHUTDOWN |
3836 WOL_DRV_WOL |
3837 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003838
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003839 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 u32 mac_mode;
3841
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003842 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003843 if (do_low_power &&
3844 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3845 tg3_phy_auxctl_write(tp,
3846 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3847 MII_TG3_AUXCTL_PCTL_WOL_EN |
3848 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3849 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003850 udelay(40);
3851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003853 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003854 mac_mode = MAC_MODE_PORT_MODE_GMII;
3855 else
3856 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003858 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00003859 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003860 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003861 SPEED_100 : SPEED_10;
3862 if (tg3_5700_link_polarity(tp, speed))
3863 mac_mode |= MAC_MODE_LINK_POLARITY;
3864 else
3865 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 } else {
3868 mac_mode = MAC_MODE_PORT_MODE_TBI;
3869 }
3870
Joe Perches63c3a662011-04-26 08:12:10 +00003871 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 tw32(MAC_LED_CTRL, tp->led_ctrl);
3873
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003874 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003875 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3876 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003877 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
Joe Perches63c3a662011-04-26 08:12:10 +00003879 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003880 mac_mode |= MAC_MODE_APE_TX_EN |
3881 MAC_MODE_APE_RX_EN |
3882 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 tw32_f(MAC_MODE, mac_mode);
3885 udelay(100);
3886
3887 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3888 udelay(10);
3889 }
3890
Joe Perches63c3a662011-04-26 08:12:10 +00003891 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00003892 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3893 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 u32 base_val;
3895
3896 base_val = tp->pci_clock_ctrl;
3897 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3898 CLOCK_CTRL_TXCLK_DISABLE);
3899
Michael Chanb401e9e2005-12-19 16:27:04 -08003900 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3901 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003902 } else if (tg3_flag(tp, 5780_CLASS) ||
3903 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00003904 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003905 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003906 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 u32 newbits1, newbits2;
3908
Joe Perches41535772013-02-16 11:20:04 +00003909 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3910 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3912 CLOCK_CTRL_TXCLK_DISABLE |
3913 CLOCK_CTRL_ALTCLK);
3914 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003915 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 newbits1 = CLOCK_CTRL_625_CORE;
3917 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3918 } else {
3919 newbits1 = CLOCK_CTRL_ALTCLK;
3920 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3921 }
3922
Michael Chanb401e9e2005-12-19 16:27:04 -08003923 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3924 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Michael Chanb401e9e2005-12-19 16:27:04 -08003926 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3927 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
Joe Perches63c3a662011-04-26 08:12:10 +00003929 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 u32 newbits3;
3931
Joe Perches41535772013-02-16 11:20:04 +00003932 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3933 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3935 CLOCK_CTRL_TXCLK_DISABLE |
3936 CLOCK_CTRL_44MHZ_CORE);
3937 } else {
3938 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3939 }
3940
Michael Chanb401e9e2005-12-19 16:27:04 -08003941 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3942 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 }
3944 }
3945
Joe Perches63c3a662011-04-26 08:12:10 +00003946 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003947 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003948
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003949 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
3951 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003952 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00003953 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
3954 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 u32 val = tr32(0x7d00);
3956
3957 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3958 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003959 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003960 int err;
3961
3962 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003964 if (!err)
3965 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 }
3968
Michael Chanbbadf502006-04-06 21:46:34 -07003969 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 return 0;
3972}
3973
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003974static void tg3_power_down(struct tg3 *tp)
3975{
3976 tg3_power_down_prepare(tp);
3977
Joe Perches63c3a662011-04-26 08:12:10 +00003978 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003979 pci_set_power_state(tp->pdev, PCI_D3hot);
3980}
3981
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3983{
3984 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3985 case MII_TG3_AUX_STAT_10HALF:
3986 *speed = SPEED_10;
3987 *duplex = DUPLEX_HALF;
3988 break;
3989
3990 case MII_TG3_AUX_STAT_10FULL:
3991 *speed = SPEED_10;
3992 *duplex = DUPLEX_FULL;
3993 break;
3994
3995 case MII_TG3_AUX_STAT_100HALF:
3996 *speed = SPEED_100;
3997 *duplex = DUPLEX_HALF;
3998 break;
3999
4000 case MII_TG3_AUX_STAT_100FULL:
4001 *speed = SPEED_100;
4002 *duplex = DUPLEX_FULL;
4003 break;
4004
4005 case MII_TG3_AUX_STAT_1000HALF:
4006 *speed = SPEED_1000;
4007 *duplex = DUPLEX_HALF;
4008 break;
4009
4010 case MII_TG3_AUX_STAT_1000FULL:
4011 *speed = SPEED_1000;
4012 *duplex = DUPLEX_FULL;
4013 break;
4014
4015 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004016 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004017 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4018 SPEED_10;
4019 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4020 DUPLEX_HALF;
4021 break;
4022 }
Matt Carlsone7405222012-02-13 15:20:16 +00004023 *speed = SPEED_UNKNOWN;
4024 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
Matt Carlson42b64a42011-05-19 12:12:49 +00004029static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030{
Matt Carlson42b64a42011-05-19 12:12:49 +00004031 int err = 0;
4032 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Matt Carlson42b64a42011-05-19 12:12:49 +00004034 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004035 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004036 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Matt Carlson42b64a42011-05-19 12:12:49 +00004038 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4039 if (err)
4040 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Matt Carlson4f272092011-12-14 11:09:57 +00004042 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4043 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004044
Joe Perches41535772013-02-16 11:20:04 +00004045 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4046 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004047 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004048
Matt Carlson4f272092011-12-14 11:09:57 +00004049 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4050 if (err)
4051 goto done;
4052 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004053
Matt Carlson42b64a42011-05-19 12:12:49 +00004054 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4055 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Matt Carlson42b64a42011-05-19 12:12:49 +00004057 tw32(TG3_CPMU_EEE_MODE,
4058 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004059
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004060 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004061 if (!err) {
4062 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004063
Matt Carlsona6b68da2010-12-06 08:28:52 +00004064 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004065 /* Advertise 100-BaseTX EEE ability */
4066 if (advertise & ADVERTISED_100baseT_Full)
4067 val |= MDIO_AN_EEE_ADV_100TX;
4068 /* Advertise 1000-BaseT EEE ability */
4069 if (advertise & ADVERTISED_1000baseT_Full)
4070 val |= MDIO_AN_EEE_ADV_1000T;
4071 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004072 if (err)
4073 val = 0;
4074
Joe Perches41535772013-02-16 11:20:04 +00004075 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004076 case ASIC_REV_5717:
4077 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004078 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004079 case ASIC_REV_5719:
4080 /* If we advertised any eee advertisements above... */
4081 if (val)
4082 val = MII_TG3_DSP_TAP26_ALNOKO |
4083 MII_TG3_DSP_TAP26_RMRXSTO |
4084 MII_TG3_DSP_TAP26_OPCSINPT;
4085 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4086 /* Fall through */
4087 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004088 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004089 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4090 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4091 MII_TG3_DSP_CH34TP2_HIBW01);
4092 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004093
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004094 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004095 if (!err)
4096 err = err2;
4097 }
4098
4099done:
4100 return err;
4101}
4102
4103static void tg3_phy_copper_begin(struct tg3 *tp)
4104{
Matt Carlsond13ba512012-02-22 12:35:19 +00004105 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4106 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4107 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004108
Matt Carlsond13ba512012-02-22 12:35:19 +00004109 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
4110 adv = ADVERTISED_10baseT_Half |
4111 ADVERTISED_10baseT_Full;
4112 if (tg3_flag(tp, WOL_SPEED_100MB))
4113 adv |= ADVERTISED_100baseT_Half |
4114 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004115
Matt Carlsond13ba512012-02-22 12:35:19 +00004116 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004117 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004118 adv = tp->link_config.advertising;
4119 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4120 adv &= ~(ADVERTISED_1000baseT_Half |
4121 ADVERTISED_1000baseT_Full);
4122
4123 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004124 }
4125
Matt Carlsond13ba512012-02-22 12:35:19 +00004126 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004127
Matt Carlsond13ba512012-02-22 12:35:19 +00004128 tg3_writephy(tp, MII_BMCR,
4129 BMCR_ANENABLE | BMCR_ANRESTART);
4130 } else {
4131 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 u32 bmcr, orig_bmcr;
4133
4134 tp->link_config.active_speed = tp->link_config.speed;
4135 tp->link_config.active_duplex = tp->link_config.duplex;
4136
4137 bmcr = 0;
4138 switch (tp->link_config.speed) {
4139 default:
4140 case SPEED_10:
4141 break;
4142
4143 case SPEED_100:
4144 bmcr |= BMCR_SPEED100;
4145 break;
4146
4147 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004148 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
4152 if (tp->link_config.duplex == DUPLEX_FULL)
4153 bmcr |= BMCR_FULLDPLX;
4154
4155 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4156 (bmcr != orig_bmcr)) {
4157 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4158 for (i = 0; i < 1500; i++) {
4159 u32 tmp;
4160
4161 udelay(10);
4162 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4163 tg3_readphy(tp, MII_BMSR, &tmp))
4164 continue;
4165 if (!(tmp & BMSR_LSTATUS)) {
4166 udelay(40);
4167 break;
4168 }
4169 }
4170 tg3_writephy(tp, MII_BMCR, bmcr);
4171 udelay(40);
4172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 }
4174}
4175
4176static int tg3_init_5401phy_dsp(struct tg3 *tp)
4177{
4178 int err;
4179
4180 /* Turn off tap power management. */
4181 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004182 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004184 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4185 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4186 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4187 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4188 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
4190 udelay(40);
4191
4192 return err;
4193}
4194
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004195static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004197 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004198
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004199 advertising = tp->link_config.advertising;
4200 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004202 advmsk = ADVERTISE_ALL;
4203 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004204 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004205 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004208 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4209 return false;
4210
4211 if ((*lcladv & advmsk) != tgtadv)
4212 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004213
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004214 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 u32 tg3_ctrl;
4216
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004217 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004218
Matt Carlson221c5632011-06-13 13:39:01 +00004219 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004220 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Matt Carlson3198e072012-02-13 15:20:10 +00004222 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004223 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4224 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004225 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4226 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4227 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4228 } else {
4229 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4230 }
4231
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004232 if (tg3_ctrl != tgtadv)
4233 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004235
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004236 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004237}
4238
Matt Carlson859edb22011-12-08 14:40:16 +00004239static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4240{
4241 u32 lpeth = 0;
4242
4243 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4244 u32 val;
4245
4246 if (tg3_readphy(tp, MII_STAT1000, &val))
4247 return false;
4248
4249 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4250 }
4251
4252 if (tg3_readphy(tp, MII_LPA, rmtadv))
4253 return false;
4254
4255 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4256 tp->link_config.rmt_adv = lpeth;
4257
4258 return true;
4259}
4260
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004261static bool tg3_test_and_report_link_chg(struct tg3 *tp, int curr_link_up)
4262{
4263 if (curr_link_up != tp->link_up) {
4264 if (curr_link_up) {
4265 tg3_carrier_on(tp);
4266 } else {
4267 tg3_carrier_off(tp);
4268 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4269 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4270 }
4271
4272 tg3_link_report(tp);
4273 return true;
4274 }
4275
4276 return false;
4277}
4278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4280{
4281 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004282 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004283 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 u16 current_speed;
4285 u8 current_duplex;
4286 int i, err;
4287
4288 tw32(MAC_EVENT, 0);
4289
4290 tw32_f(MAC_STATUS,
4291 (MAC_STATUS_SYNC_CHANGED |
4292 MAC_STATUS_CFG_CHANGED |
4293 MAC_STATUS_MI_COMPLETION |
4294 MAC_STATUS_LNKSTATE_CHANGED));
4295 udelay(40);
4296
Matt Carlson8ef21422008-05-02 16:47:53 -07004297 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4298 tw32_f(MAC_MI_MODE,
4299 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4300 udelay(80);
4301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004303 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
4305 /* Some third-party PHYs need to be reset on link going
4306 * down.
4307 */
Joe Perches41535772013-02-16 11:20:04 +00004308 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4309 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4310 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004311 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 tg3_readphy(tp, MII_BMSR, &bmsr);
4313 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4314 !(bmsr & BMSR_LSTATUS))
4315 force_reset = 1;
4316 }
4317 if (force_reset)
4318 tg3_phy_reset(tp);
4319
Matt Carlson79eb6902010-02-17 15:17:03 +00004320 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 tg3_readphy(tp, MII_BMSR, &bmsr);
4322 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004323 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 bmsr = 0;
4325
4326 if (!(bmsr & BMSR_LSTATUS)) {
4327 err = tg3_init_5401phy_dsp(tp);
4328 if (err)
4329 return err;
4330
4331 tg3_readphy(tp, MII_BMSR, &bmsr);
4332 for (i = 0; i < 1000; i++) {
4333 udelay(10);
4334 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4335 (bmsr & BMSR_LSTATUS)) {
4336 udelay(40);
4337 break;
4338 }
4339 }
4340
Matt Carlson79eb6902010-02-17 15:17:03 +00004341 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4342 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 !(bmsr & BMSR_LSTATUS) &&
4344 tp->link_config.active_speed == SPEED_1000) {
4345 err = tg3_phy_reset(tp);
4346 if (!err)
4347 err = tg3_init_5401phy_dsp(tp);
4348 if (err)
4349 return err;
4350 }
4351 }
Joe Perches41535772013-02-16 11:20:04 +00004352 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4353 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 /* 5701 {A0,B0} CRC bug workaround */
4355 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004356 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4357 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4358 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 }
4360
4361 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004362 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4363 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004365 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004367 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4369
Joe Perches41535772013-02-16 11:20:04 +00004370 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4371 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4373 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4374 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4375 else
4376 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4377 }
4378
4379 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004380 current_speed = SPEED_UNKNOWN;
4381 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004382 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004383 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004385 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004386 err = tg3_phy_auxctl_read(tp,
4387 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4388 &val);
4389 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004390 tg3_phy_auxctl_write(tp,
4391 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4392 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 goto relink;
4394 }
4395 }
4396
4397 bmsr = 0;
4398 for (i = 0; i < 100; i++) {
4399 tg3_readphy(tp, MII_BMSR, &bmsr);
4400 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4401 (bmsr & BMSR_LSTATUS))
4402 break;
4403 udelay(40);
4404 }
4405
4406 if (bmsr & BMSR_LSTATUS) {
4407 u32 aux_stat, bmcr;
4408
4409 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4410 for (i = 0; i < 2000; i++) {
4411 udelay(10);
4412 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4413 aux_stat)
4414 break;
4415 }
4416
4417 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4418 &current_speed,
4419 &current_duplex);
4420
4421 bmcr = 0;
4422 for (i = 0; i < 200; i++) {
4423 tg3_readphy(tp, MII_BMCR, &bmcr);
4424 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4425 continue;
4426 if (bmcr && bmcr != 0x7fff)
4427 break;
4428 udelay(10);
4429 }
4430
Matt Carlsonef167e22007-12-20 20:10:01 -08004431 lcl_adv = 0;
4432 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Matt Carlsonef167e22007-12-20 20:10:01 -08004434 tp->link_config.active_speed = current_speed;
4435 tp->link_config.active_duplex = current_duplex;
4436
4437 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4438 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004439 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004440 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004441 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 } else {
4443 if (!(bmcr & BMCR_ANENABLE) &&
4444 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004445 tp->link_config.duplex == current_duplex &&
4446 tp->link_config.flowctrl ==
4447 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 }
4450 }
4451
Matt Carlsonef167e22007-12-20 20:10:01 -08004452 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004453 tp->link_config.active_duplex == DUPLEX_FULL) {
4454 u32 reg, bit;
4455
4456 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4457 reg = MII_TG3_FET_GEN_STAT;
4458 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4459 } else {
4460 reg = MII_TG3_EXT_STAT;
4461 bit = MII_TG3_EXT_STAT_MDIX;
4462 }
4463
4464 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4465 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4466
Matt Carlsonef167e22007-12-20 20:10:01 -08004467 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 }
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471relink:
Matt Carlson80096062010-08-02 11:26:06 +00004472 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 tg3_phy_copper_begin(tp);
4474
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004475 if (tg3_flag(tp, ROBOSWITCH)) {
4476 current_link_up = 1;
4477 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4478 current_speed = SPEED_1000;
4479 current_duplex = DUPLEX_FULL;
4480 tp->link_config.active_speed = current_speed;
4481 tp->link_config.active_duplex = current_duplex;
4482 }
4483
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004484 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004485 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4486 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 current_link_up = 1;
4488 }
4489
4490 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4491 if (current_link_up == 1) {
4492 if (tp->link_config.active_speed == SPEED_100 ||
4493 tp->link_config.active_speed == SPEED_10)
4494 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4495 else
4496 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004497 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004498 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4499 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4501
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004502 /* In order for the 5750 core in BCM4785 chip to work properly
4503 * in RGMII mode, the Led Control Register must be set up.
4504 */
4505 if (tg3_flag(tp, RGMII_MODE)) {
4506 u32 led_ctrl = tr32(MAC_LED_CTRL);
4507 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
4508
4509 if (tp->link_config.active_speed == SPEED_10)
4510 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
4511 else if (tp->link_config.active_speed == SPEED_100)
4512 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4513 LED_CTRL_100MBPS_ON);
4514 else if (tp->link_config.active_speed == SPEED_1000)
4515 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4516 LED_CTRL_1000MBPS_ON);
4517
4518 tw32(MAC_LED_CTRL, led_ctrl);
4519 udelay(40);
4520 }
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4523 if (tp->link_config.active_duplex == DUPLEX_HALF)
4524 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4525
Joe Perches41535772013-02-16 11:20:04 +00004526 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004527 if (current_link_up == 1 &&
4528 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004530 else
4531 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 }
4533
4534 /* ??? Without this setting Netgear GA302T PHY does not
4535 * ??? send/receive packets...
4536 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004537 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00004538 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4540 tw32_f(MAC_MI_MODE, tp->mi_mode);
4541 udelay(80);
4542 }
4543
4544 tw32_f(MAC_MODE, tp->mac_mode);
4545 udelay(40);
4546
Matt Carlson52b02d02010-10-14 10:37:41 +00004547 tg3_phy_eee_adjust(tp, current_link_up);
4548
Joe Perches63c3a662011-04-26 08:12:10 +00004549 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 /* Polled via timer. */
4551 tw32_f(MAC_EVENT, 0);
4552 } else {
4553 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4554 }
4555 udelay(40);
4556
Joe Perches41535772013-02-16 11:20:04 +00004557 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 current_link_up == 1 &&
4559 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004560 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 udelay(120);
4562 tw32_f(MAC_STATUS,
4563 (MAC_STATUS_SYNC_CHANGED |
4564 MAC_STATUS_CFG_CHANGED));
4565 udelay(40);
4566 tg3_write_mem(tp,
4567 NIC_SRAM_FIRMWARE_MBOX,
4568 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4569 }
4570
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004571 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004572 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004573 if (tp->link_config.active_speed == SPEED_100 ||
4574 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004575 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4576 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004577 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004578 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4579 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004580 }
4581
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004582 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
4584 return 0;
4585}
4586
4587struct tg3_fiber_aneginfo {
4588 int state;
4589#define ANEG_STATE_UNKNOWN 0
4590#define ANEG_STATE_AN_ENABLE 1
4591#define ANEG_STATE_RESTART_INIT 2
4592#define ANEG_STATE_RESTART 3
4593#define ANEG_STATE_DISABLE_LINK_OK 4
4594#define ANEG_STATE_ABILITY_DETECT_INIT 5
4595#define ANEG_STATE_ABILITY_DETECT 6
4596#define ANEG_STATE_ACK_DETECT_INIT 7
4597#define ANEG_STATE_ACK_DETECT 8
4598#define ANEG_STATE_COMPLETE_ACK_INIT 9
4599#define ANEG_STATE_COMPLETE_ACK 10
4600#define ANEG_STATE_IDLE_DETECT_INIT 11
4601#define ANEG_STATE_IDLE_DETECT 12
4602#define ANEG_STATE_LINK_OK 13
4603#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4604#define ANEG_STATE_NEXT_PAGE_WAIT 15
4605
4606 u32 flags;
4607#define MR_AN_ENABLE 0x00000001
4608#define MR_RESTART_AN 0x00000002
4609#define MR_AN_COMPLETE 0x00000004
4610#define MR_PAGE_RX 0x00000008
4611#define MR_NP_LOADED 0x00000010
4612#define MR_TOGGLE_TX 0x00000020
4613#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4614#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4615#define MR_LP_ADV_SYM_PAUSE 0x00000100
4616#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4617#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4618#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4619#define MR_LP_ADV_NEXT_PAGE 0x00001000
4620#define MR_TOGGLE_RX 0x00002000
4621#define MR_NP_RX 0x00004000
4622
4623#define MR_LINK_OK 0x80000000
4624
4625 unsigned long link_time, cur_time;
4626
4627 u32 ability_match_cfg;
4628 int ability_match_count;
4629
4630 char ability_match, idle_match, ack_match;
4631
4632 u32 txconfig, rxconfig;
4633#define ANEG_CFG_NP 0x00000080
4634#define ANEG_CFG_ACK 0x00000040
4635#define ANEG_CFG_RF2 0x00000020
4636#define ANEG_CFG_RF1 0x00000010
4637#define ANEG_CFG_PS2 0x00000001
4638#define ANEG_CFG_PS1 0x00008000
4639#define ANEG_CFG_HD 0x00004000
4640#define ANEG_CFG_FD 0x00002000
4641#define ANEG_CFG_INVAL 0x00001f06
4642
4643};
4644#define ANEG_OK 0
4645#define ANEG_DONE 1
4646#define ANEG_TIMER_ENAB 2
4647#define ANEG_FAILED -1
4648
4649#define ANEG_STATE_SETTLE_TIME 10000
4650
4651static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4652 struct tg3_fiber_aneginfo *ap)
4653{
Matt Carlson5be73b42007-12-20 20:09:29 -08004654 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 unsigned long delta;
4656 u32 rx_cfg_reg;
4657 int ret;
4658
4659 if (ap->state == ANEG_STATE_UNKNOWN) {
4660 ap->rxconfig = 0;
4661 ap->link_time = 0;
4662 ap->cur_time = 0;
4663 ap->ability_match_cfg = 0;
4664 ap->ability_match_count = 0;
4665 ap->ability_match = 0;
4666 ap->idle_match = 0;
4667 ap->ack_match = 0;
4668 }
4669 ap->cur_time++;
4670
4671 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4672 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4673
4674 if (rx_cfg_reg != ap->ability_match_cfg) {
4675 ap->ability_match_cfg = rx_cfg_reg;
4676 ap->ability_match = 0;
4677 ap->ability_match_count = 0;
4678 } else {
4679 if (++ap->ability_match_count > 1) {
4680 ap->ability_match = 1;
4681 ap->ability_match_cfg = rx_cfg_reg;
4682 }
4683 }
4684 if (rx_cfg_reg & ANEG_CFG_ACK)
4685 ap->ack_match = 1;
4686 else
4687 ap->ack_match = 0;
4688
4689 ap->idle_match = 0;
4690 } else {
4691 ap->idle_match = 1;
4692 ap->ability_match_cfg = 0;
4693 ap->ability_match_count = 0;
4694 ap->ability_match = 0;
4695 ap->ack_match = 0;
4696
4697 rx_cfg_reg = 0;
4698 }
4699
4700 ap->rxconfig = rx_cfg_reg;
4701 ret = ANEG_OK;
4702
Matt Carlson33f401a2010-04-05 10:19:27 +00004703 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 case ANEG_STATE_UNKNOWN:
4705 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4706 ap->state = ANEG_STATE_AN_ENABLE;
4707
4708 /* fallthru */
4709 case ANEG_STATE_AN_ENABLE:
4710 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4711 if (ap->flags & MR_AN_ENABLE) {
4712 ap->link_time = 0;
4713 ap->cur_time = 0;
4714 ap->ability_match_cfg = 0;
4715 ap->ability_match_count = 0;
4716 ap->ability_match = 0;
4717 ap->idle_match = 0;
4718 ap->ack_match = 0;
4719
4720 ap->state = ANEG_STATE_RESTART_INIT;
4721 } else {
4722 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4723 }
4724 break;
4725
4726 case ANEG_STATE_RESTART_INIT:
4727 ap->link_time = ap->cur_time;
4728 ap->flags &= ~(MR_NP_LOADED);
4729 ap->txconfig = 0;
4730 tw32(MAC_TX_AUTO_NEG, 0);
4731 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4732 tw32_f(MAC_MODE, tp->mac_mode);
4733 udelay(40);
4734
4735 ret = ANEG_TIMER_ENAB;
4736 ap->state = ANEG_STATE_RESTART;
4737
4738 /* fallthru */
4739 case ANEG_STATE_RESTART:
4740 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004741 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004743 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 break;
4746
4747 case ANEG_STATE_DISABLE_LINK_OK:
4748 ret = ANEG_DONE;
4749 break;
4750
4751 case ANEG_STATE_ABILITY_DETECT_INIT:
4752 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004753 ap->txconfig = ANEG_CFG_FD;
4754 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4755 if (flowctrl & ADVERTISE_1000XPAUSE)
4756 ap->txconfig |= ANEG_CFG_PS1;
4757 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4758 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4760 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4761 tw32_f(MAC_MODE, tp->mac_mode);
4762 udelay(40);
4763
4764 ap->state = ANEG_STATE_ABILITY_DETECT;
4765 break;
4766
4767 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004768 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 break;
4771
4772 case ANEG_STATE_ACK_DETECT_INIT:
4773 ap->txconfig |= ANEG_CFG_ACK;
4774 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4775 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4776 tw32_f(MAC_MODE, tp->mac_mode);
4777 udelay(40);
4778
4779 ap->state = ANEG_STATE_ACK_DETECT;
4780
4781 /* fallthru */
4782 case ANEG_STATE_ACK_DETECT:
4783 if (ap->ack_match != 0) {
4784 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4785 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4786 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4787 } else {
4788 ap->state = ANEG_STATE_AN_ENABLE;
4789 }
4790 } else if (ap->ability_match != 0 &&
4791 ap->rxconfig == 0) {
4792 ap->state = ANEG_STATE_AN_ENABLE;
4793 }
4794 break;
4795
4796 case ANEG_STATE_COMPLETE_ACK_INIT:
4797 if (ap->rxconfig & ANEG_CFG_INVAL) {
4798 ret = ANEG_FAILED;
4799 break;
4800 }
4801 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4802 MR_LP_ADV_HALF_DUPLEX |
4803 MR_LP_ADV_SYM_PAUSE |
4804 MR_LP_ADV_ASYM_PAUSE |
4805 MR_LP_ADV_REMOTE_FAULT1 |
4806 MR_LP_ADV_REMOTE_FAULT2 |
4807 MR_LP_ADV_NEXT_PAGE |
4808 MR_TOGGLE_RX |
4809 MR_NP_RX);
4810 if (ap->rxconfig & ANEG_CFG_FD)
4811 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4812 if (ap->rxconfig & ANEG_CFG_HD)
4813 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4814 if (ap->rxconfig & ANEG_CFG_PS1)
4815 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4816 if (ap->rxconfig & ANEG_CFG_PS2)
4817 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4818 if (ap->rxconfig & ANEG_CFG_RF1)
4819 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4820 if (ap->rxconfig & ANEG_CFG_RF2)
4821 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4822 if (ap->rxconfig & ANEG_CFG_NP)
4823 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4824
4825 ap->link_time = ap->cur_time;
4826
4827 ap->flags ^= (MR_TOGGLE_TX);
4828 if (ap->rxconfig & 0x0008)
4829 ap->flags |= MR_TOGGLE_RX;
4830 if (ap->rxconfig & ANEG_CFG_NP)
4831 ap->flags |= MR_NP_RX;
4832 ap->flags |= MR_PAGE_RX;
4833
4834 ap->state = ANEG_STATE_COMPLETE_ACK;
4835 ret = ANEG_TIMER_ENAB;
4836 break;
4837
4838 case ANEG_STATE_COMPLETE_ACK:
4839 if (ap->ability_match != 0 &&
4840 ap->rxconfig == 0) {
4841 ap->state = ANEG_STATE_AN_ENABLE;
4842 break;
4843 }
4844 delta = ap->cur_time - ap->link_time;
4845 if (delta > ANEG_STATE_SETTLE_TIME) {
4846 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4847 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4848 } else {
4849 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4850 !(ap->flags & MR_NP_RX)) {
4851 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4852 } else {
4853 ret = ANEG_FAILED;
4854 }
4855 }
4856 }
4857 break;
4858
4859 case ANEG_STATE_IDLE_DETECT_INIT:
4860 ap->link_time = ap->cur_time;
4861 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4862 tw32_f(MAC_MODE, tp->mac_mode);
4863 udelay(40);
4864
4865 ap->state = ANEG_STATE_IDLE_DETECT;
4866 ret = ANEG_TIMER_ENAB;
4867 break;
4868
4869 case ANEG_STATE_IDLE_DETECT:
4870 if (ap->ability_match != 0 &&
4871 ap->rxconfig == 0) {
4872 ap->state = ANEG_STATE_AN_ENABLE;
4873 break;
4874 }
4875 delta = ap->cur_time - ap->link_time;
4876 if (delta > ANEG_STATE_SETTLE_TIME) {
4877 /* XXX another gem from the Broadcom driver :( */
4878 ap->state = ANEG_STATE_LINK_OK;
4879 }
4880 break;
4881
4882 case ANEG_STATE_LINK_OK:
4883 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4884 ret = ANEG_DONE;
4885 break;
4886
4887 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4888 /* ??? unimplemented */
4889 break;
4890
4891 case ANEG_STATE_NEXT_PAGE_WAIT:
4892 /* ??? unimplemented */
4893 break;
4894
4895 default:
4896 ret = ANEG_FAILED;
4897 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
4900 return ret;
4901}
4902
Matt Carlson5be73b42007-12-20 20:09:29 -08004903static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904{
4905 int res = 0;
4906 struct tg3_fiber_aneginfo aninfo;
4907 int status = ANEG_FAILED;
4908 unsigned int tick;
4909 u32 tmp;
4910
4911 tw32_f(MAC_TX_AUTO_NEG, 0);
4912
4913 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4914 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4915 udelay(40);
4916
4917 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4918 udelay(40);
4919
4920 memset(&aninfo, 0, sizeof(aninfo));
4921 aninfo.flags |= MR_AN_ENABLE;
4922 aninfo.state = ANEG_STATE_UNKNOWN;
4923 aninfo.cur_time = 0;
4924 tick = 0;
4925 while (++tick < 195000) {
4926 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4927 if (status == ANEG_DONE || status == ANEG_FAILED)
4928 break;
4929
4930 udelay(1);
4931 }
4932
4933 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4934 tw32_f(MAC_MODE, tp->mac_mode);
4935 udelay(40);
4936
Matt Carlson5be73b42007-12-20 20:09:29 -08004937 *txflags = aninfo.txconfig;
4938 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
4940 if (status == ANEG_DONE &&
4941 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4942 MR_LP_ADV_FULL_DUPLEX)))
4943 res = 1;
4944
4945 return res;
4946}
4947
4948static void tg3_init_bcm8002(struct tg3 *tp)
4949{
4950 u32 mac_status = tr32(MAC_STATUS);
4951 int i;
4952
4953 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004954 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 !(mac_status & MAC_STATUS_PCS_SYNCED))
4956 return;
4957
4958 /* Set PLL lock range. */
4959 tg3_writephy(tp, 0x16, 0x8007);
4960
4961 /* SW reset */
4962 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4963
4964 /* Wait for reset to complete. */
4965 /* XXX schedule_timeout() ... */
4966 for (i = 0; i < 500; i++)
4967 udelay(10);
4968
4969 /* Config mode; select PMA/Ch 1 regs. */
4970 tg3_writephy(tp, 0x10, 0x8411);
4971
4972 /* Enable auto-lock and comdet, select txclk for tx. */
4973 tg3_writephy(tp, 0x11, 0x0a10);
4974
4975 tg3_writephy(tp, 0x18, 0x00a0);
4976 tg3_writephy(tp, 0x16, 0x41ff);
4977
4978 /* Assert and deassert POR. */
4979 tg3_writephy(tp, 0x13, 0x0400);
4980 udelay(40);
4981 tg3_writephy(tp, 0x13, 0x0000);
4982
4983 tg3_writephy(tp, 0x11, 0x0a50);
4984 udelay(40);
4985 tg3_writephy(tp, 0x11, 0x0a10);
4986
4987 /* Wait for signal to stabilize */
4988 /* XXX schedule_timeout() ... */
4989 for (i = 0; i < 15000; i++)
4990 udelay(10);
4991
4992 /* Deselect the channel register so we can read the PHYID
4993 * later.
4994 */
4995 tg3_writephy(tp, 0x10, 0x8011);
4996}
4997
4998static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4999{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005000 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 u32 sg_dig_ctrl, sg_dig_status;
5002 u32 serdes_cfg, expected_sg_dig_ctrl;
5003 int workaround, port_a;
5004 int current_link_up;
5005
5006 serdes_cfg = 0;
5007 expected_sg_dig_ctrl = 0;
5008 workaround = 0;
5009 port_a = 1;
5010 current_link_up = 0;
5011
Joe Perches41535772013-02-16 11:20:04 +00005012 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5013 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 workaround = 1;
5015 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5016 port_a = 0;
5017
5018 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5019 /* preserve bits 20-23 for voltage regulator */
5020 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5021 }
5022
5023 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5024
5025 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005026 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 if (workaround) {
5028 u32 val = serdes_cfg;
5029
5030 if (port_a)
5031 val |= 0xc010000;
5032 else
5033 val |= 0x4010000;
5034 tw32_f(MAC_SERDES_CFG, val);
5035 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005036
5037 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 }
5039 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5040 tg3_setup_flow_control(tp, 0, 0);
5041 current_link_up = 1;
5042 }
5043 goto out;
5044 }
5045
5046 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005047 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
Matt Carlson82cd3d12007-12-20 20:09:00 -08005049 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5050 if (flowctrl & ADVERTISE_1000XPAUSE)
5051 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5052 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5053 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
5055 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005056 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005057 tp->serdes_counter &&
5058 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5059 MAC_STATUS_RCVD_CFG)) ==
5060 MAC_STATUS_PCS_SYNCED)) {
5061 tp->serdes_counter--;
5062 current_link_up = 1;
5063 goto out;
5064 }
5065restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 if (workaround)
5067 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005068 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 udelay(5);
5070 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5071
Michael Chan3d3ebe72006-09-27 15:59:15 -07005072 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005073 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5075 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005076 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 mac_status = tr32(MAC_STATUS);
5078
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005079 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005081 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
Matt Carlson82cd3d12007-12-20 20:09:00 -08005083 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5084 local_adv |= ADVERTISE_1000XPAUSE;
5085 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5086 local_adv |= ADVERTISE_1000XPSE_ASYM;
5087
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005088 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005089 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005090 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005091 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
Matt Carlson859edb22011-12-08 14:40:16 +00005093 tp->link_config.rmt_adv =
5094 mii_adv_to_ethtool_adv_x(remote_adv);
5095
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 tg3_setup_flow_control(tp, local_adv, remote_adv);
5097 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005098 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005099 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005100 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005101 if (tp->serdes_counter)
5102 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 else {
5104 if (workaround) {
5105 u32 val = serdes_cfg;
5106
5107 if (port_a)
5108 val |= 0xc010000;
5109 else
5110 val |= 0x4010000;
5111
5112 tw32_f(MAC_SERDES_CFG, val);
5113 }
5114
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005115 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 udelay(40);
5117
5118 /* Link parallel detection - link is up */
5119 /* only if we have PCS_SYNC and not */
5120 /* receiving config code words */
5121 mac_status = tr32(MAC_STATUS);
5122 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5123 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5124 tg3_setup_flow_control(tp, 0, 0);
5125 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005126 tp->phy_flags |=
5127 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005128 tp->serdes_counter =
5129 SERDES_PARALLEL_DET_TIMEOUT;
5130 } else
5131 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 }
5133 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005134 } else {
5135 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005136 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 }
5138
5139out:
5140 return current_link_up;
5141}
5142
5143static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
5144{
5145 int current_link_up = 0;
5146
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005147 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
5150 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005151 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005153
Matt Carlson5be73b42007-12-20 20:09:29 -08005154 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5155 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
Matt Carlson5be73b42007-12-20 20:09:29 -08005157 if (txflags & ANEG_CFG_PS1)
5158 local_adv |= ADVERTISE_1000XPAUSE;
5159 if (txflags & ANEG_CFG_PS2)
5160 local_adv |= ADVERTISE_1000XPSE_ASYM;
5161
5162 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5163 remote_adv |= LPA_1000XPAUSE;
5164 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5165 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
Matt Carlson859edb22011-12-08 14:40:16 +00005167 tp->link_config.rmt_adv =
5168 mii_adv_to_ethtool_adv_x(remote_adv);
5169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 tg3_setup_flow_control(tp, local_adv, remote_adv);
5171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 current_link_up = 1;
5173 }
5174 for (i = 0; i < 30; i++) {
5175 udelay(20);
5176 tw32_f(MAC_STATUS,
5177 (MAC_STATUS_SYNC_CHANGED |
5178 MAC_STATUS_CFG_CHANGED));
5179 udelay(40);
5180 if ((tr32(MAC_STATUS) &
5181 (MAC_STATUS_SYNC_CHANGED |
5182 MAC_STATUS_CFG_CHANGED)) == 0)
5183 break;
5184 }
5185
5186 mac_status = tr32(MAC_STATUS);
5187 if (current_link_up == 0 &&
5188 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5189 !(mac_status & MAC_STATUS_RCVD_CFG))
5190 current_link_up = 1;
5191 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005192 tg3_setup_flow_control(tp, 0, 0);
5193
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 /* Forcing 1000FD link up. */
5195 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
5197 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5198 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005199
5200 tw32_f(MAC_MODE, tp->mac_mode);
5201 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 }
5203
5204out:
5205 return current_link_up;
5206}
5207
5208static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
5209{
5210 u32 orig_pause_cfg;
5211 u16 orig_active_speed;
5212 u8 orig_active_duplex;
5213 u32 mac_status;
5214 int current_link_up;
5215 int i;
5216
Matt Carlson8d018622007-12-20 20:05:44 -08005217 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 orig_active_speed = tp->link_config.active_speed;
5219 orig_active_duplex = tp->link_config.active_duplex;
5220
Joe Perches63c3a662011-04-26 08:12:10 +00005221 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005222 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005223 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 mac_status = tr32(MAC_STATUS);
5225 mac_status &= (MAC_STATUS_PCS_SYNCED |
5226 MAC_STATUS_SIGNAL_DET |
5227 MAC_STATUS_CFG_CHANGED |
5228 MAC_STATUS_RCVD_CFG);
5229 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5230 MAC_STATUS_SIGNAL_DET)) {
5231 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5232 MAC_STATUS_CFG_CHANGED));
5233 return 0;
5234 }
5235 }
5236
5237 tw32_f(MAC_TX_AUTO_NEG, 0);
5238
5239 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5240 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5241 tw32_f(MAC_MODE, tp->mac_mode);
5242 udelay(40);
5243
Matt Carlson79eb6902010-02-17 15:17:03 +00005244 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 tg3_init_bcm8002(tp);
5246
5247 /* Enable link change event even when serdes polling. */
5248 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5249 udelay(40);
5250
5251 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005252 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 mac_status = tr32(MAC_STATUS);
5254
Joe Perches63c3a662011-04-26 08:12:10 +00005255 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5257 else
5258 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5259
Matt Carlson898a56f2009-08-28 14:02:40 +00005260 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005262 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
5264 for (i = 0; i < 100; i++) {
5265 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5266 MAC_STATUS_CFG_CHANGED));
5267 udelay(5);
5268 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005269 MAC_STATUS_CFG_CHANGED |
5270 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 break;
5272 }
5273
5274 mac_status = tr32(MAC_STATUS);
5275 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5276 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005277 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5278 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 tw32_f(MAC_MODE, (tp->mac_mode |
5280 MAC_MODE_SEND_CONFIGS));
5281 udelay(1);
5282 tw32_f(MAC_MODE, tp->mac_mode);
5283 }
5284 }
5285
5286 if (current_link_up == 1) {
5287 tp->link_config.active_speed = SPEED_1000;
5288 tp->link_config.active_duplex = DUPLEX_FULL;
5289 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5290 LED_CTRL_LNKLED_OVERRIDE |
5291 LED_CTRL_1000MBPS_ON));
5292 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005293 tp->link_config.active_speed = SPEED_UNKNOWN;
5294 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5296 LED_CTRL_LNKLED_OVERRIDE |
5297 LED_CTRL_TRAFFIC_OVERRIDE));
5298 }
5299
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005300 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005301 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 if (orig_pause_cfg != now_pause_cfg ||
5303 orig_active_speed != tp->link_config.active_speed ||
5304 orig_active_duplex != tp->link_config.active_duplex)
5305 tg3_link_report(tp);
5306 }
5307
5308 return 0;
5309}
5310
Michael Chan747e8f82005-07-25 12:33:22 -07005311static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5312{
5313 int current_link_up, err = 0;
5314 u32 bmsr, bmcr;
5315 u16 current_speed;
5316 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005317 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005318
5319 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5320 tw32_f(MAC_MODE, tp->mac_mode);
5321 udelay(40);
5322
5323 tw32(MAC_EVENT, 0);
5324
5325 tw32_f(MAC_STATUS,
5326 (MAC_STATUS_SYNC_CHANGED |
5327 MAC_STATUS_CFG_CHANGED |
5328 MAC_STATUS_MI_COMPLETION |
5329 MAC_STATUS_LNKSTATE_CHANGED));
5330 udelay(40);
5331
5332 if (force_reset)
5333 tg3_phy_reset(tp);
5334
5335 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005336 current_speed = SPEED_UNKNOWN;
5337 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005338 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005339
5340 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5341 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005342 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005343 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5344 bmsr |= BMSR_LSTATUS;
5345 else
5346 bmsr &= ~BMSR_LSTATUS;
5347 }
Michael Chan747e8f82005-07-25 12:33:22 -07005348
5349 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5350
5351 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005352 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005353 /* do nothing, just check for link up at the end */
5354 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005355 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005356
5357 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005358 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5359 ADVERTISE_1000XPAUSE |
5360 ADVERTISE_1000XPSE_ASYM |
5361 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005362
Matt Carlson28011cf2011-11-16 18:36:59 -05005363 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005364 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005365
Matt Carlson28011cf2011-11-16 18:36:59 -05005366 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5367 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005368 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5369 tg3_writephy(tp, MII_BMCR, bmcr);
5370
5371 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005372 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005373 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005374
5375 return err;
5376 }
5377 } else {
5378 u32 new_bmcr;
5379
5380 bmcr &= ~BMCR_SPEED1000;
5381 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5382
5383 if (tp->link_config.duplex == DUPLEX_FULL)
5384 new_bmcr |= BMCR_FULLDPLX;
5385
5386 if (new_bmcr != bmcr) {
5387 /* BMCR_SPEED1000 is a reserved bit that needs
5388 * to be set on write.
5389 */
5390 new_bmcr |= BMCR_SPEED1000;
5391
5392 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005393 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005394 u32 adv;
5395
5396 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5397 adv &= ~(ADVERTISE_1000XFULL |
5398 ADVERTISE_1000XHALF |
5399 ADVERTISE_SLCT);
5400 tg3_writephy(tp, MII_ADVERTISE, adv);
5401 tg3_writephy(tp, MII_BMCR, bmcr |
5402 BMCR_ANRESTART |
5403 BMCR_ANENABLE);
5404 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005405 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005406 }
5407 tg3_writephy(tp, MII_BMCR, new_bmcr);
5408 bmcr = new_bmcr;
5409 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5410 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005411 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005412 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5413 bmsr |= BMSR_LSTATUS;
5414 else
5415 bmsr &= ~BMSR_LSTATUS;
5416 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005417 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005418 }
5419 }
5420
5421 if (bmsr & BMSR_LSTATUS) {
5422 current_speed = SPEED_1000;
5423 current_link_up = 1;
5424 if (bmcr & BMCR_FULLDPLX)
5425 current_duplex = DUPLEX_FULL;
5426 else
5427 current_duplex = DUPLEX_HALF;
5428
Matt Carlsonef167e22007-12-20 20:10:01 -08005429 local_adv = 0;
5430 remote_adv = 0;
5431
Michael Chan747e8f82005-07-25 12:33:22 -07005432 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005433 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005434
5435 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5436 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5437 common = local_adv & remote_adv;
5438 if (common & (ADVERTISE_1000XHALF |
5439 ADVERTISE_1000XFULL)) {
5440 if (common & ADVERTISE_1000XFULL)
5441 current_duplex = DUPLEX_FULL;
5442 else
5443 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005444
5445 tp->link_config.rmt_adv =
5446 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005447 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005448 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005449 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005450 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005451 }
Michael Chan747e8f82005-07-25 12:33:22 -07005452 }
5453 }
5454
Matt Carlsonef167e22007-12-20 20:10:01 -08005455 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5456 tg3_setup_flow_control(tp, local_adv, remote_adv);
5457
Michael Chan747e8f82005-07-25 12:33:22 -07005458 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5459 if (tp->link_config.active_duplex == DUPLEX_HALF)
5460 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5461
5462 tw32_f(MAC_MODE, tp->mac_mode);
5463 udelay(40);
5464
5465 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5466
5467 tp->link_config.active_speed = current_speed;
5468 tp->link_config.active_duplex = current_duplex;
5469
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005470 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005471 return err;
5472}
5473
5474static void tg3_serdes_parallel_detect(struct tg3 *tp)
5475{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005476 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005477 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005478 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005479 return;
5480 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005481
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005482 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005483 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5484 u32 bmcr;
5485
5486 tg3_readphy(tp, MII_BMCR, &bmcr);
5487 if (bmcr & BMCR_ANENABLE) {
5488 u32 phy1, phy2;
5489
5490 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005491 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5492 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005493
5494 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005495 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5496 MII_TG3_DSP_EXP1_INT_STAT);
5497 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5498 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005499
5500 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5501 /* We have signal detect and not receiving
5502 * config code words, link is up by parallel
5503 * detection.
5504 */
5505
5506 bmcr &= ~BMCR_ANENABLE;
5507 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5508 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005509 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005510 }
5511 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005512 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005513 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005514 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005515 u32 phy2;
5516
5517 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005518 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5519 MII_TG3_DSP_EXP1_INT_STAT);
5520 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005521 if (phy2 & 0x20) {
5522 u32 bmcr;
5523
5524 /* Config code words received, turn on autoneg. */
5525 tg3_readphy(tp, MII_BMCR, &bmcr);
5526 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5527
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005528 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005529
5530 }
5531 }
5532}
5533
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5535{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005536 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 int err;
5538
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005539 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005541 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005542 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005543 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545
Joe Perches41535772013-02-16 11:20:04 +00005546 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005547 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005548
5549 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5550 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5551 scale = 65;
5552 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5553 scale = 6;
5554 else
5555 scale = 12;
5556
5557 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5558 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5559 tw32(GRC_MISC_CFG, val);
5560 }
5561
Matt Carlsonf2096f92011-04-05 14:22:48 +00005562 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5563 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00005564 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
5565 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005566 val |= tr32(MAC_TX_LENGTHS) &
5567 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5568 TX_LENGTHS_CNT_DWN_VAL_MSK);
5569
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 if (tp->link_config.active_speed == SPEED_1000 &&
5571 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005572 tw32(MAC_TX_LENGTHS, val |
5573 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005575 tw32(MAC_TX_LENGTHS, val |
5576 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577
Joe Perches63c3a662011-04-26 08:12:10 +00005578 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005579 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005581 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 } else {
5583 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5584 }
5585 }
5586
Joe Perches63c3a662011-04-26 08:12:10 +00005587 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005588 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005589 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07005590 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5591 tp->pwrmgmt_thresh;
5592 else
5593 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5594 tw32(PCIE_PWR_MGMT_THRESH, val);
5595 }
5596
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 return err;
5598}
5599
Matt Carlsonbe947302012-12-03 19:36:57 +00005600/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00005601static u64 tg3_refclk_read(struct tg3 *tp)
5602{
5603 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
5604 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
5605}
5606
5607/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00005608static void tg3_refclk_write(struct tg3 *tp, u64 newval)
5609{
5610 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
5611 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
5612 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
5613 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
5614}
5615
Matt Carlson7d41e492012-12-03 19:36:58 +00005616static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
5617static inline void tg3_full_unlock(struct tg3 *tp);
5618static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
5619{
5620 struct tg3 *tp = netdev_priv(dev);
5621
5622 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
5623 SOF_TIMESTAMPING_RX_SOFTWARE |
5624 SOF_TIMESTAMPING_SOFTWARE |
5625 SOF_TIMESTAMPING_TX_HARDWARE |
5626 SOF_TIMESTAMPING_RX_HARDWARE |
5627 SOF_TIMESTAMPING_RAW_HARDWARE;
5628
5629 if (tp->ptp_clock)
5630 info->phc_index = ptp_clock_index(tp->ptp_clock);
5631 else
5632 info->phc_index = -1;
5633
5634 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
5635
5636 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
5637 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
5638 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
5639 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
5640 return 0;
5641}
5642
5643static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
5644{
5645 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5646 bool neg_adj = false;
5647 u32 correction = 0;
5648
5649 if (ppb < 0) {
5650 neg_adj = true;
5651 ppb = -ppb;
5652 }
5653
5654 /* Frequency adjustment is performed using hardware with a 24 bit
5655 * accumulator and a programmable correction value. On each clk, the
5656 * correction value gets added to the accumulator and when it
5657 * overflows, the time counter is incremented/decremented.
5658 *
5659 * So conversion from ppb to correction value is
5660 * ppb * (1 << 24) / 1000000000
5661 */
5662 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
5663 TG3_EAV_REF_CLK_CORRECT_MASK;
5664
5665 tg3_full_lock(tp, 0);
5666
5667 if (correction)
5668 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
5669 TG3_EAV_REF_CLK_CORRECT_EN |
5670 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
5671 else
5672 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
5673
5674 tg3_full_unlock(tp);
5675
5676 return 0;
5677}
5678
5679static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
5680{
5681 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5682
5683 tg3_full_lock(tp, 0);
5684 tp->ptp_adjust += delta;
5685 tg3_full_unlock(tp);
5686
5687 return 0;
5688}
5689
5690static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
5691{
5692 u64 ns;
5693 u32 remainder;
5694 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5695
5696 tg3_full_lock(tp, 0);
5697 ns = tg3_refclk_read(tp);
5698 ns += tp->ptp_adjust;
5699 tg3_full_unlock(tp);
5700
5701 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
5702 ts->tv_nsec = remainder;
5703
5704 return 0;
5705}
5706
5707static int tg3_ptp_settime(struct ptp_clock_info *ptp,
5708 const struct timespec *ts)
5709{
5710 u64 ns;
5711 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5712
5713 ns = timespec_to_ns(ts);
5714
5715 tg3_full_lock(tp, 0);
5716 tg3_refclk_write(tp, ns);
5717 tp->ptp_adjust = 0;
5718 tg3_full_unlock(tp);
5719
5720 return 0;
5721}
5722
5723static int tg3_ptp_enable(struct ptp_clock_info *ptp,
5724 struct ptp_clock_request *rq, int on)
5725{
5726 return -EOPNOTSUPP;
5727}
5728
5729static const struct ptp_clock_info tg3_ptp_caps = {
5730 .owner = THIS_MODULE,
5731 .name = "tg3 clock",
5732 .max_adj = 250000000,
5733 .n_alarm = 0,
5734 .n_ext_ts = 0,
5735 .n_per_out = 0,
5736 .pps = 0,
5737 .adjfreq = tg3_ptp_adjfreq,
5738 .adjtime = tg3_ptp_adjtime,
5739 .gettime = tg3_ptp_gettime,
5740 .settime = tg3_ptp_settime,
5741 .enable = tg3_ptp_enable,
5742};
5743
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005744static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
5745 struct skb_shared_hwtstamps *timestamp)
5746{
5747 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
5748 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
5749 tp->ptp_adjust);
5750}
5751
Matt Carlsonbe947302012-12-03 19:36:57 +00005752/* tp->lock must be held */
5753static void tg3_ptp_init(struct tg3 *tp)
5754{
5755 if (!tg3_flag(tp, PTP_CAPABLE))
5756 return;
5757
5758 /* Initialize the hardware clock to the system time. */
5759 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
5760 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00005761 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00005762}
5763
5764/* tp->lock must be held */
5765static void tg3_ptp_resume(struct tg3 *tp)
5766{
5767 if (!tg3_flag(tp, PTP_CAPABLE))
5768 return;
5769
5770 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
5771 tp->ptp_adjust = 0;
5772}
5773
5774static void tg3_ptp_fini(struct tg3 *tp)
5775{
5776 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
5777 return;
5778
Matt Carlson7d41e492012-12-03 19:36:58 +00005779 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00005780 tp->ptp_clock = NULL;
5781 tp->ptp_adjust = 0;
5782}
5783
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005784static inline int tg3_irq_sync(struct tg3 *tp)
5785{
5786 return tp->irq_sync;
5787}
5788
Matt Carlson97bd8e42011-04-13 11:05:04 +00005789static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5790{
5791 int i;
5792
5793 dst = (u32 *)((u8 *)dst + off);
5794 for (i = 0; i < len; i += sizeof(u32))
5795 *dst++ = tr32(off + i);
5796}
5797
5798static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5799{
5800 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5801 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5802 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5803 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5804 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5805 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5806 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5807 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5808 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5809 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5810 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5811 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5812 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5813 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5814 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5815 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5816 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5817 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5818 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5819
Joe Perches63c3a662011-04-26 08:12:10 +00005820 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005821 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5822
5823 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5824 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5825 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5826 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5827 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5828 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5829 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5830 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5831
Joe Perches63c3a662011-04-26 08:12:10 +00005832 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005833 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5834 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5835 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5836 }
5837
5838 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5839 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5840 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5841 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5842 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5843
Joe Perches63c3a662011-04-26 08:12:10 +00005844 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005845 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5846}
5847
5848static void tg3_dump_state(struct tg3 *tp)
5849{
5850 int i;
5851 u32 *regs;
5852
5853 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00005854 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00005855 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00005856
Joe Perches63c3a662011-04-26 08:12:10 +00005857 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005858 /* Read up to but not including private PCI registers */
5859 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5860 regs[i / sizeof(u32)] = tr32(i);
5861 } else
5862 tg3_dump_legacy_regs(tp, regs);
5863
5864 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5865 if (!regs[i + 0] && !regs[i + 1] &&
5866 !regs[i + 2] && !regs[i + 3])
5867 continue;
5868
5869 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5870 i * 4,
5871 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5872 }
5873
5874 kfree(regs);
5875
5876 for (i = 0; i < tp->irq_cnt; i++) {
5877 struct tg3_napi *tnapi = &tp->napi[i];
5878
5879 /* SW status block */
5880 netdev_err(tp->dev,
5881 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5882 i,
5883 tnapi->hw_status->status,
5884 tnapi->hw_status->status_tag,
5885 tnapi->hw_status->rx_jumbo_consumer,
5886 tnapi->hw_status->rx_consumer,
5887 tnapi->hw_status->rx_mini_consumer,
5888 tnapi->hw_status->idx[0].rx_producer,
5889 tnapi->hw_status->idx[0].tx_consumer);
5890
5891 netdev_err(tp->dev,
5892 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5893 i,
5894 tnapi->last_tag, tnapi->last_irq_tag,
5895 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5896 tnapi->rx_rcb_ptr,
5897 tnapi->prodring.rx_std_prod_idx,
5898 tnapi->prodring.rx_std_cons_idx,
5899 tnapi->prodring.rx_jmb_prod_idx,
5900 tnapi->prodring.rx_jmb_cons_idx);
5901 }
5902}
5903
Michael Chandf3e6542006-05-26 17:48:07 -07005904/* This is called whenever we suspect that the system chipset is re-
5905 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5906 * is bogus tx completions. We try to recover by setting the
5907 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5908 * in the workqueue.
5909 */
5910static void tg3_tx_recover(struct tg3 *tp)
5911{
Joe Perches63c3a662011-04-26 08:12:10 +00005912 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005913 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5914
Matt Carlson5129c3a2010-04-05 10:19:23 +00005915 netdev_warn(tp->dev,
5916 "The system may be re-ordering memory-mapped I/O "
5917 "cycles to the network device, attempting to recover. "
5918 "Please report the problem to the driver maintainer "
5919 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005920
5921 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005922 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005923 spin_unlock(&tp->lock);
5924}
5925
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005926static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005927{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005928 /* Tell compiler to fetch tx indices from memory. */
5929 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005930 return tnapi->tx_pending -
5931 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005932}
5933
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934/* Tigon3 never reports partial packet sends. So we do not
5935 * need special logic to handle SKBs that have not had all
5936 * of their frags sent yet, like SunGEM does.
5937 */
Matt Carlson17375d22009-08-28 14:02:18 +00005938static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939{
Matt Carlson17375d22009-08-28 14:02:18 +00005940 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005941 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005942 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005943 struct netdev_queue *txq;
5944 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005945 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005946
Joe Perches63c3a662011-04-26 08:12:10 +00005947 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005948 index--;
5949
5950 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
5952 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005953 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005955 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956
Michael Chandf3e6542006-05-26 17:48:07 -07005957 if (unlikely(skb == NULL)) {
5958 tg3_tx_recover(tp);
5959 return;
5960 }
5961
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005962 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
5963 struct skb_shared_hwtstamps timestamp;
5964 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
5965 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
5966
5967 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
5968
5969 skb_tstamp_tx(skb, &timestamp);
5970 }
5971
Alexander Duyckf4188d82009-12-02 16:48:38 +00005972 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005973 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005974 skb_headlen(skb),
5975 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976
5977 ri->skb = NULL;
5978
Matt Carlsone01ee142011-07-27 14:20:50 +00005979 while (ri->fragmented) {
5980 ri->fragmented = false;
5981 sw_idx = NEXT_TX(sw_idx);
5982 ri = &tnapi->tx_buffers[sw_idx];
5983 }
5984
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 sw_idx = NEXT_TX(sw_idx);
5986
5987 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005988 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005989 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5990 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005991
5992 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005993 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005994 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005995 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005996
5997 while (ri->fragmented) {
5998 ri->fragmented = false;
5999 sw_idx = NEXT_TX(sw_idx);
6000 ri = &tnapi->tx_buffers[sw_idx];
6001 }
6002
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 sw_idx = NEXT_TX(sw_idx);
6004 }
6005
Tom Herbert298376d2011-11-28 16:33:30 +00006006 pkts_compl++;
6007 bytes_compl += skb->len;
6008
David S. Millerf47c11e2005-06-24 20:18:35 -07006009 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006010
6011 if (unlikely(tx_bug)) {
6012 tg3_tx_recover(tp);
6013 return;
6014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 }
6016
Tom Herbert5cb917b2012-03-05 19:53:50 +00006017 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006018
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006019 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
Michael Chan1b2a7202006-08-07 21:46:02 -07006021 /* Need to make the tx_cons update visible to tg3_start_xmit()
6022 * before checking for netif_queue_stopped(). Without the
6023 * memory barrier, there is a small possibility that tg3_start_xmit()
6024 * will miss it and cause the queue to be stopped forever.
6025 */
6026 smp_mb();
6027
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006028 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006029 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006030 __netif_tx_lock(txq, smp_processor_id());
6031 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006032 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006033 netif_tx_wake_queue(txq);
6034 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036}
6037
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006038static void tg3_frag_free(bool is_frag, void *data)
6039{
6040 if (is_frag)
6041 put_page(virt_to_head_page(data));
6042 else
6043 kfree(data);
6044}
6045
Eric Dumazet9205fd92011-11-18 06:47:01 +00006046static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006047{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006048 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6049 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6050
Eric Dumazet9205fd92011-11-18 06:47:01 +00006051 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006052 return;
6053
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006054 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006055 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006056 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006057 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006058}
6059
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006060
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061/* Returns size of skb allocated or < 0 on error.
6062 *
6063 * We only need to fill in the address because the other members
6064 * of the RX descriptor are invariant, see tg3_init_rings.
6065 *
6066 * Note the purposeful assymetry of cpu vs. chip accesses. For
6067 * posting buffers we only dirty the first cache line of the RX
6068 * descriptor (containing the address). Whereas for the RX status
6069 * buffers the cpu only reads the last cacheline of the RX descriptor
6070 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6071 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006072static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006073 u32 opaque_key, u32 dest_idx_unmasked,
6074 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075{
6076 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006077 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006078 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006080 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 switch (opaque_key) {
6083 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006084 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006085 desc = &tpr->rx_std[dest_idx];
6086 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006087 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 break;
6089
6090 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006091 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006092 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006093 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006094 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 break;
6096
6097 default:
6098 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
6101 /* Do not overwrite any of the map or rp information
6102 * until we are sure we can commit to a new buffer.
6103 *
6104 * Callers depend upon this behavior and assume that
6105 * we leave everything unchanged if we fail.
6106 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006107 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6108 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006109 if (skb_size <= PAGE_SIZE) {
6110 data = netdev_alloc_frag(skb_size);
6111 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006112 } else {
6113 data = kmalloc(skb_size, GFP_ATOMIC);
6114 *frag_size = 0;
6115 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006116 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 return -ENOMEM;
6118
Eric Dumazet9205fd92011-11-18 06:47:01 +00006119 mapping = pci_map_single(tp->pdev,
6120 data + TG3_RX_OFFSET(tp),
6121 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006123 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006124 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006125 return -EIO;
6126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Eric Dumazet9205fd92011-11-18 06:47:01 +00006128 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006129 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 desc->addr_hi = ((u64)mapping >> 32);
6132 desc->addr_lo = ((u64)mapping & 0xffffffff);
6133
Eric Dumazet9205fd92011-11-18 06:47:01 +00006134 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135}
6136
6137/* We only need to move over in the address because the other
6138 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006139 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 */
Matt Carlsona3896162009-11-13 13:03:44 +00006141static void tg3_recycle_rx(struct tg3_napi *tnapi,
6142 struct tg3_rx_prodring_set *dpr,
6143 u32 opaque_key, int src_idx,
6144 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145{
Matt Carlson17375d22009-08-28 14:02:18 +00006146 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6148 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006149 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006150 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151
6152 switch (opaque_key) {
6153 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006154 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006155 dest_desc = &dpr->rx_std[dest_idx];
6156 dest_map = &dpr->rx_std_buffers[dest_idx];
6157 src_desc = &spr->rx_std[src_idx];
6158 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 break;
6160
6161 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006162 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006163 dest_desc = &dpr->rx_jmb[dest_idx].std;
6164 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6165 src_desc = &spr->rx_jmb[src_idx].std;
6166 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 break;
6168
6169 default:
6170 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
Eric Dumazet9205fd92011-11-18 06:47:01 +00006173 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006174 dma_unmap_addr_set(dest_map, mapping,
6175 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 dest_desc->addr_hi = src_desc->addr_hi;
6177 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006178
6179 /* Ensure that the update to the skb happens after the physical
6180 * addresses have been transferred to the new BD location.
6181 */
6182 smp_wmb();
6183
Eric Dumazet9205fd92011-11-18 06:47:01 +00006184 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185}
6186
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187/* The RX ring scheme is composed of multiple rings which post fresh
6188 * buffers to the chip, and one special ring the chip uses to report
6189 * status back to the host.
6190 *
6191 * The special ring reports the status of received packets to the
6192 * host. The chip does not write into the original descriptor the
6193 * RX buffer was obtained from. The chip simply takes the original
6194 * descriptor as provided by the host, updates the status and length
6195 * field, then writes this into the next status ring entry.
6196 *
6197 * Each ring the host uses to post buffers to the chip is described
6198 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6199 * it is first placed into the on-chip ram. When the packet's length
6200 * is known, it walks down the TG3_BDINFO entries to select the ring.
6201 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6202 * which is within the range of the new packet's length is chosen.
6203 *
6204 * The "separate ring for rx status" scheme may sound queer, but it makes
6205 * sense from a cache coherency perspective. If only the host writes
6206 * to the buffer post rings, and only the chip writes to the rx status
6207 * rings, then cache lines never move beyond shared-modified state.
6208 * If both the host and chip were to write into the same ring, cache line
6209 * eviction could occur since both entities want it in an exclusive state.
6210 */
Matt Carlson17375d22009-08-28 14:02:18 +00006211static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212{
Matt Carlson17375d22009-08-28 14:02:18 +00006213 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006214 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006215 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006216 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006217 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006219 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006221 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 /*
6223 * We need to order the read of hw_idx and the read of
6224 * the opaque cookie.
6225 */
6226 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 work_mask = 0;
6228 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006229 std_prod_idx = tpr->rx_std_prod_idx;
6230 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006232 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006233 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 unsigned int len;
6235 struct sk_buff *skb;
6236 dma_addr_t dma_addr;
6237 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006238 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006239 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
6241 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6242 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6243 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006244 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006245 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006246 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006247 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006248 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006250 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006251 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006252 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006253 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006254 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
6257 work_mask |= opaque_key;
6258
6259 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6260 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6261 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006262 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 desc_idx, *post_ptr);
6264 drop_it_no_recycle:
6265 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006266 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 goto next_pkt;
6268 }
6269
Eric Dumazet9205fd92011-11-18 06:47:01 +00006270 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006271 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6272 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006274 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6275 RXD_FLAG_PTPSTAT_PTPV1 ||
6276 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6277 RXD_FLAG_PTPSTAT_PTPV2) {
6278 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6279 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6280 }
6281
Matt Carlsond2757fc2010-04-12 06:58:27 +00006282 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006284 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285
Eric Dumazet9205fd92011-11-18 06:47:01 +00006286 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006287 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 if (skb_size < 0)
6289 goto drop_it;
6290
Matt Carlson287be122009-08-28 13:58:46 +00006291 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 PCI_DMA_FROMDEVICE);
6293
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006294 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006295 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006296 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006297 goto drop_it_no_recycle;
6298 }
6299 skb_reserve(skb, TG3_RX_OFFSET(tp));
6300 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006301 * after the usage of the old DMA mapping.
6302 */
6303 smp_wmb();
6304
Eric Dumazet9205fd92011-11-18 06:47:01 +00006305 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006306
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006308 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 desc_idx, *post_ptr);
6310
Eric Dumazet9205fd92011-11-18 06:47:01 +00006311 skb = netdev_alloc_skb(tp->dev,
6312 len + TG3_RAW_IP_ALIGN);
6313 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 goto drop_it_no_recycle;
6315
Eric Dumazet9205fd92011-11-18 06:47:01 +00006316 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006318 memcpy(skb->data,
6319 data + TG3_RX_OFFSET(tp),
6320 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 }
6323
Eric Dumazet9205fd92011-11-18 06:47:01 +00006324 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006325 if (tstamp)
6326 tg3_hwclock_to_timestamp(tp, tstamp,
6327 skb_hwtstamps(skb));
6328
Michał Mirosławdc668912011-04-07 03:35:07 +00006329 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6331 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6332 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6333 skb->ip_summed = CHECKSUM_UNNECESSARY;
6334 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006335 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
6337 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006338
6339 if (len > (tp->dev->mtu + ETH_HLEN) &&
6340 skb->protocol != htons(ETH_P_8021Q)) {
6341 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006342 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006343 }
6344
Matt Carlson9dc7a112010-04-12 06:58:28 +00006345 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006346 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
6347 __vlan_hwaccel_put_tag(skb,
6348 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006349
Matt Carlsonbf933c82011-01-25 15:58:49 +00006350 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 received++;
6353 budget--;
6354
6355next_pkt:
6356 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006357
6358 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006359 tpr->rx_std_prod_idx = std_prod_idx &
6360 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006361 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6362 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006363 work_mask &= ~RXD_OPAQUE_RING_STD;
6364 rx_std_posted = 0;
6365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006367 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006368 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006369
6370 /* Refresh hw_idx to see if there is new work */
6371 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006372 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006373 rmb();
6374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 }
6376
6377 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006378 tnapi->rx_rcb_ptr = sw_idx;
6379 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380
6381 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006382 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006383 /* Sync BD data before updating mailbox */
6384 wmb();
6385
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006386 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006387 tpr->rx_std_prod_idx = std_prod_idx &
6388 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006389 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6390 tpr->rx_std_prod_idx);
6391 }
6392 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006393 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6394 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006395 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6396 tpr->rx_jmb_prod_idx);
6397 }
6398 mmiowb();
6399 } else if (work_mask) {
6400 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6401 * updated before the producer indices can be updated.
6402 */
6403 smp_wmb();
6404
Matt Carlson2c49a442010-09-30 10:34:35 +00006405 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6406 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006407
Michael Chan7ae52892012-03-21 15:38:33 +00006408 if (tnapi != &tp->napi[1]) {
6409 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006410 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413
6414 return received;
6415}
6416
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006417static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006420 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006421 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6422
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 if (sblk->status & SD_STATUS_LINK_CHG) {
6424 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006425 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006426 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006427 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006428 tw32_f(MAC_STATUS,
6429 (MAC_STATUS_SYNC_CHANGED |
6430 MAC_STATUS_CFG_CHANGED |
6431 MAC_STATUS_MI_COMPLETION |
6432 MAC_STATUS_LNKSTATE_CHANGED));
6433 udelay(40);
6434 } else
6435 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006436 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 }
6438 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006439}
6440
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006441static int tg3_rx_prodring_xfer(struct tg3 *tp,
6442 struct tg3_rx_prodring_set *dpr,
6443 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006444{
6445 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006446 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006447
6448 while (1) {
6449 src_prod_idx = spr->rx_std_prod_idx;
6450
6451 /* Make sure updates to the rx_std_buffers[] entries and the
6452 * standard producer index are seen in the correct order.
6453 */
6454 smp_rmb();
6455
6456 if (spr->rx_std_cons_idx == src_prod_idx)
6457 break;
6458
6459 if (spr->rx_std_cons_idx < src_prod_idx)
6460 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6461 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006462 cpycnt = tp->rx_std_ring_mask + 1 -
6463 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006464
Matt Carlson2c49a442010-09-30 10:34:35 +00006465 cpycnt = min(cpycnt,
6466 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006467
6468 si = spr->rx_std_cons_idx;
6469 di = dpr->rx_std_prod_idx;
6470
Matt Carlsone92967b2010-02-12 14:47:06 +00006471 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006472 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006473 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006474 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006475 break;
6476 }
6477 }
6478
6479 if (!cpycnt)
6480 break;
6481
6482 /* Ensure that updates to the rx_std_buffers ring and the
6483 * shadowed hardware producer ring from tg3_recycle_skb() are
6484 * ordered correctly WRT the skb check above.
6485 */
6486 smp_rmb();
6487
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006488 memcpy(&dpr->rx_std_buffers[di],
6489 &spr->rx_std_buffers[si],
6490 cpycnt * sizeof(struct ring_info));
6491
6492 for (i = 0; i < cpycnt; i++, di++, si++) {
6493 struct tg3_rx_buffer_desc *sbd, *dbd;
6494 sbd = &spr->rx_std[si];
6495 dbd = &dpr->rx_std[di];
6496 dbd->addr_hi = sbd->addr_hi;
6497 dbd->addr_lo = sbd->addr_lo;
6498 }
6499
Matt Carlson2c49a442010-09-30 10:34:35 +00006500 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6501 tp->rx_std_ring_mask;
6502 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6503 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006504 }
6505
6506 while (1) {
6507 src_prod_idx = spr->rx_jmb_prod_idx;
6508
6509 /* Make sure updates to the rx_jmb_buffers[] entries and
6510 * the jumbo producer index are seen in the correct order.
6511 */
6512 smp_rmb();
6513
6514 if (spr->rx_jmb_cons_idx == src_prod_idx)
6515 break;
6516
6517 if (spr->rx_jmb_cons_idx < src_prod_idx)
6518 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6519 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006520 cpycnt = tp->rx_jmb_ring_mask + 1 -
6521 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006522
6523 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006524 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006525
6526 si = spr->rx_jmb_cons_idx;
6527 di = dpr->rx_jmb_prod_idx;
6528
Matt Carlsone92967b2010-02-12 14:47:06 +00006529 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006530 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006531 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006532 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006533 break;
6534 }
6535 }
6536
6537 if (!cpycnt)
6538 break;
6539
6540 /* Ensure that updates to the rx_jmb_buffers ring and the
6541 * shadowed hardware producer ring from tg3_recycle_skb() are
6542 * ordered correctly WRT the skb check above.
6543 */
6544 smp_rmb();
6545
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006546 memcpy(&dpr->rx_jmb_buffers[di],
6547 &spr->rx_jmb_buffers[si],
6548 cpycnt * sizeof(struct ring_info));
6549
6550 for (i = 0; i < cpycnt; i++, di++, si++) {
6551 struct tg3_rx_buffer_desc *sbd, *dbd;
6552 sbd = &spr->rx_jmb[si].std;
6553 dbd = &dpr->rx_jmb[di].std;
6554 dbd->addr_hi = sbd->addr_hi;
6555 dbd->addr_lo = sbd->addr_lo;
6556 }
6557
Matt Carlson2c49a442010-09-30 10:34:35 +00006558 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6559 tp->rx_jmb_ring_mask;
6560 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6561 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006562 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006563
6564 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006565}
6566
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006567static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6568{
6569 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570
6571 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006572 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006573 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006574 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006575 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 }
6577
Matt Carlsonf891ea12012-04-24 13:37:01 +00006578 if (!tnapi->rx_rcb_prod_idx)
6579 return work_done;
6580
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 /* run RX thread, within the bounds set by NAPI.
6582 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006583 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006585 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006586 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587
Joe Perches63c3a662011-04-26 08:12:10 +00006588 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006589 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006590 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006591 u32 std_prod_idx = dpr->rx_std_prod_idx;
6592 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006593
Michael Chan7ae52892012-03-21 15:38:33 +00006594 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00006595 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006596 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006597 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006598
6599 wmb();
6600
Matt Carlsone4af1af2010-02-12 14:47:05 +00006601 if (std_prod_idx != dpr->rx_std_prod_idx)
6602 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6603 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006604
Matt Carlsone4af1af2010-02-12 14:47:05 +00006605 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6606 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6607 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006608
6609 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006610
6611 if (err)
6612 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006613 }
6614
David S. Miller6f535762007-10-11 18:08:29 -07006615 return work_done;
6616}
David S. Millerf7383c22005-05-18 22:50:53 -07006617
Matt Carlsondb219972011-11-04 09:15:03 +00006618static inline void tg3_reset_task_schedule(struct tg3 *tp)
6619{
6620 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6621 schedule_work(&tp->reset_task);
6622}
6623
6624static inline void tg3_reset_task_cancel(struct tg3 *tp)
6625{
6626 cancel_work_sync(&tp->reset_task);
6627 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006628 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006629}
6630
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006631static int tg3_poll_msix(struct napi_struct *napi, int budget)
6632{
6633 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6634 struct tg3 *tp = tnapi->tp;
6635 int work_done = 0;
6636 struct tg3_hw_status *sblk = tnapi->hw_status;
6637
6638 while (1) {
6639 work_done = tg3_poll_work(tnapi, work_done, budget);
6640
Joe Perches63c3a662011-04-26 08:12:10 +00006641 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006642 goto tx_recovery;
6643
6644 if (unlikely(work_done >= budget))
6645 break;
6646
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006647 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006648 * to tell the hw how much work has been processed,
6649 * so we must read it before checking for more work.
6650 */
6651 tnapi->last_tag = sblk->status_tag;
6652 tnapi->last_irq_tag = tnapi->last_tag;
6653 rmb();
6654
6655 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006656 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6657 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006658
6659 /* This test here is not race free, but will reduce
6660 * the number of interrupts by looping again.
6661 */
6662 if (tnapi == &tp->napi[1] && tp->rx_refill)
6663 continue;
6664
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006665 napi_complete(napi);
6666 /* Reenable interrupts. */
6667 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006668
6669 /* This test here is synchronized by napi_schedule()
6670 * and napi_complete() to close the race condition.
6671 */
6672 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6673 tw32(HOSTCC_MODE, tp->coalesce_mode |
6674 HOSTCC_MODE_ENABLE |
6675 tnapi->coal_now);
6676 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006677 mmiowb();
6678 break;
6679 }
6680 }
6681
6682 return work_done;
6683
6684tx_recovery:
6685 /* work_done is guaranteed to be less than budget. */
6686 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006687 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006688 return work_done;
6689}
6690
Matt Carlsone64de4e2011-04-13 11:05:05 +00006691static void tg3_process_error(struct tg3 *tp)
6692{
6693 u32 val;
6694 bool real_error = false;
6695
Joe Perches63c3a662011-04-26 08:12:10 +00006696 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006697 return;
6698
6699 /* Check Flow Attention register */
6700 val = tr32(HOSTCC_FLOW_ATTN);
6701 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6702 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6703 real_error = true;
6704 }
6705
6706 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6707 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6708 real_error = true;
6709 }
6710
6711 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6712 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6713 real_error = true;
6714 }
6715
6716 if (!real_error)
6717 return;
6718
6719 tg3_dump_state(tp);
6720
Joe Perches63c3a662011-04-26 08:12:10 +00006721 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006722 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006723}
6724
David S. Miller6f535762007-10-11 18:08:29 -07006725static int tg3_poll(struct napi_struct *napi, int budget)
6726{
Matt Carlson8ef04422009-08-28 14:01:37 +00006727 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6728 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006729 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006730 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006731
6732 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006733 if (sblk->status & SD_STATUS_ERROR)
6734 tg3_process_error(tp);
6735
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006736 tg3_poll_link(tp);
6737
Matt Carlson17375d22009-08-28 14:02:18 +00006738 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006739
Joe Perches63c3a662011-04-26 08:12:10 +00006740 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006741 goto tx_recovery;
6742
6743 if (unlikely(work_done >= budget))
6744 break;
6745
Joe Perches63c3a662011-04-26 08:12:10 +00006746 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006747 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006748 * to tell the hw how much work has been processed,
6749 * so we must read it before checking for more work.
6750 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006751 tnapi->last_tag = sblk->status_tag;
6752 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006753 rmb();
6754 } else
6755 sblk->status &= ~SD_STATUS_UPDATED;
6756
Matt Carlson17375d22009-08-28 14:02:18 +00006757 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006758 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006759 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006760 break;
6761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 }
6763
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006764 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006765
6766tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006767 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006768 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006769 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006770 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771}
6772
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006773static void tg3_napi_disable(struct tg3 *tp)
6774{
6775 int i;
6776
6777 for (i = tp->irq_cnt - 1; i >= 0; i--)
6778 napi_disable(&tp->napi[i].napi);
6779}
6780
6781static void tg3_napi_enable(struct tg3 *tp)
6782{
6783 int i;
6784
6785 for (i = 0; i < tp->irq_cnt; i++)
6786 napi_enable(&tp->napi[i].napi);
6787}
6788
6789static void tg3_napi_init(struct tg3 *tp)
6790{
6791 int i;
6792
6793 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6794 for (i = 1; i < tp->irq_cnt; i++)
6795 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6796}
6797
6798static void tg3_napi_fini(struct tg3 *tp)
6799{
6800 int i;
6801
6802 for (i = 0; i < tp->irq_cnt; i++)
6803 netif_napi_del(&tp->napi[i].napi);
6804}
6805
6806static inline void tg3_netif_stop(struct tg3 *tp)
6807{
6808 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6809 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006810 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006811 netif_tx_disable(tp->dev);
6812}
6813
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00006814/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006815static inline void tg3_netif_start(struct tg3 *tp)
6816{
Matt Carlsonbe947302012-12-03 19:36:57 +00006817 tg3_ptp_resume(tp);
6818
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006819 /* NOTE: unconditional netif_tx_wake_all_queues is only
6820 * appropriate so long as all callers are assured to
6821 * have free tx slots (such as after tg3_init_hw)
6822 */
6823 netif_tx_wake_all_queues(tp->dev);
6824
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006825 if (tp->link_up)
6826 netif_carrier_on(tp->dev);
6827
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006828 tg3_napi_enable(tp);
6829 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6830 tg3_enable_ints(tp);
6831}
6832
David S. Millerf47c11e2005-06-24 20:18:35 -07006833static void tg3_irq_quiesce(struct tg3 *tp)
6834{
Matt Carlson4f125f42009-09-01 12:55:02 +00006835 int i;
6836
David S. Millerf47c11e2005-06-24 20:18:35 -07006837 BUG_ON(tp->irq_sync);
6838
6839 tp->irq_sync = 1;
6840 smp_mb();
6841
Matt Carlson4f125f42009-09-01 12:55:02 +00006842 for (i = 0; i < tp->irq_cnt; i++)
6843 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006844}
6845
David S. Millerf47c11e2005-06-24 20:18:35 -07006846/* Fully shutdown all tg3 driver activity elsewhere in the system.
6847 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6848 * with as well. Most of the time, this is not necessary except when
6849 * shutting down the device.
6850 */
6851static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6852{
Michael Chan46966542007-07-11 19:47:19 -07006853 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006854 if (irq_sync)
6855 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006856}
6857
6858static inline void tg3_full_unlock(struct tg3 *tp)
6859{
David S. Millerf47c11e2005-06-24 20:18:35 -07006860 spin_unlock_bh(&tp->lock);
6861}
6862
Michael Chanfcfa0a32006-03-20 22:28:41 -08006863/* One-shot MSI handler - Chip automatically disables interrupt
6864 * after sending MSI so driver doesn't have to do it.
6865 */
David Howells7d12e782006-10-05 14:55:46 +01006866static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006867{
Matt Carlson09943a12009-08-28 14:01:57 +00006868 struct tg3_napi *tnapi = dev_id;
6869 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006870
Matt Carlson898a56f2009-08-28 14:02:40 +00006871 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006872 if (tnapi->rx_rcb)
6873 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006874
6875 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006876 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006877
6878 return IRQ_HANDLED;
6879}
6880
Michael Chan88b06bc22005-04-21 17:13:25 -07006881/* MSI ISR - No need to check for interrupt sharing and no need to
6882 * flush status block and interrupt mailbox. PCI ordering rules
6883 * guarantee that MSI will arrive after the status block.
6884 */
David Howells7d12e782006-10-05 14:55:46 +01006885static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006886{
Matt Carlson09943a12009-08-28 14:01:57 +00006887 struct tg3_napi *tnapi = dev_id;
6888 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006889
Matt Carlson898a56f2009-08-28 14:02:40 +00006890 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006891 if (tnapi->rx_rcb)
6892 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006893 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006894 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006895 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006896 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006897 * NIC to stop sending us irqs, engaging "in-intr-handler"
6898 * event coalescing.
6899 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006900 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006901 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006902 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006903
Michael Chan88b06bc22005-04-21 17:13:25 -07006904 return IRQ_RETVAL(1);
6905}
6906
David Howells7d12e782006-10-05 14:55:46 +01006907static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908{
Matt Carlson09943a12009-08-28 14:01:57 +00006909 struct tg3_napi *tnapi = dev_id;
6910 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006911 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 unsigned int handled = 1;
6913
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 /* In INTx mode, it is possible for the interrupt to arrive at
6915 * the CPU before the status block posted prior to the interrupt.
6916 * Reading the PCI State register will confirm whether the
6917 * interrupt is ours and will flush the status block.
6918 */
Michael Chand18edcb2007-03-24 20:57:11 -07006919 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006920 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006921 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6922 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006923 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006924 }
Michael Chand18edcb2007-03-24 20:57:11 -07006925 }
6926
6927 /*
6928 * Writing any value to intr-mbox-0 clears PCI INTA# and
6929 * chip-internal interrupt pending events.
6930 * Writing non-zero to intr-mbox-0 additional tells the
6931 * NIC to stop sending us irqs, engaging "in-intr-handler"
6932 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006933 *
6934 * Flush the mailbox to de-assert the IRQ immediately to prevent
6935 * spurious interrupts. The flush impacts performance but
6936 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006937 */
Michael Chanc04cb342007-05-07 00:26:15 -07006938 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006939 if (tg3_irq_sync(tp))
6940 goto out;
6941 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006942 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006943 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006944 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006945 } else {
6946 /* No work, shared interrupt perhaps? re-enable
6947 * interrupts, and flush that PCI write
6948 */
6949 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6950 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006951 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006952out:
David S. Millerfac9b832005-05-18 22:46:34 -07006953 return IRQ_RETVAL(handled);
6954}
6955
David Howells7d12e782006-10-05 14:55:46 +01006956static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006957{
Matt Carlson09943a12009-08-28 14:01:57 +00006958 struct tg3_napi *tnapi = dev_id;
6959 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006960 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006961 unsigned int handled = 1;
6962
David S. Millerfac9b832005-05-18 22:46:34 -07006963 /* In INTx mode, it is possible for the interrupt to arrive at
6964 * the CPU before the status block posted prior to the interrupt.
6965 * Reading the PCI State register will confirm whether the
6966 * interrupt is ours and will flush the status block.
6967 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006968 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006969 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006970 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6971 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006972 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 }
Michael Chand18edcb2007-03-24 20:57:11 -07006974 }
6975
6976 /*
6977 * writing any value to intr-mbox-0 clears PCI INTA# and
6978 * chip-internal interrupt pending events.
6979 * writing non-zero to intr-mbox-0 additional tells the
6980 * NIC to stop sending us irqs, engaging "in-intr-handler"
6981 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006982 *
6983 * Flush the mailbox to de-assert the IRQ immediately to prevent
6984 * spurious interrupts. The flush impacts performance but
6985 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006986 */
Michael Chanc04cb342007-05-07 00:26:15 -07006987 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006988
6989 /*
6990 * In a shared interrupt configuration, sometimes other devices'
6991 * interrupts will scream. We record the current status tag here
6992 * so that the above check can report that the screaming interrupts
6993 * are unhandled. Eventually they will be silenced.
6994 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006995 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006996
Michael Chand18edcb2007-03-24 20:57:11 -07006997 if (tg3_irq_sync(tp))
6998 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006999
Matt Carlson72334482009-08-28 14:03:01 +00007000 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007001
Matt Carlson09943a12009-08-28 14:01:57 +00007002 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007003
David S. Millerf47c11e2005-06-24 20:18:35 -07007004out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 return IRQ_RETVAL(handled);
7006}
7007
Michael Chan79381092005-04-21 17:13:59 -07007008/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007009static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007010{
Matt Carlson09943a12009-08-28 14:01:57 +00007011 struct tg3_napi *tnapi = dev_id;
7012 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007013 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007014
Michael Chanf9804dd2005-09-27 12:13:10 -07007015 if ((sblk->status & SD_STATUS_UPDATED) ||
7016 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007017 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007018 return IRQ_RETVAL(1);
7019 }
7020 return IRQ_RETVAL(0);
7021}
7022
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023#ifdef CONFIG_NET_POLL_CONTROLLER
7024static void tg3_poll_controller(struct net_device *dev)
7025{
Matt Carlson4f125f42009-09-01 12:55:02 +00007026 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007027 struct tg3 *tp = netdev_priv(dev);
7028
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007029 if (tg3_irq_sync(tp))
7030 return;
7031
Matt Carlson4f125f42009-09-01 12:55:02 +00007032 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007033 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034}
7035#endif
7036
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037static void tg3_tx_timeout(struct net_device *dev)
7038{
7039 struct tg3 *tp = netdev_priv(dev);
7040
Michael Chanb0408752007-02-13 12:18:30 -08007041 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007042 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007043 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045
Matt Carlsondb219972011-11-04 09:15:03 +00007046 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047}
7048
Michael Chanc58ec932005-09-17 00:46:27 -07007049/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7050static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7051{
7052 u32 base = (u32) mapping & 0xffffffff;
7053
Eric Dumazet807540b2010-09-23 05:40:09 +00007054 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007055}
7056
Michael Chan72f2afb2006-03-06 19:28:35 -08007057/* Test for DMA addresses > 40-bit */
7058static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7059 int len)
7060{
7061#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007062 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007063 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007064 return 0;
7065#else
7066 return 0;
7067#endif
7068}
7069
Matt Carlsond1a3b732011-07-27 14:20:51 +00007070static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007071 dma_addr_t mapping, u32 len, u32 flags,
7072 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007073{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007074 txbd->addr_hi = ((u64) mapping >> 32);
7075 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7076 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7077 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007078}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
Matt Carlson84b67b22011-07-27 14:20:52 +00007080static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007081 dma_addr_t map, u32 len, u32 flags,
7082 u32 mss, u32 vlan)
7083{
7084 struct tg3 *tp = tnapi->tp;
7085 bool hwbug = false;
7086
7087 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007088 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007089
7090 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007091 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007092
7093 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007094 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007095
Matt Carlsona4cb4282011-12-14 11:09:58 +00007096 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007097 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007098 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007099 while (len > tp->dma_limit && *budget) {
7100 u32 frag_len = tp->dma_limit;
7101 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007102
Matt Carlsonb9e45482011-11-04 09:14:59 +00007103 /* Avoid the 8byte DMA problem */
7104 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007105 len += tp->dma_limit / 2;
7106 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007107 }
7108
Matt Carlsonb9e45482011-11-04 09:14:59 +00007109 tnapi->tx_buffers[*entry].fragmented = true;
7110
7111 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7112 frag_len, tmp_flag, mss, vlan);
7113 *budget -= 1;
7114 prvidx = *entry;
7115 *entry = NEXT_TX(*entry);
7116
Matt Carlsone31aa982011-07-27 14:20:53 +00007117 map += frag_len;
7118 }
7119
7120 if (len) {
7121 if (*budget) {
7122 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7123 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007124 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007125 *entry = NEXT_TX(*entry);
7126 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007127 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007128 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007129 }
7130 }
7131 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007132 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7133 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007134 *entry = NEXT_TX(*entry);
7135 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007136
7137 return hwbug;
7138}
7139
Matt Carlson0d681b22011-07-27 14:20:49 +00007140static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007141{
7142 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007143 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007144 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007145
Matt Carlson0d681b22011-07-27 14:20:49 +00007146 skb = txb->skb;
7147 txb->skb = NULL;
7148
Matt Carlson432aa7e2011-05-19 12:12:45 +00007149 pci_unmap_single(tnapi->tp->pdev,
7150 dma_unmap_addr(txb, mapping),
7151 skb_headlen(skb),
7152 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007153
7154 while (txb->fragmented) {
7155 txb->fragmented = false;
7156 entry = NEXT_TX(entry);
7157 txb = &tnapi->tx_buffers[entry];
7158 }
7159
Matt Carlsonba1142e2011-11-04 09:15:00 +00007160 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007161 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007162
7163 entry = NEXT_TX(entry);
7164 txb = &tnapi->tx_buffers[entry];
7165
7166 pci_unmap_page(tnapi->tp->pdev,
7167 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007168 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007169
7170 while (txb->fragmented) {
7171 txb->fragmented = false;
7172 entry = NEXT_TX(entry);
7173 txb = &tnapi->tx_buffers[entry];
7174 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007175 }
7176}
7177
Michael Chan72f2afb2006-03-06 19:28:35 -08007178/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007179static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007180 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007181 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007182 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007184 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007185 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007186 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007187 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188
Joe Perches41535772013-02-16 11:20:04 +00007189 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007190 new_skb = skb_copy(skb, GFP_ATOMIC);
7191 else {
7192 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7193
7194 new_skb = skb_copy_expand(skb,
7195 skb_headroom(skb) + more_headroom,
7196 skb_tailroom(skb), GFP_ATOMIC);
7197 }
7198
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007200 ret = -1;
7201 } else {
7202 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007203 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7204 PCI_DMA_TODEVICE);
7205 /* Make sure the mapping succeeded */
7206 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007207 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007208 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007209 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007210 u32 save_entry = *entry;
7211
Matt Carlson92cd3a12011-07-27 14:20:47 +00007212 base_flags |= TXD_FLAG_END;
7213
Matt Carlson84b67b22011-07-27 14:20:52 +00007214 tnapi->tx_buffers[*entry].skb = new_skb;
7215 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007216 mapping, new_addr);
7217
Matt Carlson84b67b22011-07-27 14:20:52 +00007218 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007219 new_skb->len, base_flags,
7220 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007221 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007222 dev_kfree_skb(new_skb);
7223 ret = -1;
7224 }
Michael Chanc58ec932005-09-17 00:46:27 -07007225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 }
7227
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007229 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007230 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231}
7232
Matt Carlson2ffcc982011-05-19 12:12:44 +00007233static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007234
7235/* Use GSO to workaround a rare TSO bug that may be triggered when the
7236 * TSO header is greater than 80 bytes.
7237 */
7238static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7239{
7240 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007241 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007242
7243 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007244 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007245 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007246
7247 /* netif_tx_stop_queue() must be done before checking
7248 * checking tx index in tg3_tx_avail() below, because in
7249 * tg3_tx(), we update tx index before checking for
7250 * netif_tx_queue_stopped().
7251 */
7252 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007253 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007254 return NETDEV_TX_BUSY;
7255
7256 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007257 }
7258
7259 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007260 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007261 goto tg3_tso_bug_end;
7262
7263 do {
7264 nskb = segs;
7265 segs = segs->next;
7266 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007267 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007268 } while (segs);
7269
7270tg3_tso_bug_end:
7271 dev_kfree_skb(skb);
7272
7273 return NETDEV_TX_OK;
7274}
Michael Chan52c0fd82006-06-29 20:15:54 -07007275
Michael Chan5a6f3072006-03-20 22:28:05 -08007276/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007277 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007278 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007279static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007280{
7281 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007282 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007283 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007284 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007285 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007286 struct tg3_napi *tnapi;
7287 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007288 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007289
Matt Carlson24f4efd2009-11-13 13:03:35 +00007290 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7291 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007292 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007293 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294
Matt Carlson84b67b22011-07-27 14:20:52 +00007295 budget = tg3_tx_avail(tnapi);
7296
Michael Chan00b70502006-06-17 21:58:45 -07007297 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007298 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007299 * interrupt. Furthermore, IRQ processing runs lockless so we have
7300 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007302 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007303 if (!netif_tx_queue_stopped(txq)) {
7304 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007305
7306 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007307 netdev_err(dev,
7308 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 return NETDEV_TX_BUSY;
7311 }
7312
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007313 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007315 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007317
Matt Carlsonbe98da62010-07-11 09:31:46 +00007318 mss = skb_shinfo(skb)->gso_size;
7319 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007320 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007321 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322
7323 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007324 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7325 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326
Matt Carlson34195c32010-07-11 09:31:42 +00007327 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007328 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329
Eric Dumazeta5a11952012-01-23 01:22:09 +00007330 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007331
Eric Dumazeta5a11952012-01-23 01:22:09 +00007332 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007333 iph->check = 0;
7334 iph->tot_len = htons(mss + hdr_len);
7335 }
7336
Michael Chan52c0fd82006-06-29 20:15:54 -07007337 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007338 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007339 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007340
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7342 TXD_FLAG_CPU_POST_DMA);
7343
Joe Perches63c3a662011-04-26 08:12:10 +00007344 if (tg3_flag(tp, HW_TSO_1) ||
7345 tg3_flag(tp, HW_TSO_2) ||
7346 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007347 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007349 } else
7350 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7351 iph->daddr, 0,
7352 IPPROTO_TCP,
7353 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354
Joe Perches63c3a662011-04-26 08:12:10 +00007355 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007356 mss |= (hdr_len & 0xc) << 12;
7357 if (hdr_len & 0x10)
7358 base_flags |= 0x00000010;
7359 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007360 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007361 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007362 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007363 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007364 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 int tsflags;
7366
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007367 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368 mss |= (tsflags << 11);
7369 }
7370 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007371 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 int tsflags;
7373
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007374 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 base_flags |= tsflags << 12;
7376 }
7377 }
7378 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007379
Matt Carlson93a700a2011-08-31 11:44:54 +00007380 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7381 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7382 base_flags |= TXD_FLAG_JMB_PKT;
7383
Matt Carlson92cd3a12011-07-27 14:20:47 +00007384 if (vlan_tx_tag_present(skb)) {
7385 base_flags |= TXD_FLAG_VLAN;
7386 vlan = vlan_tx_tag_get(skb);
7387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007389 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7390 tg3_flag(tp, TX_TSTAMP_EN)) {
7391 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7392 base_flags |= TXD_FLAG_HWTSTAMP;
7393 }
7394
Alexander Duyckf4188d82009-12-02 16:48:38 +00007395 len = skb_headlen(skb);
7396
7397 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007398 if (pci_dma_mapping_error(tp->pdev, mapping))
7399 goto drop;
7400
David S. Miller90079ce2008-09-11 04:52:51 -07007401
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007402 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007403 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404
7405 would_hit_hwbug = 0;
7406
Joe Perches63c3a662011-04-26 08:12:10 +00007407 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007408 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409
Matt Carlson84b67b22011-07-27 14:20:52 +00007410 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007411 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007412 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007413 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007414 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007415 u32 tmp_mss = mss;
7416
7417 if (!tg3_flag(tp, HW_TSO_1) &&
7418 !tg3_flag(tp, HW_TSO_2) &&
7419 !tg3_flag(tp, HW_TSO_3))
7420 tmp_mss = 0;
7421
Matt Carlsonc5665a52012-02-13 10:20:12 +00007422 /* Now loop through additional data
7423 * fragments, and queue them.
7424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 last = skb_shinfo(skb)->nr_frags - 1;
7426 for (i = 0; i <= last; i++) {
7427 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7428
Eric Dumazet9e903e02011-10-18 21:00:24 +00007429 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007430 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007431 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007433 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007434 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007435 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007436 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007437 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438
Matt Carlsonb9e45482011-11-04 09:14:59 +00007439 if (!budget ||
7440 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007441 len, base_flags |
7442 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007443 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007444 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007445 break;
7446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447 }
7448 }
7449
7450 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007451 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452
7453 /* If the workaround fails due to memory/mapping
7454 * failure, silently drop this packet.
7455 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007456 entry = tnapi->tx_prod;
7457 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007458 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007459 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007460 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 }
7462
Richard Cochrand515b452011-06-19 03:31:41 +00007463 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007464 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007465
Michael Chan6541b802012-03-04 14:48:14 +00007466 /* Sync BD data before updating mailbox */
7467 wmb();
7468
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007470 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007472 tnapi->tx_prod = entry;
7473 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007474 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007475
7476 /* netif_tx_stop_queue() must be done before checking
7477 * checking tx index in tg3_tx_avail() below, because in
7478 * tg3_tx(), we update tx index before checking for
7479 * netif_tx_queue_stopped().
7480 */
7481 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007482 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007483 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007486 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007488
7489dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007490 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007491 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007492drop:
7493 dev_kfree_skb(skb);
7494drop_nofree:
7495 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007496 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497}
7498
Matt Carlson6e01b202011-08-19 13:58:20 +00007499static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7500{
7501 if (enable) {
7502 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7503 MAC_MODE_PORT_MODE_MASK);
7504
7505 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7506
7507 if (!tg3_flag(tp, 5705_PLUS))
7508 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7509
7510 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7511 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7512 else
7513 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7514 } else {
7515 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7516
7517 if (tg3_flag(tp, 5705_PLUS) ||
7518 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00007519 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00007520 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7521 }
7522
7523 tw32(MAC_MODE, tp->mac_mode);
7524 udelay(40);
7525}
7526
Matt Carlson941ec902011-08-19 13:58:23 +00007527static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007528{
Matt Carlson941ec902011-08-19 13:58:23 +00007529 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007530
7531 tg3_phy_toggle_apd(tp, false);
7532 tg3_phy_toggle_automdix(tp, 0);
7533
Matt Carlson941ec902011-08-19 13:58:23 +00007534 if (extlpbk && tg3_phy_set_extloopbk(tp))
7535 return -EIO;
7536
7537 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007538 switch (speed) {
7539 case SPEED_10:
7540 break;
7541 case SPEED_100:
7542 bmcr |= BMCR_SPEED100;
7543 break;
7544 case SPEED_1000:
7545 default:
7546 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7547 speed = SPEED_100;
7548 bmcr |= BMCR_SPEED100;
7549 } else {
7550 speed = SPEED_1000;
7551 bmcr |= BMCR_SPEED1000;
7552 }
7553 }
7554
Matt Carlson941ec902011-08-19 13:58:23 +00007555 if (extlpbk) {
7556 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7557 tg3_readphy(tp, MII_CTRL1000, &val);
7558 val |= CTL1000_AS_MASTER |
7559 CTL1000_ENABLE_MASTER;
7560 tg3_writephy(tp, MII_CTRL1000, val);
7561 } else {
7562 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7563 MII_TG3_FET_PTEST_TRIM_2;
7564 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7565 }
7566 } else
7567 bmcr |= BMCR_LOOPBACK;
7568
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007569 tg3_writephy(tp, MII_BMCR, bmcr);
7570
7571 /* The write needs to be flushed for the FETs */
7572 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7573 tg3_readphy(tp, MII_BMCR, &bmcr);
7574
7575 udelay(40);
7576
7577 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00007578 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007579 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007580 MII_TG3_FET_PTEST_FRC_TX_LINK |
7581 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7582
7583 /* The write needs to be flushed for the AC131 */
7584 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7585 }
7586
7587 /* Reset to prevent losing 1st rx packet intermittently */
7588 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7589 tg3_flag(tp, 5780_CLASS)) {
7590 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7591 udelay(10);
7592 tw32_f(MAC_RX_MODE, tp->rx_mode);
7593 }
7594
7595 mac_mode = tp->mac_mode &
7596 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7597 if (speed == SPEED_1000)
7598 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7599 else
7600 mac_mode |= MAC_MODE_PORT_MODE_MII;
7601
Joe Perches41535772013-02-16 11:20:04 +00007602 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007603 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7604
7605 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7606 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7607 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7608 mac_mode |= MAC_MODE_LINK_POLARITY;
7609
7610 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7611 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7612 }
7613
7614 tw32(MAC_MODE, mac_mode);
7615 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007616
7617 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007618}
7619
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007620static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007621{
7622 struct tg3 *tp = netdev_priv(dev);
7623
7624 if (features & NETIF_F_LOOPBACK) {
7625 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7626 return;
7627
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007628 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007629 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007630 netif_carrier_on(tp->dev);
7631 spin_unlock_bh(&tp->lock);
7632 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7633 } else {
7634 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7635 return;
7636
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007637 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007638 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007639 /* Force link status check */
7640 tg3_setup_phy(tp, 1);
7641 spin_unlock_bh(&tp->lock);
7642 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7643 }
7644}
7645
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007646static netdev_features_t tg3_fix_features(struct net_device *dev,
7647 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007648{
7649 struct tg3 *tp = netdev_priv(dev);
7650
Joe Perches63c3a662011-04-26 08:12:10 +00007651 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007652 features &= ~NETIF_F_ALL_TSO;
7653
7654 return features;
7655}
7656
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007657static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007658{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007659 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007660
7661 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7662 tg3_set_loopback(dev, features);
7663
7664 return 0;
7665}
7666
Matt Carlson21f581a2009-08-28 14:00:25 +00007667static void tg3_rx_prodring_free(struct tg3 *tp,
7668 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670 int i;
7671
Matt Carlson8fea32b2010-09-15 08:59:58 +00007672 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007673 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007674 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007675 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007676 tp->rx_pkt_map_sz);
7677
Joe Perches63c3a662011-04-26 08:12:10 +00007678 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007679 for (i = tpr->rx_jmb_cons_idx;
7680 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007681 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007682 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007683 TG3_RX_JMB_MAP_SZ);
7684 }
7685 }
7686
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007687 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689
Matt Carlson2c49a442010-09-30 10:34:35 +00007690 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007691 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007692 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693
Joe Perches63c3a662011-04-26 08:12:10 +00007694 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007695 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007696 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007697 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698 }
7699}
7700
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007701/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702 *
7703 * The chip has been shut down and the driver detached from
7704 * the networking, so no interrupts or new tx packets will
7705 * end up in the driver. tp->{tx,}lock are held and thus
7706 * we may not sleep.
7707 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007708static int tg3_rx_prodring_alloc(struct tg3 *tp,
7709 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710{
Matt Carlson287be122009-08-28 13:58:46 +00007711 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007713 tpr->rx_std_cons_idx = 0;
7714 tpr->rx_std_prod_idx = 0;
7715 tpr->rx_jmb_cons_idx = 0;
7716 tpr->rx_jmb_prod_idx = 0;
7717
Matt Carlson8fea32b2010-09-15 08:59:58 +00007718 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007719 memset(&tpr->rx_std_buffers[0], 0,
7720 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007721 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007722 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007723 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007724 goto done;
7725 }
7726
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007728 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729
Matt Carlson287be122009-08-28 13:58:46 +00007730 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007731 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007732 tp->dev->mtu > ETH_DATA_LEN)
7733 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7734 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007735
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736 /* Initialize invariants of the rings, we only set this
7737 * stuff once. This works because the card does not
7738 * write into the rx buffer posting rings.
7739 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007740 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741 struct tg3_rx_buffer_desc *rxd;
7742
Matt Carlson21f581a2009-08-28 14:00:25 +00007743 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007744 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7746 rxd->opaque = (RXD_OPAQUE_RING_STD |
7747 (i << RXD_OPAQUE_INDEX_SHIFT));
7748 }
7749
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007750 /* Now allocate fresh SKBs for each rx ring. */
7751 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007752 unsigned int frag_size;
7753
7754 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7755 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007756 netdev_warn(tp->dev,
7757 "Using a smaller RX standard ring. Only "
7758 "%d out of %d buffers were allocated "
7759 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007760 if (i == 0)
7761 goto initfail;
7762 tp->rx_pending = i;
7763 break;
7764 }
7765 }
7766
Joe Perches63c3a662011-04-26 08:12:10 +00007767 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007768 goto done;
7769
Matt Carlson2c49a442010-09-30 10:34:35 +00007770 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007771
Joe Perches63c3a662011-04-26 08:12:10 +00007772 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007773 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774
Matt Carlson2c49a442010-09-30 10:34:35 +00007775 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007776 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777
Matt Carlson0d86df82010-02-17 15:17:00 +00007778 rxd = &tpr->rx_jmb[i].std;
7779 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7780 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7781 RXD_FLAG_JUMBO;
7782 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7783 (i << RXD_OPAQUE_INDEX_SHIFT));
7784 }
7785
7786 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007787 unsigned int frag_size;
7788
7789 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7790 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007791 netdev_warn(tp->dev,
7792 "Using a smaller RX jumbo ring. Only %d "
7793 "out of %d buffers were allocated "
7794 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007795 if (i == 0)
7796 goto initfail;
7797 tp->rx_jumbo_pending = i;
7798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799 }
7800 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007801
7802done:
Michael Chan32d8c572006-07-25 16:38:29 -07007803 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007804
7805initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007806 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007807 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808}
7809
Matt Carlson21f581a2009-08-28 14:00:25 +00007810static void tg3_rx_prodring_fini(struct tg3 *tp,
7811 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812{
Matt Carlson21f581a2009-08-28 14:00:25 +00007813 kfree(tpr->rx_std_buffers);
7814 tpr->rx_std_buffers = NULL;
7815 kfree(tpr->rx_jmb_buffers);
7816 tpr->rx_jmb_buffers = NULL;
7817 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007818 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7819 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007820 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007822 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007823 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7824 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007825 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007827}
7828
Matt Carlson21f581a2009-08-28 14:00:25 +00007829static int tg3_rx_prodring_init(struct tg3 *tp,
7830 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007831{
Matt Carlson2c49a442010-09-30 10:34:35 +00007832 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7833 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007834 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007835 return -ENOMEM;
7836
Matt Carlson4bae65c2010-11-24 08:31:52 +00007837 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7838 TG3_RX_STD_RING_BYTES(tp),
7839 &tpr->rx_std_mapping,
7840 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007841 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007842 goto err_out;
7843
Joe Perches63c3a662011-04-26 08:12:10 +00007844 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007845 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007846 GFP_KERNEL);
7847 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007848 goto err_out;
7849
Matt Carlson4bae65c2010-11-24 08:31:52 +00007850 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7851 TG3_RX_JMB_RING_BYTES(tp),
7852 &tpr->rx_jmb_mapping,
7853 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007854 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007855 goto err_out;
7856 }
7857
7858 return 0;
7859
7860err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007861 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007862 return -ENOMEM;
7863}
7864
7865/* Free up pending packets in all rx/tx rings.
7866 *
7867 * The chip has been shut down and the driver detached from
7868 * the networking, so no interrupts or new tx packets will
7869 * end up in the driver. tp->{tx,}lock is not held and we are not
7870 * in an interrupt context and thus may sleep.
7871 */
7872static void tg3_free_rings(struct tg3 *tp)
7873{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007874 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007875
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007876 for (j = 0; j < tp->irq_cnt; j++) {
7877 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007878
Matt Carlson8fea32b2010-09-15 08:59:58 +00007879 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007880
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007881 if (!tnapi->tx_buffers)
7882 continue;
7883
Matt Carlson0d681b22011-07-27 14:20:49 +00007884 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7885 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007886
Matt Carlson0d681b22011-07-27 14:20:49 +00007887 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007888 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007889
Matt Carlsonba1142e2011-11-04 09:15:00 +00007890 tg3_tx_skb_unmap(tnapi, i,
7891 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007892
7893 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007894 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007895 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007896 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007897}
7898
7899/* Initialize tx/rx rings for packet processing.
7900 *
7901 * The chip has been shut down and the driver detached from
7902 * the networking, so no interrupts or new tx packets will
7903 * end up in the driver. tp->{tx,}lock are held and thus
7904 * we may not sleep.
7905 */
7906static int tg3_init_rings(struct tg3 *tp)
7907{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007908 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007909
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007910 /* Free up all the SKBs. */
7911 tg3_free_rings(tp);
7912
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007913 for (i = 0; i < tp->irq_cnt; i++) {
7914 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007915
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007916 tnapi->last_tag = 0;
7917 tnapi->last_irq_tag = 0;
7918 tnapi->hw_status->status = 0;
7919 tnapi->hw_status->status_tag = 0;
7920 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7921
7922 tnapi->tx_prod = 0;
7923 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007924 if (tnapi->tx_ring)
7925 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007926
7927 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007928 if (tnapi->rx_rcb)
7929 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007930
Matt Carlson8fea32b2010-09-15 08:59:58 +00007931 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007932 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007933 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007934 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007935 }
Matt Carlson72334482009-08-28 14:03:01 +00007936
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007937 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007938}
7939
Michael Chan49a359e2012-09-28 07:12:37 +00007940static void tg3_mem_tx_release(struct tg3 *tp)
7941{
7942 int i;
7943
7944 for (i = 0; i < tp->irq_max; i++) {
7945 struct tg3_napi *tnapi = &tp->napi[i];
7946
7947 if (tnapi->tx_ring) {
7948 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
7949 tnapi->tx_ring, tnapi->tx_desc_mapping);
7950 tnapi->tx_ring = NULL;
7951 }
7952
7953 kfree(tnapi->tx_buffers);
7954 tnapi->tx_buffers = NULL;
7955 }
7956}
7957
7958static int tg3_mem_tx_acquire(struct tg3 *tp)
7959{
7960 int i;
7961 struct tg3_napi *tnapi = &tp->napi[0];
7962
7963 /* If multivector TSS is enabled, vector 0 does not handle
7964 * tx interrupts. Don't allocate any resources for it.
7965 */
7966 if (tg3_flag(tp, ENABLE_TSS))
7967 tnapi++;
7968
7969 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
7970 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
7971 TG3_TX_RING_SIZE, GFP_KERNEL);
7972 if (!tnapi->tx_buffers)
7973 goto err_out;
7974
7975 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7976 TG3_TX_RING_BYTES,
7977 &tnapi->tx_desc_mapping,
7978 GFP_KERNEL);
7979 if (!tnapi->tx_ring)
7980 goto err_out;
7981 }
7982
7983 return 0;
7984
7985err_out:
7986 tg3_mem_tx_release(tp);
7987 return -ENOMEM;
7988}
7989
7990static void tg3_mem_rx_release(struct tg3 *tp)
7991{
7992 int i;
7993
7994 for (i = 0; i < tp->irq_max; i++) {
7995 struct tg3_napi *tnapi = &tp->napi[i];
7996
7997 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7998
7999 if (!tnapi->rx_rcb)
8000 continue;
8001
8002 dma_free_coherent(&tp->pdev->dev,
8003 TG3_RX_RCB_RING_BYTES(tp),
8004 tnapi->rx_rcb,
8005 tnapi->rx_rcb_mapping);
8006 tnapi->rx_rcb = NULL;
8007 }
8008}
8009
8010static int tg3_mem_rx_acquire(struct tg3 *tp)
8011{
8012 unsigned int i, limit;
8013
8014 limit = tp->rxq_cnt;
8015
8016 /* If RSS is enabled, we need a (dummy) producer ring
8017 * set on vector zero. This is the true hw prodring.
8018 */
8019 if (tg3_flag(tp, ENABLE_RSS))
8020 limit++;
8021
8022 for (i = 0; i < limit; i++) {
8023 struct tg3_napi *tnapi = &tp->napi[i];
8024
8025 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8026 goto err_out;
8027
8028 /* If multivector RSS is enabled, vector 0
8029 * does not handle rx or tx interrupts.
8030 * Don't allocate any resources for it.
8031 */
8032 if (!i && tg3_flag(tp, ENABLE_RSS))
8033 continue;
8034
8035 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
8036 TG3_RX_RCB_RING_BYTES(tp),
8037 &tnapi->rx_rcb_mapping,
8038 GFP_KERNEL);
8039 if (!tnapi->rx_rcb)
8040 goto err_out;
8041
8042 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
8043 }
8044
8045 return 0;
8046
8047err_out:
8048 tg3_mem_rx_release(tp);
8049 return -ENOMEM;
8050}
8051
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008052/*
8053 * Must not be invoked with interrupt sources disabled and
8054 * the hardware shutdown down.
8055 */
8056static void tg3_free_consistent(struct tg3 *tp)
8057{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008058 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008059
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008060 for (i = 0; i < tp->irq_cnt; i++) {
8061 struct tg3_napi *tnapi = &tp->napi[i];
8062
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008063 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008064 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8065 tnapi->hw_status,
8066 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008067 tnapi->hw_status = NULL;
8068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008069 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008070
Michael Chan49a359e2012-09-28 07:12:37 +00008071 tg3_mem_rx_release(tp);
8072 tg3_mem_tx_release(tp);
8073
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008075 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8076 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077 tp->hw_stats = NULL;
8078 }
8079}
8080
8081/*
8082 * Must not be invoked with interrupt sources disabled and
8083 * the hardware shutdown down. Can sleep.
8084 */
8085static int tg3_alloc_consistent(struct tg3 *tp)
8086{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008087 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008088
Matt Carlson4bae65c2010-11-24 08:31:52 +00008089 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8090 sizeof(struct tg3_hw_stats),
8091 &tp->stats_mapping,
8092 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093 if (!tp->hw_stats)
8094 goto err_out;
8095
Linus Torvalds1da177e2005-04-16 15:20:36 -07008096 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8097
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008098 for (i = 0; i < tp->irq_cnt; i++) {
8099 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008100 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008101
Matt Carlson4bae65c2010-11-24 08:31:52 +00008102 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8103 TG3_HW_STATUS_SIZE,
8104 &tnapi->status_mapping,
8105 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008106 if (!tnapi->hw_status)
8107 goto err_out;
8108
8109 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008110 sblk = tnapi->hw_status;
8111
Michael Chan49a359e2012-09-28 07:12:37 +00008112 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008113 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008114
Michael Chan49a359e2012-09-28 07:12:37 +00008115 /*
8116 * When RSS is enabled, the status block format changes
8117 * slightly. The "rx_jumbo_consumer", "reserved",
8118 * and "rx_mini_consumer" members get mapped to the
8119 * other three rx return ring producer indexes.
8120 */
8121 switch (i) {
8122 case 1:
8123 prodptr = &sblk->idx[0].rx_producer;
8124 break;
8125 case 2:
8126 prodptr = &sblk->rx_jumbo_consumer;
8127 break;
8128 case 3:
8129 prodptr = &sblk->reserved;
8130 break;
8131 case 4:
8132 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008133 break;
8134 }
Michael Chan49a359e2012-09-28 07:12:37 +00008135 tnapi->rx_rcb_prod_idx = prodptr;
8136 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008137 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008138 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008139 }
8140
Michael Chan49a359e2012-09-28 07:12:37 +00008141 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8142 goto err_out;
8143
Linus Torvalds1da177e2005-04-16 15:20:36 -07008144 return 0;
8145
8146err_out:
8147 tg3_free_consistent(tp);
8148 return -ENOMEM;
8149}
8150
8151#define MAX_WAIT_CNT 1000
8152
8153/* To stop a block, clear the enable bit and poll till it
8154 * clears. tp->lock is held.
8155 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008156static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008157{
8158 unsigned int i;
8159 u32 val;
8160
Joe Perches63c3a662011-04-26 08:12:10 +00008161 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162 switch (ofs) {
8163 case RCVLSC_MODE:
8164 case DMAC_MODE:
8165 case MBFREE_MODE:
8166 case BUFMGR_MODE:
8167 case MEMARB_MODE:
8168 /* We can't enable/disable these bits of the
8169 * 5705/5750, just say success.
8170 */
8171 return 0;
8172
8173 default:
8174 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008176 }
8177
8178 val = tr32(ofs);
8179 val &= ~enable_bit;
8180 tw32_f(ofs, val);
8181
8182 for (i = 0; i < MAX_WAIT_CNT; i++) {
8183 udelay(100);
8184 val = tr32(ofs);
8185 if ((val & enable_bit) == 0)
8186 break;
8187 }
8188
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008189 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008190 dev_err(&tp->pdev->dev,
8191 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8192 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193 return -ENODEV;
8194 }
8195
8196 return 0;
8197}
8198
8199/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008200static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201{
8202 int i, err;
8203
8204 tg3_disable_ints(tp);
8205
8206 tp->rx_mode &= ~RX_MODE_ENABLE;
8207 tw32_f(MAC_RX_MODE, tp->rx_mode);
8208 udelay(10);
8209
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008210 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8211 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8212 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8213 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8214 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8215 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008217 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8218 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8219 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8220 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8221 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8222 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8223 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224
8225 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8226 tw32_f(MAC_MODE, tp->mac_mode);
8227 udelay(40);
8228
8229 tp->tx_mode &= ~TX_MODE_ENABLE;
8230 tw32_f(MAC_TX_MODE, tp->tx_mode);
8231
8232 for (i = 0; i < MAX_WAIT_CNT; i++) {
8233 udelay(100);
8234 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8235 break;
8236 }
8237 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008238 dev_err(&tp->pdev->dev,
8239 "%s timed out, TX_MODE_ENABLE will not clear "
8240 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008241 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242 }
8243
Michael Chane6de8ad2005-05-05 14:42:41 -07008244 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008245 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8246 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008247
8248 tw32(FTQ_RESET, 0xffffffff);
8249 tw32(FTQ_RESET, 0x00000000);
8250
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008251 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8252 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008254 for (i = 0; i < tp->irq_cnt; i++) {
8255 struct tg3_napi *tnapi = &tp->napi[i];
8256 if (tnapi->hw_status)
8257 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260 return err;
8261}
8262
Michael Chanee6a99b2007-07-18 21:49:10 -07008263/* Save PCI command register before chip reset */
8264static void tg3_save_pci_state(struct tg3 *tp)
8265{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008266 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008267}
8268
8269/* Restore PCI state after chip reset */
8270static void tg3_restore_pci_state(struct tg3 *tp)
8271{
8272 u32 val;
8273
8274 /* Re-enable indirect register accesses. */
8275 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8276 tp->misc_host_ctrl);
8277
8278 /* Set MAX PCI retry to zero. */
8279 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008280 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008281 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008282 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008283 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008284 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008285 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008286 PCISTATE_ALLOW_APE_SHMEM_WR |
8287 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008288 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8289
Matt Carlson8a6eac92007-10-21 16:17:55 -07008290 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008291
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008292 if (!tg3_flag(tp, PCI_EXPRESS)) {
8293 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8294 tp->pci_cacheline_sz);
8295 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8296 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008297 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008298
Michael Chanee6a99b2007-07-18 21:49:10 -07008299 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008300 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008301 u16 pcix_cmd;
8302
8303 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8304 &pcix_cmd);
8305 pcix_cmd &= ~PCI_X_CMD_ERO;
8306 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8307 pcix_cmd);
8308 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008309
Joe Perches63c3a662011-04-26 08:12:10 +00008310 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008311
8312 /* Chip reset on 5780 will reset MSI enable bit,
8313 * so need to restore it.
8314 */
Joe Perches63c3a662011-04-26 08:12:10 +00008315 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008316 u16 ctrl;
8317
8318 pci_read_config_word(tp->pdev,
8319 tp->msi_cap + PCI_MSI_FLAGS,
8320 &ctrl);
8321 pci_write_config_word(tp->pdev,
8322 tp->msi_cap + PCI_MSI_FLAGS,
8323 ctrl | PCI_MSI_FLAGS_ENABLE);
8324 val = tr32(MSGINT_MODE);
8325 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8326 }
8327 }
8328}
8329
Linus Torvalds1da177e2005-04-16 15:20:36 -07008330/* tp->lock is held. */
8331static int tg3_chip_reset(struct tg3 *tp)
8332{
8333 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008334 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008335 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008336
David S. Millerf49639e2006-06-09 11:58:36 -07008337 tg3_nvram_lock(tp);
8338
Matt Carlson77b483f2008-08-15 14:07:24 -07008339 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8340
David S. Millerf49639e2006-06-09 11:58:36 -07008341 /* No matching tg3_nvram_unlock() after this because
8342 * chip reset below will undo the nvram lock.
8343 */
8344 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008345
Michael Chanee6a99b2007-07-18 21:49:10 -07008346 /* GRC_MISC_CFG core clock reset will clear the memory
8347 * enable bit in PCI register 4 and the MSI enable bit
8348 * on some chips, so we save relevant registers here.
8349 */
8350 tg3_save_pci_state(tp);
8351
Joe Perches41535772013-02-16 11:20:04 +00008352 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008353 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08008354 tw32(GRC_FASTBOOT_PC, 0);
8355
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356 /*
8357 * We must avoid the readl() that normally takes place.
8358 * It locks machines, causes machine checks, and other
8359 * fun things. So, temporarily disable the 5701
8360 * hardware workaround, while we do the reset.
8361 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008362 write_op = tp->write32;
8363 if (write_op == tg3_write_flush_reg32)
8364 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365
Michael Chand18edcb2007-03-24 20:57:11 -07008366 /* Prevent the irq handler from reading or writing PCI registers
8367 * during chip reset when the memory enable bit in the PCI command
8368 * register may be cleared. The chip does not generate interrupt
8369 * at this time, but the irq handler may still be called due to irq
8370 * sharing or irqpoll.
8371 */
Joe Perches63c3a662011-04-26 08:12:10 +00008372 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008373 for (i = 0; i < tp->irq_cnt; i++) {
8374 struct tg3_napi *tnapi = &tp->napi[i];
8375 if (tnapi->hw_status) {
8376 tnapi->hw_status->status = 0;
8377 tnapi->hw_status->status_tag = 0;
8378 }
8379 tnapi->last_tag = 0;
8380 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008381 }
Michael Chand18edcb2007-03-24 20:57:11 -07008382 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008383
8384 for (i = 0; i < tp->irq_cnt; i++)
8385 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008386
Joe Perches41535772013-02-16 11:20:04 +00008387 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00008388 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8389 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8390 }
8391
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392 /* do the reset */
8393 val = GRC_MISC_CFG_CORECLK_RESET;
8394
Joe Perches63c3a662011-04-26 08:12:10 +00008395 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008396 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00008397 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008398 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008399 tr32(TG3_PCIE_PHY_TSTCTL) ==
8400 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8401 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8402
Joe Perches41535772013-02-16 11:20:04 +00008403 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 tw32(GRC_MISC_CFG, (1 << 29));
8405 val |= (1 << 29);
8406 }
8407 }
8408
Joe Perches41535772013-02-16 11:20:04 +00008409 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07008410 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8411 tw32(GRC_VCPU_EXT_CTRL,
8412 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8413 }
8414
Matt Carlsonf37500d2010-08-02 11:25:59 +00008415 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008416 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008418
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419 tw32(GRC_MISC_CFG, val);
8420
Michael Chan1ee582d2005-08-09 20:16:46 -07008421 /* restore 5701 hardware bug workaround write method */
8422 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423
8424 /* Unfortunately, we have to delay before the PCI read back.
8425 * Some 575X chips even will not respond to a PCI cfg access
8426 * when the reset command is given to the chip.
8427 *
8428 * How do these hardware designers expect things to work
8429 * properly if the PCI write is posted for a long period
8430 * of time? It is always necessary to have some method by
8431 * which a register read back can occur to push the write
8432 * out which does the reset.
8433 *
8434 * For most tg3 variants the trick below was working.
8435 * Ho hum...
8436 */
8437 udelay(120);
8438
8439 /* Flush PCI posted writes. The normal MMIO registers
8440 * are inaccessible at this time so this is the only
8441 * way to make this reliably (actually, this is no longer
8442 * the case, see above). I tried to use indirect
8443 * register read/write but this upset some 5701 variants.
8444 */
8445 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8446
8447 udelay(120);
8448
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008449 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008450 u16 val16;
8451
Joe Perches41535772013-02-16 11:20:04 +00008452 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008453 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454 u32 cfg_val;
8455
8456 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008457 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458 udelay(100);
8459
8460 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8461 pci_write_config_dword(tp->pdev, 0xc4,
8462 cfg_val | (1 << 15));
8463 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008464
Matt Carlsone7126992009-08-25 10:08:16 +00008465 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008466 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008467 /*
8468 * Older PCIe devices only support the 128 byte
8469 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008470 */
Joe Perches63c3a662011-04-26 08:12:10 +00008471 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008472 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8473 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008474
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008475 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008476 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008477 PCI_EXP_DEVSTA_CED |
8478 PCI_EXP_DEVSTA_NFED |
8479 PCI_EXP_DEVSTA_FED |
8480 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481 }
8482
Michael Chanee6a99b2007-07-18 21:49:10 -07008483 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484
Joe Perches63c3a662011-04-26 08:12:10 +00008485 tg3_flag_clear(tp, CHIP_RESETTING);
8486 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008487
Michael Chanee6a99b2007-07-18 21:49:10 -07008488 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008489 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008490 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008491 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492
Joe Perches41535772013-02-16 11:20:04 +00008493 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494 tg3_stop_fw(tp);
8495 tw32(0x5000, 0x400);
8496 }
8497
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00008498 if (tg3_flag(tp, IS_SSB_CORE)) {
8499 /*
8500 * BCM4785: In order to avoid repercussions from using
8501 * potentially defective internal ROM, stop the Rx RISC CPU,
8502 * which is not required.
8503 */
8504 tg3_stop_fw(tp);
8505 tg3_halt_cpu(tp, RX_CPU_BASE);
8506 }
8507
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508 tw32(GRC_MODE, tp->grc_mode);
8509
Joe Perches41535772013-02-16 11:20:04 +00008510 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008511 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512
8513 tw32(0xc4, val | (1 << 15));
8514 }
8515
8516 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00008517 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008518 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00008519 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8521 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8522 }
8523
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008524 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008525 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008526 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008527 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008528 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008529 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008531 val = 0;
8532
8533 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534 udelay(40);
8535
Matt Carlson77b483f2008-08-15 14:07:24 -07008536 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8537
Michael Chan7a6f4362006-09-27 16:03:31 -07008538 err = tg3_poll_fw(tp);
8539 if (err)
8540 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541
Matt Carlson0a9140c2009-08-28 12:27:50 +00008542 tg3_mdio_start(tp);
8543
Joe Perches63c3a662011-04-26 08:12:10 +00008544 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00008545 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
8546 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008547 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008548 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549
8550 tw32(0x7c00, val | (1 << 25));
8551 }
8552
Joe Perches41535772013-02-16 11:20:04 +00008553 if (tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsond78b59f2011-04-05 14:22:46 +00008554 val = tr32(TG3_CPMU_CLCK_ORIDE);
8555 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8556 }
8557
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008559 tg3_flag_clear(tp, ENABLE_ASF);
8560 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8562 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8563 u32 nic_cfg;
8564
8565 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8566 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008567 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008568 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008569 if (tg3_flag(tp, 5750_PLUS))
8570 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571 }
8572 }
8573
8574 return 0;
8575}
8576
Matt Carlson65ec6982012-02-28 23:33:37 +00008577static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8578static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008579
Linus Torvalds1da177e2005-04-16 15:20:36 -07008580/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008581static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582{
8583 int err;
8584
8585 tg3_stop_fw(tp);
8586
Michael Chan944d9802005-05-29 14:57:48 -07008587 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008589 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590 err = tg3_chip_reset(tp);
8591
Matt Carlsondaba2a62009-04-20 06:58:52 +00008592 __tg3_set_mac_addr(tp, 0);
8593
Michael Chan944d9802005-05-29 14:57:48 -07008594 tg3_write_sig_legacy(tp, kind);
8595 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008596
Matt Carlson92feeab2011-12-08 14:40:14 +00008597 if (tp->hw_stats) {
8598 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008599 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008600 tg3_get_estats(tp, &tp->estats_prev);
8601
8602 /* And make sure the next sample is new data */
8603 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8604 }
8605
Linus Torvalds1da177e2005-04-16 15:20:36 -07008606 if (err)
8607 return err;
8608
8609 return 0;
8610}
8611
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612static int tg3_set_mac_addr(struct net_device *dev, void *p)
8613{
8614 struct tg3 *tp = netdev_priv(dev);
8615 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008616 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617
Michael Chanf9804dd2005-09-27 12:13:10 -07008618 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008619 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008620
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8622
Michael Chane75f7c92006-03-20 21:33:26 -08008623 if (!netif_running(dev))
8624 return 0;
8625
Joe Perches63c3a662011-04-26 08:12:10 +00008626 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008627 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008628
Michael Chan986e0ae2007-05-05 12:10:20 -07008629 addr0_high = tr32(MAC_ADDR_0_HIGH);
8630 addr0_low = tr32(MAC_ADDR_0_LOW);
8631 addr1_high = tr32(MAC_ADDR_1_HIGH);
8632 addr1_low = tr32(MAC_ADDR_1_LOW);
8633
8634 /* Skip MAC addr 1 if ASF is using it. */
8635 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8636 !(addr1_high == 0 && addr1_low == 0))
8637 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008638 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008639 spin_lock_bh(&tp->lock);
8640 __tg3_set_mac_addr(tp, skip_mac_1);
8641 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642
Michael Chanb9ec6c12006-07-25 16:37:27 -07008643 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644}
8645
8646/* tp->lock is held. */
8647static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8648 dma_addr_t mapping, u32 maxlen_flags,
8649 u32 nic_addr)
8650{
8651 tg3_write_mem(tp,
8652 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8653 ((u64) mapping >> 32));
8654 tg3_write_mem(tp,
8655 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8656 ((u64) mapping & 0xffffffff));
8657 tg3_write_mem(tp,
8658 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8659 maxlen_flags);
8660
Joe Perches63c3a662011-04-26 08:12:10 +00008661 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008662 tg3_write_mem(tp,
8663 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8664 nic_addr);
8665}
8666
Michael Chana489b6d2012-09-28 07:12:39 +00008667
8668static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008669{
Michael Chana489b6d2012-09-28 07:12:39 +00008670 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00008671
Joe Perches63c3a662011-04-26 08:12:10 +00008672 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008673 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8674 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8675 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008676 } else {
8677 tw32(HOSTCC_TXCOL_TICKS, 0);
8678 tw32(HOSTCC_TXMAX_FRAMES, 0);
8679 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00008680
8681 for (; i < tp->txq_cnt; i++) {
8682 u32 reg;
8683
8684 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8685 tw32(reg, ec->tx_coalesce_usecs);
8686 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8687 tw32(reg, ec->tx_max_coalesced_frames);
8688 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8689 tw32(reg, ec->tx_max_coalesced_frames_irq);
8690 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00008691 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008692
Michael Chana489b6d2012-09-28 07:12:39 +00008693 for (; i < tp->irq_max - 1; i++) {
8694 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8695 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8696 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8697 }
8698}
8699
8700static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
8701{
8702 int i = 0;
8703 u32 limit = tp->rxq_cnt;
8704
Joe Perches63c3a662011-04-26 08:12:10 +00008705 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008706 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8707 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8708 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00008709 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008710 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008711 tw32(HOSTCC_RXCOL_TICKS, 0);
8712 tw32(HOSTCC_RXMAX_FRAMES, 0);
8713 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008714 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008715
Michael Chana489b6d2012-09-28 07:12:39 +00008716 for (; i < limit; i++) {
8717 u32 reg;
8718
8719 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8720 tw32(reg, ec->rx_coalesce_usecs);
8721 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8722 tw32(reg, ec->rx_max_coalesced_frames);
8723 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8724 tw32(reg, ec->rx_max_coalesced_frames_irq);
8725 }
8726
8727 for (; i < tp->irq_max - 1; i++) {
8728 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
8729 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
8730 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8731 }
8732}
8733
8734static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
8735{
8736 tg3_coal_tx_init(tp, ec);
8737 tg3_coal_rx_init(tp, ec);
8738
Joe Perches63c3a662011-04-26 08:12:10 +00008739 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008740 u32 val = ec->stats_block_coalesce_usecs;
8741
Matt Carlsonb6080e12009-09-01 13:12:00 +00008742 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8743 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8744
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00008745 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07008746 val = 0;
8747
8748 tw32(HOSTCC_STAT_COAL_TICKS, val);
8749 }
8750}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751
8752/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008753static void tg3_rings_reset(struct tg3 *tp)
8754{
8755 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008756 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008757 struct tg3_napi *tnapi = &tp->napi[0];
8758
8759 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008760 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008761 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008762 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008763 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Michael Chanc65a17f2013-01-06 12:51:07 +00008764 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00008765 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonb703df62009-12-03 08:36:21 +00008766 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008767 else
8768 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8769
8770 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8771 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8772 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8773 BDINFO_FLAGS_DISABLED);
8774
8775
8776 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008777 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008778 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008779 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008780 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches41535772013-02-16 11:20:04 +00008781 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
8782 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008783 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008784 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8785 else
8786 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8787
8788 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8789 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8790 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8791 BDINFO_FLAGS_DISABLED);
8792
8793 /* Disable interrupts */
8794 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008795 tp->napi[0].chk_msi_cnt = 0;
8796 tp->napi[0].last_rx_cons = 0;
8797 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008798
8799 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008800 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008801 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008802 tp->napi[i].tx_prod = 0;
8803 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008804 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008805 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008806 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8807 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008808 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008809 tp->napi[i].last_rx_cons = 0;
8810 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008811 }
Joe Perches63c3a662011-04-26 08:12:10 +00008812 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008813 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008814 } else {
8815 tp->napi[0].tx_prod = 0;
8816 tp->napi[0].tx_cons = 0;
8817 tw32_mailbox(tp->napi[0].prodmbox, 0);
8818 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8819 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008820
8821 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008822 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008823 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8824 for (i = 0; i < 16; i++)
8825 tw32_tx_mbox(mbox + i * 8, 0);
8826 }
8827
8828 txrcb = NIC_SRAM_SEND_RCB;
8829 rxrcb = NIC_SRAM_RCV_RET_RCB;
8830
8831 /* Clear status block in ram. */
8832 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8833
8834 /* Set status block DMA address */
8835 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8836 ((u64) tnapi->status_mapping >> 32));
8837 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8838 ((u64) tnapi->status_mapping & 0xffffffff));
8839
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008840 if (tnapi->tx_ring) {
8841 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8842 (TG3_TX_RING_SIZE <<
8843 BDINFO_FLAGS_MAXLEN_SHIFT),
8844 NIC_SRAM_TX_BUFFER_DESC);
8845 txrcb += TG3_BDINFO_SIZE;
8846 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008847
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008848 if (tnapi->rx_rcb) {
8849 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008850 (tp->rx_ret_ring_mask + 1) <<
8851 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008852 rxrcb += TG3_BDINFO_SIZE;
8853 }
8854
8855 stblk = HOSTCC_STATBLCK_RING1;
8856
8857 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8858 u64 mapping = (u64)tnapi->status_mapping;
8859 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8860 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8861
8862 /* Clear status block in ram. */
8863 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8864
Matt Carlson19cfaec2009-12-03 08:36:20 +00008865 if (tnapi->tx_ring) {
8866 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8867 (TG3_TX_RING_SIZE <<
8868 BDINFO_FLAGS_MAXLEN_SHIFT),
8869 NIC_SRAM_TX_BUFFER_DESC);
8870 txrcb += TG3_BDINFO_SIZE;
8871 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008872
8873 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008874 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008875 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8876
8877 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008878 rxrcb += TG3_BDINFO_SIZE;
8879 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008880}
8881
Matt Carlsoneb07a942011-04-20 07:57:36 +00008882static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8883{
8884 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8885
Joe Perches63c3a662011-04-26 08:12:10 +00008886 if (!tg3_flag(tp, 5750_PLUS) ||
8887 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00008888 tg3_asic_rev(tp) == ASIC_REV_5750 ||
8889 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008890 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008891 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00008892 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
8893 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00008894 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8895 else
8896 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8897
8898 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8899 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8900
8901 val = min(nic_rep_thresh, host_rep_thresh);
8902 tw32(RCVBDI_STD_THRESH, val);
8903
Joe Perches63c3a662011-04-26 08:12:10 +00008904 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008905 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8906
Joe Perches63c3a662011-04-26 08:12:10 +00008907 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008908 return;
8909
Matt Carlson513aa6e2011-11-21 15:01:18 +00008910 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008911
8912 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8913
8914 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8915 tw32(RCVBDI_JUMBO_THRESH, val);
8916
Joe Perches63c3a662011-04-26 08:12:10 +00008917 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008918 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8919}
8920
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008921static inline u32 calc_crc(unsigned char *buf, int len)
8922{
8923 u32 reg;
8924 u32 tmp;
8925 int j, k;
8926
8927 reg = 0xffffffff;
8928
8929 for (j = 0; j < len; j++) {
8930 reg ^= buf[j];
8931
8932 for (k = 0; k < 8; k++) {
8933 tmp = reg & 0x01;
8934
8935 reg >>= 1;
8936
8937 if (tmp)
8938 reg ^= 0xedb88320;
8939 }
8940 }
8941
8942 return ~reg;
8943}
8944
8945static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8946{
8947 /* accept or reject all multicast frames */
8948 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8949 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8950 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8951 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8952}
8953
8954static void __tg3_set_rx_mode(struct net_device *dev)
8955{
8956 struct tg3 *tp = netdev_priv(dev);
8957 u32 rx_mode;
8958
8959 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8960 RX_MODE_KEEP_VLAN_TAG);
8961
8962#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8963 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8964 * flag clear.
8965 */
8966 if (!tg3_flag(tp, ENABLE_ASF))
8967 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8968#endif
8969
8970 if (dev->flags & IFF_PROMISC) {
8971 /* Promiscuous mode. */
8972 rx_mode |= RX_MODE_PROMISC;
8973 } else if (dev->flags & IFF_ALLMULTI) {
8974 /* Accept all multicast. */
8975 tg3_set_multi(tp, 1);
8976 } else if (netdev_mc_empty(dev)) {
8977 /* Reject all multicast. */
8978 tg3_set_multi(tp, 0);
8979 } else {
8980 /* Accept one or more multicast(s). */
8981 struct netdev_hw_addr *ha;
8982 u32 mc_filter[4] = { 0, };
8983 u32 regidx;
8984 u32 bit;
8985 u32 crc;
8986
8987 netdev_for_each_mc_addr(ha, dev) {
8988 crc = calc_crc(ha->addr, ETH_ALEN);
8989 bit = ~crc & 0x7f;
8990 regidx = (bit & 0x60) >> 5;
8991 bit &= 0x1f;
8992 mc_filter[regidx] |= (1 << bit);
8993 }
8994
8995 tw32(MAC_HASH_REG_0, mc_filter[0]);
8996 tw32(MAC_HASH_REG_1, mc_filter[1]);
8997 tw32(MAC_HASH_REG_2, mc_filter[2]);
8998 tw32(MAC_HASH_REG_3, mc_filter[3]);
8999 }
9000
9001 if (rx_mode != tp->rx_mode) {
9002 tp->rx_mode = rx_mode;
9003 tw32_f(MAC_RX_MODE, rx_mode);
9004 udelay(10);
9005 }
9006}
9007
Michael Chan91024262012-09-28 07:12:38 +00009008static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009009{
9010 int i;
9011
9012 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009013 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009014}
9015
9016static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009017{
9018 int i;
9019
9020 if (!tg3_flag(tp, SUPPORT_MSIX))
9021 return;
9022
Michael Chan0b3ba052012-11-14 14:44:29 +00009023 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009024 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009025 return;
9026 }
9027
9028 /* Validate table against current IRQ count */
9029 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009030 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009031 break;
9032 }
9033
9034 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009035 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009036}
9037
Matt Carlson90415472011-12-16 13:33:23 +00009038static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009039{
9040 int i = 0;
9041 u32 reg = MAC_RSS_INDIR_TBL_0;
9042
9043 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9044 u32 val = tp->rss_ind_tbl[i];
9045 i++;
9046 for (; i % 8; i++) {
9047 val <<= 4;
9048 val |= tp->rss_ind_tbl[i];
9049 }
9050 tw32(reg, val);
9051 reg += 4;
9052 }
9053}
9054
Matt Carlson2d31eca2009-09-01 12:53:31 +00009055/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009056static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009057{
9058 u32 val, rdmac_mode;
9059 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009060 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009061
9062 tg3_disable_ints(tp);
9063
9064 tg3_stop_fw(tp);
9065
9066 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9067
Joe Perches63c3a662011-04-26 08:12:10 +00009068 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009069 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009070
Matt Carlson699c0192010-12-06 08:28:51 +00009071 /* Enable MAC control of LPI */
9072 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009073 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
9074 TG3_CPMU_EEE_LNKIDL_UART_IDL;
Joe Perches41535772013-02-16 11:20:04 +00009075 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00009076 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
9077
9078 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
Matt Carlson699c0192010-12-06 08:28:51 +00009079
9080 tw32_f(TG3_CPMU_EEE_CTRL,
9081 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
9082
Matt Carlsona386b902010-12-06 08:28:53 +00009083 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
9084 TG3_CPMU_EEEMD_LPI_IN_TX |
9085 TG3_CPMU_EEEMD_LPI_IN_RX |
9086 TG3_CPMU_EEEMD_EEE_ENABLE;
9087
Joe Perches41535772013-02-16 11:20:04 +00009088 if (tg3_asic_rev(tp) != ASIC_REV_5717)
Matt Carlsona386b902010-12-06 08:28:53 +00009089 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
9090
Joe Perches63c3a662011-04-26 08:12:10 +00009091 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00009092 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
9093
9094 tw32_f(TG3_CPMU_EEE_MODE, val);
9095
9096 tw32_f(TG3_CPMU_EEE_DBTMR1,
9097 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
9098 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
9099
9100 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00009101 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00009102 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00009103 }
9104
Matt Carlson603f1172010-02-12 14:47:10 +00009105 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009106 tg3_phy_reset(tp);
9107
Linus Torvalds1da177e2005-04-16 15:20:36 -07009108 err = tg3_chip_reset(tp);
9109 if (err)
9110 return err;
9111
9112 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9113
Joe Perches41535772013-02-16 11:20:04 +00009114 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009115 val = tr32(TG3_CPMU_CTRL);
9116 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9117 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009118
9119 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9120 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9121 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9122 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9123
9124 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9125 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9126 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9127 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9128
9129 val = tr32(TG3_CPMU_HST_ACC);
9130 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9131 val |= CPMU_HST_ACC_MACCLK_6_25;
9132 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009133 }
9134
Joe Perches41535772013-02-16 11:20:04 +00009135 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009136 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9137 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9138 PCIE_PWR_MGMT_L1_THRESH_4MS;
9139 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009140
9141 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9142 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9143
9144 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009145
Matt Carlsonf40386c2009-11-02 14:24:02 +00009146 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9147 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009148 }
9149
Joe Perches63c3a662011-04-26 08:12:10 +00009150 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009151 u32 grc_mode = tr32(GRC_MODE);
9152
9153 /* Access the lower 1K of PL PCIE block registers. */
9154 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9155 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9156
9157 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9158 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9159 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9160
9161 tw32(GRC_MODE, grc_mode);
9162 }
9163
Matt Carlson55086ad2011-12-14 11:09:59 +00009164 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009165 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009166 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009167
Matt Carlson5093eed2010-11-24 08:31:45 +00009168 /* Access the lower 1K of PL PCIE block registers. */
9169 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9170 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009171
Matt Carlson5093eed2010-11-24 08:31:45 +00009172 val = tr32(TG3_PCIE_TLDLPL_PORT +
9173 TG3_PCIE_PL_LO_PHYCTL5);
9174 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9175 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009176
Matt Carlson5093eed2010-11-24 08:31:45 +00009177 tw32(GRC_MODE, grc_mode);
9178 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009179
Joe Perches41535772013-02-16 11:20:04 +00009180 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009181 u32 grc_mode;
9182
9183 /* Fix transmit hangs */
9184 val = tr32(TG3_CPMU_PADRNG_CTL);
9185 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9186 tw32(TG3_CPMU_PADRNG_CTL, val);
9187
9188 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009189
9190 /* Access the lower 1K of DL PCIE block registers. */
9191 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9192 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9193
9194 val = tr32(TG3_PCIE_TLDLPL_PORT +
9195 TG3_PCIE_DL_LO_FTSMAX);
9196 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9197 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9198 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9199
9200 tw32(GRC_MODE, grc_mode);
9201 }
9202
Matt Carlsona977dbe2010-04-12 06:58:26 +00009203 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9204 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9205 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9206 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009207 }
9208
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209 /* This works around an issue with Athlon chipsets on
9210 * B3 tigon3 silicon. This bit has no effect on any
9211 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009212 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009213 */
Joe Perches63c3a662011-04-26 08:12:10 +00009214 if (!tg3_flag(tp, CPMU_PRESENT)) {
9215 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009216 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9217 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219
Joe Perches41535772013-02-16 11:20:04 +00009220 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009221 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 val = tr32(TG3PCI_PCISTATE);
9223 val |= PCISTATE_RETRY_SAME_DMA;
9224 tw32(TG3PCI_PCISTATE, val);
9225 }
9226
Joe Perches63c3a662011-04-26 08:12:10 +00009227 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009228 /* Allow reads and writes to the
9229 * APE register and memory space.
9230 */
9231 val = tr32(TG3PCI_PCISTATE);
9232 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009233 PCISTATE_ALLOW_APE_SHMEM_WR |
9234 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009235 tw32(TG3PCI_PCISTATE, val);
9236 }
9237
Joe Perches41535772013-02-16 11:20:04 +00009238 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009239 /* Enable some hw fixes. */
9240 val = tr32(TG3PCI_MSI_DATA);
9241 val |= (1 << 26) | (1 << 28) | (1 << 29);
9242 tw32(TG3PCI_MSI_DATA, val);
9243 }
9244
9245 /* Descriptor ring init may make accesses to the
9246 * NIC SRAM area to setup the TX descriptors, so we
9247 * can only do this after the hardware has been
9248 * successfully reset.
9249 */
Michael Chan32d8c572006-07-25 16:38:29 -07009250 err = tg3_init_rings(tp);
9251 if (err)
9252 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253
Joe Perches63c3a662011-04-26 08:12:10 +00009254 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009255 val = tr32(TG3PCI_DMA_RW_CTRL) &
9256 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009257 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009258 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009259 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009260 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9261 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009262 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009263 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009264 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9265 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009266 /* This value is determined during the probe time DMA
9267 * engine test, tg3_test_dma.
9268 */
9269 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009271
9272 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9273 GRC_MODE_4X_NIC_SEND_RINGS |
9274 GRC_MODE_NO_TX_PHDR_CSUM |
9275 GRC_MODE_NO_RX_PHDR_CSUM);
9276 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009277
9278 /* Pseudo-header checksum is done by hardware logic and not
9279 * the offload processers, so make the chip do the pseudo-
9280 * header checksums on receive. For transmit it is more
9281 * convenient to do the pseudo-header checksum in software
9282 * as Linux does that on transmit for us in all cases.
9283 */
9284 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009285
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009286 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9287 if (tp->rxptpctl)
9288 tw32(TG3_RX_PTP_CTL,
9289 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9290
9291 if (tg3_flag(tp, PTP_CAPABLE))
9292 val |= GRC_MODE_TIME_SYNC_ENABLE;
9293
9294 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295
9296 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9297 val = tr32(GRC_MISC_CFG);
9298 val &= ~0xff;
9299 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9300 tw32(GRC_MISC_CFG, val);
9301
9302 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009303 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +00009305 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009306 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +00009307 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009308 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9309 else
9310 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9311 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9312 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009313 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314 int fw_len;
9315
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009316 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9318 tw32(BUFMGR_MB_POOL_ADDR,
9319 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9320 tw32(BUFMGR_MB_POOL_SIZE,
9321 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323
Michael Chan0f893dc2005-07-25 12:30:38 -07009324 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009325 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9326 tp->bufmgr_config.mbuf_read_dma_low_water);
9327 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9328 tp->bufmgr_config.mbuf_mac_rx_low_water);
9329 tw32(BUFMGR_MB_HIGH_WATER,
9330 tp->bufmgr_config.mbuf_high_water);
9331 } else {
9332 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9333 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9334 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9335 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9336 tw32(BUFMGR_MB_HIGH_WATER,
9337 tp->bufmgr_config.mbuf_high_water_jumbo);
9338 }
9339 tw32(BUFMGR_DMA_LOW_WATER,
9340 tp->bufmgr_config.dma_low_water);
9341 tw32(BUFMGR_DMA_HIGH_WATER,
9342 tp->bufmgr_config.dma_high_water);
9343
Matt Carlsond309a462010-09-30 10:34:31 +00009344 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009345 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +00009346 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +00009347 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
9348 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9349 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +00009350 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009351 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352 for (i = 0; i < 2000; i++) {
9353 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9354 break;
9355 udelay(10);
9356 }
9357 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009358 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009359 return -ENODEV;
9360 }
9361
Joe Perches41535772013-02-16 11:20:04 +00009362 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009363 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009364
Matt Carlsoneb07a942011-04-20 07:57:36 +00009365 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009366
9367 /* Initialize TG3_BDINFO's at:
9368 * RCVDBDI_STD_BD: standard eth size rx ring
9369 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9370 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9371 *
9372 * like so:
9373 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9374 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9375 * ring attribute flags
9376 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9377 *
9378 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9379 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9380 *
9381 * The size of each ring is fixed in the firmware, but the location is
9382 * configurable.
9383 */
9384 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009385 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009386 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009387 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009388 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009389 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9390 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009391
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009392 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009393 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009394 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9395 BDINFO_FLAGS_DISABLED);
9396
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009397 /* Program the jumbo buffer descriptor ring control
9398 * blocks on those devices that have them.
9399 */
Joe Perches41535772013-02-16 11:20:04 +00009400 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009401 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402
Joe Perches63c3a662011-04-26 08:12:10 +00009403 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009404 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009405 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009407 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009408 val = TG3_RX_JMB_RING_SIZE(tp) <<
9409 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009410 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009411 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009412 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +00009413 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009414 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +00009415 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9416 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417 } else {
9418 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9419 BDINFO_FLAGS_DISABLED);
9420 }
9421
Joe Perches63c3a662011-04-26 08:12:10 +00009422 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009423 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009424 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9425 val |= (TG3_RX_STD_DMA_SZ << 2);
9426 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009427 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009428 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009429 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009430
9431 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009432
Matt Carlson411da642009-11-13 13:03:46 +00009433 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00009434 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435
Joe Perches63c3a662011-04-26 08:12:10 +00009436 tpr->rx_jmb_prod_idx =
9437 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00009438 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439
Matt Carlson2d31eca2009-09-01 12:53:31 +00009440 tg3_rings_reset(tp);
9441
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07009443 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009444
9445 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009446 tw32(MAC_RX_MTU_SIZE,
9447 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448
9449 /* The slot time is changed by tg3_setup_phy if we
9450 * run at gigabit with half duplex.
9451 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009452 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9453 (6 << TX_LENGTHS_IPG_SHIFT) |
9454 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9455
Joe Perches41535772013-02-16 11:20:04 +00009456 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9457 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009458 val |= tr32(MAC_TX_LENGTHS) &
9459 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9460 TX_LENGTHS_CNT_DWN_VAL_MSK);
9461
9462 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463
9464 /* Receive rules. */
9465 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9466 tw32(RCVLPC_CONFIG, 0x0181);
9467
9468 /* Calculate RDMAC_MODE setting early, we need it to determine
9469 * the RCVLPC_STATE_ENABLE mask.
9470 */
9471 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9472 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9473 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9474 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9475 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009476
Joe Perches41535772013-02-16 11:20:04 +00009477 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009478 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9479
Joe Perches41535772013-02-16 11:20:04 +00009480 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
9481 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9482 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009483 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9484 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9485 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9486
Joe Perches41535772013-02-16 11:20:04 +00009487 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9488 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009489 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009490 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009491 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9492 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009493 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009494 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9495 }
9496 }
9497
Joe Perches63c3a662011-04-26 08:12:10 +00009498 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009499 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9500
Joe Perches41535772013-02-16 11:20:04 +00009501 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009502 tp->dma_limit = 0;
9503 if (tp->dev->mtu <= ETH_DATA_LEN) {
9504 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
9505 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
9506 }
9507 }
9508
Joe Perches63c3a662011-04-26 08:12:10 +00009509 if (tg3_flag(tp, HW_TSO_1) ||
9510 tg3_flag(tp, HW_TSO_2) ||
9511 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009512 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9513
Matt Carlson108a6c12011-05-19 12:12:47 +00009514 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009515 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9516 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -08009517 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009518
Joe Perches41535772013-02-16 11:20:04 +00009519 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9520 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009521 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9522
Joe Perches41535772013-02-16 11:20:04 +00009523 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
9524 tg3_asic_rev(tp) == ASIC_REV_5784 ||
9525 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9526 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009527 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009528 u32 tgtreg;
9529
Joe Perches41535772013-02-16 11:20:04 +00009530 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009531 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
9532 else
9533 tgtreg = TG3_RDMA_RSRVCTRL_REG;
9534
9535 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +00009536 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9537 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009538 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9539 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9540 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9541 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9542 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9543 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009544 }
Michael Chanc65a17f2013-01-06 12:51:07 +00009545 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009546 }
9547
Joe Perches41535772013-02-16 11:20:04 +00009548 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
9549 tg3_asic_rev(tp) == ASIC_REV_5720 ||
9550 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009551 u32 tgtreg;
9552
Joe Perches41535772013-02-16 11:20:04 +00009553 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009554 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
9555 else
9556 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
9557
9558 val = tr32(tgtreg);
9559 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +00009560 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9561 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9562 }
9563
Linus Torvalds1da177e2005-04-16 15:20:36 -07009564 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009565 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009566 val = tr32(RCVLPC_STATS_ENABLE);
9567 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9568 tw32(RCVLPC_STATS_ENABLE, val);
9569 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009570 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571 val = tr32(RCVLPC_STATS_ENABLE);
9572 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9573 tw32(RCVLPC_STATS_ENABLE, val);
9574 } else {
9575 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9576 }
9577 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9578 tw32(SNDDATAI_STATSENAB, 0xffffff);
9579 tw32(SNDDATAI_STATSCTRL,
9580 (SNDDATAI_SCTRL_ENABLE |
9581 SNDDATAI_SCTRL_FASTUPD));
9582
9583 /* Setup host coalescing engine. */
9584 tw32(HOSTCC_MODE, 0);
9585 for (i = 0; i < 2000; i++) {
9586 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9587 break;
9588 udelay(10);
9589 }
9590
Michael Chand244c892005-07-05 14:42:33 -07009591 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009592
Joe Perches63c3a662011-04-26 08:12:10 +00009593 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009594 /* Status/statistics block address. See tg3_timer,
9595 * the tg3_periodic_fetch_stats call there, and
9596 * tg3_get_stats to see how this works for 5705/5750 chips.
9597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009598 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9599 ((u64) tp->stats_mapping >> 32));
9600 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9601 ((u64) tp->stats_mapping & 0xffffffff));
9602 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009603
Linus Torvalds1da177e2005-04-16 15:20:36 -07009604 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009605
9606 /* Clear statistics and status block memory areas */
9607 for (i = NIC_SRAM_STATS_BLK;
9608 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9609 i += sizeof(u32)) {
9610 tg3_write_mem(tp, i, 0);
9611 udelay(40);
9612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009613 }
9614
9615 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9616
9617 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9618 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009619 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009620 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9621
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009622 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9623 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009624 /* reset to prevent losing 1st rx packet intermittently */
9625 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9626 udelay(10);
9627 }
9628
Matt Carlson3bda1252008-08-15 14:08:22 -07009629 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009630 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9631 MAC_MODE_FHDE_ENABLE;
9632 if (tg3_flag(tp, ENABLE_APE))
9633 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009634 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009635 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +00009636 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009637 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009638 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9639 udelay(40);
9640
Michael Chan314fba32005-04-21 17:07:04 -07009641 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009642 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009643 * register to preserve the GPIO settings for LOMs. The GPIOs,
9644 * whether used as inputs or outputs, are set by boot code after
9645 * reset.
9646 */
Joe Perches63c3a662011-04-26 08:12:10 +00009647 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009648 u32 gpio_mask;
9649
Michael Chan9d26e212006-12-07 00:21:14 -08009650 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9651 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9652 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009653
Joe Perches41535772013-02-16 11:20:04 +00009654 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -07009655 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9656 GRC_LCLCTRL_GPIO_OUTPUT3;
9657
Joe Perches41535772013-02-16 11:20:04 +00009658 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -08009659 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9660
Gary Zambranoaaf84462007-05-05 11:51:45 -07009661 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009662 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9663
9664 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009665 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009666 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9667 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009669 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9670 udelay(100);
9671
Matt Carlsonc3b50032012-01-17 15:27:23 +00009672 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009673 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009674 val |= MSGINT_MODE_ENABLE;
9675 if (tp->irq_cnt > 1)
9676 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009677 if (!tg3_flag(tp, 1SHOT_MSI))
9678 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009679 tw32(MSGINT_MODE, val);
9680 }
9681
Joe Perches63c3a662011-04-26 08:12:10 +00009682 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009683 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9684 udelay(40);
9685 }
9686
9687 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9688 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9689 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9690 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9691 WDMAC_MODE_LNGREAD_ENAB);
9692
Joe Perches41535772013-02-16 11:20:04 +00009693 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9694 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009695 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009696 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
9697 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698 /* nothing */
9699 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009700 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009701 val |= WDMAC_MODE_RX_ACCEL;
9702 }
9703 }
9704
Michael Chand9ab5ad2006-03-20 22:27:35 -08009705 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009706 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009707 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08009708
Joe Perches41535772013-02-16 11:20:04 +00009709 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +00009710 val |= WDMAC_MODE_BURST_ALL_DATA;
9711
Linus Torvalds1da177e2005-04-16 15:20:36 -07009712 tw32_f(WDMAC_MODE, val);
9713 udelay(40);
9714
Joe Perches63c3a662011-04-26 08:12:10 +00009715 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009716 u16 pcix_cmd;
9717
9718 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9719 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +00009720 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009721 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9722 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +00009723 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009724 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9725 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009726 }
Matt Carlson9974a352007-10-07 23:27:28 -07009727 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9728 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009729 }
9730
9731 tw32_f(RDMAC_MODE, rdmac_mode);
9732 udelay(40);
9733
Joe Perches41535772013-02-16 11:20:04 +00009734 if (tg3_asic_rev(tp) == ASIC_REV_5719) {
Michael Chan091f0ea2012-07-29 19:15:43 +00009735 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
9736 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
9737 break;
9738 }
9739 if (i < TG3_NUM_RDMA_CHANNELS) {
9740 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9741 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
9742 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9743 tg3_flag_set(tp, 5719_RDMA_BUG);
9744 }
9745 }
9746
Linus Torvalds1da177e2005-04-16 15:20:36 -07009747 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009748 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009749 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009750
Joe Perches41535772013-02-16 11:20:04 +00009751 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009752 tw32(SNDDATAC_MODE,
9753 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9754 else
9755 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9756
Linus Torvalds1da177e2005-04-16 15:20:36 -07009757 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9758 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009759 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009760 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009761 val |= RCVDBDI_MODE_LRG_RING_SZ;
9762 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009763 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009764 if (tg3_flag(tp, HW_TSO_1) ||
9765 tg3_flag(tp, HW_TSO_2) ||
9766 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009768 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009769 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009770 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9771 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9773
Joe Perches41535772013-02-16 11:20:04 +00009774 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009775 err = tg3_load_5701_a0_firmware_fix(tp);
9776 if (err)
9777 return err;
9778 }
9779
Joe Perches63c3a662011-04-26 08:12:10 +00009780 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009781 err = tg3_load_tso_firmware(tp);
9782 if (err)
9783 return err;
9784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785
9786 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009787
Joe Perches63c3a662011-04-26 08:12:10 +00009788 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009789 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +00009790 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009791
Joe Perches41535772013-02-16 11:20:04 +00009792 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9793 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00009794 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9795 tp->tx_mode &= ~val;
9796 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9797 }
9798
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 tw32_f(MAC_TX_MODE, tp->tx_mode);
9800 udelay(100);
9801
Joe Perches63c3a662011-04-26 08:12:10 +00009802 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009803 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009804
9805 /* Setup the "secret" hash key. */
9806 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9807 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9808 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9809 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9810 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9811 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9812 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9813 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9814 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9815 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9816 }
9817
Linus Torvalds1da177e2005-04-16 15:20:36 -07009818 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009819 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009820 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9821
Joe Perches63c3a662011-04-26 08:12:10 +00009822 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009823 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9824 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9825 RX_MODE_RSS_IPV6_HASH_EN |
9826 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9827 RX_MODE_RSS_IPV4_HASH_EN |
9828 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9829
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830 tw32_f(MAC_RX_MODE, tp->rx_mode);
9831 udelay(10);
9832
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833 tw32(MAC_LED_CTRL, tp->led_ctrl);
9834
9835 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009836 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9838 udelay(10);
9839 }
9840 tw32_f(MAC_RX_MODE, tp->rx_mode);
9841 udelay(10);
9842
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009843 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00009844 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
9845 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846 /* Set drive transmission level to 1.2V */
9847 /* only if the signal pre-emphasis bit is not set */
9848 val = tr32(MAC_SERDES_CFG);
9849 val &= 0xfffff000;
9850 val |= 0x880;
9851 tw32(MAC_SERDES_CFG, val);
9852 }
Joe Perches41535772013-02-16 11:20:04 +00009853 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854 tw32(MAC_SERDES_CFG, 0x616000);
9855 }
9856
9857 /* Prevent chip from dropping frames when flow control
9858 * is enabled.
9859 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009860 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009861 val = 1;
9862 else
9863 val = 2;
9864 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009865
Joe Perches41535772013-02-16 11:20:04 +00009866 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009867 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009868 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009869 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009870 }
9871
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009872 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +00009873 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009874 u32 tmp;
9875
9876 tmp = tr32(SERDES_RX_CTRL);
9877 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9878 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9879 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9880 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9881 }
9882
Joe Perches63c3a662011-04-26 08:12:10 +00009883 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009884 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +00009885 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886
Matt Carlsondd477002008-05-25 23:45:58 -07009887 err = tg3_setup_phy(tp, 0);
9888 if (err)
9889 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009891 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9892 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009893 u32 tmp;
9894
9895 /* Clear CRC stats. */
9896 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9897 tg3_writephy(tp, MII_TG3_TEST1,
9898 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009899 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901 }
9902 }
9903
9904 __tg3_set_rx_mode(tp->dev);
9905
9906 /* Initialize receive rules. */
9907 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9908 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9909 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9910 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9911
Joe Perches63c3a662011-04-26 08:12:10 +00009912 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009913 limit = 8;
9914 else
9915 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009916 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009917 limit -= 4;
9918 switch (limit) {
9919 case 16:
9920 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9921 case 15:
9922 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9923 case 14:
9924 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9925 case 13:
9926 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9927 case 12:
9928 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9929 case 11:
9930 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9931 case 10:
9932 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9933 case 9:
9934 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9935 case 8:
9936 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9937 case 7:
9938 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9939 case 6:
9940 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9941 case 5:
9942 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9943 case 4:
9944 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9945 case 3:
9946 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9947 case 2:
9948 case 1:
9949
9950 default:
9951 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953
Joe Perches63c3a662011-04-26 08:12:10 +00009954 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009955 /* Write our heartbeat update interval to APE. */
9956 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9957 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009958
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9960
Linus Torvalds1da177e2005-04-16 15:20:36 -07009961 return 0;
9962}
9963
9964/* Called at device open time to get the chip ready for
9965 * packet processing. Invoked with tp->lock held.
9966 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009967static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009969 tg3_switch_clocks(tp);
9970
9971 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9972
Matt Carlson2f751b62008-08-04 23:17:34 -07009973 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974}
9975
Michael Chanaed93e02012-07-16 16:24:02 +00009976static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
9977{
9978 int i;
9979
9980 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
9981 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
9982
9983 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
9984 off += len;
9985
9986 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
9987 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
9988 memset(ocir, 0, TG3_OCIR_LEN);
9989 }
9990}
9991
9992/* sysfs attributes for hwmon */
9993static ssize_t tg3_show_temp(struct device *dev,
9994 struct device_attribute *devattr, char *buf)
9995{
9996 struct pci_dev *pdev = to_pci_dev(dev);
9997 struct net_device *netdev = pci_get_drvdata(pdev);
9998 struct tg3 *tp = netdev_priv(netdev);
9999 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
10000 u32 temperature;
10001
10002 spin_lock_bh(&tp->lock);
10003 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10004 sizeof(temperature));
10005 spin_unlock_bh(&tp->lock);
10006 return sprintf(buf, "%u\n", temperature);
10007}
10008
10009
10010static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10011 TG3_TEMP_SENSOR_OFFSET);
10012static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10013 TG3_TEMP_CAUTION_OFFSET);
10014static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10015 TG3_TEMP_MAX_OFFSET);
10016
10017static struct attribute *tg3_attributes[] = {
10018 &sensor_dev_attr_temp1_input.dev_attr.attr,
10019 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10020 &sensor_dev_attr_temp1_max.dev_attr.attr,
10021 NULL
10022};
10023
10024static const struct attribute_group tg3_group = {
10025 .attrs = tg3_attributes,
10026};
10027
Michael Chanaed93e02012-07-16 16:24:02 +000010028static void tg3_hwmon_close(struct tg3 *tp)
10029{
Michael Chanaed93e02012-07-16 16:24:02 +000010030 if (tp->hwmon_dev) {
10031 hwmon_device_unregister(tp->hwmon_dev);
10032 tp->hwmon_dev = NULL;
10033 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
10034 }
Michael Chanaed93e02012-07-16 16:24:02 +000010035}
10036
10037static void tg3_hwmon_open(struct tg3 *tp)
10038{
Michael Chanaed93e02012-07-16 16:24:02 +000010039 int i, err;
10040 u32 size = 0;
10041 struct pci_dev *pdev = tp->pdev;
10042 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10043
10044 tg3_sd_scan_scratchpad(tp, ocirs);
10045
10046 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10047 if (!ocirs[i].src_data_length)
10048 continue;
10049
10050 size += ocirs[i].src_hdr_length;
10051 size += ocirs[i].src_data_length;
10052 }
10053
10054 if (!size)
10055 return;
10056
10057 /* Register hwmon sysfs hooks */
10058 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
10059 if (err) {
10060 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
10061 return;
10062 }
10063
10064 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
10065 if (IS_ERR(tp->hwmon_dev)) {
10066 tp->hwmon_dev = NULL;
10067 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
10068 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
10069 }
Michael Chanaed93e02012-07-16 16:24:02 +000010070}
10071
10072
Linus Torvalds1da177e2005-04-16 15:20:36 -070010073#define TG3_STAT_ADD32(PSTAT, REG) \
10074do { u32 __val = tr32(REG); \
10075 (PSTAT)->low += __val; \
10076 if ((PSTAT)->low < __val) \
10077 (PSTAT)->high += 1; \
10078} while (0)
10079
10080static void tg3_periodic_fetch_stats(struct tg3 *tp)
10081{
10082 struct tg3_hw_stats *sp = tp->hw_stats;
10083
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010084 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010085 return;
10086
10087 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10088 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10089 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10090 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10091 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10092 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10093 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10094 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10095 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10096 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10097 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10098 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10099 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +000010100 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
10101 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10102 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10103 u32 val;
10104
10105 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10106 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
10107 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10108 tg3_flag_clear(tp, 5719_RDMA_BUG);
10109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110
10111 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10112 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10113 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10114 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10115 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10116 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10117 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10118 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10119 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10120 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10121 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10122 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10123 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10124 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010125
10126 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010127 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
10128 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10129 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010130 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10131 } else {
10132 u32 val = tr32(HOSTCC_FLOW_ATTN);
10133 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10134 if (val) {
10135 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10136 sp->rx_discards.low += val;
10137 if (sp->rx_discards.low < val)
10138 sp->rx_discards.high += 1;
10139 }
10140 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10141 }
Michael Chan463d3052006-05-22 16:36:27 -070010142 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010143}
10144
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010145static void tg3_chk_missed_msi(struct tg3 *tp)
10146{
10147 u32 i;
10148
10149 for (i = 0; i < tp->irq_cnt; i++) {
10150 struct tg3_napi *tnapi = &tp->napi[i];
10151
10152 if (tg3_has_work(tnapi)) {
10153 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10154 tnapi->last_tx_cons == tnapi->tx_cons) {
10155 if (tnapi->chk_msi_cnt < 1) {
10156 tnapi->chk_msi_cnt++;
10157 return;
10158 }
Matt Carlson7f230732011-08-31 11:44:48 +000010159 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010160 }
10161 }
10162 tnapi->chk_msi_cnt = 0;
10163 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10164 tnapi->last_tx_cons = tnapi->tx_cons;
10165 }
10166}
10167
Linus Torvalds1da177e2005-04-16 15:20:36 -070010168static void tg3_timer(unsigned long __opaque)
10169{
10170 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171
Matt Carlson5b190622011-11-04 09:15:04 +000010172 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010173 goto restart_timer;
10174
David S. Millerf47c11e2005-06-24 20:18:35 -070010175 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176
Joe Perches41535772013-02-16 11:20:04 +000010177 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010178 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010179 tg3_chk_missed_msi(tp);
10180
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010181 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10182 /* BCM4785: Flush posted writes from GbE to host memory. */
10183 tr32(HOSTCC_MODE);
10184 }
10185
Joe Perches63c3a662011-04-26 08:12:10 +000010186 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010187 /* All of this garbage is because when using non-tagged
10188 * IRQ status the mailbox/status_block protocol the chip
10189 * uses with the cpu is race prone.
10190 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010191 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010192 tw32(GRC_LOCAL_CTRL,
10193 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10194 } else {
10195 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010196 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198
David S. Millerfac9b832005-05-18 22:46:34 -070010199 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010200 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010201 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010202 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010204 }
10205
Linus Torvalds1da177e2005-04-16 15:20:36 -070010206 /* This part only runs once per second. */
10207 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010208 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010209 tg3_periodic_fetch_stats(tp);
10210
Matt Carlsonb0c59432011-05-19 12:12:48 +000010211 if (tp->setlpicnt && !--tp->setlpicnt)
10212 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010213
Joe Perches63c3a662011-04-26 08:12:10 +000010214 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010215 u32 mac_stat;
10216 int phy_event;
10217
10218 mac_stat = tr32(MAC_STATUS);
10219
10220 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010221 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010222 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10223 phy_event = 1;
10224 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10225 phy_event = 1;
10226
10227 if (phy_event)
10228 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000010229 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010230 u32 mac_stat = tr32(MAC_STATUS);
10231 int need_setup = 0;
10232
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010233 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010234 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10235 need_setup = 1;
10236 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010237 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010238 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10239 MAC_STATUS_SIGNAL_DET))) {
10240 need_setup = 1;
10241 }
10242 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010243 if (!tp->serdes_counter) {
10244 tw32_f(MAC_MODE,
10245 (tp->mac_mode &
10246 ~MAC_MODE_PORT_MODE_MASK));
10247 udelay(40);
10248 tw32_f(MAC_MODE, tp->mac_mode);
10249 udelay(40);
10250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010251 tg3_setup_phy(tp, 0);
10252 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010253 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010254 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010255 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010257
10258 tp->timer_counter = tp->timer_multiplier;
10259 }
10260
Michael Chan130b8e42006-09-27 16:00:40 -070010261 /* Heartbeat is only sent once every 2 seconds.
10262 *
10263 * The heartbeat is to tell the ASF firmware that the host
10264 * driver is still alive. In the event that the OS crashes,
10265 * ASF needs to reset the hardware to free up the FIFO space
10266 * that may be filled with rx packets destined for the host.
10267 * If the FIFO is full, ASF will no longer function properly.
10268 *
10269 * Unintended resets have been reported on real time kernels
10270 * where the timer doesn't run on time. Netpoll will also have
10271 * same problem.
10272 *
10273 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10274 * to check the ring condition when the heartbeat is expiring
10275 * before doing the reset. This will prevent most unintended
10276 * resets.
10277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010278 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010279 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010280 tg3_wait_for_event_ack(tp);
10281
Michael Chanbbadf502006-04-06 21:46:34 -070010282 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010283 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010284 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010285 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10286 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010287
10288 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010289 }
10290 tp->asf_counter = tp->asf_multiplier;
10291 }
10292
David S. Millerf47c11e2005-06-24 20:18:35 -070010293 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010294
Michael Chanf475f162006-03-27 23:20:14 -080010295restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010296 tp->timer.expires = jiffies + tp->timer_offset;
10297 add_timer(&tp->timer);
10298}
10299
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010300static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010301{
10302 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000010303 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000010304 !tg3_flag(tp, 57765_CLASS))
10305 tp->timer_offset = HZ;
10306 else
10307 tp->timer_offset = HZ / 10;
10308
10309 BUG_ON(tp->timer_offset > HZ);
10310
10311 tp->timer_multiplier = (HZ / tp->timer_offset);
10312 tp->asf_multiplier = (HZ / tp->timer_offset) *
10313 TG3_FW_UPDATE_FREQ_SEC;
10314
10315 init_timer(&tp->timer);
10316 tp->timer.data = (unsigned long) tp;
10317 tp->timer.function = tg3_timer;
10318}
10319
10320static void tg3_timer_start(struct tg3 *tp)
10321{
10322 tp->asf_counter = tp->asf_multiplier;
10323 tp->timer_counter = tp->timer_multiplier;
10324
10325 tp->timer.expires = jiffies + tp->timer_offset;
10326 add_timer(&tp->timer);
10327}
10328
10329static void tg3_timer_stop(struct tg3 *tp)
10330{
10331 del_timer_sync(&tp->timer);
10332}
10333
10334/* Restart hardware after configuration changes, self-test, etc.
10335 * Invoked with tp->lock held.
10336 */
10337static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
10338 __releases(tp->lock)
10339 __acquires(tp->lock)
10340{
10341 int err;
10342
10343 err = tg3_init_hw(tp, reset_phy);
10344 if (err) {
10345 netdev_err(tp->dev,
10346 "Failed to re-initialize device, aborting\n");
10347 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10348 tg3_full_unlock(tp);
10349 tg3_timer_stop(tp);
10350 tp->irq_sync = 0;
10351 tg3_napi_enable(tp);
10352 dev_close(tp->dev);
10353 tg3_full_lock(tp, 0);
10354 }
10355 return err;
10356}
10357
10358static void tg3_reset_task(struct work_struct *work)
10359{
10360 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10361 int err;
10362
10363 tg3_full_lock(tp, 0);
10364
10365 if (!netif_running(tp->dev)) {
10366 tg3_flag_clear(tp, RESET_TASK_PENDING);
10367 tg3_full_unlock(tp);
10368 return;
10369 }
10370
10371 tg3_full_unlock(tp);
10372
10373 tg3_phy_stop(tp);
10374
10375 tg3_netif_stop(tp);
10376
10377 tg3_full_lock(tp, 1);
10378
10379 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10380 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10381 tp->write32_rx_mbox = tg3_write_flush_reg32;
10382 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10383 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10384 }
10385
10386 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
10387 err = tg3_init_hw(tp, 1);
10388 if (err)
10389 goto out;
10390
10391 tg3_netif_start(tp);
10392
10393out:
10394 tg3_full_unlock(tp);
10395
10396 if (!err)
10397 tg3_phy_start(tp);
10398
10399 tg3_flag_clear(tp, RESET_TASK_PENDING);
10400}
10401
Matt Carlson4f125f42009-09-01 12:55:02 +000010402static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010403{
David Howells7d12e782006-10-05 14:55:46 +010010404 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010405 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010406 char *name;
10407 struct tg3_napi *tnapi = &tp->napi[irq_num];
10408
10409 if (tp->irq_cnt == 1)
10410 name = tp->dev->name;
10411 else {
10412 name = &tnapi->irq_lbl[0];
10413 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10414 name[IFNAMSIZ-1] = 0;
10415 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010416
Joe Perches63c3a662011-04-26 08:12:10 +000010417 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010418 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010419 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010420 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010421 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010422 } else {
10423 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010424 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010425 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010426 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010427 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010428
10429 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010430}
10431
Michael Chan79381092005-04-21 17:13:59 -070010432static int tg3_test_interrupt(struct tg3 *tp)
10433{
Matt Carlson09943a12009-08-28 14:01:57 +000010434 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010435 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010436 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010437 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010438
Michael Chand4bc3922005-05-29 14:59:20 -070010439 if (!netif_running(dev))
10440 return -ENODEV;
10441
Michael Chan79381092005-04-21 17:13:59 -070010442 tg3_disable_ints(tp);
10443
Matt Carlson4f125f42009-09-01 12:55:02 +000010444 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010445
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010446 /*
10447 * Turn off MSI one shot mode. Otherwise this test has no
10448 * observable way to know whether the interrupt was delivered.
10449 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010450 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010451 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10452 tw32(MSGINT_MODE, val);
10453 }
10454
Matt Carlson4f125f42009-09-01 12:55:02 +000010455 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010456 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010457 if (err)
10458 return err;
10459
Matt Carlson898a56f2009-08-28 14:02:40 +000010460 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010461 tg3_enable_ints(tp);
10462
10463 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010464 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010465
10466 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010467 u32 int_mbox, misc_host_ctrl;
10468
Matt Carlson898a56f2009-08-28 14:02:40 +000010469 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010470 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10471
10472 if ((int_mbox != 0) ||
10473 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10474 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010475 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010476 }
10477
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010478 if (tg3_flag(tp, 57765_PLUS) &&
10479 tnapi->hw_status->status_tag != tnapi->last_tag)
10480 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10481
Michael Chan79381092005-04-21 17:13:59 -070010482 msleep(10);
10483 }
10484
10485 tg3_disable_ints(tp);
10486
Matt Carlson4f125f42009-09-01 12:55:02 +000010487 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010488
Matt Carlson4f125f42009-09-01 12:55:02 +000010489 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010490
10491 if (err)
10492 return err;
10493
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010494 if (intr_ok) {
10495 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010496 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010497 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10498 tw32(MSGINT_MODE, val);
10499 }
Michael Chan79381092005-04-21 17:13:59 -070010500 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010501 }
Michael Chan79381092005-04-21 17:13:59 -070010502
10503 return -EIO;
10504}
10505
10506/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10507 * successfully restored
10508 */
10509static int tg3_test_msi(struct tg3 *tp)
10510{
Michael Chan79381092005-04-21 17:13:59 -070010511 int err;
10512 u16 pci_cmd;
10513
Joe Perches63c3a662011-04-26 08:12:10 +000010514 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010515 return 0;
10516
10517 /* Turn off SERR reporting in case MSI terminates with Master
10518 * Abort.
10519 */
10520 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10521 pci_write_config_word(tp->pdev, PCI_COMMAND,
10522 pci_cmd & ~PCI_COMMAND_SERR);
10523
10524 err = tg3_test_interrupt(tp);
10525
10526 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10527
10528 if (!err)
10529 return 0;
10530
10531 /* other failures */
10532 if (err != -EIO)
10533 return err;
10534
10535 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010536 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10537 "to INTx mode. Please report this failure to the PCI "
10538 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010539
Matt Carlson4f125f42009-09-01 12:55:02 +000010540 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010541
Michael Chan79381092005-04-21 17:13:59 -070010542 pci_disable_msi(tp->pdev);
10543
Joe Perches63c3a662011-04-26 08:12:10 +000010544 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010545 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010546
Matt Carlson4f125f42009-09-01 12:55:02 +000010547 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010548 if (err)
10549 return err;
10550
10551 /* Need to reset the chip because the MSI cycle may have terminated
10552 * with Master Abort.
10553 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010554 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010555
Michael Chan944d9802005-05-29 14:57:48 -070010556 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010557 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010558
David S. Millerf47c11e2005-06-24 20:18:35 -070010559 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010560
10561 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000010562 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070010563
10564 return err;
10565}
10566
Matt Carlson9e9fd122009-01-19 16:57:45 -080010567static int tg3_request_firmware(struct tg3 *tp)
10568{
10569 const __be32 *fw_data;
10570
10571 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010572 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
10573 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010574 return -ENOENT;
10575 }
10576
10577 fw_data = (void *)tp->fw->data;
10578
10579 /* Firmware blob starts with version numbers, followed by
10580 * start address and _full_ length including BSS sections
10581 * (which must be longer than the actual data, of course
10582 */
10583
10584 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
10585 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010586 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
10587 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010588 release_firmware(tp->fw);
10589 tp->fw = NULL;
10590 return -EINVAL;
10591 }
10592
10593 /* We no longer need firmware; we have it. */
10594 tp->fw_needed = NULL;
10595 return 0;
10596}
10597
Michael Chan91024262012-09-28 07:12:38 +000010598static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000010599{
Michael Chan91024262012-09-28 07:12:38 +000010600 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000010601
Michael Chan91024262012-09-28 07:12:38 +000010602 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000010603 /* We want as many rx rings enabled as there are cpus.
10604 * In multiqueue MSI-X mode, the first MSI-X vector
10605 * only deals with link interrupts, etc, so we add
10606 * one to the number of vectors we are requesting.
10607 */
Michael Chan91024262012-09-28 07:12:38 +000010608 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010609 }
Matt Carlson679563f2009-09-01 12:55:46 +000010610
Michael Chan91024262012-09-28 07:12:38 +000010611 return irq_cnt;
10612}
10613
10614static bool tg3_enable_msix(struct tg3 *tp)
10615{
10616 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070010617 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000010618
Michael Chan09681692012-09-28 07:12:42 +000010619 tp->txq_cnt = tp->txq_req;
10620 tp->rxq_cnt = tp->rxq_req;
10621 if (!tp->rxq_cnt)
10622 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000010623 if (tp->rxq_cnt > tp->rxq_max)
10624 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000010625
10626 /* Disable multiple TX rings by default. Simple round-robin hardware
10627 * scheduling of the TX rings can cause starvation of rings with
10628 * small packets when other rings have TSO or jumbo packets.
10629 */
10630 if (!tp->txq_req)
10631 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000010632
10633 tp->irq_cnt = tg3_irq_count(tp);
10634
Matt Carlson679563f2009-09-01 12:55:46 +000010635 for (i = 0; i < tp->irq_max; i++) {
10636 msix_ent[i].entry = i;
10637 msix_ent[i].vector = 0;
10638 }
10639
10640 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010641 if (rc < 0) {
10642 return false;
10643 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000010644 if (pci_enable_msix(tp->pdev, msix_ent, rc))
10645 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000010646 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
10647 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000010648 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000010649 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000010650 if (tp->txq_cnt)
10651 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000010652 }
10653
10654 for (i = 0; i < tp->irq_max; i++)
10655 tp->napi[i].irq_vec = msix_ent[i].vector;
10656
Michael Chan49a359e2012-09-28 07:12:37 +000010657 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010658 pci_disable_msix(tp->pdev);
10659 return false;
10660 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000010661
Michael Chan91024262012-09-28 07:12:38 +000010662 if (tp->irq_cnt == 1)
10663 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000010664
Michael Chan91024262012-09-28 07:12:38 +000010665 tg3_flag_set(tp, ENABLE_RSS);
10666
10667 if (tp->txq_cnt > 1)
10668 tg3_flag_set(tp, ENABLE_TSS);
10669
10670 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010671
Matt Carlson679563f2009-09-01 12:55:46 +000010672 return true;
10673}
10674
Matt Carlson07b01732009-08-28 14:01:15 +000010675static void tg3_ints_init(struct tg3 *tp)
10676{
Joe Perches63c3a662011-04-26 08:12:10 +000010677 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
10678 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000010679 /* All MSI supporting chips should support tagged
10680 * status. Assert that this is the case.
10681 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010682 netdev_warn(tp->dev,
10683 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000010684 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000010685 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010686
Joe Perches63c3a662011-04-26 08:12:10 +000010687 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
10688 tg3_flag_set(tp, USING_MSIX);
10689 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
10690 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000010691
Joe Perches63c3a662011-04-26 08:12:10 +000010692 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010693 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000010694 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010695 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010696 if (!tg3_flag(tp, 1SHOT_MSI))
10697 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000010698 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
10699 }
10700defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000010701 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010702 tp->irq_cnt = 1;
10703 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000010704 }
10705
10706 if (tp->irq_cnt == 1) {
10707 tp->txq_cnt = 1;
10708 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010709 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070010710 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000010711 }
Matt Carlson07b01732009-08-28 14:01:15 +000010712}
10713
10714static void tg3_ints_fini(struct tg3 *tp)
10715{
Joe Perches63c3a662011-04-26 08:12:10 +000010716 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000010717 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010718 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000010719 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010720 tg3_flag_clear(tp, USING_MSI);
10721 tg3_flag_clear(tp, USING_MSIX);
10722 tg3_flag_clear(tp, ENABLE_RSS);
10723 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010724}
10725
Matt Carlsonbe947302012-12-03 19:36:57 +000010726static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
10727 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010728{
Michael Chand8f4cd32012-09-28 07:12:40 +000010729 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000010730 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010731
Matt Carlson679563f2009-09-01 12:55:46 +000010732 /*
10733 * Setup interrupts first so we know how
10734 * many NAPI resources to allocate
10735 */
10736 tg3_ints_init(tp);
10737
Matt Carlson90415472011-12-16 13:33:23 +000010738 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010739
Linus Torvalds1da177e2005-04-16 15:20:36 -070010740 /* The placement of this call is tied
10741 * to the setup and use of Host TX descriptors.
10742 */
10743 err = tg3_alloc_consistent(tp);
10744 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010745 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010746
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010747 tg3_napi_init(tp);
10748
Matt Carlsonfed97812009-09-01 13:10:19 +000010749 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010750
Matt Carlson4f125f42009-09-01 12:55:02 +000010751 for (i = 0; i < tp->irq_cnt; i++) {
10752 struct tg3_napi *tnapi = &tp->napi[i];
10753 err = tg3_request_irq(tp, i);
10754 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010755 for (i--; i >= 0; i--) {
10756 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010757 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010758 }
10759 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010760 }
10761 }
Matt Carlson07b01732009-08-28 14:01:15 +000010762
David S. Millerf47c11e2005-06-24 20:18:35 -070010763 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010764
Michael Chand8f4cd32012-09-28 07:12:40 +000010765 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010766 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010767 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010768 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010769 }
10770
David S. Millerf47c11e2005-06-24 20:18:35 -070010771 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010772
Matt Carlson07b01732009-08-28 14:01:15 +000010773 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010774 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010775
Michael Chand8f4cd32012-09-28 07:12:40 +000010776 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010777 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010778
Michael Chan79381092005-04-21 17:13:59 -070010779 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010780 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010781 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010782 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010783 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010784
Matt Carlson679563f2009-09-01 12:55:46 +000010785 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010786 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010787
Joe Perches63c3a662011-04-26 08:12:10 +000010788 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010789 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010790
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010791 tw32(PCIE_TRANSACTION_CFG,
10792 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010793 }
Michael Chan79381092005-04-21 17:13:59 -070010794 }
10795
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010796 tg3_phy_start(tp);
10797
Michael Chanaed93e02012-07-16 16:24:02 +000010798 tg3_hwmon_open(tp);
10799
David S. Millerf47c11e2005-06-24 20:18:35 -070010800 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010801
Matt Carlson21f76382012-02-22 12:35:21 +000010802 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010803 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010804 tg3_enable_ints(tp);
10805
Matt Carlsonbe947302012-12-03 19:36:57 +000010806 if (init)
10807 tg3_ptp_init(tp);
10808 else
10809 tg3_ptp_resume(tp);
10810
10811
David S. Millerf47c11e2005-06-24 20:18:35 -070010812 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010813
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010814 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010815
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010816 /*
10817 * Reset loopback feature if it was turned on while the device was down
10818 * make sure that it's installed properly now.
10819 */
10820 if (dev->features & NETIF_F_LOOPBACK)
10821 tg3_set_loopback(dev, dev->features);
10822
Linus Torvalds1da177e2005-04-16 15:20:36 -070010823 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010824
Matt Carlson679563f2009-09-01 12:55:46 +000010825err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010826 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10827 struct tg3_napi *tnapi = &tp->napi[i];
10828 free_irq(tnapi->irq_vec, tnapi);
10829 }
Matt Carlson07b01732009-08-28 14:01:15 +000010830
Matt Carlson679563f2009-09-01 12:55:46 +000010831err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010832 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010833 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010834 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010835
10836err_out1:
10837 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010838
Matt Carlson07b01732009-08-28 14:01:15 +000010839 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010840}
10841
Michael Chan65138592012-09-28 07:12:41 +000010842static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010843{
Matt Carlson4f125f42009-09-01 12:55:02 +000010844 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010845
Matt Carlsondb219972011-11-04 09:15:03 +000010846 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000010847 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010848
Matt Carlson21f76382012-02-22 12:35:21 +000010849 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010850
Michael Chanaed93e02012-07-16 16:24:02 +000010851 tg3_hwmon_close(tp);
10852
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010853 tg3_phy_stop(tp);
10854
David S. Millerf47c11e2005-06-24 20:18:35 -070010855 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010856
10857 tg3_disable_ints(tp);
10858
Michael Chan944d9802005-05-29 14:57:48 -070010859 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010860 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010861 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010862
David S. Millerf47c11e2005-06-24 20:18:35 -070010863 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010864
Matt Carlson4f125f42009-09-01 12:55:02 +000010865 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10866 struct tg3_napi *tnapi = &tp->napi[i];
10867 free_irq(tnapi->irq_vec, tnapi);
10868 }
Matt Carlson07b01732009-08-28 14:01:15 +000010869
10870 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010871
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010872 tg3_napi_fini(tp);
10873
Linus Torvalds1da177e2005-04-16 15:20:36 -070010874 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000010875}
10876
Michael Chand8f4cd32012-09-28 07:12:40 +000010877static int tg3_open(struct net_device *dev)
10878{
10879 struct tg3 *tp = netdev_priv(dev);
10880 int err;
10881
10882 if (tp->fw_needed) {
10883 err = tg3_request_firmware(tp);
Joe Perches41535772013-02-16 11:20:04 +000010884 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000010885 if (err)
10886 return err;
10887 } else if (err) {
10888 netdev_warn(tp->dev, "TSO capability disabled\n");
10889 tg3_flag_clear(tp, TSO_CAPABLE);
10890 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
10891 netdev_notice(tp->dev, "TSO capability restored\n");
10892 tg3_flag_set(tp, TSO_CAPABLE);
10893 }
10894 }
10895
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010896 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010897
10898 err = tg3_power_up(tp);
10899 if (err)
10900 return err;
10901
10902 tg3_full_lock(tp, 0);
10903
10904 tg3_disable_ints(tp);
10905 tg3_flag_clear(tp, INIT_COMPLETE);
10906
10907 tg3_full_unlock(tp);
10908
Matt Carlsonbe947302012-12-03 19:36:57 +000010909 err = tg3_start(tp, true, true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000010910 if (err) {
10911 tg3_frob_aux_power(tp, false);
10912 pci_set_power_state(tp->pdev, PCI_D3hot);
10913 }
Matt Carlsonbe947302012-12-03 19:36:57 +000010914
Matt Carlson7d41e492012-12-03 19:36:58 +000010915 if (tg3_flag(tp, PTP_CAPABLE)) {
10916 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
10917 &tp->pdev->dev);
10918 if (IS_ERR(tp->ptp_clock))
10919 tp->ptp_clock = NULL;
10920 }
10921
Linus Torvalds1da177e2005-04-16 15:20:36 -070010922 return err;
10923}
10924
10925static int tg3_close(struct net_device *dev)
10926{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010927 struct tg3 *tp = netdev_priv(dev);
10928
Matt Carlsonbe947302012-12-03 19:36:57 +000010929 tg3_ptp_fini(tp);
10930
Michael Chan65138592012-09-28 07:12:41 +000010931 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010932
10933 /* Clear stats across close / open calls */
10934 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10935 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010936
10937 tg3_power_down(tp);
10938
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010939 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010940
10941 return 0;
10942}
10943
10944static inline u64 get_stat64(tg3_stat64_t *val)
10945{
10946 return ((u64)val->high << 32) | ((u64)val->low);
10947}
10948
10949static u64 tg3_calc_crc_errors(struct tg3 *tp)
10950{
10951 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10952
10953 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010954 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
10955 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010956 u32 val;
10957
10958 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10959 tg3_writephy(tp, MII_TG3_TEST1,
10960 val | MII_TG3_TEST1_CRC_EN);
10961 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
10962 } else
10963 val = 0;
10964
10965 tp->phy_crc_errors += val;
10966
10967 return tp->phy_crc_errors;
10968 }
10969
10970 return get_stat64(&hw_stats->rx_fcs_errors);
10971}
10972
10973#define ESTAT_ADD(member) \
10974 estats->member = old_estats->member + \
10975 get_stat64(&hw_stats->member)
10976
10977static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
10978{
10979 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10980 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10981
10982 ESTAT_ADD(rx_octets);
10983 ESTAT_ADD(rx_fragments);
10984 ESTAT_ADD(rx_ucast_packets);
10985 ESTAT_ADD(rx_mcast_packets);
10986 ESTAT_ADD(rx_bcast_packets);
10987 ESTAT_ADD(rx_fcs_errors);
10988 ESTAT_ADD(rx_align_errors);
10989 ESTAT_ADD(rx_xon_pause_rcvd);
10990 ESTAT_ADD(rx_xoff_pause_rcvd);
10991 ESTAT_ADD(rx_mac_ctrl_rcvd);
10992 ESTAT_ADD(rx_xoff_entered);
10993 ESTAT_ADD(rx_frame_too_long_errors);
10994 ESTAT_ADD(rx_jabbers);
10995 ESTAT_ADD(rx_undersize_packets);
10996 ESTAT_ADD(rx_in_length_errors);
10997 ESTAT_ADD(rx_out_length_errors);
10998 ESTAT_ADD(rx_64_or_less_octet_packets);
10999 ESTAT_ADD(rx_65_to_127_octet_packets);
11000 ESTAT_ADD(rx_128_to_255_octet_packets);
11001 ESTAT_ADD(rx_256_to_511_octet_packets);
11002 ESTAT_ADD(rx_512_to_1023_octet_packets);
11003 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11004 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11005 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11006 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11007 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11008
11009 ESTAT_ADD(tx_octets);
11010 ESTAT_ADD(tx_collisions);
11011 ESTAT_ADD(tx_xon_sent);
11012 ESTAT_ADD(tx_xoff_sent);
11013 ESTAT_ADD(tx_flow_control);
11014 ESTAT_ADD(tx_mac_errors);
11015 ESTAT_ADD(tx_single_collisions);
11016 ESTAT_ADD(tx_mult_collisions);
11017 ESTAT_ADD(tx_deferred);
11018 ESTAT_ADD(tx_excessive_collisions);
11019 ESTAT_ADD(tx_late_collisions);
11020 ESTAT_ADD(tx_collide_2times);
11021 ESTAT_ADD(tx_collide_3times);
11022 ESTAT_ADD(tx_collide_4times);
11023 ESTAT_ADD(tx_collide_5times);
11024 ESTAT_ADD(tx_collide_6times);
11025 ESTAT_ADD(tx_collide_7times);
11026 ESTAT_ADD(tx_collide_8times);
11027 ESTAT_ADD(tx_collide_9times);
11028 ESTAT_ADD(tx_collide_10times);
11029 ESTAT_ADD(tx_collide_11times);
11030 ESTAT_ADD(tx_collide_12times);
11031 ESTAT_ADD(tx_collide_13times);
11032 ESTAT_ADD(tx_collide_14times);
11033 ESTAT_ADD(tx_collide_15times);
11034 ESTAT_ADD(tx_ucast_packets);
11035 ESTAT_ADD(tx_mcast_packets);
11036 ESTAT_ADD(tx_bcast_packets);
11037 ESTAT_ADD(tx_carrier_sense_errors);
11038 ESTAT_ADD(tx_discards);
11039 ESTAT_ADD(tx_errors);
11040
11041 ESTAT_ADD(dma_writeq_full);
11042 ESTAT_ADD(dma_write_prioq_full);
11043 ESTAT_ADD(rxbds_empty);
11044 ESTAT_ADD(rx_discards);
11045 ESTAT_ADD(rx_errors);
11046 ESTAT_ADD(rx_threshold_hit);
11047
11048 ESTAT_ADD(dma_readq_full);
11049 ESTAT_ADD(dma_read_prioq_full);
11050 ESTAT_ADD(tx_comp_queue_full);
11051
11052 ESTAT_ADD(ring_set_send_prod_index);
11053 ESTAT_ADD(ring_status_update);
11054 ESTAT_ADD(nic_irqs);
11055 ESTAT_ADD(nic_avoided_irqs);
11056 ESTAT_ADD(nic_tx_threshold_hit);
11057
Matt Carlson4452d092011-05-19 12:12:51 +000011058 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011059}
11060
Matt Carlson65ec6982012-02-28 23:33:37 +000011061static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011062{
Eric Dumazet511d2222010-07-07 20:44:24 +000011063 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011064 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11065
Linus Torvalds1da177e2005-04-16 15:20:36 -070011066 stats->rx_packets = old_stats->rx_packets +
11067 get_stat64(&hw_stats->rx_ucast_packets) +
11068 get_stat64(&hw_stats->rx_mcast_packets) +
11069 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011070
Linus Torvalds1da177e2005-04-16 15:20:36 -070011071 stats->tx_packets = old_stats->tx_packets +
11072 get_stat64(&hw_stats->tx_ucast_packets) +
11073 get_stat64(&hw_stats->tx_mcast_packets) +
11074 get_stat64(&hw_stats->tx_bcast_packets);
11075
11076 stats->rx_bytes = old_stats->rx_bytes +
11077 get_stat64(&hw_stats->rx_octets);
11078 stats->tx_bytes = old_stats->tx_bytes +
11079 get_stat64(&hw_stats->tx_octets);
11080
11081 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011082 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011083 stats->tx_errors = old_stats->tx_errors +
11084 get_stat64(&hw_stats->tx_errors) +
11085 get_stat64(&hw_stats->tx_mac_errors) +
11086 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11087 get_stat64(&hw_stats->tx_discards);
11088
11089 stats->multicast = old_stats->multicast +
11090 get_stat64(&hw_stats->rx_mcast_packets);
11091 stats->collisions = old_stats->collisions +
11092 get_stat64(&hw_stats->tx_collisions);
11093
11094 stats->rx_length_errors = old_stats->rx_length_errors +
11095 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11096 get_stat64(&hw_stats->rx_undersize_packets);
11097
11098 stats->rx_over_errors = old_stats->rx_over_errors +
11099 get_stat64(&hw_stats->rxbds_empty);
11100 stats->rx_frame_errors = old_stats->rx_frame_errors +
11101 get_stat64(&hw_stats->rx_align_errors);
11102 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11103 get_stat64(&hw_stats->tx_discards);
11104 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11105 get_stat64(&hw_stats->tx_carrier_sense_errors);
11106
11107 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011108 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011109
John W. Linville4f63b872005-09-12 14:43:18 -070011110 stats->rx_missed_errors = old_stats->rx_missed_errors +
11111 get_stat64(&hw_stats->rx_discards);
11112
Eric Dumazetb0057c52010-10-10 19:55:52 +000011113 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011114 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011115}
11116
Linus Torvalds1da177e2005-04-16 15:20:36 -070011117static int tg3_get_regs_len(struct net_device *dev)
11118{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011119 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011120}
11121
11122static void tg3_get_regs(struct net_device *dev,
11123 struct ethtool_regs *regs, void *_p)
11124{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011125 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011126
11127 regs->version = 0;
11128
Matt Carlson97bd8e42011-04-13 11:05:04 +000011129 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011130
Matt Carlson80096062010-08-02 11:26:06 +000011131 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011132 return;
11133
David S. Millerf47c11e2005-06-24 20:18:35 -070011134 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011135
Matt Carlson97bd8e42011-04-13 11:05:04 +000011136 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011137
David S. Millerf47c11e2005-06-24 20:18:35 -070011138 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011139}
11140
11141static int tg3_get_eeprom_len(struct net_device *dev)
11142{
11143 struct tg3 *tp = netdev_priv(dev);
11144
11145 return tp->nvram_size;
11146}
11147
Linus Torvalds1da177e2005-04-16 15:20:36 -070011148static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11149{
11150 struct tg3 *tp = netdev_priv(dev);
11151 int ret;
11152 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011153 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011154 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011155
Joe Perches63c3a662011-04-26 08:12:10 +000011156 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011157 return -EINVAL;
11158
Matt Carlson80096062010-08-02 11:26:06 +000011159 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011160 return -EAGAIN;
11161
Linus Torvalds1da177e2005-04-16 15:20:36 -070011162 offset = eeprom->offset;
11163 len = eeprom->len;
11164 eeprom->len = 0;
11165
11166 eeprom->magic = TG3_EEPROM_MAGIC;
11167
11168 if (offset & 3) {
11169 /* adjustments to start on required 4 byte boundary */
11170 b_offset = offset & 3;
11171 b_count = 4 - b_offset;
11172 if (b_count > len) {
11173 /* i.e. offset=1 len=2 */
11174 b_count = len;
11175 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011176 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011177 if (ret)
11178 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011179 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011180 len -= b_count;
11181 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011182 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011183 }
11184
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011185 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011186 pd = &data[eeprom->len];
11187 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011188 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011189 if (ret) {
11190 eeprom->len += i;
11191 return ret;
11192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011193 memcpy(pd + i, &val, 4);
11194 }
11195 eeprom->len += i;
11196
11197 if (len & 3) {
11198 /* read last bytes not ending on 4 byte boundary */
11199 pd = &data[eeprom->len];
11200 b_count = len & 3;
11201 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011202 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011203 if (ret)
11204 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011205 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011206 eeprom->len += b_count;
11207 }
11208 return 0;
11209}
11210
Linus Torvalds1da177e2005-04-16 15:20:36 -070011211static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11212{
11213 struct tg3 *tp = netdev_priv(dev);
11214 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011215 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011216 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011217 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011218
Matt Carlson80096062010-08-02 11:26:06 +000011219 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011220 return -EAGAIN;
11221
Joe Perches63c3a662011-04-26 08:12:10 +000011222 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011223 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011224 return -EINVAL;
11225
11226 offset = eeprom->offset;
11227 len = eeprom->len;
11228
11229 if ((b_offset = (offset & 3))) {
11230 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011231 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011232 if (ret)
11233 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011234 len += b_offset;
11235 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070011236 if (len < 4)
11237 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011238 }
11239
11240 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070011241 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011242 /* adjustments to end on required 4 byte boundary */
11243 odd_len = 1;
11244 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011245 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011246 if (ret)
11247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011248 }
11249
11250 buf = data;
11251 if (b_offset || odd_len) {
11252 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011253 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011254 return -ENOMEM;
11255 if (b_offset)
11256 memcpy(buf, &start, 4);
11257 if (odd_len)
11258 memcpy(buf+len-4, &end, 4);
11259 memcpy(buf + b_offset, data, eeprom->len);
11260 }
11261
11262 ret = tg3_nvram_write_block(tp, offset, len, buf);
11263
11264 if (buf != data)
11265 kfree(buf);
11266
11267 return ret;
11268}
11269
11270static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11271{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011272 struct tg3 *tp = netdev_priv(dev);
11273
Joe Perches63c3a662011-04-26 08:12:10 +000011274 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011275 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011276 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011277 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011278 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11279 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011280 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011281
Linus Torvalds1da177e2005-04-16 15:20:36 -070011282 cmd->supported = (SUPPORTED_Autoneg);
11283
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011284 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011285 cmd->supported |= (SUPPORTED_1000baseT_Half |
11286 SUPPORTED_1000baseT_Full);
11287
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011288 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011289 cmd->supported |= (SUPPORTED_100baseT_Half |
11290 SUPPORTED_100baseT_Full |
11291 SUPPORTED_10baseT_Half |
11292 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011293 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011294 cmd->port = PORT_TP;
11295 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011296 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011297 cmd->port = PORT_FIBRE;
11298 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011299
Linus Torvalds1da177e2005-04-16 15:20:36 -070011300 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011301 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11302 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11303 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11304 cmd->advertising |= ADVERTISED_Pause;
11305 } else {
11306 cmd->advertising |= ADVERTISED_Pause |
11307 ADVERTISED_Asym_Pause;
11308 }
11309 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11310 cmd->advertising |= ADVERTISED_Asym_Pause;
11311 }
11312 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011313 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011314 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011315 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011316 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011317 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11318 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11319 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11320 else
11321 cmd->eth_tp_mdix = ETH_TP_MDI;
11322 }
Matt Carlson64c22182010-10-14 10:37:44 +000011323 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011324 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11325 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011326 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011327 }
Matt Carlson882e9792009-09-01 13:21:36 +000011328 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011329 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011330 cmd->autoneg = tp->link_config.autoneg;
11331 cmd->maxtxpkt = 0;
11332 cmd->maxrxpkt = 0;
11333 return 0;
11334}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011335
Linus Torvalds1da177e2005-04-16 15:20:36 -070011336static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11337{
11338 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011339 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011340
Joe Perches63c3a662011-04-26 08:12:10 +000011341 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011342 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011343 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011344 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011345 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11346 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011347 }
11348
Matt Carlson7e5856b2009-02-25 14:23:01 +000011349 if (cmd->autoneg != AUTONEG_ENABLE &&
11350 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011351 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011352
11353 if (cmd->autoneg == AUTONEG_DISABLE &&
11354 cmd->duplex != DUPLEX_FULL &&
11355 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011356 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011357
Matt Carlson7e5856b2009-02-25 14:23:01 +000011358 if (cmd->autoneg == AUTONEG_ENABLE) {
11359 u32 mask = ADVERTISED_Autoneg |
11360 ADVERTISED_Pause |
11361 ADVERTISED_Asym_Pause;
11362
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011363 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011364 mask |= ADVERTISED_1000baseT_Half |
11365 ADVERTISED_1000baseT_Full;
11366
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011367 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011368 mask |= ADVERTISED_100baseT_Half |
11369 ADVERTISED_100baseT_Full |
11370 ADVERTISED_10baseT_Half |
11371 ADVERTISED_10baseT_Full |
11372 ADVERTISED_TP;
11373 else
11374 mask |= ADVERTISED_FIBRE;
11375
11376 if (cmd->advertising & ~mask)
11377 return -EINVAL;
11378
11379 mask &= (ADVERTISED_1000baseT_Half |
11380 ADVERTISED_1000baseT_Full |
11381 ADVERTISED_100baseT_Half |
11382 ADVERTISED_100baseT_Full |
11383 ADVERTISED_10baseT_Half |
11384 ADVERTISED_10baseT_Full);
11385
11386 cmd->advertising &= mask;
11387 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011388 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011389 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011390 return -EINVAL;
11391
11392 if (cmd->duplex != DUPLEX_FULL)
11393 return -EINVAL;
11394 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011395 if (speed != SPEED_100 &&
11396 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011397 return -EINVAL;
11398 }
11399 }
11400
David S. Millerf47c11e2005-06-24 20:18:35 -070011401 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011402
11403 tp->link_config.autoneg = cmd->autoneg;
11404 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011405 tp->link_config.advertising = (cmd->advertising |
11406 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011407 tp->link_config.speed = SPEED_UNKNOWN;
11408 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011409 } else {
11410 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011411 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011412 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011413 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011414
Linus Torvalds1da177e2005-04-16 15:20:36 -070011415 if (netif_running(dev))
11416 tg3_setup_phy(tp, 1);
11417
David S. Millerf47c11e2005-06-24 20:18:35 -070011418 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011419
Linus Torvalds1da177e2005-04-16 15:20:36 -070011420 return 0;
11421}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011422
Linus Torvalds1da177e2005-04-16 15:20:36 -070011423static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11424{
11425 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011426
Rick Jones68aad782011-11-07 13:29:27 +000011427 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11428 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11429 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11430 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011431}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011432
Linus Torvalds1da177e2005-04-16 15:20:36 -070011433static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11434{
11435 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011436
Joe Perches63c3a662011-04-26 08:12:10 +000011437 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011438 wol->supported = WAKE_MAGIC;
11439 else
11440 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011441 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011442 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011443 wol->wolopts = WAKE_MAGIC;
11444 memset(&wol->sopass, 0, sizeof(wol->sopass));
11445}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011446
Linus Torvalds1da177e2005-04-16 15:20:36 -070011447static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11448{
11449 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011450 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011451
Linus Torvalds1da177e2005-04-16 15:20:36 -070011452 if (wol->wolopts & ~WAKE_MAGIC)
11453 return -EINVAL;
11454 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011455 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011456 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011457
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011458 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11459
David S. Millerf47c11e2005-06-24 20:18:35 -070011460 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011461 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011462 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011463 else
Joe Perches63c3a662011-04-26 08:12:10 +000011464 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011465 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011466
Linus Torvalds1da177e2005-04-16 15:20:36 -070011467 return 0;
11468}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011469
Linus Torvalds1da177e2005-04-16 15:20:36 -070011470static u32 tg3_get_msglevel(struct net_device *dev)
11471{
11472 struct tg3 *tp = netdev_priv(dev);
11473 return tp->msg_enable;
11474}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011475
Linus Torvalds1da177e2005-04-16 15:20:36 -070011476static void tg3_set_msglevel(struct net_device *dev, u32 value)
11477{
11478 struct tg3 *tp = netdev_priv(dev);
11479 tp->msg_enable = value;
11480}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011481
Linus Torvalds1da177e2005-04-16 15:20:36 -070011482static int tg3_nway_reset(struct net_device *dev)
11483{
11484 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011485 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011486
Linus Torvalds1da177e2005-04-16 15:20:36 -070011487 if (!netif_running(dev))
11488 return -EAGAIN;
11489
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011490 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011491 return -EINVAL;
11492
Joe Perches63c3a662011-04-26 08:12:10 +000011493 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011494 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011495 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011496 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011497 } else {
11498 u32 bmcr;
11499
11500 spin_lock_bh(&tp->lock);
11501 r = -EINVAL;
11502 tg3_readphy(tp, MII_BMCR, &bmcr);
11503 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11504 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011505 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011506 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11507 BMCR_ANENABLE);
11508 r = 0;
11509 }
11510 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011511 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011512
Linus Torvalds1da177e2005-04-16 15:20:36 -070011513 return r;
11514}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011515
Linus Torvalds1da177e2005-04-16 15:20:36 -070011516static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11517{
11518 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011519
Matt Carlson2c49a442010-09-30 10:34:35 +000011520 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011521 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011522 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011523 else
11524 ering->rx_jumbo_max_pending = 0;
11525
11526 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011527
11528 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011529 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011530 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11531 else
11532 ering->rx_jumbo_pending = 0;
11533
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011534 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011535}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011536
Linus Torvalds1da177e2005-04-16 15:20:36 -070011537static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11538{
11539 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000011540 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011541
Matt Carlson2c49a442010-09-30 10:34:35 +000011542 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
11543 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070011544 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
11545 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011546 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070011547 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011548 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011549
Michael Chanbbe832c2005-06-24 20:20:04 -070011550 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011551 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011552 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011553 irq_sync = 1;
11554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011555
Michael Chanbbe832c2005-06-24 20:20:04 -070011556 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011557
Linus Torvalds1da177e2005-04-16 15:20:36 -070011558 tp->rx_pending = ering->rx_pending;
11559
Joe Perches63c3a662011-04-26 08:12:10 +000011560 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011561 tp->rx_pending > 63)
11562 tp->rx_pending = 63;
11563 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000011564
Matt Carlson6fd45cb2010-09-15 08:59:57 +000011565 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000011566 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011567
11568 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070011569 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070011570 err = tg3_restart_hw(tp, 1);
11571 if (!err)
11572 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011573 }
11574
David S. Millerf47c11e2005-06-24 20:18:35 -070011575 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011576
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011577 if (irq_sync && !err)
11578 tg3_phy_start(tp);
11579
Michael Chanb9ec6c12006-07-25 16:37:27 -070011580 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011581}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011582
Linus Torvalds1da177e2005-04-16 15:20:36 -070011583static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11584{
11585 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011586
Joe Perches63c3a662011-04-26 08:12:10 +000011587 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080011588
Matt Carlson4a2db502011-12-08 14:40:17 +000011589 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080011590 epause->rx_pause = 1;
11591 else
11592 epause->rx_pause = 0;
11593
Matt Carlson4a2db502011-12-08 14:40:17 +000011594 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080011595 epause->tx_pause = 1;
11596 else
11597 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011598}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011599
Linus Torvalds1da177e2005-04-16 15:20:36 -070011600static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11601{
11602 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011603 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011604
Joe Perches63c3a662011-04-26 08:12:10 +000011605 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000011606 u32 newadv;
11607 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011608
Matt Carlson27121682010-02-17 15:16:57 +000011609 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011610
Matt Carlson27121682010-02-17 15:16:57 +000011611 if (!(phydev->supported & SUPPORTED_Pause) ||
11612 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000011613 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000011614 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011615
Matt Carlson27121682010-02-17 15:16:57 +000011616 tp->link_config.flowctrl = 0;
11617 if (epause->rx_pause) {
11618 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011619
Matt Carlson27121682010-02-17 15:16:57 +000011620 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080011621 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000011622 newadv = ADVERTISED_Pause;
11623 } else
11624 newadv = ADVERTISED_Pause |
11625 ADVERTISED_Asym_Pause;
11626 } else if (epause->tx_pause) {
11627 tp->link_config.flowctrl |= FLOW_CTRL_TX;
11628 newadv = ADVERTISED_Asym_Pause;
11629 } else
11630 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011631
Matt Carlson27121682010-02-17 15:16:57 +000011632 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011633 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011634 else
Joe Perches63c3a662011-04-26 08:12:10 +000011635 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011636
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011637 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000011638 u32 oldadv = phydev->advertising &
11639 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
11640 if (oldadv != newadv) {
11641 phydev->advertising &=
11642 ~(ADVERTISED_Pause |
11643 ADVERTISED_Asym_Pause);
11644 phydev->advertising |= newadv;
11645 if (phydev->autoneg) {
11646 /*
11647 * Always renegotiate the link to
11648 * inform our link partner of our
11649 * flow control settings, even if the
11650 * flow control is forced. Let
11651 * tg3_adjust_link() do the final
11652 * flow control setup.
11653 */
11654 return phy_start_aneg(phydev);
11655 }
11656 }
11657
11658 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011659 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000011660 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011661 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000011662 ~(ADVERTISED_Pause |
11663 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011664 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011665 }
11666 } else {
11667 int irq_sync = 0;
11668
11669 if (netif_running(dev)) {
11670 tg3_netif_stop(tp);
11671 irq_sync = 1;
11672 }
11673
11674 tg3_full_lock(tp, irq_sync);
11675
11676 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011677 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011678 else
Joe Perches63c3a662011-04-26 08:12:10 +000011679 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011680 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011681 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011682 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011683 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011684 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011685 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011686 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011687 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011688
11689 if (netif_running(dev)) {
11690 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11691 err = tg3_restart_hw(tp, 1);
11692 if (!err)
11693 tg3_netif_start(tp);
11694 }
11695
11696 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011698
Michael Chanb9ec6c12006-07-25 16:37:27 -070011699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011700}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011701
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011702static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011703{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011704 switch (sset) {
11705 case ETH_SS_TEST:
11706 return TG3_NUM_TEST;
11707 case ETH_SS_STATS:
11708 return TG3_NUM_STATS;
11709 default:
11710 return -EOPNOTSUPP;
11711 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070011712}
11713
Matt Carlson90415472011-12-16 13:33:23 +000011714static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
11715 u32 *rules __always_unused)
11716{
11717 struct tg3 *tp = netdev_priv(dev);
11718
11719 if (!tg3_flag(tp, SUPPORT_MSIX))
11720 return -EOPNOTSUPP;
11721
11722 switch (info->cmd) {
11723 case ETHTOOL_GRXRINGS:
11724 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000011725 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000011726 else {
11727 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000011728 if (info->data > TG3_RSS_MAX_NUM_QS)
11729 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000011730 }
11731
11732 /* The first interrupt vector only
11733 * handles link interrupts.
11734 */
11735 info->data -= 1;
11736 return 0;
11737
11738 default:
11739 return -EOPNOTSUPP;
11740 }
11741}
11742
11743static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
11744{
11745 u32 size = 0;
11746 struct tg3 *tp = netdev_priv(dev);
11747
11748 if (tg3_flag(tp, SUPPORT_MSIX))
11749 size = TG3_RSS_INDIR_TBL_SIZE;
11750
11751 return size;
11752}
11753
11754static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
11755{
11756 struct tg3 *tp = netdev_priv(dev);
11757 int i;
11758
11759 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11760 indir[i] = tp->rss_ind_tbl[i];
11761
11762 return 0;
11763}
11764
11765static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11766{
11767 struct tg3 *tp = netdev_priv(dev);
11768 size_t i;
11769
11770 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11771 tp->rss_ind_tbl[i] = indir[i];
11772
11773 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11774 return 0;
11775
11776 /* It is legal to write the indirection
11777 * table while the device is running.
11778 */
11779 tg3_full_lock(tp, 0);
11780 tg3_rss_write_indir_tbl(tp);
11781 tg3_full_unlock(tp);
11782
11783 return 0;
11784}
11785
Michael Chan09681692012-09-28 07:12:42 +000011786static void tg3_get_channels(struct net_device *dev,
11787 struct ethtool_channels *channel)
11788{
11789 struct tg3 *tp = netdev_priv(dev);
11790 u32 deflt_qs = netif_get_num_default_rss_queues();
11791
11792 channel->max_rx = tp->rxq_max;
11793 channel->max_tx = tp->txq_max;
11794
11795 if (netif_running(dev)) {
11796 channel->rx_count = tp->rxq_cnt;
11797 channel->tx_count = tp->txq_cnt;
11798 } else {
11799 if (tp->rxq_req)
11800 channel->rx_count = tp->rxq_req;
11801 else
11802 channel->rx_count = min(deflt_qs, tp->rxq_max);
11803
11804 if (tp->txq_req)
11805 channel->tx_count = tp->txq_req;
11806 else
11807 channel->tx_count = min(deflt_qs, tp->txq_max);
11808 }
11809}
11810
11811static int tg3_set_channels(struct net_device *dev,
11812 struct ethtool_channels *channel)
11813{
11814 struct tg3 *tp = netdev_priv(dev);
11815
11816 if (!tg3_flag(tp, SUPPORT_MSIX))
11817 return -EOPNOTSUPP;
11818
11819 if (channel->rx_count > tp->rxq_max ||
11820 channel->tx_count > tp->txq_max)
11821 return -EINVAL;
11822
11823 tp->rxq_req = channel->rx_count;
11824 tp->txq_req = channel->tx_count;
11825
11826 if (!netif_running(dev))
11827 return 0;
11828
11829 tg3_stop(tp);
11830
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011831 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000011832
Matt Carlsonbe947302012-12-03 19:36:57 +000011833 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000011834
11835 return 0;
11836}
11837
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011838static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011839{
11840 switch (stringset) {
11841 case ETH_SS_STATS:
11842 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11843 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011844 case ETH_SS_TEST:
11845 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11846 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011847 default:
11848 WARN_ON(1); /* we need a WARN() */
11849 break;
11850 }
11851}
11852
stephen hemminger81b87092011-04-04 08:43:50 +000011853static int tg3_set_phys_id(struct net_device *dev,
11854 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011855{
11856 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011857
11858 if (!netif_running(tp->dev))
11859 return -EAGAIN;
11860
stephen hemminger81b87092011-04-04 08:43:50 +000011861 switch (state) {
11862 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011863 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011864
stephen hemminger81b87092011-04-04 08:43:50 +000011865 case ETHTOOL_ID_ON:
11866 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11867 LED_CTRL_1000MBPS_ON |
11868 LED_CTRL_100MBPS_ON |
11869 LED_CTRL_10MBPS_ON |
11870 LED_CTRL_TRAFFIC_OVERRIDE |
11871 LED_CTRL_TRAFFIC_BLINK |
11872 LED_CTRL_TRAFFIC_LED);
11873 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011874
stephen hemminger81b87092011-04-04 08:43:50 +000011875 case ETHTOOL_ID_OFF:
11876 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11877 LED_CTRL_TRAFFIC_OVERRIDE);
11878 break;
Michael Chan4009a932005-09-05 17:52:54 -070011879
stephen hemminger81b87092011-04-04 08:43:50 +000011880 case ETHTOOL_ID_INACTIVE:
11881 tw32(MAC_LED_CTRL, tp->led_ctrl);
11882 break;
Michael Chan4009a932005-09-05 17:52:54 -070011883 }
stephen hemminger81b87092011-04-04 08:43:50 +000011884
Michael Chan4009a932005-09-05 17:52:54 -070011885 return 0;
11886}
11887
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011888static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011889 struct ethtool_stats *estats, u64 *tmp_stats)
11890{
11891 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011892
Matt Carlsonb546e462012-02-13 15:20:09 +000011893 if (tp->hw_stats)
11894 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11895 else
11896 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011897}
11898
Matt Carlson535a4902011-07-20 10:20:56 +000011899static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011900{
11901 int i;
11902 __be32 *buf;
11903 u32 offset = 0, len = 0;
11904 u32 magic, val;
11905
Joe Perches63c3a662011-04-26 08:12:10 +000011906 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011907 return NULL;
11908
11909 if (magic == TG3_EEPROM_MAGIC) {
11910 for (offset = TG3_NVM_DIR_START;
11911 offset < TG3_NVM_DIR_END;
11912 offset += TG3_NVM_DIRENT_SIZE) {
11913 if (tg3_nvram_read(tp, offset, &val))
11914 return NULL;
11915
11916 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11917 TG3_NVM_DIRTYPE_EXTVPD)
11918 break;
11919 }
11920
11921 if (offset != TG3_NVM_DIR_END) {
11922 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11923 if (tg3_nvram_read(tp, offset + 4, &offset))
11924 return NULL;
11925
11926 offset = tg3_nvram_logical_addr(tp, offset);
11927 }
11928 }
11929
11930 if (!offset || !len) {
11931 offset = TG3_NVM_VPD_OFF;
11932 len = TG3_NVM_VPD_LEN;
11933 }
11934
11935 buf = kmalloc(len, GFP_KERNEL);
11936 if (buf == NULL)
11937 return NULL;
11938
11939 if (magic == TG3_EEPROM_MAGIC) {
11940 for (i = 0; i < len; i += 4) {
11941 /* The data is in little-endian format in NVRAM.
11942 * Use the big-endian read routines to preserve
11943 * the byte order as it exists in NVRAM.
11944 */
11945 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11946 goto error;
11947 }
11948 } else {
11949 u8 *ptr;
11950 ssize_t cnt;
11951 unsigned int pos = 0;
11952
11953 ptr = (u8 *)&buf[0];
11954 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11955 cnt = pci_read_vpd(tp->pdev, pos,
11956 len - pos, ptr);
11957 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11958 cnt = 0;
11959 else if (cnt < 0)
11960 goto error;
11961 }
11962 if (pos != len)
11963 goto error;
11964 }
11965
Matt Carlson535a4902011-07-20 10:20:56 +000011966 *vpdlen = len;
11967
Matt Carlsonc3e94502011-04-13 11:05:08 +000011968 return buf;
11969
11970error:
11971 kfree(buf);
11972 return NULL;
11973}
11974
Michael Chan566f86a2005-05-29 14:56:58 -070011975#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011976#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11977#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11978#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011979#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11980#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011981#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011982#define NVRAM_SELFBOOT_HW_SIZE 0x20
11983#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011984
11985static int tg3_test_nvram(struct tg3 *tp)
11986{
Matt Carlson535a4902011-07-20 10:20:56 +000011987 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011988 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011989 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011990
Joe Perches63c3a662011-04-26 08:12:10 +000011991 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011992 return 0;
11993
Matt Carlsone4f34112009-02-25 14:25:00 +000011994 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011995 return -EIO;
11996
Michael Chan1b277772006-03-20 22:27:48 -080011997 if (magic == TG3_EEPROM_MAGIC)
11998 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011999 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012000 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12001 TG3_EEPROM_SB_FORMAT_1) {
12002 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12003 case TG3_EEPROM_SB_REVISION_0:
12004 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12005 break;
12006 case TG3_EEPROM_SB_REVISION_2:
12007 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12008 break;
12009 case TG3_EEPROM_SB_REVISION_3:
12010 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12011 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012012 case TG3_EEPROM_SB_REVISION_4:
12013 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12014 break;
12015 case TG3_EEPROM_SB_REVISION_5:
12016 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12017 break;
12018 case TG3_EEPROM_SB_REVISION_6:
12019 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12020 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012021 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012022 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012023 }
12024 } else
Michael Chan1b277772006-03-20 22:27:48 -080012025 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012026 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12027 size = NVRAM_SELFBOOT_HW_SIZE;
12028 else
Michael Chan1b277772006-03-20 22:27:48 -080012029 return -EIO;
12030
12031 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012032 if (buf == NULL)
12033 return -ENOMEM;
12034
Michael Chan1b277772006-03-20 22:27:48 -080012035 err = -EIO;
12036 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012037 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12038 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012039 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012040 }
Michael Chan1b277772006-03-20 22:27:48 -080012041 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012042 goto out;
12043
Michael Chan1b277772006-03-20 22:27:48 -080012044 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012045 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012046 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012047 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012048 u8 *buf8 = (u8 *) buf, csum8 = 0;
12049
Al Virob9fc7dc2007-12-17 22:59:57 -080012050 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012051 TG3_EEPROM_SB_REVISION_2) {
12052 /* For rev 2, the csum doesn't include the MBA. */
12053 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12054 csum8 += buf8[i];
12055 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12056 csum8 += buf8[i];
12057 } else {
12058 for (i = 0; i < size; i++)
12059 csum8 += buf8[i];
12060 }
Michael Chan1b277772006-03-20 22:27:48 -080012061
Adrian Bunkad96b482006-04-05 22:21:04 -070012062 if (csum8 == 0) {
12063 err = 0;
12064 goto out;
12065 }
12066
12067 err = -EIO;
12068 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012069 }
Michael Chan566f86a2005-05-29 14:56:58 -070012070
Al Virob9fc7dc2007-12-17 22:59:57 -080012071 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012072 TG3_EEPROM_MAGIC_HW) {
12073 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012074 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012075 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012076
12077 /* Separate the parity bits and the data bytes. */
12078 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12079 if ((i == 0) || (i == 8)) {
12080 int l;
12081 u8 msk;
12082
12083 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12084 parity[k++] = buf8[i] & msk;
12085 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012086 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012087 int l;
12088 u8 msk;
12089
12090 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12091 parity[k++] = buf8[i] & msk;
12092 i++;
12093
12094 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12095 parity[k++] = buf8[i] & msk;
12096 i++;
12097 }
12098 data[j++] = buf8[i];
12099 }
12100
12101 err = -EIO;
12102 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12103 u8 hw8 = hweight8(data[i]);
12104
12105 if ((hw8 & 0x1) && parity[i])
12106 goto out;
12107 else if (!(hw8 & 0x1) && !parity[i])
12108 goto out;
12109 }
12110 err = 0;
12111 goto out;
12112 }
12113
Matt Carlson01c3a392011-03-09 16:58:20 +000012114 err = -EIO;
12115
Michael Chan566f86a2005-05-29 14:56:58 -070012116 /* Bootstrap checksum at offset 0x10 */
12117 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012118 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012119 goto out;
12120
12121 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12122 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012123 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012124 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012125
Matt Carlsonc3e94502011-04-13 11:05:08 +000012126 kfree(buf);
12127
Matt Carlson535a4902011-07-20 10:20:56 +000012128 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012129 if (!buf)
12130 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012131
Matt Carlson535a4902011-07-20 10:20:56 +000012132 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012133 if (i > 0) {
12134 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12135 if (j < 0)
12136 goto out;
12137
Matt Carlson535a4902011-07-20 10:20:56 +000012138 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012139 goto out;
12140
12141 i += PCI_VPD_LRDT_TAG_SIZE;
12142 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12143 PCI_VPD_RO_KEYWORD_CHKSUM);
12144 if (j > 0) {
12145 u8 csum8 = 0;
12146
12147 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12148
12149 for (i = 0; i <= j; i++)
12150 csum8 += ((u8 *)buf)[i];
12151
12152 if (csum8)
12153 goto out;
12154 }
12155 }
12156
Michael Chan566f86a2005-05-29 14:56:58 -070012157 err = 0;
12158
12159out:
12160 kfree(buf);
12161 return err;
12162}
12163
Michael Chanca430072005-05-29 14:57:23 -070012164#define TG3_SERDES_TIMEOUT_SEC 2
12165#define TG3_COPPER_TIMEOUT_SEC 6
12166
12167static int tg3_test_link(struct tg3 *tp)
12168{
12169 int i, max;
12170
12171 if (!netif_running(tp->dev))
12172 return -ENODEV;
12173
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012174 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012175 max = TG3_SERDES_TIMEOUT_SEC;
12176 else
12177 max = TG3_COPPER_TIMEOUT_SEC;
12178
12179 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012180 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012181 return 0;
12182
12183 if (msleep_interruptible(1000))
12184 break;
12185 }
12186
12187 return -EIO;
12188}
12189
Michael Chana71116d2005-05-29 14:58:11 -070012190/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012191static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012192{
Michael Chanb16250e2006-09-27 16:10:14 -070012193 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012194 u32 offset, read_mask, write_mask, val, save_val, read_val;
12195 static struct {
12196 u16 offset;
12197 u16 flags;
12198#define TG3_FL_5705 0x1
12199#define TG3_FL_NOT_5705 0x2
12200#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012201#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012202 u32 read_mask;
12203 u32 write_mask;
12204 } reg_tbl[] = {
12205 /* MAC Control Registers */
12206 { MAC_MODE, TG3_FL_NOT_5705,
12207 0x00000000, 0x00ef6f8c },
12208 { MAC_MODE, TG3_FL_5705,
12209 0x00000000, 0x01ef6b8c },
12210 { MAC_STATUS, TG3_FL_NOT_5705,
12211 0x03800107, 0x00000000 },
12212 { MAC_STATUS, TG3_FL_5705,
12213 0x03800100, 0x00000000 },
12214 { MAC_ADDR_0_HIGH, 0x0000,
12215 0x00000000, 0x0000ffff },
12216 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012217 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012218 { MAC_RX_MTU_SIZE, 0x0000,
12219 0x00000000, 0x0000ffff },
12220 { MAC_TX_MODE, 0x0000,
12221 0x00000000, 0x00000070 },
12222 { MAC_TX_LENGTHS, 0x0000,
12223 0x00000000, 0x00003fff },
12224 { MAC_RX_MODE, TG3_FL_NOT_5705,
12225 0x00000000, 0x000007fc },
12226 { MAC_RX_MODE, TG3_FL_5705,
12227 0x00000000, 0x000007dc },
12228 { MAC_HASH_REG_0, 0x0000,
12229 0x00000000, 0xffffffff },
12230 { MAC_HASH_REG_1, 0x0000,
12231 0x00000000, 0xffffffff },
12232 { MAC_HASH_REG_2, 0x0000,
12233 0x00000000, 0xffffffff },
12234 { MAC_HASH_REG_3, 0x0000,
12235 0x00000000, 0xffffffff },
12236
12237 /* Receive Data and Receive BD Initiator Control Registers. */
12238 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12239 0x00000000, 0xffffffff },
12240 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12241 0x00000000, 0xffffffff },
12242 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12243 0x00000000, 0x00000003 },
12244 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12245 0x00000000, 0xffffffff },
12246 { RCVDBDI_STD_BD+0, 0x0000,
12247 0x00000000, 0xffffffff },
12248 { RCVDBDI_STD_BD+4, 0x0000,
12249 0x00000000, 0xffffffff },
12250 { RCVDBDI_STD_BD+8, 0x0000,
12251 0x00000000, 0xffff0002 },
12252 { RCVDBDI_STD_BD+0xc, 0x0000,
12253 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012254
Michael Chana71116d2005-05-29 14:58:11 -070012255 /* Receive BD Initiator Control Registers. */
12256 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12257 0x00000000, 0xffffffff },
12258 { RCVBDI_STD_THRESH, TG3_FL_5705,
12259 0x00000000, 0x000003ff },
12260 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12261 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012262
Michael Chana71116d2005-05-29 14:58:11 -070012263 /* Host Coalescing Control Registers. */
12264 { HOSTCC_MODE, TG3_FL_NOT_5705,
12265 0x00000000, 0x00000004 },
12266 { HOSTCC_MODE, TG3_FL_5705,
12267 0x00000000, 0x000000f6 },
12268 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12269 0x00000000, 0xffffffff },
12270 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12271 0x00000000, 0x000003ff },
12272 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12273 0x00000000, 0xffffffff },
12274 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12275 0x00000000, 0x000003ff },
12276 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12277 0x00000000, 0xffffffff },
12278 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12279 0x00000000, 0x000000ff },
12280 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12281 0x00000000, 0xffffffff },
12282 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12283 0x00000000, 0x000000ff },
12284 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12285 0x00000000, 0xffffffff },
12286 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12287 0x00000000, 0xffffffff },
12288 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12289 0x00000000, 0xffffffff },
12290 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12291 0x00000000, 0x000000ff },
12292 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12293 0x00000000, 0xffffffff },
12294 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12295 0x00000000, 0x000000ff },
12296 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12297 0x00000000, 0xffffffff },
12298 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12299 0x00000000, 0xffffffff },
12300 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12301 0x00000000, 0xffffffff },
12302 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12303 0x00000000, 0xffffffff },
12304 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12305 0x00000000, 0xffffffff },
12306 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12307 0xffffffff, 0x00000000 },
12308 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12309 0xffffffff, 0x00000000 },
12310
12311 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012312 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012313 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012314 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012315 0x00000000, 0x007fffff },
12316 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12317 0x00000000, 0x0000003f },
12318 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12319 0x00000000, 0x000001ff },
12320 { BUFMGR_MB_HIGH_WATER, 0x0000,
12321 0x00000000, 0x000001ff },
12322 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12323 0xffffffff, 0x00000000 },
12324 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12325 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012326
Michael Chana71116d2005-05-29 14:58:11 -070012327 /* Mailbox Registers */
12328 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12329 0x00000000, 0x000001ff },
12330 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12331 0x00000000, 0x000001ff },
12332 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12333 0x00000000, 0x000007ff },
12334 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12335 0x00000000, 0x000001ff },
12336
12337 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12338 };
12339
Michael Chanb16250e2006-09-27 16:10:14 -070012340 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012341 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012342 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012343 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012344 is_5750 = 1;
12345 }
Michael Chana71116d2005-05-29 14:58:11 -070012346
12347 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12348 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12349 continue;
12350
12351 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12352 continue;
12353
Joe Perches63c3a662011-04-26 08:12:10 +000012354 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012355 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12356 continue;
12357
Michael Chanb16250e2006-09-27 16:10:14 -070012358 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12359 continue;
12360
Michael Chana71116d2005-05-29 14:58:11 -070012361 offset = (u32) reg_tbl[i].offset;
12362 read_mask = reg_tbl[i].read_mask;
12363 write_mask = reg_tbl[i].write_mask;
12364
12365 /* Save the original register content */
12366 save_val = tr32(offset);
12367
12368 /* Determine the read-only value. */
12369 read_val = save_val & read_mask;
12370
12371 /* Write zero to the register, then make sure the read-only bits
12372 * are not changed and the read/write bits are all zeros.
12373 */
12374 tw32(offset, 0);
12375
12376 val = tr32(offset);
12377
12378 /* Test the read-only and read/write bits. */
12379 if (((val & read_mask) != read_val) || (val & write_mask))
12380 goto out;
12381
12382 /* Write ones to all the bits defined by RdMask and WrMask, then
12383 * make sure the read-only bits are not changed and the
12384 * read/write bits are all ones.
12385 */
12386 tw32(offset, read_mask | write_mask);
12387
12388 val = tr32(offset);
12389
12390 /* Test the read-only bits. */
12391 if ((val & read_mask) != read_val)
12392 goto out;
12393
12394 /* Test the read/write bits. */
12395 if ((val & write_mask) != write_mask)
12396 goto out;
12397
12398 tw32(offset, save_val);
12399 }
12400
12401 return 0;
12402
12403out:
Michael Chan9f88f292006-12-07 00:22:54 -080012404 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012405 netdev_err(tp->dev,
12406 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012407 tw32(offset, save_val);
12408 return -EIO;
12409}
12410
Michael Chan7942e1d2005-05-29 14:58:36 -070012411static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12412{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012413 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012414 int i;
12415 u32 j;
12416
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012417 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012418 for (j = 0; j < len; j += 4) {
12419 u32 val;
12420
12421 tg3_write_mem(tp, offset + j, test_pattern[i]);
12422 tg3_read_mem(tp, offset + j, &val);
12423 if (val != test_pattern[i])
12424 return -EIO;
12425 }
12426 }
12427 return 0;
12428}
12429
12430static int tg3_test_memory(struct tg3 *tp)
12431{
12432 static struct mem_entry {
12433 u32 offset;
12434 u32 len;
12435 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012436 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012437 { 0x00002000, 0x1c000},
12438 { 0xffffffff, 0x00000}
12439 }, mem_tbl_5705[] = {
12440 { 0x00000100, 0x0000c},
12441 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012442 { 0x00004000, 0x00800},
12443 { 0x00006000, 0x01000},
12444 { 0x00008000, 0x02000},
12445 { 0x00010000, 0x0e000},
12446 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012447 }, mem_tbl_5755[] = {
12448 { 0x00000200, 0x00008},
12449 { 0x00004000, 0x00800},
12450 { 0x00006000, 0x00800},
12451 { 0x00008000, 0x02000},
12452 { 0x00010000, 0x0c000},
12453 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012454 }, mem_tbl_5906[] = {
12455 { 0x00000200, 0x00008},
12456 { 0x00004000, 0x00400},
12457 { 0x00006000, 0x00400},
12458 { 0x00008000, 0x01000},
12459 { 0x00010000, 0x01000},
12460 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012461 }, mem_tbl_5717[] = {
12462 { 0x00000200, 0x00008},
12463 { 0x00010000, 0x0a000},
12464 { 0x00020000, 0x13c00},
12465 { 0xffffffff, 0x00000}
12466 }, mem_tbl_57765[] = {
12467 { 0x00000200, 0x00008},
12468 { 0x00004000, 0x00800},
12469 { 0x00006000, 0x09800},
12470 { 0x00010000, 0x0a000},
12471 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012472 };
12473 struct mem_entry *mem_tbl;
12474 int err = 0;
12475 int i;
12476
Joe Perches63c3a662011-04-26 08:12:10 +000012477 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012478 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000012479 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000012480 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012481 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012482 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012483 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000012484 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080012485 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012486 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012487 mem_tbl = mem_tbl_5705;
12488 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012489 mem_tbl = mem_tbl_570x;
12490
12491 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012492 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12493 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012494 break;
12495 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012496
Michael Chan7942e1d2005-05-29 14:58:36 -070012497 return err;
12498}
12499
Matt Carlsonbb158d62011-04-25 12:42:47 +000012500#define TG3_TSO_MSS 500
12501
12502#define TG3_TSO_IP_HDR_LEN 20
12503#define TG3_TSO_TCP_HDR_LEN 20
12504#define TG3_TSO_TCP_OPT_LEN 12
12505
12506static const u8 tg3_tso_header[] = {
125070x08, 0x00,
125080x45, 0x00, 0x00, 0x00,
125090x00, 0x00, 0x40, 0x00,
125100x40, 0x06, 0x00, 0x00,
125110x0a, 0x00, 0x00, 0x01,
125120x0a, 0x00, 0x00, 0x02,
125130x0d, 0x00, 0xe0, 0x00,
125140x00, 0x00, 0x01, 0x00,
125150x00, 0x00, 0x02, 0x00,
125160x80, 0x10, 0x10, 0x00,
125170x14, 0x09, 0x00, 0x00,
125180x01, 0x01, 0x08, 0x0a,
125190x11, 0x11, 0x11, 0x11,
125200x11, 0x11, 0x11, 0x11,
12521};
Michael Chan9f40dea2005-09-05 17:53:06 -070012522
Matt Carlson28a45952011-08-19 13:58:22 +000012523static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012524{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012525 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012526 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012527 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012528 struct sk_buff *skb;
12529 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012530 dma_addr_t map;
12531 int num_pkts, tx_len, rx_len, i, err;
12532 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012533 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000012534 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070012535
Matt Carlsonc8873402010-02-12 14:47:11 +000012536 tnapi = &tp->napi[0];
12537 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012538 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000012539 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000012540 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000012541 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000012542 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012543 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012544 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000012545
Michael Chanc76949a2005-05-29 14:58:59 -070012546 err = -EIO;
12547
Matt Carlson4852a862011-04-13 11:05:07 +000012548 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070012549 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070012550 if (!skb)
12551 return -ENOMEM;
12552
Michael Chanc76949a2005-05-29 14:58:59 -070012553 tx_data = skb_put(skb, tx_len);
12554 memcpy(tx_data, tp->dev->dev_addr, 6);
12555 memset(tx_data + 6, 0x0, 8);
12556
Matt Carlson4852a862011-04-13 11:05:07 +000012557 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070012558
Matt Carlson28a45952011-08-19 13:58:22 +000012559 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012560 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
12561
12562 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
12563 TG3_TSO_TCP_OPT_LEN;
12564
12565 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
12566 sizeof(tg3_tso_header));
12567 mss = TG3_TSO_MSS;
12568
12569 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
12570 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
12571
12572 /* Set the total length field in the IP header */
12573 iph->tot_len = htons((u16)(mss + hdr_len));
12574
12575 base_flags = (TXD_FLAG_CPU_PRE_DMA |
12576 TXD_FLAG_CPU_POST_DMA);
12577
Joe Perches63c3a662011-04-26 08:12:10 +000012578 if (tg3_flag(tp, HW_TSO_1) ||
12579 tg3_flag(tp, HW_TSO_2) ||
12580 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012581 struct tcphdr *th;
12582 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
12583 th = (struct tcphdr *)&tx_data[val];
12584 th->check = 0;
12585 } else
12586 base_flags |= TXD_FLAG_TCPUDP_CSUM;
12587
Joe Perches63c3a662011-04-26 08:12:10 +000012588 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012589 mss |= (hdr_len & 0xc) << 12;
12590 if (hdr_len & 0x10)
12591 base_flags |= 0x00000010;
12592 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000012593 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012594 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000012595 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000012596 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012597 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
12598 } else {
12599 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
12600 }
12601
12602 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
12603 } else {
12604 num_pkts = 1;
12605 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000012606
12607 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
12608 tx_len > VLAN_ETH_FRAME_LEN)
12609 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012610 }
12611
12612 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070012613 tx_data[i] = (u8) (i & 0xff);
12614
Alexander Duyckf4188d82009-12-02 16:48:38 +000012615 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
12616 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000012617 dev_kfree_skb(skb);
12618 return -EIO;
12619 }
Michael Chanc76949a2005-05-29 14:58:59 -070012620
Matt Carlson0d681b22011-07-27 14:20:49 +000012621 val = tnapi->tx_prod;
12622 tnapi->tx_buffers[val].skb = skb;
12623 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
12624
Michael Chanc76949a2005-05-29 14:58:59 -070012625 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012626 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012627
12628 udelay(10);
12629
Matt Carlson898a56f2009-08-28 14:02:40 +000012630 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070012631
Matt Carlson84b67b22011-07-27 14:20:52 +000012632 budget = tg3_tx_avail(tnapi);
12633 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000012634 base_flags | TXD_FLAG_END, mss, 0)) {
12635 tnapi->tx_buffers[val].skb = NULL;
12636 dev_kfree_skb(skb);
12637 return -EIO;
12638 }
Michael Chanc76949a2005-05-29 14:58:59 -070012639
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012640 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070012641
Michael Chan6541b802012-03-04 14:48:14 +000012642 /* Sync BD data before updating mailbox */
12643 wmb();
12644
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012645 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
12646 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070012647
12648 udelay(10);
12649
Matt Carlson303fc922009-11-02 14:27:34 +000012650 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
12651 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070012652 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012653 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012654
12655 udelay(10);
12656
Matt Carlson898a56f2009-08-28 14:02:40 +000012657 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
12658 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012659 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070012660 (rx_idx == (rx_start_idx + num_pkts)))
12661 break;
12662 }
12663
Matt Carlsonba1142e2011-11-04 09:15:00 +000012664 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070012665 dev_kfree_skb(skb);
12666
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012667 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070012668 goto out;
12669
12670 if (rx_idx != rx_start_idx + num_pkts)
12671 goto out;
12672
Matt Carlsonbb158d62011-04-25 12:42:47 +000012673 val = data_off;
12674 while (rx_idx != rx_start_idx) {
12675 desc = &rnapi->rx_rcb[rx_start_idx++];
12676 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
12677 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070012678
Matt Carlsonbb158d62011-04-25 12:42:47 +000012679 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
12680 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000012681 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070012682
Matt Carlsonbb158d62011-04-25 12:42:47 +000012683 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
12684 - ETH_FCS_LEN;
12685
Matt Carlson28a45952011-08-19 13:58:22 +000012686 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012687 if (rx_len != tx_len)
12688 goto out;
12689
12690 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
12691 if (opaque_key != RXD_OPAQUE_RING_STD)
12692 goto out;
12693 } else {
12694 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
12695 goto out;
12696 }
12697 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
12698 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000012699 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012700 goto out;
12701 }
12702
12703 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012704 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012705 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
12706 mapping);
12707 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012708 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012709 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
12710 mapping);
12711 } else
Matt Carlson4852a862011-04-13 11:05:07 +000012712 goto out;
12713
Matt Carlsonbb158d62011-04-25 12:42:47 +000012714 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
12715 PCI_DMA_FROMDEVICE);
12716
Eric Dumazet9205fd92011-11-18 06:47:01 +000012717 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000012718 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012719 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012720 goto out;
12721 }
Matt Carlson4852a862011-04-13 11:05:07 +000012722 }
12723
Michael Chanc76949a2005-05-29 14:58:59 -070012724 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012725
Eric Dumazet9205fd92011-11-18 06:47:01 +000012726 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070012727out:
12728 return err;
12729}
12730
Matt Carlson00c266b2011-04-25 12:42:46 +000012731#define TG3_STD_LOOPBACK_FAILED 1
12732#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000012733#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000012734#define TG3_LOOPBACK_FAILED \
12735 (TG3_STD_LOOPBACK_FAILED | \
12736 TG3_JMB_LOOPBACK_FAILED | \
12737 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000012738
Matt Carlson941ec902011-08-19 13:58:23 +000012739static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070012740{
Matt Carlson28a45952011-08-19 13:58:22 +000012741 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000012742 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000012743 u32 jmb_pkt_sz = 9000;
12744
12745 if (tp->dma_limit)
12746 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070012747
Matt Carlsonab789042011-01-25 15:58:54 +000012748 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
12749 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
12750
Matt Carlson28a45952011-08-19 13:58:22 +000012751 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012752 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12753 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012754 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012755 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000012756 goto done;
12757 }
12758
Michael Chanb9ec6c12006-07-25 16:37:27 -070012759 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000012760 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012761 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12762 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012763 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012764 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000012765 goto done;
12766 }
Michael Chan9f40dea2005-09-05 17:53:06 -070012767
Joe Perches63c3a662011-04-26 08:12:10 +000012768 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000012769 int i;
12770
12771 /* Reroute all rx packets to the 1st queue */
12772 for (i = MAC_RSS_INDIR_TBL_0;
12773 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
12774 tw32(i, 0x0);
12775 }
12776
Matt Carlson6e01b202011-08-19 13:58:20 +000012777 /* HW errata - mac loopback fails in some cases on 5780.
12778 * Normal traffic and PHY loopback are not affected by
12779 * errata. Also, the MAC loopback test is deprecated for
12780 * all newer ASIC revisions.
12781 */
Joe Perches41535772013-02-16 11:20:04 +000012782 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000012783 !tg3_flag(tp, CPMU_PRESENT)) {
12784 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070012785
Matt Carlson28a45952011-08-19 13:58:22 +000012786 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012787 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012788
12789 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012790 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012791 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012792
12793 tg3_mac_loopback(tp, false);
12794 }
Matt Carlson4852a862011-04-13 11:05:07 +000012795
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012796 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012797 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012798 int i;
12799
Matt Carlson941ec902011-08-19 13:58:23 +000012800 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012801
12802 /* Wait for link */
12803 for (i = 0; i < 100; i++) {
12804 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
12805 break;
12806 mdelay(1);
12807 }
12808
Matt Carlson28a45952011-08-19 13:58:22 +000012809 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012810 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012811 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012812 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012813 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012814 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012815 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012816 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012817
Matt Carlson941ec902011-08-19 13:58:23 +000012818 if (do_extlpbk) {
12819 tg3_phy_lpbk_set(tp, 0, true);
12820
12821 /* All link indications report up, but the hardware
12822 * isn't really ready for about 20 msec. Double it
12823 * to be sure.
12824 */
12825 mdelay(40);
12826
12827 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012828 data[TG3_EXT_LOOPB_TEST] |=
12829 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012830 if (tg3_flag(tp, TSO_CAPABLE) &&
12831 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012832 data[TG3_EXT_LOOPB_TEST] |=
12833 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012834 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012835 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012836 data[TG3_EXT_LOOPB_TEST] |=
12837 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012838 }
12839
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012840 /* Re-enable gphy autopowerdown. */
12841 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12842 tg3_phy_toggle_apd(tp, true);
12843 }
Matt Carlson6833c042008-11-21 17:18:59 -080012844
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012845 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
12846 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000012847
Matt Carlsonab789042011-01-25 15:58:54 +000012848done:
12849 tp->phy_flags |= eee_cap;
12850
Michael Chan9f40dea2005-09-05 17:53:06 -070012851 return err;
12852}
12853
Michael Chan4cafd3f2005-05-29 14:56:34 -070012854static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12855 u64 *data)
12856{
Michael Chan566f86a2005-05-29 14:56:58 -070012857 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012858 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012859
Matt Carlsonbed98292011-07-13 09:27:29 +000012860 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12861 tg3_power_up(tp)) {
12862 etest->flags |= ETH_TEST_FL_FAILED;
12863 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12864 return;
12865 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012866
Michael Chan566f86a2005-05-29 14:56:58 -070012867 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12868
12869 if (tg3_test_nvram(tp) != 0) {
12870 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012871 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070012872 }
Matt Carlson941ec902011-08-19 13:58:23 +000012873 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012874 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012875 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070012876 }
Michael Chana71116d2005-05-29 14:58:11 -070012877 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012878 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012879
Michael Chanbbe832c2005-06-24 20:20:04 -070012880 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012881 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012882 tg3_netif_stop(tp);
12883 irq_sync = 1;
12884 }
12885
12886 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012887 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012888 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012889 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012890 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012891 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012892 if (!err)
12893 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012894
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012895 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080012896 tg3_phy_reset(tp);
12897
Michael Chana71116d2005-05-29 14:58:11 -070012898 if (tg3_test_registers(tp) != 0) {
12899 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012900 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070012901 }
Matt Carlson28a45952011-08-19 13:58:22 +000012902
Michael Chan7942e1d2005-05-29 14:58:36 -070012903 if (tg3_test_memory(tp) != 0) {
12904 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012905 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070012906 }
Matt Carlson28a45952011-08-19 13:58:22 +000012907
Matt Carlson941ec902011-08-19 13:58:23 +000012908 if (doextlpbk)
12909 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12910
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012911 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012912 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012913
David S. Millerf47c11e2005-06-24 20:18:35 -070012914 tg3_full_unlock(tp);
12915
Michael Chand4bc3922005-05-29 14:59:20 -070012916 if (tg3_test_interrupt(tp) != 0) {
12917 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012918 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012919 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012920
12921 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012922
Michael Chana71116d2005-05-29 14:58:11 -070012923 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12924 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012925 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012926 err2 = tg3_restart_hw(tp, 1);
12927 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012928 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012929 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012930
12931 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012932
12933 if (irq_sync && !err2)
12934 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012935 }
Matt Carlson80096062010-08-02 11:26:06 +000012936 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012937 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012938
Michael Chan4cafd3f2005-05-29 14:56:34 -070012939}
12940
Matt Carlson0a633ac2012-12-03 19:36:59 +000012941static int tg3_hwtstamp_ioctl(struct net_device *dev,
12942 struct ifreq *ifr, int cmd)
12943{
12944 struct tg3 *tp = netdev_priv(dev);
12945 struct hwtstamp_config stmpconf;
12946
12947 if (!tg3_flag(tp, PTP_CAPABLE))
12948 return -EINVAL;
12949
12950 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
12951 return -EFAULT;
12952
12953 if (stmpconf.flags)
12954 return -EINVAL;
12955
12956 switch (stmpconf.tx_type) {
12957 case HWTSTAMP_TX_ON:
12958 tg3_flag_set(tp, TX_TSTAMP_EN);
12959 break;
12960 case HWTSTAMP_TX_OFF:
12961 tg3_flag_clear(tp, TX_TSTAMP_EN);
12962 break;
12963 default:
12964 return -ERANGE;
12965 }
12966
12967 switch (stmpconf.rx_filter) {
12968 case HWTSTAMP_FILTER_NONE:
12969 tp->rxptpctl = 0;
12970 break;
12971 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
12972 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12973 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
12974 break;
12975 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
12976 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12977 TG3_RX_PTP_CTL_SYNC_EVNT;
12978 break;
12979 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
12980 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12981 TG3_RX_PTP_CTL_DELAY_REQ;
12982 break;
12983 case HWTSTAMP_FILTER_PTP_V2_EVENT:
12984 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12985 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12986 break;
12987 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
12988 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12989 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12990 break;
12991 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
12992 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
12993 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12994 break;
12995 case HWTSTAMP_FILTER_PTP_V2_SYNC:
12996 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12997 TG3_RX_PTP_CTL_SYNC_EVNT;
12998 break;
12999 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13000 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13001 TG3_RX_PTP_CTL_SYNC_EVNT;
13002 break;
13003 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13004 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13005 TG3_RX_PTP_CTL_SYNC_EVNT;
13006 break;
13007 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13008 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13009 TG3_RX_PTP_CTL_DELAY_REQ;
13010 break;
13011 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13012 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13013 TG3_RX_PTP_CTL_DELAY_REQ;
13014 break;
13015 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13016 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13017 TG3_RX_PTP_CTL_DELAY_REQ;
13018 break;
13019 default:
13020 return -ERANGE;
13021 }
13022
13023 if (netif_running(dev) && tp->rxptpctl)
13024 tw32(TG3_RX_PTP_CTL,
13025 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13026
13027 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13028 -EFAULT : 0;
13029}
13030
Linus Torvalds1da177e2005-04-16 15:20:36 -070013031static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13032{
13033 struct mii_ioctl_data *data = if_mii(ifr);
13034 struct tg3 *tp = netdev_priv(dev);
13035 int err;
13036
Joe Perches63c3a662011-04-26 08:12:10 +000013037 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013038 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013039 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013040 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013041 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000013042 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013043 }
13044
Matt Carlson33f401a2010-04-05 10:19:27 +000013045 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013046 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013047 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013048
13049 /* fallthru */
13050 case SIOCGMIIREG: {
13051 u32 mii_regval;
13052
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013053 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013054 break; /* We have no PHY */
13055
Matt Carlson34eea5a2011-04-20 07:57:38 +000013056 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013057 return -EAGAIN;
13058
David S. Millerf47c11e2005-06-24 20:18:35 -070013059 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013060 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13061 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013062 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013063
13064 data->val_out = mii_regval;
13065
13066 return err;
13067 }
13068
13069 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013070 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013071 break; /* We have no PHY */
13072
Matt Carlson34eea5a2011-04-20 07:57:38 +000013073 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013074 return -EAGAIN;
13075
David S. Millerf47c11e2005-06-24 20:18:35 -070013076 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013077 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13078 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013079 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013080
13081 return err;
13082
Matt Carlson0a633ac2012-12-03 19:36:59 +000013083 case SIOCSHWTSTAMP:
13084 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
13085
Linus Torvalds1da177e2005-04-16 15:20:36 -070013086 default:
13087 /* do nothing */
13088 break;
13089 }
13090 return -EOPNOTSUPP;
13091}
13092
David S. Miller15f98502005-05-18 22:49:26 -070013093static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13094{
13095 struct tg3 *tp = netdev_priv(dev);
13096
13097 memcpy(ec, &tp->coal, sizeof(*ec));
13098 return 0;
13099}
13100
Michael Chand244c892005-07-05 14:42:33 -070013101static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13102{
13103 struct tg3 *tp = netdev_priv(dev);
13104 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13105 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13106
Joe Perches63c3a662011-04-26 08:12:10 +000013107 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013108 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13109 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13110 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13111 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13112 }
13113
13114 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13115 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13116 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13117 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13118 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13119 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13120 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13121 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13122 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13123 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13124 return -EINVAL;
13125
13126 /* No rx interrupts will be generated if both are zero */
13127 if ((ec->rx_coalesce_usecs == 0) &&
13128 (ec->rx_max_coalesced_frames == 0))
13129 return -EINVAL;
13130
13131 /* No tx interrupts will be generated if both are zero */
13132 if ((ec->tx_coalesce_usecs == 0) &&
13133 (ec->tx_max_coalesced_frames == 0))
13134 return -EINVAL;
13135
13136 /* Only copy relevant parameters, ignore all others. */
13137 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13138 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13139 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13140 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13141 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13142 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13143 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13144 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13145 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13146
13147 if (netif_running(dev)) {
13148 tg3_full_lock(tp, 0);
13149 __tg3_set_coalesce(tp, &tp->coal);
13150 tg3_full_unlock(tp);
13151 }
13152 return 0;
13153}
13154
Jeff Garzik7282d492006-09-13 14:30:00 -040013155static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013156 .get_settings = tg3_get_settings,
13157 .set_settings = tg3_set_settings,
13158 .get_drvinfo = tg3_get_drvinfo,
13159 .get_regs_len = tg3_get_regs_len,
13160 .get_regs = tg3_get_regs,
13161 .get_wol = tg3_get_wol,
13162 .set_wol = tg3_set_wol,
13163 .get_msglevel = tg3_get_msglevel,
13164 .set_msglevel = tg3_set_msglevel,
13165 .nway_reset = tg3_nway_reset,
13166 .get_link = ethtool_op_get_link,
13167 .get_eeprom_len = tg3_get_eeprom_len,
13168 .get_eeprom = tg3_get_eeprom,
13169 .set_eeprom = tg3_set_eeprom,
13170 .get_ringparam = tg3_get_ringparam,
13171 .set_ringparam = tg3_set_ringparam,
13172 .get_pauseparam = tg3_get_pauseparam,
13173 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013174 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013175 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013176 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013177 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013178 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013179 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013180 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013181 .get_rxnfc = tg3_get_rxnfc,
13182 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13183 .get_rxfh_indir = tg3_get_rxfh_indir,
13184 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013185 .get_channels = tg3_get_channels,
13186 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013187 .get_ts_info = tg3_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013188};
13189
David S. Millerb4017c52012-03-01 17:57:40 -050013190static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13191 struct rtnl_link_stats64 *stats)
13192{
13193 struct tg3 *tp = netdev_priv(dev);
13194
David S. Millerb4017c52012-03-01 17:57:40 -050013195 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013196 if (!tp->hw_stats) {
13197 spin_unlock_bh(&tp->lock);
13198 return &tp->net_stats_prev;
13199 }
13200
David S. Millerb4017c52012-03-01 17:57:40 -050013201 tg3_get_nstats(tp, stats);
13202 spin_unlock_bh(&tp->lock);
13203
13204 return stats;
13205}
13206
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013207static void tg3_set_rx_mode(struct net_device *dev)
13208{
13209 struct tg3 *tp = netdev_priv(dev);
13210
13211 if (!netif_running(dev))
13212 return;
13213
13214 tg3_full_lock(tp, 0);
13215 __tg3_set_rx_mode(dev);
13216 tg3_full_unlock(tp);
13217}
13218
Matt Carlsonfaf16272012-02-13 10:20:07 +000013219static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13220 int new_mtu)
13221{
13222 dev->mtu = new_mtu;
13223
13224 if (new_mtu > ETH_DATA_LEN) {
13225 if (tg3_flag(tp, 5780_CLASS)) {
13226 netdev_update_features(dev);
13227 tg3_flag_clear(tp, TSO_CAPABLE);
13228 } else {
13229 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13230 }
13231 } else {
13232 if (tg3_flag(tp, 5780_CLASS)) {
13233 tg3_flag_set(tp, TSO_CAPABLE);
13234 netdev_update_features(dev);
13235 }
13236 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13237 }
13238}
13239
13240static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13241{
13242 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000013243 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013244
13245 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13246 return -EINVAL;
13247
13248 if (!netif_running(dev)) {
13249 /* We'll just catch it later when the
13250 * device is up'd.
13251 */
13252 tg3_set_mtu(dev, tp, new_mtu);
13253 return 0;
13254 }
13255
13256 tg3_phy_stop(tp);
13257
13258 tg3_netif_stop(tp);
13259
13260 tg3_full_lock(tp, 1);
13261
13262 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13263
13264 tg3_set_mtu(dev, tp, new_mtu);
13265
Michael Chan2fae5e32012-03-04 14:48:15 +000013266 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13267 * breaks all requests to 256 bytes.
13268 */
Joe Perches41535772013-02-16 11:20:04 +000013269 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Michael Chan2fae5e32012-03-04 14:48:15 +000013270 reset_phy = 1;
13271
13272 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013273
13274 if (!err)
13275 tg3_netif_start(tp);
13276
13277 tg3_full_unlock(tp);
13278
13279 if (!err)
13280 tg3_phy_start(tp);
13281
13282 return err;
13283}
13284
13285static const struct net_device_ops tg3_netdev_ops = {
13286 .ndo_open = tg3_open,
13287 .ndo_stop = tg3_close,
13288 .ndo_start_xmit = tg3_start_xmit,
13289 .ndo_get_stats64 = tg3_get_stats64,
13290 .ndo_validate_addr = eth_validate_addr,
13291 .ndo_set_rx_mode = tg3_set_rx_mode,
13292 .ndo_set_mac_address = tg3_set_mac_addr,
13293 .ndo_do_ioctl = tg3_ioctl,
13294 .ndo_tx_timeout = tg3_tx_timeout,
13295 .ndo_change_mtu = tg3_change_mtu,
13296 .ndo_fix_features = tg3_fix_features,
13297 .ndo_set_features = tg3_set_features,
13298#ifdef CONFIG_NET_POLL_CONTROLLER
13299 .ndo_poll_controller = tg3_poll_controller,
13300#endif
13301};
13302
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013303static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013304{
Michael Chan1b277772006-03-20 22:27:48 -080013305 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013306
13307 tp->nvram_size = EEPROM_CHIP_SIZE;
13308
Matt Carlsone4f34112009-02-25 14:25:00 +000013309 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013310 return;
13311
Michael Chanb16250e2006-09-27 16:10:14 -070013312 if ((magic != TG3_EEPROM_MAGIC) &&
13313 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13314 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013315 return;
13316
13317 /*
13318 * Size the chip by reading offsets at increasing powers of two.
13319 * When we encounter our validation signature, we know the addressing
13320 * has wrapped around, and thus have our chip size.
13321 */
Michael Chan1b277772006-03-20 22:27:48 -080013322 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013323
13324 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013325 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013326 return;
13327
Michael Chan18201802006-03-20 22:29:15 -080013328 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013329 break;
13330
13331 cursize <<= 1;
13332 }
13333
13334 tp->nvram_size = cursize;
13335}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013336
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013337static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013338{
13339 u32 val;
13340
Joe Perches63c3a662011-04-26 08:12:10 +000013341 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013342 return;
13343
13344 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013345 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013346 tg3_get_eeprom_size(tp);
13347 return;
13348 }
13349
Matt Carlson6d348f22009-02-25 14:25:52 +000013350 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013351 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013352 /* This is confusing. We want to operate on the
13353 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13354 * call will read from NVRAM and byteswap the data
13355 * according to the byteswapping settings for all
13356 * other register accesses. This ensures the data we
13357 * want will always reside in the lower 16-bits.
13358 * However, the data in NVRAM is in LE format, which
13359 * means the data from the NVRAM read will always be
13360 * opposite the endianness of the CPU. The 16-bit
13361 * byteswap then brings the data to CPU endianness.
13362 */
13363 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013364 return;
13365 }
13366 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013367 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013368}
13369
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013370static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013371{
13372 u32 nvcfg1;
13373
13374 nvcfg1 = tr32(NVRAM_CFG1);
13375 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000013376 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013377 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013378 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13379 tw32(NVRAM_CFG1, nvcfg1);
13380 }
13381
Joe Perches41535772013-02-16 11:20:04 +000013382 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013383 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013384 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013385 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
13386 tp->nvram_jedecnum = JEDEC_ATMEL;
13387 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013388 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013389 break;
13390 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
13391 tp->nvram_jedecnum = JEDEC_ATMEL;
13392 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
13393 break;
13394 case FLASH_VENDOR_ATMEL_EEPROM:
13395 tp->nvram_jedecnum = JEDEC_ATMEL;
13396 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013397 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013398 break;
13399 case FLASH_VENDOR_ST:
13400 tp->nvram_jedecnum = JEDEC_ST;
13401 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013402 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013403 break;
13404 case FLASH_VENDOR_SAIFUN:
13405 tp->nvram_jedecnum = JEDEC_SAIFUN;
13406 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
13407 break;
13408 case FLASH_VENDOR_SST_SMALL:
13409 case FLASH_VENDOR_SST_LARGE:
13410 tp->nvram_jedecnum = JEDEC_SST;
13411 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
13412 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013413 }
Matt Carlson8590a602009-08-28 12:29:16 +000013414 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013415 tp->nvram_jedecnum = JEDEC_ATMEL;
13416 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013417 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013418 }
13419}
13420
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013421static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013422{
13423 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13424 case FLASH_5752PAGE_SIZE_256:
13425 tp->nvram_pagesize = 256;
13426 break;
13427 case FLASH_5752PAGE_SIZE_512:
13428 tp->nvram_pagesize = 512;
13429 break;
13430 case FLASH_5752PAGE_SIZE_1K:
13431 tp->nvram_pagesize = 1024;
13432 break;
13433 case FLASH_5752PAGE_SIZE_2K:
13434 tp->nvram_pagesize = 2048;
13435 break;
13436 case FLASH_5752PAGE_SIZE_4K:
13437 tp->nvram_pagesize = 4096;
13438 break;
13439 case FLASH_5752PAGE_SIZE_264:
13440 tp->nvram_pagesize = 264;
13441 break;
13442 case FLASH_5752PAGE_SIZE_528:
13443 tp->nvram_pagesize = 528;
13444 break;
13445 }
13446}
13447
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013448static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070013449{
13450 u32 nvcfg1;
13451
13452 nvcfg1 = tr32(NVRAM_CFG1);
13453
Michael Chane6af3012005-04-21 17:12:05 -070013454 /* NVRAM protection for TPM */
13455 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000013456 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070013457
Michael Chan361b4ac2005-04-21 17:11:21 -070013458 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013459 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
13460 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
13461 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013462 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013463 break;
13464 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13465 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013466 tg3_flag_set(tp, NVRAM_BUFFERED);
13467 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013468 break;
13469 case FLASH_5752VENDOR_ST_M45PE10:
13470 case FLASH_5752VENDOR_ST_M45PE20:
13471 case FLASH_5752VENDOR_ST_M45PE40:
13472 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013473 tg3_flag_set(tp, NVRAM_BUFFERED);
13474 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013475 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070013476 }
13477
Joe Perches63c3a662011-04-26 08:12:10 +000013478 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000013479 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000013480 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070013481 /* For eeprom, set pagesize to maximum eeprom size */
13482 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13483
13484 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13485 tw32(NVRAM_CFG1, nvcfg1);
13486 }
13487}
13488
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013489static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080013490{
Matt Carlson989a9d22007-05-05 11:51:05 -070013491 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080013492
13493 nvcfg1 = tr32(NVRAM_CFG1);
13494
13495 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070013496 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013497 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070013498 protect = 1;
13499 }
Michael Chand3c7b882006-03-23 01:28:25 -080013500
Matt Carlson989a9d22007-05-05 11:51:05 -070013501 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13502 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013503 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13504 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13505 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13506 case FLASH_5755VENDOR_ATMEL_FLASH_5:
13507 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013508 tg3_flag_set(tp, NVRAM_BUFFERED);
13509 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013510 tp->nvram_pagesize = 264;
13511 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
13512 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
13513 tp->nvram_size = (protect ? 0x3e200 :
13514 TG3_NVRAM_SIZE_512KB);
13515 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
13516 tp->nvram_size = (protect ? 0x1f200 :
13517 TG3_NVRAM_SIZE_256KB);
13518 else
13519 tp->nvram_size = (protect ? 0x1f200 :
13520 TG3_NVRAM_SIZE_128KB);
13521 break;
13522 case FLASH_5752VENDOR_ST_M45PE10:
13523 case FLASH_5752VENDOR_ST_M45PE20:
13524 case FLASH_5752VENDOR_ST_M45PE40:
13525 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013526 tg3_flag_set(tp, NVRAM_BUFFERED);
13527 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013528 tp->nvram_pagesize = 256;
13529 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
13530 tp->nvram_size = (protect ?
13531 TG3_NVRAM_SIZE_64KB :
13532 TG3_NVRAM_SIZE_128KB);
13533 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
13534 tp->nvram_size = (protect ?
13535 TG3_NVRAM_SIZE_64KB :
13536 TG3_NVRAM_SIZE_256KB);
13537 else
13538 tp->nvram_size = (protect ?
13539 TG3_NVRAM_SIZE_128KB :
13540 TG3_NVRAM_SIZE_512KB);
13541 break;
Michael Chand3c7b882006-03-23 01:28:25 -080013542 }
13543}
13544
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013545static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080013546{
13547 u32 nvcfg1;
13548
13549 nvcfg1 = tr32(NVRAM_CFG1);
13550
13551 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013552 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
13553 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13554 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
13555 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13556 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013557 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013558 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080013559
Matt Carlson8590a602009-08-28 12:29:16 +000013560 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13561 tw32(NVRAM_CFG1, nvcfg1);
13562 break;
13563 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13564 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13565 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13566 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13567 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013568 tg3_flag_set(tp, NVRAM_BUFFERED);
13569 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013570 tp->nvram_pagesize = 264;
13571 break;
13572 case FLASH_5752VENDOR_ST_M45PE10:
13573 case FLASH_5752VENDOR_ST_M45PE20:
13574 case FLASH_5752VENDOR_ST_M45PE40:
13575 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013576 tg3_flag_set(tp, NVRAM_BUFFERED);
13577 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013578 tp->nvram_pagesize = 256;
13579 break;
Michael Chan1b277772006-03-20 22:27:48 -080013580 }
13581}
13582
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013583static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070013584{
13585 u32 nvcfg1, protect = 0;
13586
13587 nvcfg1 = tr32(NVRAM_CFG1);
13588
13589 /* NVRAM protection for TPM */
13590 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013591 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013592 protect = 1;
13593 }
13594
13595 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13596 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013597 case FLASH_5761VENDOR_ATMEL_ADB021D:
13598 case FLASH_5761VENDOR_ATMEL_ADB041D:
13599 case FLASH_5761VENDOR_ATMEL_ADB081D:
13600 case FLASH_5761VENDOR_ATMEL_ADB161D:
13601 case FLASH_5761VENDOR_ATMEL_MDB021D:
13602 case FLASH_5761VENDOR_ATMEL_MDB041D:
13603 case FLASH_5761VENDOR_ATMEL_MDB081D:
13604 case FLASH_5761VENDOR_ATMEL_MDB161D:
13605 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013606 tg3_flag_set(tp, NVRAM_BUFFERED);
13607 tg3_flag_set(tp, FLASH);
13608 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000013609 tp->nvram_pagesize = 256;
13610 break;
13611 case FLASH_5761VENDOR_ST_A_M45PE20:
13612 case FLASH_5761VENDOR_ST_A_M45PE40:
13613 case FLASH_5761VENDOR_ST_A_M45PE80:
13614 case FLASH_5761VENDOR_ST_A_M45PE16:
13615 case FLASH_5761VENDOR_ST_M_M45PE20:
13616 case FLASH_5761VENDOR_ST_M_M45PE40:
13617 case FLASH_5761VENDOR_ST_M_M45PE80:
13618 case FLASH_5761VENDOR_ST_M_M45PE16:
13619 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013620 tg3_flag_set(tp, NVRAM_BUFFERED);
13621 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013622 tp->nvram_pagesize = 256;
13623 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013624 }
13625
13626 if (protect) {
13627 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
13628 } else {
13629 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013630 case FLASH_5761VENDOR_ATMEL_ADB161D:
13631 case FLASH_5761VENDOR_ATMEL_MDB161D:
13632 case FLASH_5761VENDOR_ST_A_M45PE16:
13633 case FLASH_5761VENDOR_ST_M_M45PE16:
13634 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
13635 break;
13636 case FLASH_5761VENDOR_ATMEL_ADB081D:
13637 case FLASH_5761VENDOR_ATMEL_MDB081D:
13638 case FLASH_5761VENDOR_ST_A_M45PE80:
13639 case FLASH_5761VENDOR_ST_M_M45PE80:
13640 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13641 break;
13642 case FLASH_5761VENDOR_ATMEL_ADB041D:
13643 case FLASH_5761VENDOR_ATMEL_MDB041D:
13644 case FLASH_5761VENDOR_ST_A_M45PE40:
13645 case FLASH_5761VENDOR_ST_M_M45PE40:
13646 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13647 break;
13648 case FLASH_5761VENDOR_ATMEL_ADB021D:
13649 case FLASH_5761VENDOR_ATMEL_MDB021D:
13650 case FLASH_5761VENDOR_ST_A_M45PE20:
13651 case FLASH_5761VENDOR_ST_M_M45PE20:
13652 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13653 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013654 }
13655 }
13656}
13657
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013658static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070013659{
13660 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013661 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070013662 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13663}
13664
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013665static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080013666{
13667 u32 nvcfg1;
13668
13669 nvcfg1 = tr32(NVRAM_CFG1);
13670
13671 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13672 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13673 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13674 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013675 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080013676 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13677
13678 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13679 tw32(NVRAM_CFG1, nvcfg1);
13680 return;
13681 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13682 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13683 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13684 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13685 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13686 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13687 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13688 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013689 tg3_flag_set(tp, NVRAM_BUFFERED);
13690 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013691
13692 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13693 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13694 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13695 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13696 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13697 break;
13698 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13699 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13700 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13701 break;
13702 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13703 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13704 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13705 break;
13706 }
13707 break;
13708 case FLASH_5752VENDOR_ST_M45PE10:
13709 case FLASH_5752VENDOR_ST_M45PE20:
13710 case FLASH_5752VENDOR_ST_M45PE40:
13711 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013712 tg3_flag_set(tp, NVRAM_BUFFERED);
13713 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013714
13715 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13716 case FLASH_5752VENDOR_ST_M45PE10:
13717 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13718 break;
13719 case FLASH_5752VENDOR_ST_M45PE20:
13720 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13721 break;
13722 case FLASH_5752VENDOR_ST_M45PE40:
13723 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13724 break;
13725 }
13726 break;
13727 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013728 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080013729 return;
13730 }
13731
Matt Carlsona1b950d2009-09-01 13:20:17 +000013732 tg3_nvram_get_pagesize(tp, nvcfg1);
13733 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013734 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013735}
13736
13737
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013738static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013739{
13740 u32 nvcfg1;
13741
13742 nvcfg1 = tr32(NVRAM_CFG1);
13743
13744 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13745 case FLASH_5717VENDOR_ATMEL_EEPROM:
13746 case FLASH_5717VENDOR_MICRO_EEPROM:
13747 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013748 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013749 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13750
13751 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13752 tw32(NVRAM_CFG1, nvcfg1);
13753 return;
13754 case FLASH_5717VENDOR_ATMEL_MDB011D:
13755 case FLASH_5717VENDOR_ATMEL_ADB011B:
13756 case FLASH_5717VENDOR_ATMEL_ADB011D:
13757 case FLASH_5717VENDOR_ATMEL_MDB021D:
13758 case FLASH_5717VENDOR_ATMEL_ADB021B:
13759 case FLASH_5717VENDOR_ATMEL_ADB021D:
13760 case FLASH_5717VENDOR_ATMEL_45USPT:
13761 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013762 tg3_flag_set(tp, NVRAM_BUFFERED);
13763 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013764
13765 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13766 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013767 /* Detect size with tg3_nvram_get_size() */
13768 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013769 case FLASH_5717VENDOR_ATMEL_ADB021B:
13770 case FLASH_5717VENDOR_ATMEL_ADB021D:
13771 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13772 break;
13773 default:
13774 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13775 break;
13776 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013777 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013778 case FLASH_5717VENDOR_ST_M_M25PE10:
13779 case FLASH_5717VENDOR_ST_A_M25PE10:
13780 case FLASH_5717VENDOR_ST_M_M45PE10:
13781 case FLASH_5717VENDOR_ST_A_M45PE10:
13782 case FLASH_5717VENDOR_ST_M_M25PE20:
13783 case FLASH_5717VENDOR_ST_A_M25PE20:
13784 case FLASH_5717VENDOR_ST_M_M45PE20:
13785 case FLASH_5717VENDOR_ST_A_M45PE20:
13786 case FLASH_5717VENDOR_ST_25USPT:
13787 case FLASH_5717VENDOR_ST_45USPT:
13788 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013789 tg3_flag_set(tp, NVRAM_BUFFERED);
13790 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013791
13792 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13793 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013794 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013795 /* Detect size with tg3_nvram_get_size() */
13796 break;
13797 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013798 case FLASH_5717VENDOR_ST_A_M45PE20:
13799 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13800 break;
13801 default:
13802 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13803 break;
13804 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013805 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013806 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013807 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013808 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080013809 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000013810
13811 tg3_nvram_get_pagesize(tp, nvcfg1);
13812 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013813 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013814}
13815
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013816static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013817{
13818 u32 nvcfg1, nvmpinstrp;
13819
13820 nvcfg1 = tr32(NVRAM_CFG1);
13821 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
13822
Joe Perches41535772013-02-16 11:20:04 +000013823 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000013824 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
13825 tg3_flag_set(tp, NO_NVRAM);
13826 return;
13827 }
13828
13829 switch (nvmpinstrp) {
13830 case FLASH_5762_EEPROM_HD:
13831 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030013832 break;
Michael Chanc86a8562013-01-06 12:51:08 +000013833 case FLASH_5762_EEPROM_LD:
13834 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030013835 break;
Michael Chanc86a8562013-01-06 12:51:08 +000013836 }
13837 }
13838
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013839 switch (nvmpinstrp) {
13840 case FLASH_5720_EEPROM_HD:
13841 case FLASH_5720_EEPROM_LD:
13842 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013843 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013844
13845 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13846 tw32(NVRAM_CFG1, nvcfg1);
13847 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
13848 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13849 else
13850 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
13851 return;
13852 case FLASH_5720VENDOR_M_ATMEL_DB011D:
13853 case FLASH_5720VENDOR_A_ATMEL_DB011B:
13854 case FLASH_5720VENDOR_A_ATMEL_DB011D:
13855 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13856 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13857 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13858 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13859 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13860 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13861 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13862 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13863 case FLASH_5720VENDOR_ATMEL_45USPT:
13864 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013865 tg3_flag_set(tp, NVRAM_BUFFERED);
13866 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013867
13868 switch (nvmpinstrp) {
13869 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13870 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13871 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13872 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13873 break;
13874 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13875 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13876 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13877 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13878 break;
13879 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13880 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13881 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13882 break;
13883 default:
Joe Perches41535772013-02-16 11:20:04 +000013884 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000013885 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013886 break;
13887 }
13888 break;
13889 case FLASH_5720VENDOR_M_ST_M25PE10:
13890 case FLASH_5720VENDOR_M_ST_M45PE10:
13891 case FLASH_5720VENDOR_A_ST_M25PE10:
13892 case FLASH_5720VENDOR_A_ST_M45PE10:
13893 case FLASH_5720VENDOR_M_ST_M25PE20:
13894 case FLASH_5720VENDOR_M_ST_M45PE20:
13895 case FLASH_5720VENDOR_A_ST_M25PE20:
13896 case FLASH_5720VENDOR_A_ST_M45PE20:
13897 case FLASH_5720VENDOR_M_ST_M25PE40:
13898 case FLASH_5720VENDOR_M_ST_M45PE40:
13899 case FLASH_5720VENDOR_A_ST_M25PE40:
13900 case FLASH_5720VENDOR_A_ST_M45PE40:
13901 case FLASH_5720VENDOR_M_ST_M25PE80:
13902 case FLASH_5720VENDOR_M_ST_M45PE80:
13903 case FLASH_5720VENDOR_A_ST_M25PE80:
13904 case FLASH_5720VENDOR_A_ST_M45PE80:
13905 case FLASH_5720VENDOR_ST_25USPT:
13906 case FLASH_5720VENDOR_ST_45USPT:
13907 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013908 tg3_flag_set(tp, NVRAM_BUFFERED);
13909 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013910
13911 switch (nvmpinstrp) {
13912 case FLASH_5720VENDOR_M_ST_M25PE20:
13913 case FLASH_5720VENDOR_M_ST_M45PE20:
13914 case FLASH_5720VENDOR_A_ST_M25PE20:
13915 case FLASH_5720VENDOR_A_ST_M45PE20:
13916 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13917 break;
13918 case FLASH_5720VENDOR_M_ST_M25PE40:
13919 case FLASH_5720VENDOR_M_ST_M45PE40:
13920 case FLASH_5720VENDOR_A_ST_M25PE40:
13921 case FLASH_5720VENDOR_A_ST_M45PE40:
13922 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13923 break;
13924 case FLASH_5720VENDOR_M_ST_M25PE80:
13925 case FLASH_5720VENDOR_M_ST_M45PE80:
13926 case FLASH_5720VENDOR_A_ST_M25PE80:
13927 case FLASH_5720VENDOR_A_ST_M45PE80:
13928 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13929 break;
13930 default:
Joe Perches41535772013-02-16 11:20:04 +000013931 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000013932 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013933 break;
13934 }
13935 break;
13936 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013937 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013938 return;
13939 }
13940
13941 tg3_nvram_get_pagesize(tp, nvcfg1);
13942 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013943 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000013944
Joe Perches41535772013-02-16 11:20:04 +000013945 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000013946 u32 val;
13947
13948 if (tg3_nvram_read(tp, 0, &val))
13949 return;
13950
13951 if (val != TG3_EEPROM_MAGIC &&
13952 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
13953 tg3_flag_set(tp, NO_NVRAM);
13954 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013955}
13956
Linus Torvalds1da177e2005-04-16 15:20:36 -070013957/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013958static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013959{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000013960 if (tg3_flag(tp, IS_SSB_CORE)) {
13961 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
13962 tg3_flag_clear(tp, NVRAM);
13963 tg3_flag_clear(tp, NVRAM_BUFFERED);
13964 tg3_flag_set(tp, NO_NVRAM);
13965 return;
13966 }
13967
Linus Torvalds1da177e2005-04-16 15:20:36 -070013968 tw32_f(GRC_EEPROM_ADDR,
13969 (EEPROM_ADDR_FSM_RESET |
13970 (EEPROM_DEFAULT_CLOCK_PERIOD <<
13971 EEPROM_ADDR_CLKPERD_SHIFT)));
13972
Michael Chan9d57f012006-12-07 00:23:25 -080013973 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013974
13975 /* Enable seeprom accesses. */
13976 tw32_f(GRC_LOCAL_CTRL,
13977 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
13978 udelay(100);
13979
Joe Perches41535772013-02-16 11:20:04 +000013980 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
13981 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000013982 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013983
Michael Chanec41c7d2006-01-17 02:40:55 -080013984 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000013985 netdev_warn(tp->dev,
13986 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000013987 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080013988 return;
13989 }
Michael Chane6af3012005-04-21 17:12:05 -070013990 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013991
Matt Carlson989a9d22007-05-05 11:51:05 -070013992 tp->nvram_size = 0;
13993
Joe Perches41535772013-02-16 11:20:04 +000013994 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070013995 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000013996 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080013997 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000013998 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
13999 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14000 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014001 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014002 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014003 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014004 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014005 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014006 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014007 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014008 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014009 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14010 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014011 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014012 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14013 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014014 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014015 else
14016 tg3_get_nvram_info(tp);
14017
Matt Carlson989a9d22007-05-05 11:51:05 -070014018 if (tp->nvram_size == 0)
14019 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014020
Michael Chane6af3012005-04-21 17:12:05 -070014021 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014022 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014023
14024 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014025 tg3_flag_clear(tp, NVRAM);
14026 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014027
14028 tg3_get_eeprom_size(tp);
14029 }
14030}
14031
Linus Torvalds1da177e2005-04-16 15:20:36 -070014032struct subsys_tbl_ent {
14033 u16 subsys_vendor, subsys_devid;
14034 u32 phy_id;
14035};
14036
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014037static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014038 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014039 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014040 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014041 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014042 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014043 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014044 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014045 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14046 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14047 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014048 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014049 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014050 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014051 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14052 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14053 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014054 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014055 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014056 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014057 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014058 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014059 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014060 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014061
14062 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014063 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014064 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014065 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014066 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014067 { TG3PCI_SUBVENDOR_ID_3COM,
14068 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14069 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014070 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014071 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014072 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014073
14074 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014075 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014076 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014077 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014078 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014079 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014080 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014081 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014082 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014083
14084 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014085 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014086 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014087 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014088 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014089 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14090 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14091 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014092 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014093 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014094 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014095
14096 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014097 { TG3PCI_SUBVENDOR_ID_IBM,
14098 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014099};
14100
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014101static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014102{
14103 int i;
14104
14105 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14106 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14107 tp->pdev->subsystem_vendor) &&
14108 (subsys_id_to_phy_id[i].subsys_devid ==
14109 tp->pdev->subsystem_device))
14110 return &subsys_id_to_phy_id[i];
14111 }
14112 return NULL;
14113}
14114
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014115static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014116{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014117 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014118
Matt Carlson79eb6902010-02-17 15:17:03 +000014119 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014120 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14121
Gary Zambranoa85feb82007-05-05 11:52:19 -070014122 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014123 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14124 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014125
Joe Perches41535772013-02-16 11:20:04 +000014126 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014127 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014128 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14129 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014130 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014131 val = tr32(VCPU_CFGSHDW);
14132 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014133 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014134 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014135 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014136 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014137 device_set_wakeup_enable(&tp->pdev->dev, true);
14138 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014139 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014140 }
14141
Linus Torvalds1da177e2005-04-16 15:20:36 -070014142 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14143 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14144 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014145 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014146 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147
14148 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14149 tp->nic_sram_data_cfg = nic_cfg;
14150
14151 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14152 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000014153 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14154 tg3_asic_rev(tp) != ASIC_REV_5701 &&
14155 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014156 (ver > 0) && (ver < 0x100))
14157 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14158
Joe Perches41535772013-02-16 11:20:04 +000014159 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070014160 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14161
Linus Torvalds1da177e2005-04-16 15:20:36 -070014162 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14163 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14164 eeprom_phy_serdes = 1;
14165
14166 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14167 if (nic_phy_id != 0) {
14168 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14169 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14170
14171 eeprom_phy_id = (id1 >> 16) << 10;
14172 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14173 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14174 } else
14175 eeprom_phy_id = 0;
14176
Michael Chan7d0c41e2005-04-21 17:06:20 -070014177 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014178 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014179 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014180 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014181 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014182 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014183 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014184
Joe Perches63c3a662011-04-26 08:12:10 +000014185 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014186 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14187 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014188 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014189 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14190
14191 switch (led_cfg) {
14192 default:
14193 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14194 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14195 break;
14196
14197 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14198 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14199 break;
14200
14201 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14202 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014203
14204 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14205 * read on some older 5700/5701 bootcode.
14206 */
Joe Perches41535772013-02-16 11:20:04 +000014207 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
14208 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070014209 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14210
Linus Torvalds1da177e2005-04-16 15:20:36 -070014211 break;
14212
14213 case SHASTA_EXT_LED_SHARED:
14214 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000014215 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
14216 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014217 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14218 LED_CTRL_MODE_PHY_2);
14219 break;
14220
14221 case SHASTA_EXT_LED_MAC:
14222 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14223 break;
14224
14225 case SHASTA_EXT_LED_COMBO:
14226 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000014227 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014228 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14229 LED_CTRL_MODE_PHY_2);
14230 break;
14231
Stephen Hemminger855e1112008-04-16 16:37:28 -070014232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014233
Joe Perches41535772013-02-16 11:20:04 +000014234 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
14235 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014236 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14237 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14238
Joe Perches41535772013-02-16 11:20:04 +000014239 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014240 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014241
Michael Chan9d26e212006-12-07 00:21:14 -080014242 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014243 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014244 if ((tp->pdev->subsystem_vendor ==
14245 PCI_VENDOR_ID_ARIMA) &&
14246 (tp->pdev->subsystem_device == 0x205a ||
14247 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014248 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014249 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014250 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14251 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014253
14254 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014255 tg3_flag_set(tp, ENABLE_ASF);
14256 if (tg3_flag(tp, 5750_PLUS))
14257 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014258 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014259
14260 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014261 tg3_flag(tp, 5750_PLUS))
14262 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014263
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014264 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014265 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014266 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014267
Joe Perches63c3a662011-04-26 08:12:10 +000014268 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014269 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014270 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014271 device_set_wakeup_enable(&tp->pdev->dev, true);
14272 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014273
Linus Torvalds1da177e2005-04-16 15:20:36 -070014274 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014275 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014276
14277 /* serdes signal pre-emphasis in register 0x590 set by */
14278 /* bootcode if bit 18 is set */
14279 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014280 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014281
Joe Perches63c3a662011-04-26 08:12:10 +000014282 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000014283 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
14284 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014285 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014286 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014287
Joe Perches63c3a662011-04-26 08:12:10 +000014288 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000014289 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014290 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014291 u32 cfg3;
14292
14293 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
14294 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000014295 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070014296 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014297
Matt Carlson14417062010-02-17 15:16:59 +000014298 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014299 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014300 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014301 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014302 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014303 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014304 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014305done:
Joe Perches63c3a662011-04-26 08:12:10 +000014306 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014307 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014308 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014309 else
14310 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014311}
14312
Michael Chanc86a8562013-01-06 12:51:08 +000014313static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
14314{
14315 int i, err;
14316 u32 val2, off = offset * 8;
14317
14318 err = tg3_nvram_lock(tp);
14319 if (err)
14320 return err;
14321
14322 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
14323 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
14324 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
14325 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
14326 udelay(10);
14327
14328 for (i = 0; i < 100; i++) {
14329 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
14330 if (val2 & APE_OTP_STATUS_CMD_DONE) {
14331 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
14332 break;
14333 }
14334 udelay(10);
14335 }
14336
14337 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
14338
14339 tg3_nvram_unlock(tp);
14340 if (val2 & APE_OTP_STATUS_CMD_DONE)
14341 return 0;
14342
14343 return -EBUSY;
14344}
14345
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014346static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014347{
14348 int i;
14349 u32 val;
14350
14351 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14352 tw32(OTP_CTRL, cmd);
14353
14354 /* Wait for up to 1 ms for command to execute. */
14355 for (i = 0; i < 100; i++) {
14356 val = tr32(OTP_STATUS);
14357 if (val & OTP_STATUS_CMD_DONE)
14358 break;
14359 udelay(10);
14360 }
14361
14362 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
14363}
14364
14365/* Read the gphy configuration from the OTP region of the chip. The gphy
14366 * configuration is a 32-bit value that straddles the alignment boundary.
14367 * We do two 32-bit reads and then shift and merge the results.
14368 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014369static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014370{
14371 u32 bhalf_otp, thalf_otp;
14372
14373 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
14374
14375 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
14376 return 0;
14377
14378 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
14379
14380 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14381 return 0;
14382
14383 thalf_otp = tr32(OTP_READ_DATA);
14384
14385 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
14386
14387 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14388 return 0;
14389
14390 bhalf_otp = tr32(OTP_READ_DATA);
14391
14392 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
14393}
14394
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014395static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000014396{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000014397 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014398
14399 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14400 adv |= ADVERTISED_1000baseT_Half |
14401 ADVERTISED_1000baseT_Full;
14402
14403 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14404 adv |= ADVERTISED_100baseT_Half |
14405 ADVERTISED_100baseT_Full |
14406 ADVERTISED_10baseT_Half |
14407 ADVERTISED_10baseT_Full |
14408 ADVERTISED_TP;
14409 else
14410 adv |= ADVERTISED_FIBRE;
14411
14412 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000014413 tp->link_config.speed = SPEED_UNKNOWN;
14414 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014415 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000014416 tp->link_config.active_speed = SPEED_UNKNOWN;
14417 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000014418
14419 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014420}
14421
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014422static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070014423{
14424 u32 hw_phy_id_1, hw_phy_id_2;
14425 u32 hw_phy_id, hw_phy_id_masked;
14426 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014427
Matt Carlsone256f8a2011-03-09 16:58:24 +000014428 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000014429 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000014430 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
14431
Michael Chan8151ad52012-07-29 19:15:41 +000014432 if (tg3_flag(tp, ENABLE_APE)) {
14433 switch (tp->pci_fn) {
14434 case 0:
14435 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
14436 break;
14437 case 1:
14438 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
14439 break;
14440 case 2:
14441 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
14442 break;
14443 case 3:
14444 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
14445 break;
14446 }
14447 }
14448
Joe Perches63c3a662011-04-26 08:12:10 +000014449 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014450 return tg3_phy_init(tp);
14451
Linus Torvalds1da177e2005-04-16 15:20:36 -070014452 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010014453 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014454 */
14455 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000014456 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000014457 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014458 } else {
14459 /* Now read the physical PHY_ID from the chip and verify
14460 * that it is sane. If it doesn't look good, we fall back
14461 * to either the hard-coded table based PHY_ID and failing
14462 * that the value found in the eeprom area.
14463 */
14464 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
14465 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
14466
14467 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
14468 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
14469 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
14470
Matt Carlson79eb6902010-02-17 15:17:03 +000014471 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014472 }
14473
Matt Carlson79eb6902010-02-17 15:17:03 +000014474 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014475 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000014476 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014477 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070014478 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014479 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014480 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000014481 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070014482 /* Do nothing, phy ID already set up in
14483 * tg3_get_eeprom_hw_cfg().
14484 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014485 } else {
14486 struct subsys_tbl_ent *p;
14487
14488 /* No eeprom signature? Try the hardcoded
14489 * subsys device table.
14490 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014491 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014492 if (p) {
14493 tp->phy_id = p->phy_id;
14494 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
14495 /* For now we saw the IDs 0xbc050cd0,
14496 * 0xbc050f80 and 0xbc050c30 on devices
14497 * connected to an BCM4785 and there are
14498 * probably more. Just assume that the phy is
14499 * supported when it is connected to a SSB core
14500 * for now.
14501 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014502 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014504
Linus Torvalds1da177e2005-04-16 15:20:36 -070014505 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000014506 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014507 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014508 }
14509 }
14510
Matt Carlsona6b68da2010-12-06 08:28:52 +000014511 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000014512 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
14513 tg3_asic_rev(tp) == ASIC_REV_5720 ||
14514 tg3_asic_rev(tp) == ASIC_REV_5762 ||
14515 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
14516 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
14517 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
14518 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000014519 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
14520
Matt Carlsone256f8a2011-03-09 16:58:24 +000014521 tg3_phy_init_link_config(tp);
14522
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014523 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014524 !tg3_flag(tp, ENABLE_APE) &&
14525 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014526 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014527
14528 tg3_readphy(tp, MII_BMSR, &bmsr);
14529 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
14530 (bmsr & BMSR_LSTATUS))
14531 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014532
Linus Torvalds1da177e2005-04-16 15:20:36 -070014533 err = tg3_phy_reset(tp);
14534 if (err)
14535 return err;
14536
Matt Carlson42b64a42011-05-19 12:12:49 +000014537 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014538
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014539 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000014540 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
14541 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014542
14543 tg3_writephy(tp, MII_BMCR,
14544 BMCR_ANENABLE | BMCR_ANRESTART);
14545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014546 }
14547
14548skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000014549 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014550 err = tg3_init_5401phy_dsp(tp);
14551 if (err)
14552 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553
Linus Torvalds1da177e2005-04-16 15:20:36 -070014554 err = tg3_init_5401phy_dsp(tp);
14555 }
14556
Linus Torvalds1da177e2005-04-16 15:20:36 -070014557 return err;
14558}
14559
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014560static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014561{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014562 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014563 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000014564 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000014565 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014566
Matt Carlson535a4902011-07-20 10:20:56 +000014567 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014568 if (!vpd_data)
14569 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014570
Matt Carlson535a4902011-07-20 10:20:56 +000014571 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000014572 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014573 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014574
14575 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
14576 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
14577 i += PCI_VPD_LRDT_TAG_SIZE;
14578
Matt Carlson535a4902011-07-20 10:20:56 +000014579 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014580 goto out_not_found;
14581
Matt Carlson184b8902010-04-05 10:19:25 +000014582 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14583 PCI_VPD_RO_KEYWORD_MFR_ID);
14584 if (j > 0) {
14585 len = pci_vpd_info_field_size(&vpd_data[j]);
14586
14587 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14588 if (j + len > block_end || len != 4 ||
14589 memcmp(&vpd_data[j], "1028", 4))
14590 goto partno;
14591
14592 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14593 PCI_VPD_RO_KEYWORD_VENDOR0);
14594 if (j < 0)
14595 goto partno;
14596
14597 len = pci_vpd_info_field_size(&vpd_data[j]);
14598
14599 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14600 if (j + len > block_end)
14601 goto partno;
14602
14603 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000014604 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000014605 }
14606
14607partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000014608 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14609 PCI_VPD_RO_KEYWORD_PARTNO);
14610 if (i < 0)
14611 goto out_not_found;
14612
14613 len = pci_vpd_info_field_size(&vpd_data[i]);
14614
14615 i += PCI_VPD_INFO_FLD_HDR_SIZE;
14616 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000014617 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014618 goto out_not_found;
14619
14620 memcpy(tp->board_part_number, &vpd_data[i], len);
14621
Linus Torvalds1da177e2005-04-16 15:20:36 -070014622out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014623 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000014624 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014625 return;
14626
14627out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000014628 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000014629 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
14630 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000014631 strcpy(tp->board_part_number, "BCM5717");
14632 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
14633 strcpy(tp->board_part_number, "BCM5718");
14634 else
14635 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014636 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000014637 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
14638 strcpy(tp->board_part_number, "BCM57780");
14639 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
14640 strcpy(tp->board_part_number, "BCM57760");
14641 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
14642 strcpy(tp->board_part_number, "BCM57790");
14643 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
14644 strcpy(tp->board_part_number, "BCM57788");
14645 else
14646 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014647 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000014648 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
14649 strcpy(tp->board_part_number, "BCM57761");
14650 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
14651 strcpy(tp->board_part_number, "BCM57765");
14652 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
14653 strcpy(tp->board_part_number, "BCM57781");
14654 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
14655 strcpy(tp->board_part_number, "BCM57785");
14656 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
14657 strcpy(tp->board_part_number, "BCM57791");
14658 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
14659 strcpy(tp->board_part_number, "BCM57795");
14660 else
14661 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014662 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000014663 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
14664 strcpy(tp->board_part_number, "BCM57762");
14665 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
14666 strcpy(tp->board_part_number, "BCM57766");
14667 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
14668 strcpy(tp->board_part_number, "BCM57782");
14669 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14670 strcpy(tp->board_part_number, "BCM57786");
14671 else
14672 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014673 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070014674 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000014675 } else {
14676nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070014677 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000014678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014679}
14680
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014681static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070014682{
14683 u32 val;
14684
Matt Carlsone4f34112009-02-25 14:25:00 +000014685 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014686 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014687 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014688 val != 0)
14689 return 0;
14690
14691 return 1;
14692}
14693
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014694static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014695{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014696 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000014697 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014698 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014699
14700 if (tg3_nvram_read(tp, 0xc, &offset) ||
14701 tg3_nvram_read(tp, 0x4, &start))
14702 return;
14703
14704 offset = tg3_nvram_logical_addr(tp, offset);
14705
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014706 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014707 return;
14708
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014709 if ((val & 0xfc000000) == 0x0c000000) {
14710 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014711 return;
14712
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014713 if (val == 0)
14714 newver = true;
14715 }
14716
Matt Carlson75f99362010-04-05 10:19:24 +000014717 dst_off = strlen(tp->fw_ver);
14718
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014719 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000014720 if (TG3_VER_SIZE - dst_off < 16 ||
14721 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014722 return;
14723
14724 offset = offset + ver_offset - start;
14725 for (i = 0; i < 16; i += 4) {
14726 __be32 v;
14727 if (tg3_nvram_read_be32(tp, offset + i, &v))
14728 return;
14729
Matt Carlson75f99362010-04-05 10:19:24 +000014730 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014731 }
14732 } else {
14733 u32 major, minor;
14734
14735 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
14736 return;
14737
14738 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
14739 TG3_NVM_BCVER_MAJSFT;
14740 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000014741 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
14742 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014743 }
14744}
14745
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014746static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014747{
14748 u32 val, major, minor;
14749
14750 /* Use native endian representation */
14751 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
14752 return;
14753
14754 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
14755 TG3_NVM_HWSB_CFG1_MAJSFT;
14756 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
14757 TG3_NVM_HWSB_CFG1_MINSFT;
14758
14759 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
14760}
14761
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014762static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080014763{
14764 u32 offset, major, minor, build;
14765
Matt Carlson75f99362010-04-05 10:19:24 +000014766 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014767
14768 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
14769 return;
14770
14771 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
14772 case TG3_EEPROM_SB_REVISION_0:
14773 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
14774 break;
14775 case TG3_EEPROM_SB_REVISION_2:
14776 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
14777 break;
14778 case TG3_EEPROM_SB_REVISION_3:
14779 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
14780 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000014781 case TG3_EEPROM_SB_REVISION_4:
14782 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
14783 break;
14784 case TG3_EEPROM_SB_REVISION_5:
14785 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
14786 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000014787 case TG3_EEPROM_SB_REVISION_6:
14788 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
14789 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014790 default:
14791 return;
14792 }
14793
Matt Carlsone4f34112009-02-25 14:25:00 +000014794 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080014795 return;
14796
14797 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
14798 TG3_EEPROM_SB_EDH_BLD_SHFT;
14799 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
14800 TG3_EEPROM_SB_EDH_MAJ_SHFT;
14801 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
14802
14803 if (minor > 99 || build > 26)
14804 return;
14805
Matt Carlson75f99362010-04-05 10:19:24 +000014806 offset = strlen(tp->fw_ver);
14807 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
14808 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014809
14810 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000014811 offset = strlen(tp->fw_ver);
14812 if (offset < TG3_VER_SIZE - 1)
14813 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014814 }
14815}
14816
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014817static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080014818{
14819 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014820 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070014821
14822 for (offset = TG3_NVM_DIR_START;
14823 offset < TG3_NVM_DIR_END;
14824 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014825 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014826 return;
14827
14828 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
14829 break;
14830 }
14831
14832 if (offset == TG3_NVM_DIR_END)
14833 return;
14834
Joe Perches63c3a662011-04-26 08:12:10 +000014835 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014836 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000014837 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014838 return;
14839
Matt Carlsone4f34112009-02-25 14:25:00 +000014840 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014841 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014842 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014843 return;
14844
14845 offset += val - start;
14846
Matt Carlsonacd9c112009-02-25 14:26:33 +000014847 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014848
Matt Carlsonacd9c112009-02-25 14:26:33 +000014849 tp->fw_ver[vlen++] = ',';
14850 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070014851
14852 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000014853 __be32 v;
14854 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014855 return;
14856
Al Virob9fc7dc2007-12-17 22:59:57 -080014857 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014858
Matt Carlsonacd9c112009-02-25 14:26:33 +000014859 if (vlen > TG3_VER_SIZE - sizeof(v)) {
14860 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014861 break;
14862 }
14863
Matt Carlsonacd9c112009-02-25 14:26:33 +000014864 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
14865 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014866 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000014867}
14868
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014869static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000014870{
Matt Carlson7fd76442009-02-25 14:27:20 +000014871 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000014872
14873 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
14874 if (apedata != APE_SEG_SIG_MAGIC)
14875 return;
14876
14877 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
14878 if (!(apedata & APE_FW_STATUS_READY))
14879 return;
14880
Michael Chan165f4d12012-07-16 16:23:59 +000014881 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
14882 tg3_flag_set(tp, APE_HAS_NCSI);
14883}
14884
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014885static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000014886{
14887 int vlen;
14888 u32 apedata;
14889 char *fwtype;
14890
Matt Carlson7fd76442009-02-25 14:27:20 +000014891 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
14892
Michael Chan165f4d12012-07-16 16:23:59 +000014893 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000014894 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000014895 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
14896 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000014897 else
Matt Carlsonecc79642010-08-02 11:26:01 +000014898 fwtype = "DASH";
14899
Matt Carlson7fd76442009-02-25 14:27:20 +000014900 vlen = strlen(tp->fw_ver);
14901
Matt Carlsonecc79642010-08-02 11:26:01 +000014902 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
14903 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000014904 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
14905 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
14906 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
14907 (apedata & APE_FW_VERSION_BLDMSK));
14908}
14909
Michael Chanc86a8562013-01-06 12:51:08 +000014910static void tg3_read_otp_ver(struct tg3 *tp)
14911{
14912 u32 val, val2;
14913
Joe Perches41535772013-02-16 11:20:04 +000014914 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000014915 return;
14916
14917 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
14918 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
14919 TG3_OTP_MAGIC0_VALID(val)) {
14920 u64 val64 = (u64) val << 32 | val2;
14921 u32 ver = 0;
14922 int i, vlen;
14923
14924 for (i = 0; i < 7; i++) {
14925 if ((val64 & 0xff) == 0)
14926 break;
14927 ver = val64 & 0xff;
14928 val64 >>= 8;
14929 }
14930 vlen = strlen(tp->fw_ver);
14931 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
14932 }
14933}
14934
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014935static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014936{
14937 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000014938 bool vpd_vers = false;
14939
14940 if (tp->fw_ver[0] != 0)
14941 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014942
Joe Perches63c3a662011-04-26 08:12:10 +000014943 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000014944 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000014945 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000014946 return;
14947 }
14948
Matt Carlsonacd9c112009-02-25 14:26:33 +000014949 if (tg3_nvram_read(tp, 0, &val))
14950 return;
14951
14952 if (val == TG3_EEPROM_MAGIC)
14953 tg3_read_bc_ver(tp);
14954 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
14955 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014956 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
14957 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014958
Michael Chan165f4d12012-07-16 16:23:59 +000014959 if (tg3_flag(tp, ENABLE_ASF)) {
14960 if (tg3_flag(tp, ENABLE_APE)) {
14961 tg3_probe_ncsi(tp);
14962 if (!vpd_vers)
14963 tg3_read_dash_ver(tp);
14964 } else if (!vpd_vers) {
14965 tg3_read_mgmtfw_ver(tp);
14966 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000014967 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070014968
14969 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080014970}
14971
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014972static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
14973{
Joe Perches63c3a662011-04-26 08:12:10 +000014974 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014975 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000014976 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014977 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014978 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000014979 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014980}
14981
Matt Carlson41434702011-03-09 16:58:22 +000014982static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014983 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
14984 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
14985 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
14986 { },
14987};
14988
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014989static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000014990{
14991 struct pci_dev *peer;
14992 unsigned int func, devnr = tp->pdev->devfn & ~7;
14993
14994 for (func = 0; func < 8; func++) {
14995 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14996 if (peer && peer != tp->pdev)
14997 break;
14998 pci_dev_put(peer);
14999 }
15000 /* 5704 can be configured in single-port mode, set peer to
15001 * tp->pdev in that case.
15002 */
15003 if (!peer) {
15004 peer = tp->pdev;
15005 return peer;
15006 }
15007
15008 /*
15009 * We don't need to keep the refcount elevated; there's no way
15010 * to remove one half of this device without removing the other
15011 */
15012 pci_dev_put(peer);
15013
15014 return peer;
15015}
15016
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015017static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015018{
15019 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015020 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015021 u32 reg;
15022
15023 /* All devices that use the alternate
15024 * ASIC REV location have a CPMU.
15025 */
15026 tg3_flag_set(tp, CPMU_PRESENT);
15027
15028 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015029 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015030 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15031 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015032 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
15033 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15034 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
15035 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727)
Matt Carlson42b123b2012-02-13 15:20:13 +000015036 reg = TG3PCI_GEN2_PRODID_ASICREV;
15037 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15038 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15039 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15040 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15041 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15042 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15043 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15044 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15045 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15046 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15047 reg = TG3PCI_GEN15_PRODID_ASICREV;
15048 else
15049 reg = TG3PCI_PRODID_ASICREV;
15050
15051 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15052 }
15053
15054 /* Wrong chip ID in 5752 A0. This code can be removed later
15055 * as A0 is not in production.
15056 */
Joe Perches41535772013-02-16 11:20:04 +000015057 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015058 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15059
Joe Perches41535772013-02-16 11:20:04 +000015060 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015061 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15062
Joe Perches41535772013-02-16 11:20:04 +000015063 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15064 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15065 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015066 tg3_flag_set(tp, 5717_PLUS);
15067
Joe Perches41535772013-02-16 11:20:04 +000015068 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15069 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015070 tg3_flag_set(tp, 57765_CLASS);
15071
Michael Chanc65a17f2013-01-06 12:51:07 +000015072 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015073 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015074 tg3_flag_set(tp, 57765_PLUS);
15075
15076 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015077 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15078 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15079 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15080 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15081 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15082 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015083 tg3_flag(tp, 57765_PLUS))
15084 tg3_flag_set(tp, 5755_PLUS);
15085
Joe Perches41535772013-02-16 11:20:04 +000015086 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15087 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015088 tg3_flag_set(tp, 5780_CLASS);
15089
Joe Perches41535772013-02-16 11:20:04 +000015090 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15091 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15092 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015093 tg3_flag(tp, 5755_PLUS) ||
15094 tg3_flag(tp, 5780_CLASS))
15095 tg3_flag_set(tp, 5750_PLUS);
15096
Joe Perches41535772013-02-16 11:20:04 +000015097 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015098 tg3_flag(tp, 5750_PLUS))
15099 tg3_flag_set(tp, 5705_PLUS);
15100}
15101
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015102static bool tg3_10_100_only_device(struct tg3 *tp,
15103 const struct pci_device_id *ent)
15104{
15105 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
15106
Joe Perches41535772013-02-16 11:20:04 +000015107 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
15108 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015109 (tp->phy_flags & TG3_PHYFLG_IS_FET))
15110 return true;
15111
15112 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000015113 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015114 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
15115 return true;
15116 } else {
15117 return true;
15118 }
15119 }
15120
15121 return false;
15122}
15123
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015124static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015125{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015126 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015127 u32 pci_state_reg, grc_misc_cfg;
15128 u32 val;
15129 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015130 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015131
Linus Torvalds1da177e2005-04-16 15:20:36 -070015132 /* Force memory write invalidate off. If we leave it on,
15133 * then on 5700_BX chips we have to enable a workaround.
15134 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15135 * to match the cacheline size. The Broadcom driver have this
15136 * workaround but turns MWI off all the times so never uses
15137 * it. This seems to suggest that the workaround is insufficient.
15138 */
15139 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15140 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15141 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15142
Matt Carlson16821282011-07-13 09:27:28 +000015143 /* Important! -- Make sure register accesses are byteswapped
15144 * correctly. Also, for those chips that require it, make
15145 * sure that indirect register accesses are enabled before
15146 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015147 */
15148 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15149 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015150 tp->misc_host_ctrl |= (misc_ctrl_reg &
15151 MISC_HOST_CTRL_CHIPREV);
15152 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15153 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015154
Matt Carlson42b123b2012-02-13 15:20:13 +000015155 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015156
Michael Chan68929142005-08-09 20:17:14 -070015157 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15158 * we need to disable memory and use config. cycles
15159 * only to access all registers. The 5702/03 chips
15160 * can mistakenly decode the special cycles from the
15161 * ICH chipsets as memory write cycles, causing corruption
15162 * of register and memory space. Only certain ICH bridges
15163 * will drive special cycles with non-zero data during the
15164 * address phase which can fall within the 5703's address
15165 * range. This is not an ICH bug as the PCI spec allows
15166 * non-zero address during special cycles. However, only
15167 * these ICH bridges are known to drive non-zero addresses
15168 * during special cycles.
15169 *
15170 * Since special cycles do not cross PCI bridges, we only
15171 * enable this workaround if the 5703 is on the secondary
15172 * bus of these ICH bridges.
15173 */
Joe Perches41535772013-02-16 11:20:04 +000015174 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
15175 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070015176 static struct tg3_dev_id {
15177 u32 vendor;
15178 u32 device;
15179 u32 rev;
15180 } ich_chipsets[] = {
15181 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15182 PCI_ANY_ID },
15183 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15184 PCI_ANY_ID },
15185 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15186 0xa },
15187 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15188 PCI_ANY_ID },
15189 { },
15190 };
15191 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15192 struct pci_dev *bridge = NULL;
15193
15194 while (pci_id->vendor != 0) {
15195 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15196 bridge);
15197 if (!bridge) {
15198 pci_id++;
15199 continue;
15200 }
15201 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015202 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015203 continue;
15204 }
15205 if (bridge->subordinate &&
15206 (bridge->subordinate->number ==
15207 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015208 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015209 pci_dev_put(bridge);
15210 break;
15211 }
15212 }
15213 }
15214
Joe Perches41535772013-02-16 11:20:04 +000015215 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070015216 static struct tg3_dev_id {
15217 u32 vendor;
15218 u32 device;
15219 } bridge_chipsets[] = {
15220 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15221 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15222 { },
15223 };
15224 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15225 struct pci_dev *bridge = NULL;
15226
15227 while (pci_id->vendor != 0) {
15228 bridge = pci_get_device(pci_id->vendor,
15229 pci_id->device,
15230 bridge);
15231 if (!bridge) {
15232 pci_id++;
15233 continue;
15234 }
15235 if (bridge->subordinate &&
15236 (bridge->subordinate->number <=
15237 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015238 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070015239 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015240 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070015241 pci_dev_put(bridge);
15242 break;
15243 }
15244 }
15245 }
15246
Michael Chan4a29cc22006-03-19 13:21:12 -080015247 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15248 * DMA addresses > 40-bit. This bridge may have other additional
15249 * 57xx devices behind it in some 4-port NIC designs for example.
15250 * Any tg3 device found behind the bridge will also need the 40-bit
15251 * DMA workaround.
15252 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015253 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015254 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015255 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015256 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015257 struct pci_dev *bridge = NULL;
15258
15259 do {
15260 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15261 PCI_DEVICE_ID_SERVERWORKS_EPB,
15262 bridge);
15263 if (bridge && bridge->subordinate &&
15264 (bridge->subordinate->number <=
15265 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015266 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015267 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015268 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015269 pci_dev_put(bridge);
15270 break;
15271 }
15272 } while (bridge);
15273 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015274
Joe Perches41535772013-02-16 11:20:04 +000015275 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
15276 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015277 tp->pdev_peer = tg3_find_peer(tp);
15278
Matt Carlson507399f2009-11-13 13:03:37 +000015279 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000015280 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015281 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015282 else if (tg3_flag(tp, 57765_PLUS))
15283 tg3_flag_set(tp, HW_TSO_3);
15284 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015285 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015286 tg3_flag_set(tp, HW_TSO_2);
15287 else if (tg3_flag(tp, 5750_PLUS)) {
15288 tg3_flag_set(tp, HW_TSO_1);
15289 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015290 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
15291 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015292 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015293 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15294 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15295 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015296 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015297 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000015298 tp->fw_needed = FIRMWARE_TG3TSO5;
15299 else
15300 tp->fw_needed = FIRMWARE_TG3TSO;
15301 }
15302
Matt Carlsondabc5c62011-05-19 12:12:52 +000015303 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015304 if (tg3_flag(tp, HW_TSO_1) ||
15305 tg3_flag(tp, HW_TSO_2) ||
15306 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015307 tp->fw_needed) {
15308 /* For firmware TSO, assume ASF is disabled.
15309 * We'll disable TSO later if we discover ASF
15310 * is enabled in tg3_get_eeprom_hw_cfg().
15311 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015312 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015313 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015314 tg3_flag_clear(tp, TSO_CAPABLE);
15315 tg3_flag_clear(tp, TSO_BUG);
15316 tp->fw_needed = NULL;
15317 }
15318
Joe Perches41535772013-02-16 11:20:04 +000015319 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000015320 tp->fw_needed = FIRMWARE_TG3;
15321
Matt Carlson507399f2009-11-13 13:03:37 +000015322 tp->irq_max = 1;
15323
Joe Perches63c3a662011-04-26 08:12:10 +000015324 if (tg3_flag(tp, 5750_PLUS)) {
15325 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000015326 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
15327 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
15328 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
15329 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070015330 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015331 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015332
Joe Perches63c3a662011-04-26 08:12:10 +000015333 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015334 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015335 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015337
Joe Perches63c3a662011-04-26 08:12:10 +000015338 if (tg3_flag(tp, 57765_PLUS)) {
15339 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000015340 tp->irq_max = TG3_IRQ_MAX_VECS;
15341 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015342 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000015343
Michael Chan91024262012-09-28 07:12:38 +000015344 tp->txq_max = 1;
15345 tp->rxq_max = 1;
15346 if (tp->irq_max > 1) {
15347 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
15348 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
15349
Joe Perches41535772013-02-16 11:20:04 +000015350 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15351 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000015352 tp->txq_max = tp->irq_max - 1;
15353 }
15354
Matt Carlsonb7abee62012-06-07 12:56:54 +000015355 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015356 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015357 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015358
Joe Perches41535772013-02-16 11:20:04 +000015359 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000015360 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000015361
Joe Perches41535772013-02-16 11:20:04 +000015362 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15363 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15364 tg3_asic_rev(tp) == ASIC_REV_5720 ||
15365 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000015366 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000015367
Joe Perches63c3a662011-04-26 08:12:10 +000015368 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000015369 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000015370 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000015371
Joe Perches63c3a662011-04-26 08:12:10 +000015372 if (!tg3_flag(tp, 5705_PLUS) ||
15373 tg3_flag(tp, 5780_CLASS) ||
15374 tg3_flag(tp, USE_JUMBO_BDFLAG))
15375 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070015376
Matt Carlson52f44902008-11-21 17:17:04 -080015377 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15378 &pci_state_reg);
15379
Jon Mason708ebb32011-06-27 12:56:50 +000015380 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015381 u16 lnkctl;
15382
Joe Perches63c3a662011-04-26 08:12:10 +000015383 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080015384
Jiang Liu0f49bfb2012-08-20 13:28:20 -060015385 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015386 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000015387 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015388 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000015389 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000015390 }
Joe Perches41535772013-02-16 11:20:04 +000015391 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
15392 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15393 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
15394 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000015395 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015396 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015397 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080015398 }
Joe Perches41535772013-02-16 11:20:04 +000015399 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000015400 /* BCM5785 devices are effectively PCIe devices, and should
15401 * follow PCIe codepaths, but do not have a PCIe capabilities
15402 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000015403 */
Joe Perches63c3a662011-04-26 08:12:10 +000015404 tg3_flag_set(tp, PCI_EXPRESS);
15405 } else if (!tg3_flag(tp, 5705_PLUS) ||
15406 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080015407 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
15408 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000015409 dev_err(&tp->pdev->dev,
15410 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080015411 return -EIO;
15412 }
15413
15414 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000015415 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080015416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015417
Michael Chan399de502005-10-03 14:02:39 -070015418 /* If we have an AMD 762 or VIA K8T800 chipset, write
15419 * reordering to the mailbox registers done by the host
15420 * controller can cause major troubles. We read back from
15421 * every mailbox register write to force the writes to be
15422 * posted to the chip in order.
15423 */
Matt Carlson41434702011-03-09 16:58:22 +000015424 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015425 !tg3_flag(tp, PCI_EXPRESS))
15426 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070015427
Matt Carlson69fc4052008-12-21 20:19:57 -080015428 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
15429 &tp->pci_cacheline_sz);
15430 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15431 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000015432 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015433 tp->pci_lat_timer < 64) {
15434 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080015435 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15436 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015437 }
15438
Matt Carlson16821282011-07-13 09:27:28 +000015439 /* Important! -- It is critical that the PCI-X hw workaround
15440 * situation is decided before the first MMIO register access.
15441 */
Joe Perches41535772013-02-16 11:20:04 +000015442 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080015443 /* 5700 BX chips need to have their TX producer index
15444 * mailboxes written twice to workaround a bug.
15445 */
Joe Perches63c3a662011-04-26 08:12:10 +000015446 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070015447
Matt Carlson52f44902008-11-21 17:17:04 -080015448 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015449 *
15450 * The workaround is to use indirect register accesses
15451 * for all chip writes not to mailbox registers.
15452 */
Joe Perches63c3a662011-04-26 08:12:10 +000015453 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015454 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015455
Joe Perches63c3a662011-04-26 08:12:10 +000015456 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015457
15458 /* The chip can have it's power management PCI config
15459 * space registers clobbered due to this bug.
15460 * So explicitly force the chip into D0 here.
15461 */
Matt Carlson9974a352007-10-07 23:27:28 -070015462 pci_read_config_dword(tp->pdev,
15463 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015464 &pm_reg);
15465 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
15466 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070015467 pci_write_config_dword(tp->pdev,
15468 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015469 pm_reg);
15470
15471 /* Also, force SERR#/PERR# in PCI command. */
15472 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15473 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
15474 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15475 }
15476 }
15477
Linus Torvalds1da177e2005-04-16 15:20:36 -070015478 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015479 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015480 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015481 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015482
15483 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000015484 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015485 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
15486 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
15487 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
15488 }
15489
Michael Chan1ee582d2005-08-09 20:16:46 -070015490 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070015491 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015492 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070015493 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070015494 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015495 tp->write32_tx_mbox = tg3_write32;
15496 tp->write32_rx_mbox = tg3_write32;
15497
15498 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000015499 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070015500 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000015501 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015502 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000015503 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070015504 /*
15505 * Back to back register writes can cause problems on these
15506 * chips, the workaround is to read back all reg writes
15507 * except those to mailbox regs.
15508 *
15509 * See tg3_write_indirect_reg32().
15510 */
Michael Chan1ee582d2005-08-09 20:16:46 -070015511 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015512 }
15513
Joe Perches63c3a662011-04-26 08:12:10 +000015514 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070015515 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000015516 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070015517 tp->write32_rx_mbox = tg3_write_flush_reg32;
15518 }
Michael Chan20094932005-08-09 20:16:32 -070015519
Joe Perches63c3a662011-04-26 08:12:10 +000015520 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070015521 tp->read32 = tg3_read_indirect_reg32;
15522 tp->write32 = tg3_write_indirect_reg32;
15523 tp->read32_mbox = tg3_read_indirect_mbox;
15524 tp->write32_mbox = tg3_write_indirect_mbox;
15525 tp->write32_tx_mbox = tg3_write_indirect_mbox;
15526 tp->write32_rx_mbox = tg3_write_indirect_mbox;
15527
15528 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015529 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015530
15531 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15532 pci_cmd &= ~PCI_COMMAND_MEMORY;
15533 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15534 }
Joe Perches41535772013-02-16 11:20:04 +000015535 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015536 tp->read32_mbox = tg3_read32_mbox_5906;
15537 tp->write32_mbox = tg3_write32_mbox_5906;
15538 tp->write32_tx_mbox = tg3_write32_mbox_5906;
15539 tp->write32_rx_mbox = tg3_write32_mbox_5906;
15540 }
Michael Chan68929142005-08-09 20:17:14 -070015541
Michael Chanbbadf502006-04-06 21:46:34 -070015542 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015543 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000015544 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15545 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000015546 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070015547
Matt Carlson16821282011-07-13 09:27:28 +000015548 /* The memory arbiter has to be enabled in order for SRAM accesses
15549 * to succeed. Normally on powerup the tg3 chip firmware will make
15550 * sure it is enabled, but other entities such as system netboot
15551 * code might disable it.
15552 */
15553 val = tr32(MEMARB_MODE);
15554 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
15555
Matt Carlson9dc5e342011-11-04 09:15:02 +000015556 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000015557 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000015558 tg3_flag(tp, 5780_CLASS)) {
15559 if (tg3_flag(tp, PCIX_MODE)) {
15560 pci_read_config_dword(tp->pdev,
15561 tp->pcix_cap + PCI_X_STATUS,
15562 &val);
15563 tp->pci_fn = val & 0x7;
15564 }
Joe Perches41535772013-02-16 11:20:04 +000015565 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15566 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15567 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000015568 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000015569 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
15570 val = tr32(TG3_CPMU_STATUS);
15571
Joe Perches41535772013-02-16 11:20:04 +000015572 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000015573 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
15574 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000015575 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
15576 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000015577 }
15578
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015579 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
15580 tp->write32_tx_mbox = tg3_write_flush_reg32;
15581 tp->write32_rx_mbox = tg3_write_flush_reg32;
15582 }
15583
Michael Chan7d0c41e2005-04-21 17:06:20 -070015584 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000015585 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070015586 * determined before calling tg3_set_power_state() so that
15587 * we know whether or not to switch out of Vaux power.
15588 * When the flag is set, it means that GPIO1 is used for eeprom
15589 * write protect and also implies that it is a LOM where GPIOs
15590 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015591 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070015592 tg3_get_eeprom_hw_cfg(tp);
15593
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015594 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
15595 tg3_flag_clear(tp, TSO_CAPABLE);
15596 tg3_flag_clear(tp, TSO_BUG);
15597 tp->fw_needed = NULL;
15598 }
15599
Joe Perches63c3a662011-04-26 08:12:10 +000015600 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070015601 /* Allow reads and writes to the
15602 * APE register and memory space.
15603 */
15604 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000015605 PCISTATE_ALLOW_APE_SHMEM_WR |
15606 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015607 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
15608 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000015609
15610 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015611 }
15612
Matt Carlson16821282011-07-13 09:27:28 +000015613 /* Set up tp->grc_local_ctrl before calling
15614 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
15615 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070015616 * It is also used as eeprom write protect on LOMs.
15617 */
15618 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000015619 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015620 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070015621 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
15622 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070015623 /* Unused GPIO3 must be driven as output on 5752 because there
15624 * are no pull-up resistors on unused GPIO pins.
15625 */
Joe Perches41535772013-02-16 11:20:04 +000015626 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070015627 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070015628
Joe Perches41535772013-02-16 11:20:04 +000015629 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15630 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015631 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080015632 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
15633
Matt Carlson8d519ab2009-04-20 06:58:01 +000015634 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15635 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015636 /* Turn off the debug UART. */
15637 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000015638 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015639 /* Keep VMain power. */
15640 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
15641 GRC_LCLCTRL_GPIO_OUTPUT0;
15642 }
15643
Joe Perches41535772013-02-16 11:20:04 +000015644 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015645 tp->grc_local_ctrl |=
15646 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
15647
Matt Carlson16821282011-07-13 09:27:28 +000015648 /* Switch out of Vaux if it is a NIC */
15649 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015650
Linus Torvalds1da177e2005-04-16 15:20:36 -070015651 /* Derive initial jumbo mode from MTU assigned in
15652 * ether_setup() via the alloc_etherdev() call
15653 */
Joe Perches63c3a662011-04-26 08:12:10 +000015654 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
15655 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015656
15657 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000015658 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15659 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
15660 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
15661 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000015662 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015663 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015664 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015665 }
15666
Joe Perches41535772013-02-16 11:20:04 +000015667 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015668 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015669
Linus Torvalds1da177e2005-04-16 15:20:36 -070015670 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000015671 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15672 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
15673 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
15674 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015675 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
15676 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15677 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015678
Joe Perches41535772013-02-16 11:20:04 +000015679 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
15680 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015681 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000015682 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015683 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015684
Joe Perches63c3a662011-04-26 08:12:10 +000015685 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015686 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000015687 tg3_asic_rev(tp) != ASIC_REV_5785 &&
15688 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015689 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000015690 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15691 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15692 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15693 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080015694 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
15695 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015696 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080015697 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015698 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080015699 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015700 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070015701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015702
Joe Perches41535772013-02-16 11:20:04 +000015703 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15704 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015705 tp->phy_otp = tg3_read_otp_phycfg(tp);
15706 if (tp->phy_otp == 0)
15707 tp->phy_otp = TG3_OTP_DEFAULT;
15708 }
15709
Joe Perches63c3a662011-04-26 08:12:10 +000015710 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070015711 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
15712 else
15713 tp->mi_mode = MAC_MI_MODE_BASE;
15714
Linus Torvalds1da177e2005-04-16 15:20:36 -070015715 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000015716 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
15717 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015718 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
15719
Matt Carlson4d958472011-04-20 07:57:35 +000015720 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000015721 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15722 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
15723 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000015724 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
15725 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
15726 }
15727
Joe Perches41535772013-02-16 11:20:04 +000015728 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
15729 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000015730 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070015731
Matt Carlson158d7ab2008-05-29 01:37:54 -070015732 err = tg3_mdio_init(tp);
15733 if (err)
15734 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015735
15736 /* Initialize data/descriptor byte/word swapping. */
15737 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000015738 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
15739 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000015740 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
15741 GRC_MODE_WORD_SWAP_B2HRX_DATA |
15742 GRC_MODE_B2HRX_ENABLE |
15743 GRC_MODE_HTX2B_ENABLE |
15744 GRC_MODE_HOST_STACKUP);
15745 else
15746 val &= GRC_MODE_HOST_STACKUP;
15747
Linus Torvalds1da177e2005-04-16 15:20:36 -070015748 tw32(GRC_MODE, val | tp->grc_mode);
15749
15750 tg3_switch_clocks(tp);
15751
15752 /* Clear this out for sanity. */
15753 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
15754
15755 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15756 &pci_state_reg);
15757 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015758 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000015759 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
15760 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
15761 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
15762 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015763 void __iomem *sram_base;
15764
15765 /* Write some dummy words into the SRAM status block
15766 * area, see if it reads back correctly. If the return
15767 * value is bad, force enable the PCIX workaround.
15768 */
15769 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
15770
15771 writel(0x00000000, sram_base);
15772 writel(0x00000000, sram_base + 4);
15773 writel(0xffffffff, sram_base + 4);
15774 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000015775 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015776 }
15777 }
15778
15779 udelay(50);
15780 tg3_nvram_init(tp);
15781
15782 grc_misc_cfg = tr32(GRC_MISC_CFG);
15783 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
15784
Joe Perches41535772013-02-16 11:20:04 +000015785 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015786 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
15787 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000015788 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015789
Joe Perches63c3a662011-04-26 08:12:10 +000015790 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000015791 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015792 tg3_flag_set(tp, TAGGED_STATUS);
15793 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070015794 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
15795 HOSTCC_MODE_CLRTICK_TXBD);
15796
15797 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
15798 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15799 tp->misc_host_ctrl);
15800 }
15801
Matt Carlson3bda1252008-08-15 14:08:22 -070015802 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000015803 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000015804 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070015805 else
Matt Carlson6e01b202011-08-19 13:58:20 +000015806 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070015807
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015808 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015809 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015810
15811 err = tg3_phy_probe(tp);
15812 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015813 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015814 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015815 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015816 }
15817
Matt Carlson184b8902010-04-05 10:19:25 +000015818 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080015819 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015820
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015821 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
15822 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015823 } else {
Joe Perches41535772013-02-16 11:20:04 +000015824 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015825 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015826 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015827 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015828 }
15829
15830 /* 5700 {AX,BX} chips have a broken status block link
15831 * change bit implementation, so we must use the
15832 * status register in those cases.
15833 */
Joe Perches41535772013-02-16 11:20:04 +000015834 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015835 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015836 else
Joe Perches63c3a662011-04-26 08:12:10 +000015837 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015838
15839 /* The led_ctrl is set during tg3_phy_probe, here we might
15840 * have to force the link status polling mechanism based
15841 * upon subsystem IDs.
15842 */
15843 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000015844 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015845 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
15846 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000015847 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015848 }
15849
15850 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015851 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000015852 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015853 else
Joe Perches63c3a662011-04-26 08:12:10 +000015854 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015855
Eric Dumazet9205fd92011-11-18 06:47:01 +000015856 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015857 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000015858 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015859 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000015860 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015861#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000015862 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015863#endif
15864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015865
Matt Carlson2c49a442010-09-30 10:34:35 +000015866 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
15867 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015868 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
15869
Matt Carlson2c49a442010-09-30 10:34:35 +000015870 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070015871
15872 /* Increment the rx prod index on the rx std ring by at most
15873 * 8 for these chips to workaround hw errata.
15874 */
Joe Perches41535772013-02-16 11:20:04 +000015875 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15876 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15877 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070015878 tp->rx_std_max_post = 8;
15879
Joe Perches63c3a662011-04-26 08:12:10 +000015880 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070015881 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
15882 PCIE_PWR_MGMT_L1_THRESH_MSK;
15883
Linus Torvalds1da177e2005-04-16 15:20:36 -070015884 return err;
15885}
15886
David S. Miller49b6e95f2007-03-29 01:38:42 -070015887#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015888static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015889{
15890 struct net_device *dev = tp->dev;
15891 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015892 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070015893 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015894 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015895
David S. Miller49b6e95f2007-03-29 01:38:42 -070015896 addr = of_get_property(dp, "local-mac-address", &len);
15897 if (addr && len == 6) {
15898 memcpy(dev->dev_addr, addr, 6);
David S. Miller49b6e95f2007-03-29 01:38:42 -070015899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015900 }
15901 return -ENODEV;
15902}
15903
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015904static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015905{
15906 struct net_device *dev = tp->dev;
15907
15908 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
15909 return 0;
15910}
15911#endif
15912
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015913static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015914{
15915 struct net_device *dev = tp->dev;
15916 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080015917 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015918 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015919
David S. Miller49b6e95f2007-03-29 01:38:42 -070015920#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015921 if (!tg3_get_macaddr_sparc(tp))
15922 return 0;
15923#endif
15924
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015925 if (tg3_flag(tp, IS_SSB_CORE)) {
15926 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
15927 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
15928 return 0;
15929 }
15930
Linus Torvalds1da177e2005-04-16 15:20:36 -070015931 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000015932 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015933 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015934 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
15935 mac_offset = 0xcc;
15936 if (tg3_nvram_lock(tp))
15937 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
15938 else
15939 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015940 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000015941 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000015942 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000015943 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000015944 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000015945 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070015946 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015947
15948 /* First try to get it from MAC address mailbox. */
15949 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
15950 if ((hi >> 16) == 0x484b) {
15951 dev->dev_addr[0] = (hi >> 8) & 0xff;
15952 dev->dev_addr[1] = (hi >> 0) & 0xff;
15953
15954 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
15955 dev->dev_addr[2] = (lo >> 24) & 0xff;
15956 dev->dev_addr[3] = (lo >> 16) & 0xff;
15957 dev->dev_addr[4] = (lo >> 8) & 0xff;
15958 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015959
Michael Chan008652b2006-03-27 23:14:53 -080015960 /* Some old bootcode may report a 0 MAC address in SRAM */
15961 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
15962 }
15963 if (!addr_ok) {
15964 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000015965 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000015966 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000015967 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070015968 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
15969 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080015970 }
15971 /* Finally just fetch it out of the MAC control regs. */
15972 else {
15973 hi = tr32(MAC_ADDR_0_HIGH);
15974 lo = tr32(MAC_ADDR_0_LOW);
15975
15976 dev->dev_addr[5] = lo & 0xff;
15977 dev->dev_addr[4] = (lo >> 8) & 0xff;
15978 dev->dev_addr[3] = (lo >> 16) & 0xff;
15979 dev->dev_addr[2] = (lo >> 24) & 0xff;
15980 dev->dev_addr[1] = hi & 0xff;
15981 dev->dev_addr[0] = (hi >> 8) & 0xff;
15982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015983 }
15984
15985 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070015986#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015987 if (!tg3_get_default_macaddr_sparc(tp))
15988 return 0;
15989#endif
15990 return -EINVAL;
15991 }
15992 return 0;
15993}
15994
David S. Miller59e6b432005-05-18 22:50:10 -070015995#define BOUNDARY_SINGLE_CACHELINE 1
15996#define BOUNDARY_MULTI_CACHELINE 2
15997
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015998static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070015999{
16000 int cacheline_size;
16001 u8 byte;
16002 int goal;
16003
16004 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16005 if (byte == 0)
16006 cacheline_size = 1024;
16007 else
16008 cacheline_size = (int) byte * 4;
16009
16010 /* On 5703 and later chips, the boundary bits have no
16011 * effect.
16012 */
Joe Perches41535772013-02-16 11:20:04 +000016013 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16014 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016015 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016016 goto out;
16017
16018#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16019 goal = BOUNDARY_MULTI_CACHELINE;
16020#else
16021#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16022 goal = BOUNDARY_SINGLE_CACHELINE;
16023#else
16024 goal = 0;
16025#endif
16026#endif
16027
Joe Perches63c3a662011-04-26 08:12:10 +000016028 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016029 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16030 goto out;
16031 }
16032
David S. Miller59e6b432005-05-18 22:50:10 -070016033 if (!goal)
16034 goto out;
16035
16036 /* PCI controllers on most RISC systems tend to disconnect
16037 * when a device tries to burst across a cache-line boundary.
16038 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16039 *
16040 * Unfortunately, for PCI-E there are only limited
16041 * write-side controls for this, and thus for reads
16042 * we will still get the disconnects. We'll also waste
16043 * these PCI cycles for both read and write for chips
16044 * other than 5700 and 5701 which do not implement the
16045 * boundary bits.
16046 */
Joe Perches63c3a662011-04-26 08:12:10 +000016047 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016048 switch (cacheline_size) {
16049 case 16:
16050 case 32:
16051 case 64:
16052 case 128:
16053 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16054 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16055 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16056 } else {
16057 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16058 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16059 }
16060 break;
16061
16062 case 256:
16063 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16064 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16065 break;
16066
16067 default:
16068 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16069 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16070 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016071 }
Joe Perches63c3a662011-04-26 08:12:10 +000016072 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016073 switch (cacheline_size) {
16074 case 16:
16075 case 32:
16076 case 64:
16077 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16078 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16079 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
16080 break;
16081 }
16082 /* fallthrough */
16083 case 128:
16084 default:
16085 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16086 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
16087 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016088 }
David S. Miller59e6b432005-05-18 22:50:10 -070016089 } else {
16090 switch (cacheline_size) {
16091 case 16:
16092 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16093 val |= (DMA_RWCTRL_READ_BNDRY_16 |
16094 DMA_RWCTRL_WRITE_BNDRY_16);
16095 break;
16096 }
16097 /* fallthrough */
16098 case 32:
16099 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16100 val |= (DMA_RWCTRL_READ_BNDRY_32 |
16101 DMA_RWCTRL_WRITE_BNDRY_32);
16102 break;
16103 }
16104 /* fallthrough */
16105 case 64:
16106 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16107 val |= (DMA_RWCTRL_READ_BNDRY_64 |
16108 DMA_RWCTRL_WRITE_BNDRY_64);
16109 break;
16110 }
16111 /* fallthrough */
16112 case 128:
16113 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16114 val |= (DMA_RWCTRL_READ_BNDRY_128 |
16115 DMA_RWCTRL_WRITE_BNDRY_128);
16116 break;
16117 }
16118 /* fallthrough */
16119 case 256:
16120 val |= (DMA_RWCTRL_READ_BNDRY_256 |
16121 DMA_RWCTRL_WRITE_BNDRY_256);
16122 break;
16123 case 512:
16124 val |= (DMA_RWCTRL_READ_BNDRY_512 |
16125 DMA_RWCTRL_WRITE_BNDRY_512);
16126 break;
16127 case 1024:
16128 default:
16129 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16130 DMA_RWCTRL_WRITE_BNDRY_1024);
16131 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016132 }
David S. Miller59e6b432005-05-18 22:50:10 -070016133 }
16134
16135out:
16136 return val;
16137}
16138
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016139static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
16140 int size, int to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016141{
16142 struct tg3_internal_buffer_desc test_desc;
16143 u32 sram_dma_descs;
16144 int i, ret;
16145
16146 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16147
16148 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16149 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16150 tw32(RDMAC_STATUS, 0);
16151 tw32(WDMAC_STATUS, 0);
16152
16153 tw32(BUFMGR_MODE, 0);
16154 tw32(FTQ_RESET, 0);
16155
16156 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16157 test_desc.addr_lo = buf_dma & 0xffffffff;
16158 test_desc.nic_mbuf = 0x00002100;
16159 test_desc.len = size;
16160
16161 /*
16162 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16163 * the *second* time the tg3 driver was getting loaded after an
16164 * initial scan.
16165 *
16166 * Broadcom tells me:
16167 * ...the DMA engine is connected to the GRC block and a DMA
16168 * reset may affect the GRC block in some unpredictable way...
16169 * The behavior of resets to individual blocks has not been tested.
16170 *
16171 * Broadcom noted the GRC reset will also reset all sub-components.
16172 */
16173 if (to_device) {
16174 test_desc.cqid_sqid = (13 << 8) | 2;
16175
16176 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16177 udelay(40);
16178 } else {
16179 test_desc.cqid_sqid = (16 << 8) | 7;
16180
16181 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16182 udelay(40);
16183 }
16184 test_desc.flags = 0x00000005;
16185
16186 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16187 u32 val;
16188
16189 val = *(((u32 *)&test_desc) + i);
16190 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16191 sram_dma_descs + (i * sizeof(u32)));
16192 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16193 }
16194 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16195
Matt Carlson859a588792010-04-05 10:19:28 +000016196 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016197 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016198 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016199 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016200
16201 ret = -ENODEV;
16202 for (i = 0; i < 40; i++) {
16203 u32 val;
16204
16205 if (to_device)
16206 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16207 else
16208 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16209 if ((val & 0xffff) == sram_dma_descs) {
16210 ret = 0;
16211 break;
16212 }
16213
16214 udelay(100);
16215 }
16216
16217 return ret;
16218}
16219
David S. Millerded73402005-05-23 13:59:47 -070016220#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016221
Matt Carlson41434702011-03-09 16:58:22 +000016222static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016223 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16224 { },
16225};
16226
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016227static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016228{
16229 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016230 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016231 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016232
Matt Carlson4bae65c2010-11-24 08:31:52 +000016233 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16234 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016235 if (!buf) {
16236 ret = -ENOMEM;
16237 goto out_nofree;
16238 }
16239
16240 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
16241 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16242
David S. Miller59e6b432005-05-18 22:50:10 -070016243 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016244
Joe Perches63c3a662011-04-26 08:12:10 +000016245 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016246 goto out;
16247
Joe Perches63c3a662011-04-26 08:12:10 +000016248 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016249 /* DMA read watermark not used on PCIE */
16250 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000016251 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000016252 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
16253 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016254 tp->dma_rwctrl |= 0x003f0000;
16255 else
16256 tp->dma_rwctrl |= 0x003f000f;
16257 } else {
Joe Perches41535772013-02-16 11:20:04 +000016258 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16259 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016260 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016261 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016262
Michael Chan4a29cc22006-03-19 13:21:12 -080016263 /* If the 5704 is behind the EPB bridge, we can
16264 * do the less restrictive ONE_DMA workaround for
16265 * better performance.
16266 */
Joe Perches63c3a662011-04-26 08:12:10 +000016267 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000016268 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080016269 tp->dma_rwctrl |= 0x8000;
16270 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016271 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16272
Joe Perches41535772013-02-16 11:20:04 +000016273 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080016274 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016275 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016276 tp->dma_rwctrl |=
16277 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16278 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16279 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000016280 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070016281 /* 5780 always in PCIX mode */
16282 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000016283 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070016284 /* 5714 always in PCIX mode */
16285 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016286 } else {
16287 tp->dma_rwctrl |= 0x001b000f;
16288 }
16289 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016290 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
16291 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016292
Joe Perches41535772013-02-16 11:20:04 +000016293 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16294 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016295 tp->dma_rwctrl &= 0xfffffff0;
16296
Joe Perches41535772013-02-16 11:20:04 +000016297 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16298 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016299 /* Remove this if it causes problems for some boards. */
16300 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16301
16302 /* On 5700/5701 chips, we need to set this bit.
16303 * Otherwise the chip will issue cacheline transactions
16304 * to streamable DMA memory with not all the byte
16305 * enables turned on. This is an error on several
16306 * RISC PCI controllers, in particular sparc64.
16307 *
16308 * On 5703/5704 chips, this bit has been reassigned
16309 * a different meaning. In particular, it is used
16310 * on those chips to enable a PCI-X workaround.
16311 */
16312 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16313 }
16314
16315 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16316
16317#if 0
16318 /* Unneeded, already done by tg3_get_invariants. */
16319 tg3_switch_clocks(tp);
16320#endif
16321
Joe Perches41535772013-02-16 11:20:04 +000016322 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16323 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016324 goto out;
16325
David S. Miller59e6b432005-05-18 22:50:10 -070016326 /* It is best to perform DMA test with maximum write burst size
16327 * to expose the 5700/5701 write DMA bug.
16328 */
16329 saved_dma_rwctrl = tp->dma_rwctrl;
16330 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16331 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16332
Linus Torvalds1da177e2005-04-16 15:20:36 -070016333 while (1) {
16334 u32 *p = buf, i;
16335
16336 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
16337 p[i] = i;
16338
16339 /* Send the buffer to the chip. */
16340 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
16341 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000016342 dev_err(&tp->pdev->dev,
16343 "%s: Buffer write failed. err = %d\n",
16344 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016345 break;
16346 }
16347
16348#if 0
16349 /* validate data reached card RAM correctly. */
16350 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16351 u32 val;
16352 tg3_read_mem(tp, 0x2100 + (i*4), &val);
16353 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000016354 dev_err(&tp->pdev->dev,
16355 "%s: Buffer corrupted on device! "
16356 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016357 /* ret = -ENODEV here? */
16358 }
16359 p[i] = 0;
16360 }
16361#endif
16362 /* Now read it back. */
16363 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
16364 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000016365 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
16366 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016367 break;
16368 }
16369
16370 /* Verify it. */
16371 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16372 if (p[i] == i)
16373 continue;
16374
David S. Miller59e6b432005-05-18 22:50:10 -070016375 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16376 DMA_RWCTRL_WRITE_BNDRY_16) {
16377 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016378 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
16379 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16380 break;
16381 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000016382 dev_err(&tp->pdev->dev,
16383 "%s: Buffer corrupted on read back! "
16384 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016385 ret = -ENODEV;
16386 goto out;
16387 }
16388 }
16389
16390 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
16391 /* Success. */
16392 ret = 0;
16393 break;
16394 }
16395 }
David S. Miller59e6b432005-05-18 22:50:10 -070016396 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16397 DMA_RWCTRL_WRITE_BNDRY_16) {
16398 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070016399 * now look for chipsets that are known to expose the
16400 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070016401 */
Matt Carlson41434702011-03-09 16:58:22 +000016402 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016403 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16404 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000016405 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016406 /* Safe to use the calculated DMA boundary. */
16407 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000016408 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070016409
David S. Miller59e6b432005-05-18 22:50:10 -070016410 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016412
16413out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000016414 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016415out_nofree:
16416 return ret;
16417}
16418
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016419static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016420{
Joe Perches63c3a662011-04-26 08:12:10 +000016421 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000016422 tp->bufmgr_config.mbuf_read_dma_low_water =
16423 DEFAULT_MB_RDMA_LOW_WATER_5705;
16424 tp->bufmgr_config.mbuf_mac_rx_low_water =
16425 DEFAULT_MB_MACRX_LOW_WATER_57765;
16426 tp->bufmgr_config.mbuf_high_water =
16427 DEFAULT_MB_HIGH_WATER_57765;
16428
16429 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16430 DEFAULT_MB_RDMA_LOW_WATER_5705;
16431 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16432 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
16433 tp->bufmgr_config.mbuf_high_water_jumbo =
16434 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000016435 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070016436 tp->bufmgr_config.mbuf_read_dma_low_water =
16437 DEFAULT_MB_RDMA_LOW_WATER_5705;
16438 tp->bufmgr_config.mbuf_mac_rx_low_water =
16439 DEFAULT_MB_MACRX_LOW_WATER_5705;
16440 tp->bufmgr_config.mbuf_high_water =
16441 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000016442 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016443 tp->bufmgr_config.mbuf_mac_rx_low_water =
16444 DEFAULT_MB_MACRX_LOW_WATER_5906;
16445 tp->bufmgr_config.mbuf_high_water =
16446 DEFAULT_MB_HIGH_WATER_5906;
16447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016448
Michael Chanfdfec1722005-07-25 12:31:48 -070016449 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16450 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
16451 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16452 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
16453 tp->bufmgr_config.mbuf_high_water_jumbo =
16454 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
16455 } else {
16456 tp->bufmgr_config.mbuf_read_dma_low_water =
16457 DEFAULT_MB_RDMA_LOW_WATER;
16458 tp->bufmgr_config.mbuf_mac_rx_low_water =
16459 DEFAULT_MB_MACRX_LOW_WATER;
16460 tp->bufmgr_config.mbuf_high_water =
16461 DEFAULT_MB_HIGH_WATER;
16462
16463 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16464 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
16465 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16466 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
16467 tp->bufmgr_config.mbuf_high_water_jumbo =
16468 DEFAULT_MB_HIGH_WATER_JUMBO;
16469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016470
16471 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
16472 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
16473}
16474
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016475static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016476{
Matt Carlson79eb6902010-02-17 15:17:03 +000016477 switch (tp->phy_id & TG3_PHY_ID_MASK) {
16478 case TG3_PHY_ID_BCM5400: return "5400";
16479 case TG3_PHY_ID_BCM5401: return "5401";
16480 case TG3_PHY_ID_BCM5411: return "5411";
16481 case TG3_PHY_ID_BCM5701: return "5701";
16482 case TG3_PHY_ID_BCM5703: return "5703";
16483 case TG3_PHY_ID_BCM5704: return "5704";
16484 case TG3_PHY_ID_BCM5705: return "5705";
16485 case TG3_PHY_ID_BCM5750: return "5750";
16486 case TG3_PHY_ID_BCM5752: return "5752";
16487 case TG3_PHY_ID_BCM5714: return "5714";
16488 case TG3_PHY_ID_BCM5780: return "5780";
16489 case TG3_PHY_ID_BCM5755: return "5755";
16490 case TG3_PHY_ID_BCM5787: return "5787";
16491 case TG3_PHY_ID_BCM5784: return "5784";
16492 case TG3_PHY_ID_BCM5756: return "5722/5756";
16493 case TG3_PHY_ID_BCM5906: return "5906";
16494 case TG3_PHY_ID_BCM5761: return "5761";
16495 case TG3_PHY_ID_BCM5718C: return "5718C";
16496 case TG3_PHY_ID_BCM5718S: return "5718S";
16497 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000016498 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000016499 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000016500 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000016501 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070016502 case 0: return "serdes";
16503 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070016504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016505}
16506
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016507static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070016508{
Joe Perches63c3a662011-04-26 08:12:10 +000016509 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016510 strcpy(str, "PCI Express");
16511 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000016512 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016513 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
16514
16515 strcpy(str, "PCIX:");
16516
16517 if ((clock_ctrl == 7) ||
16518 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
16519 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
16520 strcat(str, "133MHz");
16521 else if (clock_ctrl == 0)
16522 strcat(str, "33MHz");
16523 else if (clock_ctrl == 2)
16524 strcat(str, "50MHz");
16525 else if (clock_ctrl == 4)
16526 strcat(str, "66MHz");
16527 else if (clock_ctrl == 6)
16528 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070016529 } else {
16530 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000016531 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070016532 strcat(str, "66MHz");
16533 else
16534 strcat(str, "33MHz");
16535 }
Joe Perches63c3a662011-04-26 08:12:10 +000016536 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070016537 strcat(str, ":32-bit");
16538 else
16539 strcat(str, ":64-bit");
16540 return str;
16541}
16542
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016543static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070016544{
16545 struct ethtool_coalesce *ec = &tp->coal;
16546
16547 memset(ec, 0, sizeof(*ec));
16548 ec->cmd = ETHTOOL_GCOALESCE;
16549 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
16550 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
16551 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
16552 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
16553 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
16554 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
16555 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
16556 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
16557 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
16558
16559 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
16560 HOSTCC_MODE_CLRTICK_TXBD)) {
16561 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
16562 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
16563 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
16564 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
16565 }
Michael Chand244c892005-07-05 14:42:33 -070016566
Joe Perches63c3a662011-04-26 08:12:10 +000016567 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070016568 ec->rx_coalesce_usecs_irq = 0;
16569 ec->tx_coalesce_usecs_irq = 0;
16570 ec->stats_block_coalesce_usecs = 0;
16571 }
David S. Miller15f98502005-05-18 22:49:26 -070016572}
16573
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016574static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016575 const struct pci_device_id *ent)
16576{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016577 struct net_device *dev;
16578 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000016579 int i, err, pm_cap;
16580 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070016581 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080016582 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000016583 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016584
Joe Perches05dbe002010-02-17 19:44:19 +000016585 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016586
16587 err = pci_enable_device(pdev);
16588 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016589 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016590 return err;
16591 }
16592
Linus Torvalds1da177e2005-04-16 15:20:36 -070016593 err = pci_request_regions(pdev, DRV_MODULE_NAME);
16594 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016595 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016596 goto err_out_disable_pdev;
16597 }
16598
16599 pci_set_master(pdev);
16600
16601 /* Find power-management capability. */
16602 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
16603 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000016604 dev_err(&pdev->dev,
16605 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016606 err = -EIO;
16607 goto err_out_free_res;
16608 }
16609
Matt Carlson16821282011-07-13 09:27:28 +000016610 err = pci_set_power_state(pdev, PCI_D0);
16611 if (err) {
16612 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
16613 goto err_out_free_res;
16614 }
16615
Matt Carlsonfe5f5782009-09-01 13:09:39 +000016616 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016617 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016618 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000016619 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016620 }
16621
Linus Torvalds1da177e2005-04-16 15:20:36 -070016622 SET_NETDEV_DEV(dev, &pdev->dev);
16623
Linus Torvalds1da177e2005-04-16 15:20:36 -070016624 tp = netdev_priv(dev);
16625 tp->pdev = pdev;
16626 tp->dev = dev;
16627 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016628 tp->rx_mode = TG3_DEF_RX_MODE;
16629 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000016630 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070016631
Linus Torvalds1da177e2005-04-16 15:20:36 -070016632 if (tg3_debug > 0)
16633 tp->msg_enable = tg3_debug;
16634 else
16635 tp->msg_enable = TG3_DEF_MSG_ENABLE;
16636
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016637 if (pdev_is_ssb_gige_core(pdev)) {
16638 tg3_flag_set(tp, IS_SSB_CORE);
16639 if (ssb_gige_must_flush_posted_writes(pdev))
16640 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
16641 if (ssb_gige_one_dma_at_once(pdev))
16642 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
16643 if (ssb_gige_have_roboswitch(pdev))
16644 tg3_flag_set(tp, ROBOSWITCH);
16645 if (ssb_gige_is_rgmii(pdev))
16646 tg3_flag_set(tp, RGMII_MODE);
16647 }
16648
Linus Torvalds1da177e2005-04-16 15:20:36 -070016649 /* The word/byte swap controls here control register access byte
16650 * swapping. DMA data byte swapping is controlled in the GRC_MODE
16651 * setting below.
16652 */
16653 tp->misc_host_ctrl =
16654 MISC_HOST_CTRL_MASK_PCI_INT |
16655 MISC_HOST_CTRL_WORD_SWAP |
16656 MISC_HOST_CTRL_INDIR_ACCESS |
16657 MISC_HOST_CTRL_PCISTATE_RW;
16658
16659 /* The NONFRM (non-frame) byte/word swap controls take effect
16660 * on descriptor entries, anything which isn't packet data.
16661 *
16662 * The StrongARM chips on the board (one for tx, one for rx)
16663 * are running in big-endian mode.
16664 */
16665 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
16666 GRC_MODE_WSWAP_NONFRM_DATA);
16667#ifdef __BIG_ENDIAN
16668 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
16669#endif
16670 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016671 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000016672 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016673
Matt Carlsond5fe4882008-11-21 17:20:32 -080016674 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010016675 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016676 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016677 err = -ENOMEM;
16678 goto err_out_free_dev;
16679 }
16680
Matt Carlsonc9cab242011-07-13 09:27:27 +000016681 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16682 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
16683 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
16684 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
16685 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016686 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000016687 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16688 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016689 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
16690 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
16691 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
16692 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000016693 tg3_flag_set(tp, ENABLE_APE);
16694 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
16695 if (!tp->aperegs) {
16696 dev_err(&pdev->dev,
16697 "Cannot map APE registers, aborting\n");
16698 err = -ENOMEM;
16699 goto err_out_iounmap;
16700 }
16701 }
16702
Linus Torvalds1da177e2005-04-16 15:20:36 -070016703 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
16704 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016705
Linus Torvalds1da177e2005-04-16 15:20:36 -070016706 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016707 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000016708 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016709 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016710
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016711 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016712 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016713 dev_err(&pdev->dev,
16714 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016715 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016716 }
16717
Michael Chan4a29cc22006-03-19 13:21:12 -080016718 /* The EPB bridge inside 5714, 5715, and 5780 and any
16719 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080016720 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
16721 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
16722 * do DMA address check in tg3_start_xmit().
16723 */
Joe Perches63c3a662011-04-26 08:12:10 +000016724 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070016725 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000016726 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070016727 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080016728#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070016729 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016730#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080016731 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070016732 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016733
16734 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070016735 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080016736 err = pci_set_dma_mask(pdev, dma_mask);
16737 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000016738 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080016739 err = pci_set_consistent_dma_mask(pdev,
16740 persist_dma_mask);
16741 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016742 dev_err(&pdev->dev, "Unable to obtain 64 bit "
16743 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016744 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016745 }
16746 }
16747 }
Yang Hongyang284901a2009-04-06 19:01:15 -070016748 if (err || dma_mask == DMA_BIT_MASK(32)) {
16749 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080016750 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016751 dev_err(&pdev->dev,
16752 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016753 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016754 }
16755 }
16756
Michael Chanfdfec1722005-07-25 12:31:48 -070016757 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016758
Matt Carlson0da06062011-05-19 12:12:53 +000016759 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
16760
16761 /* 5700 B0 chips do not support checksumming correctly due
16762 * to hardware bugs.
16763 */
Joe Perches41535772013-02-16 11:20:04 +000016764 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000016765 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
16766
16767 if (tg3_flag(tp, 5755_PLUS))
16768 features |= NETIF_F_IPV6_CSUM;
16769 }
16770
Michael Chan4e3a7aa2006-03-20 17:47:44 -080016771 /* TSO is on by default on chips that support hardware TSO.
16772 * Firmware TSO on older chips gives lower performance, so it
16773 * is off by default, but can be enabled using ethtool.
16774 */
Joe Perches63c3a662011-04-26 08:12:10 +000016775 if ((tg3_flag(tp, HW_TSO_1) ||
16776 tg3_flag(tp, HW_TSO_2) ||
16777 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000016778 (features & NETIF_F_IP_CSUM))
16779 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000016780 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000016781 if (features & NETIF_F_IPV6_CSUM)
16782 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000016783 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000016784 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16785 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16786 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
16787 tg3_asic_rev(tp) == ASIC_REV_5785 ||
16788 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000016789 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070016790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016791
Matt Carlsond542fe22011-05-19 16:02:43 +000016792 dev->features |= features;
16793 dev->vlan_features |= features;
16794
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016795 /*
16796 * Add loopback capability only for a subset of devices that support
16797 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
16798 * loopback for the remaining devices.
16799 */
Joe Perches41535772013-02-16 11:20:04 +000016800 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016801 !tg3_flag(tp, CPMU_PRESENT))
16802 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000016803 features |= NETIF_F_LOOPBACK;
16804
Matt Carlson0da06062011-05-19 12:12:53 +000016805 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016806
Joe Perches41535772013-02-16 11:20:04 +000016807 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016808 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016809 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016810 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016811 tp->rx_pending = 63;
16812 }
16813
Linus Torvalds1da177e2005-04-16 15:20:36 -070016814 err = tg3_get_device_address(tp);
16815 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016816 dev_err(&pdev->dev,
16817 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016818 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016819 }
16820
Matt Carlsonc88864d2007-11-12 21:07:01 -080016821 /*
16822 * Reset chip in case UNDI or EFI driver did not shutdown
16823 * DMA self test will enable WDMAC and we'll see (spurious)
16824 * pending DMA on the PCI bus at that point.
16825 */
16826 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
16827 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
16828 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
16829 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
16830 }
16831
16832 err = tg3_test_dma(tp);
16833 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016834 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080016835 goto err_out_apeunmap;
16836 }
16837
Matt Carlson78f90dc2009-11-13 13:03:42 +000016838 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
16839 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
16840 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000016841 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000016842 struct tg3_napi *tnapi = &tp->napi[i];
16843
16844 tnapi->tp = tp;
16845 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
16846
16847 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000016848 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016849 intmbx += 0x8;
16850 else
16851 intmbx += 0x4;
16852
16853 tnapi->consmbox = rcvmbx;
16854 tnapi->prodmbox = sndmbx;
16855
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016856 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016857 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016858 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000016859 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000016860
Joe Perches63c3a662011-04-26 08:12:10 +000016861 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000016862 break;
16863
16864 /*
16865 * If we support MSIX, we'll be using RSS. If we're using
16866 * RSS, the first vector only handles link interrupts and the
16867 * remaining vectors handle rx and tx interrupts. Reuse the
16868 * mailbox values for the next iteration. The values we setup
16869 * above are still useful for the single vectored mode.
16870 */
16871 if (!i)
16872 continue;
16873
16874 rcvmbx += 0x8;
16875
16876 if (sndmbx & 0x4)
16877 sndmbx -= 0x4;
16878 else
16879 sndmbx += 0xc;
16880 }
16881
Matt Carlsonc88864d2007-11-12 21:07:01 -080016882 tg3_init_coal(tp);
16883
Michael Chanc49a1562006-12-17 17:07:29 -080016884 pci_set_drvdata(pdev, dev);
16885
Joe Perches41535772013-02-16 11:20:04 +000016886 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16887 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16888 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000016889 tg3_flag_set(tp, PTP_CAPABLE);
16890
Matt Carlsoncd0d7222011-07-13 09:27:33 +000016891 if (tg3_flag(tp, 5717_PLUS)) {
16892 /* Resume a low-power mode */
16893 tg3_frob_aux_power(tp, false);
16894 }
16895
Matt Carlson21f76382012-02-22 12:35:21 +000016896 tg3_timer_init(tp);
16897
Michael Chan402e1392013-02-14 12:13:41 +000016898 tg3_carrier_off(tp);
16899
Linus Torvalds1da177e2005-04-16 15:20:36 -070016900 err = register_netdev(dev);
16901 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016902 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070016903 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016904 }
16905
Joe Perches05dbe002010-02-17 19:44:19 +000016906 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
16907 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000016908 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000016909 tg3_bus_string(tp, str),
16910 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016911
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016912 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000016913 struct phy_device *phydev;
16914 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000016915 netdev_info(dev,
16916 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000016917 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016918 } else {
16919 char *ethtype;
16920
16921 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
16922 ethtype = "10/100Base-TX";
16923 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
16924 ethtype = "1000Base-SX";
16925 else
16926 ethtype = "10/100/1000Base-T";
16927
Matt Carlson5129c3a2010-04-05 10:19:23 +000016928 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000016929 "(WireSpeed[%d], EEE[%d])\n",
16930 tg3_phy_string(tp), ethtype,
16931 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
16932 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016933 }
Matt Carlsondf59c942008-11-03 16:52:56 -080016934
Joe Perches05dbe002010-02-17 19:44:19 +000016935 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000016936 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016937 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016938 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016939 tg3_flag(tp, ENABLE_ASF) != 0,
16940 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000016941 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
16942 tp->dma_rwctrl,
16943 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
16944 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016945
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016946 pci_save_state(pdev);
16947
Linus Torvalds1da177e2005-04-16 15:20:36 -070016948 return 0;
16949
Matt Carlson0d3031d2007-10-10 18:02:43 -070016950err_out_apeunmap:
16951 if (tp->aperegs) {
16952 iounmap(tp->aperegs);
16953 tp->aperegs = NULL;
16954 }
16955
Linus Torvalds1da177e2005-04-16 15:20:36 -070016956err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070016957 if (tp->regs) {
16958 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016959 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016961
16962err_out_free_dev:
16963 free_netdev(dev);
16964
Matt Carlson16821282011-07-13 09:27:28 +000016965err_out_power_down:
16966 pci_set_power_state(pdev, PCI_D3hot);
16967
Linus Torvalds1da177e2005-04-16 15:20:36 -070016968err_out_free_res:
16969 pci_release_regions(pdev);
16970
16971err_out_disable_pdev:
16972 pci_disable_device(pdev);
16973 pci_set_drvdata(pdev, NULL);
16974 return err;
16975}
16976
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016977static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016978{
16979 struct net_device *dev = pci_get_drvdata(pdev);
16980
16981 if (dev) {
16982 struct tg3 *tp = netdev_priv(dev);
16983
Jesper Juhle3c55302012-04-09 22:50:15 +020016984 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080016985
Matt Carlsondb219972011-11-04 09:15:03 +000016986 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016987
David S. Miller1805b2f2011-10-24 18:18:09 -040016988 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016989 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016990 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016991 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070016992
Linus Torvalds1da177e2005-04-16 15:20:36 -070016993 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016994 if (tp->aperegs) {
16995 iounmap(tp->aperegs);
16996 tp->aperegs = NULL;
16997 }
Michael Chan68929142005-08-09 20:17:14 -070016998 if (tp->regs) {
16999 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017000 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017002 free_netdev(dev);
17003 pci_release_regions(pdev);
17004 pci_disable_device(pdev);
17005 pci_set_drvdata(pdev, NULL);
17006 }
17007}
17008
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017009#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017010static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017011{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017012 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017013 struct net_device *dev = pci_get_drvdata(pdev);
17014 struct tg3 *tp = netdev_priv(dev);
17015 int err;
17016
17017 if (!netif_running(dev))
17018 return 0;
17019
Matt Carlsondb219972011-11-04 09:15:03 +000017020 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017021 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017022 tg3_netif_stop(tp);
17023
Matt Carlson21f76382012-02-22 12:35:21 +000017024 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017025
David S. Millerf47c11e2005-06-24 20:18:35 -070017026 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017027 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017028 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017029
17030 netif_device_detach(dev);
17031
David S. Millerf47c11e2005-06-24 20:18:35 -070017032 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017033 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017034 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017035 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017036
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017037 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017038 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017039 int err2;
17040
David S. Millerf47c11e2005-06-24 20:18:35 -070017041 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017042
Joe Perches63c3a662011-04-26 08:12:10 +000017043 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017044 err2 = tg3_restart_hw(tp, 1);
17045 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017046 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017047
Matt Carlson21f76382012-02-22 12:35:21 +000017048 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017049
17050 netif_device_attach(dev);
17051 tg3_netif_start(tp);
17052
Michael Chanb9ec6c12006-07-25 16:37:27 -070017053out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017054 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017055
17056 if (!err2)
17057 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017058 }
17059
17060 return err;
17061}
17062
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017063static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017064{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017065 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017066 struct net_device *dev = pci_get_drvdata(pdev);
17067 struct tg3 *tp = netdev_priv(dev);
17068 int err;
17069
17070 if (!netif_running(dev))
17071 return 0;
17072
Linus Torvalds1da177e2005-04-16 15:20:36 -070017073 netif_device_attach(dev);
17074
David S. Millerf47c11e2005-06-24 20:18:35 -070017075 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017076
Joe Perches63c3a662011-04-26 08:12:10 +000017077 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070017078 err = tg3_restart_hw(tp, 1);
17079 if (err)
17080 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017081
Matt Carlson21f76382012-02-22 12:35:21 +000017082 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017083
Linus Torvalds1da177e2005-04-16 15:20:36 -070017084 tg3_netif_start(tp);
17085
Michael Chanb9ec6c12006-07-25 16:37:27 -070017086out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017087 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017088
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017089 if (!err)
17090 tg3_phy_start(tp);
17091
Michael Chanb9ec6c12006-07-25 16:37:27 -070017092 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017093}
17094
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017095static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017096#define TG3_PM_OPS (&tg3_pm_ops)
17097
17098#else
17099
17100#define TG3_PM_OPS NULL
17101
17102#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017103
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017104/**
17105 * tg3_io_error_detected - called when PCI error is detected
17106 * @pdev: Pointer to PCI device
17107 * @state: The current pci connection state
17108 *
17109 * This function is called after a PCI bus error affecting
17110 * this device has been detected.
17111 */
17112static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
17113 pci_channel_state_t state)
17114{
17115 struct net_device *netdev = pci_get_drvdata(pdev);
17116 struct tg3 *tp = netdev_priv(netdev);
17117 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
17118
17119 netdev_info(netdev, "PCI I/O error detected\n");
17120
17121 rtnl_lock();
17122
17123 if (!netif_running(netdev))
17124 goto done;
17125
17126 tg3_phy_stop(tp);
17127
17128 tg3_netif_stop(tp);
17129
Matt Carlson21f76382012-02-22 12:35:21 +000017130 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017131
17132 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000017133 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017134
17135 netif_device_detach(netdev);
17136
17137 /* Clean up software state, even if MMIO is blocked */
17138 tg3_full_lock(tp, 0);
17139 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
17140 tg3_full_unlock(tp);
17141
17142done:
17143 if (state == pci_channel_io_perm_failure)
17144 err = PCI_ERS_RESULT_DISCONNECT;
17145 else
17146 pci_disable_device(pdev);
17147
17148 rtnl_unlock();
17149
17150 return err;
17151}
17152
17153/**
17154 * tg3_io_slot_reset - called after the pci bus has been reset.
17155 * @pdev: Pointer to PCI device
17156 *
17157 * Restart the card from scratch, as if from a cold-boot.
17158 * At this point, the card has exprienced a hard reset,
17159 * followed by fixups by BIOS, and has its config space
17160 * set up identically to what it was at cold boot.
17161 */
17162static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17163{
17164 struct net_device *netdev = pci_get_drvdata(pdev);
17165 struct tg3 *tp = netdev_priv(netdev);
17166 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17167 int err;
17168
17169 rtnl_lock();
17170
17171 if (pci_enable_device(pdev)) {
17172 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
17173 goto done;
17174 }
17175
17176 pci_set_master(pdev);
17177 pci_restore_state(pdev);
17178 pci_save_state(pdev);
17179
17180 if (!netif_running(netdev)) {
17181 rc = PCI_ERS_RESULT_RECOVERED;
17182 goto done;
17183 }
17184
17185 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017186 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017187 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017188
17189 rc = PCI_ERS_RESULT_RECOVERED;
17190
17191done:
17192 rtnl_unlock();
17193
17194 return rc;
17195}
17196
17197/**
17198 * tg3_io_resume - called when traffic can start flowing again.
17199 * @pdev: Pointer to PCI device
17200 *
17201 * This callback is called when the error recovery driver tells
17202 * us that its OK to resume normal operation.
17203 */
17204static void tg3_io_resume(struct pci_dev *pdev)
17205{
17206 struct net_device *netdev = pci_get_drvdata(pdev);
17207 struct tg3 *tp = netdev_priv(netdev);
17208 int err;
17209
17210 rtnl_lock();
17211
17212 if (!netif_running(netdev))
17213 goto done;
17214
17215 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000017216 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017217 err = tg3_restart_hw(tp, 1);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017218 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017219 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017220 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17221 goto done;
17222 }
17223
17224 netif_device_attach(netdev);
17225
Matt Carlson21f76382012-02-22 12:35:21 +000017226 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017227
17228 tg3_netif_start(tp);
17229
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017230 tg3_full_unlock(tp);
17231
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017232 tg3_phy_start(tp);
17233
17234done:
17235 rtnl_unlock();
17236}
17237
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017238static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017239 .error_detected = tg3_io_error_detected,
17240 .slot_reset = tg3_io_slot_reset,
17241 .resume = tg3_io_resume
17242};
17243
Linus Torvalds1da177e2005-04-16 15:20:36 -070017244static struct pci_driver tg3_driver = {
17245 .name = DRV_MODULE_NAME,
17246 .id_table = tg3_pci_tbl,
17247 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017248 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017249 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017250 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017251};
17252
17253static int __init tg3_init(void)
17254{
Jeff Garzik29917622006-08-19 17:48:59 -040017255 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017256}
17257
17258static void __exit tg3_cleanup(void)
17259{
17260 pci_unregister_driver(&tg3_driver);
17261}
17262
17263module_init(tg3_init);
17264module_exit(tg3_cleanup);