blob: 54f604bbaeac1afc5d935d9cfc2dfb254ca17125 [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. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003455static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003456{
3457 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003458 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003459
Nithin Sujir837c45b2013-03-06 17:02:30 +00003460 for (i = 0; i < iters; i++) {
3461 tw32(cpu_base + CPU_STATE, 0xffffffff);
3462 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3463 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3464 break;
3465 }
3466
3467 return (i == iters) ? -EBUSY : 0;
3468}
3469
3470/* tp->lock is held. */
3471static int tg3_rxcpu_pause(struct tg3 *tp)
3472{
3473 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3474
3475 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3476 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3477 udelay(10);
3478
3479 return rc;
3480}
3481
3482/* tp->lock is held. */
3483static int tg3_txcpu_pause(struct tg3 *tp)
3484{
3485 return tg3_pause_cpu(tp, TX_CPU_BASE);
3486}
3487
3488/* tp->lock is held. */
3489static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3490{
3491 tw32(cpu_base + CPU_STATE, 0xffffffff);
3492 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3493}
3494
3495/* tp->lock is held. */
3496static void tg3_rxcpu_resume(struct tg3 *tp)
3497{
3498 tg3_resume_cpu(tp, RX_CPU_BASE);
3499}
3500
3501/* tp->lock is held. */
3502static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3503{
3504 int rc;
3505
3506 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003507
Joe Perches41535772013-02-16 11:20:04 +00003508 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003509 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3510
3511 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3512 return 0;
3513 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003514 if (cpu_base == RX_CPU_BASE) {
3515 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003516 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003517 /*
3518 * There is only an Rx CPU for the 5750 derivative in the
3519 * BCM4785.
3520 */
3521 if (tg3_flag(tp, IS_SSB_CORE))
3522 return 0;
3523
Nithin Sujir837c45b2013-03-06 17:02:30 +00003524 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003525 }
3526
Nithin Sujir837c45b2013-03-06 17:02:30 +00003527 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003528 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003529 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003530 return -ENODEV;
3531 }
3532
3533 /* Clear firmware's nvram arbitration. */
3534 if (tg3_flag(tp, NVRAM))
3535 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3536 return 0;
3537}
3538
Nithin Sujir31f11a92013-03-06 17:02:33 +00003539static int tg3_fw_data_len(struct tg3 *tp,
3540 const struct tg3_firmware_hdr *fw_hdr)
3541{
3542 int fw_len;
3543
3544 /* Non fragmented firmware have one firmware header followed by a
3545 * contiguous chunk of data to be written. The length field in that
3546 * header is not the length of data to be written but the complete
3547 * length of the bss. The data length is determined based on
3548 * tp->fw->size minus headers.
3549 *
3550 * Fragmented firmware have a main header followed by multiple
3551 * fragments. Each fragment is identical to non fragmented firmware
3552 * with a firmware header followed by a contiguous chunk of data. In
3553 * the main header, the length field is unused and set to 0xffffffff.
3554 * In each fragment header the length is the entire size of that
3555 * fragment i.e. fragment data + header length. Data length is
3556 * therefore length field in the header minus TG3_FW_HDR_LEN.
3557 */
3558 if (tp->fw_len == 0xffffffff)
3559 fw_len = be32_to_cpu(fw_hdr->len);
3560 else
3561 fw_len = tp->fw->size;
3562
3563 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3564}
3565
Matt Carlson997b4f12011-08-31 11:44:53 +00003566/* tp->lock is held. */
3567static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3568 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003569 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003570{
3571 int err, lock_err, i;
3572 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003573 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003574
3575 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3576 netdev_err(tp->dev,
3577 "%s: Trying to load TX cpu firmware which is 5705\n",
3578 __func__);
3579 return -EINVAL;
3580 }
3581
3582 if (tg3_flag(tp, 5705_PLUS))
3583 write_op = tg3_write_mem;
3584 else
3585 write_op = tg3_write_indirect_reg32;
3586
3587 /* It is possible that bootcode is still loading at this point.
3588 * Get the nvram lock first before halting the cpu.
3589 */
3590 lock_err = tg3_nvram_lock(tp);
3591 err = tg3_halt_cpu(tp, cpu_base);
3592 if (!lock_err)
3593 tg3_nvram_unlock(tp);
3594 if (err)
3595 goto out;
3596
3597 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3598 write_op(tp, cpu_scratch_base + i, 0);
3599 tw32(cpu_base + CPU_STATE, 0xffffffff);
3600 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Nithin Sujir77997ea2013-03-06 17:02:32 +00003601
Nithin Sujir31f11a92013-03-06 17:02:33 +00003602 do {
3603 u32 *fw_data = (u32 *)(fw_hdr + 1);
3604 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3605 write_op(tp, cpu_scratch_base +
3606 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3607 (i * sizeof(u32)),
3608 be32_to_cpu(fw_data[i]));
3609
3610 total_len -= be32_to_cpu(fw_hdr->len);
3611
3612 /* Advance to next fragment */
3613 fw_hdr = (struct tg3_firmware_hdr *)
3614 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3615 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003616
3617 err = 0;
3618
3619out:
3620 return err;
3621}
3622
3623/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003624static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3625{
3626 int i;
3627 const int iters = 5;
3628
3629 tw32(cpu_base + CPU_STATE, 0xffffffff);
3630 tw32_f(cpu_base + CPU_PC, pc);
3631
3632 for (i = 0; i < iters; i++) {
3633 if (tr32(cpu_base + CPU_PC) == pc)
3634 break;
3635 tw32(cpu_base + CPU_STATE, 0xffffffff);
3636 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3637 tw32_f(cpu_base + CPU_PC, pc);
3638 udelay(1000);
3639 }
3640
3641 return (i == iters) ? -EBUSY : 0;
3642}
3643
3644/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003645static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3646{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003647 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003648 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003649
Nithin Sujir77997ea2013-03-06 17:02:32 +00003650 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003651
3652 /* Firmware blob starts with version numbers, followed by
3653 start address and length. We are setting complete length.
3654 length = end_address_of_bss - start_address_of_text.
3655 Remainder is the blob to be loaded contiguously
3656 from start address. */
3657
Matt Carlson997b4f12011-08-31 11:44:53 +00003658 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3659 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003660 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003661 if (err)
3662 return err;
3663
3664 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3665 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003666 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003667 if (err)
3668 return err;
3669
3670 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003671 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3672 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003673 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003674 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3675 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003676 tr32(RX_CPU_BASE + CPU_PC),
3677 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003678 return -ENODEV;
3679 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003680
3681 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003682
3683 return 0;
3684}
3685
3686/* tp->lock is held. */
3687static int tg3_load_tso_firmware(struct tg3 *tp)
3688{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003689 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003690 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003691 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003692
Matt Carlson1caf13e2013-03-06 17:02:29 +00003693 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003694 return 0;
3695
Nithin Sujir77997ea2013-03-06 17:02:32 +00003696 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003697
3698 /* Firmware blob starts with version numbers, followed by
3699 start address and length. We are setting complete length.
3700 length = end_address_of_bss - start_address_of_text.
3701 Remainder is the blob to be loaded contiguously
3702 from start address. */
3703
Matt Carlson997b4f12011-08-31 11:44:53 +00003704 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003705
Joe Perches41535772013-02-16 11:20:04 +00003706 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003707 cpu_base = RX_CPU_BASE;
3708 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3709 } else {
3710 cpu_base = TX_CPU_BASE;
3711 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3712 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3713 }
3714
3715 err = tg3_load_firmware_cpu(tp, cpu_base,
3716 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003717 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003718 if (err)
3719 return err;
3720
3721 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003722 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3723 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003724 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003725 netdev_err(tp->dev,
3726 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003727 __func__, tr32(cpu_base + CPU_PC),
3728 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003729 return -ENODEV;
3730 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003731
3732 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003733 return 0;
3734}
3735
3736
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003737/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003738static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3739{
3740 u32 addr_high, addr_low;
3741 int i;
3742
3743 addr_high = ((tp->dev->dev_addr[0] << 8) |
3744 tp->dev->dev_addr[1]);
3745 addr_low = ((tp->dev->dev_addr[2] << 24) |
3746 (tp->dev->dev_addr[3] << 16) |
3747 (tp->dev->dev_addr[4] << 8) |
3748 (tp->dev->dev_addr[5] << 0));
3749 for (i = 0; i < 4; i++) {
3750 if (i == 1 && skip_mac_1)
3751 continue;
3752 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3753 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3754 }
3755
Joe Perches41535772013-02-16 11:20:04 +00003756 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3757 tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson3f007892008-11-03 16:51:36 -08003758 for (i = 0; i < 12; i++) {
3759 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3760 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3761 }
3762 }
3763
3764 addr_high = (tp->dev->dev_addr[0] +
3765 tp->dev->dev_addr[1] +
3766 tp->dev->dev_addr[2] +
3767 tp->dev->dev_addr[3] +
3768 tp->dev->dev_addr[4] +
3769 tp->dev->dev_addr[5]) &
3770 TX_BACKOFF_SEED_MASK;
3771 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3772}
3773
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003774static void tg3_enable_register_access(struct tg3 *tp)
3775{
3776 /*
3777 * Make sure register accesses (indirect or otherwise) will function
3778 * correctly.
3779 */
3780 pci_write_config_dword(tp->pdev,
3781 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3782}
3783
3784static int tg3_power_up(struct tg3 *tp)
3785{
Matt Carlsonbed98292011-07-13 09:27:29 +00003786 int err;
3787
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003788 tg3_enable_register_access(tp);
3789
Matt Carlsonbed98292011-07-13 09:27:29 +00003790 err = pci_set_power_state(tp->pdev, PCI_D0);
3791 if (!err) {
3792 /* Switch out of Vaux if it is a NIC */
3793 tg3_pwrsrc_switch_to_vmain(tp);
3794 } else {
3795 netdev_err(tp->dev, "Transition to D0 failed\n");
3796 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003797
Matt Carlsonbed98292011-07-13 09:27:29 +00003798 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003799}
3800
Matt Carlson4b409522012-02-13 10:20:11 +00003801static int tg3_setup_phy(struct tg3 *, int);
3802
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003803static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
3805 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003806 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003808 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003809
3810 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003811 if (tg3_flag(tp, CLKREQ_BUG))
3812 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3813 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003814
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3816 tw32(TG3PCI_MISC_HOST_CTRL,
3817 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3818
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003819 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003820 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003821
Joe Perches63c3a662011-04-26 08:12:10 +00003822 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003823 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003824 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003825 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003826 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003827 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003828
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003829 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003830
Matt Carlson80096062010-08-02 11:26:06 +00003831 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003832
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003833 tp->link_config.speed = phydev->speed;
3834 tp->link_config.duplex = phydev->duplex;
3835 tp->link_config.autoneg = phydev->autoneg;
3836 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003837
3838 advertising = ADVERTISED_TP |
3839 ADVERTISED_Pause |
3840 ADVERTISED_Autoneg |
3841 ADVERTISED_10baseT_Half;
3842
Joe Perches63c3a662011-04-26 08:12:10 +00003843 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3844 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003845 advertising |=
3846 ADVERTISED_100baseT_Half |
3847 ADVERTISED_100baseT_Full |
3848 ADVERTISED_10baseT_Full;
3849 else
3850 advertising |= ADVERTISED_10baseT_Full;
3851 }
3852
3853 phydev->advertising = advertising;
3854
3855 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003856
3857 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003858 if (phyid != PHY_ID_BCMAC131) {
3859 phyid &= PHY_BCM_OUI_MASK;
3860 if (phyid == PHY_BCM_OUI_1 ||
3861 phyid == PHY_BCM_OUI_2 ||
3862 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003863 do_low_power = true;
3864 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003865 }
Matt Carlsondd477002008-05-25 23:45:58 -07003866 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003867 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003868
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003869 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003870 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
Matt Carlson2855b9f2012-02-13 15:20:14 +00003872 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003873 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 }
3875
Joe Perches41535772013-02-16 11:20:04 +00003876 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07003877 u32 val;
3878
3879 val = tr32(GRC_VCPU_EXT_CTRL);
3880 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003881 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003882 int i;
3883 u32 val;
3884
3885 for (i = 0; i < 200; i++) {
3886 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3887 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3888 break;
3889 msleep(1);
3890 }
3891 }
Joe Perches63c3a662011-04-26 08:12:10 +00003892 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003893 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3894 WOL_DRV_STATE_SHUTDOWN |
3895 WOL_DRV_WOL |
3896 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003897
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003898 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 u32 mac_mode;
3900
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003901 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003902 if (do_low_power &&
3903 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3904 tg3_phy_auxctl_write(tp,
3905 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3906 MII_TG3_AUXCTL_PCTL_WOL_EN |
3907 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3908 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003909 udelay(40);
3910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003912 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003913 mac_mode = MAC_MODE_PORT_MODE_GMII;
3914 else
3915 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003917 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00003918 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003919 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003920 SPEED_100 : SPEED_10;
3921 if (tg3_5700_link_polarity(tp, speed))
3922 mac_mode |= MAC_MODE_LINK_POLARITY;
3923 else
3924 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 } else {
3927 mac_mode = MAC_MODE_PORT_MODE_TBI;
3928 }
3929
Joe Perches63c3a662011-04-26 08:12:10 +00003930 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 tw32(MAC_LED_CTRL, tp->led_ctrl);
3932
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003933 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003934 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3935 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003936 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Joe Perches63c3a662011-04-26 08:12:10 +00003938 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003939 mac_mode |= MAC_MODE_APE_TX_EN |
3940 MAC_MODE_APE_RX_EN |
3941 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003942
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 tw32_f(MAC_MODE, mac_mode);
3944 udelay(100);
3945
3946 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3947 udelay(10);
3948 }
3949
Joe Perches63c3a662011-04-26 08:12:10 +00003950 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00003951 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3952 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 u32 base_val;
3954
3955 base_val = tp->pci_clock_ctrl;
3956 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3957 CLOCK_CTRL_TXCLK_DISABLE);
3958
Michael Chanb401e9e2005-12-19 16:27:04 -08003959 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3960 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003961 } else if (tg3_flag(tp, 5780_CLASS) ||
3962 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00003963 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003964 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003965 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 u32 newbits1, newbits2;
3967
Joe Perches41535772013-02-16 11:20:04 +00003968 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3969 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3971 CLOCK_CTRL_TXCLK_DISABLE |
3972 CLOCK_CTRL_ALTCLK);
3973 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003974 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 newbits1 = CLOCK_CTRL_625_CORE;
3976 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3977 } else {
3978 newbits1 = CLOCK_CTRL_ALTCLK;
3979 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3980 }
3981
Michael Chanb401e9e2005-12-19 16:27:04 -08003982 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3983 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
Michael Chanb401e9e2005-12-19 16:27:04 -08003985 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3986 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Joe Perches63c3a662011-04-26 08:12:10 +00003988 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 u32 newbits3;
3990
Joe Perches41535772013-02-16 11:20:04 +00003991 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3992 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3994 CLOCK_CTRL_TXCLK_DISABLE |
3995 CLOCK_CTRL_44MHZ_CORE);
3996 } else {
3997 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3998 }
3999
Michael Chanb401e9e2005-12-19 16:27:04 -08004000 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4001 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
4003 }
4004
Joe Perches63c3a662011-04-26 08:12:10 +00004005 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004006 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004007
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004008 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
4010 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004011 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004012 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4013 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 u32 val = tr32(0x7d00);
4015
4016 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4017 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004018 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004019 int err;
4020
4021 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004023 if (!err)
4024 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 }
4027
Michael Chanbbadf502006-04-06 21:46:34 -07004028 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 return 0;
4031}
4032
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004033static void tg3_power_down(struct tg3 *tp)
4034{
4035 tg3_power_down_prepare(tp);
4036
Joe Perches63c3a662011-04-26 08:12:10 +00004037 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004038 pci_set_power_state(tp->pdev, PCI_D3hot);
4039}
4040
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4042{
4043 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4044 case MII_TG3_AUX_STAT_10HALF:
4045 *speed = SPEED_10;
4046 *duplex = DUPLEX_HALF;
4047 break;
4048
4049 case MII_TG3_AUX_STAT_10FULL:
4050 *speed = SPEED_10;
4051 *duplex = DUPLEX_FULL;
4052 break;
4053
4054 case MII_TG3_AUX_STAT_100HALF:
4055 *speed = SPEED_100;
4056 *duplex = DUPLEX_HALF;
4057 break;
4058
4059 case MII_TG3_AUX_STAT_100FULL:
4060 *speed = SPEED_100;
4061 *duplex = DUPLEX_FULL;
4062 break;
4063
4064 case MII_TG3_AUX_STAT_1000HALF:
4065 *speed = SPEED_1000;
4066 *duplex = DUPLEX_HALF;
4067 break;
4068
4069 case MII_TG3_AUX_STAT_1000FULL:
4070 *speed = SPEED_1000;
4071 *duplex = DUPLEX_FULL;
4072 break;
4073
4074 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004075 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004076 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4077 SPEED_10;
4078 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4079 DUPLEX_HALF;
4080 break;
4081 }
Matt Carlsone7405222012-02-13 15:20:16 +00004082 *speed = SPEED_UNKNOWN;
4083 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086}
4087
Matt Carlson42b64a42011-05-19 12:12:49 +00004088static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089{
Matt Carlson42b64a42011-05-19 12:12:49 +00004090 int err = 0;
4091 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Matt Carlson42b64a42011-05-19 12:12:49 +00004093 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004094 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004095 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Matt Carlson42b64a42011-05-19 12:12:49 +00004097 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4098 if (err)
4099 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Matt Carlson4f272092011-12-14 11:09:57 +00004101 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4102 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004103
Joe Perches41535772013-02-16 11:20:04 +00004104 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4105 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004106 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004107
Matt Carlson4f272092011-12-14 11:09:57 +00004108 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4109 if (err)
4110 goto done;
4111 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004112
Matt Carlson42b64a42011-05-19 12:12:49 +00004113 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4114 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Matt Carlson42b64a42011-05-19 12:12:49 +00004116 tw32(TG3_CPMU_EEE_MODE,
4117 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004118
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004119 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004120 if (!err) {
4121 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004122
Matt Carlsona6b68da2010-12-06 08:28:52 +00004123 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004124 /* Advertise 100-BaseTX EEE ability */
4125 if (advertise & ADVERTISED_100baseT_Full)
4126 val |= MDIO_AN_EEE_ADV_100TX;
4127 /* Advertise 1000-BaseT EEE ability */
4128 if (advertise & ADVERTISED_1000baseT_Full)
4129 val |= MDIO_AN_EEE_ADV_1000T;
4130 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004131 if (err)
4132 val = 0;
4133
Joe Perches41535772013-02-16 11:20:04 +00004134 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004135 case ASIC_REV_5717:
4136 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004137 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004138 case ASIC_REV_5719:
4139 /* If we advertised any eee advertisements above... */
4140 if (val)
4141 val = MII_TG3_DSP_TAP26_ALNOKO |
4142 MII_TG3_DSP_TAP26_RMRXSTO |
4143 MII_TG3_DSP_TAP26_OPCSINPT;
4144 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4145 /* Fall through */
4146 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004147 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004148 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4149 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4150 MII_TG3_DSP_CH34TP2_HIBW01);
4151 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004152
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004153 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004154 if (!err)
4155 err = err2;
4156 }
4157
4158done:
4159 return err;
4160}
4161
4162static void tg3_phy_copper_begin(struct tg3 *tp)
4163{
Matt Carlsond13ba512012-02-22 12:35:19 +00004164 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4165 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4166 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004167
Matt Carlsond13ba512012-02-22 12:35:19 +00004168 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
4169 adv = ADVERTISED_10baseT_Half |
4170 ADVERTISED_10baseT_Full;
4171 if (tg3_flag(tp, WOL_SPEED_100MB))
4172 adv |= ADVERTISED_100baseT_Half |
4173 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004174
Matt Carlsond13ba512012-02-22 12:35:19 +00004175 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004176 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004177 adv = tp->link_config.advertising;
4178 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4179 adv &= ~(ADVERTISED_1000baseT_Half |
4180 ADVERTISED_1000baseT_Full);
4181
4182 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004183 }
4184
Matt Carlsond13ba512012-02-22 12:35:19 +00004185 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004186
Matt Carlsond13ba512012-02-22 12:35:19 +00004187 tg3_writephy(tp, MII_BMCR,
4188 BMCR_ANENABLE | BMCR_ANRESTART);
4189 } else {
4190 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 u32 bmcr, orig_bmcr;
4192
4193 tp->link_config.active_speed = tp->link_config.speed;
4194 tp->link_config.active_duplex = tp->link_config.duplex;
4195
4196 bmcr = 0;
4197 switch (tp->link_config.speed) {
4198 default:
4199 case SPEED_10:
4200 break;
4201
4202 case SPEED_100:
4203 bmcr |= BMCR_SPEED100;
4204 break;
4205
4206 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004207 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 if (tp->link_config.duplex == DUPLEX_FULL)
4212 bmcr |= BMCR_FULLDPLX;
4213
4214 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4215 (bmcr != orig_bmcr)) {
4216 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4217 for (i = 0; i < 1500; i++) {
4218 u32 tmp;
4219
4220 udelay(10);
4221 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4222 tg3_readphy(tp, MII_BMSR, &tmp))
4223 continue;
4224 if (!(tmp & BMSR_LSTATUS)) {
4225 udelay(40);
4226 break;
4227 }
4228 }
4229 tg3_writephy(tp, MII_BMCR, bmcr);
4230 udelay(40);
4231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 }
4233}
4234
4235static int tg3_init_5401phy_dsp(struct tg3 *tp)
4236{
4237 int err;
4238
4239 /* Turn off tap power management. */
4240 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004241 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004243 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4244 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4245 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4246 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4247 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
4249 udelay(40);
4250
4251 return err;
4252}
4253
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004254static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004256 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004257
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004258 advertising = tp->link_config.advertising;
4259 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004261 advmsk = ADVERTISE_ALL;
4262 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004263 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004264 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004267 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4268 return false;
4269
4270 if ((*lcladv & advmsk) != tgtadv)
4271 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004272
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004273 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 u32 tg3_ctrl;
4275
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004276 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004277
Matt Carlson221c5632011-06-13 13:39:01 +00004278 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004279 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Matt Carlson3198e072012-02-13 15:20:10 +00004281 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004282 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4283 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004284 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4285 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4286 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4287 } else {
4288 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4289 }
4290
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004291 if (tg3_ctrl != tgtadv)
4292 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004294
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004295 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004296}
4297
Matt Carlson859edb22011-12-08 14:40:16 +00004298static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4299{
4300 u32 lpeth = 0;
4301
4302 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4303 u32 val;
4304
4305 if (tg3_readphy(tp, MII_STAT1000, &val))
4306 return false;
4307
4308 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4309 }
4310
4311 if (tg3_readphy(tp, MII_LPA, rmtadv))
4312 return false;
4313
4314 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4315 tp->link_config.rmt_adv = lpeth;
4316
4317 return true;
4318}
4319
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004320static bool tg3_test_and_report_link_chg(struct tg3 *tp, int curr_link_up)
4321{
4322 if (curr_link_up != tp->link_up) {
4323 if (curr_link_up) {
4324 tg3_carrier_on(tp);
4325 } else {
4326 tg3_carrier_off(tp);
4327 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4328 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4329 }
4330
4331 tg3_link_report(tp);
4332 return true;
4333 }
4334
4335 return false;
4336}
4337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4339{
4340 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004341 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004342 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 u16 current_speed;
4344 u8 current_duplex;
4345 int i, err;
4346
4347 tw32(MAC_EVENT, 0);
4348
4349 tw32_f(MAC_STATUS,
4350 (MAC_STATUS_SYNC_CHANGED |
4351 MAC_STATUS_CFG_CHANGED |
4352 MAC_STATUS_MI_COMPLETION |
4353 MAC_STATUS_LNKSTATE_CHANGED));
4354 udelay(40);
4355
Matt Carlson8ef21422008-05-02 16:47:53 -07004356 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4357 tw32_f(MAC_MI_MODE,
4358 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4359 udelay(80);
4360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004362 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
4364 /* Some third-party PHYs need to be reset on link going
4365 * down.
4366 */
Joe Perches41535772013-02-16 11:20:04 +00004367 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4368 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4369 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004370 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 tg3_readphy(tp, MII_BMSR, &bmsr);
4372 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4373 !(bmsr & BMSR_LSTATUS))
4374 force_reset = 1;
4375 }
4376 if (force_reset)
4377 tg3_phy_reset(tp);
4378
Matt Carlson79eb6902010-02-17 15:17:03 +00004379 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 tg3_readphy(tp, MII_BMSR, &bmsr);
4381 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004382 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 bmsr = 0;
4384
4385 if (!(bmsr & BMSR_LSTATUS)) {
4386 err = tg3_init_5401phy_dsp(tp);
4387 if (err)
4388 return err;
4389
4390 tg3_readphy(tp, MII_BMSR, &bmsr);
4391 for (i = 0; i < 1000; i++) {
4392 udelay(10);
4393 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4394 (bmsr & BMSR_LSTATUS)) {
4395 udelay(40);
4396 break;
4397 }
4398 }
4399
Matt Carlson79eb6902010-02-17 15:17:03 +00004400 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4401 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 !(bmsr & BMSR_LSTATUS) &&
4403 tp->link_config.active_speed == SPEED_1000) {
4404 err = tg3_phy_reset(tp);
4405 if (!err)
4406 err = tg3_init_5401phy_dsp(tp);
4407 if (err)
4408 return err;
4409 }
4410 }
Joe Perches41535772013-02-16 11:20:04 +00004411 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4412 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 /* 5701 {A0,B0} CRC bug workaround */
4414 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004415 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4416 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4417 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 }
4419
4420 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004421 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4422 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004424 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004426 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4428
Joe Perches41535772013-02-16 11:20:04 +00004429 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4430 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4432 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4433 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4434 else
4435 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4436 }
4437
4438 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004439 current_speed = SPEED_UNKNOWN;
4440 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004441 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004442 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004444 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004445 err = tg3_phy_auxctl_read(tp,
4446 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4447 &val);
4448 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004449 tg3_phy_auxctl_write(tp,
4450 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4451 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 goto relink;
4453 }
4454 }
4455
4456 bmsr = 0;
4457 for (i = 0; i < 100; i++) {
4458 tg3_readphy(tp, MII_BMSR, &bmsr);
4459 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4460 (bmsr & BMSR_LSTATUS))
4461 break;
4462 udelay(40);
4463 }
4464
4465 if (bmsr & BMSR_LSTATUS) {
4466 u32 aux_stat, bmcr;
4467
4468 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4469 for (i = 0; i < 2000; i++) {
4470 udelay(10);
4471 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4472 aux_stat)
4473 break;
4474 }
4475
4476 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4477 &current_speed,
4478 &current_duplex);
4479
4480 bmcr = 0;
4481 for (i = 0; i < 200; i++) {
4482 tg3_readphy(tp, MII_BMCR, &bmcr);
4483 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4484 continue;
4485 if (bmcr && bmcr != 0x7fff)
4486 break;
4487 udelay(10);
4488 }
4489
Matt Carlsonef167e22007-12-20 20:10:01 -08004490 lcl_adv = 0;
4491 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Matt Carlsonef167e22007-12-20 20:10:01 -08004493 tp->link_config.active_speed = current_speed;
4494 tp->link_config.active_duplex = current_duplex;
4495
4496 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4497 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004498 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004499 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004500 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 } else {
4502 if (!(bmcr & BMCR_ANENABLE) &&
4503 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004504 tp->link_config.duplex == current_duplex &&
4505 tp->link_config.flowctrl ==
4506 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 }
4509 }
4510
Matt Carlsonef167e22007-12-20 20:10:01 -08004511 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004512 tp->link_config.active_duplex == DUPLEX_FULL) {
4513 u32 reg, bit;
4514
4515 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4516 reg = MII_TG3_FET_GEN_STAT;
4517 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4518 } else {
4519 reg = MII_TG3_EXT_STAT;
4520 bit = MII_TG3_EXT_STAT_MDIX;
4521 }
4522
4523 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4524 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4525
Matt Carlsonef167e22007-12-20 20:10:01 -08004526 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 }
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530relink:
Matt Carlson80096062010-08-02 11:26:06 +00004531 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 tg3_phy_copper_begin(tp);
4533
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004534 if (tg3_flag(tp, ROBOSWITCH)) {
4535 current_link_up = 1;
4536 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4537 current_speed = SPEED_1000;
4538 current_duplex = DUPLEX_FULL;
4539 tp->link_config.active_speed = current_speed;
4540 tp->link_config.active_duplex = current_duplex;
4541 }
4542
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004543 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004544 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4545 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 current_link_up = 1;
4547 }
4548
4549 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4550 if (current_link_up == 1) {
4551 if (tp->link_config.active_speed == SPEED_100 ||
4552 tp->link_config.active_speed == SPEED_10)
4553 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4554 else
4555 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004556 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004557 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4558 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4560
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004561 /* In order for the 5750 core in BCM4785 chip to work properly
4562 * in RGMII mode, the Led Control Register must be set up.
4563 */
4564 if (tg3_flag(tp, RGMII_MODE)) {
4565 u32 led_ctrl = tr32(MAC_LED_CTRL);
4566 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
4567
4568 if (tp->link_config.active_speed == SPEED_10)
4569 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
4570 else if (tp->link_config.active_speed == SPEED_100)
4571 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4572 LED_CTRL_100MBPS_ON);
4573 else if (tp->link_config.active_speed == SPEED_1000)
4574 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4575 LED_CTRL_1000MBPS_ON);
4576
4577 tw32(MAC_LED_CTRL, led_ctrl);
4578 udelay(40);
4579 }
4580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4582 if (tp->link_config.active_duplex == DUPLEX_HALF)
4583 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4584
Joe Perches41535772013-02-16 11:20:04 +00004585 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004586 if (current_link_up == 1 &&
4587 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004589 else
4590 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 }
4592
4593 /* ??? Without this setting Netgear GA302T PHY does not
4594 * ??? send/receive packets...
4595 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004596 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00004597 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4599 tw32_f(MAC_MI_MODE, tp->mi_mode);
4600 udelay(80);
4601 }
4602
4603 tw32_f(MAC_MODE, tp->mac_mode);
4604 udelay(40);
4605
Matt Carlson52b02d02010-10-14 10:37:41 +00004606 tg3_phy_eee_adjust(tp, current_link_up);
4607
Joe Perches63c3a662011-04-26 08:12:10 +00004608 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 /* Polled via timer. */
4610 tw32_f(MAC_EVENT, 0);
4611 } else {
4612 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4613 }
4614 udelay(40);
4615
Joe Perches41535772013-02-16 11:20:04 +00004616 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 current_link_up == 1 &&
4618 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004619 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 udelay(120);
4621 tw32_f(MAC_STATUS,
4622 (MAC_STATUS_SYNC_CHANGED |
4623 MAC_STATUS_CFG_CHANGED));
4624 udelay(40);
4625 tg3_write_mem(tp,
4626 NIC_SRAM_FIRMWARE_MBOX,
4627 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4628 }
4629
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004630 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004631 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004632 if (tp->link_config.active_speed == SPEED_100 ||
4633 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004634 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4635 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004636 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004637 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4638 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004639 }
4640
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004641 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
4643 return 0;
4644}
4645
4646struct tg3_fiber_aneginfo {
4647 int state;
4648#define ANEG_STATE_UNKNOWN 0
4649#define ANEG_STATE_AN_ENABLE 1
4650#define ANEG_STATE_RESTART_INIT 2
4651#define ANEG_STATE_RESTART 3
4652#define ANEG_STATE_DISABLE_LINK_OK 4
4653#define ANEG_STATE_ABILITY_DETECT_INIT 5
4654#define ANEG_STATE_ABILITY_DETECT 6
4655#define ANEG_STATE_ACK_DETECT_INIT 7
4656#define ANEG_STATE_ACK_DETECT 8
4657#define ANEG_STATE_COMPLETE_ACK_INIT 9
4658#define ANEG_STATE_COMPLETE_ACK 10
4659#define ANEG_STATE_IDLE_DETECT_INIT 11
4660#define ANEG_STATE_IDLE_DETECT 12
4661#define ANEG_STATE_LINK_OK 13
4662#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4663#define ANEG_STATE_NEXT_PAGE_WAIT 15
4664
4665 u32 flags;
4666#define MR_AN_ENABLE 0x00000001
4667#define MR_RESTART_AN 0x00000002
4668#define MR_AN_COMPLETE 0x00000004
4669#define MR_PAGE_RX 0x00000008
4670#define MR_NP_LOADED 0x00000010
4671#define MR_TOGGLE_TX 0x00000020
4672#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4673#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4674#define MR_LP_ADV_SYM_PAUSE 0x00000100
4675#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4676#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4677#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4678#define MR_LP_ADV_NEXT_PAGE 0x00001000
4679#define MR_TOGGLE_RX 0x00002000
4680#define MR_NP_RX 0x00004000
4681
4682#define MR_LINK_OK 0x80000000
4683
4684 unsigned long link_time, cur_time;
4685
4686 u32 ability_match_cfg;
4687 int ability_match_count;
4688
4689 char ability_match, idle_match, ack_match;
4690
4691 u32 txconfig, rxconfig;
4692#define ANEG_CFG_NP 0x00000080
4693#define ANEG_CFG_ACK 0x00000040
4694#define ANEG_CFG_RF2 0x00000020
4695#define ANEG_CFG_RF1 0x00000010
4696#define ANEG_CFG_PS2 0x00000001
4697#define ANEG_CFG_PS1 0x00008000
4698#define ANEG_CFG_HD 0x00004000
4699#define ANEG_CFG_FD 0x00002000
4700#define ANEG_CFG_INVAL 0x00001f06
4701
4702};
4703#define ANEG_OK 0
4704#define ANEG_DONE 1
4705#define ANEG_TIMER_ENAB 2
4706#define ANEG_FAILED -1
4707
4708#define ANEG_STATE_SETTLE_TIME 10000
4709
4710static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4711 struct tg3_fiber_aneginfo *ap)
4712{
Matt Carlson5be73b42007-12-20 20:09:29 -08004713 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 unsigned long delta;
4715 u32 rx_cfg_reg;
4716 int ret;
4717
4718 if (ap->state == ANEG_STATE_UNKNOWN) {
4719 ap->rxconfig = 0;
4720 ap->link_time = 0;
4721 ap->cur_time = 0;
4722 ap->ability_match_cfg = 0;
4723 ap->ability_match_count = 0;
4724 ap->ability_match = 0;
4725 ap->idle_match = 0;
4726 ap->ack_match = 0;
4727 }
4728 ap->cur_time++;
4729
4730 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4731 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4732
4733 if (rx_cfg_reg != ap->ability_match_cfg) {
4734 ap->ability_match_cfg = rx_cfg_reg;
4735 ap->ability_match = 0;
4736 ap->ability_match_count = 0;
4737 } else {
4738 if (++ap->ability_match_count > 1) {
4739 ap->ability_match = 1;
4740 ap->ability_match_cfg = rx_cfg_reg;
4741 }
4742 }
4743 if (rx_cfg_reg & ANEG_CFG_ACK)
4744 ap->ack_match = 1;
4745 else
4746 ap->ack_match = 0;
4747
4748 ap->idle_match = 0;
4749 } else {
4750 ap->idle_match = 1;
4751 ap->ability_match_cfg = 0;
4752 ap->ability_match_count = 0;
4753 ap->ability_match = 0;
4754 ap->ack_match = 0;
4755
4756 rx_cfg_reg = 0;
4757 }
4758
4759 ap->rxconfig = rx_cfg_reg;
4760 ret = ANEG_OK;
4761
Matt Carlson33f401a2010-04-05 10:19:27 +00004762 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 case ANEG_STATE_UNKNOWN:
4764 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4765 ap->state = ANEG_STATE_AN_ENABLE;
4766
4767 /* fallthru */
4768 case ANEG_STATE_AN_ENABLE:
4769 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4770 if (ap->flags & MR_AN_ENABLE) {
4771 ap->link_time = 0;
4772 ap->cur_time = 0;
4773 ap->ability_match_cfg = 0;
4774 ap->ability_match_count = 0;
4775 ap->ability_match = 0;
4776 ap->idle_match = 0;
4777 ap->ack_match = 0;
4778
4779 ap->state = ANEG_STATE_RESTART_INIT;
4780 } else {
4781 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4782 }
4783 break;
4784
4785 case ANEG_STATE_RESTART_INIT:
4786 ap->link_time = ap->cur_time;
4787 ap->flags &= ~(MR_NP_LOADED);
4788 ap->txconfig = 0;
4789 tw32(MAC_TX_AUTO_NEG, 0);
4790 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4791 tw32_f(MAC_MODE, tp->mac_mode);
4792 udelay(40);
4793
4794 ret = ANEG_TIMER_ENAB;
4795 ap->state = ANEG_STATE_RESTART;
4796
4797 /* fallthru */
4798 case ANEG_STATE_RESTART:
4799 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004800 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004802 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 break;
4805
4806 case ANEG_STATE_DISABLE_LINK_OK:
4807 ret = ANEG_DONE;
4808 break;
4809
4810 case ANEG_STATE_ABILITY_DETECT_INIT:
4811 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004812 ap->txconfig = ANEG_CFG_FD;
4813 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4814 if (flowctrl & ADVERTISE_1000XPAUSE)
4815 ap->txconfig |= ANEG_CFG_PS1;
4816 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4817 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4819 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4820 tw32_f(MAC_MODE, tp->mac_mode);
4821 udelay(40);
4822
4823 ap->state = ANEG_STATE_ABILITY_DETECT;
4824 break;
4825
4826 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004827 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 break;
4830
4831 case ANEG_STATE_ACK_DETECT_INIT:
4832 ap->txconfig |= ANEG_CFG_ACK;
4833 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4834 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4835 tw32_f(MAC_MODE, tp->mac_mode);
4836 udelay(40);
4837
4838 ap->state = ANEG_STATE_ACK_DETECT;
4839
4840 /* fallthru */
4841 case ANEG_STATE_ACK_DETECT:
4842 if (ap->ack_match != 0) {
4843 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4844 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4845 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4846 } else {
4847 ap->state = ANEG_STATE_AN_ENABLE;
4848 }
4849 } else if (ap->ability_match != 0 &&
4850 ap->rxconfig == 0) {
4851 ap->state = ANEG_STATE_AN_ENABLE;
4852 }
4853 break;
4854
4855 case ANEG_STATE_COMPLETE_ACK_INIT:
4856 if (ap->rxconfig & ANEG_CFG_INVAL) {
4857 ret = ANEG_FAILED;
4858 break;
4859 }
4860 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4861 MR_LP_ADV_HALF_DUPLEX |
4862 MR_LP_ADV_SYM_PAUSE |
4863 MR_LP_ADV_ASYM_PAUSE |
4864 MR_LP_ADV_REMOTE_FAULT1 |
4865 MR_LP_ADV_REMOTE_FAULT2 |
4866 MR_LP_ADV_NEXT_PAGE |
4867 MR_TOGGLE_RX |
4868 MR_NP_RX);
4869 if (ap->rxconfig & ANEG_CFG_FD)
4870 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4871 if (ap->rxconfig & ANEG_CFG_HD)
4872 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4873 if (ap->rxconfig & ANEG_CFG_PS1)
4874 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4875 if (ap->rxconfig & ANEG_CFG_PS2)
4876 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4877 if (ap->rxconfig & ANEG_CFG_RF1)
4878 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4879 if (ap->rxconfig & ANEG_CFG_RF2)
4880 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4881 if (ap->rxconfig & ANEG_CFG_NP)
4882 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4883
4884 ap->link_time = ap->cur_time;
4885
4886 ap->flags ^= (MR_TOGGLE_TX);
4887 if (ap->rxconfig & 0x0008)
4888 ap->flags |= MR_TOGGLE_RX;
4889 if (ap->rxconfig & ANEG_CFG_NP)
4890 ap->flags |= MR_NP_RX;
4891 ap->flags |= MR_PAGE_RX;
4892
4893 ap->state = ANEG_STATE_COMPLETE_ACK;
4894 ret = ANEG_TIMER_ENAB;
4895 break;
4896
4897 case ANEG_STATE_COMPLETE_ACK:
4898 if (ap->ability_match != 0 &&
4899 ap->rxconfig == 0) {
4900 ap->state = ANEG_STATE_AN_ENABLE;
4901 break;
4902 }
4903 delta = ap->cur_time - ap->link_time;
4904 if (delta > ANEG_STATE_SETTLE_TIME) {
4905 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4906 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4907 } else {
4908 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4909 !(ap->flags & MR_NP_RX)) {
4910 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4911 } else {
4912 ret = ANEG_FAILED;
4913 }
4914 }
4915 }
4916 break;
4917
4918 case ANEG_STATE_IDLE_DETECT_INIT:
4919 ap->link_time = ap->cur_time;
4920 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4921 tw32_f(MAC_MODE, tp->mac_mode);
4922 udelay(40);
4923
4924 ap->state = ANEG_STATE_IDLE_DETECT;
4925 ret = ANEG_TIMER_ENAB;
4926 break;
4927
4928 case ANEG_STATE_IDLE_DETECT:
4929 if (ap->ability_match != 0 &&
4930 ap->rxconfig == 0) {
4931 ap->state = ANEG_STATE_AN_ENABLE;
4932 break;
4933 }
4934 delta = ap->cur_time - ap->link_time;
4935 if (delta > ANEG_STATE_SETTLE_TIME) {
4936 /* XXX another gem from the Broadcom driver :( */
4937 ap->state = ANEG_STATE_LINK_OK;
4938 }
4939 break;
4940
4941 case ANEG_STATE_LINK_OK:
4942 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4943 ret = ANEG_DONE;
4944 break;
4945
4946 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4947 /* ??? unimplemented */
4948 break;
4949
4950 case ANEG_STATE_NEXT_PAGE_WAIT:
4951 /* ??? unimplemented */
4952 break;
4953
4954 default:
4955 ret = ANEG_FAILED;
4956 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
4959 return ret;
4960}
4961
Matt Carlson5be73b42007-12-20 20:09:29 -08004962static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963{
4964 int res = 0;
4965 struct tg3_fiber_aneginfo aninfo;
4966 int status = ANEG_FAILED;
4967 unsigned int tick;
4968 u32 tmp;
4969
4970 tw32_f(MAC_TX_AUTO_NEG, 0);
4971
4972 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4973 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4974 udelay(40);
4975
4976 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4977 udelay(40);
4978
4979 memset(&aninfo, 0, sizeof(aninfo));
4980 aninfo.flags |= MR_AN_ENABLE;
4981 aninfo.state = ANEG_STATE_UNKNOWN;
4982 aninfo.cur_time = 0;
4983 tick = 0;
4984 while (++tick < 195000) {
4985 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4986 if (status == ANEG_DONE || status == ANEG_FAILED)
4987 break;
4988
4989 udelay(1);
4990 }
4991
4992 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4993 tw32_f(MAC_MODE, tp->mac_mode);
4994 udelay(40);
4995
Matt Carlson5be73b42007-12-20 20:09:29 -08004996 *txflags = aninfo.txconfig;
4997 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
4999 if (status == ANEG_DONE &&
5000 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5001 MR_LP_ADV_FULL_DUPLEX)))
5002 res = 1;
5003
5004 return res;
5005}
5006
5007static void tg3_init_bcm8002(struct tg3 *tp)
5008{
5009 u32 mac_status = tr32(MAC_STATUS);
5010 int i;
5011
5012 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005013 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 !(mac_status & MAC_STATUS_PCS_SYNCED))
5015 return;
5016
5017 /* Set PLL lock range. */
5018 tg3_writephy(tp, 0x16, 0x8007);
5019
5020 /* SW reset */
5021 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5022
5023 /* Wait for reset to complete. */
5024 /* XXX schedule_timeout() ... */
5025 for (i = 0; i < 500; i++)
5026 udelay(10);
5027
5028 /* Config mode; select PMA/Ch 1 regs. */
5029 tg3_writephy(tp, 0x10, 0x8411);
5030
5031 /* Enable auto-lock and comdet, select txclk for tx. */
5032 tg3_writephy(tp, 0x11, 0x0a10);
5033
5034 tg3_writephy(tp, 0x18, 0x00a0);
5035 tg3_writephy(tp, 0x16, 0x41ff);
5036
5037 /* Assert and deassert POR. */
5038 tg3_writephy(tp, 0x13, 0x0400);
5039 udelay(40);
5040 tg3_writephy(tp, 0x13, 0x0000);
5041
5042 tg3_writephy(tp, 0x11, 0x0a50);
5043 udelay(40);
5044 tg3_writephy(tp, 0x11, 0x0a10);
5045
5046 /* Wait for signal to stabilize */
5047 /* XXX schedule_timeout() ... */
5048 for (i = 0; i < 15000; i++)
5049 udelay(10);
5050
5051 /* Deselect the channel register so we can read the PHYID
5052 * later.
5053 */
5054 tg3_writephy(tp, 0x10, 0x8011);
5055}
5056
5057static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
5058{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005059 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 u32 sg_dig_ctrl, sg_dig_status;
5061 u32 serdes_cfg, expected_sg_dig_ctrl;
5062 int workaround, port_a;
5063 int current_link_up;
5064
5065 serdes_cfg = 0;
5066 expected_sg_dig_ctrl = 0;
5067 workaround = 0;
5068 port_a = 1;
5069 current_link_up = 0;
5070
Joe Perches41535772013-02-16 11:20:04 +00005071 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5072 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 workaround = 1;
5074 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5075 port_a = 0;
5076
5077 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5078 /* preserve bits 20-23 for voltage regulator */
5079 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5080 }
5081
5082 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5083
5084 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005085 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 if (workaround) {
5087 u32 val = serdes_cfg;
5088
5089 if (port_a)
5090 val |= 0xc010000;
5091 else
5092 val |= 0x4010000;
5093 tw32_f(MAC_SERDES_CFG, val);
5094 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005095
5096 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 }
5098 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5099 tg3_setup_flow_control(tp, 0, 0);
5100 current_link_up = 1;
5101 }
5102 goto out;
5103 }
5104
5105 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005106 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Matt Carlson82cd3d12007-12-20 20:09:00 -08005108 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5109 if (flowctrl & ADVERTISE_1000XPAUSE)
5110 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5111 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5112 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
5114 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005115 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005116 tp->serdes_counter &&
5117 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5118 MAC_STATUS_RCVD_CFG)) ==
5119 MAC_STATUS_PCS_SYNCED)) {
5120 tp->serdes_counter--;
5121 current_link_up = 1;
5122 goto out;
5123 }
5124restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 if (workaround)
5126 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005127 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 udelay(5);
5129 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5130
Michael Chan3d3ebe72006-09-27 15:59:15 -07005131 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005132 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5134 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005135 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 mac_status = tr32(MAC_STATUS);
5137
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005138 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005140 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
Matt Carlson82cd3d12007-12-20 20:09:00 -08005142 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5143 local_adv |= ADVERTISE_1000XPAUSE;
5144 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5145 local_adv |= ADVERTISE_1000XPSE_ASYM;
5146
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005147 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005148 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005149 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005150 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151
Matt Carlson859edb22011-12-08 14:40:16 +00005152 tp->link_config.rmt_adv =
5153 mii_adv_to_ethtool_adv_x(remote_adv);
5154
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 tg3_setup_flow_control(tp, local_adv, remote_adv);
5156 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005157 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005158 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005159 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005160 if (tp->serdes_counter)
5161 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 else {
5163 if (workaround) {
5164 u32 val = serdes_cfg;
5165
5166 if (port_a)
5167 val |= 0xc010000;
5168 else
5169 val |= 0x4010000;
5170
5171 tw32_f(MAC_SERDES_CFG, val);
5172 }
5173
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005174 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 udelay(40);
5176
5177 /* Link parallel detection - link is up */
5178 /* only if we have PCS_SYNC and not */
5179 /* receiving config code words */
5180 mac_status = tr32(MAC_STATUS);
5181 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5182 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5183 tg3_setup_flow_control(tp, 0, 0);
5184 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005185 tp->phy_flags |=
5186 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005187 tp->serdes_counter =
5188 SERDES_PARALLEL_DET_TIMEOUT;
5189 } else
5190 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 }
5192 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005193 } else {
5194 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005195 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 }
5197
5198out:
5199 return current_link_up;
5200}
5201
5202static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
5203{
5204 int current_link_up = 0;
5205
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005206 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
5209 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005210 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005212
Matt Carlson5be73b42007-12-20 20:09:29 -08005213 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5214 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215
Matt Carlson5be73b42007-12-20 20:09:29 -08005216 if (txflags & ANEG_CFG_PS1)
5217 local_adv |= ADVERTISE_1000XPAUSE;
5218 if (txflags & ANEG_CFG_PS2)
5219 local_adv |= ADVERTISE_1000XPSE_ASYM;
5220
5221 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5222 remote_adv |= LPA_1000XPAUSE;
5223 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5224 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
Matt Carlson859edb22011-12-08 14:40:16 +00005226 tp->link_config.rmt_adv =
5227 mii_adv_to_ethtool_adv_x(remote_adv);
5228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 tg3_setup_flow_control(tp, local_adv, remote_adv);
5230
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 current_link_up = 1;
5232 }
5233 for (i = 0; i < 30; i++) {
5234 udelay(20);
5235 tw32_f(MAC_STATUS,
5236 (MAC_STATUS_SYNC_CHANGED |
5237 MAC_STATUS_CFG_CHANGED));
5238 udelay(40);
5239 if ((tr32(MAC_STATUS) &
5240 (MAC_STATUS_SYNC_CHANGED |
5241 MAC_STATUS_CFG_CHANGED)) == 0)
5242 break;
5243 }
5244
5245 mac_status = tr32(MAC_STATUS);
5246 if (current_link_up == 0 &&
5247 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5248 !(mac_status & MAC_STATUS_RCVD_CFG))
5249 current_link_up = 1;
5250 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005251 tg3_setup_flow_control(tp, 0, 0);
5252
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 /* Forcing 1000FD link up. */
5254 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
5256 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5257 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005258
5259 tw32_f(MAC_MODE, tp->mac_mode);
5260 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 }
5262
5263out:
5264 return current_link_up;
5265}
5266
5267static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
5268{
5269 u32 orig_pause_cfg;
5270 u16 orig_active_speed;
5271 u8 orig_active_duplex;
5272 u32 mac_status;
5273 int current_link_up;
5274 int i;
5275
Matt Carlson8d018622007-12-20 20:05:44 -08005276 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 orig_active_speed = tp->link_config.active_speed;
5278 orig_active_duplex = tp->link_config.active_duplex;
5279
Joe Perches63c3a662011-04-26 08:12:10 +00005280 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005281 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005282 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 mac_status = tr32(MAC_STATUS);
5284 mac_status &= (MAC_STATUS_PCS_SYNCED |
5285 MAC_STATUS_SIGNAL_DET |
5286 MAC_STATUS_CFG_CHANGED |
5287 MAC_STATUS_RCVD_CFG);
5288 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5289 MAC_STATUS_SIGNAL_DET)) {
5290 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5291 MAC_STATUS_CFG_CHANGED));
5292 return 0;
5293 }
5294 }
5295
5296 tw32_f(MAC_TX_AUTO_NEG, 0);
5297
5298 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5299 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5300 tw32_f(MAC_MODE, tp->mac_mode);
5301 udelay(40);
5302
Matt Carlson79eb6902010-02-17 15:17:03 +00005303 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 tg3_init_bcm8002(tp);
5305
5306 /* Enable link change event even when serdes polling. */
5307 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5308 udelay(40);
5309
5310 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005311 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 mac_status = tr32(MAC_STATUS);
5313
Joe Perches63c3a662011-04-26 08:12:10 +00005314 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5316 else
5317 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5318
Matt Carlson898a56f2009-08-28 14:02:40 +00005319 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005321 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
5323 for (i = 0; i < 100; i++) {
5324 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5325 MAC_STATUS_CFG_CHANGED));
5326 udelay(5);
5327 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005328 MAC_STATUS_CFG_CHANGED |
5329 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 break;
5331 }
5332
5333 mac_status = tr32(MAC_STATUS);
5334 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5335 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005336 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5337 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 tw32_f(MAC_MODE, (tp->mac_mode |
5339 MAC_MODE_SEND_CONFIGS));
5340 udelay(1);
5341 tw32_f(MAC_MODE, tp->mac_mode);
5342 }
5343 }
5344
5345 if (current_link_up == 1) {
5346 tp->link_config.active_speed = SPEED_1000;
5347 tp->link_config.active_duplex = DUPLEX_FULL;
5348 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5349 LED_CTRL_LNKLED_OVERRIDE |
5350 LED_CTRL_1000MBPS_ON));
5351 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005352 tp->link_config.active_speed = SPEED_UNKNOWN;
5353 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5355 LED_CTRL_LNKLED_OVERRIDE |
5356 LED_CTRL_TRAFFIC_OVERRIDE));
5357 }
5358
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005359 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005360 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 if (orig_pause_cfg != now_pause_cfg ||
5362 orig_active_speed != tp->link_config.active_speed ||
5363 orig_active_duplex != tp->link_config.active_duplex)
5364 tg3_link_report(tp);
5365 }
5366
5367 return 0;
5368}
5369
Michael Chan747e8f82005-07-25 12:33:22 -07005370static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5371{
5372 int current_link_up, err = 0;
5373 u32 bmsr, bmcr;
5374 u16 current_speed;
5375 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005376 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005377
5378 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5379 tw32_f(MAC_MODE, tp->mac_mode);
5380 udelay(40);
5381
5382 tw32(MAC_EVENT, 0);
5383
5384 tw32_f(MAC_STATUS,
5385 (MAC_STATUS_SYNC_CHANGED |
5386 MAC_STATUS_CFG_CHANGED |
5387 MAC_STATUS_MI_COMPLETION |
5388 MAC_STATUS_LNKSTATE_CHANGED));
5389 udelay(40);
5390
5391 if (force_reset)
5392 tg3_phy_reset(tp);
5393
5394 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005395 current_speed = SPEED_UNKNOWN;
5396 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005397 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005398
5399 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5400 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005401 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005402 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5403 bmsr |= BMSR_LSTATUS;
5404 else
5405 bmsr &= ~BMSR_LSTATUS;
5406 }
Michael Chan747e8f82005-07-25 12:33:22 -07005407
5408 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5409
5410 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005411 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005412 /* do nothing, just check for link up at the end */
5413 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005414 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005415
5416 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005417 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5418 ADVERTISE_1000XPAUSE |
5419 ADVERTISE_1000XPSE_ASYM |
5420 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005421
Matt Carlson28011cf2011-11-16 18:36:59 -05005422 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005423 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005424
Matt Carlson28011cf2011-11-16 18:36:59 -05005425 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5426 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005427 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5428 tg3_writephy(tp, MII_BMCR, bmcr);
5429
5430 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005431 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005432 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005433
5434 return err;
5435 }
5436 } else {
5437 u32 new_bmcr;
5438
5439 bmcr &= ~BMCR_SPEED1000;
5440 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5441
5442 if (tp->link_config.duplex == DUPLEX_FULL)
5443 new_bmcr |= BMCR_FULLDPLX;
5444
5445 if (new_bmcr != bmcr) {
5446 /* BMCR_SPEED1000 is a reserved bit that needs
5447 * to be set on write.
5448 */
5449 new_bmcr |= BMCR_SPEED1000;
5450
5451 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005452 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005453 u32 adv;
5454
5455 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5456 adv &= ~(ADVERTISE_1000XFULL |
5457 ADVERTISE_1000XHALF |
5458 ADVERTISE_SLCT);
5459 tg3_writephy(tp, MII_ADVERTISE, adv);
5460 tg3_writephy(tp, MII_BMCR, bmcr |
5461 BMCR_ANRESTART |
5462 BMCR_ANENABLE);
5463 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005464 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005465 }
5466 tg3_writephy(tp, MII_BMCR, new_bmcr);
5467 bmcr = new_bmcr;
5468 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5469 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005470 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005471 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5472 bmsr |= BMSR_LSTATUS;
5473 else
5474 bmsr &= ~BMSR_LSTATUS;
5475 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005476 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005477 }
5478 }
5479
5480 if (bmsr & BMSR_LSTATUS) {
5481 current_speed = SPEED_1000;
5482 current_link_up = 1;
5483 if (bmcr & BMCR_FULLDPLX)
5484 current_duplex = DUPLEX_FULL;
5485 else
5486 current_duplex = DUPLEX_HALF;
5487
Matt Carlsonef167e22007-12-20 20:10:01 -08005488 local_adv = 0;
5489 remote_adv = 0;
5490
Michael Chan747e8f82005-07-25 12:33:22 -07005491 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005492 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005493
5494 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5495 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5496 common = local_adv & remote_adv;
5497 if (common & (ADVERTISE_1000XHALF |
5498 ADVERTISE_1000XFULL)) {
5499 if (common & ADVERTISE_1000XFULL)
5500 current_duplex = DUPLEX_FULL;
5501 else
5502 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005503
5504 tp->link_config.rmt_adv =
5505 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005506 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005507 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005508 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005509 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005510 }
Michael Chan747e8f82005-07-25 12:33:22 -07005511 }
5512 }
5513
Matt Carlsonef167e22007-12-20 20:10:01 -08005514 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5515 tg3_setup_flow_control(tp, local_adv, remote_adv);
5516
Michael Chan747e8f82005-07-25 12:33:22 -07005517 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5518 if (tp->link_config.active_duplex == DUPLEX_HALF)
5519 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5520
5521 tw32_f(MAC_MODE, tp->mac_mode);
5522 udelay(40);
5523
5524 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5525
5526 tp->link_config.active_speed = current_speed;
5527 tp->link_config.active_duplex = current_duplex;
5528
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005529 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005530 return err;
5531}
5532
5533static void tg3_serdes_parallel_detect(struct tg3 *tp)
5534{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005535 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005536 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005537 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005538 return;
5539 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005540
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005541 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005542 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5543 u32 bmcr;
5544
5545 tg3_readphy(tp, MII_BMCR, &bmcr);
5546 if (bmcr & BMCR_ANENABLE) {
5547 u32 phy1, phy2;
5548
5549 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005550 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5551 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005552
5553 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005554 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5555 MII_TG3_DSP_EXP1_INT_STAT);
5556 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5557 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005558
5559 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5560 /* We have signal detect and not receiving
5561 * config code words, link is up by parallel
5562 * detection.
5563 */
5564
5565 bmcr &= ~BMCR_ANENABLE;
5566 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5567 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005568 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005569 }
5570 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005571 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005572 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005573 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005574 u32 phy2;
5575
5576 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005577 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5578 MII_TG3_DSP_EXP1_INT_STAT);
5579 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005580 if (phy2 & 0x20) {
5581 u32 bmcr;
5582
5583 /* Config code words received, turn on autoneg. */
5584 tg3_readphy(tp, MII_BMCR, &bmcr);
5585 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5586
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005587 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005588
5589 }
5590 }
5591}
5592
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5594{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005595 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 int err;
5597
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005598 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005600 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005601 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005602 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
Joe Perches41535772013-02-16 11:20:04 +00005605 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005606 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005607
5608 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5609 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5610 scale = 65;
5611 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5612 scale = 6;
5613 else
5614 scale = 12;
5615
5616 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5617 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5618 tw32(GRC_MISC_CFG, val);
5619 }
5620
Matt Carlsonf2096f92011-04-05 14:22:48 +00005621 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5622 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00005623 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
5624 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005625 val |= tr32(MAC_TX_LENGTHS) &
5626 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5627 TX_LENGTHS_CNT_DWN_VAL_MSK);
5628
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 if (tp->link_config.active_speed == SPEED_1000 &&
5630 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005631 tw32(MAC_TX_LENGTHS, val |
5632 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005634 tw32(MAC_TX_LENGTHS, val |
5635 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
Joe Perches63c3a662011-04-26 08:12:10 +00005637 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005638 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005640 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 } else {
5642 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5643 }
5644 }
5645
Joe Perches63c3a662011-04-26 08:12:10 +00005646 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005647 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005648 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07005649 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5650 tp->pwrmgmt_thresh;
5651 else
5652 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5653 tw32(PCIE_PWR_MGMT_THRESH, val);
5654 }
5655
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 return err;
5657}
5658
Matt Carlsonbe947302012-12-03 19:36:57 +00005659/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00005660static u64 tg3_refclk_read(struct tg3 *tp)
5661{
5662 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
5663 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
5664}
5665
5666/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00005667static void tg3_refclk_write(struct tg3 *tp, u64 newval)
5668{
5669 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
5670 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
5671 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
5672 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
5673}
5674
Matt Carlson7d41e492012-12-03 19:36:58 +00005675static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
5676static inline void tg3_full_unlock(struct tg3 *tp);
5677static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
5678{
5679 struct tg3 *tp = netdev_priv(dev);
5680
5681 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
5682 SOF_TIMESTAMPING_RX_SOFTWARE |
5683 SOF_TIMESTAMPING_SOFTWARE |
5684 SOF_TIMESTAMPING_TX_HARDWARE |
5685 SOF_TIMESTAMPING_RX_HARDWARE |
5686 SOF_TIMESTAMPING_RAW_HARDWARE;
5687
5688 if (tp->ptp_clock)
5689 info->phc_index = ptp_clock_index(tp->ptp_clock);
5690 else
5691 info->phc_index = -1;
5692
5693 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
5694
5695 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
5696 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
5697 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
5698 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
5699 return 0;
5700}
5701
5702static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
5703{
5704 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5705 bool neg_adj = false;
5706 u32 correction = 0;
5707
5708 if (ppb < 0) {
5709 neg_adj = true;
5710 ppb = -ppb;
5711 }
5712
5713 /* Frequency adjustment is performed using hardware with a 24 bit
5714 * accumulator and a programmable correction value. On each clk, the
5715 * correction value gets added to the accumulator and when it
5716 * overflows, the time counter is incremented/decremented.
5717 *
5718 * So conversion from ppb to correction value is
5719 * ppb * (1 << 24) / 1000000000
5720 */
5721 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
5722 TG3_EAV_REF_CLK_CORRECT_MASK;
5723
5724 tg3_full_lock(tp, 0);
5725
5726 if (correction)
5727 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
5728 TG3_EAV_REF_CLK_CORRECT_EN |
5729 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
5730 else
5731 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
5732
5733 tg3_full_unlock(tp);
5734
5735 return 0;
5736}
5737
5738static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
5739{
5740 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5741
5742 tg3_full_lock(tp, 0);
5743 tp->ptp_adjust += delta;
5744 tg3_full_unlock(tp);
5745
5746 return 0;
5747}
5748
5749static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
5750{
5751 u64 ns;
5752 u32 remainder;
5753 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5754
5755 tg3_full_lock(tp, 0);
5756 ns = tg3_refclk_read(tp);
5757 ns += tp->ptp_adjust;
5758 tg3_full_unlock(tp);
5759
5760 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
5761 ts->tv_nsec = remainder;
5762
5763 return 0;
5764}
5765
5766static int tg3_ptp_settime(struct ptp_clock_info *ptp,
5767 const struct timespec *ts)
5768{
5769 u64 ns;
5770 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5771
5772 ns = timespec_to_ns(ts);
5773
5774 tg3_full_lock(tp, 0);
5775 tg3_refclk_write(tp, ns);
5776 tp->ptp_adjust = 0;
5777 tg3_full_unlock(tp);
5778
5779 return 0;
5780}
5781
5782static int tg3_ptp_enable(struct ptp_clock_info *ptp,
5783 struct ptp_clock_request *rq, int on)
5784{
5785 return -EOPNOTSUPP;
5786}
5787
5788static const struct ptp_clock_info tg3_ptp_caps = {
5789 .owner = THIS_MODULE,
5790 .name = "tg3 clock",
5791 .max_adj = 250000000,
5792 .n_alarm = 0,
5793 .n_ext_ts = 0,
5794 .n_per_out = 0,
5795 .pps = 0,
5796 .adjfreq = tg3_ptp_adjfreq,
5797 .adjtime = tg3_ptp_adjtime,
5798 .gettime = tg3_ptp_gettime,
5799 .settime = tg3_ptp_settime,
5800 .enable = tg3_ptp_enable,
5801};
5802
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005803static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
5804 struct skb_shared_hwtstamps *timestamp)
5805{
5806 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
5807 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
5808 tp->ptp_adjust);
5809}
5810
Matt Carlsonbe947302012-12-03 19:36:57 +00005811/* tp->lock must be held */
5812static void tg3_ptp_init(struct tg3 *tp)
5813{
5814 if (!tg3_flag(tp, PTP_CAPABLE))
5815 return;
5816
5817 /* Initialize the hardware clock to the system time. */
5818 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
5819 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00005820 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00005821}
5822
5823/* tp->lock must be held */
5824static void tg3_ptp_resume(struct tg3 *tp)
5825{
5826 if (!tg3_flag(tp, PTP_CAPABLE))
5827 return;
5828
5829 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
5830 tp->ptp_adjust = 0;
5831}
5832
5833static void tg3_ptp_fini(struct tg3 *tp)
5834{
5835 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
5836 return;
5837
Matt Carlson7d41e492012-12-03 19:36:58 +00005838 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00005839 tp->ptp_clock = NULL;
5840 tp->ptp_adjust = 0;
5841}
5842
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005843static inline int tg3_irq_sync(struct tg3 *tp)
5844{
5845 return tp->irq_sync;
5846}
5847
Matt Carlson97bd8e42011-04-13 11:05:04 +00005848static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5849{
5850 int i;
5851
5852 dst = (u32 *)((u8 *)dst + off);
5853 for (i = 0; i < len; i += sizeof(u32))
5854 *dst++ = tr32(off + i);
5855}
5856
5857static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5858{
5859 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5860 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5861 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5862 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5863 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5864 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5865 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5866 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5867 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5868 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5869 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5870 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5871 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5872 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5873 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5874 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5875 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5876 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5877 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5878
Joe Perches63c3a662011-04-26 08:12:10 +00005879 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005880 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5881
5882 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5883 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5884 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5885 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5886 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5887 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5888 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5889 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5890
Joe Perches63c3a662011-04-26 08:12:10 +00005891 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005892 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5893 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5894 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5895 }
5896
5897 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5898 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5899 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5900 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5901 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5902
Joe Perches63c3a662011-04-26 08:12:10 +00005903 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005904 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5905}
5906
5907static void tg3_dump_state(struct tg3 *tp)
5908{
5909 int i;
5910 u32 *regs;
5911
5912 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00005913 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00005914 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00005915
Joe Perches63c3a662011-04-26 08:12:10 +00005916 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005917 /* Read up to but not including private PCI registers */
5918 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5919 regs[i / sizeof(u32)] = tr32(i);
5920 } else
5921 tg3_dump_legacy_regs(tp, regs);
5922
5923 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5924 if (!regs[i + 0] && !regs[i + 1] &&
5925 !regs[i + 2] && !regs[i + 3])
5926 continue;
5927
5928 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5929 i * 4,
5930 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5931 }
5932
5933 kfree(regs);
5934
5935 for (i = 0; i < tp->irq_cnt; i++) {
5936 struct tg3_napi *tnapi = &tp->napi[i];
5937
5938 /* SW status block */
5939 netdev_err(tp->dev,
5940 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5941 i,
5942 tnapi->hw_status->status,
5943 tnapi->hw_status->status_tag,
5944 tnapi->hw_status->rx_jumbo_consumer,
5945 tnapi->hw_status->rx_consumer,
5946 tnapi->hw_status->rx_mini_consumer,
5947 tnapi->hw_status->idx[0].rx_producer,
5948 tnapi->hw_status->idx[0].tx_consumer);
5949
5950 netdev_err(tp->dev,
5951 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5952 i,
5953 tnapi->last_tag, tnapi->last_irq_tag,
5954 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5955 tnapi->rx_rcb_ptr,
5956 tnapi->prodring.rx_std_prod_idx,
5957 tnapi->prodring.rx_std_cons_idx,
5958 tnapi->prodring.rx_jmb_prod_idx,
5959 tnapi->prodring.rx_jmb_cons_idx);
5960 }
5961}
5962
Michael Chandf3e6542006-05-26 17:48:07 -07005963/* This is called whenever we suspect that the system chipset is re-
5964 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5965 * is bogus tx completions. We try to recover by setting the
5966 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5967 * in the workqueue.
5968 */
5969static void tg3_tx_recover(struct tg3 *tp)
5970{
Joe Perches63c3a662011-04-26 08:12:10 +00005971 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005972 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5973
Matt Carlson5129c3a2010-04-05 10:19:23 +00005974 netdev_warn(tp->dev,
5975 "The system may be re-ordering memory-mapped I/O "
5976 "cycles to the network device, attempting to recover. "
5977 "Please report the problem to the driver maintainer "
5978 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005979
5980 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005981 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005982 spin_unlock(&tp->lock);
5983}
5984
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005985static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005986{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005987 /* Tell compiler to fetch tx indices from memory. */
5988 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005989 return tnapi->tx_pending -
5990 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005991}
5992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993/* Tigon3 never reports partial packet sends. So we do not
5994 * need special logic to handle SKBs that have not had all
5995 * of their frags sent yet, like SunGEM does.
5996 */
Matt Carlson17375d22009-08-28 14:02:18 +00005997static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998{
Matt Carlson17375d22009-08-28 14:02:18 +00005999 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006000 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006001 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006002 struct netdev_queue *txq;
6003 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006004 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006005
Joe Perches63c3a662011-04-26 08:12:10 +00006006 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006007 index--;
6008
6009 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
6011 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006012 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006014 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
Michael Chandf3e6542006-05-26 17:48:07 -07006016 if (unlikely(skb == NULL)) {
6017 tg3_tx_recover(tp);
6018 return;
6019 }
6020
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006021 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6022 struct skb_shared_hwtstamps timestamp;
6023 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6024 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6025
6026 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6027
6028 skb_tstamp_tx(skb, &timestamp);
6029 }
6030
Alexander Duyckf4188d82009-12-02 16:48:38 +00006031 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006032 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006033 skb_headlen(skb),
6034 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
6036 ri->skb = NULL;
6037
Matt Carlsone01ee142011-07-27 14:20:50 +00006038 while (ri->fragmented) {
6039 ri->fragmented = false;
6040 sw_idx = NEXT_TX(sw_idx);
6041 ri = &tnapi->tx_buffers[sw_idx];
6042 }
6043
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 sw_idx = NEXT_TX(sw_idx);
6045
6046 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006047 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006048 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6049 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006050
6051 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006052 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006053 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006054 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006055
6056 while (ri->fragmented) {
6057 ri->fragmented = false;
6058 sw_idx = NEXT_TX(sw_idx);
6059 ri = &tnapi->tx_buffers[sw_idx];
6060 }
6061
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 sw_idx = NEXT_TX(sw_idx);
6063 }
6064
Tom Herbert298376d2011-11-28 16:33:30 +00006065 pkts_compl++;
6066 bytes_compl += skb->len;
6067
David S. Millerf47c11e2005-06-24 20:18:35 -07006068 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006069
6070 if (unlikely(tx_bug)) {
6071 tg3_tx_recover(tp);
6072 return;
6073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 }
6075
Tom Herbert5cb917b2012-03-05 19:53:50 +00006076 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006077
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006078 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
Michael Chan1b2a7202006-08-07 21:46:02 -07006080 /* Need to make the tx_cons update visible to tg3_start_xmit()
6081 * before checking for netif_queue_stopped(). Without the
6082 * memory barrier, there is a small possibility that tg3_start_xmit()
6083 * will miss it and cause the queue to be stopped forever.
6084 */
6085 smp_mb();
6086
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006087 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006088 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006089 __netif_tx_lock(txq, smp_processor_id());
6090 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006091 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006092 netif_tx_wake_queue(txq);
6093 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095}
6096
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006097static void tg3_frag_free(bool is_frag, void *data)
6098{
6099 if (is_frag)
6100 put_page(virt_to_head_page(data));
6101 else
6102 kfree(data);
6103}
6104
Eric Dumazet9205fd92011-11-18 06:47:01 +00006105static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006106{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006107 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6108 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6109
Eric Dumazet9205fd92011-11-18 06:47:01 +00006110 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006111 return;
6112
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006113 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006114 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006115 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006116 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006117}
6118
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006119
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120/* Returns size of skb allocated or < 0 on error.
6121 *
6122 * We only need to fill in the address because the other members
6123 * of the RX descriptor are invariant, see tg3_init_rings.
6124 *
6125 * Note the purposeful assymetry of cpu vs. chip accesses. For
6126 * posting buffers we only dirty the first cache line of the RX
6127 * descriptor (containing the address). Whereas for the RX status
6128 * buffers the cpu only reads the last cacheline of the RX descriptor
6129 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6130 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006131static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006132 u32 opaque_key, u32 dest_idx_unmasked,
6133 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134{
6135 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006136 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006137 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006139 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 switch (opaque_key) {
6142 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006143 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006144 desc = &tpr->rx_std[dest_idx];
6145 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006146 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 break;
6148
6149 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006150 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006151 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006152 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006153 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 break;
6155
6156 default:
6157 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
6160 /* Do not overwrite any of the map or rp information
6161 * until we are sure we can commit to a new buffer.
6162 *
6163 * Callers depend upon this behavior and assume that
6164 * we leave everything unchanged if we fail.
6165 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006166 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6167 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006168 if (skb_size <= PAGE_SIZE) {
6169 data = netdev_alloc_frag(skb_size);
6170 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006171 } else {
6172 data = kmalloc(skb_size, GFP_ATOMIC);
6173 *frag_size = 0;
6174 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006175 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 return -ENOMEM;
6177
Eric Dumazet9205fd92011-11-18 06:47:01 +00006178 mapping = pci_map_single(tp->pdev,
6179 data + TG3_RX_OFFSET(tp),
6180 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006182 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006183 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006184 return -EIO;
6185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
Eric Dumazet9205fd92011-11-18 06:47:01 +00006187 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006188 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 desc->addr_hi = ((u64)mapping >> 32);
6191 desc->addr_lo = ((u64)mapping & 0xffffffff);
6192
Eric Dumazet9205fd92011-11-18 06:47:01 +00006193 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194}
6195
6196/* We only need to move over in the address because the other
6197 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006198 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 */
Matt Carlsona3896162009-11-13 13:03:44 +00006200static void tg3_recycle_rx(struct tg3_napi *tnapi,
6201 struct tg3_rx_prodring_set *dpr,
6202 u32 opaque_key, int src_idx,
6203 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204{
Matt Carlson17375d22009-08-28 14:02:18 +00006205 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6207 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006208 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006209 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
6211 switch (opaque_key) {
6212 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006213 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006214 dest_desc = &dpr->rx_std[dest_idx];
6215 dest_map = &dpr->rx_std_buffers[dest_idx];
6216 src_desc = &spr->rx_std[src_idx];
6217 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 break;
6219
6220 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006221 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006222 dest_desc = &dpr->rx_jmb[dest_idx].std;
6223 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6224 src_desc = &spr->rx_jmb[src_idx].std;
6225 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 break;
6227
6228 default:
6229 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231
Eric Dumazet9205fd92011-11-18 06:47:01 +00006232 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006233 dma_unmap_addr_set(dest_map, mapping,
6234 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 dest_desc->addr_hi = src_desc->addr_hi;
6236 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006237
6238 /* Ensure that the update to the skb happens after the physical
6239 * addresses have been transferred to the new BD location.
6240 */
6241 smp_wmb();
6242
Eric Dumazet9205fd92011-11-18 06:47:01 +00006243 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244}
6245
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246/* The RX ring scheme is composed of multiple rings which post fresh
6247 * buffers to the chip, and one special ring the chip uses to report
6248 * status back to the host.
6249 *
6250 * The special ring reports the status of received packets to the
6251 * host. The chip does not write into the original descriptor the
6252 * RX buffer was obtained from. The chip simply takes the original
6253 * descriptor as provided by the host, updates the status and length
6254 * field, then writes this into the next status ring entry.
6255 *
6256 * Each ring the host uses to post buffers to the chip is described
6257 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6258 * it is first placed into the on-chip ram. When the packet's length
6259 * is known, it walks down the TG3_BDINFO entries to select the ring.
6260 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6261 * which is within the range of the new packet's length is chosen.
6262 *
6263 * The "separate ring for rx status" scheme may sound queer, but it makes
6264 * sense from a cache coherency perspective. If only the host writes
6265 * to the buffer post rings, and only the chip writes to the rx status
6266 * rings, then cache lines never move beyond shared-modified state.
6267 * If both the host and chip were to write into the same ring, cache line
6268 * eviction could occur since both entities want it in an exclusive state.
6269 */
Matt Carlson17375d22009-08-28 14:02:18 +00006270static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271{
Matt Carlson17375d22009-08-28 14:02:18 +00006272 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006273 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006274 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006275 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006276 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006278 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006280 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 /*
6282 * We need to order the read of hw_idx and the read of
6283 * the opaque cookie.
6284 */
6285 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 work_mask = 0;
6287 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006288 std_prod_idx = tpr->rx_std_prod_idx;
6289 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006291 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006292 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 unsigned int len;
6294 struct sk_buff *skb;
6295 dma_addr_t dma_addr;
6296 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006297 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006298 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
6300 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6301 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6302 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006303 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006304 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006305 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006306 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006307 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006309 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006310 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006311 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006312 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006313 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
6316 work_mask |= opaque_key;
6317
6318 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6319 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6320 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006321 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 desc_idx, *post_ptr);
6323 drop_it_no_recycle:
6324 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006325 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 goto next_pkt;
6327 }
6328
Eric Dumazet9205fd92011-11-18 06:47:01 +00006329 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006330 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6331 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006333 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6334 RXD_FLAG_PTPSTAT_PTPV1 ||
6335 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6336 RXD_FLAG_PTPSTAT_PTPV2) {
6337 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6338 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6339 }
6340
Matt Carlsond2757fc2010-04-12 06:58:27 +00006341 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006343 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
Eric Dumazet9205fd92011-11-18 06:47:01 +00006345 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006346 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 if (skb_size < 0)
6348 goto drop_it;
6349
Matt Carlson287be122009-08-28 13:58:46 +00006350 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 PCI_DMA_FROMDEVICE);
6352
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006353 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006354 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006355 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006356 goto drop_it_no_recycle;
6357 }
6358 skb_reserve(skb, TG3_RX_OFFSET(tp));
6359 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006360 * after the usage of the old DMA mapping.
6361 */
6362 smp_wmb();
6363
Eric Dumazet9205fd92011-11-18 06:47:01 +00006364 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006365
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006367 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 desc_idx, *post_ptr);
6369
Eric Dumazet9205fd92011-11-18 06:47:01 +00006370 skb = netdev_alloc_skb(tp->dev,
6371 len + TG3_RAW_IP_ALIGN);
6372 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 goto drop_it_no_recycle;
6374
Eric Dumazet9205fd92011-11-18 06:47:01 +00006375 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006377 memcpy(skb->data,
6378 data + TG3_RX_OFFSET(tp),
6379 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 }
6382
Eric Dumazet9205fd92011-11-18 06:47:01 +00006383 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006384 if (tstamp)
6385 tg3_hwclock_to_timestamp(tp, tstamp,
6386 skb_hwtstamps(skb));
6387
Michał Mirosławdc668912011-04-07 03:35:07 +00006388 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6390 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6391 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6392 skb->ip_summed = CHECKSUM_UNNECESSARY;
6393 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006394 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
6396 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006397
6398 if (len > (tp->dev->mtu + ETH_HLEN) &&
6399 skb->protocol != htons(ETH_P_8021Q)) {
6400 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006401 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006402 }
6403
Matt Carlson9dc7a112010-04-12 06:58:28 +00006404 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006405 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
6406 __vlan_hwaccel_put_tag(skb,
6407 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006408
Matt Carlsonbf933c82011-01-25 15:58:49 +00006409 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 received++;
6412 budget--;
6413
6414next_pkt:
6415 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006416
6417 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006418 tpr->rx_std_prod_idx = std_prod_idx &
6419 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006420 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6421 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006422 work_mask &= ~RXD_OPAQUE_RING_STD;
6423 rx_std_posted = 0;
6424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006426 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006427 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006428
6429 /* Refresh hw_idx to see if there is new work */
6430 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006431 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006432 rmb();
6433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 }
6435
6436 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006437 tnapi->rx_rcb_ptr = sw_idx;
6438 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439
6440 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006441 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006442 /* Sync BD data before updating mailbox */
6443 wmb();
6444
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006445 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006446 tpr->rx_std_prod_idx = std_prod_idx &
6447 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006448 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6449 tpr->rx_std_prod_idx);
6450 }
6451 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006452 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6453 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006454 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6455 tpr->rx_jmb_prod_idx);
6456 }
6457 mmiowb();
6458 } else if (work_mask) {
6459 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6460 * updated before the producer indices can be updated.
6461 */
6462 smp_wmb();
6463
Matt Carlson2c49a442010-09-30 10:34:35 +00006464 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6465 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006466
Michael Chan7ae52892012-03-21 15:38:33 +00006467 if (tnapi != &tp->napi[1]) {
6468 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006469 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472
6473 return received;
6474}
6475
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006476static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006479 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006480 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6481
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 if (sblk->status & SD_STATUS_LINK_CHG) {
6483 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006484 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006485 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006486 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006487 tw32_f(MAC_STATUS,
6488 (MAC_STATUS_SYNC_CHANGED |
6489 MAC_STATUS_CFG_CHANGED |
6490 MAC_STATUS_MI_COMPLETION |
6491 MAC_STATUS_LNKSTATE_CHANGED));
6492 udelay(40);
6493 } else
6494 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006495 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 }
6497 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006498}
6499
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006500static int tg3_rx_prodring_xfer(struct tg3 *tp,
6501 struct tg3_rx_prodring_set *dpr,
6502 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006503{
6504 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006505 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006506
6507 while (1) {
6508 src_prod_idx = spr->rx_std_prod_idx;
6509
6510 /* Make sure updates to the rx_std_buffers[] entries and the
6511 * standard producer index are seen in the correct order.
6512 */
6513 smp_rmb();
6514
6515 if (spr->rx_std_cons_idx == src_prod_idx)
6516 break;
6517
6518 if (spr->rx_std_cons_idx < src_prod_idx)
6519 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6520 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006521 cpycnt = tp->rx_std_ring_mask + 1 -
6522 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006523
Matt Carlson2c49a442010-09-30 10:34:35 +00006524 cpycnt = min(cpycnt,
6525 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006526
6527 si = spr->rx_std_cons_idx;
6528 di = dpr->rx_std_prod_idx;
6529
Matt Carlsone92967b2010-02-12 14:47:06 +00006530 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006531 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006532 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006533 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006534 break;
6535 }
6536 }
6537
6538 if (!cpycnt)
6539 break;
6540
6541 /* Ensure that updates to the rx_std_buffers ring and the
6542 * shadowed hardware producer ring from tg3_recycle_skb() are
6543 * ordered correctly WRT the skb check above.
6544 */
6545 smp_rmb();
6546
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006547 memcpy(&dpr->rx_std_buffers[di],
6548 &spr->rx_std_buffers[si],
6549 cpycnt * sizeof(struct ring_info));
6550
6551 for (i = 0; i < cpycnt; i++, di++, si++) {
6552 struct tg3_rx_buffer_desc *sbd, *dbd;
6553 sbd = &spr->rx_std[si];
6554 dbd = &dpr->rx_std[di];
6555 dbd->addr_hi = sbd->addr_hi;
6556 dbd->addr_lo = sbd->addr_lo;
6557 }
6558
Matt Carlson2c49a442010-09-30 10:34:35 +00006559 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6560 tp->rx_std_ring_mask;
6561 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6562 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006563 }
6564
6565 while (1) {
6566 src_prod_idx = spr->rx_jmb_prod_idx;
6567
6568 /* Make sure updates to the rx_jmb_buffers[] entries and
6569 * the jumbo producer index are seen in the correct order.
6570 */
6571 smp_rmb();
6572
6573 if (spr->rx_jmb_cons_idx == src_prod_idx)
6574 break;
6575
6576 if (spr->rx_jmb_cons_idx < src_prod_idx)
6577 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6578 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006579 cpycnt = tp->rx_jmb_ring_mask + 1 -
6580 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006581
6582 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006583 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006584
6585 si = spr->rx_jmb_cons_idx;
6586 di = dpr->rx_jmb_prod_idx;
6587
Matt Carlsone92967b2010-02-12 14:47:06 +00006588 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006589 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006590 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006591 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006592 break;
6593 }
6594 }
6595
6596 if (!cpycnt)
6597 break;
6598
6599 /* Ensure that updates to the rx_jmb_buffers ring and the
6600 * shadowed hardware producer ring from tg3_recycle_skb() are
6601 * ordered correctly WRT the skb check above.
6602 */
6603 smp_rmb();
6604
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006605 memcpy(&dpr->rx_jmb_buffers[di],
6606 &spr->rx_jmb_buffers[si],
6607 cpycnt * sizeof(struct ring_info));
6608
6609 for (i = 0; i < cpycnt; i++, di++, si++) {
6610 struct tg3_rx_buffer_desc *sbd, *dbd;
6611 sbd = &spr->rx_jmb[si].std;
6612 dbd = &dpr->rx_jmb[di].std;
6613 dbd->addr_hi = sbd->addr_hi;
6614 dbd->addr_lo = sbd->addr_lo;
6615 }
6616
Matt Carlson2c49a442010-09-30 10:34:35 +00006617 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6618 tp->rx_jmb_ring_mask;
6619 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6620 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006621 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006622
6623 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006624}
6625
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006626static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6627{
6628 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629
6630 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006631 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006632 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006633 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006634 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 }
6636
Matt Carlsonf891ea12012-04-24 13:37:01 +00006637 if (!tnapi->rx_rcb_prod_idx)
6638 return work_done;
6639
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 /* run RX thread, within the bounds set by NAPI.
6641 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006642 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006644 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006645 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646
Joe Perches63c3a662011-04-26 08:12:10 +00006647 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006648 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006649 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006650 u32 std_prod_idx = dpr->rx_std_prod_idx;
6651 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006652
Michael Chan7ae52892012-03-21 15:38:33 +00006653 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00006654 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006655 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006656 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006657
6658 wmb();
6659
Matt Carlsone4af1af2010-02-12 14:47:05 +00006660 if (std_prod_idx != dpr->rx_std_prod_idx)
6661 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6662 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006663
Matt Carlsone4af1af2010-02-12 14:47:05 +00006664 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6665 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6666 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006667
6668 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006669
6670 if (err)
6671 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006672 }
6673
David S. Miller6f535762007-10-11 18:08:29 -07006674 return work_done;
6675}
David S. Millerf7383c22005-05-18 22:50:53 -07006676
Matt Carlsondb219972011-11-04 09:15:03 +00006677static inline void tg3_reset_task_schedule(struct tg3 *tp)
6678{
6679 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6680 schedule_work(&tp->reset_task);
6681}
6682
6683static inline void tg3_reset_task_cancel(struct tg3 *tp)
6684{
6685 cancel_work_sync(&tp->reset_task);
6686 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006687 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006688}
6689
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006690static int tg3_poll_msix(struct napi_struct *napi, int budget)
6691{
6692 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6693 struct tg3 *tp = tnapi->tp;
6694 int work_done = 0;
6695 struct tg3_hw_status *sblk = tnapi->hw_status;
6696
6697 while (1) {
6698 work_done = tg3_poll_work(tnapi, work_done, budget);
6699
Joe Perches63c3a662011-04-26 08:12:10 +00006700 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006701 goto tx_recovery;
6702
6703 if (unlikely(work_done >= budget))
6704 break;
6705
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006706 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006707 * to tell the hw how much work has been processed,
6708 * so we must read it before checking for more work.
6709 */
6710 tnapi->last_tag = sblk->status_tag;
6711 tnapi->last_irq_tag = tnapi->last_tag;
6712 rmb();
6713
6714 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006715 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6716 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006717
6718 /* This test here is not race free, but will reduce
6719 * the number of interrupts by looping again.
6720 */
6721 if (tnapi == &tp->napi[1] && tp->rx_refill)
6722 continue;
6723
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006724 napi_complete(napi);
6725 /* Reenable interrupts. */
6726 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006727
6728 /* This test here is synchronized by napi_schedule()
6729 * and napi_complete() to close the race condition.
6730 */
6731 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6732 tw32(HOSTCC_MODE, tp->coalesce_mode |
6733 HOSTCC_MODE_ENABLE |
6734 tnapi->coal_now);
6735 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006736 mmiowb();
6737 break;
6738 }
6739 }
6740
6741 return work_done;
6742
6743tx_recovery:
6744 /* work_done is guaranteed to be less than budget. */
6745 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006746 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006747 return work_done;
6748}
6749
Matt Carlsone64de4e2011-04-13 11:05:05 +00006750static void tg3_process_error(struct tg3 *tp)
6751{
6752 u32 val;
6753 bool real_error = false;
6754
Joe Perches63c3a662011-04-26 08:12:10 +00006755 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006756 return;
6757
6758 /* Check Flow Attention register */
6759 val = tr32(HOSTCC_FLOW_ATTN);
6760 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6761 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6762 real_error = true;
6763 }
6764
6765 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6766 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6767 real_error = true;
6768 }
6769
6770 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6771 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6772 real_error = true;
6773 }
6774
6775 if (!real_error)
6776 return;
6777
6778 tg3_dump_state(tp);
6779
Joe Perches63c3a662011-04-26 08:12:10 +00006780 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006781 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006782}
6783
David S. Miller6f535762007-10-11 18:08:29 -07006784static int tg3_poll(struct napi_struct *napi, int budget)
6785{
Matt Carlson8ef04422009-08-28 14:01:37 +00006786 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6787 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006788 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006789 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006790
6791 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006792 if (sblk->status & SD_STATUS_ERROR)
6793 tg3_process_error(tp);
6794
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006795 tg3_poll_link(tp);
6796
Matt Carlson17375d22009-08-28 14:02:18 +00006797 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006798
Joe Perches63c3a662011-04-26 08:12:10 +00006799 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006800 goto tx_recovery;
6801
6802 if (unlikely(work_done >= budget))
6803 break;
6804
Joe Perches63c3a662011-04-26 08:12:10 +00006805 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006806 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006807 * to tell the hw how much work has been processed,
6808 * so we must read it before checking for more work.
6809 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006810 tnapi->last_tag = sblk->status_tag;
6811 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006812 rmb();
6813 } else
6814 sblk->status &= ~SD_STATUS_UPDATED;
6815
Matt Carlson17375d22009-08-28 14:02:18 +00006816 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006817 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006818 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006819 break;
6820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 }
6822
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006823 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006824
6825tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006826 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006827 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006828 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006829 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830}
6831
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006832static void tg3_napi_disable(struct tg3 *tp)
6833{
6834 int i;
6835
6836 for (i = tp->irq_cnt - 1; i >= 0; i--)
6837 napi_disable(&tp->napi[i].napi);
6838}
6839
6840static void tg3_napi_enable(struct tg3 *tp)
6841{
6842 int i;
6843
6844 for (i = 0; i < tp->irq_cnt; i++)
6845 napi_enable(&tp->napi[i].napi);
6846}
6847
6848static void tg3_napi_init(struct tg3 *tp)
6849{
6850 int i;
6851
6852 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6853 for (i = 1; i < tp->irq_cnt; i++)
6854 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6855}
6856
6857static void tg3_napi_fini(struct tg3 *tp)
6858{
6859 int i;
6860
6861 for (i = 0; i < tp->irq_cnt; i++)
6862 netif_napi_del(&tp->napi[i].napi);
6863}
6864
6865static inline void tg3_netif_stop(struct tg3 *tp)
6866{
6867 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6868 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006869 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006870 netif_tx_disable(tp->dev);
6871}
6872
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00006873/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006874static inline void tg3_netif_start(struct tg3 *tp)
6875{
Matt Carlsonbe947302012-12-03 19:36:57 +00006876 tg3_ptp_resume(tp);
6877
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006878 /* NOTE: unconditional netif_tx_wake_all_queues is only
6879 * appropriate so long as all callers are assured to
6880 * have free tx slots (such as after tg3_init_hw)
6881 */
6882 netif_tx_wake_all_queues(tp->dev);
6883
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006884 if (tp->link_up)
6885 netif_carrier_on(tp->dev);
6886
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006887 tg3_napi_enable(tp);
6888 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6889 tg3_enable_ints(tp);
6890}
6891
David S. Millerf47c11e2005-06-24 20:18:35 -07006892static void tg3_irq_quiesce(struct tg3 *tp)
6893{
Matt Carlson4f125f42009-09-01 12:55:02 +00006894 int i;
6895
David S. Millerf47c11e2005-06-24 20:18:35 -07006896 BUG_ON(tp->irq_sync);
6897
6898 tp->irq_sync = 1;
6899 smp_mb();
6900
Matt Carlson4f125f42009-09-01 12:55:02 +00006901 for (i = 0; i < tp->irq_cnt; i++)
6902 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006903}
6904
David S. Millerf47c11e2005-06-24 20:18:35 -07006905/* Fully shutdown all tg3 driver activity elsewhere in the system.
6906 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6907 * with as well. Most of the time, this is not necessary except when
6908 * shutting down the device.
6909 */
6910static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6911{
Michael Chan46966542007-07-11 19:47:19 -07006912 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006913 if (irq_sync)
6914 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006915}
6916
6917static inline void tg3_full_unlock(struct tg3 *tp)
6918{
David S. Millerf47c11e2005-06-24 20:18:35 -07006919 spin_unlock_bh(&tp->lock);
6920}
6921
Michael Chanfcfa0a32006-03-20 22:28:41 -08006922/* One-shot MSI handler - Chip automatically disables interrupt
6923 * after sending MSI so driver doesn't have to do it.
6924 */
David Howells7d12e782006-10-05 14:55:46 +01006925static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006926{
Matt Carlson09943a12009-08-28 14:01:57 +00006927 struct tg3_napi *tnapi = dev_id;
6928 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006929
Matt Carlson898a56f2009-08-28 14:02:40 +00006930 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006931 if (tnapi->rx_rcb)
6932 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006933
6934 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006935 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006936
6937 return IRQ_HANDLED;
6938}
6939
Michael Chan88b06bc22005-04-21 17:13:25 -07006940/* MSI ISR - No need to check for interrupt sharing and no need to
6941 * flush status block and interrupt mailbox. PCI ordering rules
6942 * guarantee that MSI will arrive after the status block.
6943 */
David Howells7d12e782006-10-05 14:55:46 +01006944static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006945{
Matt Carlson09943a12009-08-28 14:01:57 +00006946 struct tg3_napi *tnapi = dev_id;
6947 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006948
Matt Carlson898a56f2009-08-28 14:02:40 +00006949 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006950 if (tnapi->rx_rcb)
6951 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006952 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006953 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006954 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006955 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006956 * NIC to stop sending us irqs, engaging "in-intr-handler"
6957 * event coalescing.
6958 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006959 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006960 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006961 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006962
Michael Chan88b06bc22005-04-21 17:13:25 -07006963 return IRQ_RETVAL(1);
6964}
6965
David Howells7d12e782006-10-05 14:55:46 +01006966static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967{
Matt Carlson09943a12009-08-28 14:01:57 +00006968 struct tg3_napi *tnapi = dev_id;
6969 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006970 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 unsigned int handled = 1;
6972
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 /* In INTx mode, it is possible for the interrupt to arrive at
6974 * the CPU before the status block posted prior to the interrupt.
6975 * Reading the PCI State register will confirm whether the
6976 * interrupt is ours and will flush the status block.
6977 */
Michael Chand18edcb2007-03-24 20:57:11 -07006978 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006979 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006980 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6981 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006982 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006983 }
Michael Chand18edcb2007-03-24 20:57:11 -07006984 }
6985
6986 /*
6987 * Writing any value to intr-mbox-0 clears PCI INTA# and
6988 * chip-internal interrupt pending events.
6989 * Writing non-zero to intr-mbox-0 additional tells the
6990 * NIC to stop sending us irqs, engaging "in-intr-handler"
6991 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006992 *
6993 * Flush the mailbox to de-assert the IRQ immediately to prevent
6994 * spurious interrupts. The flush impacts performance but
6995 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006996 */
Michael Chanc04cb342007-05-07 00:26:15 -07006997 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006998 if (tg3_irq_sync(tp))
6999 goto out;
7000 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007001 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007002 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007003 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007004 } else {
7005 /* No work, shared interrupt perhaps? re-enable
7006 * interrupts, and flush that PCI write
7007 */
7008 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7009 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007010 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007011out:
David S. Millerfac9b832005-05-18 22:46:34 -07007012 return IRQ_RETVAL(handled);
7013}
7014
David Howells7d12e782006-10-05 14:55:46 +01007015static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007016{
Matt Carlson09943a12009-08-28 14:01:57 +00007017 struct tg3_napi *tnapi = dev_id;
7018 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007019 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007020 unsigned int handled = 1;
7021
David S. Millerfac9b832005-05-18 22:46:34 -07007022 /* In INTx mode, it is possible for the interrupt to arrive at
7023 * the CPU before the status block posted prior to the interrupt.
7024 * Reading the PCI State register will confirm whether the
7025 * interrupt is ours and will flush the status block.
7026 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007027 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007028 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007029 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7030 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007031 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 }
Michael Chand18edcb2007-03-24 20:57:11 -07007033 }
7034
7035 /*
7036 * writing any value to intr-mbox-0 clears PCI INTA# and
7037 * chip-internal interrupt pending events.
7038 * writing non-zero to intr-mbox-0 additional tells the
7039 * NIC to stop sending us irqs, engaging "in-intr-handler"
7040 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007041 *
7042 * Flush the mailbox to de-assert the IRQ immediately to prevent
7043 * spurious interrupts. The flush impacts performance but
7044 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007045 */
Michael Chanc04cb342007-05-07 00:26:15 -07007046 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007047
7048 /*
7049 * In a shared interrupt configuration, sometimes other devices'
7050 * interrupts will scream. We record the current status tag here
7051 * so that the above check can report that the screaming interrupts
7052 * are unhandled. Eventually they will be silenced.
7053 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007054 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007055
Michael Chand18edcb2007-03-24 20:57:11 -07007056 if (tg3_irq_sync(tp))
7057 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007058
Matt Carlson72334482009-08-28 14:03:01 +00007059 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007060
Matt Carlson09943a12009-08-28 14:01:57 +00007061 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007062
David S. Millerf47c11e2005-06-24 20:18:35 -07007063out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 return IRQ_RETVAL(handled);
7065}
7066
Michael Chan79381092005-04-21 17:13:59 -07007067/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007068static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007069{
Matt Carlson09943a12009-08-28 14:01:57 +00007070 struct tg3_napi *tnapi = dev_id;
7071 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007072 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007073
Michael Chanf9804dd2005-09-27 12:13:10 -07007074 if ((sblk->status & SD_STATUS_UPDATED) ||
7075 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007076 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007077 return IRQ_RETVAL(1);
7078 }
7079 return IRQ_RETVAL(0);
7080}
7081
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082#ifdef CONFIG_NET_POLL_CONTROLLER
7083static void tg3_poll_controller(struct net_device *dev)
7084{
Matt Carlson4f125f42009-09-01 12:55:02 +00007085 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007086 struct tg3 *tp = netdev_priv(dev);
7087
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007088 if (tg3_irq_sync(tp))
7089 return;
7090
Matt Carlson4f125f42009-09-01 12:55:02 +00007091 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007092 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093}
7094#endif
7095
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096static void tg3_tx_timeout(struct net_device *dev)
7097{
7098 struct tg3 *tp = netdev_priv(dev);
7099
Michael Chanb0408752007-02-13 12:18:30 -08007100 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007101 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007102 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104
Matt Carlsondb219972011-11-04 09:15:03 +00007105 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106}
7107
Michael Chanc58ec932005-09-17 00:46:27 -07007108/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7109static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7110{
7111 u32 base = (u32) mapping & 0xffffffff;
7112
Eric Dumazet807540b2010-09-23 05:40:09 +00007113 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007114}
7115
Michael Chan72f2afb2006-03-06 19:28:35 -08007116/* Test for DMA addresses > 40-bit */
7117static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7118 int len)
7119{
7120#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007121 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007122 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007123 return 0;
7124#else
7125 return 0;
7126#endif
7127}
7128
Matt Carlsond1a3b732011-07-27 14:20:51 +00007129static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007130 dma_addr_t mapping, u32 len, u32 flags,
7131 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007132{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007133 txbd->addr_hi = ((u64) mapping >> 32);
7134 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7135 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7136 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007137}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138
Matt Carlson84b67b22011-07-27 14:20:52 +00007139static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007140 dma_addr_t map, u32 len, u32 flags,
7141 u32 mss, u32 vlan)
7142{
7143 struct tg3 *tp = tnapi->tp;
7144 bool hwbug = false;
7145
7146 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007147 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007148
7149 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007150 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007151
7152 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007153 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007154
Matt Carlsona4cb4282011-12-14 11:09:58 +00007155 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007156 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007157 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007158 while (len > tp->dma_limit && *budget) {
7159 u32 frag_len = tp->dma_limit;
7160 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007161
Matt Carlsonb9e45482011-11-04 09:14:59 +00007162 /* Avoid the 8byte DMA problem */
7163 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007164 len += tp->dma_limit / 2;
7165 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007166 }
7167
Matt Carlsonb9e45482011-11-04 09:14:59 +00007168 tnapi->tx_buffers[*entry].fragmented = true;
7169
7170 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7171 frag_len, tmp_flag, mss, vlan);
7172 *budget -= 1;
7173 prvidx = *entry;
7174 *entry = NEXT_TX(*entry);
7175
Matt Carlsone31aa982011-07-27 14:20:53 +00007176 map += frag_len;
7177 }
7178
7179 if (len) {
7180 if (*budget) {
7181 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7182 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007183 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007184 *entry = NEXT_TX(*entry);
7185 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007186 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007187 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007188 }
7189 }
7190 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007191 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7192 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007193 *entry = NEXT_TX(*entry);
7194 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007195
7196 return hwbug;
7197}
7198
Matt Carlson0d681b22011-07-27 14:20:49 +00007199static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007200{
7201 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007202 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007203 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007204
Matt Carlson0d681b22011-07-27 14:20:49 +00007205 skb = txb->skb;
7206 txb->skb = NULL;
7207
Matt Carlson432aa7e2011-05-19 12:12:45 +00007208 pci_unmap_single(tnapi->tp->pdev,
7209 dma_unmap_addr(txb, mapping),
7210 skb_headlen(skb),
7211 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007212
7213 while (txb->fragmented) {
7214 txb->fragmented = false;
7215 entry = NEXT_TX(entry);
7216 txb = &tnapi->tx_buffers[entry];
7217 }
7218
Matt Carlsonba1142e2011-11-04 09:15:00 +00007219 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007220 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007221
7222 entry = NEXT_TX(entry);
7223 txb = &tnapi->tx_buffers[entry];
7224
7225 pci_unmap_page(tnapi->tp->pdev,
7226 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007227 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007228
7229 while (txb->fragmented) {
7230 txb->fragmented = false;
7231 entry = NEXT_TX(entry);
7232 txb = &tnapi->tx_buffers[entry];
7233 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007234 }
7235}
7236
Michael Chan72f2afb2006-03-06 19:28:35 -08007237/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007238static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007239 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007240 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007241 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007243 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007244 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007245 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007246 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247
Joe Perches41535772013-02-16 11:20:04 +00007248 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007249 new_skb = skb_copy(skb, GFP_ATOMIC);
7250 else {
7251 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7252
7253 new_skb = skb_copy_expand(skb,
7254 skb_headroom(skb) + more_headroom,
7255 skb_tailroom(skb), GFP_ATOMIC);
7256 }
7257
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007259 ret = -1;
7260 } else {
7261 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007262 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7263 PCI_DMA_TODEVICE);
7264 /* Make sure the mapping succeeded */
7265 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007266 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007267 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007268 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007269 u32 save_entry = *entry;
7270
Matt Carlson92cd3a12011-07-27 14:20:47 +00007271 base_flags |= TXD_FLAG_END;
7272
Matt Carlson84b67b22011-07-27 14:20:52 +00007273 tnapi->tx_buffers[*entry].skb = new_skb;
7274 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007275 mapping, new_addr);
7276
Matt Carlson84b67b22011-07-27 14:20:52 +00007277 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007278 new_skb->len, base_flags,
7279 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007280 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007281 dev_kfree_skb(new_skb);
7282 ret = -1;
7283 }
Michael Chanc58ec932005-09-17 00:46:27 -07007284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 }
7286
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007288 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007289 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290}
7291
Matt Carlson2ffcc982011-05-19 12:12:44 +00007292static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007293
7294/* Use GSO to workaround a rare TSO bug that may be triggered when the
7295 * TSO header is greater than 80 bytes.
7296 */
7297static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7298{
7299 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007300 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007301
7302 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007303 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007304 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007305
7306 /* netif_tx_stop_queue() must be done before checking
7307 * checking tx index in tg3_tx_avail() below, because in
7308 * tg3_tx(), we update tx index before checking for
7309 * netif_tx_queue_stopped().
7310 */
7311 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007312 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007313 return NETDEV_TX_BUSY;
7314
7315 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007316 }
7317
7318 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007319 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007320 goto tg3_tso_bug_end;
7321
7322 do {
7323 nskb = segs;
7324 segs = segs->next;
7325 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007326 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007327 } while (segs);
7328
7329tg3_tso_bug_end:
7330 dev_kfree_skb(skb);
7331
7332 return NETDEV_TX_OK;
7333}
Michael Chan52c0fd82006-06-29 20:15:54 -07007334
Michael Chan5a6f3072006-03-20 22:28:05 -08007335/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007336 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007337 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007338static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007339{
7340 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007341 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007342 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007343 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007344 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007345 struct tg3_napi *tnapi;
7346 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007347 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007348
Matt Carlson24f4efd2009-11-13 13:03:35 +00007349 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7350 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007351 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007352 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353
Matt Carlson84b67b22011-07-27 14:20:52 +00007354 budget = tg3_tx_avail(tnapi);
7355
Michael Chan00b70502006-06-17 21:58:45 -07007356 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007357 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007358 * interrupt. Furthermore, IRQ processing runs lockless so we have
7359 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007361 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007362 if (!netif_tx_queue_stopped(txq)) {
7363 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007364
7365 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007366 netdev_err(dev,
7367 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 return NETDEV_TX_BUSY;
7370 }
7371
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007372 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007374 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007376
Matt Carlsonbe98da62010-07-11 09:31:46 +00007377 mss = skb_shinfo(skb)->gso_size;
7378 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007379 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007380 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
7382 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007383 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7384 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385
Matt Carlson34195c32010-07-11 09:31:42 +00007386 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007387 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388
Eric Dumazeta5a11952012-01-23 01:22:09 +00007389 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007390
Eric Dumazeta5a11952012-01-23 01:22:09 +00007391 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007392 iph->check = 0;
7393 iph->tot_len = htons(mss + hdr_len);
7394 }
7395
Michael Chan52c0fd82006-06-29 20:15:54 -07007396 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007397 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007398 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007399
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7401 TXD_FLAG_CPU_POST_DMA);
7402
Joe Perches63c3a662011-04-26 08:12:10 +00007403 if (tg3_flag(tp, HW_TSO_1) ||
7404 tg3_flag(tp, HW_TSO_2) ||
7405 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007406 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007408 } else
7409 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7410 iph->daddr, 0,
7411 IPPROTO_TCP,
7412 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413
Joe Perches63c3a662011-04-26 08:12:10 +00007414 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007415 mss |= (hdr_len & 0xc) << 12;
7416 if (hdr_len & 0x10)
7417 base_flags |= 0x00000010;
7418 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007419 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007420 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007421 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007422 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007423 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424 int tsflags;
7425
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007426 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 mss |= (tsflags << 11);
7428 }
7429 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007430 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 int tsflags;
7432
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007433 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 base_flags |= tsflags << 12;
7435 }
7436 }
7437 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007438
Matt Carlson93a700a2011-08-31 11:44:54 +00007439 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7440 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7441 base_flags |= TXD_FLAG_JMB_PKT;
7442
Matt Carlson92cd3a12011-07-27 14:20:47 +00007443 if (vlan_tx_tag_present(skb)) {
7444 base_flags |= TXD_FLAG_VLAN;
7445 vlan = vlan_tx_tag_get(skb);
7446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007448 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7449 tg3_flag(tp, TX_TSTAMP_EN)) {
7450 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7451 base_flags |= TXD_FLAG_HWTSTAMP;
7452 }
7453
Alexander Duyckf4188d82009-12-02 16:48:38 +00007454 len = skb_headlen(skb);
7455
7456 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007457 if (pci_dma_mapping_error(tp->pdev, mapping))
7458 goto drop;
7459
David S. Miller90079ce2008-09-11 04:52:51 -07007460
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007461 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007462 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463
7464 would_hit_hwbug = 0;
7465
Joe Perches63c3a662011-04-26 08:12:10 +00007466 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007467 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468
Matt Carlson84b67b22011-07-27 14:20:52 +00007469 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007470 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007471 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007472 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007473 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007474 u32 tmp_mss = mss;
7475
7476 if (!tg3_flag(tp, HW_TSO_1) &&
7477 !tg3_flag(tp, HW_TSO_2) &&
7478 !tg3_flag(tp, HW_TSO_3))
7479 tmp_mss = 0;
7480
Matt Carlsonc5665a52012-02-13 10:20:12 +00007481 /* Now loop through additional data
7482 * fragments, and queue them.
7483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 last = skb_shinfo(skb)->nr_frags - 1;
7485 for (i = 0; i <= last; i++) {
7486 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7487
Eric Dumazet9e903e02011-10-18 21:00:24 +00007488 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007489 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007490 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007492 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007493 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007494 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007495 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007496 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497
Matt Carlsonb9e45482011-11-04 09:14:59 +00007498 if (!budget ||
7499 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007500 len, base_flags |
7501 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007502 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007503 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007504 break;
7505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506 }
7507 }
7508
7509 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007510 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511
7512 /* If the workaround fails due to memory/mapping
7513 * failure, silently drop this packet.
7514 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007515 entry = tnapi->tx_prod;
7516 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007517 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007518 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007519 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520 }
7521
Richard Cochrand515b452011-06-19 03:31:41 +00007522 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007523 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007524
Michael Chan6541b802012-03-04 14:48:14 +00007525 /* Sync BD data before updating mailbox */
7526 wmb();
7527
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007529 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007531 tnapi->tx_prod = entry;
7532 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007533 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007534
7535 /* netif_tx_stop_queue() must be done before checking
7536 * checking tx index in tg3_tx_avail() below, because in
7537 * tg3_tx(), we update tx index before checking for
7538 * netif_tx_queue_stopped().
7539 */
7540 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007541 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007542 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007545 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007547
7548dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007549 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007550 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007551drop:
7552 dev_kfree_skb(skb);
7553drop_nofree:
7554 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007555 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556}
7557
Matt Carlson6e01b202011-08-19 13:58:20 +00007558static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7559{
7560 if (enable) {
7561 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7562 MAC_MODE_PORT_MODE_MASK);
7563
7564 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7565
7566 if (!tg3_flag(tp, 5705_PLUS))
7567 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7568
7569 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7570 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7571 else
7572 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7573 } else {
7574 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7575
7576 if (tg3_flag(tp, 5705_PLUS) ||
7577 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00007578 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00007579 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7580 }
7581
7582 tw32(MAC_MODE, tp->mac_mode);
7583 udelay(40);
7584}
7585
Matt Carlson941ec902011-08-19 13:58:23 +00007586static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007587{
Matt Carlson941ec902011-08-19 13:58:23 +00007588 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007589
7590 tg3_phy_toggle_apd(tp, false);
7591 tg3_phy_toggle_automdix(tp, 0);
7592
Matt Carlson941ec902011-08-19 13:58:23 +00007593 if (extlpbk && tg3_phy_set_extloopbk(tp))
7594 return -EIO;
7595
7596 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007597 switch (speed) {
7598 case SPEED_10:
7599 break;
7600 case SPEED_100:
7601 bmcr |= BMCR_SPEED100;
7602 break;
7603 case SPEED_1000:
7604 default:
7605 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7606 speed = SPEED_100;
7607 bmcr |= BMCR_SPEED100;
7608 } else {
7609 speed = SPEED_1000;
7610 bmcr |= BMCR_SPEED1000;
7611 }
7612 }
7613
Matt Carlson941ec902011-08-19 13:58:23 +00007614 if (extlpbk) {
7615 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7616 tg3_readphy(tp, MII_CTRL1000, &val);
7617 val |= CTL1000_AS_MASTER |
7618 CTL1000_ENABLE_MASTER;
7619 tg3_writephy(tp, MII_CTRL1000, val);
7620 } else {
7621 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7622 MII_TG3_FET_PTEST_TRIM_2;
7623 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7624 }
7625 } else
7626 bmcr |= BMCR_LOOPBACK;
7627
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007628 tg3_writephy(tp, MII_BMCR, bmcr);
7629
7630 /* The write needs to be flushed for the FETs */
7631 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7632 tg3_readphy(tp, MII_BMCR, &bmcr);
7633
7634 udelay(40);
7635
7636 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00007637 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007638 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007639 MII_TG3_FET_PTEST_FRC_TX_LINK |
7640 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7641
7642 /* The write needs to be flushed for the AC131 */
7643 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7644 }
7645
7646 /* Reset to prevent losing 1st rx packet intermittently */
7647 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7648 tg3_flag(tp, 5780_CLASS)) {
7649 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7650 udelay(10);
7651 tw32_f(MAC_RX_MODE, tp->rx_mode);
7652 }
7653
7654 mac_mode = tp->mac_mode &
7655 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7656 if (speed == SPEED_1000)
7657 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7658 else
7659 mac_mode |= MAC_MODE_PORT_MODE_MII;
7660
Joe Perches41535772013-02-16 11:20:04 +00007661 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007662 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7663
7664 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7665 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7666 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7667 mac_mode |= MAC_MODE_LINK_POLARITY;
7668
7669 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7670 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7671 }
7672
7673 tw32(MAC_MODE, mac_mode);
7674 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007675
7676 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007677}
7678
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007679static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007680{
7681 struct tg3 *tp = netdev_priv(dev);
7682
7683 if (features & NETIF_F_LOOPBACK) {
7684 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7685 return;
7686
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007687 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007688 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007689 netif_carrier_on(tp->dev);
7690 spin_unlock_bh(&tp->lock);
7691 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7692 } else {
7693 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7694 return;
7695
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007696 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007697 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007698 /* Force link status check */
7699 tg3_setup_phy(tp, 1);
7700 spin_unlock_bh(&tp->lock);
7701 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7702 }
7703}
7704
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007705static netdev_features_t tg3_fix_features(struct net_device *dev,
7706 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007707{
7708 struct tg3 *tp = netdev_priv(dev);
7709
Joe Perches63c3a662011-04-26 08:12:10 +00007710 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007711 features &= ~NETIF_F_ALL_TSO;
7712
7713 return features;
7714}
7715
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007716static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007717{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007718 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007719
7720 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7721 tg3_set_loopback(dev, features);
7722
7723 return 0;
7724}
7725
Matt Carlson21f581a2009-08-28 14:00:25 +00007726static void tg3_rx_prodring_free(struct tg3 *tp,
7727 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 int i;
7730
Matt Carlson8fea32b2010-09-15 08:59:58 +00007731 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007732 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007733 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007734 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007735 tp->rx_pkt_map_sz);
7736
Joe Perches63c3a662011-04-26 08:12:10 +00007737 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007738 for (i = tpr->rx_jmb_cons_idx;
7739 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007740 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007741 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007742 TG3_RX_JMB_MAP_SZ);
7743 }
7744 }
7745
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007746 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748
Matt Carlson2c49a442010-09-30 10:34:35 +00007749 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007750 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007751 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007752
Joe Perches63c3a662011-04-26 08:12:10 +00007753 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007754 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007755 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007756 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757 }
7758}
7759
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007760/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761 *
7762 * The chip has been shut down and the driver detached from
7763 * the networking, so no interrupts or new tx packets will
7764 * end up in the driver. tp->{tx,}lock are held and thus
7765 * we may not sleep.
7766 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007767static int tg3_rx_prodring_alloc(struct tg3 *tp,
7768 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769{
Matt Carlson287be122009-08-28 13:58:46 +00007770 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007772 tpr->rx_std_cons_idx = 0;
7773 tpr->rx_std_prod_idx = 0;
7774 tpr->rx_jmb_cons_idx = 0;
7775 tpr->rx_jmb_prod_idx = 0;
7776
Matt Carlson8fea32b2010-09-15 08:59:58 +00007777 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007778 memset(&tpr->rx_std_buffers[0], 0,
7779 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007780 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007781 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007782 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007783 goto done;
7784 }
7785
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007787 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788
Matt Carlson287be122009-08-28 13:58:46 +00007789 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007790 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007791 tp->dev->mtu > ETH_DATA_LEN)
7792 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7793 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007794
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 /* Initialize invariants of the rings, we only set this
7796 * stuff once. This works because the card does not
7797 * write into the rx buffer posting rings.
7798 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007799 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800 struct tg3_rx_buffer_desc *rxd;
7801
Matt Carlson21f581a2009-08-28 14:00:25 +00007802 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007803 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7805 rxd->opaque = (RXD_OPAQUE_RING_STD |
7806 (i << RXD_OPAQUE_INDEX_SHIFT));
7807 }
7808
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007809 /* Now allocate fresh SKBs for each rx ring. */
7810 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007811 unsigned int frag_size;
7812
7813 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7814 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007815 netdev_warn(tp->dev,
7816 "Using a smaller RX standard ring. Only "
7817 "%d out of %d buffers were allocated "
7818 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007819 if (i == 0)
7820 goto initfail;
7821 tp->rx_pending = i;
7822 break;
7823 }
7824 }
7825
Joe Perches63c3a662011-04-26 08:12:10 +00007826 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007827 goto done;
7828
Matt Carlson2c49a442010-09-30 10:34:35 +00007829 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007830
Joe Perches63c3a662011-04-26 08:12:10 +00007831 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007832 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833
Matt Carlson2c49a442010-09-30 10:34:35 +00007834 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007835 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836
Matt Carlson0d86df82010-02-17 15:17:00 +00007837 rxd = &tpr->rx_jmb[i].std;
7838 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7839 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7840 RXD_FLAG_JUMBO;
7841 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7842 (i << RXD_OPAQUE_INDEX_SHIFT));
7843 }
7844
7845 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007846 unsigned int frag_size;
7847
7848 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7849 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007850 netdev_warn(tp->dev,
7851 "Using a smaller RX jumbo ring. Only %d "
7852 "out of %d buffers were allocated "
7853 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007854 if (i == 0)
7855 goto initfail;
7856 tp->rx_jumbo_pending = i;
7857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858 }
7859 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007860
7861done:
Michael Chan32d8c572006-07-25 16:38:29 -07007862 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007863
7864initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007865 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007866 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867}
7868
Matt Carlson21f581a2009-08-28 14:00:25 +00007869static void tg3_rx_prodring_fini(struct tg3 *tp,
7870 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871{
Matt Carlson21f581a2009-08-28 14:00:25 +00007872 kfree(tpr->rx_std_buffers);
7873 tpr->rx_std_buffers = NULL;
7874 kfree(tpr->rx_jmb_buffers);
7875 tpr->rx_jmb_buffers = NULL;
7876 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007877 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7878 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007879 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007881 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007882 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7883 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007884 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007885 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007886}
7887
Matt Carlson21f581a2009-08-28 14:00:25 +00007888static int tg3_rx_prodring_init(struct tg3 *tp,
7889 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007890{
Matt Carlson2c49a442010-09-30 10:34:35 +00007891 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7892 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007893 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007894 return -ENOMEM;
7895
Matt Carlson4bae65c2010-11-24 08:31:52 +00007896 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7897 TG3_RX_STD_RING_BYTES(tp),
7898 &tpr->rx_std_mapping,
7899 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007900 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007901 goto err_out;
7902
Joe Perches63c3a662011-04-26 08:12:10 +00007903 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007904 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007905 GFP_KERNEL);
7906 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007907 goto err_out;
7908
Matt Carlson4bae65c2010-11-24 08:31:52 +00007909 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7910 TG3_RX_JMB_RING_BYTES(tp),
7911 &tpr->rx_jmb_mapping,
7912 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007913 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007914 goto err_out;
7915 }
7916
7917 return 0;
7918
7919err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007920 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007921 return -ENOMEM;
7922}
7923
7924/* Free up pending packets in all rx/tx rings.
7925 *
7926 * The chip has been shut down and the driver detached from
7927 * the networking, so no interrupts or new tx packets will
7928 * end up in the driver. tp->{tx,}lock is not held and we are not
7929 * in an interrupt context and thus may sleep.
7930 */
7931static void tg3_free_rings(struct tg3 *tp)
7932{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007933 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007934
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007935 for (j = 0; j < tp->irq_cnt; j++) {
7936 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007937
Matt Carlson8fea32b2010-09-15 08:59:58 +00007938 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007939
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007940 if (!tnapi->tx_buffers)
7941 continue;
7942
Matt Carlson0d681b22011-07-27 14:20:49 +00007943 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7944 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007945
Matt Carlson0d681b22011-07-27 14:20:49 +00007946 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007947 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007948
Matt Carlsonba1142e2011-11-04 09:15:00 +00007949 tg3_tx_skb_unmap(tnapi, i,
7950 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007951
7952 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007953 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007954 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007955 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007956}
7957
7958/* Initialize tx/rx rings for packet processing.
7959 *
7960 * The chip has been shut down and the driver detached from
7961 * the networking, so no interrupts or new tx packets will
7962 * end up in the driver. tp->{tx,}lock are held and thus
7963 * we may not sleep.
7964 */
7965static int tg3_init_rings(struct tg3 *tp)
7966{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007967 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007968
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007969 /* Free up all the SKBs. */
7970 tg3_free_rings(tp);
7971
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007972 for (i = 0; i < tp->irq_cnt; i++) {
7973 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007974
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007975 tnapi->last_tag = 0;
7976 tnapi->last_irq_tag = 0;
7977 tnapi->hw_status->status = 0;
7978 tnapi->hw_status->status_tag = 0;
7979 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7980
7981 tnapi->tx_prod = 0;
7982 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007983 if (tnapi->tx_ring)
7984 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007985
7986 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007987 if (tnapi->rx_rcb)
7988 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007989
Matt Carlson8fea32b2010-09-15 08:59:58 +00007990 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007991 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007992 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007993 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007994 }
Matt Carlson72334482009-08-28 14:03:01 +00007995
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007996 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007997}
7998
Michael Chan49a359e2012-09-28 07:12:37 +00007999static void tg3_mem_tx_release(struct tg3 *tp)
8000{
8001 int i;
8002
8003 for (i = 0; i < tp->irq_max; i++) {
8004 struct tg3_napi *tnapi = &tp->napi[i];
8005
8006 if (tnapi->tx_ring) {
8007 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8008 tnapi->tx_ring, tnapi->tx_desc_mapping);
8009 tnapi->tx_ring = NULL;
8010 }
8011
8012 kfree(tnapi->tx_buffers);
8013 tnapi->tx_buffers = NULL;
8014 }
8015}
8016
8017static int tg3_mem_tx_acquire(struct tg3 *tp)
8018{
8019 int i;
8020 struct tg3_napi *tnapi = &tp->napi[0];
8021
8022 /* If multivector TSS is enabled, vector 0 does not handle
8023 * tx interrupts. Don't allocate any resources for it.
8024 */
8025 if (tg3_flag(tp, ENABLE_TSS))
8026 tnapi++;
8027
8028 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8029 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8030 TG3_TX_RING_SIZE, GFP_KERNEL);
8031 if (!tnapi->tx_buffers)
8032 goto err_out;
8033
8034 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8035 TG3_TX_RING_BYTES,
8036 &tnapi->tx_desc_mapping,
8037 GFP_KERNEL);
8038 if (!tnapi->tx_ring)
8039 goto err_out;
8040 }
8041
8042 return 0;
8043
8044err_out:
8045 tg3_mem_tx_release(tp);
8046 return -ENOMEM;
8047}
8048
8049static void tg3_mem_rx_release(struct tg3 *tp)
8050{
8051 int i;
8052
8053 for (i = 0; i < tp->irq_max; i++) {
8054 struct tg3_napi *tnapi = &tp->napi[i];
8055
8056 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8057
8058 if (!tnapi->rx_rcb)
8059 continue;
8060
8061 dma_free_coherent(&tp->pdev->dev,
8062 TG3_RX_RCB_RING_BYTES(tp),
8063 tnapi->rx_rcb,
8064 tnapi->rx_rcb_mapping);
8065 tnapi->rx_rcb = NULL;
8066 }
8067}
8068
8069static int tg3_mem_rx_acquire(struct tg3 *tp)
8070{
8071 unsigned int i, limit;
8072
8073 limit = tp->rxq_cnt;
8074
8075 /* If RSS is enabled, we need a (dummy) producer ring
8076 * set on vector zero. This is the true hw prodring.
8077 */
8078 if (tg3_flag(tp, ENABLE_RSS))
8079 limit++;
8080
8081 for (i = 0; i < limit; i++) {
8082 struct tg3_napi *tnapi = &tp->napi[i];
8083
8084 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8085 goto err_out;
8086
8087 /* If multivector RSS is enabled, vector 0
8088 * does not handle rx or tx interrupts.
8089 * Don't allocate any resources for it.
8090 */
8091 if (!i && tg3_flag(tp, ENABLE_RSS))
8092 continue;
8093
8094 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
8095 TG3_RX_RCB_RING_BYTES(tp),
8096 &tnapi->rx_rcb_mapping,
8097 GFP_KERNEL);
8098 if (!tnapi->rx_rcb)
8099 goto err_out;
8100
8101 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
8102 }
8103
8104 return 0;
8105
8106err_out:
8107 tg3_mem_rx_release(tp);
8108 return -ENOMEM;
8109}
8110
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008111/*
8112 * Must not be invoked with interrupt sources disabled and
8113 * the hardware shutdown down.
8114 */
8115static void tg3_free_consistent(struct tg3 *tp)
8116{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008117 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008118
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008119 for (i = 0; i < tp->irq_cnt; i++) {
8120 struct tg3_napi *tnapi = &tp->napi[i];
8121
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008122 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008123 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8124 tnapi->hw_status,
8125 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008126 tnapi->hw_status = NULL;
8127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008128 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008129
Michael Chan49a359e2012-09-28 07:12:37 +00008130 tg3_mem_rx_release(tp);
8131 tg3_mem_tx_release(tp);
8132
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008134 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8135 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136 tp->hw_stats = NULL;
8137 }
8138}
8139
8140/*
8141 * Must not be invoked with interrupt sources disabled and
8142 * the hardware shutdown down. Can sleep.
8143 */
8144static int tg3_alloc_consistent(struct tg3 *tp)
8145{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008146 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008147
Matt Carlson4bae65c2010-11-24 08:31:52 +00008148 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8149 sizeof(struct tg3_hw_stats),
8150 &tp->stats_mapping,
8151 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152 if (!tp->hw_stats)
8153 goto err_out;
8154
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8156
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008157 for (i = 0; i < tp->irq_cnt; i++) {
8158 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008159 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008160
Matt Carlson4bae65c2010-11-24 08:31:52 +00008161 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8162 TG3_HW_STATUS_SIZE,
8163 &tnapi->status_mapping,
8164 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008165 if (!tnapi->hw_status)
8166 goto err_out;
8167
8168 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008169 sblk = tnapi->hw_status;
8170
Michael Chan49a359e2012-09-28 07:12:37 +00008171 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008172 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008173
Michael Chan49a359e2012-09-28 07:12:37 +00008174 /*
8175 * When RSS is enabled, the status block format changes
8176 * slightly. The "rx_jumbo_consumer", "reserved",
8177 * and "rx_mini_consumer" members get mapped to the
8178 * other three rx return ring producer indexes.
8179 */
8180 switch (i) {
8181 case 1:
8182 prodptr = &sblk->idx[0].rx_producer;
8183 break;
8184 case 2:
8185 prodptr = &sblk->rx_jumbo_consumer;
8186 break;
8187 case 3:
8188 prodptr = &sblk->reserved;
8189 break;
8190 case 4:
8191 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008192 break;
8193 }
Michael Chan49a359e2012-09-28 07:12:37 +00008194 tnapi->rx_rcb_prod_idx = prodptr;
8195 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008196 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008197 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008198 }
8199
Michael Chan49a359e2012-09-28 07:12:37 +00008200 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8201 goto err_out;
8202
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 return 0;
8204
8205err_out:
8206 tg3_free_consistent(tp);
8207 return -ENOMEM;
8208}
8209
8210#define MAX_WAIT_CNT 1000
8211
8212/* To stop a block, clear the enable bit and poll till it
8213 * clears. tp->lock is held.
8214 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008215static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216{
8217 unsigned int i;
8218 u32 val;
8219
Joe Perches63c3a662011-04-26 08:12:10 +00008220 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 switch (ofs) {
8222 case RCVLSC_MODE:
8223 case DMAC_MODE:
8224 case MBFREE_MODE:
8225 case BUFMGR_MODE:
8226 case MEMARB_MODE:
8227 /* We can't enable/disable these bits of the
8228 * 5705/5750, just say success.
8229 */
8230 return 0;
8231
8232 default:
8233 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008235 }
8236
8237 val = tr32(ofs);
8238 val &= ~enable_bit;
8239 tw32_f(ofs, val);
8240
8241 for (i = 0; i < MAX_WAIT_CNT; i++) {
8242 udelay(100);
8243 val = tr32(ofs);
8244 if ((val & enable_bit) == 0)
8245 break;
8246 }
8247
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008248 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008249 dev_err(&tp->pdev->dev,
8250 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8251 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252 return -ENODEV;
8253 }
8254
8255 return 0;
8256}
8257
8258/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008259static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260{
8261 int i, err;
8262
8263 tg3_disable_ints(tp);
8264
8265 tp->rx_mode &= ~RX_MODE_ENABLE;
8266 tw32_f(MAC_RX_MODE, tp->rx_mode);
8267 udelay(10);
8268
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008269 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8270 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8271 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8272 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8273 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8274 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008276 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8277 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8278 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8279 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8280 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8281 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8282 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283
8284 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8285 tw32_f(MAC_MODE, tp->mac_mode);
8286 udelay(40);
8287
8288 tp->tx_mode &= ~TX_MODE_ENABLE;
8289 tw32_f(MAC_TX_MODE, tp->tx_mode);
8290
8291 for (i = 0; i < MAX_WAIT_CNT; i++) {
8292 udelay(100);
8293 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8294 break;
8295 }
8296 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008297 dev_err(&tp->pdev->dev,
8298 "%s timed out, TX_MODE_ENABLE will not clear "
8299 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008300 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301 }
8302
Michael Chane6de8ad2005-05-05 14:42:41 -07008303 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008304 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8305 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008306
8307 tw32(FTQ_RESET, 0xffffffff);
8308 tw32(FTQ_RESET, 0x00000000);
8309
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008310 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8311 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008313 for (i = 0; i < tp->irq_cnt; i++) {
8314 struct tg3_napi *tnapi = &tp->napi[i];
8315 if (tnapi->hw_status)
8316 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319 return err;
8320}
8321
Michael Chanee6a99b2007-07-18 21:49:10 -07008322/* Save PCI command register before chip reset */
8323static void tg3_save_pci_state(struct tg3 *tp)
8324{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008325 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008326}
8327
8328/* Restore PCI state after chip reset */
8329static void tg3_restore_pci_state(struct tg3 *tp)
8330{
8331 u32 val;
8332
8333 /* Re-enable indirect register accesses. */
8334 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8335 tp->misc_host_ctrl);
8336
8337 /* Set MAX PCI retry to zero. */
8338 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008339 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008340 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008341 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008342 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008343 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008344 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008345 PCISTATE_ALLOW_APE_SHMEM_WR |
8346 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008347 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8348
Matt Carlson8a6eac92007-10-21 16:17:55 -07008349 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008350
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008351 if (!tg3_flag(tp, PCI_EXPRESS)) {
8352 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8353 tp->pci_cacheline_sz);
8354 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8355 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008356 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008357
Michael Chanee6a99b2007-07-18 21:49:10 -07008358 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008359 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008360 u16 pcix_cmd;
8361
8362 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8363 &pcix_cmd);
8364 pcix_cmd &= ~PCI_X_CMD_ERO;
8365 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8366 pcix_cmd);
8367 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008368
Joe Perches63c3a662011-04-26 08:12:10 +00008369 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008370
8371 /* Chip reset on 5780 will reset MSI enable bit,
8372 * so need to restore it.
8373 */
Joe Perches63c3a662011-04-26 08:12:10 +00008374 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008375 u16 ctrl;
8376
8377 pci_read_config_word(tp->pdev,
8378 tp->msi_cap + PCI_MSI_FLAGS,
8379 &ctrl);
8380 pci_write_config_word(tp->pdev,
8381 tp->msi_cap + PCI_MSI_FLAGS,
8382 ctrl | PCI_MSI_FLAGS_ENABLE);
8383 val = tr32(MSGINT_MODE);
8384 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8385 }
8386 }
8387}
8388
Linus Torvalds1da177e2005-04-16 15:20:36 -07008389/* tp->lock is held. */
8390static int tg3_chip_reset(struct tg3 *tp)
8391{
8392 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008393 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008394 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395
David S. Millerf49639e2006-06-09 11:58:36 -07008396 tg3_nvram_lock(tp);
8397
Matt Carlson77b483f2008-08-15 14:07:24 -07008398 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8399
David S. Millerf49639e2006-06-09 11:58:36 -07008400 /* No matching tg3_nvram_unlock() after this because
8401 * chip reset below will undo the nvram lock.
8402 */
8403 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404
Michael Chanee6a99b2007-07-18 21:49:10 -07008405 /* GRC_MISC_CFG core clock reset will clear the memory
8406 * enable bit in PCI register 4 and the MSI enable bit
8407 * on some chips, so we save relevant registers here.
8408 */
8409 tg3_save_pci_state(tp);
8410
Joe Perches41535772013-02-16 11:20:04 +00008411 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008412 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008413 tw32(GRC_FASTBOOT_PC, 0);
8414
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 /*
8416 * We must avoid the readl() that normally takes place.
8417 * It locks machines, causes machine checks, and other
8418 * fun things. So, temporarily disable the 5701
8419 * hardware workaround, while we do the reset.
8420 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008421 write_op = tp->write32;
8422 if (write_op == tg3_write_flush_reg32)
8423 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424
Michael Chand18edcb2007-03-24 20:57:11 -07008425 /* Prevent the irq handler from reading or writing PCI registers
8426 * during chip reset when the memory enable bit in the PCI command
8427 * register may be cleared. The chip does not generate interrupt
8428 * at this time, but the irq handler may still be called due to irq
8429 * sharing or irqpoll.
8430 */
Joe Perches63c3a662011-04-26 08:12:10 +00008431 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008432 for (i = 0; i < tp->irq_cnt; i++) {
8433 struct tg3_napi *tnapi = &tp->napi[i];
8434 if (tnapi->hw_status) {
8435 tnapi->hw_status->status = 0;
8436 tnapi->hw_status->status_tag = 0;
8437 }
8438 tnapi->last_tag = 0;
8439 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008440 }
Michael Chand18edcb2007-03-24 20:57:11 -07008441 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008442
8443 for (i = 0; i < tp->irq_cnt; i++)
8444 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008445
Joe Perches41535772013-02-16 11:20:04 +00008446 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00008447 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8448 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8449 }
8450
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 /* do the reset */
8452 val = GRC_MISC_CFG_CORECLK_RESET;
8453
Joe Perches63c3a662011-04-26 08:12:10 +00008454 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008455 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00008456 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008457 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008458 tr32(TG3_PCIE_PHY_TSTCTL) ==
8459 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8460 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8461
Joe Perches41535772013-02-16 11:20:04 +00008462 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 tw32(GRC_MISC_CFG, (1 << 29));
8464 val |= (1 << 29);
8465 }
8466 }
8467
Joe Perches41535772013-02-16 11:20:04 +00008468 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07008469 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8470 tw32(GRC_VCPU_EXT_CTRL,
8471 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8472 }
8473
Matt Carlsonf37500d2010-08-02 11:25:59 +00008474 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008475 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008477
Linus Torvalds1da177e2005-04-16 15:20:36 -07008478 tw32(GRC_MISC_CFG, val);
8479
Michael Chan1ee582d2005-08-09 20:16:46 -07008480 /* restore 5701 hardware bug workaround write method */
8481 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482
8483 /* Unfortunately, we have to delay before the PCI read back.
8484 * Some 575X chips even will not respond to a PCI cfg access
8485 * when the reset command is given to the chip.
8486 *
8487 * How do these hardware designers expect things to work
8488 * properly if the PCI write is posted for a long period
8489 * of time? It is always necessary to have some method by
8490 * which a register read back can occur to push the write
8491 * out which does the reset.
8492 *
8493 * For most tg3 variants the trick below was working.
8494 * Ho hum...
8495 */
8496 udelay(120);
8497
8498 /* Flush PCI posted writes. The normal MMIO registers
8499 * are inaccessible at this time so this is the only
8500 * way to make this reliably (actually, this is no longer
8501 * the case, see above). I tried to use indirect
8502 * register read/write but this upset some 5701 variants.
8503 */
8504 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8505
8506 udelay(120);
8507
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008508 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008509 u16 val16;
8510
Joe Perches41535772013-02-16 11:20:04 +00008511 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008512 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 u32 cfg_val;
8514
8515 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008516 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517 udelay(100);
8518
8519 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8520 pci_write_config_dword(tp->pdev, 0xc4,
8521 cfg_val | (1 << 15));
8522 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008523
Matt Carlsone7126992009-08-25 10:08:16 +00008524 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008525 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008526 /*
8527 * Older PCIe devices only support the 128 byte
8528 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008529 */
Joe Perches63c3a662011-04-26 08:12:10 +00008530 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008531 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8532 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008533
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008534 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008535 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008536 PCI_EXP_DEVSTA_CED |
8537 PCI_EXP_DEVSTA_NFED |
8538 PCI_EXP_DEVSTA_FED |
8539 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540 }
8541
Michael Chanee6a99b2007-07-18 21:49:10 -07008542 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543
Joe Perches63c3a662011-04-26 08:12:10 +00008544 tg3_flag_clear(tp, CHIP_RESETTING);
8545 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008546
Michael Chanee6a99b2007-07-18 21:49:10 -07008547 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008548 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008549 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008550 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551
Joe Perches41535772013-02-16 11:20:04 +00008552 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008553 tg3_stop_fw(tp);
8554 tw32(0x5000, 0x400);
8555 }
8556
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00008557 if (tg3_flag(tp, IS_SSB_CORE)) {
8558 /*
8559 * BCM4785: In order to avoid repercussions from using
8560 * potentially defective internal ROM, stop the Rx RISC CPU,
8561 * which is not required.
8562 */
8563 tg3_stop_fw(tp);
8564 tg3_halt_cpu(tp, RX_CPU_BASE);
8565 }
8566
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567 tw32(GRC_MODE, tp->grc_mode);
8568
Joe Perches41535772013-02-16 11:20:04 +00008569 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008570 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571
8572 tw32(0xc4, val | (1 << 15));
8573 }
8574
8575 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00008576 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00008578 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008579 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8580 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8581 }
8582
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008583 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008584 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008585 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008586 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008587 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008588 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008590 val = 0;
8591
8592 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008593 udelay(40);
8594
Matt Carlson77b483f2008-08-15 14:07:24 -07008595 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8596
Michael Chan7a6f4362006-09-27 16:03:31 -07008597 err = tg3_poll_fw(tp);
8598 if (err)
8599 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008600
Matt Carlson0a9140c2009-08-28 12:27:50 +00008601 tg3_mdio_start(tp);
8602
Joe Perches63c3a662011-04-26 08:12:10 +00008603 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00008604 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
8605 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008606 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008607 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608
8609 tw32(0x7c00, val | (1 << 25));
8610 }
8611
Joe Perches41535772013-02-16 11:20:04 +00008612 if (tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsond78b59f2011-04-05 14:22:46 +00008613 val = tr32(TG3_CPMU_CLCK_ORIDE);
8614 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8615 }
8616
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008618 tg3_flag_clear(tp, ENABLE_ASF);
8619 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008620 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8621 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8622 u32 nic_cfg;
8623
8624 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8625 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008626 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008627 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008628 if (tg3_flag(tp, 5750_PLUS))
8629 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630 }
8631 }
8632
8633 return 0;
8634}
8635
Matt Carlson65ec6982012-02-28 23:33:37 +00008636static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8637static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008638
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008640static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641{
8642 int err;
8643
8644 tg3_stop_fw(tp);
8645
Michael Chan944d9802005-05-29 14:57:48 -07008646 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008648 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649 err = tg3_chip_reset(tp);
8650
Matt Carlsondaba2a62009-04-20 06:58:52 +00008651 __tg3_set_mac_addr(tp, 0);
8652
Michael Chan944d9802005-05-29 14:57:48 -07008653 tg3_write_sig_legacy(tp, kind);
8654 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008655
Matt Carlson92feeab2011-12-08 14:40:14 +00008656 if (tp->hw_stats) {
8657 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008658 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008659 tg3_get_estats(tp, &tp->estats_prev);
8660
8661 /* And make sure the next sample is new data */
8662 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8663 }
8664
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665 if (err)
8666 return err;
8667
8668 return 0;
8669}
8670
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671static int tg3_set_mac_addr(struct net_device *dev, void *p)
8672{
8673 struct tg3 *tp = netdev_priv(dev);
8674 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008675 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676
Michael Chanf9804dd2005-09-27 12:13:10 -07008677 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008678 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008679
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8681
Michael Chane75f7c92006-03-20 21:33:26 -08008682 if (!netif_running(dev))
8683 return 0;
8684
Joe Perches63c3a662011-04-26 08:12:10 +00008685 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008686 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008687
Michael Chan986e0ae2007-05-05 12:10:20 -07008688 addr0_high = tr32(MAC_ADDR_0_HIGH);
8689 addr0_low = tr32(MAC_ADDR_0_LOW);
8690 addr1_high = tr32(MAC_ADDR_1_HIGH);
8691 addr1_low = tr32(MAC_ADDR_1_LOW);
8692
8693 /* Skip MAC addr 1 if ASF is using it. */
8694 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8695 !(addr1_high == 0 && addr1_low == 0))
8696 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008697 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008698 spin_lock_bh(&tp->lock);
8699 __tg3_set_mac_addr(tp, skip_mac_1);
8700 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008701
Michael Chanb9ec6c12006-07-25 16:37:27 -07008702 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703}
8704
8705/* tp->lock is held. */
8706static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8707 dma_addr_t mapping, u32 maxlen_flags,
8708 u32 nic_addr)
8709{
8710 tg3_write_mem(tp,
8711 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8712 ((u64) mapping >> 32));
8713 tg3_write_mem(tp,
8714 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8715 ((u64) mapping & 0xffffffff));
8716 tg3_write_mem(tp,
8717 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8718 maxlen_flags);
8719
Joe Perches63c3a662011-04-26 08:12:10 +00008720 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721 tg3_write_mem(tp,
8722 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8723 nic_addr);
8724}
8725
Michael Chana489b6d2012-09-28 07:12:39 +00008726
8727static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008728{
Michael Chana489b6d2012-09-28 07:12:39 +00008729 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00008730
Joe Perches63c3a662011-04-26 08:12:10 +00008731 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008732 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8733 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8734 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008735 } else {
8736 tw32(HOSTCC_TXCOL_TICKS, 0);
8737 tw32(HOSTCC_TXMAX_FRAMES, 0);
8738 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00008739
8740 for (; i < tp->txq_cnt; i++) {
8741 u32 reg;
8742
8743 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8744 tw32(reg, ec->tx_coalesce_usecs);
8745 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8746 tw32(reg, ec->tx_max_coalesced_frames);
8747 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8748 tw32(reg, ec->tx_max_coalesced_frames_irq);
8749 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00008750 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008751
Michael Chana489b6d2012-09-28 07:12:39 +00008752 for (; i < tp->irq_max - 1; i++) {
8753 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8754 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8755 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8756 }
8757}
8758
8759static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
8760{
8761 int i = 0;
8762 u32 limit = tp->rxq_cnt;
8763
Joe Perches63c3a662011-04-26 08:12:10 +00008764 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008765 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8766 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8767 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00008768 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008769 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008770 tw32(HOSTCC_RXCOL_TICKS, 0);
8771 tw32(HOSTCC_RXMAX_FRAMES, 0);
8772 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008773 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008774
Michael Chana489b6d2012-09-28 07:12:39 +00008775 for (; i < limit; i++) {
8776 u32 reg;
8777
8778 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8779 tw32(reg, ec->rx_coalesce_usecs);
8780 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8781 tw32(reg, ec->rx_max_coalesced_frames);
8782 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8783 tw32(reg, ec->rx_max_coalesced_frames_irq);
8784 }
8785
8786 for (; i < tp->irq_max - 1; i++) {
8787 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
8788 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
8789 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8790 }
8791}
8792
8793static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
8794{
8795 tg3_coal_tx_init(tp, ec);
8796 tg3_coal_rx_init(tp, ec);
8797
Joe Perches63c3a662011-04-26 08:12:10 +00008798 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008799 u32 val = ec->stats_block_coalesce_usecs;
8800
Matt Carlsonb6080e12009-09-01 13:12:00 +00008801 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8802 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8803
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00008804 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07008805 val = 0;
8806
8807 tw32(HOSTCC_STAT_COAL_TICKS, val);
8808 }
8809}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008810
8811/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008812static void tg3_rings_reset(struct tg3 *tp)
8813{
8814 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008815 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008816 struct tg3_napi *tnapi = &tp->napi[0];
8817
8818 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008819 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008820 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008821 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008822 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Michael Chanc65a17f2013-01-06 12:51:07 +00008823 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00008824 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonb703df62009-12-03 08:36:21 +00008825 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008826 else
8827 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8828
8829 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8830 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8831 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8832 BDINFO_FLAGS_DISABLED);
8833
8834
8835 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008836 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008837 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008838 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008839 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches41535772013-02-16 11:20:04 +00008840 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
8841 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008842 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008843 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8844 else
8845 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8846
8847 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8848 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8849 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8850 BDINFO_FLAGS_DISABLED);
8851
8852 /* Disable interrupts */
8853 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008854 tp->napi[0].chk_msi_cnt = 0;
8855 tp->napi[0].last_rx_cons = 0;
8856 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008857
8858 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008859 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008860 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008861 tp->napi[i].tx_prod = 0;
8862 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008863 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008864 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008865 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8866 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008867 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008868 tp->napi[i].last_rx_cons = 0;
8869 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008870 }
Joe Perches63c3a662011-04-26 08:12:10 +00008871 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008872 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008873 } else {
8874 tp->napi[0].tx_prod = 0;
8875 tp->napi[0].tx_cons = 0;
8876 tw32_mailbox(tp->napi[0].prodmbox, 0);
8877 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8878 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008879
8880 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008881 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008882 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8883 for (i = 0; i < 16; i++)
8884 tw32_tx_mbox(mbox + i * 8, 0);
8885 }
8886
8887 txrcb = NIC_SRAM_SEND_RCB;
8888 rxrcb = NIC_SRAM_RCV_RET_RCB;
8889
8890 /* Clear status block in ram. */
8891 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8892
8893 /* Set status block DMA address */
8894 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8895 ((u64) tnapi->status_mapping >> 32));
8896 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8897 ((u64) tnapi->status_mapping & 0xffffffff));
8898
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008899 if (tnapi->tx_ring) {
8900 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8901 (TG3_TX_RING_SIZE <<
8902 BDINFO_FLAGS_MAXLEN_SHIFT),
8903 NIC_SRAM_TX_BUFFER_DESC);
8904 txrcb += TG3_BDINFO_SIZE;
8905 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008906
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008907 if (tnapi->rx_rcb) {
8908 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008909 (tp->rx_ret_ring_mask + 1) <<
8910 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008911 rxrcb += TG3_BDINFO_SIZE;
8912 }
8913
8914 stblk = HOSTCC_STATBLCK_RING1;
8915
8916 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8917 u64 mapping = (u64)tnapi->status_mapping;
8918 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8919 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8920
8921 /* Clear status block in ram. */
8922 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8923
Matt Carlson19cfaec2009-12-03 08:36:20 +00008924 if (tnapi->tx_ring) {
8925 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8926 (TG3_TX_RING_SIZE <<
8927 BDINFO_FLAGS_MAXLEN_SHIFT),
8928 NIC_SRAM_TX_BUFFER_DESC);
8929 txrcb += TG3_BDINFO_SIZE;
8930 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008931
8932 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008933 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008934 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8935
8936 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008937 rxrcb += TG3_BDINFO_SIZE;
8938 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008939}
8940
Matt Carlsoneb07a942011-04-20 07:57:36 +00008941static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8942{
8943 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8944
Joe Perches63c3a662011-04-26 08:12:10 +00008945 if (!tg3_flag(tp, 5750_PLUS) ||
8946 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00008947 tg3_asic_rev(tp) == ASIC_REV_5750 ||
8948 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008949 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008950 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00008951 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
8952 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00008953 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8954 else
8955 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8956
8957 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8958 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8959
8960 val = min(nic_rep_thresh, host_rep_thresh);
8961 tw32(RCVBDI_STD_THRESH, val);
8962
Joe Perches63c3a662011-04-26 08:12:10 +00008963 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008964 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8965
Joe Perches63c3a662011-04-26 08:12:10 +00008966 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008967 return;
8968
Matt Carlson513aa6e2011-11-21 15:01:18 +00008969 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008970
8971 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8972
8973 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8974 tw32(RCVBDI_JUMBO_THRESH, val);
8975
Joe Perches63c3a662011-04-26 08:12:10 +00008976 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008977 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8978}
8979
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008980static inline u32 calc_crc(unsigned char *buf, int len)
8981{
8982 u32 reg;
8983 u32 tmp;
8984 int j, k;
8985
8986 reg = 0xffffffff;
8987
8988 for (j = 0; j < len; j++) {
8989 reg ^= buf[j];
8990
8991 for (k = 0; k < 8; k++) {
8992 tmp = reg & 0x01;
8993
8994 reg >>= 1;
8995
8996 if (tmp)
8997 reg ^= 0xedb88320;
8998 }
8999 }
9000
9001 return ~reg;
9002}
9003
9004static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9005{
9006 /* accept or reject all multicast frames */
9007 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9008 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9009 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9010 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9011}
9012
9013static void __tg3_set_rx_mode(struct net_device *dev)
9014{
9015 struct tg3 *tp = netdev_priv(dev);
9016 u32 rx_mode;
9017
9018 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9019 RX_MODE_KEEP_VLAN_TAG);
9020
9021#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9022 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9023 * flag clear.
9024 */
9025 if (!tg3_flag(tp, ENABLE_ASF))
9026 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9027#endif
9028
9029 if (dev->flags & IFF_PROMISC) {
9030 /* Promiscuous mode. */
9031 rx_mode |= RX_MODE_PROMISC;
9032 } else if (dev->flags & IFF_ALLMULTI) {
9033 /* Accept all multicast. */
9034 tg3_set_multi(tp, 1);
9035 } else if (netdev_mc_empty(dev)) {
9036 /* Reject all multicast. */
9037 tg3_set_multi(tp, 0);
9038 } else {
9039 /* Accept one or more multicast(s). */
9040 struct netdev_hw_addr *ha;
9041 u32 mc_filter[4] = { 0, };
9042 u32 regidx;
9043 u32 bit;
9044 u32 crc;
9045
9046 netdev_for_each_mc_addr(ha, dev) {
9047 crc = calc_crc(ha->addr, ETH_ALEN);
9048 bit = ~crc & 0x7f;
9049 regidx = (bit & 0x60) >> 5;
9050 bit &= 0x1f;
9051 mc_filter[regidx] |= (1 << bit);
9052 }
9053
9054 tw32(MAC_HASH_REG_0, mc_filter[0]);
9055 tw32(MAC_HASH_REG_1, mc_filter[1]);
9056 tw32(MAC_HASH_REG_2, mc_filter[2]);
9057 tw32(MAC_HASH_REG_3, mc_filter[3]);
9058 }
9059
9060 if (rx_mode != tp->rx_mode) {
9061 tp->rx_mode = rx_mode;
9062 tw32_f(MAC_RX_MODE, rx_mode);
9063 udelay(10);
9064 }
9065}
9066
Michael Chan91024262012-09-28 07:12:38 +00009067static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009068{
9069 int i;
9070
9071 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009072 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009073}
9074
9075static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009076{
9077 int i;
9078
9079 if (!tg3_flag(tp, SUPPORT_MSIX))
9080 return;
9081
Michael Chan0b3ba052012-11-14 14:44:29 +00009082 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009083 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009084 return;
9085 }
9086
9087 /* Validate table against current IRQ count */
9088 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009089 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009090 break;
9091 }
9092
9093 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009094 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009095}
9096
Matt Carlson90415472011-12-16 13:33:23 +00009097static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009098{
9099 int i = 0;
9100 u32 reg = MAC_RSS_INDIR_TBL_0;
9101
9102 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9103 u32 val = tp->rss_ind_tbl[i];
9104 i++;
9105 for (; i % 8; i++) {
9106 val <<= 4;
9107 val |= tp->rss_ind_tbl[i];
9108 }
9109 tw32(reg, val);
9110 reg += 4;
9111 }
9112}
9113
Matt Carlson2d31eca2009-09-01 12:53:31 +00009114/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009115static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116{
9117 u32 val, rdmac_mode;
9118 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009119 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120
9121 tg3_disable_ints(tp);
9122
9123 tg3_stop_fw(tp);
9124
9125 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9126
Joe Perches63c3a662011-04-26 08:12:10 +00009127 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009128 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009129
Matt Carlson699c0192010-12-06 08:28:51 +00009130 /* Enable MAC control of LPI */
9131 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009132 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
9133 TG3_CPMU_EEE_LNKIDL_UART_IDL;
Joe Perches41535772013-02-16 11:20:04 +00009134 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00009135 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
9136
9137 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
Matt Carlson699c0192010-12-06 08:28:51 +00009138
9139 tw32_f(TG3_CPMU_EEE_CTRL,
9140 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
9141
Matt Carlsona386b902010-12-06 08:28:53 +00009142 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
9143 TG3_CPMU_EEEMD_LPI_IN_TX |
9144 TG3_CPMU_EEEMD_LPI_IN_RX |
9145 TG3_CPMU_EEEMD_EEE_ENABLE;
9146
Joe Perches41535772013-02-16 11:20:04 +00009147 if (tg3_asic_rev(tp) != ASIC_REV_5717)
Matt Carlsona386b902010-12-06 08:28:53 +00009148 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
9149
Joe Perches63c3a662011-04-26 08:12:10 +00009150 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00009151 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
9152
9153 tw32_f(TG3_CPMU_EEE_MODE, val);
9154
9155 tw32_f(TG3_CPMU_EEE_DBTMR1,
9156 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
9157 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
9158
9159 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00009160 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00009161 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00009162 }
9163
Matt Carlson603f1172010-02-12 14:47:10 +00009164 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009165 tg3_phy_reset(tp);
9166
Linus Torvalds1da177e2005-04-16 15:20:36 -07009167 err = tg3_chip_reset(tp);
9168 if (err)
9169 return err;
9170
9171 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9172
Joe Perches41535772013-02-16 11:20:04 +00009173 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009174 val = tr32(TG3_CPMU_CTRL);
9175 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9176 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009177
9178 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9179 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9180 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9181 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9182
9183 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9184 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9185 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9186 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9187
9188 val = tr32(TG3_CPMU_HST_ACC);
9189 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9190 val |= CPMU_HST_ACC_MACCLK_6_25;
9191 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009192 }
9193
Joe Perches41535772013-02-16 11:20:04 +00009194 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009195 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9196 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9197 PCIE_PWR_MGMT_L1_THRESH_4MS;
9198 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009199
9200 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9201 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9202
9203 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009204
Matt Carlsonf40386c2009-11-02 14:24:02 +00009205 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9206 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009207 }
9208
Joe Perches63c3a662011-04-26 08:12:10 +00009209 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009210 u32 grc_mode = tr32(GRC_MODE);
9211
9212 /* Access the lower 1K of PL PCIE block registers. */
9213 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9214 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9215
9216 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9217 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9218 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9219
9220 tw32(GRC_MODE, grc_mode);
9221 }
9222
Matt Carlson55086ad2011-12-14 11:09:59 +00009223 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009224 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009225 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009226
Matt Carlson5093eed2010-11-24 08:31:45 +00009227 /* Access the lower 1K of PL PCIE block registers. */
9228 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9229 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009230
Matt Carlson5093eed2010-11-24 08:31:45 +00009231 val = tr32(TG3_PCIE_TLDLPL_PORT +
9232 TG3_PCIE_PL_LO_PHYCTL5);
9233 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9234 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009235
Matt Carlson5093eed2010-11-24 08:31:45 +00009236 tw32(GRC_MODE, grc_mode);
9237 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009238
Joe Perches41535772013-02-16 11:20:04 +00009239 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009240 u32 grc_mode;
9241
9242 /* Fix transmit hangs */
9243 val = tr32(TG3_CPMU_PADRNG_CTL);
9244 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9245 tw32(TG3_CPMU_PADRNG_CTL, val);
9246
9247 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009248
9249 /* Access the lower 1K of DL PCIE block registers. */
9250 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9251 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9252
9253 val = tr32(TG3_PCIE_TLDLPL_PORT +
9254 TG3_PCIE_DL_LO_FTSMAX);
9255 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9256 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9257 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9258
9259 tw32(GRC_MODE, grc_mode);
9260 }
9261
Matt Carlsona977dbe2010-04-12 06:58:26 +00009262 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9263 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9264 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9265 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009266 }
9267
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 /* This works around an issue with Athlon chipsets on
9269 * B3 tigon3 silicon. This bit has no effect on any
9270 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009271 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009272 */
Joe Perches63c3a662011-04-26 08:12:10 +00009273 if (!tg3_flag(tp, CPMU_PRESENT)) {
9274 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009275 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9276 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278
Joe Perches41535772013-02-16 11:20:04 +00009279 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009280 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281 val = tr32(TG3PCI_PCISTATE);
9282 val |= PCISTATE_RETRY_SAME_DMA;
9283 tw32(TG3PCI_PCISTATE, val);
9284 }
9285
Joe Perches63c3a662011-04-26 08:12:10 +00009286 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009287 /* Allow reads and writes to the
9288 * APE register and memory space.
9289 */
9290 val = tr32(TG3PCI_PCISTATE);
9291 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009292 PCISTATE_ALLOW_APE_SHMEM_WR |
9293 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009294 tw32(TG3PCI_PCISTATE, val);
9295 }
9296
Joe Perches41535772013-02-16 11:20:04 +00009297 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009298 /* Enable some hw fixes. */
9299 val = tr32(TG3PCI_MSI_DATA);
9300 val |= (1 << 26) | (1 << 28) | (1 << 29);
9301 tw32(TG3PCI_MSI_DATA, val);
9302 }
9303
9304 /* Descriptor ring init may make accesses to the
9305 * NIC SRAM area to setup the TX descriptors, so we
9306 * can only do this after the hardware has been
9307 * successfully reset.
9308 */
Michael Chan32d8c572006-07-25 16:38:29 -07009309 err = tg3_init_rings(tp);
9310 if (err)
9311 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312
Joe Perches63c3a662011-04-26 08:12:10 +00009313 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009314 val = tr32(TG3PCI_DMA_RW_CTRL) &
9315 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009316 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009317 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009318 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009319 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9320 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009321 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009322 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009323 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9324 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009325 /* This value is determined during the probe time DMA
9326 * engine test, tg3_test_dma.
9327 */
9328 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330
9331 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9332 GRC_MODE_4X_NIC_SEND_RINGS |
9333 GRC_MODE_NO_TX_PHDR_CSUM |
9334 GRC_MODE_NO_RX_PHDR_CSUM);
9335 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009336
9337 /* Pseudo-header checksum is done by hardware logic and not
9338 * the offload processers, so make the chip do the pseudo-
9339 * header checksums on receive. For transmit it is more
9340 * convenient to do the pseudo-header checksum in software
9341 * as Linux does that on transmit for us in all cases.
9342 */
9343 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009345 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9346 if (tp->rxptpctl)
9347 tw32(TG3_RX_PTP_CTL,
9348 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9349
9350 if (tg3_flag(tp, PTP_CAPABLE))
9351 val |= GRC_MODE_TIME_SYNC_ENABLE;
9352
9353 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354
9355 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9356 val = tr32(GRC_MISC_CFG);
9357 val &= ~0xff;
9358 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9359 tw32(GRC_MISC_CFG, val);
9360
9361 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009362 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +00009364 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +00009366 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9368 else
9369 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9370 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9371 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009372 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009373 int fw_len;
9374
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009375 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9377 tw32(BUFMGR_MB_POOL_ADDR,
9378 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9379 tw32(BUFMGR_MB_POOL_SIZE,
9380 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382
Michael Chan0f893dc2005-07-25 12:30:38 -07009383 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009384 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9385 tp->bufmgr_config.mbuf_read_dma_low_water);
9386 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9387 tp->bufmgr_config.mbuf_mac_rx_low_water);
9388 tw32(BUFMGR_MB_HIGH_WATER,
9389 tp->bufmgr_config.mbuf_high_water);
9390 } else {
9391 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9392 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9393 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9394 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9395 tw32(BUFMGR_MB_HIGH_WATER,
9396 tp->bufmgr_config.mbuf_high_water_jumbo);
9397 }
9398 tw32(BUFMGR_DMA_LOW_WATER,
9399 tp->bufmgr_config.dma_low_water);
9400 tw32(BUFMGR_DMA_HIGH_WATER,
9401 tp->bufmgr_config.dma_high_water);
9402
Matt Carlsond309a462010-09-30 10:34:31 +00009403 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009404 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +00009405 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +00009406 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
9407 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9408 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +00009409 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009410 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009411 for (i = 0; i < 2000; i++) {
9412 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9413 break;
9414 udelay(10);
9415 }
9416 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009417 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009418 return -ENODEV;
9419 }
9420
Joe Perches41535772013-02-16 11:20:04 +00009421 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009422 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009423
Matt Carlsoneb07a942011-04-20 07:57:36 +00009424 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009425
9426 /* Initialize TG3_BDINFO's at:
9427 * RCVDBDI_STD_BD: standard eth size rx ring
9428 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9429 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9430 *
9431 * like so:
9432 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9433 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9434 * ring attribute flags
9435 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9436 *
9437 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9438 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9439 *
9440 * The size of each ring is fixed in the firmware, but the location is
9441 * configurable.
9442 */
9443 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009444 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009445 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009446 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009447 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009448 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9449 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009450
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009451 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009452 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009453 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9454 BDINFO_FLAGS_DISABLED);
9455
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009456 /* Program the jumbo buffer descriptor ring control
9457 * blocks on those devices that have them.
9458 */
Joe Perches41535772013-02-16 11:20:04 +00009459 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009460 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009461
Joe Perches63c3a662011-04-26 08:12:10 +00009462 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009464 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009465 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009466 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009467 val = TG3_RX_JMB_RING_SIZE(tp) <<
9468 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009469 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009470 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009471 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +00009472 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009473 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +00009474 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9475 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009476 } else {
9477 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9478 BDINFO_FLAGS_DISABLED);
9479 }
9480
Joe Perches63c3a662011-04-26 08:12:10 +00009481 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009482 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009483 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9484 val |= (TG3_RX_STD_DMA_SZ << 2);
9485 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009486 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009487 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009488 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009489
9490 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009491
Matt Carlson411da642009-11-13 13:03:46 +00009492 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00009493 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009494
Joe Perches63c3a662011-04-26 08:12:10 +00009495 tpr->rx_jmb_prod_idx =
9496 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00009497 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009498
Matt Carlson2d31eca2009-09-01 12:53:31 +00009499 tg3_rings_reset(tp);
9500
Linus Torvalds1da177e2005-04-16 15:20:36 -07009501 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07009502 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009503
9504 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009505 tw32(MAC_RX_MTU_SIZE,
9506 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507
9508 /* The slot time is changed by tg3_setup_phy if we
9509 * run at gigabit with half duplex.
9510 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009511 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9512 (6 << TX_LENGTHS_IPG_SHIFT) |
9513 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9514
Joe Perches41535772013-02-16 11:20:04 +00009515 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9516 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009517 val |= tr32(MAC_TX_LENGTHS) &
9518 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9519 TX_LENGTHS_CNT_DWN_VAL_MSK);
9520
9521 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009522
9523 /* Receive rules. */
9524 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9525 tw32(RCVLPC_CONFIG, 0x0181);
9526
9527 /* Calculate RDMAC_MODE setting early, we need it to determine
9528 * the RCVLPC_STATE_ENABLE mask.
9529 */
9530 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9531 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9532 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9533 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9534 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009535
Joe Perches41535772013-02-16 11:20:04 +00009536 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009537 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9538
Joe Perches41535772013-02-16 11:20:04 +00009539 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
9540 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9541 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009542 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9543 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9544 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9545
Joe Perches41535772013-02-16 11:20:04 +00009546 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9547 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009548 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009549 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009550 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9551 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009552 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009553 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9554 }
9555 }
9556
Joe Perches63c3a662011-04-26 08:12:10 +00009557 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009558 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9559
Joe Perches41535772013-02-16 11:20:04 +00009560 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009561 tp->dma_limit = 0;
9562 if (tp->dev->mtu <= ETH_DATA_LEN) {
9563 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
9564 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
9565 }
9566 }
9567
Joe Perches63c3a662011-04-26 08:12:10 +00009568 if (tg3_flag(tp, HW_TSO_1) ||
9569 tg3_flag(tp, HW_TSO_2) ||
9570 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009571 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9572
Matt Carlson108a6c12011-05-19 12:12:47 +00009573 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009574 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9575 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -08009576 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009577
Joe Perches41535772013-02-16 11:20:04 +00009578 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9579 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009580 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9581
Joe Perches41535772013-02-16 11:20:04 +00009582 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
9583 tg3_asic_rev(tp) == ASIC_REV_5784 ||
9584 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9585 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009586 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009587 u32 tgtreg;
9588
Joe Perches41535772013-02-16 11:20:04 +00009589 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009590 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
9591 else
9592 tgtreg = TG3_RDMA_RSRVCTRL_REG;
9593
9594 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +00009595 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9596 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009597 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9598 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9599 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9600 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9601 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9602 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009603 }
Michael Chanc65a17f2013-01-06 12:51:07 +00009604 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009605 }
9606
Joe Perches41535772013-02-16 11:20:04 +00009607 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
9608 tg3_asic_rev(tp) == ASIC_REV_5720 ||
9609 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009610 u32 tgtreg;
9611
Joe Perches41535772013-02-16 11:20:04 +00009612 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009613 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
9614 else
9615 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
9616
9617 val = tr32(tgtreg);
9618 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +00009619 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9620 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9621 }
9622
Linus Torvalds1da177e2005-04-16 15:20:36 -07009623 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009624 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009625 val = tr32(RCVLPC_STATS_ENABLE);
9626 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9627 tw32(RCVLPC_STATS_ENABLE, val);
9628 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009629 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009630 val = tr32(RCVLPC_STATS_ENABLE);
9631 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9632 tw32(RCVLPC_STATS_ENABLE, val);
9633 } else {
9634 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9635 }
9636 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9637 tw32(SNDDATAI_STATSENAB, 0xffffff);
9638 tw32(SNDDATAI_STATSCTRL,
9639 (SNDDATAI_SCTRL_ENABLE |
9640 SNDDATAI_SCTRL_FASTUPD));
9641
9642 /* Setup host coalescing engine. */
9643 tw32(HOSTCC_MODE, 0);
9644 for (i = 0; i < 2000; i++) {
9645 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9646 break;
9647 udelay(10);
9648 }
9649
Michael Chand244c892005-07-05 14:42:33 -07009650 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009651
Joe Perches63c3a662011-04-26 08:12:10 +00009652 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009653 /* Status/statistics block address. See tg3_timer,
9654 * the tg3_periodic_fetch_stats call there, and
9655 * tg3_get_stats to see how this works for 5705/5750 chips.
9656 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009657 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9658 ((u64) tp->stats_mapping >> 32));
9659 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9660 ((u64) tp->stats_mapping & 0xffffffff));
9661 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009662
Linus Torvalds1da177e2005-04-16 15:20:36 -07009663 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009664
9665 /* Clear statistics and status block memory areas */
9666 for (i = NIC_SRAM_STATS_BLK;
9667 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9668 i += sizeof(u32)) {
9669 tg3_write_mem(tp, i, 0);
9670 udelay(40);
9671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009672 }
9673
9674 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9675
9676 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9677 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009678 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009679 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9680
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009681 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9682 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009683 /* reset to prevent losing 1st rx packet intermittently */
9684 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9685 udelay(10);
9686 }
9687
Matt Carlson3bda1252008-08-15 14:08:22 -07009688 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009689 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9690 MAC_MODE_FHDE_ENABLE;
9691 if (tg3_flag(tp, ENABLE_APE))
9692 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009693 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009694 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +00009695 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009696 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009697 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9698 udelay(40);
9699
Michael Chan314fba32005-04-21 17:07:04 -07009700 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009701 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009702 * register to preserve the GPIO settings for LOMs. The GPIOs,
9703 * whether used as inputs or outputs, are set by boot code after
9704 * reset.
9705 */
Joe Perches63c3a662011-04-26 08:12:10 +00009706 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009707 u32 gpio_mask;
9708
Michael Chan9d26e212006-12-07 00:21:14 -08009709 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9710 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9711 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009712
Joe Perches41535772013-02-16 11:20:04 +00009713 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -07009714 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9715 GRC_LCLCTRL_GPIO_OUTPUT3;
9716
Joe Perches41535772013-02-16 11:20:04 +00009717 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -08009718 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9719
Gary Zambranoaaf84462007-05-05 11:51:45 -07009720 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009721 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9722
9723 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009724 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009725 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9726 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009728 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9729 udelay(100);
9730
Matt Carlsonc3b50032012-01-17 15:27:23 +00009731 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009732 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009733 val |= MSGINT_MODE_ENABLE;
9734 if (tp->irq_cnt > 1)
9735 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009736 if (!tg3_flag(tp, 1SHOT_MSI))
9737 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009738 tw32(MSGINT_MODE, val);
9739 }
9740
Joe Perches63c3a662011-04-26 08:12:10 +00009741 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009742 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9743 udelay(40);
9744 }
9745
9746 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9747 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9748 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9749 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9750 WDMAC_MODE_LNGREAD_ENAB);
9751
Joe Perches41535772013-02-16 11:20:04 +00009752 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9753 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009754 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009755 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
9756 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009757 /* nothing */
9758 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009759 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009760 val |= WDMAC_MODE_RX_ACCEL;
9761 }
9762 }
9763
Michael Chand9ab5ad12006-03-20 22:27:35 -08009764 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009765 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009766 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08009767
Joe Perches41535772013-02-16 11:20:04 +00009768 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +00009769 val |= WDMAC_MODE_BURST_ALL_DATA;
9770
Linus Torvalds1da177e2005-04-16 15:20:36 -07009771 tw32_f(WDMAC_MODE, val);
9772 udelay(40);
9773
Joe Perches63c3a662011-04-26 08:12:10 +00009774 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009775 u16 pcix_cmd;
9776
9777 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9778 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +00009779 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009780 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9781 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +00009782 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009783 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9784 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 }
Matt Carlson9974a352007-10-07 23:27:28 -07009786 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9787 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 }
9789
9790 tw32_f(RDMAC_MODE, rdmac_mode);
9791 udelay(40);
9792
Joe Perches41535772013-02-16 11:20:04 +00009793 if (tg3_asic_rev(tp) == ASIC_REV_5719) {
Michael Chan091f0ea2012-07-29 19:15:43 +00009794 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
9795 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
9796 break;
9797 }
9798 if (i < TG3_NUM_RDMA_CHANNELS) {
9799 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9800 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
9801 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9802 tg3_flag_set(tp, 5719_RDMA_BUG);
9803 }
9804 }
9805
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009807 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009809
Joe Perches41535772013-02-16 11:20:04 +00009810 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009811 tw32(SNDDATAC_MODE,
9812 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9813 else
9814 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9815
Linus Torvalds1da177e2005-04-16 15:20:36 -07009816 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9817 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009818 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009819 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009820 val |= RCVDBDI_MODE_LRG_RING_SZ;
9821 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009822 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009823 if (tg3_flag(tp, HW_TSO_1) ||
9824 tg3_flag(tp, HW_TSO_2) ||
9825 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009827 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009828 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009829 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9830 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009831 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9832
Joe Perches41535772013-02-16 11:20:04 +00009833 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834 err = tg3_load_5701_a0_firmware_fix(tp);
9835 if (err)
9836 return err;
9837 }
9838
Joe Perches63c3a662011-04-26 08:12:10 +00009839 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 err = tg3_load_tso_firmware(tp);
9841 if (err)
9842 return err;
9843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844
9845 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009846
Joe Perches63c3a662011-04-26 08:12:10 +00009847 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009848 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +00009849 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009850
Joe Perches41535772013-02-16 11:20:04 +00009851 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9852 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00009853 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9854 tp->tx_mode &= ~val;
9855 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9856 }
9857
Linus Torvalds1da177e2005-04-16 15:20:36 -07009858 tw32_f(MAC_TX_MODE, tp->tx_mode);
9859 udelay(100);
9860
Joe Perches63c3a662011-04-26 08:12:10 +00009861 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009862 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009863
9864 /* Setup the "secret" hash key. */
9865 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9866 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9867 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9868 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9869 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9870 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9871 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9872 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9873 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9874 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9875 }
9876
Linus Torvalds1da177e2005-04-16 15:20:36 -07009877 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009878 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009879 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9880
Joe Perches63c3a662011-04-26 08:12:10 +00009881 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009882 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9883 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9884 RX_MODE_RSS_IPV6_HASH_EN |
9885 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9886 RX_MODE_RSS_IPV4_HASH_EN |
9887 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9888
Linus Torvalds1da177e2005-04-16 15:20:36 -07009889 tw32_f(MAC_RX_MODE, tp->rx_mode);
9890 udelay(10);
9891
Linus Torvalds1da177e2005-04-16 15:20:36 -07009892 tw32(MAC_LED_CTRL, tp->led_ctrl);
9893
9894 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009895 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009896 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9897 udelay(10);
9898 }
9899 tw32_f(MAC_RX_MODE, tp->rx_mode);
9900 udelay(10);
9901
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009902 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00009903 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
9904 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009905 /* Set drive transmission level to 1.2V */
9906 /* only if the signal pre-emphasis bit is not set */
9907 val = tr32(MAC_SERDES_CFG);
9908 val &= 0xfffff000;
9909 val |= 0x880;
9910 tw32(MAC_SERDES_CFG, val);
9911 }
Joe Perches41535772013-02-16 11:20:04 +00009912 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009913 tw32(MAC_SERDES_CFG, 0x616000);
9914 }
9915
9916 /* Prevent chip from dropping frames when flow control
9917 * is enabled.
9918 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009919 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009920 val = 1;
9921 else
9922 val = 2;
9923 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009924
Joe Perches41535772013-02-16 11:20:04 +00009925 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009926 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009927 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009928 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009929 }
9930
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009931 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +00009932 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009933 u32 tmp;
9934
9935 tmp = tr32(SERDES_RX_CTRL);
9936 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9937 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9938 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9939 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9940 }
9941
Joe Perches63c3a662011-04-26 08:12:10 +00009942 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009943 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +00009944 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009945
Matt Carlsondd477002008-05-25 23:45:58 -07009946 err = tg3_setup_phy(tp, 0);
9947 if (err)
9948 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009949
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009950 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9951 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009952 u32 tmp;
9953
9954 /* Clear CRC stats. */
9955 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9956 tg3_writephy(tp, MII_TG3_TEST1,
9957 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009958 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009960 }
9961 }
9962
9963 __tg3_set_rx_mode(tp->dev);
9964
9965 /* Initialize receive rules. */
9966 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9967 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9968 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9969 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9970
Joe Perches63c3a662011-04-26 08:12:10 +00009971 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009972 limit = 8;
9973 else
9974 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009975 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009976 limit -= 4;
9977 switch (limit) {
9978 case 16:
9979 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9980 case 15:
9981 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9982 case 14:
9983 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9984 case 13:
9985 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9986 case 12:
9987 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9988 case 11:
9989 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9990 case 10:
9991 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9992 case 9:
9993 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9994 case 8:
9995 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9996 case 7:
9997 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9998 case 6:
9999 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10000 case 5:
10001 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10002 case 4:
10003 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10004 case 3:
10005 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10006 case 2:
10007 case 1:
10008
10009 default:
10010 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012
Joe Perches63c3a662011-04-26 08:12:10 +000010013 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010014 /* Write our heartbeat update interval to APE. */
10015 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10016 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010017
Linus Torvalds1da177e2005-04-16 15:20:36 -070010018 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10019
Linus Torvalds1da177e2005-04-16 15:20:36 -070010020 return 0;
10021}
10022
10023/* Called at device open time to get the chip ready for
10024 * packet processing. Invoked with tp->lock held.
10025 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010026static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010027{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010028 tg3_switch_clocks(tp);
10029
10030 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10031
Matt Carlson2f751b62008-08-04 23:17:34 -070010032 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010033}
10034
Michael Chanaed93e02012-07-16 16:24:02 +000010035static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10036{
10037 int i;
10038
10039 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10040 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10041
10042 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10043 off += len;
10044
10045 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10046 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10047 memset(ocir, 0, TG3_OCIR_LEN);
10048 }
10049}
10050
10051/* sysfs attributes for hwmon */
10052static ssize_t tg3_show_temp(struct device *dev,
10053 struct device_attribute *devattr, char *buf)
10054{
10055 struct pci_dev *pdev = to_pci_dev(dev);
10056 struct net_device *netdev = pci_get_drvdata(pdev);
10057 struct tg3 *tp = netdev_priv(netdev);
10058 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
10059 u32 temperature;
10060
10061 spin_lock_bh(&tp->lock);
10062 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10063 sizeof(temperature));
10064 spin_unlock_bh(&tp->lock);
10065 return sprintf(buf, "%u\n", temperature);
10066}
10067
10068
10069static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10070 TG3_TEMP_SENSOR_OFFSET);
10071static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10072 TG3_TEMP_CAUTION_OFFSET);
10073static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10074 TG3_TEMP_MAX_OFFSET);
10075
10076static struct attribute *tg3_attributes[] = {
10077 &sensor_dev_attr_temp1_input.dev_attr.attr,
10078 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10079 &sensor_dev_attr_temp1_max.dev_attr.attr,
10080 NULL
10081};
10082
10083static const struct attribute_group tg3_group = {
10084 .attrs = tg3_attributes,
10085};
10086
Michael Chanaed93e02012-07-16 16:24:02 +000010087static void tg3_hwmon_close(struct tg3 *tp)
10088{
Michael Chanaed93e02012-07-16 16:24:02 +000010089 if (tp->hwmon_dev) {
10090 hwmon_device_unregister(tp->hwmon_dev);
10091 tp->hwmon_dev = NULL;
10092 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
10093 }
Michael Chanaed93e02012-07-16 16:24:02 +000010094}
10095
10096static void tg3_hwmon_open(struct tg3 *tp)
10097{
Michael Chanaed93e02012-07-16 16:24:02 +000010098 int i, err;
10099 u32 size = 0;
10100 struct pci_dev *pdev = tp->pdev;
10101 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10102
10103 tg3_sd_scan_scratchpad(tp, ocirs);
10104
10105 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10106 if (!ocirs[i].src_data_length)
10107 continue;
10108
10109 size += ocirs[i].src_hdr_length;
10110 size += ocirs[i].src_data_length;
10111 }
10112
10113 if (!size)
10114 return;
10115
10116 /* Register hwmon sysfs hooks */
10117 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
10118 if (err) {
10119 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
10120 return;
10121 }
10122
10123 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
10124 if (IS_ERR(tp->hwmon_dev)) {
10125 tp->hwmon_dev = NULL;
10126 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
10127 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
10128 }
Michael Chanaed93e02012-07-16 16:24:02 +000010129}
10130
10131
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132#define TG3_STAT_ADD32(PSTAT, REG) \
10133do { u32 __val = tr32(REG); \
10134 (PSTAT)->low += __val; \
10135 if ((PSTAT)->low < __val) \
10136 (PSTAT)->high += 1; \
10137} while (0)
10138
10139static void tg3_periodic_fetch_stats(struct tg3 *tp)
10140{
10141 struct tg3_hw_stats *sp = tp->hw_stats;
10142
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010143 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010144 return;
10145
10146 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10147 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10148 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10149 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10150 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10151 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10152 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10153 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10154 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10155 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10156 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10157 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10158 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +000010159 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
10160 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10161 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10162 u32 val;
10163
10164 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10165 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
10166 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10167 tg3_flag_clear(tp, 5719_RDMA_BUG);
10168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169
10170 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10171 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10172 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10173 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10174 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10175 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10176 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10177 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10178 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10179 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10180 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10181 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10182 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10183 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010184
10185 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010186 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
10187 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10188 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010189 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10190 } else {
10191 u32 val = tr32(HOSTCC_FLOW_ATTN);
10192 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10193 if (val) {
10194 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10195 sp->rx_discards.low += val;
10196 if (sp->rx_discards.low < val)
10197 sp->rx_discards.high += 1;
10198 }
10199 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10200 }
Michael Chan463d3052006-05-22 16:36:27 -070010201 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010202}
10203
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010204static void tg3_chk_missed_msi(struct tg3 *tp)
10205{
10206 u32 i;
10207
10208 for (i = 0; i < tp->irq_cnt; i++) {
10209 struct tg3_napi *tnapi = &tp->napi[i];
10210
10211 if (tg3_has_work(tnapi)) {
10212 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10213 tnapi->last_tx_cons == tnapi->tx_cons) {
10214 if (tnapi->chk_msi_cnt < 1) {
10215 tnapi->chk_msi_cnt++;
10216 return;
10217 }
Matt Carlson7f230732011-08-31 11:44:48 +000010218 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010219 }
10220 }
10221 tnapi->chk_msi_cnt = 0;
10222 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10223 tnapi->last_tx_cons = tnapi->tx_cons;
10224 }
10225}
10226
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227static void tg3_timer(unsigned long __opaque)
10228{
10229 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010230
Matt Carlson5b190622011-11-04 09:15:04 +000010231 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010232 goto restart_timer;
10233
David S. Millerf47c11e2005-06-24 20:18:35 -070010234 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010235
Joe Perches41535772013-02-16 11:20:04 +000010236 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010237 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010238 tg3_chk_missed_msi(tp);
10239
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010240 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10241 /* BCM4785: Flush posted writes from GbE to host memory. */
10242 tr32(HOSTCC_MODE);
10243 }
10244
Joe Perches63c3a662011-04-26 08:12:10 +000010245 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010246 /* All of this garbage is because when using non-tagged
10247 * IRQ status the mailbox/status_block protocol the chip
10248 * uses with the cpu is race prone.
10249 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010250 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010251 tw32(GRC_LOCAL_CTRL,
10252 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10253 } else {
10254 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010255 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010257
David S. Millerfac9b832005-05-18 22:46:34 -070010258 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010259 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010260 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010261 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010263 }
10264
Linus Torvalds1da177e2005-04-16 15:20:36 -070010265 /* This part only runs once per second. */
10266 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010267 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010268 tg3_periodic_fetch_stats(tp);
10269
Matt Carlsonb0c59432011-05-19 12:12:48 +000010270 if (tp->setlpicnt && !--tp->setlpicnt)
10271 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010272
Joe Perches63c3a662011-04-26 08:12:10 +000010273 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010274 u32 mac_stat;
10275 int phy_event;
10276
10277 mac_stat = tr32(MAC_STATUS);
10278
10279 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010280 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010281 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10282 phy_event = 1;
10283 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10284 phy_event = 1;
10285
10286 if (phy_event)
10287 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000010288 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010289 u32 mac_stat = tr32(MAC_STATUS);
10290 int need_setup = 0;
10291
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010292 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010293 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10294 need_setup = 1;
10295 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010296 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010297 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10298 MAC_STATUS_SIGNAL_DET))) {
10299 need_setup = 1;
10300 }
10301 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010302 if (!tp->serdes_counter) {
10303 tw32_f(MAC_MODE,
10304 (tp->mac_mode &
10305 ~MAC_MODE_PORT_MODE_MASK));
10306 udelay(40);
10307 tw32_f(MAC_MODE, tp->mac_mode);
10308 udelay(40);
10309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010310 tg3_setup_phy(tp, 0);
10311 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010312 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010313 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010314 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316
10317 tp->timer_counter = tp->timer_multiplier;
10318 }
10319
Michael Chan130b8e42006-09-27 16:00:40 -070010320 /* Heartbeat is only sent once every 2 seconds.
10321 *
10322 * The heartbeat is to tell the ASF firmware that the host
10323 * driver is still alive. In the event that the OS crashes,
10324 * ASF needs to reset the hardware to free up the FIFO space
10325 * that may be filled with rx packets destined for the host.
10326 * If the FIFO is full, ASF will no longer function properly.
10327 *
10328 * Unintended resets have been reported on real time kernels
10329 * where the timer doesn't run on time. Netpoll will also have
10330 * same problem.
10331 *
10332 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10333 * to check the ring condition when the heartbeat is expiring
10334 * before doing the reset. This will prevent most unintended
10335 * resets.
10336 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010337 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010338 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010339 tg3_wait_for_event_ack(tp);
10340
Michael Chanbbadf502006-04-06 21:46:34 -070010341 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010342 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010343 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010344 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10345 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010346
10347 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010348 }
10349 tp->asf_counter = tp->asf_multiplier;
10350 }
10351
David S. Millerf47c11e2005-06-24 20:18:35 -070010352 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010353
Michael Chanf475f162006-03-27 23:20:14 -080010354restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010355 tp->timer.expires = jiffies + tp->timer_offset;
10356 add_timer(&tp->timer);
10357}
10358
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010359static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010360{
10361 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000010362 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000010363 !tg3_flag(tp, 57765_CLASS))
10364 tp->timer_offset = HZ;
10365 else
10366 tp->timer_offset = HZ / 10;
10367
10368 BUG_ON(tp->timer_offset > HZ);
10369
10370 tp->timer_multiplier = (HZ / tp->timer_offset);
10371 tp->asf_multiplier = (HZ / tp->timer_offset) *
10372 TG3_FW_UPDATE_FREQ_SEC;
10373
10374 init_timer(&tp->timer);
10375 tp->timer.data = (unsigned long) tp;
10376 tp->timer.function = tg3_timer;
10377}
10378
10379static void tg3_timer_start(struct tg3 *tp)
10380{
10381 tp->asf_counter = tp->asf_multiplier;
10382 tp->timer_counter = tp->timer_multiplier;
10383
10384 tp->timer.expires = jiffies + tp->timer_offset;
10385 add_timer(&tp->timer);
10386}
10387
10388static void tg3_timer_stop(struct tg3 *tp)
10389{
10390 del_timer_sync(&tp->timer);
10391}
10392
10393/* Restart hardware after configuration changes, self-test, etc.
10394 * Invoked with tp->lock held.
10395 */
10396static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
10397 __releases(tp->lock)
10398 __acquires(tp->lock)
10399{
10400 int err;
10401
10402 err = tg3_init_hw(tp, reset_phy);
10403 if (err) {
10404 netdev_err(tp->dev,
10405 "Failed to re-initialize device, aborting\n");
10406 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10407 tg3_full_unlock(tp);
10408 tg3_timer_stop(tp);
10409 tp->irq_sync = 0;
10410 tg3_napi_enable(tp);
10411 dev_close(tp->dev);
10412 tg3_full_lock(tp, 0);
10413 }
10414 return err;
10415}
10416
10417static void tg3_reset_task(struct work_struct *work)
10418{
10419 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10420 int err;
10421
10422 tg3_full_lock(tp, 0);
10423
10424 if (!netif_running(tp->dev)) {
10425 tg3_flag_clear(tp, RESET_TASK_PENDING);
10426 tg3_full_unlock(tp);
10427 return;
10428 }
10429
10430 tg3_full_unlock(tp);
10431
10432 tg3_phy_stop(tp);
10433
10434 tg3_netif_stop(tp);
10435
10436 tg3_full_lock(tp, 1);
10437
10438 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10439 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10440 tp->write32_rx_mbox = tg3_write_flush_reg32;
10441 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10442 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10443 }
10444
10445 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
10446 err = tg3_init_hw(tp, 1);
10447 if (err)
10448 goto out;
10449
10450 tg3_netif_start(tp);
10451
10452out:
10453 tg3_full_unlock(tp);
10454
10455 if (!err)
10456 tg3_phy_start(tp);
10457
10458 tg3_flag_clear(tp, RESET_TASK_PENDING);
10459}
10460
Matt Carlson4f125f42009-09-01 12:55:02 +000010461static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010462{
David Howells7d12e782006-10-05 14:55:46 +010010463 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010464 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010465 char *name;
10466 struct tg3_napi *tnapi = &tp->napi[irq_num];
10467
10468 if (tp->irq_cnt == 1)
10469 name = tp->dev->name;
10470 else {
10471 name = &tnapi->irq_lbl[0];
10472 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10473 name[IFNAMSIZ-1] = 0;
10474 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010475
Joe Perches63c3a662011-04-26 08:12:10 +000010476 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010477 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010478 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010479 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010480 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010481 } else {
10482 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010483 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010484 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010485 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010486 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010487
10488 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010489}
10490
Michael Chan79381092005-04-21 17:13:59 -070010491static int tg3_test_interrupt(struct tg3 *tp)
10492{
Matt Carlson09943a12009-08-28 14:01:57 +000010493 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010494 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010495 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010496 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010497
Michael Chand4bc3922005-05-29 14:59:20 -070010498 if (!netif_running(dev))
10499 return -ENODEV;
10500
Michael Chan79381092005-04-21 17:13:59 -070010501 tg3_disable_ints(tp);
10502
Matt Carlson4f125f42009-09-01 12:55:02 +000010503 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010504
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010505 /*
10506 * Turn off MSI one shot mode. Otherwise this test has no
10507 * observable way to know whether the interrupt was delivered.
10508 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010509 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010510 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10511 tw32(MSGINT_MODE, val);
10512 }
10513
Matt Carlson4f125f42009-09-01 12:55:02 +000010514 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010515 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010516 if (err)
10517 return err;
10518
Matt Carlson898a56f2009-08-28 14:02:40 +000010519 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010520 tg3_enable_ints(tp);
10521
10522 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010523 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010524
10525 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010526 u32 int_mbox, misc_host_ctrl;
10527
Matt Carlson898a56f2009-08-28 14:02:40 +000010528 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010529 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10530
10531 if ((int_mbox != 0) ||
10532 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10533 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010534 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010535 }
10536
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010537 if (tg3_flag(tp, 57765_PLUS) &&
10538 tnapi->hw_status->status_tag != tnapi->last_tag)
10539 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10540
Michael Chan79381092005-04-21 17:13:59 -070010541 msleep(10);
10542 }
10543
10544 tg3_disable_ints(tp);
10545
Matt Carlson4f125f42009-09-01 12:55:02 +000010546 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010547
Matt Carlson4f125f42009-09-01 12:55:02 +000010548 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010549
10550 if (err)
10551 return err;
10552
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010553 if (intr_ok) {
10554 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010555 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010556 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10557 tw32(MSGINT_MODE, val);
10558 }
Michael Chan79381092005-04-21 17:13:59 -070010559 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010560 }
Michael Chan79381092005-04-21 17:13:59 -070010561
10562 return -EIO;
10563}
10564
10565/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10566 * successfully restored
10567 */
10568static int tg3_test_msi(struct tg3 *tp)
10569{
Michael Chan79381092005-04-21 17:13:59 -070010570 int err;
10571 u16 pci_cmd;
10572
Joe Perches63c3a662011-04-26 08:12:10 +000010573 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010574 return 0;
10575
10576 /* Turn off SERR reporting in case MSI terminates with Master
10577 * Abort.
10578 */
10579 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10580 pci_write_config_word(tp->pdev, PCI_COMMAND,
10581 pci_cmd & ~PCI_COMMAND_SERR);
10582
10583 err = tg3_test_interrupt(tp);
10584
10585 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10586
10587 if (!err)
10588 return 0;
10589
10590 /* other failures */
10591 if (err != -EIO)
10592 return err;
10593
10594 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010595 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10596 "to INTx mode. Please report this failure to the PCI "
10597 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010598
Matt Carlson4f125f42009-09-01 12:55:02 +000010599 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010600
Michael Chan79381092005-04-21 17:13:59 -070010601 pci_disable_msi(tp->pdev);
10602
Joe Perches63c3a662011-04-26 08:12:10 +000010603 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010604 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010605
Matt Carlson4f125f42009-09-01 12:55:02 +000010606 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010607 if (err)
10608 return err;
10609
10610 /* Need to reset the chip because the MSI cycle may have terminated
10611 * with Master Abort.
10612 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010613 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010614
Michael Chan944d9802005-05-29 14:57:48 -070010615 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010616 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010617
David S. Millerf47c11e2005-06-24 20:18:35 -070010618 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010619
10620 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000010621 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070010622
10623 return err;
10624}
10625
Matt Carlson9e9fd122009-01-19 16:57:45 -080010626static int tg3_request_firmware(struct tg3 *tp)
10627{
Nithin Sujir77997ea2013-03-06 17:02:32 +000010628 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080010629
10630 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010631 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
10632 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010633 return -ENOENT;
10634 }
10635
Nithin Sujir77997ea2013-03-06 17:02:32 +000010636 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080010637
10638 /* Firmware blob starts with version numbers, followed by
10639 * start address and _full_ length including BSS sections
10640 * (which must be longer than the actual data, of course
10641 */
10642
Nithin Sujir77997ea2013-03-06 17:02:32 +000010643 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
10644 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010645 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
10646 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010647 release_firmware(tp->fw);
10648 tp->fw = NULL;
10649 return -EINVAL;
10650 }
10651
10652 /* We no longer need firmware; we have it. */
10653 tp->fw_needed = NULL;
10654 return 0;
10655}
10656
Michael Chan91024262012-09-28 07:12:38 +000010657static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000010658{
Michael Chan91024262012-09-28 07:12:38 +000010659 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000010660
Michael Chan91024262012-09-28 07:12:38 +000010661 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000010662 /* We want as many rx rings enabled as there are cpus.
10663 * In multiqueue MSI-X mode, the first MSI-X vector
10664 * only deals with link interrupts, etc, so we add
10665 * one to the number of vectors we are requesting.
10666 */
Michael Chan91024262012-09-28 07:12:38 +000010667 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010668 }
Matt Carlson679563f2009-09-01 12:55:46 +000010669
Michael Chan91024262012-09-28 07:12:38 +000010670 return irq_cnt;
10671}
10672
10673static bool tg3_enable_msix(struct tg3 *tp)
10674{
10675 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070010676 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000010677
Michael Chan09681692012-09-28 07:12:42 +000010678 tp->txq_cnt = tp->txq_req;
10679 tp->rxq_cnt = tp->rxq_req;
10680 if (!tp->rxq_cnt)
10681 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000010682 if (tp->rxq_cnt > tp->rxq_max)
10683 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000010684
10685 /* Disable multiple TX rings by default. Simple round-robin hardware
10686 * scheduling of the TX rings can cause starvation of rings with
10687 * small packets when other rings have TSO or jumbo packets.
10688 */
10689 if (!tp->txq_req)
10690 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000010691
10692 tp->irq_cnt = tg3_irq_count(tp);
10693
Matt Carlson679563f2009-09-01 12:55:46 +000010694 for (i = 0; i < tp->irq_max; i++) {
10695 msix_ent[i].entry = i;
10696 msix_ent[i].vector = 0;
10697 }
10698
10699 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010700 if (rc < 0) {
10701 return false;
10702 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000010703 if (pci_enable_msix(tp->pdev, msix_ent, rc))
10704 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000010705 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
10706 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000010707 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000010708 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000010709 if (tp->txq_cnt)
10710 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000010711 }
10712
10713 for (i = 0; i < tp->irq_max; i++)
10714 tp->napi[i].irq_vec = msix_ent[i].vector;
10715
Michael Chan49a359e2012-09-28 07:12:37 +000010716 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010717 pci_disable_msix(tp->pdev);
10718 return false;
10719 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000010720
Michael Chan91024262012-09-28 07:12:38 +000010721 if (tp->irq_cnt == 1)
10722 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000010723
Michael Chan91024262012-09-28 07:12:38 +000010724 tg3_flag_set(tp, ENABLE_RSS);
10725
10726 if (tp->txq_cnt > 1)
10727 tg3_flag_set(tp, ENABLE_TSS);
10728
10729 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010730
Matt Carlson679563f2009-09-01 12:55:46 +000010731 return true;
10732}
10733
Matt Carlson07b01732009-08-28 14:01:15 +000010734static void tg3_ints_init(struct tg3 *tp)
10735{
Joe Perches63c3a662011-04-26 08:12:10 +000010736 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
10737 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000010738 /* All MSI supporting chips should support tagged
10739 * status. Assert that this is the case.
10740 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010741 netdev_warn(tp->dev,
10742 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000010743 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000010744 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010745
Joe Perches63c3a662011-04-26 08:12:10 +000010746 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
10747 tg3_flag_set(tp, USING_MSIX);
10748 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
10749 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000010750
Joe Perches63c3a662011-04-26 08:12:10 +000010751 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010752 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000010753 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010754 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010755 if (!tg3_flag(tp, 1SHOT_MSI))
10756 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000010757 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
10758 }
10759defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000010760 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010761 tp->irq_cnt = 1;
10762 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000010763 }
10764
10765 if (tp->irq_cnt == 1) {
10766 tp->txq_cnt = 1;
10767 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010768 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070010769 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000010770 }
Matt Carlson07b01732009-08-28 14:01:15 +000010771}
10772
10773static void tg3_ints_fini(struct tg3 *tp)
10774{
Joe Perches63c3a662011-04-26 08:12:10 +000010775 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000010776 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010777 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000010778 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010779 tg3_flag_clear(tp, USING_MSI);
10780 tg3_flag_clear(tp, USING_MSIX);
10781 tg3_flag_clear(tp, ENABLE_RSS);
10782 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010783}
10784
Matt Carlsonbe947302012-12-03 19:36:57 +000010785static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
10786 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010787{
Michael Chand8f4cd32012-09-28 07:12:40 +000010788 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000010789 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010790
Matt Carlson679563f2009-09-01 12:55:46 +000010791 /*
10792 * Setup interrupts first so we know how
10793 * many NAPI resources to allocate
10794 */
10795 tg3_ints_init(tp);
10796
Matt Carlson90415472011-12-16 13:33:23 +000010797 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010798
Linus Torvalds1da177e2005-04-16 15:20:36 -070010799 /* The placement of this call is tied
10800 * to the setup and use of Host TX descriptors.
10801 */
10802 err = tg3_alloc_consistent(tp);
10803 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010804 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010805
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010806 tg3_napi_init(tp);
10807
Matt Carlsonfed97812009-09-01 13:10:19 +000010808 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010809
Matt Carlson4f125f42009-09-01 12:55:02 +000010810 for (i = 0; i < tp->irq_cnt; i++) {
10811 struct tg3_napi *tnapi = &tp->napi[i];
10812 err = tg3_request_irq(tp, i);
10813 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010814 for (i--; i >= 0; i--) {
10815 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010816 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010817 }
10818 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010819 }
10820 }
Matt Carlson07b01732009-08-28 14:01:15 +000010821
David S. Millerf47c11e2005-06-24 20:18:35 -070010822 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010823
Michael Chand8f4cd32012-09-28 07:12:40 +000010824 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010825 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010826 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010827 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010828 }
10829
David S. Millerf47c11e2005-06-24 20:18:35 -070010830 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010831
Matt Carlson07b01732009-08-28 14:01:15 +000010832 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010833 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010834
Michael Chand8f4cd32012-09-28 07:12:40 +000010835 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010836 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010837
Michael Chan79381092005-04-21 17:13:59 -070010838 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010839 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010840 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010841 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010842 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010843
Matt Carlson679563f2009-09-01 12:55:46 +000010844 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010845 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010846
Joe Perches63c3a662011-04-26 08:12:10 +000010847 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010848 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010849
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010850 tw32(PCIE_TRANSACTION_CFG,
10851 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010852 }
Michael Chan79381092005-04-21 17:13:59 -070010853 }
10854
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010855 tg3_phy_start(tp);
10856
Michael Chanaed93e02012-07-16 16:24:02 +000010857 tg3_hwmon_open(tp);
10858
David S. Millerf47c11e2005-06-24 20:18:35 -070010859 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010860
Matt Carlson21f76382012-02-22 12:35:21 +000010861 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010862 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010863 tg3_enable_ints(tp);
10864
Matt Carlsonbe947302012-12-03 19:36:57 +000010865 if (init)
10866 tg3_ptp_init(tp);
10867 else
10868 tg3_ptp_resume(tp);
10869
10870
David S. Millerf47c11e2005-06-24 20:18:35 -070010871 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010872
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010873 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010874
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010875 /*
10876 * Reset loopback feature if it was turned on while the device was down
10877 * make sure that it's installed properly now.
10878 */
10879 if (dev->features & NETIF_F_LOOPBACK)
10880 tg3_set_loopback(dev, dev->features);
10881
Linus Torvalds1da177e2005-04-16 15:20:36 -070010882 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010883
Matt Carlson679563f2009-09-01 12:55:46 +000010884err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010885 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10886 struct tg3_napi *tnapi = &tp->napi[i];
10887 free_irq(tnapi->irq_vec, tnapi);
10888 }
Matt Carlson07b01732009-08-28 14:01:15 +000010889
Matt Carlson679563f2009-09-01 12:55:46 +000010890err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010891 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010892 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010893 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010894
10895err_out1:
10896 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010897
Matt Carlson07b01732009-08-28 14:01:15 +000010898 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010899}
10900
Michael Chan65138592012-09-28 07:12:41 +000010901static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010902{
Matt Carlson4f125f42009-09-01 12:55:02 +000010903 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010904
Matt Carlsondb219972011-11-04 09:15:03 +000010905 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000010906 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010907
Matt Carlson21f76382012-02-22 12:35:21 +000010908 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010909
Michael Chanaed93e02012-07-16 16:24:02 +000010910 tg3_hwmon_close(tp);
10911
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010912 tg3_phy_stop(tp);
10913
David S. Millerf47c11e2005-06-24 20:18:35 -070010914 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010915
10916 tg3_disable_ints(tp);
10917
Michael Chan944d9802005-05-29 14:57:48 -070010918 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010919 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010920 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010921
David S. Millerf47c11e2005-06-24 20:18:35 -070010922 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010923
Matt Carlson4f125f42009-09-01 12:55:02 +000010924 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10925 struct tg3_napi *tnapi = &tp->napi[i];
10926 free_irq(tnapi->irq_vec, tnapi);
10927 }
Matt Carlson07b01732009-08-28 14:01:15 +000010928
10929 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010930
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010931 tg3_napi_fini(tp);
10932
Linus Torvalds1da177e2005-04-16 15:20:36 -070010933 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000010934}
10935
Michael Chand8f4cd32012-09-28 07:12:40 +000010936static int tg3_open(struct net_device *dev)
10937{
10938 struct tg3 *tp = netdev_priv(dev);
10939 int err;
10940
10941 if (tp->fw_needed) {
10942 err = tg3_request_firmware(tp);
Joe Perches41535772013-02-16 11:20:04 +000010943 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000010944 if (err)
10945 return err;
10946 } else if (err) {
10947 netdev_warn(tp->dev, "TSO capability disabled\n");
10948 tg3_flag_clear(tp, TSO_CAPABLE);
10949 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
10950 netdev_notice(tp->dev, "TSO capability restored\n");
10951 tg3_flag_set(tp, TSO_CAPABLE);
10952 }
10953 }
10954
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010955 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010956
10957 err = tg3_power_up(tp);
10958 if (err)
10959 return err;
10960
10961 tg3_full_lock(tp, 0);
10962
10963 tg3_disable_ints(tp);
10964 tg3_flag_clear(tp, INIT_COMPLETE);
10965
10966 tg3_full_unlock(tp);
10967
Matt Carlsonbe947302012-12-03 19:36:57 +000010968 err = tg3_start(tp, true, true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000010969 if (err) {
10970 tg3_frob_aux_power(tp, false);
10971 pci_set_power_state(tp->pdev, PCI_D3hot);
10972 }
Matt Carlsonbe947302012-12-03 19:36:57 +000010973
Matt Carlson7d41e492012-12-03 19:36:58 +000010974 if (tg3_flag(tp, PTP_CAPABLE)) {
10975 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
10976 &tp->pdev->dev);
10977 if (IS_ERR(tp->ptp_clock))
10978 tp->ptp_clock = NULL;
10979 }
10980
Linus Torvalds1da177e2005-04-16 15:20:36 -070010981 return err;
10982}
10983
10984static int tg3_close(struct net_device *dev)
10985{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010986 struct tg3 *tp = netdev_priv(dev);
10987
Matt Carlsonbe947302012-12-03 19:36:57 +000010988 tg3_ptp_fini(tp);
10989
Michael Chan65138592012-09-28 07:12:41 +000010990 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010991
10992 /* Clear stats across close / open calls */
10993 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10994 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010995
10996 tg3_power_down(tp);
10997
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010998 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010999
11000 return 0;
11001}
11002
11003static inline u64 get_stat64(tg3_stat64_t *val)
11004{
11005 return ((u64)val->high << 32) | ((u64)val->low);
11006}
11007
11008static u64 tg3_calc_crc_errors(struct tg3 *tp)
11009{
11010 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11011
11012 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011013 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11014 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011015 u32 val;
11016
11017 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11018 tg3_writephy(tp, MII_TG3_TEST1,
11019 val | MII_TG3_TEST1_CRC_EN);
11020 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11021 } else
11022 val = 0;
11023
11024 tp->phy_crc_errors += val;
11025
11026 return tp->phy_crc_errors;
11027 }
11028
11029 return get_stat64(&hw_stats->rx_fcs_errors);
11030}
11031
11032#define ESTAT_ADD(member) \
11033 estats->member = old_estats->member + \
11034 get_stat64(&hw_stats->member)
11035
11036static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11037{
11038 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11039 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11040
11041 ESTAT_ADD(rx_octets);
11042 ESTAT_ADD(rx_fragments);
11043 ESTAT_ADD(rx_ucast_packets);
11044 ESTAT_ADD(rx_mcast_packets);
11045 ESTAT_ADD(rx_bcast_packets);
11046 ESTAT_ADD(rx_fcs_errors);
11047 ESTAT_ADD(rx_align_errors);
11048 ESTAT_ADD(rx_xon_pause_rcvd);
11049 ESTAT_ADD(rx_xoff_pause_rcvd);
11050 ESTAT_ADD(rx_mac_ctrl_rcvd);
11051 ESTAT_ADD(rx_xoff_entered);
11052 ESTAT_ADD(rx_frame_too_long_errors);
11053 ESTAT_ADD(rx_jabbers);
11054 ESTAT_ADD(rx_undersize_packets);
11055 ESTAT_ADD(rx_in_length_errors);
11056 ESTAT_ADD(rx_out_length_errors);
11057 ESTAT_ADD(rx_64_or_less_octet_packets);
11058 ESTAT_ADD(rx_65_to_127_octet_packets);
11059 ESTAT_ADD(rx_128_to_255_octet_packets);
11060 ESTAT_ADD(rx_256_to_511_octet_packets);
11061 ESTAT_ADD(rx_512_to_1023_octet_packets);
11062 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11063 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11064 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11065 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11066 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11067
11068 ESTAT_ADD(tx_octets);
11069 ESTAT_ADD(tx_collisions);
11070 ESTAT_ADD(tx_xon_sent);
11071 ESTAT_ADD(tx_xoff_sent);
11072 ESTAT_ADD(tx_flow_control);
11073 ESTAT_ADD(tx_mac_errors);
11074 ESTAT_ADD(tx_single_collisions);
11075 ESTAT_ADD(tx_mult_collisions);
11076 ESTAT_ADD(tx_deferred);
11077 ESTAT_ADD(tx_excessive_collisions);
11078 ESTAT_ADD(tx_late_collisions);
11079 ESTAT_ADD(tx_collide_2times);
11080 ESTAT_ADD(tx_collide_3times);
11081 ESTAT_ADD(tx_collide_4times);
11082 ESTAT_ADD(tx_collide_5times);
11083 ESTAT_ADD(tx_collide_6times);
11084 ESTAT_ADD(tx_collide_7times);
11085 ESTAT_ADD(tx_collide_8times);
11086 ESTAT_ADD(tx_collide_9times);
11087 ESTAT_ADD(tx_collide_10times);
11088 ESTAT_ADD(tx_collide_11times);
11089 ESTAT_ADD(tx_collide_12times);
11090 ESTAT_ADD(tx_collide_13times);
11091 ESTAT_ADD(tx_collide_14times);
11092 ESTAT_ADD(tx_collide_15times);
11093 ESTAT_ADD(tx_ucast_packets);
11094 ESTAT_ADD(tx_mcast_packets);
11095 ESTAT_ADD(tx_bcast_packets);
11096 ESTAT_ADD(tx_carrier_sense_errors);
11097 ESTAT_ADD(tx_discards);
11098 ESTAT_ADD(tx_errors);
11099
11100 ESTAT_ADD(dma_writeq_full);
11101 ESTAT_ADD(dma_write_prioq_full);
11102 ESTAT_ADD(rxbds_empty);
11103 ESTAT_ADD(rx_discards);
11104 ESTAT_ADD(rx_errors);
11105 ESTAT_ADD(rx_threshold_hit);
11106
11107 ESTAT_ADD(dma_readq_full);
11108 ESTAT_ADD(dma_read_prioq_full);
11109 ESTAT_ADD(tx_comp_queue_full);
11110
11111 ESTAT_ADD(ring_set_send_prod_index);
11112 ESTAT_ADD(ring_status_update);
11113 ESTAT_ADD(nic_irqs);
11114 ESTAT_ADD(nic_avoided_irqs);
11115 ESTAT_ADD(nic_tx_threshold_hit);
11116
Matt Carlson4452d092011-05-19 12:12:51 +000011117 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011118}
11119
Matt Carlson65ec6982012-02-28 23:33:37 +000011120static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011121{
Eric Dumazet511d2222010-07-07 20:44:24 +000011122 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011123 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11124
Linus Torvalds1da177e2005-04-16 15:20:36 -070011125 stats->rx_packets = old_stats->rx_packets +
11126 get_stat64(&hw_stats->rx_ucast_packets) +
11127 get_stat64(&hw_stats->rx_mcast_packets) +
11128 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011129
Linus Torvalds1da177e2005-04-16 15:20:36 -070011130 stats->tx_packets = old_stats->tx_packets +
11131 get_stat64(&hw_stats->tx_ucast_packets) +
11132 get_stat64(&hw_stats->tx_mcast_packets) +
11133 get_stat64(&hw_stats->tx_bcast_packets);
11134
11135 stats->rx_bytes = old_stats->rx_bytes +
11136 get_stat64(&hw_stats->rx_octets);
11137 stats->tx_bytes = old_stats->tx_bytes +
11138 get_stat64(&hw_stats->tx_octets);
11139
11140 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011141 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011142 stats->tx_errors = old_stats->tx_errors +
11143 get_stat64(&hw_stats->tx_errors) +
11144 get_stat64(&hw_stats->tx_mac_errors) +
11145 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11146 get_stat64(&hw_stats->tx_discards);
11147
11148 stats->multicast = old_stats->multicast +
11149 get_stat64(&hw_stats->rx_mcast_packets);
11150 stats->collisions = old_stats->collisions +
11151 get_stat64(&hw_stats->tx_collisions);
11152
11153 stats->rx_length_errors = old_stats->rx_length_errors +
11154 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11155 get_stat64(&hw_stats->rx_undersize_packets);
11156
11157 stats->rx_over_errors = old_stats->rx_over_errors +
11158 get_stat64(&hw_stats->rxbds_empty);
11159 stats->rx_frame_errors = old_stats->rx_frame_errors +
11160 get_stat64(&hw_stats->rx_align_errors);
11161 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11162 get_stat64(&hw_stats->tx_discards);
11163 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11164 get_stat64(&hw_stats->tx_carrier_sense_errors);
11165
11166 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011167 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011168
John W. Linville4f63b872005-09-12 14:43:18 -070011169 stats->rx_missed_errors = old_stats->rx_missed_errors +
11170 get_stat64(&hw_stats->rx_discards);
11171
Eric Dumazetb0057c52010-10-10 19:55:52 +000011172 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011173 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011174}
11175
Linus Torvalds1da177e2005-04-16 15:20:36 -070011176static int tg3_get_regs_len(struct net_device *dev)
11177{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011178 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011179}
11180
11181static void tg3_get_regs(struct net_device *dev,
11182 struct ethtool_regs *regs, void *_p)
11183{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011184 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011185
11186 regs->version = 0;
11187
Matt Carlson97bd8e42011-04-13 11:05:04 +000011188 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011189
Matt Carlson80096062010-08-02 11:26:06 +000011190 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011191 return;
11192
David S. Millerf47c11e2005-06-24 20:18:35 -070011193 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011194
Matt Carlson97bd8e42011-04-13 11:05:04 +000011195 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011196
David S. Millerf47c11e2005-06-24 20:18:35 -070011197 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011198}
11199
11200static int tg3_get_eeprom_len(struct net_device *dev)
11201{
11202 struct tg3 *tp = netdev_priv(dev);
11203
11204 return tp->nvram_size;
11205}
11206
Linus Torvalds1da177e2005-04-16 15:20:36 -070011207static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11208{
11209 struct tg3 *tp = netdev_priv(dev);
11210 int ret;
11211 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011212 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011213 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011214
Joe Perches63c3a662011-04-26 08:12:10 +000011215 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011216 return -EINVAL;
11217
Matt Carlson80096062010-08-02 11:26:06 +000011218 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011219 return -EAGAIN;
11220
Linus Torvalds1da177e2005-04-16 15:20:36 -070011221 offset = eeprom->offset;
11222 len = eeprom->len;
11223 eeprom->len = 0;
11224
11225 eeprom->magic = TG3_EEPROM_MAGIC;
11226
11227 if (offset & 3) {
11228 /* adjustments to start on required 4 byte boundary */
11229 b_offset = offset & 3;
11230 b_count = 4 - b_offset;
11231 if (b_count > len) {
11232 /* i.e. offset=1 len=2 */
11233 b_count = len;
11234 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011235 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011236 if (ret)
11237 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011238 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011239 len -= b_count;
11240 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011241 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011242 }
11243
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011244 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011245 pd = &data[eeprom->len];
11246 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011247 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011248 if (ret) {
11249 eeprom->len += i;
11250 return ret;
11251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011252 memcpy(pd + i, &val, 4);
11253 }
11254 eeprom->len += i;
11255
11256 if (len & 3) {
11257 /* read last bytes not ending on 4 byte boundary */
11258 pd = &data[eeprom->len];
11259 b_count = len & 3;
11260 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011261 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011262 if (ret)
11263 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011264 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011265 eeprom->len += b_count;
11266 }
11267 return 0;
11268}
11269
Linus Torvalds1da177e2005-04-16 15:20:36 -070011270static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11271{
11272 struct tg3 *tp = netdev_priv(dev);
11273 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011274 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011275 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011276 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011277
Matt Carlson80096062010-08-02 11:26:06 +000011278 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011279 return -EAGAIN;
11280
Joe Perches63c3a662011-04-26 08:12:10 +000011281 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011282 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011283 return -EINVAL;
11284
11285 offset = eeprom->offset;
11286 len = eeprom->len;
11287
11288 if ((b_offset = (offset & 3))) {
11289 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011290 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011291 if (ret)
11292 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011293 len += b_offset;
11294 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070011295 if (len < 4)
11296 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011297 }
11298
11299 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070011300 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011301 /* adjustments to end on required 4 byte boundary */
11302 odd_len = 1;
11303 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011304 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011305 if (ret)
11306 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011307 }
11308
11309 buf = data;
11310 if (b_offset || odd_len) {
11311 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011312 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011313 return -ENOMEM;
11314 if (b_offset)
11315 memcpy(buf, &start, 4);
11316 if (odd_len)
11317 memcpy(buf+len-4, &end, 4);
11318 memcpy(buf + b_offset, data, eeprom->len);
11319 }
11320
11321 ret = tg3_nvram_write_block(tp, offset, len, buf);
11322
11323 if (buf != data)
11324 kfree(buf);
11325
11326 return ret;
11327}
11328
11329static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11330{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011331 struct tg3 *tp = netdev_priv(dev);
11332
Joe Perches63c3a662011-04-26 08:12:10 +000011333 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011334 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011335 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011336 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011337 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11338 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011339 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011340
Linus Torvalds1da177e2005-04-16 15:20:36 -070011341 cmd->supported = (SUPPORTED_Autoneg);
11342
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011343 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011344 cmd->supported |= (SUPPORTED_1000baseT_Half |
11345 SUPPORTED_1000baseT_Full);
11346
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011347 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011348 cmd->supported |= (SUPPORTED_100baseT_Half |
11349 SUPPORTED_100baseT_Full |
11350 SUPPORTED_10baseT_Half |
11351 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011352 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011353 cmd->port = PORT_TP;
11354 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011355 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011356 cmd->port = PORT_FIBRE;
11357 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011358
Linus Torvalds1da177e2005-04-16 15:20:36 -070011359 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011360 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11361 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11362 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11363 cmd->advertising |= ADVERTISED_Pause;
11364 } else {
11365 cmd->advertising |= ADVERTISED_Pause |
11366 ADVERTISED_Asym_Pause;
11367 }
11368 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11369 cmd->advertising |= ADVERTISED_Asym_Pause;
11370 }
11371 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011372 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011373 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011374 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011375 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011376 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11377 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11378 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11379 else
11380 cmd->eth_tp_mdix = ETH_TP_MDI;
11381 }
Matt Carlson64c22182010-10-14 10:37:44 +000011382 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011383 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11384 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011385 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011386 }
Matt Carlson882e9792009-09-01 13:21:36 +000011387 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011388 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011389 cmd->autoneg = tp->link_config.autoneg;
11390 cmd->maxtxpkt = 0;
11391 cmd->maxrxpkt = 0;
11392 return 0;
11393}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011394
Linus Torvalds1da177e2005-04-16 15:20:36 -070011395static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11396{
11397 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011398 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011399
Joe Perches63c3a662011-04-26 08:12:10 +000011400 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011401 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011402 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011403 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011404 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11405 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011406 }
11407
Matt Carlson7e5856b2009-02-25 14:23:01 +000011408 if (cmd->autoneg != AUTONEG_ENABLE &&
11409 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011410 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011411
11412 if (cmd->autoneg == AUTONEG_DISABLE &&
11413 cmd->duplex != DUPLEX_FULL &&
11414 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011415 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011416
Matt Carlson7e5856b2009-02-25 14:23:01 +000011417 if (cmd->autoneg == AUTONEG_ENABLE) {
11418 u32 mask = ADVERTISED_Autoneg |
11419 ADVERTISED_Pause |
11420 ADVERTISED_Asym_Pause;
11421
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011422 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011423 mask |= ADVERTISED_1000baseT_Half |
11424 ADVERTISED_1000baseT_Full;
11425
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011426 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011427 mask |= ADVERTISED_100baseT_Half |
11428 ADVERTISED_100baseT_Full |
11429 ADVERTISED_10baseT_Half |
11430 ADVERTISED_10baseT_Full |
11431 ADVERTISED_TP;
11432 else
11433 mask |= ADVERTISED_FIBRE;
11434
11435 if (cmd->advertising & ~mask)
11436 return -EINVAL;
11437
11438 mask &= (ADVERTISED_1000baseT_Half |
11439 ADVERTISED_1000baseT_Full |
11440 ADVERTISED_100baseT_Half |
11441 ADVERTISED_100baseT_Full |
11442 ADVERTISED_10baseT_Half |
11443 ADVERTISED_10baseT_Full);
11444
11445 cmd->advertising &= mask;
11446 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011447 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011448 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011449 return -EINVAL;
11450
11451 if (cmd->duplex != DUPLEX_FULL)
11452 return -EINVAL;
11453 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011454 if (speed != SPEED_100 &&
11455 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011456 return -EINVAL;
11457 }
11458 }
11459
David S. Millerf47c11e2005-06-24 20:18:35 -070011460 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011461
11462 tp->link_config.autoneg = cmd->autoneg;
11463 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011464 tp->link_config.advertising = (cmd->advertising |
11465 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011466 tp->link_config.speed = SPEED_UNKNOWN;
11467 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011468 } else {
11469 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011470 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011471 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011472 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011473
Linus Torvalds1da177e2005-04-16 15:20:36 -070011474 if (netif_running(dev))
11475 tg3_setup_phy(tp, 1);
11476
David S. Millerf47c11e2005-06-24 20:18:35 -070011477 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011478
Linus Torvalds1da177e2005-04-16 15:20:36 -070011479 return 0;
11480}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011481
Linus Torvalds1da177e2005-04-16 15:20:36 -070011482static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11483{
11484 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011485
Rick Jones68aad782011-11-07 13:29:27 +000011486 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11487 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11488 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11489 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011490}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011491
Linus Torvalds1da177e2005-04-16 15:20:36 -070011492static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11493{
11494 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011495
Joe Perches63c3a662011-04-26 08:12:10 +000011496 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011497 wol->supported = WAKE_MAGIC;
11498 else
11499 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011500 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011501 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011502 wol->wolopts = WAKE_MAGIC;
11503 memset(&wol->sopass, 0, sizeof(wol->sopass));
11504}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011505
Linus Torvalds1da177e2005-04-16 15:20:36 -070011506static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11507{
11508 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011509 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011510
Linus Torvalds1da177e2005-04-16 15:20:36 -070011511 if (wol->wolopts & ~WAKE_MAGIC)
11512 return -EINVAL;
11513 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011514 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011515 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011516
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011517 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11518
David S. Millerf47c11e2005-06-24 20:18:35 -070011519 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011520 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011521 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011522 else
Joe Perches63c3a662011-04-26 08:12:10 +000011523 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011524 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011525
Linus Torvalds1da177e2005-04-16 15:20:36 -070011526 return 0;
11527}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011528
Linus Torvalds1da177e2005-04-16 15:20:36 -070011529static u32 tg3_get_msglevel(struct net_device *dev)
11530{
11531 struct tg3 *tp = netdev_priv(dev);
11532 return tp->msg_enable;
11533}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011534
Linus Torvalds1da177e2005-04-16 15:20:36 -070011535static void tg3_set_msglevel(struct net_device *dev, u32 value)
11536{
11537 struct tg3 *tp = netdev_priv(dev);
11538 tp->msg_enable = value;
11539}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011540
Linus Torvalds1da177e2005-04-16 15:20:36 -070011541static int tg3_nway_reset(struct net_device *dev)
11542{
11543 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011544 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011545
Linus Torvalds1da177e2005-04-16 15:20:36 -070011546 if (!netif_running(dev))
11547 return -EAGAIN;
11548
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011549 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011550 return -EINVAL;
11551
Joe Perches63c3a662011-04-26 08:12:10 +000011552 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011553 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011554 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011555 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011556 } else {
11557 u32 bmcr;
11558
11559 spin_lock_bh(&tp->lock);
11560 r = -EINVAL;
11561 tg3_readphy(tp, MII_BMCR, &bmcr);
11562 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11563 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011564 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011565 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11566 BMCR_ANENABLE);
11567 r = 0;
11568 }
11569 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011570 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011571
Linus Torvalds1da177e2005-04-16 15:20:36 -070011572 return r;
11573}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011574
Linus Torvalds1da177e2005-04-16 15:20:36 -070011575static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11576{
11577 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011578
Matt Carlson2c49a442010-09-30 10:34:35 +000011579 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011580 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011581 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011582 else
11583 ering->rx_jumbo_max_pending = 0;
11584
11585 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011586
11587 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011588 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011589 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11590 else
11591 ering->rx_jumbo_pending = 0;
11592
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011593 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011594}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011595
Linus Torvalds1da177e2005-04-16 15:20:36 -070011596static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11597{
11598 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000011599 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011600
Matt Carlson2c49a442010-09-30 10:34:35 +000011601 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
11602 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070011603 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
11604 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011605 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070011606 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011607 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011608
Michael Chanbbe832c2005-06-24 20:20:04 -070011609 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011610 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011611 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011612 irq_sync = 1;
11613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011614
Michael Chanbbe832c2005-06-24 20:20:04 -070011615 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011616
Linus Torvalds1da177e2005-04-16 15:20:36 -070011617 tp->rx_pending = ering->rx_pending;
11618
Joe Perches63c3a662011-04-26 08:12:10 +000011619 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011620 tp->rx_pending > 63)
11621 tp->rx_pending = 63;
11622 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000011623
Matt Carlson6fd45cb2010-09-15 08:59:57 +000011624 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000011625 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011626
11627 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070011628 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070011629 err = tg3_restart_hw(tp, 1);
11630 if (!err)
11631 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011632 }
11633
David S. Millerf47c11e2005-06-24 20:18:35 -070011634 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011635
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011636 if (irq_sync && !err)
11637 tg3_phy_start(tp);
11638
Michael Chanb9ec6c12006-07-25 16:37:27 -070011639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011640}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011641
Linus Torvalds1da177e2005-04-16 15:20:36 -070011642static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11643{
11644 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011645
Joe Perches63c3a662011-04-26 08:12:10 +000011646 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080011647
Matt Carlson4a2db502011-12-08 14:40:17 +000011648 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080011649 epause->rx_pause = 1;
11650 else
11651 epause->rx_pause = 0;
11652
Matt Carlson4a2db502011-12-08 14:40:17 +000011653 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080011654 epause->tx_pause = 1;
11655 else
11656 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011657}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011658
Linus Torvalds1da177e2005-04-16 15:20:36 -070011659static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11660{
11661 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011662 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011663
Joe Perches63c3a662011-04-26 08:12:10 +000011664 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000011665 u32 newadv;
11666 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011667
Matt Carlson27121682010-02-17 15:16:57 +000011668 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011669
Matt Carlson27121682010-02-17 15:16:57 +000011670 if (!(phydev->supported & SUPPORTED_Pause) ||
11671 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000011672 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000011673 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011674
Matt Carlson27121682010-02-17 15:16:57 +000011675 tp->link_config.flowctrl = 0;
11676 if (epause->rx_pause) {
11677 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011678
Matt Carlson27121682010-02-17 15:16:57 +000011679 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080011680 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000011681 newadv = ADVERTISED_Pause;
11682 } else
11683 newadv = ADVERTISED_Pause |
11684 ADVERTISED_Asym_Pause;
11685 } else if (epause->tx_pause) {
11686 tp->link_config.flowctrl |= FLOW_CTRL_TX;
11687 newadv = ADVERTISED_Asym_Pause;
11688 } else
11689 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011690
Matt Carlson27121682010-02-17 15:16:57 +000011691 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011692 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011693 else
Joe Perches63c3a662011-04-26 08:12:10 +000011694 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011695
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011696 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000011697 u32 oldadv = phydev->advertising &
11698 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
11699 if (oldadv != newadv) {
11700 phydev->advertising &=
11701 ~(ADVERTISED_Pause |
11702 ADVERTISED_Asym_Pause);
11703 phydev->advertising |= newadv;
11704 if (phydev->autoneg) {
11705 /*
11706 * Always renegotiate the link to
11707 * inform our link partner of our
11708 * flow control settings, even if the
11709 * flow control is forced. Let
11710 * tg3_adjust_link() do the final
11711 * flow control setup.
11712 */
11713 return phy_start_aneg(phydev);
11714 }
11715 }
11716
11717 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011718 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000011719 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011720 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000011721 ~(ADVERTISED_Pause |
11722 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011723 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011724 }
11725 } else {
11726 int irq_sync = 0;
11727
11728 if (netif_running(dev)) {
11729 tg3_netif_stop(tp);
11730 irq_sync = 1;
11731 }
11732
11733 tg3_full_lock(tp, irq_sync);
11734
11735 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011736 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011737 else
Joe Perches63c3a662011-04-26 08:12:10 +000011738 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011739 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011740 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011741 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011742 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011743 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011744 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011745 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011746 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011747
11748 if (netif_running(dev)) {
11749 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11750 err = tg3_restart_hw(tp, 1);
11751 if (!err)
11752 tg3_netif_start(tp);
11753 }
11754
11755 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011757
Michael Chanb9ec6c12006-07-25 16:37:27 -070011758 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011759}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011760
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011761static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011762{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011763 switch (sset) {
11764 case ETH_SS_TEST:
11765 return TG3_NUM_TEST;
11766 case ETH_SS_STATS:
11767 return TG3_NUM_STATS;
11768 default:
11769 return -EOPNOTSUPP;
11770 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070011771}
11772
Matt Carlson90415472011-12-16 13:33:23 +000011773static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
11774 u32 *rules __always_unused)
11775{
11776 struct tg3 *tp = netdev_priv(dev);
11777
11778 if (!tg3_flag(tp, SUPPORT_MSIX))
11779 return -EOPNOTSUPP;
11780
11781 switch (info->cmd) {
11782 case ETHTOOL_GRXRINGS:
11783 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000011784 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000011785 else {
11786 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000011787 if (info->data > TG3_RSS_MAX_NUM_QS)
11788 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000011789 }
11790
11791 /* The first interrupt vector only
11792 * handles link interrupts.
11793 */
11794 info->data -= 1;
11795 return 0;
11796
11797 default:
11798 return -EOPNOTSUPP;
11799 }
11800}
11801
11802static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
11803{
11804 u32 size = 0;
11805 struct tg3 *tp = netdev_priv(dev);
11806
11807 if (tg3_flag(tp, SUPPORT_MSIX))
11808 size = TG3_RSS_INDIR_TBL_SIZE;
11809
11810 return size;
11811}
11812
11813static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
11814{
11815 struct tg3 *tp = netdev_priv(dev);
11816 int i;
11817
11818 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11819 indir[i] = tp->rss_ind_tbl[i];
11820
11821 return 0;
11822}
11823
11824static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11825{
11826 struct tg3 *tp = netdev_priv(dev);
11827 size_t i;
11828
11829 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11830 tp->rss_ind_tbl[i] = indir[i];
11831
11832 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11833 return 0;
11834
11835 /* It is legal to write the indirection
11836 * table while the device is running.
11837 */
11838 tg3_full_lock(tp, 0);
11839 tg3_rss_write_indir_tbl(tp);
11840 tg3_full_unlock(tp);
11841
11842 return 0;
11843}
11844
Michael Chan09681692012-09-28 07:12:42 +000011845static void tg3_get_channels(struct net_device *dev,
11846 struct ethtool_channels *channel)
11847{
11848 struct tg3 *tp = netdev_priv(dev);
11849 u32 deflt_qs = netif_get_num_default_rss_queues();
11850
11851 channel->max_rx = tp->rxq_max;
11852 channel->max_tx = tp->txq_max;
11853
11854 if (netif_running(dev)) {
11855 channel->rx_count = tp->rxq_cnt;
11856 channel->tx_count = tp->txq_cnt;
11857 } else {
11858 if (tp->rxq_req)
11859 channel->rx_count = tp->rxq_req;
11860 else
11861 channel->rx_count = min(deflt_qs, tp->rxq_max);
11862
11863 if (tp->txq_req)
11864 channel->tx_count = tp->txq_req;
11865 else
11866 channel->tx_count = min(deflt_qs, tp->txq_max);
11867 }
11868}
11869
11870static int tg3_set_channels(struct net_device *dev,
11871 struct ethtool_channels *channel)
11872{
11873 struct tg3 *tp = netdev_priv(dev);
11874
11875 if (!tg3_flag(tp, SUPPORT_MSIX))
11876 return -EOPNOTSUPP;
11877
11878 if (channel->rx_count > tp->rxq_max ||
11879 channel->tx_count > tp->txq_max)
11880 return -EINVAL;
11881
11882 tp->rxq_req = channel->rx_count;
11883 tp->txq_req = channel->tx_count;
11884
11885 if (!netif_running(dev))
11886 return 0;
11887
11888 tg3_stop(tp);
11889
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011890 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000011891
Matt Carlsonbe947302012-12-03 19:36:57 +000011892 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000011893
11894 return 0;
11895}
11896
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011897static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011898{
11899 switch (stringset) {
11900 case ETH_SS_STATS:
11901 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11902 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011903 case ETH_SS_TEST:
11904 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11905 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011906 default:
11907 WARN_ON(1); /* we need a WARN() */
11908 break;
11909 }
11910}
11911
stephen hemminger81b87092011-04-04 08:43:50 +000011912static int tg3_set_phys_id(struct net_device *dev,
11913 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011914{
11915 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011916
11917 if (!netif_running(tp->dev))
11918 return -EAGAIN;
11919
stephen hemminger81b87092011-04-04 08:43:50 +000011920 switch (state) {
11921 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011922 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011923
stephen hemminger81b87092011-04-04 08:43:50 +000011924 case ETHTOOL_ID_ON:
11925 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11926 LED_CTRL_1000MBPS_ON |
11927 LED_CTRL_100MBPS_ON |
11928 LED_CTRL_10MBPS_ON |
11929 LED_CTRL_TRAFFIC_OVERRIDE |
11930 LED_CTRL_TRAFFIC_BLINK |
11931 LED_CTRL_TRAFFIC_LED);
11932 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011933
stephen hemminger81b87092011-04-04 08:43:50 +000011934 case ETHTOOL_ID_OFF:
11935 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11936 LED_CTRL_TRAFFIC_OVERRIDE);
11937 break;
Michael Chan4009a932005-09-05 17:52:54 -070011938
stephen hemminger81b87092011-04-04 08:43:50 +000011939 case ETHTOOL_ID_INACTIVE:
11940 tw32(MAC_LED_CTRL, tp->led_ctrl);
11941 break;
Michael Chan4009a932005-09-05 17:52:54 -070011942 }
stephen hemminger81b87092011-04-04 08:43:50 +000011943
Michael Chan4009a932005-09-05 17:52:54 -070011944 return 0;
11945}
11946
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011947static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011948 struct ethtool_stats *estats, u64 *tmp_stats)
11949{
11950 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011951
Matt Carlsonb546e462012-02-13 15:20:09 +000011952 if (tp->hw_stats)
11953 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11954 else
11955 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011956}
11957
Matt Carlson535a4902011-07-20 10:20:56 +000011958static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011959{
11960 int i;
11961 __be32 *buf;
11962 u32 offset = 0, len = 0;
11963 u32 magic, val;
11964
Joe Perches63c3a662011-04-26 08:12:10 +000011965 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011966 return NULL;
11967
11968 if (magic == TG3_EEPROM_MAGIC) {
11969 for (offset = TG3_NVM_DIR_START;
11970 offset < TG3_NVM_DIR_END;
11971 offset += TG3_NVM_DIRENT_SIZE) {
11972 if (tg3_nvram_read(tp, offset, &val))
11973 return NULL;
11974
11975 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11976 TG3_NVM_DIRTYPE_EXTVPD)
11977 break;
11978 }
11979
11980 if (offset != TG3_NVM_DIR_END) {
11981 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11982 if (tg3_nvram_read(tp, offset + 4, &offset))
11983 return NULL;
11984
11985 offset = tg3_nvram_logical_addr(tp, offset);
11986 }
11987 }
11988
11989 if (!offset || !len) {
11990 offset = TG3_NVM_VPD_OFF;
11991 len = TG3_NVM_VPD_LEN;
11992 }
11993
11994 buf = kmalloc(len, GFP_KERNEL);
11995 if (buf == NULL)
11996 return NULL;
11997
11998 if (magic == TG3_EEPROM_MAGIC) {
11999 for (i = 0; i < len; i += 4) {
12000 /* The data is in little-endian format in NVRAM.
12001 * Use the big-endian read routines to preserve
12002 * the byte order as it exists in NVRAM.
12003 */
12004 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12005 goto error;
12006 }
12007 } else {
12008 u8 *ptr;
12009 ssize_t cnt;
12010 unsigned int pos = 0;
12011
12012 ptr = (u8 *)&buf[0];
12013 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12014 cnt = pci_read_vpd(tp->pdev, pos,
12015 len - pos, ptr);
12016 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12017 cnt = 0;
12018 else if (cnt < 0)
12019 goto error;
12020 }
12021 if (pos != len)
12022 goto error;
12023 }
12024
Matt Carlson535a4902011-07-20 10:20:56 +000012025 *vpdlen = len;
12026
Matt Carlsonc3e94502011-04-13 11:05:08 +000012027 return buf;
12028
12029error:
12030 kfree(buf);
12031 return NULL;
12032}
12033
Michael Chan566f86a2005-05-29 14:56:58 -070012034#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012035#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12036#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12037#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012038#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12039#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012040#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012041#define NVRAM_SELFBOOT_HW_SIZE 0x20
12042#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012043
12044static int tg3_test_nvram(struct tg3 *tp)
12045{
Matt Carlson535a4902011-07-20 10:20:56 +000012046 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012047 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012048 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012049
Joe Perches63c3a662011-04-26 08:12:10 +000012050 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012051 return 0;
12052
Matt Carlsone4f34112009-02-25 14:25:00 +000012053 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012054 return -EIO;
12055
Michael Chan1b277772006-03-20 22:27:48 -080012056 if (magic == TG3_EEPROM_MAGIC)
12057 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012058 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012059 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12060 TG3_EEPROM_SB_FORMAT_1) {
12061 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12062 case TG3_EEPROM_SB_REVISION_0:
12063 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12064 break;
12065 case TG3_EEPROM_SB_REVISION_2:
12066 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12067 break;
12068 case TG3_EEPROM_SB_REVISION_3:
12069 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12070 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012071 case TG3_EEPROM_SB_REVISION_4:
12072 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12073 break;
12074 case TG3_EEPROM_SB_REVISION_5:
12075 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12076 break;
12077 case TG3_EEPROM_SB_REVISION_6:
12078 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12079 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012080 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012081 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012082 }
12083 } else
Michael Chan1b277772006-03-20 22:27:48 -080012084 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012085 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12086 size = NVRAM_SELFBOOT_HW_SIZE;
12087 else
Michael Chan1b277772006-03-20 22:27:48 -080012088 return -EIO;
12089
12090 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012091 if (buf == NULL)
12092 return -ENOMEM;
12093
Michael Chan1b277772006-03-20 22:27:48 -080012094 err = -EIO;
12095 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012096 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12097 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012098 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012099 }
Michael Chan1b277772006-03-20 22:27:48 -080012100 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012101 goto out;
12102
Michael Chan1b277772006-03-20 22:27:48 -080012103 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012104 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012105 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012106 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012107 u8 *buf8 = (u8 *) buf, csum8 = 0;
12108
Al Virob9fc7dc2007-12-17 22:59:57 -080012109 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012110 TG3_EEPROM_SB_REVISION_2) {
12111 /* For rev 2, the csum doesn't include the MBA. */
12112 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12113 csum8 += buf8[i];
12114 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12115 csum8 += buf8[i];
12116 } else {
12117 for (i = 0; i < size; i++)
12118 csum8 += buf8[i];
12119 }
Michael Chan1b277772006-03-20 22:27:48 -080012120
Adrian Bunkad96b482006-04-05 22:21:04 -070012121 if (csum8 == 0) {
12122 err = 0;
12123 goto out;
12124 }
12125
12126 err = -EIO;
12127 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012128 }
Michael Chan566f86a2005-05-29 14:56:58 -070012129
Al Virob9fc7dc2007-12-17 22:59:57 -080012130 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012131 TG3_EEPROM_MAGIC_HW) {
12132 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012133 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012134 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012135
12136 /* Separate the parity bits and the data bytes. */
12137 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12138 if ((i == 0) || (i == 8)) {
12139 int l;
12140 u8 msk;
12141
12142 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12143 parity[k++] = buf8[i] & msk;
12144 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012145 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012146 int l;
12147 u8 msk;
12148
12149 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12150 parity[k++] = buf8[i] & msk;
12151 i++;
12152
12153 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12154 parity[k++] = buf8[i] & msk;
12155 i++;
12156 }
12157 data[j++] = buf8[i];
12158 }
12159
12160 err = -EIO;
12161 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12162 u8 hw8 = hweight8(data[i]);
12163
12164 if ((hw8 & 0x1) && parity[i])
12165 goto out;
12166 else if (!(hw8 & 0x1) && !parity[i])
12167 goto out;
12168 }
12169 err = 0;
12170 goto out;
12171 }
12172
Matt Carlson01c3a392011-03-09 16:58:20 +000012173 err = -EIO;
12174
Michael Chan566f86a2005-05-29 14:56:58 -070012175 /* Bootstrap checksum at offset 0x10 */
12176 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012177 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012178 goto out;
12179
12180 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12181 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012182 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012183 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012184
Matt Carlsonc3e94502011-04-13 11:05:08 +000012185 kfree(buf);
12186
Matt Carlson535a4902011-07-20 10:20:56 +000012187 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012188 if (!buf)
12189 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012190
Matt Carlson535a4902011-07-20 10:20:56 +000012191 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012192 if (i > 0) {
12193 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12194 if (j < 0)
12195 goto out;
12196
Matt Carlson535a4902011-07-20 10:20:56 +000012197 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012198 goto out;
12199
12200 i += PCI_VPD_LRDT_TAG_SIZE;
12201 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12202 PCI_VPD_RO_KEYWORD_CHKSUM);
12203 if (j > 0) {
12204 u8 csum8 = 0;
12205
12206 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12207
12208 for (i = 0; i <= j; i++)
12209 csum8 += ((u8 *)buf)[i];
12210
12211 if (csum8)
12212 goto out;
12213 }
12214 }
12215
Michael Chan566f86a2005-05-29 14:56:58 -070012216 err = 0;
12217
12218out:
12219 kfree(buf);
12220 return err;
12221}
12222
Michael Chanca430072005-05-29 14:57:23 -070012223#define TG3_SERDES_TIMEOUT_SEC 2
12224#define TG3_COPPER_TIMEOUT_SEC 6
12225
12226static int tg3_test_link(struct tg3 *tp)
12227{
12228 int i, max;
12229
12230 if (!netif_running(tp->dev))
12231 return -ENODEV;
12232
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012233 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012234 max = TG3_SERDES_TIMEOUT_SEC;
12235 else
12236 max = TG3_COPPER_TIMEOUT_SEC;
12237
12238 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012239 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012240 return 0;
12241
12242 if (msleep_interruptible(1000))
12243 break;
12244 }
12245
12246 return -EIO;
12247}
12248
Michael Chana71116d2005-05-29 14:58:11 -070012249/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012250static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012251{
Michael Chanb16250e2006-09-27 16:10:14 -070012252 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012253 u32 offset, read_mask, write_mask, val, save_val, read_val;
12254 static struct {
12255 u16 offset;
12256 u16 flags;
12257#define TG3_FL_5705 0x1
12258#define TG3_FL_NOT_5705 0x2
12259#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012260#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012261 u32 read_mask;
12262 u32 write_mask;
12263 } reg_tbl[] = {
12264 /* MAC Control Registers */
12265 { MAC_MODE, TG3_FL_NOT_5705,
12266 0x00000000, 0x00ef6f8c },
12267 { MAC_MODE, TG3_FL_5705,
12268 0x00000000, 0x01ef6b8c },
12269 { MAC_STATUS, TG3_FL_NOT_5705,
12270 0x03800107, 0x00000000 },
12271 { MAC_STATUS, TG3_FL_5705,
12272 0x03800100, 0x00000000 },
12273 { MAC_ADDR_0_HIGH, 0x0000,
12274 0x00000000, 0x0000ffff },
12275 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012276 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012277 { MAC_RX_MTU_SIZE, 0x0000,
12278 0x00000000, 0x0000ffff },
12279 { MAC_TX_MODE, 0x0000,
12280 0x00000000, 0x00000070 },
12281 { MAC_TX_LENGTHS, 0x0000,
12282 0x00000000, 0x00003fff },
12283 { MAC_RX_MODE, TG3_FL_NOT_5705,
12284 0x00000000, 0x000007fc },
12285 { MAC_RX_MODE, TG3_FL_5705,
12286 0x00000000, 0x000007dc },
12287 { MAC_HASH_REG_0, 0x0000,
12288 0x00000000, 0xffffffff },
12289 { MAC_HASH_REG_1, 0x0000,
12290 0x00000000, 0xffffffff },
12291 { MAC_HASH_REG_2, 0x0000,
12292 0x00000000, 0xffffffff },
12293 { MAC_HASH_REG_3, 0x0000,
12294 0x00000000, 0xffffffff },
12295
12296 /* Receive Data and Receive BD Initiator Control Registers. */
12297 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12298 0x00000000, 0xffffffff },
12299 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12300 0x00000000, 0xffffffff },
12301 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12302 0x00000000, 0x00000003 },
12303 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12304 0x00000000, 0xffffffff },
12305 { RCVDBDI_STD_BD+0, 0x0000,
12306 0x00000000, 0xffffffff },
12307 { RCVDBDI_STD_BD+4, 0x0000,
12308 0x00000000, 0xffffffff },
12309 { RCVDBDI_STD_BD+8, 0x0000,
12310 0x00000000, 0xffff0002 },
12311 { RCVDBDI_STD_BD+0xc, 0x0000,
12312 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012313
Michael Chana71116d2005-05-29 14:58:11 -070012314 /* Receive BD Initiator Control Registers. */
12315 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12316 0x00000000, 0xffffffff },
12317 { RCVBDI_STD_THRESH, TG3_FL_5705,
12318 0x00000000, 0x000003ff },
12319 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12320 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012321
Michael Chana71116d2005-05-29 14:58:11 -070012322 /* Host Coalescing Control Registers. */
12323 { HOSTCC_MODE, TG3_FL_NOT_5705,
12324 0x00000000, 0x00000004 },
12325 { HOSTCC_MODE, TG3_FL_5705,
12326 0x00000000, 0x000000f6 },
12327 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12328 0x00000000, 0xffffffff },
12329 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12330 0x00000000, 0x000003ff },
12331 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12332 0x00000000, 0xffffffff },
12333 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12334 0x00000000, 0x000003ff },
12335 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12336 0x00000000, 0xffffffff },
12337 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12338 0x00000000, 0x000000ff },
12339 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12340 0x00000000, 0xffffffff },
12341 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12342 0x00000000, 0x000000ff },
12343 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12344 0x00000000, 0xffffffff },
12345 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12346 0x00000000, 0xffffffff },
12347 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12348 0x00000000, 0xffffffff },
12349 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12350 0x00000000, 0x000000ff },
12351 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12352 0x00000000, 0xffffffff },
12353 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12354 0x00000000, 0x000000ff },
12355 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12356 0x00000000, 0xffffffff },
12357 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12358 0x00000000, 0xffffffff },
12359 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12360 0x00000000, 0xffffffff },
12361 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12362 0x00000000, 0xffffffff },
12363 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12364 0x00000000, 0xffffffff },
12365 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12366 0xffffffff, 0x00000000 },
12367 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12368 0xffffffff, 0x00000000 },
12369
12370 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012371 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012372 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012373 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012374 0x00000000, 0x007fffff },
12375 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12376 0x00000000, 0x0000003f },
12377 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12378 0x00000000, 0x000001ff },
12379 { BUFMGR_MB_HIGH_WATER, 0x0000,
12380 0x00000000, 0x000001ff },
12381 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12382 0xffffffff, 0x00000000 },
12383 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12384 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012385
Michael Chana71116d2005-05-29 14:58:11 -070012386 /* Mailbox Registers */
12387 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12388 0x00000000, 0x000001ff },
12389 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12390 0x00000000, 0x000001ff },
12391 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12392 0x00000000, 0x000007ff },
12393 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12394 0x00000000, 0x000001ff },
12395
12396 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12397 };
12398
Michael Chanb16250e2006-09-27 16:10:14 -070012399 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012400 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012401 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012402 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012403 is_5750 = 1;
12404 }
Michael Chana71116d2005-05-29 14:58:11 -070012405
12406 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12407 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12408 continue;
12409
12410 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12411 continue;
12412
Joe Perches63c3a662011-04-26 08:12:10 +000012413 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012414 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12415 continue;
12416
Michael Chanb16250e2006-09-27 16:10:14 -070012417 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12418 continue;
12419
Michael Chana71116d2005-05-29 14:58:11 -070012420 offset = (u32) reg_tbl[i].offset;
12421 read_mask = reg_tbl[i].read_mask;
12422 write_mask = reg_tbl[i].write_mask;
12423
12424 /* Save the original register content */
12425 save_val = tr32(offset);
12426
12427 /* Determine the read-only value. */
12428 read_val = save_val & read_mask;
12429
12430 /* Write zero to the register, then make sure the read-only bits
12431 * are not changed and the read/write bits are all zeros.
12432 */
12433 tw32(offset, 0);
12434
12435 val = tr32(offset);
12436
12437 /* Test the read-only and read/write bits. */
12438 if (((val & read_mask) != read_val) || (val & write_mask))
12439 goto out;
12440
12441 /* Write ones to all the bits defined by RdMask and WrMask, then
12442 * make sure the read-only bits are not changed and the
12443 * read/write bits are all ones.
12444 */
12445 tw32(offset, read_mask | write_mask);
12446
12447 val = tr32(offset);
12448
12449 /* Test the read-only bits. */
12450 if ((val & read_mask) != read_val)
12451 goto out;
12452
12453 /* Test the read/write bits. */
12454 if ((val & write_mask) != write_mask)
12455 goto out;
12456
12457 tw32(offset, save_val);
12458 }
12459
12460 return 0;
12461
12462out:
Michael Chan9f88f292006-12-07 00:22:54 -080012463 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012464 netdev_err(tp->dev,
12465 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012466 tw32(offset, save_val);
12467 return -EIO;
12468}
12469
Michael Chan7942e1d2005-05-29 14:58:36 -070012470static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12471{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012472 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012473 int i;
12474 u32 j;
12475
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012476 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012477 for (j = 0; j < len; j += 4) {
12478 u32 val;
12479
12480 tg3_write_mem(tp, offset + j, test_pattern[i]);
12481 tg3_read_mem(tp, offset + j, &val);
12482 if (val != test_pattern[i])
12483 return -EIO;
12484 }
12485 }
12486 return 0;
12487}
12488
12489static int tg3_test_memory(struct tg3 *tp)
12490{
12491 static struct mem_entry {
12492 u32 offset;
12493 u32 len;
12494 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012495 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012496 { 0x00002000, 0x1c000},
12497 { 0xffffffff, 0x00000}
12498 }, mem_tbl_5705[] = {
12499 { 0x00000100, 0x0000c},
12500 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012501 { 0x00004000, 0x00800},
12502 { 0x00006000, 0x01000},
12503 { 0x00008000, 0x02000},
12504 { 0x00010000, 0x0e000},
12505 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012506 }, mem_tbl_5755[] = {
12507 { 0x00000200, 0x00008},
12508 { 0x00004000, 0x00800},
12509 { 0x00006000, 0x00800},
12510 { 0x00008000, 0x02000},
12511 { 0x00010000, 0x0c000},
12512 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012513 }, mem_tbl_5906[] = {
12514 { 0x00000200, 0x00008},
12515 { 0x00004000, 0x00400},
12516 { 0x00006000, 0x00400},
12517 { 0x00008000, 0x01000},
12518 { 0x00010000, 0x01000},
12519 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012520 }, mem_tbl_5717[] = {
12521 { 0x00000200, 0x00008},
12522 { 0x00010000, 0x0a000},
12523 { 0x00020000, 0x13c00},
12524 { 0xffffffff, 0x00000}
12525 }, mem_tbl_57765[] = {
12526 { 0x00000200, 0x00008},
12527 { 0x00004000, 0x00800},
12528 { 0x00006000, 0x09800},
12529 { 0x00010000, 0x0a000},
12530 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012531 };
12532 struct mem_entry *mem_tbl;
12533 int err = 0;
12534 int i;
12535
Joe Perches63c3a662011-04-26 08:12:10 +000012536 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012537 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000012538 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000012539 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012540 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012541 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012542 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000012543 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080012544 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012545 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012546 mem_tbl = mem_tbl_5705;
12547 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012548 mem_tbl = mem_tbl_570x;
12549
12550 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012551 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12552 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012553 break;
12554 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012555
Michael Chan7942e1d2005-05-29 14:58:36 -070012556 return err;
12557}
12558
Matt Carlsonbb158d62011-04-25 12:42:47 +000012559#define TG3_TSO_MSS 500
12560
12561#define TG3_TSO_IP_HDR_LEN 20
12562#define TG3_TSO_TCP_HDR_LEN 20
12563#define TG3_TSO_TCP_OPT_LEN 12
12564
12565static const u8 tg3_tso_header[] = {
125660x08, 0x00,
125670x45, 0x00, 0x00, 0x00,
125680x00, 0x00, 0x40, 0x00,
125690x40, 0x06, 0x00, 0x00,
125700x0a, 0x00, 0x00, 0x01,
125710x0a, 0x00, 0x00, 0x02,
125720x0d, 0x00, 0xe0, 0x00,
125730x00, 0x00, 0x01, 0x00,
125740x00, 0x00, 0x02, 0x00,
125750x80, 0x10, 0x10, 0x00,
125760x14, 0x09, 0x00, 0x00,
125770x01, 0x01, 0x08, 0x0a,
125780x11, 0x11, 0x11, 0x11,
125790x11, 0x11, 0x11, 0x11,
12580};
Michael Chan9f40dea2005-09-05 17:53:06 -070012581
Matt Carlson28a45952011-08-19 13:58:22 +000012582static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012583{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012584 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012585 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012586 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012587 struct sk_buff *skb;
12588 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012589 dma_addr_t map;
12590 int num_pkts, tx_len, rx_len, i, err;
12591 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012592 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000012593 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070012594
Matt Carlsonc8873402010-02-12 14:47:11 +000012595 tnapi = &tp->napi[0];
12596 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012597 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000012598 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000012599 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000012600 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000012601 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012602 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012603 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000012604
Michael Chanc76949a2005-05-29 14:58:59 -070012605 err = -EIO;
12606
Matt Carlson4852a862011-04-13 11:05:07 +000012607 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070012608 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070012609 if (!skb)
12610 return -ENOMEM;
12611
Michael Chanc76949a2005-05-29 14:58:59 -070012612 tx_data = skb_put(skb, tx_len);
12613 memcpy(tx_data, tp->dev->dev_addr, 6);
12614 memset(tx_data + 6, 0x0, 8);
12615
Matt Carlson4852a862011-04-13 11:05:07 +000012616 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070012617
Matt Carlson28a45952011-08-19 13:58:22 +000012618 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012619 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
12620
12621 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
12622 TG3_TSO_TCP_OPT_LEN;
12623
12624 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
12625 sizeof(tg3_tso_header));
12626 mss = TG3_TSO_MSS;
12627
12628 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
12629 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
12630
12631 /* Set the total length field in the IP header */
12632 iph->tot_len = htons((u16)(mss + hdr_len));
12633
12634 base_flags = (TXD_FLAG_CPU_PRE_DMA |
12635 TXD_FLAG_CPU_POST_DMA);
12636
Joe Perches63c3a662011-04-26 08:12:10 +000012637 if (tg3_flag(tp, HW_TSO_1) ||
12638 tg3_flag(tp, HW_TSO_2) ||
12639 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012640 struct tcphdr *th;
12641 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
12642 th = (struct tcphdr *)&tx_data[val];
12643 th->check = 0;
12644 } else
12645 base_flags |= TXD_FLAG_TCPUDP_CSUM;
12646
Joe Perches63c3a662011-04-26 08:12:10 +000012647 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012648 mss |= (hdr_len & 0xc) << 12;
12649 if (hdr_len & 0x10)
12650 base_flags |= 0x00000010;
12651 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000012652 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012653 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000012654 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000012655 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012656 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
12657 } else {
12658 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
12659 }
12660
12661 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
12662 } else {
12663 num_pkts = 1;
12664 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000012665
12666 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
12667 tx_len > VLAN_ETH_FRAME_LEN)
12668 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012669 }
12670
12671 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070012672 tx_data[i] = (u8) (i & 0xff);
12673
Alexander Duyckf4188d82009-12-02 16:48:38 +000012674 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
12675 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000012676 dev_kfree_skb(skb);
12677 return -EIO;
12678 }
Michael Chanc76949a2005-05-29 14:58:59 -070012679
Matt Carlson0d681b22011-07-27 14:20:49 +000012680 val = tnapi->tx_prod;
12681 tnapi->tx_buffers[val].skb = skb;
12682 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
12683
Michael Chanc76949a2005-05-29 14:58:59 -070012684 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012685 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012686
12687 udelay(10);
12688
Matt Carlson898a56f2009-08-28 14:02:40 +000012689 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070012690
Matt Carlson84b67b22011-07-27 14:20:52 +000012691 budget = tg3_tx_avail(tnapi);
12692 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000012693 base_flags | TXD_FLAG_END, mss, 0)) {
12694 tnapi->tx_buffers[val].skb = NULL;
12695 dev_kfree_skb(skb);
12696 return -EIO;
12697 }
Michael Chanc76949a2005-05-29 14:58:59 -070012698
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012699 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070012700
Michael Chan6541b802012-03-04 14:48:14 +000012701 /* Sync BD data before updating mailbox */
12702 wmb();
12703
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012704 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
12705 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070012706
12707 udelay(10);
12708
Matt Carlson303fc922009-11-02 14:27:34 +000012709 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
12710 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070012711 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012712 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012713
12714 udelay(10);
12715
Matt Carlson898a56f2009-08-28 14:02:40 +000012716 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
12717 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012718 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070012719 (rx_idx == (rx_start_idx + num_pkts)))
12720 break;
12721 }
12722
Matt Carlsonba1142e2011-11-04 09:15:00 +000012723 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070012724 dev_kfree_skb(skb);
12725
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012726 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070012727 goto out;
12728
12729 if (rx_idx != rx_start_idx + num_pkts)
12730 goto out;
12731
Matt Carlsonbb158d62011-04-25 12:42:47 +000012732 val = data_off;
12733 while (rx_idx != rx_start_idx) {
12734 desc = &rnapi->rx_rcb[rx_start_idx++];
12735 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
12736 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070012737
Matt Carlsonbb158d62011-04-25 12:42:47 +000012738 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
12739 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000012740 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070012741
Matt Carlsonbb158d62011-04-25 12:42:47 +000012742 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
12743 - ETH_FCS_LEN;
12744
Matt Carlson28a45952011-08-19 13:58:22 +000012745 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012746 if (rx_len != tx_len)
12747 goto out;
12748
12749 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
12750 if (opaque_key != RXD_OPAQUE_RING_STD)
12751 goto out;
12752 } else {
12753 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
12754 goto out;
12755 }
12756 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
12757 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000012758 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012759 goto out;
12760 }
12761
12762 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012763 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012764 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
12765 mapping);
12766 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012767 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012768 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
12769 mapping);
12770 } else
Matt Carlson4852a862011-04-13 11:05:07 +000012771 goto out;
12772
Matt Carlsonbb158d62011-04-25 12:42:47 +000012773 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
12774 PCI_DMA_FROMDEVICE);
12775
Eric Dumazet9205fd92011-11-18 06:47:01 +000012776 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000012777 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012778 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012779 goto out;
12780 }
Matt Carlson4852a862011-04-13 11:05:07 +000012781 }
12782
Michael Chanc76949a2005-05-29 14:58:59 -070012783 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012784
Eric Dumazet9205fd92011-11-18 06:47:01 +000012785 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070012786out:
12787 return err;
12788}
12789
Matt Carlson00c266b2011-04-25 12:42:46 +000012790#define TG3_STD_LOOPBACK_FAILED 1
12791#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000012792#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000012793#define TG3_LOOPBACK_FAILED \
12794 (TG3_STD_LOOPBACK_FAILED | \
12795 TG3_JMB_LOOPBACK_FAILED | \
12796 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000012797
Matt Carlson941ec902011-08-19 13:58:23 +000012798static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070012799{
Matt Carlson28a45952011-08-19 13:58:22 +000012800 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000012801 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000012802 u32 jmb_pkt_sz = 9000;
12803
12804 if (tp->dma_limit)
12805 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070012806
Matt Carlsonab789042011-01-25 15:58:54 +000012807 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
12808 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
12809
Matt Carlson28a45952011-08-19 13:58:22 +000012810 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012811 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12812 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012813 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012814 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000012815 goto done;
12816 }
12817
Michael Chanb9ec6c12006-07-25 16:37:27 -070012818 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000012819 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012820 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12821 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012822 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012823 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000012824 goto done;
12825 }
Michael Chan9f40dea2005-09-05 17:53:06 -070012826
Joe Perches63c3a662011-04-26 08:12:10 +000012827 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000012828 int i;
12829
12830 /* Reroute all rx packets to the 1st queue */
12831 for (i = MAC_RSS_INDIR_TBL_0;
12832 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
12833 tw32(i, 0x0);
12834 }
12835
Matt Carlson6e01b202011-08-19 13:58:20 +000012836 /* HW errata - mac loopback fails in some cases on 5780.
12837 * Normal traffic and PHY loopback are not affected by
12838 * errata. Also, the MAC loopback test is deprecated for
12839 * all newer ASIC revisions.
12840 */
Joe Perches41535772013-02-16 11:20:04 +000012841 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000012842 !tg3_flag(tp, CPMU_PRESENT)) {
12843 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070012844
Matt Carlson28a45952011-08-19 13:58:22 +000012845 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012846 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012847
12848 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012849 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012850 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012851
12852 tg3_mac_loopback(tp, false);
12853 }
Matt Carlson4852a862011-04-13 11:05:07 +000012854
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012855 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012856 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012857 int i;
12858
Matt Carlson941ec902011-08-19 13:58:23 +000012859 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012860
12861 /* Wait for link */
12862 for (i = 0; i < 100; i++) {
12863 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
12864 break;
12865 mdelay(1);
12866 }
12867
Matt Carlson28a45952011-08-19 13:58:22 +000012868 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012869 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012870 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012871 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012872 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012873 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012874 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012875 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012876
Matt Carlson941ec902011-08-19 13:58:23 +000012877 if (do_extlpbk) {
12878 tg3_phy_lpbk_set(tp, 0, true);
12879
12880 /* All link indications report up, but the hardware
12881 * isn't really ready for about 20 msec. Double it
12882 * to be sure.
12883 */
12884 mdelay(40);
12885
12886 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012887 data[TG3_EXT_LOOPB_TEST] |=
12888 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012889 if (tg3_flag(tp, TSO_CAPABLE) &&
12890 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012891 data[TG3_EXT_LOOPB_TEST] |=
12892 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012893 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012894 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012895 data[TG3_EXT_LOOPB_TEST] |=
12896 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012897 }
12898
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012899 /* Re-enable gphy autopowerdown. */
12900 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12901 tg3_phy_toggle_apd(tp, true);
12902 }
Matt Carlson6833c042008-11-21 17:18:59 -080012903
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012904 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
12905 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000012906
Matt Carlsonab789042011-01-25 15:58:54 +000012907done:
12908 tp->phy_flags |= eee_cap;
12909
Michael Chan9f40dea2005-09-05 17:53:06 -070012910 return err;
12911}
12912
Michael Chan4cafd3f2005-05-29 14:56:34 -070012913static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12914 u64 *data)
12915{
Michael Chan566f86a2005-05-29 14:56:58 -070012916 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012917 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012918
Matt Carlsonbed98292011-07-13 09:27:29 +000012919 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12920 tg3_power_up(tp)) {
12921 etest->flags |= ETH_TEST_FL_FAILED;
12922 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12923 return;
12924 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012925
Michael Chan566f86a2005-05-29 14:56:58 -070012926 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12927
12928 if (tg3_test_nvram(tp) != 0) {
12929 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012930 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070012931 }
Matt Carlson941ec902011-08-19 13:58:23 +000012932 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012933 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012934 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070012935 }
Michael Chana71116d2005-05-29 14:58:11 -070012936 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012937 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012938
Michael Chanbbe832c2005-06-24 20:20:04 -070012939 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012940 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012941 tg3_netif_stop(tp);
12942 irq_sync = 1;
12943 }
12944
12945 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012946 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012947 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012948 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012949 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012950 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012951 if (!err)
12952 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012953
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012954 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080012955 tg3_phy_reset(tp);
12956
Michael Chana71116d2005-05-29 14:58:11 -070012957 if (tg3_test_registers(tp) != 0) {
12958 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012959 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070012960 }
Matt Carlson28a45952011-08-19 13:58:22 +000012961
Michael Chan7942e1d2005-05-29 14:58:36 -070012962 if (tg3_test_memory(tp) != 0) {
12963 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012964 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070012965 }
Matt Carlson28a45952011-08-19 13:58:22 +000012966
Matt Carlson941ec902011-08-19 13:58:23 +000012967 if (doextlpbk)
12968 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12969
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012970 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012971 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012972
David S. Millerf47c11e2005-06-24 20:18:35 -070012973 tg3_full_unlock(tp);
12974
Michael Chand4bc3922005-05-29 14:59:20 -070012975 if (tg3_test_interrupt(tp) != 0) {
12976 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012977 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012978 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012979
12980 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012981
Michael Chana71116d2005-05-29 14:58:11 -070012982 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12983 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012984 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012985 err2 = tg3_restart_hw(tp, 1);
12986 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012987 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012988 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012989
12990 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012991
12992 if (irq_sync && !err2)
12993 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012994 }
Matt Carlson80096062010-08-02 11:26:06 +000012995 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012996 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012997
Michael Chan4cafd3f2005-05-29 14:56:34 -070012998}
12999
Matt Carlson0a633ac2012-12-03 19:36:59 +000013000static int tg3_hwtstamp_ioctl(struct net_device *dev,
13001 struct ifreq *ifr, int cmd)
13002{
13003 struct tg3 *tp = netdev_priv(dev);
13004 struct hwtstamp_config stmpconf;
13005
13006 if (!tg3_flag(tp, PTP_CAPABLE))
13007 return -EINVAL;
13008
13009 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13010 return -EFAULT;
13011
13012 if (stmpconf.flags)
13013 return -EINVAL;
13014
13015 switch (stmpconf.tx_type) {
13016 case HWTSTAMP_TX_ON:
13017 tg3_flag_set(tp, TX_TSTAMP_EN);
13018 break;
13019 case HWTSTAMP_TX_OFF:
13020 tg3_flag_clear(tp, TX_TSTAMP_EN);
13021 break;
13022 default:
13023 return -ERANGE;
13024 }
13025
13026 switch (stmpconf.rx_filter) {
13027 case HWTSTAMP_FILTER_NONE:
13028 tp->rxptpctl = 0;
13029 break;
13030 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13031 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13032 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13033 break;
13034 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13035 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13036 TG3_RX_PTP_CTL_SYNC_EVNT;
13037 break;
13038 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13039 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13040 TG3_RX_PTP_CTL_DELAY_REQ;
13041 break;
13042 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13043 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13044 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13045 break;
13046 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13047 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13048 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13049 break;
13050 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13051 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13052 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13053 break;
13054 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13055 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13056 TG3_RX_PTP_CTL_SYNC_EVNT;
13057 break;
13058 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13059 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13060 TG3_RX_PTP_CTL_SYNC_EVNT;
13061 break;
13062 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13063 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13064 TG3_RX_PTP_CTL_SYNC_EVNT;
13065 break;
13066 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13067 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13068 TG3_RX_PTP_CTL_DELAY_REQ;
13069 break;
13070 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13071 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13072 TG3_RX_PTP_CTL_DELAY_REQ;
13073 break;
13074 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13075 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13076 TG3_RX_PTP_CTL_DELAY_REQ;
13077 break;
13078 default:
13079 return -ERANGE;
13080 }
13081
13082 if (netif_running(dev) && tp->rxptpctl)
13083 tw32(TG3_RX_PTP_CTL,
13084 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13085
13086 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13087 -EFAULT : 0;
13088}
13089
Linus Torvalds1da177e2005-04-16 15:20:36 -070013090static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13091{
13092 struct mii_ioctl_data *data = if_mii(ifr);
13093 struct tg3 *tp = netdev_priv(dev);
13094 int err;
13095
Joe Perches63c3a662011-04-26 08:12:10 +000013096 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013097 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013098 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013099 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013100 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000013101 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013102 }
13103
Matt Carlson33f401a2010-04-05 10:19:27 +000013104 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013105 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013106 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013107
13108 /* fallthru */
13109 case SIOCGMIIREG: {
13110 u32 mii_regval;
13111
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013112 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013113 break; /* We have no PHY */
13114
Matt Carlson34eea5a2011-04-20 07:57:38 +000013115 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013116 return -EAGAIN;
13117
David S. Millerf47c11e2005-06-24 20:18:35 -070013118 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013119 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13120 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013121 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013122
13123 data->val_out = mii_regval;
13124
13125 return err;
13126 }
13127
13128 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013129 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013130 break; /* We have no PHY */
13131
Matt Carlson34eea5a2011-04-20 07:57:38 +000013132 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013133 return -EAGAIN;
13134
David S. Millerf47c11e2005-06-24 20:18:35 -070013135 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013136 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13137 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013138 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013139
13140 return err;
13141
Matt Carlson0a633ac2012-12-03 19:36:59 +000013142 case SIOCSHWTSTAMP:
13143 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
13144
Linus Torvalds1da177e2005-04-16 15:20:36 -070013145 default:
13146 /* do nothing */
13147 break;
13148 }
13149 return -EOPNOTSUPP;
13150}
13151
David S. Miller15f98502005-05-18 22:49:26 -070013152static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13153{
13154 struct tg3 *tp = netdev_priv(dev);
13155
13156 memcpy(ec, &tp->coal, sizeof(*ec));
13157 return 0;
13158}
13159
Michael Chand244c892005-07-05 14:42:33 -070013160static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13161{
13162 struct tg3 *tp = netdev_priv(dev);
13163 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13164 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13165
Joe Perches63c3a662011-04-26 08:12:10 +000013166 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013167 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13168 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13169 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13170 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13171 }
13172
13173 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13174 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13175 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13176 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13177 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13178 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13179 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13180 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13181 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13182 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13183 return -EINVAL;
13184
13185 /* No rx interrupts will be generated if both are zero */
13186 if ((ec->rx_coalesce_usecs == 0) &&
13187 (ec->rx_max_coalesced_frames == 0))
13188 return -EINVAL;
13189
13190 /* No tx interrupts will be generated if both are zero */
13191 if ((ec->tx_coalesce_usecs == 0) &&
13192 (ec->tx_max_coalesced_frames == 0))
13193 return -EINVAL;
13194
13195 /* Only copy relevant parameters, ignore all others. */
13196 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13197 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13198 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13199 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13200 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13201 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13202 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13203 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13204 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13205
13206 if (netif_running(dev)) {
13207 tg3_full_lock(tp, 0);
13208 __tg3_set_coalesce(tp, &tp->coal);
13209 tg3_full_unlock(tp);
13210 }
13211 return 0;
13212}
13213
Jeff Garzik7282d492006-09-13 14:30:00 -040013214static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013215 .get_settings = tg3_get_settings,
13216 .set_settings = tg3_set_settings,
13217 .get_drvinfo = tg3_get_drvinfo,
13218 .get_regs_len = tg3_get_regs_len,
13219 .get_regs = tg3_get_regs,
13220 .get_wol = tg3_get_wol,
13221 .set_wol = tg3_set_wol,
13222 .get_msglevel = tg3_get_msglevel,
13223 .set_msglevel = tg3_set_msglevel,
13224 .nway_reset = tg3_nway_reset,
13225 .get_link = ethtool_op_get_link,
13226 .get_eeprom_len = tg3_get_eeprom_len,
13227 .get_eeprom = tg3_get_eeprom,
13228 .set_eeprom = tg3_set_eeprom,
13229 .get_ringparam = tg3_get_ringparam,
13230 .set_ringparam = tg3_set_ringparam,
13231 .get_pauseparam = tg3_get_pauseparam,
13232 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013233 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013234 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013235 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013236 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013237 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013238 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013239 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013240 .get_rxnfc = tg3_get_rxnfc,
13241 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13242 .get_rxfh_indir = tg3_get_rxfh_indir,
13243 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013244 .get_channels = tg3_get_channels,
13245 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013246 .get_ts_info = tg3_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013247};
13248
David S. Millerb4017c52012-03-01 17:57:40 -050013249static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13250 struct rtnl_link_stats64 *stats)
13251{
13252 struct tg3 *tp = netdev_priv(dev);
13253
David S. Millerb4017c52012-03-01 17:57:40 -050013254 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013255 if (!tp->hw_stats) {
13256 spin_unlock_bh(&tp->lock);
13257 return &tp->net_stats_prev;
13258 }
13259
David S. Millerb4017c52012-03-01 17:57:40 -050013260 tg3_get_nstats(tp, stats);
13261 spin_unlock_bh(&tp->lock);
13262
13263 return stats;
13264}
13265
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013266static void tg3_set_rx_mode(struct net_device *dev)
13267{
13268 struct tg3 *tp = netdev_priv(dev);
13269
13270 if (!netif_running(dev))
13271 return;
13272
13273 tg3_full_lock(tp, 0);
13274 __tg3_set_rx_mode(dev);
13275 tg3_full_unlock(tp);
13276}
13277
Matt Carlsonfaf16272012-02-13 10:20:07 +000013278static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13279 int new_mtu)
13280{
13281 dev->mtu = new_mtu;
13282
13283 if (new_mtu > ETH_DATA_LEN) {
13284 if (tg3_flag(tp, 5780_CLASS)) {
13285 netdev_update_features(dev);
13286 tg3_flag_clear(tp, TSO_CAPABLE);
13287 } else {
13288 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13289 }
13290 } else {
13291 if (tg3_flag(tp, 5780_CLASS)) {
13292 tg3_flag_set(tp, TSO_CAPABLE);
13293 netdev_update_features(dev);
13294 }
13295 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13296 }
13297}
13298
13299static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13300{
13301 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000013302 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013303
13304 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13305 return -EINVAL;
13306
13307 if (!netif_running(dev)) {
13308 /* We'll just catch it later when the
13309 * device is up'd.
13310 */
13311 tg3_set_mtu(dev, tp, new_mtu);
13312 return 0;
13313 }
13314
13315 tg3_phy_stop(tp);
13316
13317 tg3_netif_stop(tp);
13318
13319 tg3_full_lock(tp, 1);
13320
13321 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13322
13323 tg3_set_mtu(dev, tp, new_mtu);
13324
Michael Chan2fae5e32012-03-04 14:48:15 +000013325 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13326 * breaks all requests to 256 bytes.
13327 */
Joe Perches41535772013-02-16 11:20:04 +000013328 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Michael Chan2fae5e32012-03-04 14:48:15 +000013329 reset_phy = 1;
13330
13331 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013332
13333 if (!err)
13334 tg3_netif_start(tp);
13335
13336 tg3_full_unlock(tp);
13337
13338 if (!err)
13339 tg3_phy_start(tp);
13340
13341 return err;
13342}
13343
13344static const struct net_device_ops tg3_netdev_ops = {
13345 .ndo_open = tg3_open,
13346 .ndo_stop = tg3_close,
13347 .ndo_start_xmit = tg3_start_xmit,
13348 .ndo_get_stats64 = tg3_get_stats64,
13349 .ndo_validate_addr = eth_validate_addr,
13350 .ndo_set_rx_mode = tg3_set_rx_mode,
13351 .ndo_set_mac_address = tg3_set_mac_addr,
13352 .ndo_do_ioctl = tg3_ioctl,
13353 .ndo_tx_timeout = tg3_tx_timeout,
13354 .ndo_change_mtu = tg3_change_mtu,
13355 .ndo_fix_features = tg3_fix_features,
13356 .ndo_set_features = tg3_set_features,
13357#ifdef CONFIG_NET_POLL_CONTROLLER
13358 .ndo_poll_controller = tg3_poll_controller,
13359#endif
13360};
13361
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013362static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013363{
Michael Chan1b277772006-03-20 22:27:48 -080013364 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013365
13366 tp->nvram_size = EEPROM_CHIP_SIZE;
13367
Matt Carlsone4f34112009-02-25 14:25:00 +000013368 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013369 return;
13370
Michael Chanb16250e2006-09-27 16:10:14 -070013371 if ((magic != TG3_EEPROM_MAGIC) &&
13372 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13373 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013374 return;
13375
13376 /*
13377 * Size the chip by reading offsets at increasing powers of two.
13378 * When we encounter our validation signature, we know the addressing
13379 * has wrapped around, and thus have our chip size.
13380 */
Michael Chan1b277772006-03-20 22:27:48 -080013381 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013382
13383 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013384 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013385 return;
13386
Michael Chan18201802006-03-20 22:29:15 -080013387 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013388 break;
13389
13390 cursize <<= 1;
13391 }
13392
13393 tp->nvram_size = cursize;
13394}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013395
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013396static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013397{
13398 u32 val;
13399
Joe Perches63c3a662011-04-26 08:12:10 +000013400 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013401 return;
13402
13403 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013404 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013405 tg3_get_eeprom_size(tp);
13406 return;
13407 }
13408
Matt Carlson6d348f22009-02-25 14:25:52 +000013409 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013410 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013411 /* This is confusing. We want to operate on the
13412 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13413 * call will read from NVRAM and byteswap the data
13414 * according to the byteswapping settings for all
13415 * other register accesses. This ensures the data we
13416 * want will always reside in the lower 16-bits.
13417 * However, the data in NVRAM is in LE format, which
13418 * means the data from the NVRAM read will always be
13419 * opposite the endianness of the CPU. The 16-bit
13420 * byteswap then brings the data to CPU endianness.
13421 */
13422 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013423 return;
13424 }
13425 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013426 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013427}
13428
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013429static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013430{
13431 u32 nvcfg1;
13432
13433 nvcfg1 = tr32(NVRAM_CFG1);
13434 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000013435 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013436 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013437 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13438 tw32(NVRAM_CFG1, nvcfg1);
13439 }
13440
Joe Perches41535772013-02-16 11:20:04 +000013441 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013442 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013443 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013444 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
13445 tp->nvram_jedecnum = JEDEC_ATMEL;
13446 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013447 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013448 break;
13449 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
13450 tp->nvram_jedecnum = JEDEC_ATMEL;
13451 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
13452 break;
13453 case FLASH_VENDOR_ATMEL_EEPROM:
13454 tp->nvram_jedecnum = JEDEC_ATMEL;
13455 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013456 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013457 break;
13458 case FLASH_VENDOR_ST:
13459 tp->nvram_jedecnum = JEDEC_ST;
13460 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013461 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013462 break;
13463 case FLASH_VENDOR_SAIFUN:
13464 tp->nvram_jedecnum = JEDEC_SAIFUN;
13465 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
13466 break;
13467 case FLASH_VENDOR_SST_SMALL:
13468 case FLASH_VENDOR_SST_LARGE:
13469 tp->nvram_jedecnum = JEDEC_SST;
13470 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
13471 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013472 }
Matt Carlson8590a602009-08-28 12:29:16 +000013473 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013474 tp->nvram_jedecnum = JEDEC_ATMEL;
13475 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013476 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013477 }
13478}
13479
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013480static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013481{
13482 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13483 case FLASH_5752PAGE_SIZE_256:
13484 tp->nvram_pagesize = 256;
13485 break;
13486 case FLASH_5752PAGE_SIZE_512:
13487 tp->nvram_pagesize = 512;
13488 break;
13489 case FLASH_5752PAGE_SIZE_1K:
13490 tp->nvram_pagesize = 1024;
13491 break;
13492 case FLASH_5752PAGE_SIZE_2K:
13493 tp->nvram_pagesize = 2048;
13494 break;
13495 case FLASH_5752PAGE_SIZE_4K:
13496 tp->nvram_pagesize = 4096;
13497 break;
13498 case FLASH_5752PAGE_SIZE_264:
13499 tp->nvram_pagesize = 264;
13500 break;
13501 case FLASH_5752PAGE_SIZE_528:
13502 tp->nvram_pagesize = 528;
13503 break;
13504 }
13505}
13506
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013507static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070013508{
13509 u32 nvcfg1;
13510
13511 nvcfg1 = tr32(NVRAM_CFG1);
13512
Michael Chane6af3012005-04-21 17:12:05 -070013513 /* NVRAM protection for TPM */
13514 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000013515 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070013516
Michael Chan361b4ac2005-04-21 17:11:21 -070013517 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013518 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
13519 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
13520 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013521 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013522 break;
13523 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13524 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013525 tg3_flag_set(tp, NVRAM_BUFFERED);
13526 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013527 break;
13528 case FLASH_5752VENDOR_ST_M45PE10:
13529 case FLASH_5752VENDOR_ST_M45PE20:
13530 case FLASH_5752VENDOR_ST_M45PE40:
13531 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013532 tg3_flag_set(tp, NVRAM_BUFFERED);
13533 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013534 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070013535 }
13536
Joe Perches63c3a662011-04-26 08:12:10 +000013537 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000013538 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000013539 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070013540 /* For eeprom, set pagesize to maximum eeprom size */
13541 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13542
13543 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13544 tw32(NVRAM_CFG1, nvcfg1);
13545 }
13546}
13547
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013548static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080013549{
Matt Carlson989a9d22007-05-05 11:51:05 -070013550 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080013551
13552 nvcfg1 = tr32(NVRAM_CFG1);
13553
13554 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070013555 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013556 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070013557 protect = 1;
13558 }
Michael Chand3c7b882006-03-23 01:28:25 -080013559
Matt Carlson989a9d22007-05-05 11:51:05 -070013560 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13561 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013562 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13563 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13564 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13565 case FLASH_5755VENDOR_ATMEL_FLASH_5:
13566 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013567 tg3_flag_set(tp, NVRAM_BUFFERED);
13568 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013569 tp->nvram_pagesize = 264;
13570 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
13571 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
13572 tp->nvram_size = (protect ? 0x3e200 :
13573 TG3_NVRAM_SIZE_512KB);
13574 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
13575 tp->nvram_size = (protect ? 0x1f200 :
13576 TG3_NVRAM_SIZE_256KB);
13577 else
13578 tp->nvram_size = (protect ? 0x1f200 :
13579 TG3_NVRAM_SIZE_128KB);
13580 break;
13581 case FLASH_5752VENDOR_ST_M45PE10:
13582 case FLASH_5752VENDOR_ST_M45PE20:
13583 case FLASH_5752VENDOR_ST_M45PE40:
13584 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013585 tg3_flag_set(tp, NVRAM_BUFFERED);
13586 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013587 tp->nvram_pagesize = 256;
13588 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
13589 tp->nvram_size = (protect ?
13590 TG3_NVRAM_SIZE_64KB :
13591 TG3_NVRAM_SIZE_128KB);
13592 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
13593 tp->nvram_size = (protect ?
13594 TG3_NVRAM_SIZE_64KB :
13595 TG3_NVRAM_SIZE_256KB);
13596 else
13597 tp->nvram_size = (protect ?
13598 TG3_NVRAM_SIZE_128KB :
13599 TG3_NVRAM_SIZE_512KB);
13600 break;
Michael Chand3c7b882006-03-23 01:28:25 -080013601 }
13602}
13603
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013604static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080013605{
13606 u32 nvcfg1;
13607
13608 nvcfg1 = tr32(NVRAM_CFG1);
13609
13610 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013611 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
13612 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13613 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
13614 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13615 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013616 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013617 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080013618
Matt Carlson8590a602009-08-28 12:29:16 +000013619 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13620 tw32(NVRAM_CFG1, nvcfg1);
13621 break;
13622 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13623 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13624 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13625 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13626 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013627 tg3_flag_set(tp, NVRAM_BUFFERED);
13628 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013629 tp->nvram_pagesize = 264;
13630 break;
13631 case FLASH_5752VENDOR_ST_M45PE10:
13632 case FLASH_5752VENDOR_ST_M45PE20:
13633 case FLASH_5752VENDOR_ST_M45PE40:
13634 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013635 tg3_flag_set(tp, NVRAM_BUFFERED);
13636 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013637 tp->nvram_pagesize = 256;
13638 break;
Michael Chan1b277772006-03-20 22:27:48 -080013639 }
13640}
13641
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013642static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070013643{
13644 u32 nvcfg1, protect = 0;
13645
13646 nvcfg1 = tr32(NVRAM_CFG1);
13647
13648 /* NVRAM protection for TPM */
13649 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013650 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013651 protect = 1;
13652 }
13653
13654 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13655 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013656 case FLASH_5761VENDOR_ATMEL_ADB021D:
13657 case FLASH_5761VENDOR_ATMEL_ADB041D:
13658 case FLASH_5761VENDOR_ATMEL_ADB081D:
13659 case FLASH_5761VENDOR_ATMEL_ADB161D:
13660 case FLASH_5761VENDOR_ATMEL_MDB021D:
13661 case FLASH_5761VENDOR_ATMEL_MDB041D:
13662 case FLASH_5761VENDOR_ATMEL_MDB081D:
13663 case FLASH_5761VENDOR_ATMEL_MDB161D:
13664 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013665 tg3_flag_set(tp, NVRAM_BUFFERED);
13666 tg3_flag_set(tp, FLASH);
13667 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000013668 tp->nvram_pagesize = 256;
13669 break;
13670 case FLASH_5761VENDOR_ST_A_M45PE20:
13671 case FLASH_5761VENDOR_ST_A_M45PE40:
13672 case FLASH_5761VENDOR_ST_A_M45PE80:
13673 case FLASH_5761VENDOR_ST_A_M45PE16:
13674 case FLASH_5761VENDOR_ST_M_M45PE20:
13675 case FLASH_5761VENDOR_ST_M_M45PE40:
13676 case FLASH_5761VENDOR_ST_M_M45PE80:
13677 case FLASH_5761VENDOR_ST_M_M45PE16:
13678 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013679 tg3_flag_set(tp, NVRAM_BUFFERED);
13680 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013681 tp->nvram_pagesize = 256;
13682 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013683 }
13684
13685 if (protect) {
13686 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
13687 } else {
13688 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013689 case FLASH_5761VENDOR_ATMEL_ADB161D:
13690 case FLASH_5761VENDOR_ATMEL_MDB161D:
13691 case FLASH_5761VENDOR_ST_A_M45PE16:
13692 case FLASH_5761VENDOR_ST_M_M45PE16:
13693 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
13694 break;
13695 case FLASH_5761VENDOR_ATMEL_ADB081D:
13696 case FLASH_5761VENDOR_ATMEL_MDB081D:
13697 case FLASH_5761VENDOR_ST_A_M45PE80:
13698 case FLASH_5761VENDOR_ST_M_M45PE80:
13699 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13700 break;
13701 case FLASH_5761VENDOR_ATMEL_ADB041D:
13702 case FLASH_5761VENDOR_ATMEL_MDB041D:
13703 case FLASH_5761VENDOR_ST_A_M45PE40:
13704 case FLASH_5761VENDOR_ST_M_M45PE40:
13705 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13706 break;
13707 case FLASH_5761VENDOR_ATMEL_ADB021D:
13708 case FLASH_5761VENDOR_ATMEL_MDB021D:
13709 case FLASH_5761VENDOR_ST_A_M45PE20:
13710 case FLASH_5761VENDOR_ST_M_M45PE20:
13711 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13712 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013713 }
13714 }
13715}
13716
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013717static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070013718{
13719 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013720 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070013721 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13722}
13723
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013724static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080013725{
13726 u32 nvcfg1;
13727
13728 nvcfg1 = tr32(NVRAM_CFG1);
13729
13730 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13731 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13732 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13733 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013734 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080013735 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13736
13737 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13738 tw32(NVRAM_CFG1, nvcfg1);
13739 return;
13740 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13741 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13742 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13743 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13744 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13745 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13746 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13747 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013748 tg3_flag_set(tp, NVRAM_BUFFERED);
13749 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013750
13751 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13752 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13753 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13754 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13755 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13756 break;
13757 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13758 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13759 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13760 break;
13761 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13762 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13763 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13764 break;
13765 }
13766 break;
13767 case FLASH_5752VENDOR_ST_M45PE10:
13768 case FLASH_5752VENDOR_ST_M45PE20:
13769 case FLASH_5752VENDOR_ST_M45PE40:
13770 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013771 tg3_flag_set(tp, NVRAM_BUFFERED);
13772 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013773
13774 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13775 case FLASH_5752VENDOR_ST_M45PE10:
13776 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13777 break;
13778 case FLASH_5752VENDOR_ST_M45PE20:
13779 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13780 break;
13781 case FLASH_5752VENDOR_ST_M45PE40:
13782 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13783 break;
13784 }
13785 break;
13786 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013787 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080013788 return;
13789 }
13790
Matt Carlsona1b950d2009-09-01 13:20:17 +000013791 tg3_nvram_get_pagesize(tp, nvcfg1);
13792 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013793 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013794}
13795
13796
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013797static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013798{
13799 u32 nvcfg1;
13800
13801 nvcfg1 = tr32(NVRAM_CFG1);
13802
13803 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13804 case FLASH_5717VENDOR_ATMEL_EEPROM:
13805 case FLASH_5717VENDOR_MICRO_EEPROM:
13806 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013807 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013808 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13809
13810 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13811 tw32(NVRAM_CFG1, nvcfg1);
13812 return;
13813 case FLASH_5717VENDOR_ATMEL_MDB011D:
13814 case FLASH_5717VENDOR_ATMEL_ADB011B:
13815 case FLASH_5717VENDOR_ATMEL_ADB011D:
13816 case FLASH_5717VENDOR_ATMEL_MDB021D:
13817 case FLASH_5717VENDOR_ATMEL_ADB021B:
13818 case FLASH_5717VENDOR_ATMEL_ADB021D:
13819 case FLASH_5717VENDOR_ATMEL_45USPT:
13820 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013821 tg3_flag_set(tp, NVRAM_BUFFERED);
13822 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013823
13824 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13825 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013826 /* Detect size with tg3_nvram_get_size() */
13827 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013828 case FLASH_5717VENDOR_ATMEL_ADB021B:
13829 case FLASH_5717VENDOR_ATMEL_ADB021D:
13830 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13831 break;
13832 default:
13833 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13834 break;
13835 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013836 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013837 case FLASH_5717VENDOR_ST_M_M25PE10:
13838 case FLASH_5717VENDOR_ST_A_M25PE10:
13839 case FLASH_5717VENDOR_ST_M_M45PE10:
13840 case FLASH_5717VENDOR_ST_A_M45PE10:
13841 case FLASH_5717VENDOR_ST_M_M25PE20:
13842 case FLASH_5717VENDOR_ST_A_M25PE20:
13843 case FLASH_5717VENDOR_ST_M_M45PE20:
13844 case FLASH_5717VENDOR_ST_A_M45PE20:
13845 case FLASH_5717VENDOR_ST_25USPT:
13846 case FLASH_5717VENDOR_ST_45USPT:
13847 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013848 tg3_flag_set(tp, NVRAM_BUFFERED);
13849 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013850
13851 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13852 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013853 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013854 /* Detect size with tg3_nvram_get_size() */
13855 break;
13856 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013857 case FLASH_5717VENDOR_ST_A_M45PE20:
13858 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13859 break;
13860 default:
13861 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13862 break;
13863 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013864 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013865 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013866 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013867 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080013868 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000013869
13870 tg3_nvram_get_pagesize(tp, nvcfg1);
13871 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013872 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013873}
13874
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013875static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013876{
13877 u32 nvcfg1, nvmpinstrp;
13878
13879 nvcfg1 = tr32(NVRAM_CFG1);
13880 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
13881
Joe Perches41535772013-02-16 11:20:04 +000013882 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000013883 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
13884 tg3_flag_set(tp, NO_NVRAM);
13885 return;
13886 }
13887
13888 switch (nvmpinstrp) {
13889 case FLASH_5762_EEPROM_HD:
13890 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030013891 break;
Michael Chanc86a8562013-01-06 12:51:08 +000013892 case FLASH_5762_EEPROM_LD:
13893 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030013894 break;
Michael Chanc86a8562013-01-06 12:51:08 +000013895 }
13896 }
13897
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013898 switch (nvmpinstrp) {
13899 case FLASH_5720_EEPROM_HD:
13900 case FLASH_5720_EEPROM_LD:
13901 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013902 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013903
13904 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13905 tw32(NVRAM_CFG1, nvcfg1);
13906 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
13907 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13908 else
13909 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
13910 return;
13911 case FLASH_5720VENDOR_M_ATMEL_DB011D:
13912 case FLASH_5720VENDOR_A_ATMEL_DB011B:
13913 case FLASH_5720VENDOR_A_ATMEL_DB011D:
13914 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13915 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13916 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13917 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13918 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13919 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13920 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13921 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13922 case FLASH_5720VENDOR_ATMEL_45USPT:
13923 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013924 tg3_flag_set(tp, NVRAM_BUFFERED);
13925 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013926
13927 switch (nvmpinstrp) {
13928 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13929 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13930 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13931 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13932 break;
13933 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13934 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13935 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13936 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13937 break;
13938 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13939 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13940 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13941 break;
13942 default:
Joe Perches41535772013-02-16 11:20:04 +000013943 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000013944 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013945 break;
13946 }
13947 break;
13948 case FLASH_5720VENDOR_M_ST_M25PE10:
13949 case FLASH_5720VENDOR_M_ST_M45PE10:
13950 case FLASH_5720VENDOR_A_ST_M25PE10:
13951 case FLASH_5720VENDOR_A_ST_M45PE10:
13952 case FLASH_5720VENDOR_M_ST_M25PE20:
13953 case FLASH_5720VENDOR_M_ST_M45PE20:
13954 case FLASH_5720VENDOR_A_ST_M25PE20:
13955 case FLASH_5720VENDOR_A_ST_M45PE20:
13956 case FLASH_5720VENDOR_M_ST_M25PE40:
13957 case FLASH_5720VENDOR_M_ST_M45PE40:
13958 case FLASH_5720VENDOR_A_ST_M25PE40:
13959 case FLASH_5720VENDOR_A_ST_M45PE40:
13960 case FLASH_5720VENDOR_M_ST_M25PE80:
13961 case FLASH_5720VENDOR_M_ST_M45PE80:
13962 case FLASH_5720VENDOR_A_ST_M25PE80:
13963 case FLASH_5720VENDOR_A_ST_M45PE80:
13964 case FLASH_5720VENDOR_ST_25USPT:
13965 case FLASH_5720VENDOR_ST_45USPT:
13966 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013967 tg3_flag_set(tp, NVRAM_BUFFERED);
13968 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013969
13970 switch (nvmpinstrp) {
13971 case FLASH_5720VENDOR_M_ST_M25PE20:
13972 case FLASH_5720VENDOR_M_ST_M45PE20:
13973 case FLASH_5720VENDOR_A_ST_M25PE20:
13974 case FLASH_5720VENDOR_A_ST_M45PE20:
13975 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13976 break;
13977 case FLASH_5720VENDOR_M_ST_M25PE40:
13978 case FLASH_5720VENDOR_M_ST_M45PE40:
13979 case FLASH_5720VENDOR_A_ST_M25PE40:
13980 case FLASH_5720VENDOR_A_ST_M45PE40:
13981 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13982 break;
13983 case FLASH_5720VENDOR_M_ST_M25PE80:
13984 case FLASH_5720VENDOR_M_ST_M45PE80:
13985 case FLASH_5720VENDOR_A_ST_M25PE80:
13986 case FLASH_5720VENDOR_A_ST_M45PE80:
13987 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13988 break;
13989 default:
Joe Perches41535772013-02-16 11:20:04 +000013990 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000013991 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013992 break;
13993 }
13994 break;
13995 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013996 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013997 return;
13998 }
13999
14000 tg3_nvram_get_pagesize(tp, nvcfg1);
14001 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014002 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014003
Joe Perches41535772013-02-16 11:20:04 +000014004 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014005 u32 val;
14006
14007 if (tg3_nvram_read(tp, 0, &val))
14008 return;
14009
14010 if (val != TG3_EEPROM_MAGIC &&
14011 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14012 tg3_flag_set(tp, NO_NVRAM);
14013 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014014}
14015
Linus Torvalds1da177e2005-04-16 15:20:36 -070014016/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014017static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014018{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014019 if (tg3_flag(tp, IS_SSB_CORE)) {
14020 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14021 tg3_flag_clear(tp, NVRAM);
14022 tg3_flag_clear(tp, NVRAM_BUFFERED);
14023 tg3_flag_set(tp, NO_NVRAM);
14024 return;
14025 }
14026
Linus Torvalds1da177e2005-04-16 15:20:36 -070014027 tw32_f(GRC_EEPROM_ADDR,
14028 (EEPROM_ADDR_FSM_RESET |
14029 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14030 EEPROM_ADDR_CLKPERD_SHIFT)));
14031
Michael Chan9d57f012006-12-07 00:23:25 -080014032 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014033
14034 /* Enable seeprom accesses. */
14035 tw32_f(GRC_LOCAL_CTRL,
14036 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14037 udelay(100);
14038
Joe Perches41535772013-02-16 11:20:04 +000014039 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14040 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014041 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014042
Michael Chanec41c7d2006-01-17 02:40:55 -080014043 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014044 netdev_warn(tp->dev,
14045 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014046 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014047 return;
14048 }
Michael Chane6af3012005-04-21 17:12:05 -070014049 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014050
Matt Carlson989a9d22007-05-05 11:51:05 -070014051 tp->nvram_size = 0;
14052
Joe Perches41535772013-02-16 11:20:04 +000014053 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014054 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014055 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014056 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014057 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14058 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14059 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014060 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014061 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014062 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014063 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014064 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014065 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014066 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014067 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014068 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14069 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014070 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014071 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14072 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014073 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014074 else
14075 tg3_get_nvram_info(tp);
14076
Matt Carlson989a9d22007-05-05 11:51:05 -070014077 if (tp->nvram_size == 0)
14078 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014079
Michael Chane6af3012005-04-21 17:12:05 -070014080 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014081 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014082
14083 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014084 tg3_flag_clear(tp, NVRAM);
14085 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014086
14087 tg3_get_eeprom_size(tp);
14088 }
14089}
14090
Linus Torvalds1da177e2005-04-16 15:20:36 -070014091struct subsys_tbl_ent {
14092 u16 subsys_vendor, subsys_devid;
14093 u32 phy_id;
14094};
14095
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014096static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014097 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014098 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014099 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014100 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014101 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014102 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014103 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014104 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14105 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14106 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014107 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014108 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014109 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014110 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14111 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14112 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014113 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014114 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014115 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014116 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014117 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014118 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014119 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014120
14121 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014122 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014123 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014124 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014125 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014126 { TG3PCI_SUBVENDOR_ID_3COM,
14127 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14128 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014129 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014130 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014131 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014132
14133 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014134 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014135 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014136 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014137 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014138 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014139 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014140 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014141 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014142
14143 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014144 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014145 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014146 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014147 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014148 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14149 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14150 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014151 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014152 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014153 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014154
14155 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014156 { TG3PCI_SUBVENDOR_ID_IBM,
14157 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014158};
14159
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014160static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014161{
14162 int i;
14163
14164 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14165 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14166 tp->pdev->subsystem_vendor) &&
14167 (subsys_id_to_phy_id[i].subsys_devid ==
14168 tp->pdev->subsystem_device))
14169 return &subsys_id_to_phy_id[i];
14170 }
14171 return NULL;
14172}
14173
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014174static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014175{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014176 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014177
Matt Carlson79eb6902010-02-17 15:17:03 +000014178 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014179 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14180
Gary Zambranoa85feb82007-05-05 11:52:19 -070014181 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014182 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14183 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014184
Joe Perches41535772013-02-16 11:20:04 +000014185 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014186 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014187 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14188 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014189 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014190 val = tr32(VCPU_CFGSHDW);
14191 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014192 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014193 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014194 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014195 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014196 device_set_wakeup_enable(&tp->pdev->dev, true);
14197 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014198 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014199 }
14200
Linus Torvalds1da177e2005-04-16 15:20:36 -070014201 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14202 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14203 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014204 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014205 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014206
14207 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14208 tp->nic_sram_data_cfg = nic_cfg;
14209
14210 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14211 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000014212 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14213 tg3_asic_rev(tp) != ASIC_REV_5701 &&
14214 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014215 (ver > 0) && (ver < 0x100))
14216 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14217
Joe Perches41535772013-02-16 11:20:04 +000014218 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070014219 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14220
Linus Torvalds1da177e2005-04-16 15:20:36 -070014221 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14222 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14223 eeprom_phy_serdes = 1;
14224
14225 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14226 if (nic_phy_id != 0) {
14227 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14228 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14229
14230 eeprom_phy_id = (id1 >> 16) << 10;
14231 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14232 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14233 } else
14234 eeprom_phy_id = 0;
14235
Michael Chan7d0c41e2005-04-21 17:06:20 -070014236 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014237 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014238 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014239 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014240 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014241 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014242 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014243
Joe Perches63c3a662011-04-26 08:12:10 +000014244 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014245 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14246 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014247 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014248 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14249
14250 switch (led_cfg) {
14251 default:
14252 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14253 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14254 break;
14255
14256 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14257 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14258 break;
14259
14260 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14261 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014262
14263 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14264 * read on some older 5700/5701 bootcode.
14265 */
Joe Perches41535772013-02-16 11:20:04 +000014266 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
14267 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070014268 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14269
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270 break;
14271
14272 case SHASTA_EXT_LED_SHARED:
14273 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000014274 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
14275 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014276 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14277 LED_CTRL_MODE_PHY_2);
14278 break;
14279
14280 case SHASTA_EXT_LED_MAC:
14281 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14282 break;
14283
14284 case SHASTA_EXT_LED_COMBO:
14285 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000014286 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014287 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14288 LED_CTRL_MODE_PHY_2);
14289 break;
14290
Stephen Hemminger855e1112008-04-16 16:37:28 -070014291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014292
Joe Perches41535772013-02-16 11:20:04 +000014293 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
14294 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014295 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14296 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14297
Joe Perches41535772013-02-16 11:20:04 +000014298 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014299 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014300
Michael Chan9d26e212006-12-07 00:21:14 -080014301 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014302 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014303 if ((tp->pdev->subsystem_vendor ==
14304 PCI_VENDOR_ID_ARIMA) &&
14305 (tp->pdev->subsystem_device == 0x205a ||
14306 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014307 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014308 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014309 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14310 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014312
14313 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014314 tg3_flag_set(tp, ENABLE_ASF);
14315 if (tg3_flag(tp, 5750_PLUS))
14316 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014317 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014318
14319 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014320 tg3_flag(tp, 5750_PLUS))
14321 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014322
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014323 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014324 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014325 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326
Joe Perches63c3a662011-04-26 08:12:10 +000014327 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014328 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014329 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014330 device_set_wakeup_enable(&tp->pdev->dev, true);
14331 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014332
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014334 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014335
14336 /* serdes signal pre-emphasis in register 0x590 set by */
14337 /* bootcode if bit 18 is set */
14338 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014339 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014340
Joe Perches63c3a662011-04-26 08:12:10 +000014341 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000014342 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
14343 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014344 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014345 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014346
Joe Perches63c3a662011-04-26 08:12:10 +000014347 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000014348 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014349 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014350 u32 cfg3;
14351
14352 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
14353 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000014354 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070014355 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014356
Matt Carlson14417062010-02-17 15:16:59 +000014357 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014358 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014359 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014360 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014361 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014362 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014363 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014364done:
Joe Perches63c3a662011-04-26 08:12:10 +000014365 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014366 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014367 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014368 else
14369 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014370}
14371
Michael Chanc86a8562013-01-06 12:51:08 +000014372static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
14373{
14374 int i, err;
14375 u32 val2, off = offset * 8;
14376
14377 err = tg3_nvram_lock(tp);
14378 if (err)
14379 return err;
14380
14381 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
14382 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
14383 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
14384 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
14385 udelay(10);
14386
14387 for (i = 0; i < 100; i++) {
14388 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
14389 if (val2 & APE_OTP_STATUS_CMD_DONE) {
14390 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
14391 break;
14392 }
14393 udelay(10);
14394 }
14395
14396 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
14397
14398 tg3_nvram_unlock(tp);
14399 if (val2 & APE_OTP_STATUS_CMD_DONE)
14400 return 0;
14401
14402 return -EBUSY;
14403}
14404
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014405static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014406{
14407 int i;
14408 u32 val;
14409
14410 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14411 tw32(OTP_CTRL, cmd);
14412
14413 /* Wait for up to 1 ms for command to execute. */
14414 for (i = 0; i < 100; i++) {
14415 val = tr32(OTP_STATUS);
14416 if (val & OTP_STATUS_CMD_DONE)
14417 break;
14418 udelay(10);
14419 }
14420
14421 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
14422}
14423
14424/* Read the gphy configuration from the OTP region of the chip. The gphy
14425 * configuration is a 32-bit value that straddles the alignment boundary.
14426 * We do two 32-bit reads and then shift and merge the results.
14427 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014428static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014429{
14430 u32 bhalf_otp, thalf_otp;
14431
14432 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
14433
14434 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
14435 return 0;
14436
14437 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
14438
14439 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14440 return 0;
14441
14442 thalf_otp = tr32(OTP_READ_DATA);
14443
14444 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
14445
14446 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14447 return 0;
14448
14449 bhalf_otp = tr32(OTP_READ_DATA);
14450
14451 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
14452}
14453
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014454static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000014455{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000014456 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014457
14458 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14459 adv |= ADVERTISED_1000baseT_Half |
14460 ADVERTISED_1000baseT_Full;
14461
14462 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14463 adv |= ADVERTISED_100baseT_Half |
14464 ADVERTISED_100baseT_Full |
14465 ADVERTISED_10baseT_Half |
14466 ADVERTISED_10baseT_Full |
14467 ADVERTISED_TP;
14468 else
14469 adv |= ADVERTISED_FIBRE;
14470
14471 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000014472 tp->link_config.speed = SPEED_UNKNOWN;
14473 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014474 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000014475 tp->link_config.active_speed = SPEED_UNKNOWN;
14476 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000014477
14478 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014479}
14480
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014481static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070014482{
14483 u32 hw_phy_id_1, hw_phy_id_2;
14484 u32 hw_phy_id, hw_phy_id_masked;
14485 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014486
Matt Carlsone256f8a2011-03-09 16:58:24 +000014487 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000014488 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000014489 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
14490
Michael Chan8151ad52012-07-29 19:15:41 +000014491 if (tg3_flag(tp, ENABLE_APE)) {
14492 switch (tp->pci_fn) {
14493 case 0:
14494 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
14495 break;
14496 case 1:
14497 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
14498 break;
14499 case 2:
14500 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
14501 break;
14502 case 3:
14503 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
14504 break;
14505 }
14506 }
14507
Joe Perches63c3a662011-04-26 08:12:10 +000014508 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014509 return tg3_phy_init(tp);
14510
Linus Torvalds1da177e2005-04-16 15:20:36 -070014511 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010014512 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014513 */
14514 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000014515 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000014516 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014517 } else {
14518 /* Now read the physical PHY_ID from the chip and verify
14519 * that it is sane. If it doesn't look good, we fall back
14520 * to either the hard-coded table based PHY_ID and failing
14521 * that the value found in the eeprom area.
14522 */
14523 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
14524 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
14525
14526 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
14527 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
14528 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
14529
Matt Carlson79eb6902010-02-17 15:17:03 +000014530 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014531 }
14532
Matt Carlson79eb6902010-02-17 15:17:03 +000014533 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000014535 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014536 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070014537 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014538 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014539 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000014540 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070014541 /* Do nothing, phy ID already set up in
14542 * tg3_get_eeprom_hw_cfg().
14543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014544 } else {
14545 struct subsys_tbl_ent *p;
14546
14547 /* No eeprom signature? Try the hardcoded
14548 * subsys device table.
14549 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014550 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014551 if (p) {
14552 tp->phy_id = p->phy_id;
14553 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
14554 /* For now we saw the IDs 0xbc050cd0,
14555 * 0xbc050f80 and 0xbc050c30 on devices
14556 * connected to an BCM4785 and there are
14557 * probably more. Just assume that the phy is
14558 * supported when it is connected to a SSB core
14559 * for now.
14560 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014561 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014563
Linus Torvalds1da177e2005-04-16 15:20:36 -070014564 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000014565 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014566 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014567 }
14568 }
14569
Matt Carlsona6b68da2010-12-06 08:28:52 +000014570 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000014571 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
14572 tg3_asic_rev(tp) == ASIC_REV_5720 ||
14573 tg3_asic_rev(tp) == ASIC_REV_5762 ||
14574 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
14575 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
14576 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
14577 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000014578 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
14579
Matt Carlsone256f8a2011-03-09 16:58:24 +000014580 tg3_phy_init_link_config(tp);
14581
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014582 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014583 !tg3_flag(tp, ENABLE_APE) &&
14584 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014585 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014586
14587 tg3_readphy(tp, MII_BMSR, &bmsr);
14588 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
14589 (bmsr & BMSR_LSTATUS))
14590 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014591
Linus Torvalds1da177e2005-04-16 15:20:36 -070014592 err = tg3_phy_reset(tp);
14593 if (err)
14594 return err;
14595
Matt Carlson42b64a42011-05-19 12:12:49 +000014596 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014597
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014598 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000014599 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
14600 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014601
14602 tg3_writephy(tp, MII_BMCR,
14603 BMCR_ANENABLE | BMCR_ANRESTART);
14604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014605 }
14606
14607skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000014608 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014609 err = tg3_init_5401phy_dsp(tp);
14610 if (err)
14611 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014612
Linus Torvalds1da177e2005-04-16 15:20:36 -070014613 err = tg3_init_5401phy_dsp(tp);
14614 }
14615
Linus Torvalds1da177e2005-04-16 15:20:36 -070014616 return err;
14617}
14618
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014619static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014620{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014621 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014622 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000014623 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000014624 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014625
Matt Carlson535a4902011-07-20 10:20:56 +000014626 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014627 if (!vpd_data)
14628 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014629
Matt Carlson535a4902011-07-20 10:20:56 +000014630 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000014631 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014632 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014633
14634 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
14635 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
14636 i += PCI_VPD_LRDT_TAG_SIZE;
14637
Matt Carlson535a4902011-07-20 10:20:56 +000014638 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014639 goto out_not_found;
14640
Matt Carlson184b8902010-04-05 10:19:25 +000014641 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14642 PCI_VPD_RO_KEYWORD_MFR_ID);
14643 if (j > 0) {
14644 len = pci_vpd_info_field_size(&vpd_data[j]);
14645
14646 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14647 if (j + len > block_end || len != 4 ||
14648 memcmp(&vpd_data[j], "1028", 4))
14649 goto partno;
14650
14651 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14652 PCI_VPD_RO_KEYWORD_VENDOR0);
14653 if (j < 0)
14654 goto partno;
14655
14656 len = pci_vpd_info_field_size(&vpd_data[j]);
14657
14658 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14659 if (j + len > block_end)
14660 goto partno;
14661
14662 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000014663 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000014664 }
14665
14666partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000014667 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14668 PCI_VPD_RO_KEYWORD_PARTNO);
14669 if (i < 0)
14670 goto out_not_found;
14671
14672 len = pci_vpd_info_field_size(&vpd_data[i]);
14673
14674 i += PCI_VPD_INFO_FLD_HDR_SIZE;
14675 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000014676 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014677 goto out_not_found;
14678
14679 memcpy(tp->board_part_number, &vpd_data[i], len);
14680
Linus Torvalds1da177e2005-04-16 15:20:36 -070014681out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014682 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000014683 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014684 return;
14685
14686out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000014687 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000014688 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
14689 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000014690 strcpy(tp->board_part_number, "BCM5717");
14691 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
14692 strcpy(tp->board_part_number, "BCM5718");
14693 else
14694 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014695 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000014696 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
14697 strcpy(tp->board_part_number, "BCM57780");
14698 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
14699 strcpy(tp->board_part_number, "BCM57760");
14700 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
14701 strcpy(tp->board_part_number, "BCM57790");
14702 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
14703 strcpy(tp->board_part_number, "BCM57788");
14704 else
14705 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014706 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000014707 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
14708 strcpy(tp->board_part_number, "BCM57761");
14709 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
14710 strcpy(tp->board_part_number, "BCM57765");
14711 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
14712 strcpy(tp->board_part_number, "BCM57781");
14713 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
14714 strcpy(tp->board_part_number, "BCM57785");
14715 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
14716 strcpy(tp->board_part_number, "BCM57791");
14717 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
14718 strcpy(tp->board_part_number, "BCM57795");
14719 else
14720 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014721 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000014722 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
14723 strcpy(tp->board_part_number, "BCM57762");
14724 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
14725 strcpy(tp->board_part_number, "BCM57766");
14726 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
14727 strcpy(tp->board_part_number, "BCM57782");
14728 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14729 strcpy(tp->board_part_number, "BCM57786");
14730 else
14731 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014732 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070014733 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000014734 } else {
14735nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070014736 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000014737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014738}
14739
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014740static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070014741{
14742 u32 val;
14743
Matt Carlsone4f34112009-02-25 14:25:00 +000014744 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014745 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014746 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014747 val != 0)
14748 return 0;
14749
14750 return 1;
14751}
14752
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014753static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014754{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014755 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000014756 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014757 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014758
14759 if (tg3_nvram_read(tp, 0xc, &offset) ||
14760 tg3_nvram_read(tp, 0x4, &start))
14761 return;
14762
14763 offset = tg3_nvram_logical_addr(tp, offset);
14764
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014765 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014766 return;
14767
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014768 if ((val & 0xfc000000) == 0x0c000000) {
14769 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014770 return;
14771
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014772 if (val == 0)
14773 newver = true;
14774 }
14775
Matt Carlson75f99362010-04-05 10:19:24 +000014776 dst_off = strlen(tp->fw_ver);
14777
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014778 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000014779 if (TG3_VER_SIZE - dst_off < 16 ||
14780 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014781 return;
14782
14783 offset = offset + ver_offset - start;
14784 for (i = 0; i < 16; i += 4) {
14785 __be32 v;
14786 if (tg3_nvram_read_be32(tp, offset + i, &v))
14787 return;
14788
Matt Carlson75f99362010-04-05 10:19:24 +000014789 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014790 }
14791 } else {
14792 u32 major, minor;
14793
14794 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
14795 return;
14796
14797 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
14798 TG3_NVM_BCVER_MAJSFT;
14799 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000014800 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
14801 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014802 }
14803}
14804
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014805static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014806{
14807 u32 val, major, minor;
14808
14809 /* Use native endian representation */
14810 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
14811 return;
14812
14813 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
14814 TG3_NVM_HWSB_CFG1_MAJSFT;
14815 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
14816 TG3_NVM_HWSB_CFG1_MINSFT;
14817
14818 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
14819}
14820
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014821static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080014822{
14823 u32 offset, major, minor, build;
14824
Matt Carlson75f99362010-04-05 10:19:24 +000014825 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014826
14827 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
14828 return;
14829
14830 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
14831 case TG3_EEPROM_SB_REVISION_0:
14832 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
14833 break;
14834 case TG3_EEPROM_SB_REVISION_2:
14835 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
14836 break;
14837 case TG3_EEPROM_SB_REVISION_3:
14838 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
14839 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000014840 case TG3_EEPROM_SB_REVISION_4:
14841 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
14842 break;
14843 case TG3_EEPROM_SB_REVISION_5:
14844 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
14845 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000014846 case TG3_EEPROM_SB_REVISION_6:
14847 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
14848 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014849 default:
14850 return;
14851 }
14852
Matt Carlsone4f34112009-02-25 14:25:00 +000014853 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080014854 return;
14855
14856 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
14857 TG3_EEPROM_SB_EDH_BLD_SHFT;
14858 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
14859 TG3_EEPROM_SB_EDH_MAJ_SHFT;
14860 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
14861
14862 if (minor > 99 || build > 26)
14863 return;
14864
Matt Carlson75f99362010-04-05 10:19:24 +000014865 offset = strlen(tp->fw_ver);
14866 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
14867 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014868
14869 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000014870 offset = strlen(tp->fw_ver);
14871 if (offset < TG3_VER_SIZE - 1)
14872 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014873 }
14874}
14875
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014876static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080014877{
14878 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014879 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070014880
14881 for (offset = TG3_NVM_DIR_START;
14882 offset < TG3_NVM_DIR_END;
14883 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014884 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014885 return;
14886
14887 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
14888 break;
14889 }
14890
14891 if (offset == TG3_NVM_DIR_END)
14892 return;
14893
Joe Perches63c3a662011-04-26 08:12:10 +000014894 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014895 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000014896 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014897 return;
14898
Matt Carlsone4f34112009-02-25 14:25:00 +000014899 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014900 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014901 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014902 return;
14903
14904 offset += val - start;
14905
Matt Carlsonacd9c112009-02-25 14:26:33 +000014906 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014907
Matt Carlsonacd9c112009-02-25 14:26:33 +000014908 tp->fw_ver[vlen++] = ',';
14909 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070014910
14911 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000014912 __be32 v;
14913 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014914 return;
14915
Al Virob9fc7dc2007-12-17 22:59:57 -080014916 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014917
Matt Carlsonacd9c112009-02-25 14:26:33 +000014918 if (vlen > TG3_VER_SIZE - sizeof(v)) {
14919 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014920 break;
14921 }
14922
Matt Carlsonacd9c112009-02-25 14:26:33 +000014923 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
14924 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014925 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000014926}
14927
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014928static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000014929{
Matt Carlson7fd76442009-02-25 14:27:20 +000014930 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000014931
14932 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
14933 if (apedata != APE_SEG_SIG_MAGIC)
14934 return;
14935
14936 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
14937 if (!(apedata & APE_FW_STATUS_READY))
14938 return;
14939
Michael Chan165f4d12012-07-16 16:23:59 +000014940 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
14941 tg3_flag_set(tp, APE_HAS_NCSI);
14942}
14943
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014944static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000014945{
14946 int vlen;
14947 u32 apedata;
14948 char *fwtype;
14949
Matt Carlson7fd76442009-02-25 14:27:20 +000014950 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
14951
Michael Chan165f4d12012-07-16 16:23:59 +000014952 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000014953 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000014954 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
14955 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000014956 else
Matt Carlsonecc79642010-08-02 11:26:01 +000014957 fwtype = "DASH";
14958
Matt Carlson7fd76442009-02-25 14:27:20 +000014959 vlen = strlen(tp->fw_ver);
14960
Matt Carlsonecc79642010-08-02 11:26:01 +000014961 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
14962 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000014963 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
14964 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
14965 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
14966 (apedata & APE_FW_VERSION_BLDMSK));
14967}
14968
Michael Chanc86a8562013-01-06 12:51:08 +000014969static void tg3_read_otp_ver(struct tg3 *tp)
14970{
14971 u32 val, val2;
14972
Joe Perches41535772013-02-16 11:20:04 +000014973 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000014974 return;
14975
14976 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
14977 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
14978 TG3_OTP_MAGIC0_VALID(val)) {
14979 u64 val64 = (u64) val << 32 | val2;
14980 u32 ver = 0;
14981 int i, vlen;
14982
14983 for (i = 0; i < 7; i++) {
14984 if ((val64 & 0xff) == 0)
14985 break;
14986 ver = val64 & 0xff;
14987 val64 >>= 8;
14988 }
14989 vlen = strlen(tp->fw_ver);
14990 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
14991 }
14992}
14993
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014994static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014995{
14996 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000014997 bool vpd_vers = false;
14998
14999 if (tp->fw_ver[0] != 0)
15000 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015001
Joe Perches63c3a662011-04-26 08:12:10 +000015002 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015003 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015004 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015005 return;
15006 }
15007
Matt Carlsonacd9c112009-02-25 14:26:33 +000015008 if (tg3_nvram_read(tp, 0, &val))
15009 return;
15010
15011 if (val == TG3_EEPROM_MAGIC)
15012 tg3_read_bc_ver(tp);
15013 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15014 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015015 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15016 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015017
Michael Chan165f4d12012-07-16 16:23:59 +000015018 if (tg3_flag(tp, ENABLE_ASF)) {
15019 if (tg3_flag(tp, ENABLE_APE)) {
15020 tg3_probe_ncsi(tp);
15021 if (!vpd_vers)
15022 tg3_read_dash_ver(tp);
15023 } else if (!vpd_vers) {
15024 tg3_read_mgmtfw_ver(tp);
15025 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015026 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015027
15028 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015029}
15030
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015031static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15032{
Joe Perches63c3a662011-04-26 08:12:10 +000015033 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015034 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015035 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015036 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015037 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015038 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015039}
15040
Matt Carlson41434702011-03-09 16:58:22 +000015041static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015042 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15043 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15044 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15045 { },
15046};
15047
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015048static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015049{
15050 struct pci_dev *peer;
15051 unsigned int func, devnr = tp->pdev->devfn & ~7;
15052
15053 for (func = 0; func < 8; func++) {
15054 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15055 if (peer && peer != tp->pdev)
15056 break;
15057 pci_dev_put(peer);
15058 }
15059 /* 5704 can be configured in single-port mode, set peer to
15060 * tp->pdev in that case.
15061 */
15062 if (!peer) {
15063 peer = tp->pdev;
15064 return peer;
15065 }
15066
15067 /*
15068 * We don't need to keep the refcount elevated; there's no way
15069 * to remove one half of this device without removing the other
15070 */
15071 pci_dev_put(peer);
15072
15073 return peer;
15074}
15075
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015076static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015077{
15078 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015079 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015080 u32 reg;
15081
15082 /* All devices that use the alternate
15083 * ASIC REV location have a CPMU.
15084 */
15085 tg3_flag_set(tp, CPMU_PRESENT);
15086
15087 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015088 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015089 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15090 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015091 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
15092 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15093 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
15094 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727)
Matt Carlson42b123b2012-02-13 15:20:13 +000015095 reg = TG3PCI_GEN2_PRODID_ASICREV;
15096 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15097 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15098 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15099 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15100 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15101 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15102 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15103 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15104 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15105 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15106 reg = TG3PCI_GEN15_PRODID_ASICREV;
15107 else
15108 reg = TG3PCI_PRODID_ASICREV;
15109
15110 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15111 }
15112
15113 /* Wrong chip ID in 5752 A0. This code can be removed later
15114 * as A0 is not in production.
15115 */
Joe Perches41535772013-02-16 11:20:04 +000015116 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015117 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15118
Joe Perches41535772013-02-16 11:20:04 +000015119 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015120 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15121
Joe Perches41535772013-02-16 11:20:04 +000015122 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15123 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15124 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015125 tg3_flag_set(tp, 5717_PLUS);
15126
Joe Perches41535772013-02-16 11:20:04 +000015127 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15128 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015129 tg3_flag_set(tp, 57765_CLASS);
15130
Michael Chanc65a17f2013-01-06 12:51:07 +000015131 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015132 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015133 tg3_flag_set(tp, 57765_PLUS);
15134
15135 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015136 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15137 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15138 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15139 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15140 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15141 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015142 tg3_flag(tp, 57765_PLUS))
15143 tg3_flag_set(tp, 5755_PLUS);
15144
Joe Perches41535772013-02-16 11:20:04 +000015145 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15146 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015147 tg3_flag_set(tp, 5780_CLASS);
15148
Joe Perches41535772013-02-16 11:20:04 +000015149 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15150 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15151 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015152 tg3_flag(tp, 5755_PLUS) ||
15153 tg3_flag(tp, 5780_CLASS))
15154 tg3_flag_set(tp, 5750_PLUS);
15155
Joe Perches41535772013-02-16 11:20:04 +000015156 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015157 tg3_flag(tp, 5750_PLUS))
15158 tg3_flag_set(tp, 5705_PLUS);
15159}
15160
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015161static bool tg3_10_100_only_device(struct tg3 *tp,
15162 const struct pci_device_id *ent)
15163{
15164 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
15165
Joe Perches41535772013-02-16 11:20:04 +000015166 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
15167 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015168 (tp->phy_flags & TG3_PHYFLG_IS_FET))
15169 return true;
15170
15171 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000015172 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015173 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
15174 return true;
15175 } else {
15176 return true;
15177 }
15178 }
15179
15180 return false;
15181}
15182
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015183static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015184{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015185 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015186 u32 pci_state_reg, grc_misc_cfg;
15187 u32 val;
15188 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015189 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015190
Linus Torvalds1da177e2005-04-16 15:20:36 -070015191 /* Force memory write invalidate off. If we leave it on,
15192 * then on 5700_BX chips we have to enable a workaround.
15193 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15194 * to match the cacheline size. The Broadcom driver have this
15195 * workaround but turns MWI off all the times so never uses
15196 * it. This seems to suggest that the workaround is insufficient.
15197 */
15198 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15199 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15200 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15201
Matt Carlson16821282011-07-13 09:27:28 +000015202 /* Important! -- Make sure register accesses are byteswapped
15203 * correctly. Also, for those chips that require it, make
15204 * sure that indirect register accesses are enabled before
15205 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015206 */
15207 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15208 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015209 tp->misc_host_ctrl |= (misc_ctrl_reg &
15210 MISC_HOST_CTRL_CHIPREV);
15211 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15212 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015213
Matt Carlson42b123b2012-02-13 15:20:13 +000015214 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015215
Michael Chan68929142005-08-09 20:17:14 -070015216 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15217 * we need to disable memory and use config. cycles
15218 * only to access all registers. The 5702/03 chips
15219 * can mistakenly decode the special cycles from the
15220 * ICH chipsets as memory write cycles, causing corruption
15221 * of register and memory space. Only certain ICH bridges
15222 * will drive special cycles with non-zero data during the
15223 * address phase which can fall within the 5703's address
15224 * range. This is not an ICH bug as the PCI spec allows
15225 * non-zero address during special cycles. However, only
15226 * these ICH bridges are known to drive non-zero addresses
15227 * during special cycles.
15228 *
15229 * Since special cycles do not cross PCI bridges, we only
15230 * enable this workaround if the 5703 is on the secondary
15231 * bus of these ICH bridges.
15232 */
Joe Perches41535772013-02-16 11:20:04 +000015233 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
15234 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070015235 static struct tg3_dev_id {
15236 u32 vendor;
15237 u32 device;
15238 u32 rev;
15239 } ich_chipsets[] = {
15240 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15241 PCI_ANY_ID },
15242 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15243 PCI_ANY_ID },
15244 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15245 0xa },
15246 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15247 PCI_ANY_ID },
15248 { },
15249 };
15250 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15251 struct pci_dev *bridge = NULL;
15252
15253 while (pci_id->vendor != 0) {
15254 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15255 bridge);
15256 if (!bridge) {
15257 pci_id++;
15258 continue;
15259 }
15260 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015261 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015262 continue;
15263 }
15264 if (bridge->subordinate &&
15265 (bridge->subordinate->number ==
15266 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015267 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015268 pci_dev_put(bridge);
15269 break;
15270 }
15271 }
15272 }
15273
Joe Perches41535772013-02-16 11:20:04 +000015274 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070015275 static struct tg3_dev_id {
15276 u32 vendor;
15277 u32 device;
15278 } bridge_chipsets[] = {
15279 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15280 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15281 { },
15282 };
15283 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15284 struct pci_dev *bridge = NULL;
15285
15286 while (pci_id->vendor != 0) {
15287 bridge = pci_get_device(pci_id->vendor,
15288 pci_id->device,
15289 bridge);
15290 if (!bridge) {
15291 pci_id++;
15292 continue;
15293 }
15294 if (bridge->subordinate &&
15295 (bridge->subordinate->number <=
15296 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015297 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070015298 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015299 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070015300 pci_dev_put(bridge);
15301 break;
15302 }
15303 }
15304 }
15305
Michael Chan4a29cc22006-03-19 13:21:12 -080015306 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15307 * DMA addresses > 40-bit. This bridge may have other additional
15308 * 57xx devices behind it in some 4-port NIC designs for example.
15309 * Any tg3 device found behind the bridge will also need the 40-bit
15310 * DMA workaround.
15311 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015312 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015313 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015314 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015315 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015316 struct pci_dev *bridge = NULL;
15317
15318 do {
15319 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15320 PCI_DEVICE_ID_SERVERWORKS_EPB,
15321 bridge);
15322 if (bridge && bridge->subordinate &&
15323 (bridge->subordinate->number <=
15324 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015325 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015326 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015327 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015328 pci_dev_put(bridge);
15329 break;
15330 }
15331 } while (bridge);
15332 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015333
Joe Perches41535772013-02-16 11:20:04 +000015334 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
15335 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015336 tp->pdev_peer = tg3_find_peer(tp);
15337
Matt Carlson507399f2009-11-13 13:03:37 +000015338 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000015339 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015340 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015341 else if (tg3_flag(tp, 57765_PLUS))
15342 tg3_flag_set(tp, HW_TSO_3);
15343 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015344 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015345 tg3_flag_set(tp, HW_TSO_2);
15346 else if (tg3_flag(tp, 5750_PLUS)) {
15347 tg3_flag_set(tp, HW_TSO_1);
15348 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015349 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
15350 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015351 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015352 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15353 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15354 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000015355 tg3_flag_set(tp, FW_TSO);
15356 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015357 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000015358 tp->fw_needed = FIRMWARE_TG3TSO5;
15359 else
15360 tp->fw_needed = FIRMWARE_TG3TSO;
15361 }
15362
Matt Carlsondabc5c62011-05-19 12:12:52 +000015363 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015364 if (tg3_flag(tp, HW_TSO_1) ||
15365 tg3_flag(tp, HW_TSO_2) ||
15366 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000015367 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015368 /* For firmware TSO, assume ASF is disabled.
15369 * We'll disable TSO later if we discover ASF
15370 * is enabled in tg3_get_eeprom_hw_cfg().
15371 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015372 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015373 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015374 tg3_flag_clear(tp, TSO_CAPABLE);
15375 tg3_flag_clear(tp, TSO_BUG);
15376 tp->fw_needed = NULL;
15377 }
15378
Joe Perches41535772013-02-16 11:20:04 +000015379 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000015380 tp->fw_needed = FIRMWARE_TG3;
15381
Matt Carlson507399f2009-11-13 13:03:37 +000015382 tp->irq_max = 1;
15383
Joe Perches63c3a662011-04-26 08:12:10 +000015384 if (tg3_flag(tp, 5750_PLUS)) {
15385 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000015386 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
15387 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
15388 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
15389 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070015390 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015391 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015392
Joe Perches63c3a662011-04-26 08:12:10 +000015393 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015394 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015395 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015397
Joe Perches63c3a662011-04-26 08:12:10 +000015398 if (tg3_flag(tp, 57765_PLUS)) {
15399 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000015400 tp->irq_max = TG3_IRQ_MAX_VECS;
15401 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015402 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000015403
Michael Chan91024262012-09-28 07:12:38 +000015404 tp->txq_max = 1;
15405 tp->rxq_max = 1;
15406 if (tp->irq_max > 1) {
15407 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
15408 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
15409
Joe Perches41535772013-02-16 11:20:04 +000015410 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15411 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000015412 tp->txq_max = tp->irq_max - 1;
15413 }
15414
Matt Carlsonb7abee62012-06-07 12:56:54 +000015415 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015416 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015417 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015418
Joe Perches41535772013-02-16 11:20:04 +000015419 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000015420 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000015421
Joe Perches41535772013-02-16 11:20:04 +000015422 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15423 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15424 tg3_asic_rev(tp) == ASIC_REV_5720 ||
15425 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000015426 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000015427
Joe Perches63c3a662011-04-26 08:12:10 +000015428 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000015429 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000015430 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000015431
Joe Perches63c3a662011-04-26 08:12:10 +000015432 if (!tg3_flag(tp, 5705_PLUS) ||
15433 tg3_flag(tp, 5780_CLASS) ||
15434 tg3_flag(tp, USE_JUMBO_BDFLAG))
15435 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070015436
Matt Carlson52f44902008-11-21 17:17:04 -080015437 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15438 &pci_state_reg);
15439
Jon Mason708ebb3a2011-06-27 12:56:50 +000015440 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015441 u16 lnkctl;
15442
Joe Perches63c3a662011-04-26 08:12:10 +000015443 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080015444
Jiang Liu0f49bfb2012-08-20 13:28:20 -060015445 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015446 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000015447 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015448 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000015449 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000015450 }
Joe Perches41535772013-02-16 11:20:04 +000015451 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
15452 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15453 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
15454 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000015455 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015456 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015457 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080015458 }
Joe Perches41535772013-02-16 11:20:04 +000015459 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000015460 /* BCM5785 devices are effectively PCIe devices, and should
15461 * follow PCIe codepaths, but do not have a PCIe capabilities
15462 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000015463 */
Joe Perches63c3a662011-04-26 08:12:10 +000015464 tg3_flag_set(tp, PCI_EXPRESS);
15465 } else if (!tg3_flag(tp, 5705_PLUS) ||
15466 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080015467 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
15468 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000015469 dev_err(&tp->pdev->dev,
15470 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080015471 return -EIO;
15472 }
15473
15474 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000015475 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080015476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015477
Michael Chan399de502005-10-03 14:02:39 -070015478 /* If we have an AMD 762 or VIA K8T800 chipset, write
15479 * reordering to the mailbox registers done by the host
15480 * controller can cause major troubles. We read back from
15481 * every mailbox register write to force the writes to be
15482 * posted to the chip in order.
15483 */
Matt Carlson41434702011-03-09 16:58:22 +000015484 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015485 !tg3_flag(tp, PCI_EXPRESS))
15486 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070015487
Matt Carlson69fc4052008-12-21 20:19:57 -080015488 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
15489 &tp->pci_cacheline_sz);
15490 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15491 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000015492 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015493 tp->pci_lat_timer < 64) {
15494 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080015495 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15496 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015497 }
15498
Matt Carlson16821282011-07-13 09:27:28 +000015499 /* Important! -- It is critical that the PCI-X hw workaround
15500 * situation is decided before the first MMIO register access.
15501 */
Joe Perches41535772013-02-16 11:20:04 +000015502 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080015503 /* 5700 BX chips need to have their TX producer index
15504 * mailboxes written twice to workaround a bug.
15505 */
Joe Perches63c3a662011-04-26 08:12:10 +000015506 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070015507
Matt Carlson52f44902008-11-21 17:17:04 -080015508 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015509 *
15510 * The workaround is to use indirect register accesses
15511 * for all chip writes not to mailbox registers.
15512 */
Joe Perches63c3a662011-04-26 08:12:10 +000015513 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015514 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015515
Joe Perches63c3a662011-04-26 08:12:10 +000015516 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015517
15518 /* The chip can have it's power management PCI config
15519 * space registers clobbered due to this bug.
15520 * So explicitly force the chip into D0 here.
15521 */
Matt Carlson9974a352007-10-07 23:27:28 -070015522 pci_read_config_dword(tp->pdev,
15523 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015524 &pm_reg);
15525 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
15526 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070015527 pci_write_config_dword(tp->pdev,
15528 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015529 pm_reg);
15530
15531 /* Also, force SERR#/PERR# in PCI command. */
15532 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15533 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
15534 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15535 }
15536 }
15537
Linus Torvalds1da177e2005-04-16 15:20:36 -070015538 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015539 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015540 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015541 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015542
15543 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000015544 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015545 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
15546 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
15547 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
15548 }
15549
Michael Chan1ee582d2005-08-09 20:16:46 -070015550 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070015551 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015552 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070015553 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070015554 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015555 tp->write32_tx_mbox = tg3_write32;
15556 tp->write32_rx_mbox = tg3_write32;
15557
15558 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000015559 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070015560 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000015561 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015562 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000015563 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070015564 /*
15565 * Back to back register writes can cause problems on these
15566 * chips, the workaround is to read back all reg writes
15567 * except those to mailbox regs.
15568 *
15569 * See tg3_write_indirect_reg32().
15570 */
Michael Chan1ee582d2005-08-09 20:16:46 -070015571 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015572 }
15573
Joe Perches63c3a662011-04-26 08:12:10 +000015574 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070015575 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000015576 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070015577 tp->write32_rx_mbox = tg3_write_flush_reg32;
15578 }
Michael Chan20094932005-08-09 20:16:32 -070015579
Joe Perches63c3a662011-04-26 08:12:10 +000015580 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070015581 tp->read32 = tg3_read_indirect_reg32;
15582 tp->write32 = tg3_write_indirect_reg32;
15583 tp->read32_mbox = tg3_read_indirect_mbox;
15584 tp->write32_mbox = tg3_write_indirect_mbox;
15585 tp->write32_tx_mbox = tg3_write_indirect_mbox;
15586 tp->write32_rx_mbox = tg3_write_indirect_mbox;
15587
15588 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015589 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015590
15591 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15592 pci_cmd &= ~PCI_COMMAND_MEMORY;
15593 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15594 }
Joe Perches41535772013-02-16 11:20:04 +000015595 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015596 tp->read32_mbox = tg3_read32_mbox_5906;
15597 tp->write32_mbox = tg3_write32_mbox_5906;
15598 tp->write32_tx_mbox = tg3_write32_mbox_5906;
15599 tp->write32_rx_mbox = tg3_write32_mbox_5906;
15600 }
Michael Chan68929142005-08-09 20:17:14 -070015601
Michael Chanbbadf502006-04-06 21:46:34 -070015602 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015603 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000015604 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15605 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000015606 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070015607
Matt Carlson16821282011-07-13 09:27:28 +000015608 /* The memory arbiter has to be enabled in order for SRAM accesses
15609 * to succeed. Normally on powerup the tg3 chip firmware will make
15610 * sure it is enabled, but other entities such as system netboot
15611 * code might disable it.
15612 */
15613 val = tr32(MEMARB_MODE);
15614 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
15615
Matt Carlson9dc5e342011-11-04 09:15:02 +000015616 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000015617 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000015618 tg3_flag(tp, 5780_CLASS)) {
15619 if (tg3_flag(tp, PCIX_MODE)) {
15620 pci_read_config_dword(tp->pdev,
15621 tp->pcix_cap + PCI_X_STATUS,
15622 &val);
15623 tp->pci_fn = val & 0x7;
15624 }
Joe Perches41535772013-02-16 11:20:04 +000015625 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15626 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15627 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000015628 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000015629 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
15630 val = tr32(TG3_CPMU_STATUS);
15631
Joe Perches41535772013-02-16 11:20:04 +000015632 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000015633 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
15634 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000015635 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
15636 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000015637 }
15638
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015639 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
15640 tp->write32_tx_mbox = tg3_write_flush_reg32;
15641 tp->write32_rx_mbox = tg3_write_flush_reg32;
15642 }
15643
Michael Chan7d0c41e2005-04-21 17:06:20 -070015644 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000015645 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070015646 * determined before calling tg3_set_power_state() so that
15647 * we know whether or not to switch out of Vaux power.
15648 * When the flag is set, it means that GPIO1 is used for eeprom
15649 * write protect and also implies that it is a LOM where GPIOs
15650 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015651 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070015652 tg3_get_eeprom_hw_cfg(tp);
15653
Matt Carlson1caf13e2013-03-06 17:02:29 +000015654 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015655 tg3_flag_clear(tp, TSO_CAPABLE);
15656 tg3_flag_clear(tp, TSO_BUG);
15657 tp->fw_needed = NULL;
15658 }
15659
Joe Perches63c3a662011-04-26 08:12:10 +000015660 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070015661 /* Allow reads and writes to the
15662 * APE register and memory space.
15663 */
15664 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000015665 PCISTATE_ALLOW_APE_SHMEM_WR |
15666 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015667 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
15668 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000015669
15670 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015671 }
15672
Matt Carlson16821282011-07-13 09:27:28 +000015673 /* Set up tp->grc_local_ctrl before calling
15674 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
15675 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070015676 * It is also used as eeprom write protect on LOMs.
15677 */
15678 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000015679 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015680 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070015681 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
15682 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070015683 /* Unused GPIO3 must be driven as output on 5752 because there
15684 * are no pull-up resistors on unused GPIO pins.
15685 */
Joe Perches41535772013-02-16 11:20:04 +000015686 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070015687 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070015688
Joe Perches41535772013-02-16 11:20:04 +000015689 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15690 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015691 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080015692 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
15693
Matt Carlson8d519ab2009-04-20 06:58:01 +000015694 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15695 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015696 /* Turn off the debug UART. */
15697 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000015698 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015699 /* Keep VMain power. */
15700 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
15701 GRC_LCLCTRL_GPIO_OUTPUT0;
15702 }
15703
Joe Perches41535772013-02-16 11:20:04 +000015704 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015705 tp->grc_local_ctrl |=
15706 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
15707
Matt Carlson16821282011-07-13 09:27:28 +000015708 /* Switch out of Vaux if it is a NIC */
15709 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015710
Linus Torvalds1da177e2005-04-16 15:20:36 -070015711 /* Derive initial jumbo mode from MTU assigned in
15712 * ether_setup() via the alloc_etherdev() call
15713 */
Joe Perches63c3a662011-04-26 08:12:10 +000015714 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
15715 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015716
15717 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000015718 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15719 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
15720 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
15721 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000015722 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015723 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015724 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015725 }
15726
Joe Perches41535772013-02-16 11:20:04 +000015727 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015728 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015729
Linus Torvalds1da177e2005-04-16 15:20:36 -070015730 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000015731 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15732 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
15733 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
15734 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015735 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
15736 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15737 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015738
Joe Perches41535772013-02-16 11:20:04 +000015739 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
15740 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015741 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000015742 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015743 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015744
Joe Perches63c3a662011-04-26 08:12:10 +000015745 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015746 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000015747 tg3_asic_rev(tp) != ASIC_REV_5785 &&
15748 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015749 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000015750 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15751 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15752 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15753 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080015754 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
15755 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015756 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080015757 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015758 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080015759 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015760 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070015761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015762
Joe Perches41535772013-02-16 11:20:04 +000015763 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15764 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015765 tp->phy_otp = tg3_read_otp_phycfg(tp);
15766 if (tp->phy_otp == 0)
15767 tp->phy_otp = TG3_OTP_DEFAULT;
15768 }
15769
Joe Perches63c3a662011-04-26 08:12:10 +000015770 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070015771 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
15772 else
15773 tp->mi_mode = MAC_MI_MODE_BASE;
15774
Linus Torvalds1da177e2005-04-16 15:20:36 -070015775 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000015776 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
15777 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015778 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
15779
Matt Carlson4d958472011-04-20 07:57:35 +000015780 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000015781 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15782 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
15783 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000015784 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
15785 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
15786 }
15787
Joe Perches41535772013-02-16 11:20:04 +000015788 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
15789 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000015790 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070015791
Matt Carlson158d7ab2008-05-29 01:37:54 -070015792 err = tg3_mdio_init(tp);
15793 if (err)
15794 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015795
15796 /* Initialize data/descriptor byte/word swapping. */
15797 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000015798 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
15799 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000015800 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
15801 GRC_MODE_WORD_SWAP_B2HRX_DATA |
15802 GRC_MODE_B2HRX_ENABLE |
15803 GRC_MODE_HTX2B_ENABLE |
15804 GRC_MODE_HOST_STACKUP);
15805 else
15806 val &= GRC_MODE_HOST_STACKUP;
15807
Linus Torvalds1da177e2005-04-16 15:20:36 -070015808 tw32(GRC_MODE, val | tp->grc_mode);
15809
15810 tg3_switch_clocks(tp);
15811
15812 /* Clear this out for sanity. */
15813 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
15814
15815 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15816 &pci_state_reg);
15817 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015818 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000015819 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
15820 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
15821 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
15822 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015823 void __iomem *sram_base;
15824
15825 /* Write some dummy words into the SRAM status block
15826 * area, see if it reads back correctly. If the return
15827 * value is bad, force enable the PCIX workaround.
15828 */
15829 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
15830
15831 writel(0x00000000, sram_base);
15832 writel(0x00000000, sram_base + 4);
15833 writel(0xffffffff, sram_base + 4);
15834 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000015835 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015836 }
15837 }
15838
15839 udelay(50);
15840 tg3_nvram_init(tp);
15841
15842 grc_misc_cfg = tr32(GRC_MISC_CFG);
15843 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
15844
Joe Perches41535772013-02-16 11:20:04 +000015845 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015846 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
15847 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000015848 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015849
Joe Perches63c3a662011-04-26 08:12:10 +000015850 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000015851 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015852 tg3_flag_set(tp, TAGGED_STATUS);
15853 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070015854 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
15855 HOSTCC_MODE_CLRTICK_TXBD);
15856
15857 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
15858 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15859 tp->misc_host_ctrl);
15860 }
15861
Matt Carlson3bda1252008-08-15 14:08:22 -070015862 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000015863 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000015864 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070015865 else
Matt Carlson6e01b202011-08-19 13:58:20 +000015866 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070015867
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015868 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015869 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015870
15871 err = tg3_phy_probe(tp);
15872 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015873 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015874 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015875 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015876 }
15877
Matt Carlson184b8902010-04-05 10:19:25 +000015878 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080015879 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015880
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015881 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
15882 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015883 } else {
Joe Perches41535772013-02-16 11:20:04 +000015884 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015885 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015886 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015887 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015888 }
15889
15890 /* 5700 {AX,BX} chips have a broken status block link
15891 * change bit implementation, so we must use the
15892 * status register in those cases.
15893 */
Joe Perches41535772013-02-16 11:20:04 +000015894 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015895 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015896 else
Joe Perches63c3a662011-04-26 08:12:10 +000015897 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015898
15899 /* The led_ctrl is set during tg3_phy_probe, here we might
15900 * have to force the link status polling mechanism based
15901 * upon subsystem IDs.
15902 */
15903 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000015904 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015905 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
15906 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000015907 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015908 }
15909
15910 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015911 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000015912 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015913 else
Joe Perches63c3a662011-04-26 08:12:10 +000015914 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015915
Eric Dumazet9205fd92011-11-18 06:47:01 +000015916 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015917 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000015918 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015919 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000015920 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015921#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000015922 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015923#endif
15924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015925
Matt Carlson2c49a442010-09-30 10:34:35 +000015926 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
15927 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015928 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
15929
Matt Carlson2c49a442010-09-30 10:34:35 +000015930 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070015931
15932 /* Increment the rx prod index on the rx std ring by at most
15933 * 8 for these chips to workaround hw errata.
15934 */
Joe Perches41535772013-02-16 11:20:04 +000015935 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15936 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15937 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070015938 tp->rx_std_max_post = 8;
15939
Joe Perches63c3a662011-04-26 08:12:10 +000015940 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070015941 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
15942 PCIE_PWR_MGMT_L1_THRESH_MSK;
15943
Linus Torvalds1da177e2005-04-16 15:20:36 -070015944 return err;
15945}
15946
David S. Miller49b6e95f2007-03-29 01:38:42 -070015947#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015948static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015949{
15950 struct net_device *dev = tp->dev;
15951 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015952 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070015953 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015954 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015955
David S. Miller49b6e95f2007-03-29 01:38:42 -070015956 addr = of_get_property(dp, "local-mac-address", &len);
15957 if (addr && len == 6) {
15958 memcpy(dev->dev_addr, addr, 6);
David S. Miller49b6e95f2007-03-29 01:38:42 -070015959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015960 }
15961 return -ENODEV;
15962}
15963
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015964static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015965{
15966 struct net_device *dev = tp->dev;
15967
15968 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
15969 return 0;
15970}
15971#endif
15972
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015973static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015974{
15975 struct net_device *dev = tp->dev;
15976 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080015977 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015978 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015979
David S. Miller49b6e95f2007-03-29 01:38:42 -070015980#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015981 if (!tg3_get_macaddr_sparc(tp))
15982 return 0;
15983#endif
15984
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015985 if (tg3_flag(tp, IS_SSB_CORE)) {
15986 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
15987 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
15988 return 0;
15989 }
15990
Linus Torvalds1da177e2005-04-16 15:20:36 -070015991 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000015992 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015993 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015994 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
15995 mac_offset = 0xcc;
15996 if (tg3_nvram_lock(tp))
15997 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
15998 else
15999 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016000 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016001 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016002 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016003 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016004 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016005 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016006 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016007
16008 /* First try to get it from MAC address mailbox. */
16009 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16010 if ((hi >> 16) == 0x484b) {
16011 dev->dev_addr[0] = (hi >> 8) & 0xff;
16012 dev->dev_addr[1] = (hi >> 0) & 0xff;
16013
16014 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16015 dev->dev_addr[2] = (lo >> 24) & 0xff;
16016 dev->dev_addr[3] = (lo >> 16) & 0xff;
16017 dev->dev_addr[4] = (lo >> 8) & 0xff;
16018 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016019
Michael Chan008652b2006-03-27 23:14:53 -080016020 /* Some old bootcode may report a 0 MAC address in SRAM */
16021 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16022 }
16023 if (!addr_ok) {
16024 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016025 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016026 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016027 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016028 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16029 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016030 }
16031 /* Finally just fetch it out of the MAC control regs. */
16032 else {
16033 hi = tr32(MAC_ADDR_0_HIGH);
16034 lo = tr32(MAC_ADDR_0_LOW);
16035
16036 dev->dev_addr[5] = lo & 0xff;
16037 dev->dev_addr[4] = (lo >> 8) & 0xff;
16038 dev->dev_addr[3] = (lo >> 16) & 0xff;
16039 dev->dev_addr[2] = (lo >> 24) & 0xff;
16040 dev->dev_addr[1] = hi & 0xff;
16041 dev->dev_addr[0] = (hi >> 8) & 0xff;
16042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016043 }
16044
16045 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016046#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016047 if (!tg3_get_default_macaddr_sparc(tp))
16048 return 0;
16049#endif
16050 return -EINVAL;
16051 }
16052 return 0;
16053}
16054
David S. Miller59e6b432005-05-18 22:50:10 -070016055#define BOUNDARY_SINGLE_CACHELINE 1
16056#define BOUNDARY_MULTI_CACHELINE 2
16057
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016058static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016059{
16060 int cacheline_size;
16061 u8 byte;
16062 int goal;
16063
16064 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16065 if (byte == 0)
16066 cacheline_size = 1024;
16067 else
16068 cacheline_size = (int) byte * 4;
16069
16070 /* On 5703 and later chips, the boundary bits have no
16071 * effect.
16072 */
Joe Perches41535772013-02-16 11:20:04 +000016073 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16074 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016075 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016076 goto out;
16077
16078#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16079 goal = BOUNDARY_MULTI_CACHELINE;
16080#else
16081#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16082 goal = BOUNDARY_SINGLE_CACHELINE;
16083#else
16084 goal = 0;
16085#endif
16086#endif
16087
Joe Perches63c3a662011-04-26 08:12:10 +000016088 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016089 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16090 goto out;
16091 }
16092
David S. Miller59e6b432005-05-18 22:50:10 -070016093 if (!goal)
16094 goto out;
16095
16096 /* PCI controllers on most RISC systems tend to disconnect
16097 * when a device tries to burst across a cache-line boundary.
16098 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16099 *
16100 * Unfortunately, for PCI-E there are only limited
16101 * write-side controls for this, and thus for reads
16102 * we will still get the disconnects. We'll also waste
16103 * these PCI cycles for both read and write for chips
16104 * other than 5700 and 5701 which do not implement the
16105 * boundary bits.
16106 */
Joe Perches63c3a662011-04-26 08:12:10 +000016107 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016108 switch (cacheline_size) {
16109 case 16:
16110 case 32:
16111 case 64:
16112 case 128:
16113 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16114 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16115 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16116 } else {
16117 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16118 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16119 }
16120 break;
16121
16122 case 256:
16123 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16124 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16125 break;
16126
16127 default:
16128 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16129 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16130 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016131 }
Joe Perches63c3a662011-04-26 08:12:10 +000016132 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016133 switch (cacheline_size) {
16134 case 16:
16135 case 32:
16136 case 64:
16137 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16138 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16139 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
16140 break;
16141 }
16142 /* fallthrough */
16143 case 128:
16144 default:
16145 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16146 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
16147 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016148 }
David S. Miller59e6b432005-05-18 22:50:10 -070016149 } else {
16150 switch (cacheline_size) {
16151 case 16:
16152 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16153 val |= (DMA_RWCTRL_READ_BNDRY_16 |
16154 DMA_RWCTRL_WRITE_BNDRY_16);
16155 break;
16156 }
16157 /* fallthrough */
16158 case 32:
16159 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16160 val |= (DMA_RWCTRL_READ_BNDRY_32 |
16161 DMA_RWCTRL_WRITE_BNDRY_32);
16162 break;
16163 }
16164 /* fallthrough */
16165 case 64:
16166 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16167 val |= (DMA_RWCTRL_READ_BNDRY_64 |
16168 DMA_RWCTRL_WRITE_BNDRY_64);
16169 break;
16170 }
16171 /* fallthrough */
16172 case 128:
16173 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16174 val |= (DMA_RWCTRL_READ_BNDRY_128 |
16175 DMA_RWCTRL_WRITE_BNDRY_128);
16176 break;
16177 }
16178 /* fallthrough */
16179 case 256:
16180 val |= (DMA_RWCTRL_READ_BNDRY_256 |
16181 DMA_RWCTRL_WRITE_BNDRY_256);
16182 break;
16183 case 512:
16184 val |= (DMA_RWCTRL_READ_BNDRY_512 |
16185 DMA_RWCTRL_WRITE_BNDRY_512);
16186 break;
16187 case 1024:
16188 default:
16189 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16190 DMA_RWCTRL_WRITE_BNDRY_1024);
16191 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016192 }
David S. Miller59e6b432005-05-18 22:50:10 -070016193 }
16194
16195out:
16196 return val;
16197}
16198
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016199static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
16200 int size, int to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016201{
16202 struct tg3_internal_buffer_desc test_desc;
16203 u32 sram_dma_descs;
16204 int i, ret;
16205
16206 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16207
16208 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16209 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16210 tw32(RDMAC_STATUS, 0);
16211 tw32(WDMAC_STATUS, 0);
16212
16213 tw32(BUFMGR_MODE, 0);
16214 tw32(FTQ_RESET, 0);
16215
16216 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16217 test_desc.addr_lo = buf_dma & 0xffffffff;
16218 test_desc.nic_mbuf = 0x00002100;
16219 test_desc.len = size;
16220
16221 /*
16222 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16223 * the *second* time the tg3 driver was getting loaded after an
16224 * initial scan.
16225 *
16226 * Broadcom tells me:
16227 * ...the DMA engine is connected to the GRC block and a DMA
16228 * reset may affect the GRC block in some unpredictable way...
16229 * The behavior of resets to individual blocks has not been tested.
16230 *
16231 * Broadcom noted the GRC reset will also reset all sub-components.
16232 */
16233 if (to_device) {
16234 test_desc.cqid_sqid = (13 << 8) | 2;
16235
16236 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16237 udelay(40);
16238 } else {
16239 test_desc.cqid_sqid = (16 << 8) | 7;
16240
16241 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16242 udelay(40);
16243 }
16244 test_desc.flags = 0x00000005;
16245
16246 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16247 u32 val;
16248
16249 val = *(((u32 *)&test_desc) + i);
16250 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16251 sram_dma_descs + (i * sizeof(u32)));
16252 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16253 }
16254 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16255
Matt Carlson859a588792010-04-05 10:19:28 +000016256 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016257 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016258 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016259 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016260
16261 ret = -ENODEV;
16262 for (i = 0; i < 40; i++) {
16263 u32 val;
16264
16265 if (to_device)
16266 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16267 else
16268 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16269 if ((val & 0xffff) == sram_dma_descs) {
16270 ret = 0;
16271 break;
16272 }
16273
16274 udelay(100);
16275 }
16276
16277 return ret;
16278}
16279
David S. Millerded73402005-05-23 13:59:47 -070016280#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016281
Matt Carlson41434702011-03-09 16:58:22 +000016282static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016283 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16284 { },
16285};
16286
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016287static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016288{
16289 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016290 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016291 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016292
Matt Carlson4bae65c2010-11-24 08:31:52 +000016293 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16294 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016295 if (!buf) {
16296 ret = -ENOMEM;
16297 goto out_nofree;
16298 }
16299
16300 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
16301 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16302
David S. Miller59e6b432005-05-18 22:50:10 -070016303 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016304
Joe Perches63c3a662011-04-26 08:12:10 +000016305 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016306 goto out;
16307
Joe Perches63c3a662011-04-26 08:12:10 +000016308 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016309 /* DMA read watermark not used on PCIE */
16310 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000016311 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000016312 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
16313 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016314 tp->dma_rwctrl |= 0x003f0000;
16315 else
16316 tp->dma_rwctrl |= 0x003f000f;
16317 } else {
Joe Perches41535772013-02-16 11:20:04 +000016318 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16319 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016320 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016321 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016322
Michael Chan4a29cc22006-03-19 13:21:12 -080016323 /* If the 5704 is behind the EPB bridge, we can
16324 * do the less restrictive ONE_DMA workaround for
16325 * better performance.
16326 */
Joe Perches63c3a662011-04-26 08:12:10 +000016327 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000016328 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080016329 tp->dma_rwctrl |= 0x8000;
16330 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016331 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16332
Joe Perches41535772013-02-16 11:20:04 +000016333 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080016334 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016335 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016336 tp->dma_rwctrl |=
16337 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16338 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16339 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000016340 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070016341 /* 5780 always in PCIX mode */
16342 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000016343 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070016344 /* 5714 always in PCIX mode */
16345 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016346 } else {
16347 tp->dma_rwctrl |= 0x001b000f;
16348 }
16349 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016350 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
16351 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016352
Joe Perches41535772013-02-16 11:20:04 +000016353 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16354 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016355 tp->dma_rwctrl &= 0xfffffff0;
16356
Joe Perches41535772013-02-16 11:20:04 +000016357 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16358 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016359 /* Remove this if it causes problems for some boards. */
16360 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16361
16362 /* On 5700/5701 chips, we need to set this bit.
16363 * Otherwise the chip will issue cacheline transactions
16364 * to streamable DMA memory with not all the byte
16365 * enables turned on. This is an error on several
16366 * RISC PCI controllers, in particular sparc64.
16367 *
16368 * On 5703/5704 chips, this bit has been reassigned
16369 * a different meaning. In particular, it is used
16370 * on those chips to enable a PCI-X workaround.
16371 */
16372 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16373 }
16374
16375 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16376
16377#if 0
16378 /* Unneeded, already done by tg3_get_invariants. */
16379 tg3_switch_clocks(tp);
16380#endif
16381
Joe Perches41535772013-02-16 11:20:04 +000016382 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16383 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016384 goto out;
16385
David S. Miller59e6b432005-05-18 22:50:10 -070016386 /* It is best to perform DMA test with maximum write burst size
16387 * to expose the 5700/5701 write DMA bug.
16388 */
16389 saved_dma_rwctrl = tp->dma_rwctrl;
16390 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16391 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16392
Linus Torvalds1da177e2005-04-16 15:20:36 -070016393 while (1) {
16394 u32 *p = buf, i;
16395
16396 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
16397 p[i] = i;
16398
16399 /* Send the buffer to the chip. */
16400 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
16401 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000016402 dev_err(&tp->pdev->dev,
16403 "%s: Buffer write failed. err = %d\n",
16404 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016405 break;
16406 }
16407
16408#if 0
16409 /* validate data reached card RAM correctly. */
16410 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16411 u32 val;
16412 tg3_read_mem(tp, 0x2100 + (i*4), &val);
16413 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000016414 dev_err(&tp->pdev->dev,
16415 "%s: Buffer corrupted on device! "
16416 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016417 /* ret = -ENODEV here? */
16418 }
16419 p[i] = 0;
16420 }
16421#endif
16422 /* Now read it back. */
16423 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
16424 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000016425 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
16426 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016427 break;
16428 }
16429
16430 /* Verify it. */
16431 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16432 if (p[i] == i)
16433 continue;
16434
David S. Miller59e6b432005-05-18 22:50:10 -070016435 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16436 DMA_RWCTRL_WRITE_BNDRY_16) {
16437 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016438 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
16439 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16440 break;
16441 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000016442 dev_err(&tp->pdev->dev,
16443 "%s: Buffer corrupted on read back! "
16444 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016445 ret = -ENODEV;
16446 goto out;
16447 }
16448 }
16449
16450 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
16451 /* Success. */
16452 ret = 0;
16453 break;
16454 }
16455 }
David S. Miller59e6b432005-05-18 22:50:10 -070016456 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16457 DMA_RWCTRL_WRITE_BNDRY_16) {
16458 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070016459 * now look for chipsets that are known to expose the
16460 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070016461 */
Matt Carlson41434702011-03-09 16:58:22 +000016462 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016463 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16464 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000016465 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016466 /* Safe to use the calculated DMA boundary. */
16467 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000016468 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070016469
David S. Miller59e6b432005-05-18 22:50:10 -070016470 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016472
16473out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000016474 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016475out_nofree:
16476 return ret;
16477}
16478
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016479static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016480{
Joe Perches63c3a662011-04-26 08:12:10 +000016481 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000016482 tp->bufmgr_config.mbuf_read_dma_low_water =
16483 DEFAULT_MB_RDMA_LOW_WATER_5705;
16484 tp->bufmgr_config.mbuf_mac_rx_low_water =
16485 DEFAULT_MB_MACRX_LOW_WATER_57765;
16486 tp->bufmgr_config.mbuf_high_water =
16487 DEFAULT_MB_HIGH_WATER_57765;
16488
16489 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16490 DEFAULT_MB_RDMA_LOW_WATER_5705;
16491 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16492 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
16493 tp->bufmgr_config.mbuf_high_water_jumbo =
16494 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000016495 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070016496 tp->bufmgr_config.mbuf_read_dma_low_water =
16497 DEFAULT_MB_RDMA_LOW_WATER_5705;
16498 tp->bufmgr_config.mbuf_mac_rx_low_water =
16499 DEFAULT_MB_MACRX_LOW_WATER_5705;
16500 tp->bufmgr_config.mbuf_high_water =
16501 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000016502 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016503 tp->bufmgr_config.mbuf_mac_rx_low_water =
16504 DEFAULT_MB_MACRX_LOW_WATER_5906;
16505 tp->bufmgr_config.mbuf_high_water =
16506 DEFAULT_MB_HIGH_WATER_5906;
16507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016508
Michael Chanfdfec1722005-07-25 12:31:48 -070016509 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16510 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
16511 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16512 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
16513 tp->bufmgr_config.mbuf_high_water_jumbo =
16514 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
16515 } else {
16516 tp->bufmgr_config.mbuf_read_dma_low_water =
16517 DEFAULT_MB_RDMA_LOW_WATER;
16518 tp->bufmgr_config.mbuf_mac_rx_low_water =
16519 DEFAULT_MB_MACRX_LOW_WATER;
16520 tp->bufmgr_config.mbuf_high_water =
16521 DEFAULT_MB_HIGH_WATER;
16522
16523 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16524 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
16525 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16526 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
16527 tp->bufmgr_config.mbuf_high_water_jumbo =
16528 DEFAULT_MB_HIGH_WATER_JUMBO;
16529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016530
16531 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
16532 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
16533}
16534
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016535static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016536{
Matt Carlson79eb6902010-02-17 15:17:03 +000016537 switch (tp->phy_id & TG3_PHY_ID_MASK) {
16538 case TG3_PHY_ID_BCM5400: return "5400";
16539 case TG3_PHY_ID_BCM5401: return "5401";
16540 case TG3_PHY_ID_BCM5411: return "5411";
16541 case TG3_PHY_ID_BCM5701: return "5701";
16542 case TG3_PHY_ID_BCM5703: return "5703";
16543 case TG3_PHY_ID_BCM5704: return "5704";
16544 case TG3_PHY_ID_BCM5705: return "5705";
16545 case TG3_PHY_ID_BCM5750: return "5750";
16546 case TG3_PHY_ID_BCM5752: return "5752";
16547 case TG3_PHY_ID_BCM5714: return "5714";
16548 case TG3_PHY_ID_BCM5780: return "5780";
16549 case TG3_PHY_ID_BCM5755: return "5755";
16550 case TG3_PHY_ID_BCM5787: return "5787";
16551 case TG3_PHY_ID_BCM5784: return "5784";
16552 case TG3_PHY_ID_BCM5756: return "5722/5756";
16553 case TG3_PHY_ID_BCM5906: return "5906";
16554 case TG3_PHY_ID_BCM5761: return "5761";
16555 case TG3_PHY_ID_BCM5718C: return "5718C";
16556 case TG3_PHY_ID_BCM5718S: return "5718S";
16557 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000016558 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000016559 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000016560 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000016561 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070016562 case 0: return "serdes";
16563 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070016564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016565}
16566
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016567static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070016568{
Joe Perches63c3a662011-04-26 08:12:10 +000016569 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016570 strcpy(str, "PCI Express");
16571 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000016572 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016573 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
16574
16575 strcpy(str, "PCIX:");
16576
16577 if ((clock_ctrl == 7) ||
16578 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
16579 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
16580 strcat(str, "133MHz");
16581 else if (clock_ctrl == 0)
16582 strcat(str, "33MHz");
16583 else if (clock_ctrl == 2)
16584 strcat(str, "50MHz");
16585 else if (clock_ctrl == 4)
16586 strcat(str, "66MHz");
16587 else if (clock_ctrl == 6)
16588 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070016589 } else {
16590 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000016591 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070016592 strcat(str, "66MHz");
16593 else
16594 strcat(str, "33MHz");
16595 }
Joe Perches63c3a662011-04-26 08:12:10 +000016596 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070016597 strcat(str, ":32-bit");
16598 else
16599 strcat(str, ":64-bit");
16600 return str;
16601}
16602
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016603static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070016604{
16605 struct ethtool_coalesce *ec = &tp->coal;
16606
16607 memset(ec, 0, sizeof(*ec));
16608 ec->cmd = ETHTOOL_GCOALESCE;
16609 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
16610 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
16611 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
16612 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
16613 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
16614 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
16615 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
16616 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
16617 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
16618
16619 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
16620 HOSTCC_MODE_CLRTICK_TXBD)) {
16621 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
16622 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
16623 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
16624 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
16625 }
Michael Chand244c892005-07-05 14:42:33 -070016626
Joe Perches63c3a662011-04-26 08:12:10 +000016627 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070016628 ec->rx_coalesce_usecs_irq = 0;
16629 ec->tx_coalesce_usecs_irq = 0;
16630 ec->stats_block_coalesce_usecs = 0;
16631 }
David S. Miller15f98502005-05-18 22:49:26 -070016632}
16633
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016634static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016635 const struct pci_device_id *ent)
16636{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016637 struct net_device *dev;
16638 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000016639 int i, err, pm_cap;
16640 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070016641 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080016642 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000016643 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016644
Joe Perches05dbe002010-02-17 19:44:19 +000016645 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016646
16647 err = pci_enable_device(pdev);
16648 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016649 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016650 return err;
16651 }
16652
Linus Torvalds1da177e2005-04-16 15:20:36 -070016653 err = pci_request_regions(pdev, DRV_MODULE_NAME);
16654 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016655 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016656 goto err_out_disable_pdev;
16657 }
16658
16659 pci_set_master(pdev);
16660
16661 /* Find power-management capability. */
16662 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
16663 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000016664 dev_err(&pdev->dev,
16665 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016666 err = -EIO;
16667 goto err_out_free_res;
16668 }
16669
Matt Carlson16821282011-07-13 09:27:28 +000016670 err = pci_set_power_state(pdev, PCI_D0);
16671 if (err) {
16672 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
16673 goto err_out_free_res;
16674 }
16675
Matt Carlsonfe5f5782009-09-01 13:09:39 +000016676 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016677 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016678 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000016679 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016680 }
16681
Linus Torvalds1da177e2005-04-16 15:20:36 -070016682 SET_NETDEV_DEV(dev, &pdev->dev);
16683
Linus Torvalds1da177e2005-04-16 15:20:36 -070016684 tp = netdev_priv(dev);
16685 tp->pdev = pdev;
16686 tp->dev = dev;
16687 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016688 tp->rx_mode = TG3_DEF_RX_MODE;
16689 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000016690 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070016691
Linus Torvalds1da177e2005-04-16 15:20:36 -070016692 if (tg3_debug > 0)
16693 tp->msg_enable = tg3_debug;
16694 else
16695 tp->msg_enable = TG3_DEF_MSG_ENABLE;
16696
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016697 if (pdev_is_ssb_gige_core(pdev)) {
16698 tg3_flag_set(tp, IS_SSB_CORE);
16699 if (ssb_gige_must_flush_posted_writes(pdev))
16700 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
16701 if (ssb_gige_one_dma_at_once(pdev))
16702 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
16703 if (ssb_gige_have_roboswitch(pdev))
16704 tg3_flag_set(tp, ROBOSWITCH);
16705 if (ssb_gige_is_rgmii(pdev))
16706 tg3_flag_set(tp, RGMII_MODE);
16707 }
16708
Linus Torvalds1da177e2005-04-16 15:20:36 -070016709 /* The word/byte swap controls here control register access byte
16710 * swapping. DMA data byte swapping is controlled in the GRC_MODE
16711 * setting below.
16712 */
16713 tp->misc_host_ctrl =
16714 MISC_HOST_CTRL_MASK_PCI_INT |
16715 MISC_HOST_CTRL_WORD_SWAP |
16716 MISC_HOST_CTRL_INDIR_ACCESS |
16717 MISC_HOST_CTRL_PCISTATE_RW;
16718
16719 /* The NONFRM (non-frame) byte/word swap controls take effect
16720 * on descriptor entries, anything which isn't packet data.
16721 *
16722 * The StrongARM chips on the board (one for tx, one for rx)
16723 * are running in big-endian mode.
16724 */
16725 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
16726 GRC_MODE_WSWAP_NONFRM_DATA);
16727#ifdef __BIG_ENDIAN
16728 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
16729#endif
16730 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016731 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000016732 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016733
Matt Carlsond5fe4882008-11-21 17:20:32 -080016734 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010016735 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016736 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016737 err = -ENOMEM;
16738 goto err_out_free_dev;
16739 }
16740
Matt Carlsonc9cab242011-07-13 09:27:27 +000016741 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16742 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
16743 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
16744 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
16745 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016746 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000016747 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16748 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016749 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
16750 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
16751 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
16752 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000016753 tg3_flag_set(tp, ENABLE_APE);
16754 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
16755 if (!tp->aperegs) {
16756 dev_err(&pdev->dev,
16757 "Cannot map APE registers, aborting\n");
16758 err = -ENOMEM;
16759 goto err_out_iounmap;
16760 }
16761 }
16762
Linus Torvalds1da177e2005-04-16 15:20:36 -070016763 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
16764 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016765
Linus Torvalds1da177e2005-04-16 15:20:36 -070016766 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016767 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000016768 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016769 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016770
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016771 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016772 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016773 dev_err(&pdev->dev,
16774 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016775 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016776 }
16777
Michael Chan4a29cc22006-03-19 13:21:12 -080016778 /* The EPB bridge inside 5714, 5715, and 5780 and any
16779 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080016780 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
16781 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
16782 * do DMA address check in tg3_start_xmit().
16783 */
Joe Perches63c3a662011-04-26 08:12:10 +000016784 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070016785 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000016786 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070016787 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080016788#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070016789 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016790#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080016791 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070016792 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016793
16794 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070016795 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080016796 err = pci_set_dma_mask(pdev, dma_mask);
16797 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000016798 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080016799 err = pci_set_consistent_dma_mask(pdev,
16800 persist_dma_mask);
16801 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016802 dev_err(&pdev->dev, "Unable to obtain 64 bit "
16803 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016804 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016805 }
16806 }
16807 }
Yang Hongyang284901a2009-04-06 19:01:15 -070016808 if (err || dma_mask == DMA_BIT_MASK(32)) {
16809 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080016810 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016811 dev_err(&pdev->dev,
16812 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016813 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016814 }
16815 }
16816
Michael Chanfdfec1722005-07-25 12:31:48 -070016817 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016818
Matt Carlson0da06062011-05-19 12:12:53 +000016819 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
16820
16821 /* 5700 B0 chips do not support checksumming correctly due
16822 * to hardware bugs.
16823 */
Joe Perches41535772013-02-16 11:20:04 +000016824 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000016825 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
16826
16827 if (tg3_flag(tp, 5755_PLUS))
16828 features |= NETIF_F_IPV6_CSUM;
16829 }
16830
Michael Chan4e3a7aa2006-03-20 17:47:44 -080016831 /* TSO is on by default on chips that support hardware TSO.
16832 * Firmware TSO on older chips gives lower performance, so it
16833 * is off by default, but can be enabled using ethtool.
16834 */
Joe Perches63c3a662011-04-26 08:12:10 +000016835 if ((tg3_flag(tp, HW_TSO_1) ||
16836 tg3_flag(tp, HW_TSO_2) ||
16837 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000016838 (features & NETIF_F_IP_CSUM))
16839 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000016840 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000016841 if (features & NETIF_F_IPV6_CSUM)
16842 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000016843 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000016844 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16845 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16846 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
16847 tg3_asic_rev(tp) == ASIC_REV_5785 ||
16848 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000016849 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070016850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016851
Matt Carlsond542fe22011-05-19 16:02:43 +000016852 dev->features |= features;
16853 dev->vlan_features |= features;
16854
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016855 /*
16856 * Add loopback capability only for a subset of devices that support
16857 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
16858 * loopback for the remaining devices.
16859 */
Joe Perches41535772013-02-16 11:20:04 +000016860 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016861 !tg3_flag(tp, CPMU_PRESENT))
16862 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000016863 features |= NETIF_F_LOOPBACK;
16864
Matt Carlson0da06062011-05-19 12:12:53 +000016865 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016866
Joe Perches41535772013-02-16 11:20:04 +000016867 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016868 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016869 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016870 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016871 tp->rx_pending = 63;
16872 }
16873
Linus Torvalds1da177e2005-04-16 15:20:36 -070016874 err = tg3_get_device_address(tp);
16875 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016876 dev_err(&pdev->dev,
16877 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016878 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016879 }
16880
Matt Carlsonc88864d2007-11-12 21:07:01 -080016881 /*
16882 * Reset chip in case UNDI or EFI driver did not shutdown
16883 * DMA self test will enable WDMAC and we'll see (spurious)
16884 * pending DMA on the PCI bus at that point.
16885 */
16886 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
16887 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
16888 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
16889 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
16890 }
16891
16892 err = tg3_test_dma(tp);
16893 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016894 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080016895 goto err_out_apeunmap;
16896 }
16897
Matt Carlson78f90dc2009-11-13 13:03:42 +000016898 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
16899 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
16900 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000016901 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000016902 struct tg3_napi *tnapi = &tp->napi[i];
16903
16904 tnapi->tp = tp;
16905 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
16906
16907 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000016908 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016909 intmbx += 0x8;
16910 else
16911 intmbx += 0x4;
16912
16913 tnapi->consmbox = rcvmbx;
16914 tnapi->prodmbox = sndmbx;
16915
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016916 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016917 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016918 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000016919 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000016920
Joe Perches63c3a662011-04-26 08:12:10 +000016921 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000016922 break;
16923
16924 /*
16925 * If we support MSIX, we'll be using RSS. If we're using
16926 * RSS, the first vector only handles link interrupts and the
16927 * remaining vectors handle rx and tx interrupts. Reuse the
16928 * mailbox values for the next iteration. The values we setup
16929 * above are still useful for the single vectored mode.
16930 */
16931 if (!i)
16932 continue;
16933
16934 rcvmbx += 0x8;
16935
16936 if (sndmbx & 0x4)
16937 sndmbx -= 0x4;
16938 else
16939 sndmbx += 0xc;
16940 }
16941
Matt Carlsonc88864d2007-11-12 21:07:01 -080016942 tg3_init_coal(tp);
16943
Michael Chanc49a1562006-12-17 17:07:29 -080016944 pci_set_drvdata(pdev, dev);
16945
Joe Perches41535772013-02-16 11:20:04 +000016946 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16947 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16948 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000016949 tg3_flag_set(tp, PTP_CAPABLE);
16950
Matt Carlsoncd0d7222011-07-13 09:27:33 +000016951 if (tg3_flag(tp, 5717_PLUS)) {
16952 /* Resume a low-power mode */
16953 tg3_frob_aux_power(tp, false);
16954 }
16955
Matt Carlson21f76382012-02-22 12:35:21 +000016956 tg3_timer_init(tp);
16957
Michael Chan402e1392013-02-14 12:13:41 +000016958 tg3_carrier_off(tp);
16959
Linus Torvalds1da177e2005-04-16 15:20:36 -070016960 err = register_netdev(dev);
16961 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016962 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070016963 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016964 }
16965
Joe Perches05dbe002010-02-17 19:44:19 +000016966 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
16967 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000016968 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000016969 tg3_bus_string(tp, str),
16970 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016971
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016972 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000016973 struct phy_device *phydev;
16974 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000016975 netdev_info(dev,
16976 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000016977 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016978 } else {
16979 char *ethtype;
16980
16981 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
16982 ethtype = "10/100Base-TX";
16983 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
16984 ethtype = "1000Base-SX";
16985 else
16986 ethtype = "10/100/1000Base-T";
16987
Matt Carlson5129c3a2010-04-05 10:19:23 +000016988 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000016989 "(WireSpeed[%d], EEE[%d])\n",
16990 tg3_phy_string(tp), ethtype,
16991 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
16992 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016993 }
Matt Carlsondf59c942008-11-03 16:52:56 -080016994
Joe Perches05dbe002010-02-17 19:44:19 +000016995 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000016996 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016997 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016998 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016999 tg3_flag(tp, ENABLE_ASF) != 0,
17000 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017001 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17002 tp->dma_rwctrl,
17003 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17004 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017005
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017006 pci_save_state(pdev);
17007
Linus Torvalds1da177e2005-04-16 15:20:36 -070017008 return 0;
17009
Matt Carlson0d3031d2007-10-10 18:02:43 -070017010err_out_apeunmap:
17011 if (tp->aperegs) {
17012 iounmap(tp->aperegs);
17013 tp->aperegs = NULL;
17014 }
17015
Linus Torvalds1da177e2005-04-16 15:20:36 -070017016err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017017 if (tp->regs) {
17018 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017019 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017021
17022err_out_free_dev:
17023 free_netdev(dev);
17024
Matt Carlson16821282011-07-13 09:27:28 +000017025err_out_power_down:
17026 pci_set_power_state(pdev, PCI_D3hot);
17027
Linus Torvalds1da177e2005-04-16 15:20:36 -070017028err_out_free_res:
17029 pci_release_regions(pdev);
17030
17031err_out_disable_pdev:
17032 pci_disable_device(pdev);
17033 pci_set_drvdata(pdev, NULL);
17034 return err;
17035}
17036
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017037static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017038{
17039 struct net_device *dev = pci_get_drvdata(pdev);
17040
17041 if (dev) {
17042 struct tg3 *tp = netdev_priv(dev);
17043
Jesper Juhle3c55302012-04-09 22:50:15 +020017044 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017045
Matt Carlsondb219972011-11-04 09:15:03 +000017046 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017047
David S. Miller1805b2f2011-10-24 18:18:09 -040017048 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017049 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017050 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017051 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017052
Linus Torvalds1da177e2005-04-16 15:20:36 -070017053 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017054 if (tp->aperegs) {
17055 iounmap(tp->aperegs);
17056 tp->aperegs = NULL;
17057 }
Michael Chan68929142005-08-09 20:17:14 -070017058 if (tp->regs) {
17059 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017060 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017062 free_netdev(dev);
17063 pci_release_regions(pdev);
17064 pci_disable_device(pdev);
17065 pci_set_drvdata(pdev, NULL);
17066 }
17067}
17068
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017069#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017070static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017071{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017072 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017073 struct net_device *dev = pci_get_drvdata(pdev);
17074 struct tg3 *tp = netdev_priv(dev);
17075 int err;
17076
17077 if (!netif_running(dev))
17078 return 0;
17079
Matt Carlsondb219972011-11-04 09:15:03 +000017080 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017081 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017082 tg3_netif_stop(tp);
17083
Matt Carlson21f76382012-02-22 12:35:21 +000017084 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017085
David S. Millerf47c11e2005-06-24 20:18:35 -070017086 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017087 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017088 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017089
17090 netif_device_detach(dev);
17091
David S. Millerf47c11e2005-06-24 20:18:35 -070017092 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017093 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017094 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017095 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017096
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017097 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017098 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017099 int err2;
17100
David S. Millerf47c11e2005-06-24 20:18:35 -070017101 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017102
Joe Perches63c3a662011-04-26 08:12:10 +000017103 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017104 err2 = tg3_restart_hw(tp, 1);
17105 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017107
Matt Carlson21f76382012-02-22 12:35:21 +000017108 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017109
17110 netif_device_attach(dev);
17111 tg3_netif_start(tp);
17112
Michael Chanb9ec6c12006-07-25 16:37:27 -070017113out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017114 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017115
17116 if (!err2)
17117 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017118 }
17119
17120 return err;
17121}
17122
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017123static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017124{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017125 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017126 struct net_device *dev = pci_get_drvdata(pdev);
17127 struct tg3 *tp = netdev_priv(dev);
17128 int err;
17129
17130 if (!netif_running(dev))
17131 return 0;
17132
Linus Torvalds1da177e2005-04-16 15:20:36 -070017133 netif_device_attach(dev);
17134
David S. Millerf47c11e2005-06-24 20:18:35 -070017135 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017136
Joe Perches63c3a662011-04-26 08:12:10 +000017137 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070017138 err = tg3_restart_hw(tp, 1);
17139 if (err)
17140 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017141
Matt Carlson21f76382012-02-22 12:35:21 +000017142 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017143
Linus Torvalds1da177e2005-04-16 15:20:36 -070017144 tg3_netif_start(tp);
17145
Michael Chanb9ec6c12006-07-25 16:37:27 -070017146out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017147 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017148
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017149 if (!err)
17150 tg3_phy_start(tp);
17151
Michael Chanb9ec6c12006-07-25 16:37:27 -070017152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017153}
17154
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017155static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017156#define TG3_PM_OPS (&tg3_pm_ops)
17157
17158#else
17159
17160#define TG3_PM_OPS NULL
17161
17162#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017163
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017164/**
17165 * tg3_io_error_detected - called when PCI error is detected
17166 * @pdev: Pointer to PCI device
17167 * @state: The current pci connection state
17168 *
17169 * This function is called after a PCI bus error affecting
17170 * this device has been detected.
17171 */
17172static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
17173 pci_channel_state_t state)
17174{
17175 struct net_device *netdev = pci_get_drvdata(pdev);
17176 struct tg3 *tp = netdev_priv(netdev);
17177 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
17178
17179 netdev_info(netdev, "PCI I/O error detected\n");
17180
17181 rtnl_lock();
17182
17183 if (!netif_running(netdev))
17184 goto done;
17185
17186 tg3_phy_stop(tp);
17187
17188 tg3_netif_stop(tp);
17189
Matt Carlson21f76382012-02-22 12:35:21 +000017190 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017191
17192 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000017193 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017194
17195 netif_device_detach(netdev);
17196
17197 /* Clean up software state, even if MMIO is blocked */
17198 tg3_full_lock(tp, 0);
17199 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
17200 tg3_full_unlock(tp);
17201
17202done:
17203 if (state == pci_channel_io_perm_failure)
17204 err = PCI_ERS_RESULT_DISCONNECT;
17205 else
17206 pci_disable_device(pdev);
17207
17208 rtnl_unlock();
17209
17210 return err;
17211}
17212
17213/**
17214 * tg3_io_slot_reset - called after the pci bus has been reset.
17215 * @pdev: Pointer to PCI device
17216 *
17217 * Restart the card from scratch, as if from a cold-boot.
17218 * At this point, the card has exprienced a hard reset,
17219 * followed by fixups by BIOS, and has its config space
17220 * set up identically to what it was at cold boot.
17221 */
17222static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17223{
17224 struct net_device *netdev = pci_get_drvdata(pdev);
17225 struct tg3 *tp = netdev_priv(netdev);
17226 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17227 int err;
17228
17229 rtnl_lock();
17230
17231 if (pci_enable_device(pdev)) {
17232 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
17233 goto done;
17234 }
17235
17236 pci_set_master(pdev);
17237 pci_restore_state(pdev);
17238 pci_save_state(pdev);
17239
17240 if (!netif_running(netdev)) {
17241 rc = PCI_ERS_RESULT_RECOVERED;
17242 goto done;
17243 }
17244
17245 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017246 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017247 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017248
17249 rc = PCI_ERS_RESULT_RECOVERED;
17250
17251done:
17252 rtnl_unlock();
17253
17254 return rc;
17255}
17256
17257/**
17258 * tg3_io_resume - called when traffic can start flowing again.
17259 * @pdev: Pointer to PCI device
17260 *
17261 * This callback is called when the error recovery driver tells
17262 * us that its OK to resume normal operation.
17263 */
17264static void tg3_io_resume(struct pci_dev *pdev)
17265{
17266 struct net_device *netdev = pci_get_drvdata(pdev);
17267 struct tg3 *tp = netdev_priv(netdev);
17268 int err;
17269
17270 rtnl_lock();
17271
17272 if (!netif_running(netdev))
17273 goto done;
17274
17275 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000017276 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017277 err = tg3_restart_hw(tp, 1);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017278 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017279 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017280 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17281 goto done;
17282 }
17283
17284 netif_device_attach(netdev);
17285
Matt Carlson21f76382012-02-22 12:35:21 +000017286 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017287
17288 tg3_netif_start(tp);
17289
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017290 tg3_full_unlock(tp);
17291
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017292 tg3_phy_start(tp);
17293
17294done:
17295 rtnl_unlock();
17296}
17297
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017298static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017299 .error_detected = tg3_io_error_detected,
17300 .slot_reset = tg3_io_slot_reset,
17301 .resume = tg3_io_resume
17302};
17303
Linus Torvalds1da177e2005-04-16 15:20:36 -070017304static struct pci_driver tg3_driver = {
17305 .name = DRV_MODULE_NAME,
17306 .id_table = tg3_pci_tbl,
17307 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017308 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017309 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017310 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017311};
17312
17313static int __init tg3_init(void)
17314{
Jeff Garzik29917622006-08-19 17:48:59 -040017315 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017316}
17317
17318static void __exit tg3_cleanup(void)
17319{
17320 pci_unregister_driver(&tg3_driver);
17321}
17322
17323module_init(tg3_init);
17324module_exit(tg3_cleanup);