blob: d88ef551dfcda026d0f31cb15c2d61e699de50f8 [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>
Michael Chane565eec2014-01-03 10:09:12 -080040#include <linux/if.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/if_vlan.h>
42#include <linux/ip.h>
43#include <linux/tcp.h>
44#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070045#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020046#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080047#include <linux/firmware.h>
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000048#include <linux/ssb/ssb_driver_gige.h>
Michael Chanaed93e02012-07-16 16:24:02 +000049#include <linux/hwmon.h>
50#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030053#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000055#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000057#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Matt Carlsonbe947302012-12-03 19:36:57 +000059#include <uapi/linux/net_tstamp.h>
60#include <linux/ptp_clock_kernel.h>
61
David S. Miller49b6e95f2007-03-29 01:38:42 -070062#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070064#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#endif
66
Matt Carlson63532392008-11-03 16:49:57 -080067#define BAR_0 0
68#define BAR_2 2
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include "tg3.h"
71
Joe Perches63c3a662011-04-26 08:12:10 +000072/* Functions & macros to verify TG3_FLAGS types */
73
74static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
75{
76 return test_bit(flag, bits);
77}
78
79static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
80{
81 set_bit(flag, bits);
82}
83
84static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
85{
86 clear_bit(flag, bits);
87}
88
89#define tg3_flag(tp, flag) \
90 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
91#define tg3_flag_set(tp, flag) \
92 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
93#define tg3_flag_clear(tp, flag) \
94 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000097#define TG3_MAJ_NUM 3
Nithin Sujir20170e72014-01-03 10:09:15 -080098#define TG3_MIN_NUM 136
Matt Carlson6867c842010-07-11 09:31:44 +000099#define DRV_MODULE_VERSION \
100 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Nithin Sujir20170e72014-01-03 10:09:15 -0800101#define DRV_MODULE_RELDATE "Jan 03, 2014"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000103#define RESET_KIND_SHUTDOWN 0
104#define RESET_KIND_INIT 1
105#define RESET_KIND_SUSPEND 2
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define TG3_DEF_RX_MODE 0
108#define TG3_DEF_TX_MODE 0
109#define TG3_DEF_MSG_ENABLE \
110 (NETIF_MSG_DRV | \
111 NETIF_MSG_PROBE | \
112 NETIF_MSG_LINK | \
113 NETIF_MSG_TIMER | \
114 NETIF_MSG_IFDOWN | \
115 NETIF_MSG_IFUP | \
116 NETIF_MSG_RX_ERR | \
117 NETIF_MSG_TX_ERR)
118
Matt Carlson520b2752011-06-13 13:39:02 +0000119#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/* length of time before we decide the hardware is borked,
122 * and dev->tx_timeout() should be called to fix the problem
123 */
Joe Perches63c3a662011-04-26 08:12:10 +0000124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define TG3_TX_TIMEOUT (5 * HZ)
126
127/* hardware minimum and maximum for a single frame's data payload */
128#define TG3_MIN_MTU 60
129#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000130 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132/* These numbers seem to be hard coded in the NIC firmware somehow.
133 * You can't change the ring sizes, but you can change where you place
134 * them in the NIC onboard memory.
135 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000136#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000137 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000138 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000140#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000141 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000142 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#define TG3_DEF_RX_JUMBO_RING_PENDING 100
144
145/* Do not place this n-ring entries value into the tp struct itself,
146 * we really want to expose these constants to GCC so that modulo et
147 * al. operations are done with shifts and masks instead of with
148 * hw multiply/modulo instructions. Another solution would be to
149 * replace things like '% foo' with '& (foo - 1)'.
150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152#define TG3_TX_RING_SIZE 512
153#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
154
Matt Carlson2c49a442010-09-30 10:34:35 +0000155#define TG3_RX_STD_RING_BYTES(tp) \
156 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
157#define TG3_RX_JMB_RING_BYTES(tp) \
158 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
159#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000160 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
162 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
164
Matt Carlson287be122009-08-28 13:58:46 +0000165#define TG3_DMA_BYTE_ENAB 64
166
167#define TG3_RX_STD_DMA_SZ 1536
168#define TG3_RX_JMB_DMA_SZ 9046
169
170#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
171
172#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
173#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Matt Carlson2c49a442010-09-30 10:34:35 +0000175#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
176 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000177
Matt Carlson2c49a442010-09-30 10:34:35 +0000178#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
179 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000180
Matt Carlsond2757fc2010-04-12 06:58:27 +0000181/* Due to a hardware bug, the 5701 can only DMA to memory addresses
182 * that are at least dword aligned when used in PCIX mode. The driver
183 * works around this bug by double copying the packet. This workaround
184 * is built into the normal double copy length check for efficiency.
185 *
186 * However, the double copy is only necessary on those architectures
187 * where unaligned memory accesses are inefficient. For those architectures
188 * where unaligned memory accesses incur little penalty, we can reintegrate
189 * the 5701 in the normal rx path. Doing so saves a device structure
190 * dereference by hardcoding the double copy threshold in place.
191 */
192#define TG3_RX_COPY_THRESHOLD 256
193#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
194 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
195#else
196 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
197#endif
198
Matt Carlson81389f52011-08-31 11:44:49 +0000199#if (NET_IP_ALIGN != 0)
200#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
201#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000202#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000203#endif
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000206#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000207#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000208#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Matt Carlsonad829262008-11-21 17:16:16 -0800210#define TG3_RAW_IP_ALIGN 2
211
Michael Chane565eec2014-01-03 10:09:12 -0800212#define TG3_MAX_UCAST_ADDR(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 3)
213#define TG3_UCAST_ADDR_IDX(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 1)
214
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000215#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000216#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000217
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800218#define FIRMWARE_TG3 "tigon/tg3.bin"
Nithin Sujirc4dab502013-03-06 17:02:34 +0000219#define FIRMWARE_TG357766 "tigon/tg357766.bin"
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800220#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
221#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
222
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500223static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000224 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
227MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
228MODULE_LICENSE("GPL");
229MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800230MODULE_FIRMWARE(FIRMWARE_TG3);
231MODULE_FIRMWARE(FIRMWARE_TG3TSO);
232MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
235module_param(tg3_debug, int, 0);
236MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
237
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000238#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
239#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
240
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000241static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
261 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
262 TG3_DRV_DATA_FLAG_5705_10_100},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
264 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
265 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
268 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
269 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
276 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
282 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000290 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
291 PCI_VENDOR_ID_LENOVO,
292 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
293 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
296 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
310 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800311 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
312 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000313 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
314 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000315 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
316 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
317 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
318 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
319 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
320 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
324 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000329 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
330 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
332 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000333 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
334 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
335 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
336 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000338 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000339 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000340 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000341 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
342 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
343 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Nithin Sujir68273712013-09-20 16:46:56 -0700344 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57764)},
345 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57767)},
346 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57787)},
347 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57782)},
348 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57786)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700349 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
350 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
351 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
352 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
353 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
354 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
355 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000356 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700357 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358};
359
360MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
361
Andreas Mohr50da8592006-08-14 23:54:30 -0700362static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000364} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 { "rx_octets" },
366 { "rx_fragments" },
367 { "rx_ucast_packets" },
368 { "rx_mcast_packets" },
369 { "rx_bcast_packets" },
370 { "rx_fcs_errors" },
371 { "rx_align_errors" },
372 { "rx_xon_pause_rcvd" },
373 { "rx_xoff_pause_rcvd" },
374 { "rx_mac_ctrl_rcvd" },
375 { "rx_xoff_entered" },
376 { "rx_frame_too_long_errors" },
377 { "rx_jabbers" },
378 { "rx_undersize_packets" },
379 { "rx_in_length_errors" },
380 { "rx_out_length_errors" },
381 { "rx_64_or_less_octet_packets" },
382 { "rx_65_to_127_octet_packets" },
383 { "rx_128_to_255_octet_packets" },
384 { "rx_256_to_511_octet_packets" },
385 { "rx_512_to_1023_octet_packets" },
386 { "rx_1024_to_1522_octet_packets" },
387 { "rx_1523_to_2047_octet_packets" },
388 { "rx_2048_to_4095_octet_packets" },
389 { "rx_4096_to_8191_octet_packets" },
390 { "rx_8192_to_9022_octet_packets" },
391
392 { "tx_octets" },
393 { "tx_collisions" },
394
395 { "tx_xon_sent" },
396 { "tx_xoff_sent" },
397 { "tx_flow_control" },
398 { "tx_mac_errors" },
399 { "tx_single_collisions" },
400 { "tx_mult_collisions" },
401 { "tx_deferred" },
402 { "tx_excessive_collisions" },
403 { "tx_late_collisions" },
404 { "tx_collide_2times" },
405 { "tx_collide_3times" },
406 { "tx_collide_4times" },
407 { "tx_collide_5times" },
408 { "tx_collide_6times" },
409 { "tx_collide_7times" },
410 { "tx_collide_8times" },
411 { "tx_collide_9times" },
412 { "tx_collide_10times" },
413 { "tx_collide_11times" },
414 { "tx_collide_12times" },
415 { "tx_collide_13times" },
416 { "tx_collide_14times" },
417 { "tx_collide_15times" },
418 { "tx_ucast_packets" },
419 { "tx_mcast_packets" },
420 { "tx_bcast_packets" },
421 { "tx_carrier_sense_errors" },
422 { "tx_discards" },
423 { "tx_errors" },
424
425 { "dma_writeq_full" },
426 { "dma_write_prioq_full" },
427 { "rxbds_empty" },
428 { "rx_discards" },
429 { "rx_errors" },
430 { "rx_threshold_hit" },
431
432 { "dma_readq_full" },
433 { "dma_read_prioq_full" },
434 { "tx_comp_queue_full" },
435
436 { "ring_set_send_prod_index" },
437 { "ring_status_update" },
438 { "nic_irqs" },
439 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000440 { "nic_tx_threshold_hit" },
441
442 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443};
444
Matt Carlson48fa55a2011-04-13 11:05:06 +0000445#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000446#define TG3_NVRAM_TEST 0
447#define TG3_LINK_TEST 1
448#define TG3_REGISTER_TEST 2
449#define TG3_MEMORY_TEST 3
450#define TG3_MAC_LOOPB_TEST 4
451#define TG3_PHY_LOOPB_TEST 5
452#define TG3_EXT_LOOPB_TEST 6
453#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000454
455
Andreas Mohr50da8592006-08-14 23:54:30 -0700456static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700457 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000458} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000459 [TG3_NVRAM_TEST] = { "nvram test (online) " },
460 [TG3_LINK_TEST] = { "link test (online) " },
461 [TG3_REGISTER_TEST] = { "register test (offline)" },
462 [TG3_MEMORY_TEST] = { "memory test (offline)" },
463 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
464 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
465 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
466 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700467};
468
Matt Carlson48fa55a2011-04-13 11:05:06 +0000469#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
470
471
Michael Chanb401e9e2005-12-19 16:27:04 -0800472static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
473{
474 writel(val, tp->regs + off);
475}
476
477static u32 tg3_read32(struct tg3 *tp, u32 off)
478{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000479 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800480}
481
Matt Carlson0d3031d2007-10-10 18:02:43 -0700482static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
483{
484 writel(val, tp->aperegs + off);
485}
486
487static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
488{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000489 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
493{
Michael Chan68929142005-08-09 20:17:14 -0700494 unsigned long flags;
495
496 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700497 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
498 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700499 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700500}
501
502static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
503{
504 writel(val, tp->regs + off);
505 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Michael Chan68929142005-08-09 20:17:14 -0700508static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
509{
510 unsigned long flags;
511 u32 val;
512
513 spin_lock_irqsave(&tp->indirect_lock, flags);
514 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
515 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
516 spin_unlock_irqrestore(&tp->indirect_lock, flags);
517 return val;
518}
519
520static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
521{
522 unsigned long flags;
523
524 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
525 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
526 TG3_64BIT_REG_LOW, val);
527 return;
528 }
Matt Carlson66711e62009-11-13 13:03:49 +0000529 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700530 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
531 TG3_64BIT_REG_LOW, val);
532 return;
533 }
534
535 spin_lock_irqsave(&tp->indirect_lock, flags);
536 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
537 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
538 spin_unlock_irqrestore(&tp->indirect_lock, flags);
539
540 /* In indirect mode when disabling interrupts, we also need
541 * to clear the interrupt bit in the GRC local ctrl register.
542 */
543 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
544 (val == 0x1)) {
545 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
546 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
547 }
548}
549
550static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
551{
552 unsigned long flags;
553 u32 val;
554
555 spin_lock_irqsave(&tp->indirect_lock, flags);
556 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
557 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
558 spin_unlock_irqrestore(&tp->indirect_lock, flags);
559 return val;
560}
561
Michael Chanb401e9e2005-12-19 16:27:04 -0800562/* usec_wait specifies the wait time in usec when writing to certain registers
563 * where it is unsafe to read back the register without some delay.
564 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
565 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
566 */
567static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Joe Perches63c3a662011-04-26 08:12:10 +0000569 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800570 /* Non-posted methods */
571 tp->write32(tp, off, val);
572 else {
573 /* Posted method */
574 tg3_write32(tp, off, val);
575 if (usec_wait)
576 udelay(usec_wait);
577 tp->read32(tp, off);
578 }
579 /* Wait again after the read for the posted method to guarantee that
580 * the wait time is met.
581 */
582 if (usec_wait)
583 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Michael Chan09ee9292005-08-09 20:17:00 -0700586static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
587{
588 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000589 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
590 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
591 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700592 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700593}
594
Michael Chan20094932005-08-09 20:16:32 -0700595static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 void __iomem *mbox = tp->regs + off;
598 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000599 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000601 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
602 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 readl(mbox);
604}
605
Michael Chanb5d37722006-09-27 16:06:21 -0700606static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
607{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000608 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700609}
610
611static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
612{
613 writel(val, tp->regs + off + GRCMBOX_BASE);
614}
615
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000616#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700617#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000618#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
619#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
620#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700621
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000622#define tw32(reg, val) tp->write32(tp, reg, val)
623#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
624#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
625#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
628{
Michael Chan68929142005-08-09 20:17:14 -0700629 unsigned long flags;
630
Joe Perches41535772013-02-16 11:20:04 +0000631 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700632 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
633 return;
634
Michael Chan68929142005-08-09 20:17:14 -0700635 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000636 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700637 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
638 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Michael Chanbbadf502006-04-06 21:46:34 -0700640 /* Always leave this as zero. */
641 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
642 } else {
643 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
644 tw32_f(TG3PCI_MEM_WIN_DATA, val);
645
646 /* Always leave this as zero. */
647 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
648 }
Michael Chan68929142005-08-09 20:17:14 -0700649 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
653{
Michael Chan68929142005-08-09 20:17:14 -0700654 unsigned long flags;
655
Joe Perches41535772013-02-16 11:20:04 +0000656 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700657 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
658 *val = 0;
659 return;
660 }
661
Michael Chan68929142005-08-09 20:17:14 -0700662 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000663 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700664 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
665 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Michael Chanbbadf502006-04-06 21:46:34 -0700667 /* Always leave this as zero. */
668 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
669 } else {
670 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
671 *val = tr32(TG3PCI_MEM_WIN_DATA);
672
673 /* Always leave this as zero. */
674 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
675 }
Michael Chan68929142005-08-09 20:17:14 -0700676 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Matt Carlson0d3031d2007-10-10 18:02:43 -0700679static void tg3_ape_lock_init(struct tg3 *tp)
680{
681 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000682 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000683
Joe Perches41535772013-02-16 11:20:04 +0000684 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000685 regbase = TG3_APE_LOCK_GRANT;
686 else
687 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700688
689 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000690 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
691 switch (i) {
692 case TG3_APE_LOCK_PHY0:
693 case TG3_APE_LOCK_PHY1:
694 case TG3_APE_LOCK_PHY2:
695 case TG3_APE_LOCK_PHY3:
696 bit = APE_LOCK_GRANT_DRIVER;
697 break;
698 default:
699 if (!tp->pci_fn)
700 bit = APE_LOCK_GRANT_DRIVER;
701 else
702 bit = 1 << tp->pci_fn;
703 }
704 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000705 }
706
Matt Carlson0d3031d2007-10-10 18:02:43 -0700707}
708
709static int tg3_ape_lock(struct tg3 *tp, int locknum)
710{
711 int i, off;
712 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000713 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700714
Joe Perches63c3a662011-04-26 08:12:10 +0000715 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700716 return 0;
717
718 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000719 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000720 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000721 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000722 case TG3_APE_LOCK_GRC:
723 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000724 if (!tp->pci_fn)
725 bit = APE_LOCK_REQ_DRIVER;
726 else
727 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000728 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000729 case TG3_APE_LOCK_PHY0:
730 case TG3_APE_LOCK_PHY1:
731 case TG3_APE_LOCK_PHY2:
732 case TG3_APE_LOCK_PHY3:
733 bit = APE_LOCK_REQ_DRIVER;
734 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000735 default:
736 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700737 }
738
Joe Perches41535772013-02-16 11:20:04 +0000739 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000740 req = TG3_APE_LOCK_REQ;
741 gnt = TG3_APE_LOCK_GRANT;
742 } else {
743 req = TG3_APE_PER_LOCK_REQ;
744 gnt = TG3_APE_PER_LOCK_GRANT;
745 }
746
Matt Carlson0d3031d2007-10-10 18:02:43 -0700747 off = 4 * locknum;
748
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000749 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700750
751 /* Wait for up to 1 millisecond to acquire lock. */
752 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000753 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000754 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700755 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +0800756 if (pci_channel_offline(tp->pdev))
757 break;
758
Matt Carlson0d3031d2007-10-10 18:02:43 -0700759 udelay(10);
760 }
761
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000762 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700763 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000764 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700765 ret = -EBUSY;
766 }
767
768 return ret;
769}
770
771static void tg3_ape_unlock(struct tg3 *tp, int locknum)
772{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000773 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700774
Joe Perches63c3a662011-04-26 08:12:10 +0000775 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700776 return;
777
778 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000779 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000780 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000781 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000782 case TG3_APE_LOCK_GRC:
783 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000784 if (!tp->pci_fn)
785 bit = APE_LOCK_GRANT_DRIVER;
786 else
787 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000788 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000789 case TG3_APE_LOCK_PHY0:
790 case TG3_APE_LOCK_PHY1:
791 case TG3_APE_LOCK_PHY2:
792 case TG3_APE_LOCK_PHY3:
793 bit = APE_LOCK_GRANT_DRIVER;
794 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000795 default:
796 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700797 }
798
Joe Perches41535772013-02-16 11:20:04 +0000799 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000800 gnt = TG3_APE_LOCK_GRANT;
801 else
802 gnt = TG3_APE_PER_LOCK_GRANT;
803
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000804 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700805}
806
Matt Carlsonb65a3722012-07-16 16:24:00 +0000807static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000808{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000809 u32 apedata;
810
Matt Carlsonb65a3722012-07-16 16:24:00 +0000811 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000812 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000813 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000814
815 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000816 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
817 break;
818
Matt Carlsonb65a3722012-07-16 16:24:00 +0000819 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
820
821 udelay(10);
822 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000823 }
824
Matt Carlsonb65a3722012-07-16 16:24:00 +0000825 return timeout_us ? 0 : -EBUSY;
826}
827
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000828static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
829{
830 u32 i, apedata;
831
832 for (i = 0; i < timeout_us / 10; i++) {
833 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
834
835 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
836 break;
837
838 udelay(10);
839 }
840
841 return i == timeout_us / 10;
842}
843
Michael Chan86449942012-10-02 20:31:14 -0700844static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
845 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000846{
847 int err;
848 u32 i, bufoff, msgoff, maxlen, apedata;
849
850 if (!tg3_flag(tp, APE_HAS_NCSI))
851 return 0;
852
853 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
854 if (apedata != APE_SEG_SIG_MAGIC)
855 return -ENODEV;
856
857 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
858 if (!(apedata & APE_FW_STATUS_READY))
859 return -EAGAIN;
860
861 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
862 TG3_APE_SHMEM_BASE;
863 msgoff = bufoff + 2 * sizeof(u32);
864 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
865
866 while (len) {
867 u32 length;
868
869 /* Cap xfer sizes to scratchpad limits. */
870 length = (len > maxlen) ? maxlen : len;
871 len -= length;
872
873 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
874 if (!(apedata & APE_FW_STATUS_READY))
875 return -EAGAIN;
876
877 /* Wait for up to 1 msec for APE to service previous event. */
878 err = tg3_ape_event_lock(tp, 1000);
879 if (err)
880 return err;
881
882 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
883 APE_EVENT_STATUS_SCRTCHPD_READ |
884 APE_EVENT_STATUS_EVENT_PENDING;
885 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
886
887 tg3_ape_write32(tp, bufoff, base_off);
888 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
889
890 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
891 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
892
893 base_off += length;
894
895 if (tg3_ape_wait_for_event(tp, 30000))
896 return -EAGAIN;
897
898 for (i = 0; length; i += 4, length -= 4) {
899 u32 val = tg3_ape_read32(tp, msgoff + i);
900 memcpy(data, &val, sizeof(u32));
901 data++;
902 }
903 }
904
905 return 0;
906}
907
Matt Carlsonb65a3722012-07-16 16:24:00 +0000908static int tg3_ape_send_event(struct tg3 *tp, u32 event)
909{
910 int err;
911 u32 apedata;
912
913 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
914 if (apedata != APE_SEG_SIG_MAGIC)
915 return -EAGAIN;
916
917 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
918 if (!(apedata & APE_FW_STATUS_READY))
919 return -EAGAIN;
920
921 /* Wait for up to 1 millisecond for APE to service previous event. */
922 err = tg3_ape_event_lock(tp, 1000);
923 if (err)
924 return err;
925
926 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
927 event | APE_EVENT_STATUS_EVENT_PENDING);
928
929 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
930 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
931
932 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000933}
934
935static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
936{
937 u32 event;
938 u32 apedata;
939
940 if (!tg3_flag(tp, ENABLE_APE))
941 return;
942
943 switch (kind) {
944 case RESET_KIND_INIT:
945 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
946 APE_HOST_SEG_SIG_MAGIC);
947 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
948 APE_HOST_SEG_LEN_MAGIC);
949 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
950 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
951 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
952 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
953 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
954 APE_HOST_BEHAV_NO_PHYLOCK);
955 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
956 TG3_APE_HOST_DRVR_STATE_START);
957
958 event = APE_EVENT_STATUS_STATE_START;
959 break;
960 case RESET_KIND_SHUTDOWN:
961 /* With the interface we are currently using,
962 * APE does not track driver state. Wiping
963 * out the HOST SEGMENT SIGNATURE forces
964 * the APE to assume OS absent status.
965 */
966 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
967
968 if (device_may_wakeup(&tp->pdev->dev) &&
969 tg3_flag(tp, WOL_ENABLE)) {
970 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
971 TG3_APE_HOST_WOL_SPEED_AUTO);
972 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
973 } else
974 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
975
976 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
977
978 event = APE_EVENT_STATUS_STATE_UNLOAD;
979 break;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000980 default:
981 return;
982 }
983
984 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
985
986 tg3_ape_send_event(tp, event);
987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989static void tg3_disable_ints(struct tg3 *tp)
990{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000991 int i;
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 tw32(TG3PCI_MISC_HOST_CTRL,
994 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000995 for (i = 0; i < tp->irq_max; i++)
996 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999static void tg3_enable_ints(struct tg3 *tp)
1000{
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001001 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001002
Michael Chanbbe832c2005-06-24 20:20:04 -07001003 tp->irq_sync = 0;
1004 wmb();
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 tw32(TG3PCI_MISC_HOST_CTRL,
1007 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001008
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001009 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001010 for (i = 0; i < tp->irq_cnt; i++) {
1011 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001012
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001013 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001014 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001015 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1016
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001017 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001018 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001019
1020 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001021 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001022 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1023 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1024 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001025 tw32(HOSTCC_MODE, tp->coal_now);
1026
1027 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Matt Carlson17375d22009-08-28 14:02:18 +00001030static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001031{
Matt Carlson17375d22009-08-28 14:02:18 +00001032 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001033 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001034 unsigned int work_exists = 0;
1035
1036 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001037 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001038 if (sblk->status & SD_STATUS_LINK_CHG)
1039 work_exists = 1;
1040 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001041
1042 /* check for TX work to do */
1043 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1044 work_exists = 1;
1045
1046 /* check for RX work to do */
1047 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001048 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001049 work_exists = 1;
1050
1051 return work_exists;
1052}
1053
Matt Carlson17375d22009-08-28 14:02:18 +00001054/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001055 * similar to tg3_enable_ints, but it accurately determines whether there
1056 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001057 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
Matt Carlson17375d22009-08-28 14:02:18 +00001059static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Matt Carlson17375d22009-08-28 14:02:18 +00001061 struct tg3 *tp = tnapi->tp;
1062
Matt Carlson898a56f2009-08-28 14:02:40 +00001063 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 mmiowb();
1065
David S. Millerfac9b832005-05-18 22:46:34 -07001066 /* When doing tagged status, this work check is unnecessary.
1067 * The last_tag we write above tells the chip which piece of
1068 * work we've completed.
1069 */
Joe Perches63c3a662011-04-26 08:12:10 +00001070 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001071 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001072 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075static void tg3_switch_clocks(struct tg3 *tp)
1076{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001077 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 u32 orig_clock_ctrl;
1079
Joe Perches63c3a662011-04-26 08:12:10 +00001080 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001081 return;
1082
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001083 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 orig_clock_ctrl = clock_ctrl;
1086 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1087 CLOCK_CTRL_CLKRUN_OENABLE |
1088 0x1f);
1089 tp->pci_clock_ctrl = clock_ctrl;
1090
Joe Perches63c3a662011-04-26 08:12:10 +00001091 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001093 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1094 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001097 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1098 clock_ctrl |
1099 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1100 40);
1101 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1102 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1103 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001105 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108#define PHY_BUSY_LOOPS 5000
1109
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001110static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1111 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 u32 frame_val;
1114 unsigned int loops;
1115 int ret;
1116
1117 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1118 tw32_f(MAC_MI_MODE,
1119 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1120 udelay(80);
1121 }
1122
Michael Chan8151ad52012-07-29 19:15:41 +00001123 tg3_ape_lock(tp, tp->phy_ape_lock);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 *val = 0x0;
1126
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001127 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 MI_COM_PHY_ADDR_MASK);
1129 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1130 MI_COM_REG_ADDR_MASK);
1131 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 tw32_f(MAC_MI_COM, frame_val);
1134
1135 loops = PHY_BUSY_LOOPS;
1136 while (loops != 0) {
1137 udelay(10);
1138 frame_val = tr32(MAC_MI_COM);
1139
1140 if ((frame_val & MI_COM_BUSY) == 0) {
1141 udelay(5);
1142 frame_val = tr32(MAC_MI_COM);
1143 break;
1144 }
1145 loops -= 1;
1146 }
1147
1148 ret = -EBUSY;
1149 if (loops != 0) {
1150 *val = frame_val & MI_COM_DATA_MASK;
1151 ret = 0;
1152 }
1153
1154 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1155 tw32_f(MAC_MI_MODE, tp->mi_mode);
1156 udelay(80);
1157 }
1158
Michael Chan8151ad52012-07-29 19:15:41 +00001159 tg3_ape_unlock(tp, tp->phy_ape_lock);
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return ret;
1162}
1163
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001164static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1165{
1166 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1167}
1168
1169static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1170 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 u32 frame_val;
1173 unsigned int loops;
1174 int ret;
1175
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001176 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001177 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001178 return 0;
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1181 tw32_f(MAC_MI_MODE,
1182 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1183 udelay(80);
1184 }
1185
Michael Chan8151ad52012-07-29 19:15:41 +00001186 tg3_ape_lock(tp, tp->phy_ape_lock);
1187
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001188 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 MI_COM_PHY_ADDR_MASK);
1190 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1191 MI_COM_REG_ADDR_MASK);
1192 frame_val |= (val & MI_COM_DATA_MASK);
1193 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 tw32_f(MAC_MI_COM, frame_val);
1196
1197 loops = PHY_BUSY_LOOPS;
1198 while (loops != 0) {
1199 udelay(10);
1200 frame_val = tr32(MAC_MI_COM);
1201 if ((frame_val & MI_COM_BUSY) == 0) {
1202 udelay(5);
1203 frame_val = tr32(MAC_MI_COM);
1204 break;
1205 }
1206 loops -= 1;
1207 }
1208
1209 ret = -EBUSY;
1210 if (loops != 0)
1211 ret = 0;
1212
1213 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1214 tw32_f(MAC_MI_MODE, tp->mi_mode);
1215 udelay(80);
1216 }
1217
Michael Chan8151ad52012-07-29 19:15:41 +00001218 tg3_ape_unlock(tp, tp->phy_ape_lock);
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return ret;
1221}
1222
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001223static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1224{
1225 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1226}
1227
Matt Carlsonb0988c12011-04-20 07:57:39 +00001228static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1229{
1230 int err;
1231
1232 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1233 if (err)
1234 goto done;
1235
1236 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1237 if (err)
1238 goto done;
1239
1240 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1241 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1242 if (err)
1243 goto done;
1244
1245 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1246
1247done:
1248 return err;
1249}
1250
1251static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1252{
1253 int err;
1254
1255 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1256 if (err)
1257 goto done;
1258
1259 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1260 if (err)
1261 goto done;
1262
1263 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1264 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1265 if (err)
1266 goto done;
1267
1268 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1269
1270done:
1271 return err;
1272}
1273
1274static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1275{
1276 int err;
1277
1278 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1279 if (!err)
1280 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1281
1282 return err;
1283}
1284
1285static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1286{
1287 int err;
1288
1289 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1290 if (!err)
1291 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1292
1293 return err;
1294}
1295
Matt Carlson15ee95c2011-04-20 07:57:40 +00001296static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1297{
1298 int err;
1299
1300 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1301 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1302 MII_TG3_AUXCTL_SHDWSEL_MISC);
1303 if (!err)
1304 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1305
1306 return err;
1307}
1308
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001309static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1310{
1311 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1312 set |= MII_TG3_AUXCTL_MISC_WREN;
1313
1314 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1315}
1316
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001317static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1318{
1319 u32 val;
1320 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001321
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001322 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1323
1324 if (err)
1325 return err;
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001326
Nithin Sujir7c10ee32013-05-23 11:11:26 +00001327 if (enable)
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001328 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1329 else
1330 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1331
1332 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1333 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1334
1335 return err;
1336}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001337
Nithin Sujir3ab71072013-09-20 16:46:55 -07001338static int tg3_phy_shdw_write(struct tg3 *tp, int reg, u32 val)
1339{
1340 return tg3_writephy(tp, MII_TG3_MISC_SHDW,
1341 reg | val | MII_TG3_MISC_SHDW_WREN);
1342}
1343
Matt Carlson95e28692008-05-25 23:44:14 -07001344static int tg3_bmcr_reset(struct tg3 *tp)
1345{
1346 u32 phy_control;
1347 int limit, err;
1348
1349 /* OK, reset it, and poll the BMCR_RESET bit until it
1350 * clears or we time out.
1351 */
1352 phy_control = BMCR_RESET;
1353 err = tg3_writephy(tp, MII_BMCR, phy_control);
1354 if (err != 0)
1355 return -EBUSY;
1356
1357 limit = 5000;
1358 while (limit--) {
1359 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1360 if (err != 0)
1361 return -EBUSY;
1362
1363 if ((phy_control & BMCR_RESET) == 0) {
1364 udelay(40);
1365 break;
1366 }
1367 udelay(10);
1368 }
Roel Kluind4675b52009-02-12 16:33:27 -08001369 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001370 return -EBUSY;
1371
1372 return 0;
1373}
1374
Matt Carlson158d7ab2008-05-29 01:37:54 -07001375static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1376{
Francois Romieu3d165432009-01-19 16:56:50 -08001377 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001378 u32 val;
1379
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001380 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001381
Hauke Mehrtensead24022013-09-28 23:15:26 +02001382 if (__tg3_readphy(tp, mii_id, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001383 val = -EIO;
1384
1385 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001386
1387 return val;
1388}
1389
1390static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1391{
Francois Romieu3d165432009-01-19 16:56:50 -08001392 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001393 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001394
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001395 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001396
Hauke Mehrtensead24022013-09-28 23:15:26 +02001397 if (__tg3_writephy(tp, mii_id, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001398 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001399
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001400 spin_unlock_bh(&tp->lock);
1401
1402 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001403}
1404
1405static int tg3_mdio_reset(struct mii_bus *bp)
1406{
1407 return 0;
1408}
1409
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001410static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001411{
1412 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001413 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001414
Hauke Mehrtensead24022013-09-28 23:15:26 +02001415 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001416 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001417 case PHY_ID_BCM50610:
1418 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001419 val = MAC_PHYCFG2_50610_LED_MODES;
1420 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001421 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001422 val = MAC_PHYCFG2_AC131_LED_MODES;
1423 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001424 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001425 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1426 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001427 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001428 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1429 break;
1430 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001431 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001432 }
1433
1434 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1435 tw32(MAC_PHYCFG2, val);
1436
1437 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001438 val &= ~(MAC_PHYCFG1_RGMII_INT |
1439 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1440 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001441 tw32(MAC_PHYCFG1, val);
1442
1443 return;
1444 }
1445
Joe Perches63c3a662011-04-26 08:12:10 +00001446 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001447 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1448 MAC_PHYCFG2_FMODE_MASK_MASK |
1449 MAC_PHYCFG2_GMODE_MASK_MASK |
1450 MAC_PHYCFG2_ACT_MASK_MASK |
1451 MAC_PHYCFG2_QUAL_MASK_MASK |
1452 MAC_PHYCFG2_INBAND_ENABLE;
1453
1454 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001455
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001456 val = tr32(MAC_PHYCFG1);
1457 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1458 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001459 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1460 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001461 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001462 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001463 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1464 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001465 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1466 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1467 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001468
Matt Carlsona9daf362008-05-25 23:49:44 -07001469 val = tr32(MAC_EXT_RGMII_MODE);
1470 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1471 MAC_RGMII_MODE_RX_QUALITY |
1472 MAC_RGMII_MODE_RX_ACTIVITY |
1473 MAC_RGMII_MODE_RX_ENG_DET |
1474 MAC_RGMII_MODE_TX_ENABLE |
1475 MAC_RGMII_MODE_TX_LOWPWR |
1476 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001477 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1478 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001479 val |= MAC_RGMII_MODE_RX_INT_B |
1480 MAC_RGMII_MODE_RX_QUALITY |
1481 MAC_RGMII_MODE_RX_ACTIVITY |
1482 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001483 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001484 val |= MAC_RGMII_MODE_TX_ENABLE |
1485 MAC_RGMII_MODE_TX_LOWPWR |
1486 MAC_RGMII_MODE_TX_RESET;
1487 }
1488 tw32(MAC_EXT_RGMII_MODE, val);
1489}
1490
Matt Carlson158d7ab2008-05-29 01:37:54 -07001491static void tg3_mdio_start(struct tg3 *tp)
1492{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001493 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1494 tw32_f(MAC_MI_MODE, tp->mi_mode);
1495 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001496
Joe Perches63c3a662011-04-26 08:12:10 +00001497 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001498 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001499 tg3_mdio_config_5785(tp);
1500}
1501
1502static int tg3_mdio_init(struct tg3 *tp)
1503{
1504 int i;
1505 u32 reg;
1506 struct phy_device *phydev;
1507
Joe Perches63c3a662011-04-26 08:12:10 +00001508 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001509 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001510
Matt Carlson69f11c92011-07-13 09:27:30 +00001511 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001512
Joe Perches41535772013-02-16 11:20:04 +00001513 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001514 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1515 else
1516 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1517 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001518 if (is_serdes)
1519 tp->phy_addr += 7;
Hauke Mehrtensee002b62013-09-28 23:15:28 +02001520 } else if (tg3_flag(tp, IS_SSB_CORE) && tg3_flag(tp, ROBOSWITCH)) {
1521 int addr;
1522
1523 addr = ssb_gige_get_phyaddr(tp->pdev);
1524 if (addr < 0)
1525 return addr;
1526 tp->phy_addr = addr;
Matt Carlson882e9792009-09-01 13:21:36 +00001527 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001528 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001529
Matt Carlson158d7ab2008-05-29 01:37:54 -07001530 tg3_mdio_start(tp);
1531
Joe Perches63c3a662011-04-26 08:12:10 +00001532 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001533 return 0;
1534
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001535 tp->mdio_bus = mdiobus_alloc();
1536 if (tp->mdio_bus == NULL)
1537 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001538
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001539 tp->mdio_bus->name = "tg3 mdio bus";
1540 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001541 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001542 tp->mdio_bus->priv = tp;
1543 tp->mdio_bus->parent = &tp->pdev->dev;
1544 tp->mdio_bus->read = &tg3_mdio_read;
1545 tp->mdio_bus->write = &tg3_mdio_write;
1546 tp->mdio_bus->reset = &tg3_mdio_reset;
Hauke Mehrtensead24022013-09-28 23:15:26 +02001547 tp->mdio_bus->phy_mask = ~(1 << tp->phy_addr);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001548 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001549
1550 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001551 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001552
1553 /* The bus registration will look for all the PHYs on the mdio bus.
1554 * Unfortunately, it does not ensure the PHY is powered up before
1555 * accessing the PHY ID registers. A chip reset is the
1556 * quickest way to bring the device back to an operational state..
1557 */
1558 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1559 tg3_bmcr_reset(tp);
1560
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001561 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001562 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001563 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001564 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001565 return i;
1566 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001567
Hauke Mehrtensead24022013-09-28 23:15:26 +02001568 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsona9daf362008-05-25 23:49:44 -07001569
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001570 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001571 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001572 mdiobus_unregister(tp->mdio_bus);
1573 mdiobus_free(tp->mdio_bus);
1574 return -ENODEV;
1575 }
1576
1577 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001578 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001579 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001580 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001581 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001582 case PHY_ID_BCM50610:
1583 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001584 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001585 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001586 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001587 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001588 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001589 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001590 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001591 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001592 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001593 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001594 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001595 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001596 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001597 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001598 case PHY_ID_RTL8201E:
1599 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001600 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001601 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001602 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001603 break;
1604 }
1605
Joe Perches63c3a662011-04-26 08:12:10 +00001606 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001607
Joe Perches41535772013-02-16 11:20:04 +00001608 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001609 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001610
1611 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001612}
1613
1614static void tg3_mdio_fini(struct tg3 *tp)
1615{
Joe Perches63c3a662011-04-26 08:12:10 +00001616 if (tg3_flag(tp, MDIOBUS_INITED)) {
1617 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001618 mdiobus_unregister(tp->mdio_bus);
1619 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001620 }
1621}
1622
Matt Carlson95e28692008-05-25 23:44:14 -07001623/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001624static inline void tg3_generate_fw_event(struct tg3 *tp)
1625{
1626 u32 val;
1627
1628 val = tr32(GRC_RX_CPU_EVENT);
1629 val |= GRC_RX_CPU_DRIVER_EVENT;
1630 tw32_f(GRC_RX_CPU_EVENT, val);
1631
1632 tp->last_event_jiffies = jiffies;
1633}
1634
1635#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1636
1637/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001638static void tg3_wait_for_event_ack(struct tg3 *tp)
1639{
1640 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001641 unsigned int delay_cnt;
1642 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001643
Matt Carlson4ba526c2008-08-15 14:10:04 -07001644 /* If enough time has passed, no wait is necessary. */
1645 time_remain = (long)(tp->last_event_jiffies + 1 +
1646 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1647 (long)jiffies;
1648 if (time_remain < 0)
1649 return;
1650
1651 /* Check if we can shorten the wait time. */
1652 delay_cnt = jiffies_to_usecs(time_remain);
1653 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1654 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1655 delay_cnt = (delay_cnt >> 3) + 1;
1656
1657 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001658 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1659 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001660 if (pci_channel_offline(tp->pdev))
1661 break;
1662
Matt Carlson4ba526c2008-08-15 14:10:04 -07001663 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001664 }
1665}
1666
1667/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001668static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001669{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001670 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001671
1672 val = 0;
1673 if (!tg3_readphy(tp, MII_BMCR, &reg))
1674 val = reg << 16;
1675 if (!tg3_readphy(tp, MII_BMSR, &reg))
1676 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001677 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001678
1679 val = 0;
1680 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1681 val = reg << 16;
1682 if (!tg3_readphy(tp, MII_LPA, &reg))
1683 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001684 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001685
1686 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001687 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001688 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1689 val = reg << 16;
1690 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1691 val |= (reg & 0xffff);
1692 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001693 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001694
1695 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1696 val = reg << 16;
1697 else
1698 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001699 *data++ = val;
1700}
1701
1702/* tp->lock is held. */
1703static void tg3_ump_link_report(struct tg3 *tp)
1704{
1705 u32 data[4];
1706
1707 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1708 return;
1709
1710 tg3_phy_gather_ump_data(tp, data);
1711
1712 tg3_wait_for_event_ack(tp);
1713
1714 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1715 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1716 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1717 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1718 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1719 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001720
Matt Carlson4ba526c2008-08-15 14:10:04 -07001721 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001722}
1723
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001724/* tp->lock is held. */
1725static void tg3_stop_fw(struct tg3 *tp)
1726{
1727 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1728 /* Wait for RX cpu to ACK the previous event. */
1729 tg3_wait_for_event_ack(tp);
1730
1731 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1732
1733 tg3_generate_fw_event(tp);
1734
1735 /* Wait for RX cpu to ACK this event. */
1736 tg3_wait_for_event_ack(tp);
1737 }
1738}
1739
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001740/* tp->lock is held. */
1741static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1742{
1743 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1744 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1745
1746 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1747 switch (kind) {
1748 case RESET_KIND_INIT:
1749 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1750 DRV_STATE_START);
1751 break;
1752
1753 case RESET_KIND_SHUTDOWN:
1754 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1755 DRV_STATE_UNLOAD);
1756 break;
1757
1758 case RESET_KIND_SUSPEND:
1759 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1760 DRV_STATE_SUSPEND);
1761 break;
1762
1763 default:
1764 break;
1765 }
1766 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001767}
1768
1769/* tp->lock is held. */
1770static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1771{
1772 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1773 switch (kind) {
1774 case RESET_KIND_INIT:
1775 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1776 DRV_STATE_START_DONE);
1777 break;
1778
1779 case RESET_KIND_SHUTDOWN:
1780 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1781 DRV_STATE_UNLOAD_DONE);
1782 break;
1783
1784 default:
1785 break;
1786 }
1787 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001788}
1789
1790/* tp->lock is held. */
1791static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1792{
1793 if (tg3_flag(tp, ENABLE_ASF)) {
1794 switch (kind) {
1795 case RESET_KIND_INIT:
1796 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1797 DRV_STATE_START);
1798 break;
1799
1800 case RESET_KIND_SHUTDOWN:
1801 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1802 DRV_STATE_UNLOAD);
1803 break;
1804
1805 case RESET_KIND_SUSPEND:
1806 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1807 DRV_STATE_SUSPEND);
1808 break;
1809
1810 default:
1811 break;
1812 }
1813 }
1814}
1815
1816static int tg3_poll_fw(struct tg3 *tp)
1817{
1818 int i;
1819 u32 val;
1820
Nithin Sujirdf465ab2013-06-12 11:08:59 -07001821 if (tg3_flag(tp, NO_FWARE_REPORTED))
1822 return 0;
1823
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001824 if (tg3_flag(tp, IS_SSB_CORE)) {
1825 /* We don't use firmware. */
1826 return 0;
1827 }
1828
Joe Perches41535772013-02-16 11:20:04 +00001829 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001830 /* Wait up to 20ms for init done. */
1831 for (i = 0; i < 200; i++) {
1832 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1833 return 0;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001834 if (pci_channel_offline(tp->pdev))
1835 return -ENODEV;
1836
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001837 udelay(100);
1838 }
1839 return -ENODEV;
1840 }
1841
1842 /* Wait for firmware initialization to complete. */
1843 for (i = 0; i < 100000; i++) {
1844 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1845 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1846 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001847 if (pci_channel_offline(tp->pdev)) {
1848 if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
1849 tg3_flag_set(tp, NO_FWARE_REPORTED);
1850 netdev_info(tp->dev, "No firmware running\n");
1851 }
1852
1853 break;
1854 }
1855
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001856 udelay(10);
1857 }
1858
1859 /* Chip might not be fitted with firmware. Some Sun onboard
1860 * parts are configured like that. So don't signal the timeout
1861 * of the above loop as an error, but do report the lack of
1862 * running firmware once.
1863 */
1864 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1865 tg3_flag_set(tp, NO_FWARE_REPORTED);
1866
1867 netdev_info(tp->dev, "No firmware running\n");
1868 }
1869
Joe Perches41535772013-02-16 11:20:04 +00001870 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001871 /* The 57765 A0 needs a little more
1872 * time to do some important work.
1873 */
1874 mdelay(10);
1875 }
1876
1877 return 0;
1878}
1879
Matt Carlson95e28692008-05-25 23:44:14 -07001880static void tg3_link_report(struct tg3 *tp)
1881{
1882 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001883 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001884 tg3_ump_link_report(tp);
1885 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001886 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1887 (tp->link_config.active_speed == SPEED_1000 ?
1888 1000 :
1889 (tp->link_config.active_speed == SPEED_100 ?
1890 100 : 10)),
1891 (tp->link_config.active_duplex == DUPLEX_FULL ?
1892 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001893
Joe Perches05dbe002010-02-17 19:44:19 +00001894 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1895 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1896 "on" : "off",
1897 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1898 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001899
1900 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1901 netdev_info(tp->dev, "EEE is %s\n",
1902 tp->setlpicnt ? "enabled" : "disabled");
1903
Matt Carlson95e28692008-05-25 23:44:14 -07001904 tg3_ump_link_report(tp);
1905 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001906
1907 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001908}
1909
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001910static u32 tg3_decode_flowctrl_1000T(u32 adv)
1911{
1912 u32 flowctrl = 0;
1913
1914 if (adv & ADVERTISE_PAUSE_CAP) {
1915 flowctrl |= FLOW_CTRL_RX;
1916 if (!(adv & ADVERTISE_PAUSE_ASYM))
1917 flowctrl |= FLOW_CTRL_TX;
1918 } else if (adv & ADVERTISE_PAUSE_ASYM)
1919 flowctrl |= FLOW_CTRL_TX;
1920
1921 return flowctrl;
1922}
1923
Matt Carlson95e28692008-05-25 23:44:14 -07001924static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1925{
1926 u16 miireg;
1927
Steve Glendinninge18ce342008-12-16 02:00:00 -08001928 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001929 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001930 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001931 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001932 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001933 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1934 else
1935 miireg = 0;
1936
1937 return miireg;
1938}
1939
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001940static u32 tg3_decode_flowctrl_1000X(u32 adv)
1941{
1942 u32 flowctrl = 0;
1943
1944 if (adv & ADVERTISE_1000XPAUSE) {
1945 flowctrl |= FLOW_CTRL_RX;
1946 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1947 flowctrl |= FLOW_CTRL_TX;
1948 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1949 flowctrl |= FLOW_CTRL_TX;
1950
1951 return flowctrl;
1952}
1953
Matt Carlson95e28692008-05-25 23:44:14 -07001954static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1955{
1956 u8 cap = 0;
1957
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001958 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1959 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1960 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1961 if (lcladv & ADVERTISE_1000XPAUSE)
1962 cap = FLOW_CTRL_RX;
1963 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001964 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001965 }
1966
1967 return cap;
1968}
1969
Matt Carlsonf51f3562008-05-25 23:45:08 -07001970static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001971{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001972 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001973 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001974 u32 old_rx_mode = tp->rx_mode;
1975 u32 old_tx_mode = tp->tx_mode;
1976
Joe Perches63c3a662011-04-26 08:12:10 +00001977 if (tg3_flag(tp, USE_PHYLIB))
Hauke Mehrtensead24022013-09-28 23:15:26 +02001978 autoneg = tp->mdio_bus->phy_map[tp->phy_addr]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001979 else
1980 autoneg = tp->link_config.autoneg;
1981
Joe Perches63c3a662011-04-26 08:12:10 +00001982 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001983 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001984 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001985 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001986 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001987 } else
1988 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001989
Matt Carlsonf51f3562008-05-25 23:45:08 -07001990 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001991
Steve Glendinninge18ce342008-12-16 02:00:00 -08001992 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001993 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1994 else
1995 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1996
Matt Carlsonf51f3562008-05-25 23:45:08 -07001997 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001998 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001999
Steve Glendinninge18ce342008-12-16 02:00:00 -08002000 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07002001 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
2002 else
2003 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
2004
Matt Carlsonf51f3562008-05-25 23:45:08 -07002005 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07002006 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07002007}
2008
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002009static void tg3_adjust_link(struct net_device *dev)
2010{
2011 u8 oldflowctrl, linkmesg = 0;
2012 u32 mac_mode, lcl_adv, rmt_adv;
2013 struct tg3 *tp = netdev_priv(dev);
Hauke Mehrtensead24022013-09-28 23:15:26 +02002014 struct phy_device *phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002015
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002016 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002017
2018 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
2019 MAC_MODE_HALF_DUPLEX);
2020
2021 oldflowctrl = tp->link_config.active_flowctrl;
2022
2023 if (phydev->link) {
2024 lcl_adv = 0;
2025 rmt_adv = 0;
2026
2027 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
2028 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002029 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00002030 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002031 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002032 else
2033 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002034
2035 if (phydev->duplex == DUPLEX_HALF)
2036 mac_mode |= MAC_MODE_HALF_DUPLEX;
2037 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002038 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002039 tp->link_config.flowctrl);
2040
2041 if (phydev->pause)
2042 rmt_adv = LPA_PAUSE_CAP;
2043 if (phydev->asym_pause)
2044 rmt_adv |= LPA_PAUSE_ASYM;
2045 }
2046
2047 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2048 } else
2049 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2050
2051 if (mac_mode != tp->mac_mode) {
2052 tp->mac_mode = mac_mode;
2053 tw32_f(MAC_MODE, tp->mac_mode);
2054 udelay(40);
2055 }
2056
Joe Perches41535772013-02-16 11:20:04 +00002057 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002058 if (phydev->speed == SPEED_10)
2059 tw32(MAC_MI_STAT,
2060 MAC_MI_STAT_10MBPS_MODE |
2061 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2062 else
2063 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2064 }
2065
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002066 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2067 tw32(MAC_TX_LENGTHS,
2068 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2069 (6 << TX_LENGTHS_IPG_SHIFT) |
2070 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2071 else
2072 tw32(MAC_TX_LENGTHS,
2073 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2074 (6 << TX_LENGTHS_IPG_SHIFT) |
2075 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2076
Matt Carlson34655ad2012-02-22 12:35:18 +00002077 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002078 phydev->speed != tp->link_config.active_speed ||
2079 phydev->duplex != tp->link_config.active_duplex ||
2080 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002081 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002082
Matt Carlson34655ad2012-02-22 12:35:18 +00002083 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002084 tp->link_config.active_speed = phydev->speed;
2085 tp->link_config.active_duplex = phydev->duplex;
2086
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002087 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002088
2089 if (linkmesg)
2090 tg3_link_report(tp);
2091}
2092
2093static int tg3_phy_init(struct tg3 *tp)
2094{
2095 struct phy_device *phydev;
2096
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002097 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002098 return 0;
2099
2100 /* Bring the PHY back to a known state. */
2101 tg3_bmcr_reset(tp);
2102
Hauke Mehrtensead24022013-09-28 23:15:26 +02002103 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002104
2105 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002106 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2107 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002108 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002109 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002110 return PTR_ERR(phydev);
2111 }
2112
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002113 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002114 switch (phydev->interface) {
2115 case PHY_INTERFACE_MODE_GMII:
2116 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002117 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002118 phydev->supported &= (PHY_GBIT_FEATURES |
2119 SUPPORTED_Pause |
2120 SUPPORTED_Asym_Pause);
2121 break;
2122 }
2123 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002124 case PHY_INTERFACE_MODE_MII:
2125 phydev->supported &= (PHY_BASIC_FEATURES |
2126 SUPPORTED_Pause |
2127 SUPPORTED_Asym_Pause);
2128 break;
2129 default:
Hauke Mehrtensead24022013-09-28 23:15:26 +02002130 phy_disconnect(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002131 return -EINVAL;
2132 }
2133
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002134 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002135
2136 phydev->advertising = phydev->supported;
2137
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002138 return 0;
2139}
2140
2141static void tg3_phy_start(struct tg3 *tp)
2142{
2143 struct phy_device *phydev;
2144
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002145 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002146 return;
2147
Hauke Mehrtensead24022013-09-28 23:15:26 +02002148 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002149
Matt Carlson80096062010-08-02 11:26:06 +00002150 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2151 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002152 phydev->speed = tp->link_config.speed;
2153 phydev->duplex = tp->link_config.duplex;
2154 phydev->autoneg = tp->link_config.autoneg;
2155 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002156 }
2157
2158 phy_start(phydev);
2159
2160 phy_start_aneg(phydev);
2161}
2162
2163static void tg3_phy_stop(struct tg3 *tp)
2164{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002165 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002166 return;
2167
Hauke Mehrtensead24022013-09-28 23:15:26 +02002168 phy_stop(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002169}
2170
2171static void tg3_phy_fini(struct tg3 *tp)
2172{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002173 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Hauke Mehrtensead24022013-09-28 23:15:26 +02002174 phy_disconnect(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002175 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002176 }
2177}
2178
Matt Carlson941ec902011-08-19 13:58:23 +00002179static int tg3_phy_set_extloopbk(struct tg3 *tp)
2180{
2181 int err;
2182 u32 val;
2183
2184 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2185 return 0;
2186
2187 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2188 /* Cannot do read-modify-write on 5401 */
2189 err = tg3_phy_auxctl_write(tp,
2190 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2191 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2192 0x4c20);
2193 goto done;
2194 }
2195
2196 err = tg3_phy_auxctl_read(tp,
2197 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2198 if (err)
2199 return err;
2200
2201 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2202 err = tg3_phy_auxctl_write(tp,
2203 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2204
2205done:
2206 return err;
2207}
2208
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002209static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2210{
2211 u32 phytest;
2212
2213 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2214 u32 phy;
2215
2216 tg3_writephy(tp, MII_TG3_FET_TEST,
2217 phytest | MII_TG3_FET_SHADOW_EN);
2218 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2219 if (enable)
2220 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2221 else
2222 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2223 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2224 }
2225 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2226 }
2227}
2228
Matt Carlson6833c042008-11-21 17:18:59 -08002229static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2230{
2231 u32 reg;
2232
Joe Perches63c3a662011-04-26 08:12:10 +00002233 if (!tg3_flag(tp, 5705_PLUS) ||
2234 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002235 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002236 return;
2237
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002238 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002239 tg3_phy_fet_toggle_apd(tp, enable);
2240 return;
2241 }
2242
Nithin Sujir3ab71072013-09-20 16:46:55 -07002243 reg = MII_TG3_MISC_SHDW_SCR5_LPED |
Matt Carlson6833c042008-11-21 17:18:59 -08002244 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2245 MII_TG3_MISC_SHDW_SCR5_SDTL |
2246 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002247 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002248 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2249
Nithin Sujir3ab71072013-09-20 16:46:55 -07002250 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002251
2252
Nithin Sujir3ab71072013-09-20 16:46:55 -07002253 reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
Matt Carlson6833c042008-11-21 17:18:59 -08002254 if (enable)
2255 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2256
Nithin Sujir3ab71072013-09-20 16:46:55 -07002257 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002258}
2259
Joe Perches953c96e2013-04-09 10:18:14 +00002260static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002261{
2262 u32 phy;
2263
Joe Perches63c3a662011-04-26 08:12:10 +00002264 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002265 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002266 return;
2267
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002268 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002269 u32 ephy;
2270
Matt Carlson535ef6e2009-08-25 10:09:36 +00002271 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2272 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2273
2274 tg3_writephy(tp, MII_TG3_FET_TEST,
2275 ephy | MII_TG3_FET_SHADOW_EN);
2276 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002277 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002278 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002279 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002280 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2281 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002282 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002283 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002284 }
2285 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002286 int ret;
2287
2288 ret = tg3_phy_auxctl_read(tp,
2289 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2290 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002291 if (enable)
2292 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2293 else
2294 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002295 tg3_phy_auxctl_write(tp,
2296 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002297 }
2298 }
2299}
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301static void tg3_phy_set_wirespeed(struct tg3 *tp)
2302{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002303 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 u32 val;
2305
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002306 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return;
2308
Matt Carlson15ee95c2011-04-20 07:57:40 +00002309 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2310 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002311 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2312 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002315static void tg3_phy_apply_otp(struct tg3 *tp)
2316{
2317 u32 otp, phy;
2318
2319 if (!tp->phy_otp)
2320 return;
2321
2322 otp = tp->phy_otp;
2323
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002324 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002325 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002326
2327 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2328 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2329 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2330
2331 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2332 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2333 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2334
2335 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2336 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2337 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2338
2339 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2340 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2341
2342 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2343 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2344
2345 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2346 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2347 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2348
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002349 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002350}
2351
Nithin Sujir400dfba2013-05-18 06:26:53 +00002352static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2353{
2354 u32 val;
2355 struct ethtool_eee *dest = &tp->eee;
2356
2357 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2358 return;
2359
2360 if (eee)
2361 dest = eee;
2362
2363 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2364 return;
2365
2366 /* Pull eee_active */
2367 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2368 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2369 dest->eee_active = 1;
2370 } else
2371 dest->eee_active = 0;
2372
2373 /* Pull lp advertised settings */
2374 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2375 return;
2376 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2377
2378 /* Pull advertised and eee_enabled settings */
2379 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2380 return;
2381 dest->eee_enabled = !!val;
2382 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2383
2384 /* Pull tx_lpi_enabled */
2385 val = tr32(TG3_CPMU_EEE_MODE);
2386 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2387
2388 /* Pull lpi timer value */
2389 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2390}
2391
Joe Perches953c96e2013-04-09 10:18:14 +00002392static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002393{
2394 u32 val;
2395
2396 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2397 return;
2398
2399 tp->setlpicnt = 0;
2400
2401 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002402 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002403 tp->link_config.active_duplex == DUPLEX_FULL &&
2404 (tp->link_config.active_speed == SPEED_100 ||
2405 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002406 u32 eeectl;
2407
2408 if (tp->link_config.active_speed == SPEED_1000)
2409 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2410 else
2411 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2412
2413 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2414
Nithin Sujir400dfba2013-05-18 06:26:53 +00002415 tg3_eee_pull_config(tp, NULL);
2416 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002417 tp->setlpicnt = 2;
2418 }
2419
2420 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002421 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002422 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002423 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002424 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002425 }
2426
Matt Carlson52b02d02010-10-14 10:37:41 +00002427 val = tr32(TG3_CPMU_EEE_MODE);
2428 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2429 }
2430}
2431
Matt Carlsonb0c59432011-05-19 12:12:48 +00002432static void tg3_phy_eee_enable(struct tg3 *tp)
2433{
2434 u32 val;
2435
2436 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002437 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2438 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002439 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002440 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002441 val = MII_TG3_DSP_TAP26_ALNOKO |
2442 MII_TG3_DSP_TAP26_RMRXSTO;
2443 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002444 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002445 }
2446
2447 val = tr32(TG3_CPMU_EEE_MODE);
2448 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2449}
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451static int tg3_wait_macro_done(struct tg3 *tp)
2452{
2453 int limit = 100;
2454
2455 while (limit--) {
2456 u32 tmp32;
2457
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002458 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if ((tmp32 & 0x1000) == 0)
2460 break;
2461 }
2462 }
Roel Kluind4675b52009-02-12 16:33:27 -08002463 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return -EBUSY;
2465
2466 return 0;
2467}
2468
2469static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2470{
2471 static const u32 test_pat[4][6] = {
2472 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2473 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2474 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2475 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2476 };
2477 int chan;
2478
2479 for (chan = 0; chan < 4; chan++) {
2480 int i;
2481
2482 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2483 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002484 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 for (i = 0; i < 6; i++)
2487 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2488 test_pat[chan][i]);
2489
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002490 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (tg3_wait_macro_done(tp)) {
2492 *resetp = 1;
2493 return -EBUSY;
2494 }
2495
2496 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2497 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002498 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 if (tg3_wait_macro_done(tp)) {
2500 *resetp = 1;
2501 return -EBUSY;
2502 }
2503
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002504 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (tg3_wait_macro_done(tp)) {
2506 *resetp = 1;
2507 return -EBUSY;
2508 }
2509
2510 for (i = 0; i < 6; i += 2) {
2511 u32 low, high;
2512
2513 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2514 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2515 tg3_wait_macro_done(tp)) {
2516 *resetp = 1;
2517 return -EBUSY;
2518 }
2519 low &= 0x7fff;
2520 high &= 0x000f;
2521 if (low != test_pat[chan][i] ||
2522 high != test_pat[chan][i+1]) {
2523 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2524 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2525 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2526
2527 return -EBUSY;
2528 }
2529 }
2530 }
2531
2532 return 0;
2533}
2534
2535static int tg3_phy_reset_chanpat(struct tg3 *tp)
2536{
2537 int chan;
2538
2539 for (chan = 0; chan < 4; chan++) {
2540 int i;
2541
2542 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2543 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002544 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 for (i = 0; i < 6; i++)
2546 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002547 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (tg3_wait_macro_done(tp))
2549 return -EBUSY;
2550 }
2551
2552 return 0;
2553}
2554
2555static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2556{
2557 u32 reg32, phy9_orig;
2558 int retries, do_phy_reset, err;
2559
2560 retries = 10;
2561 do_phy_reset = 1;
2562 do {
2563 if (do_phy_reset) {
2564 err = tg3_bmcr_reset(tp);
2565 if (err)
2566 return err;
2567 do_phy_reset = 0;
2568 }
2569
2570 /* Disable transmitter and interrupt. */
2571 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2572 continue;
2573
2574 reg32 |= 0x3000;
2575 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2576
2577 /* Set full-duplex, 1000 mbps. */
2578 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002579 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002582 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 continue;
2584
Matt Carlson221c5632011-06-13 13:39:01 +00002585 tg3_writephy(tp, MII_CTRL1000,
2586 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002588 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002589 if (err)
2590 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002593 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2596 if (!err)
2597 break;
2598 } while (--retries);
2599
2600 err = tg3_phy_reset_chanpat(tp);
2601 if (err)
2602 return err;
2603
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002604 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002607 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002609 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Matt Carlson221c5632011-06-13 13:39:01 +00002611 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2614 reg32 &= ~0x3000;
2615 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2616 } else if (!err)
2617 err = -EBUSY;
2618
2619 return err;
2620}
2621
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002622static void tg3_carrier_off(struct tg3 *tp)
2623{
2624 netif_carrier_off(tp->dev);
2625 tp->link_up = false;
2626}
2627
Nithin Sujirce20f162013-04-09 08:48:04 +00002628static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2629{
2630 if (tg3_flag(tp, ENABLE_ASF))
2631 netdev_warn(tp->dev,
2632 "Management side-band traffic will be interrupted during phy settings change\n");
2633}
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635/* This will reset the tigon3 PHY if there is no valid
2636 * link unless the FORCE argument is non-zero.
2637 */
2638static int tg3_phy_reset(struct tg3 *tp)
2639{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002640 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 int err;
2642
Joe Perches41535772013-02-16 11:20:04 +00002643 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002644 val = tr32(GRC_MISC_CFG);
2645 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2646 udelay(40);
2647 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002648 err = tg3_readphy(tp, MII_BMSR, &val);
2649 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 if (err != 0)
2651 return -EBUSY;
2652
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002653 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002654 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002655 tg3_link_report(tp);
2656 }
2657
Joe Perches41535772013-02-16 11:20:04 +00002658 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2659 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2660 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 err = tg3_phy_reset_5703_4_5(tp);
2662 if (err)
2663 return err;
2664 goto out;
2665 }
2666
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002667 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002668 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2669 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002670 cpmuctrl = tr32(TG3_CPMU_CTRL);
2671 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2672 tw32(TG3_CPMU_CTRL,
2673 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2674 }
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 err = tg3_bmcr_reset(tp);
2677 if (err)
2678 return err;
2679
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002680 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002681 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2682 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002683
2684 tw32(TG3_CPMU_CTRL, cpmuctrl);
2685 }
2686
Joe Perches41535772013-02-16 11:20:04 +00002687 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2688 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002689 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2690 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2691 CPMU_LSPD_1000MB_MACCLK_12_5) {
2692 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2693 udelay(40);
2694 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2695 }
2696 }
2697
Joe Perches63c3a662011-04-26 08:12:10 +00002698 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002699 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002700 return 0;
2701
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002702 tg3_phy_apply_otp(tp);
2703
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002704 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002705 tg3_phy_toggle_apd(tp, true);
2706 else
2707 tg3_phy_toggle_apd(tp, false);
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002710 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002711 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002712 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2713 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002714 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002716
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002717 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002718 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2719 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002721
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002722 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002723 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002724 tg3_phydsp_write(tp, 0x000a, 0x310b);
2725 tg3_phydsp_write(tp, 0x201f, 0x9506);
2726 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002727 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002728 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002729 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002730 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002731 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2732 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2733 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2734 tg3_writephy(tp, MII_TG3_TEST1,
2735 MII_TG3_TEST1_TRIM_EN | 0x4);
2736 } else
2737 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2738
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002739 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002740 }
Michael Chanc424cb22006-04-29 18:56:34 -07002741 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 /* Set Extended packet length bit (bit 14) on all chips that */
2744 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002745 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002747 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002748 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002750 err = tg3_phy_auxctl_read(tp,
2751 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2752 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002753 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2754 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
2756
2757 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2758 * jumbo frames transmission.
2759 */
Joe Perches63c3a662011-04-26 08:12:10 +00002760 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002761 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002762 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002763 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
2765
Joe Perches41535772013-02-16 11:20:04 +00002766 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002767 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002768 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002769 }
2770
Joe Perches41535772013-02-16 11:20:04 +00002771 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002772 tg3_phydsp_write(tp, 0xffb, 0x4000);
2773
Joe Perches953c96e2013-04-09 10:18:14 +00002774 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 tg3_phy_set_wirespeed(tp);
2776 return 0;
2777}
2778
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002779#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2780#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2781#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2782 TG3_GPIO_MSG_NEED_VAUX)
2783#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2784 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2785 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2786 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2787 (TG3_GPIO_MSG_DRVR_PRES << 12))
2788
2789#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2790 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2791 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2792 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2793 (TG3_GPIO_MSG_NEED_VAUX << 12))
2794
2795static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2796{
2797 u32 status, shift;
2798
Joe Perches41535772013-02-16 11:20:04 +00002799 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2800 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002801 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2802 else
2803 status = tr32(TG3_CPMU_DRV_STATUS);
2804
2805 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2806 status &= ~(TG3_GPIO_MSG_MASK << shift);
2807 status |= (newstat << shift);
2808
Joe Perches41535772013-02-16 11:20:04 +00002809 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2810 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002811 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2812 else
2813 tw32(TG3_CPMU_DRV_STATUS, status);
2814
2815 return status >> TG3_APE_GPIO_MSG_SHIFT;
2816}
2817
Matt Carlson520b2752011-06-13 13:39:02 +00002818static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2819{
2820 if (!tg3_flag(tp, IS_NIC))
2821 return 0;
2822
Joe Perches41535772013-02-16 11:20:04 +00002823 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2824 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2825 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002826 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2827 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002828
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002829 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2830
2831 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2832 TG3_GRC_LCLCTL_PWRSW_DELAY);
2833
2834 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2835 } else {
2836 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2837 TG3_GRC_LCLCTL_PWRSW_DELAY);
2838 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002839
Matt Carlson520b2752011-06-13 13:39:02 +00002840 return 0;
2841}
2842
2843static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2844{
2845 u32 grc_local_ctrl;
2846
2847 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002848 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2849 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002850 return;
2851
2852 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2853
2854 tw32_wait_f(GRC_LOCAL_CTRL,
2855 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2856 TG3_GRC_LCLCTL_PWRSW_DELAY);
2857
2858 tw32_wait_f(GRC_LOCAL_CTRL,
2859 grc_local_ctrl,
2860 TG3_GRC_LCLCTL_PWRSW_DELAY);
2861
2862 tw32_wait_f(GRC_LOCAL_CTRL,
2863 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2864 TG3_GRC_LCLCTL_PWRSW_DELAY);
2865}
2866
2867static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2868{
2869 if (!tg3_flag(tp, IS_NIC))
2870 return;
2871
Joe Perches41535772013-02-16 11:20:04 +00002872 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2873 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002874 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2875 (GRC_LCLCTRL_GPIO_OE0 |
2876 GRC_LCLCTRL_GPIO_OE1 |
2877 GRC_LCLCTRL_GPIO_OE2 |
2878 GRC_LCLCTRL_GPIO_OUTPUT0 |
2879 GRC_LCLCTRL_GPIO_OUTPUT1),
2880 TG3_GRC_LCLCTL_PWRSW_DELAY);
2881 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2882 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2883 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2884 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2885 GRC_LCLCTRL_GPIO_OE1 |
2886 GRC_LCLCTRL_GPIO_OE2 |
2887 GRC_LCLCTRL_GPIO_OUTPUT0 |
2888 GRC_LCLCTRL_GPIO_OUTPUT1 |
2889 tp->grc_local_ctrl;
2890 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2891 TG3_GRC_LCLCTL_PWRSW_DELAY);
2892
2893 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2894 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2895 TG3_GRC_LCLCTL_PWRSW_DELAY);
2896
2897 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2898 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2899 TG3_GRC_LCLCTL_PWRSW_DELAY);
2900 } else {
2901 u32 no_gpio2;
2902 u32 grc_local_ctrl = 0;
2903
2904 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002905 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002906 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2907 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2908 grc_local_ctrl,
2909 TG3_GRC_LCLCTL_PWRSW_DELAY);
2910 }
2911
2912 /* On 5753 and variants, GPIO2 cannot be used. */
2913 no_gpio2 = tp->nic_sram_data_cfg &
2914 NIC_SRAM_DATA_CFG_NO_GPIO2;
2915
2916 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2917 GRC_LCLCTRL_GPIO_OE1 |
2918 GRC_LCLCTRL_GPIO_OE2 |
2919 GRC_LCLCTRL_GPIO_OUTPUT1 |
2920 GRC_LCLCTRL_GPIO_OUTPUT2;
2921 if (no_gpio2) {
2922 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2923 GRC_LCLCTRL_GPIO_OUTPUT2);
2924 }
2925 tw32_wait_f(GRC_LOCAL_CTRL,
2926 tp->grc_local_ctrl | grc_local_ctrl,
2927 TG3_GRC_LCLCTL_PWRSW_DELAY);
2928
2929 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2930
2931 tw32_wait_f(GRC_LOCAL_CTRL,
2932 tp->grc_local_ctrl | grc_local_ctrl,
2933 TG3_GRC_LCLCTL_PWRSW_DELAY);
2934
2935 if (!no_gpio2) {
2936 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2937 tw32_wait_f(GRC_LOCAL_CTRL,
2938 tp->grc_local_ctrl | grc_local_ctrl,
2939 TG3_GRC_LCLCTL_PWRSW_DELAY);
2940 }
2941 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002942}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002943
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002944static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002945{
2946 u32 msg = 0;
2947
2948 /* Serialize power state transitions */
2949 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2950 return;
2951
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002952 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002953 msg = TG3_GPIO_MSG_NEED_VAUX;
2954
2955 msg = tg3_set_function_status(tp, msg);
2956
2957 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2958 goto done;
2959
2960 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2961 tg3_pwrsrc_switch_to_vaux(tp);
2962 else
2963 tg3_pwrsrc_die_with_vmain(tp);
2964
2965done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002966 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002967}
2968
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002969static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Matt Carlson683644b2011-03-09 16:58:23 +00002971 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Matt Carlson334355a2010-01-20 16:58:10 +00002973 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002974 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return;
2976
Joe Perches41535772013-02-16 11:20:04 +00002977 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2978 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2979 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002980 tg3_frob_aux_power_5717(tp, include_wol ?
2981 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002982 return;
2983 }
2984
2985 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002986 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002988 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002989
Michael Chanbc1c7562006-03-20 17:48:03 -08002990 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002991 if (dev_peer) {
2992 struct tg3 *tp_peer = netdev_priv(dev_peer);
2993
Joe Perches63c3a662011-04-26 08:12:10 +00002994 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002995 return;
2996
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002997 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002998 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002999 need_vaux = true;
3000 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08003001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003003 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
3004 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00003005 need_vaux = true;
3006
Matt Carlson520b2752011-06-13 13:39:02 +00003007 if (need_vaux)
3008 tg3_pwrsrc_switch_to_vaux(tp);
3009 else
3010 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003013static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
3014{
3015 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
3016 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00003017 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003018 if (speed != SPEED_10)
3019 return 1;
3020 } else if (speed == SPEED_10)
3021 return 1;
3022
3023 return 0;
3024}
3025
Nithin Sujir44f3b502013-05-13 11:04:15 +00003026static bool tg3_phy_power_bug(struct tg3 *tp)
3027{
3028 switch (tg3_asic_rev(tp)) {
3029 case ASIC_REV_5700:
3030 case ASIC_REV_5704:
3031 return true;
3032 case ASIC_REV_5780:
3033 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
3034 return true;
3035 return false;
3036 case ASIC_REV_5717:
3037 if (!tp->pci_fn)
3038 return true;
3039 return false;
3040 case ASIC_REV_5719:
3041 case ASIC_REV_5720:
3042 if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
3043 !tp->pci_fn)
3044 return true;
3045 return false;
3046 }
3047
3048 return false;
3049}
3050
Nithin Sujir989038e2013-08-30 17:01:36 -07003051static bool tg3_phy_led_bug(struct tg3 *tp)
3052{
3053 switch (tg3_asic_rev(tp)) {
3054 case ASIC_REV_5719:
Nithin Sujir300cf9b2013-09-12 14:01:31 -07003055 case ASIC_REV_5720:
Nithin Sujir989038e2013-08-30 17:01:36 -07003056 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
3057 !tp->pci_fn)
3058 return true;
3059 return false;
3060 }
3061
3062 return false;
3063}
3064
Matt Carlson0a459aa2008-11-03 16:54:15 -08003065static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08003066{
Matt Carlsonce057f02007-11-12 21:08:03 -08003067 u32 val;
3068
Nithin Sujir942d1af2013-04-09 08:48:07 +00003069 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3070 return;
3071
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003072 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003073 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003074 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3075 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3076
3077 sg_dig_ctrl |=
3078 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3079 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3080 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3081 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003082 return;
Michael Chan51297242007-02-13 12:17:57 -08003083 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003084
Joe Perches41535772013-02-16 11:20:04 +00003085 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003086 tg3_bmcr_reset(tp);
3087 val = tr32(GRC_MISC_CFG);
3088 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3089 udelay(40);
3090 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003091 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003092 u32 phytest;
3093 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3094 u32 phy;
3095
3096 tg3_writephy(tp, MII_ADVERTISE, 0);
3097 tg3_writephy(tp, MII_BMCR,
3098 BMCR_ANENABLE | BMCR_ANRESTART);
3099
3100 tg3_writephy(tp, MII_TG3_FET_TEST,
3101 phytest | MII_TG3_FET_SHADOW_EN);
3102 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3103 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3104 tg3_writephy(tp,
3105 MII_TG3_FET_SHDW_AUXMODE4,
3106 phy);
3107 }
3108 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3109 }
3110 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003111 } else if (do_low_power) {
Nithin Sujir989038e2013-08-30 17:01:36 -07003112 if (!tg3_phy_led_bug(tp))
3113 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3114 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003115
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003116 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3117 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3118 MII_TG3_AUXCTL_PCTL_VREG_11V;
3119 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003120 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003121
Michael Chan15c3b692006-03-22 01:06:52 -08003122 /* The PHY should not be powered down on some chips because
3123 * of bugs.
3124 */
Nithin Sujir44f3b502013-05-13 11:04:15 +00003125 if (tg3_phy_power_bug(tp))
Michael Chan15c3b692006-03-22 01:06:52 -08003126 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003127
Joe Perches41535772013-02-16 11:20:04 +00003128 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3129 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003130 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3131 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3132 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3133 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3134 }
3135
Michael Chan15c3b692006-03-22 01:06:52 -08003136 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3137}
3138
Matt Carlson3f007892008-11-03 16:51:36 -08003139/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003140static int tg3_nvram_lock(struct tg3 *tp)
3141{
Joe Perches63c3a662011-04-26 08:12:10 +00003142 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003143 int i;
3144
3145 if (tp->nvram_lock_cnt == 0) {
3146 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3147 for (i = 0; i < 8000; i++) {
3148 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3149 break;
3150 udelay(20);
3151 }
3152 if (i == 8000) {
3153 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3154 return -ENODEV;
3155 }
3156 }
3157 tp->nvram_lock_cnt++;
3158 }
3159 return 0;
3160}
3161
3162/* tp->lock is held. */
3163static void tg3_nvram_unlock(struct tg3 *tp)
3164{
Joe Perches63c3a662011-04-26 08:12:10 +00003165 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003166 if (tp->nvram_lock_cnt > 0)
3167 tp->nvram_lock_cnt--;
3168 if (tp->nvram_lock_cnt == 0)
3169 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3170 }
3171}
3172
3173/* tp->lock is held. */
3174static void tg3_enable_nvram_access(struct tg3 *tp)
3175{
Joe Perches63c3a662011-04-26 08:12:10 +00003176 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003177 u32 nvaccess = tr32(NVRAM_ACCESS);
3178
3179 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3180 }
3181}
3182
3183/* tp->lock is held. */
3184static void tg3_disable_nvram_access(struct tg3 *tp)
3185{
Joe Perches63c3a662011-04-26 08:12:10 +00003186 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003187 u32 nvaccess = tr32(NVRAM_ACCESS);
3188
3189 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3190 }
3191}
3192
3193static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3194 u32 offset, u32 *val)
3195{
3196 u32 tmp;
3197 int i;
3198
3199 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3200 return -EINVAL;
3201
3202 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3203 EEPROM_ADDR_DEVID_MASK |
3204 EEPROM_ADDR_READ);
3205 tw32(GRC_EEPROM_ADDR,
3206 tmp |
3207 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3208 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3209 EEPROM_ADDR_ADDR_MASK) |
3210 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3211
3212 for (i = 0; i < 1000; i++) {
3213 tmp = tr32(GRC_EEPROM_ADDR);
3214
3215 if (tmp & EEPROM_ADDR_COMPLETE)
3216 break;
3217 msleep(1);
3218 }
3219 if (!(tmp & EEPROM_ADDR_COMPLETE))
3220 return -EBUSY;
3221
Matt Carlson62cedd12009-04-20 14:52:29 -07003222 tmp = tr32(GRC_EEPROM_DATA);
3223
3224 /*
3225 * The data will always be opposite the native endian
3226 * format. Perform a blind byteswap to compensate.
3227 */
3228 *val = swab32(tmp);
3229
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003230 return 0;
3231}
3232
3233#define NVRAM_CMD_TIMEOUT 10000
3234
3235static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3236{
3237 int i;
3238
3239 tw32(NVRAM_CMD, nvram_cmd);
3240 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3241 udelay(10);
3242 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3243 udelay(10);
3244 break;
3245 }
3246 }
3247
3248 if (i == NVRAM_CMD_TIMEOUT)
3249 return -EBUSY;
3250
3251 return 0;
3252}
3253
3254static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3255{
Joe Perches63c3a662011-04-26 08:12:10 +00003256 if (tg3_flag(tp, NVRAM) &&
3257 tg3_flag(tp, NVRAM_BUFFERED) &&
3258 tg3_flag(tp, FLASH) &&
3259 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003260 (tp->nvram_jedecnum == JEDEC_ATMEL))
3261
3262 addr = ((addr / tp->nvram_pagesize) <<
3263 ATMEL_AT45DB0X1B_PAGE_POS) +
3264 (addr % tp->nvram_pagesize);
3265
3266 return addr;
3267}
3268
3269static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3270{
Joe Perches63c3a662011-04-26 08:12:10 +00003271 if (tg3_flag(tp, NVRAM) &&
3272 tg3_flag(tp, NVRAM_BUFFERED) &&
3273 tg3_flag(tp, FLASH) &&
3274 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003275 (tp->nvram_jedecnum == JEDEC_ATMEL))
3276
3277 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3278 tp->nvram_pagesize) +
3279 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3280
3281 return addr;
3282}
3283
Matt Carlsone4f34112009-02-25 14:25:00 +00003284/* NOTE: Data read in from NVRAM is byteswapped according to
3285 * the byteswapping settings for all other register accesses.
3286 * tg3 devices are BE devices, so on a BE machine, the data
3287 * returned will be exactly as it is seen in NVRAM. On a LE
3288 * machine, the 32-bit value will be byteswapped.
3289 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003290static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3291{
3292 int ret;
3293
Joe Perches63c3a662011-04-26 08:12:10 +00003294 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003295 return tg3_nvram_read_using_eeprom(tp, offset, val);
3296
3297 offset = tg3_nvram_phys_addr(tp, offset);
3298
3299 if (offset > NVRAM_ADDR_MSK)
3300 return -EINVAL;
3301
3302 ret = tg3_nvram_lock(tp);
3303 if (ret)
3304 return ret;
3305
3306 tg3_enable_nvram_access(tp);
3307
3308 tw32(NVRAM_ADDR, offset);
3309 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3310 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3311
3312 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003313 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003314
3315 tg3_disable_nvram_access(tp);
3316
3317 tg3_nvram_unlock(tp);
3318
3319 return ret;
3320}
3321
Matt Carlsona9dc5292009-02-25 14:25:30 +00003322/* Ensures NVRAM data is in bytestream format. */
3323static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003324{
3325 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003326 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003327 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003328 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003329 return res;
3330}
3331
Matt Carlsondbe9b922012-02-13 10:20:09 +00003332static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3333 u32 offset, u32 len, u8 *buf)
3334{
3335 int i, j, rc = 0;
3336 u32 val;
3337
3338 for (i = 0; i < len; i += 4) {
3339 u32 addr;
3340 __be32 data;
3341
3342 addr = offset + i;
3343
3344 memcpy(&data, buf + i, 4);
3345
3346 /*
3347 * The SEEPROM interface expects the data to always be opposite
3348 * the native endian format. We accomplish this by reversing
3349 * all the operations that would have been performed on the
3350 * data from a call to tg3_nvram_read_be32().
3351 */
3352 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3353
3354 val = tr32(GRC_EEPROM_ADDR);
3355 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3356
3357 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3358 EEPROM_ADDR_READ);
3359 tw32(GRC_EEPROM_ADDR, val |
3360 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3361 (addr & EEPROM_ADDR_ADDR_MASK) |
3362 EEPROM_ADDR_START |
3363 EEPROM_ADDR_WRITE);
3364
3365 for (j = 0; j < 1000; j++) {
3366 val = tr32(GRC_EEPROM_ADDR);
3367
3368 if (val & EEPROM_ADDR_COMPLETE)
3369 break;
3370 msleep(1);
3371 }
3372 if (!(val & EEPROM_ADDR_COMPLETE)) {
3373 rc = -EBUSY;
3374 break;
3375 }
3376 }
3377
3378 return rc;
3379}
3380
3381/* offset and length are dword aligned */
3382static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3383 u8 *buf)
3384{
3385 int ret = 0;
3386 u32 pagesize = tp->nvram_pagesize;
3387 u32 pagemask = pagesize - 1;
3388 u32 nvram_cmd;
3389 u8 *tmp;
3390
3391 tmp = kmalloc(pagesize, GFP_KERNEL);
3392 if (tmp == NULL)
3393 return -ENOMEM;
3394
3395 while (len) {
3396 int j;
3397 u32 phy_addr, page_off, size;
3398
3399 phy_addr = offset & ~pagemask;
3400
3401 for (j = 0; j < pagesize; j += 4) {
3402 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3403 (__be32 *) (tmp + j));
3404 if (ret)
3405 break;
3406 }
3407 if (ret)
3408 break;
3409
3410 page_off = offset & pagemask;
3411 size = pagesize;
3412 if (len < size)
3413 size = len;
3414
3415 len -= size;
3416
3417 memcpy(tmp + page_off, buf, size);
3418
3419 offset = offset + (pagesize - page_off);
3420
3421 tg3_enable_nvram_access(tp);
3422
3423 /*
3424 * Before we can erase the flash page, we need
3425 * to issue a special "write enable" command.
3426 */
3427 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3428
3429 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3430 break;
3431
3432 /* Erase the target page */
3433 tw32(NVRAM_ADDR, phy_addr);
3434
3435 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3436 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3437
3438 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3439 break;
3440
3441 /* Issue another write enable to start the write. */
3442 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3443
3444 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3445 break;
3446
3447 for (j = 0; j < pagesize; j += 4) {
3448 __be32 data;
3449
3450 data = *((__be32 *) (tmp + j));
3451
3452 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3453
3454 tw32(NVRAM_ADDR, phy_addr + j);
3455
3456 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3457 NVRAM_CMD_WR;
3458
3459 if (j == 0)
3460 nvram_cmd |= NVRAM_CMD_FIRST;
3461 else if (j == (pagesize - 4))
3462 nvram_cmd |= NVRAM_CMD_LAST;
3463
3464 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3465 if (ret)
3466 break;
3467 }
3468 if (ret)
3469 break;
3470 }
3471
3472 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3473 tg3_nvram_exec_cmd(tp, nvram_cmd);
3474
3475 kfree(tmp);
3476
3477 return ret;
3478}
3479
3480/* offset and length are dword aligned */
3481static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3482 u8 *buf)
3483{
3484 int i, ret = 0;
3485
3486 for (i = 0; i < len; i += 4, offset += 4) {
3487 u32 page_off, phy_addr, nvram_cmd;
3488 __be32 data;
3489
3490 memcpy(&data, buf + i, 4);
3491 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3492
3493 page_off = offset % tp->nvram_pagesize;
3494
3495 phy_addr = tg3_nvram_phys_addr(tp, offset);
3496
Matt Carlsondbe9b922012-02-13 10:20:09 +00003497 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3498
3499 if (page_off == 0 || i == 0)
3500 nvram_cmd |= NVRAM_CMD_FIRST;
3501 if (page_off == (tp->nvram_pagesize - 4))
3502 nvram_cmd |= NVRAM_CMD_LAST;
3503
3504 if (i == (len - 4))
3505 nvram_cmd |= NVRAM_CMD_LAST;
3506
Matt Carlson42278222012-02-13 15:20:11 +00003507 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3508 !tg3_flag(tp, FLASH) ||
3509 !tg3_flag(tp, 57765_PLUS))
3510 tw32(NVRAM_ADDR, phy_addr);
3511
Joe Perches41535772013-02-16 11:20:04 +00003512 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003513 !tg3_flag(tp, 5755_PLUS) &&
3514 (tp->nvram_jedecnum == JEDEC_ST) &&
3515 (nvram_cmd & NVRAM_CMD_FIRST)) {
3516 u32 cmd;
3517
3518 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3519 ret = tg3_nvram_exec_cmd(tp, cmd);
3520 if (ret)
3521 break;
3522 }
3523 if (!tg3_flag(tp, FLASH)) {
3524 /* We always do complete word writes to eeprom. */
3525 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3526 }
3527
3528 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3529 if (ret)
3530 break;
3531 }
3532 return ret;
3533}
3534
3535/* offset and length are dword aligned */
3536static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3537{
3538 int ret;
3539
3540 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3541 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3542 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3543 udelay(40);
3544 }
3545
3546 if (!tg3_flag(tp, NVRAM)) {
3547 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3548 } else {
3549 u32 grc_mode;
3550
3551 ret = tg3_nvram_lock(tp);
3552 if (ret)
3553 return ret;
3554
3555 tg3_enable_nvram_access(tp);
3556 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3557 tw32(NVRAM_WRITE1, 0x406);
3558
3559 grc_mode = tr32(GRC_MODE);
3560 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3561
3562 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3563 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3564 buf);
3565 } else {
3566 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3567 buf);
3568 }
3569
3570 grc_mode = tr32(GRC_MODE);
3571 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3572
3573 tg3_disable_nvram_access(tp);
3574 tg3_nvram_unlock(tp);
3575 }
3576
3577 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3578 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3579 udelay(40);
3580 }
3581
3582 return ret;
3583}
3584
Matt Carlson997b4f12011-08-31 11:44:53 +00003585#define RX_CPU_SCRATCH_BASE 0x30000
3586#define RX_CPU_SCRATCH_SIZE 0x04000
3587#define TX_CPU_SCRATCH_BASE 0x34000
3588#define TX_CPU_SCRATCH_SIZE 0x04000
3589
3590/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003591static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003592{
3593 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003594 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003595
Nithin Sujir837c45b2013-03-06 17:02:30 +00003596 for (i = 0; i < iters; i++) {
3597 tw32(cpu_base + CPU_STATE, 0xffffffff);
3598 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3599 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3600 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08003601 if (pci_channel_offline(tp->pdev))
3602 return -EBUSY;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003603 }
3604
3605 return (i == iters) ? -EBUSY : 0;
3606}
3607
3608/* tp->lock is held. */
3609static int tg3_rxcpu_pause(struct tg3 *tp)
3610{
3611 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3612
3613 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3614 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3615 udelay(10);
3616
3617 return rc;
3618}
3619
3620/* tp->lock is held. */
3621static int tg3_txcpu_pause(struct tg3 *tp)
3622{
3623 return tg3_pause_cpu(tp, TX_CPU_BASE);
3624}
3625
3626/* tp->lock is held. */
3627static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3628{
3629 tw32(cpu_base + CPU_STATE, 0xffffffff);
3630 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3631}
3632
3633/* tp->lock is held. */
3634static void tg3_rxcpu_resume(struct tg3 *tp)
3635{
3636 tg3_resume_cpu(tp, RX_CPU_BASE);
3637}
3638
3639/* tp->lock is held. */
3640static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3641{
3642 int rc;
3643
3644 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003645
Joe Perches41535772013-02-16 11:20:04 +00003646 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003647 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3648
3649 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3650 return 0;
3651 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003652 if (cpu_base == RX_CPU_BASE) {
3653 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003654 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003655 /*
3656 * There is only an Rx CPU for the 5750 derivative in the
3657 * BCM4785.
3658 */
3659 if (tg3_flag(tp, IS_SSB_CORE))
3660 return 0;
3661
Nithin Sujir837c45b2013-03-06 17:02:30 +00003662 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003663 }
3664
Nithin Sujir837c45b2013-03-06 17:02:30 +00003665 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003666 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003667 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003668 return -ENODEV;
3669 }
3670
3671 /* Clear firmware's nvram arbitration. */
3672 if (tg3_flag(tp, NVRAM))
3673 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3674 return 0;
3675}
3676
Nithin Sujir31f11a92013-03-06 17:02:33 +00003677static int tg3_fw_data_len(struct tg3 *tp,
3678 const struct tg3_firmware_hdr *fw_hdr)
3679{
3680 int fw_len;
3681
3682 /* Non fragmented firmware have one firmware header followed by a
3683 * contiguous chunk of data to be written. The length field in that
3684 * header is not the length of data to be written but the complete
3685 * length of the bss. The data length is determined based on
3686 * tp->fw->size minus headers.
3687 *
3688 * Fragmented firmware have a main header followed by multiple
3689 * fragments. Each fragment is identical to non fragmented firmware
3690 * with a firmware header followed by a contiguous chunk of data. In
3691 * the main header, the length field is unused and set to 0xffffffff.
3692 * In each fragment header the length is the entire size of that
3693 * fragment i.e. fragment data + header length. Data length is
3694 * therefore length field in the header minus TG3_FW_HDR_LEN.
3695 */
3696 if (tp->fw_len == 0xffffffff)
3697 fw_len = be32_to_cpu(fw_hdr->len);
3698 else
3699 fw_len = tp->fw->size;
3700
3701 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3702}
3703
Matt Carlson997b4f12011-08-31 11:44:53 +00003704/* tp->lock is held. */
3705static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3706 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003707 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003708{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003709 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003710 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003711 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003712
3713 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3714 netdev_err(tp->dev,
3715 "%s: Trying to load TX cpu firmware which is 5705\n",
3716 __func__);
3717 return -EINVAL;
3718 }
3719
Nithin Sujirc4dab502013-03-06 17:02:34 +00003720 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003721 write_op = tg3_write_mem;
3722 else
3723 write_op = tg3_write_indirect_reg32;
3724
Nithin Sujirc4dab502013-03-06 17:02:34 +00003725 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3726 /* It is possible that bootcode is still loading at this point.
3727 * Get the nvram lock first before halting the cpu.
3728 */
3729 int lock_err = tg3_nvram_lock(tp);
3730 err = tg3_halt_cpu(tp, cpu_base);
3731 if (!lock_err)
3732 tg3_nvram_unlock(tp);
3733 if (err)
3734 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003735
Nithin Sujirc4dab502013-03-06 17:02:34 +00003736 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3737 write_op(tp, cpu_scratch_base + i, 0);
3738 tw32(cpu_base + CPU_STATE, 0xffffffff);
3739 tw32(cpu_base + CPU_MODE,
3740 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3741 } else {
3742 /* Subtract additional main header for fragmented firmware and
3743 * advance to the first fragment
3744 */
3745 total_len -= TG3_FW_HDR_LEN;
3746 fw_hdr++;
3747 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003748
Nithin Sujir31f11a92013-03-06 17:02:33 +00003749 do {
3750 u32 *fw_data = (u32 *)(fw_hdr + 1);
3751 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3752 write_op(tp, cpu_scratch_base +
3753 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3754 (i * sizeof(u32)),
3755 be32_to_cpu(fw_data[i]));
3756
3757 total_len -= be32_to_cpu(fw_hdr->len);
3758
3759 /* Advance to next fragment */
3760 fw_hdr = (struct tg3_firmware_hdr *)
3761 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3762 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003763
3764 err = 0;
3765
3766out:
3767 return err;
3768}
3769
3770/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003771static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3772{
3773 int i;
3774 const int iters = 5;
3775
3776 tw32(cpu_base + CPU_STATE, 0xffffffff);
3777 tw32_f(cpu_base + CPU_PC, pc);
3778
3779 for (i = 0; i < iters; i++) {
3780 if (tr32(cpu_base + CPU_PC) == pc)
3781 break;
3782 tw32(cpu_base + CPU_STATE, 0xffffffff);
3783 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3784 tw32_f(cpu_base + CPU_PC, pc);
3785 udelay(1000);
3786 }
3787
3788 return (i == iters) ? -EBUSY : 0;
3789}
3790
3791/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003792static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3793{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003794 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003795 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003796
Nithin Sujir77997ea2013-03-06 17:02:32 +00003797 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003798
3799 /* Firmware blob starts with version numbers, followed by
3800 start address and length. We are setting complete length.
3801 length = end_address_of_bss - start_address_of_text.
3802 Remainder is the blob to be loaded contiguously
3803 from start address. */
3804
Matt Carlson997b4f12011-08-31 11:44:53 +00003805 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3806 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003807 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003808 if (err)
3809 return err;
3810
3811 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3812 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003813 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003814 if (err)
3815 return err;
3816
3817 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003818 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3819 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003820 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003821 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3822 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003823 tr32(RX_CPU_BASE + CPU_PC),
3824 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003825 return -ENODEV;
3826 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003827
3828 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003829
3830 return 0;
3831}
3832
Nithin Sujirc4dab502013-03-06 17:02:34 +00003833static int tg3_validate_rxcpu_state(struct tg3 *tp)
3834{
3835 const int iters = 1000;
3836 int i;
3837 u32 val;
3838
3839 /* Wait for boot code to complete initialization and enter service
3840 * loop. It is then safe to download service patches
3841 */
3842 for (i = 0; i < iters; i++) {
3843 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3844 break;
3845
3846 udelay(10);
3847 }
3848
3849 if (i == iters) {
3850 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3851 return -EBUSY;
3852 }
3853
3854 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3855 if (val & 0xff) {
3856 netdev_warn(tp->dev,
3857 "Other patches exist. Not downloading EEE patch\n");
3858 return -EEXIST;
3859 }
3860
3861 return 0;
3862}
3863
3864/* tp->lock is held. */
3865static void tg3_load_57766_firmware(struct tg3 *tp)
3866{
3867 struct tg3_firmware_hdr *fw_hdr;
3868
3869 if (!tg3_flag(tp, NO_NVRAM))
3870 return;
3871
3872 if (tg3_validate_rxcpu_state(tp))
3873 return;
3874
3875 if (!tp->fw)
3876 return;
3877
3878 /* This firmware blob has a different format than older firmware
3879 * releases as given below. The main difference is we have fragmented
3880 * data to be written to non-contiguous locations.
3881 *
3882 * In the beginning we have a firmware header identical to other
3883 * firmware which consists of version, base addr and length. The length
3884 * here is unused and set to 0xffffffff.
3885 *
3886 * This is followed by a series of firmware fragments which are
3887 * individually identical to previous firmware. i.e. they have the
3888 * firmware header and followed by data for that fragment. The version
3889 * field of the individual fragment header is unused.
3890 */
3891
3892 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3893 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3894 return;
3895
3896 if (tg3_rxcpu_pause(tp))
3897 return;
3898
3899 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3900 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3901
3902 tg3_rxcpu_resume(tp);
3903}
3904
Matt Carlson997b4f12011-08-31 11:44:53 +00003905/* tp->lock is held. */
3906static int tg3_load_tso_firmware(struct tg3 *tp)
3907{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003908 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003909 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003910 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003911
Matt Carlson1caf13e2013-03-06 17:02:29 +00003912 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003913 return 0;
3914
Nithin Sujir77997ea2013-03-06 17:02:32 +00003915 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003916
3917 /* Firmware blob starts with version numbers, followed by
3918 start address and length. We are setting complete length.
3919 length = end_address_of_bss - start_address_of_text.
3920 Remainder is the blob to be loaded contiguously
3921 from start address. */
3922
Matt Carlson997b4f12011-08-31 11:44:53 +00003923 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003924
Joe Perches41535772013-02-16 11:20:04 +00003925 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003926 cpu_base = RX_CPU_BASE;
3927 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3928 } else {
3929 cpu_base = TX_CPU_BASE;
3930 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3931 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3932 }
3933
3934 err = tg3_load_firmware_cpu(tp, cpu_base,
3935 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003936 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003937 if (err)
3938 return err;
3939
3940 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003941 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3942 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003943 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003944 netdev_err(tp->dev,
3945 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003946 __func__, tr32(cpu_base + CPU_PC),
3947 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003948 return -ENODEV;
3949 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003950
3951 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003952 return 0;
3953}
3954
Michael Chanf022ae62014-01-03 10:09:11 -08003955/* tp->lock is held. */
3956static void __tg3_set_one_mac_addr(struct tg3 *tp, u8 *mac_addr, int index)
3957{
3958 u32 addr_high, addr_low;
3959
3960 addr_high = ((mac_addr[0] << 8) | mac_addr[1]);
3961 addr_low = ((mac_addr[2] << 24) | (mac_addr[3] << 16) |
3962 (mac_addr[4] << 8) | mac_addr[5]);
3963
3964 if (index < 4) {
3965 tw32(MAC_ADDR_0_HIGH + (index * 8), addr_high);
3966 tw32(MAC_ADDR_0_LOW + (index * 8), addr_low);
3967 } else {
3968 index -= 4;
3969 tw32(MAC_EXTADDR_0_HIGH + (index * 8), addr_high);
3970 tw32(MAC_EXTADDR_0_LOW + (index * 8), addr_low);
3971 }
3972}
Matt Carlson997b4f12011-08-31 11:44:53 +00003973
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003974/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003975static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003976{
Michael Chanf022ae62014-01-03 10:09:11 -08003977 u32 addr_high;
Matt Carlson3f007892008-11-03 16:51:36 -08003978 int i;
3979
Matt Carlson3f007892008-11-03 16:51:36 -08003980 for (i = 0; i < 4; i++) {
3981 if (i == 1 && skip_mac_1)
3982 continue;
Michael Chanf022ae62014-01-03 10:09:11 -08003983 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003984 }
3985
Joe Perches41535772013-02-16 11:20:04 +00003986 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3987 tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chanf022ae62014-01-03 10:09:11 -08003988 for (i = 4; i < 16; i++)
3989 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003990 }
3991
3992 addr_high = (tp->dev->dev_addr[0] +
3993 tp->dev->dev_addr[1] +
3994 tp->dev->dev_addr[2] +
3995 tp->dev->dev_addr[3] +
3996 tp->dev->dev_addr[4] +
3997 tp->dev->dev_addr[5]) &
3998 TX_BACKOFF_SEED_MASK;
3999 tw32(MAC_TX_BACKOFF_SEED, addr_high);
4000}
4001
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004002static void tg3_enable_register_access(struct tg3 *tp)
4003{
4004 /*
4005 * Make sure register accesses (indirect or otherwise) will function
4006 * correctly.
4007 */
4008 pci_write_config_dword(tp->pdev,
4009 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
4010}
4011
4012static int tg3_power_up(struct tg3 *tp)
4013{
Matt Carlsonbed98292011-07-13 09:27:29 +00004014 int err;
4015
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004016 tg3_enable_register_access(tp);
4017
Matt Carlsonbed98292011-07-13 09:27:29 +00004018 err = pci_set_power_state(tp->pdev, PCI_D0);
4019 if (!err) {
4020 /* Switch out of Vaux if it is a NIC */
4021 tg3_pwrsrc_switch_to_vmain(tp);
4022 } else {
4023 netdev_err(tp->dev, "Transition to D0 failed\n");
4024 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004025
Matt Carlsonbed98292011-07-13 09:27:29 +00004026 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004027}
4028
Joe Perches953c96e2013-04-09 10:18:14 +00004029static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00004030
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004031static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032{
4033 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004034 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004036 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004037
4038 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004039 if (tg3_flag(tp, CLKREQ_BUG))
4040 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4041 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
4044 tw32(TG3PCI_MISC_HOST_CTRL,
4045 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
4046
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004047 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004048 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004049
Joe Perches63c3a662011-04-26 08:12:10 +00004050 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08004051 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004052 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00004053 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004054 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004055 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004056
Hauke Mehrtensead24022013-09-28 23:15:26 +02004057 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004058
Matt Carlson80096062010-08-02 11:26:06 +00004059 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004060
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004061 tp->link_config.speed = phydev->speed;
4062 tp->link_config.duplex = phydev->duplex;
4063 tp->link_config.autoneg = phydev->autoneg;
4064 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004065
4066 advertising = ADVERTISED_TP |
4067 ADVERTISED_Pause |
4068 ADVERTISED_Autoneg |
4069 ADVERTISED_10baseT_Half;
4070
Joe Perches63c3a662011-04-26 08:12:10 +00004071 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
4072 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004073 advertising |=
4074 ADVERTISED_100baseT_Half |
4075 ADVERTISED_100baseT_Full |
4076 ADVERTISED_10baseT_Full;
4077 else
4078 advertising |= ADVERTISED_10baseT_Full;
4079 }
4080
4081 phydev->advertising = advertising;
4082
4083 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004084
4085 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004086 if (phyid != PHY_ID_BCMAC131) {
4087 phyid &= PHY_BCM_OUI_MASK;
4088 if (phyid == PHY_BCM_OUI_1 ||
4089 phyid == PHY_BCM_OUI_2 ||
4090 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004091 do_low_power = true;
4092 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004093 }
Matt Carlsondd477002008-05-25 23:45:58 -07004094 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004095 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004096
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004097 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00004098 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Matt Carlson2855b9f2012-02-13 15:20:14 +00004100 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004101 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 }
4103
Joe Perches41535772013-02-16 11:20:04 +00004104 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004105 u32 val;
4106
4107 val = tr32(GRC_VCPU_EXT_CTRL);
4108 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004109 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004110 int i;
4111 u32 val;
4112
4113 for (i = 0; i < 200; i++) {
4114 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4115 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4116 break;
4117 msleep(1);
4118 }
4119 }
Joe Perches63c3a662011-04-26 08:12:10 +00004120 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004121 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4122 WOL_DRV_STATE_SHUTDOWN |
4123 WOL_DRV_WOL |
4124 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004125
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004126 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 u32 mac_mode;
4128
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004129 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004130 if (do_low_power &&
4131 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4132 tg3_phy_auxctl_write(tp,
4133 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4134 MII_TG3_AUXCTL_PCTL_WOL_EN |
4135 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4136 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004137 udelay(40);
4138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004140 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004141 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004142 else if (tp->phy_flags &
4143 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4144 if (tp->link_config.active_speed == SPEED_1000)
4145 mac_mode = MAC_MODE_PORT_MODE_GMII;
4146 else
4147 mac_mode = MAC_MODE_PORT_MODE_MII;
4148 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004149 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004151 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004152 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004153 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004154 SPEED_100 : SPEED_10;
4155 if (tg3_5700_link_polarity(tp, speed))
4156 mac_mode |= MAC_MODE_LINK_POLARITY;
4157 else
4158 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 } else {
4161 mac_mode = MAC_MODE_PORT_MODE_TBI;
4162 }
4163
Joe Perches63c3a662011-04-26 08:12:10 +00004164 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 tw32(MAC_LED_CTRL, tp->led_ctrl);
4166
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004167 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004168 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4169 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004170 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Joe Perches63c3a662011-04-26 08:12:10 +00004172 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004173 mac_mode |= MAC_MODE_APE_TX_EN |
4174 MAC_MODE_APE_RX_EN |
4175 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004176
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 tw32_f(MAC_MODE, mac_mode);
4178 udelay(100);
4179
4180 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4181 udelay(10);
4182 }
4183
Joe Perches63c3a662011-04-26 08:12:10 +00004184 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004185 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4186 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 u32 base_val;
4188
4189 base_val = tp->pci_clock_ctrl;
4190 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4191 CLOCK_CTRL_TXCLK_DISABLE);
4192
Michael Chanb401e9e2005-12-19 16:27:04 -08004193 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4194 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004195 } else if (tg3_flag(tp, 5780_CLASS) ||
4196 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004197 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004198 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004199 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 u32 newbits1, newbits2;
4201
Joe Perches41535772013-02-16 11:20:04 +00004202 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4203 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4205 CLOCK_CTRL_TXCLK_DISABLE |
4206 CLOCK_CTRL_ALTCLK);
4207 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004208 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 newbits1 = CLOCK_CTRL_625_CORE;
4210 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4211 } else {
4212 newbits1 = CLOCK_CTRL_ALTCLK;
4213 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4214 }
4215
Michael Chanb401e9e2005-12-19 16:27:04 -08004216 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4217 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Michael Chanb401e9e2005-12-19 16:27:04 -08004219 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4220 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Joe Perches63c3a662011-04-26 08:12:10 +00004222 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 u32 newbits3;
4224
Joe Perches41535772013-02-16 11:20:04 +00004225 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4226 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4228 CLOCK_CTRL_TXCLK_DISABLE |
4229 CLOCK_CTRL_44MHZ_CORE);
4230 } else {
4231 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4232 }
4233
Michael Chanb401e9e2005-12-19 16:27:04 -08004234 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4235 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 }
4237 }
4238
Joe Perches63c3a662011-04-26 08:12:10 +00004239 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004240 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004241
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004242 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004245 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004246 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4247 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 u32 val = tr32(0x7d00);
4249
4250 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4251 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004252 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004253 int err;
4254
4255 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004257 if (!err)
4258 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 }
4261
Michael Chanbbadf502006-04-06 21:46:34 -07004262 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4263
Nithin Sujir2e460fc2013-05-23 11:11:22 +00004264 tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
4265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return 0;
4267}
4268
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004269static void tg3_power_down(struct tg3 *tp)
4270{
Joe Perches63c3a662011-04-26 08:12:10 +00004271 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004272 pci_set_power_state(tp->pdev, PCI_D3hot);
4273}
4274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4276{
4277 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4278 case MII_TG3_AUX_STAT_10HALF:
4279 *speed = SPEED_10;
4280 *duplex = DUPLEX_HALF;
4281 break;
4282
4283 case MII_TG3_AUX_STAT_10FULL:
4284 *speed = SPEED_10;
4285 *duplex = DUPLEX_FULL;
4286 break;
4287
4288 case MII_TG3_AUX_STAT_100HALF:
4289 *speed = SPEED_100;
4290 *duplex = DUPLEX_HALF;
4291 break;
4292
4293 case MII_TG3_AUX_STAT_100FULL:
4294 *speed = SPEED_100;
4295 *duplex = DUPLEX_FULL;
4296 break;
4297
4298 case MII_TG3_AUX_STAT_1000HALF:
4299 *speed = SPEED_1000;
4300 *duplex = DUPLEX_HALF;
4301 break;
4302
4303 case MII_TG3_AUX_STAT_1000FULL:
4304 *speed = SPEED_1000;
4305 *duplex = DUPLEX_FULL;
4306 break;
4307
4308 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004309 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004310 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4311 SPEED_10;
4312 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4313 DUPLEX_HALF;
4314 break;
4315 }
Matt Carlsone7405222012-02-13 15:20:16 +00004316 *speed = SPEED_UNKNOWN;
4317 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320}
4321
Matt Carlson42b64a42011-05-19 12:12:49 +00004322static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
Matt Carlson42b64a42011-05-19 12:12:49 +00004324 int err = 0;
4325 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Matt Carlson42b64a42011-05-19 12:12:49 +00004327 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004328 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004329 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Matt Carlson42b64a42011-05-19 12:12:49 +00004331 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4332 if (err)
4333 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Matt Carlson4f272092011-12-14 11:09:57 +00004335 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4336 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004337
Joe Perches41535772013-02-16 11:20:04 +00004338 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4339 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004340 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004341
Matt Carlson4f272092011-12-14 11:09:57 +00004342 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4343 if (err)
4344 goto done;
4345 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004346
Matt Carlson42b64a42011-05-19 12:12:49 +00004347 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4348 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Matt Carlson42b64a42011-05-19 12:12:49 +00004350 tw32(TG3_CPMU_EEE_MODE,
4351 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004352
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004353 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004354 if (!err) {
4355 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004356
Matt Carlsona6b68da2010-12-06 08:28:52 +00004357 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004358 /* Advertise 100-BaseTX EEE ability */
4359 if (advertise & ADVERTISED_100baseT_Full)
4360 val |= MDIO_AN_EEE_ADV_100TX;
4361 /* Advertise 1000-BaseT EEE ability */
4362 if (advertise & ADVERTISED_1000baseT_Full)
4363 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004364
4365 if (!tp->eee.eee_enabled) {
4366 val = 0;
4367 tp->eee.advertised = 0;
4368 } else {
4369 tp->eee.advertised = advertise &
4370 (ADVERTISED_100baseT_Full |
4371 ADVERTISED_1000baseT_Full);
4372 }
4373
Matt Carlson42b64a42011-05-19 12:12:49 +00004374 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004375 if (err)
4376 val = 0;
4377
Joe Perches41535772013-02-16 11:20:04 +00004378 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004379 case ASIC_REV_5717:
4380 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004381 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004382 case ASIC_REV_5719:
4383 /* If we advertised any eee advertisements above... */
4384 if (val)
4385 val = MII_TG3_DSP_TAP26_ALNOKO |
4386 MII_TG3_DSP_TAP26_RMRXSTO |
4387 MII_TG3_DSP_TAP26_OPCSINPT;
4388 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4389 /* Fall through */
4390 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004391 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004392 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4393 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4394 MII_TG3_DSP_CH34TP2_HIBW01);
4395 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004396
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004397 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004398 if (!err)
4399 err = err2;
4400 }
4401
4402done:
4403 return err;
4404}
4405
4406static void tg3_phy_copper_begin(struct tg3 *tp)
4407{
Matt Carlsond13ba512012-02-22 12:35:19 +00004408 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4409 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4410 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004411
Nithin Sujir942d1af2013-04-09 08:48:07 +00004412 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4413 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004414 adv = ADVERTISED_10baseT_Half |
4415 ADVERTISED_10baseT_Full;
4416 if (tg3_flag(tp, WOL_SPEED_100MB))
4417 adv |= ADVERTISED_100baseT_Half |
4418 ADVERTISED_100baseT_Full;
Nithin Sujir7c786062013-12-06 09:53:17 -08004419 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK) {
4420 if (!(tp->phy_flags &
4421 TG3_PHYFLG_DISABLE_1G_HD_ADV))
4422 adv |= ADVERTISED_1000baseT_Half;
4423 adv |= ADVERTISED_1000baseT_Full;
4424 }
Matt Carlson42b64a42011-05-19 12:12:49 +00004425
Matt Carlsond13ba512012-02-22 12:35:19 +00004426 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004427 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004428 adv = tp->link_config.advertising;
4429 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4430 adv &= ~(ADVERTISED_1000baseT_Half |
4431 ADVERTISED_1000baseT_Full);
4432
4433 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004434 }
4435
Matt Carlsond13ba512012-02-22 12:35:19 +00004436 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004437
Nithin Sujir942d1af2013-04-09 08:48:07 +00004438 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4439 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4440 /* Normally during power down we want to autonegotiate
4441 * the lowest possible speed for WOL. However, to avoid
4442 * link flap, we leave it untouched.
4443 */
4444 return;
4445 }
4446
Matt Carlsond13ba512012-02-22 12:35:19 +00004447 tg3_writephy(tp, MII_BMCR,
4448 BMCR_ANENABLE | BMCR_ANRESTART);
4449 } else {
4450 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 u32 bmcr, orig_bmcr;
4452
4453 tp->link_config.active_speed = tp->link_config.speed;
4454 tp->link_config.active_duplex = tp->link_config.duplex;
4455
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004456 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4457 /* With autoneg disabled, 5715 only links up when the
4458 * advertisement register has the configured speed
4459 * enabled.
4460 */
4461 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4462 }
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 bmcr = 0;
4465 switch (tp->link_config.speed) {
4466 default:
4467 case SPEED_10:
4468 break;
4469
4470 case SPEED_100:
4471 bmcr |= BMCR_SPEED100;
4472 break;
4473
4474 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004475 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 if (tp->link_config.duplex == DUPLEX_FULL)
4480 bmcr |= BMCR_FULLDPLX;
4481
4482 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4483 (bmcr != orig_bmcr)) {
4484 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4485 for (i = 0; i < 1500; i++) {
4486 u32 tmp;
4487
4488 udelay(10);
4489 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4490 tg3_readphy(tp, MII_BMSR, &tmp))
4491 continue;
4492 if (!(tmp & BMSR_LSTATUS)) {
4493 udelay(40);
4494 break;
4495 }
4496 }
4497 tg3_writephy(tp, MII_BMCR, bmcr);
4498 udelay(40);
4499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
4501}
4502
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004503static int tg3_phy_pull_config(struct tg3 *tp)
4504{
4505 int err;
4506 u32 val;
4507
4508 err = tg3_readphy(tp, MII_BMCR, &val);
4509 if (err)
4510 goto done;
4511
4512 if (!(val & BMCR_ANENABLE)) {
4513 tp->link_config.autoneg = AUTONEG_DISABLE;
4514 tp->link_config.advertising = 0;
4515 tg3_flag_clear(tp, PAUSE_AUTONEG);
4516
4517 err = -EIO;
4518
4519 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4520 case 0:
4521 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4522 goto done;
4523
4524 tp->link_config.speed = SPEED_10;
4525 break;
4526 case BMCR_SPEED100:
4527 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4528 goto done;
4529
4530 tp->link_config.speed = SPEED_100;
4531 break;
4532 case BMCR_SPEED1000:
4533 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4534 tp->link_config.speed = SPEED_1000;
4535 break;
4536 }
4537 /* Fall through */
4538 default:
4539 goto done;
4540 }
4541
4542 if (val & BMCR_FULLDPLX)
4543 tp->link_config.duplex = DUPLEX_FULL;
4544 else
4545 tp->link_config.duplex = DUPLEX_HALF;
4546
4547 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4548
4549 err = 0;
4550 goto done;
4551 }
4552
4553 tp->link_config.autoneg = AUTONEG_ENABLE;
4554 tp->link_config.advertising = ADVERTISED_Autoneg;
4555 tg3_flag_set(tp, PAUSE_AUTONEG);
4556
4557 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4558 u32 adv;
4559
4560 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4561 if (err)
4562 goto done;
4563
4564 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4565 tp->link_config.advertising |= adv | ADVERTISED_TP;
4566
4567 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4568 } else {
4569 tp->link_config.advertising |= ADVERTISED_FIBRE;
4570 }
4571
4572 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4573 u32 adv;
4574
4575 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4576 err = tg3_readphy(tp, MII_CTRL1000, &val);
4577 if (err)
4578 goto done;
4579
4580 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4581 } else {
4582 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4583 if (err)
4584 goto done;
4585
4586 adv = tg3_decode_flowctrl_1000X(val);
4587 tp->link_config.flowctrl = adv;
4588
4589 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4590 adv = mii_adv_to_ethtool_adv_x(val);
4591 }
4592
4593 tp->link_config.advertising |= adv;
4594 }
4595
4596done:
4597 return err;
4598}
4599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600static int tg3_init_5401phy_dsp(struct tg3 *tp)
4601{
4602 int err;
4603
4604 /* Turn off tap power management. */
4605 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004606 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004608 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4609 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4610 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4611 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4612 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
4614 udelay(40);
4615
4616 return err;
4617}
4618
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004619static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4620{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004621 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004622
4623 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4624 return true;
4625
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004626 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004627
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004628 if (tp->eee.eee_enabled) {
4629 if (tp->eee.advertised != eee.advertised ||
4630 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4631 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4632 return false;
4633 } else {
4634 /* EEE is disabled but we're advertising */
4635 if (eee.advertised)
4636 return false;
4637 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004638
4639 return true;
4640}
4641
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004642static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004644 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004645
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004646 advertising = tp->link_config.advertising;
4647 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004649 advmsk = ADVERTISE_ALL;
4650 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004651 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004652 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004655 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4656 return false;
4657
4658 if ((*lcladv & advmsk) != tgtadv)
4659 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004660
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004661 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 u32 tg3_ctrl;
4663
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004664 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004665
Matt Carlson221c5632011-06-13 13:39:01 +00004666 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004667 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Matt Carlson3198e072012-02-13 15:20:10 +00004669 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004670 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4671 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004672 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4673 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4674 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4675 } else {
4676 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4677 }
4678
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004679 if (tg3_ctrl != tgtadv)
4680 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004682
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004683 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004684}
4685
Matt Carlson859edb22011-12-08 14:40:16 +00004686static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4687{
4688 u32 lpeth = 0;
4689
4690 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4691 u32 val;
4692
4693 if (tg3_readphy(tp, MII_STAT1000, &val))
4694 return false;
4695
4696 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4697 }
4698
4699 if (tg3_readphy(tp, MII_LPA, rmtadv))
4700 return false;
4701
4702 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4703 tp->link_config.rmt_adv = lpeth;
4704
4705 return true;
4706}
4707
Joe Perches953c96e2013-04-09 10:18:14 +00004708static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004709{
4710 if (curr_link_up != tp->link_up) {
4711 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004712 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004713 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004714 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004715 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4716 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4717 }
4718
4719 tg3_link_report(tp);
4720 return true;
4721 }
4722
4723 return false;
4724}
4725
Michael Chan3310e242013-04-09 08:48:05 +00004726static void tg3_clear_mac_status(struct tg3 *tp)
4727{
4728 tw32(MAC_EVENT, 0);
4729
4730 tw32_f(MAC_STATUS,
4731 MAC_STATUS_SYNC_CHANGED |
4732 MAC_STATUS_CFG_CHANGED |
4733 MAC_STATUS_MI_COMPLETION |
4734 MAC_STATUS_LNKSTATE_CHANGED);
4735 udelay(40);
4736}
4737
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004738static void tg3_setup_eee(struct tg3 *tp)
4739{
4740 u32 val;
4741
4742 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4743 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4744 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4745 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4746
4747 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4748
4749 tw32_f(TG3_CPMU_EEE_CTRL,
4750 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4751
4752 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4753 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4754 TG3_CPMU_EEEMD_LPI_IN_RX |
4755 TG3_CPMU_EEEMD_EEE_ENABLE;
4756
4757 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4758 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4759
4760 if (tg3_flag(tp, ENABLE_APE))
4761 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4762
4763 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4764
4765 tw32_f(TG3_CPMU_EEE_DBTMR1,
4766 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4767 (tp->eee.tx_lpi_timer & 0xffff));
4768
4769 tw32_f(TG3_CPMU_EEE_DBTMR2,
4770 TG3_CPMU_DBTMR2_APE_TX_2047US |
4771 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4772}
4773
Joe Perches953c96e2013-04-09 10:18:14 +00004774static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
Joe Perches953c96e2013-04-09 10:18:14 +00004776 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004777 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004778 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 u16 current_speed;
4780 u8 current_duplex;
4781 int i, err;
4782
Michael Chan3310e242013-04-09 08:48:05 +00004783 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
Matt Carlson8ef21422008-05-02 16:47:53 -07004785 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4786 tw32_f(MAC_MI_MODE,
4787 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4788 udelay(80);
4789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004791 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
4793 /* Some third-party PHYs need to be reset on link going
4794 * down.
4795 */
Joe Perches41535772013-02-16 11:20:04 +00004796 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4797 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4798 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004799 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 tg3_readphy(tp, MII_BMSR, &bmsr);
4801 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4802 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004803 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 }
4805 if (force_reset)
4806 tg3_phy_reset(tp);
4807
Matt Carlson79eb6902010-02-17 15:17:03 +00004808 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 tg3_readphy(tp, MII_BMSR, &bmsr);
4810 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004811 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 bmsr = 0;
4813
4814 if (!(bmsr & BMSR_LSTATUS)) {
4815 err = tg3_init_5401phy_dsp(tp);
4816 if (err)
4817 return err;
4818
4819 tg3_readphy(tp, MII_BMSR, &bmsr);
4820 for (i = 0; i < 1000; i++) {
4821 udelay(10);
4822 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4823 (bmsr & BMSR_LSTATUS)) {
4824 udelay(40);
4825 break;
4826 }
4827 }
4828
Matt Carlson79eb6902010-02-17 15:17:03 +00004829 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4830 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 !(bmsr & BMSR_LSTATUS) &&
4832 tp->link_config.active_speed == SPEED_1000) {
4833 err = tg3_phy_reset(tp);
4834 if (!err)
4835 err = tg3_init_5401phy_dsp(tp);
4836 if (err)
4837 return err;
4838 }
4839 }
Joe Perches41535772013-02-16 11:20:04 +00004840 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4841 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 /* 5701 {A0,B0} CRC bug workaround */
4843 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004844 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4845 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4846 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 }
4848
4849 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004850 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4851 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004853 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004855 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4857
Joe Perches41535772013-02-16 11:20:04 +00004858 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4859 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4861 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4862 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4863 else
4864 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4865 }
4866
Joe Perches953c96e2013-04-09 10:18:14 +00004867 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004868 current_speed = SPEED_UNKNOWN;
4869 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004870 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004871 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004873 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004874 err = tg3_phy_auxctl_read(tp,
4875 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4876 &val);
4877 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004878 tg3_phy_auxctl_write(tp,
4879 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4880 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 goto relink;
4882 }
4883 }
4884
4885 bmsr = 0;
4886 for (i = 0; i < 100; i++) {
4887 tg3_readphy(tp, MII_BMSR, &bmsr);
4888 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4889 (bmsr & BMSR_LSTATUS))
4890 break;
4891 udelay(40);
4892 }
4893
4894 if (bmsr & BMSR_LSTATUS) {
4895 u32 aux_stat, bmcr;
4896
4897 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4898 for (i = 0; i < 2000; i++) {
4899 udelay(10);
4900 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4901 aux_stat)
4902 break;
4903 }
4904
4905 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4906 &current_speed,
4907 &current_duplex);
4908
4909 bmcr = 0;
4910 for (i = 0; i < 200; i++) {
4911 tg3_readphy(tp, MII_BMCR, &bmcr);
4912 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4913 continue;
4914 if (bmcr && bmcr != 0x7fff)
4915 break;
4916 udelay(10);
4917 }
4918
Matt Carlsonef167e22007-12-20 20:10:01 -08004919 lcl_adv = 0;
4920 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Matt Carlsonef167e22007-12-20 20:10:01 -08004922 tp->link_config.active_speed = current_speed;
4923 tp->link_config.active_duplex = current_duplex;
4924
4925 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004926 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4927
Matt Carlsonef167e22007-12-20 20:10:01 -08004928 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004929 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004930 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004931 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004932 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004933
4934 /* EEE settings changes take effect only after a phy
4935 * reset. If we have skipped a reset due to Link Flap
4936 * Avoidance being enabled, do it now.
4937 */
4938 if (!eee_config_ok &&
4939 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004940 !force_reset) {
4941 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004942 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 } else {
4945 if (!(bmcr & BMCR_ANENABLE) &&
4946 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004947 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004948 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 }
4950 }
4951
Joe Perches953c96e2013-04-09 10:18:14 +00004952 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004953 tp->link_config.active_duplex == DUPLEX_FULL) {
4954 u32 reg, bit;
4955
4956 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4957 reg = MII_TG3_FET_GEN_STAT;
4958 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4959 } else {
4960 reg = MII_TG3_EXT_STAT;
4961 bit = MII_TG3_EXT_STAT_MDIX;
4962 }
4963
4964 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4965 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4966
Matt Carlsonef167e22007-12-20 20:10:01 -08004967 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 }
4970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004972 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 tg3_phy_copper_begin(tp);
4974
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004975 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004976 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004977 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4978 current_speed = SPEED_1000;
4979 current_duplex = DUPLEX_FULL;
4980 tp->link_config.active_speed = current_speed;
4981 tp->link_config.active_duplex = current_duplex;
4982 }
4983
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004984 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004985 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4986 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004987 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 }
4989
4990 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004991 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 if (tp->link_config.active_speed == SPEED_100 ||
4993 tp->link_config.active_speed == SPEED_10)
4994 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4995 else
4996 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004997 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004998 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4999 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5001
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00005002 /* In order for the 5750 core in BCM4785 chip to work properly
5003 * in RGMII mode, the Led Control Register must be set up.
5004 */
5005 if (tg3_flag(tp, RGMII_MODE)) {
5006 u32 led_ctrl = tr32(MAC_LED_CTRL);
5007 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
5008
5009 if (tp->link_config.active_speed == SPEED_10)
5010 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
5011 else if (tp->link_config.active_speed == SPEED_100)
5012 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5013 LED_CTRL_100MBPS_ON);
5014 else if (tp->link_config.active_speed == SPEED_1000)
5015 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5016 LED_CTRL_1000MBPS_ON);
5017
5018 tw32(MAC_LED_CTRL, led_ctrl);
5019 udelay(40);
5020 }
5021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5023 if (tp->link_config.active_duplex == DUPLEX_HALF)
5024 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5025
Joe Perches41535772013-02-16 11:20:04 +00005026 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00005027 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005028 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005030 else
5031 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 }
5033
5034 /* ??? Without this setting Netgear GA302T PHY does not
5035 * ??? send/receive packets...
5036 */
Matt Carlson79eb6902010-02-17 15:17:03 +00005037 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00005038 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
5040 tw32_f(MAC_MI_MODE, tp->mi_mode);
5041 udelay(80);
5042 }
5043
5044 tw32_f(MAC_MODE, tp->mac_mode);
5045 udelay(40);
5046
Matt Carlson52b02d02010-10-14 10:37:41 +00005047 tg3_phy_eee_adjust(tp, current_link_up);
5048
Joe Perches63c3a662011-04-26 08:12:10 +00005049 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 /* Polled via timer. */
5051 tw32_f(MAC_EVENT, 0);
5052 } else {
5053 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5054 }
5055 udelay(40);
5056
Joe Perches41535772013-02-16 11:20:04 +00005057 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00005058 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00005060 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 udelay(120);
5062 tw32_f(MAC_STATUS,
5063 (MAC_STATUS_SYNC_CHANGED |
5064 MAC_STATUS_CFG_CHANGED));
5065 udelay(40);
5066 tg3_write_mem(tp,
5067 NIC_SRAM_FIRMWARE_MBOX,
5068 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
5069 }
5070
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005071 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00005072 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005073 if (tp->link_config.active_speed == SPEED_100 ||
5074 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005075 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
5076 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005077 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005078 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5079 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005080 }
5081
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005082 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
5084 return 0;
5085}
5086
5087struct tg3_fiber_aneginfo {
5088 int state;
5089#define ANEG_STATE_UNKNOWN 0
5090#define ANEG_STATE_AN_ENABLE 1
5091#define ANEG_STATE_RESTART_INIT 2
5092#define ANEG_STATE_RESTART 3
5093#define ANEG_STATE_DISABLE_LINK_OK 4
5094#define ANEG_STATE_ABILITY_DETECT_INIT 5
5095#define ANEG_STATE_ABILITY_DETECT 6
5096#define ANEG_STATE_ACK_DETECT_INIT 7
5097#define ANEG_STATE_ACK_DETECT 8
5098#define ANEG_STATE_COMPLETE_ACK_INIT 9
5099#define ANEG_STATE_COMPLETE_ACK 10
5100#define ANEG_STATE_IDLE_DETECT_INIT 11
5101#define ANEG_STATE_IDLE_DETECT 12
5102#define ANEG_STATE_LINK_OK 13
5103#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5104#define ANEG_STATE_NEXT_PAGE_WAIT 15
5105
5106 u32 flags;
5107#define MR_AN_ENABLE 0x00000001
5108#define MR_RESTART_AN 0x00000002
5109#define MR_AN_COMPLETE 0x00000004
5110#define MR_PAGE_RX 0x00000008
5111#define MR_NP_LOADED 0x00000010
5112#define MR_TOGGLE_TX 0x00000020
5113#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5114#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5115#define MR_LP_ADV_SYM_PAUSE 0x00000100
5116#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5117#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5118#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5119#define MR_LP_ADV_NEXT_PAGE 0x00001000
5120#define MR_TOGGLE_RX 0x00002000
5121#define MR_NP_RX 0x00004000
5122
5123#define MR_LINK_OK 0x80000000
5124
5125 unsigned long link_time, cur_time;
5126
5127 u32 ability_match_cfg;
5128 int ability_match_count;
5129
5130 char ability_match, idle_match, ack_match;
5131
5132 u32 txconfig, rxconfig;
5133#define ANEG_CFG_NP 0x00000080
5134#define ANEG_CFG_ACK 0x00000040
5135#define ANEG_CFG_RF2 0x00000020
5136#define ANEG_CFG_RF1 0x00000010
5137#define ANEG_CFG_PS2 0x00000001
5138#define ANEG_CFG_PS1 0x00008000
5139#define ANEG_CFG_HD 0x00004000
5140#define ANEG_CFG_FD 0x00002000
5141#define ANEG_CFG_INVAL 0x00001f06
5142
5143};
5144#define ANEG_OK 0
5145#define ANEG_DONE 1
5146#define ANEG_TIMER_ENAB 2
5147#define ANEG_FAILED -1
5148
5149#define ANEG_STATE_SETTLE_TIME 10000
5150
5151static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5152 struct tg3_fiber_aneginfo *ap)
5153{
Matt Carlson5be73b42007-12-20 20:09:29 -08005154 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 unsigned long delta;
5156 u32 rx_cfg_reg;
5157 int ret;
5158
5159 if (ap->state == ANEG_STATE_UNKNOWN) {
5160 ap->rxconfig = 0;
5161 ap->link_time = 0;
5162 ap->cur_time = 0;
5163 ap->ability_match_cfg = 0;
5164 ap->ability_match_count = 0;
5165 ap->ability_match = 0;
5166 ap->idle_match = 0;
5167 ap->ack_match = 0;
5168 }
5169 ap->cur_time++;
5170
5171 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5172 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5173
5174 if (rx_cfg_reg != ap->ability_match_cfg) {
5175 ap->ability_match_cfg = rx_cfg_reg;
5176 ap->ability_match = 0;
5177 ap->ability_match_count = 0;
5178 } else {
5179 if (++ap->ability_match_count > 1) {
5180 ap->ability_match = 1;
5181 ap->ability_match_cfg = rx_cfg_reg;
5182 }
5183 }
5184 if (rx_cfg_reg & ANEG_CFG_ACK)
5185 ap->ack_match = 1;
5186 else
5187 ap->ack_match = 0;
5188
5189 ap->idle_match = 0;
5190 } else {
5191 ap->idle_match = 1;
5192 ap->ability_match_cfg = 0;
5193 ap->ability_match_count = 0;
5194 ap->ability_match = 0;
5195 ap->ack_match = 0;
5196
5197 rx_cfg_reg = 0;
5198 }
5199
5200 ap->rxconfig = rx_cfg_reg;
5201 ret = ANEG_OK;
5202
Matt Carlson33f401a2010-04-05 10:19:27 +00005203 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 case ANEG_STATE_UNKNOWN:
5205 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5206 ap->state = ANEG_STATE_AN_ENABLE;
5207
5208 /* fallthru */
5209 case ANEG_STATE_AN_ENABLE:
5210 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5211 if (ap->flags & MR_AN_ENABLE) {
5212 ap->link_time = 0;
5213 ap->cur_time = 0;
5214 ap->ability_match_cfg = 0;
5215 ap->ability_match_count = 0;
5216 ap->ability_match = 0;
5217 ap->idle_match = 0;
5218 ap->ack_match = 0;
5219
5220 ap->state = ANEG_STATE_RESTART_INIT;
5221 } else {
5222 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5223 }
5224 break;
5225
5226 case ANEG_STATE_RESTART_INIT:
5227 ap->link_time = ap->cur_time;
5228 ap->flags &= ~(MR_NP_LOADED);
5229 ap->txconfig = 0;
5230 tw32(MAC_TX_AUTO_NEG, 0);
5231 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5232 tw32_f(MAC_MODE, tp->mac_mode);
5233 udelay(40);
5234
5235 ret = ANEG_TIMER_ENAB;
5236 ap->state = ANEG_STATE_RESTART;
5237
5238 /* fallthru */
5239 case ANEG_STATE_RESTART:
5240 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005241 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005243 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 break;
5246
5247 case ANEG_STATE_DISABLE_LINK_OK:
5248 ret = ANEG_DONE;
5249 break;
5250
5251 case ANEG_STATE_ABILITY_DETECT_INIT:
5252 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005253 ap->txconfig = ANEG_CFG_FD;
5254 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5255 if (flowctrl & ADVERTISE_1000XPAUSE)
5256 ap->txconfig |= ANEG_CFG_PS1;
5257 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5258 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5260 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5261 tw32_f(MAC_MODE, tp->mac_mode);
5262 udelay(40);
5263
5264 ap->state = ANEG_STATE_ABILITY_DETECT;
5265 break;
5266
5267 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005268 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 break;
5271
5272 case ANEG_STATE_ACK_DETECT_INIT:
5273 ap->txconfig |= ANEG_CFG_ACK;
5274 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5275 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5276 tw32_f(MAC_MODE, tp->mac_mode);
5277 udelay(40);
5278
5279 ap->state = ANEG_STATE_ACK_DETECT;
5280
5281 /* fallthru */
5282 case ANEG_STATE_ACK_DETECT:
5283 if (ap->ack_match != 0) {
5284 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5285 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5286 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5287 } else {
5288 ap->state = ANEG_STATE_AN_ENABLE;
5289 }
5290 } else if (ap->ability_match != 0 &&
5291 ap->rxconfig == 0) {
5292 ap->state = ANEG_STATE_AN_ENABLE;
5293 }
5294 break;
5295
5296 case ANEG_STATE_COMPLETE_ACK_INIT:
5297 if (ap->rxconfig & ANEG_CFG_INVAL) {
5298 ret = ANEG_FAILED;
5299 break;
5300 }
5301 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5302 MR_LP_ADV_HALF_DUPLEX |
5303 MR_LP_ADV_SYM_PAUSE |
5304 MR_LP_ADV_ASYM_PAUSE |
5305 MR_LP_ADV_REMOTE_FAULT1 |
5306 MR_LP_ADV_REMOTE_FAULT2 |
5307 MR_LP_ADV_NEXT_PAGE |
5308 MR_TOGGLE_RX |
5309 MR_NP_RX);
5310 if (ap->rxconfig & ANEG_CFG_FD)
5311 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5312 if (ap->rxconfig & ANEG_CFG_HD)
5313 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5314 if (ap->rxconfig & ANEG_CFG_PS1)
5315 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5316 if (ap->rxconfig & ANEG_CFG_PS2)
5317 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5318 if (ap->rxconfig & ANEG_CFG_RF1)
5319 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5320 if (ap->rxconfig & ANEG_CFG_RF2)
5321 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5322 if (ap->rxconfig & ANEG_CFG_NP)
5323 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5324
5325 ap->link_time = ap->cur_time;
5326
5327 ap->flags ^= (MR_TOGGLE_TX);
5328 if (ap->rxconfig & 0x0008)
5329 ap->flags |= MR_TOGGLE_RX;
5330 if (ap->rxconfig & ANEG_CFG_NP)
5331 ap->flags |= MR_NP_RX;
5332 ap->flags |= MR_PAGE_RX;
5333
5334 ap->state = ANEG_STATE_COMPLETE_ACK;
5335 ret = ANEG_TIMER_ENAB;
5336 break;
5337
5338 case ANEG_STATE_COMPLETE_ACK:
5339 if (ap->ability_match != 0 &&
5340 ap->rxconfig == 0) {
5341 ap->state = ANEG_STATE_AN_ENABLE;
5342 break;
5343 }
5344 delta = ap->cur_time - ap->link_time;
5345 if (delta > ANEG_STATE_SETTLE_TIME) {
5346 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5347 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5348 } else {
5349 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5350 !(ap->flags & MR_NP_RX)) {
5351 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5352 } else {
5353 ret = ANEG_FAILED;
5354 }
5355 }
5356 }
5357 break;
5358
5359 case ANEG_STATE_IDLE_DETECT_INIT:
5360 ap->link_time = ap->cur_time;
5361 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5362 tw32_f(MAC_MODE, tp->mac_mode);
5363 udelay(40);
5364
5365 ap->state = ANEG_STATE_IDLE_DETECT;
5366 ret = ANEG_TIMER_ENAB;
5367 break;
5368
5369 case ANEG_STATE_IDLE_DETECT:
5370 if (ap->ability_match != 0 &&
5371 ap->rxconfig == 0) {
5372 ap->state = ANEG_STATE_AN_ENABLE;
5373 break;
5374 }
5375 delta = ap->cur_time - ap->link_time;
5376 if (delta > ANEG_STATE_SETTLE_TIME) {
5377 /* XXX another gem from the Broadcom driver :( */
5378 ap->state = ANEG_STATE_LINK_OK;
5379 }
5380 break;
5381
5382 case ANEG_STATE_LINK_OK:
5383 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5384 ret = ANEG_DONE;
5385 break;
5386
5387 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5388 /* ??? unimplemented */
5389 break;
5390
5391 case ANEG_STATE_NEXT_PAGE_WAIT:
5392 /* ??? unimplemented */
5393 break;
5394
5395 default:
5396 ret = ANEG_FAILED;
5397 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
5400 return ret;
5401}
5402
Matt Carlson5be73b42007-12-20 20:09:29 -08005403static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404{
5405 int res = 0;
5406 struct tg3_fiber_aneginfo aninfo;
5407 int status = ANEG_FAILED;
5408 unsigned int tick;
5409 u32 tmp;
5410
5411 tw32_f(MAC_TX_AUTO_NEG, 0);
5412
5413 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5414 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5415 udelay(40);
5416
5417 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5418 udelay(40);
5419
5420 memset(&aninfo, 0, sizeof(aninfo));
5421 aninfo.flags |= MR_AN_ENABLE;
5422 aninfo.state = ANEG_STATE_UNKNOWN;
5423 aninfo.cur_time = 0;
5424 tick = 0;
5425 while (++tick < 195000) {
5426 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5427 if (status == ANEG_DONE || status == ANEG_FAILED)
5428 break;
5429
5430 udelay(1);
5431 }
5432
5433 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5434 tw32_f(MAC_MODE, tp->mac_mode);
5435 udelay(40);
5436
Matt Carlson5be73b42007-12-20 20:09:29 -08005437 *txflags = aninfo.txconfig;
5438 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
5440 if (status == ANEG_DONE &&
5441 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5442 MR_LP_ADV_FULL_DUPLEX)))
5443 res = 1;
5444
5445 return res;
5446}
5447
5448static void tg3_init_bcm8002(struct tg3 *tp)
5449{
5450 u32 mac_status = tr32(MAC_STATUS);
5451 int i;
5452
5453 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005454 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 !(mac_status & MAC_STATUS_PCS_SYNCED))
5456 return;
5457
5458 /* Set PLL lock range. */
5459 tg3_writephy(tp, 0x16, 0x8007);
5460
5461 /* SW reset */
5462 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5463
5464 /* Wait for reset to complete. */
5465 /* XXX schedule_timeout() ... */
5466 for (i = 0; i < 500; i++)
5467 udelay(10);
5468
5469 /* Config mode; select PMA/Ch 1 regs. */
5470 tg3_writephy(tp, 0x10, 0x8411);
5471
5472 /* Enable auto-lock and comdet, select txclk for tx. */
5473 tg3_writephy(tp, 0x11, 0x0a10);
5474
5475 tg3_writephy(tp, 0x18, 0x00a0);
5476 tg3_writephy(tp, 0x16, 0x41ff);
5477
5478 /* Assert and deassert POR. */
5479 tg3_writephy(tp, 0x13, 0x0400);
5480 udelay(40);
5481 tg3_writephy(tp, 0x13, 0x0000);
5482
5483 tg3_writephy(tp, 0x11, 0x0a50);
5484 udelay(40);
5485 tg3_writephy(tp, 0x11, 0x0a10);
5486
5487 /* Wait for signal to stabilize */
5488 /* XXX schedule_timeout() ... */
5489 for (i = 0; i < 15000; i++)
5490 udelay(10);
5491
5492 /* Deselect the channel register so we can read the PHYID
5493 * later.
5494 */
5495 tg3_writephy(tp, 0x10, 0x8011);
5496}
5497
Joe Perches953c96e2013-04-09 10:18:14 +00005498static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005500 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005501 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 u32 sg_dig_ctrl, sg_dig_status;
5503 u32 serdes_cfg, expected_sg_dig_ctrl;
5504 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
5506 serdes_cfg = 0;
5507 expected_sg_dig_ctrl = 0;
5508 workaround = 0;
5509 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005510 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
Joe Perches41535772013-02-16 11:20:04 +00005512 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5513 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 workaround = 1;
5515 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5516 port_a = 0;
5517
5518 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5519 /* preserve bits 20-23 for voltage regulator */
5520 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5521 }
5522
5523 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5524
5525 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005526 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 if (workaround) {
5528 u32 val = serdes_cfg;
5529
5530 if (port_a)
5531 val |= 0xc010000;
5532 else
5533 val |= 0x4010000;
5534 tw32_f(MAC_SERDES_CFG, val);
5535 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005536
5537 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 }
5539 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5540 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005541 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 }
5543 goto out;
5544 }
5545
5546 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005547 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
Matt Carlson82cd3d12007-12-20 20:09:00 -08005549 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5550 if (flowctrl & ADVERTISE_1000XPAUSE)
5551 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5552 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5553 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
5555 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005556 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005557 tp->serdes_counter &&
5558 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5559 MAC_STATUS_RCVD_CFG)) ==
5560 MAC_STATUS_PCS_SYNCED)) {
5561 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005562 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005563 goto out;
5564 }
5565restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 if (workaround)
5567 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005568 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 udelay(5);
5570 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5571
Michael Chan3d3ebe72006-09-27 15:59:15 -07005572 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005573 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5575 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005576 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 mac_status = tr32(MAC_STATUS);
5578
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005579 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005581 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
Matt Carlson82cd3d12007-12-20 20:09:00 -08005583 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5584 local_adv |= ADVERTISE_1000XPAUSE;
5585 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5586 local_adv |= ADVERTISE_1000XPSE_ASYM;
5587
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005588 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005589 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005590 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005591 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Matt Carlson859edb22011-12-08 14:40:16 +00005593 tp->link_config.rmt_adv =
5594 mii_adv_to_ethtool_adv_x(remote_adv);
5595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005597 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005598 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005599 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005600 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005601 if (tp->serdes_counter)
5602 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 else {
5604 if (workaround) {
5605 u32 val = serdes_cfg;
5606
5607 if (port_a)
5608 val |= 0xc010000;
5609 else
5610 val |= 0x4010000;
5611
5612 tw32_f(MAC_SERDES_CFG, val);
5613 }
5614
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005615 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 udelay(40);
5617
5618 /* Link parallel detection - link is up */
5619 /* only if we have PCS_SYNC and not */
5620 /* receiving config code words */
5621 mac_status = tr32(MAC_STATUS);
5622 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5623 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5624 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005625 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005626 tp->phy_flags |=
5627 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005628 tp->serdes_counter =
5629 SERDES_PARALLEL_DET_TIMEOUT;
5630 } else
5631 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 }
5633 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005634 } else {
5635 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005636 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 }
5638
5639out:
5640 return current_link_up;
5641}
5642
Joe Perches953c96e2013-04-09 10:18:14 +00005643static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644{
Joe Perches953c96e2013-04-09 10:18:14 +00005645 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005647 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
5650 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005651 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005653
Matt Carlson5be73b42007-12-20 20:09:29 -08005654 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5655 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
Matt Carlson5be73b42007-12-20 20:09:29 -08005657 if (txflags & ANEG_CFG_PS1)
5658 local_adv |= ADVERTISE_1000XPAUSE;
5659 if (txflags & ANEG_CFG_PS2)
5660 local_adv |= ADVERTISE_1000XPSE_ASYM;
5661
5662 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5663 remote_adv |= LPA_1000XPAUSE;
5664 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5665 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
Matt Carlson859edb22011-12-08 14:40:16 +00005667 tp->link_config.rmt_adv =
5668 mii_adv_to_ethtool_adv_x(remote_adv);
5669
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 tg3_setup_flow_control(tp, local_adv, remote_adv);
5671
Joe Perches953c96e2013-04-09 10:18:14 +00005672 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 }
5674 for (i = 0; i < 30; i++) {
5675 udelay(20);
5676 tw32_f(MAC_STATUS,
5677 (MAC_STATUS_SYNC_CHANGED |
5678 MAC_STATUS_CFG_CHANGED));
5679 udelay(40);
5680 if ((tr32(MAC_STATUS) &
5681 (MAC_STATUS_SYNC_CHANGED |
5682 MAC_STATUS_CFG_CHANGED)) == 0)
5683 break;
5684 }
5685
5686 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005687 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5689 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005690 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005692 tg3_setup_flow_control(tp, 0, 0);
5693
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005695 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5698 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005699
5700 tw32_f(MAC_MODE, tp->mac_mode);
5701 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 }
5703
5704out:
5705 return current_link_up;
5706}
5707
Joe Perches953c96e2013-04-09 10:18:14 +00005708static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709{
5710 u32 orig_pause_cfg;
5711 u16 orig_active_speed;
5712 u8 orig_active_duplex;
5713 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005714 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 int i;
5716
Matt Carlson8d018622007-12-20 20:05:44 -08005717 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 orig_active_speed = tp->link_config.active_speed;
5719 orig_active_duplex = tp->link_config.active_duplex;
5720
Joe Perches63c3a662011-04-26 08:12:10 +00005721 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005722 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005723 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 mac_status = tr32(MAC_STATUS);
5725 mac_status &= (MAC_STATUS_PCS_SYNCED |
5726 MAC_STATUS_SIGNAL_DET |
5727 MAC_STATUS_CFG_CHANGED |
5728 MAC_STATUS_RCVD_CFG);
5729 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5730 MAC_STATUS_SIGNAL_DET)) {
5731 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5732 MAC_STATUS_CFG_CHANGED));
5733 return 0;
5734 }
5735 }
5736
5737 tw32_f(MAC_TX_AUTO_NEG, 0);
5738
5739 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5740 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5741 tw32_f(MAC_MODE, tp->mac_mode);
5742 udelay(40);
5743
Matt Carlson79eb6902010-02-17 15:17:03 +00005744 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 tg3_init_bcm8002(tp);
5746
5747 /* Enable link change event even when serdes polling. */
5748 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5749 udelay(40);
5750
Joe Perches953c96e2013-04-09 10:18:14 +00005751 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005752 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 mac_status = tr32(MAC_STATUS);
5754
Joe Perches63c3a662011-04-26 08:12:10 +00005755 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5757 else
5758 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5759
Matt Carlson898a56f2009-08-28 14:02:40 +00005760 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005762 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763
5764 for (i = 0; i < 100; i++) {
5765 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5766 MAC_STATUS_CFG_CHANGED));
5767 udelay(5);
5768 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005769 MAC_STATUS_CFG_CHANGED |
5770 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 break;
5772 }
5773
5774 mac_status = tr32(MAC_STATUS);
5775 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005776 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005777 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5778 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 tw32_f(MAC_MODE, (tp->mac_mode |
5780 MAC_MODE_SEND_CONFIGS));
5781 udelay(1);
5782 tw32_f(MAC_MODE, tp->mac_mode);
5783 }
5784 }
5785
Joe Perches953c96e2013-04-09 10:18:14 +00005786 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 tp->link_config.active_speed = SPEED_1000;
5788 tp->link_config.active_duplex = DUPLEX_FULL;
5789 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5790 LED_CTRL_LNKLED_OVERRIDE |
5791 LED_CTRL_1000MBPS_ON));
5792 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005793 tp->link_config.active_speed = SPEED_UNKNOWN;
5794 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5796 LED_CTRL_LNKLED_OVERRIDE |
5797 LED_CTRL_TRAFFIC_OVERRIDE));
5798 }
5799
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005800 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005801 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 if (orig_pause_cfg != now_pause_cfg ||
5803 orig_active_speed != tp->link_config.active_speed ||
5804 orig_active_duplex != tp->link_config.active_duplex)
5805 tg3_link_report(tp);
5806 }
5807
5808 return 0;
5809}
5810
Joe Perches953c96e2013-04-09 10:18:14 +00005811static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005812{
Joe Perches953c96e2013-04-09 10:18:14 +00005813 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005814 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005815 u16 current_speed = SPEED_UNKNOWN;
5816 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005817 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005818 u32 local_adv, remote_adv, sgsr;
5819
5820 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5821 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5822 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5823 (sgsr & SERDES_TG3_SGMII_MODE)) {
5824
5825 if (force_reset)
5826 tg3_phy_reset(tp);
5827
5828 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5829
5830 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5831 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5832 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005833 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005834 if (sgsr & SERDES_TG3_SPEED_1000) {
5835 current_speed = SPEED_1000;
5836 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5837 } else if (sgsr & SERDES_TG3_SPEED_100) {
5838 current_speed = SPEED_100;
5839 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5840 } else {
5841 current_speed = SPEED_10;
5842 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5843 }
5844
5845 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5846 current_duplex = DUPLEX_FULL;
5847 else
5848 current_duplex = DUPLEX_HALF;
5849 }
5850
5851 tw32_f(MAC_MODE, tp->mac_mode);
5852 udelay(40);
5853
5854 tg3_clear_mac_status(tp);
5855
5856 goto fiber_setup_done;
5857 }
Michael Chan747e8f82005-07-25 12:33:22 -07005858
5859 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5860 tw32_f(MAC_MODE, tp->mac_mode);
5861 udelay(40);
5862
Michael Chan3310e242013-04-09 08:48:05 +00005863 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005864
5865 if (force_reset)
5866 tg3_phy_reset(tp);
5867
Matt Carlson859edb22011-12-08 14:40:16 +00005868 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005869
5870 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5871 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005872 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005873 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5874 bmsr |= BMSR_LSTATUS;
5875 else
5876 bmsr &= ~BMSR_LSTATUS;
5877 }
Michael Chan747e8f82005-07-25 12:33:22 -07005878
5879 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5880
5881 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005882 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005883 /* do nothing, just check for link up at the end */
5884 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005885 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005886
5887 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005888 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5889 ADVERTISE_1000XPAUSE |
5890 ADVERTISE_1000XPSE_ASYM |
5891 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005892
Matt Carlson28011cf2011-11-16 18:36:59 -05005893 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005894 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005895
Matt Carlson28011cf2011-11-16 18:36:59 -05005896 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5897 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005898 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5899 tg3_writephy(tp, MII_BMCR, bmcr);
5900
5901 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005902 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005903 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005904
5905 return err;
5906 }
5907 } else {
5908 u32 new_bmcr;
5909
5910 bmcr &= ~BMCR_SPEED1000;
5911 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5912
5913 if (tp->link_config.duplex == DUPLEX_FULL)
5914 new_bmcr |= BMCR_FULLDPLX;
5915
5916 if (new_bmcr != bmcr) {
5917 /* BMCR_SPEED1000 is a reserved bit that needs
5918 * to be set on write.
5919 */
5920 new_bmcr |= BMCR_SPEED1000;
5921
5922 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005923 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005924 u32 adv;
5925
5926 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5927 adv &= ~(ADVERTISE_1000XFULL |
5928 ADVERTISE_1000XHALF |
5929 ADVERTISE_SLCT);
5930 tg3_writephy(tp, MII_ADVERTISE, adv);
5931 tg3_writephy(tp, MII_BMCR, bmcr |
5932 BMCR_ANRESTART |
5933 BMCR_ANENABLE);
5934 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005935 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005936 }
5937 tg3_writephy(tp, MII_BMCR, new_bmcr);
5938 bmcr = new_bmcr;
5939 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5940 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005941 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005942 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5943 bmsr |= BMSR_LSTATUS;
5944 else
5945 bmsr &= ~BMSR_LSTATUS;
5946 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005947 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005948 }
5949 }
5950
5951 if (bmsr & BMSR_LSTATUS) {
5952 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005953 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005954 if (bmcr & BMCR_FULLDPLX)
5955 current_duplex = DUPLEX_FULL;
5956 else
5957 current_duplex = DUPLEX_HALF;
5958
Matt Carlsonef167e22007-12-20 20:10:01 -08005959 local_adv = 0;
5960 remote_adv = 0;
5961
Michael Chan747e8f82005-07-25 12:33:22 -07005962 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005963 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005964
5965 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5966 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5967 common = local_adv & remote_adv;
5968 if (common & (ADVERTISE_1000XHALF |
5969 ADVERTISE_1000XFULL)) {
5970 if (common & ADVERTISE_1000XFULL)
5971 current_duplex = DUPLEX_FULL;
5972 else
5973 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005974
5975 tp->link_config.rmt_adv =
5976 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005977 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005978 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005979 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005980 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005981 }
Michael Chan747e8f82005-07-25 12:33:22 -07005982 }
5983 }
5984
Michael Chan85730a62013-04-09 08:48:06 +00005985fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005986 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005987 tg3_setup_flow_control(tp, local_adv, remote_adv);
5988
Michael Chan747e8f82005-07-25 12:33:22 -07005989 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5990 if (tp->link_config.active_duplex == DUPLEX_HALF)
5991 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5992
5993 tw32_f(MAC_MODE, tp->mac_mode);
5994 udelay(40);
5995
5996 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5997
5998 tp->link_config.active_speed = current_speed;
5999 tp->link_config.active_duplex = current_duplex;
6000
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006001 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07006002 return err;
6003}
6004
6005static void tg3_serdes_parallel_detect(struct tg3 *tp)
6006{
Michael Chan3d3ebe72006-09-27 15:59:15 -07006007 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07006008 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07006009 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07006010 return;
6011 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006012
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006013 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07006014 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
6015 u32 bmcr;
6016
6017 tg3_readphy(tp, MII_BMCR, &bmcr);
6018 if (bmcr & BMCR_ANENABLE) {
6019 u32 phy1, phy2;
6020
6021 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006022 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
6023 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07006024
6025 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006026 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6027 MII_TG3_DSP_EXP1_INT_STAT);
6028 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
6029 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006030
6031 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
6032 /* We have signal detect and not receiving
6033 * config code words, link is up by parallel
6034 * detection.
6035 */
6036
6037 bmcr &= ~BMCR_ANENABLE;
6038 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
6039 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006040 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006041 }
6042 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006043 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00006044 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006045 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07006046 u32 phy2;
6047
6048 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006049 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6050 MII_TG3_DSP_EXP1_INT_STAT);
6051 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006052 if (phy2 & 0x20) {
6053 u32 bmcr;
6054
6055 /* Config code words received, turn on autoneg. */
6056 tg3_readphy(tp, MII_BMCR, &bmcr);
6057 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
6058
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006059 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006060
6061 }
6062 }
6063}
6064
Joe Perches953c96e2013-04-09 10:18:14 +00006065static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066{
Matt Carlsonf2096f92011-04-05 14:22:48 +00006067 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 int err;
6069
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006070 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006072 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07006073 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00006074 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
Joe Perches41535772013-02-16 11:20:04 +00006077 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006078 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006079
6080 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6081 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6082 scale = 65;
6083 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6084 scale = 6;
6085 else
6086 scale = 12;
6087
6088 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6089 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6090 tw32(GRC_MISC_CFG, val);
6091 }
6092
Matt Carlsonf2096f92011-04-05 14:22:48 +00006093 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6094 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006095 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6096 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006097 val |= tr32(MAC_TX_LENGTHS) &
6098 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6099 TX_LENGTHS_CNT_DWN_VAL_MSK);
6100
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 if (tp->link_config.active_speed == SPEED_1000 &&
6102 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006103 tw32(MAC_TX_LENGTHS, val |
6104 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006106 tw32(MAC_TX_LENGTHS, val |
6107 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
Joe Perches63c3a662011-04-26 08:12:10 +00006109 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006110 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006112 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 } else {
6114 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6115 }
6116 }
6117
Joe Perches63c3a662011-04-26 08:12:10 +00006118 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006119 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006120 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006121 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6122 tp->pwrmgmt_thresh;
6123 else
6124 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6125 tw32(PCIE_PWR_MGMT_THRESH, val);
6126 }
6127
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 return err;
6129}
6130
Matt Carlsonbe947302012-12-03 19:36:57 +00006131/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006132static u64 tg3_refclk_read(struct tg3 *tp)
6133{
6134 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6135 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6136}
6137
6138/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006139static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6140{
Nithin Sujir92e64572013-07-29 13:58:38 -07006141 u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6142
6143 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
Matt Carlsonbe947302012-12-03 19:36:57 +00006144 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6145 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
Nithin Sujir92e64572013-07-29 13:58:38 -07006146 tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
Matt Carlsonbe947302012-12-03 19:36:57 +00006147}
6148
Matt Carlson7d41e492012-12-03 19:36:58 +00006149static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6150static inline void tg3_full_unlock(struct tg3 *tp);
6151static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6152{
6153 struct tg3 *tp = netdev_priv(dev);
6154
6155 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6156 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006157 SOF_TIMESTAMPING_SOFTWARE;
6158
6159 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006160 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006161 SOF_TIMESTAMPING_RX_HARDWARE |
6162 SOF_TIMESTAMPING_RAW_HARDWARE;
6163 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006164
6165 if (tp->ptp_clock)
6166 info->phc_index = ptp_clock_index(tp->ptp_clock);
6167 else
6168 info->phc_index = -1;
6169
6170 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6171
6172 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6173 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6174 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6175 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6176 return 0;
6177}
6178
6179static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6180{
6181 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6182 bool neg_adj = false;
6183 u32 correction = 0;
6184
6185 if (ppb < 0) {
6186 neg_adj = true;
6187 ppb = -ppb;
6188 }
6189
6190 /* Frequency adjustment is performed using hardware with a 24 bit
6191 * accumulator and a programmable correction value. On each clk, the
6192 * correction value gets added to the accumulator and when it
6193 * overflows, the time counter is incremented/decremented.
6194 *
6195 * So conversion from ppb to correction value is
6196 * ppb * (1 << 24) / 1000000000
6197 */
6198 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6199 TG3_EAV_REF_CLK_CORRECT_MASK;
6200
6201 tg3_full_lock(tp, 0);
6202
6203 if (correction)
6204 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6205 TG3_EAV_REF_CLK_CORRECT_EN |
6206 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6207 else
6208 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6209
6210 tg3_full_unlock(tp);
6211
6212 return 0;
6213}
6214
6215static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6216{
6217 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6218
6219 tg3_full_lock(tp, 0);
6220 tp->ptp_adjust += delta;
6221 tg3_full_unlock(tp);
6222
6223 return 0;
6224}
6225
6226static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
6227{
6228 u64 ns;
6229 u32 remainder;
6230 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6231
6232 tg3_full_lock(tp, 0);
6233 ns = tg3_refclk_read(tp);
6234 ns += tp->ptp_adjust;
6235 tg3_full_unlock(tp);
6236
6237 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
6238 ts->tv_nsec = remainder;
6239
6240 return 0;
6241}
6242
6243static int tg3_ptp_settime(struct ptp_clock_info *ptp,
6244 const struct timespec *ts)
6245{
6246 u64 ns;
6247 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6248
6249 ns = timespec_to_ns(ts);
6250
6251 tg3_full_lock(tp, 0);
6252 tg3_refclk_write(tp, ns);
6253 tp->ptp_adjust = 0;
6254 tg3_full_unlock(tp);
6255
6256 return 0;
6257}
6258
6259static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6260 struct ptp_clock_request *rq, int on)
6261{
Nithin Sujir92e64572013-07-29 13:58:38 -07006262 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6263 u32 clock_ctl;
6264 int rval = 0;
6265
6266 switch (rq->type) {
6267 case PTP_CLK_REQ_PEROUT:
6268 if (rq->perout.index != 0)
6269 return -EINVAL;
6270
6271 tg3_full_lock(tp, 0);
6272 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6273 clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
6274
6275 if (on) {
6276 u64 nsec;
6277
6278 nsec = rq->perout.start.sec * 1000000000ULL +
6279 rq->perout.start.nsec;
6280
6281 if (rq->perout.period.sec || rq->perout.period.nsec) {
6282 netdev_warn(tp->dev,
6283 "Device supports only a one-shot timesync output, period must be 0\n");
6284 rval = -EINVAL;
6285 goto err_out;
6286 }
6287
6288 if (nsec & (1ULL << 63)) {
6289 netdev_warn(tp->dev,
6290 "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
6291 rval = -EINVAL;
6292 goto err_out;
6293 }
6294
6295 tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
6296 tw32(TG3_EAV_WATCHDOG0_MSB,
6297 TG3_EAV_WATCHDOG0_EN |
6298 ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
6299
6300 tw32(TG3_EAV_REF_CLCK_CTL,
6301 clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
6302 } else {
6303 tw32(TG3_EAV_WATCHDOG0_MSB, 0);
6304 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
6305 }
6306
6307err_out:
6308 tg3_full_unlock(tp);
6309 return rval;
6310
6311 default:
6312 break;
6313 }
6314
Matt Carlson7d41e492012-12-03 19:36:58 +00006315 return -EOPNOTSUPP;
6316}
6317
6318static const struct ptp_clock_info tg3_ptp_caps = {
6319 .owner = THIS_MODULE,
6320 .name = "tg3 clock",
6321 .max_adj = 250000000,
6322 .n_alarm = 0,
6323 .n_ext_ts = 0,
Nithin Sujir92e64572013-07-29 13:58:38 -07006324 .n_per_out = 1,
Matt Carlson7d41e492012-12-03 19:36:58 +00006325 .pps = 0,
6326 .adjfreq = tg3_ptp_adjfreq,
6327 .adjtime = tg3_ptp_adjtime,
6328 .gettime = tg3_ptp_gettime,
6329 .settime = tg3_ptp_settime,
6330 .enable = tg3_ptp_enable,
6331};
6332
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006333static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6334 struct skb_shared_hwtstamps *timestamp)
6335{
6336 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6337 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6338 tp->ptp_adjust);
6339}
6340
Matt Carlsonbe947302012-12-03 19:36:57 +00006341/* tp->lock must be held */
6342static void tg3_ptp_init(struct tg3 *tp)
6343{
6344 if (!tg3_flag(tp, PTP_CAPABLE))
6345 return;
6346
6347 /* Initialize the hardware clock to the system time. */
6348 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6349 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006350 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006351}
6352
6353/* tp->lock must be held */
6354static void tg3_ptp_resume(struct tg3 *tp)
6355{
6356 if (!tg3_flag(tp, PTP_CAPABLE))
6357 return;
6358
6359 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6360 tp->ptp_adjust = 0;
6361}
6362
6363static void tg3_ptp_fini(struct tg3 *tp)
6364{
6365 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6366 return;
6367
Matt Carlson7d41e492012-12-03 19:36:58 +00006368 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006369 tp->ptp_clock = NULL;
6370 tp->ptp_adjust = 0;
6371}
6372
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006373static inline int tg3_irq_sync(struct tg3 *tp)
6374{
6375 return tp->irq_sync;
6376}
6377
Matt Carlson97bd8e42011-04-13 11:05:04 +00006378static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6379{
6380 int i;
6381
6382 dst = (u32 *)((u8 *)dst + off);
6383 for (i = 0; i < len; i += sizeof(u32))
6384 *dst++ = tr32(off + i);
6385}
6386
6387static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6388{
6389 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6390 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6391 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6392 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6393 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6394 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6395 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6396 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6397 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6398 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6399 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6400 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6401 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6402 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6403 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6404 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6405 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6406 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6407 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6408
Joe Perches63c3a662011-04-26 08:12:10 +00006409 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006410 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6411
6412 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6413 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6414 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6415 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6416 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6417 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6418 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6419 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6420
Joe Perches63c3a662011-04-26 08:12:10 +00006421 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006422 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6423 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6424 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6425 }
6426
6427 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6428 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6429 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6430 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6431 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6432
Joe Perches63c3a662011-04-26 08:12:10 +00006433 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006434 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6435}
6436
6437static void tg3_dump_state(struct tg3 *tp)
6438{
6439 int i;
6440 u32 *regs;
6441
6442 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006443 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006444 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006445
Joe Perches63c3a662011-04-26 08:12:10 +00006446 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006447 /* Read up to but not including private PCI registers */
6448 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6449 regs[i / sizeof(u32)] = tr32(i);
6450 } else
6451 tg3_dump_legacy_regs(tp, regs);
6452
6453 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6454 if (!regs[i + 0] && !regs[i + 1] &&
6455 !regs[i + 2] && !regs[i + 3])
6456 continue;
6457
6458 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6459 i * 4,
6460 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6461 }
6462
6463 kfree(regs);
6464
6465 for (i = 0; i < tp->irq_cnt; i++) {
6466 struct tg3_napi *tnapi = &tp->napi[i];
6467
6468 /* SW status block */
6469 netdev_err(tp->dev,
6470 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6471 i,
6472 tnapi->hw_status->status,
6473 tnapi->hw_status->status_tag,
6474 tnapi->hw_status->rx_jumbo_consumer,
6475 tnapi->hw_status->rx_consumer,
6476 tnapi->hw_status->rx_mini_consumer,
6477 tnapi->hw_status->idx[0].rx_producer,
6478 tnapi->hw_status->idx[0].tx_consumer);
6479
6480 netdev_err(tp->dev,
6481 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6482 i,
6483 tnapi->last_tag, tnapi->last_irq_tag,
6484 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6485 tnapi->rx_rcb_ptr,
6486 tnapi->prodring.rx_std_prod_idx,
6487 tnapi->prodring.rx_std_cons_idx,
6488 tnapi->prodring.rx_jmb_prod_idx,
6489 tnapi->prodring.rx_jmb_cons_idx);
6490 }
6491}
6492
Michael Chandf3e6542006-05-26 17:48:07 -07006493/* This is called whenever we suspect that the system chipset is re-
6494 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6495 * is bogus tx completions. We try to recover by setting the
6496 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6497 * in the workqueue.
6498 */
6499static void tg3_tx_recover(struct tg3 *tp)
6500{
Joe Perches63c3a662011-04-26 08:12:10 +00006501 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006502 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6503
Matt Carlson5129c3a2010-04-05 10:19:23 +00006504 netdev_warn(tp->dev,
6505 "The system may be re-ordering memory-mapped I/O "
6506 "cycles to the network device, attempting to recover. "
6507 "Please report the problem to the driver maintainer "
6508 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006509
Joe Perches63c3a662011-04-26 08:12:10 +00006510 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006511}
6512
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006513static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006514{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006515 /* Tell compiler to fetch tx indices from memory. */
6516 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006517 return tnapi->tx_pending -
6518 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006519}
6520
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521/* Tigon3 never reports partial packet sends. So we do not
6522 * need special logic to handle SKBs that have not had all
6523 * of their frags sent yet, like SunGEM does.
6524 */
Matt Carlson17375d22009-08-28 14:02:18 +00006525static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526{
Matt Carlson17375d22009-08-28 14:02:18 +00006527 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006528 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006529 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006530 struct netdev_queue *txq;
6531 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006532 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006533
Joe Perches63c3a662011-04-26 08:12:10 +00006534 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006535 index--;
6536
6537 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538
6539 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006540 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006542 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543
Michael Chandf3e6542006-05-26 17:48:07 -07006544 if (unlikely(skb == NULL)) {
6545 tg3_tx_recover(tp);
6546 return;
6547 }
6548
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006549 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6550 struct skb_shared_hwtstamps timestamp;
6551 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6552 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6553
6554 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6555
6556 skb_tstamp_tx(skb, &timestamp);
6557 }
6558
Alexander Duyckf4188d82009-12-02 16:48:38 +00006559 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006560 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006561 skb_headlen(skb),
6562 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563
6564 ri->skb = NULL;
6565
Matt Carlsone01ee142011-07-27 14:20:50 +00006566 while (ri->fragmented) {
6567 ri->fragmented = false;
6568 sw_idx = NEXT_TX(sw_idx);
6569 ri = &tnapi->tx_buffers[sw_idx];
6570 }
6571
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 sw_idx = NEXT_TX(sw_idx);
6573
6574 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006575 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006576 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6577 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006578
6579 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006580 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006581 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006582 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006583
6584 while (ri->fragmented) {
6585 ri->fragmented = false;
6586 sw_idx = NEXT_TX(sw_idx);
6587 ri = &tnapi->tx_buffers[sw_idx];
6588 }
6589
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 sw_idx = NEXT_TX(sw_idx);
6591 }
6592
Tom Herbert298376d2011-11-28 16:33:30 +00006593 pkts_compl++;
6594 bytes_compl += skb->len;
6595
David S. Millerf47c11e2005-06-24 20:18:35 -07006596 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006597
6598 if (unlikely(tx_bug)) {
6599 tg3_tx_recover(tp);
6600 return;
6601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 }
6603
Tom Herbert5cb917b2012-03-05 19:53:50 +00006604 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006605
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006606 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607
Michael Chan1b2a7202006-08-07 21:46:02 -07006608 /* Need to make the tx_cons update visible to tg3_start_xmit()
6609 * before checking for netif_queue_stopped(). Without the
6610 * memory barrier, there is a small possibility that tg3_start_xmit()
6611 * will miss it and cause the queue to be stopped forever.
6612 */
6613 smp_mb();
6614
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006615 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006616 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006617 __netif_tx_lock(txq, smp_processor_id());
6618 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006619 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006620 netif_tx_wake_queue(txq);
6621 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623}
6624
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006625static void tg3_frag_free(bool is_frag, void *data)
6626{
6627 if (is_frag)
6628 put_page(virt_to_head_page(data));
6629 else
6630 kfree(data);
6631}
6632
Eric Dumazet9205fd92011-11-18 06:47:01 +00006633static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006634{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006635 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6636 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6637
Eric Dumazet9205fd92011-11-18 06:47:01 +00006638 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006639 return;
6640
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006641 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006642 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006643 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006644 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006645}
6646
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006647
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648/* Returns size of skb allocated or < 0 on error.
6649 *
6650 * We only need to fill in the address because the other members
6651 * of the RX descriptor are invariant, see tg3_init_rings.
6652 *
6653 * Note the purposeful assymetry of cpu vs. chip accesses. For
6654 * posting buffers we only dirty the first cache line of the RX
6655 * descriptor (containing the address). Whereas for the RX status
6656 * buffers the cpu only reads the last cacheline of the RX descriptor
6657 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6658 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006659static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006660 u32 opaque_key, u32 dest_idx_unmasked,
6661 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662{
6663 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006664 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006665 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006667 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669 switch (opaque_key) {
6670 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006671 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006672 desc = &tpr->rx_std[dest_idx];
6673 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006674 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 break;
6676
6677 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006678 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006679 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006680 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006681 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 break;
6683
6684 default:
6685 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687
6688 /* Do not overwrite any of the map or rp information
6689 * until we are sure we can commit to a new buffer.
6690 *
6691 * Callers depend upon this behavior and assume that
6692 * we leave everything unchanged if we fail.
6693 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006694 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6695 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006696 if (skb_size <= PAGE_SIZE) {
6697 data = netdev_alloc_frag(skb_size);
6698 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006699 } else {
6700 data = kmalloc(skb_size, GFP_ATOMIC);
6701 *frag_size = 0;
6702 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006703 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 return -ENOMEM;
6705
Eric Dumazet9205fd92011-11-18 06:47:01 +00006706 mapping = pci_map_single(tp->pdev,
6707 data + TG3_RX_OFFSET(tp),
6708 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006710 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006711 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006712 return -EIO;
6713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
Eric Dumazet9205fd92011-11-18 06:47:01 +00006715 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006716 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 desc->addr_hi = ((u64)mapping >> 32);
6719 desc->addr_lo = ((u64)mapping & 0xffffffff);
6720
Eric Dumazet9205fd92011-11-18 06:47:01 +00006721 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722}
6723
6724/* We only need to move over in the address because the other
6725 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006726 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 */
Matt Carlsona3896162009-11-13 13:03:44 +00006728static void tg3_recycle_rx(struct tg3_napi *tnapi,
6729 struct tg3_rx_prodring_set *dpr,
6730 u32 opaque_key, int src_idx,
6731 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732{
Matt Carlson17375d22009-08-28 14:02:18 +00006733 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6735 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006736 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006737 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
6739 switch (opaque_key) {
6740 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006741 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006742 dest_desc = &dpr->rx_std[dest_idx];
6743 dest_map = &dpr->rx_std_buffers[dest_idx];
6744 src_desc = &spr->rx_std[src_idx];
6745 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 break;
6747
6748 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006749 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006750 dest_desc = &dpr->rx_jmb[dest_idx].std;
6751 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6752 src_desc = &spr->rx_jmb[src_idx].std;
6753 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 break;
6755
6756 default:
6757 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759
Eric Dumazet9205fd92011-11-18 06:47:01 +00006760 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006761 dma_unmap_addr_set(dest_map, mapping,
6762 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 dest_desc->addr_hi = src_desc->addr_hi;
6764 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006765
6766 /* Ensure that the update to the skb happens after the physical
6767 * addresses have been transferred to the new BD location.
6768 */
6769 smp_wmb();
6770
Eric Dumazet9205fd92011-11-18 06:47:01 +00006771 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772}
6773
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774/* The RX ring scheme is composed of multiple rings which post fresh
6775 * buffers to the chip, and one special ring the chip uses to report
6776 * status back to the host.
6777 *
6778 * The special ring reports the status of received packets to the
6779 * host. The chip does not write into the original descriptor the
6780 * RX buffer was obtained from. The chip simply takes the original
6781 * descriptor as provided by the host, updates the status and length
6782 * field, then writes this into the next status ring entry.
6783 *
6784 * Each ring the host uses to post buffers to the chip is described
6785 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6786 * it is first placed into the on-chip ram. When the packet's length
6787 * is known, it walks down the TG3_BDINFO entries to select the ring.
6788 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6789 * which is within the range of the new packet's length is chosen.
6790 *
6791 * The "separate ring for rx status" scheme may sound queer, but it makes
6792 * sense from a cache coherency perspective. If only the host writes
6793 * to the buffer post rings, and only the chip writes to the rx status
6794 * rings, then cache lines never move beyond shared-modified state.
6795 * If both the host and chip were to write into the same ring, cache line
6796 * eviction could occur since both entities want it in an exclusive state.
6797 */
Matt Carlson17375d22009-08-28 14:02:18 +00006798static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799{
Matt Carlson17375d22009-08-28 14:02:18 +00006800 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006801 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006802 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006803 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006804 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006806 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006808 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 /*
6810 * We need to order the read of hw_idx and the read of
6811 * the opaque cookie.
6812 */
6813 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 work_mask = 0;
6815 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006816 std_prod_idx = tpr->rx_std_prod_idx;
6817 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006819 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006820 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 unsigned int len;
6822 struct sk_buff *skb;
6823 dma_addr_t dma_addr;
6824 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006825 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006826 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827
6828 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6829 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6830 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006831 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006832 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006833 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006834 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006835 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006837 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006838 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006839 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006840 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006841 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
6844 work_mask |= opaque_key;
6845
6846 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6847 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6848 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006849 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 desc_idx, *post_ptr);
6851 drop_it_no_recycle:
6852 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006853 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 goto next_pkt;
6855 }
6856
Eric Dumazet9205fd92011-11-18 06:47:01 +00006857 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006858 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6859 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006861 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6862 RXD_FLAG_PTPSTAT_PTPV1 ||
6863 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6864 RXD_FLAG_PTPSTAT_PTPV2) {
6865 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6866 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6867 }
6868
Matt Carlsond2757fc2010-04-12 06:58:27 +00006869 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006871 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872
Eric Dumazet9205fd92011-11-18 06:47:01 +00006873 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006874 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 if (skb_size < 0)
6876 goto drop_it;
6877
Matt Carlson287be122009-08-28 13:58:46 +00006878 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 PCI_DMA_FROMDEVICE);
6880
Eric Dumazet9205fd92011-11-18 06:47:01 +00006881 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006882 * after the usage of the old DMA mapping.
6883 */
6884 smp_wmb();
6885
Eric Dumazet9205fd92011-11-18 06:47:01 +00006886 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006887
Ivan Vecera85aec732013-11-06 14:02:36 +01006888 skb = build_skb(data, frag_size);
6889 if (!skb) {
6890 tg3_frag_free(frag_size != 0, data);
6891 goto drop_it_no_recycle;
6892 }
6893 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006895 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 desc_idx, *post_ptr);
6897
Eric Dumazet9205fd92011-11-18 06:47:01 +00006898 skb = netdev_alloc_skb(tp->dev,
6899 len + TG3_RAW_IP_ALIGN);
6900 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 goto drop_it_no_recycle;
6902
Eric Dumazet9205fd92011-11-18 06:47:01 +00006903 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006905 memcpy(skb->data,
6906 data + TG3_RX_OFFSET(tp),
6907 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 }
6910
Eric Dumazet9205fd92011-11-18 06:47:01 +00006911 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006912 if (tstamp)
6913 tg3_hwclock_to_timestamp(tp, tstamp,
6914 skb_hwtstamps(skb));
6915
Michał Mirosławdc668912011-04-07 03:35:07 +00006916 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6918 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6919 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6920 skb->ip_summed = CHECKSUM_UNNECESSARY;
6921 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006922 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
6924 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006925
6926 if (len > (tp->dev->mtu + ETH_HLEN) &&
6927 skb->protocol != htons(ETH_P_8021Q)) {
6928 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006929 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006930 }
6931
Matt Carlson9dc7a112010-04-12 06:58:28 +00006932 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006933 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006934 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006935 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006936
Matt Carlsonbf933c82011-01-25 15:58:49 +00006937 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 received++;
6940 budget--;
6941
6942next_pkt:
6943 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006944
6945 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006946 tpr->rx_std_prod_idx = std_prod_idx &
6947 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006948 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6949 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006950 work_mask &= ~RXD_OPAQUE_RING_STD;
6951 rx_std_posted = 0;
6952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006954 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006955 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006956
6957 /* Refresh hw_idx to see if there is new work */
6958 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006959 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006960 rmb();
6961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962 }
6963
6964 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006965 tnapi->rx_rcb_ptr = sw_idx;
6966 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967
6968 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006969 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006970 /* Sync BD data before updating mailbox */
6971 wmb();
6972
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006973 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006974 tpr->rx_std_prod_idx = std_prod_idx &
6975 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006976 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6977 tpr->rx_std_prod_idx);
6978 }
6979 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006980 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6981 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006982 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6983 tpr->rx_jmb_prod_idx);
6984 }
6985 mmiowb();
6986 } else if (work_mask) {
6987 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6988 * updated before the producer indices can be updated.
6989 */
6990 smp_wmb();
6991
Matt Carlson2c49a442010-09-30 10:34:35 +00006992 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6993 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006994
Michael Chan7ae52892012-03-21 15:38:33 +00006995 if (tnapi != &tp->napi[1]) {
6996 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006997 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000
7001 return received;
7002}
7003
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007004static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00007007 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007008 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
7009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 if (sblk->status & SD_STATUS_LINK_CHG) {
7011 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007012 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07007013 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00007014 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07007015 tw32_f(MAC_STATUS,
7016 (MAC_STATUS_SYNC_CHANGED |
7017 MAC_STATUS_CFG_CHANGED |
7018 MAC_STATUS_MI_COMPLETION |
7019 MAC_STATUS_LNKSTATE_CHANGED));
7020 udelay(40);
7021 } else
Joe Perches953c96e2013-04-09 10:18:14 +00007022 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07007023 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 }
7025 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007026}
7027
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007028static int tg3_rx_prodring_xfer(struct tg3 *tp,
7029 struct tg3_rx_prodring_set *dpr,
7030 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007031{
7032 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007033 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007034
7035 while (1) {
7036 src_prod_idx = spr->rx_std_prod_idx;
7037
7038 /* Make sure updates to the rx_std_buffers[] entries and the
7039 * standard producer index are seen in the correct order.
7040 */
7041 smp_rmb();
7042
7043 if (spr->rx_std_cons_idx == src_prod_idx)
7044 break;
7045
7046 if (spr->rx_std_cons_idx < src_prod_idx)
7047 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
7048 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007049 cpycnt = tp->rx_std_ring_mask + 1 -
7050 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007051
Matt Carlson2c49a442010-09-30 10:34:35 +00007052 cpycnt = min(cpycnt,
7053 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007054
7055 si = spr->rx_std_cons_idx;
7056 di = dpr->rx_std_prod_idx;
7057
Matt Carlsone92967b2010-02-12 14:47:06 +00007058 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007059 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007060 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007061 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007062 break;
7063 }
7064 }
7065
7066 if (!cpycnt)
7067 break;
7068
7069 /* Ensure that updates to the rx_std_buffers ring and the
7070 * shadowed hardware producer ring from tg3_recycle_skb() are
7071 * ordered correctly WRT the skb check above.
7072 */
7073 smp_rmb();
7074
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007075 memcpy(&dpr->rx_std_buffers[di],
7076 &spr->rx_std_buffers[si],
7077 cpycnt * sizeof(struct ring_info));
7078
7079 for (i = 0; i < cpycnt; i++, di++, si++) {
7080 struct tg3_rx_buffer_desc *sbd, *dbd;
7081 sbd = &spr->rx_std[si];
7082 dbd = &dpr->rx_std[di];
7083 dbd->addr_hi = sbd->addr_hi;
7084 dbd->addr_lo = sbd->addr_lo;
7085 }
7086
Matt Carlson2c49a442010-09-30 10:34:35 +00007087 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
7088 tp->rx_std_ring_mask;
7089 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
7090 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007091 }
7092
7093 while (1) {
7094 src_prod_idx = spr->rx_jmb_prod_idx;
7095
7096 /* Make sure updates to the rx_jmb_buffers[] entries and
7097 * the jumbo producer index are seen in the correct order.
7098 */
7099 smp_rmb();
7100
7101 if (spr->rx_jmb_cons_idx == src_prod_idx)
7102 break;
7103
7104 if (spr->rx_jmb_cons_idx < src_prod_idx)
7105 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
7106 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007107 cpycnt = tp->rx_jmb_ring_mask + 1 -
7108 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007109
7110 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00007111 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007112
7113 si = spr->rx_jmb_cons_idx;
7114 di = dpr->rx_jmb_prod_idx;
7115
Matt Carlsone92967b2010-02-12 14:47:06 +00007116 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007117 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007118 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007119 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007120 break;
7121 }
7122 }
7123
7124 if (!cpycnt)
7125 break;
7126
7127 /* Ensure that updates to the rx_jmb_buffers ring and the
7128 * shadowed hardware producer ring from tg3_recycle_skb() are
7129 * ordered correctly WRT the skb check above.
7130 */
7131 smp_rmb();
7132
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007133 memcpy(&dpr->rx_jmb_buffers[di],
7134 &spr->rx_jmb_buffers[si],
7135 cpycnt * sizeof(struct ring_info));
7136
7137 for (i = 0; i < cpycnt; i++, di++, si++) {
7138 struct tg3_rx_buffer_desc *sbd, *dbd;
7139 sbd = &spr->rx_jmb[si].std;
7140 dbd = &dpr->rx_jmb[di].std;
7141 dbd->addr_hi = sbd->addr_hi;
7142 dbd->addr_lo = sbd->addr_lo;
7143 }
7144
Matt Carlson2c49a442010-09-30 10:34:35 +00007145 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7146 tp->rx_jmb_ring_mask;
7147 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7148 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007149 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007150
7151 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007152}
7153
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007154static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7155{
7156 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157
7158 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007159 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007160 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007161 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007162 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 }
7164
Matt Carlsonf891ea12012-04-24 13:37:01 +00007165 if (!tnapi->rx_rcb_prod_idx)
7166 return work_done;
7167
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 /* run RX thread, within the bounds set by NAPI.
7169 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007170 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007172 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007173 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174
Joe Perches63c3a662011-04-26 08:12:10 +00007175 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007176 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007177 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007178 u32 std_prod_idx = dpr->rx_std_prod_idx;
7179 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007180
Michael Chan7ae52892012-03-21 15:38:33 +00007181 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007182 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007183 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007184 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007185
7186 wmb();
7187
Matt Carlsone4af1af2010-02-12 14:47:05 +00007188 if (std_prod_idx != dpr->rx_std_prod_idx)
7189 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7190 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007191
Matt Carlsone4af1af2010-02-12 14:47:05 +00007192 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7193 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7194 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007195
7196 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007197
7198 if (err)
7199 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007200 }
7201
David S. Miller6f535762007-10-11 18:08:29 -07007202 return work_done;
7203}
David S. Millerf7383c22005-05-18 22:50:53 -07007204
Matt Carlsondb219972011-11-04 09:15:03 +00007205static inline void tg3_reset_task_schedule(struct tg3 *tp)
7206{
7207 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7208 schedule_work(&tp->reset_task);
7209}
7210
7211static inline void tg3_reset_task_cancel(struct tg3 *tp)
7212{
7213 cancel_work_sync(&tp->reset_task);
7214 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007215 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007216}
7217
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007218static int tg3_poll_msix(struct napi_struct *napi, int budget)
7219{
7220 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7221 struct tg3 *tp = tnapi->tp;
7222 int work_done = 0;
7223 struct tg3_hw_status *sblk = tnapi->hw_status;
7224
7225 while (1) {
7226 work_done = tg3_poll_work(tnapi, work_done, budget);
7227
Joe Perches63c3a662011-04-26 08:12:10 +00007228 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007229 goto tx_recovery;
7230
7231 if (unlikely(work_done >= budget))
7232 break;
7233
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007234 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007235 * to tell the hw how much work has been processed,
7236 * so we must read it before checking for more work.
7237 */
7238 tnapi->last_tag = sblk->status_tag;
7239 tnapi->last_irq_tag = tnapi->last_tag;
7240 rmb();
7241
7242 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007243 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7244 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007245
7246 /* This test here is not race free, but will reduce
7247 * the number of interrupts by looping again.
7248 */
7249 if (tnapi == &tp->napi[1] && tp->rx_refill)
7250 continue;
7251
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007252 napi_complete(napi);
7253 /* Reenable interrupts. */
7254 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007255
7256 /* This test here is synchronized by napi_schedule()
7257 * and napi_complete() to close the race condition.
7258 */
7259 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7260 tw32(HOSTCC_MODE, tp->coalesce_mode |
7261 HOSTCC_MODE_ENABLE |
7262 tnapi->coal_now);
7263 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007264 mmiowb();
7265 break;
7266 }
7267 }
7268
7269 return work_done;
7270
7271tx_recovery:
7272 /* work_done is guaranteed to be less than budget. */
7273 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007274 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007275 return work_done;
7276}
7277
Matt Carlsone64de4e2011-04-13 11:05:05 +00007278static void tg3_process_error(struct tg3 *tp)
7279{
7280 u32 val;
7281 bool real_error = false;
7282
Joe Perches63c3a662011-04-26 08:12:10 +00007283 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007284 return;
7285
7286 /* Check Flow Attention register */
7287 val = tr32(HOSTCC_FLOW_ATTN);
7288 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7289 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7290 real_error = true;
7291 }
7292
7293 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7294 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7295 real_error = true;
7296 }
7297
7298 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7299 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7300 real_error = true;
7301 }
7302
7303 if (!real_error)
7304 return;
7305
7306 tg3_dump_state(tp);
7307
Joe Perches63c3a662011-04-26 08:12:10 +00007308 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007309 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007310}
7311
David S. Miller6f535762007-10-11 18:08:29 -07007312static int tg3_poll(struct napi_struct *napi, int budget)
7313{
Matt Carlson8ef04422009-08-28 14:01:37 +00007314 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7315 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007316 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007317 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007318
7319 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007320 if (sblk->status & SD_STATUS_ERROR)
7321 tg3_process_error(tp);
7322
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007323 tg3_poll_link(tp);
7324
Matt Carlson17375d22009-08-28 14:02:18 +00007325 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007326
Joe Perches63c3a662011-04-26 08:12:10 +00007327 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007328 goto tx_recovery;
7329
7330 if (unlikely(work_done >= budget))
7331 break;
7332
Joe Perches63c3a662011-04-26 08:12:10 +00007333 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007334 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007335 * to tell the hw how much work has been processed,
7336 * so we must read it before checking for more work.
7337 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007338 tnapi->last_tag = sblk->status_tag;
7339 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007340 rmb();
7341 } else
7342 sblk->status &= ~SD_STATUS_UPDATED;
7343
Matt Carlson17375d22009-08-28 14:02:18 +00007344 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007345 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00007346 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007347 break;
7348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 }
7350
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007351 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007352
7353tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007354 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007355 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007356 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007357 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358}
7359
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007360static void tg3_napi_disable(struct tg3 *tp)
7361{
7362 int i;
7363
7364 for (i = tp->irq_cnt - 1; i >= 0; i--)
7365 napi_disable(&tp->napi[i].napi);
7366}
7367
7368static void tg3_napi_enable(struct tg3 *tp)
7369{
7370 int i;
7371
7372 for (i = 0; i < tp->irq_cnt; i++)
7373 napi_enable(&tp->napi[i].napi);
7374}
7375
7376static void tg3_napi_init(struct tg3 *tp)
7377{
7378 int i;
7379
7380 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7381 for (i = 1; i < tp->irq_cnt; i++)
7382 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7383}
7384
7385static void tg3_napi_fini(struct tg3 *tp)
7386{
7387 int i;
7388
7389 for (i = 0; i < tp->irq_cnt; i++)
7390 netif_napi_del(&tp->napi[i].napi);
7391}
7392
7393static inline void tg3_netif_stop(struct tg3 *tp)
7394{
7395 tp->dev->trans_start = jiffies; /* prevent tx timeout */
7396 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007397 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007398 netif_tx_disable(tp->dev);
7399}
7400
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007401/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007402static inline void tg3_netif_start(struct tg3 *tp)
7403{
Matt Carlsonbe947302012-12-03 19:36:57 +00007404 tg3_ptp_resume(tp);
7405
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007406 /* NOTE: unconditional netif_tx_wake_all_queues is only
7407 * appropriate so long as all callers are assured to
7408 * have free tx slots (such as after tg3_init_hw)
7409 */
7410 netif_tx_wake_all_queues(tp->dev);
7411
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007412 if (tp->link_up)
7413 netif_carrier_on(tp->dev);
7414
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007415 tg3_napi_enable(tp);
7416 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7417 tg3_enable_ints(tp);
7418}
7419
David S. Millerf47c11e2005-06-24 20:18:35 -07007420static void tg3_irq_quiesce(struct tg3 *tp)
7421{
Matt Carlson4f125f42009-09-01 12:55:02 +00007422 int i;
7423
David S. Millerf47c11e2005-06-24 20:18:35 -07007424 BUG_ON(tp->irq_sync);
7425
7426 tp->irq_sync = 1;
7427 smp_mb();
7428
Matt Carlson4f125f42009-09-01 12:55:02 +00007429 for (i = 0; i < tp->irq_cnt; i++)
7430 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07007431}
7432
David S. Millerf47c11e2005-06-24 20:18:35 -07007433/* Fully shutdown all tg3 driver activity elsewhere in the system.
7434 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7435 * with as well. Most of the time, this is not necessary except when
7436 * shutting down the device.
7437 */
7438static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7439{
Michael Chan46966542007-07-11 19:47:19 -07007440 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007441 if (irq_sync)
7442 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007443}
7444
7445static inline void tg3_full_unlock(struct tg3 *tp)
7446{
David S. Millerf47c11e2005-06-24 20:18:35 -07007447 spin_unlock_bh(&tp->lock);
7448}
7449
Michael Chanfcfa0a32006-03-20 22:28:41 -08007450/* One-shot MSI handler - Chip automatically disables interrupt
7451 * after sending MSI so driver doesn't have to do it.
7452 */
David Howells7d12e782006-10-05 14:55:46 +01007453static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007454{
Matt Carlson09943a12009-08-28 14:01:57 +00007455 struct tg3_napi *tnapi = dev_id;
7456 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007457
Matt Carlson898a56f2009-08-28 14:02:40 +00007458 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007459 if (tnapi->rx_rcb)
7460 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007461
7462 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007463 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007464
7465 return IRQ_HANDLED;
7466}
7467
Michael Chan88b06bc22005-04-21 17:13:25 -07007468/* MSI ISR - No need to check for interrupt sharing and no need to
7469 * flush status block and interrupt mailbox. PCI ordering rules
7470 * guarantee that MSI will arrive after the status block.
7471 */
David Howells7d12e782006-10-05 14:55:46 +01007472static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007473{
Matt Carlson09943a12009-08-28 14:01:57 +00007474 struct tg3_napi *tnapi = dev_id;
7475 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007476
Matt Carlson898a56f2009-08-28 14:02:40 +00007477 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007478 if (tnapi->rx_rcb)
7479 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007480 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007481 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007482 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007483 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007484 * NIC to stop sending us irqs, engaging "in-intr-handler"
7485 * event coalescing.
7486 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007487 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007488 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007489 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007490
Michael Chan88b06bc22005-04-21 17:13:25 -07007491 return IRQ_RETVAL(1);
7492}
7493
David Howells7d12e782006-10-05 14:55:46 +01007494static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495{
Matt Carlson09943a12009-08-28 14:01:57 +00007496 struct tg3_napi *tnapi = dev_id;
7497 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007498 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 unsigned int handled = 1;
7500
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 /* In INTx mode, it is possible for the interrupt to arrive at
7502 * the CPU before the status block posted prior to the interrupt.
7503 * Reading the PCI State register will confirm whether the
7504 * interrupt is ours and will flush the status block.
7505 */
Michael Chand18edcb2007-03-24 20:57:11 -07007506 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007507 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007508 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7509 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007510 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007511 }
Michael Chand18edcb2007-03-24 20:57:11 -07007512 }
7513
7514 /*
7515 * Writing any value to intr-mbox-0 clears PCI INTA# and
7516 * chip-internal interrupt pending events.
7517 * Writing non-zero to intr-mbox-0 additional tells the
7518 * NIC to stop sending us irqs, engaging "in-intr-handler"
7519 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007520 *
7521 * Flush the mailbox to de-assert the IRQ immediately to prevent
7522 * spurious interrupts. The flush impacts performance but
7523 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007524 */
Michael Chanc04cb342007-05-07 00:26:15 -07007525 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007526 if (tg3_irq_sync(tp))
7527 goto out;
7528 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007529 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007530 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007531 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007532 } else {
7533 /* No work, shared interrupt perhaps? re-enable
7534 * interrupts, and flush that PCI write
7535 */
7536 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7537 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007538 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007539out:
David S. Millerfac9b832005-05-18 22:46:34 -07007540 return IRQ_RETVAL(handled);
7541}
7542
David Howells7d12e782006-10-05 14:55:46 +01007543static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007544{
Matt Carlson09943a12009-08-28 14:01:57 +00007545 struct tg3_napi *tnapi = dev_id;
7546 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007547 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007548 unsigned int handled = 1;
7549
David S. Millerfac9b832005-05-18 22:46:34 -07007550 /* In INTx mode, it is possible for the interrupt to arrive at
7551 * the CPU before the status block posted prior to the interrupt.
7552 * Reading the PCI State register will confirm whether the
7553 * interrupt is ours and will flush the status block.
7554 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007555 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007556 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007557 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7558 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 }
Michael Chand18edcb2007-03-24 20:57:11 -07007561 }
7562
7563 /*
7564 * writing any value to intr-mbox-0 clears PCI INTA# and
7565 * chip-internal interrupt pending events.
7566 * writing non-zero to intr-mbox-0 additional tells the
7567 * NIC to stop sending us irqs, engaging "in-intr-handler"
7568 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007569 *
7570 * Flush the mailbox to de-assert the IRQ immediately to prevent
7571 * spurious interrupts. The flush impacts performance but
7572 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007573 */
Michael Chanc04cb342007-05-07 00:26:15 -07007574 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007575
7576 /*
7577 * In a shared interrupt configuration, sometimes other devices'
7578 * interrupts will scream. We record the current status tag here
7579 * so that the above check can report that the screaming interrupts
7580 * are unhandled. Eventually they will be silenced.
7581 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007582 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007583
Michael Chand18edcb2007-03-24 20:57:11 -07007584 if (tg3_irq_sync(tp))
7585 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007586
Matt Carlson72334482009-08-28 14:03:01 +00007587 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007588
Matt Carlson09943a12009-08-28 14:01:57 +00007589 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007590
David S. Millerf47c11e2005-06-24 20:18:35 -07007591out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 return IRQ_RETVAL(handled);
7593}
7594
Michael Chan79381092005-04-21 17:13:59 -07007595/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007596static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007597{
Matt Carlson09943a12009-08-28 14:01:57 +00007598 struct tg3_napi *tnapi = dev_id;
7599 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007600 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007601
Michael Chanf9804dd2005-09-27 12:13:10 -07007602 if ((sblk->status & SD_STATUS_UPDATED) ||
7603 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007604 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007605 return IRQ_RETVAL(1);
7606 }
7607 return IRQ_RETVAL(0);
7608}
7609
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610#ifdef CONFIG_NET_POLL_CONTROLLER
7611static void tg3_poll_controller(struct net_device *dev)
7612{
Matt Carlson4f125f42009-09-01 12:55:02 +00007613 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007614 struct tg3 *tp = netdev_priv(dev);
7615
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007616 if (tg3_irq_sync(tp))
7617 return;
7618
Matt Carlson4f125f42009-09-01 12:55:02 +00007619 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007620 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621}
7622#endif
7623
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624static void tg3_tx_timeout(struct net_device *dev)
7625{
7626 struct tg3 *tp = netdev_priv(dev);
7627
Michael Chanb0408752007-02-13 12:18:30 -08007628 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007629 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007630 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632
Matt Carlsondb219972011-11-04 09:15:03 +00007633 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634}
7635
Michael Chanc58ec932005-09-17 00:46:27 -07007636/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7637static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7638{
7639 u32 base = (u32) mapping & 0xffffffff;
7640
Eric Dumazet807540b2010-09-23 05:40:09 +00007641 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007642}
7643
Michael Chan0f0d1512013-05-13 11:04:16 +00007644/* Test for TSO DMA buffers that cross into regions which are within MSS bytes
7645 * of any 4GB boundaries: 4G, 8G, etc
7646 */
7647static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7648 u32 len, u32 mss)
7649{
7650 if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
7651 u32 base = (u32) mapping & 0xffffffff;
7652
7653 return ((base + len + (mss & 0x3fff)) < base);
7654 }
7655 return 0;
7656}
7657
Michael Chan72f2afb2006-03-06 19:28:35 -08007658/* Test for DMA addresses > 40-bit */
7659static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7660 int len)
7661{
7662#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007663 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007664 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007665 return 0;
7666#else
7667 return 0;
7668#endif
7669}
7670
Matt Carlsond1a3b732011-07-27 14:20:51 +00007671static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007672 dma_addr_t mapping, u32 len, u32 flags,
7673 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007674{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007675 txbd->addr_hi = ((u64) mapping >> 32);
7676 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7677 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7678 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007679}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680
Matt Carlson84b67b22011-07-27 14:20:52 +00007681static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007682 dma_addr_t map, u32 len, u32 flags,
7683 u32 mss, u32 vlan)
7684{
7685 struct tg3 *tp = tnapi->tp;
7686 bool hwbug = false;
7687
7688 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007689 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007690
7691 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007692 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007693
Michael Chan0f0d1512013-05-13 11:04:16 +00007694 if (tg3_4g_tso_overflow_test(tp, map, len, mss))
7695 hwbug = true;
7696
Matt Carlsond1a3b732011-07-27 14:20:51 +00007697 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007698 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007699
Matt Carlsona4cb4282011-12-14 11:09:58 +00007700 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007701 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007702 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007703 while (len > tp->dma_limit && *budget) {
7704 u32 frag_len = tp->dma_limit;
7705 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007706
Matt Carlsonb9e45482011-11-04 09:14:59 +00007707 /* Avoid the 8byte DMA problem */
7708 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007709 len += tp->dma_limit / 2;
7710 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007711 }
7712
Matt Carlsonb9e45482011-11-04 09:14:59 +00007713 tnapi->tx_buffers[*entry].fragmented = true;
7714
7715 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7716 frag_len, tmp_flag, mss, vlan);
7717 *budget -= 1;
7718 prvidx = *entry;
7719 *entry = NEXT_TX(*entry);
7720
Matt Carlsone31aa982011-07-27 14:20:53 +00007721 map += frag_len;
7722 }
7723
7724 if (len) {
7725 if (*budget) {
7726 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7727 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007728 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007729 *entry = NEXT_TX(*entry);
7730 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007731 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007732 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007733 }
7734 }
7735 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007736 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7737 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007738 *entry = NEXT_TX(*entry);
7739 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007740
7741 return hwbug;
7742}
7743
Matt Carlson0d681b22011-07-27 14:20:49 +00007744static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007745{
7746 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007747 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007748 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007749
Matt Carlson0d681b22011-07-27 14:20:49 +00007750 skb = txb->skb;
7751 txb->skb = NULL;
7752
Matt Carlson432aa7e2011-05-19 12:12:45 +00007753 pci_unmap_single(tnapi->tp->pdev,
7754 dma_unmap_addr(txb, mapping),
7755 skb_headlen(skb),
7756 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007757
7758 while (txb->fragmented) {
7759 txb->fragmented = false;
7760 entry = NEXT_TX(entry);
7761 txb = &tnapi->tx_buffers[entry];
7762 }
7763
Matt Carlsonba1142e2011-11-04 09:15:00 +00007764 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007765 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007766
7767 entry = NEXT_TX(entry);
7768 txb = &tnapi->tx_buffers[entry];
7769
7770 pci_unmap_page(tnapi->tp->pdev,
7771 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007772 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007773
7774 while (txb->fragmented) {
7775 txb->fragmented = false;
7776 entry = NEXT_TX(entry);
7777 txb = &tnapi->tx_buffers[entry];
7778 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007779 }
7780}
7781
Michael Chan72f2afb2006-03-06 19:28:35 -08007782/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007783static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007784 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007785 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007786 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007788 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007789 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007790 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007791 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792
Joe Perches41535772013-02-16 11:20:04 +00007793 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007794 new_skb = skb_copy(skb, GFP_ATOMIC);
7795 else {
7796 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7797
7798 new_skb = skb_copy_expand(skb,
7799 skb_headroom(skb) + more_headroom,
7800 skb_tailroom(skb), GFP_ATOMIC);
7801 }
7802
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007804 ret = -1;
7805 } else {
7806 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007807 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7808 PCI_DMA_TODEVICE);
7809 /* Make sure the mapping succeeded */
7810 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007811 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007812 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007813 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007814 u32 save_entry = *entry;
7815
Matt Carlson92cd3a12011-07-27 14:20:47 +00007816 base_flags |= TXD_FLAG_END;
7817
Matt Carlson84b67b22011-07-27 14:20:52 +00007818 tnapi->tx_buffers[*entry].skb = new_skb;
7819 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007820 mapping, new_addr);
7821
Matt Carlson84b67b22011-07-27 14:20:52 +00007822 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007823 new_skb->len, base_flags,
7824 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007825 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007826 dev_kfree_skb(new_skb);
7827 ret = -1;
7828 }
Michael Chanc58ec932005-09-17 00:46:27 -07007829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830 }
7831
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007833 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007834 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835}
7836
Matt Carlson2ffcc982011-05-19 12:12:44 +00007837static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007838
7839/* Use GSO to workaround a rare TSO bug that may be triggered when the
7840 * TSO header is greater than 80 bytes.
7841 */
7842static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7843{
7844 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007845 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007846
7847 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007848 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007849 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007850
7851 /* netif_tx_stop_queue() must be done before checking
7852 * checking tx index in tg3_tx_avail() below, because in
7853 * tg3_tx(), we update tx index before checking for
7854 * netif_tx_queue_stopped().
7855 */
7856 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007857 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007858 return NETDEV_TX_BUSY;
7859
7860 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007861 }
7862
7863 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007864 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007865 goto tg3_tso_bug_end;
7866
7867 do {
7868 nskb = segs;
7869 segs = segs->next;
7870 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007871 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007872 } while (segs);
7873
7874tg3_tso_bug_end:
7875 dev_kfree_skb(skb);
7876
7877 return NETDEV_TX_OK;
7878}
Michael Chan52c0fd82006-06-29 20:15:54 -07007879
Michael Chan5a6f3072006-03-20 22:28:05 -08007880/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007881 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007882 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007883static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007884{
7885 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007886 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007887 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007888 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007889 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007890 struct tg3_napi *tnapi;
7891 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007892 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007893
Matt Carlson24f4efd2009-11-13 13:03:35 +00007894 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7895 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007896 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007897 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898
Matt Carlson84b67b22011-07-27 14:20:52 +00007899 budget = tg3_tx_avail(tnapi);
7900
Michael Chan00b70502006-06-17 21:58:45 -07007901 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007902 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007903 * interrupt. Furthermore, IRQ processing runs lockless so we have
7904 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007906 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007907 if (!netif_tx_queue_stopped(txq)) {
7908 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007909
7910 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007911 netdev_err(dev,
7912 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 return NETDEV_TX_BUSY;
7915 }
7916
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007917 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007919 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007920 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007921
Matt Carlsonbe98da62010-07-11 09:31:46 +00007922 mss = skb_shinfo(skb)->gso_size;
7923 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007924 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007925 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926
7927 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007928 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7929 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930
Matt Carlson34195c32010-07-11 09:31:42 +00007931 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007932 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933
Eric Dumazeta5a11952012-01-23 01:22:09 +00007934 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007935
Eric Dumazeta5a11952012-01-23 01:22:09 +00007936 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007937 iph->check = 0;
7938 iph->tot_len = htons(mss + hdr_len);
7939 }
7940
Michael Chan52c0fd82006-06-29 20:15:54 -07007941 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007942 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007943 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007944
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7946 TXD_FLAG_CPU_POST_DMA);
7947
Joe Perches63c3a662011-04-26 08:12:10 +00007948 if (tg3_flag(tp, HW_TSO_1) ||
7949 tg3_flag(tp, HW_TSO_2) ||
7950 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007951 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007953 } else
7954 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7955 iph->daddr, 0,
7956 IPPROTO_TCP,
7957 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958
Joe Perches63c3a662011-04-26 08:12:10 +00007959 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007960 mss |= (hdr_len & 0xc) << 12;
7961 if (hdr_len & 0x10)
7962 base_flags |= 0x00000010;
7963 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007964 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007965 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007966 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007967 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007968 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969 int tsflags;
7970
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007971 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 mss |= (tsflags << 11);
7973 }
7974 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007975 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976 int tsflags;
7977
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007978 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979 base_flags |= tsflags << 12;
7980 }
7981 }
7982 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007983
Matt Carlson93a700a2011-08-31 11:44:54 +00007984 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7985 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7986 base_flags |= TXD_FLAG_JMB_PKT;
7987
Matt Carlson92cd3a12011-07-27 14:20:47 +00007988 if (vlan_tx_tag_present(skb)) {
7989 base_flags |= TXD_FLAG_VLAN;
7990 vlan = vlan_tx_tag_get(skb);
7991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007993 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7994 tg3_flag(tp, TX_TSTAMP_EN)) {
7995 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7996 base_flags |= TXD_FLAG_HWTSTAMP;
7997 }
7998
Alexander Duyckf4188d82009-12-02 16:48:38 +00007999 len = skb_headlen(skb);
8000
8001 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00008002 if (pci_dma_mapping_error(tp->pdev, mapping))
8003 goto drop;
8004
David S. Miller90079ce2008-09-11 04:52:51 -07008005
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008006 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008007 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008
8009 would_hit_hwbug = 0;
8010
Joe Perches63c3a662011-04-26 08:12:10 +00008011 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07008012 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013
Matt Carlson84b67b22011-07-27 14:20:52 +00008014 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00008015 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00008016 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00008017 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00008018 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008019 u32 tmp_mss = mss;
8020
8021 if (!tg3_flag(tp, HW_TSO_1) &&
8022 !tg3_flag(tp, HW_TSO_2) &&
8023 !tg3_flag(tp, HW_TSO_3))
8024 tmp_mss = 0;
8025
Matt Carlsonc5665a52012-02-13 10:20:12 +00008026 /* Now loop through additional data
8027 * fragments, and queue them.
8028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029 last = skb_shinfo(skb)->nr_frags - 1;
8030 for (i = 0; i <= last; i++) {
8031 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
8032
Eric Dumazet9e903e02011-10-18 21:00:24 +00008033 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00008034 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008035 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008036
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008037 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008038 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00008039 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008040 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00008041 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008042
Matt Carlsonb9e45482011-11-04 09:14:59 +00008043 if (!budget ||
8044 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00008045 len, base_flags |
8046 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00008047 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00008048 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00008049 break;
8050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051 }
8052 }
8053
8054 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00008055 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056
8057 /* If the workaround fails due to memory/mapping
8058 * failure, silently drop this packet.
8059 */
Matt Carlson84b67b22011-07-27 14:20:52 +00008060 entry = tnapi->tx_prod;
8061 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04008062 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00008063 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00008064 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008065 }
8066
Richard Cochrand515b452011-06-19 03:31:41 +00008067 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00008068 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00008069
Michael Chan6541b802012-03-04 14:48:14 +00008070 /* Sync BD data before updating mailbox */
8071 wmb();
8072
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00008074 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008076 tnapi->tx_prod = entry;
8077 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00008078 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00008079
8080 /* netif_tx_stop_queue() must be done before checking
8081 * checking tx index in tg3_tx_avail() below, because in
8082 * tg3_tx(), we update tx index before checking for
8083 * netif_tx_queue_stopped().
8084 */
8085 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008086 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00008087 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07008088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089
Eric Dumazetcdd0db02009-05-28 00:00:41 +00008090 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008092
8093dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00008094 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00008095 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00008096drop:
8097 dev_kfree_skb(skb);
8098drop_nofree:
8099 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008100 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101}
8102
Matt Carlson6e01b202011-08-19 13:58:20 +00008103static void tg3_mac_loopback(struct tg3 *tp, bool enable)
8104{
8105 if (enable) {
8106 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
8107 MAC_MODE_PORT_MODE_MASK);
8108
8109 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
8110
8111 if (!tg3_flag(tp, 5705_PLUS))
8112 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8113
8114 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
8115 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
8116 else
8117 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
8118 } else {
8119 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8120
8121 if (tg3_flag(tp, 5705_PLUS) ||
8122 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008123 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008124 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8125 }
8126
8127 tw32(MAC_MODE, tp->mac_mode);
8128 udelay(40);
8129}
8130
Matt Carlson941ec902011-08-19 13:58:23 +00008131static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008132{
Matt Carlson941ec902011-08-19 13:58:23 +00008133 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008134
8135 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008136 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008137
Matt Carlson941ec902011-08-19 13:58:23 +00008138 if (extlpbk && tg3_phy_set_extloopbk(tp))
8139 return -EIO;
8140
8141 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008142 switch (speed) {
8143 case SPEED_10:
8144 break;
8145 case SPEED_100:
8146 bmcr |= BMCR_SPEED100;
8147 break;
8148 case SPEED_1000:
8149 default:
8150 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8151 speed = SPEED_100;
8152 bmcr |= BMCR_SPEED100;
8153 } else {
8154 speed = SPEED_1000;
8155 bmcr |= BMCR_SPEED1000;
8156 }
8157 }
8158
Matt Carlson941ec902011-08-19 13:58:23 +00008159 if (extlpbk) {
8160 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8161 tg3_readphy(tp, MII_CTRL1000, &val);
8162 val |= CTL1000_AS_MASTER |
8163 CTL1000_ENABLE_MASTER;
8164 tg3_writephy(tp, MII_CTRL1000, val);
8165 } else {
8166 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8167 MII_TG3_FET_PTEST_TRIM_2;
8168 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8169 }
8170 } else
8171 bmcr |= BMCR_LOOPBACK;
8172
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008173 tg3_writephy(tp, MII_BMCR, bmcr);
8174
8175 /* The write needs to be flushed for the FETs */
8176 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8177 tg3_readphy(tp, MII_BMCR, &bmcr);
8178
8179 udelay(40);
8180
8181 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008182 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008183 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008184 MII_TG3_FET_PTEST_FRC_TX_LINK |
8185 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8186
8187 /* The write needs to be flushed for the AC131 */
8188 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8189 }
8190
8191 /* Reset to prevent losing 1st rx packet intermittently */
8192 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8193 tg3_flag(tp, 5780_CLASS)) {
8194 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8195 udelay(10);
8196 tw32_f(MAC_RX_MODE, tp->rx_mode);
8197 }
8198
8199 mac_mode = tp->mac_mode &
8200 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8201 if (speed == SPEED_1000)
8202 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8203 else
8204 mac_mode |= MAC_MODE_PORT_MODE_MII;
8205
Joe Perches41535772013-02-16 11:20:04 +00008206 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008207 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8208
8209 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8210 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8211 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8212 mac_mode |= MAC_MODE_LINK_POLARITY;
8213
8214 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8215 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8216 }
8217
8218 tw32(MAC_MODE, mac_mode);
8219 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008220
8221 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008222}
8223
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008224static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008225{
8226 struct tg3 *tp = netdev_priv(dev);
8227
8228 if (features & NETIF_F_LOOPBACK) {
8229 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8230 return;
8231
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008232 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008233 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008234 netif_carrier_on(tp->dev);
8235 spin_unlock_bh(&tp->lock);
8236 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8237 } else {
8238 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8239 return;
8240
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008241 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008242 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008243 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008244 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008245 spin_unlock_bh(&tp->lock);
8246 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8247 }
8248}
8249
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008250static netdev_features_t tg3_fix_features(struct net_device *dev,
8251 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008252{
8253 struct tg3 *tp = netdev_priv(dev);
8254
Joe Perches63c3a662011-04-26 08:12:10 +00008255 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008256 features &= ~NETIF_F_ALL_TSO;
8257
8258 return features;
8259}
8260
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008261static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008262{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008263 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008264
8265 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8266 tg3_set_loopback(dev, features);
8267
8268 return 0;
8269}
8270
Matt Carlson21f581a2009-08-28 14:00:25 +00008271static void tg3_rx_prodring_free(struct tg3 *tp,
8272 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274 int i;
8275
Matt Carlson8fea32b2010-09-15 08:59:58 +00008276 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008277 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008278 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008279 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008280 tp->rx_pkt_map_sz);
8281
Joe Perches63c3a662011-04-26 08:12:10 +00008282 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008283 for (i = tpr->rx_jmb_cons_idx;
8284 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008285 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008286 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008287 TG3_RX_JMB_MAP_SZ);
8288 }
8289 }
8290
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008291 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293
Matt Carlson2c49a442010-09-30 10:34:35 +00008294 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008295 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008296 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297
Joe Perches63c3a662011-04-26 08:12:10 +00008298 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008299 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008300 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008301 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302 }
8303}
8304
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008305/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008306 *
8307 * The chip has been shut down and the driver detached from
8308 * the networking, so no interrupts or new tx packets will
8309 * end up in the driver. tp->{tx,}lock are held and thus
8310 * we may not sleep.
8311 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008312static int tg3_rx_prodring_alloc(struct tg3 *tp,
8313 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314{
Matt Carlson287be122009-08-28 13:58:46 +00008315 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008317 tpr->rx_std_cons_idx = 0;
8318 tpr->rx_std_prod_idx = 0;
8319 tpr->rx_jmb_cons_idx = 0;
8320 tpr->rx_jmb_prod_idx = 0;
8321
Matt Carlson8fea32b2010-09-15 08:59:58 +00008322 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008323 memset(&tpr->rx_std_buffers[0], 0,
8324 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008325 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008326 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008327 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008328 goto done;
8329 }
8330
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008332 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333
Matt Carlson287be122009-08-28 13:58:46 +00008334 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008335 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008336 tp->dev->mtu > ETH_DATA_LEN)
8337 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8338 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008339
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 /* Initialize invariants of the rings, we only set this
8341 * stuff once. This works because the card does not
8342 * write into the rx buffer posting rings.
8343 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008344 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008345 struct tg3_rx_buffer_desc *rxd;
8346
Matt Carlson21f581a2009-08-28 14:00:25 +00008347 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008348 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8350 rxd->opaque = (RXD_OPAQUE_RING_STD |
8351 (i << RXD_OPAQUE_INDEX_SHIFT));
8352 }
8353
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008354 /* Now allocate fresh SKBs for each rx ring. */
8355 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008356 unsigned int frag_size;
8357
8358 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8359 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008360 netdev_warn(tp->dev,
8361 "Using a smaller RX standard ring. Only "
8362 "%d out of %d buffers were allocated "
8363 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008364 if (i == 0)
8365 goto initfail;
8366 tp->rx_pending = i;
8367 break;
8368 }
8369 }
8370
Joe Perches63c3a662011-04-26 08:12:10 +00008371 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008372 goto done;
8373
Matt Carlson2c49a442010-09-30 10:34:35 +00008374 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008375
Joe Perches63c3a662011-04-26 08:12:10 +00008376 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008377 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378
Matt Carlson2c49a442010-09-30 10:34:35 +00008379 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008380 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381
Matt Carlson0d86df82010-02-17 15:17:00 +00008382 rxd = &tpr->rx_jmb[i].std;
8383 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8384 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8385 RXD_FLAG_JUMBO;
8386 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8387 (i << RXD_OPAQUE_INDEX_SHIFT));
8388 }
8389
8390 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008391 unsigned int frag_size;
8392
8393 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8394 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008395 netdev_warn(tp->dev,
8396 "Using a smaller RX jumbo ring. Only %d "
8397 "out of %d buffers were allocated "
8398 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008399 if (i == 0)
8400 goto initfail;
8401 tp->rx_jumbo_pending = i;
8402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008403 }
8404 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008405
8406done:
Michael Chan32d8c572006-07-25 16:38:29 -07008407 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008408
8409initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008410 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008411 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412}
8413
Matt Carlson21f581a2009-08-28 14:00:25 +00008414static void tg3_rx_prodring_fini(struct tg3 *tp,
8415 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416{
Matt Carlson21f581a2009-08-28 14:00:25 +00008417 kfree(tpr->rx_std_buffers);
8418 tpr->rx_std_buffers = NULL;
8419 kfree(tpr->rx_jmb_buffers);
8420 tpr->rx_jmb_buffers = NULL;
8421 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008422 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8423 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008424 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008426 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008427 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8428 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008429 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008431}
8432
Matt Carlson21f581a2009-08-28 14:00:25 +00008433static int tg3_rx_prodring_init(struct tg3 *tp,
8434 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008435{
Matt Carlson2c49a442010-09-30 10:34:35 +00008436 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8437 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008438 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008439 return -ENOMEM;
8440
Matt Carlson4bae65c2010-11-24 08:31:52 +00008441 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8442 TG3_RX_STD_RING_BYTES(tp),
8443 &tpr->rx_std_mapping,
8444 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008445 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008446 goto err_out;
8447
Joe Perches63c3a662011-04-26 08:12:10 +00008448 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008449 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008450 GFP_KERNEL);
8451 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008452 goto err_out;
8453
Matt Carlson4bae65c2010-11-24 08:31:52 +00008454 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8455 TG3_RX_JMB_RING_BYTES(tp),
8456 &tpr->rx_jmb_mapping,
8457 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008458 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008459 goto err_out;
8460 }
8461
8462 return 0;
8463
8464err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008465 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008466 return -ENOMEM;
8467}
8468
8469/* Free up pending packets in all rx/tx rings.
8470 *
8471 * The chip has been shut down and the driver detached from
8472 * the networking, so no interrupts or new tx packets will
8473 * end up in the driver. tp->{tx,}lock is not held and we are not
8474 * in an interrupt context and thus may sleep.
8475 */
8476static void tg3_free_rings(struct tg3 *tp)
8477{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008478 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008479
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008480 for (j = 0; j < tp->irq_cnt; j++) {
8481 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008482
Matt Carlson8fea32b2010-09-15 08:59:58 +00008483 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008484
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008485 if (!tnapi->tx_buffers)
8486 continue;
8487
Matt Carlson0d681b22011-07-27 14:20:49 +00008488 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8489 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008490
Matt Carlson0d681b22011-07-27 14:20:49 +00008491 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008492 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008493
Matt Carlsonba1142e2011-11-04 09:15:00 +00008494 tg3_tx_skb_unmap(tnapi, i,
8495 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008496
8497 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008498 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008499 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008500 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008501}
8502
8503/* Initialize tx/rx rings for packet processing.
8504 *
8505 * The chip has been shut down and the driver detached from
8506 * the networking, so no interrupts or new tx packets will
8507 * end up in the driver. tp->{tx,}lock are held and thus
8508 * we may not sleep.
8509 */
8510static int tg3_init_rings(struct tg3 *tp)
8511{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008512 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008513
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008514 /* Free up all the SKBs. */
8515 tg3_free_rings(tp);
8516
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008517 for (i = 0; i < tp->irq_cnt; i++) {
8518 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008519
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008520 tnapi->last_tag = 0;
8521 tnapi->last_irq_tag = 0;
8522 tnapi->hw_status->status = 0;
8523 tnapi->hw_status->status_tag = 0;
8524 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8525
8526 tnapi->tx_prod = 0;
8527 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008528 if (tnapi->tx_ring)
8529 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008530
8531 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008532 if (tnapi->rx_rcb)
8533 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008534
Matt Carlson8fea32b2010-09-15 08:59:58 +00008535 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008536 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008537 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008538 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008539 }
Matt Carlson72334482009-08-28 14:03:01 +00008540
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008541 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008542}
8543
Michael Chan49a359e2012-09-28 07:12:37 +00008544static void tg3_mem_tx_release(struct tg3 *tp)
8545{
8546 int i;
8547
8548 for (i = 0; i < tp->irq_max; i++) {
8549 struct tg3_napi *tnapi = &tp->napi[i];
8550
8551 if (tnapi->tx_ring) {
8552 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8553 tnapi->tx_ring, tnapi->tx_desc_mapping);
8554 tnapi->tx_ring = NULL;
8555 }
8556
8557 kfree(tnapi->tx_buffers);
8558 tnapi->tx_buffers = NULL;
8559 }
8560}
8561
8562static int tg3_mem_tx_acquire(struct tg3 *tp)
8563{
8564 int i;
8565 struct tg3_napi *tnapi = &tp->napi[0];
8566
8567 /* If multivector TSS is enabled, vector 0 does not handle
8568 * tx interrupts. Don't allocate any resources for it.
8569 */
8570 if (tg3_flag(tp, ENABLE_TSS))
8571 tnapi++;
8572
8573 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8574 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8575 TG3_TX_RING_SIZE, GFP_KERNEL);
8576 if (!tnapi->tx_buffers)
8577 goto err_out;
8578
8579 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8580 TG3_TX_RING_BYTES,
8581 &tnapi->tx_desc_mapping,
8582 GFP_KERNEL);
8583 if (!tnapi->tx_ring)
8584 goto err_out;
8585 }
8586
8587 return 0;
8588
8589err_out:
8590 tg3_mem_tx_release(tp);
8591 return -ENOMEM;
8592}
8593
8594static void tg3_mem_rx_release(struct tg3 *tp)
8595{
8596 int i;
8597
8598 for (i = 0; i < tp->irq_max; i++) {
8599 struct tg3_napi *tnapi = &tp->napi[i];
8600
8601 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8602
8603 if (!tnapi->rx_rcb)
8604 continue;
8605
8606 dma_free_coherent(&tp->pdev->dev,
8607 TG3_RX_RCB_RING_BYTES(tp),
8608 tnapi->rx_rcb,
8609 tnapi->rx_rcb_mapping);
8610 tnapi->rx_rcb = NULL;
8611 }
8612}
8613
8614static int tg3_mem_rx_acquire(struct tg3 *tp)
8615{
8616 unsigned int i, limit;
8617
8618 limit = tp->rxq_cnt;
8619
8620 /* If RSS is enabled, we need a (dummy) producer ring
8621 * set on vector zero. This is the true hw prodring.
8622 */
8623 if (tg3_flag(tp, ENABLE_RSS))
8624 limit++;
8625
8626 for (i = 0; i < limit; i++) {
8627 struct tg3_napi *tnapi = &tp->napi[i];
8628
8629 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8630 goto err_out;
8631
8632 /* If multivector RSS is enabled, vector 0
8633 * does not handle rx or tx interrupts.
8634 * Don't allocate any resources for it.
8635 */
8636 if (!i && tg3_flag(tp, ENABLE_RSS))
8637 continue;
8638
Joe Perchesede23fa82013-08-26 22:45:23 -07008639 tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
8640 TG3_RX_RCB_RING_BYTES(tp),
8641 &tnapi->rx_rcb_mapping,
8642 GFP_KERNEL);
Michael Chan49a359e2012-09-28 07:12:37 +00008643 if (!tnapi->rx_rcb)
8644 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008645 }
8646
8647 return 0;
8648
8649err_out:
8650 tg3_mem_rx_release(tp);
8651 return -ENOMEM;
8652}
8653
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008654/*
8655 * Must not be invoked with interrupt sources disabled and
8656 * the hardware shutdown down.
8657 */
8658static void tg3_free_consistent(struct tg3 *tp)
8659{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008660 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008661
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008662 for (i = 0; i < tp->irq_cnt; i++) {
8663 struct tg3_napi *tnapi = &tp->napi[i];
8664
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008665 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008666 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8667 tnapi->hw_status,
8668 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008669 tnapi->hw_status = NULL;
8670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008672
Michael Chan49a359e2012-09-28 07:12:37 +00008673 tg3_mem_rx_release(tp);
8674 tg3_mem_tx_release(tp);
8675
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008677 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8678 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679 tp->hw_stats = NULL;
8680 }
8681}
8682
8683/*
8684 * Must not be invoked with interrupt sources disabled and
8685 * the hardware shutdown down. Can sleep.
8686 */
8687static int tg3_alloc_consistent(struct tg3 *tp)
8688{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008689 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008690
Joe Perchesede23fa82013-08-26 22:45:23 -07008691 tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
8692 sizeof(struct tg3_hw_stats),
8693 &tp->stats_mapping, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008694 if (!tp->hw_stats)
8695 goto err_out;
8696
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008697 for (i = 0; i < tp->irq_cnt; i++) {
8698 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008699 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008700
Joe Perchesede23fa82013-08-26 22:45:23 -07008701 tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
8702 TG3_HW_STATUS_SIZE,
8703 &tnapi->status_mapping,
8704 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008705 if (!tnapi->hw_status)
8706 goto err_out;
8707
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008708 sblk = tnapi->hw_status;
8709
Michael Chan49a359e2012-09-28 07:12:37 +00008710 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008711 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008712
Michael Chan49a359e2012-09-28 07:12:37 +00008713 /*
8714 * When RSS is enabled, the status block format changes
8715 * slightly. The "rx_jumbo_consumer", "reserved",
8716 * and "rx_mini_consumer" members get mapped to the
8717 * other three rx return ring producer indexes.
8718 */
8719 switch (i) {
8720 case 1:
8721 prodptr = &sblk->idx[0].rx_producer;
8722 break;
8723 case 2:
8724 prodptr = &sblk->rx_jumbo_consumer;
8725 break;
8726 case 3:
8727 prodptr = &sblk->reserved;
8728 break;
8729 case 4:
8730 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008731 break;
8732 }
Michael Chan49a359e2012-09-28 07:12:37 +00008733 tnapi->rx_rcb_prod_idx = prodptr;
8734 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008735 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008736 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008737 }
8738
Michael Chan49a359e2012-09-28 07:12:37 +00008739 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8740 goto err_out;
8741
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742 return 0;
8743
8744err_out:
8745 tg3_free_consistent(tp);
8746 return -ENOMEM;
8747}
8748
8749#define MAX_WAIT_CNT 1000
8750
8751/* To stop a block, clear the enable bit and poll till it
8752 * clears. tp->lock is held.
8753 */
Joe Perches953c96e2013-04-09 10:18:14 +00008754static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755{
8756 unsigned int i;
8757 u32 val;
8758
Joe Perches63c3a662011-04-26 08:12:10 +00008759 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008760 switch (ofs) {
8761 case RCVLSC_MODE:
8762 case DMAC_MODE:
8763 case MBFREE_MODE:
8764 case BUFMGR_MODE:
8765 case MEMARB_MODE:
8766 /* We can't enable/disable these bits of the
8767 * 5705/5750, just say success.
8768 */
8769 return 0;
8770
8771 default:
8772 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008774 }
8775
8776 val = tr32(ofs);
8777 val &= ~enable_bit;
8778 tw32_f(ofs, val);
8779
8780 for (i = 0; i < MAX_WAIT_CNT; i++) {
Gavin Shan6d446ec2013-06-25 15:24:32 +08008781 if (pci_channel_offline(tp->pdev)) {
8782 dev_err(&tp->pdev->dev,
8783 "tg3_stop_block device offline, "
8784 "ofs=%lx enable_bit=%x\n",
8785 ofs, enable_bit);
8786 return -ENODEV;
8787 }
8788
Linus Torvalds1da177e2005-04-16 15:20:36 -07008789 udelay(100);
8790 val = tr32(ofs);
8791 if ((val & enable_bit) == 0)
8792 break;
8793 }
8794
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008795 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008796 dev_err(&tp->pdev->dev,
8797 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8798 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799 return -ENODEV;
8800 }
8801
8802 return 0;
8803}
8804
8805/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008806static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008807{
8808 int i, err;
8809
8810 tg3_disable_ints(tp);
8811
Gavin Shan6d446ec2013-06-25 15:24:32 +08008812 if (pci_channel_offline(tp->pdev)) {
8813 tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
8814 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8815 err = -ENODEV;
8816 goto err_no_dev;
8817 }
8818
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819 tp->rx_mode &= ~RX_MODE_ENABLE;
8820 tw32_f(MAC_RX_MODE, tp->rx_mode);
8821 udelay(10);
8822
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008823 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8824 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8825 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8826 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8827 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8828 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008829
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008830 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8831 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8832 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8833 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8834 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8835 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8836 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008837
8838 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8839 tw32_f(MAC_MODE, tp->mac_mode);
8840 udelay(40);
8841
8842 tp->tx_mode &= ~TX_MODE_ENABLE;
8843 tw32_f(MAC_TX_MODE, tp->tx_mode);
8844
8845 for (i = 0; i < MAX_WAIT_CNT; i++) {
8846 udelay(100);
8847 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8848 break;
8849 }
8850 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008851 dev_err(&tp->pdev->dev,
8852 "%s timed out, TX_MODE_ENABLE will not clear "
8853 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008854 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008855 }
8856
Michael Chane6de8ad2005-05-05 14:42:41 -07008857 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008858 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8859 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860
8861 tw32(FTQ_RESET, 0xffffffff);
8862 tw32(FTQ_RESET, 0x00000000);
8863
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008864 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8865 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866
Gavin Shan6d446ec2013-06-25 15:24:32 +08008867err_no_dev:
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008868 for (i = 0; i < tp->irq_cnt; i++) {
8869 struct tg3_napi *tnapi = &tp->napi[i];
8870 if (tnapi->hw_status)
8871 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874 return err;
8875}
8876
Michael Chanee6a99b2007-07-18 21:49:10 -07008877/* Save PCI command register before chip reset */
8878static void tg3_save_pci_state(struct tg3 *tp)
8879{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008880 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008881}
8882
8883/* Restore PCI state after chip reset */
8884static void tg3_restore_pci_state(struct tg3 *tp)
8885{
8886 u32 val;
8887
8888 /* Re-enable indirect register accesses. */
8889 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8890 tp->misc_host_ctrl);
8891
8892 /* Set MAX PCI retry to zero. */
8893 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008894 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008895 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008896 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008897 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008898 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008899 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008900 PCISTATE_ALLOW_APE_SHMEM_WR |
8901 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008902 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8903
Matt Carlson8a6eac92007-10-21 16:17:55 -07008904 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008905
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008906 if (!tg3_flag(tp, PCI_EXPRESS)) {
8907 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8908 tp->pci_cacheline_sz);
8909 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8910 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008911 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008912
Michael Chanee6a99b2007-07-18 21:49:10 -07008913 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008914 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008915 u16 pcix_cmd;
8916
8917 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8918 &pcix_cmd);
8919 pcix_cmd &= ~PCI_X_CMD_ERO;
8920 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8921 pcix_cmd);
8922 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008923
Joe Perches63c3a662011-04-26 08:12:10 +00008924 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008925
8926 /* Chip reset on 5780 will reset MSI enable bit,
8927 * so need to restore it.
8928 */
Joe Perches63c3a662011-04-26 08:12:10 +00008929 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008930 u16 ctrl;
8931
8932 pci_read_config_word(tp->pdev,
8933 tp->msi_cap + PCI_MSI_FLAGS,
8934 &ctrl);
8935 pci_write_config_word(tp->pdev,
8936 tp->msi_cap + PCI_MSI_FLAGS,
8937 ctrl | PCI_MSI_FLAGS_ENABLE);
8938 val = tr32(MSGINT_MODE);
8939 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8940 }
8941 }
8942}
8943
Nithin Sujirf82995b2014-01-03 10:09:13 -08008944static void tg3_override_clk(struct tg3 *tp)
8945{
8946 u32 val;
8947
8948 switch (tg3_asic_rev(tp)) {
8949 case ASIC_REV_5717:
8950 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
8951 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
8952 TG3_CPMU_MAC_ORIDE_ENABLE);
8953 break;
8954
8955 case ASIC_REV_5719:
8956 case ASIC_REV_5720:
8957 tw32(TG3_CPMU_CLCK_ORIDE, CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8958 break;
8959
8960 default:
8961 return;
8962 }
8963}
8964
8965static void tg3_restore_clk(struct tg3 *tp)
8966{
8967 u32 val;
8968
8969 switch (tg3_asic_rev(tp)) {
8970 case ASIC_REV_5717:
8971 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
8972 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE,
8973 val & ~TG3_CPMU_MAC_ORIDE_ENABLE);
8974 break;
8975
8976 case ASIC_REV_5719:
8977 case ASIC_REV_5720:
8978 val = tr32(TG3_CPMU_CLCK_ORIDE);
8979 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8980 break;
8981
8982 default:
8983 return;
8984 }
8985}
8986
Linus Torvalds1da177e2005-04-16 15:20:36 -07008987/* tp->lock is held. */
8988static int tg3_chip_reset(struct tg3 *tp)
8989{
8990 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008991 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008992 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01008994 if (!pci_device_is_present(tp->pdev))
8995 return -ENODEV;
8996
David S. Millerf49639e2006-06-09 11:58:36 -07008997 tg3_nvram_lock(tp);
8998
Matt Carlson77b483f2008-08-15 14:07:24 -07008999 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
9000
David S. Millerf49639e2006-06-09 11:58:36 -07009001 /* No matching tg3_nvram_unlock() after this because
9002 * chip reset below will undo the nvram lock.
9003 */
9004 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005
Michael Chanee6a99b2007-07-18 21:49:10 -07009006 /* GRC_MISC_CFG core clock reset will clear the memory
9007 * enable bit in PCI register 4 and the MSI enable bit
9008 * on some chips, so we save relevant registers here.
9009 */
9010 tg3_save_pci_state(tp);
9011
Joe Perches41535772013-02-16 11:20:04 +00009012 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009013 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08009014 tw32(GRC_FASTBOOT_PC, 0);
9015
Linus Torvalds1da177e2005-04-16 15:20:36 -07009016 /*
9017 * We must avoid the readl() that normally takes place.
9018 * It locks machines, causes machine checks, and other
9019 * fun things. So, temporarily disable the 5701
9020 * hardware workaround, while we do the reset.
9021 */
Michael Chan1ee582d2005-08-09 20:16:46 -07009022 write_op = tp->write32;
9023 if (write_op == tg3_write_flush_reg32)
9024 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025
Michael Chand18edcb2007-03-24 20:57:11 -07009026 /* Prevent the irq handler from reading or writing PCI registers
9027 * during chip reset when the memory enable bit in the PCI command
9028 * register may be cleared. The chip does not generate interrupt
9029 * at this time, but the irq handler may still be called due to irq
9030 * sharing or irqpoll.
9031 */
Joe Perches63c3a662011-04-26 08:12:10 +00009032 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009033 for (i = 0; i < tp->irq_cnt; i++) {
9034 struct tg3_napi *tnapi = &tp->napi[i];
9035 if (tnapi->hw_status) {
9036 tnapi->hw_status->status = 0;
9037 tnapi->hw_status->status_tag = 0;
9038 }
9039 tnapi->last_tag = 0;
9040 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07009041 }
Michael Chand18edcb2007-03-24 20:57:11 -07009042 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00009043
9044 for (i = 0; i < tp->irq_cnt; i++)
9045 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07009046
Joe Perches41535772013-02-16 11:20:04 +00009047 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00009048 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9049 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
9050 }
9051
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052 /* do the reset */
9053 val = GRC_MISC_CFG_CORECLK_RESET;
9054
Joe Perches63c3a662011-04-26 08:12:10 +00009055 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00009056 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00009057 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009058 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00009059 tr32(TG3_PCIE_PHY_TSTCTL) ==
9060 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
9061 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
9062
Joe Perches41535772013-02-16 11:20:04 +00009063 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009064 tw32(GRC_MISC_CFG, (1 << 29));
9065 val |= (1 << 29);
9066 }
9067 }
9068
Joe Perches41535772013-02-16 11:20:04 +00009069 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07009070 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
9071 tw32(GRC_VCPU_EXT_CTRL,
9072 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
9073 }
9074
Nithin Sujirf82995b2014-01-03 10:09:13 -08009075 /* Set the clock to the highest frequency to avoid timeouts. With link
9076 * aware mode, the clock speed could be slow and bootcode does not
9077 * complete within the expected time. Override the clock to allow the
9078 * bootcode to finish sooner and then restore it.
9079 */
9080 tg3_override_clk(tp);
9081
Matt Carlsonf37500d2010-08-02 11:25:59 +00009082 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00009083 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009084 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00009085
Linus Torvalds1da177e2005-04-16 15:20:36 -07009086 tw32(GRC_MISC_CFG, val);
9087
Michael Chan1ee582d2005-08-09 20:16:46 -07009088 /* restore 5701 hardware bug workaround write method */
9089 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009090
9091 /* Unfortunately, we have to delay before the PCI read back.
9092 * Some 575X chips even will not respond to a PCI cfg access
9093 * when the reset command is given to the chip.
9094 *
9095 * How do these hardware designers expect things to work
9096 * properly if the PCI write is posted for a long period
9097 * of time? It is always necessary to have some method by
9098 * which a register read back can occur to push the write
9099 * out which does the reset.
9100 *
9101 * For most tg3 variants the trick below was working.
9102 * Ho hum...
9103 */
9104 udelay(120);
9105
9106 /* Flush PCI posted writes. The normal MMIO registers
9107 * are inaccessible at this time so this is the only
9108 * way to make this reliably (actually, this is no longer
9109 * the case, see above). I tried to use indirect
9110 * register read/write but this upset some 5701 variants.
9111 */
9112 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
9113
9114 udelay(120);
9115
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009116 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00009117 u16 val16;
9118
Joe Perches41535772013-02-16 11:20:04 +00009119 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07009120 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009121 u32 cfg_val;
9122
9123 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07009124 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125 udelay(100);
9126
9127 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
9128 pci_write_config_dword(tp->pdev, 0xc4,
9129 cfg_val | (1 << 15));
9130 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009131
Matt Carlsone7126992009-08-25 10:08:16 +00009132 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009133 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00009134 /*
9135 * Older PCIe devices only support the 128 byte
9136 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009137 */
Joe Perches63c3a662011-04-26 08:12:10 +00009138 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009139 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
9140 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009141
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009142 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009143 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009144 PCI_EXP_DEVSTA_CED |
9145 PCI_EXP_DEVSTA_NFED |
9146 PCI_EXP_DEVSTA_FED |
9147 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009148 }
9149
Michael Chanee6a99b2007-07-18 21:49:10 -07009150 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009151
Joe Perches63c3a662011-04-26 08:12:10 +00009152 tg3_flag_clear(tp, CHIP_RESETTING);
9153 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07009154
Michael Chanee6a99b2007-07-18 21:49:10 -07009155 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009156 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07009157 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07009158 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159
Joe Perches41535772013-02-16 11:20:04 +00009160 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161 tg3_stop_fw(tp);
9162 tw32(0x5000, 0x400);
9163 }
9164
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00009165 if (tg3_flag(tp, IS_SSB_CORE)) {
9166 /*
9167 * BCM4785: In order to avoid repercussions from using
9168 * potentially defective internal ROM, stop the Rx RISC CPU,
9169 * which is not required.
9170 */
9171 tg3_stop_fw(tp);
9172 tg3_halt_cpu(tp, RX_CPU_BASE);
9173 }
9174
Nithin Sujirfb03a432013-05-21 12:57:32 +00009175 err = tg3_poll_fw(tp);
9176 if (err)
9177 return err;
9178
Linus Torvalds1da177e2005-04-16 15:20:36 -07009179 tw32(GRC_MODE, tp->grc_mode);
9180
Joe Perches41535772013-02-16 11:20:04 +00009181 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009182 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009183
9184 tw32(0xc4, val | (1 << 15));
9185 }
9186
9187 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009188 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009189 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009190 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009191 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9192 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9193 }
9194
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009195 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009196 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009197 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009198 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009199 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009200 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009201 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009202 val = 0;
9203
9204 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205 udelay(40);
9206
Matt Carlson77b483f2008-08-15 14:07:24 -07009207 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9208
Matt Carlson0a9140c2009-08-28 12:27:50 +00009209 tg3_mdio_start(tp);
9210
Joe Perches63c3a662011-04-26 08:12:10 +00009211 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009212 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9213 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009214 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009215 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216
9217 tw32(0x7c00, val | (1 << 25));
9218 }
9219
Nithin Sujirf82995b2014-01-03 10:09:13 -08009220 tg3_restore_clk(tp);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009221
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009223 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009224 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9225 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9226
Joe Perches63c3a662011-04-26 08:12:10 +00009227 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9229 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9230 u32 nic_cfg;
9231
9232 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9233 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009234 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009235 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009236 if (tg3_flag(tp, 5750_PLUS))
9237 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009238
9239 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9240 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9241 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9242 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9243 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009244 }
9245 }
9246
9247 return 0;
9248}
9249
Matt Carlson65ec6982012-02-28 23:33:37 +00009250static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9251static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Michael Chane565eec2014-01-03 10:09:12 -08009252static void __tg3_set_rx_mode(struct net_device *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009253
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009255static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256{
9257 int err;
9258
9259 tg3_stop_fw(tp);
9260
Michael Chan944d9802005-05-29 14:57:48 -07009261 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009263 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009264 err = tg3_chip_reset(tp);
9265
Joe Perches953c96e2013-04-09 10:18:14 +00009266 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009267
Michael Chan944d9802005-05-29 14:57:48 -07009268 tg3_write_sig_legacy(tp, kind);
9269 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009270
Matt Carlson92feeab2011-12-08 14:40:14 +00009271 if (tp->hw_stats) {
9272 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009273 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009274 tg3_get_estats(tp, &tp->estats_prev);
9275
9276 /* And make sure the next sample is new data */
9277 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9278 }
9279
Nithin Sujir4bc814a2013-09-20 16:46:59 -07009280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281}
9282
Linus Torvalds1da177e2005-04-16 15:20:36 -07009283static int tg3_set_mac_addr(struct net_device *dev, void *p)
9284{
9285 struct tg3 *tp = netdev_priv(dev);
9286 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009287 int err = 0;
9288 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009289
Michael Chanf9804dd2005-09-27 12:13:10 -07009290 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009291 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009292
Linus Torvalds1da177e2005-04-16 15:20:36 -07009293 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9294
Michael Chane75f7c92006-03-20 21:33:26 -08009295 if (!netif_running(dev))
9296 return 0;
9297
Joe Perches63c3a662011-04-26 08:12:10 +00009298 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009299 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009300
Michael Chan986e0ae2007-05-05 12:10:20 -07009301 addr0_high = tr32(MAC_ADDR_0_HIGH);
9302 addr0_low = tr32(MAC_ADDR_0_LOW);
9303 addr1_high = tr32(MAC_ADDR_1_HIGH);
9304 addr1_low = tr32(MAC_ADDR_1_LOW);
9305
9306 /* Skip MAC addr 1 if ASF is using it. */
9307 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9308 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009309 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009310 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009311 spin_lock_bh(&tp->lock);
9312 __tg3_set_mac_addr(tp, skip_mac_1);
Michael Chane565eec2014-01-03 10:09:12 -08009313 __tg3_set_rx_mode(dev);
Michael Chan986e0ae2007-05-05 12:10:20 -07009314 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315
Michael Chanb9ec6c12006-07-25 16:37:27 -07009316 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317}
9318
9319/* tp->lock is held. */
9320static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9321 dma_addr_t mapping, u32 maxlen_flags,
9322 u32 nic_addr)
9323{
9324 tg3_write_mem(tp,
9325 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9326 ((u64) mapping >> 32));
9327 tg3_write_mem(tp,
9328 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9329 ((u64) mapping & 0xffffffff));
9330 tg3_write_mem(tp,
9331 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9332 maxlen_flags);
9333
Joe Perches63c3a662011-04-26 08:12:10 +00009334 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335 tg3_write_mem(tp,
9336 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9337 nic_addr);
9338}
9339
Michael Chana489b6d2012-09-28 07:12:39 +00009340
9341static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009342{
Michael Chana489b6d2012-09-28 07:12:39 +00009343 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009344
Joe Perches63c3a662011-04-26 08:12:10 +00009345 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009346 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9347 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9348 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009349 } else {
9350 tw32(HOSTCC_TXCOL_TICKS, 0);
9351 tw32(HOSTCC_TXMAX_FRAMES, 0);
9352 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009353
9354 for (; i < tp->txq_cnt; i++) {
9355 u32 reg;
9356
9357 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9358 tw32(reg, ec->tx_coalesce_usecs);
9359 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9360 tw32(reg, ec->tx_max_coalesced_frames);
9361 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9362 tw32(reg, ec->tx_max_coalesced_frames_irq);
9363 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009364 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009365
Michael Chana489b6d2012-09-28 07:12:39 +00009366 for (; i < tp->irq_max - 1; i++) {
9367 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9368 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9369 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9370 }
9371}
9372
9373static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9374{
9375 int i = 0;
9376 u32 limit = tp->rxq_cnt;
9377
Joe Perches63c3a662011-04-26 08:12:10 +00009378 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009379 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9380 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9381 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009382 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009383 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009384 tw32(HOSTCC_RXCOL_TICKS, 0);
9385 tw32(HOSTCC_RXMAX_FRAMES, 0);
9386 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009387 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009388
Michael Chana489b6d2012-09-28 07:12:39 +00009389 for (; i < limit; i++) {
9390 u32 reg;
9391
9392 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9393 tw32(reg, ec->rx_coalesce_usecs);
9394 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9395 tw32(reg, ec->rx_max_coalesced_frames);
9396 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9397 tw32(reg, ec->rx_max_coalesced_frames_irq);
9398 }
9399
9400 for (; i < tp->irq_max - 1; i++) {
9401 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9402 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9403 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9404 }
9405}
9406
9407static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9408{
9409 tg3_coal_tx_init(tp, ec);
9410 tg3_coal_rx_init(tp, ec);
9411
Joe Perches63c3a662011-04-26 08:12:10 +00009412 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009413 u32 val = ec->stats_block_coalesce_usecs;
9414
Matt Carlsonb6080e12009-09-01 13:12:00 +00009415 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9416 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9417
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009418 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009419 val = 0;
9420
9421 tw32(HOSTCC_STAT_COAL_TICKS, val);
9422 }
9423}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424
9425/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009426static void tg3_tx_rcbs_disable(struct tg3 *tp)
9427{
9428 u32 txrcb, limit;
9429
9430 /* Disable all transmit rings but the first. */
9431 if (!tg3_flag(tp, 5705_PLUS))
9432 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
9433 else if (tg3_flag(tp, 5717_PLUS))
9434 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
9435 else if (tg3_flag(tp, 57765_CLASS) ||
9436 tg3_asic_rev(tp) == ASIC_REV_5762)
9437 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
9438 else
9439 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9440
9441 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9442 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9443 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9444 BDINFO_FLAGS_DISABLED);
9445}
9446
9447/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009448static void tg3_tx_rcbs_init(struct tg3 *tp)
9449{
9450 int i = 0;
9451 u32 txrcb = NIC_SRAM_SEND_RCB;
9452
9453 if (tg3_flag(tp, ENABLE_TSS))
9454 i++;
9455
9456 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9457 struct tg3_napi *tnapi = &tp->napi[i];
9458
9459 if (!tnapi->tx_ring)
9460 continue;
9461
9462 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9463 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9464 NIC_SRAM_TX_BUFFER_DESC);
9465 }
9466}
9467
9468/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009469static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
9470{
9471 u32 rxrcb, limit;
9472
9473 /* Disable all receive return rings but the first. */
9474 if (tg3_flag(tp, 5717_PLUS))
9475 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
9476 else if (!tg3_flag(tp, 5705_PLUS))
9477 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
9478 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9479 tg3_asic_rev(tp) == ASIC_REV_5762 ||
9480 tg3_flag(tp, 57765_CLASS))
9481 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9482 else
9483 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9484
9485 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9486 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9487 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9488 BDINFO_FLAGS_DISABLED);
9489}
9490
9491/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009492static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9493{
9494 int i = 0;
9495 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9496
9497 if (tg3_flag(tp, ENABLE_RSS))
9498 i++;
9499
9500 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9501 struct tg3_napi *tnapi = &tp->napi[i];
9502
9503 if (!tnapi->rx_rcb)
9504 continue;
9505
9506 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9507 (tp->rx_ret_ring_mask + 1) <<
9508 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9509 }
9510}
9511
9512/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009513static void tg3_rings_reset(struct tg3 *tp)
9514{
9515 int i;
Nithin Sujir328947f2013-05-23 11:11:24 +00009516 u32 stblk;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009517 struct tg3_napi *tnapi = &tp->napi[0];
9518
Nithin Sujir328947f2013-05-23 11:11:24 +00009519 tg3_tx_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009520
Nithin Sujir328947f2013-05-23 11:11:24 +00009521 tg3_rx_ret_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009522
9523 /* Disable interrupts */
9524 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009525 tp->napi[0].chk_msi_cnt = 0;
9526 tp->napi[0].last_rx_cons = 0;
9527 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009528
9529 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009530 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009531 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009532 tp->napi[i].tx_prod = 0;
9533 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009534 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009535 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009536 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9537 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009538 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009539 tp->napi[i].last_rx_cons = 0;
9540 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009541 }
Joe Perches63c3a662011-04-26 08:12:10 +00009542 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009543 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009544 } else {
9545 tp->napi[0].tx_prod = 0;
9546 tp->napi[0].tx_cons = 0;
9547 tw32_mailbox(tp->napi[0].prodmbox, 0);
9548 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9549 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009550
9551 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009552 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009553 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9554 for (i = 0; i < 16; i++)
9555 tw32_tx_mbox(mbox + i * 8, 0);
9556 }
9557
Matt Carlson2d31eca2009-09-01 12:53:31 +00009558 /* Clear status block in ram. */
9559 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9560
9561 /* Set status block DMA address */
9562 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9563 ((u64) tnapi->status_mapping >> 32));
9564 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9565 ((u64) tnapi->status_mapping & 0xffffffff));
9566
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009567 stblk = HOSTCC_STATBLCK_RING1;
9568
9569 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9570 u64 mapping = (u64)tnapi->status_mapping;
9571 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9572 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009573 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009574
9575 /* Clear status block in ram. */
9576 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009577 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009578
9579 tg3_tx_rcbs_init(tp);
9580 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009581}
9582
Matt Carlsoneb07a942011-04-20 07:57:36 +00009583static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9584{
9585 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9586
Joe Perches63c3a662011-04-26 08:12:10 +00009587 if (!tg3_flag(tp, 5750_PLUS) ||
9588 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009589 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9590 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009591 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009592 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009593 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9594 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009595 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9596 else
9597 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9598
9599 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9600 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9601
9602 val = min(nic_rep_thresh, host_rep_thresh);
9603 tw32(RCVBDI_STD_THRESH, val);
9604
Joe Perches63c3a662011-04-26 08:12:10 +00009605 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009606 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9607
Joe Perches63c3a662011-04-26 08:12:10 +00009608 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009609 return;
9610
Matt Carlson513aa6e2011-11-21 15:01:18 +00009611 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009612
9613 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9614
9615 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9616 tw32(RCVBDI_JUMBO_THRESH, val);
9617
Joe Perches63c3a662011-04-26 08:12:10 +00009618 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009619 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9620}
9621
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009622static inline u32 calc_crc(unsigned char *buf, int len)
9623{
9624 u32 reg;
9625 u32 tmp;
9626 int j, k;
9627
9628 reg = 0xffffffff;
9629
9630 for (j = 0; j < len; j++) {
9631 reg ^= buf[j];
9632
9633 for (k = 0; k < 8; k++) {
9634 tmp = reg & 0x01;
9635
9636 reg >>= 1;
9637
9638 if (tmp)
9639 reg ^= 0xedb88320;
9640 }
9641 }
9642
9643 return ~reg;
9644}
9645
9646static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9647{
9648 /* accept or reject all multicast frames */
9649 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9650 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9651 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9652 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9653}
9654
9655static void __tg3_set_rx_mode(struct net_device *dev)
9656{
9657 struct tg3 *tp = netdev_priv(dev);
9658 u32 rx_mode;
9659
9660 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9661 RX_MODE_KEEP_VLAN_TAG);
9662
9663#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9664 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9665 * flag clear.
9666 */
9667 if (!tg3_flag(tp, ENABLE_ASF))
9668 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9669#endif
9670
9671 if (dev->flags & IFF_PROMISC) {
9672 /* Promiscuous mode. */
9673 rx_mode |= RX_MODE_PROMISC;
9674 } else if (dev->flags & IFF_ALLMULTI) {
9675 /* Accept all multicast. */
9676 tg3_set_multi(tp, 1);
9677 } else if (netdev_mc_empty(dev)) {
9678 /* Reject all multicast. */
9679 tg3_set_multi(tp, 0);
9680 } else {
9681 /* Accept one or more multicast(s). */
9682 struct netdev_hw_addr *ha;
9683 u32 mc_filter[4] = { 0, };
9684 u32 regidx;
9685 u32 bit;
9686 u32 crc;
9687
9688 netdev_for_each_mc_addr(ha, dev) {
9689 crc = calc_crc(ha->addr, ETH_ALEN);
9690 bit = ~crc & 0x7f;
9691 regidx = (bit & 0x60) >> 5;
9692 bit &= 0x1f;
9693 mc_filter[regidx] |= (1 << bit);
9694 }
9695
9696 tw32(MAC_HASH_REG_0, mc_filter[0]);
9697 tw32(MAC_HASH_REG_1, mc_filter[1]);
9698 tw32(MAC_HASH_REG_2, mc_filter[2]);
9699 tw32(MAC_HASH_REG_3, mc_filter[3]);
9700 }
9701
Michael Chane565eec2014-01-03 10:09:12 -08009702 if (netdev_uc_count(dev) > TG3_MAX_UCAST_ADDR(tp)) {
9703 rx_mode |= RX_MODE_PROMISC;
9704 } else if (!(dev->flags & IFF_PROMISC)) {
9705 /* Add all entries into to the mac addr filter list */
9706 int i = 0;
9707 struct netdev_hw_addr *ha;
9708
9709 netdev_for_each_uc_addr(ha, dev) {
9710 __tg3_set_one_mac_addr(tp, ha->addr,
9711 i + TG3_UCAST_ADDR_IDX(tp));
9712 i++;
9713 }
9714 }
9715
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009716 if (rx_mode != tp->rx_mode) {
9717 tp->rx_mode = rx_mode;
9718 tw32_f(MAC_RX_MODE, rx_mode);
9719 udelay(10);
9720 }
9721}
9722
Michael Chan91024262012-09-28 07:12:38 +00009723static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009724{
9725 int i;
9726
9727 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009728 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009729}
9730
9731static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009732{
9733 int i;
9734
9735 if (!tg3_flag(tp, SUPPORT_MSIX))
9736 return;
9737
Michael Chan0b3ba052012-11-14 14:44:29 +00009738 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009739 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009740 return;
9741 }
9742
9743 /* Validate table against current IRQ count */
9744 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009745 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009746 break;
9747 }
9748
9749 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009750 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009751}
9752
Matt Carlson90415472011-12-16 13:33:23 +00009753static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009754{
9755 int i = 0;
9756 u32 reg = MAC_RSS_INDIR_TBL_0;
9757
9758 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9759 u32 val = tp->rss_ind_tbl[i];
9760 i++;
9761 for (; i % 8; i++) {
9762 val <<= 4;
9763 val |= tp->rss_ind_tbl[i];
9764 }
9765 tw32(reg, val);
9766 reg += 4;
9767 }
9768}
9769
Nithin Sujir9bc297e2013-06-03 09:19:34 +00009770static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
9771{
9772 if (tg3_asic_rev(tp) == ASIC_REV_5719)
9773 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
9774 else
9775 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
9776}
9777
Matt Carlson2d31eca2009-09-01 12:53:31 +00009778/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009779static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780{
9781 u32 val, rdmac_mode;
9782 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009783 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784
9785 tg3_disable_ints(tp);
9786
9787 tg3_stop_fw(tp);
9788
9789 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9790
Joe Perches63c3a662011-04-26 08:12:10 +00009791 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009792 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009794 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9795 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9796 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009797 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009798 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9799 }
9800
Nithin Sujir400dfba2013-05-18 06:26:53 +00009801 /* Enable MAC control of LPI */
9802 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9803 tg3_setup_eee(tp);
9804
Matt Carlson603f1172010-02-12 14:47:10 +00009805 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009806 tg3_phy_reset(tp);
9807
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808 err = tg3_chip_reset(tp);
9809 if (err)
9810 return err;
9811
9812 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9813
Joe Perches41535772013-02-16 11:20:04 +00009814 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009815 val = tr32(TG3_CPMU_CTRL);
9816 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9817 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009818
9819 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9820 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9821 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9822 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9823
9824 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9825 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9826 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9827 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9828
9829 val = tr32(TG3_CPMU_HST_ACC);
9830 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9831 val |= CPMU_HST_ACC_MACCLK_6_25;
9832 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009833 }
9834
Joe Perches41535772013-02-16 11:20:04 +00009835 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009836 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9837 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9838 PCIE_PWR_MGMT_L1_THRESH_4MS;
9839 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009840
9841 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9842 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9843
9844 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009845
Matt Carlsonf40386c2009-11-02 14:24:02 +00009846 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9847 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009848 }
9849
Joe Perches63c3a662011-04-26 08:12:10 +00009850 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009851 u32 grc_mode = tr32(GRC_MODE);
9852
9853 /* Access the lower 1K of PL PCIE block registers. */
9854 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9855 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9856
9857 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9858 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9859 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9860
9861 tw32(GRC_MODE, grc_mode);
9862 }
9863
Matt Carlson55086ad2011-12-14 11:09:59 +00009864 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009865 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009866 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009867
Matt Carlson5093eed2010-11-24 08:31:45 +00009868 /* Access the lower 1K of PL PCIE block registers. */
9869 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9870 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009871
Matt Carlson5093eed2010-11-24 08:31:45 +00009872 val = tr32(TG3_PCIE_TLDLPL_PORT +
9873 TG3_PCIE_PL_LO_PHYCTL5);
9874 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9875 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009876
Matt Carlson5093eed2010-11-24 08:31:45 +00009877 tw32(GRC_MODE, grc_mode);
9878 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009879
Joe Perches41535772013-02-16 11:20:04 +00009880 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009881 u32 grc_mode;
9882
9883 /* Fix transmit hangs */
9884 val = tr32(TG3_CPMU_PADRNG_CTL);
9885 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9886 tw32(TG3_CPMU_PADRNG_CTL, val);
9887
9888 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009889
9890 /* Access the lower 1K of DL PCIE block registers. */
9891 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9892 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9893
9894 val = tr32(TG3_PCIE_TLDLPL_PORT +
9895 TG3_PCIE_DL_LO_FTSMAX);
9896 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9897 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9898 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9899
9900 tw32(GRC_MODE, grc_mode);
9901 }
9902
Matt Carlsona977dbe2010-04-12 06:58:26 +00009903 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9904 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9905 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9906 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009907 }
9908
Linus Torvalds1da177e2005-04-16 15:20:36 -07009909 /* This works around an issue with Athlon chipsets on
9910 * B3 tigon3 silicon. This bit has no effect on any
9911 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009912 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009913 */
Joe Perches63c3a662011-04-26 08:12:10 +00009914 if (!tg3_flag(tp, CPMU_PRESENT)) {
9915 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009916 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9917 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009919
Joe Perches41535772013-02-16 11:20:04 +00009920 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009921 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009922 val = tr32(TG3PCI_PCISTATE);
9923 val |= PCISTATE_RETRY_SAME_DMA;
9924 tw32(TG3PCI_PCISTATE, val);
9925 }
9926
Joe Perches63c3a662011-04-26 08:12:10 +00009927 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009928 /* Allow reads and writes to the
9929 * APE register and memory space.
9930 */
9931 val = tr32(TG3PCI_PCISTATE);
9932 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009933 PCISTATE_ALLOW_APE_SHMEM_WR |
9934 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009935 tw32(TG3PCI_PCISTATE, val);
9936 }
9937
Joe Perches41535772013-02-16 11:20:04 +00009938 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009939 /* Enable some hw fixes. */
9940 val = tr32(TG3PCI_MSI_DATA);
9941 val |= (1 << 26) | (1 << 28) | (1 << 29);
9942 tw32(TG3PCI_MSI_DATA, val);
9943 }
9944
9945 /* Descriptor ring init may make accesses to the
9946 * NIC SRAM area to setup the TX descriptors, so we
9947 * can only do this after the hardware has been
9948 * successfully reset.
9949 */
Michael Chan32d8c572006-07-25 16:38:29 -07009950 err = tg3_init_rings(tp);
9951 if (err)
9952 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953
Joe Perches63c3a662011-04-26 08:12:10 +00009954 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009955 val = tr32(TG3PCI_DMA_RW_CTRL) &
9956 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009957 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009958 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009959 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009960 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9961 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009962 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009963 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009964 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9965 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009966 /* This value is determined during the probe time DMA
9967 * engine test, tg3_test_dma.
9968 */
9969 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971
9972 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9973 GRC_MODE_4X_NIC_SEND_RINGS |
9974 GRC_MODE_NO_TX_PHDR_CSUM |
9975 GRC_MODE_NO_RX_PHDR_CSUM);
9976 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009977
9978 /* Pseudo-header checksum is done by hardware logic and not
9979 * the offload processers, so make the chip do the pseudo-
9980 * header checksums on receive. For transmit it is more
9981 * convenient to do the pseudo-header checksum in software
9982 * as Linux does that on transmit for us in all cases.
9983 */
9984 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009985
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009986 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9987 if (tp->rxptpctl)
9988 tw32(TG3_RX_PTP_CTL,
9989 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9990
9991 if (tg3_flag(tp, PTP_CAPABLE))
9992 val |= GRC_MODE_TIME_SYNC_ENABLE;
9993
9994 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009995
9996 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9997 val = tr32(GRC_MISC_CFG);
9998 val &= ~0xff;
9999 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
10000 tw32(GRC_MISC_CFG, val);
10001
10002 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +000010003 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010004 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +000010005 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +000010007 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010008 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
10009 else
10010 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
10011 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
10012 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +000010013 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010014 int fw_len;
10015
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080010016 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010017 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
10018 tw32(BUFMGR_MB_POOL_ADDR,
10019 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
10020 tw32(BUFMGR_MB_POOL_SIZE,
10021 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
10022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010023
Michael Chan0f893dc2005-07-25 12:30:38 -070010024 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010025 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10026 tp->bufmgr_config.mbuf_read_dma_low_water);
10027 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10028 tp->bufmgr_config.mbuf_mac_rx_low_water);
10029 tw32(BUFMGR_MB_HIGH_WATER,
10030 tp->bufmgr_config.mbuf_high_water);
10031 } else {
10032 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10033 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
10034 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10035 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
10036 tw32(BUFMGR_MB_HIGH_WATER,
10037 tp->bufmgr_config.mbuf_high_water_jumbo);
10038 }
10039 tw32(BUFMGR_DMA_LOW_WATER,
10040 tp->bufmgr_config.dma_low_water);
10041 tw32(BUFMGR_DMA_HIGH_WATER,
10042 tp->bufmgr_config.dma_high_water);
10043
Matt Carlsond309a462010-09-30 10:34:31 +000010044 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +000010045 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +000010046 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +000010047 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080010048 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000010049 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10050 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +000010051 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +000010052 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010053 for (i = 0; i < 2000; i++) {
10054 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
10055 break;
10056 udelay(10);
10057 }
10058 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +000010059 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010060 return -ENODEV;
10061 }
10062
Joe Perches41535772013-02-16 11:20:04 +000010063 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +000010064 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -070010065
Matt Carlsoneb07a942011-04-20 07:57:36 +000010066 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067
10068 /* Initialize TG3_BDINFO's at:
10069 * RCVDBDI_STD_BD: standard eth size rx ring
10070 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
10071 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
10072 *
10073 * like so:
10074 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
10075 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
10076 * ring attribute flags
10077 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
10078 *
10079 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
10080 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
10081 *
10082 * The size of each ring is fixed in the firmware, but the location is
10083 * configurable.
10084 */
10085 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010086 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010087 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010088 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +000010089 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +000010090 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
10091 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010092
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010093 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +000010094 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010095 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
10096 BDINFO_FLAGS_DISABLED);
10097
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010098 /* Program the jumbo buffer descriptor ring control
10099 * blocks on those devices that have them.
10100 */
Joe Perches41535772013-02-16 11:20:04 +000010101 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010102 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010103
Joe Perches63c3a662011-04-26 08:12:10 +000010104 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010106 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010107 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010108 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +000010109 val = TG3_RX_JMB_RING_SIZE(tp) <<
10110 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +000010112 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +000010113 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +000010114 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000010115 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +000010116 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
10117 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118 } else {
10119 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
10120 BDINFO_FLAGS_DISABLED);
10121 }
10122
Joe Perches63c3a662011-04-26 08:12:10 +000010123 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000010124 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010125 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
10126 val |= (TG3_RX_STD_DMA_SZ << 2);
10127 } else
Matt Carlson04380d42010-04-12 06:58:29 +000010128 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010129 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +000010130 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010131
10132 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133
Matt Carlson411da642009-11-13 13:03:46 +000010134 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +000010135 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136
Joe Perches63c3a662011-04-26 08:12:10 +000010137 tpr->rx_jmb_prod_idx =
10138 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +000010139 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140
Matt Carlson2d31eca2009-09-01 12:53:31 +000010141 tg3_rings_reset(tp);
10142
Linus Torvalds1da177e2005-04-16 15:20:36 -070010143 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +000010144 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145
10146 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +000010147 tw32(MAC_RX_MTU_SIZE,
10148 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010149
10150 /* The slot time is changed by tg3_setup_phy if we
10151 * run at gigabit with half duplex.
10152 */
Matt Carlsonf2096f92011-04-05 14:22:48 +000010153 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
10154 (6 << TX_LENGTHS_IPG_SHIFT) |
10155 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
10156
Joe Perches41535772013-02-16 11:20:04 +000010157 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10158 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010159 val |= tr32(MAC_TX_LENGTHS) &
10160 (TX_LENGTHS_JMB_FRM_LEN_MSK |
10161 TX_LENGTHS_CNT_DWN_VAL_MSK);
10162
10163 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164
10165 /* Receive rules. */
10166 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
10167 tw32(RCVLPC_CONFIG, 0x0181);
10168
10169 /* Calculate RDMAC_MODE setting early, we need it to determine
10170 * the RCVLPC_STATE_ENABLE mask.
10171 */
10172 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
10173 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
10174 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
10175 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
10176 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -070010177
Joe Perches41535772013-02-16 11:20:04 +000010178 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +000010179 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
10180
Joe Perches41535772013-02-16 11:20:04 +000010181 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
10182 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10183 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070010184 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
10185 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
10186 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
10187
Joe Perches41535772013-02-16 11:20:04 +000010188 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10189 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010190 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010191 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
10193 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010194 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10196 }
10197 }
10198
Joe Perches63c3a662011-04-26 08:12:10 +000010199 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -070010200 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10201
Joe Perches41535772013-02-16 11:20:04 +000010202 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +000010203 tp->dma_limit = 0;
10204 if (tp->dev->mtu <= ETH_DATA_LEN) {
10205 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
10206 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
10207 }
10208 }
10209
Joe Perches63c3a662011-04-26 08:12:10 +000010210 if (tg3_flag(tp, HW_TSO_1) ||
10211 tg3_flag(tp, HW_TSO_2) ||
10212 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -080010213 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
10214
Matt Carlson108a6c12011-05-19 12:12:47 +000010215 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010216 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10217 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -080010218 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010219
Joe Perches41535772013-02-16 11:20:04 +000010220 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10221 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010222 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10223
Joe Perches41535772013-02-16 11:20:04 +000010224 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10225 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10226 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10227 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010228 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010229 u32 tgtreg;
10230
Joe Perches41535772013-02-16 11:20:04 +000010231 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010232 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
10233 else
10234 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10235
10236 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010237 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10238 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010239 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10240 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10241 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10242 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10243 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10244 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010245 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010246 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010247 }
10248
Joe Perches41535772013-02-16 11:20:04 +000010249 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10250 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10251 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010252 u32 tgtreg;
10253
Joe Perches41535772013-02-16 11:20:04 +000010254 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010255 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10256 else
10257 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10258
10259 val = tr32(tgtreg);
10260 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010261 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10262 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10263 }
10264
Linus Torvalds1da177e2005-04-16 15:20:36 -070010265 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010266 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010267 val = tr32(RCVLPC_STATS_ENABLE);
10268 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10269 tw32(RCVLPC_STATS_ENABLE, val);
10270 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010271 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010272 val = tr32(RCVLPC_STATS_ENABLE);
10273 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10274 tw32(RCVLPC_STATS_ENABLE, val);
10275 } else {
10276 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10277 }
10278 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10279 tw32(SNDDATAI_STATSENAB, 0xffffff);
10280 tw32(SNDDATAI_STATSCTRL,
10281 (SNDDATAI_SCTRL_ENABLE |
10282 SNDDATAI_SCTRL_FASTUPD));
10283
10284 /* Setup host coalescing engine. */
10285 tw32(HOSTCC_MODE, 0);
10286 for (i = 0; i < 2000; i++) {
10287 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10288 break;
10289 udelay(10);
10290 }
10291
Michael Chand244c892005-07-05 14:42:33 -070010292 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010293
Joe Perches63c3a662011-04-26 08:12:10 +000010294 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010295 /* Status/statistics block address. See tg3_timer,
10296 * the tg3_periodic_fetch_stats call there, and
10297 * tg3_get_stats to see how this works for 5705/5750 chips.
10298 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010299 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10300 ((u64) tp->stats_mapping >> 32));
10301 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10302 ((u64) tp->stats_mapping & 0xffffffff));
10303 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010304
Linus Torvalds1da177e2005-04-16 15:20:36 -070010305 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010306
10307 /* Clear statistics and status block memory areas */
10308 for (i = NIC_SRAM_STATS_BLK;
10309 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10310 i += sizeof(u32)) {
10311 tg3_write_mem(tp, i, 0);
10312 udelay(40);
10313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010314 }
10315
10316 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10317
10318 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10319 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010320 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10322
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010323 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10324 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010325 /* reset to prevent losing 1st rx packet intermittently */
10326 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10327 udelay(10);
10328 }
10329
Matt Carlson3bda1252008-08-15 14:08:22 -070010330 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010331 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10332 MAC_MODE_FHDE_ENABLE;
10333 if (tg3_flag(tp, ENABLE_APE))
10334 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010335 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010336 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010337 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010338 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010339 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10340 udelay(40);
10341
Michael Chan314fba32005-04-21 17:07:04 -070010342 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010343 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010344 * register to preserve the GPIO settings for LOMs. The GPIOs,
10345 * whether used as inputs or outputs, are set by boot code after
10346 * reset.
10347 */
Joe Perches63c3a662011-04-26 08:12:10 +000010348 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010349 u32 gpio_mask;
10350
Michael Chan9d26e212006-12-07 00:21:14 -080010351 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10352 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10353 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010354
Joe Perches41535772013-02-16 11:20:04 +000010355 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010356 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10357 GRC_LCLCTRL_GPIO_OUTPUT3;
10358
Joe Perches41535772013-02-16 11:20:04 +000010359 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010360 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10361
Gary Zambranoaaf84462007-05-05 11:51:45 -070010362 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010363 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10364
10365 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010366 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010367 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10368 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010370 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10371 udelay(100);
10372
Matt Carlsonc3b50032012-01-17 15:27:23 +000010373 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010374 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010375 val |= MSGINT_MODE_ENABLE;
10376 if (tp->irq_cnt > 1)
10377 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010378 if (!tg3_flag(tp, 1SHOT_MSI))
10379 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010380 tw32(MSGINT_MODE, val);
10381 }
10382
Joe Perches63c3a662011-04-26 08:12:10 +000010383 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010384 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10385 udelay(40);
10386 }
10387
10388 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10389 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10390 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10391 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10392 WDMAC_MODE_LNGREAD_ENAB);
10393
Joe Perches41535772013-02-16 11:20:04 +000010394 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10395 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010396 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010397 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10398 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399 /* nothing */
10400 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010401 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010402 val |= WDMAC_MODE_RX_ACCEL;
10403 }
10404 }
10405
Michael Chand9ab5ad12006-03-20 22:27:35 -080010406 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010407 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010408 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010409
Joe Perches41535772013-02-16 11:20:04 +000010410 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010411 val |= WDMAC_MODE_BURST_ALL_DATA;
10412
Linus Torvalds1da177e2005-04-16 15:20:36 -070010413 tw32_f(WDMAC_MODE, val);
10414 udelay(40);
10415
Joe Perches63c3a662011-04-26 08:12:10 +000010416 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010417 u16 pcix_cmd;
10418
10419 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10420 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010421 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010422 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10423 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010424 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010425 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10426 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010427 }
Matt Carlson9974a352007-10-07 23:27:28 -070010428 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10429 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010430 }
10431
10432 tw32_f(RDMAC_MODE, rdmac_mode);
10433 udelay(40);
10434
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010435 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10436 tg3_asic_rev(tp) == ASIC_REV_5720) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010437 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10438 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10439 break;
10440 }
10441 if (i < TG3_NUM_RDMA_CHANNELS) {
10442 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010443 val |= tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010444 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010445 tg3_flag_set(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010446 }
10447 }
10448
Linus Torvalds1da177e2005-04-16 15:20:36 -070010449 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010450 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010451 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010452
Joe Perches41535772013-02-16 11:20:04 +000010453 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010454 tw32(SNDDATAC_MODE,
10455 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10456 else
10457 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10458
Linus Torvalds1da177e2005-04-16 15:20:36 -070010459 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10460 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010461 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010462 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010463 val |= RCVDBDI_MODE_LRG_RING_SZ;
10464 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010465 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010466 if (tg3_flag(tp, HW_TSO_1) ||
10467 tg3_flag(tp, HW_TSO_2) ||
10468 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010469 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010470 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010471 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010472 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10473 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010474 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10475
Joe Perches41535772013-02-16 11:20:04 +000010476 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010477 err = tg3_load_5701_a0_firmware_fix(tp);
10478 if (err)
10479 return err;
10480 }
10481
Nithin Sujirc4dab502013-03-06 17:02:34 +000010482 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10483 /* Ignore any errors for the firmware download. If download
10484 * fails, the device will operate with EEE disabled
10485 */
10486 tg3_load_57766_firmware(tp);
10487 }
10488
Joe Perches63c3a662011-04-26 08:12:10 +000010489 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010490 err = tg3_load_tso_firmware(tp);
10491 if (err)
10492 return err;
10493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010494
10495 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010496
Joe Perches63c3a662011-04-26 08:12:10 +000010497 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010498 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010499 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010500
Joe Perches41535772013-02-16 11:20:04 +000010501 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10502 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010503 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10504 tp->tx_mode &= ~val;
10505 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10506 }
10507
Linus Torvalds1da177e2005-04-16 15:20:36 -070010508 tw32_f(MAC_TX_MODE, tp->tx_mode);
10509 udelay(100);
10510
Joe Perches63c3a662011-04-26 08:12:10 +000010511 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010512 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010513
10514 /* Setup the "secret" hash key. */
10515 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
10516 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
10517 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
10518 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
10519 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
10520 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
10521 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
10522 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
10523 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
10524 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
10525 }
10526
Linus Torvalds1da177e2005-04-16 15:20:36 -070010527 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010528 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010529 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10530
Nithin Sujir378b72c2013-07-29 13:58:39 -070010531 if (tg3_asic_rev(tp) == ASIC_REV_5762)
10532 tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
10533
Joe Perches63c3a662011-04-26 08:12:10 +000010534 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010535 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10536 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10537 RX_MODE_RSS_IPV6_HASH_EN |
10538 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10539 RX_MODE_RSS_IPV4_HASH_EN |
10540 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10541
Linus Torvalds1da177e2005-04-16 15:20:36 -070010542 tw32_f(MAC_RX_MODE, tp->rx_mode);
10543 udelay(10);
10544
Linus Torvalds1da177e2005-04-16 15:20:36 -070010545 tw32(MAC_LED_CTRL, tp->led_ctrl);
10546
10547 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010548 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010549 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10550 udelay(10);
10551 }
10552 tw32_f(MAC_RX_MODE, tp->rx_mode);
10553 udelay(10);
10554
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010555 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010556 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10557 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010558 /* Set drive transmission level to 1.2V */
10559 /* only if the signal pre-emphasis bit is not set */
10560 val = tr32(MAC_SERDES_CFG);
10561 val &= 0xfffff000;
10562 val |= 0x880;
10563 tw32(MAC_SERDES_CFG, val);
10564 }
Joe Perches41535772013-02-16 11:20:04 +000010565 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010566 tw32(MAC_SERDES_CFG, 0x616000);
10567 }
10568
10569 /* Prevent chip from dropping frames when flow control
10570 * is enabled.
10571 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010572 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010573 val = 1;
10574 else
10575 val = 2;
10576 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010577
Joe Perches41535772013-02-16 11:20:04 +000010578 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010579 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010580 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010581 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010582 }
10583
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010584 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010585 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010586 u32 tmp;
10587
10588 tmp = tr32(SERDES_RX_CTRL);
10589 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10590 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10591 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10592 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10593 }
10594
Joe Perches63c3a662011-04-26 08:12:10 +000010595 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010596 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010597 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010598
Joe Perches953c96e2013-04-09 10:18:14 +000010599 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010600 if (err)
10601 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010602
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010603 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10604 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010605 u32 tmp;
10606
10607 /* Clear CRC stats. */
10608 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10609 tg3_writephy(tp, MII_TG3_TEST1,
10610 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010611 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010613 }
10614 }
10615
10616 __tg3_set_rx_mode(tp->dev);
10617
10618 /* Initialize receive rules. */
10619 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10620 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10621 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10622 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10623
Joe Perches63c3a662011-04-26 08:12:10 +000010624 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010625 limit = 8;
10626 else
10627 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010628 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010629 limit -= 4;
10630 switch (limit) {
10631 case 16:
10632 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10633 case 15:
10634 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10635 case 14:
10636 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10637 case 13:
10638 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10639 case 12:
10640 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10641 case 11:
10642 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10643 case 10:
10644 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10645 case 9:
10646 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10647 case 8:
10648 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10649 case 7:
10650 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10651 case 6:
10652 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10653 case 5:
10654 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10655 case 4:
10656 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10657 case 3:
10658 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10659 case 2:
10660 case 1:
10661
10662 default:
10663 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010665
Joe Perches63c3a662011-04-26 08:12:10 +000010666 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010667 /* Write our heartbeat update interval to APE. */
10668 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10669 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010670
Linus Torvalds1da177e2005-04-16 15:20:36 -070010671 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10672
Linus Torvalds1da177e2005-04-16 15:20:36 -070010673 return 0;
10674}
10675
10676/* Called at device open time to get the chip ready for
10677 * packet processing. Invoked with tp->lock held.
10678 */
Joe Perches953c96e2013-04-09 10:18:14 +000010679static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010680{
Nithin Sujirdf465ab2013-06-12 11:08:59 -070010681 /* Chip may have been just powered on. If so, the boot code may still
10682 * be running initialization. Wait for it to finish to avoid races in
10683 * accessing the hardware.
10684 */
10685 tg3_enable_register_access(tp);
10686 tg3_poll_fw(tp);
10687
Linus Torvalds1da177e2005-04-16 15:20:36 -070010688 tg3_switch_clocks(tp);
10689
10690 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10691
Matt Carlson2f751b62008-08-04 23:17:34 -070010692 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010693}
10694
Michael Chanaed93e02012-07-16 16:24:02 +000010695static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10696{
10697 int i;
10698
10699 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10700 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10701
10702 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10703 off += len;
10704
10705 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10706 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10707 memset(ocir, 0, TG3_OCIR_LEN);
10708 }
10709}
10710
10711/* sysfs attributes for hwmon */
10712static ssize_t tg3_show_temp(struct device *dev,
10713 struct device_attribute *devattr, char *buf)
10714{
Michael Chanaed93e02012-07-16 16:24:02 +000010715 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010716 struct tg3 *tp = dev_get_drvdata(dev);
Michael Chanaed93e02012-07-16 16:24:02 +000010717 u32 temperature;
10718
10719 spin_lock_bh(&tp->lock);
10720 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10721 sizeof(temperature));
10722 spin_unlock_bh(&tp->lock);
10723 return sprintf(buf, "%u\n", temperature);
10724}
10725
10726
10727static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10728 TG3_TEMP_SENSOR_OFFSET);
10729static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10730 TG3_TEMP_CAUTION_OFFSET);
10731static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10732 TG3_TEMP_MAX_OFFSET);
10733
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010734static struct attribute *tg3_attrs[] = {
Michael Chanaed93e02012-07-16 16:24:02 +000010735 &sensor_dev_attr_temp1_input.dev_attr.attr,
10736 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10737 &sensor_dev_attr_temp1_max.dev_attr.attr,
10738 NULL
10739};
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010740ATTRIBUTE_GROUPS(tg3);
Michael Chanaed93e02012-07-16 16:24:02 +000010741
Michael Chanaed93e02012-07-16 16:24:02 +000010742static void tg3_hwmon_close(struct tg3 *tp)
10743{
Michael Chanaed93e02012-07-16 16:24:02 +000010744 if (tp->hwmon_dev) {
10745 hwmon_device_unregister(tp->hwmon_dev);
10746 tp->hwmon_dev = NULL;
Michael Chanaed93e02012-07-16 16:24:02 +000010747 }
Michael Chanaed93e02012-07-16 16:24:02 +000010748}
10749
10750static void tg3_hwmon_open(struct tg3 *tp)
10751{
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010752 int i;
Michael Chanaed93e02012-07-16 16:24:02 +000010753 u32 size = 0;
10754 struct pci_dev *pdev = tp->pdev;
10755 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10756
10757 tg3_sd_scan_scratchpad(tp, ocirs);
10758
10759 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10760 if (!ocirs[i].src_data_length)
10761 continue;
10762
10763 size += ocirs[i].src_hdr_length;
10764 size += ocirs[i].src_data_length;
10765 }
10766
10767 if (!size)
10768 return;
10769
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010770 tp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, "tg3",
10771 tp, tg3_groups);
Michael Chanaed93e02012-07-16 16:24:02 +000010772 if (IS_ERR(tp->hwmon_dev)) {
10773 tp->hwmon_dev = NULL;
10774 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
Michael Chanaed93e02012-07-16 16:24:02 +000010775 }
Michael Chanaed93e02012-07-16 16:24:02 +000010776}
10777
10778
Linus Torvalds1da177e2005-04-16 15:20:36 -070010779#define TG3_STAT_ADD32(PSTAT, REG) \
10780do { u32 __val = tr32(REG); \
10781 (PSTAT)->low += __val; \
10782 if ((PSTAT)->low < __val) \
10783 (PSTAT)->high += 1; \
10784} while (0)
10785
10786static void tg3_periodic_fetch_stats(struct tg3 *tp)
10787{
10788 struct tg3_hw_stats *sp = tp->hw_stats;
10789
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010790 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010791 return;
10792
10793 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10794 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10795 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10796 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10797 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10798 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10799 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10800 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10801 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10802 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10803 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10804 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10805 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010806 if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
Michael Chan091f0ea2012-07-29 19:15:43 +000010807 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10808 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10809 u32 val;
10810
10811 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010812 val &= ~tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010813 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010814 tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010816
10817 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10818 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10819 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10820 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10821 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10822 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10823 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10824 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10825 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10826 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10827 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10828 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10829 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10830 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010831
10832 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010833 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
Nithin Sujir94962f72013-12-06 09:53:19 -080010834 tg3_asic_rev(tp) != ASIC_REV_5762 &&
Joe Perches41535772013-02-16 11:20:04 +000010835 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10836 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010837 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10838 } else {
10839 u32 val = tr32(HOSTCC_FLOW_ATTN);
10840 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10841 if (val) {
10842 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10843 sp->rx_discards.low += val;
10844 if (sp->rx_discards.low < val)
10845 sp->rx_discards.high += 1;
10846 }
10847 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10848 }
Michael Chan463d3052006-05-22 16:36:27 -070010849 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010850}
10851
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010852static void tg3_chk_missed_msi(struct tg3 *tp)
10853{
10854 u32 i;
10855
10856 for (i = 0; i < tp->irq_cnt; i++) {
10857 struct tg3_napi *tnapi = &tp->napi[i];
10858
10859 if (tg3_has_work(tnapi)) {
10860 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10861 tnapi->last_tx_cons == tnapi->tx_cons) {
10862 if (tnapi->chk_msi_cnt < 1) {
10863 tnapi->chk_msi_cnt++;
10864 return;
10865 }
Matt Carlson7f230732011-08-31 11:44:48 +000010866 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010867 }
10868 }
10869 tnapi->chk_msi_cnt = 0;
10870 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10871 tnapi->last_tx_cons = tnapi->tx_cons;
10872 }
10873}
10874
Linus Torvalds1da177e2005-04-16 15:20:36 -070010875static void tg3_timer(unsigned long __opaque)
10876{
10877 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010878
Matt Carlson5b190622011-11-04 09:15:04 +000010879 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010880 goto restart_timer;
10881
David S. Millerf47c11e2005-06-24 20:18:35 -070010882 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010883
Joe Perches41535772013-02-16 11:20:04 +000010884 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010885 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010886 tg3_chk_missed_msi(tp);
10887
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010888 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10889 /* BCM4785: Flush posted writes from GbE to host memory. */
10890 tr32(HOSTCC_MODE);
10891 }
10892
Joe Perches63c3a662011-04-26 08:12:10 +000010893 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010894 /* All of this garbage is because when using non-tagged
10895 * IRQ status the mailbox/status_block protocol the chip
10896 * uses with the cpu is race prone.
10897 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010898 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010899 tw32(GRC_LOCAL_CTRL,
10900 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10901 } else {
10902 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010903 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010905
David S. Millerfac9b832005-05-18 22:46:34 -070010906 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010907 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010908 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010909 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010911 }
10912
Linus Torvalds1da177e2005-04-16 15:20:36 -070010913 /* This part only runs once per second. */
10914 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010915 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010916 tg3_periodic_fetch_stats(tp);
10917
Matt Carlsonb0c59432011-05-19 12:12:48 +000010918 if (tp->setlpicnt && !--tp->setlpicnt)
10919 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010920
Joe Perches63c3a662011-04-26 08:12:10 +000010921 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010922 u32 mac_stat;
10923 int phy_event;
10924
10925 mac_stat = tr32(MAC_STATUS);
10926
10927 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010928 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010929 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10930 phy_event = 1;
10931 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10932 phy_event = 1;
10933
10934 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010935 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010936 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010937 u32 mac_stat = tr32(MAC_STATUS);
10938 int need_setup = 0;
10939
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010940 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010941 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10942 need_setup = 1;
10943 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010944 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010945 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10946 MAC_STATUS_SIGNAL_DET))) {
10947 need_setup = 1;
10948 }
10949 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010950 if (!tp->serdes_counter) {
10951 tw32_f(MAC_MODE,
10952 (tp->mac_mode &
10953 ~MAC_MODE_PORT_MODE_MASK));
10954 udelay(40);
10955 tw32_f(MAC_MODE, tp->mac_mode);
10956 udelay(40);
10957 }
Joe Perches953c96e2013-04-09 10:18:14 +000010958 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010959 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010960 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010961 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010962 tg3_serdes_parallel_detect(tp);
Nithin Sujir1743b832014-01-03 10:09:14 -080010963 } else if (tg3_flag(tp, POLL_CPMU_LINK)) {
10964 u32 cpmu = tr32(TG3_CPMU_STATUS);
10965 bool link_up = !((cpmu & TG3_CPMU_STATUS_LINK_MASK) ==
10966 TG3_CPMU_STATUS_LINK_MASK);
10967
10968 if (link_up != tp->link_up)
10969 tg3_setup_phy(tp, false);
Matt Carlson57d8b882010-06-05 17:24:35 +000010970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010971
10972 tp->timer_counter = tp->timer_multiplier;
10973 }
10974
Michael Chan130b8e42006-09-27 16:00:40 -070010975 /* Heartbeat is only sent once every 2 seconds.
10976 *
10977 * The heartbeat is to tell the ASF firmware that the host
10978 * driver is still alive. In the event that the OS crashes,
10979 * ASF needs to reset the hardware to free up the FIFO space
10980 * that may be filled with rx packets destined for the host.
10981 * If the FIFO is full, ASF will no longer function properly.
10982 *
10983 * Unintended resets have been reported on real time kernels
10984 * where the timer doesn't run on time. Netpoll will also have
10985 * same problem.
10986 *
10987 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10988 * to check the ring condition when the heartbeat is expiring
10989 * before doing the reset. This will prevent most unintended
10990 * resets.
10991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010992 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010993 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010994 tg3_wait_for_event_ack(tp);
10995
Michael Chanbbadf502006-04-06 21:46:34 -070010996 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010997 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010998 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010999 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
11000 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070011001
11002 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011003 }
11004 tp->asf_counter = tp->asf_multiplier;
11005 }
11006
David S. Millerf47c11e2005-06-24 20:18:35 -070011007 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011008
Michael Chanf475f162006-03-27 23:20:14 -080011009restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070011010 tp->timer.expires = jiffies + tp->timer_offset;
11011 add_timer(&tp->timer);
11012}
11013
Bill Pemberton229b1ad2012-12-03 09:22:59 -050011014static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000011015{
11016 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000011017 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000011018 !tg3_flag(tp, 57765_CLASS))
11019 tp->timer_offset = HZ;
11020 else
11021 tp->timer_offset = HZ / 10;
11022
11023 BUG_ON(tp->timer_offset > HZ);
11024
11025 tp->timer_multiplier = (HZ / tp->timer_offset);
11026 tp->asf_multiplier = (HZ / tp->timer_offset) *
11027 TG3_FW_UPDATE_FREQ_SEC;
11028
11029 init_timer(&tp->timer);
11030 tp->timer.data = (unsigned long) tp;
11031 tp->timer.function = tg3_timer;
11032}
11033
11034static void tg3_timer_start(struct tg3 *tp)
11035{
11036 tp->asf_counter = tp->asf_multiplier;
11037 tp->timer_counter = tp->timer_multiplier;
11038
11039 tp->timer.expires = jiffies + tp->timer_offset;
11040 add_timer(&tp->timer);
11041}
11042
11043static void tg3_timer_stop(struct tg3 *tp)
11044{
11045 del_timer_sync(&tp->timer);
11046}
11047
11048/* Restart hardware after configuration changes, self-test, etc.
11049 * Invoked with tp->lock held.
11050 */
Joe Perches953c96e2013-04-09 10:18:14 +000011051static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000011052 __releases(tp->lock)
11053 __acquires(tp->lock)
11054{
11055 int err;
11056
11057 err = tg3_init_hw(tp, reset_phy);
11058 if (err) {
11059 netdev_err(tp->dev,
11060 "Failed to re-initialize device, aborting\n");
11061 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11062 tg3_full_unlock(tp);
11063 tg3_timer_stop(tp);
11064 tp->irq_sync = 0;
11065 tg3_napi_enable(tp);
11066 dev_close(tp->dev);
11067 tg3_full_lock(tp, 0);
11068 }
11069 return err;
11070}
11071
11072static void tg3_reset_task(struct work_struct *work)
11073{
11074 struct tg3 *tp = container_of(work, struct tg3, reset_task);
11075 int err;
11076
11077 tg3_full_lock(tp, 0);
11078
11079 if (!netif_running(tp->dev)) {
11080 tg3_flag_clear(tp, RESET_TASK_PENDING);
11081 tg3_full_unlock(tp);
11082 return;
11083 }
11084
11085 tg3_full_unlock(tp);
11086
11087 tg3_phy_stop(tp);
11088
11089 tg3_netif_stop(tp);
11090
11091 tg3_full_lock(tp, 1);
11092
11093 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
11094 tp->write32_tx_mbox = tg3_write32_tx_mbox;
11095 tp->write32_rx_mbox = tg3_write_flush_reg32;
11096 tg3_flag_set(tp, MBOX_WRITE_REORDER);
11097 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
11098 }
11099
11100 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000011101 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000011102 if (err)
11103 goto out;
11104
11105 tg3_netif_start(tp);
11106
11107out:
11108 tg3_full_unlock(tp);
11109
11110 if (!err)
11111 tg3_phy_start(tp);
11112
11113 tg3_flag_clear(tp, RESET_TASK_PENDING);
11114}
11115
Matt Carlson4f125f42009-09-01 12:55:02 +000011116static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080011117{
David Howells7d12e782006-10-05 14:55:46 +010011118 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011119 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000011120 char *name;
11121 struct tg3_napi *tnapi = &tp->napi[irq_num];
11122
11123 if (tp->irq_cnt == 1)
11124 name = tp->dev->name;
11125 else {
11126 name = &tnapi->irq_lbl[0];
Nithin Sujir21e315e2013-09-20 16:47:00 -070011127 if (tnapi->tx_buffers && tnapi->rx_rcb)
11128 snprintf(name, IFNAMSIZ,
11129 "%s-txrx-%d", tp->dev->name, irq_num);
11130 else if (tnapi->tx_buffers)
11131 snprintf(name, IFNAMSIZ,
11132 "%s-tx-%d", tp->dev->name, irq_num);
11133 else if (tnapi->rx_rcb)
11134 snprintf(name, IFNAMSIZ,
11135 "%s-rx-%d", tp->dev->name, irq_num);
11136 else
11137 snprintf(name, IFNAMSIZ,
11138 "%s-%d", tp->dev->name, irq_num);
Matt Carlson4f125f42009-09-01 12:55:02 +000011139 name[IFNAMSIZ-1] = 0;
11140 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011141
Joe Perches63c3a662011-04-26 08:12:10 +000011142 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080011143 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000011144 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011145 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011146 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011147 } else {
11148 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000011149 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011150 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011151 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011152 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011153
11154 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011155}
11156
Michael Chan79381092005-04-21 17:13:59 -070011157static int tg3_test_interrupt(struct tg3 *tp)
11158{
Matt Carlson09943a12009-08-28 14:01:57 +000011159 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070011160 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070011161 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011162 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070011163
Michael Chand4bc3922005-05-29 14:59:20 -070011164 if (!netif_running(dev))
11165 return -ENODEV;
11166
Michael Chan79381092005-04-21 17:13:59 -070011167 tg3_disable_ints(tp);
11168
Matt Carlson4f125f42009-09-01 12:55:02 +000011169 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011170
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011171 /*
11172 * Turn off MSI one shot mode. Otherwise this test has no
11173 * observable way to know whether the interrupt was delivered.
11174 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011175 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011176 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
11177 tw32(MSGINT_MODE, val);
11178 }
11179
Matt Carlson4f125f42009-09-01 12:55:02 +000011180 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000011181 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011182 if (err)
11183 return err;
11184
Matt Carlson898a56f2009-08-28 14:02:40 +000011185 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070011186 tg3_enable_ints(tp);
11187
11188 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011189 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070011190
11191 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070011192 u32 int_mbox, misc_host_ctrl;
11193
Matt Carlson898a56f2009-08-28 14:02:40 +000011194 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070011195 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
11196
11197 if ((int_mbox != 0) ||
11198 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
11199 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070011200 break;
Michael Chanb16250e2006-09-27 16:10:14 -070011201 }
11202
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011203 if (tg3_flag(tp, 57765_PLUS) &&
11204 tnapi->hw_status->status_tag != tnapi->last_tag)
11205 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
11206
Michael Chan79381092005-04-21 17:13:59 -070011207 msleep(10);
11208 }
11209
11210 tg3_disable_ints(tp);
11211
Matt Carlson4f125f42009-09-01 12:55:02 +000011212 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011213
Matt Carlson4f125f42009-09-01 12:55:02 +000011214 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011215
11216 if (err)
11217 return err;
11218
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011219 if (intr_ok) {
11220 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000011221 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011222 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
11223 tw32(MSGINT_MODE, val);
11224 }
Michael Chan79381092005-04-21 17:13:59 -070011225 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011226 }
Michael Chan79381092005-04-21 17:13:59 -070011227
11228 return -EIO;
11229}
11230
11231/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
11232 * successfully restored
11233 */
11234static int tg3_test_msi(struct tg3 *tp)
11235{
Michael Chan79381092005-04-21 17:13:59 -070011236 int err;
11237 u16 pci_cmd;
11238
Joe Perches63c3a662011-04-26 08:12:10 +000011239 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011240 return 0;
11241
11242 /* Turn off SERR reporting in case MSI terminates with Master
11243 * Abort.
11244 */
11245 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11246 pci_write_config_word(tp->pdev, PCI_COMMAND,
11247 pci_cmd & ~PCI_COMMAND_SERR);
11248
11249 err = tg3_test_interrupt(tp);
11250
11251 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11252
11253 if (!err)
11254 return 0;
11255
11256 /* other failures */
11257 if (err != -EIO)
11258 return err;
11259
11260 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011261 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11262 "to INTx mode. Please report this failure to the PCI "
11263 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011264
Matt Carlson4f125f42009-09-01 12:55:02 +000011265 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011266
Michael Chan79381092005-04-21 17:13:59 -070011267 pci_disable_msi(tp->pdev);
11268
Joe Perches63c3a662011-04-26 08:12:10 +000011269 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011270 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011271
Matt Carlson4f125f42009-09-01 12:55:02 +000011272 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011273 if (err)
11274 return err;
11275
11276 /* Need to reset the chip because the MSI cycle may have terminated
11277 * with Master Abort.
11278 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011279 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011280
Michael Chan944d9802005-05-29 14:57:48 -070011281 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011282 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011283
David S. Millerf47c11e2005-06-24 20:18:35 -070011284 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011285
11286 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011287 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011288
11289 return err;
11290}
11291
Matt Carlson9e9fd122009-01-19 16:57:45 -080011292static int tg3_request_firmware(struct tg3 *tp)
11293{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011294 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011295
11296 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011297 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11298 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011299 return -ENOENT;
11300 }
11301
Nithin Sujir77997ea2013-03-06 17:02:32 +000011302 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011303
11304 /* Firmware blob starts with version numbers, followed by
11305 * start address and _full_ length including BSS sections
11306 * (which must be longer than the actual data, of course
11307 */
11308
Nithin Sujir77997ea2013-03-06 17:02:32 +000011309 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11310 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011311 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11312 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011313 release_firmware(tp->fw);
11314 tp->fw = NULL;
11315 return -EINVAL;
11316 }
11317
11318 /* We no longer need firmware; we have it. */
11319 tp->fw_needed = NULL;
11320 return 0;
11321}
11322
Michael Chan91024262012-09-28 07:12:38 +000011323static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011324{
Michael Chan91024262012-09-28 07:12:38 +000011325 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011326
Michael Chan91024262012-09-28 07:12:38 +000011327 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011328 /* We want as many rx rings enabled as there are cpus.
11329 * In multiqueue MSI-X mode, the first MSI-X vector
11330 * only deals with link interrupts, etc, so we add
11331 * one to the number of vectors we are requesting.
11332 */
Michael Chan91024262012-09-28 07:12:38 +000011333 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011334 }
Matt Carlson679563f2009-09-01 12:55:46 +000011335
Michael Chan91024262012-09-28 07:12:38 +000011336 return irq_cnt;
11337}
11338
11339static bool tg3_enable_msix(struct tg3 *tp)
11340{
11341 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011342 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011343
Michael Chan09681692012-09-28 07:12:42 +000011344 tp->txq_cnt = tp->txq_req;
11345 tp->rxq_cnt = tp->rxq_req;
11346 if (!tp->rxq_cnt)
11347 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011348 if (tp->rxq_cnt > tp->rxq_max)
11349 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011350
11351 /* Disable multiple TX rings by default. Simple round-robin hardware
11352 * scheduling of the TX rings can cause starvation of rings with
11353 * small packets when other rings have TSO or jumbo packets.
11354 */
11355 if (!tp->txq_req)
11356 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011357
11358 tp->irq_cnt = tg3_irq_count(tp);
11359
Matt Carlson679563f2009-09-01 12:55:46 +000011360 for (i = 0; i < tp->irq_max; i++) {
11361 msix_ent[i].entry = i;
11362 msix_ent[i].vector = 0;
11363 }
11364
11365 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011366 if (rc < 0) {
11367 return false;
11368 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000011369 if (pci_enable_msix(tp->pdev, msix_ent, rc))
11370 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000011371 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11372 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011373 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011374 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011375 if (tp->txq_cnt)
11376 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011377 }
11378
11379 for (i = 0; i < tp->irq_max; i++)
11380 tp->napi[i].irq_vec = msix_ent[i].vector;
11381
Michael Chan49a359e2012-09-28 07:12:37 +000011382 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011383 pci_disable_msix(tp->pdev);
11384 return false;
11385 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011386
Michael Chan91024262012-09-28 07:12:38 +000011387 if (tp->irq_cnt == 1)
11388 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011389
Michael Chan91024262012-09-28 07:12:38 +000011390 tg3_flag_set(tp, ENABLE_RSS);
11391
11392 if (tp->txq_cnt > 1)
11393 tg3_flag_set(tp, ENABLE_TSS);
11394
11395 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011396
Matt Carlson679563f2009-09-01 12:55:46 +000011397 return true;
11398}
11399
Matt Carlson07b01732009-08-28 14:01:15 +000011400static void tg3_ints_init(struct tg3 *tp)
11401{
Joe Perches63c3a662011-04-26 08:12:10 +000011402 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11403 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011404 /* All MSI supporting chips should support tagged
11405 * status. Assert that this is the case.
11406 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011407 netdev_warn(tp->dev,
11408 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011409 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011410 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011411
Joe Perches63c3a662011-04-26 08:12:10 +000011412 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11413 tg3_flag_set(tp, USING_MSIX);
11414 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11415 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011416
Joe Perches63c3a662011-04-26 08:12:10 +000011417 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011418 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011419 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011420 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011421 if (!tg3_flag(tp, 1SHOT_MSI))
11422 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011423 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11424 }
11425defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011426 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011427 tp->irq_cnt = 1;
11428 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011429 }
11430
11431 if (tp->irq_cnt == 1) {
11432 tp->txq_cnt = 1;
11433 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011434 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011435 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011436 }
Matt Carlson07b01732009-08-28 14:01:15 +000011437}
11438
11439static void tg3_ints_fini(struct tg3 *tp)
11440{
Joe Perches63c3a662011-04-26 08:12:10 +000011441 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011442 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011443 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011444 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011445 tg3_flag_clear(tp, USING_MSI);
11446 tg3_flag_clear(tp, USING_MSIX);
11447 tg3_flag_clear(tp, ENABLE_RSS);
11448 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011449}
11450
Matt Carlsonbe947302012-12-03 19:36:57 +000011451static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11452 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011453{
Michael Chand8f4cd32012-09-28 07:12:40 +000011454 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011455 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011456
Matt Carlson679563f2009-09-01 12:55:46 +000011457 /*
11458 * Setup interrupts first so we know how
11459 * many NAPI resources to allocate
11460 */
11461 tg3_ints_init(tp);
11462
Matt Carlson90415472011-12-16 13:33:23 +000011463 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011464
Linus Torvalds1da177e2005-04-16 15:20:36 -070011465 /* The placement of this call is tied
11466 * to the setup and use of Host TX descriptors.
11467 */
11468 err = tg3_alloc_consistent(tp);
11469 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011470 goto out_ints_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011471
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011472 tg3_napi_init(tp);
11473
Matt Carlsonfed97812009-09-01 13:10:19 +000011474 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011475
Matt Carlson4f125f42009-09-01 12:55:02 +000011476 for (i = 0; i < tp->irq_cnt; i++) {
11477 struct tg3_napi *tnapi = &tp->napi[i];
11478 err = tg3_request_irq(tp, i);
11479 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011480 for (i--; i >= 0; i--) {
11481 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011482 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011483 }
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011484 goto out_napi_fini;
Matt Carlson4f125f42009-09-01 12:55:02 +000011485 }
11486 }
Matt Carlson07b01732009-08-28 14:01:15 +000011487
David S. Millerf47c11e2005-06-24 20:18:35 -070011488 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011489
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011490 if (init)
11491 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11492
Michael Chand8f4cd32012-09-28 07:12:40 +000011493 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011494 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011495 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011496 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011497 }
11498
David S. Millerf47c11e2005-06-24 20:18:35 -070011499 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011500
Matt Carlson07b01732009-08-28 14:01:15 +000011501 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011502 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011503
Michael Chand8f4cd32012-09-28 07:12:40 +000011504 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011505 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011506
Michael Chan79381092005-04-21 17:13:59 -070011507 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011508 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011509 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011510 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011511 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011512
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011513 goto out_napi_fini;
Michael Chan79381092005-04-21 17:13:59 -070011514 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011515
Joe Perches63c3a662011-04-26 08:12:10 +000011516 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011517 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011518
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011519 tw32(PCIE_TRANSACTION_CFG,
11520 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011521 }
Michael Chan79381092005-04-21 17:13:59 -070011522 }
11523
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011524 tg3_phy_start(tp);
11525
Michael Chanaed93e02012-07-16 16:24:02 +000011526 tg3_hwmon_open(tp);
11527
David S. Millerf47c11e2005-06-24 20:18:35 -070011528 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011529
Matt Carlson21f76382012-02-22 12:35:21 +000011530 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011531 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011532 tg3_enable_ints(tp);
11533
Matt Carlsonbe947302012-12-03 19:36:57 +000011534 if (init)
11535 tg3_ptp_init(tp);
11536 else
11537 tg3_ptp_resume(tp);
11538
11539
David S. Millerf47c11e2005-06-24 20:18:35 -070011540 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011541
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011542 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011543
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011544 /*
11545 * Reset loopback feature if it was turned on while the device was down
11546 * make sure that it's installed properly now.
11547 */
11548 if (dev->features & NETIF_F_LOOPBACK)
11549 tg3_set_loopback(dev, dev->features);
11550
Linus Torvalds1da177e2005-04-16 15:20:36 -070011551 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011552
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011553out_free_irq:
Matt Carlson4f125f42009-09-01 12:55:02 +000011554 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11555 struct tg3_napi *tnapi = &tp->napi[i];
11556 free_irq(tnapi->irq_vec, tnapi);
11557 }
Matt Carlson07b01732009-08-28 14:01:15 +000011558
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011559out_napi_fini:
Matt Carlsonfed97812009-09-01 13:10:19 +000011560 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011561 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011562 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011563
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011564out_ints_fini:
Matt Carlson679563f2009-09-01 12:55:46 +000011565 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011566
Matt Carlson07b01732009-08-28 14:01:15 +000011567 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011568}
11569
Michael Chan65138592012-09-28 07:12:41 +000011570static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011571{
Matt Carlson4f125f42009-09-01 12:55:02 +000011572 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011573
Matt Carlsondb219972011-11-04 09:15:03 +000011574 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011575 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011576
Matt Carlson21f76382012-02-22 12:35:21 +000011577 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011578
Michael Chanaed93e02012-07-16 16:24:02 +000011579 tg3_hwmon_close(tp);
11580
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011581 tg3_phy_stop(tp);
11582
David S. Millerf47c11e2005-06-24 20:18:35 -070011583 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011584
11585 tg3_disable_ints(tp);
11586
Michael Chan944d9802005-05-29 14:57:48 -070011587 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011588 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011589 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011590
David S. Millerf47c11e2005-06-24 20:18:35 -070011591 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011592
Matt Carlson4f125f42009-09-01 12:55:02 +000011593 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11594 struct tg3_napi *tnapi = &tp->napi[i];
11595 free_irq(tnapi->irq_vec, tnapi);
11596 }
Matt Carlson07b01732009-08-28 14:01:15 +000011597
11598 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011599
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011600 tg3_napi_fini(tp);
11601
Linus Torvalds1da177e2005-04-16 15:20:36 -070011602 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011603}
11604
Michael Chand8f4cd32012-09-28 07:12:40 +000011605static int tg3_open(struct net_device *dev)
11606{
11607 struct tg3 *tp = netdev_priv(dev);
11608 int err;
11609
11610 if (tp->fw_needed) {
11611 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011612 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11613 if (err) {
11614 netdev_warn(tp->dev, "EEE capability disabled\n");
11615 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11616 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11617 netdev_warn(tp->dev, "EEE capability restored\n");
11618 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11619 }
11620 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011621 if (err)
11622 return err;
11623 } else if (err) {
11624 netdev_warn(tp->dev, "TSO capability disabled\n");
11625 tg3_flag_clear(tp, TSO_CAPABLE);
11626 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11627 netdev_notice(tp->dev, "TSO capability restored\n");
11628 tg3_flag_set(tp, TSO_CAPABLE);
11629 }
11630 }
11631
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011632 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011633
11634 err = tg3_power_up(tp);
11635 if (err)
11636 return err;
11637
11638 tg3_full_lock(tp, 0);
11639
11640 tg3_disable_ints(tp);
11641 tg3_flag_clear(tp, INIT_COMPLETE);
11642
11643 tg3_full_unlock(tp);
11644
Nithin Sujir942d1af2013-04-09 08:48:07 +000011645 err = tg3_start(tp,
11646 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11647 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011648 if (err) {
11649 tg3_frob_aux_power(tp, false);
11650 pci_set_power_state(tp->pdev, PCI_D3hot);
11651 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011652
Matt Carlson7d41e492012-12-03 19:36:58 +000011653 if (tg3_flag(tp, PTP_CAPABLE)) {
11654 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
11655 &tp->pdev->dev);
11656 if (IS_ERR(tp->ptp_clock))
11657 tp->ptp_clock = NULL;
11658 }
11659
Linus Torvalds1da177e2005-04-16 15:20:36 -070011660 return err;
11661}
11662
11663static int tg3_close(struct net_device *dev)
11664{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011665 struct tg3 *tp = netdev_priv(dev);
11666
Matt Carlsonbe947302012-12-03 19:36:57 +000011667 tg3_ptp_fini(tp);
11668
Michael Chan65138592012-09-28 07:12:41 +000011669 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011670
11671 /* Clear stats across close / open calls */
11672 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11673 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011674
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011675 if (pci_device_is_present(tp->pdev)) {
11676 tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011677
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011678 tg3_carrier_off(tp);
11679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011680 return 0;
11681}
11682
11683static inline u64 get_stat64(tg3_stat64_t *val)
11684{
11685 return ((u64)val->high << 32) | ((u64)val->low);
11686}
11687
11688static u64 tg3_calc_crc_errors(struct tg3 *tp)
11689{
11690 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11691
11692 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011693 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11694 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011695 u32 val;
11696
11697 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11698 tg3_writephy(tp, MII_TG3_TEST1,
11699 val | MII_TG3_TEST1_CRC_EN);
11700 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11701 } else
11702 val = 0;
11703
11704 tp->phy_crc_errors += val;
11705
11706 return tp->phy_crc_errors;
11707 }
11708
11709 return get_stat64(&hw_stats->rx_fcs_errors);
11710}
11711
11712#define ESTAT_ADD(member) \
11713 estats->member = old_estats->member + \
11714 get_stat64(&hw_stats->member)
11715
11716static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11717{
11718 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11719 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11720
11721 ESTAT_ADD(rx_octets);
11722 ESTAT_ADD(rx_fragments);
11723 ESTAT_ADD(rx_ucast_packets);
11724 ESTAT_ADD(rx_mcast_packets);
11725 ESTAT_ADD(rx_bcast_packets);
11726 ESTAT_ADD(rx_fcs_errors);
11727 ESTAT_ADD(rx_align_errors);
11728 ESTAT_ADD(rx_xon_pause_rcvd);
11729 ESTAT_ADD(rx_xoff_pause_rcvd);
11730 ESTAT_ADD(rx_mac_ctrl_rcvd);
11731 ESTAT_ADD(rx_xoff_entered);
11732 ESTAT_ADD(rx_frame_too_long_errors);
11733 ESTAT_ADD(rx_jabbers);
11734 ESTAT_ADD(rx_undersize_packets);
11735 ESTAT_ADD(rx_in_length_errors);
11736 ESTAT_ADD(rx_out_length_errors);
11737 ESTAT_ADD(rx_64_or_less_octet_packets);
11738 ESTAT_ADD(rx_65_to_127_octet_packets);
11739 ESTAT_ADD(rx_128_to_255_octet_packets);
11740 ESTAT_ADD(rx_256_to_511_octet_packets);
11741 ESTAT_ADD(rx_512_to_1023_octet_packets);
11742 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11743 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11744 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11745 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11746 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11747
11748 ESTAT_ADD(tx_octets);
11749 ESTAT_ADD(tx_collisions);
11750 ESTAT_ADD(tx_xon_sent);
11751 ESTAT_ADD(tx_xoff_sent);
11752 ESTAT_ADD(tx_flow_control);
11753 ESTAT_ADD(tx_mac_errors);
11754 ESTAT_ADD(tx_single_collisions);
11755 ESTAT_ADD(tx_mult_collisions);
11756 ESTAT_ADD(tx_deferred);
11757 ESTAT_ADD(tx_excessive_collisions);
11758 ESTAT_ADD(tx_late_collisions);
11759 ESTAT_ADD(tx_collide_2times);
11760 ESTAT_ADD(tx_collide_3times);
11761 ESTAT_ADD(tx_collide_4times);
11762 ESTAT_ADD(tx_collide_5times);
11763 ESTAT_ADD(tx_collide_6times);
11764 ESTAT_ADD(tx_collide_7times);
11765 ESTAT_ADD(tx_collide_8times);
11766 ESTAT_ADD(tx_collide_9times);
11767 ESTAT_ADD(tx_collide_10times);
11768 ESTAT_ADD(tx_collide_11times);
11769 ESTAT_ADD(tx_collide_12times);
11770 ESTAT_ADD(tx_collide_13times);
11771 ESTAT_ADD(tx_collide_14times);
11772 ESTAT_ADD(tx_collide_15times);
11773 ESTAT_ADD(tx_ucast_packets);
11774 ESTAT_ADD(tx_mcast_packets);
11775 ESTAT_ADD(tx_bcast_packets);
11776 ESTAT_ADD(tx_carrier_sense_errors);
11777 ESTAT_ADD(tx_discards);
11778 ESTAT_ADD(tx_errors);
11779
11780 ESTAT_ADD(dma_writeq_full);
11781 ESTAT_ADD(dma_write_prioq_full);
11782 ESTAT_ADD(rxbds_empty);
11783 ESTAT_ADD(rx_discards);
11784 ESTAT_ADD(rx_errors);
11785 ESTAT_ADD(rx_threshold_hit);
11786
11787 ESTAT_ADD(dma_readq_full);
11788 ESTAT_ADD(dma_read_prioq_full);
11789 ESTAT_ADD(tx_comp_queue_full);
11790
11791 ESTAT_ADD(ring_set_send_prod_index);
11792 ESTAT_ADD(ring_status_update);
11793 ESTAT_ADD(nic_irqs);
11794 ESTAT_ADD(nic_avoided_irqs);
11795 ESTAT_ADD(nic_tx_threshold_hit);
11796
Matt Carlson4452d092011-05-19 12:12:51 +000011797 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011798}
11799
Matt Carlson65ec6982012-02-28 23:33:37 +000011800static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011801{
Eric Dumazet511d2222010-07-07 20:44:24 +000011802 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011803 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11804
Linus Torvalds1da177e2005-04-16 15:20:36 -070011805 stats->rx_packets = old_stats->rx_packets +
11806 get_stat64(&hw_stats->rx_ucast_packets) +
11807 get_stat64(&hw_stats->rx_mcast_packets) +
11808 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011809
Linus Torvalds1da177e2005-04-16 15:20:36 -070011810 stats->tx_packets = old_stats->tx_packets +
11811 get_stat64(&hw_stats->tx_ucast_packets) +
11812 get_stat64(&hw_stats->tx_mcast_packets) +
11813 get_stat64(&hw_stats->tx_bcast_packets);
11814
11815 stats->rx_bytes = old_stats->rx_bytes +
11816 get_stat64(&hw_stats->rx_octets);
11817 stats->tx_bytes = old_stats->tx_bytes +
11818 get_stat64(&hw_stats->tx_octets);
11819
11820 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011821 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011822 stats->tx_errors = old_stats->tx_errors +
11823 get_stat64(&hw_stats->tx_errors) +
11824 get_stat64(&hw_stats->tx_mac_errors) +
11825 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11826 get_stat64(&hw_stats->tx_discards);
11827
11828 stats->multicast = old_stats->multicast +
11829 get_stat64(&hw_stats->rx_mcast_packets);
11830 stats->collisions = old_stats->collisions +
11831 get_stat64(&hw_stats->tx_collisions);
11832
11833 stats->rx_length_errors = old_stats->rx_length_errors +
11834 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11835 get_stat64(&hw_stats->rx_undersize_packets);
11836
Linus Torvalds1da177e2005-04-16 15:20:36 -070011837 stats->rx_frame_errors = old_stats->rx_frame_errors +
11838 get_stat64(&hw_stats->rx_align_errors);
11839 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11840 get_stat64(&hw_stats->tx_discards);
11841 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11842 get_stat64(&hw_stats->tx_carrier_sense_errors);
11843
11844 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011845 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011846
John W. Linville4f63b872005-09-12 14:43:18 -070011847 stats->rx_missed_errors = old_stats->rx_missed_errors +
11848 get_stat64(&hw_stats->rx_discards);
11849
Eric Dumazetb0057c52010-10-10 19:55:52 +000011850 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011851 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011852}
11853
Linus Torvalds1da177e2005-04-16 15:20:36 -070011854static int tg3_get_regs_len(struct net_device *dev)
11855{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011856 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011857}
11858
11859static void tg3_get_regs(struct net_device *dev,
11860 struct ethtool_regs *regs, void *_p)
11861{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011862 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011863
11864 regs->version = 0;
11865
Matt Carlson97bd8e42011-04-13 11:05:04 +000011866 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011867
Matt Carlson80096062010-08-02 11:26:06 +000011868 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011869 return;
11870
David S. Millerf47c11e2005-06-24 20:18:35 -070011871 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011872
Matt Carlson97bd8e42011-04-13 11:05:04 +000011873 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011874
David S. Millerf47c11e2005-06-24 20:18:35 -070011875 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011876}
11877
11878static int tg3_get_eeprom_len(struct net_device *dev)
11879{
11880 struct tg3 *tp = netdev_priv(dev);
11881
11882 return tp->nvram_size;
11883}
11884
Linus Torvalds1da177e2005-04-16 15:20:36 -070011885static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11886{
11887 struct tg3 *tp = netdev_priv(dev);
11888 int ret;
11889 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011890 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011891 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011892
Joe Perches63c3a662011-04-26 08:12:10 +000011893 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011894 return -EINVAL;
11895
Linus Torvalds1da177e2005-04-16 15:20:36 -070011896 offset = eeprom->offset;
11897 len = eeprom->len;
11898 eeprom->len = 0;
11899
11900 eeprom->magic = TG3_EEPROM_MAGIC;
11901
11902 if (offset & 3) {
11903 /* adjustments to start on required 4 byte boundary */
11904 b_offset = offset & 3;
11905 b_count = 4 - b_offset;
11906 if (b_count > len) {
11907 /* i.e. offset=1 len=2 */
11908 b_count = len;
11909 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011910 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011911 if (ret)
11912 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011913 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011914 len -= b_count;
11915 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011916 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011917 }
11918
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011919 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011920 pd = &data[eeprom->len];
11921 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011922 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011923 if (ret) {
11924 eeprom->len += i;
11925 return ret;
11926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011927 memcpy(pd + i, &val, 4);
11928 }
11929 eeprom->len += i;
11930
11931 if (len & 3) {
11932 /* read last bytes not ending on 4 byte boundary */
11933 pd = &data[eeprom->len];
11934 b_count = len & 3;
11935 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011936 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011937 if (ret)
11938 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011939 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011940 eeprom->len += b_count;
11941 }
11942 return 0;
11943}
11944
Linus Torvalds1da177e2005-04-16 15:20:36 -070011945static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11946{
11947 struct tg3 *tp = netdev_priv(dev);
11948 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011949 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011950 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011951 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011952
Joe Perches63c3a662011-04-26 08:12:10 +000011953 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011954 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011955 return -EINVAL;
11956
11957 offset = eeprom->offset;
11958 len = eeprom->len;
11959
11960 if ((b_offset = (offset & 3))) {
11961 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011962 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011963 if (ret)
11964 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011965 len += b_offset;
11966 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070011967 if (len < 4)
11968 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011969 }
11970
11971 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070011972 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011973 /* adjustments to end on required 4 byte boundary */
11974 odd_len = 1;
11975 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011976 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011977 if (ret)
11978 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011979 }
11980
11981 buf = data;
11982 if (b_offset || odd_len) {
11983 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011984 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011985 return -ENOMEM;
11986 if (b_offset)
11987 memcpy(buf, &start, 4);
11988 if (odd_len)
11989 memcpy(buf+len-4, &end, 4);
11990 memcpy(buf + b_offset, data, eeprom->len);
11991 }
11992
11993 ret = tg3_nvram_write_block(tp, offset, len, buf);
11994
11995 if (buf != data)
11996 kfree(buf);
11997
11998 return ret;
11999}
12000
12001static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
12002{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012003 struct tg3 *tp = netdev_priv(dev);
12004
Joe Perches63c3a662011-04-26 08:12:10 +000012005 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012006 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012007 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012008 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012009 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012010 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012011 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012012
Linus Torvalds1da177e2005-04-16 15:20:36 -070012013 cmd->supported = (SUPPORTED_Autoneg);
12014
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012015 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012016 cmd->supported |= (SUPPORTED_1000baseT_Half |
12017 SUPPORTED_1000baseT_Full);
12018
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012019 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012020 cmd->supported |= (SUPPORTED_100baseT_Half |
12021 SUPPORTED_100baseT_Full |
12022 SUPPORTED_10baseT_Half |
12023 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080012024 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070012025 cmd->port = PORT_TP;
12026 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012027 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070012028 cmd->port = PORT_FIBRE;
12029 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012030
Linus Torvalds1da177e2005-04-16 15:20:36 -070012031 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000012032 if (tg3_flag(tp, PAUSE_AUTONEG)) {
12033 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
12034 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
12035 cmd->advertising |= ADVERTISED_Pause;
12036 } else {
12037 cmd->advertising |= ADVERTISED_Pause |
12038 ADVERTISED_Asym_Pause;
12039 }
12040 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
12041 cmd->advertising |= ADVERTISED_Asym_Pause;
12042 }
12043 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012044 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000012045 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012046 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000012047 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012048 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
12049 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
12050 cmd->eth_tp_mdix = ETH_TP_MDI_X;
12051 else
12052 cmd->eth_tp_mdix = ETH_TP_MDI;
12053 }
Matt Carlson64c22182010-10-14 10:37:44 +000012054 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000012055 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
12056 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012057 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012058 }
Matt Carlson882e9792009-09-01 13:21:36 +000012059 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012060 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012061 cmd->autoneg = tp->link_config.autoneg;
12062 cmd->maxtxpkt = 0;
12063 cmd->maxrxpkt = 0;
12064 return 0;
12065}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012066
Linus Torvalds1da177e2005-04-16 15:20:36 -070012067static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
12068{
12069 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000012070 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012071
Joe Perches63c3a662011-04-26 08:12:10 +000012072 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012073 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012074 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012075 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012076 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012077 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012078 }
12079
Matt Carlson7e5856b2009-02-25 14:23:01 +000012080 if (cmd->autoneg != AUTONEG_ENABLE &&
12081 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070012082 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012083
12084 if (cmd->autoneg == AUTONEG_DISABLE &&
12085 cmd->duplex != DUPLEX_FULL &&
12086 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070012087 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012088
Matt Carlson7e5856b2009-02-25 14:23:01 +000012089 if (cmd->autoneg == AUTONEG_ENABLE) {
12090 u32 mask = ADVERTISED_Autoneg |
12091 ADVERTISED_Pause |
12092 ADVERTISED_Asym_Pause;
12093
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012094 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012095 mask |= ADVERTISED_1000baseT_Half |
12096 ADVERTISED_1000baseT_Full;
12097
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012098 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012099 mask |= ADVERTISED_100baseT_Half |
12100 ADVERTISED_100baseT_Full |
12101 ADVERTISED_10baseT_Half |
12102 ADVERTISED_10baseT_Full |
12103 ADVERTISED_TP;
12104 else
12105 mask |= ADVERTISED_FIBRE;
12106
12107 if (cmd->advertising & ~mask)
12108 return -EINVAL;
12109
12110 mask &= (ADVERTISED_1000baseT_Half |
12111 ADVERTISED_1000baseT_Full |
12112 ADVERTISED_100baseT_Half |
12113 ADVERTISED_100baseT_Full |
12114 ADVERTISED_10baseT_Half |
12115 ADVERTISED_10baseT_Full);
12116
12117 cmd->advertising &= mask;
12118 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012119 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000012120 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012121 return -EINVAL;
12122
12123 if (cmd->duplex != DUPLEX_FULL)
12124 return -EINVAL;
12125 } else {
David Decotigny25db0332011-04-27 18:32:39 +000012126 if (speed != SPEED_100 &&
12127 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012128 return -EINVAL;
12129 }
12130 }
12131
David S. Millerf47c11e2005-06-24 20:18:35 -070012132 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012133
12134 tp->link_config.autoneg = cmd->autoneg;
12135 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070012136 tp->link_config.advertising = (cmd->advertising |
12137 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000012138 tp->link_config.speed = SPEED_UNKNOWN;
12139 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012140 } else {
12141 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000012142 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012143 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012144 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012145
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012146 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12147
Nithin Sujirce20f162013-04-09 08:48:04 +000012148 tg3_warn_mgmt_link_flap(tp);
12149
Linus Torvalds1da177e2005-04-16 15:20:36 -070012150 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000012151 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012152
David S. Millerf47c11e2005-06-24 20:18:35 -070012153 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012154
Linus Torvalds1da177e2005-04-16 15:20:36 -070012155 return 0;
12156}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012157
Linus Torvalds1da177e2005-04-16 15:20:36 -070012158static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
12159{
12160 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012161
Rick Jones68aad782011-11-07 13:29:27 +000012162 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
12163 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
12164 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
12165 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012166}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012167
Linus Torvalds1da177e2005-04-16 15:20:36 -070012168static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12169{
12170 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012171
Joe Perches63c3a662011-04-26 08:12:10 +000012172 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070012173 wol->supported = WAKE_MAGIC;
12174 else
12175 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012176 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012177 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012178 wol->wolopts = WAKE_MAGIC;
12179 memset(&wol->sopass, 0, sizeof(wol->sopass));
12180}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012181
Linus Torvalds1da177e2005-04-16 15:20:36 -070012182static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12183{
12184 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012185 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012186
Linus Torvalds1da177e2005-04-16 15:20:36 -070012187 if (wol->wolopts & ~WAKE_MAGIC)
12188 return -EINVAL;
12189 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012190 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012191 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012192
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012193 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
12194
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012195 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000012196 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012197 else
Joe Perches63c3a662011-04-26 08:12:10 +000012198 tg3_flag_clear(tp, WOL_ENABLE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012199
Linus Torvalds1da177e2005-04-16 15:20:36 -070012200 return 0;
12201}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012202
Linus Torvalds1da177e2005-04-16 15:20:36 -070012203static u32 tg3_get_msglevel(struct net_device *dev)
12204{
12205 struct tg3 *tp = netdev_priv(dev);
12206 return tp->msg_enable;
12207}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012208
Linus Torvalds1da177e2005-04-16 15:20:36 -070012209static void tg3_set_msglevel(struct net_device *dev, u32 value)
12210{
12211 struct tg3 *tp = netdev_priv(dev);
12212 tp->msg_enable = value;
12213}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012214
Linus Torvalds1da177e2005-04-16 15:20:36 -070012215static int tg3_nway_reset(struct net_device *dev)
12216{
12217 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012218 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012219
Linus Torvalds1da177e2005-04-16 15:20:36 -070012220 if (!netif_running(dev))
12221 return -EAGAIN;
12222
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012223 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070012224 return -EINVAL;
12225
Nithin Sujirce20f162013-04-09 08:48:04 +000012226 tg3_warn_mgmt_link_flap(tp);
12227
Joe Perches63c3a662011-04-26 08:12:10 +000012228 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012229 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012230 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012231 r = phy_start_aneg(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012232 } else {
12233 u32 bmcr;
12234
12235 spin_lock_bh(&tp->lock);
12236 r = -EINVAL;
12237 tg3_readphy(tp, MII_BMCR, &bmcr);
12238 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12239 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012240 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012241 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12242 BMCR_ANENABLE);
12243 r = 0;
12244 }
12245 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012246 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012247
Linus Torvalds1da177e2005-04-16 15:20:36 -070012248 return r;
12249}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012250
Linus Torvalds1da177e2005-04-16 15:20:36 -070012251static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12252{
12253 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012254
Matt Carlson2c49a442010-09-30 10:34:35 +000012255 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012256 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012257 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012258 else
12259 ering->rx_jumbo_max_pending = 0;
12260
12261 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012262
12263 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012264 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012265 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12266 else
12267 ering->rx_jumbo_pending = 0;
12268
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012269 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012270}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012271
Linus Torvalds1da177e2005-04-16 15:20:36 -070012272static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12273{
12274 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012275 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012276
Matt Carlson2c49a442010-09-30 10:34:35 +000012277 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12278 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012279 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12280 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012281 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012282 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012283 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012284
Michael Chanbbe832c2005-06-24 20:20:04 -070012285 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012286 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012287 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012288 irq_sync = 1;
12289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290
Michael Chanbbe832c2005-06-24 20:20:04 -070012291 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012292
Linus Torvalds1da177e2005-04-16 15:20:36 -070012293 tp->rx_pending = ering->rx_pending;
12294
Joe Perches63c3a662011-04-26 08:12:10 +000012295 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012296 tp->rx_pending > 63)
12297 tp->rx_pending = 63;
12298 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012299
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012300 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012301 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012302
12303 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012304 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012305 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012306 if (!err)
12307 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012308 }
12309
David S. Millerf47c11e2005-06-24 20:18:35 -070012310 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012311
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012312 if (irq_sync && !err)
12313 tg3_phy_start(tp);
12314
Michael Chanb9ec6c12006-07-25 16:37:27 -070012315 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012316}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012317
Linus Torvalds1da177e2005-04-16 15:20:36 -070012318static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12319{
12320 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012321
Joe Perches63c3a662011-04-26 08:12:10 +000012322 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012323
Matt Carlson4a2db502011-12-08 14:40:17 +000012324 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012325 epause->rx_pause = 1;
12326 else
12327 epause->rx_pause = 0;
12328
Matt Carlson4a2db502011-12-08 14:40:17 +000012329 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012330 epause->tx_pause = 1;
12331 else
12332 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012333}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012334
Linus Torvalds1da177e2005-04-16 15:20:36 -070012335static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12336{
12337 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012338 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012339
Nithin Sujirce20f162013-04-09 08:48:04 +000012340 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12341 tg3_warn_mgmt_link_flap(tp);
12342
Joe Perches63c3a662011-04-26 08:12:10 +000012343 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012344 u32 newadv;
12345 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012346
Hauke Mehrtensead24022013-09-28 23:15:26 +020012347 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012348
Matt Carlson27121682010-02-17 15:16:57 +000012349 if (!(phydev->supported & SUPPORTED_Pause) ||
12350 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012351 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012352 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012353
Matt Carlson27121682010-02-17 15:16:57 +000012354 tp->link_config.flowctrl = 0;
12355 if (epause->rx_pause) {
12356 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012357
Matt Carlson27121682010-02-17 15:16:57 +000012358 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012359 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012360 newadv = ADVERTISED_Pause;
12361 } else
12362 newadv = ADVERTISED_Pause |
12363 ADVERTISED_Asym_Pause;
12364 } else if (epause->tx_pause) {
12365 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12366 newadv = ADVERTISED_Asym_Pause;
12367 } else
12368 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012369
Matt Carlson27121682010-02-17 15:16:57 +000012370 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012371 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012372 else
Joe Perches63c3a662011-04-26 08:12:10 +000012373 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012374
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012375 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012376 u32 oldadv = phydev->advertising &
12377 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12378 if (oldadv != newadv) {
12379 phydev->advertising &=
12380 ~(ADVERTISED_Pause |
12381 ADVERTISED_Asym_Pause);
12382 phydev->advertising |= newadv;
12383 if (phydev->autoneg) {
12384 /*
12385 * Always renegotiate the link to
12386 * inform our link partner of our
12387 * flow control settings, even if the
12388 * flow control is forced. Let
12389 * tg3_adjust_link() do the final
12390 * flow control setup.
12391 */
12392 return phy_start_aneg(phydev);
12393 }
12394 }
12395
12396 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012397 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012398 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012399 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012400 ~(ADVERTISED_Pause |
12401 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012402 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012403 }
12404 } else {
12405 int irq_sync = 0;
12406
12407 if (netif_running(dev)) {
12408 tg3_netif_stop(tp);
12409 irq_sync = 1;
12410 }
12411
12412 tg3_full_lock(tp, irq_sync);
12413
12414 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012415 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012416 else
Joe Perches63c3a662011-04-26 08:12:10 +000012417 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012418 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012419 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012420 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012421 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012422 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012423 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012424 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012425 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012426
12427 if (netif_running(dev)) {
12428 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012429 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012430 if (!err)
12431 tg3_netif_start(tp);
12432 }
12433
12434 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012436
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012437 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12438
Michael Chanb9ec6c12006-07-25 16:37:27 -070012439 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012440}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012441
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012442static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012443{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012444 switch (sset) {
12445 case ETH_SS_TEST:
12446 return TG3_NUM_TEST;
12447 case ETH_SS_STATS:
12448 return TG3_NUM_STATS;
12449 default:
12450 return -EOPNOTSUPP;
12451 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012452}
12453
Matt Carlson90415472011-12-16 13:33:23 +000012454static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12455 u32 *rules __always_unused)
12456{
12457 struct tg3 *tp = netdev_priv(dev);
12458
12459 if (!tg3_flag(tp, SUPPORT_MSIX))
12460 return -EOPNOTSUPP;
12461
12462 switch (info->cmd) {
12463 case ETHTOOL_GRXRINGS:
12464 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012465 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012466 else {
12467 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012468 if (info->data > TG3_RSS_MAX_NUM_QS)
12469 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012470 }
12471
12472 /* The first interrupt vector only
12473 * handles link interrupts.
12474 */
12475 info->data -= 1;
12476 return 0;
12477
12478 default:
12479 return -EOPNOTSUPP;
12480 }
12481}
12482
12483static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12484{
12485 u32 size = 0;
12486 struct tg3 *tp = netdev_priv(dev);
12487
12488 if (tg3_flag(tp, SUPPORT_MSIX))
12489 size = TG3_RSS_INDIR_TBL_SIZE;
12490
12491 return size;
12492}
12493
12494static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
12495{
12496 struct tg3 *tp = netdev_priv(dev);
12497 int i;
12498
12499 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12500 indir[i] = tp->rss_ind_tbl[i];
12501
12502 return 0;
12503}
12504
12505static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
12506{
12507 struct tg3 *tp = netdev_priv(dev);
12508 size_t i;
12509
12510 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12511 tp->rss_ind_tbl[i] = indir[i];
12512
12513 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12514 return 0;
12515
12516 /* It is legal to write the indirection
12517 * table while the device is running.
12518 */
12519 tg3_full_lock(tp, 0);
12520 tg3_rss_write_indir_tbl(tp);
12521 tg3_full_unlock(tp);
12522
12523 return 0;
12524}
12525
Michael Chan09681692012-09-28 07:12:42 +000012526static void tg3_get_channels(struct net_device *dev,
12527 struct ethtool_channels *channel)
12528{
12529 struct tg3 *tp = netdev_priv(dev);
12530 u32 deflt_qs = netif_get_num_default_rss_queues();
12531
12532 channel->max_rx = tp->rxq_max;
12533 channel->max_tx = tp->txq_max;
12534
12535 if (netif_running(dev)) {
12536 channel->rx_count = tp->rxq_cnt;
12537 channel->tx_count = tp->txq_cnt;
12538 } else {
12539 if (tp->rxq_req)
12540 channel->rx_count = tp->rxq_req;
12541 else
12542 channel->rx_count = min(deflt_qs, tp->rxq_max);
12543
12544 if (tp->txq_req)
12545 channel->tx_count = tp->txq_req;
12546 else
12547 channel->tx_count = min(deflt_qs, tp->txq_max);
12548 }
12549}
12550
12551static int tg3_set_channels(struct net_device *dev,
12552 struct ethtool_channels *channel)
12553{
12554 struct tg3 *tp = netdev_priv(dev);
12555
12556 if (!tg3_flag(tp, SUPPORT_MSIX))
12557 return -EOPNOTSUPP;
12558
12559 if (channel->rx_count > tp->rxq_max ||
12560 channel->tx_count > tp->txq_max)
12561 return -EINVAL;
12562
12563 tp->rxq_req = channel->rx_count;
12564 tp->txq_req = channel->tx_count;
12565
12566 if (!netif_running(dev))
12567 return 0;
12568
12569 tg3_stop(tp);
12570
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012571 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012572
Matt Carlsonbe947302012-12-03 19:36:57 +000012573 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012574
12575 return 0;
12576}
12577
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012578static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012579{
12580 switch (stringset) {
12581 case ETH_SS_STATS:
12582 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12583 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012584 case ETH_SS_TEST:
12585 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012587 default:
12588 WARN_ON(1); /* we need a WARN() */
12589 break;
12590 }
12591}
12592
stephen hemminger81b87092011-04-04 08:43:50 +000012593static int tg3_set_phys_id(struct net_device *dev,
12594 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012595{
12596 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012597
12598 if (!netif_running(tp->dev))
12599 return -EAGAIN;
12600
stephen hemminger81b87092011-04-04 08:43:50 +000012601 switch (state) {
12602 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012603 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012604
stephen hemminger81b87092011-04-04 08:43:50 +000012605 case ETHTOOL_ID_ON:
12606 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12607 LED_CTRL_1000MBPS_ON |
12608 LED_CTRL_100MBPS_ON |
12609 LED_CTRL_10MBPS_ON |
12610 LED_CTRL_TRAFFIC_OVERRIDE |
12611 LED_CTRL_TRAFFIC_BLINK |
12612 LED_CTRL_TRAFFIC_LED);
12613 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012614
stephen hemminger81b87092011-04-04 08:43:50 +000012615 case ETHTOOL_ID_OFF:
12616 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12617 LED_CTRL_TRAFFIC_OVERRIDE);
12618 break;
Michael Chan4009a932005-09-05 17:52:54 -070012619
stephen hemminger81b87092011-04-04 08:43:50 +000012620 case ETHTOOL_ID_INACTIVE:
12621 tw32(MAC_LED_CTRL, tp->led_ctrl);
12622 break;
Michael Chan4009a932005-09-05 17:52:54 -070012623 }
stephen hemminger81b87092011-04-04 08:43:50 +000012624
Michael Chan4009a932005-09-05 17:52:54 -070012625 return 0;
12626}
12627
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012628static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012629 struct ethtool_stats *estats, u64 *tmp_stats)
12630{
12631 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012632
Matt Carlsonb546e462012-02-13 15:20:09 +000012633 if (tp->hw_stats)
12634 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12635 else
12636 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012637}
12638
Matt Carlson535a4902011-07-20 10:20:56 +000012639static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012640{
12641 int i;
12642 __be32 *buf;
12643 u32 offset = 0, len = 0;
12644 u32 magic, val;
12645
Joe Perches63c3a662011-04-26 08:12:10 +000012646 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012647 return NULL;
12648
12649 if (magic == TG3_EEPROM_MAGIC) {
12650 for (offset = TG3_NVM_DIR_START;
12651 offset < TG3_NVM_DIR_END;
12652 offset += TG3_NVM_DIRENT_SIZE) {
12653 if (tg3_nvram_read(tp, offset, &val))
12654 return NULL;
12655
12656 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12657 TG3_NVM_DIRTYPE_EXTVPD)
12658 break;
12659 }
12660
12661 if (offset != TG3_NVM_DIR_END) {
12662 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12663 if (tg3_nvram_read(tp, offset + 4, &offset))
12664 return NULL;
12665
12666 offset = tg3_nvram_logical_addr(tp, offset);
12667 }
12668 }
12669
12670 if (!offset || !len) {
12671 offset = TG3_NVM_VPD_OFF;
12672 len = TG3_NVM_VPD_LEN;
12673 }
12674
12675 buf = kmalloc(len, GFP_KERNEL);
12676 if (buf == NULL)
12677 return NULL;
12678
12679 if (magic == TG3_EEPROM_MAGIC) {
12680 for (i = 0; i < len; i += 4) {
12681 /* The data is in little-endian format in NVRAM.
12682 * Use the big-endian read routines to preserve
12683 * the byte order as it exists in NVRAM.
12684 */
12685 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12686 goto error;
12687 }
12688 } else {
12689 u8 *ptr;
12690 ssize_t cnt;
12691 unsigned int pos = 0;
12692
12693 ptr = (u8 *)&buf[0];
12694 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12695 cnt = pci_read_vpd(tp->pdev, pos,
12696 len - pos, ptr);
12697 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12698 cnt = 0;
12699 else if (cnt < 0)
12700 goto error;
12701 }
12702 if (pos != len)
12703 goto error;
12704 }
12705
Matt Carlson535a4902011-07-20 10:20:56 +000012706 *vpdlen = len;
12707
Matt Carlsonc3e94502011-04-13 11:05:08 +000012708 return buf;
12709
12710error:
12711 kfree(buf);
12712 return NULL;
12713}
12714
Michael Chan566f86a2005-05-29 14:56:58 -070012715#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012716#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12717#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12718#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012719#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12720#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012721#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012722#define NVRAM_SELFBOOT_HW_SIZE 0x20
12723#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012724
12725static int tg3_test_nvram(struct tg3 *tp)
12726{
Matt Carlson535a4902011-07-20 10:20:56 +000012727 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012728 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012729 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012730
Joe Perches63c3a662011-04-26 08:12:10 +000012731 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012732 return 0;
12733
Matt Carlsone4f34112009-02-25 14:25:00 +000012734 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012735 return -EIO;
12736
Michael Chan1b277772006-03-20 22:27:48 -080012737 if (magic == TG3_EEPROM_MAGIC)
12738 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012739 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012740 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12741 TG3_EEPROM_SB_FORMAT_1) {
12742 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12743 case TG3_EEPROM_SB_REVISION_0:
12744 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12745 break;
12746 case TG3_EEPROM_SB_REVISION_2:
12747 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12748 break;
12749 case TG3_EEPROM_SB_REVISION_3:
12750 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12751 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012752 case TG3_EEPROM_SB_REVISION_4:
12753 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12754 break;
12755 case TG3_EEPROM_SB_REVISION_5:
12756 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12757 break;
12758 case TG3_EEPROM_SB_REVISION_6:
12759 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12760 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012761 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012762 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012763 }
12764 } else
Michael Chan1b277772006-03-20 22:27:48 -080012765 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012766 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12767 size = NVRAM_SELFBOOT_HW_SIZE;
12768 else
Michael Chan1b277772006-03-20 22:27:48 -080012769 return -EIO;
12770
12771 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012772 if (buf == NULL)
12773 return -ENOMEM;
12774
Michael Chan1b277772006-03-20 22:27:48 -080012775 err = -EIO;
12776 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012777 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12778 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012779 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012780 }
Michael Chan1b277772006-03-20 22:27:48 -080012781 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012782 goto out;
12783
Michael Chan1b277772006-03-20 22:27:48 -080012784 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012785 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012786 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012787 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012788 u8 *buf8 = (u8 *) buf, csum8 = 0;
12789
Al Virob9fc7dc2007-12-17 22:59:57 -080012790 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012791 TG3_EEPROM_SB_REVISION_2) {
12792 /* For rev 2, the csum doesn't include the MBA. */
12793 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12794 csum8 += buf8[i];
12795 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12796 csum8 += buf8[i];
12797 } else {
12798 for (i = 0; i < size; i++)
12799 csum8 += buf8[i];
12800 }
Michael Chan1b277772006-03-20 22:27:48 -080012801
Adrian Bunkad96b482006-04-05 22:21:04 -070012802 if (csum8 == 0) {
12803 err = 0;
12804 goto out;
12805 }
12806
12807 err = -EIO;
12808 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012809 }
Michael Chan566f86a2005-05-29 14:56:58 -070012810
Al Virob9fc7dc2007-12-17 22:59:57 -080012811 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012812 TG3_EEPROM_MAGIC_HW) {
12813 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012814 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012815 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012816
12817 /* Separate the parity bits and the data bytes. */
12818 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12819 if ((i == 0) || (i == 8)) {
12820 int l;
12821 u8 msk;
12822
12823 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12824 parity[k++] = buf8[i] & msk;
12825 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012826 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012827 int l;
12828 u8 msk;
12829
12830 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12831 parity[k++] = buf8[i] & msk;
12832 i++;
12833
12834 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12835 parity[k++] = buf8[i] & msk;
12836 i++;
12837 }
12838 data[j++] = buf8[i];
12839 }
12840
12841 err = -EIO;
12842 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12843 u8 hw8 = hweight8(data[i]);
12844
12845 if ((hw8 & 0x1) && parity[i])
12846 goto out;
12847 else if (!(hw8 & 0x1) && !parity[i])
12848 goto out;
12849 }
12850 err = 0;
12851 goto out;
12852 }
12853
Matt Carlson01c3a392011-03-09 16:58:20 +000012854 err = -EIO;
12855
Michael Chan566f86a2005-05-29 14:56:58 -070012856 /* Bootstrap checksum at offset 0x10 */
12857 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012858 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012859 goto out;
12860
12861 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12862 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012863 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012864 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012865
Matt Carlsonc3e94502011-04-13 11:05:08 +000012866 kfree(buf);
12867
Matt Carlson535a4902011-07-20 10:20:56 +000012868 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012869 if (!buf)
12870 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012871
Matt Carlson535a4902011-07-20 10:20:56 +000012872 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012873 if (i > 0) {
12874 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12875 if (j < 0)
12876 goto out;
12877
Matt Carlson535a4902011-07-20 10:20:56 +000012878 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012879 goto out;
12880
12881 i += PCI_VPD_LRDT_TAG_SIZE;
12882 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12883 PCI_VPD_RO_KEYWORD_CHKSUM);
12884 if (j > 0) {
12885 u8 csum8 = 0;
12886
12887 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12888
12889 for (i = 0; i <= j; i++)
12890 csum8 += ((u8 *)buf)[i];
12891
12892 if (csum8)
12893 goto out;
12894 }
12895 }
12896
Michael Chan566f86a2005-05-29 14:56:58 -070012897 err = 0;
12898
12899out:
12900 kfree(buf);
12901 return err;
12902}
12903
Michael Chanca430072005-05-29 14:57:23 -070012904#define TG3_SERDES_TIMEOUT_SEC 2
12905#define TG3_COPPER_TIMEOUT_SEC 6
12906
12907static int tg3_test_link(struct tg3 *tp)
12908{
12909 int i, max;
12910
12911 if (!netif_running(tp->dev))
12912 return -ENODEV;
12913
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012914 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012915 max = TG3_SERDES_TIMEOUT_SEC;
12916 else
12917 max = TG3_COPPER_TIMEOUT_SEC;
12918
12919 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012920 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012921 return 0;
12922
12923 if (msleep_interruptible(1000))
12924 break;
12925 }
12926
12927 return -EIO;
12928}
12929
Michael Chana71116d2005-05-29 14:58:11 -070012930/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012931static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012932{
Michael Chanb16250e2006-09-27 16:10:14 -070012933 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012934 u32 offset, read_mask, write_mask, val, save_val, read_val;
12935 static struct {
12936 u16 offset;
12937 u16 flags;
12938#define TG3_FL_5705 0x1
12939#define TG3_FL_NOT_5705 0x2
12940#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012941#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012942 u32 read_mask;
12943 u32 write_mask;
12944 } reg_tbl[] = {
12945 /* MAC Control Registers */
12946 { MAC_MODE, TG3_FL_NOT_5705,
12947 0x00000000, 0x00ef6f8c },
12948 { MAC_MODE, TG3_FL_5705,
12949 0x00000000, 0x01ef6b8c },
12950 { MAC_STATUS, TG3_FL_NOT_5705,
12951 0x03800107, 0x00000000 },
12952 { MAC_STATUS, TG3_FL_5705,
12953 0x03800100, 0x00000000 },
12954 { MAC_ADDR_0_HIGH, 0x0000,
12955 0x00000000, 0x0000ffff },
12956 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012957 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012958 { MAC_RX_MTU_SIZE, 0x0000,
12959 0x00000000, 0x0000ffff },
12960 { MAC_TX_MODE, 0x0000,
12961 0x00000000, 0x00000070 },
12962 { MAC_TX_LENGTHS, 0x0000,
12963 0x00000000, 0x00003fff },
12964 { MAC_RX_MODE, TG3_FL_NOT_5705,
12965 0x00000000, 0x000007fc },
12966 { MAC_RX_MODE, TG3_FL_5705,
12967 0x00000000, 0x000007dc },
12968 { MAC_HASH_REG_0, 0x0000,
12969 0x00000000, 0xffffffff },
12970 { MAC_HASH_REG_1, 0x0000,
12971 0x00000000, 0xffffffff },
12972 { MAC_HASH_REG_2, 0x0000,
12973 0x00000000, 0xffffffff },
12974 { MAC_HASH_REG_3, 0x0000,
12975 0x00000000, 0xffffffff },
12976
12977 /* Receive Data and Receive BD Initiator Control Registers. */
12978 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12979 0x00000000, 0xffffffff },
12980 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12981 0x00000000, 0xffffffff },
12982 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12983 0x00000000, 0x00000003 },
12984 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12985 0x00000000, 0xffffffff },
12986 { RCVDBDI_STD_BD+0, 0x0000,
12987 0x00000000, 0xffffffff },
12988 { RCVDBDI_STD_BD+4, 0x0000,
12989 0x00000000, 0xffffffff },
12990 { RCVDBDI_STD_BD+8, 0x0000,
12991 0x00000000, 0xffff0002 },
12992 { RCVDBDI_STD_BD+0xc, 0x0000,
12993 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012994
Michael Chana71116d2005-05-29 14:58:11 -070012995 /* Receive BD Initiator Control Registers. */
12996 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12997 0x00000000, 0xffffffff },
12998 { RCVBDI_STD_THRESH, TG3_FL_5705,
12999 0x00000000, 0x000003ff },
13000 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
13001 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013002
Michael Chana71116d2005-05-29 14:58:11 -070013003 /* Host Coalescing Control Registers. */
13004 { HOSTCC_MODE, TG3_FL_NOT_5705,
13005 0x00000000, 0x00000004 },
13006 { HOSTCC_MODE, TG3_FL_5705,
13007 0x00000000, 0x000000f6 },
13008 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
13009 0x00000000, 0xffffffff },
13010 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
13011 0x00000000, 0x000003ff },
13012 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
13013 0x00000000, 0xffffffff },
13014 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
13015 0x00000000, 0x000003ff },
13016 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
13017 0x00000000, 0xffffffff },
13018 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13019 0x00000000, 0x000000ff },
13020 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
13021 0x00000000, 0xffffffff },
13022 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13023 0x00000000, 0x000000ff },
13024 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
13025 0x00000000, 0xffffffff },
13026 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
13027 0x00000000, 0xffffffff },
13028 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13029 0x00000000, 0xffffffff },
13030 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13031 0x00000000, 0x000000ff },
13032 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13033 0x00000000, 0xffffffff },
13034 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13035 0x00000000, 0x000000ff },
13036 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
13037 0x00000000, 0xffffffff },
13038 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
13039 0x00000000, 0xffffffff },
13040 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
13041 0x00000000, 0xffffffff },
13042 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
13043 0x00000000, 0xffffffff },
13044 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
13045 0x00000000, 0xffffffff },
13046 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
13047 0xffffffff, 0x00000000 },
13048 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
13049 0xffffffff, 0x00000000 },
13050
13051 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070013052 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013053 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070013054 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013055 0x00000000, 0x007fffff },
13056 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
13057 0x00000000, 0x0000003f },
13058 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
13059 0x00000000, 0x000001ff },
13060 { BUFMGR_MB_HIGH_WATER, 0x0000,
13061 0x00000000, 0x000001ff },
13062 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
13063 0xffffffff, 0x00000000 },
13064 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
13065 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013066
Michael Chana71116d2005-05-29 14:58:11 -070013067 /* Mailbox Registers */
13068 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
13069 0x00000000, 0x000001ff },
13070 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
13071 0x00000000, 0x000001ff },
13072 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
13073 0x00000000, 0x000007ff },
13074 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
13075 0x00000000, 0x000001ff },
13076
13077 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
13078 };
13079
Michael Chanb16250e2006-09-27 16:10:14 -070013080 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013081 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070013082 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000013083 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070013084 is_5750 = 1;
13085 }
Michael Chana71116d2005-05-29 14:58:11 -070013086
13087 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
13088 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
13089 continue;
13090
13091 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
13092 continue;
13093
Joe Perches63c3a662011-04-26 08:12:10 +000013094 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070013095 (reg_tbl[i].flags & TG3_FL_NOT_5788))
13096 continue;
13097
Michael Chanb16250e2006-09-27 16:10:14 -070013098 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
13099 continue;
13100
Michael Chana71116d2005-05-29 14:58:11 -070013101 offset = (u32) reg_tbl[i].offset;
13102 read_mask = reg_tbl[i].read_mask;
13103 write_mask = reg_tbl[i].write_mask;
13104
13105 /* Save the original register content */
13106 save_val = tr32(offset);
13107
13108 /* Determine the read-only value. */
13109 read_val = save_val & read_mask;
13110
13111 /* Write zero to the register, then make sure the read-only bits
13112 * are not changed and the read/write bits are all zeros.
13113 */
13114 tw32(offset, 0);
13115
13116 val = tr32(offset);
13117
13118 /* Test the read-only and read/write bits. */
13119 if (((val & read_mask) != read_val) || (val & write_mask))
13120 goto out;
13121
13122 /* Write ones to all the bits defined by RdMask and WrMask, then
13123 * make sure the read-only bits are not changed and the
13124 * read/write bits are all ones.
13125 */
13126 tw32(offset, read_mask | write_mask);
13127
13128 val = tr32(offset);
13129
13130 /* Test the read-only bits. */
13131 if ((val & read_mask) != read_val)
13132 goto out;
13133
13134 /* Test the read/write bits. */
13135 if ((val & write_mask) != write_mask)
13136 goto out;
13137
13138 tw32(offset, save_val);
13139 }
13140
13141 return 0;
13142
13143out:
Michael Chan9f88f292006-12-07 00:22:54 -080013144 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000013145 netdev_err(tp->dev,
13146 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070013147 tw32(offset, save_val);
13148 return -EIO;
13149}
13150
Michael Chan7942e1d2005-05-29 14:58:36 -070013151static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
13152{
Arjan van de Venf71e1302006-03-03 21:33:57 -050013153 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070013154 int i;
13155 u32 j;
13156
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020013157 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070013158 for (j = 0; j < len; j += 4) {
13159 u32 val;
13160
13161 tg3_write_mem(tp, offset + j, test_pattern[i]);
13162 tg3_read_mem(tp, offset + j, &val);
13163 if (val != test_pattern[i])
13164 return -EIO;
13165 }
13166 }
13167 return 0;
13168}
13169
13170static int tg3_test_memory(struct tg3 *tp)
13171{
13172 static struct mem_entry {
13173 u32 offset;
13174 u32 len;
13175 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080013176 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070013177 { 0x00002000, 0x1c000},
13178 { 0xffffffff, 0x00000}
13179 }, mem_tbl_5705[] = {
13180 { 0x00000100, 0x0000c},
13181 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070013182 { 0x00004000, 0x00800},
13183 { 0x00006000, 0x01000},
13184 { 0x00008000, 0x02000},
13185 { 0x00010000, 0x0e000},
13186 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080013187 }, mem_tbl_5755[] = {
13188 { 0x00000200, 0x00008},
13189 { 0x00004000, 0x00800},
13190 { 0x00006000, 0x00800},
13191 { 0x00008000, 0x02000},
13192 { 0x00010000, 0x0c000},
13193 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070013194 }, mem_tbl_5906[] = {
13195 { 0x00000200, 0x00008},
13196 { 0x00004000, 0x00400},
13197 { 0x00006000, 0x00400},
13198 { 0x00008000, 0x01000},
13199 { 0x00010000, 0x01000},
13200 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013201 }, mem_tbl_5717[] = {
13202 { 0x00000200, 0x00008},
13203 { 0x00010000, 0x0a000},
13204 { 0x00020000, 0x13c00},
13205 { 0xffffffff, 0x00000}
13206 }, mem_tbl_57765[] = {
13207 { 0x00000200, 0x00008},
13208 { 0x00004000, 0x00800},
13209 { 0x00006000, 0x09800},
13210 { 0x00010000, 0x0a000},
13211 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070013212 };
13213 struct mem_entry *mem_tbl;
13214 int err = 0;
13215 int i;
13216
Joe Perches63c3a662011-04-26 08:12:10 +000013217 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013218 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000013219 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000013220 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013221 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000013222 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013223 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000013224 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080013225 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000013226 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013227 mem_tbl = mem_tbl_5705;
13228 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013229 mem_tbl = mem_tbl_570x;
13230
13231 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013232 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13233 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013234 break;
13235 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013236
Michael Chan7942e1d2005-05-29 14:58:36 -070013237 return err;
13238}
13239
Matt Carlsonbb158d62011-04-25 12:42:47 +000013240#define TG3_TSO_MSS 500
13241
13242#define TG3_TSO_IP_HDR_LEN 20
13243#define TG3_TSO_TCP_HDR_LEN 20
13244#define TG3_TSO_TCP_OPT_LEN 12
13245
13246static const u8 tg3_tso_header[] = {
132470x08, 0x00,
132480x45, 0x00, 0x00, 0x00,
132490x00, 0x00, 0x40, 0x00,
132500x40, 0x06, 0x00, 0x00,
132510x0a, 0x00, 0x00, 0x01,
132520x0a, 0x00, 0x00, 0x02,
132530x0d, 0x00, 0xe0, 0x00,
132540x00, 0x00, 0x01, 0x00,
132550x00, 0x00, 0x02, 0x00,
132560x80, 0x10, 0x10, 0x00,
132570x14, 0x09, 0x00, 0x00,
132580x01, 0x01, 0x08, 0x0a,
132590x11, 0x11, 0x11, 0x11,
132600x11, 0x11, 0x11, 0x11,
13261};
Michael Chan9f40dea2005-09-05 17:53:06 -070013262
Matt Carlson28a45952011-08-19 13:58:22 +000013263static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013264{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013265 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013266 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013267 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013268 struct sk_buff *skb;
13269 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013270 dma_addr_t map;
13271 int num_pkts, tx_len, rx_len, i, err;
13272 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013273 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013274 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013275
Matt Carlsonc8873402010-02-12 14:47:11 +000013276 tnapi = &tp->napi[0];
13277 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013278 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013279 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013280 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013281 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013282 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013283 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013284 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013285
Michael Chanc76949a2005-05-29 14:58:59 -070013286 err = -EIO;
13287
Matt Carlson4852a862011-04-13 11:05:07 +000013288 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013289 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013290 if (!skb)
13291 return -ENOMEM;
13292
Michael Chanc76949a2005-05-29 14:58:59 -070013293 tx_data = skb_put(skb, tx_len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070013294 memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
13295 memset(tx_data + ETH_ALEN, 0x0, 8);
Michael Chanc76949a2005-05-29 14:58:59 -070013296
Matt Carlson4852a862011-04-13 11:05:07 +000013297 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013298
Matt Carlson28a45952011-08-19 13:58:22 +000013299 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013300 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13301
13302 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13303 TG3_TSO_TCP_OPT_LEN;
13304
13305 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13306 sizeof(tg3_tso_header));
13307 mss = TG3_TSO_MSS;
13308
13309 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13310 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13311
13312 /* Set the total length field in the IP header */
13313 iph->tot_len = htons((u16)(mss + hdr_len));
13314
13315 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13316 TXD_FLAG_CPU_POST_DMA);
13317
Joe Perches63c3a662011-04-26 08:12:10 +000013318 if (tg3_flag(tp, HW_TSO_1) ||
13319 tg3_flag(tp, HW_TSO_2) ||
13320 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013321 struct tcphdr *th;
13322 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13323 th = (struct tcphdr *)&tx_data[val];
13324 th->check = 0;
13325 } else
13326 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13327
Joe Perches63c3a662011-04-26 08:12:10 +000013328 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013329 mss |= (hdr_len & 0xc) << 12;
13330 if (hdr_len & 0x10)
13331 base_flags |= 0x00000010;
13332 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013333 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013334 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013335 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013336 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013337 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13338 } else {
13339 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13340 }
13341
13342 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13343 } else {
13344 num_pkts = 1;
13345 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013346
13347 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13348 tx_len > VLAN_ETH_FRAME_LEN)
13349 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013350 }
13351
13352 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013353 tx_data[i] = (u8) (i & 0xff);
13354
Alexander Duyckf4188d82009-12-02 16:48:38 +000013355 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13356 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013357 dev_kfree_skb(skb);
13358 return -EIO;
13359 }
Michael Chanc76949a2005-05-29 14:58:59 -070013360
Matt Carlson0d681b22011-07-27 14:20:49 +000013361 val = tnapi->tx_prod;
13362 tnapi->tx_buffers[val].skb = skb;
13363 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13364
Michael Chanc76949a2005-05-29 14:58:59 -070013365 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013366 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013367
13368 udelay(10);
13369
Matt Carlson898a56f2009-08-28 14:02:40 +000013370 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013371
Matt Carlson84b67b22011-07-27 14:20:52 +000013372 budget = tg3_tx_avail(tnapi);
13373 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013374 base_flags | TXD_FLAG_END, mss, 0)) {
13375 tnapi->tx_buffers[val].skb = NULL;
13376 dev_kfree_skb(skb);
13377 return -EIO;
13378 }
Michael Chanc76949a2005-05-29 14:58:59 -070013379
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013380 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013381
Michael Chan6541b802012-03-04 14:48:14 +000013382 /* Sync BD data before updating mailbox */
13383 wmb();
13384
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013385 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13386 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013387
13388 udelay(10);
13389
Matt Carlson303fc922009-11-02 14:27:34 +000013390 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13391 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013392 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013393 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013394
13395 udelay(10);
13396
Matt Carlson898a56f2009-08-28 14:02:40 +000013397 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13398 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013399 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013400 (rx_idx == (rx_start_idx + num_pkts)))
13401 break;
13402 }
13403
Matt Carlsonba1142e2011-11-04 09:15:00 +000013404 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013405 dev_kfree_skb(skb);
13406
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013407 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013408 goto out;
13409
13410 if (rx_idx != rx_start_idx + num_pkts)
13411 goto out;
13412
Matt Carlsonbb158d62011-04-25 12:42:47 +000013413 val = data_off;
13414 while (rx_idx != rx_start_idx) {
13415 desc = &rnapi->rx_rcb[rx_start_idx++];
13416 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13417 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013418
Matt Carlsonbb158d62011-04-25 12:42:47 +000013419 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13420 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013421 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013422
Matt Carlsonbb158d62011-04-25 12:42:47 +000013423 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13424 - ETH_FCS_LEN;
13425
Matt Carlson28a45952011-08-19 13:58:22 +000013426 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013427 if (rx_len != tx_len)
13428 goto out;
13429
13430 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13431 if (opaque_key != RXD_OPAQUE_RING_STD)
13432 goto out;
13433 } else {
13434 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13435 goto out;
13436 }
13437 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13438 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013439 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013440 goto out;
13441 }
13442
13443 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013444 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013445 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13446 mapping);
13447 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013448 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013449 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13450 mapping);
13451 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013452 goto out;
13453
Matt Carlsonbb158d62011-04-25 12:42:47 +000013454 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13455 PCI_DMA_FROMDEVICE);
13456
Eric Dumazet9205fd92011-11-18 06:47:01 +000013457 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013458 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013459 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013460 goto out;
13461 }
Matt Carlson4852a862011-04-13 11:05:07 +000013462 }
13463
Michael Chanc76949a2005-05-29 14:58:59 -070013464 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013465
Eric Dumazet9205fd92011-11-18 06:47:01 +000013466 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013467out:
13468 return err;
13469}
13470
Matt Carlson00c266b2011-04-25 12:42:46 +000013471#define TG3_STD_LOOPBACK_FAILED 1
13472#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013473#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013474#define TG3_LOOPBACK_FAILED \
13475 (TG3_STD_LOOPBACK_FAILED | \
13476 TG3_JMB_LOOPBACK_FAILED | \
13477 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013478
Matt Carlson941ec902011-08-19 13:58:23 +000013479static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013480{
Matt Carlson28a45952011-08-19 13:58:22 +000013481 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013482 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013483 u32 jmb_pkt_sz = 9000;
13484
13485 if (tp->dma_limit)
13486 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013487
Matt Carlsonab789042011-01-25 15:58:54 +000013488 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13489 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13490
Matt Carlson28a45952011-08-19 13:58:22 +000013491 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013492 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13493 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013494 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013495 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013496 goto done;
13497 }
13498
Joe Perches953c96e2013-04-09 10:18:14 +000013499 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013500 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013501 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13502 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013503 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013504 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013505 goto done;
13506 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013507
Joe Perches63c3a662011-04-26 08:12:10 +000013508 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013509 int i;
13510
13511 /* Reroute all rx packets to the 1st queue */
13512 for (i = MAC_RSS_INDIR_TBL_0;
13513 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13514 tw32(i, 0x0);
13515 }
13516
Matt Carlson6e01b202011-08-19 13:58:20 +000013517 /* HW errata - mac loopback fails in some cases on 5780.
13518 * Normal traffic and PHY loopback are not affected by
13519 * errata. Also, the MAC loopback test is deprecated for
13520 * all newer ASIC revisions.
13521 */
Joe Perches41535772013-02-16 11:20:04 +000013522 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013523 !tg3_flag(tp, CPMU_PRESENT)) {
13524 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013525
Matt Carlson28a45952011-08-19 13:58:22 +000013526 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013527 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013528
13529 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013530 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013531 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013532
13533 tg3_mac_loopback(tp, false);
13534 }
Matt Carlson4852a862011-04-13 11:05:07 +000013535
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013536 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013537 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013538 int i;
13539
Matt Carlson941ec902011-08-19 13:58:23 +000013540 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013541
13542 /* Wait for link */
13543 for (i = 0; i < 100; i++) {
13544 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13545 break;
13546 mdelay(1);
13547 }
13548
Matt Carlson28a45952011-08-19 13:58:22 +000013549 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013550 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013551 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013552 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013553 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013554 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013555 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013556 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013557
Matt Carlson941ec902011-08-19 13:58:23 +000013558 if (do_extlpbk) {
13559 tg3_phy_lpbk_set(tp, 0, true);
13560
13561 /* All link indications report up, but the hardware
13562 * isn't really ready for about 20 msec. Double it
13563 * to be sure.
13564 */
13565 mdelay(40);
13566
13567 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013568 data[TG3_EXT_LOOPB_TEST] |=
13569 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013570 if (tg3_flag(tp, TSO_CAPABLE) &&
13571 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013572 data[TG3_EXT_LOOPB_TEST] |=
13573 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013574 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013575 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013576 data[TG3_EXT_LOOPB_TEST] |=
13577 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013578 }
13579
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013580 /* Re-enable gphy autopowerdown. */
13581 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13582 tg3_phy_toggle_apd(tp, true);
13583 }
Matt Carlson6833c042008-11-21 17:18:59 -080013584
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013585 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13586 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013587
Matt Carlsonab789042011-01-25 15:58:54 +000013588done:
13589 tp->phy_flags |= eee_cap;
13590
Michael Chan9f40dea2005-09-05 17:53:06 -070013591 return err;
13592}
13593
Michael Chan4cafd3f2005-05-29 14:56:34 -070013594static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13595 u64 *data)
13596{
Michael Chan566f86a2005-05-29 14:56:58 -070013597 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013598 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013599
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013600 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13601 if (tg3_power_up(tp)) {
13602 etest->flags |= ETH_TEST_FL_FAILED;
13603 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13604 return;
13605 }
13606 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013607 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013608
Michael Chan566f86a2005-05-29 14:56:58 -070013609 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13610
13611 if (tg3_test_nvram(tp) != 0) {
13612 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013613 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013614 }
Matt Carlson941ec902011-08-19 13:58:23 +000013615 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013616 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013617 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013618 }
Michael Chana71116d2005-05-29 14:58:11 -070013619 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013620 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013621
Michael Chanbbe832c2005-06-24 20:20:04 -070013622 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013623 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013624 tg3_netif_stop(tp);
13625 irq_sync = 1;
13626 }
13627
13628 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013629 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013630 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013631 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013632 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013633 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013634 if (!err)
13635 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013636
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013637 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013638 tg3_phy_reset(tp);
13639
Michael Chana71116d2005-05-29 14:58:11 -070013640 if (tg3_test_registers(tp) != 0) {
13641 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013642 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013643 }
Matt Carlson28a45952011-08-19 13:58:22 +000013644
Michael Chan7942e1d2005-05-29 14:58:36 -070013645 if (tg3_test_memory(tp) != 0) {
13646 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013647 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013648 }
Matt Carlson28a45952011-08-19 13:58:22 +000013649
Matt Carlson941ec902011-08-19 13:58:23 +000013650 if (doextlpbk)
13651 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13652
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013653 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013654 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013655
David S. Millerf47c11e2005-06-24 20:18:35 -070013656 tg3_full_unlock(tp);
13657
Michael Chand4bc3922005-05-29 14:59:20 -070013658 if (tg3_test_interrupt(tp) != 0) {
13659 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013660 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013661 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013662
13663 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013664
Michael Chana71116d2005-05-29 14:58:11 -070013665 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13666 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013667 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013668 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013669 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013670 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013671 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013672
13673 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013674
13675 if (irq_sync && !err2)
13676 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013677 }
Matt Carlson80096062010-08-02 11:26:06 +000013678 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Nithin Sujir5137a2e2013-07-29 13:58:36 -070013679 tg3_power_down_prepare(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013680
Michael Chan4cafd3f2005-05-29 14:56:34 -070013681}
13682
Ben Hutchings72608992013-11-18 22:59:43 +000013683static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013684{
13685 struct tg3 *tp = netdev_priv(dev);
13686 struct hwtstamp_config stmpconf;
13687
13688 if (!tg3_flag(tp, PTP_CAPABLE))
Ben Hutchings72608992013-11-18 22:59:43 +000013689 return -EOPNOTSUPP;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013690
13691 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13692 return -EFAULT;
13693
13694 if (stmpconf.flags)
13695 return -EINVAL;
13696
Ben Hutchings58b187c2013-11-14 00:40:56 +000013697 if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
13698 stmpconf.tx_type != HWTSTAMP_TX_OFF)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013699 return -ERANGE;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013700
13701 switch (stmpconf.rx_filter) {
13702 case HWTSTAMP_FILTER_NONE:
13703 tp->rxptpctl = 0;
13704 break;
13705 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13706 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13707 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13708 break;
13709 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13710 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13711 TG3_RX_PTP_CTL_SYNC_EVNT;
13712 break;
13713 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13714 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13715 TG3_RX_PTP_CTL_DELAY_REQ;
13716 break;
13717 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13718 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13719 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13720 break;
13721 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13722 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13723 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13724 break;
13725 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13726 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13727 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13728 break;
13729 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13730 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13731 TG3_RX_PTP_CTL_SYNC_EVNT;
13732 break;
13733 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13734 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13735 TG3_RX_PTP_CTL_SYNC_EVNT;
13736 break;
13737 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13738 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13739 TG3_RX_PTP_CTL_SYNC_EVNT;
13740 break;
13741 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13742 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13743 TG3_RX_PTP_CTL_DELAY_REQ;
13744 break;
13745 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13746 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13747 TG3_RX_PTP_CTL_DELAY_REQ;
13748 break;
13749 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13750 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13751 TG3_RX_PTP_CTL_DELAY_REQ;
13752 break;
13753 default:
13754 return -ERANGE;
13755 }
13756
13757 if (netif_running(dev) && tp->rxptpctl)
13758 tw32(TG3_RX_PTP_CTL,
13759 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13760
Ben Hutchings58b187c2013-11-14 00:40:56 +000013761 if (stmpconf.tx_type == HWTSTAMP_TX_ON)
13762 tg3_flag_set(tp, TX_TSTAMP_EN);
13763 else
13764 tg3_flag_clear(tp, TX_TSTAMP_EN);
13765
Matt Carlson0a633ac2012-12-03 19:36:59 +000013766 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13767 -EFAULT : 0;
13768}
13769
Ben Hutchings72608992013-11-18 22:59:43 +000013770static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
13771{
13772 struct tg3 *tp = netdev_priv(dev);
13773 struct hwtstamp_config stmpconf;
13774
13775 if (!tg3_flag(tp, PTP_CAPABLE))
13776 return -EOPNOTSUPP;
13777
13778 stmpconf.flags = 0;
13779 stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
13780 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
13781
13782 switch (tp->rxptpctl) {
13783 case 0:
13784 stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
13785 break;
13786 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
13787 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
13788 break;
13789 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13790 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
13791 break;
13792 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13793 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
13794 break;
13795 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13796 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
13797 break;
13798 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13799 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
13800 break;
13801 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13802 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
13803 break;
13804 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13805 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
13806 break;
13807 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13808 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
13809 break;
13810 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13811 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
13812 break;
13813 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13814 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
13815 break;
13816 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13817 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
13818 break;
13819 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13820 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
13821 break;
13822 default:
13823 WARN_ON_ONCE(1);
13824 return -ERANGE;
13825 }
13826
13827 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13828 -EFAULT : 0;
13829}
13830
Linus Torvalds1da177e2005-04-16 15:20:36 -070013831static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13832{
13833 struct mii_ioctl_data *data = if_mii(ifr);
13834 struct tg3 *tp = netdev_priv(dev);
13835 int err;
13836
Joe Perches63c3a662011-04-26 08:12:10 +000013837 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013838 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013839 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013840 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020013841 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Richard Cochran28b04112010-07-17 08:48:55 +000013842 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013843 }
13844
Matt Carlson33f401a2010-04-05 10:19:27 +000013845 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013846 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013847 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013848
13849 /* fallthru */
13850 case SIOCGMIIREG: {
13851 u32 mii_regval;
13852
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013853 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013854 break; /* We have no PHY */
13855
Matt Carlson34eea5a2011-04-20 07:57:38 +000013856 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013857 return -EAGAIN;
13858
David S. Millerf47c11e2005-06-24 20:18:35 -070013859 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013860 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13861 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013862 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013863
13864 data->val_out = mii_regval;
13865
13866 return err;
13867 }
13868
13869 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013870 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013871 break; /* We have no PHY */
13872
Matt Carlson34eea5a2011-04-20 07:57:38 +000013873 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013874 return -EAGAIN;
13875
David S. Millerf47c11e2005-06-24 20:18:35 -070013876 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013877 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13878 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013879 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013880
13881 return err;
13882
Matt Carlson0a633ac2012-12-03 19:36:59 +000013883 case SIOCSHWTSTAMP:
Ben Hutchings72608992013-11-18 22:59:43 +000013884 return tg3_hwtstamp_set(dev, ifr);
13885
13886 case SIOCGHWTSTAMP:
13887 return tg3_hwtstamp_get(dev, ifr);
Matt Carlson0a633ac2012-12-03 19:36:59 +000013888
Linus Torvalds1da177e2005-04-16 15:20:36 -070013889 default:
13890 /* do nothing */
13891 break;
13892 }
13893 return -EOPNOTSUPP;
13894}
13895
David S. Miller15f98502005-05-18 22:49:26 -070013896static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13897{
13898 struct tg3 *tp = netdev_priv(dev);
13899
13900 memcpy(ec, &tp->coal, sizeof(*ec));
13901 return 0;
13902}
13903
Michael Chand244c892005-07-05 14:42:33 -070013904static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13905{
13906 struct tg3 *tp = netdev_priv(dev);
13907 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13908 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13909
Joe Perches63c3a662011-04-26 08:12:10 +000013910 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013911 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13912 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13913 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13914 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13915 }
13916
13917 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13918 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13919 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13920 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13921 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13922 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13923 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13924 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13925 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13926 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13927 return -EINVAL;
13928
13929 /* No rx interrupts will be generated if both are zero */
13930 if ((ec->rx_coalesce_usecs == 0) &&
13931 (ec->rx_max_coalesced_frames == 0))
13932 return -EINVAL;
13933
13934 /* No tx interrupts will be generated if both are zero */
13935 if ((ec->tx_coalesce_usecs == 0) &&
13936 (ec->tx_max_coalesced_frames == 0))
13937 return -EINVAL;
13938
13939 /* Only copy relevant parameters, ignore all others. */
13940 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13941 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13942 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13943 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13944 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13945 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13946 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13947 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13948 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13949
13950 if (netif_running(dev)) {
13951 tg3_full_lock(tp, 0);
13952 __tg3_set_coalesce(tp, &tp->coal);
13953 tg3_full_unlock(tp);
13954 }
13955 return 0;
13956}
13957
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013958static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
13959{
13960 struct tg3 *tp = netdev_priv(dev);
13961
13962 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13963 netdev_warn(tp->dev, "Board does not support EEE!\n");
13964 return -EOPNOTSUPP;
13965 }
13966
13967 if (edata->advertised != tp->eee.advertised) {
13968 netdev_warn(tp->dev,
13969 "Direct manipulation of EEE advertisement is not supported\n");
13970 return -EINVAL;
13971 }
13972
13973 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
13974 netdev_warn(tp->dev,
13975 "Maximal Tx Lpi timer supported is %#x(u)\n",
13976 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
13977 return -EINVAL;
13978 }
13979
13980 tp->eee = *edata;
13981
13982 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
13983 tg3_warn_mgmt_link_flap(tp);
13984
13985 if (netif_running(tp->dev)) {
13986 tg3_full_lock(tp, 0);
13987 tg3_setup_eee(tp);
13988 tg3_phy_reset(tp);
13989 tg3_full_unlock(tp);
13990 }
13991
13992 return 0;
13993}
13994
13995static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
13996{
13997 struct tg3 *tp = netdev_priv(dev);
13998
13999 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14000 netdev_warn(tp->dev,
14001 "Board does not support EEE!\n");
14002 return -EOPNOTSUPP;
14003 }
14004
14005 *edata = tp->eee;
14006 return 0;
14007}
14008
Jeff Garzik7282d492006-09-13 14:30:00 -040014009static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014010 .get_settings = tg3_get_settings,
14011 .set_settings = tg3_set_settings,
14012 .get_drvinfo = tg3_get_drvinfo,
14013 .get_regs_len = tg3_get_regs_len,
14014 .get_regs = tg3_get_regs,
14015 .get_wol = tg3_get_wol,
14016 .set_wol = tg3_set_wol,
14017 .get_msglevel = tg3_get_msglevel,
14018 .set_msglevel = tg3_set_msglevel,
14019 .nway_reset = tg3_nway_reset,
14020 .get_link = ethtool_op_get_link,
14021 .get_eeprom_len = tg3_get_eeprom_len,
14022 .get_eeprom = tg3_get_eeprom,
14023 .set_eeprom = tg3_set_eeprom,
14024 .get_ringparam = tg3_get_ringparam,
14025 .set_ringparam = tg3_set_ringparam,
14026 .get_pauseparam = tg3_get_pauseparam,
14027 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070014028 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014029 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000014030 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014031 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070014032 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070014033 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070014034 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000014035 .get_rxnfc = tg3_get_rxnfc,
14036 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
14037 .get_rxfh_indir = tg3_get_rxfh_indir,
14038 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000014039 .get_channels = tg3_get_channels,
14040 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000014041 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014042 .get_eee = tg3_get_eee,
14043 .set_eee = tg3_set_eee,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014044};
14045
David S. Millerb4017c52012-03-01 17:57:40 -050014046static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
14047 struct rtnl_link_stats64 *stats)
14048{
14049 struct tg3 *tp = netdev_priv(dev);
14050
David S. Millerb4017c52012-03-01 17:57:40 -050014051 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000014052 if (!tp->hw_stats) {
14053 spin_unlock_bh(&tp->lock);
14054 return &tp->net_stats_prev;
14055 }
14056
David S. Millerb4017c52012-03-01 17:57:40 -050014057 tg3_get_nstats(tp, stats);
14058 spin_unlock_bh(&tp->lock);
14059
14060 return stats;
14061}
14062
Matt Carlsonccd5ba92012-02-13 10:20:08 +000014063static void tg3_set_rx_mode(struct net_device *dev)
14064{
14065 struct tg3 *tp = netdev_priv(dev);
14066
14067 if (!netif_running(dev))
14068 return;
14069
14070 tg3_full_lock(tp, 0);
14071 __tg3_set_rx_mode(dev);
14072 tg3_full_unlock(tp);
14073}
14074
Matt Carlsonfaf16272012-02-13 10:20:07 +000014075static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
14076 int new_mtu)
14077{
14078 dev->mtu = new_mtu;
14079
14080 if (new_mtu > ETH_DATA_LEN) {
14081 if (tg3_flag(tp, 5780_CLASS)) {
14082 netdev_update_features(dev);
14083 tg3_flag_clear(tp, TSO_CAPABLE);
14084 } else {
14085 tg3_flag_set(tp, JUMBO_RING_ENABLE);
14086 }
14087 } else {
14088 if (tg3_flag(tp, 5780_CLASS)) {
14089 tg3_flag_set(tp, TSO_CAPABLE);
14090 netdev_update_features(dev);
14091 }
14092 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
14093 }
14094}
14095
14096static int tg3_change_mtu(struct net_device *dev, int new_mtu)
14097{
14098 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000014099 int err;
14100 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000014101
14102 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
14103 return -EINVAL;
14104
14105 if (!netif_running(dev)) {
14106 /* We'll just catch it later when the
14107 * device is up'd.
14108 */
14109 tg3_set_mtu(dev, tp, new_mtu);
14110 return 0;
14111 }
14112
14113 tg3_phy_stop(tp);
14114
14115 tg3_netif_stop(tp);
14116
14117 tg3_full_lock(tp, 1);
14118
14119 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14120
14121 tg3_set_mtu(dev, tp, new_mtu);
14122
Michael Chan2fae5e32012-03-04 14:48:15 +000014123 /* Reset PHY, otherwise the read DMA engine will be in a mode that
14124 * breaks all requests to 256 bytes.
14125 */
Joe Perches41535772013-02-16 11:20:04 +000014126 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000014127 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000014128
14129 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000014130
14131 if (!err)
14132 tg3_netif_start(tp);
14133
14134 tg3_full_unlock(tp);
14135
14136 if (!err)
14137 tg3_phy_start(tp);
14138
14139 return err;
14140}
14141
14142static const struct net_device_ops tg3_netdev_ops = {
14143 .ndo_open = tg3_open,
14144 .ndo_stop = tg3_close,
14145 .ndo_start_xmit = tg3_start_xmit,
14146 .ndo_get_stats64 = tg3_get_stats64,
14147 .ndo_validate_addr = eth_validate_addr,
14148 .ndo_set_rx_mode = tg3_set_rx_mode,
14149 .ndo_set_mac_address = tg3_set_mac_addr,
14150 .ndo_do_ioctl = tg3_ioctl,
14151 .ndo_tx_timeout = tg3_tx_timeout,
14152 .ndo_change_mtu = tg3_change_mtu,
14153 .ndo_fix_features = tg3_fix_features,
14154 .ndo_set_features = tg3_set_features,
14155#ifdef CONFIG_NET_POLL_CONTROLLER
14156 .ndo_poll_controller = tg3_poll_controller,
14157#endif
14158};
14159
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014160static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014161{
Michael Chan1b277772006-03-20 22:27:48 -080014162 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014163
14164 tp->nvram_size = EEPROM_CHIP_SIZE;
14165
Matt Carlsone4f34112009-02-25 14:25:00 +000014166 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014167 return;
14168
Michael Chanb16250e2006-09-27 16:10:14 -070014169 if ((magic != TG3_EEPROM_MAGIC) &&
14170 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
14171 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014172 return;
14173
14174 /*
14175 * Size the chip by reading offsets at increasing powers of two.
14176 * When we encounter our validation signature, we know the addressing
14177 * has wrapped around, and thus have our chip size.
14178 */
Michael Chan1b277772006-03-20 22:27:48 -080014179 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014180
14181 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014182 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014183 return;
14184
Michael Chan18201802006-03-20 22:29:15 -080014185 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014186 break;
14187
14188 cursize <<= 1;
14189 }
14190
14191 tp->nvram_size = cursize;
14192}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014193
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014194static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014195{
14196 u32 val;
14197
Joe Perches63c3a662011-04-26 08:12:10 +000014198 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080014199 return;
14200
14201 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080014202 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080014203 tg3_get_eeprom_size(tp);
14204 return;
14205 }
14206
Matt Carlson6d348f22009-02-25 14:25:52 +000014207 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014208 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000014209 /* This is confusing. We want to operate on the
14210 * 16-bit value at offset 0xf2. The tg3_nvram_read()
14211 * call will read from NVRAM and byteswap the data
14212 * according to the byteswapping settings for all
14213 * other register accesses. This ensures the data we
14214 * want will always reside in the lower 16-bits.
14215 * However, the data in NVRAM is in LE format, which
14216 * means the data from the NVRAM read will always be
14217 * opposite the endianness of the CPU. The 16-bit
14218 * byteswap then brings the data to CPU endianness.
14219 */
14220 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014221 return;
14222 }
14223 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070014224 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014225}
14226
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014227static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014228{
14229 u32 nvcfg1;
14230
14231 nvcfg1 = tr32(NVRAM_CFG1);
14232 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000014233 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014234 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014235 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14236 tw32(NVRAM_CFG1, nvcfg1);
14237 }
14238
Joe Perches41535772013-02-16 11:20:04 +000014239 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014240 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014241 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014242 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
14243 tp->nvram_jedecnum = JEDEC_ATMEL;
14244 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014245 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014246 break;
14247 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
14248 tp->nvram_jedecnum = JEDEC_ATMEL;
14249 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
14250 break;
14251 case FLASH_VENDOR_ATMEL_EEPROM:
14252 tp->nvram_jedecnum = JEDEC_ATMEL;
14253 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014254 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014255 break;
14256 case FLASH_VENDOR_ST:
14257 tp->nvram_jedecnum = JEDEC_ST;
14258 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014259 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014260 break;
14261 case FLASH_VENDOR_SAIFUN:
14262 tp->nvram_jedecnum = JEDEC_SAIFUN;
14263 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
14264 break;
14265 case FLASH_VENDOR_SST_SMALL:
14266 case FLASH_VENDOR_SST_LARGE:
14267 tp->nvram_jedecnum = JEDEC_SST;
14268 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
14269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270 }
Matt Carlson8590a602009-08-28 12:29:16 +000014271 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014272 tp->nvram_jedecnum = JEDEC_ATMEL;
14273 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014274 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014275 }
14276}
14277
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014278static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014279{
14280 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
14281 case FLASH_5752PAGE_SIZE_256:
14282 tp->nvram_pagesize = 256;
14283 break;
14284 case FLASH_5752PAGE_SIZE_512:
14285 tp->nvram_pagesize = 512;
14286 break;
14287 case FLASH_5752PAGE_SIZE_1K:
14288 tp->nvram_pagesize = 1024;
14289 break;
14290 case FLASH_5752PAGE_SIZE_2K:
14291 tp->nvram_pagesize = 2048;
14292 break;
14293 case FLASH_5752PAGE_SIZE_4K:
14294 tp->nvram_pagesize = 4096;
14295 break;
14296 case FLASH_5752PAGE_SIZE_264:
14297 tp->nvram_pagesize = 264;
14298 break;
14299 case FLASH_5752PAGE_SIZE_528:
14300 tp->nvram_pagesize = 528;
14301 break;
14302 }
14303}
14304
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014305static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014306{
14307 u32 nvcfg1;
14308
14309 nvcfg1 = tr32(NVRAM_CFG1);
14310
Michael Chane6af3012005-04-21 17:12:05 -070014311 /* NVRAM protection for TPM */
14312 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014313 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014314
Michael Chan361b4ac2005-04-21 17:11:21 -070014315 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014316 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14317 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14318 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014319 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014320 break;
14321 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14322 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014323 tg3_flag_set(tp, NVRAM_BUFFERED);
14324 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014325 break;
14326 case FLASH_5752VENDOR_ST_M45PE10:
14327 case FLASH_5752VENDOR_ST_M45PE20:
14328 case FLASH_5752VENDOR_ST_M45PE40:
14329 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014330 tg3_flag_set(tp, NVRAM_BUFFERED);
14331 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014332 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014333 }
14334
Joe Perches63c3a662011-04-26 08:12:10 +000014335 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014336 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014337 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014338 /* For eeprom, set pagesize to maximum eeprom size */
14339 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14340
14341 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14342 tw32(NVRAM_CFG1, nvcfg1);
14343 }
14344}
14345
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014346static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014347{
Matt Carlson989a9d22007-05-05 11:51:05 -070014348 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014349
14350 nvcfg1 = tr32(NVRAM_CFG1);
14351
14352 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014353 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014354 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014355 protect = 1;
14356 }
Michael Chand3c7b882006-03-23 01:28:25 -080014357
Matt Carlson989a9d22007-05-05 11:51:05 -070014358 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14359 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014360 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14361 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14362 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14363 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14364 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014365 tg3_flag_set(tp, NVRAM_BUFFERED);
14366 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014367 tp->nvram_pagesize = 264;
14368 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14369 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14370 tp->nvram_size = (protect ? 0x3e200 :
14371 TG3_NVRAM_SIZE_512KB);
14372 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14373 tp->nvram_size = (protect ? 0x1f200 :
14374 TG3_NVRAM_SIZE_256KB);
14375 else
14376 tp->nvram_size = (protect ? 0x1f200 :
14377 TG3_NVRAM_SIZE_128KB);
14378 break;
14379 case FLASH_5752VENDOR_ST_M45PE10:
14380 case FLASH_5752VENDOR_ST_M45PE20:
14381 case FLASH_5752VENDOR_ST_M45PE40:
14382 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014383 tg3_flag_set(tp, NVRAM_BUFFERED);
14384 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014385 tp->nvram_pagesize = 256;
14386 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14387 tp->nvram_size = (protect ?
14388 TG3_NVRAM_SIZE_64KB :
14389 TG3_NVRAM_SIZE_128KB);
14390 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14391 tp->nvram_size = (protect ?
14392 TG3_NVRAM_SIZE_64KB :
14393 TG3_NVRAM_SIZE_256KB);
14394 else
14395 tp->nvram_size = (protect ?
14396 TG3_NVRAM_SIZE_128KB :
14397 TG3_NVRAM_SIZE_512KB);
14398 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014399 }
14400}
14401
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014402static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014403{
14404 u32 nvcfg1;
14405
14406 nvcfg1 = tr32(NVRAM_CFG1);
14407
14408 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014409 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14410 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14411 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14412 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14413 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014414 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014415 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014416
Matt Carlson8590a602009-08-28 12:29:16 +000014417 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14418 tw32(NVRAM_CFG1, nvcfg1);
14419 break;
14420 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14421 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14422 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14423 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14424 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014425 tg3_flag_set(tp, NVRAM_BUFFERED);
14426 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014427 tp->nvram_pagesize = 264;
14428 break;
14429 case FLASH_5752VENDOR_ST_M45PE10:
14430 case FLASH_5752VENDOR_ST_M45PE20:
14431 case FLASH_5752VENDOR_ST_M45PE40:
14432 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014433 tg3_flag_set(tp, NVRAM_BUFFERED);
14434 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014435 tp->nvram_pagesize = 256;
14436 break;
Michael Chan1b277772006-03-20 22:27:48 -080014437 }
14438}
14439
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014440static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014441{
14442 u32 nvcfg1, protect = 0;
14443
14444 nvcfg1 = tr32(NVRAM_CFG1);
14445
14446 /* NVRAM protection for TPM */
14447 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014448 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014449 protect = 1;
14450 }
14451
14452 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14453 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014454 case FLASH_5761VENDOR_ATMEL_ADB021D:
14455 case FLASH_5761VENDOR_ATMEL_ADB041D:
14456 case FLASH_5761VENDOR_ATMEL_ADB081D:
14457 case FLASH_5761VENDOR_ATMEL_ADB161D:
14458 case FLASH_5761VENDOR_ATMEL_MDB021D:
14459 case FLASH_5761VENDOR_ATMEL_MDB041D:
14460 case FLASH_5761VENDOR_ATMEL_MDB081D:
14461 case FLASH_5761VENDOR_ATMEL_MDB161D:
14462 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014463 tg3_flag_set(tp, NVRAM_BUFFERED);
14464 tg3_flag_set(tp, FLASH);
14465 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014466 tp->nvram_pagesize = 256;
14467 break;
14468 case FLASH_5761VENDOR_ST_A_M45PE20:
14469 case FLASH_5761VENDOR_ST_A_M45PE40:
14470 case FLASH_5761VENDOR_ST_A_M45PE80:
14471 case FLASH_5761VENDOR_ST_A_M45PE16:
14472 case FLASH_5761VENDOR_ST_M_M45PE20:
14473 case FLASH_5761VENDOR_ST_M_M45PE40:
14474 case FLASH_5761VENDOR_ST_M_M45PE80:
14475 case FLASH_5761VENDOR_ST_M_M45PE16:
14476 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014477 tg3_flag_set(tp, NVRAM_BUFFERED);
14478 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014479 tp->nvram_pagesize = 256;
14480 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014481 }
14482
14483 if (protect) {
14484 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14485 } else {
14486 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014487 case FLASH_5761VENDOR_ATMEL_ADB161D:
14488 case FLASH_5761VENDOR_ATMEL_MDB161D:
14489 case FLASH_5761VENDOR_ST_A_M45PE16:
14490 case FLASH_5761VENDOR_ST_M_M45PE16:
14491 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14492 break;
14493 case FLASH_5761VENDOR_ATMEL_ADB081D:
14494 case FLASH_5761VENDOR_ATMEL_MDB081D:
14495 case FLASH_5761VENDOR_ST_A_M45PE80:
14496 case FLASH_5761VENDOR_ST_M_M45PE80:
14497 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14498 break;
14499 case FLASH_5761VENDOR_ATMEL_ADB041D:
14500 case FLASH_5761VENDOR_ATMEL_MDB041D:
14501 case FLASH_5761VENDOR_ST_A_M45PE40:
14502 case FLASH_5761VENDOR_ST_M_M45PE40:
14503 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14504 break;
14505 case FLASH_5761VENDOR_ATMEL_ADB021D:
14506 case FLASH_5761VENDOR_ATMEL_MDB021D:
14507 case FLASH_5761VENDOR_ST_A_M45PE20:
14508 case FLASH_5761VENDOR_ST_M_M45PE20:
14509 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14510 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014511 }
14512 }
14513}
14514
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014515static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014516{
14517 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014518 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014519 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14520}
14521
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014522static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014523{
14524 u32 nvcfg1;
14525
14526 nvcfg1 = tr32(NVRAM_CFG1);
14527
14528 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14529 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14530 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14531 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014532 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014533 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14534
14535 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14536 tw32(NVRAM_CFG1, nvcfg1);
14537 return;
14538 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14539 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14540 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14541 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14542 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14543 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14544 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14545 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014546 tg3_flag_set(tp, NVRAM_BUFFERED);
14547 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014548
14549 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14550 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14551 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14552 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14553 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14554 break;
14555 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14556 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14557 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14558 break;
14559 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14560 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14561 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14562 break;
14563 }
14564 break;
14565 case FLASH_5752VENDOR_ST_M45PE10:
14566 case FLASH_5752VENDOR_ST_M45PE20:
14567 case FLASH_5752VENDOR_ST_M45PE40:
14568 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014569 tg3_flag_set(tp, NVRAM_BUFFERED);
14570 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014571
14572 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14573 case FLASH_5752VENDOR_ST_M45PE10:
14574 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14575 break;
14576 case FLASH_5752VENDOR_ST_M45PE20:
14577 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14578 break;
14579 case FLASH_5752VENDOR_ST_M45PE40:
14580 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14581 break;
14582 }
14583 break;
14584 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014585 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014586 return;
14587 }
14588
Matt Carlsona1b950d2009-09-01 13:20:17 +000014589 tg3_nvram_get_pagesize(tp, nvcfg1);
14590 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014591 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014592}
14593
14594
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014595static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014596{
14597 u32 nvcfg1;
14598
14599 nvcfg1 = tr32(NVRAM_CFG1);
14600
14601 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14602 case FLASH_5717VENDOR_ATMEL_EEPROM:
14603 case FLASH_5717VENDOR_MICRO_EEPROM:
14604 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014605 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014606 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14607
14608 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14609 tw32(NVRAM_CFG1, nvcfg1);
14610 return;
14611 case FLASH_5717VENDOR_ATMEL_MDB011D:
14612 case FLASH_5717VENDOR_ATMEL_ADB011B:
14613 case FLASH_5717VENDOR_ATMEL_ADB011D:
14614 case FLASH_5717VENDOR_ATMEL_MDB021D:
14615 case FLASH_5717VENDOR_ATMEL_ADB021B:
14616 case FLASH_5717VENDOR_ATMEL_ADB021D:
14617 case FLASH_5717VENDOR_ATMEL_45USPT:
14618 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014619 tg3_flag_set(tp, NVRAM_BUFFERED);
14620 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014621
14622 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14623 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014624 /* Detect size with tg3_nvram_get_size() */
14625 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014626 case FLASH_5717VENDOR_ATMEL_ADB021B:
14627 case FLASH_5717VENDOR_ATMEL_ADB021D:
14628 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14629 break;
14630 default:
14631 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14632 break;
14633 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014634 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014635 case FLASH_5717VENDOR_ST_M_M25PE10:
14636 case FLASH_5717VENDOR_ST_A_M25PE10:
14637 case FLASH_5717VENDOR_ST_M_M45PE10:
14638 case FLASH_5717VENDOR_ST_A_M45PE10:
14639 case FLASH_5717VENDOR_ST_M_M25PE20:
14640 case FLASH_5717VENDOR_ST_A_M25PE20:
14641 case FLASH_5717VENDOR_ST_M_M45PE20:
14642 case FLASH_5717VENDOR_ST_A_M45PE20:
14643 case FLASH_5717VENDOR_ST_25USPT:
14644 case FLASH_5717VENDOR_ST_45USPT:
14645 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014646 tg3_flag_set(tp, NVRAM_BUFFERED);
14647 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014648
14649 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14650 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014651 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014652 /* Detect size with tg3_nvram_get_size() */
14653 break;
14654 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014655 case FLASH_5717VENDOR_ST_A_M45PE20:
14656 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14657 break;
14658 default:
14659 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14660 break;
14661 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014662 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014663 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014664 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014665 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014666 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014667
14668 tg3_nvram_get_pagesize(tp, nvcfg1);
14669 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014670 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014671}
14672
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014673static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014674{
14675 u32 nvcfg1, nvmpinstrp;
14676
14677 nvcfg1 = tr32(NVRAM_CFG1);
14678 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14679
Joe Perches41535772013-02-16 11:20:04 +000014680 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014681 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14682 tg3_flag_set(tp, NO_NVRAM);
14683 return;
14684 }
14685
14686 switch (nvmpinstrp) {
14687 case FLASH_5762_EEPROM_HD:
14688 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014689 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014690 case FLASH_5762_EEPROM_LD:
14691 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014692 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014693 case FLASH_5720VENDOR_M_ST_M45PE20:
14694 /* This pinstrap supports multiple sizes, so force it
14695 * to read the actual size from location 0xf0.
14696 */
14697 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14698 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014699 }
14700 }
14701
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014702 switch (nvmpinstrp) {
14703 case FLASH_5720_EEPROM_HD:
14704 case FLASH_5720_EEPROM_LD:
14705 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014706 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014707
14708 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14709 tw32(NVRAM_CFG1, nvcfg1);
14710 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14711 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14712 else
14713 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14714 return;
14715 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14716 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14717 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14718 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14719 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14720 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14721 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14722 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14723 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14724 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14725 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14726 case FLASH_5720VENDOR_ATMEL_45USPT:
14727 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014728 tg3_flag_set(tp, NVRAM_BUFFERED);
14729 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014730
14731 switch (nvmpinstrp) {
14732 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14733 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14734 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14735 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14736 break;
14737 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14738 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14739 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14740 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14741 break;
14742 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14743 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14744 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14745 break;
14746 default:
Joe Perches41535772013-02-16 11:20:04 +000014747 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014748 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014749 break;
14750 }
14751 break;
14752 case FLASH_5720VENDOR_M_ST_M25PE10:
14753 case FLASH_5720VENDOR_M_ST_M45PE10:
14754 case FLASH_5720VENDOR_A_ST_M25PE10:
14755 case FLASH_5720VENDOR_A_ST_M45PE10:
14756 case FLASH_5720VENDOR_M_ST_M25PE20:
14757 case FLASH_5720VENDOR_M_ST_M45PE20:
14758 case FLASH_5720VENDOR_A_ST_M25PE20:
14759 case FLASH_5720VENDOR_A_ST_M45PE20:
14760 case FLASH_5720VENDOR_M_ST_M25PE40:
14761 case FLASH_5720VENDOR_M_ST_M45PE40:
14762 case FLASH_5720VENDOR_A_ST_M25PE40:
14763 case FLASH_5720VENDOR_A_ST_M45PE40:
14764 case FLASH_5720VENDOR_M_ST_M25PE80:
14765 case FLASH_5720VENDOR_M_ST_M45PE80:
14766 case FLASH_5720VENDOR_A_ST_M25PE80:
14767 case FLASH_5720VENDOR_A_ST_M45PE80:
14768 case FLASH_5720VENDOR_ST_25USPT:
14769 case FLASH_5720VENDOR_ST_45USPT:
14770 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014771 tg3_flag_set(tp, NVRAM_BUFFERED);
14772 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014773
14774 switch (nvmpinstrp) {
14775 case FLASH_5720VENDOR_M_ST_M25PE20:
14776 case FLASH_5720VENDOR_M_ST_M45PE20:
14777 case FLASH_5720VENDOR_A_ST_M25PE20:
14778 case FLASH_5720VENDOR_A_ST_M45PE20:
14779 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14780 break;
14781 case FLASH_5720VENDOR_M_ST_M25PE40:
14782 case FLASH_5720VENDOR_M_ST_M45PE40:
14783 case FLASH_5720VENDOR_A_ST_M25PE40:
14784 case FLASH_5720VENDOR_A_ST_M45PE40:
14785 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14786 break;
14787 case FLASH_5720VENDOR_M_ST_M25PE80:
14788 case FLASH_5720VENDOR_M_ST_M45PE80:
14789 case FLASH_5720VENDOR_A_ST_M25PE80:
14790 case FLASH_5720VENDOR_A_ST_M45PE80:
14791 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14792 break;
14793 default:
Joe Perches41535772013-02-16 11:20:04 +000014794 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014795 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014796 break;
14797 }
14798 break;
14799 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014800 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014801 return;
14802 }
14803
14804 tg3_nvram_get_pagesize(tp, nvcfg1);
14805 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014806 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014807
Joe Perches41535772013-02-16 11:20:04 +000014808 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014809 u32 val;
14810
14811 if (tg3_nvram_read(tp, 0, &val))
14812 return;
14813
14814 if (val != TG3_EEPROM_MAGIC &&
14815 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14816 tg3_flag_set(tp, NO_NVRAM);
14817 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014818}
14819
Linus Torvalds1da177e2005-04-16 15:20:36 -070014820/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014821static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014822{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014823 if (tg3_flag(tp, IS_SSB_CORE)) {
14824 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14825 tg3_flag_clear(tp, NVRAM);
14826 tg3_flag_clear(tp, NVRAM_BUFFERED);
14827 tg3_flag_set(tp, NO_NVRAM);
14828 return;
14829 }
14830
Linus Torvalds1da177e2005-04-16 15:20:36 -070014831 tw32_f(GRC_EEPROM_ADDR,
14832 (EEPROM_ADDR_FSM_RESET |
14833 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14834 EEPROM_ADDR_CLKPERD_SHIFT)));
14835
Michael Chan9d57f012006-12-07 00:23:25 -080014836 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014837
14838 /* Enable seeprom accesses. */
14839 tw32_f(GRC_LOCAL_CTRL,
14840 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14841 udelay(100);
14842
Joe Perches41535772013-02-16 11:20:04 +000014843 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14844 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014845 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014846
Michael Chanec41c7d2006-01-17 02:40:55 -080014847 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014848 netdev_warn(tp->dev,
14849 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014850 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014851 return;
14852 }
Michael Chane6af3012005-04-21 17:12:05 -070014853 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014854
Matt Carlson989a9d22007-05-05 11:51:05 -070014855 tp->nvram_size = 0;
14856
Joe Perches41535772013-02-16 11:20:04 +000014857 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014858 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014859 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014860 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014861 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14862 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14863 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014864 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014865 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014866 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014867 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014868 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014869 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014870 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014871 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014872 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14873 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014874 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014875 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14876 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014877 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014878 else
14879 tg3_get_nvram_info(tp);
14880
Matt Carlson989a9d22007-05-05 11:51:05 -070014881 if (tp->nvram_size == 0)
14882 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014883
Michael Chane6af3012005-04-21 17:12:05 -070014884 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014885 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014886
14887 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014888 tg3_flag_clear(tp, NVRAM);
14889 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014890
14891 tg3_get_eeprom_size(tp);
14892 }
14893}
14894
Linus Torvalds1da177e2005-04-16 15:20:36 -070014895struct subsys_tbl_ent {
14896 u16 subsys_vendor, subsys_devid;
14897 u32 phy_id;
14898};
14899
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014900static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014901 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014902 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014903 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014904 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014905 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014906 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014907 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014908 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14909 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14910 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014911 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014912 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014913 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014914 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14915 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14916 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014917 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014918 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014919 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014920 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014921 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014922 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014923 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014924
14925 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014926 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014927 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014928 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014929 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014930 { TG3PCI_SUBVENDOR_ID_3COM,
14931 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14932 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014933 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014934 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014935 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014936
14937 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014938 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014939 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014940 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014941 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014942 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014943 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014944 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014945 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014946
14947 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014948 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014949 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014950 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014951 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014952 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14953 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14954 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014955 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014956 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014957 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014958
14959 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014960 { TG3PCI_SUBVENDOR_ID_IBM,
14961 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014962};
14963
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014964static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014965{
14966 int i;
14967
14968 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14969 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14970 tp->pdev->subsystem_vendor) &&
14971 (subsys_id_to_phy_id[i].subsys_devid ==
14972 tp->pdev->subsystem_device))
14973 return &subsys_id_to_phy_id[i];
14974 }
14975 return NULL;
14976}
14977
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014978static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014979{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014980 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014981
Matt Carlson79eb6902010-02-17 15:17:03 +000014982 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014983 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14984
Gary Zambranoa85feb82007-05-05 11:52:19 -070014985 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014986 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14987 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014988
Joe Perches41535772013-02-16 11:20:04 +000014989 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014990 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014991 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14992 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014993 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014994 val = tr32(VCPU_CFGSHDW);
14995 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014996 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014997 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014998 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014999 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015000 device_set_wakeup_enable(&tp->pdev->dev, true);
15001 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015002 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070015003 }
15004
Linus Torvalds1da177e2005-04-16 15:20:36 -070015005 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
15006 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
15007 u32 nic_cfg, led_cfg;
Nithin Sujir7c786062013-12-06 09:53:17 -080015008 u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
15009 u32 nic_phy_id, ver, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015010 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015011
15012 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
15013 tp->nic_sram_data_cfg = nic_cfg;
15014
15015 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
15016 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015017 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15018 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15019 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015020 (ver > 0) && (ver < 0x100))
15021 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
15022
Joe Perches41535772013-02-16 11:20:04 +000015023 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070015024 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
15025
Nithin Sujir7c786062013-12-06 09:53:17 -080015026 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15027 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15028 tg3_asic_rev(tp) == ASIC_REV_5720)
15029 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
15030
Linus Torvalds1da177e2005-04-16 15:20:36 -070015031 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
15032 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
15033 eeprom_phy_serdes = 1;
15034
15035 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
15036 if (nic_phy_id != 0) {
15037 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
15038 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
15039
15040 eeprom_phy_id = (id1 >> 16) << 10;
15041 eeprom_phy_id |= (id2 & 0xfc00) << 16;
15042 eeprom_phy_id |= (id2 & 0x03ff) << 0;
15043 } else
15044 eeprom_phy_id = 0;
15045
Michael Chan7d0c41e2005-04-21 17:06:20 -070015046 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070015047 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000015048 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015049 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000015050 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015051 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070015052 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070015053
Joe Perches63c3a662011-04-26 08:12:10 +000015054 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070015055 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
15056 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070015057 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015058 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
15059
15060 switch (led_cfg) {
15061 default:
15062 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
15063 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15064 break;
15065
15066 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
15067 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15068 break;
15069
15070 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
15071 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070015072
15073 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
15074 * read on some older 5700/5701 bootcode.
15075 */
Joe Perches41535772013-02-16 11:20:04 +000015076 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15077 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070015078 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15079
Linus Torvalds1da177e2005-04-16 15:20:36 -070015080 break;
15081
15082 case SHASTA_EXT_LED_SHARED:
15083 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000015084 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
15085 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015086 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15087 LED_CTRL_MODE_PHY_2);
Nithin Sujir89f67972013-09-20 16:46:57 -070015088
15089 if (tg3_flag(tp, 5717_PLUS) ||
15090 tg3_asic_rev(tp) == ASIC_REV_5762)
15091 tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
15092 LED_CTRL_BLINK_RATE_MASK;
15093
Linus Torvalds1da177e2005-04-16 15:20:36 -070015094 break;
15095
15096 case SHASTA_EXT_LED_MAC:
15097 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
15098 break;
15099
15100 case SHASTA_EXT_LED_COMBO:
15101 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000015102 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015103 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15104 LED_CTRL_MODE_PHY_2);
15105 break;
15106
Stephen Hemminger855e1112008-04-16 16:37:28 -070015107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015108
Joe Perches41535772013-02-16 11:20:04 +000015109 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
15110 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015111 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
15112 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15113
Joe Perches41535772013-02-16 11:20:04 +000015114 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015115 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080015116
Michael Chan9d26e212006-12-07 00:21:14 -080015117 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000015118 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015119 if ((tp->pdev->subsystem_vendor ==
15120 PCI_VENDOR_ID_ARIMA) &&
15121 (tp->pdev->subsystem_device == 0x205a ||
15122 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000015123 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015124 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015125 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15126 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015128
15129 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000015130 tg3_flag_set(tp, ENABLE_ASF);
15131 if (tg3_flag(tp, 5750_PLUS))
15132 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015133 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015134
15135 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015136 tg3_flag(tp, 5750_PLUS))
15137 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015138
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015139 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070015140 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000015141 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015142
Joe Perches63c3a662011-04-26 08:12:10 +000015143 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015144 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015145 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015146 device_set_wakeup_enable(&tp->pdev->dev, true);
15147 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015148
Linus Torvalds1da177e2005-04-16 15:20:36 -070015149 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015150 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015151
15152 /* serdes signal pre-emphasis in register 0x590 set by */
15153 /* bootcode if bit 18 is set */
15154 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015155 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015156
Joe Perches63c3a662011-04-26 08:12:10 +000015157 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015158 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15159 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080015160 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015161 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080015162
Nithin Sujir942d1af2013-04-09 08:48:07 +000015163 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070015164 u32 cfg3;
15165
15166 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015167 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
15168 !tg3_flag(tp, 57765_PLUS) &&
15169 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000015170 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015171 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
15172 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
15173 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
15174 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015175 }
Matt Carlsona9daf362008-05-25 23:49:44 -070015176
Matt Carlson14417062010-02-17 15:16:59 +000015177 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000015178 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070015179 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015180 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070015181 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015182 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Nithin Sujir7c786062013-12-06 09:53:17 -080015183
15184 if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
15185 tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015186 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015187done:
Joe Perches63c3a662011-04-26 08:12:10 +000015188 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015189 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000015190 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015191 else
15192 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070015193}
15194
Michael Chanc86a8562013-01-06 12:51:08 +000015195static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
15196{
15197 int i, err;
15198 u32 val2, off = offset * 8;
15199
15200 err = tg3_nvram_lock(tp);
15201 if (err)
15202 return err;
15203
15204 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
15205 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
15206 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
15207 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
15208 udelay(10);
15209
15210 for (i = 0; i < 100; i++) {
15211 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
15212 if (val2 & APE_OTP_STATUS_CMD_DONE) {
15213 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
15214 break;
15215 }
15216 udelay(10);
15217 }
15218
15219 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
15220
15221 tg3_nvram_unlock(tp);
15222 if (val2 & APE_OTP_STATUS_CMD_DONE)
15223 return 0;
15224
15225 return -EBUSY;
15226}
15227
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015228static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015229{
15230 int i;
15231 u32 val;
15232
15233 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
15234 tw32(OTP_CTRL, cmd);
15235
15236 /* Wait for up to 1 ms for command to execute. */
15237 for (i = 0; i < 100; i++) {
15238 val = tr32(OTP_STATUS);
15239 if (val & OTP_STATUS_CMD_DONE)
15240 break;
15241 udelay(10);
15242 }
15243
15244 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
15245}
15246
15247/* Read the gphy configuration from the OTP region of the chip. The gphy
15248 * configuration is a 32-bit value that straddles the alignment boundary.
15249 * We do two 32-bit reads and then shift and merge the results.
15250 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015251static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015252{
15253 u32 bhalf_otp, thalf_otp;
15254
15255 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
15256
15257 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
15258 return 0;
15259
15260 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
15261
15262 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15263 return 0;
15264
15265 thalf_otp = tr32(OTP_READ_DATA);
15266
15267 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
15268
15269 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15270 return 0;
15271
15272 bhalf_otp = tr32(OTP_READ_DATA);
15273
15274 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
15275}
15276
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015277static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000015278{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000015279 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015280
Nithin Sujir7c786062013-12-06 09:53:17 -080015281 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
15282 if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
15283 adv |= ADVERTISED_1000baseT_Half;
15284 adv |= ADVERTISED_1000baseT_Full;
15285 }
Matt Carlsone256f8a2011-03-09 16:58:24 +000015286
15287 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15288 adv |= ADVERTISED_100baseT_Half |
15289 ADVERTISED_100baseT_Full |
15290 ADVERTISED_10baseT_Half |
15291 ADVERTISED_10baseT_Full |
15292 ADVERTISED_TP;
15293 else
15294 adv |= ADVERTISED_FIBRE;
15295
15296 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000015297 tp->link_config.speed = SPEED_UNKNOWN;
15298 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015299 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000015300 tp->link_config.active_speed = SPEED_UNKNOWN;
15301 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000015302
15303 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015304}
15305
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015306static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015307{
15308 u32 hw_phy_id_1, hw_phy_id_2;
15309 u32 hw_phy_id, hw_phy_id_masked;
15310 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015311
Matt Carlsone256f8a2011-03-09 16:58:24 +000015312 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015313 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015314 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15315
Michael Chan8151ad52012-07-29 19:15:41 +000015316 if (tg3_flag(tp, ENABLE_APE)) {
15317 switch (tp->pci_fn) {
15318 case 0:
15319 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15320 break;
15321 case 1:
15322 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15323 break;
15324 case 2:
15325 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15326 break;
15327 case 3:
15328 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15329 break;
15330 }
15331 }
15332
Nithin Sujir942d1af2013-04-09 08:48:07 +000015333 if (!tg3_flag(tp, ENABLE_ASF) &&
15334 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15335 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15336 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15337 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15338
Joe Perches63c3a662011-04-26 08:12:10 +000015339 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015340 return tg3_phy_init(tp);
15341
Linus Torvalds1da177e2005-04-16 15:20:36 -070015342 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015343 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015344 */
15345 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015346 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015347 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015348 } else {
15349 /* Now read the physical PHY_ID from the chip and verify
15350 * that it is sane. If it doesn't look good, we fall back
15351 * to either the hard-coded table based PHY_ID and failing
15352 * that the value found in the eeprom area.
15353 */
15354 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15355 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15356
15357 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15358 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15359 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15360
Matt Carlson79eb6902010-02-17 15:17:03 +000015361 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015362 }
15363
Matt Carlson79eb6902010-02-17 15:17:03 +000015364 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015365 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015366 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015367 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015368 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015369 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015370 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015371 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015372 /* Do nothing, phy ID already set up in
15373 * tg3_get_eeprom_hw_cfg().
15374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015375 } else {
15376 struct subsys_tbl_ent *p;
15377
15378 /* No eeprom signature? Try the hardcoded
15379 * subsys device table.
15380 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015381 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015382 if (p) {
15383 tp->phy_id = p->phy_id;
15384 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15385 /* For now we saw the IDs 0xbc050cd0,
15386 * 0xbc050f80 and 0xbc050c30 on devices
15387 * connected to an BCM4785 and there are
15388 * probably more. Just assume that the phy is
15389 * supported when it is connected to a SSB core
15390 * for now.
15391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015392 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015394
Linus Torvalds1da177e2005-04-16 15:20:36 -070015395 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015396 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015397 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015398 }
15399 }
15400
Matt Carlsona6b68da2010-12-06 08:28:52 +000015401 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015402 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15403 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015404 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015405 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15406 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15407 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15408 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015409 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015410 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15411
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015412 tp->eee.supported = SUPPORTED_100baseT_Full |
15413 SUPPORTED_1000baseT_Full;
15414 tp->eee.advertised = ADVERTISED_100baseT_Full |
15415 ADVERTISED_1000baseT_Full;
15416 tp->eee.eee_enabled = 1;
15417 tp->eee.tx_lpi_enabled = 1;
15418 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15419 }
15420
Matt Carlsone256f8a2011-03-09 16:58:24 +000015421 tg3_phy_init_link_config(tp);
15422
Nithin Sujir942d1af2013-04-09 08:48:07 +000015423 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15424 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015425 !tg3_flag(tp, ENABLE_APE) &&
15426 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015427 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015428
15429 tg3_readphy(tp, MII_BMSR, &bmsr);
15430 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15431 (bmsr & BMSR_LSTATUS))
15432 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015433
Linus Torvalds1da177e2005-04-16 15:20:36 -070015434 err = tg3_phy_reset(tp);
15435 if (err)
15436 return err;
15437
Matt Carlson42b64a42011-05-19 12:12:49 +000015438 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015439
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015440 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015441 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15442 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015443
15444 tg3_writephy(tp, MII_BMCR,
15445 BMCR_ANENABLE | BMCR_ANRESTART);
15446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015447 }
15448
15449skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015450 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015451 err = tg3_init_5401phy_dsp(tp);
15452 if (err)
15453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015454
Linus Torvalds1da177e2005-04-16 15:20:36 -070015455 err = tg3_init_5401phy_dsp(tp);
15456 }
15457
Linus Torvalds1da177e2005-04-16 15:20:36 -070015458 return err;
15459}
15460
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015461static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015462{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015463 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015464 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015465 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015466 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015467
Matt Carlson535a4902011-07-20 10:20:56 +000015468 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015469 if (!vpd_data)
15470 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015471
Matt Carlson535a4902011-07-20 10:20:56 +000015472 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015473 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015474 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015475
15476 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15477 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15478 i += PCI_VPD_LRDT_TAG_SIZE;
15479
Matt Carlson535a4902011-07-20 10:20:56 +000015480 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015481 goto out_not_found;
15482
Matt Carlson184b8902010-04-05 10:19:25 +000015483 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15484 PCI_VPD_RO_KEYWORD_MFR_ID);
15485 if (j > 0) {
15486 len = pci_vpd_info_field_size(&vpd_data[j]);
15487
15488 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15489 if (j + len > block_end || len != 4 ||
15490 memcmp(&vpd_data[j], "1028", 4))
15491 goto partno;
15492
15493 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15494 PCI_VPD_RO_KEYWORD_VENDOR0);
15495 if (j < 0)
15496 goto partno;
15497
15498 len = pci_vpd_info_field_size(&vpd_data[j]);
15499
15500 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15501 if (j + len > block_end)
15502 goto partno;
15503
Kees Cook715230a2013-03-27 06:40:50 +000015504 if (len >= sizeof(tp->fw_ver))
15505 len = sizeof(tp->fw_ver) - 1;
15506 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15507 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15508 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015509 }
15510
15511partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015512 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15513 PCI_VPD_RO_KEYWORD_PARTNO);
15514 if (i < 0)
15515 goto out_not_found;
15516
15517 len = pci_vpd_info_field_size(&vpd_data[i]);
15518
15519 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15520 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015521 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015522 goto out_not_found;
15523
15524 memcpy(tp->board_part_number, &vpd_data[i], len);
15525
Linus Torvalds1da177e2005-04-16 15:20:36 -070015526out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015527 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015528 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015529 return;
15530
15531out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015532 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015533 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15534 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015535 strcpy(tp->board_part_number, "BCM5717");
15536 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15537 strcpy(tp->board_part_number, "BCM5718");
15538 else
15539 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015540 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015541 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15542 strcpy(tp->board_part_number, "BCM57780");
15543 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15544 strcpy(tp->board_part_number, "BCM57760");
15545 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15546 strcpy(tp->board_part_number, "BCM57790");
15547 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15548 strcpy(tp->board_part_number, "BCM57788");
15549 else
15550 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015551 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015552 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15553 strcpy(tp->board_part_number, "BCM57761");
15554 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15555 strcpy(tp->board_part_number, "BCM57765");
15556 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15557 strcpy(tp->board_part_number, "BCM57781");
15558 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15559 strcpy(tp->board_part_number, "BCM57785");
15560 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15561 strcpy(tp->board_part_number, "BCM57791");
15562 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15563 strcpy(tp->board_part_number, "BCM57795");
15564 else
15565 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015566 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015567 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15568 strcpy(tp->board_part_number, "BCM57762");
15569 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15570 strcpy(tp->board_part_number, "BCM57766");
15571 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15572 strcpy(tp->board_part_number, "BCM57782");
15573 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15574 strcpy(tp->board_part_number, "BCM57786");
15575 else
15576 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015577 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015578 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015579 } else {
15580nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015581 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015583}
15584
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015585static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015586{
15587 u32 val;
15588
Matt Carlsone4f34112009-02-25 14:25:00 +000015589 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015590 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015591 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015592 val != 0)
15593 return 0;
15594
15595 return 1;
15596}
15597
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015598static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015599{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015600 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015601 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015602 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015603
15604 if (tg3_nvram_read(tp, 0xc, &offset) ||
15605 tg3_nvram_read(tp, 0x4, &start))
15606 return;
15607
15608 offset = tg3_nvram_logical_addr(tp, offset);
15609
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015610 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015611 return;
15612
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015613 if ((val & 0xfc000000) == 0x0c000000) {
15614 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015615 return;
15616
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015617 if (val == 0)
15618 newver = true;
15619 }
15620
Matt Carlson75f99362010-04-05 10:19:24 +000015621 dst_off = strlen(tp->fw_ver);
15622
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015623 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015624 if (TG3_VER_SIZE - dst_off < 16 ||
15625 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015626 return;
15627
15628 offset = offset + ver_offset - start;
15629 for (i = 0; i < 16; i += 4) {
15630 __be32 v;
15631 if (tg3_nvram_read_be32(tp, offset + i, &v))
15632 return;
15633
Matt Carlson75f99362010-04-05 10:19:24 +000015634 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015635 }
15636 } else {
15637 u32 major, minor;
15638
15639 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15640 return;
15641
15642 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15643 TG3_NVM_BCVER_MAJSFT;
15644 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015645 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15646 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015647 }
15648}
15649
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015650static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015651{
15652 u32 val, major, minor;
15653
15654 /* Use native endian representation */
15655 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15656 return;
15657
15658 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15659 TG3_NVM_HWSB_CFG1_MAJSFT;
15660 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15661 TG3_NVM_HWSB_CFG1_MINSFT;
15662
15663 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15664}
15665
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015666static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015667{
15668 u32 offset, major, minor, build;
15669
Matt Carlson75f99362010-04-05 10:19:24 +000015670 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015671
15672 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15673 return;
15674
15675 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15676 case TG3_EEPROM_SB_REVISION_0:
15677 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15678 break;
15679 case TG3_EEPROM_SB_REVISION_2:
15680 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15681 break;
15682 case TG3_EEPROM_SB_REVISION_3:
15683 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15684 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015685 case TG3_EEPROM_SB_REVISION_4:
15686 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15687 break;
15688 case TG3_EEPROM_SB_REVISION_5:
15689 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15690 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015691 case TG3_EEPROM_SB_REVISION_6:
15692 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15693 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015694 default:
15695 return;
15696 }
15697
Matt Carlsone4f34112009-02-25 14:25:00 +000015698 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015699 return;
15700
15701 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15702 TG3_EEPROM_SB_EDH_BLD_SHFT;
15703 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15704 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15705 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15706
15707 if (minor > 99 || build > 26)
15708 return;
15709
Matt Carlson75f99362010-04-05 10:19:24 +000015710 offset = strlen(tp->fw_ver);
15711 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15712 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015713
15714 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015715 offset = strlen(tp->fw_ver);
15716 if (offset < TG3_VER_SIZE - 1)
15717 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015718 }
15719}
15720
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015721static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015722{
15723 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015724 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015725
15726 for (offset = TG3_NVM_DIR_START;
15727 offset < TG3_NVM_DIR_END;
15728 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015729 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015730 return;
15731
15732 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15733 break;
15734 }
15735
15736 if (offset == TG3_NVM_DIR_END)
15737 return;
15738
Joe Perches63c3a662011-04-26 08:12:10 +000015739 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015740 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015741 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015742 return;
15743
Matt Carlsone4f34112009-02-25 14:25:00 +000015744 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015745 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015746 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015747 return;
15748
15749 offset += val - start;
15750
Matt Carlsonacd9c112009-02-25 14:26:33 +000015751 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015752
Matt Carlsonacd9c112009-02-25 14:26:33 +000015753 tp->fw_ver[vlen++] = ',';
15754 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015755
15756 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015757 __be32 v;
15758 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015759 return;
15760
Al Virob9fc7dc2007-12-17 22:59:57 -080015761 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015762
Matt Carlsonacd9c112009-02-25 14:26:33 +000015763 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15764 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015765 break;
15766 }
15767
Matt Carlsonacd9c112009-02-25 14:26:33 +000015768 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15769 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015770 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015771}
15772
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015773static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015774{
Matt Carlson7fd76442009-02-25 14:27:20 +000015775 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015776
15777 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15778 if (apedata != APE_SEG_SIG_MAGIC)
15779 return;
15780
15781 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15782 if (!(apedata & APE_FW_STATUS_READY))
15783 return;
15784
Michael Chan165f4d12012-07-16 16:23:59 +000015785 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15786 tg3_flag_set(tp, APE_HAS_NCSI);
15787}
15788
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015789static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015790{
15791 int vlen;
15792 u32 apedata;
15793 char *fwtype;
15794
Matt Carlson7fd76442009-02-25 14:27:20 +000015795 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15796
Michael Chan165f4d12012-07-16 16:23:59 +000015797 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015798 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015799 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15800 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015801 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015802 fwtype = "DASH";
15803
Matt Carlson7fd76442009-02-25 14:27:20 +000015804 vlen = strlen(tp->fw_ver);
15805
Matt Carlsonecc79642010-08-02 11:26:01 +000015806 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15807 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015808 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15809 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15810 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15811 (apedata & APE_FW_VERSION_BLDMSK));
15812}
15813
Michael Chanc86a8562013-01-06 12:51:08 +000015814static void tg3_read_otp_ver(struct tg3 *tp)
15815{
15816 u32 val, val2;
15817
Joe Perches41535772013-02-16 11:20:04 +000015818 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015819 return;
15820
15821 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15822 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15823 TG3_OTP_MAGIC0_VALID(val)) {
15824 u64 val64 = (u64) val << 32 | val2;
15825 u32 ver = 0;
15826 int i, vlen;
15827
15828 for (i = 0; i < 7; i++) {
15829 if ((val64 & 0xff) == 0)
15830 break;
15831 ver = val64 & 0xff;
15832 val64 >>= 8;
15833 }
15834 vlen = strlen(tp->fw_ver);
15835 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15836 }
15837}
15838
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015839static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015840{
15841 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015842 bool vpd_vers = false;
15843
15844 if (tp->fw_ver[0] != 0)
15845 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015846
Joe Perches63c3a662011-04-26 08:12:10 +000015847 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015848 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015849 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015850 return;
15851 }
15852
Matt Carlsonacd9c112009-02-25 14:26:33 +000015853 if (tg3_nvram_read(tp, 0, &val))
15854 return;
15855
15856 if (val == TG3_EEPROM_MAGIC)
15857 tg3_read_bc_ver(tp);
15858 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15859 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015860 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15861 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015862
Michael Chan165f4d12012-07-16 16:23:59 +000015863 if (tg3_flag(tp, ENABLE_ASF)) {
15864 if (tg3_flag(tp, ENABLE_APE)) {
15865 tg3_probe_ncsi(tp);
15866 if (!vpd_vers)
15867 tg3_read_dash_ver(tp);
15868 } else if (!vpd_vers) {
15869 tg3_read_mgmtfw_ver(tp);
15870 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015871 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015872
15873 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015874}
15875
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015876static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15877{
Joe Perches63c3a662011-04-26 08:12:10 +000015878 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015879 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015880 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015881 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015882 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015883 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015884}
15885
Matt Carlson41434702011-03-09 16:58:22 +000015886static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015887 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15888 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15889 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15890 { },
15891};
15892
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015893static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015894{
15895 struct pci_dev *peer;
15896 unsigned int func, devnr = tp->pdev->devfn & ~7;
15897
15898 for (func = 0; func < 8; func++) {
15899 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15900 if (peer && peer != tp->pdev)
15901 break;
15902 pci_dev_put(peer);
15903 }
15904 /* 5704 can be configured in single-port mode, set peer to
15905 * tp->pdev in that case.
15906 */
15907 if (!peer) {
15908 peer = tp->pdev;
15909 return peer;
15910 }
15911
15912 /*
15913 * We don't need to keep the refcount elevated; there's no way
15914 * to remove one half of this device without removing the other
15915 */
15916 pci_dev_put(peer);
15917
15918 return peer;
15919}
15920
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015921static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015922{
15923 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015924 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015925 u32 reg;
15926
15927 /* All devices that use the alternate
15928 * ASIC REV location have a CPMU.
15929 */
15930 tg3_flag_set(tp, CPMU_PRESENT);
15931
15932 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015933 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015934 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15935 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015936 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070015937 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
15938 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015939 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15940 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070015941 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
15942 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
Matt Carlson42b123b2012-02-13 15:20:13 +000015943 reg = TG3PCI_GEN2_PRODID_ASICREV;
15944 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15945 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15946 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15947 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15948 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15949 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15950 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15951 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15952 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15953 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15954 reg = TG3PCI_GEN15_PRODID_ASICREV;
15955 else
15956 reg = TG3PCI_PRODID_ASICREV;
15957
15958 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15959 }
15960
15961 /* Wrong chip ID in 5752 A0. This code can be removed later
15962 * as A0 is not in production.
15963 */
Joe Perches41535772013-02-16 11:20:04 +000015964 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015965 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15966
Joe Perches41535772013-02-16 11:20:04 +000015967 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015968 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15969
Joe Perches41535772013-02-16 11:20:04 +000015970 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15971 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15972 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015973 tg3_flag_set(tp, 5717_PLUS);
15974
Joe Perches41535772013-02-16 11:20:04 +000015975 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15976 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015977 tg3_flag_set(tp, 57765_CLASS);
15978
Michael Chanc65a17f2013-01-06 12:51:07 +000015979 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015980 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015981 tg3_flag_set(tp, 57765_PLUS);
15982
15983 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015984 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15985 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15986 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15987 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15988 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15989 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015990 tg3_flag(tp, 57765_PLUS))
15991 tg3_flag_set(tp, 5755_PLUS);
15992
Joe Perches41535772013-02-16 11:20:04 +000015993 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15994 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015995 tg3_flag_set(tp, 5780_CLASS);
15996
Joe Perches41535772013-02-16 11:20:04 +000015997 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15998 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15999 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016000 tg3_flag(tp, 5755_PLUS) ||
16001 tg3_flag(tp, 5780_CLASS))
16002 tg3_flag_set(tp, 5750_PLUS);
16003
Joe Perches41535772013-02-16 11:20:04 +000016004 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016005 tg3_flag(tp, 5750_PLUS))
16006 tg3_flag_set(tp, 5705_PLUS);
16007}
16008
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016009static bool tg3_10_100_only_device(struct tg3 *tp,
16010 const struct pci_device_id *ent)
16011{
16012 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
16013
Joe Perches41535772013-02-16 11:20:04 +000016014 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
16015 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016016 (tp->phy_flags & TG3_PHYFLG_IS_FET))
16017 return true;
16018
16019 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000016020 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016021 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
16022 return true;
16023 } else {
16024 return true;
16025 }
16026 }
16027
16028 return false;
16029}
16030
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000016031static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016032{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016033 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016034 u32 pci_state_reg, grc_misc_cfg;
16035 u32 val;
16036 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016037 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016038
Linus Torvalds1da177e2005-04-16 15:20:36 -070016039 /* Force memory write invalidate off. If we leave it on,
16040 * then on 5700_BX chips we have to enable a workaround.
16041 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
16042 * to match the cacheline size. The Broadcom driver have this
16043 * workaround but turns MWI off all the times so never uses
16044 * it. This seems to suggest that the workaround is insufficient.
16045 */
16046 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16047 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
16048 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16049
Matt Carlson16821282011-07-13 09:27:28 +000016050 /* Important! -- Make sure register accesses are byteswapped
16051 * correctly. Also, for those chips that require it, make
16052 * sure that indirect register accesses are enabled before
16053 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016054 */
16055 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16056 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000016057 tp->misc_host_ctrl |= (misc_ctrl_reg &
16058 MISC_HOST_CTRL_CHIPREV);
16059 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16060 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016061
Matt Carlson42b123b2012-02-13 15:20:13 +000016062 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070016063
Michael Chan68929142005-08-09 20:17:14 -070016064 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
16065 * we need to disable memory and use config. cycles
16066 * only to access all registers. The 5702/03 chips
16067 * can mistakenly decode the special cycles from the
16068 * ICH chipsets as memory write cycles, causing corruption
16069 * of register and memory space. Only certain ICH bridges
16070 * will drive special cycles with non-zero data during the
16071 * address phase which can fall within the 5703's address
16072 * range. This is not an ICH bug as the PCI spec allows
16073 * non-zero address during special cycles. However, only
16074 * these ICH bridges are known to drive non-zero addresses
16075 * during special cycles.
16076 *
16077 * Since special cycles do not cross PCI bridges, we only
16078 * enable this workaround if the 5703 is on the secondary
16079 * bus of these ICH bridges.
16080 */
Joe Perches41535772013-02-16 11:20:04 +000016081 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
16082 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070016083 static struct tg3_dev_id {
16084 u32 vendor;
16085 u32 device;
16086 u32 rev;
16087 } ich_chipsets[] = {
16088 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
16089 PCI_ANY_ID },
16090 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
16091 PCI_ANY_ID },
16092 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
16093 0xa },
16094 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
16095 PCI_ANY_ID },
16096 { },
16097 };
16098 struct tg3_dev_id *pci_id = &ich_chipsets[0];
16099 struct pci_dev *bridge = NULL;
16100
16101 while (pci_id->vendor != 0) {
16102 bridge = pci_get_device(pci_id->vendor, pci_id->device,
16103 bridge);
16104 if (!bridge) {
16105 pci_id++;
16106 continue;
16107 }
16108 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070016109 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070016110 continue;
16111 }
16112 if (bridge->subordinate &&
16113 (bridge->subordinate->number ==
16114 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016115 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070016116 pci_dev_put(bridge);
16117 break;
16118 }
16119 }
16120 }
16121
Joe Perches41535772013-02-16 11:20:04 +000016122 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070016123 static struct tg3_dev_id {
16124 u32 vendor;
16125 u32 device;
16126 } bridge_chipsets[] = {
16127 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
16128 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
16129 { },
16130 };
16131 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
16132 struct pci_dev *bridge = NULL;
16133
16134 while (pci_id->vendor != 0) {
16135 bridge = pci_get_device(pci_id->vendor,
16136 pci_id->device,
16137 bridge);
16138 if (!bridge) {
16139 pci_id++;
16140 continue;
16141 }
16142 if (bridge->subordinate &&
16143 (bridge->subordinate->number <=
16144 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016145 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070016146 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016147 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070016148 pci_dev_put(bridge);
16149 break;
16150 }
16151 }
16152 }
16153
Michael Chan4a29cc22006-03-19 13:21:12 -080016154 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
16155 * DMA addresses > 40-bit. This bridge may have other additional
16156 * 57xx devices behind it in some 4-port NIC designs for example.
16157 * Any tg3 device found behind the bridge will also need the 40-bit
16158 * DMA workaround.
16159 */
Matt Carlson42b123b2012-02-13 15:20:13 +000016160 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016161 tg3_flag_set(tp, 40BIT_DMA_BUG);
Yijing Wang0f847582013-08-08 21:03:12 +080016162 tp->msi_cap = tp->pdev->msi_cap;
Matt Carlson859a588792010-04-05 10:19:28 +000016163 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080016164 struct pci_dev *bridge = NULL;
16165
16166 do {
16167 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
16168 PCI_DEVICE_ID_SERVERWORKS_EPB,
16169 bridge);
16170 if (bridge && bridge->subordinate &&
16171 (bridge->subordinate->number <=
16172 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016173 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080016174 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016175 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080016176 pci_dev_put(bridge);
16177 break;
16178 }
16179 } while (bridge);
16180 }
Michael Chan4cf78e42005-07-25 12:29:19 -070016181
Joe Perches41535772013-02-16 11:20:04 +000016182 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
16183 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070016184 tp->pdev_peer = tg3_find_peer(tp);
16185
Matt Carlson507399f2009-11-13 13:03:37 +000016186 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000016187 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000016188 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000016189 else if (tg3_flag(tp, 57765_PLUS))
16190 tg3_flag_set(tp, HW_TSO_3);
16191 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016192 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016193 tg3_flag_set(tp, HW_TSO_2);
16194 else if (tg3_flag(tp, 5750_PLUS)) {
16195 tg3_flag_set(tp, HW_TSO_1);
16196 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016197 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
16198 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000016199 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016200 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16201 tg3_asic_rev(tp) != ASIC_REV_5701 &&
16202 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000016203 tg3_flag_set(tp, FW_TSO);
16204 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016205 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000016206 tp->fw_needed = FIRMWARE_TG3TSO5;
16207 else
16208 tp->fw_needed = FIRMWARE_TG3TSO;
16209 }
16210
Matt Carlsondabc5c62011-05-19 12:12:52 +000016211 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000016212 if (tg3_flag(tp, HW_TSO_1) ||
16213 tg3_flag(tp, HW_TSO_2) ||
16214 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000016215 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016216 /* For firmware TSO, assume ASF is disabled.
16217 * We'll disable TSO later if we discover ASF
16218 * is enabled in tg3_get_eeprom_hw_cfg().
16219 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000016220 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016221 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000016222 tg3_flag_clear(tp, TSO_CAPABLE);
16223 tg3_flag_clear(tp, TSO_BUG);
16224 tp->fw_needed = NULL;
16225 }
16226
Joe Perches41535772013-02-16 11:20:04 +000016227 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000016228 tp->fw_needed = FIRMWARE_TG3;
16229
Nithin Sujirc4dab502013-03-06 17:02:34 +000016230 if (tg3_asic_rev(tp) == ASIC_REV_57766)
16231 tp->fw_needed = FIRMWARE_TG357766;
16232
Matt Carlson507399f2009-11-13 13:03:37 +000016233 tp->irq_max = 1;
16234
Joe Perches63c3a662011-04-26 08:12:10 +000016235 if (tg3_flag(tp, 5750_PLUS)) {
16236 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000016237 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
16238 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
16239 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
16240 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070016241 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000016242 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070016243
Joe Perches63c3a662011-04-26 08:12:10 +000016244 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016245 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016246 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070016247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016248
Joe Perches63c3a662011-04-26 08:12:10 +000016249 if (tg3_flag(tp, 57765_PLUS)) {
16250 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000016251 tp->irq_max = TG3_IRQ_MAX_VECS;
16252 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016253 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000016254
Michael Chan91024262012-09-28 07:12:38 +000016255 tp->txq_max = 1;
16256 tp->rxq_max = 1;
16257 if (tp->irq_max > 1) {
16258 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
16259 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
16260
Joe Perches41535772013-02-16 11:20:04 +000016261 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16262 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000016263 tp->txq_max = tp->irq_max - 1;
16264 }
16265
Matt Carlsonb7abee62012-06-07 12:56:54 +000016266 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016267 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016268 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016269
Joe Perches41535772013-02-16 11:20:04 +000016270 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000016271 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000016272
Joe Perches41535772013-02-16 11:20:04 +000016273 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16274 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16275 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16276 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000016277 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000016278
Joe Perches63c3a662011-04-26 08:12:10 +000016279 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000016280 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000016281 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000016282
Joe Perches63c3a662011-04-26 08:12:10 +000016283 if (!tg3_flag(tp, 5705_PLUS) ||
16284 tg3_flag(tp, 5780_CLASS) ||
16285 tg3_flag(tp, USE_JUMBO_BDFLAG))
16286 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070016287
Matt Carlson52f44902008-11-21 17:17:04 -080016288 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16289 &pci_state_reg);
16290
Jon Mason708ebb3a2011-06-27 12:56:50 +000016291 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016292 u16 lnkctl;
16293
Joe Perches63c3a662011-04-26 08:12:10 +000016294 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080016295
Jiang Liu0f49bfb2012-08-20 13:28:20 -060016296 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016297 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000016298 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016299 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000016300 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000016301 }
Joe Perches41535772013-02-16 11:20:04 +000016302 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
16303 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16304 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
16305 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000016306 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016307 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000016308 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080016309 }
Joe Perches41535772013-02-16 11:20:04 +000016310 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000016311 /* BCM5785 devices are effectively PCIe devices, and should
16312 * follow PCIe codepaths, but do not have a PCIe capabilities
16313 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016314 */
Joe Perches63c3a662011-04-26 08:12:10 +000016315 tg3_flag_set(tp, PCI_EXPRESS);
16316 } else if (!tg3_flag(tp, 5705_PLUS) ||
16317 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016318 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16319 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016320 dev_err(&tp->pdev->dev,
16321 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016322 return -EIO;
16323 }
16324
16325 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016326 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016328
Michael Chan399de502005-10-03 14:02:39 -070016329 /* If we have an AMD 762 or VIA K8T800 chipset, write
16330 * reordering to the mailbox registers done by the host
16331 * controller can cause major troubles. We read back from
16332 * every mailbox register write to force the writes to be
16333 * posted to the chip in order.
16334 */
Matt Carlson41434702011-03-09 16:58:22 +000016335 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016336 !tg3_flag(tp, PCI_EXPRESS))
16337 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016338
Matt Carlson69fc4052008-12-21 20:19:57 -080016339 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16340 &tp->pci_cacheline_sz);
16341 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16342 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016343 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016344 tp->pci_lat_timer < 64) {
16345 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016346 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16347 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016348 }
16349
Matt Carlson16821282011-07-13 09:27:28 +000016350 /* Important! -- It is critical that the PCI-X hw workaround
16351 * situation is decided before the first MMIO register access.
16352 */
Joe Perches41535772013-02-16 11:20:04 +000016353 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016354 /* 5700 BX chips need to have their TX producer index
16355 * mailboxes written twice to workaround a bug.
16356 */
Joe Perches63c3a662011-04-26 08:12:10 +000016357 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016358
Matt Carlson52f44902008-11-21 17:17:04 -080016359 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016360 *
16361 * The workaround is to use indirect register accesses
16362 * for all chip writes not to mailbox registers.
16363 */
Joe Perches63c3a662011-04-26 08:12:10 +000016364 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016365 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016366
Joe Perches63c3a662011-04-26 08:12:10 +000016367 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016368
16369 /* The chip can have it's power management PCI config
16370 * space registers clobbered due to this bug.
16371 * So explicitly force the chip into D0 here.
16372 */
Matt Carlson9974a352007-10-07 23:27:28 -070016373 pci_read_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016374 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016375 &pm_reg);
16376 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16377 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016378 pci_write_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016379 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016380 pm_reg);
16381
16382 /* Also, force SERR#/PERR# in PCI command. */
16383 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16384 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16385 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16386 }
16387 }
16388
Linus Torvalds1da177e2005-04-16 15:20:36 -070016389 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016390 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016391 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016392 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016393
16394 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016395 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016396 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16397 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16398 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16399 }
16400
Michael Chan1ee582d2005-08-09 20:16:46 -070016401 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016402 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016403 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016404 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016405 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016406 tp->write32_tx_mbox = tg3_write32;
16407 tp->write32_rx_mbox = tg3_write32;
16408
16409 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016410 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016411 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016412 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016413 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016414 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016415 /*
16416 * Back to back register writes can cause problems on these
16417 * chips, the workaround is to read back all reg writes
16418 * except those to mailbox regs.
16419 *
16420 * See tg3_write_indirect_reg32().
16421 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016422 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016423 }
16424
Joe Perches63c3a662011-04-26 08:12:10 +000016425 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016426 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016427 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016428 tp->write32_rx_mbox = tg3_write_flush_reg32;
16429 }
Michael Chan20094932005-08-09 20:16:32 -070016430
Joe Perches63c3a662011-04-26 08:12:10 +000016431 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016432 tp->read32 = tg3_read_indirect_reg32;
16433 tp->write32 = tg3_write_indirect_reg32;
16434 tp->read32_mbox = tg3_read_indirect_mbox;
16435 tp->write32_mbox = tg3_write_indirect_mbox;
16436 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16437 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16438
16439 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016440 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016441
16442 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16443 pci_cmd &= ~PCI_COMMAND_MEMORY;
16444 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16445 }
Joe Perches41535772013-02-16 11:20:04 +000016446 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016447 tp->read32_mbox = tg3_read32_mbox_5906;
16448 tp->write32_mbox = tg3_write32_mbox_5906;
16449 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16450 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16451 }
Michael Chan68929142005-08-09 20:17:14 -070016452
Michael Chanbbadf502006-04-06 21:46:34 -070016453 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016454 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016455 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16456 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016457 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016458
Matt Carlson16821282011-07-13 09:27:28 +000016459 /* The memory arbiter has to be enabled in order for SRAM accesses
16460 * to succeed. Normally on powerup the tg3 chip firmware will make
16461 * sure it is enabled, but other entities such as system netboot
16462 * code might disable it.
16463 */
16464 val = tr32(MEMARB_MODE);
16465 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16466
Matt Carlson9dc5e342011-11-04 09:15:02 +000016467 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016468 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016469 tg3_flag(tp, 5780_CLASS)) {
16470 if (tg3_flag(tp, PCIX_MODE)) {
16471 pci_read_config_dword(tp->pdev,
16472 tp->pcix_cap + PCI_X_STATUS,
16473 &val);
16474 tp->pci_fn = val & 0x7;
16475 }
Joe Perches41535772013-02-16 11:20:04 +000016476 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16477 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16478 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016479 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016480 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16481 val = tr32(TG3_CPMU_STATUS);
16482
Joe Perches41535772013-02-16 11:20:04 +000016483 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016484 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16485 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016486 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16487 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016488 }
16489
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016490 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16491 tp->write32_tx_mbox = tg3_write_flush_reg32;
16492 tp->write32_rx_mbox = tg3_write_flush_reg32;
16493 }
16494
Michael Chan7d0c41e2005-04-21 17:06:20 -070016495 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016496 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016497 * determined before calling tg3_set_power_state() so that
16498 * we know whether or not to switch out of Vaux power.
16499 * When the flag is set, it means that GPIO1 is used for eeprom
16500 * write protect and also implies that it is a LOM where GPIOs
16501 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016502 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016503 tg3_get_eeprom_hw_cfg(tp);
16504
Matt Carlson1caf13e2013-03-06 17:02:29 +000016505 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016506 tg3_flag_clear(tp, TSO_CAPABLE);
16507 tg3_flag_clear(tp, TSO_BUG);
16508 tp->fw_needed = NULL;
16509 }
16510
Joe Perches63c3a662011-04-26 08:12:10 +000016511 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016512 /* Allow reads and writes to the
16513 * APE register and memory space.
16514 */
16515 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016516 PCISTATE_ALLOW_APE_SHMEM_WR |
16517 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016518 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16519 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016520
16521 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016522 }
16523
Matt Carlson16821282011-07-13 09:27:28 +000016524 /* Set up tp->grc_local_ctrl before calling
16525 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16526 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016527 * It is also used as eeprom write protect on LOMs.
16528 */
16529 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016530 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016531 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016532 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16533 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016534 /* Unused GPIO3 must be driven as output on 5752 because there
16535 * are no pull-up resistors on unused GPIO pins.
16536 */
Joe Perches41535772013-02-16 11:20:04 +000016537 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016538 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016539
Joe Perches41535772013-02-16 11:20:04 +000016540 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16541 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016542 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016543 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16544
Matt Carlson8d519ab2009-04-20 06:58:01 +000016545 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16546 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016547 /* Turn off the debug UART. */
16548 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016549 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016550 /* Keep VMain power. */
16551 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16552 GRC_LCLCTRL_GPIO_OUTPUT0;
16553 }
16554
Joe Perches41535772013-02-16 11:20:04 +000016555 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016556 tp->grc_local_ctrl |=
16557 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16558
Matt Carlson16821282011-07-13 09:27:28 +000016559 /* Switch out of Vaux if it is a NIC */
16560 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016561
Linus Torvalds1da177e2005-04-16 15:20:36 -070016562 /* Derive initial jumbo mode from MTU assigned in
16563 * ether_setup() via the alloc_etherdev() call
16564 */
Joe Perches63c3a662011-04-26 08:12:10 +000016565 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16566 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016567
16568 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016569 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16570 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16571 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16572 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016573 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016574 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016575 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016576 }
16577
Joe Perches41535772013-02-16 11:20:04 +000016578 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016579 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016580
Linus Torvalds1da177e2005-04-16 15:20:36 -070016581 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016582 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16583 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16584 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16585 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016586 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16587 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16588 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016589
Joe Perches41535772013-02-16 11:20:04 +000016590 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16591 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016592 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016593 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016594 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016595
Joe Perches63c3a662011-04-26 08:12:10 +000016596 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016597 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016598 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16599 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016600 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016601 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16602 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16603 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16604 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016605 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16606 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016607 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016608 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016609 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016610 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016611 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016613
Joe Perches41535772013-02-16 11:20:04 +000016614 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16615 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016616 tp->phy_otp = tg3_read_otp_phycfg(tp);
16617 if (tp->phy_otp == 0)
16618 tp->phy_otp = TG3_OTP_DEFAULT;
16619 }
16620
Joe Perches63c3a662011-04-26 08:12:10 +000016621 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016622 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16623 else
16624 tp->mi_mode = MAC_MI_MODE_BASE;
16625
Linus Torvalds1da177e2005-04-16 15:20:36 -070016626 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016627 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16628 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016629 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16630
Matt Carlson4d958472011-04-20 07:57:35 +000016631 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016632 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080016633 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000016634 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16635 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016636 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16637 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16638 }
16639
Joe Perches41535772013-02-16 11:20:04 +000016640 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16641 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016642 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016643
Matt Carlson158d7ab2008-05-29 01:37:54 -070016644 err = tg3_mdio_init(tp);
16645 if (err)
16646 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016647
16648 /* Initialize data/descriptor byte/word swapping. */
16649 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016650 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16651 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016652 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16653 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16654 GRC_MODE_B2HRX_ENABLE |
16655 GRC_MODE_HTX2B_ENABLE |
16656 GRC_MODE_HOST_STACKUP);
16657 else
16658 val &= GRC_MODE_HOST_STACKUP;
16659
Linus Torvalds1da177e2005-04-16 15:20:36 -070016660 tw32(GRC_MODE, val | tp->grc_mode);
16661
16662 tg3_switch_clocks(tp);
16663
16664 /* Clear this out for sanity. */
16665 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16666
Nat Gurumoorthy388d3332013-12-09 10:43:21 -080016667 /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
16668 tw32(TG3PCI_REG_BASE_ADDR, 0);
16669
Linus Torvalds1da177e2005-04-16 15:20:36 -070016670 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16671 &pci_state_reg);
16672 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016673 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016674 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16675 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16676 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16677 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016678 void __iomem *sram_base;
16679
16680 /* Write some dummy words into the SRAM status block
16681 * area, see if it reads back correctly. If the return
16682 * value is bad, force enable the PCIX workaround.
16683 */
16684 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16685
16686 writel(0x00000000, sram_base);
16687 writel(0x00000000, sram_base + 4);
16688 writel(0xffffffff, sram_base + 4);
16689 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016690 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016691 }
16692 }
16693
16694 udelay(50);
16695 tg3_nvram_init(tp);
16696
Nithin Sujirc4dab502013-03-06 17:02:34 +000016697 /* If the device has an NVRAM, no need to load patch firmware */
16698 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16699 !tg3_flag(tp, NO_NVRAM))
16700 tp->fw_needed = NULL;
16701
Linus Torvalds1da177e2005-04-16 15:20:36 -070016702 grc_misc_cfg = tr32(GRC_MISC_CFG);
16703 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16704
Joe Perches41535772013-02-16 11:20:04 +000016705 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016706 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16707 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016708 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016709
Joe Perches63c3a662011-04-26 08:12:10 +000016710 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016711 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016712 tg3_flag_set(tp, TAGGED_STATUS);
16713 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016714 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16715 HOSTCC_MODE_CLRTICK_TXBD);
16716
16717 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16718 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16719 tp->misc_host_ctrl);
16720 }
16721
Matt Carlson3bda1252008-08-15 14:08:22 -070016722 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016723 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016724 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016725 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016726 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016727
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016728 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016729 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016730
16731 err = tg3_phy_probe(tp);
16732 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016733 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016734 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016735 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016736 }
16737
Matt Carlson184b8902010-04-05 10:19:25 +000016738 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016739 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016740
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016741 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16742 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016743 } else {
Joe Perches41535772013-02-16 11:20:04 +000016744 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016745 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016746 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016747 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016748 }
16749
16750 /* 5700 {AX,BX} chips have a broken status block link
16751 * change bit implementation, so we must use the
16752 * status register in those cases.
16753 */
Joe Perches41535772013-02-16 11:20:04 +000016754 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016755 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016756 else
Joe Perches63c3a662011-04-26 08:12:10 +000016757 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016758
16759 /* The led_ctrl is set during tg3_phy_probe, here we might
16760 * have to force the link status polling mechanism based
16761 * upon subsystem IDs.
16762 */
16763 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016764 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016765 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16766 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016767 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016768 }
16769
16770 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016771 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016772 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016773 else
Joe Perches63c3a662011-04-26 08:12:10 +000016774 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016775
Nithin Sujir1743b832014-01-03 10:09:14 -080016776 if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
16777 tg3_flag_set(tp, POLL_CPMU_LINK);
16778
Eric Dumazet9205fd92011-11-18 06:47:01 +000016779 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016780 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016781 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016782 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016783 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016784#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016785 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016786#endif
16787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016788
Matt Carlson2c49a442010-09-30 10:34:35 +000016789 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16790 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016791 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16792
Matt Carlson2c49a442010-09-30 10:34:35 +000016793 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016794
16795 /* Increment the rx prod index on the rx std ring by at most
16796 * 8 for these chips to workaround hw errata.
16797 */
Joe Perches41535772013-02-16 11:20:04 +000016798 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16799 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16800 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016801 tp->rx_std_max_post = 8;
16802
Joe Perches63c3a662011-04-26 08:12:10 +000016803 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016804 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16805 PCIE_PWR_MGMT_L1_THRESH_MSK;
16806
Linus Torvalds1da177e2005-04-16 15:20:36 -070016807 return err;
16808}
16809
David S. Miller49b6e95f2007-03-29 01:38:42 -070016810#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016811static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016812{
16813 struct net_device *dev = tp->dev;
16814 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016815 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016816 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016817 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016818
David S. Miller49b6e95f2007-03-29 01:38:42 -070016819 addr = of_get_property(dp, "local-mac-address", &len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070016820 if (addr && len == ETH_ALEN) {
16821 memcpy(dev->dev_addr, addr, ETH_ALEN);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016823 }
16824 return -ENODEV;
16825}
16826
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016827static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016828{
16829 struct net_device *dev = tp->dev;
16830
Joe Perchesd458cdf2013-10-01 19:04:40 -070016831 memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016832 return 0;
16833}
16834#endif
16835
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016836static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016837{
16838 struct net_device *dev = tp->dev;
16839 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016840 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016841 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016842
David S. Miller49b6e95f2007-03-29 01:38:42 -070016843#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016844 if (!tg3_get_macaddr_sparc(tp))
16845 return 0;
16846#endif
16847
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016848 if (tg3_flag(tp, IS_SSB_CORE)) {
16849 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16850 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16851 return 0;
16852 }
16853
Linus Torvalds1da177e2005-04-16 15:20:36 -070016854 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016855 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016856 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016857 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16858 mac_offset = 0xcc;
16859 if (tg3_nvram_lock(tp))
16860 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16861 else
16862 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016863 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016864 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016865 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016866 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016867 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016868 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016869 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016870
16871 /* First try to get it from MAC address mailbox. */
16872 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16873 if ((hi >> 16) == 0x484b) {
16874 dev->dev_addr[0] = (hi >> 8) & 0xff;
16875 dev->dev_addr[1] = (hi >> 0) & 0xff;
16876
16877 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16878 dev->dev_addr[2] = (lo >> 24) & 0xff;
16879 dev->dev_addr[3] = (lo >> 16) & 0xff;
16880 dev->dev_addr[4] = (lo >> 8) & 0xff;
16881 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016882
Michael Chan008652b2006-03-27 23:14:53 -080016883 /* Some old bootcode may report a 0 MAC address in SRAM */
16884 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16885 }
16886 if (!addr_ok) {
16887 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016888 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016889 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016890 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016891 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16892 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016893 }
16894 /* Finally just fetch it out of the MAC control regs. */
16895 else {
16896 hi = tr32(MAC_ADDR_0_HIGH);
16897 lo = tr32(MAC_ADDR_0_LOW);
16898
16899 dev->dev_addr[5] = lo & 0xff;
16900 dev->dev_addr[4] = (lo >> 8) & 0xff;
16901 dev->dev_addr[3] = (lo >> 16) & 0xff;
16902 dev->dev_addr[2] = (lo >> 24) & 0xff;
16903 dev->dev_addr[1] = hi & 0xff;
16904 dev->dev_addr[0] = (hi >> 8) & 0xff;
16905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016906 }
16907
16908 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016909#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016910 if (!tg3_get_default_macaddr_sparc(tp))
16911 return 0;
16912#endif
16913 return -EINVAL;
16914 }
16915 return 0;
16916}
16917
David S. Miller59e6b432005-05-18 22:50:10 -070016918#define BOUNDARY_SINGLE_CACHELINE 1
16919#define BOUNDARY_MULTI_CACHELINE 2
16920
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016921static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016922{
16923 int cacheline_size;
16924 u8 byte;
16925 int goal;
16926
16927 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16928 if (byte == 0)
16929 cacheline_size = 1024;
16930 else
16931 cacheline_size = (int) byte * 4;
16932
16933 /* On 5703 and later chips, the boundary bits have no
16934 * effect.
16935 */
Joe Perches41535772013-02-16 11:20:04 +000016936 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16937 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016938 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016939 goto out;
16940
16941#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16942 goal = BOUNDARY_MULTI_CACHELINE;
16943#else
16944#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16945 goal = BOUNDARY_SINGLE_CACHELINE;
16946#else
16947 goal = 0;
16948#endif
16949#endif
16950
Joe Perches63c3a662011-04-26 08:12:10 +000016951 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016952 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16953 goto out;
16954 }
16955
David S. Miller59e6b432005-05-18 22:50:10 -070016956 if (!goal)
16957 goto out;
16958
16959 /* PCI controllers on most RISC systems tend to disconnect
16960 * when a device tries to burst across a cache-line boundary.
16961 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16962 *
16963 * Unfortunately, for PCI-E there are only limited
16964 * write-side controls for this, and thus for reads
16965 * we will still get the disconnects. We'll also waste
16966 * these PCI cycles for both read and write for chips
16967 * other than 5700 and 5701 which do not implement the
16968 * boundary bits.
16969 */
Joe Perches63c3a662011-04-26 08:12:10 +000016970 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016971 switch (cacheline_size) {
16972 case 16:
16973 case 32:
16974 case 64:
16975 case 128:
16976 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16977 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16978 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16979 } else {
16980 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16981 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16982 }
16983 break;
16984
16985 case 256:
16986 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16987 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16988 break;
16989
16990 default:
16991 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16992 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16993 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016994 }
Joe Perches63c3a662011-04-26 08:12:10 +000016995 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016996 switch (cacheline_size) {
16997 case 16:
16998 case 32:
16999 case 64:
17000 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17001 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17002 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
17003 break;
17004 }
17005 /* fallthrough */
17006 case 128:
17007 default:
17008 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17009 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
17010 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017011 }
David S. Miller59e6b432005-05-18 22:50:10 -070017012 } else {
17013 switch (cacheline_size) {
17014 case 16:
17015 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17016 val |= (DMA_RWCTRL_READ_BNDRY_16 |
17017 DMA_RWCTRL_WRITE_BNDRY_16);
17018 break;
17019 }
17020 /* fallthrough */
17021 case 32:
17022 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17023 val |= (DMA_RWCTRL_READ_BNDRY_32 |
17024 DMA_RWCTRL_WRITE_BNDRY_32);
17025 break;
17026 }
17027 /* fallthrough */
17028 case 64:
17029 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17030 val |= (DMA_RWCTRL_READ_BNDRY_64 |
17031 DMA_RWCTRL_WRITE_BNDRY_64);
17032 break;
17033 }
17034 /* fallthrough */
17035 case 128:
17036 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17037 val |= (DMA_RWCTRL_READ_BNDRY_128 |
17038 DMA_RWCTRL_WRITE_BNDRY_128);
17039 break;
17040 }
17041 /* fallthrough */
17042 case 256:
17043 val |= (DMA_RWCTRL_READ_BNDRY_256 |
17044 DMA_RWCTRL_WRITE_BNDRY_256);
17045 break;
17046 case 512:
17047 val |= (DMA_RWCTRL_READ_BNDRY_512 |
17048 DMA_RWCTRL_WRITE_BNDRY_512);
17049 break;
17050 case 1024:
17051 default:
17052 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
17053 DMA_RWCTRL_WRITE_BNDRY_1024);
17054 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017055 }
David S. Miller59e6b432005-05-18 22:50:10 -070017056 }
17057
17058out:
17059 return val;
17060}
17061
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017062static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000017063 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017064{
17065 struct tg3_internal_buffer_desc test_desc;
17066 u32 sram_dma_descs;
17067 int i, ret;
17068
17069 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
17070
17071 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
17072 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
17073 tw32(RDMAC_STATUS, 0);
17074 tw32(WDMAC_STATUS, 0);
17075
17076 tw32(BUFMGR_MODE, 0);
17077 tw32(FTQ_RESET, 0);
17078
17079 test_desc.addr_hi = ((u64) buf_dma) >> 32;
17080 test_desc.addr_lo = buf_dma & 0xffffffff;
17081 test_desc.nic_mbuf = 0x00002100;
17082 test_desc.len = size;
17083
17084 /*
17085 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
17086 * the *second* time the tg3 driver was getting loaded after an
17087 * initial scan.
17088 *
17089 * Broadcom tells me:
17090 * ...the DMA engine is connected to the GRC block and a DMA
17091 * reset may affect the GRC block in some unpredictable way...
17092 * The behavior of resets to individual blocks has not been tested.
17093 *
17094 * Broadcom noted the GRC reset will also reset all sub-components.
17095 */
17096 if (to_device) {
17097 test_desc.cqid_sqid = (13 << 8) | 2;
17098
17099 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
17100 udelay(40);
17101 } else {
17102 test_desc.cqid_sqid = (16 << 8) | 7;
17103
17104 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
17105 udelay(40);
17106 }
17107 test_desc.flags = 0x00000005;
17108
17109 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
17110 u32 val;
17111
17112 val = *(((u32 *)&test_desc) + i);
17113 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
17114 sram_dma_descs + (i * sizeof(u32)));
17115 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
17116 }
17117 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
17118
Matt Carlson859a588792010-04-05 10:19:28 +000017119 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017120 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000017121 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070017122 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017123
17124 ret = -ENODEV;
17125 for (i = 0; i < 40; i++) {
17126 u32 val;
17127
17128 if (to_device)
17129 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
17130 else
17131 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
17132 if ((val & 0xffff) == sram_dma_descs) {
17133 ret = 0;
17134 break;
17135 }
17136
17137 udelay(100);
17138 }
17139
17140 return ret;
17141}
17142
David S. Millerded73402005-05-23 13:59:47 -070017143#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070017144
Matt Carlson41434702011-03-09 16:58:22 +000017145static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080017146 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
17147 { },
17148};
17149
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017150static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017151{
17152 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070017153 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017154 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017155
Matt Carlson4bae65c2010-11-24 08:31:52 +000017156 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
17157 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017158 if (!buf) {
17159 ret = -ENOMEM;
17160 goto out_nofree;
17161 }
17162
17163 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
17164 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
17165
David S. Miller59e6b432005-05-18 22:50:10 -070017166 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017167
Joe Perches63c3a662011-04-26 08:12:10 +000017168 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017169 goto out;
17170
Joe Perches63c3a662011-04-26 08:12:10 +000017171 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017172 /* DMA read watermark not used on PCIE */
17173 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000017174 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000017175 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
17176 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017177 tp->dma_rwctrl |= 0x003f0000;
17178 else
17179 tp->dma_rwctrl |= 0x003f000f;
17180 } else {
Joe Perches41535772013-02-16 11:20:04 +000017181 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17182 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017183 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080017184 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017185
Michael Chan4a29cc22006-03-19 13:21:12 -080017186 /* If the 5704 is behind the EPB bridge, we can
17187 * do the less restrictive ONE_DMA workaround for
17188 * better performance.
17189 */
Joe Perches63c3a662011-04-26 08:12:10 +000017190 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000017191 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080017192 tp->dma_rwctrl |= 0x8000;
17193 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017194 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
17195
Joe Perches41535772013-02-16 11:20:04 +000017196 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080017197 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070017198 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080017199 tp->dma_rwctrl |=
17200 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
17201 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
17202 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000017203 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070017204 /* 5780 always in PCIX mode */
17205 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000017206 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070017207 /* 5714 always in PCIX mode */
17208 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017209 } else {
17210 tp->dma_rwctrl |= 0x001b000f;
17211 }
17212 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017213 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
17214 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017215
Joe Perches41535772013-02-16 11:20:04 +000017216 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17217 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017218 tp->dma_rwctrl &= 0xfffffff0;
17219
Joe Perches41535772013-02-16 11:20:04 +000017220 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
17221 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017222 /* Remove this if it causes problems for some boards. */
17223 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
17224
17225 /* On 5700/5701 chips, we need to set this bit.
17226 * Otherwise the chip will issue cacheline transactions
17227 * to streamable DMA memory with not all the byte
17228 * enables turned on. This is an error on several
17229 * RISC PCI controllers, in particular sparc64.
17230 *
17231 * On 5703/5704 chips, this bit has been reassigned
17232 * a different meaning. In particular, it is used
17233 * on those chips to enable a PCI-X workaround.
17234 */
17235 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
17236 }
17237
17238 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17239
Linus Torvalds1da177e2005-04-16 15:20:36 -070017240
Joe Perches41535772013-02-16 11:20:04 +000017241 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17242 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017243 goto out;
17244
David S. Miller59e6b432005-05-18 22:50:10 -070017245 /* It is best to perform DMA test with maximum write burst size
17246 * to expose the 5700/5701 write DMA bug.
17247 */
17248 saved_dma_rwctrl = tp->dma_rwctrl;
17249 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17250 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17251
Linus Torvalds1da177e2005-04-16 15:20:36 -070017252 while (1) {
17253 u32 *p = buf, i;
17254
17255 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
17256 p[i] = i;
17257
17258 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000017259 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017260 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000017261 dev_err(&tp->pdev->dev,
17262 "%s: Buffer write failed. err = %d\n",
17263 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017264 break;
17265 }
17266
Linus Torvalds1da177e2005-04-16 15:20:36 -070017267 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000017268 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017269 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000017270 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
17271 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017272 break;
17273 }
17274
17275 /* Verify it. */
17276 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17277 if (p[i] == i)
17278 continue;
17279
David S. Miller59e6b432005-05-18 22:50:10 -070017280 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17281 DMA_RWCTRL_WRITE_BNDRY_16) {
17282 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017283 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
17284 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17285 break;
17286 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000017287 dev_err(&tp->pdev->dev,
17288 "%s: Buffer corrupted on read back! "
17289 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017290 ret = -ENODEV;
17291 goto out;
17292 }
17293 }
17294
17295 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
17296 /* Success. */
17297 ret = 0;
17298 break;
17299 }
17300 }
David S. Miller59e6b432005-05-18 22:50:10 -070017301 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17302 DMA_RWCTRL_WRITE_BNDRY_16) {
17303 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017304 * now look for chipsets that are known to expose the
17305 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017306 */
Matt Carlson41434702011-03-09 16:58:22 +000017307 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017308 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17309 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017310 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017311 /* Safe to use the calculated DMA boundary. */
17312 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017313 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017314
David S. Miller59e6b432005-05-18 22:50:10 -070017315 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017317
17318out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017319 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017320out_nofree:
17321 return ret;
17322}
17323
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017324static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017325{
Joe Perches63c3a662011-04-26 08:12:10 +000017326 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017327 tp->bufmgr_config.mbuf_read_dma_low_water =
17328 DEFAULT_MB_RDMA_LOW_WATER_5705;
17329 tp->bufmgr_config.mbuf_mac_rx_low_water =
17330 DEFAULT_MB_MACRX_LOW_WATER_57765;
17331 tp->bufmgr_config.mbuf_high_water =
17332 DEFAULT_MB_HIGH_WATER_57765;
17333
17334 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17335 DEFAULT_MB_RDMA_LOW_WATER_5705;
17336 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17337 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17338 tp->bufmgr_config.mbuf_high_water_jumbo =
17339 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017340 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017341 tp->bufmgr_config.mbuf_read_dma_low_water =
17342 DEFAULT_MB_RDMA_LOW_WATER_5705;
17343 tp->bufmgr_config.mbuf_mac_rx_low_water =
17344 DEFAULT_MB_MACRX_LOW_WATER_5705;
17345 tp->bufmgr_config.mbuf_high_water =
17346 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017347 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017348 tp->bufmgr_config.mbuf_mac_rx_low_water =
17349 DEFAULT_MB_MACRX_LOW_WATER_5906;
17350 tp->bufmgr_config.mbuf_high_water =
17351 DEFAULT_MB_HIGH_WATER_5906;
17352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017353
Michael Chanfdfec1722005-07-25 12:31:48 -070017354 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17355 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17356 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17357 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17358 tp->bufmgr_config.mbuf_high_water_jumbo =
17359 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17360 } else {
17361 tp->bufmgr_config.mbuf_read_dma_low_water =
17362 DEFAULT_MB_RDMA_LOW_WATER;
17363 tp->bufmgr_config.mbuf_mac_rx_low_water =
17364 DEFAULT_MB_MACRX_LOW_WATER;
17365 tp->bufmgr_config.mbuf_high_water =
17366 DEFAULT_MB_HIGH_WATER;
17367
17368 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17369 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17370 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17371 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17372 tp->bufmgr_config.mbuf_high_water_jumbo =
17373 DEFAULT_MB_HIGH_WATER_JUMBO;
17374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017375
17376 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17377 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17378}
17379
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017380static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017381{
Matt Carlson79eb6902010-02-17 15:17:03 +000017382 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17383 case TG3_PHY_ID_BCM5400: return "5400";
17384 case TG3_PHY_ID_BCM5401: return "5401";
17385 case TG3_PHY_ID_BCM5411: return "5411";
17386 case TG3_PHY_ID_BCM5701: return "5701";
17387 case TG3_PHY_ID_BCM5703: return "5703";
17388 case TG3_PHY_ID_BCM5704: return "5704";
17389 case TG3_PHY_ID_BCM5705: return "5705";
17390 case TG3_PHY_ID_BCM5750: return "5750";
17391 case TG3_PHY_ID_BCM5752: return "5752";
17392 case TG3_PHY_ID_BCM5714: return "5714";
17393 case TG3_PHY_ID_BCM5780: return "5780";
17394 case TG3_PHY_ID_BCM5755: return "5755";
17395 case TG3_PHY_ID_BCM5787: return "5787";
17396 case TG3_PHY_ID_BCM5784: return "5784";
17397 case TG3_PHY_ID_BCM5756: return "5722/5756";
17398 case TG3_PHY_ID_BCM5906: return "5906";
17399 case TG3_PHY_ID_BCM5761: return "5761";
17400 case TG3_PHY_ID_BCM5718C: return "5718C";
17401 case TG3_PHY_ID_BCM5718S: return "5718S";
17402 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017403 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017404 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017405 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017406 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017407 case 0: return "serdes";
17408 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017410}
17411
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017412static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017413{
Joe Perches63c3a662011-04-26 08:12:10 +000017414 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017415 strcpy(str, "PCI Express");
17416 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017417 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017418 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17419
17420 strcpy(str, "PCIX:");
17421
17422 if ((clock_ctrl == 7) ||
17423 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17424 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17425 strcat(str, "133MHz");
17426 else if (clock_ctrl == 0)
17427 strcat(str, "33MHz");
17428 else if (clock_ctrl == 2)
17429 strcat(str, "50MHz");
17430 else if (clock_ctrl == 4)
17431 strcat(str, "66MHz");
17432 else if (clock_ctrl == 6)
17433 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017434 } else {
17435 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017436 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017437 strcat(str, "66MHz");
17438 else
17439 strcat(str, "33MHz");
17440 }
Joe Perches63c3a662011-04-26 08:12:10 +000017441 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017442 strcat(str, ":32-bit");
17443 else
17444 strcat(str, ":64-bit");
17445 return str;
17446}
17447
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017448static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017449{
17450 struct ethtool_coalesce *ec = &tp->coal;
17451
17452 memset(ec, 0, sizeof(*ec));
17453 ec->cmd = ETHTOOL_GCOALESCE;
17454 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17455 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17456 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17457 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17458 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17459 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17460 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17461 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17462 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17463
17464 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17465 HOSTCC_MODE_CLRTICK_TXBD)) {
17466 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17467 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17468 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17469 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17470 }
Michael Chand244c892005-07-05 14:42:33 -070017471
Joe Perches63c3a662011-04-26 08:12:10 +000017472 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017473 ec->rx_coalesce_usecs_irq = 0;
17474 ec->tx_coalesce_usecs_irq = 0;
17475 ec->stats_block_coalesce_usecs = 0;
17476 }
David S. Miller15f98502005-05-18 22:49:26 -070017477}
17478
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017479static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017480 const struct pci_device_id *ent)
17481{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017482 struct net_device *dev;
17483 struct tg3 *tp;
Yijing Wang5865fc12013-06-02 21:36:21 +000017484 int i, err;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017485 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017486 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017487 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017488 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017489
Joe Perches05dbe002010-02-17 19:44:19 +000017490 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017491
17492 err = pci_enable_device(pdev);
17493 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017494 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017495 return err;
17496 }
17497
Linus Torvalds1da177e2005-04-16 15:20:36 -070017498 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17499 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017500 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017501 goto err_out_disable_pdev;
17502 }
17503
17504 pci_set_master(pdev);
17505
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017506 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017507 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017508 err = -ENOMEM;
Yijing Wang5865fc12013-06-02 21:36:21 +000017509 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017510 }
17511
Linus Torvalds1da177e2005-04-16 15:20:36 -070017512 SET_NETDEV_DEV(dev, &pdev->dev);
17513
Linus Torvalds1da177e2005-04-16 15:20:36 -070017514 tp = netdev_priv(dev);
17515 tp->pdev = pdev;
17516 tp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017517 tp->rx_mode = TG3_DEF_RX_MODE;
17518 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017519 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070017520
Linus Torvalds1da177e2005-04-16 15:20:36 -070017521 if (tg3_debug > 0)
17522 tp->msg_enable = tg3_debug;
17523 else
17524 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17525
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017526 if (pdev_is_ssb_gige_core(pdev)) {
17527 tg3_flag_set(tp, IS_SSB_CORE);
17528 if (ssb_gige_must_flush_posted_writes(pdev))
17529 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17530 if (ssb_gige_one_dma_at_once(pdev))
17531 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017532 if (ssb_gige_have_roboswitch(pdev)) {
17533 tg3_flag_set(tp, USE_PHYLIB);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017534 tg3_flag_set(tp, ROBOSWITCH);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017535 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017536 if (ssb_gige_is_rgmii(pdev))
17537 tg3_flag_set(tp, RGMII_MODE);
17538 }
17539
Linus Torvalds1da177e2005-04-16 15:20:36 -070017540 /* The word/byte swap controls here control register access byte
17541 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17542 * setting below.
17543 */
17544 tp->misc_host_ctrl =
17545 MISC_HOST_CTRL_MASK_PCI_INT |
17546 MISC_HOST_CTRL_WORD_SWAP |
17547 MISC_HOST_CTRL_INDIR_ACCESS |
17548 MISC_HOST_CTRL_PCISTATE_RW;
17549
17550 /* The NONFRM (non-frame) byte/word swap controls take effect
17551 * on descriptor entries, anything which isn't packet data.
17552 *
17553 * The StrongARM chips on the board (one for tx, one for rx)
17554 * are running in big-endian mode.
17555 */
17556 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17557 GRC_MODE_WSWAP_NONFRM_DATA);
17558#ifdef __BIG_ENDIAN
17559 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17560#endif
17561 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017562 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017563 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017564
Matt Carlsond5fe4882008-11-21 17:20:32 -080017565 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017566 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017567 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017568 err = -ENOMEM;
17569 goto err_out_free_dev;
17570 }
17571
Matt Carlsonc9cab242011-07-13 09:27:27 +000017572 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17573 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17574 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17575 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17576 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017577 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017578 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17579 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017580 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017581 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
17582 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017583 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17584 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017585 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
17586 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017587 tg3_flag_set(tp, ENABLE_APE);
17588 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17589 if (!tp->aperegs) {
17590 dev_err(&pdev->dev,
17591 "Cannot map APE registers, aborting\n");
17592 err = -ENOMEM;
17593 goto err_out_iounmap;
17594 }
17595 }
17596
Linus Torvalds1da177e2005-04-16 15:20:36 -070017597 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17598 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017599
Linus Torvalds1da177e2005-04-16 15:20:36 -070017600 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017601 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017602 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017603 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017604
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017605 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017606 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017607 dev_err(&pdev->dev,
17608 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017609 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017610 }
17611
Michael Chan4a29cc22006-03-19 13:21:12 -080017612 /* The EPB bridge inside 5714, 5715, and 5780 and any
17613 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017614 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17615 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17616 * do DMA address check in tg3_start_xmit().
17617 */
Joe Perches63c3a662011-04-26 08:12:10 +000017618 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017619 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017620 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017621 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017622#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017623 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017624#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017625 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017626 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017627
17628 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017629 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017630 err = pci_set_dma_mask(pdev, dma_mask);
17631 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017632 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017633 err = pci_set_consistent_dma_mask(pdev,
17634 persist_dma_mask);
17635 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017636 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17637 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017638 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017639 }
17640 }
17641 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017642 if (err || dma_mask == DMA_BIT_MASK(32)) {
17643 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017644 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017645 dev_err(&pdev->dev,
17646 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017647 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017648 }
17649 }
17650
Michael Chanfdfec1722005-07-25 12:31:48 -070017651 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017652
Patrick McHardyf6469682013-04-19 02:04:27 +000017653 features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlson0da06062011-05-19 12:12:53 +000017654
17655 /* 5700 B0 chips do not support checksumming correctly due
17656 * to hardware bugs.
17657 */
Joe Perches41535772013-02-16 11:20:04 +000017658 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017659 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17660
17661 if (tg3_flag(tp, 5755_PLUS))
17662 features |= NETIF_F_IPV6_CSUM;
17663 }
17664
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017665 /* TSO is on by default on chips that support hardware TSO.
17666 * Firmware TSO on older chips gives lower performance, so it
17667 * is off by default, but can be enabled using ethtool.
17668 */
Joe Perches63c3a662011-04-26 08:12:10 +000017669 if ((tg3_flag(tp, HW_TSO_1) ||
17670 tg3_flag(tp, HW_TSO_2) ||
17671 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017672 (features & NETIF_F_IP_CSUM))
17673 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017674 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017675 if (features & NETIF_F_IPV6_CSUM)
17676 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017677 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017678 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17679 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17680 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17681 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17682 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017683 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017685
Matt Carlsond542fe22011-05-19 16:02:43 +000017686 dev->features |= features;
17687 dev->vlan_features |= features;
17688
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017689 /*
17690 * Add loopback capability only for a subset of devices that support
17691 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17692 * loopback for the remaining devices.
17693 */
Joe Perches41535772013-02-16 11:20:04 +000017694 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017695 !tg3_flag(tp, CPMU_PRESENT))
17696 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017697 features |= NETIF_F_LOOPBACK;
17698
Matt Carlson0da06062011-05-19 12:12:53 +000017699 dev->hw_features |= features;
Michael Chane565eec2014-01-03 10:09:12 -080017700 dev->priv_flags |= IFF_UNICAST_FLT;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017701
Joe Perches41535772013-02-16 11:20:04 +000017702 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017703 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017704 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017705 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017706 tp->rx_pending = 63;
17707 }
17708
Linus Torvalds1da177e2005-04-16 15:20:36 -070017709 err = tg3_get_device_address(tp);
17710 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017711 dev_err(&pdev->dev,
17712 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017713 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017714 }
17715
Matt Carlsonc88864d2007-11-12 21:07:01 -080017716 /*
17717 * Reset chip in case UNDI or EFI driver did not shutdown
17718 * DMA self test will enable WDMAC and we'll see (spurious)
17719 * pending DMA on the PCI bus at that point.
17720 */
17721 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17722 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
17723 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17724 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
17725 }
17726
17727 err = tg3_test_dma(tp);
17728 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017729 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080017730 goto err_out_apeunmap;
17731 }
17732
Matt Carlson78f90dc2009-11-13 13:03:42 +000017733 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17734 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17735 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017736 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017737 struct tg3_napi *tnapi = &tp->napi[i];
17738
17739 tnapi->tp = tp;
17740 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17741
17742 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017743 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017744 intmbx += 0x8;
17745 else
17746 intmbx += 0x4;
17747
17748 tnapi->consmbox = rcvmbx;
17749 tnapi->prodmbox = sndmbx;
17750
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017751 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017752 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017753 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017754 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017755
Joe Perches63c3a662011-04-26 08:12:10 +000017756 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017757 break;
17758
17759 /*
17760 * If we support MSIX, we'll be using RSS. If we're using
17761 * RSS, the first vector only handles link interrupts and the
17762 * remaining vectors handle rx and tx interrupts. Reuse the
17763 * mailbox values for the next iteration. The values we setup
17764 * above are still useful for the single vectored mode.
17765 */
17766 if (!i)
17767 continue;
17768
17769 rcvmbx += 0x8;
17770
17771 if (sndmbx & 0x4)
17772 sndmbx -= 0x4;
17773 else
17774 sndmbx += 0xc;
17775 }
17776
Matt Carlsonc88864d2007-11-12 21:07:01 -080017777 tg3_init_coal(tp);
17778
Michael Chanc49a1562006-12-17 17:07:29 -080017779 pci_set_drvdata(pdev, dev);
17780
Joe Perches41535772013-02-16 11:20:04 +000017781 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17782 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17783 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017784 tg3_flag_set(tp, PTP_CAPABLE);
17785
Matt Carlson21f76382012-02-22 12:35:21 +000017786 tg3_timer_init(tp);
17787
Michael Chan402e1392013-02-14 12:13:41 +000017788 tg3_carrier_off(tp);
17789
Linus Torvalds1da177e2005-04-16 15:20:36 -070017790 err = register_netdev(dev);
17791 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017792 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017793 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017794 }
17795
Joe Perches05dbe002010-02-17 19:44:19 +000017796 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17797 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017798 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017799 tg3_bus_string(tp, str),
17800 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017801
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017802 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000017803 struct phy_device *phydev;
Hauke Mehrtensead24022013-09-28 23:15:26 +020017804 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson5129c3a2010-04-05 10:19:23 +000017805 netdev_info(dev,
17806 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000017807 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017808 } else {
17809 char *ethtype;
17810
17811 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17812 ethtype = "10/100Base-TX";
17813 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17814 ethtype = "1000Base-SX";
17815 else
17816 ethtype = "10/100/1000Base-T";
17817
Matt Carlson5129c3a2010-04-05 10:19:23 +000017818 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017819 "(WireSpeed[%d], EEE[%d])\n",
17820 tg3_phy_string(tp), ethtype,
17821 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17822 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017823 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017824
Joe Perches05dbe002010-02-17 19:44:19 +000017825 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017826 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017827 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017828 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017829 tg3_flag(tp, ENABLE_ASF) != 0,
17830 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017831 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17832 tp->dma_rwctrl,
17833 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17834 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017835
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017836 pci_save_state(pdev);
17837
Linus Torvalds1da177e2005-04-16 15:20:36 -070017838 return 0;
17839
Matt Carlson0d3031d2007-10-10 18:02:43 -070017840err_out_apeunmap:
17841 if (tp->aperegs) {
17842 iounmap(tp->aperegs);
17843 tp->aperegs = NULL;
17844 }
17845
Linus Torvalds1da177e2005-04-16 15:20:36 -070017846err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017847 if (tp->regs) {
17848 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017849 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017851
17852err_out_free_dev:
17853 free_netdev(dev);
17854
17855err_out_free_res:
17856 pci_release_regions(pdev);
17857
17858err_out_disable_pdev:
Gavin Shanc80dc132013-07-24 17:25:09 +080017859 if (pci_is_enabled(pdev))
17860 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017861 return err;
17862}
17863
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017864static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017865{
17866 struct net_device *dev = pci_get_drvdata(pdev);
17867
17868 if (dev) {
17869 struct tg3 *tp = netdev_priv(dev);
17870
Jesper Juhle3c55302012-04-09 22:50:15 +020017871 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017872
Matt Carlsondb219972011-11-04 09:15:03 +000017873 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017874
David S. Miller1805b2f2011-10-24 18:18:09 -040017875 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017876 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017877 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017878 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017879
Linus Torvalds1da177e2005-04-16 15:20:36 -070017880 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017881 if (tp->aperegs) {
17882 iounmap(tp->aperegs);
17883 tp->aperegs = NULL;
17884 }
Michael Chan68929142005-08-09 20:17:14 -070017885 if (tp->regs) {
17886 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017887 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017889 free_netdev(dev);
17890 pci_release_regions(pdev);
17891 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017892 }
17893}
17894
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017895#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017896static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017897{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017898 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017899 struct net_device *dev = pci_get_drvdata(pdev);
17900 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017901 int err = 0;
17902
17903 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070017904
17905 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017906 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017907
Matt Carlsondb219972011-11-04 09:15:03 +000017908 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017909 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017910 tg3_netif_stop(tp);
17911
Matt Carlson21f76382012-02-22 12:35:21 +000017912 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017913
David S. Millerf47c11e2005-06-24 20:18:35 -070017914 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017915 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017916 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017917
17918 netif_device_detach(dev);
17919
David S. Millerf47c11e2005-06-24 20:18:35 -070017920 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017921 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017922 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017923 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017924
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017925 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017926 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017927 int err2;
17928
David S. Millerf47c11e2005-06-24 20:18:35 -070017929 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017930
Joe Perches63c3a662011-04-26 08:12:10 +000017931 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017932 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017933 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017935
Matt Carlson21f76382012-02-22 12:35:21 +000017936 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017937
17938 netif_device_attach(dev);
17939 tg3_netif_start(tp);
17940
Michael Chanb9ec6c12006-07-25 16:37:27 -070017941out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017942 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017943
17944 if (!err2)
17945 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017946 }
17947
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017948unlock:
17949 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070017950 return err;
17951}
17952
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017953static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017954{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017955 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017956 struct net_device *dev = pci_get_drvdata(pdev);
17957 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017958 int err = 0;
17959
17960 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070017961
17962 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017963 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017964
Linus Torvalds1da177e2005-04-16 15:20:36 -070017965 netif_device_attach(dev);
17966
David S. Millerf47c11e2005-06-24 20:18:35 -070017967 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017968
Nithin Sujir2e460fc2013-05-23 11:11:22 +000017969 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
17970
Joe Perches63c3a662011-04-26 08:12:10 +000017971 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000017972 err = tg3_restart_hw(tp,
17973 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070017974 if (err)
17975 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017976
Matt Carlson21f76382012-02-22 12:35:21 +000017977 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017978
Linus Torvalds1da177e2005-04-16 15:20:36 -070017979 tg3_netif_start(tp);
17980
Michael Chanb9ec6c12006-07-25 16:37:27 -070017981out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017982 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017983
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017984 if (!err)
17985 tg3_phy_start(tp);
17986
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017987unlock:
17988 rtnl_unlock();
Michael Chanb9ec6c12006-07-25 16:37:27 -070017989 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017990}
Fabio Estevam42df36a2013-04-16 09:28:29 +000017991#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017992
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017993static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
17994
Nithin Sujir4c305fa2013-07-29 13:58:37 -070017995static void tg3_shutdown(struct pci_dev *pdev)
17996{
17997 struct net_device *dev = pci_get_drvdata(pdev);
17998 struct tg3 *tp = netdev_priv(dev);
17999
18000 rtnl_lock();
18001 netif_device_detach(dev);
18002
18003 if (netif_running(dev))
18004 dev_close(dev);
18005
18006 if (system_state == SYSTEM_POWER_OFF)
18007 tg3_power_down(tp);
18008
18009 rtnl_unlock();
18010}
18011
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018012/**
18013 * tg3_io_error_detected - called when PCI error is detected
18014 * @pdev: Pointer to PCI device
18015 * @state: The current pci connection state
18016 *
18017 * This function is called after a PCI bus error affecting
18018 * this device has been detected.
18019 */
18020static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
18021 pci_channel_state_t state)
18022{
18023 struct net_device *netdev = pci_get_drvdata(pdev);
18024 struct tg3 *tp = netdev_priv(netdev);
18025 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
18026
18027 netdev_info(netdev, "PCI I/O error detected\n");
18028
18029 rtnl_lock();
18030
Gavin Shand8af4df2013-07-24 17:25:08 +080018031 /* We probably don't have netdev yet */
18032 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018033 goto done;
18034
18035 tg3_phy_stop(tp);
18036
18037 tg3_netif_stop(tp);
18038
Matt Carlson21f76382012-02-22 12:35:21 +000018039 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018040
18041 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000018042 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018043
18044 netif_device_detach(netdev);
18045
18046 /* Clean up software state, even if MMIO is blocked */
18047 tg3_full_lock(tp, 0);
18048 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
18049 tg3_full_unlock(tp);
18050
18051done:
Michael Chan72bb72b2013-06-17 13:47:25 -070018052 if (state == pci_channel_io_perm_failure) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018053 if (netdev) {
18054 tg3_napi_enable(tp);
18055 dev_close(netdev);
18056 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018057 err = PCI_ERS_RESULT_DISCONNECT;
Michael Chan72bb72b2013-06-17 13:47:25 -070018058 } else {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018059 pci_disable_device(pdev);
Michael Chan72bb72b2013-06-17 13:47:25 -070018060 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018061
18062 rtnl_unlock();
18063
18064 return err;
18065}
18066
18067/**
18068 * tg3_io_slot_reset - called after the pci bus has been reset.
18069 * @pdev: Pointer to PCI device
18070 *
18071 * Restart the card from scratch, as if from a cold-boot.
18072 * At this point, the card has exprienced a hard reset,
18073 * followed by fixups by BIOS, and has its config space
18074 * set up identically to what it was at cold boot.
18075 */
18076static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
18077{
18078 struct net_device *netdev = pci_get_drvdata(pdev);
18079 struct tg3 *tp = netdev_priv(netdev);
18080 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
18081 int err;
18082
18083 rtnl_lock();
18084
18085 if (pci_enable_device(pdev)) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018086 dev_err(&pdev->dev,
18087 "Cannot re-enable PCI device after reset.\n");
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018088 goto done;
18089 }
18090
18091 pci_set_master(pdev);
18092 pci_restore_state(pdev);
18093 pci_save_state(pdev);
18094
Daniel Borkmann68293092013-08-13 11:45:13 -070018095 if (!netdev || !netif_running(netdev)) {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018096 rc = PCI_ERS_RESULT_RECOVERED;
18097 goto done;
18098 }
18099
18100 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000018101 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018102 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018103
18104 rc = PCI_ERS_RESULT_RECOVERED;
18105
18106done:
Daniel Borkmann68293092013-08-13 11:45:13 -070018107 if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
Michael Chan72bb72b2013-06-17 13:47:25 -070018108 tg3_napi_enable(tp);
18109 dev_close(netdev);
18110 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018111 rtnl_unlock();
18112
18113 return rc;
18114}
18115
18116/**
18117 * tg3_io_resume - called when traffic can start flowing again.
18118 * @pdev: Pointer to PCI device
18119 *
18120 * This callback is called when the error recovery driver tells
18121 * us that its OK to resume normal operation.
18122 */
18123static void tg3_io_resume(struct pci_dev *pdev)
18124{
18125 struct net_device *netdev = pci_get_drvdata(pdev);
18126 struct tg3 *tp = netdev_priv(netdev);
18127 int err;
18128
18129 rtnl_lock();
18130
18131 if (!netif_running(netdev))
18132 goto done;
18133
18134 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018135 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000018136 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018137 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018138 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018139 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018140 netdev_err(netdev, "Cannot restart hardware after reset.\n");
18141 goto done;
18142 }
18143
18144 netif_device_attach(netdev);
18145
Matt Carlson21f76382012-02-22 12:35:21 +000018146 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018147
18148 tg3_netif_start(tp);
18149
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018150 tg3_full_unlock(tp);
18151
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018152 tg3_phy_start(tp);
18153
18154done:
18155 rtnl_unlock();
18156}
18157
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070018158static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018159 .error_detected = tg3_io_error_detected,
18160 .slot_reset = tg3_io_slot_reset,
18161 .resume = tg3_io_resume
18162};
18163
Linus Torvalds1da177e2005-04-16 15:20:36 -070018164static struct pci_driver tg3_driver = {
18165 .name = DRV_MODULE_NAME,
18166 .id_table = tg3_pci_tbl,
18167 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018168 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018169 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000018170 .driver.pm = &tg3_pm_ops,
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018171 .shutdown = tg3_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018172};
18173
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000018174module_pci_driver(tg3_driver);