blob: f533e17cac6c73ca04657fd9d73ff5013cf2cd5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlson9e056c02012-02-13 15:20:17 +00007 * Copyright (C) 2005-2012 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Michael Chanaed93e02012-07-16 16:24:02 +000047#include <linux/hwmon.h>
48#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030051#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000055#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David S. Miller49b6e95f2007-03-29 01:38:42 -070057#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070059#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
Matt Carlson63532392008-11-03 16:49:57 -080062#define BAR_0 0
63#define BAR_2 2
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include "tg3.h"
66
Joe Perches63c3a662011-04-26 08:12:10 +000067/* Functions & macros to verify TG3_FLAGS types */
68
69static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
70{
71 return test_bit(flag, bits);
72}
73
74static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
75{
76 set_bit(flag, bits);
77}
78
79static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
80{
81 clear_bit(flag, bits);
82}
83
84#define tg3_flag(tp, flag) \
85 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
86#define tg3_flag_set(tp, flag) \
87 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
88#define tg3_flag_clear(tp, flag) \
89 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000092#define TG3_MAJ_NUM 3
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000093#define TG3_MIN_NUM 126
Matt Carlson6867c842010-07-11 09:31:44 +000094#define DRV_MODULE_VERSION \
95 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000096#define DRV_MODULE_RELDATE "November 05, 2012"
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Matt Carlsonfd6d3f02011-08-31 11:44:52 +000098#define RESET_KIND_SHUTDOWN 0
99#define RESET_KIND_INIT 1
100#define RESET_KIND_SUSPEND 2
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define TG3_DEF_RX_MODE 0
103#define TG3_DEF_TX_MODE 0
104#define TG3_DEF_MSG_ENABLE \
105 (NETIF_MSG_DRV | \
106 NETIF_MSG_PROBE | \
107 NETIF_MSG_LINK | \
108 NETIF_MSG_TIMER | \
109 NETIF_MSG_IFDOWN | \
110 NETIF_MSG_IFUP | \
111 NETIF_MSG_RX_ERR | \
112 NETIF_MSG_TX_ERR)
113
Matt Carlson520b2752011-06-13 13:39:02 +0000114#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/* length of time before we decide the hardware is borked,
117 * and dev->tx_timeout() should be called to fix the problem
118 */
Joe Perches63c3a662011-04-26 08:12:10 +0000119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#define TG3_TX_TIMEOUT (5 * HZ)
121
122/* hardware minimum and maximum for a single frame's data payload */
123#define TG3_MIN_MTU 60
124#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000125 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* These numbers seem to be hard coded in the NIC firmware somehow.
128 * You can't change the ring sizes, but you can change where you place
129 * them in the NIC onboard memory.
130 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000131#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000132 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000133 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000135#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000136 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000137 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define TG3_DEF_RX_JUMBO_RING_PENDING 100
139
140/* Do not place this n-ring entries value into the tp struct itself,
141 * we really want to expose these constants to GCC so that modulo et
142 * al. operations are done with shifts and masks instead of with
143 * hw multiply/modulo instructions. Another solution would be to
144 * replace things like '% foo' with '& (foo - 1)'.
145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147#define TG3_TX_RING_SIZE 512
148#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
149
Matt Carlson2c49a442010-09-30 10:34:35 +0000150#define TG3_RX_STD_RING_BYTES(tp) \
151 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
152#define TG3_RX_JMB_RING_BYTES(tp) \
153 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
154#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000155 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
157 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
159
Matt Carlson287be122009-08-28 13:58:46 +0000160#define TG3_DMA_BYTE_ENAB 64
161
162#define TG3_RX_STD_DMA_SZ 1536
163#define TG3_RX_JMB_DMA_SZ 9046
164
165#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
166
167#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
168#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Matt Carlson2c49a442010-09-30 10:34:35 +0000170#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
171 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000172
Matt Carlson2c49a442010-09-30 10:34:35 +0000173#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
174 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000175
Matt Carlsond2757fc2010-04-12 06:58:27 +0000176/* Due to a hardware bug, the 5701 can only DMA to memory addresses
177 * that are at least dword aligned when used in PCIX mode. The driver
178 * works around this bug by double copying the packet. This workaround
179 * is built into the normal double copy length check for efficiency.
180 *
181 * However, the double copy is only necessary on those architectures
182 * where unaligned memory accesses are inefficient. For those architectures
183 * where unaligned memory accesses incur little penalty, we can reintegrate
184 * the 5701 in the normal rx path. Doing so saves a device structure
185 * dereference by hardcoding the double copy threshold in place.
186 */
187#define TG3_RX_COPY_THRESHOLD 256
188#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
189 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
190#else
191 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
192#endif
193
Matt Carlson81389f52011-08-31 11:44:49 +0000194#if (NET_IP_ALIGN != 0)
195#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
196#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000197#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000198#endif
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000201#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000202#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000203#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Matt Carlsonad829262008-11-21 17:16:16 -0800205#define TG3_RAW_IP_ALIGN 2
206
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000207#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000208#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000209
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800210#define FIRMWARE_TG3 "tigon/tg3.bin"
211#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
212#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000215 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
218MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
219MODULE_LICENSE("GPL");
220MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800221MODULE_FIRMWARE(FIRMWARE_TG3);
222MODULE_FIRMWARE(FIRMWARE_TG3TSO);
223MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
226module_param(tg3_debug, int, 0);
227MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
228
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000229#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
230#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
231
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000232static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
252 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
253 TG3_DRV_DATA_FLAG_5705_10_100},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
255 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
256 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
259 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
260 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
266 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
272 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000280 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
281 PCI_VENDOR_ID_LENOVO,
282 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
283 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
286 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000305 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
306 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
307 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
308 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
309 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
310 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800311 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
312 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000313 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
314 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000315 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000316 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000317 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000318 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000319 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
320 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
324 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
326 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000329 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700330 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
331 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
332 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
333 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
334 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
335 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
336 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000337 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700338 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339};
340
341MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
342
Andreas Mohr50da8592006-08-14 23:54:30 -0700343static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000345} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 { "rx_octets" },
347 { "rx_fragments" },
348 { "rx_ucast_packets" },
349 { "rx_mcast_packets" },
350 { "rx_bcast_packets" },
351 { "rx_fcs_errors" },
352 { "rx_align_errors" },
353 { "rx_xon_pause_rcvd" },
354 { "rx_xoff_pause_rcvd" },
355 { "rx_mac_ctrl_rcvd" },
356 { "rx_xoff_entered" },
357 { "rx_frame_too_long_errors" },
358 { "rx_jabbers" },
359 { "rx_undersize_packets" },
360 { "rx_in_length_errors" },
361 { "rx_out_length_errors" },
362 { "rx_64_or_less_octet_packets" },
363 { "rx_65_to_127_octet_packets" },
364 { "rx_128_to_255_octet_packets" },
365 { "rx_256_to_511_octet_packets" },
366 { "rx_512_to_1023_octet_packets" },
367 { "rx_1024_to_1522_octet_packets" },
368 { "rx_1523_to_2047_octet_packets" },
369 { "rx_2048_to_4095_octet_packets" },
370 { "rx_4096_to_8191_octet_packets" },
371 { "rx_8192_to_9022_octet_packets" },
372
373 { "tx_octets" },
374 { "tx_collisions" },
375
376 { "tx_xon_sent" },
377 { "tx_xoff_sent" },
378 { "tx_flow_control" },
379 { "tx_mac_errors" },
380 { "tx_single_collisions" },
381 { "tx_mult_collisions" },
382 { "tx_deferred" },
383 { "tx_excessive_collisions" },
384 { "tx_late_collisions" },
385 { "tx_collide_2times" },
386 { "tx_collide_3times" },
387 { "tx_collide_4times" },
388 { "tx_collide_5times" },
389 { "tx_collide_6times" },
390 { "tx_collide_7times" },
391 { "tx_collide_8times" },
392 { "tx_collide_9times" },
393 { "tx_collide_10times" },
394 { "tx_collide_11times" },
395 { "tx_collide_12times" },
396 { "tx_collide_13times" },
397 { "tx_collide_14times" },
398 { "tx_collide_15times" },
399 { "tx_ucast_packets" },
400 { "tx_mcast_packets" },
401 { "tx_bcast_packets" },
402 { "tx_carrier_sense_errors" },
403 { "tx_discards" },
404 { "tx_errors" },
405
406 { "dma_writeq_full" },
407 { "dma_write_prioq_full" },
408 { "rxbds_empty" },
409 { "rx_discards" },
410 { "rx_errors" },
411 { "rx_threshold_hit" },
412
413 { "dma_readq_full" },
414 { "dma_read_prioq_full" },
415 { "tx_comp_queue_full" },
416
417 { "ring_set_send_prod_index" },
418 { "ring_status_update" },
419 { "nic_irqs" },
420 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000421 { "nic_tx_threshold_hit" },
422
423 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424};
425
Matt Carlson48fa55a2011-04-13 11:05:06 +0000426#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
427
428
Andreas Mohr50da8592006-08-14 23:54:30 -0700429static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700430 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000431} ethtool_test_keys[] = {
Matt Carlson28a45952011-08-19 13:58:22 +0000432 { "nvram test (online) " },
433 { "link test (online) " },
434 { "register test (offline)" },
435 { "memory test (offline)" },
436 { "mac loopback test (offline)" },
437 { "phy loopback test (offline)" },
Matt Carlson941ec902011-08-19 13:58:23 +0000438 { "ext loopback test (offline)" },
Matt Carlson28a45952011-08-19 13:58:22 +0000439 { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700440};
441
Matt Carlson48fa55a2011-04-13 11:05:06 +0000442#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
443
444
Michael Chanb401e9e2005-12-19 16:27:04 -0800445static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
446{
447 writel(val, tp->regs + off);
448}
449
450static u32 tg3_read32(struct tg3 *tp, u32 off)
451{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000452 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800453}
454
Matt Carlson0d3031d2007-10-10 18:02:43 -0700455static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
456{
457 writel(val, tp->aperegs + off);
458}
459
460static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
461{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000462 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
466{
Michael Chan68929142005-08-09 20:17:14 -0700467 unsigned long flags;
468
469 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700470 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
471 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700472 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700473}
474
475static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
476{
477 writel(val, tp->regs + off);
478 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Michael Chan68929142005-08-09 20:17:14 -0700481static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
482{
483 unsigned long flags;
484 u32 val;
485
486 spin_lock_irqsave(&tp->indirect_lock, flags);
487 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
488 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
489 spin_unlock_irqrestore(&tp->indirect_lock, flags);
490 return val;
491}
492
493static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
494{
495 unsigned long flags;
496
497 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
498 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
499 TG3_64BIT_REG_LOW, val);
500 return;
501 }
Matt Carlson66711e62009-11-13 13:03:49 +0000502 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700503 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
504 TG3_64BIT_REG_LOW, val);
505 return;
506 }
507
508 spin_lock_irqsave(&tp->indirect_lock, flags);
509 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
510 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
511 spin_unlock_irqrestore(&tp->indirect_lock, flags);
512
513 /* In indirect mode when disabling interrupts, we also need
514 * to clear the interrupt bit in the GRC local ctrl register.
515 */
516 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
517 (val == 0x1)) {
518 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
519 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
520 }
521}
522
523static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
524{
525 unsigned long flags;
526 u32 val;
527
528 spin_lock_irqsave(&tp->indirect_lock, flags);
529 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
530 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
531 spin_unlock_irqrestore(&tp->indirect_lock, flags);
532 return val;
533}
534
Michael Chanb401e9e2005-12-19 16:27:04 -0800535/* usec_wait specifies the wait time in usec when writing to certain registers
536 * where it is unsafe to read back the register without some delay.
537 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
538 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
539 */
540static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Joe Perches63c3a662011-04-26 08:12:10 +0000542 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800543 /* Non-posted methods */
544 tp->write32(tp, off, val);
545 else {
546 /* Posted method */
547 tg3_write32(tp, off, val);
548 if (usec_wait)
549 udelay(usec_wait);
550 tp->read32(tp, off);
551 }
552 /* Wait again after the read for the posted method to guarantee that
553 * the wait time is met.
554 */
555 if (usec_wait)
556 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Michael Chan09ee9292005-08-09 20:17:00 -0700559static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
560{
561 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000562 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700563 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700564}
565
Michael Chan20094932005-08-09 20:16:32 -0700566static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 void __iomem *mbox = tp->regs + off;
569 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000570 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000572 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 readl(mbox);
574}
575
Michael Chanb5d37722006-09-27 16:06:21 -0700576static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
577{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000578 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700579}
580
581static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
582{
583 writel(val, tp->regs + off + GRCMBOX_BASE);
584}
585
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000586#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700587#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000588#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
589#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
590#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700591
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000592#define tw32(reg, val) tp->write32(tp, reg, val)
593#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
594#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
595#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
598{
Michael Chan68929142005-08-09 20:17:14 -0700599 unsigned long flags;
600
Matt Carlson6ff6f812011-05-19 12:12:54 +0000601 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700602 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
603 return;
604
Michael Chan68929142005-08-09 20:17:14 -0700605 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000606 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700607 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
608 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Michael Chanbbadf502006-04-06 21:46:34 -0700610 /* Always leave this as zero. */
611 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
612 } else {
613 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
614 tw32_f(TG3PCI_MEM_WIN_DATA, val);
615
616 /* Always leave this as zero. */
617 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
618 }
Michael Chan68929142005-08-09 20:17:14 -0700619 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
623{
Michael Chan68929142005-08-09 20:17:14 -0700624 unsigned long flags;
625
Matt Carlson6ff6f812011-05-19 12:12:54 +0000626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700627 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
628 *val = 0;
629 return;
630 }
631
Michael Chan68929142005-08-09 20:17:14 -0700632 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000633 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700634 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
635 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Michael Chanbbadf502006-04-06 21:46:34 -0700637 /* Always leave this as zero. */
638 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
639 } else {
640 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
641 *val = tr32(TG3PCI_MEM_WIN_DATA);
642
643 /* Always leave this as zero. */
644 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
645 }
Michael Chan68929142005-08-09 20:17:14 -0700646 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Matt Carlson0d3031d2007-10-10 18:02:43 -0700649static void tg3_ape_lock_init(struct tg3 *tp)
650{
651 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000652 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000653
654 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
655 regbase = TG3_APE_LOCK_GRANT;
656 else
657 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700658
659 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000660 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
661 switch (i) {
662 case TG3_APE_LOCK_PHY0:
663 case TG3_APE_LOCK_PHY1:
664 case TG3_APE_LOCK_PHY2:
665 case TG3_APE_LOCK_PHY3:
666 bit = APE_LOCK_GRANT_DRIVER;
667 break;
668 default:
669 if (!tp->pci_fn)
670 bit = APE_LOCK_GRANT_DRIVER;
671 else
672 bit = 1 << tp->pci_fn;
673 }
674 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000675 }
676
Matt Carlson0d3031d2007-10-10 18:02:43 -0700677}
678
679static int tg3_ape_lock(struct tg3 *tp, int locknum)
680{
681 int i, off;
682 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000683 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700684
Joe Perches63c3a662011-04-26 08:12:10 +0000685 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700686 return 0;
687
688 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000689 case TG3_APE_LOCK_GPIO:
690 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
691 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000692 case TG3_APE_LOCK_GRC:
693 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000694 if (!tp->pci_fn)
695 bit = APE_LOCK_REQ_DRIVER;
696 else
697 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000698 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000699 case TG3_APE_LOCK_PHY0:
700 case TG3_APE_LOCK_PHY1:
701 case TG3_APE_LOCK_PHY2:
702 case TG3_APE_LOCK_PHY3:
703 bit = APE_LOCK_REQ_DRIVER;
704 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000705 default:
706 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700707 }
708
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000709 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
710 req = TG3_APE_LOCK_REQ;
711 gnt = TG3_APE_LOCK_GRANT;
712 } else {
713 req = TG3_APE_PER_LOCK_REQ;
714 gnt = TG3_APE_PER_LOCK_GRANT;
715 }
716
Matt Carlson0d3031d2007-10-10 18:02:43 -0700717 off = 4 * locknum;
718
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000719 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700720
721 /* Wait for up to 1 millisecond to acquire lock. */
722 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000723 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000724 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700725 break;
726 udelay(10);
727 }
728
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000729 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700730 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000731 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700732 ret = -EBUSY;
733 }
734
735 return ret;
736}
737
738static void tg3_ape_unlock(struct tg3 *tp, int locknum)
739{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000740 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700741
Joe Perches63c3a662011-04-26 08:12:10 +0000742 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700743 return;
744
745 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000746 case TG3_APE_LOCK_GPIO:
747 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
748 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000749 case TG3_APE_LOCK_GRC:
750 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000751 if (!tp->pci_fn)
752 bit = APE_LOCK_GRANT_DRIVER;
753 else
754 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000755 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000756 case TG3_APE_LOCK_PHY0:
757 case TG3_APE_LOCK_PHY1:
758 case TG3_APE_LOCK_PHY2:
759 case TG3_APE_LOCK_PHY3:
760 bit = APE_LOCK_GRANT_DRIVER;
761 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000762 default:
763 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700764 }
765
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000766 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
767 gnt = TG3_APE_LOCK_GRANT;
768 else
769 gnt = TG3_APE_PER_LOCK_GRANT;
770
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000771 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700772}
773
Matt Carlsonb65a3722012-07-16 16:24:00 +0000774static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000775{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000776 u32 apedata;
777
Matt Carlsonb65a3722012-07-16 16:24:00 +0000778 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000779 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000780 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000781
782 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000783 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
784 break;
785
Matt Carlsonb65a3722012-07-16 16:24:00 +0000786 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
787
788 udelay(10);
789 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000790 }
791
Matt Carlsonb65a3722012-07-16 16:24:00 +0000792 return timeout_us ? 0 : -EBUSY;
793}
794
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000795static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
796{
797 u32 i, apedata;
798
799 for (i = 0; i < timeout_us / 10; i++) {
800 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
801
802 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
803 break;
804
805 udelay(10);
806 }
807
808 return i == timeout_us / 10;
809}
810
Michael Chan86449942012-10-02 20:31:14 -0700811static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
812 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000813{
814 int err;
815 u32 i, bufoff, msgoff, maxlen, apedata;
816
817 if (!tg3_flag(tp, APE_HAS_NCSI))
818 return 0;
819
820 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
821 if (apedata != APE_SEG_SIG_MAGIC)
822 return -ENODEV;
823
824 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
825 if (!(apedata & APE_FW_STATUS_READY))
826 return -EAGAIN;
827
828 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
829 TG3_APE_SHMEM_BASE;
830 msgoff = bufoff + 2 * sizeof(u32);
831 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
832
833 while (len) {
834 u32 length;
835
836 /* Cap xfer sizes to scratchpad limits. */
837 length = (len > maxlen) ? maxlen : len;
838 len -= length;
839
840 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
841 if (!(apedata & APE_FW_STATUS_READY))
842 return -EAGAIN;
843
844 /* Wait for up to 1 msec for APE to service previous event. */
845 err = tg3_ape_event_lock(tp, 1000);
846 if (err)
847 return err;
848
849 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
850 APE_EVENT_STATUS_SCRTCHPD_READ |
851 APE_EVENT_STATUS_EVENT_PENDING;
852 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
853
854 tg3_ape_write32(tp, bufoff, base_off);
855 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
856
857 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
858 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
859
860 base_off += length;
861
862 if (tg3_ape_wait_for_event(tp, 30000))
863 return -EAGAIN;
864
865 for (i = 0; length; i += 4, length -= 4) {
866 u32 val = tg3_ape_read32(tp, msgoff + i);
867 memcpy(data, &val, sizeof(u32));
868 data++;
869 }
870 }
871
872 return 0;
873}
874
Matt Carlsonb65a3722012-07-16 16:24:00 +0000875static int tg3_ape_send_event(struct tg3 *tp, u32 event)
876{
877 int err;
878 u32 apedata;
879
880 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
881 if (apedata != APE_SEG_SIG_MAGIC)
882 return -EAGAIN;
883
884 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
885 if (!(apedata & APE_FW_STATUS_READY))
886 return -EAGAIN;
887
888 /* Wait for up to 1 millisecond for APE to service previous event. */
889 err = tg3_ape_event_lock(tp, 1000);
890 if (err)
891 return err;
892
893 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
894 event | APE_EVENT_STATUS_EVENT_PENDING);
895
896 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
897 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
898
899 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000900}
901
902static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
903{
904 u32 event;
905 u32 apedata;
906
907 if (!tg3_flag(tp, ENABLE_APE))
908 return;
909
910 switch (kind) {
911 case RESET_KIND_INIT:
912 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
913 APE_HOST_SEG_SIG_MAGIC);
914 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
915 APE_HOST_SEG_LEN_MAGIC);
916 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
917 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
918 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
919 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
920 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
921 APE_HOST_BEHAV_NO_PHYLOCK);
922 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
923 TG3_APE_HOST_DRVR_STATE_START);
924
925 event = APE_EVENT_STATUS_STATE_START;
926 break;
927 case RESET_KIND_SHUTDOWN:
928 /* With the interface we are currently using,
929 * APE does not track driver state. Wiping
930 * out the HOST SEGMENT SIGNATURE forces
931 * the APE to assume OS absent status.
932 */
933 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
934
935 if (device_may_wakeup(&tp->pdev->dev) &&
936 tg3_flag(tp, WOL_ENABLE)) {
937 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
938 TG3_APE_HOST_WOL_SPEED_AUTO);
939 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
940 } else
941 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
942
943 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
944
945 event = APE_EVENT_STATUS_STATE_UNLOAD;
946 break;
947 case RESET_KIND_SUSPEND:
948 event = APE_EVENT_STATUS_STATE_SUSPEND;
949 break;
950 default:
951 return;
952 }
953
954 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
955
956 tg3_ape_send_event(tp, event);
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959static void tg3_disable_ints(struct tg3 *tp)
960{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000961 int i;
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 tw32(TG3PCI_MISC_HOST_CTRL,
964 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000965 for (i = 0; i < tp->irq_max; i++)
966 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969static void tg3_enable_ints(struct tg3 *tp)
970{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000971 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000972
Michael Chanbbe832c2005-06-24 20:20:04 -0700973 tp->irq_sync = 0;
974 wmb();
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 tw32(TG3PCI_MISC_HOST_CTRL,
977 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000978
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000979 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000980 for (i = 0; i < tp->irq_cnt; i++) {
981 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000982
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000983 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000984 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000985 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
986
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000987 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000988 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000989
990 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000991 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000992 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
993 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
994 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000995 tw32(HOSTCC_MODE, tp->coal_now);
996
997 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Matt Carlson17375d22009-08-28 14:02:18 +00001000static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001001{
Matt Carlson17375d22009-08-28 14:02:18 +00001002 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001003 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001004 unsigned int work_exists = 0;
1005
1006 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001007 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001008 if (sblk->status & SD_STATUS_LINK_CHG)
1009 work_exists = 1;
1010 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001011
1012 /* check for TX work to do */
1013 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1014 work_exists = 1;
1015
1016 /* check for RX work to do */
1017 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001018 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001019 work_exists = 1;
1020
1021 return work_exists;
1022}
1023
Matt Carlson17375d22009-08-28 14:02:18 +00001024/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001025 * similar to tg3_enable_ints, but it accurately determines whether there
1026 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001027 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 */
Matt Carlson17375d22009-08-28 14:02:18 +00001029static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Matt Carlson17375d22009-08-28 14:02:18 +00001031 struct tg3 *tp = tnapi->tp;
1032
Matt Carlson898a56f2009-08-28 14:02:40 +00001033 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 mmiowb();
1035
David S. Millerfac9b832005-05-18 22:46:34 -07001036 /* When doing tagged status, this work check is unnecessary.
1037 * The last_tag we write above tells the chip which piece of
1038 * work we've completed.
1039 */
Joe Perches63c3a662011-04-26 08:12:10 +00001040 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001041 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001042 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045static void tg3_switch_clocks(struct tg3 *tp)
1046{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001047 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 u32 orig_clock_ctrl;
1049
Joe Perches63c3a662011-04-26 08:12:10 +00001050 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001051 return;
1052
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001053 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 orig_clock_ctrl = clock_ctrl;
1056 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1057 CLOCK_CTRL_CLKRUN_OENABLE |
1058 0x1f);
1059 tp->pci_clock_ctrl = clock_ctrl;
1060
Joe Perches63c3a662011-04-26 08:12:10 +00001061 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001063 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1064 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001067 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1068 clock_ctrl |
1069 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1070 40);
1071 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1072 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1073 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001075 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
1078#define PHY_BUSY_LOOPS 5000
1079
1080static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1081{
1082 u32 frame_val;
1083 unsigned int loops;
1084 int ret;
1085
1086 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1087 tw32_f(MAC_MI_MODE,
1088 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1089 udelay(80);
1090 }
1091
Michael Chan8151ad52012-07-29 19:15:41 +00001092 tg3_ape_lock(tp, tp->phy_ape_lock);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 *val = 0x0;
1095
Matt Carlson882e9792009-09-01 13:21:36 +00001096 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 MI_COM_PHY_ADDR_MASK);
1098 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1099 MI_COM_REG_ADDR_MASK);
1100 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 tw32_f(MAC_MI_COM, frame_val);
1103
1104 loops = PHY_BUSY_LOOPS;
1105 while (loops != 0) {
1106 udelay(10);
1107 frame_val = tr32(MAC_MI_COM);
1108
1109 if ((frame_val & MI_COM_BUSY) == 0) {
1110 udelay(5);
1111 frame_val = tr32(MAC_MI_COM);
1112 break;
1113 }
1114 loops -= 1;
1115 }
1116
1117 ret = -EBUSY;
1118 if (loops != 0) {
1119 *val = frame_val & MI_COM_DATA_MASK;
1120 ret = 0;
1121 }
1122
1123 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1124 tw32_f(MAC_MI_MODE, tp->mi_mode);
1125 udelay(80);
1126 }
1127
Michael Chan8151ad52012-07-29 19:15:41 +00001128 tg3_ape_unlock(tp, tp->phy_ape_lock);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return ret;
1131}
1132
1133static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1134{
1135 u32 frame_val;
1136 unsigned int loops;
1137 int ret;
1138
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001139 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001140 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001141 return 0;
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1144 tw32_f(MAC_MI_MODE,
1145 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1146 udelay(80);
1147 }
1148
Michael Chan8151ad52012-07-29 19:15:41 +00001149 tg3_ape_lock(tp, tp->phy_ape_lock);
1150
Matt Carlson882e9792009-09-01 13:21:36 +00001151 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 MI_COM_PHY_ADDR_MASK);
1153 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1154 MI_COM_REG_ADDR_MASK);
1155 frame_val |= (val & MI_COM_DATA_MASK);
1156 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 tw32_f(MAC_MI_COM, frame_val);
1159
1160 loops = PHY_BUSY_LOOPS;
1161 while (loops != 0) {
1162 udelay(10);
1163 frame_val = tr32(MAC_MI_COM);
1164 if ((frame_val & MI_COM_BUSY) == 0) {
1165 udelay(5);
1166 frame_val = tr32(MAC_MI_COM);
1167 break;
1168 }
1169 loops -= 1;
1170 }
1171
1172 ret = -EBUSY;
1173 if (loops != 0)
1174 ret = 0;
1175
1176 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1177 tw32_f(MAC_MI_MODE, tp->mi_mode);
1178 udelay(80);
1179 }
1180
Michael Chan8151ad52012-07-29 19:15:41 +00001181 tg3_ape_unlock(tp, tp->phy_ape_lock);
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return ret;
1184}
1185
Matt Carlsonb0988c12011-04-20 07:57:39 +00001186static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1187{
1188 int err;
1189
1190 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1191 if (err)
1192 goto done;
1193
1194 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1195 if (err)
1196 goto done;
1197
1198 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1199 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1200 if (err)
1201 goto done;
1202
1203 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1204
1205done:
1206 return err;
1207}
1208
1209static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1210{
1211 int err;
1212
1213 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1214 if (err)
1215 goto done;
1216
1217 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1218 if (err)
1219 goto done;
1220
1221 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1222 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1223 if (err)
1224 goto done;
1225
1226 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1227
1228done:
1229 return err;
1230}
1231
1232static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1233{
1234 int err;
1235
1236 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1237 if (!err)
1238 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1239
1240 return err;
1241}
1242
1243static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1244{
1245 int err;
1246
1247 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1248 if (!err)
1249 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1250
1251 return err;
1252}
1253
Matt Carlson15ee95c2011-04-20 07:57:40 +00001254static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1255{
1256 int err;
1257
1258 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1259 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1260 MII_TG3_AUXCTL_SHDWSEL_MISC);
1261 if (!err)
1262 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1263
1264 return err;
1265}
1266
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001267static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1268{
1269 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1270 set |= MII_TG3_AUXCTL_MISC_WREN;
1271
1272 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1273}
1274
Matt Carlson1d36ba42011-04-20 07:57:42 +00001275#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1276 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1277 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1278 MII_TG3_AUXCTL_ACTL_TX_6DB)
1279
1280#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1281 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1282 MII_TG3_AUXCTL_ACTL_TX_6DB);
1283
Matt Carlson95e28692008-05-25 23:44:14 -07001284static int tg3_bmcr_reset(struct tg3 *tp)
1285{
1286 u32 phy_control;
1287 int limit, err;
1288
1289 /* OK, reset it, and poll the BMCR_RESET bit until it
1290 * clears or we time out.
1291 */
1292 phy_control = BMCR_RESET;
1293 err = tg3_writephy(tp, MII_BMCR, phy_control);
1294 if (err != 0)
1295 return -EBUSY;
1296
1297 limit = 5000;
1298 while (limit--) {
1299 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1300 if (err != 0)
1301 return -EBUSY;
1302
1303 if ((phy_control & BMCR_RESET) == 0) {
1304 udelay(40);
1305 break;
1306 }
1307 udelay(10);
1308 }
Roel Kluind4675b52009-02-12 16:33:27 -08001309 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001310 return -EBUSY;
1311
1312 return 0;
1313}
1314
Matt Carlson158d7ab2008-05-29 01:37:54 -07001315static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1316{
Francois Romieu3d165432009-01-19 16:56:50 -08001317 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001318 u32 val;
1319
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001320 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001321
1322 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001323 val = -EIO;
1324
1325 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001326
1327 return val;
1328}
1329
1330static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1331{
Francois Romieu3d165432009-01-19 16:56:50 -08001332 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001333 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001334
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001335 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001336
1337 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001338 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001339
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001340 spin_unlock_bh(&tp->lock);
1341
1342 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001343}
1344
1345static int tg3_mdio_reset(struct mii_bus *bp)
1346{
1347 return 0;
1348}
1349
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001350static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001351{
1352 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001353 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001354
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001355 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001356 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001357 case PHY_ID_BCM50610:
1358 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001359 val = MAC_PHYCFG2_50610_LED_MODES;
1360 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001361 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001362 val = MAC_PHYCFG2_AC131_LED_MODES;
1363 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001364 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001365 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1366 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001367 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001368 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1369 break;
1370 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001371 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001372 }
1373
1374 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1375 tw32(MAC_PHYCFG2, val);
1376
1377 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001378 val &= ~(MAC_PHYCFG1_RGMII_INT |
1379 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1380 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001381 tw32(MAC_PHYCFG1, val);
1382
1383 return;
1384 }
1385
Joe Perches63c3a662011-04-26 08:12:10 +00001386 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001387 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1388 MAC_PHYCFG2_FMODE_MASK_MASK |
1389 MAC_PHYCFG2_GMODE_MASK_MASK |
1390 MAC_PHYCFG2_ACT_MASK_MASK |
1391 MAC_PHYCFG2_QUAL_MASK_MASK |
1392 MAC_PHYCFG2_INBAND_ENABLE;
1393
1394 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001395
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001396 val = tr32(MAC_PHYCFG1);
1397 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1398 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001399 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1400 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001401 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001402 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001403 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1404 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001405 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1406 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1407 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001408
Matt Carlsona9daf362008-05-25 23:49:44 -07001409 val = tr32(MAC_EXT_RGMII_MODE);
1410 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1411 MAC_RGMII_MODE_RX_QUALITY |
1412 MAC_RGMII_MODE_RX_ACTIVITY |
1413 MAC_RGMII_MODE_RX_ENG_DET |
1414 MAC_RGMII_MODE_TX_ENABLE |
1415 MAC_RGMII_MODE_TX_LOWPWR |
1416 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001417 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1418 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001419 val |= MAC_RGMII_MODE_RX_INT_B |
1420 MAC_RGMII_MODE_RX_QUALITY |
1421 MAC_RGMII_MODE_RX_ACTIVITY |
1422 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001423 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001424 val |= MAC_RGMII_MODE_TX_ENABLE |
1425 MAC_RGMII_MODE_TX_LOWPWR |
1426 MAC_RGMII_MODE_TX_RESET;
1427 }
1428 tw32(MAC_EXT_RGMII_MODE, val);
1429}
1430
Matt Carlson158d7ab2008-05-29 01:37:54 -07001431static void tg3_mdio_start(struct tg3 *tp)
1432{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001433 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1434 tw32_f(MAC_MI_MODE, tp->mi_mode);
1435 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001436
Joe Perches63c3a662011-04-26 08:12:10 +00001437 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001438 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1439 tg3_mdio_config_5785(tp);
1440}
1441
1442static int tg3_mdio_init(struct tg3 *tp)
1443{
1444 int i;
1445 u32 reg;
1446 struct phy_device *phydev;
1447
Joe Perches63c3a662011-04-26 08:12:10 +00001448 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001449 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001450
Matt Carlson69f11c92011-07-13 09:27:30 +00001451 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001452
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001453 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1454 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1455 else
1456 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1457 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001458 if (is_serdes)
1459 tp->phy_addr += 7;
1460 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001461 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001462
Matt Carlson158d7ab2008-05-29 01:37:54 -07001463 tg3_mdio_start(tp);
1464
Joe Perches63c3a662011-04-26 08:12:10 +00001465 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001466 return 0;
1467
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001468 tp->mdio_bus = mdiobus_alloc();
1469 if (tp->mdio_bus == NULL)
1470 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001471
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001472 tp->mdio_bus->name = "tg3 mdio bus";
1473 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001474 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001475 tp->mdio_bus->priv = tp;
1476 tp->mdio_bus->parent = &tp->pdev->dev;
1477 tp->mdio_bus->read = &tg3_mdio_read;
1478 tp->mdio_bus->write = &tg3_mdio_write;
1479 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001480 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001481 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001482
1483 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001484 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001485
1486 /* The bus registration will look for all the PHYs on the mdio bus.
1487 * Unfortunately, it does not ensure the PHY is powered up before
1488 * accessing the PHY ID registers. A chip reset is the
1489 * quickest way to bring the device back to an operational state..
1490 */
1491 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1492 tg3_bmcr_reset(tp);
1493
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001494 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001495 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001496 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001497 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001498 return i;
1499 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001500
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001501 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001502
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001503 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001504 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001505 mdiobus_unregister(tp->mdio_bus);
1506 mdiobus_free(tp->mdio_bus);
1507 return -ENODEV;
1508 }
1509
1510 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001511 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001512 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001513 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001514 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001515 case PHY_ID_BCM50610:
1516 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001517 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001518 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001519 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001520 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001521 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001522 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001523 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001524 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001525 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001526 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001527 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001528 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001529 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001530 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001531 case PHY_ID_RTL8201E:
1532 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001533 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001534 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001535 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001536 break;
1537 }
1538
Joe Perches63c3a662011-04-26 08:12:10 +00001539 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001540
1541 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1542 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001543
1544 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001545}
1546
1547static void tg3_mdio_fini(struct tg3 *tp)
1548{
Joe Perches63c3a662011-04-26 08:12:10 +00001549 if (tg3_flag(tp, MDIOBUS_INITED)) {
1550 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001551 mdiobus_unregister(tp->mdio_bus);
1552 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001553 }
1554}
1555
Matt Carlson95e28692008-05-25 23:44:14 -07001556/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001557static inline void tg3_generate_fw_event(struct tg3 *tp)
1558{
1559 u32 val;
1560
1561 val = tr32(GRC_RX_CPU_EVENT);
1562 val |= GRC_RX_CPU_DRIVER_EVENT;
1563 tw32_f(GRC_RX_CPU_EVENT, val);
1564
1565 tp->last_event_jiffies = jiffies;
1566}
1567
1568#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1569
1570/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001571static void tg3_wait_for_event_ack(struct tg3 *tp)
1572{
1573 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001574 unsigned int delay_cnt;
1575 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001576
Matt Carlson4ba526c2008-08-15 14:10:04 -07001577 /* If enough time has passed, no wait is necessary. */
1578 time_remain = (long)(tp->last_event_jiffies + 1 +
1579 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1580 (long)jiffies;
1581 if (time_remain < 0)
1582 return;
1583
1584 /* Check if we can shorten the wait time. */
1585 delay_cnt = jiffies_to_usecs(time_remain);
1586 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1587 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1588 delay_cnt = (delay_cnt >> 3) + 1;
1589
1590 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001591 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1592 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001593 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001594 }
1595}
1596
1597/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001598static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001599{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001600 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001601
1602 val = 0;
1603 if (!tg3_readphy(tp, MII_BMCR, &reg))
1604 val = reg << 16;
1605 if (!tg3_readphy(tp, MII_BMSR, &reg))
1606 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001607 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001608
1609 val = 0;
1610 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1611 val = reg << 16;
1612 if (!tg3_readphy(tp, MII_LPA, &reg))
1613 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001614 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001615
1616 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001617 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001618 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1619 val = reg << 16;
1620 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1621 val |= (reg & 0xffff);
1622 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001623 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001624
1625 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1626 val = reg << 16;
1627 else
1628 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001629 *data++ = val;
1630}
1631
1632/* tp->lock is held. */
1633static void tg3_ump_link_report(struct tg3 *tp)
1634{
1635 u32 data[4];
1636
1637 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1638 return;
1639
1640 tg3_phy_gather_ump_data(tp, data);
1641
1642 tg3_wait_for_event_ack(tp);
1643
1644 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1645 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1646 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1647 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1648 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1649 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001650
Matt Carlson4ba526c2008-08-15 14:10:04 -07001651 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001652}
1653
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001654/* tp->lock is held. */
1655static void tg3_stop_fw(struct tg3 *tp)
1656{
1657 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1658 /* Wait for RX cpu to ACK the previous event. */
1659 tg3_wait_for_event_ack(tp);
1660
1661 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1662
1663 tg3_generate_fw_event(tp);
1664
1665 /* Wait for RX cpu to ACK this event. */
1666 tg3_wait_for_event_ack(tp);
1667 }
1668}
1669
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001670/* tp->lock is held. */
1671static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1672{
1673 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1674 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1675
1676 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1677 switch (kind) {
1678 case RESET_KIND_INIT:
1679 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1680 DRV_STATE_START);
1681 break;
1682
1683 case RESET_KIND_SHUTDOWN:
1684 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1685 DRV_STATE_UNLOAD);
1686 break;
1687
1688 case RESET_KIND_SUSPEND:
1689 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1690 DRV_STATE_SUSPEND);
1691 break;
1692
1693 default:
1694 break;
1695 }
1696 }
1697
1698 if (kind == RESET_KIND_INIT ||
1699 kind == RESET_KIND_SUSPEND)
1700 tg3_ape_driver_state_change(tp, kind);
1701}
1702
1703/* tp->lock is held. */
1704static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1705{
1706 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1707 switch (kind) {
1708 case RESET_KIND_INIT:
1709 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1710 DRV_STATE_START_DONE);
1711 break;
1712
1713 case RESET_KIND_SHUTDOWN:
1714 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1715 DRV_STATE_UNLOAD_DONE);
1716 break;
1717
1718 default:
1719 break;
1720 }
1721 }
1722
1723 if (kind == RESET_KIND_SHUTDOWN)
1724 tg3_ape_driver_state_change(tp, kind);
1725}
1726
1727/* tp->lock is held. */
1728static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1729{
1730 if (tg3_flag(tp, ENABLE_ASF)) {
1731 switch (kind) {
1732 case RESET_KIND_INIT:
1733 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1734 DRV_STATE_START);
1735 break;
1736
1737 case RESET_KIND_SHUTDOWN:
1738 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1739 DRV_STATE_UNLOAD);
1740 break;
1741
1742 case RESET_KIND_SUSPEND:
1743 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1744 DRV_STATE_SUSPEND);
1745 break;
1746
1747 default:
1748 break;
1749 }
1750 }
1751}
1752
1753static int tg3_poll_fw(struct tg3 *tp)
1754{
1755 int i;
1756 u32 val;
1757
1758 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1759 /* Wait up to 20ms for init done. */
1760 for (i = 0; i < 200; i++) {
1761 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1762 return 0;
1763 udelay(100);
1764 }
1765 return -ENODEV;
1766 }
1767
1768 /* Wait for firmware initialization to complete. */
1769 for (i = 0; i < 100000; i++) {
1770 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1771 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1772 break;
1773 udelay(10);
1774 }
1775
1776 /* Chip might not be fitted with firmware. Some Sun onboard
1777 * parts are configured like that. So don't signal the timeout
1778 * of the above loop as an error, but do report the lack of
1779 * running firmware once.
1780 */
1781 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1782 tg3_flag_set(tp, NO_FWARE_REPORTED);
1783
1784 netdev_info(tp->dev, "No firmware running\n");
1785 }
1786
1787 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1788 /* The 57765 A0 needs a little more
1789 * time to do some important work.
1790 */
1791 mdelay(10);
1792 }
1793
1794 return 0;
1795}
1796
Matt Carlson95e28692008-05-25 23:44:14 -07001797static void tg3_link_report(struct tg3 *tp)
1798{
1799 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001800 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001801 tg3_ump_link_report(tp);
1802 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001803 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1804 (tp->link_config.active_speed == SPEED_1000 ?
1805 1000 :
1806 (tp->link_config.active_speed == SPEED_100 ?
1807 100 : 10)),
1808 (tp->link_config.active_duplex == DUPLEX_FULL ?
1809 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001810
Joe Perches05dbe002010-02-17 19:44:19 +00001811 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1812 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1813 "on" : "off",
1814 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1815 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001816
1817 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1818 netdev_info(tp->dev, "EEE is %s\n",
1819 tp->setlpicnt ? "enabled" : "disabled");
1820
Matt Carlson95e28692008-05-25 23:44:14 -07001821 tg3_ump_link_report(tp);
1822 }
1823}
1824
Matt Carlson95e28692008-05-25 23:44:14 -07001825static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1826{
1827 u16 miireg;
1828
Steve Glendinninge18ce342008-12-16 02:00:00 -08001829 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001830 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001831 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001832 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001833 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001834 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1835 else
1836 miireg = 0;
1837
1838 return miireg;
1839}
1840
Matt Carlson95e28692008-05-25 23:44:14 -07001841static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1842{
1843 u8 cap = 0;
1844
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001845 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1846 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1847 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1848 if (lcladv & ADVERTISE_1000XPAUSE)
1849 cap = FLOW_CTRL_RX;
1850 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001851 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001852 }
1853
1854 return cap;
1855}
1856
Matt Carlsonf51f3562008-05-25 23:45:08 -07001857static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001858{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001859 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001860 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001861 u32 old_rx_mode = tp->rx_mode;
1862 u32 old_tx_mode = tp->tx_mode;
1863
Joe Perches63c3a662011-04-26 08:12:10 +00001864 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001865 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001866 else
1867 autoneg = tp->link_config.autoneg;
1868
Joe Perches63c3a662011-04-26 08:12:10 +00001869 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001870 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001871 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001872 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001873 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001874 } else
1875 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001876
Matt Carlsonf51f3562008-05-25 23:45:08 -07001877 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001878
Steve Glendinninge18ce342008-12-16 02:00:00 -08001879 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001880 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1881 else
1882 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1883
Matt Carlsonf51f3562008-05-25 23:45:08 -07001884 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001885 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001886
Steve Glendinninge18ce342008-12-16 02:00:00 -08001887 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001888 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1889 else
1890 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1891
Matt Carlsonf51f3562008-05-25 23:45:08 -07001892 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001893 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001894}
1895
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001896static void tg3_adjust_link(struct net_device *dev)
1897{
1898 u8 oldflowctrl, linkmesg = 0;
1899 u32 mac_mode, lcl_adv, rmt_adv;
1900 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001901 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001902
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001903 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001904
1905 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1906 MAC_MODE_HALF_DUPLEX);
1907
1908 oldflowctrl = tp->link_config.active_flowctrl;
1909
1910 if (phydev->link) {
1911 lcl_adv = 0;
1912 rmt_adv = 0;
1913
1914 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1915 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001916 else if (phydev->speed == SPEED_1000 ||
1917 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001918 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001919 else
1920 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001921
1922 if (phydev->duplex == DUPLEX_HALF)
1923 mac_mode |= MAC_MODE_HALF_DUPLEX;
1924 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001925 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001926 tp->link_config.flowctrl);
1927
1928 if (phydev->pause)
1929 rmt_adv = LPA_PAUSE_CAP;
1930 if (phydev->asym_pause)
1931 rmt_adv |= LPA_PAUSE_ASYM;
1932 }
1933
1934 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1935 } else
1936 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1937
1938 if (mac_mode != tp->mac_mode) {
1939 tp->mac_mode = mac_mode;
1940 tw32_f(MAC_MODE, tp->mac_mode);
1941 udelay(40);
1942 }
1943
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001944 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1945 if (phydev->speed == SPEED_10)
1946 tw32(MAC_MI_STAT,
1947 MAC_MI_STAT_10MBPS_MODE |
1948 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1949 else
1950 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1951 }
1952
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001953 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1954 tw32(MAC_TX_LENGTHS,
1955 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1956 (6 << TX_LENGTHS_IPG_SHIFT) |
1957 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1958 else
1959 tw32(MAC_TX_LENGTHS,
1960 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1961 (6 << TX_LENGTHS_IPG_SHIFT) |
1962 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1963
Matt Carlson34655ad2012-02-22 12:35:18 +00001964 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001965 phydev->speed != tp->link_config.active_speed ||
1966 phydev->duplex != tp->link_config.active_duplex ||
1967 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001968 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001969
Matt Carlson34655ad2012-02-22 12:35:18 +00001970 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001971 tp->link_config.active_speed = phydev->speed;
1972 tp->link_config.active_duplex = phydev->duplex;
1973
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001974 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001975
1976 if (linkmesg)
1977 tg3_link_report(tp);
1978}
1979
1980static int tg3_phy_init(struct tg3 *tp)
1981{
1982 struct phy_device *phydev;
1983
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001984 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001985 return 0;
1986
1987 /* Bring the PHY back to a known state. */
1988 tg3_bmcr_reset(tp);
1989
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001990 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001991
1992 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001993 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001994 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001995 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001996 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001997 return PTR_ERR(phydev);
1998 }
1999
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002000 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002001 switch (phydev->interface) {
2002 case PHY_INTERFACE_MODE_GMII:
2003 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002004 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002005 phydev->supported &= (PHY_GBIT_FEATURES |
2006 SUPPORTED_Pause |
2007 SUPPORTED_Asym_Pause);
2008 break;
2009 }
2010 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002011 case PHY_INTERFACE_MODE_MII:
2012 phydev->supported &= (PHY_BASIC_FEATURES |
2013 SUPPORTED_Pause |
2014 SUPPORTED_Asym_Pause);
2015 break;
2016 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002017 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002018 return -EINVAL;
2019 }
2020
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002021 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002022
2023 phydev->advertising = phydev->supported;
2024
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002025 return 0;
2026}
2027
2028static void tg3_phy_start(struct tg3 *tp)
2029{
2030 struct phy_device *phydev;
2031
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002032 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002033 return;
2034
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002035 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002036
Matt Carlson800960682010-08-02 11:26:06 +00002037 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2038 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002039 phydev->speed = tp->link_config.speed;
2040 phydev->duplex = tp->link_config.duplex;
2041 phydev->autoneg = tp->link_config.autoneg;
2042 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002043 }
2044
2045 phy_start(phydev);
2046
2047 phy_start_aneg(phydev);
2048}
2049
2050static void tg3_phy_stop(struct tg3 *tp)
2051{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002052 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002053 return;
2054
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002055 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002056}
2057
2058static void tg3_phy_fini(struct tg3 *tp)
2059{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002060 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002061 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002062 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002063 }
2064}
2065
Matt Carlson941ec902011-08-19 13:58:23 +00002066static int tg3_phy_set_extloopbk(struct tg3 *tp)
2067{
2068 int err;
2069 u32 val;
2070
2071 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2072 return 0;
2073
2074 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2075 /* Cannot do read-modify-write on 5401 */
2076 err = tg3_phy_auxctl_write(tp,
2077 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2078 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2079 0x4c20);
2080 goto done;
2081 }
2082
2083 err = tg3_phy_auxctl_read(tp,
2084 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2085 if (err)
2086 return err;
2087
2088 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2089 err = tg3_phy_auxctl_write(tp,
2090 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2091
2092done:
2093 return err;
2094}
2095
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002096static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2097{
2098 u32 phytest;
2099
2100 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2101 u32 phy;
2102
2103 tg3_writephy(tp, MII_TG3_FET_TEST,
2104 phytest | MII_TG3_FET_SHADOW_EN);
2105 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2106 if (enable)
2107 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2108 else
2109 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2110 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2111 }
2112 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2113 }
2114}
2115
Matt Carlson6833c042008-11-21 17:18:59 -08002116static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2117{
2118 u32 reg;
2119
Joe Perches63c3a662011-04-26 08:12:10 +00002120 if (!tg3_flag(tp, 5705_PLUS) ||
2121 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002122 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002123 return;
2124
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002125 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002126 tg3_phy_fet_toggle_apd(tp, enable);
2127 return;
2128 }
2129
Matt Carlson6833c042008-11-21 17:18:59 -08002130 reg = MII_TG3_MISC_SHDW_WREN |
2131 MII_TG3_MISC_SHDW_SCR5_SEL |
2132 MII_TG3_MISC_SHDW_SCR5_LPED |
2133 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2134 MII_TG3_MISC_SHDW_SCR5_SDTL |
2135 MII_TG3_MISC_SHDW_SCR5_C125OE;
2136 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2137 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2138
2139 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2140
2141
2142 reg = MII_TG3_MISC_SHDW_WREN |
2143 MII_TG3_MISC_SHDW_APD_SEL |
2144 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2145 if (enable)
2146 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2147
2148 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2149}
2150
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002151static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2152{
2153 u32 phy;
2154
Joe Perches63c3a662011-04-26 08:12:10 +00002155 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002156 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002157 return;
2158
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002159 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002160 u32 ephy;
2161
Matt Carlson535ef6e2009-08-25 10:09:36 +00002162 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2163 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2164
2165 tg3_writephy(tp, MII_TG3_FET_TEST,
2166 ephy | MII_TG3_FET_SHADOW_EN);
2167 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002168 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002169 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002170 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002171 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2172 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002173 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002174 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002175 }
2176 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002177 int ret;
2178
2179 ret = tg3_phy_auxctl_read(tp,
2180 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2181 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002182 if (enable)
2183 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2184 else
2185 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002186 tg3_phy_auxctl_write(tp,
2187 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002188 }
2189 }
2190}
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192static void tg3_phy_set_wirespeed(struct tg3 *tp)
2193{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002194 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 u32 val;
2196
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002197 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return;
2199
Matt Carlson15ee95c2011-04-20 07:57:40 +00002200 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2201 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002202 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2203 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002206static void tg3_phy_apply_otp(struct tg3 *tp)
2207{
2208 u32 otp, phy;
2209
2210 if (!tp->phy_otp)
2211 return;
2212
2213 otp = tp->phy_otp;
2214
Matt Carlson1d36ba42011-04-20 07:57:42 +00002215 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2216 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002217
2218 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2219 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2220 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2221
2222 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2223 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2224 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2225
2226 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2227 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2228 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2229
2230 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2231 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2232
2233 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2234 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2235
2236 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2237 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2238 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2239
Matt Carlson1d36ba42011-04-20 07:57:42 +00002240 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002241}
2242
Matt Carlson52b02d02010-10-14 10:37:41 +00002243static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2244{
2245 u32 val;
2246
2247 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2248 return;
2249
2250 tp->setlpicnt = 0;
2251
2252 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2253 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002254 tp->link_config.active_duplex == DUPLEX_FULL &&
2255 (tp->link_config.active_speed == SPEED_100 ||
2256 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002257 u32 eeectl;
2258
2259 if (tp->link_config.active_speed == SPEED_1000)
2260 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2261 else
2262 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2263
2264 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2265
Matt Carlson3110f5f52010-12-06 08:28:50 +00002266 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2267 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002268
Matt Carlsonb0c59432011-05-19 12:12:48 +00002269 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2270 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002271 tp->setlpicnt = 2;
2272 }
2273
2274 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002275 if (current_link_up == 1 &&
2276 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2277 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2278 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2279 }
2280
Matt Carlson52b02d02010-10-14 10:37:41 +00002281 val = tr32(TG3_CPMU_EEE_MODE);
2282 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2283 }
2284}
2285
Matt Carlsonb0c59432011-05-19 12:12:48 +00002286static void tg3_phy_eee_enable(struct tg3 *tp)
2287{
2288 u32 val;
2289
2290 if (tp->link_config.active_speed == SPEED_1000 &&
2291 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2292 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002293 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002294 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002295 val = MII_TG3_DSP_TAP26_ALNOKO |
2296 MII_TG3_DSP_TAP26_RMRXSTO;
2297 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002298 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2299 }
2300
2301 val = tr32(TG3_CPMU_EEE_MODE);
2302 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2303}
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305static int tg3_wait_macro_done(struct tg3 *tp)
2306{
2307 int limit = 100;
2308
2309 while (limit--) {
2310 u32 tmp32;
2311
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002312 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if ((tmp32 & 0x1000) == 0)
2314 break;
2315 }
2316 }
Roel Kluind4675b52009-02-12 16:33:27 -08002317 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return -EBUSY;
2319
2320 return 0;
2321}
2322
2323static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2324{
2325 static const u32 test_pat[4][6] = {
2326 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2327 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2328 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2329 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2330 };
2331 int chan;
2332
2333 for (chan = 0; chan < 4; chan++) {
2334 int i;
2335
2336 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2337 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002338 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 for (i = 0; i < 6; i++)
2341 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2342 test_pat[chan][i]);
2343
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002344 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (tg3_wait_macro_done(tp)) {
2346 *resetp = 1;
2347 return -EBUSY;
2348 }
2349
2350 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2351 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002352 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (tg3_wait_macro_done(tp)) {
2354 *resetp = 1;
2355 return -EBUSY;
2356 }
2357
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002358 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (tg3_wait_macro_done(tp)) {
2360 *resetp = 1;
2361 return -EBUSY;
2362 }
2363
2364 for (i = 0; i < 6; i += 2) {
2365 u32 low, high;
2366
2367 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2368 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2369 tg3_wait_macro_done(tp)) {
2370 *resetp = 1;
2371 return -EBUSY;
2372 }
2373 low &= 0x7fff;
2374 high &= 0x000f;
2375 if (low != test_pat[chan][i] ||
2376 high != test_pat[chan][i+1]) {
2377 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2378 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2379 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2380
2381 return -EBUSY;
2382 }
2383 }
2384 }
2385
2386 return 0;
2387}
2388
2389static int tg3_phy_reset_chanpat(struct tg3 *tp)
2390{
2391 int chan;
2392
2393 for (chan = 0; chan < 4; chan++) {
2394 int i;
2395
2396 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2397 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002398 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 for (i = 0; i < 6; i++)
2400 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002401 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (tg3_wait_macro_done(tp))
2403 return -EBUSY;
2404 }
2405
2406 return 0;
2407}
2408
2409static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2410{
2411 u32 reg32, phy9_orig;
2412 int retries, do_phy_reset, err;
2413
2414 retries = 10;
2415 do_phy_reset = 1;
2416 do {
2417 if (do_phy_reset) {
2418 err = tg3_bmcr_reset(tp);
2419 if (err)
2420 return err;
2421 do_phy_reset = 0;
2422 }
2423
2424 /* Disable transmitter and interrupt. */
2425 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2426 continue;
2427
2428 reg32 |= 0x3000;
2429 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2430
2431 /* Set full-duplex, 1000 mbps. */
2432 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002433 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002436 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 continue;
2438
Matt Carlson221c5632011-06-13 13:39:01 +00002439 tg3_writephy(tp, MII_CTRL1000,
2440 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Matt Carlson1d36ba42011-04-20 07:57:42 +00002442 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2443 if (err)
2444 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002447 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2450 if (!err)
2451 break;
2452 } while (--retries);
2453
2454 err = tg3_phy_reset_chanpat(tp);
2455 if (err)
2456 return err;
2457
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002458 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002461 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Matt Carlson1d36ba42011-04-20 07:57:42 +00002463 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Matt Carlson221c5632011-06-13 13:39:01 +00002465 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2468 reg32 &= ~0x3000;
2469 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2470 } else if (!err)
2471 err = -EBUSY;
2472
2473 return err;
2474}
2475
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002476static void tg3_carrier_on(struct tg3 *tp)
2477{
2478 netif_carrier_on(tp->dev);
2479 tp->link_up = true;
2480}
2481
2482static void tg3_carrier_off(struct tg3 *tp)
2483{
2484 netif_carrier_off(tp->dev);
2485 tp->link_up = false;
2486}
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488/* This will reset the tigon3 PHY if there is no valid
2489 * link unless the FORCE argument is non-zero.
2490 */
2491static int tg3_phy_reset(struct tg3 *tp)
2492{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002493 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 int err;
2495
Michael Chan60189dd2006-12-17 17:08:07 -08002496 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002497 val = tr32(GRC_MISC_CFG);
2498 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2499 udelay(40);
2500 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002501 err = tg3_readphy(tp, MII_BMSR, &val);
2502 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (err != 0)
2504 return -EBUSY;
2505
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002506 if (netif_running(tp->dev) && tp->link_up) {
2507 tg3_carrier_off(tp);
Michael Chanc8e1e822006-04-29 18:55:17 -07002508 tg3_link_report(tp);
2509 }
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2512 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2513 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2514 err = tg3_phy_reset_5703_4_5(tp);
2515 if (err)
2516 return err;
2517 goto out;
2518 }
2519
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002520 cpmuctrl = 0;
2521 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2522 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2523 cpmuctrl = tr32(TG3_CPMU_CTRL);
2524 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2525 tw32(TG3_CPMU_CTRL,
2526 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2527 }
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 err = tg3_bmcr_reset(tp);
2530 if (err)
2531 return err;
2532
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002533 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002534 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2535 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002536
2537 tw32(TG3_CPMU_CTRL, cpmuctrl);
2538 }
2539
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002540 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2541 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002542 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2543 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2544 CPMU_LSPD_1000MB_MACCLK_12_5) {
2545 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2546 udelay(40);
2547 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2548 }
2549 }
2550
Joe Perches63c3a662011-04-26 08:12:10 +00002551 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002552 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002553 return 0;
2554
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002555 tg3_phy_apply_otp(tp);
2556
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002557 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002558 tg3_phy_toggle_apd(tp, true);
2559 else
2560 tg3_phy_toggle_apd(tp, false);
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002563 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2564 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002565 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2566 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002567 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002569
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002570 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002571 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2572 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002574
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002575 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002576 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2577 tg3_phydsp_write(tp, 0x000a, 0x310b);
2578 tg3_phydsp_write(tp, 0x201f, 0x9506);
2579 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2580 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2581 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002582 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002583 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2584 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2585 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2586 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2587 tg3_writephy(tp, MII_TG3_TEST1,
2588 MII_TG3_TEST1_TRIM_EN | 0x4);
2589 } else
2590 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2591
2592 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2593 }
Michael Chanc424cb22006-04-29 18:56:34 -07002594 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 /* Set Extended packet length bit (bit 14) on all chips that */
2597 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002598 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002600 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002601 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002603 err = tg3_phy_auxctl_read(tp,
2604 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2605 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002606 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2607 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
2609
2610 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2611 * jumbo frames transmission.
2612 */
Joe Perches63c3a662011-04-26 08:12:10 +00002613 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002614 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002615 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002616 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618
Michael Chan715116a2006-09-27 16:09:25 -07002619 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002620 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002621 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002622 }
2623
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002624 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 tg3_phy_set_wirespeed(tp);
2626 return 0;
2627}
2628
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002629#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2630#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2631#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2632 TG3_GPIO_MSG_NEED_VAUX)
2633#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2634 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2635 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2636 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2637 (TG3_GPIO_MSG_DRVR_PRES << 12))
2638
2639#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2640 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2641 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2642 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2643 (TG3_GPIO_MSG_NEED_VAUX << 12))
2644
2645static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2646{
2647 u32 status, shift;
2648
2649 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2650 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2651 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2652 else
2653 status = tr32(TG3_CPMU_DRV_STATUS);
2654
2655 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2656 status &= ~(TG3_GPIO_MSG_MASK << shift);
2657 status |= (newstat << shift);
2658
2659 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2660 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2661 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2662 else
2663 tw32(TG3_CPMU_DRV_STATUS, status);
2664
2665 return status >> TG3_APE_GPIO_MSG_SHIFT;
2666}
2667
Matt Carlson520b2752011-06-13 13:39:02 +00002668static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2669{
2670 if (!tg3_flag(tp, IS_NIC))
2671 return 0;
2672
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002673 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2674 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2675 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2676 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2677 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002678
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002679 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2680
2681 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2682 TG3_GRC_LCLCTL_PWRSW_DELAY);
2683
2684 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2685 } else {
2686 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2687 TG3_GRC_LCLCTL_PWRSW_DELAY);
2688 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002689
Matt Carlson520b2752011-06-13 13:39:02 +00002690 return 0;
2691}
2692
2693static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2694{
2695 u32 grc_local_ctrl;
2696
2697 if (!tg3_flag(tp, IS_NIC) ||
2698 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2699 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2700 return;
2701
2702 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2703
2704 tw32_wait_f(GRC_LOCAL_CTRL,
2705 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2706 TG3_GRC_LCLCTL_PWRSW_DELAY);
2707
2708 tw32_wait_f(GRC_LOCAL_CTRL,
2709 grc_local_ctrl,
2710 TG3_GRC_LCLCTL_PWRSW_DELAY);
2711
2712 tw32_wait_f(GRC_LOCAL_CTRL,
2713 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2714 TG3_GRC_LCLCTL_PWRSW_DELAY);
2715}
2716
2717static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2718{
2719 if (!tg3_flag(tp, IS_NIC))
2720 return;
2721
2722 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2723 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2724 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2725 (GRC_LCLCTRL_GPIO_OE0 |
2726 GRC_LCLCTRL_GPIO_OE1 |
2727 GRC_LCLCTRL_GPIO_OE2 |
2728 GRC_LCLCTRL_GPIO_OUTPUT0 |
2729 GRC_LCLCTRL_GPIO_OUTPUT1),
2730 TG3_GRC_LCLCTL_PWRSW_DELAY);
2731 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2732 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2733 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2734 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2735 GRC_LCLCTRL_GPIO_OE1 |
2736 GRC_LCLCTRL_GPIO_OE2 |
2737 GRC_LCLCTRL_GPIO_OUTPUT0 |
2738 GRC_LCLCTRL_GPIO_OUTPUT1 |
2739 tp->grc_local_ctrl;
2740 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2741 TG3_GRC_LCLCTL_PWRSW_DELAY);
2742
2743 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2744 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2745 TG3_GRC_LCLCTL_PWRSW_DELAY);
2746
2747 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2748 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2749 TG3_GRC_LCLCTL_PWRSW_DELAY);
2750 } else {
2751 u32 no_gpio2;
2752 u32 grc_local_ctrl = 0;
2753
2754 /* Workaround to prevent overdrawing Amps. */
2755 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2756 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2757 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2758 grc_local_ctrl,
2759 TG3_GRC_LCLCTL_PWRSW_DELAY);
2760 }
2761
2762 /* On 5753 and variants, GPIO2 cannot be used. */
2763 no_gpio2 = tp->nic_sram_data_cfg &
2764 NIC_SRAM_DATA_CFG_NO_GPIO2;
2765
2766 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2767 GRC_LCLCTRL_GPIO_OE1 |
2768 GRC_LCLCTRL_GPIO_OE2 |
2769 GRC_LCLCTRL_GPIO_OUTPUT1 |
2770 GRC_LCLCTRL_GPIO_OUTPUT2;
2771 if (no_gpio2) {
2772 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2773 GRC_LCLCTRL_GPIO_OUTPUT2);
2774 }
2775 tw32_wait_f(GRC_LOCAL_CTRL,
2776 tp->grc_local_ctrl | grc_local_ctrl,
2777 TG3_GRC_LCLCTL_PWRSW_DELAY);
2778
2779 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2780
2781 tw32_wait_f(GRC_LOCAL_CTRL,
2782 tp->grc_local_ctrl | grc_local_ctrl,
2783 TG3_GRC_LCLCTL_PWRSW_DELAY);
2784
2785 if (!no_gpio2) {
2786 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2787 tw32_wait_f(GRC_LOCAL_CTRL,
2788 tp->grc_local_ctrl | grc_local_ctrl,
2789 TG3_GRC_LCLCTL_PWRSW_DELAY);
2790 }
2791 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002792}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002793
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002794static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002795{
2796 u32 msg = 0;
2797
2798 /* Serialize power state transitions */
2799 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2800 return;
2801
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002802 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002803 msg = TG3_GPIO_MSG_NEED_VAUX;
2804
2805 msg = tg3_set_function_status(tp, msg);
2806
2807 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2808 goto done;
2809
2810 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2811 tg3_pwrsrc_switch_to_vaux(tp);
2812 else
2813 tg3_pwrsrc_die_with_vmain(tp);
2814
2815done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002816 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002817}
2818
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002819static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Matt Carlson683644b2011-03-09 16:58:23 +00002821 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Matt Carlson334355a2010-01-20 16:58:10 +00002823 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002824 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return;
2826
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002827 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2828 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2829 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002830 tg3_frob_aux_power_5717(tp, include_wol ?
2831 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002832 return;
2833 }
2834
2835 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002836 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002838 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002839
Michael Chanbc1c7562006-03-20 17:48:03 -08002840 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002841 if (dev_peer) {
2842 struct tg3 *tp_peer = netdev_priv(dev_peer);
2843
Joe Perches63c3a662011-04-26 08:12:10 +00002844 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002845 return;
2846
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002847 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002848 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002849 need_vaux = true;
2850 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002853 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2854 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002855 need_vaux = true;
2856
Matt Carlson520b2752011-06-13 13:39:02 +00002857 if (need_vaux)
2858 tg3_pwrsrc_switch_to_vaux(tp);
2859 else
2860 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861}
2862
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002863static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2864{
2865 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2866 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002867 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002868 if (speed != SPEED_10)
2869 return 1;
2870 } else if (speed == SPEED_10)
2871 return 1;
2872
2873 return 0;
2874}
2875
Matt Carlson0a459aa2008-11-03 16:54:15 -08002876static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002877{
Matt Carlsonce057f02007-11-12 21:08:03 -08002878 u32 val;
2879
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002880 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002881 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2882 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2883 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2884
2885 sg_dig_ctrl |=
2886 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2887 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2888 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2889 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002890 return;
Michael Chan51297242007-02-13 12:17:57 -08002891 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002892
Michael Chan60189dd2006-12-17 17:08:07 -08002893 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002894 tg3_bmcr_reset(tp);
2895 val = tr32(GRC_MISC_CFG);
2896 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2897 udelay(40);
2898 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002899 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002900 u32 phytest;
2901 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2902 u32 phy;
2903
2904 tg3_writephy(tp, MII_ADVERTISE, 0);
2905 tg3_writephy(tp, MII_BMCR,
2906 BMCR_ANENABLE | BMCR_ANRESTART);
2907
2908 tg3_writephy(tp, MII_TG3_FET_TEST,
2909 phytest | MII_TG3_FET_SHADOW_EN);
2910 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2911 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2912 tg3_writephy(tp,
2913 MII_TG3_FET_SHDW_AUXMODE4,
2914 phy);
2915 }
2916 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2917 }
2918 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002919 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002920 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2921 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002922
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002923 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2924 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2925 MII_TG3_AUXCTL_PCTL_VREG_11V;
2926 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002927 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002928
Michael Chan15c3b692006-03-22 01:06:52 -08002929 /* The PHY should not be powered down on some chips because
2930 * of bugs.
2931 */
2932 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2933 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2934 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002935 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
2936 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
2937 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002938 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002939
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002940 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2941 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002942 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2943 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2944 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2945 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2946 }
2947
Michael Chan15c3b692006-03-22 01:06:52 -08002948 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2949}
2950
Matt Carlson3f007892008-11-03 16:51:36 -08002951/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002952static int tg3_nvram_lock(struct tg3 *tp)
2953{
Joe Perches63c3a662011-04-26 08:12:10 +00002954 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002955 int i;
2956
2957 if (tp->nvram_lock_cnt == 0) {
2958 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2959 for (i = 0; i < 8000; i++) {
2960 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2961 break;
2962 udelay(20);
2963 }
2964 if (i == 8000) {
2965 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2966 return -ENODEV;
2967 }
2968 }
2969 tp->nvram_lock_cnt++;
2970 }
2971 return 0;
2972}
2973
2974/* tp->lock is held. */
2975static void tg3_nvram_unlock(struct tg3 *tp)
2976{
Joe Perches63c3a662011-04-26 08:12:10 +00002977 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002978 if (tp->nvram_lock_cnt > 0)
2979 tp->nvram_lock_cnt--;
2980 if (tp->nvram_lock_cnt == 0)
2981 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2982 }
2983}
2984
2985/* tp->lock is held. */
2986static void tg3_enable_nvram_access(struct tg3 *tp)
2987{
Joe Perches63c3a662011-04-26 08:12:10 +00002988 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002989 u32 nvaccess = tr32(NVRAM_ACCESS);
2990
2991 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2992 }
2993}
2994
2995/* tp->lock is held. */
2996static void tg3_disable_nvram_access(struct tg3 *tp)
2997{
Joe Perches63c3a662011-04-26 08:12:10 +00002998 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002999 u32 nvaccess = tr32(NVRAM_ACCESS);
3000
3001 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3002 }
3003}
3004
3005static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3006 u32 offset, u32 *val)
3007{
3008 u32 tmp;
3009 int i;
3010
3011 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3012 return -EINVAL;
3013
3014 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3015 EEPROM_ADDR_DEVID_MASK |
3016 EEPROM_ADDR_READ);
3017 tw32(GRC_EEPROM_ADDR,
3018 tmp |
3019 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3020 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3021 EEPROM_ADDR_ADDR_MASK) |
3022 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3023
3024 for (i = 0; i < 1000; i++) {
3025 tmp = tr32(GRC_EEPROM_ADDR);
3026
3027 if (tmp & EEPROM_ADDR_COMPLETE)
3028 break;
3029 msleep(1);
3030 }
3031 if (!(tmp & EEPROM_ADDR_COMPLETE))
3032 return -EBUSY;
3033
Matt Carlson62cedd12009-04-20 14:52:29 -07003034 tmp = tr32(GRC_EEPROM_DATA);
3035
3036 /*
3037 * The data will always be opposite the native endian
3038 * format. Perform a blind byteswap to compensate.
3039 */
3040 *val = swab32(tmp);
3041
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003042 return 0;
3043}
3044
3045#define NVRAM_CMD_TIMEOUT 10000
3046
3047static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3048{
3049 int i;
3050
3051 tw32(NVRAM_CMD, nvram_cmd);
3052 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3053 udelay(10);
3054 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3055 udelay(10);
3056 break;
3057 }
3058 }
3059
3060 if (i == NVRAM_CMD_TIMEOUT)
3061 return -EBUSY;
3062
3063 return 0;
3064}
3065
3066static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3067{
Joe Perches63c3a662011-04-26 08:12:10 +00003068 if (tg3_flag(tp, NVRAM) &&
3069 tg3_flag(tp, NVRAM_BUFFERED) &&
3070 tg3_flag(tp, FLASH) &&
3071 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003072 (tp->nvram_jedecnum == JEDEC_ATMEL))
3073
3074 addr = ((addr / tp->nvram_pagesize) <<
3075 ATMEL_AT45DB0X1B_PAGE_POS) +
3076 (addr % tp->nvram_pagesize);
3077
3078 return addr;
3079}
3080
3081static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3082{
Joe Perches63c3a662011-04-26 08:12:10 +00003083 if (tg3_flag(tp, NVRAM) &&
3084 tg3_flag(tp, NVRAM_BUFFERED) &&
3085 tg3_flag(tp, FLASH) &&
3086 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003087 (tp->nvram_jedecnum == JEDEC_ATMEL))
3088
3089 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3090 tp->nvram_pagesize) +
3091 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3092
3093 return addr;
3094}
3095
Matt Carlsone4f34112009-02-25 14:25:00 +00003096/* NOTE: Data read in from NVRAM is byteswapped according to
3097 * the byteswapping settings for all other register accesses.
3098 * tg3 devices are BE devices, so on a BE machine, the data
3099 * returned will be exactly as it is seen in NVRAM. On a LE
3100 * machine, the 32-bit value will be byteswapped.
3101 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003102static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3103{
3104 int ret;
3105
Joe Perches63c3a662011-04-26 08:12:10 +00003106 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003107 return tg3_nvram_read_using_eeprom(tp, offset, val);
3108
3109 offset = tg3_nvram_phys_addr(tp, offset);
3110
3111 if (offset > NVRAM_ADDR_MSK)
3112 return -EINVAL;
3113
3114 ret = tg3_nvram_lock(tp);
3115 if (ret)
3116 return ret;
3117
3118 tg3_enable_nvram_access(tp);
3119
3120 tw32(NVRAM_ADDR, offset);
3121 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3122 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3123
3124 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003125 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003126
3127 tg3_disable_nvram_access(tp);
3128
3129 tg3_nvram_unlock(tp);
3130
3131 return ret;
3132}
3133
Matt Carlsona9dc5292009-02-25 14:25:30 +00003134/* Ensures NVRAM data is in bytestream format. */
3135static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003136{
3137 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003138 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003139 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003140 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003141 return res;
3142}
3143
Matt Carlsondbe9b922012-02-13 10:20:09 +00003144static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3145 u32 offset, u32 len, u8 *buf)
3146{
3147 int i, j, rc = 0;
3148 u32 val;
3149
3150 for (i = 0; i < len; i += 4) {
3151 u32 addr;
3152 __be32 data;
3153
3154 addr = offset + i;
3155
3156 memcpy(&data, buf + i, 4);
3157
3158 /*
3159 * The SEEPROM interface expects the data to always be opposite
3160 * the native endian format. We accomplish this by reversing
3161 * all the operations that would have been performed on the
3162 * data from a call to tg3_nvram_read_be32().
3163 */
3164 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3165
3166 val = tr32(GRC_EEPROM_ADDR);
3167 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3168
3169 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3170 EEPROM_ADDR_READ);
3171 tw32(GRC_EEPROM_ADDR, val |
3172 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3173 (addr & EEPROM_ADDR_ADDR_MASK) |
3174 EEPROM_ADDR_START |
3175 EEPROM_ADDR_WRITE);
3176
3177 for (j = 0; j < 1000; j++) {
3178 val = tr32(GRC_EEPROM_ADDR);
3179
3180 if (val & EEPROM_ADDR_COMPLETE)
3181 break;
3182 msleep(1);
3183 }
3184 if (!(val & EEPROM_ADDR_COMPLETE)) {
3185 rc = -EBUSY;
3186 break;
3187 }
3188 }
3189
3190 return rc;
3191}
3192
3193/* offset and length are dword aligned */
3194static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3195 u8 *buf)
3196{
3197 int ret = 0;
3198 u32 pagesize = tp->nvram_pagesize;
3199 u32 pagemask = pagesize - 1;
3200 u32 nvram_cmd;
3201 u8 *tmp;
3202
3203 tmp = kmalloc(pagesize, GFP_KERNEL);
3204 if (tmp == NULL)
3205 return -ENOMEM;
3206
3207 while (len) {
3208 int j;
3209 u32 phy_addr, page_off, size;
3210
3211 phy_addr = offset & ~pagemask;
3212
3213 for (j = 0; j < pagesize; j += 4) {
3214 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3215 (__be32 *) (tmp + j));
3216 if (ret)
3217 break;
3218 }
3219 if (ret)
3220 break;
3221
3222 page_off = offset & pagemask;
3223 size = pagesize;
3224 if (len < size)
3225 size = len;
3226
3227 len -= size;
3228
3229 memcpy(tmp + page_off, buf, size);
3230
3231 offset = offset + (pagesize - page_off);
3232
3233 tg3_enable_nvram_access(tp);
3234
3235 /*
3236 * Before we can erase the flash page, we need
3237 * to issue a special "write enable" command.
3238 */
3239 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3240
3241 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3242 break;
3243
3244 /* Erase the target page */
3245 tw32(NVRAM_ADDR, phy_addr);
3246
3247 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3248 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3249
3250 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3251 break;
3252
3253 /* Issue another write enable to start the write. */
3254 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3255
3256 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3257 break;
3258
3259 for (j = 0; j < pagesize; j += 4) {
3260 __be32 data;
3261
3262 data = *((__be32 *) (tmp + j));
3263
3264 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3265
3266 tw32(NVRAM_ADDR, phy_addr + j);
3267
3268 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3269 NVRAM_CMD_WR;
3270
3271 if (j == 0)
3272 nvram_cmd |= NVRAM_CMD_FIRST;
3273 else if (j == (pagesize - 4))
3274 nvram_cmd |= NVRAM_CMD_LAST;
3275
3276 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3277 if (ret)
3278 break;
3279 }
3280 if (ret)
3281 break;
3282 }
3283
3284 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3285 tg3_nvram_exec_cmd(tp, nvram_cmd);
3286
3287 kfree(tmp);
3288
3289 return ret;
3290}
3291
3292/* offset and length are dword aligned */
3293static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3294 u8 *buf)
3295{
3296 int i, ret = 0;
3297
3298 for (i = 0; i < len; i += 4, offset += 4) {
3299 u32 page_off, phy_addr, nvram_cmd;
3300 __be32 data;
3301
3302 memcpy(&data, buf + i, 4);
3303 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3304
3305 page_off = offset % tp->nvram_pagesize;
3306
3307 phy_addr = tg3_nvram_phys_addr(tp, offset);
3308
Matt Carlsondbe9b922012-02-13 10:20:09 +00003309 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3310
3311 if (page_off == 0 || i == 0)
3312 nvram_cmd |= NVRAM_CMD_FIRST;
3313 if (page_off == (tp->nvram_pagesize - 4))
3314 nvram_cmd |= NVRAM_CMD_LAST;
3315
3316 if (i == (len - 4))
3317 nvram_cmd |= NVRAM_CMD_LAST;
3318
Matt Carlson42278222012-02-13 15:20:11 +00003319 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3320 !tg3_flag(tp, FLASH) ||
3321 !tg3_flag(tp, 57765_PLUS))
3322 tw32(NVRAM_ADDR, phy_addr);
3323
Matt Carlsondbe9b922012-02-13 10:20:09 +00003324 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3325 !tg3_flag(tp, 5755_PLUS) &&
3326 (tp->nvram_jedecnum == JEDEC_ST) &&
3327 (nvram_cmd & NVRAM_CMD_FIRST)) {
3328 u32 cmd;
3329
3330 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3331 ret = tg3_nvram_exec_cmd(tp, cmd);
3332 if (ret)
3333 break;
3334 }
3335 if (!tg3_flag(tp, FLASH)) {
3336 /* We always do complete word writes to eeprom. */
3337 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3338 }
3339
3340 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3341 if (ret)
3342 break;
3343 }
3344 return ret;
3345}
3346
3347/* offset and length are dword aligned */
3348static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3349{
3350 int ret;
3351
3352 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3353 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3354 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3355 udelay(40);
3356 }
3357
3358 if (!tg3_flag(tp, NVRAM)) {
3359 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3360 } else {
3361 u32 grc_mode;
3362
3363 ret = tg3_nvram_lock(tp);
3364 if (ret)
3365 return ret;
3366
3367 tg3_enable_nvram_access(tp);
3368 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3369 tw32(NVRAM_WRITE1, 0x406);
3370
3371 grc_mode = tr32(GRC_MODE);
3372 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3373
3374 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3375 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3376 buf);
3377 } else {
3378 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3379 buf);
3380 }
3381
3382 grc_mode = tr32(GRC_MODE);
3383 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3384
3385 tg3_disable_nvram_access(tp);
3386 tg3_nvram_unlock(tp);
3387 }
3388
3389 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3390 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3391 udelay(40);
3392 }
3393
3394 return ret;
3395}
3396
Matt Carlson997b4f12011-08-31 11:44:53 +00003397#define RX_CPU_SCRATCH_BASE 0x30000
3398#define RX_CPU_SCRATCH_SIZE 0x04000
3399#define TX_CPU_SCRATCH_BASE 0x34000
3400#define TX_CPU_SCRATCH_SIZE 0x04000
3401
3402/* tp->lock is held. */
3403static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3404{
3405 int i;
3406
3407 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3408
3409 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3410 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3411
3412 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3413 return 0;
3414 }
3415 if (offset == RX_CPU_BASE) {
3416 for (i = 0; i < 10000; i++) {
3417 tw32(offset + CPU_STATE, 0xffffffff);
3418 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3419 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3420 break;
3421 }
3422
3423 tw32(offset + CPU_STATE, 0xffffffff);
3424 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3425 udelay(10);
3426 } else {
3427 for (i = 0; i < 10000; i++) {
3428 tw32(offset + CPU_STATE, 0xffffffff);
3429 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3430 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3431 break;
3432 }
3433 }
3434
3435 if (i >= 10000) {
3436 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3437 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3438 return -ENODEV;
3439 }
3440
3441 /* Clear firmware's nvram arbitration. */
3442 if (tg3_flag(tp, NVRAM))
3443 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3444 return 0;
3445}
3446
3447struct fw_info {
3448 unsigned int fw_base;
3449 unsigned int fw_len;
3450 const __be32 *fw_data;
3451};
3452
3453/* tp->lock is held. */
3454static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3455 u32 cpu_scratch_base, int cpu_scratch_size,
3456 struct fw_info *info)
3457{
3458 int err, lock_err, i;
3459 void (*write_op)(struct tg3 *, u32, u32);
3460
3461 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3462 netdev_err(tp->dev,
3463 "%s: Trying to load TX cpu firmware which is 5705\n",
3464 __func__);
3465 return -EINVAL;
3466 }
3467
3468 if (tg3_flag(tp, 5705_PLUS))
3469 write_op = tg3_write_mem;
3470 else
3471 write_op = tg3_write_indirect_reg32;
3472
3473 /* It is possible that bootcode is still loading at this point.
3474 * Get the nvram lock first before halting the cpu.
3475 */
3476 lock_err = tg3_nvram_lock(tp);
3477 err = tg3_halt_cpu(tp, cpu_base);
3478 if (!lock_err)
3479 tg3_nvram_unlock(tp);
3480 if (err)
3481 goto out;
3482
3483 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3484 write_op(tp, cpu_scratch_base + i, 0);
3485 tw32(cpu_base + CPU_STATE, 0xffffffff);
3486 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3487 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3488 write_op(tp, (cpu_scratch_base +
3489 (info->fw_base & 0xffff) +
3490 (i * sizeof(u32))),
3491 be32_to_cpu(info->fw_data[i]));
3492
3493 err = 0;
3494
3495out:
3496 return err;
3497}
3498
3499/* tp->lock is held. */
3500static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3501{
3502 struct fw_info info;
3503 const __be32 *fw_data;
3504 int err, i;
3505
3506 fw_data = (void *)tp->fw->data;
3507
3508 /* Firmware blob starts with version numbers, followed by
3509 start address and length. We are setting complete length.
3510 length = end_address_of_bss - start_address_of_text.
3511 Remainder is the blob to be loaded contiguously
3512 from start address. */
3513
3514 info.fw_base = be32_to_cpu(fw_data[1]);
3515 info.fw_len = tp->fw->size - 12;
3516 info.fw_data = &fw_data[3];
3517
3518 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3519 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3520 &info);
3521 if (err)
3522 return err;
3523
3524 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3525 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3526 &info);
3527 if (err)
3528 return err;
3529
3530 /* Now startup only the RX cpu. */
3531 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3532 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3533
3534 for (i = 0; i < 5; i++) {
3535 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3536 break;
3537 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3538 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3539 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3540 udelay(1000);
3541 }
3542 if (i >= 5) {
3543 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3544 "should be %08x\n", __func__,
3545 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3546 return -ENODEV;
3547 }
3548 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3549 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3550
3551 return 0;
3552}
3553
3554/* tp->lock is held. */
3555static int tg3_load_tso_firmware(struct tg3 *tp)
3556{
3557 struct fw_info info;
3558 const __be32 *fw_data;
3559 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3560 int err, i;
3561
3562 if (tg3_flag(tp, HW_TSO_1) ||
3563 tg3_flag(tp, HW_TSO_2) ||
3564 tg3_flag(tp, HW_TSO_3))
3565 return 0;
3566
3567 fw_data = (void *)tp->fw->data;
3568
3569 /* Firmware blob starts with version numbers, followed by
3570 start address and length. We are setting complete length.
3571 length = end_address_of_bss - start_address_of_text.
3572 Remainder is the blob to be loaded contiguously
3573 from start address. */
3574
3575 info.fw_base = be32_to_cpu(fw_data[1]);
3576 cpu_scratch_size = tp->fw_len;
3577 info.fw_len = tp->fw->size - 12;
3578 info.fw_data = &fw_data[3];
3579
3580 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3581 cpu_base = RX_CPU_BASE;
3582 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3583 } else {
3584 cpu_base = TX_CPU_BASE;
3585 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3586 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3587 }
3588
3589 err = tg3_load_firmware_cpu(tp, cpu_base,
3590 cpu_scratch_base, cpu_scratch_size,
3591 &info);
3592 if (err)
3593 return err;
3594
3595 /* Now startup the cpu. */
3596 tw32(cpu_base + CPU_STATE, 0xffffffff);
3597 tw32_f(cpu_base + CPU_PC, info.fw_base);
3598
3599 for (i = 0; i < 5; i++) {
3600 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3601 break;
3602 tw32(cpu_base + CPU_STATE, 0xffffffff);
3603 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3604 tw32_f(cpu_base + CPU_PC, info.fw_base);
3605 udelay(1000);
3606 }
3607 if (i >= 5) {
3608 netdev_err(tp->dev,
3609 "%s fails to set CPU PC, is %08x should be %08x\n",
3610 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3611 return -ENODEV;
3612 }
3613 tw32(cpu_base + CPU_STATE, 0xffffffff);
3614 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3615 return 0;
3616}
3617
3618
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003619/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003620static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3621{
3622 u32 addr_high, addr_low;
3623 int i;
3624
3625 addr_high = ((tp->dev->dev_addr[0] << 8) |
3626 tp->dev->dev_addr[1]);
3627 addr_low = ((tp->dev->dev_addr[2] << 24) |
3628 (tp->dev->dev_addr[3] << 16) |
3629 (tp->dev->dev_addr[4] << 8) |
3630 (tp->dev->dev_addr[5] << 0));
3631 for (i = 0; i < 4; i++) {
3632 if (i == 1 && skip_mac_1)
3633 continue;
3634 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3635 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3636 }
3637
3638 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3639 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3640 for (i = 0; i < 12; i++) {
3641 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3642 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3643 }
3644 }
3645
3646 addr_high = (tp->dev->dev_addr[0] +
3647 tp->dev->dev_addr[1] +
3648 tp->dev->dev_addr[2] +
3649 tp->dev->dev_addr[3] +
3650 tp->dev->dev_addr[4] +
3651 tp->dev->dev_addr[5]) &
3652 TX_BACKOFF_SEED_MASK;
3653 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3654}
3655
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003656static void tg3_enable_register_access(struct tg3 *tp)
3657{
3658 /*
3659 * Make sure register accesses (indirect or otherwise) will function
3660 * correctly.
3661 */
3662 pci_write_config_dword(tp->pdev,
3663 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3664}
3665
3666static int tg3_power_up(struct tg3 *tp)
3667{
Matt Carlsonbed98292011-07-13 09:27:29 +00003668 int err;
3669
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003670 tg3_enable_register_access(tp);
3671
Matt Carlsonbed98292011-07-13 09:27:29 +00003672 err = pci_set_power_state(tp->pdev, PCI_D0);
3673 if (!err) {
3674 /* Switch out of Vaux if it is a NIC */
3675 tg3_pwrsrc_switch_to_vmain(tp);
3676 } else {
3677 netdev_err(tp->dev, "Transition to D0 failed\n");
3678 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003679
Matt Carlsonbed98292011-07-13 09:27:29 +00003680 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003681}
3682
Matt Carlson4b409522012-02-13 10:20:11 +00003683static int tg3_setup_phy(struct tg3 *, int);
3684
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003685static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
3687 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003688 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003690 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003691
3692 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003693 if (tg3_flag(tp, CLKREQ_BUG))
3694 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3695 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3698 tw32(TG3PCI_MISC_HOST_CTRL,
3699 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3700
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003701 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003702 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003703
Joe Perches63c3a662011-04-26 08:12:10 +00003704 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003705 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003706 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson800960682010-08-02 11:26:06 +00003707 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003708 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003709 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003710
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003711 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003712
Matt Carlson800960682010-08-02 11:26:06 +00003713 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003714
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003715 tp->link_config.speed = phydev->speed;
3716 tp->link_config.duplex = phydev->duplex;
3717 tp->link_config.autoneg = phydev->autoneg;
3718 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003719
3720 advertising = ADVERTISED_TP |
3721 ADVERTISED_Pause |
3722 ADVERTISED_Autoneg |
3723 ADVERTISED_10baseT_Half;
3724
Joe Perches63c3a662011-04-26 08:12:10 +00003725 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3726 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003727 advertising |=
3728 ADVERTISED_100baseT_Half |
3729 ADVERTISED_100baseT_Full |
3730 ADVERTISED_10baseT_Full;
3731 else
3732 advertising |= ADVERTISED_10baseT_Full;
3733 }
3734
3735 phydev->advertising = advertising;
3736
3737 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003738
3739 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003740 if (phyid != PHY_ID_BCMAC131) {
3741 phyid &= PHY_BCM_OUI_MASK;
3742 if (phyid == PHY_BCM_OUI_1 ||
3743 phyid == PHY_BCM_OUI_2 ||
3744 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003745 do_low_power = true;
3746 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003747 }
Matt Carlsondd477002008-05-25 23:45:58 -07003748 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003749 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003750
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003751 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson800960682010-08-02 11:26:06 +00003752 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Matt Carlson2855b9f2012-02-13 15:20:14 +00003754 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003755 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 }
3757
Michael Chanb5d37722006-09-27 16:06:21 -07003758 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3759 u32 val;
3760
3761 val = tr32(GRC_VCPU_EXT_CTRL);
3762 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003763 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003764 int i;
3765 u32 val;
3766
3767 for (i = 0; i < 200; i++) {
3768 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3769 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3770 break;
3771 msleep(1);
3772 }
3773 }
Joe Perches63c3a662011-04-26 08:12:10 +00003774 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003775 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3776 WOL_DRV_STATE_SHUTDOWN |
3777 WOL_DRV_WOL |
3778 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003779
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003780 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 u32 mac_mode;
3782
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003783 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003784 if (do_low_power &&
3785 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3786 tg3_phy_auxctl_write(tp,
3787 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3788 MII_TG3_AUXCTL_PCTL_WOL_EN |
3789 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3790 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003791 udelay(40);
3792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003794 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003795 mac_mode = MAC_MODE_PORT_MODE_GMII;
3796 else
3797 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003799 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3800 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3801 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003802 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003803 SPEED_100 : SPEED_10;
3804 if (tg3_5700_link_polarity(tp, speed))
3805 mac_mode |= MAC_MODE_LINK_POLARITY;
3806 else
3807 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 } else {
3810 mac_mode = MAC_MODE_PORT_MODE_TBI;
3811 }
3812
Joe Perches63c3a662011-04-26 08:12:10 +00003813 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 tw32(MAC_LED_CTRL, tp->led_ctrl);
3815
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003816 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003817 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3818 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003819 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
Joe Perches63c3a662011-04-26 08:12:10 +00003821 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003822 mac_mode |= MAC_MODE_APE_TX_EN |
3823 MAC_MODE_APE_RX_EN |
3824 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003825
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 tw32_f(MAC_MODE, mac_mode);
3827 udelay(100);
3828
3829 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3830 udelay(10);
3831 }
3832
Joe Perches63c3a662011-04-26 08:12:10 +00003833 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3835 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3836 u32 base_val;
3837
3838 base_val = tp->pci_clock_ctrl;
3839 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3840 CLOCK_CTRL_TXCLK_DISABLE);
3841
Michael Chanb401e9e2005-12-19 16:27:04 -08003842 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3843 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003844 } else if (tg3_flag(tp, 5780_CLASS) ||
3845 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003846 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003847 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003848 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 u32 newbits1, newbits2;
3850
3851 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3852 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3853 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3854 CLOCK_CTRL_TXCLK_DISABLE |
3855 CLOCK_CTRL_ALTCLK);
3856 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003857 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 newbits1 = CLOCK_CTRL_625_CORE;
3859 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3860 } else {
3861 newbits1 = CLOCK_CTRL_ALTCLK;
3862 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3863 }
3864
Michael Chanb401e9e2005-12-19 16:27:04 -08003865 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3866 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Michael Chanb401e9e2005-12-19 16:27:04 -08003868 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3869 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Joe Perches63c3a662011-04-26 08:12:10 +00003871 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 u32 newbits3;
3873
3874 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3875 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3876 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3877 CLOCK_CTRL_TXCLK_DISABLE |
3878 CLOCK_CTRL_44MHZ_CORE);
3879 } else {
3880 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3881 }
3882
Michael Chanb401e9e2005-12-19 16:27:04 -08003883 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3884 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 }
3886 }
3887
Joe Perches63c3a662011-04-26 08:12:10 +00003888 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003889 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003890
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003891 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
3893 /* Workaround for unstable PLL clock */
3894 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3895 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3896 u32 val = tr32(0x7d00);
3897
3898 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3899 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003900 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003901 int err;
3902
3903 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003905 if (!err)
3906 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 }
3909
Michael Chanbbadf502006-04-06 21:46:34 -07003910 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 return 0;
3913}
3914
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003915static void tg3_power_down(struct tg3 *tp)
3916{
3917 tg3_power_down_prepare(tp);
3918
Joe Perches63c3a662011-04-26 08:12:10 +00003919 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003920 pci_set_power_state(tp->pdev, PCI_D3hot);
3921}
3922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3924{
3925 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3926 case MII_TG3_AUX_STAT_10HALF:
3927 *speed = SPEED_10;
3928 *duplex = DUPLEX_HALF;
3929 break;
3930
3931 case MII_TG3_AUX_STAT_10FULL:
3932 *speed = SPEED_10;
3933 *duplex = DUPLEX_FULL;
3934 break;
3935
3936 case MII_TG3_AUX_STAT_100HALF:
3937 *speed = SPEED_100;
3938 *duplex = DUPLEX_HALF;
3939 break;
3940
3941 case MII_TG3_AUX_STAT_100FULL:
3942 *speed = SPEED_100;
3943 *duplex = DUPLEX_FULL;
3944 break;
3945
3946 case MII_TG3_AUX_STAT_1000HALF:
3947 *speed = SPEED_1000;
3948 *duplex = DUPLEX_HALF;
3949 break;
3950
3951 case MII_TG3_AUX_STAT_1000FULL:
3952 *speed = SPEED_1000;
3953 *duplex = DUPLEX_FULL;
3954 break;
3955
3956 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003957 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003958 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3959 SPEED_10;
3960 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3961 DUPLEX_HALF;
3962 break;
3963 }
Matt Carlsone7405222012-02-13 15:20:16 +00003964 *speed = SPEED_UNKNOWN;
3965 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968}
3969
Matt Carlson42b64a42011-05-19 12:12:49 +00003970static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
Matt Carlson42b64a42011-05-19 12:12:49 +00003972 int err = 0;
3973 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Matt Carlson42b64a42011-05-19 12:12:49 +00003975 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003976 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003977 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Matt Carlson42b64a42011-05-19 12:12:49 +00003979 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3980 if (err)
3981 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Matt Carlson4f272092011-12-14 11:09:57 +00003983 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3984 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003985
Matt Carlson4f272092011-12-14 11:09:57 +00003986 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3987 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3988 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003989
Matt Carlson4f272092011-12-14 11:09:57 +00003990 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3991 if (err)
3992 goto done;
3993 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003994
Matt Carlson42b64a42011-05-19 12:12:49 +00003995 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3996 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Matt Carlson42b64a42011-05-19 12:12:49 +00003998 tw32(TG3_CPMU_EEE_MODE,
3999 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004000
Matt Carlson42b64a42011-05-19 12:12:49 +00004001 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
4002 if (!err) {
4003 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004004
Matt Carlsona6b68da2010-12-06 08:28:52 +00004005 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004006 /* Advertise 100-BaseTX EEE ability */
4007 if (advertise & ADVERTISED_100baseT_Full)
4008 val |= MDIO_AN_EEE_ADV_100TX;
4009 /* Advertise 1000-BaseT EEE ability */
4010 if (advertise & ADVERTISED_1000baseT_Full)
4011 val |= MDIO_AN_EEE_ADV_1000T;
4012 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004013 if (err)
4014 val = 0;
4015
4016 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
4017 case ASIC_REV_5717:
4018 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004019 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004020 case ASIC_REV_5719:
4021 /* If we advertised any eee advertisements above... */
4022 if (val)
4023 val = MII_TG3_DSP_TAP26_ALNOKO |
4024 MII_TG3_DSP_TAP26_RMRXSTO |
4025 MII_TG3_DSP_TAP26_OPCSINPT;
4026 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4027 /* Fall through */
4028 case ASIC_REV_5720:
4029 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4030 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4031 MII_TG3_DSP_CH34TP2_HIBW01);
4032 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004033
Matt Carlson42b64a42011-05-19 12:12:49 +00004034 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
4035 if (!err)
4036 err = err2;
4037 }
4038
4039done:
4040 return err;
4041}
4042
4043static void tg3_phy_copper_begin(struct tg3 *tp)
4044{
Matt Carlsond13ba512012-02-22 12:35:19 +00004045 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4046 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4047 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004048
Matt Carlsond13ba512012-02-22 12:35:19 +00004049 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
4050 adv = ADVERTISED_10baseT_Half |
4051 ADVERTISED_10baseT_Full;
4052 if (tg3_flag(tp, WOL_SPEED_100MB))
4053 adv |= ADVERTISED_100baseT_Half |
4054 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004055
Matt Carlsond13ba512012-02-22 12:35:19 +00004056 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004057 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004058 adv = tp->link_config.advertising;
4059 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4060 adv &= ~(ADVERTISED_1000baseT_Half |
4061 ADVERTISED_1000baseT_Full);
4062
4063 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004064 }
4065
Matt Carlsond13ba512012-02-22 12:35:19 +00004066 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004067
Matt Carlsond13ba512012-02-22 12:35:19 +00004068 tg3_writephy(tp, MII_BMCR,
4069 BMCR_ANENABLE | BMCR_ANRESTART);
4070 } else {
4071 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 u32 bmcr, orig_bmcr;
4073
4074 tp->link_config.active_speed = tp->link_config.speed;
4075 tp->link_config.active_duplex = tp->link_config.duplex;
4076
4077 bmcr = 0;
4078 switch (tp->link_config.speed) {
4079 default:
4080 case SPEED_10:
4081 break;
4082
4083 case SPEED_100:
4084 bmcr |= BMCR_SPEED100;
4085 break;
4086
4087 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004088 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
4092 if (tp->link_config.duplex == DUPLEX_FULL)
4093 bmcr |= BMCR_FULLDPLX;
4094
4095 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4096 (bmcr != orig_bmcr)) {
4097 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4098 for (i = 0; i < 1500; i++) {
4099 u32 tmp;
4100
4101 udelay(10);
4102 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4103 tg3_readphy(tp, MII_BMSR, &tmp))
4104 continue;
4105 if (!(tmp & BMSR_LSTATUS)) {
4106 udelay(40);
4107 break;
4108 }
4109 }
4110 tg3_writephy(tp, MII_BMCR, bmcr);
4111 udelay(40);
4112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 }
4114}
4115
4116static int tg3_init_5401phy_dsp(struct tg3 *tp)
4117{
4118 int err;
4119
4120 /* Turn off tap power management. */
4121 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004122 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004124 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4125 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4126 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4127 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4128 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
4130 udelay(40);
4131
4132 return err;
4133}
4134
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004135static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004137 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004138
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004139 advertising = tp->link_config.advertising;
4140 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004142 advmsk = ADVERTISE_ALL;
4143 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004144 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004145 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004148 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4149 return false;
4150
4151 if ((*lcladv & advmsk) != tgtadv)
4152 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004153
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004154 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 u32 tg3_ctrl;
4156
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004157 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004158
Matt Carlson221c5632011-06-13 13:39:01 +00004159 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004160 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Matt Carlson3198e072012-02-13 15:20:10 +00004162 if (tgtadv &&
4163 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4164 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4165 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4166 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4167 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4168 } else {
4169 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4170 }
4171
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004172 if (tg3_ctrl != tgtadv)
4173 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004175
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004176 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004177}
4178
Matt Carlson859edb22011-12-08 14:40:16 +00004179static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4180{
4181 u32 lpeth = 0;
4182
4183 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4184 u32 val;
4185
4186 if (tg3_readphy(tp, MII_STAT1000, &val))
4187 return false;
4188
4189 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4190 }
4191
4192 if (tg3_readphy(tp, MII_LPA, rmtadv))
4193 return false;
4194
4195 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4196 tp->link_config.rmt_adv = lpeth;
4197
4198 return true;
4199}
4200
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004201static bool tg3_test_and_report_link_chg(struct tg3 *tp, int curr_link_up)
4202{
4203 if (curr_link_up != tp->link_up) {
4204 if (curr_link_up) {
4205 tg3_carrier_on(tp);
4206 } else {
4207 tg3_carrier_off(tp);
4208 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4209 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4210 }
4211
4212 tg3_link_report(tp);
4213 return true;
4214 }
4215
4216 return false;
4217}
4218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4220{
4221 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004222 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004223 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 u16 current_speed;
4225 u8 current_duplex;
4226 int i, err;
4227
4228 tw32(MAC_EVENT, 0);
4229
4230 tw32_f(MAC_STATUS,
4231 (MAC_STATUS_SYNC_CHANGED |
4232 MAC_STATUS_CFG_CHANGED |
4233 MAC_STATUS_MI_COMPLETION |
4234 MAC_STATUS_LNKSTATE_CHANGED));
4235 udelay(40);
4236
Matt Carlson8ef21422008-05-02 16:47:53 -07004237 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4238 tw32_f(MAC_MI_MODE,
4239 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4240 udelay(80);
4241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004243 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
4245 /* Some third-party PHYs need to be reset on link going
4246 * down.
4247 */
4248 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4249 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4250 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004251 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 tg3_readphy(tp, MII_BMSR, &bmsr);
4253 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4254 !(bmsr & BMSR_LSTATUS))
4255 force_reset = 1;
4256 }
4257 if (force_reset)
4258 tg3_phy_reset(tp);
4259
Matt Carlson79eb6902010-02-17 15:17:03 +00004260 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 tg3_readphy(tp, MII_BMSR, &bmsr);
4262 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004263 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 bmsr = 0;
4265
4266 if (!(bmsr & BMSR_LSTATUS)) {
4267 err = tg3_init_5401phy_dsp(tp);
4268 if (err)
4269 return err;
4270
4271 tg3_readphy(tp, MII_BMSR, &bmsr);
4272 for (i = 0; i < 1000; i++) {
4273 udelay(10);
4274 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4275 (bmsr & BMSR_LSTATUS)) {
4276 udelay(40);
4277 break;
4278 }
4279 }
4280
Matt Carlson79eb6902010-02-17 15:17:03 +00004281 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4282 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 !(bmsr & BMSR_LSTATUS) &&
4284 tp->link_config.active_speed == SPEED_1000) {
4285 err = tg3_phy_reset(tp);
4286 if (!err)
4287 err = tg3_init_5401phy_dsp(tp);
4288 if (err)
4289 return err;
4290 }
4291 }
4292 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4293 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4294 /* 5701 {A0,B0} CRC bug workaround */
4295 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004296 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4297 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4298 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 }
4300
4301 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004302 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4303 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004305 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004307 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4309
4310 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4311 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4312 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4313 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4314 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4315 else
4316 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4317 }
4318
4319 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004320 current_speed = SPEED_UNKNOWN;
4321 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004322 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004323 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004325 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004326 err = tg3_phy_auxctl_read(tp,
4327 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4328 &val);
4329 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004330 tg3_phy_auxctl_write(tp,
4331 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4332 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 goto relink;
4334 }
4335 }
4336
4337 bmsr = 0;
4338 for (i = 0; i < 100; i++) {
4339 tg3_readphy(tp, MII_BMSR, &bmsr);
4340 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4341 (bmsr & BMSR_LSTATUS))
4342 break;
4343 udelay(40);
4344 }
4345
4346 if (bmsr & BMSR_LSTATUS) {
4347 u32 aux_stat, bmcr;
4348
4349 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4350 for (i = 0; i < 2000; i++) {
4351 udelay(10);
4352 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4353 aux_stat)
4354 break;
4355 }
4356
4357 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4358 &current_speed,
4359 &current_duplex);
4360
4361 bmcr = 0;
4362 for (i = 0; i < 200; i++) {
4363 tg3_readphy(tp, MII_BMCR, &bmcr);
4364 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4365 continue;
4366 if (bmcr && bmcr != 0x7fff)
4367 break;
4368 udelay(10);
4369 }
4370
Matt Carlsonef167e22007-12-20 20:10:01 -08004371 lcl_adv = 0;
4372 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
Matt Carlsonef167e22007-12-20 20:10:01 -08004374 tp->link_config.active_speed = current_speed;
4375 tp->link_config.active_duplex = current_duplex;
4376
4377 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4378 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004379 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004380 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004381 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 } else {
4383 if (!(bmcr & BMCR_ANENABLE) &&
4384 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004385 tp->link_config.duplex == current_duplex &&
4386 tp->link_config.flowctrl ==
4387 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 }
4390 }
4391
Matt Carlsonef167e22007-12-20 20:10:01 -08004392 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004393 tp->link_config.active_duplex == DUPLEX_FULL) {
4394 u32 reg, bit;
4395
4396 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4397 reg = MII_TG3_FET_GEN_STAT;
4398 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4399 } else {
4400 reg = MII_TG3_EXT_STAT;
4401 bit = MII_TG3_EXT_STAT_MDIX;
4402 }
4403
4404 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4405 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4406
Matt Carlsonef167e22007-12-20 20:10:01 -08004407 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411relink:
Matt Carlson800960682010-08-02 11:26:06 +00004412 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 tg3_phy_copper_begin(tp);
4414
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004415 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004416 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4417 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 current_link_up = 1;
4419 }
4420
4421 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4422 if (current_link_up == 1) {
4423 if (tp->link_config.active_speed == SPEED_100 ||
4424 tp->link_config.active_speed == SPEED_10)
4425 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4426 else
4427 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004428 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004429 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4430 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4432
4433 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4434 if (tp->link_config.active_duplex == DUPLEX_HALF)
4435 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004438 if (current_link_up == 1 &&
4439 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004441 else
4442 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 }
4444
4445 /* ??? Without this setting Netgear GA302T PHY does not
4446 * ??? send/receive packets...
4447 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004448 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4450 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4451 tw32_f(MAC_MI_MODE, tp->mi_mode);
4452 udelay(80);
4453 }
4454
4455 tw32_f(MAC_MODE, tp->mac_mode);
4456 udelay(40);
4457
Matt Carlson52b02d02010-10-14 10:37:41 +00004458 tg3_phy_eee_adjust(tp, current_link_up);
4459
Joe Perches63c3a662011-04-26 08:12:10 +00004460 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 /* Polled via timer. */
4462 tw32_f(MAC_EVENT, 0);
4463 } else {
4464 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4465 }
4466 udelay(40);
4467
4468 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4469 current_link_up == 1 &&
4470 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004471 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 udelay(120);
4473 tw32_f(MAC_STATUS,
4474 (MAC_STATUS_SYNC_CHANGED |
4475 MAC_STATUS_CFG_CHANGED));
4476 udelay(40);
4477 tg3_write_mem(tp,
4478 NIC_SRAM_FIRMWARE_MBOX,
4479 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4480 }
4481
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004482 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004483 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004484 if (tp->link_config.active_speed == SPEED_100 ||
4485 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004486 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4487 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004488 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004489 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4490 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004491 }
4492
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004493 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
4495 return 0;
4496}
4497
4498struct tg3_fiber_aneginfo {
4499 int state;
4500#define ANEG_STATE_UNKNOWN 0
4501#define ANEG_STATE_AN_ENABLE 1
4502#define ANEG_STATE_RESTART_INIT 2
4503#define ANEG_STATE_RESTART 3
4504#define ANEG_STATE_DISABLE_LINK_OK 4
4505#define ANEG_STATE_ABILITY_DETECT_INIT 5
4506#define ANEG_STATE_ABILITY_DETECT 6
4507#define ANEG_STATE_ACK_DETECT_INIT 7
4508#define ANEG_STATE_ACK_DETECT 8
4509#define ANEG_STATE_COMPLETE_ACK_INIT 9
4510#define ANEG_STATE_COMPLETE_ACK 10
4511#define ANEG_STATE_IDLE_DETECT_INIT 11
4512#define ANEG_STATE_IDLE_DETECT 12
4513#define ANEG_STATE_LINK_OK 13
4514#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4515#define ANEG_STATE_NEXT_PAGE_WAIT 15
4516
4517 u32 flags;
4518#define MR_AN_ENABLE 0x00000001
4519#define MR_RESTART_AN 0x00000002
4520#define MR_AN_COMPLETE 0x00000004
4521#define MR_PAGE_RX 0x00000008
4522#define MR_NP_LOADED 0x00000010
4523#define MR_TOGGLE_TX 0x00000020
4524#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4525#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4526#define MR_LP_ADV_SYM_PAUSE 0x00000100
4527#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4528#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4529#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4530#define MR_LP_ADV_NEXT_PAGE 0x00001000
4531#define MR_TOGGLE_RX 0x00002000
4532#define MR_NP_RX 0x00004000
4533
4534#define MR_LINK_OK 0x80000000
4535
4536 unsigned long link_time, cur_time;
4537
4538 u32 ability_match_cfg;
4539 int ability_match_count;
4540
4541 char ability_match, idle_match, ack_match;
4542
4543 u32 txconfig, rxconfig;
4544#define ANEG_CFG_NP 0x00000080
4545#define ANEG_CFG_ACK 0x00000040
4546#define ANEG_CFG_RF2 0x00000020
4547#define ANEG_CFG_RF1 0x00000010
4548#define ANEG_CFG_PS2 0x00000001
4549#define ANEG_CFG_PS1 0x00008000
4550#define ANEG_CFG_HD 0x00004000
4551#define ANEG_CFG_FD 0x00002000
4552#define ANEG_CFG_INVAL 0x00001f06
4553
4554};
4555#define ANEG_OK 0
4556#define ANEG_DONE 1
4557#define ANEG_TIMER_ENAB 2
4558#define ANEG_FAILED -1
4559
4560#define ANEG_STATE_SETTLE_TIME 10000
4561
4562static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4563 struct tg3_fiber_aneginfo *ap)
4564{
Matt Carlson5be73b42007-12-20 20:09:29 -08004565 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 unsigned long delta;
4567 u32 rx_cfg_reg;
4568 int ret;
4569
4570 if (ap->state == ANEG_STATE_UNKNOWN) {
4571 ap->rxconfig = 0;
4572 ap->link_time = 0;
4573 ap->cur_time = 0;
4574 ap->ability_match_cfg = 0;
4575 ap->ability_match_count = 0;
4576 ap->ability_match = 0;
4577 ap->idle_match = 0;
4578 ap->ack_match = 0;
4579 }
4580 ap->cur_time++;
4581
4582 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4583 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4584
4585 if (rx_cfg_reg != ap->ability_match_cfg) {
4586 ap->ability_match_cfg = rx_cfg_reg;
4587 ap->ability_match = 0;
4588 ap->ability_match_count = 0;
4589 } else {
4590 if (++ap->ability_match_count > 1) {
4591 ap->ability_match = 1;
4592 ap->ability_match_cfg = rx_cfg_reg;
4593 }
4594 }
4595 if (rx_cfg_reg & ANEG_CFG_ACK)
4596 ap->ack_match = 1;
4597 else
4598 ap->ack_match = 0;
4599
4600 ap->idle_match = 0;
4601 } else {
4602 ap->idle_match = 1;
4603 ap->ability_match_cfg = 0;
4604 ap->ability_match_count = 0;
4605 ap->ability_match = 0;
4606 ap->ack_match = 0;
4607
4608 rx_cfg_reg = 0;
4609 }
4610
4611 ap->rxconfig = rx_cfg_reg;
4612 ret = ANEG_OK;
4613
Matt Carlson33f401a2010-04-05 10:19:27 +00004614 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 case ANEG_STATE_UNKNOWN:
4616 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4617 ap->state = ANEG_STATE_AN_ENABLE;
4618
4619 /* fallthru */
4620 case ANEG_STATE_AN_ENABLE:
4621 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4622 if (ap->flags & MR_AN_ENABLE) {
4623 ap->link_time = 0;
4624 ap->cur_time = 0;
4625 ap->ability_match_cfg = 0;
4626 ap->ability_match_count = 0;
4627 ap->ability_match = 0;
4628 ap->idle_match = 0;
4629 ap->ack_match = 0;
4630
4631 ap->state = ANEG_STATE_RESTART_INIT;
4632 } else {
4633 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4634 }
4635 break;
4636
4637 case ANEG_STATE_RESTART_INIT:
4638 ap->link_time = ap->cur_time;
4639 ap->flags &= ~(MR_NP_LOADED);
4640 ap->txconfig = 0;
4641 tw32(MAC_TX_AUTO_NEG, 0);
4642 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4643 tw32_f(MAC_MODE, tp->mac_mode);
4644 udelay(40);
4645
4646 ret = ANEG_TIMER_ENAB;
4647 ap->state = ANEG_STATE_RESTART;
4648
4649 /* fallthru */
4650 case ANEG_STATE_RESTART:
4651 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004652 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004654 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 break;
4657
4658 case ANEG_STATE_DISABLE_LINK_OK:
4659 ret = ANEG_DONE;
4660 break;
4661
4662 case ANEG_STATE_ABILITY_DETECT_INIT:
4663 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004664 ap->txconfig = ANEG_CFG_FD;
4665 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4666 if (flowctrl & ADVERTISE_1000XPAUSE)
4667 ap->txconfig |= ANEG_CFG_PS1;
4668 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4669 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4671 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4672 tw32_f(MAC_MODE, tp->mac_mode);
4673 udelay(40);
4674
4675 ap->state = ANEG_STATE_ABILITY_DETECT;
4676 break;
4677
4678 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004679 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 break;
4682
4683 case ANEG_STATE_ACK_DETECT_INIT:
4684 ap->txconfig |= ANEG_CFG_ACK;
4685 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4686 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4687 tw32_f(MAC_MODE, tp->mac_mode);
4688 udelay(40);
4689
4690 ap->state = ANEG_STATE_ACK_DETECT;
4691
4692 /* fallthru */
4693 case ANEG_STATE_ACK_DETECT:
4694 if (ap->ack_match != 0) {
4695 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4696 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4697 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4698 } else {
4699 ap->state = ANEG_STATE_AN_ENABLE;
4700 }
4701 } else if (ap->ability_match != 0 &&
4702 ap->rxconfig == 0) {
4703 ap->state = ANEG_STATE_AN_ENABLE;
4704 }
4705 break;
4706
4707 case ANEG_STATE_COMPLETE_ACK_INIT:
4708 if (ap->rxconfig & ANEG_CFG_INVAL) {
4709 ret = ANEG_FAILED;
4710 break;
4711 }
4712 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4713 MR_LP_ADV_HALF_DUPLEX |
4714 MR_LP_ADV_SYM_PAUSE |
4715 MR_LP_ADV_ASYM_PAUSE |
4716 MR_LP_ADV_REMOTE_FAULT1 |
4717 MR_LP_ADV_REMOTE_FAULT2 |
4718 MR_LP_ADV_NEXT_PAGE |
4719 MR_TOGGLE_RX |
4720 MR_NP_RX);
4721 if (ap->rxconfig & ANEG_CFG_FD)
4722 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4723 if (ap->rxconfig & ANEG_CFG_HD)
4724 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4725 if (ap->rxconfig & ANEG_CFG_PS1)
4726 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4727 if (ap->rxconfig & ANEG_CFG_PS2)
4728 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4729 if (ap->rxconfig & ANEG_CFG_RF1)
4730 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4731 if (ap->rxconfig & ANEG_CFG_RF2)
4732 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4733 if (ap->rxconfig & ANEG_CFG_NP)
4734 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4735
4736 ap->link_time = ap->cur_time;
4737
4738 ap->flags ^= (MR_TOGGLE_TX);
4739 if (ap->rxconfig & 0x0008)
4740 ap->flags |= MR_TOGGLE_RX;
4741 if (ap->rxconfig & ANEG_CFG_NP)
4742 ap->flags |= MR_NP_RX;
4743 ap->flags |= MR_PAGE_RX;
4744
4745 ap->state = ANEG_STATE_COMPLETE_ACK;
4746 ret = ANEG_TIMER_ENAB;
4747 break;
4748
4749 case ANEG_STATE_COMPLETE_ACK:
4750 if (ap->ability_match != 0 &&
4751 ap->rxconfig == 0) {
4752 ap->state = ANEG_STATE_AN_ENABLE;
4753 break;
4754 }
4755 delta = ap->cur_time - ap->link_time;
4756 if (delta > ANEG_STATE_SETTLE_TIME) {
4757 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4758 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4759 } else {
4760 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4761 !(ap->flags & MR_NP_RX)) {
4762 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4763 } else {
4764 ret = ANEG_FAILED;
4765 }
4766 }
4767 }
4768 break;
4769
4770 case ANEG_STATE_IDLE_DETECT_INIT:
4771 ap->link_time = ap->cur_time;
4772 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4773 tw32_f(MAC_MODE, tp->mac_mode);
4774 udelay(40);
4775
4776 ap->state = ANEG_STATE_IDLE_DETECT;
4777 ret = ANEG_TIMER_ENAB;
4778 break;
4779
4780 case ANEG_STATE_IDLE_DETECT:
4781 if (ap->ability_match != 0 &&
4782 ap->rxconfig == 0) {
4783 ap->state = ANEG_STATE_AN_ENABLE;
4784 break;
4785 }
4786 delta = ap->cur_time - ap->link_time;
4787 if (delta > ANEG_STATE_SETTLE_TIME) {
4788 /* XXX another gem from the Broadcom driver :( */
4789 ap->state = ANEG_STATE_LINK_OK;
4790 }
4791 break;
4792
4793 case ANEG_STATE_LINK_OK:
4794 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4795 ret = ANEG_DONE;
4796 break;
4797
4798 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4799 /* ??? unimplemented */
4800 break;
4801
4802 case ANEG_STATE_NEXT_PAGE_WAIT:
4803 /* ??? unimplemented */
4804 break;
4805
4806 default:
4807 ret = ANEG_FAILED;
4808 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
4811 return ret;
4812}
4813
Matt Carlson5be73b42007-12-20 20:09:29 -08004814static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815{
4816 int res = 0;
4817 struct tg3_fiber_aneginfo aninfo;
4818 int status = ANEG_FAILED;
4819 unsigned int tick;
4820 u32 tmp;
4821
4822 tw32_f(MAC_TX_AUTO_NEG, 0);
4823
4824 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4825 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4826 udelay(40);
4827
4828 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4829 udelay(40);
4830
4831 memset(&aninfo, 0, sizeof(aninfo));
4832 aninfo.flags |= MR_AN_ENABLE;
4833 aninfo.state = ANEG_STATE_UNKNOWN;
4834 aninfo.cur_time = 0;
4835 tick = 0;
4836 while (++tick < 195000) {
4837 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4838 if (status == ANEG_DONE || status == ANEG_FAILED)
4839 break;
4840
4841 udelay(1);
4842 }
4843
4844 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4845 tw32_f(MAC_MODE, tp->mac_mode);
4846 udelay(40);
4847
Matt Carlson5be73b42007-12-20 20:09:29 -08004848 *txflags = aninfo.txconfig;
4849 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
4851 if (status == ANEG_DONE &&
4852 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4853 MR_LP_ADV_FULL_DUPLEX)))
4854 res = 1;
4855
4856 return res;
4857}
4858
4859static void tg3_init_bcm8002(struct tg3 *tp)
4860{
4861 u32 mac_status = tr32(MAC_STATUS);
4862 int i;
4863
4864 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004865 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 !(mac_status & MAC_STATUS_PCS_SYNCED))
4867 return;
4868
4869 /* Set PLL lock range. */
4870 tg3_writephy(tp, 0x16, 0x8007);
4871
4872 /* SW reset */
4873 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4874
4875 /* Wait for reset to complete. */
4876 /* XXX schedule_timeout() ... */
4877 for (i = 0; i < 500; i++)
4878 udelay(10);
4879
4880 /* Config mode; select PMA/Ch 1 regs. */
4881 tg3_writephy(tp, 0x10, 0x8411);
4882
4883 /* Enable auto-lock and comdet, select txclk for tx. */
4884 tg3_writephy(tp, 0x11, 0x0a10);
4885
4886 tg3_writephy(tp, 0x18, 0x00a0);
4887 tg3_writephy(tp, 0x16, 0x41ff);
4888
4889 /* Assert and deassert POR. */
4890 tg3_writephy(tp, 0x13, 0x0400);
4891 udelay(40);
4892 tg3_writephy(tp, 0x13, 0x0000);
4893
4894 tg3_writephy(tp, 0x11, 0x0a50);
4895 udelay(40);
4896 tg3_writephy(tp, 0x11, 0x0a10);
4897
4898 /* Wait for signal to stabilize */
4899 /* XXX schedule_timeout() ... */
4900 for (i = 0; i < 15000; i++)
4901 udelay(10);
4902
4903 /* Deselect the channel register so we can read the PHYID
4904 * later.
4905 */
4906 tg3_writephy(tp, 0x10, 0x8011);
4907}
4908
4909static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4910{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004911 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 u32 sg_dig_ctrl, sg_dig_status;
4913 u32 serdes_cfg, expected_sg_dig_ctrl;
4914 int workaround, port_a;
4915 int current_link_up;
4916
4917 serdes_cfg = 0;
4918 expected_sg_dig_ctrl = 0;
4919 workaround = 0;
4920 port_a = 1;
4921 current_link_up = 0;
4922
4923 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4924 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4925 workaround = 1;
4926 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4927 port_a = 0;
4928
4929 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4930 /* preserve bits 20-23 for voltage regulator */
4931 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4932 }
4933
4934 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4935
4936 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004937 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 if (workaround) {
4939 u32 val = serdes_cfg;
4940
4941 if (port_a)
4942 val |= 0xc010000;
4943 else
4944 val |= 0x4010000;
4945 tw32_f(MAC_SERDES_CFG, val);
4946 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004947
4948 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 }
4950 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4951 tg3_setup_flow_control(tp, 0, 0);
4952 current_link_up = 1;
4953 }
4954 goto out;
4955 }
4956
4957 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004958 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
Matt Carlson82cd3d12007-12-20 20:09:00 -08004960 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4961 if (flowctrl & ADVERTISE_1000XPAUSE)
4962 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4963 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4964 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
4966 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004967 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004968 tp->serdes_counter &&
4969 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4970 MAC_STATUS_RCVD_CFG)) ==
4971 MAC_STATUS_PCS_SYNCED)) {
4972 tp->serdes_counter--;
4973 current_link_up = 1;
4974 goto out;
4975 }
4976restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 if (workaround)
4978 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004979 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 udelay(5);
4981 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4982
Michael Chan3d3ebe72006-09-27 15:59:15 -07004983 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004984 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4986 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004987 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 mac_status = tr32(MAC_STATUS);
4989
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004990 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004992 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Matt Carlson82cd3d12007-12-20 20:09:00 -08004994 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4995 local_adv |= ADVERTISE_1000XPAUSE;
4996 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4997 local_adv |= ADVERTISE_1000XPSE_ASYM;
4998
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004999 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005000 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005001 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005002 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
Matt Carlson859edb22011-12-08 14:40:16 +00005004 tp->link_config.rmt_adv =
5005 mii_adv_to_ethtool_adv_x(remote_adv);
5006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 tg3_setup_flow_control(tp, local_adv, remote_adv);
5008 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005009 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005010 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005011 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005012 if (tp->serdes_counter)
5013 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 else {
5015 if (workaround) {
5016 u32 val = serdes_cfg;
5017
5018 if (port_a)
5019 val |= 0xc010000;
5020 else
5021 val |= 0x4010000;
5022
5023 tw32_f(MAC_SERDES_CFG, val);
5024 }
5025
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005026 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 udelay(40);
5028
5029 /* Link parallel detection - link is up */
5030 /* only if we have PCS_SYNC and not */
5031 /* receiving config code words */
5032 mac_status = tr32(MAC_STATUS);
5033 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5034 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5035 tg3_setup_flow_control(tp, 0, 0);
5036 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005037 tp->phy_flags |=
5038 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005039 tp->serdes_counter =
5040 SERDES_PARALLEL_DET_TIMEOUT;
5041 } else
5042 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 }
5044 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005045 } else {
5046 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005047 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 }
5049
5050out:
5051 return current_link_up;
5052}
5053
5054static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
5055{
5056 int current_link_up = 0;
5057
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005058 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
5061 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005062 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005064
Matt Carlson5be73b42007-12-20 20:09:29 -08005065 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5066 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
Matt Carlson5be73b42007-12-20 20:09:29 -08005068 if (txflags & ANEG_CFG_PS1)
5069 local_adv |= ADVERTISE_1000XPAUSE;
5070 if (txflags & ANEG_CFG_PS2)
5071 local_adv |= ADVERTISE_1000XPSE_ASYM;
5072
5073 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5074 remote_adv |= LPA_1000XPAUSE;
5075 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5076 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
Matt Carlson859edb22011-12-08 14:40:16 +00005078 tp->link_config.rmt_adv =
5079 mii_adv_to_ethtool_adv_x(remote_adv);
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 tg3_setup_flow_control(tp, local_adv, remote_adv);
5082
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 current_link_up = 1;
5084 }
5085 for (i = 0; i < 30; i++) {
5086 udelay(20);
5087 tw32_f(MAC_STATUS,
5088 (MAC_STATUS_SYNC_CHANGED |
5089 MAC_STATUS_CFG_CHANGED));
5090 udelay(40);
5091 if ((tr32(MAC_STATUS) &
5092 (MAC_STATUS_SYNC_CHANGED |
5093 MAC_STATUS_CFG_CHANGED)) == 0)
5094 break;
5095 }
5096
5097 mac_status = tr32(MAC_STATUS);
5098 if (current_link_up == 0 &&
5099 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5100 !(mac_status & MAC_STATUS_RCVD_CFG))
5101 current_link_up = 1;
5102 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005103 tg3_setup_flow_control(tp, 0, 0);
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 /* Forcing 1000FD link up. */
5106 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
5108 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5109 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005110
5111 tw32_f(MAC_MODE, tp->mac_mode);
5112 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 }
5114
5115out:
5116 return current_link_up;
5117}
5118
5119static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
5120{
5121 u32 orig_pause_cfg;
5122 u16 orig_active_speed;
5123 u8 orig_active_duplex;
5124 u32 mac_status;
5125 int current_link_up;
5126 int i;
5127
Matt Carlson8d018622007-12-20 20:05:44 -08005128 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 orig_active_speed = tp->link_config.active_speed;
5130 orig_active_duplex = tp->link_config.active_duplex;
5131
Joe Perches63c3a662011-04-26 08:12:10 +00005132 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005133 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005134 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 mac_status = tr32(MAC_STATUS);
5136 mac_status &= (MAC_STATUS_PCS_SYNCED |
5137 MAC_STATUS_SIGNAL_DET |
5138 MAC_STATUS_CFG_CHANGED |
5139 MAC_STATUS_RCVD_CFG);
5140 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5141 MAC_STATUS_SIGNAL_DET)) {
5142 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5143 MAC_STATUS_CFG_CHANGED));
5144 return 0;
5145 }
5146 }
5147
5148 tw32_f(MAC_TX_AUTO_NEG, 0);
5149
5150 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5151 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5152 tw32_f(MAC_MODE, tp->mac_mode);
5153 udelay(40);
5154
Matt Carlson79eb6902010-02-17 15:17:03 +00005155 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 tg3_init_bcm8002(tp);
5157
5158 /* Enable link change event even when serdes polling. */
5159 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5160 udelay(40);
5161
5162 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005163 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 mac_status = tr32(MAC_STATUS);
5165
Joe Perches63c3a662011-04-26 08:12:10 +00005166 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5168 else
5169 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5170
Matt Carlson898a56f2009-08-28 14:02:40 +00005171 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005173 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
5175 for (i = 0; i < 100; i++) {
5176 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5177 MAC_STATUS_CFG_CHANGED));
5178 udelay(5);
5179 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005180 MAC_STATUS_CFG_CHANGED |
5181 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 break;
5183 }
5184
5185 mac_status = tr32(MAC_STATUS);
5186 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5187 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005188 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5189 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 tw32_f(MAC_MODE, (tp->mac_mode |
5191 MAC_MODE_SEND_CONFIGS));
5192 udelay(1);
5193 tw32_f(MAC_MODE, tp->mac_mode);
5194 }
5195 }
5196
5197 if (current_link_up == 1) {
5198 tp->link_config.active_speed = SPEED_1000;
5199 tp->link_config.active_duplex = DUPLEX_FULL;
5200 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5201 LED_CTRL_LNKLED_OVERRIDE |
5202 LED_CTRL_1000MBPS_ON));
5203 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005204 tp->link_config.active_speed = SPEED_UNKNOWN;
5205 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5207 LED_CTRL_LNKLED_OVERRIDE |
5208 LED_CTRL_TRAFFIC_OVERRIDE));
5209 }
5210
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005211 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005212 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 if (orig_pause_cfg != now_pause_cfg ||
5214 orig_active_speed != tp->link_config.active_speed ||
5215 orig_active_duplex != tp->link_config.active_duplex)
5216 tg3_link_report(tp);
5217 }
5218
5219 return 0;
5220}
5221
Michael Chan747e8f82005-07-25 12:33:22 -07005222static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5223{
5224 int current_link_up, err = 0;
5225 u32 bmsr, bmcr;
5226 u16 current_speed;
5227 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005228 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005229
5230 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5231 tw32_f(MAC_MODE, tp->mac_mode);
5232 udelay(40);
5233
5234 tw32(MAC_EVENT, 0);
5235
5236 tw32_f(MAC_STATUS,
5237 (MAC_STATUS_SYNC_CHANGED |
5238 MAC_STATUS_CFG_CHANGED |
5239 MAC_STATUS_MI_COMPLETION |
5240 MAC_STATUS_LNKSTATE_CHANGED));
5241 udelay(40);
5242
5243 if (force_reset)
5244 tg3_phy_reset(tp);
5245
5246 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005247 current_speed = SPEED_UNKNOWN;
5248 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005249 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005250
5251 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5252 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005253 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5254 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5255 bmsr |= BMSR_LSTATUS;
5256 else
5257 bmsr &= ~BMSR_LSTATUS;
5258 }
Michael Chan747e8f82005-07-25 12:33:22 -07005259
5260 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5261
5262 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005263 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005264 /* do nothing, just check for link up at the end */
5265 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005266 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005267
5268 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005269 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5270 ADVERTISE_1000XPAUSE |
5271 ADVERTISE_1000XPSE_ASYM |
5272 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005273
Matt Carlson28011cf2011-11-16 18:36:59 -05005274 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005275 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005276
Matt Carlson28011cf2011-11-16 18:36:59 -05005277 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5278 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005279 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5280 tg3_writephy(tp, MII_BMCR, bmcr);
5281
5282 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005283 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005284 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005285
5286 return err;
5287 }
5288 } else {
5289 u32 new_bmcr;
5290
5291 bmcr &= ~BMCR_SPEED1000;
5292 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5293
5294 if (tp->link_config.duplex == DUPLEX_FULL)
5295 new_bmcr |= BMCR_FULLDPLX;
5296
5297 if (new_bmcr != bmcr) {
5298 /* BMCR_SPEED1000 is a reserved bit that needs
5299 * to be set on write.
5300 */
5301 new_bmcr |= BMCR_SPEED1000;
5302
5303 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005304 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005305 u32 adv;
5306
5307 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5308 adv &= ~(ADVERTISE_1000XFULL |
5309 ADVERTISE_1000XHALF |
5310 ADVERTISE_SLCT);
5311 tg3_writephy(tp, MII_ADVERTISE, adv);
5312 tg3_writephy(tp, MII_BMCR, bmcr |
5313 BMCR_ANRESTART |
5314 BMCR_ANENABLE);
5315 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005316 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005317 }
5318 tg3_writephy(tp, MII_BMCR, new_bmcr);
5319 bmcr = new_bmcr;
5320 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5321 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005322 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5323 ASIC_REV_5714) {
5324 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5325 bmsr |= BMSR_LSTATUS;
5326 else
5327 bmsr &= ~BMSR_LSTATUS;
5328 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005329 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005330 }
5331 }
5332
5333 if (bmsr & BMSR_LSTATUS) {
5334 current_speed = SPEED_1000;
5335 current_link_up = 1;
5336 if (bmcr & BMCR_FULLDPLX)
5337 current_duplex = DUPLEX_FULL;
5338 else
5339 current_duplex = DUPLEX_HALF;
5340
Matt Carlsonef167e22007-12-20 20:10:01 -08005341 local_adv = 0;
5342 remote_adv = 0;
5343
Michael Chan747e8f82005-07-25 12:33:22 -07005344 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005345 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005346
5347 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5348 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5349 common = local_adv & remote_adv;
5350 if (common & (ADVERTISE_1000XHALF |
5351 ADVERTISE_1000XFULL)) {
5352 if (common & ADVERTISE_1000XFULL)
5353 current_duplex = DUPLEX_FULL;
5354 else
5355 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005356
5357 tp->link_config.rmt_adv =
5358 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005359 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005360 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005361 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005362 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005363 }
Michael Chan747e8f82005-07-25 12:33:22 -07005364 }
5365 }
5366
Matt Carlsonef167e22007-12-20 20:10:01 -08005367 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5368 tg3_setup_flow_control(tp, local_adv, remote_adv);
5369
Michael Chan747e8f82005-07-25 12:33:22 -07005370 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5371 if (tp->link_config.active_duplex == DUPLEX_HALF)
5372 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5373
5374 tw32_f(MAC_MODE, tp->mac_mode);
5375 udelay(40);
5376
5377 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5378
5379 tp->link_config.active_speed = current_speed;
5380 tp->link_config.active_duplex = current_duplex;
5381
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005382 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005383 return err;
5384}
5385
5386static void tg3_serdes_parallel_detect(struct tg3 *tp)
5387{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005388 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005389 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005390 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005391 return;
5392 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005393
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005394 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005395 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5396 u32 bmcr;
5397
5398 tg3_readphy(tp, MII_BMCR, &bmcr);
5399 if (bmcr & BMCR_ANENABLE) {
5400 u32 phy1, phy2;
5401
5402 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005403 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5404 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005405
5406 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005407 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5408 MII_TG3_DSP_EXP1_INT_STAT);
5409 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5410 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005411
5412 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5413 /* We have signal detect and not receiving
5414 * config code words, link is up by parallel
5415 * detection.
5416 */
5417
5418 bmcr &= ~BMCR_ANENABLE;
5419 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5420 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005421 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005422 }
5423 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005424 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005425 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005426 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005427 u32 phy2;
5428
5429 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005430 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5431 MII_TG3_DSP_EXP1_INT_STAT);
5432 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005433 if (phy2 & 0x20) {
5434 u32 bmcr;
5435
5436 /* Config code words received, turn on autoneg. */
5437 tg3_readphy(tp, MII_BMCR, &bmcr);
5438 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5439
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005440 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005441
5442 }
5443 }
5444}
5445
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5447{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005448 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 int err;
5450
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005451 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005453 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005454 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005455 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005458 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005459 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005460
5461 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5462 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5463 scale = 65;
5464 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5465 scale = 6;
5466 else
5467 scale = 12;
5468
5469 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5470 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5471 tw32(GRC_MISC_CFG, val);
5472 }
5473
Matt Carlsonf2096f92011-04-05 14:22:48 +00005474 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5475 (6 << TX_LENGTHS_IPG_SHIFT);
5476 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5477 val |= tr32(MAC_TX_LENGTHS) &
5478 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5479 TX_LENGTHS_CNT_DWN_VAL_MSK);
5480
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 if (tp->link_config.active_speed == SPEED_1000 &&
5482 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005483 tw32(MAC_TX_LENGTHS, val |
5484 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005486 tw32(MAC_TX_LENGTHS, val |
5487 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
Joe Perches63c3a662011-04-26 08:12:10 +00005489 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005490 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005492 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 } else {
5494 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5495 }
5496 }
5497
Joe Perches63c3a662011-04-26 08:12:10 +00005498 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005499 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005500 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07005501 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5502 tp->pwrmgmt_thresh;
5503 else
5504 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5505 tw32(PCIE_PWR_MGMT_THRESH, val);
5506 }
5507
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 return err;
5509}
5510
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005511static inline int tg3_irq_sync(struct tg3 *tp)
5512{
5513 return tp->irq_sync;
5514}
5515
Matt Carlson97bd8e42011-04-13 11:05:04 +00005516static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5517{
5518 int i;
5519
5520 dst = (u32 *)((u8 *)dst + off);
5521 for (i = 0; i < len; i += sizeof(u32))
5522 *dst++ = tr32(off + i);
5523}
5524
5525static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5526{
5527 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5528 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5529 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5530 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5531 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5532 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5533 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5534 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5535 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5536 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5537 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5538 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5539 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5540 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5541 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5542 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5543 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5544 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5545 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5546
Joe Perches63c3a662011-04-26 08:12:10 +00005547 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005548 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5549
5550 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5551 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5552 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5553 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5554 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5555 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5556 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5557 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5558
Joe Perches63c3a662011-04-26 08:12:10 +00005559 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005560 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5561 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5562 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5563 }
5564
5565 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5566 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5567 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5568 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5569 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5570
Joe Perches63c3a662011-04-26 08:12:10 +00005571 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005572 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5573}
5574
5575static void tg3_dump_state(struct tg3 *tp)
5576{
5577 int i;
5578 u32 *regs;
5579
5580 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5581 if (!regs) {
5582 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5583 return;
5584 }
5585
Joe Perches63c3a662011-04-26 08:12:10 +00005586 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005587 /* Read up to but not including private PCI registers */
5588 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5589 regs[i / sizeof(u32)] = tr32(i);
5590 } else
5591 tg3_dump_legacy_regs(tp, regs);
5592
5593 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5594 if (!regs[i + 0] && !regs[i + 1] &&
5595 !regs[i + 2] && !regs[i + 3])
5596 continue;
5597
5598 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5599 i * 4,
5600 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5601 }
5602
5603 kfree(regs);
5604
5605 for (i = 0; i < tp->irq_cnt; i++) {
5606 struct tg3_napi *tnapi = &tp->napi[i];
5607
5608 /* SW status block */
5609 netdev_err(tp->dev,
5610 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5611 i,
5612 tnapi->hw_status->status,
5613 tnapi->hw_status->status_tag,
5614 tnapi->hw_status->rx_jumbo_consumer,
5615 tnapi->hw_status->rx_consumer,
5616 tnapi->hw_status->rx_mini_consumer,
5617 tnapi->hw_status->idx[0].rx_producer,
5618 tnapi->hw_status->idx[0].tx_consumer);
5619
5620 netdev_err(tp->dev,
5621 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5622 i,
5623 tnapi->last_tag, tnapi->last_irq_tag,
5624 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5625 tnapi->rx_rcb_ptr,
5626 tnapi->prodring.rx_std_prod_idx,
5627 tnapi->prodring.rx_std_cons_idx,
5628 tnapi->prodring.rx_jmb_prod_idx,
5629 tnapi->prodring.rx_jmb_cons_idx);
5630 }
5631}
5632
Michael Chandf3e6542006-05-26 17:48:07 -07005633/* This is called whenever we suspect that the system chipset is re-
5634 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5635 * is bogus tx completions. We try to recover by setting the
5636 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5637 * in the workqueue.
5638 */
5639static void tg3_tx_recover(struct tg3 *tp)
5640{
Joe Perches63c3a662011-04-26 08:12:10 +00005641 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005642 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5643
Matt Carlson5129c3a2010-04-05 10:19:23 +00005644 netdev_warn(tp->dev,
5645 "The system may be re-ordering memory-mapped I/O "
5646 "cycles to the network device, attempting to recover. "
5647 "Please report the problem to the driver maintainer "
5648 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005649
5650 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005651 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005652 spin_unlock(&tp->lock);
5653}
5654
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005655static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005656{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005657 /* Tell compiler to fetch tx indices from memory. */
5658 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005659 return tnapi->tx_pending -
5660 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005661}
5662
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663/* Tigon3 never reports partial packet sends. So we do not
5664 * need special logic to handle SKBs that have not had all
5665 * of their frags sent yet, like SunGEM does.
5666 */
Matt Carlson17375d22009-08-28 14:02:18 +00005667static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668{
Matt Carlson17375d22009-08-28 14:02:18 +00005669 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005670 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005671 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005672 struct netdev_queue *txq;
5673 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005674 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005675
Joe Perches63c3a662011-04-26 08:12:10 +00005676 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005677 index--;
5678
5679 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
5681 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005682 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005684 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
Michael Chandf3e6542006-05-26 17:48:07 -07005686 if (unlikely(skb == NULL)) {
5687 tg3_tx_recover(tp);
5688 return;
5689 }
5690
Alexander Duyckf4188d82009-12-02 16:48:38 +00005691 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005692 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005693 skb_headlen(skb),
5694 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
5696 ri->skb = NULL;
5697
Matt Carlsone01ee142011-07-27 14:20:50 +00005698 while (ri->fragmented) {
5699 ri->fragmented = false;
5700 sw_idx = NEXT_TX(sw_idx);
5701 ri = &tnapi->tx_buffers[sw_idx];
5702 }
5703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 sw_idx = NEXT_TX(sw_idx);
5705
5706 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005707 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005708 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5709 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005710
5711 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005712 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005713 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005714 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005715
5716 while (ri->fragmented) {
5717 ri->fragmented = false;
5718 sw_idx = NEXT_TX(sw_idx);
5719 ri = &tnapi->tx_buffers[sw_idx];
5720 }
5721
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 sw_idx = NEXT_TX(sw_idx);
5723 }
5724
Tom Herbert298376d2011-11-28 16:33:30 +00005725 pkts_compl++;
5726 bytes_compl += skb->len;
5727
David S. Millerf47c11e2005-06-24 20:18:35 -07005728 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005729
5730 if (unlikely(tx_bug)) {
5731 tg3_tx_recover(tp);
5732 return;
5733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 }
5735
Tom Herbert5cb917b2012-03-05 19:53:50 +00005736 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00005737
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005738 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
Michael Chan1b2a7202006-08-07 21:46:02 -07005740 /* Need to make the tx_cons update visible to tg3_start_xmit()
5741 * before checking for netif_queue_stopped(). Without the
5742 * memory barrier, there is a small possibility that tg3_start_xmit()
5743 * will miss it and cause the queue to be stopped forever.
5744 */
5745 smp_mb();
5746
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005747 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005748 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005749 __netif_tx_lock(txq, smp_processor_id());
5750 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005751 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005752 netif_tx_wake_queue(txq);
5753 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755}
5756
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005757static void tg3_frag_free(bool is_frag, void *data)
5758{
5759 if (is_frag)
5760 put_page(virt_to_head_page(data));
5761 else
5762 kfree(data);
5763}
5764
Eric Dumazet9205fd92011-11-18 06:47:01 +00005765static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005766{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005767 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
5768 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5769
Eric Dumazet9205fd92011-11-18 06:47:01 +00005770 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005771 return;
5772
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005773 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005774 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005775 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005776 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005777}
5778
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005779
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780/* Returns size of skb allocated or < 0 on error.
5781 *
5782 * We only need to fill in the address because the other members
5783 * of the RX descriptor are invariant, see tg3_init_rings.
5784 *
5785 * Note the purposeful assymetry of cpu vs. chip accesses. For
5786 * posting buffers we only dirty the first cache line of the RX
5787 * descriptor (containing the address). Whereas for the RX status
5788 * buffers the cpu only reads the last cacheline of the RX descriptor
5789 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5790 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005791static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005792 u32 opaque_key, u32 dest_idx_unmasked,
5793 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794{
5795 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005796 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005797 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005799 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 switch (opaque_key) {
5802 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005803 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005804 desc = &tpr->rx_std[dest_idx];
5805 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005806 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 break;
5808
5809 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005810 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005811 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005812 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005813 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 break;
5815
5816 default:
5817 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819
5820 /* Do not overwrite any of the map or rp information
5821 * until we are sure we can commit to a new buffer.
5822 *
5823 * Callers depend upon this behavior and assume that
5824 * we leave everything unchanged if we fail.
5825 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005826 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5827 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005828 if (skb_size <= PAGE_SIZE) {
5829 data = netdev_alloc_frag(skb_size);
5830 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005831 } else {
5832 data = kmalloc(skb_size, GFP_ATOMIC);
5833 *frag_size = 0;
5834 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00005835 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 return -ENOMEM;
5837
Eric Dumazet9205fd92011-11-18 06:47:01 +00005838 mapping = pci_map_single(tp->pdev,
5839 data + TG3_RX_OFFSET(tp),
5840 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005842 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005843 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005844 return -EIO;
5845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
Eric Dumazet9205fd92011-11-18 06:47:01 +00005847 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005848 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 desc->addr_hi = ((u64)mapping >> 32);
5851 desc->addr_lo = ((u64)mapping & 0xffffffff);
5852
Eric Dumazet9205fd92011-11-18 06:47:01 +00005853 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854}
5855
5856/* We only need to move over in the address because the other
5857 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005858 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 */
Matt Carlsona3896162009-11-13 13:03:44 +00005860static void tg3_recycle_rx(struct tg3_napi *tnapi,
5861 struct tg3_rx_prodring_set *dpr,
5862 u32 opaque_key, int src_idx,
5863 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864{
Matt Carlson17375d22009-08-28 14:02:18 +00005865 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5867 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005868 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005869 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
5871 switch (opaque_key) {
5872 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005873 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005874 dest_desc = &dpr->rx_std[dest_idx];
5875 dest_map = &dpr->rx_std_buffers[dest_idx];
5876 src_desc = &spr->rx_std[src_idx];
5877 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 break;
5879
5880 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005881 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005882 dest_desc = &dpr->rx_jmb[dest_idx].std;
5883 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5884 src_desc = &spr->rx_jmb[src_idx].std;
5885 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 break;
5887
5888 default:
5889 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
Eric Dumazet9205fd92011-11-18 06:47:01 +00005892 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005893 dma_unmap_addr_set(dest_map, mapping,
5894 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 dest_desc->addr_hi = src_desc->addr_hi;
5896 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005897
5898 /* Ensure that the update to the skb happens after the physical
5899 * addresses have been transferred to the new BD location.
5900 */
5901 smp_wmb();
5902
Eric Dumazet9205fd92011-11-18 06:47:01 +00005903 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904}
5905
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906/* The RX ring scheme is composed of multiple rings which post fresh
5907 * buffers to the chip, and one special ring the chip uses to report
5908 * status back to the host.
5909 *
5910 * The special ring reports the status of received packets to the
5911 * host. The chip does not write into the original descriptor the
5912 * RX buffer was obtained from. The chip simply takes the original
5913 * descriptor as provided by the host, updates the status and length
5914 * field, then writes this into the next status ring entry.
5915 *
5916 * Each ring the host uses to post buffers to the chip is described
5917 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5918 * it is first placed into the on-chip ram. When the packet's length
5919 * is known, it walks down the TG3_BDINFO entries to select the ring.
5920 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5921 * which is within the range of the new packet's length is chosen.
5922 *
5923 * The "separate ring for rx status" scheme may sound queer, but it makes
5924 * sense from a cache coherency perspective. If only the host writes
5925 * to the buffer post rings, and only the chip writes to the rx status
5926 * rings, then cache lines never move beyond shared-modified state.
5927 * If both the host and chip were to write into the same ring, cache line
5928 * eviction could occur since both entities want it in an exclusive state.
5929 */
Matt Carlson17375d22009-08-28 14:02:18 +00005930static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931{
Matt Carlson17375d22009-08-28 14:02:18 +00005932 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005933 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005934 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005935 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005936 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005938 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005940 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 /*
5942 * We need to order the read of hw_idx and the read of
5943 * the opaque cookie.
5944 */
5945 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 work_mask = 0;
5947 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005948 std_prod_idx = tpr->rx_std_prod_idx;
5949 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005951 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005952 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 unsigned int len;
5954 struct sk_buff *skb;
5955 dma_addr_t dma_addr;
5956 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005957 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958
5959 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5960 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5961 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005962 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005963 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005964 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005965 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005966 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005968 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005969 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005970 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005971 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005972 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974
5975 work_mask |= opaque_key;
5976
5977 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5978 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5979 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005980 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 desc_idx, *post_ptr);
5982 drop_it_no_recycle:
5983 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005984 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 goto next_pkt;
5986 }
5987
Eric Dumazet9205fd92011-11-18 06:47:01 +00005988 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005989 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5990 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
Matt Carlsond2757fc2010-04-12 06:58:27 +00005992 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005994 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995
Eric Dumazet9205fd92011-11-18 06:47:01 +00005996 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005997 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 if (skb_size < 0)
5999 goto drop_it;
6000
Matt Carlson287be122009-08-28 13:58:46 +00006001 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 PCI_DMA_FROMDEVICE);
6003
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006004 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006005 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006006 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006007 goto drop_it_no_recycle;
6008 }
6009 skb_reserve(skb, TG3_RX_OFFSET(tp));
6010 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006011 * after the usage of the old DMA mapping.
6012 */
6013 smp_wmb();
6014
Eric Dumazet9205fd92011-11-18 06:47:01 +00006015 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006016
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006018 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 desc_idx, *post_ptr);
6020
Eric Dumazet9205fd92011-11-18 06:47:01 +00006021 skb = netdev_alloc_skb(tp->dev,
6022 len + TG3_RAW_IP_ALIGN);
6023 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 goto drop_it_no_recycle;
6025
Eric Dumazet9205fd92011-11-18 06:47:01 +00006026 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006028 memcpy(skb->data,
6029 data + TG3_RX_OFFSET(tp),
6030 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 }
6033
Eric Dumazet9205fd92011-11-18 06:47:01 +00006034 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00006035 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6037 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6038 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6039 skb->ip_summed = CHECKSUM_UNNECESSARY;
6040 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006041 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
6043 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006044
6045 if (len > (tp->dev->mtu + ETH_HLEN) &&
6046 skb->protocol != htons(ETH_P_8021Q)) {
6047 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006048 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006049 }
6050
Matt Carlson9dc7a112010-04-12 06:58:28 +00006051 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006052 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
6053 __vlan_hwaccel_put_tag(skb,
6054 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006055
Matt Carlsonbf933c82011-01-25 15:58:49 +00006056 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 received++;
6059 budget--;
6060
6061next_pkt:
6062 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006063
6064 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006065 tpr->rx_std_prod_idx = std_prod_idx &
6066 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006067 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6068 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006069 work_mask &= ~RXD_OPAQUE_RING_STD;
6070 rx_std_posted = 0;
6071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006073 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006074 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006075
6076 /* Refresh hw_idx to see if there is new work */
6077 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006078 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006079 rmb();
6080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 }
6082
6083 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006084 tnapi->rx_rcb_ptr = sw_idx;
6085 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
6087 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006088 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006089 /* Sync BD data before updating mailbox */
6090 wmb();
6091
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006092 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006093 tpr->rx_std_prod_idx = std_prod_idx &
6094 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006095 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6096 tpr->rx_std_prod_idx);
6097 }
6098 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006099 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6100 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006101 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6102 tpr->rx_jmb_prod_idx);
6103 }
6104 mmiowb();
6105 } else if (work_mask) {
6106 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6107 * updated before the producer indices can be updated.
6108 */
6109 smp_wmb();
6110
Matt Carlson2c49a442010-09-30 10:34:35 +00006111 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6112 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006113
Michael Chan7ae52892012-03-21 15:38:33 +00006114 if (tnapi != &tp->napi[1]) {
6115 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006116 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
6120 return received;
6121}
6122
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006123static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006126 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006127 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6128
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 if (sblk->status & SD_STATUS_LINK_CHG) {
6130 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006131 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006132 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006133 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006134 tw32_f(MAC_STATUS,
6135 (MAC_STATUS_SYNC_CHANGED |
6136 MAC_STATUS_CFG_CHANGED |
6137 MAC_STATUS_MI_COMPLETION |
6138 MAC_STATUS_LNKSTATE_CHANGED));
6139 udelay(40);
6140 } else
6141 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006142 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 }
6144 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006145}
6146
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006147static int tg3_rx_prodring_xfer(struct tg3 *tp,
6148 struct tg3_rx_prodring_set *dpr,
6149 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006150{
6151 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006152 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006153
6154 while (1) {
6155 src_prod_idx = spr->rx_std_prod_idx;
6156
6157 /* Make sure updates to the rx_std_buffers[] entries and the
6158 * standard producer index are seen in the correct order.
6159 */
6160 smp_rmb();
6161
6162 if (spr->rx_std_cons_idx == src_prod_idx)
6163 break;
6164
6165 if (spr->rx_std_cons_idx < src_prod_idx)
6166 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6167 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006168 cpycnt = tp->rx_std_ring_mask + 1 -
6169 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006170
Matt Carlson2c49a442010-09-30 10:34:35 +00006171 cpycnt = min(cpycnt,
6172 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006173
6174 si = spr->rx_std_cons_idx;
6175 di = dpr->rx_std_prod_idx;
6176
Matt Carlsone92967b2010-02-12 14:47:06 +00006177 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006178 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006179 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006180 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006181 break;
6182 }
6183 }
6184
6185 if (!cpycnt)
6186 break;
6187
6188 /* Ensure that updates to the rx_std_buffers ring and the
6189 * shadowed hardware producer ring from tg3_recycle_skb() are
6190 * ordered correctly WRT the skb check above.
6191 */
6192 smp_rmb();
6193
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006194 memcpy(&dpr->rx_std_buffers[di],
6195 &spr->rx_std_buffers[si],
6196 cpycnt * sizeof(struct ring_info));
6197
6198 for (i = 0; i < cpycnt; i++, di++, si++) {
6199 struct tg3_rx_buffer_desc *sbd, *dbd;
6200 sbd = &spr->rx_std[si];
6201 dbd = &dpr->rx_std[di];
6202 dbd->addr_hi = sbd->addr_hi;
6203 dbd->addr_lo = sbd->addr_lo;
6204 }
6205
Matt Carlson2c49a442010-09-30 10:34:35 +00006206 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6207 tp->rx_std_ring_mask;
6208 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6209 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006210 }
6211
6212 while (1) {
6213 src_prod_idx = spr->rx_jmb_prod_idx;
6214
6215 /* Make sure updates to the rx_jmb_buffers[] entries and
6216 * the jumbo producer index are seen in the correct order.
6217 */
6218 smp_rmb();
6219
6220 if (spr->rx_jmb_cons_idx == src_prod_idx)
6221 break;
6222
6223 if (spr->rx_jmb_cons_idx < src_prod_idx)
6224 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6225 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006226 cpycnt = tp->rx_jmb_ring_mask + 1 -
6227 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006228
6229 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006230 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006231
6232 si = spr->rx_jmb_cons_idx;
6233 di = dpr->rx_jmb_prod_idx;
6234
Matt Carlsone92967b2010-02-12 14:47:06 +00006235 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006236 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006237 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006238 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006239 break;
6240 }
6241 }
6242
6243 if (!cpycnt)
6244 break;
6245
6246 /* Ensure that updates to the rx_jmb_buffers ring and the
6247 * shadowed hardware producer ring from tg3_recycle_skb() are
6248 * ordered correctly WRT the skb check above.
6249 */
6250 smp_rmb();
6251
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006252 memcpy(&dpr->rx_jmb_buffers[di],
6253 &spr->rx_jmb_buffers[si],
6254 cpycnt * sizeof(struct ring_info));
6255
6256 for (i = 0; i < cpycnt; i++, di++, si++) {
6257 struct tg3_rx_buffer_desc *sbd, *dbd;
6258 sbd = &spr->rx_jmb[si].std;
6259 dbd = &dpr->rx_jmb[di].std;
6260 dbd->addr_hi = sbd->addr_hi;
6261 dbd->addr_lo = sbd->addr_lo;
6262 }
6263
Matt Carlson2c49a442010-09-30 10:34:35 +00006264 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6265 tp->rx_jmb_ring_mask;
6266 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6267 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006268 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006269
6270 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006271}
6272
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006273static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6274{
6275 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276
6277 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006278 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006279 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006280 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006281 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 }
6283
Matt Carlsonf891ea12012-04-24 13:37:01 +00006284 if (!tnapi->rx_rcb_prod_idx)
6285 return work_done;
6286
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 /* run RX thread, within the bounds set by NAPI.
6288 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006289 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006291 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006292 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
Joe Perches63c3a662011-04-26 08:12:10 +00006294 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006295 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006296 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006297 u32 std_prod_idx = dpr->rx_std_prod_idx;
6298 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006299
Michael Chan7ae52892012-03-21 15:38:33 +00006300 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00006301 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006302 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006303 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006304
6305 wmb();
6306
Matt Carlsone4af1af2010-02-12 14:47:05 +00006307 if (std_prod_idx != dpr->rx_std_prod_idx)
6308 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6309 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006310
Matt Carlsone4af1af2010-02-12 14:47:05 +00006311 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6312 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6313 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006314
6315 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006316
6317 if (err)
6318 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006319 }
6320
David S. Miller6f535762007-10-11 18:08:29 -07006321 return work_done;
6322}
David S. Millerf7383c22005-05-18 22:50:53 -07006323
Matt Carlsondb219972011-11-04 09:15:03 +00006324static inline void tg3_reset_task_schedule(struct tg3 *tp)
6325{
6326 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6327 schedule_work(&tp->reset_task);
6328}
6329
6330static inline void tg3_reset_task_cancel(struct tg3 *tp)
6331{
6332 cancel_work_sync(&tp->reset_task);
6333 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006334 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006335}
6336
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006337static int tg3_poll_msix(struct napi_struct *napi, int budget)
6338{
6339 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6340 struct tg3 *tp = tnapi->tp;
6341 int work_done = 0;
6342 struct tg3_hw_status *sblk = tnapi->hw_status;
6343
6344 while (1) {
6345 work_done = tg3_poll_work(tnapi, work_done, budget);
6346
Joe Perches63c3a662011-04-26 08:12:10 +00006347 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006348 goto tx_recovery;
6349
6350 if (unlikely(work_done >= budget))
6351 break;
6352
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006353 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006354 * to tell the hw how much work has been processed,
6355 * so we must read it before checking for more work.
6356 */
6357 tnapi->last_tag = sblk->status_tag;
6358 tnapi->last_irq_tag = tnapi->last_tag;
6359 rmb();
6360
6361 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006362 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6363 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006364
6365 /* This test here is not race free, but will reduce
6366 * the number of interrupts by looping again.
6367 */
6368 if (tnapi == &tp->napi[1] && tp->rx_refill)
6369 continue;
6370
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006371 napi_complete(napi);
6372 /* Reenable interrupts. */
6373 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006374
6375 /* This test here is synchronized by napi_schedule()
6376 * and napi_complete() to close the race condition.
6377 */
6378 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6379 tw32(HOSTCC_MODE, tp->coalesce_mode |
6380 HOSTCC_MODE_ENABLE |
6381 tnapi->coal_now);
6382 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006383 mmiowb();
6384 break;
6385 }
6386 }
6387
6388 return work_done;
6389
6390tx_recovery:
6391 /* work_done is guaranteed to be less than budget. */
6392 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006393 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006394 return work_done;
6395}
6396
Matt Carlsone64de4e2011-04-13 11:05:05 +00006397static void tg3_process_error(struct tg3 *tp)
6398{
6399 u32 val;
6400 bool real_error = false;
6401
Joe Perches63c3a662011-04-26 08:12:10 +00006402 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006403 return;
6404
6405 /* Check Flow Attention register */
6406 val = tr32(HOSTCC_FLOW_ATTN);
6407 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6408 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6409 real_error = true;
6410 }
6411
6412 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6413 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6414 real_error = true;
6415 }
6416
6417 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6418 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6419 real_error = true;
6420 }
6421
6422 if (!real_error)
6423 return;
6424
6425 tg3_dump_state(tp);
6426
Joe Perches63c3a662011-04-26 08:12:10 +00006427 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006428 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006429}
6430
David S. Miller6f535762007-10-11 18:08:29 -07006431static int tg3_poll(struct napi_struct *napi, int budget)
6432{
Matt Carlson8ef04422009-08-28 14:01:37 +00006433 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6434 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006435 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006436 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006437
6438 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006439 if (sblk->status & SD_STATUS_ERROR)
6440 tg3_process_error(tp);
6441
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006442 tg3_poll_link(tp);
6443
Matt Carlson17375d22009-08-28 14:02:18 +00006444 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006445
Joe Perches63c3a662011-04-26 08:12:10 +00006446 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006447 goto tx_recovery;
6448
6449 if (unlikely(work_done >= budget))
6450 break;
6451
Joe Perches63c3a662011-04-26 08:12:10 +00006452 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006453 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006454 * to tell the hw how much work has been processed,
6455 * so we must read it before checking for more work.
6456 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006457 tnapi->last_tag = sblk->status_tag;
6458 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006459 rmb();
6460 } else
6461 sblk->status &= ~SD_STATUS_UPDATED;
6462
Matt Carlson17375d22009-08-28 14:02:18 +00006463 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006464 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006465 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006466 break;
6467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 }
6469
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006470 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006471
6472tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006473 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006474 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006475 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006476 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477}
6478
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006479static void tg3_napi_disable(struct tg3 *tp)
6480{
6481 int i;
6482
6483 for (i = tp->irq_cnt - 1; i >= 0; i--)
6484 napi_disable(&tp->napi[i].napi);
6485}
6486
6487static void tg3_napi_enable(struct tg3 *tp)
6488{
6489 int i;
6490
6491 for (i = 0; i < tp->irq_cnt; i++)
6492 napi_enable(&tp->napi[i].napi);
6493}
6494
6495static void tg3_napi_init(struct tg3 *tp)
6496{
6497 int i;
6498
6499 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6500 for (i = 1; i < tp->irq_cnt; i++)
6501 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6502}
6503
6504static void tg3_napi_fini(struct tg3 *tp)
6505{
6506 int i;
6507
6508 for (i = 0; i < tp->irq_cnt; i++)
6509 netif_napi_del(&tp->napi[i].napi);
6510}
6511
6512static inline void tg3_netif_stop(struct tg3 *tp)
6513{
6514 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6515 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006516 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006517 netif_tx_disable(tp->dev);
6518}
6519
6520static inline void tg3_netif_start(struct tg3 *tp)
6521{
6522 /* NOTE: unconditional netif_tx_wake_all_queues is only
6523 * appropriate so long as all callers are assured to
6524 * have free tx slots (such as after tg3_init_hw)
6525 */
6526 netif_tx_wake_all_queues(tp->dev);
6527
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006528 if (tp->link_up)
6529 netif_carrier_on(tp->dev);
6530
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006531 tg3_napi_enable(tp);
6532 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6533 tg3_enable_ints(tp);
6534}
6535
David S. Millerf47c11e2005-06-24 20:18:35 -07006536static void tg3_irq_quiesce(struct tg3 *tp)
6537{
Matt Carlson4f125f42009-09-01 12:55:02 +00006538 int i;
6539
David S. Millerf47c11e2005-06-24 20:18:35 -07006540 BUG_ON(tp->irq_sync);
6541
6542 tp->irq_sync = 1;
6543 smp_mb();
6544
Matt Carlson4f125f42009-09-01 12:55:02 +00006545 for (i = 0; i < tp->irq_cnt; i++)
6546 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006547}
6548
David S. Millerf47c11e2005-06-24 20:18:35 -07006549/* Fully shutdown all tg3 driver activity elsewhere in the system.
6550 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6551 * with as well. Most of the time, this is not necessary except when
6552 * shutting down the device.
6553 */
6554static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6555{
Michael Chan46966542007-07-11 19:47:19 -07006556 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006557 if (irq_sync)
6558 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006559}
6560
6561static inline void tg3_full_unlock(struct tg3 *tp)
6562{
David S. Millerf47c11e2005-06-24 20:18:35 -07006563 spin_unlock_bh(&tp->lock);
6564}
6565
Michael Chanfcfa0a32006-03-20 22:28:41 -08006566/* One-shot MSI handler - Chip automatically disables interrupt
6567 * after sending MSI so driver doesn't have to do it.
6568 */
David Howells7d12e782006-10-05 14:55:46 +01006569static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006570{
Matt Carlson09943a12009-08-28 14:01:57 +00006571 struct tg3_napi *tnapi = dev_id;
6572 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006573
Matt Carlson898a56f2009-08-28 14:02:40 +00006574 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006575 if (tnapi->rx_rcb)
6576 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006577
6578 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006579 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006580
6581 return IRQ_HANDLED;
6582}
6583
Michael Chan88b06bc22005-04-21 17:13:25 -07006584/* MSI ISR - No need to check for interrupt sharing and no need to
6585 * flush status block and interrupt mailbox. PCI ordering rules
6586 * guarantee that MSI will arrive after the status block.
6587 */
David Howells7d12e782006-10-05 14:55:46 +01006588static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006589{
Matt Carlson09943a12009-08-28 14:01:57 +00006590 struct tg3_napi *tnapi = dev_id;
6591 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006592
Matt Carlson898a56f2009-08-28 14:02:40 +00006593 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006594 if (tnapi->rx_rcb)
6595 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006596 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006597 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006598 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006599 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006600 * NIC to stop sending us irqs, engaging "in-intr-handler"
6601 * event coalescing.
6602 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006603 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006604 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006605 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006606
Michael Chan88b06bc22005-04-21 17:13:25 -07006607 return IRQ_RETVAL(1);
6608}
6609
David Howells7d12e782006-10-05 14:55:46 +01006610static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611{
Matt Carlson09943a12009-08-28 14:01:57 +00006612 struct tg3_napi *tnapi = dev_id;
6613 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006614 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 unsigned int handled = 1;
6616
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 /* In INTx mode, it is possible for the interrupt to arrive at
6618 * the CPU before the status block posted prior to the interrupt.
6619 * Reading the PCI State register will confirm whether the
6620 * interrupt is ours and will flush the status block.
6621 */
Michael Chand18edcb2007-03-24 20:57:11 -07006622 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006623 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006624 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6625 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006626 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006627 }
Michael Chand18edcb2007-03-24 20:57:11 -07006628 }
6629
6630 /*
6631 * Writing any value to intr-mbox-0 clears PCI INTA# and
6632 * chip-internal interrupt pending events.
6633 * Writing non-zero to intr-mbox-0 additional tells the
6634 * NIC to stop sending us irqs, engaging "in-intr-handler"
6635 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006636 *
6637 * Flush the mailbox to de-assert the IRQ immediately to prevent
6638 * spurious interrupts. The flush impacts performance but
6639 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006640 */
Michael Chanc04cb342007-05-07 00:26:15 -07006641 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006642 if (tg3_irq_sync(tp))
6643 goto out;
6644 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006645 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006646 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006647 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006648 } else {
6649 /* No work, shared interrupt perhaps? re-enable
6650 * interrupts, and flush that PCI write
6651 */
6652 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6653 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006654 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006655out:
David S. Millerfac9b832005-05-18 22:46:34 -07006656 return IRQ_RETVAL(handled);
6657}
6658
David Howells7d12e782006-10-05 14:55:46 +01006659static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006660{
Matt Carlson09943a12009-08-28 14:01:57 +00006661 struct tg3_napi *tnapi = dev_id;
6662 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006663 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006664 unsigned int handled = 1;
6665
David S. Millerfac9b832005-05-18 22:46:34 -07006666 /* In INTx mode, it is possible for the interrupt to arrive at
6667 * the CPU before the status block posted prior to the interrupt.
6668 * Reading the PCI State register will confirm whether the
6669 * interrupt is ours and will flush the status block.
6670 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006671 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006672 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006673 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6674 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006675 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676 }
Michael Chand18edcb2007-03-24 20:57:11 -07006677 }
6678
6679 /*
6680 * writing any value to intr-mbox-0 clears PCI INTA# and
6681 * chip-internal interrupt pending events.
6682 * writing non-zero to intr-mbox-0 additional tells the
6683 * NIC to stop sending us irqs, engaging "in-intr-handler"
6684 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006685 *
6686 * Flush the mailbox to de-assert the IRQ immediately to prevent
6687 * spurious interrupts. The flush impacts performance but
6688 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006689 */
Michael Chanc04cb342007-05-07 00:26:15 -07006690 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006691
6692 /*
6693 * In a shared interrupt configuration, sometimes other devices'
6694 * interrupts will scream. We record the current status tag here
6695 * so that the above check can report that the screaming interrupts
6696 * are unhandled. Eventually they will be silenced.
6697 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006698 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006699
Michael Chand18edcb2007-03-24 20:57:11 -07006700 if (tg3_irq_sync(tp))
6701 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006702
Matt Carlson72334482009-08-28 14:03:01 +00006703 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006704
Matt Carlson09943a12009-08-28 14:01:57 +00006705 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006706
David S. Millerf47c11e2005-06-24 20:18:35 -07006707out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 return IRQ_RETVAL(handled);
6709}
6710
Michael Chan79381092005-04-21 17:13:59 -07006711/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006712static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006713{
Matt Carlson09943a12009-08-28 14:01:57 +00006714 struct tg3_napi *tnapi = dev_id;
6715 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006716 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006717
Michael Chanf9804dd2005-09-27 12:13:10 -07006718 if ((sblk->status & SD_STATUS_UPDATED) ||
6719 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006720 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006721 return IRQ_RETVAL(1);
6722 }
6723 return IRQ_RETVAL(0);
6724}
6725
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726#ifdef CONFIG_NET_POLL_CONTROLLER
6727static void tg3_poll_controller(struct net_device *dev)
6728{
Matt Carlson4f125f42009-09-01 12:55:02 +00006729 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006730 struct tg3 *tp = netdev_priv(dev);
6731
Matt Carlson4f125f42009-09-01 12:55:02 +00006732 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006733 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734}
6735#endif
6736
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737static void tg3_tx_timeout(struct net_device *dev)
6738{
6739 struct tg3 *tp = netdev_priv(dev);
6740
Michael Chanb0408752007-02-13 12:18:30 -08006741 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006742 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006743 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745
Matt Carlsondb219972011-11-04 09:15:03 +00006746 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747}
6748
Michael Chanc58ec932005-09-17 00:46:27 -07006749/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6750static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6751{
6752 u32 base = (u32) mapping & 0xffffffff;
6753
Eric Dumazet807540b2010-09-23 05:40:09 +00006754 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006755}
6756
Michael Chan72f2afb2006-03-06 19:28:35 -08006757/* Test for DMA addresses > 40-bit */
6758static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6759 int len)
6760{
6761#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006762 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006763 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006764 return 0;
6765#else
6766 return 0;
6767#endif
6768}
6769
Matt Carlsond1a3b732011-07-27 14:20:51 +00006770static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006771 dma_addr_t mapping, u32 len, u32 flags,
6772 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006773{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006774 txbd->addr_hi = ((u64) mapping >> 32);
6775 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6776 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6777 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006778}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779
Matt Carlson84b67b22011-07-27 14:20:52 +00006780static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006781 dma_addr_t map, u32 len, u32 flags,
6782 u32 mss, u32 vlan)
6783{
6784 struct tg3 *tp = tnapi->tp;
6785 bool hwbug = false;
6786
6787 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00006788 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006789
6790 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006791 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006792
6793 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006794 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006795
Matt Carlsona4cb4282011-12-14 11:09:58 +00006796 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006797 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006798 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006799 while (len > tp->dma_limit && *budget) {
6800 u32 frag_len = tp->dma_limit;
6801 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006802
Matt Carlsonb9e45482011-11-04 09:14:59 +00006803 /* Avoid the 8byte DMA problem */
6804 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006805 len += tp->dma_limit / 2;
6806 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006807 }
6808
Matt Carlsonb9e45482011-11-04 09:14:59 +00006809 tnapi->tx_buffers[*entry].fragmented = true;
6810
6811 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6812 frag_len, tmp_flag, mss, vlan);
6813 *budget -= 1;
6814 prvidx = *entry;
6815 *entry = NEXT_TX(*entry);
6816
Matt Carlsone31aa982011-07-27 14:20:53 +00006817 map += frag_len;
6818 }
6819
6820 if (len) {
6821 if (*budget) {
6822 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6823 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006824 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006825 *entry = NEXT_TX(*entry);
6826 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00006827 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006828 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006829 }
6830 }
6831 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006832 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6833 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006834 *entry = NEXT_TX(*entry);
6835 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006836
6837 return hwbug;
6838}
6839
Matt Carlson0d681b22011-07-27 14:20:49 +00006840static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006841{
6842 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006843 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006844 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006845
Matt Carlson0d681b22011-07-27 14:20:49 +00006846 skb = txb->skb;
6847 txb->skb = NULL;
6848
Matt Carlson432aa7e2011-05-19 12:12:45 +00006849 pci_unmap_single(tnapi->tp->pdev,
6850 dma_unmap_addr(txb, mapping),
6851 skb_headlen(skb),
6852 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006853
6854 while (txb->fragmented) {
6855 txb->fragmented = false;
6856 entry = NEXT_TX(entry);
6857 txb = &tnapi->tx_buffers[entry];
6858 }
6859
Matt Carlsonba1142e2011-11-04 09:15:00 +00006860 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006861 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006862
6863 entry = NEXT_TX(entry);
6864 txb = &tnapi->tx_buffers[entry];
6865
6866 pci_unmap_page(tnapi->tp->pdev,
6867 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006868 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006869
6870 while (txb->fragmented) {
6871 txb->fragmented = false;
6872 entry = NEXT_TX(entry);
6873 txb = &tnapi->tx_buffers[entry];
6874 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006875 }
6876}
6877
Michael Chan72f2afb2006-03-06 19:28:35 -08006878/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006879static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006880 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006881 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006882 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006884 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006885 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006886 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006887 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
Matt Carlson41588ba2008-04-19 18:12:33 -07006889 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6890 new_skb = skb_copy(skb, GFP_ATOMIC);
6891 else {
6892 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6893
6894 new_skb = skb_copy_expand(skb,
6895 skb_headroom(skb) + more_headroom,
6896 skb_tailroom(skb), GFP_ATOMIC);
6897 }
6898
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006900 ret = -1;
6901 } else {
6902 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006903 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6904 PCI_DMA_TODEVICE);
6905 /* Make sure the mapping succeeded */
6906 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006907 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006908 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006909 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006910 u32 save_entry = *entry;
6911
Matt Carlson92cd3a12011-07-27 14:20:47 +00006912 base_flags |= TXD_FLAG_END;
6913
Matt Carlson84b67b22011-07-27 14:20:52 +00006914 tnapi->tx_buffers[*entry].skb = new_skb;
6915 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006916 mapping, new_addr);
6917
Matt Carlson84b67b22011-07-27 14:20:52 +00006918 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006919 new_skb->len, base_flags,
6920 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006921 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006922 dev_kfree_skb(new_skb);
6923 ret = -1;
6924 }
Michael Chanc58ec932005-09-17 00:46:27 -07006925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 }
6927
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006929 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006930 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931}
6932
Matt Carlson2ffcc982011-05-19 12:12:44 +00006933static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006934
6935/* Use GSO to workaround a rare TSO bug that may be triggered when the
6936 * TSO header is greater than 80 bytes.
6937 */
6938static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6939{
6940 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006941 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006942
6943 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006944 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006945 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006946
6947 /* netif_tx_stop_queue() must be done before checking
6948 * checking tx index in tg3_tx_avail() below, because in
6949 * tg3_tx(), we update tx index before checking for
6950 * netif_tx_queue_stopped().
6951 */
6952 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006953 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006954 return NETDEV_TX_BUSY;
6955
6956 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006957 }
6958
6959 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006960 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006961 goto tg3_tso_bug_end;
6962
6963 do {
6964 nskb = segs;
6965 segs = segs->next;
6966 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006967 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006968 } while (segs);
6969
6970tg3_tso_bug_end:
6971 dev_kfree_skb(skb);
6972
6973 return NETDEV_TX_OK;
6974}
Michael Chan52c0fd82006-06-29 20:15:54 -07006975
Michael Chan5a6f3072006-03-20 22:28:05 -08006976/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006977 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006978 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006979static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006980{
6981 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006982 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006983 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006984 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006985 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006986 struct tg3_napi *tnapi;
6987 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006988 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006989
Matt Carlson24f4efd2009-11-13 13:03:35 +00006990 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6991 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006992 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006993 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994
Matt Carlson84b67b22011-07-27 14:20:52 +00006995 budget = tg3_tx_avail(tnapi);
6996
Michael Chan00b70502006-06-17 21:58:45 -07006997 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006998 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006999 * interrupt. Furthermore, IRQ processing runs lockless so we have
7000 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007002 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007003 if (!netif_tx_queue_stopped(txq)) {
7004 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007005
7006 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007007 netdev_err(dev,
7008 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 return NETDEV_TX_BUSY;
7011 }
7012
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007013 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007015 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007017
Matt Carlsonbe98da62010-07-11 09:31:46 +00007018 mss = skb_shinfo(skb)->gso_size;
7019 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007020 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007021 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022
7023 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007024 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7025 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026
Matt Carlson34195c32010-07-11 09:31:42 +00007027 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007028 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029
Eric Dumazeta5a11952012-01-23 01:22:09 +00007030 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007031
Eric Dumazeta5a11952012-01-23 01:22:09 +00007032 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007033 iph->check = 0;
7034 iph->tot_len = htons(mss + hdr_len);
7035 }
7036
Michael Chan52c0fd82006-06-29 20:15:54 -07007037 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007038 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007039 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007040
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7042 TXD_FLAG_CPU_POST_DMA);
7043
Joe Perches63c3a662011-04-26 08:12:10 +00007044 if (tg3_flag(tp, HW_TSO_1) ||
7045 tg3_flag(tp, HW_TSO_2) ||
7046 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007047 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007049 } else
7050 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7051 iph->daddr, 0,
7052 IPPROTO_TCP,
7053 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054
Joe Perches63c3a662011-04-26 08:12:10 +00007055 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007056 mss |= (hdr_len & 0xc) << 12;
7057 if (hdr_len & 0x10)
7058 base_flags |= 0x00000010;
7059 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007060 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007061 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007062 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007063 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007064 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 int tsflags;
7066
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007067 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068 mss |= (tsflags << 11);
7069 }
7070 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007071 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 int tsflags;
7073
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007074 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 base_flags |= tsflags << 12;
7076 }
7077 }
7078 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007079
Matt Carlson93a700a2011-08-31 11:44:54 +00007080 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7081 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7082 base_flags |= TXD_FLAG_JMB_PKT;
7083
Matt Carlson92cd3a12011-07-27 14:20:47 +00007084 if (vlan_tx_tag_present(skb)) {
7085 base_flags |= TXD_FLAG_VLAN;
7086 vlan = vlan_tx_tag_get(skb);
7087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088
Alexander Duyckf4188d82009-12-02 16:48:38 +00007089 len = skb_headlen(skb);
7090
7091 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007092 if (pci_dma_mapping_error(tp->pdev, mapping))
7093 goto drop;
7094
David S. Miller90079ce2008-09-11 04:52:51 -07007095
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007096 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007097 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098
7099 would_hit_hwbug = 0;
7100
Joe Perches63c3a662011-04-26 08:12:10 +00007101 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007102 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103
Matt Carlson84b67b22011-07-27 14:20:52 +00007104 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007105 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007106 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007107 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007108 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007109 u32 tmp_mss = mss;
7110
7111 if (!tg3_flag(tp, HW_TSO_1) &&
7112 !tg3_flag(tp, HW_TSO_2) &&
7113 !tg3_flag(tp, HW_TSO_3))
7114 tmp_mss = 0;
7115
Matt Carlsonc5665a52012-02-13 10:20:12 +00007116 /* Now loop through additional data
7117 * fragments, and queue them.
7118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 last = skb_shinfo(skb)->nr_frags - 1;
7120 for (i = 0; i <= last; i++) {
7121 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7122
Eric Dumazet9e903e02011-10-18 21:00:24 +00007123 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007124 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007125 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007127 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007128 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007129 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007130 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007131 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
Matt Carlsonb9e45482011-11-04 09:14:59 +00007133 if (!budget ||
7134 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007135 len, base_flags |
7136 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007137 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007138 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007139 break;
7140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 }
7142 }
7143
7144 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007145 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
7147 /* If the workaround fails due to memory/mapping
7148 * failure, silently drop this packet.
7149 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007150 entry = tnapi->tx_prod;
7151 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007152 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007153 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007154 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 }
7156
Richard Cochrand515b452011-06-19 03:31:41 +00007157 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007158 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007159
Michael Chan6541b802012-03-04 14:48:14 +00007160 /* Sync BD data before updating mailbox */
7161 wmb();
7162
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007164 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007166 tnapi->tx_prod = entry;
7167 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007168 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007169
7170 /* netif_tx_stop_queue() must be done before checking
7171 * checking tx index in tg3_tx_avail() below, because in
7172 * tg3_tx(), we update tx index before checking for
7173 * netif_tx_queue_stopped().
7174 */
7175 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007176 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007177 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007180 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007182
7183dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007184 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007185 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007186drop:
7187 dev_kfree_skb(skb);
7188drop_nofree:
7189 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007190 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191}
7192
Matt Carlson6e01b202011-08-19 13:58:20 +00007193static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7194{
7195 if (enable) {
7196 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7197 MAC_MODE_PORT_MODE_MASK);
7198
7199 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7200
7201 if (!tg3_flag(tp, 5705_PLUS))
7202 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7203
7204 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7205 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7206 else
7207 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7208 } else {
7209 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7210
7211 if (tg3_flag(tp, 5705_PLUS) ||
7212 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7213 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7214 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7215 }
7216
7217 tw32(MAC_MODE, tp->mac_mode);
7218 udelay(40);
7219}
7220
Matt Carlson941ec902011-08-19 13:58:23 +00007221static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007222{
Matt Carlson941ec902011-08-19 13:58:23 +00007223 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007224
7225 tg3_phy_toggle_apd(tp, false);
7226 tg3_phy_toggle_automdix(tp, 0);
7227
Matt Carlson941ec902011-08-19 13:58:23 +00007228 if (extlpbk && tg3_phy_set_extloopbk(tp))
7229 return -EIO;
7230
7231 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007232 switch (speed) {
7233 case SPEED_10:
7234 break;
7235 case SPEED_100:
7236 bmcr |= BMCR_SPEED100;
7237 break;
7238 case SPEED_1000:
7239 default:
7240 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7241 speed = SPEED_100;
7242 bmcr |= BMCR_SPEED100;
7243 } else {
7244 speed = SPEED_1000;
7245 bmcr |= BMCR_SPEED1000;
7246 }
7247 }
7248
Matt Carlson941ec902011-08-19 13:58:23 +00007249 if (extlpbk) {
7250 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7251 tg3_readphy(tp, MII_CTRL1000, &val);
7252 val |= CTL1000_AS_MASTER |
7253 CTL1000_ENABLE_MASTER;
7254 tg3_writephy(tp, MII_CTRL1000, val);
7255 } else {
7256 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7257 MII_TG3_FET_PTEST_TRIM_2;
7258 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7259 }
7260 } else
7261 bmcr |= BMCR_LOOPBACK;
7262
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007263 tg3_writephy(tp, MII_BMCR, bmcr);
7264
7265 /* The write needs to be flushed for the FETs */
7266 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7267 tg3_readphy(tp, MII_BMCR, &bmcr);
7268
7269 udelay(40);
7270
7271 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7272 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007273 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007274 MII_TG3_FET_PTEST_FRC_TX_LINK |
7275 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7276
7277 /* The write needs to be flushed for the AC131 */
7278 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7279 }
7280
7281 /* Reset to prevent losing 1st rx packet intermittently */
7282 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7283 tg3_flag(tp, 5780_CLASS)) {
7284 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7285 udelay(10);
7286 tw32_f(MAC_RX_MODE, tp->rx_mode);
7287 }
7288
7289 mac_mode = tp->mac_mode &
7290 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7291 if (speed == SPEED_1000)
7292 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7293 else
7294 mac_mode |= MAC_MODE_PORT_MODE_MII;
7295
7296 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7297 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7298
7299 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7300 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7301 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7302 mac_mode |= MAC_MODE_LINK_POLARITY;
7303
7304 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7305 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7306 }
7307
7308 tw32(MAC_MODE, mac_mode);
7309 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007310
7311 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007312}
7313
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007314static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007315{
7316 struct tg3 *tp = netdev_priv(dev);
7317
7318 if (features & NETIF_F_LOOPBACK) {
7319 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7320 return;
7321
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007322 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007323 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007324 netif_carrier_on(tp->dev);
7325 spin_unlock_bh(&tp->lock);
7326 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7327 } else {
7328 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7329 return;
7330
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007331 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007332 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007333 /* Force link status check */
7334 tg3_setup_phy(tp, 1);
7335 spin_unlock_bh(&tp->lock);
7336 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7337 }
7338}
7339
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007340static netdev_features_t tg3_fix_features(struct net_device *dev,
7341 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007342{
7343 struct tg3 *tp = netdev_priv(dev);
7344
Joe Perches63c3a662011-04-26 08:12:10 +00007345 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007346 features &= ~NETIF_F_ALL_TSO;
7347
7348 return features;
7349}
7350
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007351static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007352{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007353 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007354
7355 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7356 tg3_set_loopback(dev, features);
7357
7358 return 0;
7359}
7360
Matt Carlson21f581a2009-08-28 14:00:25 +00007361static void tg3_rx_prodring_free(struct tg3 *tp,
7362 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 int i;
7365
Matt Carlson8fea32b2010-09-15 08:59:58 +00007366 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007367 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007368 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007369 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007370 tp->rx_pkt_map_sz);
7371
Joe Perches63c3a662011-04-26 08:12:10 +00007372 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007373 for (i = tpr->rx_jmb_cons_idx;
7374 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007375 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007376 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007377 TG3_RX_JMB_MAP_SZ);
7378 }
7379 }
7380
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007381 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383
Matt Carlson2c49a442010-09-30 10:34:35 +00007384 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007385 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007386 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
Joe Perches63c3a662011-04-26 08:12:10 +00007388 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007389 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007390 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007391 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 }
7393}
7394
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007395/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 *
7397 * The chip has been shut down and the driver detached from
7398 * the networking, so no interrupts or new tx packets will
7399 * end up in the driver. tp->{tx,}lock are held and thus
7400 * we may not sleep.
7401 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007402static int tg3_rx_prodring_alloc(struct tg3 *tp,
7403 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404{
Matt Carlson287be122009-08-28 13:58:46 +00007405 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007407 tpr->rx_std_cons_idx = 0;
7408 tpr->rx_std_prod_idx = 0;
7409 tpr->rx_jmb_cons_idx = 0;
7410 tpr->rx_jmb_prod_idx = 0;
7411
Matt Carlson8fea32b2010-09-15 08:59:58 +00007412 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007413 memset(&tpr->rx_std_buffers[0], 0,
7414 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007415 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007416 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007417 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007418 goto done;
7419 }
7420
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007422 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423
Matt Carlson287be122009-08-28 13:58:46 +00007424 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007425 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007426 tp->dev->mtu > ETH_DATA_LEN)
7427 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7428 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007429
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430 /* Initialize invariants of the rings, we only set this
7431 * stuff once. This works because the card does not
7432 * write into the rx buffer posting rings.
7433 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007434 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435 struct tg3_rx_buffer_desc *rxd;
7436
Matt Carlson21f581a2009-08-28 14:00:25 +00007437 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007438 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7440 rxd->opaque = (RXD_OPAQUE_RING_STD |
7441 (i << RXD_OPAQUE_INDEX_SHIFT));
7442 }
7443
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007444 /* Now allocate fresh SKBs for each rx ring. */
7445 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007446 unsigned int frag_size;
7447
7448 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7449 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007450 netdev_warn(tp->dev,
7451 "Using a smaller RX standard ring. Only "
7452 "%d out of %d buffers were allocated "
7453 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007454 if (i == 0)
7455 goto initfail;
7456 tp->rx_pending = i;
7457 break;
7458 }
7459 }
7460
Joe Perches63c3a662011-04-26 08:12:10 +00007461 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007462 goto done;
7463
Matt Carlson2c49a442010-09-30 10:34:35 +00007464 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007465
Joe Perches63c3a662011-04-26 08:12:10 +00007466 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007467 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468
Matt Carlson2c49a442010-09-30 10:34:35 +00007469 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007470 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471
Matt Carlson0d86df82010-02-17 15:17:00 +00007472 rxd = &tpr->rx_jmb[i].std;
7473 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7474 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7475 RXD_FLAG_JUMBO;
7476 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7477 (i << RXD_OPAQUE_INDEX_SHIFT));
7478 }
7479
7480 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007481 unsigned int frag_size;
7482
7483 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7484 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007485 netdev_warn(tp->dev,
7486 "Using a smaller RX jumbo ring. Only %d "
7487 "out of %d buffers were allocated "
7488 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007489 if (i == 0)
7490 goto initfail;
7491 tp->rx_jumbo_pending = i;
7492 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 }
7494 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007495
7496done:
Michael Chan32d8c572006-07-25 16:38:29 -07007497 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007498
7499initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007500 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007501 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502}
7503
Matt Carlson21f581a2009-08-28 14:00:25 +00007504static void tg3_rx_prodring_fini(struct tg3 *tp,
7505 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506{
Matt Carlson21f581a2009-08-28 14:00:25 +00007507 kfree(tpr->rx_std_buffers);
7508 tpr->rx_std_buffers = NULL;
7509 kfree(tpr->rx_jmb_buffers);
7510 tpr->rx_jmb_buffers = NULL;
7511 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007512 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7513 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007514 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007516 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007517 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7518 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007519 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007521}
7522
Matt Carlson21f581a2009-08-28 14:00:25 +00007523static int tg3_rx_prodring_init(struct tg3 *tp,
7524 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007525{
Matt Carlson2c49a442010-09-30 10:34:35 +00007526 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7527 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007528 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007529 return -ENOMEM;
7530
Matt Carlson4bae65c2010-11-24 08:31:52 +00007531 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7532 TG3_RX_STD_RING_BYTES(tp),
7533 &tpr->rx_std_mapping,
7534 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007535 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007536 goto err_out;
7537
Joe Perches63c3a662011-04-26 08:12:10 +00007538 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007539 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007540 GFP_KERNEL);
7541 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007542 goto err_out;
7543
Matt Carlson4bae65c2010-11-24 08:31:52 +00007544 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7545 TG3_RX_JMB_RING_BYTES(tp),
7546 &tpr->rx_jmb_mapping,
7547 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007548 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007549 goto err_out;
7550 }
7551
7552 return 0;
7553
7554err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007555 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007556 return -ENOMEM;
7557}
7558
7559/* Free up pending packets in all rx/tx rings.
7560 *
7561 * The chip has been shut down and the driver detached from
7562 * the networking, so no interrupts or new tx packets will
7563 * end up in the driver. tp->{tx,}lock is not held and we are not
7564 * in an interrupt context and thus may sleep.
7565 */
7566static void tg3_free_rings(struct tg3 *tp)
7567{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007568 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007569
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007570 for (j = 0; j < tp->irq_cnt; j++) {
7571 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007572
Matt Carlson8fea32b2010-09-15 08:59:58 +00007573 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007574
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007575 if (!tnapi->tx_buffers)
7576 continue;
7577
Matt Carlson0d681b22011-07-27 14:20:49 +00007578 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7579 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007580
Matt Carlson0d681b22011-07-27 14:20:49 +00007581 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007582 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007583
Matt Carlsonba1142e2011-11-04 09:15:00 +00007584 tg3_tx_skb_unmap(tnapi, i,
7585 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007586
7587 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007588 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007589 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007590 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007591}
7592
7593/* Initialize tx/rx rings for packet processing.
7594 *
7595 * The chip has been shut down and the driver detached from
7596 * the networking, so no interrupts or new tx packets will
7597 * end up in the driver. tp->{tx,}lock are held and thus
7598 * we may not sleep.
7599 */
7600static int tg3_init_rings(struct tg3 *tp)
7601{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007602 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007603
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007604 /* Free up all the SKBs. */
7605 tg3_free_rings(tp);
7606
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007607 for (i = 0; i < tp->irq_cnt; i++) {
7608 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007609
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007610 tnapi->last_tag = 0;
7611 tnapi->last_irq_tag = 0;
7612 tnapi->hw_status->status = 0;
7613 tnapi->hw_status->status_tag = 0;
7614 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7615
7616 tnapi->tx_prod = 0;
7617 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007618 if (tnapi->tx_ring)
7619 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007620
7621 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007622 if (tnapi->rx_rcb)
7623 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007624
Matt Carlson8fea32b2010-09-15 08:59:58 +00007625 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007626 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007627 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007628 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007629 }
Matt Carlson72334482009-08-28 14:03:01 +00007630
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007631 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007632}
7633
Michael Chan49a359e2012-09-28 07:12:37 +00007634static void tg3_mem_tx_release(struct tg3 *tp)
7635{
7636 int i;
7637
7638 for (i = 0; i < tp->irq_max; i++) {
7639 struct tg3_napi *tnapi = &tp->napi[i];
7640
7641 if (tnapi->tx_ring) {
7642 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
7643 tnapi->tx_ring, tnapi->tx_desc_mapping);
7644 tnapi->tx_ring = NULL;
7645 }
7646
7647 kfree(tnapi->tx_buffers);
7648 tnapi->tx_buffers = NULL;
7649 }
7650}
7651
7652static int tg3_mem_tx_acquire(struct tg3 *tp)
7653{
7654 int i;
7655 struct tg3_napi *tnapi = &tp->napi[0];
7656
7657 /* If multivector TSS is enabled, vector 0 does not handle
7658 * tx interrupts. Don't allocate any resources for it.
7659 */
7660 if (tg3_flag(tp, ENABLE_TSS))
7661 tnapi++;
7662
7663 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
7664 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
7665 TG3_TX_RING_SIZE, GFP_KERNEL);
7666 if (!tnapi->tx_buffers)
7667 goto err_out;
7668
7669 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7670 TG3_TX_RING_BYTES,
7671 &tnapi->tx_desc_mapping,
7672 GFP_KERNEL);
7673 if (!tnapi->tx_ring)
7674 goto err_out;
7675 }
7676
7677 return 0;
7678
7679err_out:
7680 tg3_mem_tx_release(tp);
7681 return -ENOMEM;
7682}
7683
7684static void tg3_mem_rx_release(struct tg3 *tp)
7685{
7686 int i;
7687
7688 for (i = 0; i < tp->irq_max; i++) {
7689 struct tg3_napi *tnapi = &tp->napi[i];
7690
7691 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7692
7693 if (!tnapi->rx_rcb)
7694 continue;
7695
7696 dma_free_coherent(&tp->pdev->dev,
7697 TG3_RX_RCB_RING_BYTES(tp),
7698 tnapi->rx_rcb,
7699 tnapi->rx_rcb_mapping);
7700 tnapi->rx_rcb = NULL;
7701 }
7702}
7703
7704static int tg3_mem_rx_acquire(struct tg3 *tp)
7705{
7706 unsigned int i, limit;
7707
7708 limit = tp->rxq_cnt;
7709
7710 /* If RSS is enabled, we need a (dummy) producer ring
7711 * set on vector zero. This is the true hw prodring.
7712 */
7713 if (tg3_flag(tp, ENABLE_RSS))
7714 limit++;
7715
7716 for (i = 0; i < limit; i++) {
7717 struct tg3_napi *tnapi = &tp->napi[i];
7718
7719 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7720 goto err_out;
7721
7722 /* If multivector RSS is enabled, vector 0
7723 * does not handle rx or tx interrupts.
7724 * Don't allocate any resources for it.
7725 */
7726 if (!i && tg3_flag(tp, ENABLE_RSS))
7727 continue;
7728
7729 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7730 TG3_RX_RCB_RING_BYTES(tp),
7731 &tnapi->rx_rcb_mapping,
7732 GFP_KERNEL);
7733 if (!tnapi->rx_rcb)
7734 goto err_out;
7735
7736 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
7737 }
7738
7739 return 0;
7740
7741err_out:
7742 tg3_mem_rx_release(tp);
7743 return -ENOMEM;
7744}
7745
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007746/*
7747 * Must not be invoked with interrupt sources disabled and
7748 * the hardware shutdown down.
7749 */
7750static void tg3_free_consistent(struct tg3 *tp)
7751{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007752 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007753
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007754 for (i = 0; i < tp->irq_cnt; i++) {
7755 struct tg3_napi *tnapi = &tp->napi[i];
7756
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007757 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007758 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7759 tnapi->hw_status,
7760 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007761 tnapi->hw_status = NULL;
7762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007763 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007764
Michael Chan49a359e2012-09-28 07:12:37 +00007765 tg3_mem_rx_release(tp);
7766 tg3_mem_tx_release(tp);
7767
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007769 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7770 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 tp->hw_stats = NULL;
7772 }
7773}
7774
7775/*
7776 * Must not be invoked with interrupt sources disabled and
7777 * the hardware shutdown down. Can sleep.
7778 */
7779static int tg3_alloc_consistent(struct tg3 *tp)
7780{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007781 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007782
Matt Carlson4bae65c2010-11-24 08:31:52 +00007783 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7784 sizeof(struct tg3_hw_stats),
7785 &tp->stats_mapping,
7786 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787 if (!tp->hw_stats)
7788 goto err_out;
7789
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7791
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007792 for (i = 0; i < tp->irq_cnt; i++) {
7793 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007794 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007795
Matt Carlson4bae65c2010-11-24 08:31:52 +00007796 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7797 TG3_HW_STATUS_SIZE,
7798 &tnapi->status_mapping,
7799 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007800 if (!tnapi->hw_status)
7801 goto err_out;
7802
7803 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007804 sblk = tnapi->hw_status;
7805
Michael Chan49a359e2012-09-28 07:12:37 +00007806 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07007807 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007808
Michael Chan49a359e2012-09-28 07:12:37 +00007809 /*
7810 * When RSS is enabled, the status block format changes
7811 * slightly. The "rx_jumbo_consumer", "reserved",
7812 * and "rx_mini_consumer" members get mapped to the
7813 * other three rx return ring producer indexes.
7814 */
7815 switch (i) {
7816 case 1:
7817 prodptr = &sblk->idx[0].rx_producer;
7818 break;
7819 case 2:
7820 prodptr = &sblk->rx_jumbo_consumer;
7821 break;
7822 case 3:
7823 prodptr = &sblk->reserved;
7824 break;
7825 case 4:
7826 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00007827 break;
7828 }
Michael Chan49a359e2012-09-28 07:12:37 +00007829 tnapi->rx_rcb_prod_idx = prodptr;
7830 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007831 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007832 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007833 }
7834
Michael Chan49a359e2012-09-28 07:12:37 +00007835 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
7836 goto err_out;
7837
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 return 0;
7839
7840err_out:
7841 tg3_free_consistent(tp);
7842 return -ENOMEM;
7843}
7844
7845#define MAX_WAIT_CNT 1000
7846
7847/* To stop a block, clear the enable bit and poll till it
7848 * clears. tp->lock is held.
7849 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007850static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851{
7852 unsigned int i;
7853 u32 val;
7854
Joe Perches63c3a662011-04-26 08:12:10 +00007855 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 switch (ofs) {
7857 case RCVLSC_MODE:
7858 case DMAC_MODE:
7859 case MBFREE_MODE:
7860 case BUFMGR_MODE:
7861 case MEMARB_MODE:
7862 /* We can't enable/disable these bits of the
7863 * 5705/5750, just say success.
7864 */
7865 return 0;
7866
7867 default:
7868 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 }
7871
7872 val = tr32(ofs);
7873 val &= ~enable_bit;
7874 tw32_f(ofs, val);
7875
7876 for (i = 0; i < MAX_WAIT_CNT; i++) {
7877 udelay(100);
7878 val = tr32(ofs);
7879 if ((val & enable_bit) == 0)
7880 break;
7881 }
7882
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007883 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007884 dev_err(&tp->pdev->dev,
7885 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7886 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 return -ENODEV;
7888 }
7889
7890 return 0;
7891}
7892
7893/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007894static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895{
7896 int i, err;
7897
7898 tg3_disable_ints(tp);
7899
7900 tp->rx_mode &= ~RX_MODE_ENABLE;
7901 tw32_f(MAC_RX_MODE, tp->rx_mode);
7902 udelay(10);
7903
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007904 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7905 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7906 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7907 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7908 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7909 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007911 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7912 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7913 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7914 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7915 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7916 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7917 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918
7919 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7920 tw32_f(MAC_MODE, tp->mac_mode);
7921 udelay(40);
7922
7923 tp->tx_mode &= ~TX_MODE_ENABLE;
7924 tw32_f(MAC_TX_MODE, tp->tx_mode);
7925
7926 for (i = 0; i < MAX_WAIT_CNT; i++) {
7927 udelay(100);
7928 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7929 break;
7930 }
7931 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007932 dev_err(&tp->pdev->dev,
7933 "%s timed out, TX_MODE_ENABLE will not clear "
7934 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007935 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936 }
7937
Michael Chane6de8ad2005-05-05 14:42:41 -07007938 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007939 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7940 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941
7942 tw32(FTQ_RESET, 0xffffffff);
7943 tw32(FTQ_RESET, 0x00000000);
7944
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007945 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7946 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007947
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007948 for (i = 0; i < tp->irq_cnt; i++) {
7949 struct tg3_napi *tnapi = &tp->napi[i];
7950 if (tnapi->hw_status)
7951 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953
Linus Torvalds1da177e2005-04-16 15:20:36 -07007954 return err;
7955}
7956
Michael Chanee6a99b2007-07-18 21:49:10 -07007957/* Save PCI command register before chip reset */
7958static void tg3_save_pci_state(struct tg3 *tp)
7959{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007960 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007961}
7962
7963/* Restore PCI state after chip reset */
7964static void tg3_restore_pci_state(struct tg3 *tp)
7965{
7966 u32 val;
7967
7968 /* Re-enable indirect register accesses. */
7969 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7970 tp->misc_host_ctrl);
7971
7972 /* Set MAX PCI retry to zero. */
7973 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7974 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007975 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007976 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007977 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007978 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007979 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007980 PCISTATE_ALLOW_APE_SHMEM_WR |
7981 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007982 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7983
Matt Carlson8a6eac92007-10-21 16:17:55 -07007984 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007985
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007986 if (!tg3_flag(tp, PCI_EXPRESS)) {
7987 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7988 tp->pci_cacheline_sz);
7989 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7990 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007991 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007992
Michael Chanee6a99b2007-07-18 21:49:10 -07007993 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007994 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007995 u16 pcix_cmd;
7996
7997 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7998 &pcix_cmd);
7999 pcix_cmd &= ~PCI_X_CMD_ERO;
8000 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8001 pcix_cmd);
8002 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008003
Joe Perches63c3a662011-04-26 08:12:10 +00008004 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008005
8006 /* Chip reset on 5780 will reset MSI enable bit,
8007 * so need to restore it.
8008 */
Joe Perches63c3a662011-04-26 08:12:10 +00008009 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008010 u16 ctrl;
8011
8012 pci_read_config_word(tp->pdev,
8013 tp->msi_cap + PCI_MSI_FLAGS,
8014 &ctrl);
8015 pci_write_config_word(tp->pdev,
8016 tp->msi_cap + PCI_MSI_FLAGS,
8017 ctrl | PCI_MSI_FLAGS_ENABLE);
8018 val = tr32(MSGINT_MODE);
8019 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8020 }
8021 }
8022}
8023
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024/* tp->lock is held. */
8025static int tg3_chip_reset(struct tg3 *tp)
8026{
8027 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008028 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008029 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008030
David S. Millerf49639e2006-06-09 11:58:36 -07008031 tg3_nvram_lock(tp);
8032
Matt Carlson77b483f2008-08-15 14:07:24 -07008033 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8034
David S. Millerf49639e2006-06-09 11:58:36 -07008035 /* No matching tg3_nvram_unlock() after this because
8036 * chip reset below will undo the nvram lock.
8037 */
8038 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039
Michael Chanee6a99b2007-07-18 21:49:10 -07008040 /* GRC_MISC_CFG core clock reset will clear the memory
8041 * enable bit in PCI register 4 and the MSI enable bit
8042 * on some chips, so we save relevant registers here.
8043 */
8044 tg3_save_pci_state(tp);
8045
Michael Chand9ab5ad2006-03-20 22:27:35 -08008046 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008047 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08008048 tw32(GRC_FASTBOOT_PC, 0);
8049
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050 /*
8051 * We must avoid the readl() that normally takes place.
8052 * It locks machines, causes machine checks, and other
8053 * fun things. So, temporarily disable the 5701
8054 * hardware workaround, while we do the reset.
8055 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008056 write_op = tp->write32;
8057 if (write_op == tg3_write_flush_reg32)
8058 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059
Michael Chand18edcb2007-03-24 20:57:11 -07008060 /* Prevent the irq handler from reading or writing PCI registers
8061 * during chip reset when the memory enable bit in the PCI command
8062 * register may be cleared. The chip does not generate interrupt
8063 * at this time, but the irq handler may still be called due to irq
8064 * sharing or irqpoll.
8065 */
Joe Perches63c3a662011-04-26 08:12:10 +00008066 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008067 for (i = 0; i < tp->irq_cnt; i++) {
8068 struct tg3_napi *tnapi = &tp->napi[i];
8069 if (tnapi->hw_status) {
8070 tnapi->hw_status->status = 0;
8071 tnapi->hw_status->status_tag = 0;
8072 }
8073 tnapi->last_tag = 0;
8074 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008075 }
Michael Chand18edcb2007-03-24 20:57:11 -07008076 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008077
8078 for (i = 0; i < tp->irq_cnt; i++)
8079 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008080
Matt Carlson255ca312009-08-25 10:07:27 +00008081 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8082 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8083 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8084 }
8085
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086 /* do the reset */
8087 val = GRC_MISC_CFG_CORECLK_RESET;
8088
Joe Perches63c3a662011-04-26 08:12:10 +00008089 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008090 /* Force PCIe 1.0a mode */
8091 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008092 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008093 tr32(TG3_PCIE_PHY_TSTCTL) ==
8094 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8095 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8096
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
8098 tw32(GRC_MISC_CFG, (1 << 29));
8099 val |= (1 << 29);
8100 }
8101 }
8102
Michael Chanb5d37722006-09-27 16:06:21 -07008103 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8104 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8105 tw32(GRC_VCPU_EXT_CTRL,
8106 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8107 }
8108
Matt Carlsonf37500d2010-08-02 11:25:59 +00008109 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008110 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008112
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113 tw32(GRC_MISC_CFG, val);
8114
Michael Chan1ee582d2005-08-09 20:16:46 -07008115 /* restore 5701 hardware bug workaround write method */
8116 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117
8118 /* Unfortunately, we have to delay before the PCI read back.
8119 * Some 575X chips even will not respond to a PCI cfg access
8120 * when the reset command is given to the chip.
8121 *
8122 * How do these hardware designers expect things to work
8123 * properly if the PCI write is posted for a long period
8124 * of time? It is always necessary to have some method by
8125 * which a register read back can occur to push the write
8126 * out which does the reset.
8127 *
8128 * For most tg3 variants the trick below was working.
8129 * Ho hum...
8130 */
8131 udelay(120);
8132
8133 /* Flush PCI posted writes. The normal MMIO registers
8134 * are inaccessible at this time so this is the only
8135 * way to make this reliably (actually, this is no longer
8136 * the case, see above). I tried to use indirect
8137 * register read/write but this upset some 5701 variants.
8138 */
8139 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8140
8141 udelay(120);
8142
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008143 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008144 u16 val16;
8145
Linus Torvalds1da177e2005-04-16 15:20:36 -07008146 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008147 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148 u32 cfg_val;
8149
8150 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008151 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152 udelay(100);
8153
8154 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8155 pci_write_config_dword(tp->pdev, 0xc4,
8156 cfg_val | (1 << 15));
8157 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008158
Matt Carlsone7126992009-08-25 10:08:16 +00008159 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008160 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008161 /*
8162 * Older PCIe devices only support the 128 byte
8163 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008164 */
Joe Perches63c3a662011-04-26 08:12:10 +00008165 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008166 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8167 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008168
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008169 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008170 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008171 PCI_EXP_DEVSTA_CED |
8172 PCI_EXP_DEVSTA_NFED |
8173 PCI_EXP_DEVSTA_FED |
8174 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175 }
8176
Michael Chanee6a99b2007-07-18 21:49:10 -07008177 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178
Joe Perches63c3a662011-04-26 08:12:10 +00008179 tg3_flag_clear(tp, CHIP_RESETTING);
8180 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008181
Michael Chanee6a99b2007-07-18 21:49:10 -07008182 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008183 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008184 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008185 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186
8187 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
8188 tg3_stop_fw(tp);
8189 tw32(0x5000, 0x400);
8190 }
8191
8192 tw32(GRC_MODE, tp->grc_mode);
8193
8194 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008195 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196
8197 tw32(0xc4, val | (1 << 15));
8198 }
8199
8200 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
8201 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
8202 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
8203 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
8204 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8205 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8206 }
8207
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008208 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008209 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008210 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008211 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008212 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008213 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008215 val = 0;
8216
8217 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218 udelay(40);
8219
Matt Carlson77b483f2008-08-15 14:07:24 -07008220 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8221
Michael Chan7a6f4362006-09-27 16:03:31 -07008222 err = tg3_poll_fw(tp);
8223 if (err)
8224 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008225
Matt Carlson0a9140c2009-08-28 12:27:50 +00008226 tg3_mdio_start(tp);
8227
Joe Perches63c3a662011-04-26 08:12:10 +00008228 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008229 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
8230 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008231 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008232 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233
8234 tw32(0x7c00, val | (1 << 25));
8235 }
8236
Matt Carlsond78b59f2011-04-05 14:22:46 +00008237 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8238 val = tr32(TG3_CPMU_CLCK_ORIDE);
8239 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8240 }
8241
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008243 tg3_flag_clear(tp, ENABLE_ASF);
8244 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8246 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8247 u32 nic_cfg;
8248
8249 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8250 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008251 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008252 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008253 if (tg3_flag(tp, 5750_PLUS))
8254 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255 }
8256 }
8257
8258 return 0;
8259}
8260
Matt Carlson65ec6982012-02-28 23:33:37 +00008261static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8262static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008263
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008265static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266{
8267 int err;
8268
8269 tg3_stop_fw(tp);
8270
Michael Chan944d9802005-05-29 14:57:48 -07008271 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008273 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274 err = tg3_chip_reset(tp);
8275
Matt Carlsondaba2a62009-04-20 06:58:52 +00008276 __tg3_set_mac_addr(tp, 0);
8277
Michael Chan944d9802005-05-29 14:57:48 -07008278 tg3_write_sig_legacy(tp, kind);
8279 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280
Matt Carlson92feeab2011-12-08 14:40:14 +00008281 if (tp->hw_stats) {
8282 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008283 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008284 tg3_get_estats(tp, &tp->estats_prev);
8285
8286 /* And make sure the next sample is new data */
8287 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8288 }
8289
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290 if (err)
8291 return err;
8292
8293 return 0;
8294}
8295
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296static int tg3_set_mac_addr(struct net_device *dev, void *p)
8297{
8298 struct tg3 *tp = netdev_priv(dev);
8299 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008300 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301
Michael Chanf9804dd2005-09-27 12:13:10 -07008302 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008303 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008304
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8306
Michael Chane75f7c92006-03-20 21:33:26 -08008307 if (!netif_running(dev))
8308 return 0;
8309
Joe Perches63c3a662011-04-26 08:12:10 +00008310 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008311 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008312
Michael Chan986e0ae2007-05-05 12:10:20 -07008313 addr0_high = tr32(MAC_ADDR_0_HIGH);
8314 addr0_low = tr32(MAC_ADDR_0_LOW);
8315 addr1_high = tr32(MAC_ADDR_1_HIGH);
8316 addr1_low = tr32(MAC_ADDR_1_LOW);
8317
8318 /* Skip MAC addr 1 if ASF is using it. */
8319 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8320 !(addr1_high == 0 && addr1_low == 0))
8321 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008322 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008323 spin_lock_bh(&tp->lock);
8324 __tg3_set_mac_addr(tp, skip_mac_1);
8325 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326
Michael Chanb9ec6c12006-07-25 16:37:27 -07008327 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328}
8329
8330/* tp->lock is held. */
8331static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8332 dma_addr_t mapping, u32 maxlen_flags,
8333 u32 nic_addr)
8334{
8335 tg3_write_mem(tp,
8336 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8337 ((u64) mapping >> 32));
8338 tg3_write_mem(tp,
8339 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8340 ((u64) mapping & 0xffffffff));
8341 tg3_write_mem(tp,
8342 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8343 maxlen_flags);
8344
Joe Perches63c3a662011-04-26 08:12:10 +00008345 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346 tg3_write_mem(tp,
8347 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8348 nic_addr);
8349}
8350
Michael Chana489b6d2012-09-28 07:12:39 +00008351
8352static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008353{
Michael Chana489b6d2012-09-28 07:12:39 +00008354 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00008355
Joe Perches63c3a662011-04-26 08:12:10 +00008356 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008357 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8358 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8359 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008360 } else {
8361 tw32(HOSTCC_TXCOL_TICKS, 0);
8362 tw32(HOSTCC_TXMAX_FRAMES, 0);
8363 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00008364
8365 for (; i < tp->txq_cnt; i++) {
8366 u32 reg;
8367
8368 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8369 tw32(reg, ec->tx_coalesce_usecs);
8370 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8371 tw32(reg, ec->tx_max_coalesced_frames);
8372 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8373 tw32(reg, ec->tx_max_coalesced_frames_irq);
8374 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00008375 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008376
Michael Chana489b6d2012-09-28 07:12:39 +00008377 for (; i < tp->irq_max - 1; i++) {
8378 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8379 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8380 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8381 }
8382}
8383
8384static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
8385{
8386 int i = 0;
8387 u32 limit = tp->rxq_cnt;
8388
Joe Perches63c3a662011-04-26 08:12:10 +00008389 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008390 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8391 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8392 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00008393 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008394 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008395 tw32(HOSTCC_RXCOL_TICKS, 0);
8396 tw32(HOSTCC_RXMAX_FRAMES, 0);
8397 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008398 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008399
Michael Chana489b6d2012-09-28 07:12:39 +00008400 for (; i < limit; i++) {
8401 u32 reg;
8402
8403 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8404 tw32(reg, ec->rx_coalesce_usecs);
8405 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8406 tw32(reg, ec->rx_max_coalesced_frames);
8407 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8408 tw32(reg, ec->rx_max_coalesced_frames_irq);
8409 }
8410
8411 for (; i < tp->irq_max - 1; i++) {
8412 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
8413 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
8414 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8415 }
8416}
8417
8418static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
8419{
8420 tg3_coal_tx_init(tp, ec);
8421 tg3_coal_rx_init(tp, ec);
8422
Joe Perches63c3a662011-04-26 08:12:10 +00008423 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008424 u32 val = ec->stats_block_coalesce_usecs;
8425
Matt Carlsonb6080e12009-09-01 13:12:00 +00008426 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8427 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8428
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00008429 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07008430 val = 0;
8431
8432 tw32(HOSTCC_STAT_COAL_TICKS, val);
8433 }
8434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435
8436/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008437static void tg3_rings_reset(struct tg3 *tp)
8438{
8439 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008440 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008441 struct tg3_napi *tnapi = &tp->napi[0];
8442
8443 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008444 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008445 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008446 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008447 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008448 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008449 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008450 else
8451 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8452
8453 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8454 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8455 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8456 BDINFO_FLAGS_DISABLED);
8457
8458
8459 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008460 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008461 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008462 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008463 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008464 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008465 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008466 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8467 else
8468 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8469
8470 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8471 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8472 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8473 BDINFO_FLAGS_DISABLED);
8474
8475 /* Disable interrupts */
8476 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008477 tp->napi[0].chk_msi_cnt = 0;
8478 tp->napi[0].last_rx_cons = 0;
8479 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008480
8481 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008482 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008483 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008484 tp->napi[i].tx_prod = 0;
8485 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008486 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008487 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008488 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8489 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008490 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008491 tp->napi[i].last_rx_cons = 0;
8492 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008493 }
Joe Perches63c3a662011-04-26 08:12:10 +00008494 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008495 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008496 } else {
8497 tp->napi[0].tx_prod = 0;
8498 tp->napi[0].tx_cons = 0;
8499 tw32_mailbox(tp->napi[0].prodmbox, 0);
8500 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8501 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008502
8503 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008504 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008505 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8506 for (i = 0; i < 16; i++)
8507 tw32_tx_mbox(mbox + i * 8, 0);
8508 }
8509
8510 txrcb = NIC_SRAM_SEND_RCB;
8511 rxrcb = NIC_SRAM_RCV_RET_RCB;
8512
8513 /* Clear status block in ram. */
8514 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8515
8516 /* Set status block DMA address */
8517 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8518 ((u64) tnapi->status_mapping >> 32));
8519 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8520 ((u64) tnapi->status_mapping & 0xffffffff));
8521
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008522 if (tnapi->tx_ring) {
8523 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8524 (TG3_TX_RING_SIZE <<
8525 BDINFO_FLAGS_MAXLEN_SHIFT),
8526 NIC_SRAM_TX_BUFFER_DESC);
8527 txrcb += TG3_BDINFO_SIZE;
8528 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008529
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008530 if (tnapi->rx_rcb) {
8531 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008532 (tp->rx_ret_ring_mask + 1) <<
8533 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008534 rxrcb += TG3_BDINFO_SIZE;
8535 }
8536
8537 stblk = HOSTCC_STATBLCK_RING1;
8538
8539 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8540 u64 mapping = (u64)tnapi->status_mapping;
8541 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8542 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8543
8544 /* Clear status block in ram. */
8545 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8546
Matt Carlson19cfaec2009-12-03 08:36:20 +00008547 if (tnapi->tx_ring) {
8548 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8549 (TG3_TX_RING_SIZE <<
8550 BDINFO_FLAGS_MAXLEN_SHIFT),
8551 NIC_SRAM_TX_BUFFER_DESC);
8552 txrcb += TG3_BDINFO_SIZE;
8553 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008554
8555 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008556 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008557 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8558
8559 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008560 rxrcb += TG3_BDINFO_SIZE;
8561 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008562}
8563
Matt Carlsoneb07a942011-04-20 07:57:36 +00008564static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8565{
8566 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8567
Joe Perches63c3a662011-04-26 08:12:10 +00008568 if (!tg3_flag(tp, 5750_PLUS) ||
8569 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008570 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008571 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8572 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008573 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8574 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8575 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8576 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8577 else
8578 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8579
8580 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8581 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8582
8583 val = min(nic_rep_thresh, host_rep_thresh);
8584 tw32(RCVBDI_STD_THRESH, val);
8585
Joe Perches63c3a662011-04-26 08:12:10 +00008586 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008587 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8588
Joe Perches63c3a662011-04-26 08:12:10 +00008589 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008590 return;
8591
Matt Carlson513aa6e2011-11-21 15:01:18 +00008592 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008593
8594 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8595
8596 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8597 tw32(RCVBDI_JUMBO_THRESH, val);
8598
Joe Perches63c3a662011-04-26 08:12:10 +00008599 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008600 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8601}
8602
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008603static inline u32 calc_crc(unsigned char *buf, int len)
8604{
8605 u32 reg;
8606 u32 tmp;
8607 int j, k;
8608
8609 reg = 0xffffffff;
8610
8611 for (j = 0; j < len; j++) {
8612 reg ^= buf[j];
8613
8614 for (k = 0; k < 8; k++) {
8615 tmp = reg & 0x01;
8616
8617 reg >>= 1;
8618
8619 if (tmp)
8620 reg ^= 0xedb88320;
8621 }
8622 }
8623
8624 return ~reg;
8625}
8626
8627static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8628{
8629 /* accept or reject all multicast frames */
8630 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8631 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8632 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8633 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8634}
8635
8636static void __tg3_set_rx_mode(struct net_device *dev)
8637{
8638 struct tg3 *tp = netdev_priv(dev);
8639 u32 rx_mode;
8640
8641 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8642 RX_MODE_KEEP_VLAN_TAG);
8643
8644#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8645 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8646 * flag clear.
8647 */
8648 if (!tg3_flag(tp, ENABLE_ASF))
8649 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8650#endif
8651
8652 if (dev->flags & IFF_PROMISC) {
8653 /* Promiscuous mode. */
8654 rx_mode |= RX_MODE_PROMISC;
8655 } else if (dev->flags & IFF_ALLMULTI) {
8656 /* Accept all multicast. */
8657 tg3_set_multi(tp, 1);
8658 } else if (netdev_mc_empty(dev)) {
8659 /* Reject all multicast. */
8660 tg3_set_multi(tp, 0);
8661 } else {
8662 /* Accept one or more multicast(s). */
8663 struct netdev_hw_addr *ha;
8664 u32 mc_filter[4] = { 0, };
8665 u32 regidx;
8666 u32 bit;
8667 u32 crc;
8668
8669 netdev_for_each_mc_addr(ha, dev) {
8670 crc = calc_crc(ha->addr, ETH_ALEN);
8671 bit = ~crc & 0x7f;
8672 regidx = (bit & 0x60) >> 5;
8673 bit &= 0x1f;
8674 mc_filter[regidx] |= (1 << bit);
8675 }
8676
8677 tw32(MAC_HASH_REG_0, mc_filter[0]);
8678 tw32(MAC_HASH_REG_1, mc_filter[1]);
8679 tw32(MAC_HASH_REG_2, mc_filter[2]);
8680 tw32(MAC_HASH_REG_3, mc_filter[3]);
8681 }
8682
8683 if (rx_mode != tp->rx_mode) {
8684 tp->rx_mode = rx_mode;
8685 tw32_f(MAC_RX_MODE, rx_mode);
8686 udelay(10);
8687 }
8688}
8689
Michael Chan91024262012-09-28 07:12:38 +00008690static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00008691{
8692 int i;
8693
8694 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00008695 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00008696}
8697
8698static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008699{
8700 int i;
8701
8702 if (!tg3_flag(tp, SUPPORT_MSIX))
8703 return;
8704
Matt Carlson90415472011-12-16 13:33:23 +00008705 if (tp->irq_cnt <= 2) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008706 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008707 return;
8708 }
8709
8710 /* Validate table against current IRQ count */
8711 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8712 if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
8713 break;
8714 }
8715
8716 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00008717 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008718}
8719
Matt Carlson90415472011-12-16 13:33:23 +00008720static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008721{
8722 int i = 0;
8723 u32 reg = MAC_RSS_INDIR_TBL_0;
8724
8725 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8726 u32 val = tp->rss_ind_tbl[i];
8727 i++;
8728 for (; i % 8; i++) {
8729 val <<= 4;
8730 val |= tp->rss_ind_tbl[i];
8731 }
8732 tw32(reg, val);
8733 reg += 4;
8734 }
8735}
8736
Matt Carlson2d31eca2009-09-01 12:53:31 +00008737/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008738static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739{
8740 u32 val, rdmac_mode;
8741 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008742 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743
8744 tg3_disable_ints(tp);
8745
8746 tg3_stop_fw(tp);
8747
8748 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8749
Joe Perches63c3a662011-04-26 08:12:10 +00008750 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008751 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752
Matt Carlson699c0192010-12-06 08:28:51 +00008753 /* Enable MAC control of LPI */
8754 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8755 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8756 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8757 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8758
8759 tw32_f(TG3_CPMU_EEE_CTRL,
8760 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8761
Matt Carlsona386b902010-12-06 08:28:53 +00008762 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8763 TG3_CPMU_EEEMD_LPI_IN_TX |
8764 TG3_CPMU_EEEMD_LPI_IN_RX |
8765 TG3_CPMU_EEEMD_EEE_ENABLE;
8766
8767 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8768 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8769
Joe Perches63c3a662011-04-26 08:12:10 +00008770 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008771 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8772
8773 tw32_f(TG3_CPMU_EEE_MODE, val);
8774
8775 tw32_f(TG3_CPMU_EEE_DBTMR1,
8776 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8777 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8778
8779 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008780 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008781 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008782 }
8783
Matt Carlson603f1172010-02-12 14:47:10 +00008784 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008785 tg3_phy_reset(tp);
8786
Linus Torvalds1da177e2005-04-16 15:20:36 -07008787 err = tg3_chip_reset(tp);
8788 if (err)
8789 return err;
8790
8791 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8792
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008793 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008794 val = tr32(TG3_CPMU_CTRL);
8795 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8796 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008797
8798 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8799 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8800 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8801 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8802
8803 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8804 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8805 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8806 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8807
8808 val = tr32(TG3_CPMU_HST_ACC);
8809 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8810 val |= CPMU_HST_ACC_MACCLK_6_25;
8811 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008812 }
8813
Matt Carlson33466d92009-04-20 06:57:41 +00008814 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8815 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8816 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8817 PCIE_PWR_MGMT_L1_THRESH_4MS;
8818 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008819
8820 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8821 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8822
8823 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008824
Matt Carlsonf40386c2009-11-02 14:24:02 +00008825 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8826 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008827 }
8828
Joe Perches63c3a662011-04-26 08:12:10 +00008829 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008830 u32 grc_mode = tr32(GRC_MODE);
8831
8832 /* Access the lower 1K of PL PCIE block registers. */
8833 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8834 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8835
8836 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8837 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8838 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8839
8840 tw32(GRC_MODE, grc_mode);
8841 }
8842
Matt Carlson55086ad2011-12-14 11:09:59 +00008843 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008844 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8845 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008846
Matt Carlson5093eed2010-11-24 08:31:45 +00008847 /* Access the lower 1K of PL PCIE block registers. */
8848 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8849 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008850
Matt Carlson5093eed2010-11-24 08:31:45 +00008851 val = tr32(TG3_PCIE_TLDLPL_PORT +
8852 TG3_PCIE_PL_LO_PHYCTL5);
8853 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8854 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008855
Matt Carlson5093eed2010-11-24 08:31:45 +00008856 tw32(GRC_MODE, grc_mode);
8857 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008858
Matt Carlson1ff30a52011-05-19 12:12:46 +00008859 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8860 u32 grc_mode = tr32(GRC_MODE);
8861
8862 /* Access the lower 1K of DL PCIE block registers. */
8863 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8864 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8865
8866 val = tr32(TG3_PCIE_TLDLPL_PORT +
8867 TG3_PCIE_DL_LO_FTSMAX);
8868 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8869 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8870 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8871
8872 tw32(GRC_MODE, grc_mode);
8873 }
8874
Matt Carlsona977dbe2010-04-12 06:58:26 +00008875 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8876 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8877 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8878 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008879 }
8880
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881 /* This works around an issue with Athlon chipsets on
8882 * B3 tigon3 silicon. This bit has no effect on any
8883 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008884 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885 */
Joe Perches63c3a662011-04-26 08:12:10 +00008886 if (!tg3_flag(tp, CPMU_PRESENT)) {
8887 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008888 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8889 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008891
8892 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008893 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894 val = tr32(TG3PCI_PCISTATE);
8895 val |= PCISTATE_RETRY_SAME_DMA;
8896 tw32(TG3PCI_PCISTATE, val);
8897 }
8898
Joe Perches63c3a662011-04-26 08:12:10 +00008899 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008900 /* Allow reads and writes to the
8901 * APE register and memory space.
8902 */
8903 val = tr32(TG3PCI_PCISTATE);
8904 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008905 PCISTATE_ALLOW_APE_SHMEM_WR |
8906 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008907 tw32(TG3PCI_PCISTATE, val);
8908 }
8909
Linus Torvalds1da177e2005-04-16 15:20:36 -07008910 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8911 /* Enable some hw fixes. */
8912 val = tr32(TG3PCI_MSI_DATA);
8913 val |= (1 << 26) | (1 << 28) | (1 << 29);
8914 tw32(TG3PCI_MSI_DATA, val);
8915 }
8916
8917 /* Descriptor ring init may make accesses to the
8918 * NIC SRAM area to setup the TX descriptors, so we
8919 * can only do this after the hardware has been
8920 * successfully reset.
8921 */
Michael Chan32d8c572006-07-25 16:38:29 -07008922 err = tg3_init_rings(tp);
8923 if (err)
8924 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925
Joe Perches63c3a662011-04-26 08:12:10 +00008926 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008927 val = tr32(TG3PCI_DMA_RW_CTRL) &
8928 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008929 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8930 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008931 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008932 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8933 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008934 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8935 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8936 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008937 /* This value is determined during the probe time DMA
8938 * engine test, tg3_test_dma.
8939 */
8940 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008942
8943 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8944 GRC_MODE_4X_NIC_SEND_RINGS |
8945 GRC_MODE_NO_TX_PHDR_CSUM |
8946 GRC_MODE_NO_RX_PHDR_CSUM);
8947 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008948
8949 /* Pseudo-header checksum is done by hardware logic and not
8950 * the offload processers, so make the chip do the pseudo-
8951 * header checksums on receive. For transmit it is more
8952 * convenient to do the pseudo-header checksum in software
8953 * as Linux does that on transmit for us in all cases.
8954 */
8955 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008956
8957 tw32(GRC_MODE,
8958 tp->grc_mode |
8959 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8960
8961 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8962 val = tr32(GRC_MISC_CFG);
8963 val &= ~0xff;
8964 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8965 tw32(GRC_MISC_CFG, val);
8966
8967 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008968 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008969 /* Do nothing. */
8970 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8971 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8972 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8973 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8974 else
8975 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8976 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8977 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008978 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979 int fw_len;
8980
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008981 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008982 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8983 tw32(BUFMGR_MB_POOL_ADDR,
8984 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8985 tw32(BUFMGR_MB_POOL_SIZE,
8986 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008988
Michael Chan0f893dc2005-07-25 12:30:38 -07008989 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8991 tp->bufmgr_config.mbuf_read_dma_low_water);
8992 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8993 tp->bufmgr_config.mbuf_mac_rx_low_water);
8994 tw32(BUFMGR_MB_HIGH_WATER,
8995 tp->bufmgr_config.mbuf_high_water);
8996 } else {
8997 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8998 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8999 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9000 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9001 tw32(BUFMGR_MB_HIGH_WATER,
9002 tp->bufmgr_config.mbuf_high_water_jumbo);
9003 }
9004 tw32(BUFMGR_DMA_LOW_WATER,
9005 tp->bufmgr_config.dma_low_water);
9006 tw32(BUFMGR_DMA_HIGH_WATER,
9007 tp->bufmgr_config.dma_high_water);
9008
Matt Carlsond309a462010-09-30 10:34:31 +00009009 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
9010 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
9011 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00009012 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9013 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
9014 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
9015 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009016 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009017 for (i = 0; i < 2000; i++) {
9018 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9019 break;
9020 udelay(10);
9021 }
9022 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009023 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009024 return -ENODEV;
9025 }
9026
Matt Carlsoneb07a942011-04-20 07:57:36 +00009027 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
9028 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009029
Matt Carlsoneb07a942011-04-20 07:57:36 +00009030 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009031
9032 /* Initialize TG3_BDINFO's at:
9033 * RCVDBDI_STD_BD: standard eth size rx ring
9034 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9035 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9036 *
9037 * like so:
9038 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9039 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9040 * ring attribute flags
9041 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9042 *
9043 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9044 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9045 *
9046 * The size of each ring is fixed in the firmware, but the location is
9047 * configurable.
9048 */
9049 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009050 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009052 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009053 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009054 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9055 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009057 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009058 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009059 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9060 BDINFO_FLAGS_DISABLED);
9061
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009062 /* Program the jumbo buffer descriptor ring control
9063 * blocks on those devices that have them.
9064 */
Matt Carlsona0512942011-07-27 14:20:54 +00009065 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009066 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009067
Joe Perches63c3a662011-04-26 08:12:10 +00009068 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009070 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009071 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009072 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009073 val = TG3_RX_JMB_RING_SIZE(tp) <<
9074 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009075 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009076 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009077 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009078 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00009079 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9080 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009081 } else {
9082 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9083 BDINFO_FLAGS_DISABLED);
9084 }
9085
Joe Perches63c3a662011-04-26 08:12:10 +00009086 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009087 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009088 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9089 val |= (TG3_RX_STD_DMA_SZ << 2);
9090 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009091 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009092 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009093 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009094
9095 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096
Matt Carlson411da642009-11-13 13:03:46 +00009097 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00009098 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009099
Joe Perches63c3a662011-04-26 08:12:10 +00009100 tpr->rx_jmb_prod_idx =
9101 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00009102 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103
Matt Carlson2d31eca2009-09-01 12:53:31 +00009104 tg3_rings_reset(tp);
9105
Linus Torvalds1da177e2005-04-16 15:20:36 -07009106 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07009107 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009108
9109 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009110 tw32(MAC_RX_MTU_SIZE,
9111 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009112
9113 /* The slot time is changed by tg3_setup_phy if we
9114 * run at gigabit with half duplex.
9115 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009116 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9117 (6 << TX_LENGTHS_IPG_SHIFT) |
9118 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9119
9120 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
9121 val |= tr32(MAC_TX_LENGTHS) &
9122 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9123 TX_LENGTHS_CNT_DWN_VAL_MSK);
9124
9125 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009126
9127 /* Receive rules. */
9128 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9129 tw32(RCVLPC_CONFIG, 0x0181);
9130
9131 /* Calculate RDMAC_MODE setting early, we need it to determine
9132 * the RCVLPC_STATE_ENABLE mask.
9133 */
9134 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9135 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9136 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9137 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9138 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009139
Matt Carlsondeabaac2010-11-24 08:31:50 +00009140 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009141 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9142
Matt Carlson57e69832008-05-25 23:48:31 -07009143 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009144 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9145 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009146 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9147 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9148 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9149
Matt Carlsonc5908932011-03-09 16:58:25 +00009150 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9151 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009152 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07009153 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9155 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009156 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009157 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9158 }
9159 }
9160
Joe Perches63c3a662011-04-26 08:12:10 +00009161 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009162 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9163
Joe Perches63c3a662011-04-26 08:12:10 +00009164 if (tg3_flag(tp, HW_TSO_1) ||
9165 tg3_flag(tp, HW_TSO_2) ||
9166 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009167 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9168
Matt Carlson108a6c12011-05-19 12:12:47 +00009169 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009170 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08009171 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
9172 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009173
Matt Carlsonf2096f92011-04-05 14:22:48 +00009174 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
9175 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9176
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009177 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
9178 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
9179 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9180 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009181 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009182 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Michael Chan10ce95d2012-07-29 19:15:42 +00009183 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009184 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9185 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9186 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9187 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9188 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9189 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009190 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009191 tw32(TG3_RDMA_RSRVCTRL_REG,
9192 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
9193 }
9194
Matt Carlsond78b59f2011-04-05 14:22:46 +00009195 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9196 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00009197 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9198 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
9199 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9200 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9201 }
9202
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009204 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009205 val = tr32(RCVLPC_STATS_ENABLE);
9206 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9207 tw32(RCVLPC_STATS_ENABLE, val);
9208 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009209 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009210 val = tr32(RCVLPC_STATS_ENABLE);
9211 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9212 tw32(RCVLPC_STATS_ENABLE, val);
9213 } else {
9214 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9215 }
9216 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9217 tw32(SNDDATAI_STATSENAB, 0xffffff);
9218 tw32(SNDDATAI_STATSCTRL,
9219 (SNDDATAI_SCTRL_ENABLE |
9220 SNDDATAI_SCTRL_FASTUPD));
9221
9222 /* Setup host coalescing engine. */
9223 tw32(HOSTCC_MODE, 0);
9224 for (i = 0; i < 2000; i++) {
9225 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9226 break;
9227 udelay(10);
9228 }
9229
Michael Chand244c892005-07-05 14:42:33 -07009230 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231
Joe Perches63c3a662011-04-26 08:12:10 +00009232 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233 /* Status/statistics block address. See tg3_timer,
9234 * the tg3_periodic_fetch_stats call there, and
9235 * tg3_get_stats to see how this works for 5705/5750 chips.
9236 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9238 ((u64) tp->stats_mapping >> 32));
9239 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9240 ((u64) tp->stats_mapping & 0xffffffff));
9241 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009242
Linus Torvalds1da177e2005-04-16 15:20:36 -07009243 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009244
9245 /* Clear statistics and status block memory areas */
9246 for (i = NIC_SRAM_STATS_BLK;
9247 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9248 i += sizeof(u32)) {
9249 tg3_write_mem(tp, i, 0);
9250 udelay(40);
9251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009252 }
9253
9254 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9255
9256 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9257 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009258 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009259 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9260
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009261 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9262 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009263 /* reset to prevent losing 1st rx packet intermittently */
9264 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9265 udelay(10);
9266 }
9267
Matt Carlson3bda1252008-08-15 14:08:22 -07009268 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009269 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9270 MAC_MODE_FHDE_ENABLE;
9271 if (tg3_flag(tp, ENABLE_APE))
9272 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009273 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009274 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009275 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9276 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009277 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9278 udelay(40);
9279
Michael Chan314fba32005-04-21 17:07:04 -07009280 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009281 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009282 * register to preserve the GPIO settings for LOMs. The GPIOs,
9283 * whether used as inputs or outputs, are set by boot code after
9284 * reset.
9285 */
Joe Perches63c3a662011-04-26 08:12:10 +00009286 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009287 u32 gpio_mask;
9288
Michael Chan9d26e212006-12-07 00:21:14 -08009289 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9290 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9291 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009292
9293 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9294 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9295 GRC_LCLCTRL_GPIO_OUTPUT3;
9296
Michael Chanaf36e6b2006-03-23 01:28:06 -08009297 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9298 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9299
Gary Zambranoaaf84462007-05-05 11:51:45 -07009300 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009301 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9302
9303 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009304 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009305 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9306 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009308 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9309 udelay(100);
9310
Matt Carlsonc3b50032012-01-17 15:27:23 +00009311 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009312 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009313 val |= MSGINT_MODE_ENABLE;
9314 if (tp->irq_cnt > 1)
9315 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009316 if (!tg3_flag(tp, 1SHOT_MSI))
9317 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009318 tw32(MSGINT_MODE, val);
9319 }
9320
Joe Perches63c3a662011-04-26 08:12:10 +00009321 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9323 udelay(40);
9324 }
9325
9326 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9327 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9328 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9329 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9330 WDMAC_MODE_LNGREAD_ENAB);
9331
Matt Carlsonc5908932011-03-09 16:58:25 +00009332 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9333 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009334 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9336 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9337 /* nothing */
9338 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009339 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009340 val |= WDMAC_MODE_RX_ACCEL;
9341 }
9342 }
9343
Michael Chand9ab5ad2006-03-20 22:27:35 -08009344 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009345 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009346 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08009347
Matt Carlson788a0352009-11-02 14:26:03 +00009348 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9349 val |= WDMAC_MODE_BURST_ALL_DATA;
9350
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351 tw32_f(WDMAC_MODE, val);
9352 udelay(40);
9353
Joe Perches63c3a662011-04-26 08:12:10 +00009354 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009355 u16 pcix_cmd;
9356
9357 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9358 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009359 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009360 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9361 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009362 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009363 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9364 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 }
Matt Carlson9974a352007-10-07 23:27:28 -07009366 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9367 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368 }
9369
9370 tw32_f(RDMAC_MODE, rdmac_mode);
9371 udelay(40);
9372
Michael Chan091f0ea2012-07-29 19:15:43 +00009373 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
9374 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
9375 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
9376 break;
9377 }
9378 if (i < TG3_NUM_RDMA_CHANNELS) {
9379 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9380 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
9381 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9382 tg3_flag_set(tp, 5719_RDMA_BUG);
9383 }
9384 }
9385
Linus Torvalds1da177e2005-04-16 15:20:36 -07009386 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009387 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009388 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009389
9390 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9391 tw32(SNDDATAC_MODE,
9392 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9393 else
9394 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9395
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9397 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009398 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009399 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009400 val |= RCVDBDI_MODE_LRG_RING_SZ;
9401 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009403 if (tg3_flag(tp, HW_TSO_1) ||
9404 tg3_flag(tp, HW_TSO_2) ||
9405 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009407 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009408 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009409 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9410 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009411 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9412
9413 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9414 err = tg3_load_5701_a0_firmware_fix(tp);
9415 if (err)
9416 return err;
9417 }
9418
Joe Perches63c3a662011-04-26 08:12:10 +00009419 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009420 err = tg3_load_tso_firmware(tp);
9421 if (err)
9422 return err;
9423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424
9425 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009426
Joe Perches63c3a662011-04-26 08:12:10 +00009427 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009428 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9429 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009430
9431 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9432 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9433 tp->tx_mode &= ~val;
9434 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9435 }
9436
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437 tw32_f(MAC_TX_MODE, tp->tx_mode);
9438 udelay(100);
9439
Joe Perches63c3a662011-04-26 08:12:10 +00009440 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009441 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009442
9443 /* Setup the "secret" hash key. */
9444 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9445 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9446 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9447 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9448 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9449 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9450 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9451 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9452 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9453 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9454 }
9455
Linus Torvalds1da177e2005-04-16 15:20:36 -07009456 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009457 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009458 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9459
Joe Perches63c3a662011-04-26 08:12:10 +00009460 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009461 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9462 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9463 RX_MODE_RSS_IPV6_HASH_EN |
9464 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9465 RX_MODE_RSS_IPV4_HASH_EN |
9466 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9467
Linus Torvalds1da177e2005-04-16 15:20:36 -07009468 tw32_f(MAC_RX_MODE, tp->rx_mode);
9469 udelay(10);
9470
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471 tw32(MAC_LED_CTRL, tp->led_ctrl);
9472
9473 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009474 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009475 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9476 udelay(10);
9477 }
9478 tw32_f(MAC_RX_MODE, tp->rx_mode);
9479 udelay(10);
9480
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009481 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009482 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009483 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484 /* Set drive transmission level to 1.2V */
9485 /* only if the signal pre-emphasis bit is not set */
9486 val = tr32(MAC_SERDES_CFG);
9487 val &= 0xfffff000;
9488 val |= 0x880;
9489 tw32(MAC_SERDES_CFG, val);
9490 }
9491 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9492 tw32(MAC_SERDES_CFG, 0x616000);
9493 }
9494
9495 /* Prevent chip from dropping frames when flow control
9496 * is enabled.
9497 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009498 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009499 val = 1;
9500 else
9501 val = 2;
9502 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009503
9504 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009505 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009506 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009507 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508 }
9509
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009510 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009511 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009512 u32 tmp;
9513
9514 tmp = tr32(SERDES_RX_CTRL);
9515 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9516 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9517 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9518 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9519 }
9520
Joe Perches63c3a662011-04-26 08:12:10 +00009521 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009522 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson800960682010-08-02 11:26:06 +00009523 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524
Matt Carlsondd477002008-05-25 23:45:58 -07009525 err = tg3_setup_phy(tp, 0);
9526 if (err)
9527 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009528
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009529 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9530 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009531 u32 tmp;
9532
9533 /* Clear CRC stats. */
9534 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9535 tg3_writephy(tp, MII_TG3_TEST1,
9536 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009537 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009539 }
9540 }
9541
9542 __tg3_set_rx_mode(tp->dev);
9543
9544 /* Initialize receive rules. */
9545 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9546 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9547 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9548 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9549
Joe Perches63c3a662011-04-26 08:12:10 +00009550 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009551 limit = 8;
9552 else
9553 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009554 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555 limit -= 4;
9556 switch (limit) {
9557 case 16:
9558 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9559 case 15:
9560 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9561 case 14:
9562 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9563 case 13:
9564 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9565 case 12:
9566 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9567 case 11:
9568 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9569 case 10:
9570 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9571 case 9:
9572 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9573 case 8:
9574 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9575 case 7:
9576 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9577 case 6:
9578 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9579 case 5:
9580 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9581 case 4:
9582 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9583 case 3:
9584 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9585 case 2:
9586 case 1:
9587
9588 default:
9589 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009591
Joe Perches63c3a662011-04-26 08:12:10 +00009592 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009593 /* Write our heartbeat update interval to APE. */
9594 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9595 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009596
Linus Torvalds1da177e2005-04-16 15:20:36 -07009597 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9598
Linus Torvalds1da177e2005-04-16 15:20:36 -07009599 return 0;
9600}
9601
9602/* Called at device open time to get the chip ready for
9603 * packet processing. Invoked with tp->lock held.
9604 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009605static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009606{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009607 tg3_switch_clocks(tp);
9608
9609 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9610
Matt Carlson2f751b62008-08-04 23:17:34 -07009611 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009612}
9613
Michael Chanaed93e02012-07-16 16:24:02 +00009614static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
9615{
9616 int i;
9617
9618 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
9619 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
9620
9621 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
9622 off += len;
9623
9624 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
9625 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
9626 memset(ocir, 0, TG3_OCIR_LEN);
9627 }
9628}
9629
9630/* sysfs attributes for hwmon */
9631static ssize_t tg3_show_temp(struct device *dev,
9632 struct device_attribute *devattr, char *buf)
9633{
9634 struct pci_dev *pdev = to_pci_dev(dev);
9635 struct net_device *netdev = pci_get_drvdata(pdev);
9636 struct tg3 *tp = netdev_priv(netdev);
9637 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9638 u32 temperature;
9639
9640 spin_lock_bh(&tp->lock);
9641 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
9642 sizeof(temperature));
9643 spin_unlock_bh(&tp->lock);
9644 return sprintf(buf, "%u\n", temperature);
9645}
9646
9647
9648static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
9649 TG3_TEMP_SENSOR_OFFSET);
9650static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
9651 TG3_TEMP_CAUTION_OFFSET);
9652static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
9653 TG3_TEMP_MAX_OFFSET);
9654
9655static struct attribute *tg3_attributes[] = {
9656 &sensor_dev_attr_temp1_input.dev_attr.attr,
9657 &sensor_dev_attr_temp1_crit.dev_attr.attr,
9658 &sensor_dev_attr_temp1_max.dev_attr.attr,
9659 NULL
9660};
9661
9662static const struct attribute_group tg3_group = {
9663 .attrs = tg3_attributes,
9664};
9665
Michael Chanaed93e02012-07-16 16:24:02 +00009666static void tg3_hwmon_close(struct tg3 *tp)
9667{
Michael Chanaed93e02012-07-16 16:24:02 +00009668 if (tp->hwmon_dev) {
9669 hwmon_device_unregister(tp->hwmon_dev);
9670 tp->hwmon_dev = NULL;
9671 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
9672 }
Michael Chanaed93e02012-07-16 16:24:02 +00009673}
9674
9675static void tg3_hwmon_open(struct tg3 *tp)
9676{
Michael Chanaed93e02012-07-16 16:24:02 +00009677 int i, err;
9678 u32 size = 0;
9679 struct pci_dev *pdev = tp->pdev;
9680 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
9681
9682 tg3_sd_scan_scratchpad(tp, ocirs);
9683
9684 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
9685 if (!ocirs[i].src_data_length)
9686 continue;
9687
9688 size += ocirs[i].src_hdr_length;
9689 size += ocirs[i].src_data_length;
9690 }
9691
9692 if (!size)
9693 return;
9694
9695 /* Register hwmon sysfs hooks */
9696 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
9697 if (err) {
9698 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
9699 return;
9700 }
9701
9702 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
9703 if (IS_ERR(tp->hwmon_dev)) {
9704 tp->hwmon_dev = NULL;
9705 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
9706 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
9707 }
Michael Chanaed93e02012-07-16 16:24:02 +00009708}
9709
9710
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711#define TG3_STAT_ADD32(PSTAT, REG) \
9712do { u32 __val = tr32(REG); \
9713 (PSTAT)->low += __val; \
9714 if ((PSTAT)->low < __val) \
9715 (PSTAT)->high += 1; \
9716} while (0)
9717
9718static void tg3_periodic_fetch_stats(struct tg3 *tp)
9719{
9720 struct tg3_hw_stats *sp = tp->hw_stats;
9721
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009722 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009723 return;
9724
9725 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9726 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9727 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9728 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9729 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9730 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9731 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9732 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9733 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9734 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9735 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9736 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9737 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +00009738 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
9739 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
9740 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
9741 u32 val;
9742
9743 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9744 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
9745 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9746 tg3_flag_clear(tp, 5719_RDMA_BUG);
9747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748
9749 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9750 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9751 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9752 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9753 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9754 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9755 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9756 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9757 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9758 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9759 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9760 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9761 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9762 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009763
9764 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009765 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9766 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9767 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009768 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9769 } else {
9770 u32 val = tr32(HOSTCC_FLOW_ATTN);
9771 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9772 if (val) {
9773 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9774 sp->rx_discards.low += val;
9775 if (sp->rx_discards.low < val)
9776 sp->rx_discards.high += 1;
9777 }
9778 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9779 }
Michael Chan463d3052006-05-22 16:36:27 -07009780 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009781}
9782
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009783static void tg3_chk_missed_msi(struct tg3 *tp)
9784{
9785 u32 i;
9786
9787 for (i = 0; i < tp->irq_cnt; i++) {
9788 struct tg3_napi *tnapi = &tp->napi[i];
9789
9790 if (tg3_has_work(tnapi)) {
9791 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9792 tnapi->last_tx_cons == tnapi->tx_cons) {
9793 if (tnapi->chk_msi_cnt < 1) {
9794 tnapi->chk_msi_cnt++;
9795 return;
9796 }
Matt Carlson7f230732011-08-31 11:44:48 +00009797 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009798 }
9799 }
9800 tnapi->chk_msi_cnt = 0;
9801 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9802 tnapi->last_tx_cons = tnapi->tx_cons;
9803 }
9804}
9805
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806static void tg3_timer(unsigned long __opaque)
9807{
9808 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009809
Matt Carlson5b190622011-11-04 09:15:04 +00009810 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009811 goto restart_timer;
9812
David S. Millerf47c11e2005-06-24 20:18:35 -07009813 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009815 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009816 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009817 tg3_chk_missed_msi(tp);
9818
Joe Perches63c3a662011-04-26 08:12:10 +00009819 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009820 /* All of this garbage is because when using non-tagged
9821 * IRQ status the mailbox/status_block protocol the chip
9822 * uses with the cpu is race prone.
9823 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009824 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009825 tw32(GRC_LOCAL_CTRL,
9826 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9827 } else {
9828 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009829 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009831
David S. Millerfac9b832005-05-18 22:46:34 -07009832 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009833 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009834 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009835 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837 }
9838
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839 /* This part only runs once per second. */
9840 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009841 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009842 tg3_periodic_fetch_stats(tp);
9843
Matt Carlsonb0c59432011-05-19 12:12:48 +00009844 if (tp->setlpicnt && !--tp->setlpicnt)
9845 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009846
Joe Perches63c3a662011-04-26 08:12:10 +00009847 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848 u32 mac_stat;
9849 int phy_event;
9850
9851 mac_stat = tr32(MAC_STATUS);
9852
9853 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009854 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9856 phy_event = 1;
9857 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9858 phy_event = 1;
9859
9860 if (phy_event)
9861 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009862 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009863 u32 mac_stat = tr32(MAC_STATUS);
9864 int need_setup = 0;
9865
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009866 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009867 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9868 need_setup = 1;
9869 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009870 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009871 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9872 MAC_STATUS_SIGNAL_DET))) {
9873 need_setup = 1;
9874 }
9875 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009876 if (!tp->serdes_counter) {
9877 tw32_f(MAC_MODE,
9878 (tp->mac_mode &
9879 ~MAC_MODE_PORT_MODE_MASK));
9880 udelay(40);
9881 tw32_f(MAC_MODE, tp->mac_mode);
9882 udelay(40);
9883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009884 tg3_setup_phy(tp, 0);
9885 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009886 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009887 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009888 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890
9891 tp->timer_counter = tp->timer_multiplier;
9892 }
9893
Michael Chan130b8e42006-09-27 16:00:40 -07009894 /* Heartbeat is only sent once every 2 seconds.
9895 *
9896 * The heartbeat is to tell the ASF firmware that the host
9897 * driver is still alive. In the event that the OS crashes,
9898 * ASF needs to reset the hardware to free up the FIFO space
9899 * that may be filled with rx packets destined for the host.
9900 * If the FIFO is full, ASF will no longer function properly.
9901 *
9902 * Unintended resets have been reported on real time kernels
9903 * where the timer doesn't run on time. Netpoll will also have
9904 * same problem.
9905 *
9906 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9907 * to check the ring condition when the heartbeat is expiring
9908 * before doing the reset. This will prevent most unintended
9909 * resets.
9910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009911 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009912 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009913 tg3_wait_for_event_ack(tp);
9914
Michael Chanbbadf502006-04-06 21:46:34 -07009915 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009916 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009917 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009918 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9919 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009920
9921 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009922 }
9923 tp->asf_counter = tp->asf_multiplier;
9924 }
9925
David S. Millerf47c11e2005-06-24 20:18:35 -07009926 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009927
Michael Chanf475f162006-03-27 23:20:14 -08009928restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009929 tp->timer.expires = jiffies + tp->timer_offset;
9930 add_timer(&tp->timer);
9931}
9932
Matt Carlson21f76382012-02-22 12:35:21 +00009933static void __devinit tg3_timer_init(struct tg3 *tp)
9934{
9935 if (tg3_flag(tp, TAGGED_STATUS) &&
9936 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9937 !tg3_flag(tp, 57765_CLASS))
9938 tp->timer_offset = HZ;
9939 else
9940 tp->timer_offset = HZ / 10;
9941
9942 BUG_ON(tp->timer_offset > HZ);
9943
9944 tp->timer_multiplier = (HZ / tp->timer_offset);
9945 tp->asf_multiplier = (HZ / tp->timer_offset) *
9946 TG3_FW_UPDATE_FREQ_SEC;
9947
9948 init_timer(&tp->timer);
9949 tp->timer.data = (unsigned long) tp;
9950 tp->timer.function = tg3_timer;
9951}
9952
9953static void tg3_timer_start(struct tg3 *tp)
9954{
9955 tp->asf_counter = tp->asf_multiplier;
9956 tp->timer_counter = tp->timer_multiplier;
9957
9958 tp->timer.expires = jiffies + tp->timer_offset;
9959 add_timer(&tp->timer);
9960}
9961
9962static void tg3_timer_stop(struct tg3 *tp)
9963{
9964 del_timer_sync(&tp->timer);
9965}
9966
9967/* Restart hardware after configuration changes, self-test, etc.
9968 * Invoked with tp->lock held.
9969 */
9970static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
9971 __releases(tp->lock)
9972 __acquires(tp->lock)
9973{
9974 int err;
9975
9976 err = tg3_init_hw(tp, reset_phy);
9977 if (err) {
9978 netdev_err(tp->dev,
9979 "Failed to re-initialize device, aborting\n");
9980 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9981 tg3_full_unlock(tp);
9982 tg3_timer_stop(tp);
9983 tp->irq_sync = 0;
9984 tg3_napi_enable(tp);
9985 dev_close(tp->dev);
9986 tg3_full_lock(tp, 0);
9987 }
9988 return err;
9989}
9990
9991static void tg3_reset_task(struct work_struct *work)
9992{
9993 struct tg3 *tp = container_of(work, struct tg3, reset_task);
9994 int err;
9995
9996 tg3_full_lock(tp, 0);
9997
9998 if (!netif_running(tp->dev)) {
9999 tg3_flag_clear(tp, RESET_TASK_PENDING);
10000 tg3_full_unlock(tp);
10001 return;
10002 }
10003
10004 tg3_full_unlock(tp);
10005
10006 tg3_phy_stop(tp);
10007
10008 tg3_netif_stop(tp);
10009
10010 tg3_full_lock(tp, 1);
10011
10012 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10013 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10014 tp->write32_rx_mbox = tg3_write_flush_reg32;
10015 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10016 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10017 }
10018
10019 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
10020 err = tg3_init_hw(tp, 1);
10021 if (err)
10022 goto out;
10023
10024 tg3_netif_start(tp);
10025
10026out:
10027 tg3_full_unlock(tp);
10028
10029 if (!err)
10030 tg3_phy_start(tp);
10031
10032 tg3_flag_clear(tp, RESET_TASK_PENDING);
10033}
10034
Matt Carlson4f125f42009-09-01 12:55:02 +000010035static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010036{
David Howells7d12e782006-10-05 14:55:46 +010010037 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010038 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010039 char *name;
10040 struct tg3_napi *tnapi = &tp->napi[irq_num];
10041
10042 if (tp->irq_cnt == 1)
10043 name = tp->dev->name;
10044 else {
10045 name = &tnapi->irq_lbl[0];
10046 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10047 name[IFNAMSIZ-1] = 0;
10048 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010049
Joe Perches63c3a662011-04-26 08:12:10 +000010050 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010051 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010052 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010053 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010054 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010055 } else {
10056 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010057 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010058 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010059 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010060 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010061
10062 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010063}
10064
Michael Chan79381092005-04-21 17:13:59 -070010065static int tg3_test_interrupt(struct tg3 *tp)
10066{
Matt Carlson09943a12009-08-28 14:01:57 +000010067 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010068 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010069 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010070 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010071
Michael Chand4bc3922005-05-29 14:59:20 -070010072 if (!netif_running(dev))
10073 return -ENODEV;
10074
Michael Chan79381092005-04-21 17:13:59 -070010075 tg3_disable_ints(tp);
10076
Matt Carlson4f125f42009-09-01 12:55:02 +000010077 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010078
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010079 /*
10080 * Turn off MSI one shot mode. Otherwise this test has no
10081 * observable way to know whether the interrupt was delivered.
10082 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010083 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010084 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10085 tw32(MSGINT_MODE, val);
10086 }
10087
Matt Carlson4f125f42009-09-01 12:55:02 +000010088 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010089 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010090 if (err)
10091 return err;
10092
Matt Carlson898a56f2009-08-28 14:02:40 +000010093 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010094 tg3_enable_ints(tp);
10095
10096 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010097 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010098
10099 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010100 u32 int_mbox, misc_host_ctrl;
10101
Matt Carlson898a56f2009-08-28 14:02:40 +000010102 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010103 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10104
10105 if ((int_mbox != 0) ||
10106 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10107 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010108 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010109 }
10110
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010111 if (tg3_flag(tp, 57765_PLUS) &&
10112 tnapi->hw_status->status_tag != tnapi->last_tag)
10113 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10114
Michael Chan79381092005-04-21 17:13:59 -070010115 msleep(10);
10116 }
10117
10118 tg3_disable_ints(tp);
10119
Matt Carlson4f125f42009-09-01 12:55:02 +000010120 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010121
Matt Carlson4f125f42009-09-01 12:55:02 +000010122 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010123
10124 if (err)
10125 return err;
10126
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010127 if (intr_ok) {
10128 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010129 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010130 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10131 tw32(MSGINT_MODE, val);
10132 }
Michael Chan79381092005-04-21 17:13:59 -070010133 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010134 }
Michael Chan79381092005-04-21 17:13:59 -070010135
10136 return -EIO;
10137}
10138
10139/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10140 * successfully restored
10141 */
10142static int tg3_test_msi(struct tg3 *tp)
10143{
Michael Chan79381092005-04-21 17:13:59 -070010144 int err;
10145 u16 pci_cmd;
10146
Joe Perches63c3a662011-04-26 08:12:10 +000010147 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010148 return 0;
10149
10150 /* Turn off SERR reporting in case MSI terminates with Master
10151 * Abort.
10152 */
10153 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10154 pci_write_config_word(tp->pdev, PCI_COMMAND,
10155 pci_cmd & ~PCI_COMMAND_SERR);
10156
10157 err = tg3_test_interrupt(tp);
10158
10159 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10160
10161 if (!err)
10162 return 0;
10163
10164 /* other failures */
10165 if (err != -EIO)
10166 return err;
10167
10168 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010169 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10170 "to INTx mode. Please report this failure to the PCI "
10171 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010172
Matt Carlson4f125f42009-09-01 12:55:02 +000010173 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010174
Michael Chan79381092005-04-21 17:13:59 -070010175 pci_disable_msi(tp->pdev);
10176
Joe Perches63c3a662011-04-26 08:12:10 +000010177 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010178 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010179
Matt Carlson4f125f42009-09-01 12:55:02 +000010180 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010181 if (err)
10182 return err;
10183
10184 /* Need to reset the chip because the MSI cycle may have terminated
10185 * with Master Abort.
10186 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010187 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010188
Michael Chan944d9802005-05-29 14:57:48 -070010189 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010190 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010191
David S. Millerf47c11e2005-06-24 20:18:35 -070010192 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010193
10194 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000010195 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070010196
10197 return err;
10198}
10199
Matt Carlson9e9fd122009-01-19 16:57:45 -080010200static int tg3_request_firmware(struct tg3 *tp)
10201{
10202 const __be32 *fw_data;
10203
10204 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010205 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
10206 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010207 return -ENOENT;
10208 }
10209
10210 fw_data = (void *)tp->fw->data;
10211
10212 /* Firmware blob starts with version numbers, followed by
10213 * start address and _full_ length including BSS sections
10214 * (which must be longer than the actual data, of course
10215 */
10216
10217 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
10218 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010219 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
10220 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010221 release_firmware(tp->fw);
10222 tp->fw = NULL;
10223 return -EINVAL;
10224 }
10225
10226 /* We no longer need firmware; we have it. */
10227 tp->fw_needed = NULL;
10228 return 0;
10229}
10230
Michael Chan91024262012-09-28 07:12:38 +000010231static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000010232{
Michael Chan91024262012-09-28 07:12:38 +000010233 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000010234
Michael Chan91024262012-09-28 07:12:38 +000010235 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000010236 /* We want as many rx rings enabled as there are cpus.
10237 * In multiqueue MSI-X mode, the first MSI-X vector
10238 * only deals with link interrupts, etc, so we add
10239 * one to the number of vectors we are requesting.
10240 */
Michael Chan91024262012-09-28 07:12:38 +000010241 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010242 }
Matt Carlson679563f2009-09-01 12:55:46 +000010243
Michael Chan91024262012-09-28 07:12:38 +000010244 return irq_cnt;
10245}
10246
10247static bool tg3_enable_msix(struct tg3 *tp)
10248{
10249 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070010250 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000010251
Michael Chan09681692012-09-28 07:12:42 +000010252 tp->txq_cnt = tp->txq_req;
10253 tp->rxq_cnt = tp->rxq_req;
10254 if (!tp->rxq_cnt)
10255 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000010256 if (tp->rxq_cnt > tp->rxq_max)
10257 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000010258
10259 /* Disable multiple TX rings by default. Simple round-robin hardware
10260 * scheduling of the TX rings can cause starvation of rings with
10261 * small packets when other rings have TSO or jumbo packets.
10262 */
10263 if (!tp->txq_req)
10264 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000010265
10266 tp->irq_cnt = tg3_irq_count(tp);
10267
Matt Carlson679563f2009-09-01 12:55:46 +000010268 for (i = 0; i < tp->irq_max; i++) {
10269 msix_ent[i].entry = i;
10270 msix_ent[i].vector = 0;
10271 }
10272
10273 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010274 if (rc < 0) {
10275 return false;
10276 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000010277 if (pci_enable_msix(tp->pdev, msix_ent, rc))
10278 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000010279 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
10280 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000010281 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000010282 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000010283 if (tp->txq_cnt)
10284 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000010285 }
10286
10287 for (i = 0; i < tp->irq_max; i++)
10288 tp->napi[i].irq_vec = msix_ent[i].vector;
10289
Michael Chan49a359e2012-09-28 07:12:37 +000010290 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010291 pci_disable_msix(tp->pdev);
10292 return false;
10293 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000010294
Michael Chan91024262012-09-28 07:12:38 +000010295 if (tp->irq_cnt == 1)
10296 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000010297
Michael Chan91024262012-09-28 07:12:38 +000010298 tg3_flag_set(tp, ENABLE_RSS);
10299
10300 if (tp->txq_cnt > 1)
10301 tg3_flag_set(tp, ENABLE_TSS);
10302
10303 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010304
Matt Carlson679563f2009-09-01 12:55:46 +000010305 return true;
10306}
10307
Matt Carlson07b01732009-08-28 14:01:15 +000010308static void tg3_ints_init(struct tg3 *tp)
10309{
Joe Perches63c3a662011-04-26 08:12:10 +000010310 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
10311 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000010312 /* All MSI supporting chips should support tagged
10313 * status. Assert that this is the case.
10314 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010315 netdev_warn(tp->dev,
10316 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000010317 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000010318 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010319
Joe Perches63c3a662011-04-26 08:12:10 +000010320 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
10321 tg3_flag_set(tp, USING_MSIX);
10322 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
10323 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000010324
Joe Perches63c3a662011-04-26 08:12:10 +000010325 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010326 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000010327 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010328 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010329 if (!tg3_flag(tp, 1SHOT_MSI))
10330 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000010331 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
10332 }
10333defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000010334 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010335 tp->irq_cnt = 1;
10336 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000010337 }
10338
10339 if (tp->irq_cnt == 1) {
10340 tp->txq_cnt = 1;
10341 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010342 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070010343 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000010344 }
Matt Carlson07b01732009-08-28 14:01:15 +000010345}
10346
10347static void tg3_ints_fini(struct tg3 *tp)
10348{
Joe Perches63c3a662011-04-26 08:12:10 +000010349 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000010350 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010351 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000010352 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010353 tg3_flag_clear(tp, USING_MSI);
10354 tg3_flag_clear(tp, USING_MSIX);
10355 tg3_flag_clear(tp, ENABLE_RSS);
10356 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010357}
10358
Michael Chand8f4cd32012-09-28 07:12:40 +000010359static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010360{
Michael Chand8f4cd32012-09-28 07:12:40 +000010361 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000010362 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010363
Matt Carlson679563f2009-09-01 12:55:46 +000010364 /*
10365 * Setup interrupts first so we know how
10366 * many NAPI resources to allocate
10367 */
10368 tg3_ints_init(tp);
10369
Matt Carlson90415472011-12-16 13:33:23 +000010370 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010371
Linus Torvalds1da177e2005-04-16 15:20:36 -070010372 /* The placement of this call is tied
10373 * to the setup and use of Host TX descriptors.
10374 */
10375 err = tg3_alloc_consistent(tp);
10376 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010377 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010378
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010379 tg3_napi_init(tp);
10380
Matt Carlsonfed97812009-09-01 13:10:19 +000010381 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010382
Matt Carlson4f125f42009-09-01 12:55:02 +000010383 for (i = 0; i < tp->irq_cnt; i++) {
10384 struct tg3_napi *tnapi = &tp->napi[i];
10385 err = tg3_request_irq(tp, i);
10386 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010387 for (i--; i >= 0; i--) {
10388 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010389 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010390 }
10391 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010392 }
10393 }
Matt Carlson07b01732009-08-28 14:01:15 +000010394
David S. Millerf47c11e2005-06-24 20:18:35 -070010395 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010396
Michael Chand8f4cd32012-09-28 07:12:40 +000010397 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010398 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010399 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010400 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010401 }
10402
David S. Millerf47c11e2005-06-24 20:18:35 -070010403 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010404
Matt Carlson07b01732009-08-28 14:01:15 +000010405 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010406 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407
Michael Chand8f4cd32012-09-28 07:12:40 +000010408 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010409 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010410
Michael Chan79381092005-04-21 17:13:59 -070010411 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010412 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010413 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010414 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010415 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010416
Matt Carlson679563f2009-09-01 12:55:46 +000010417 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010418 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010419
Joe Perches63c3a662011-04-26 08:12:10 +000010420 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010421 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010422
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010423 tw32(PCIE_TRANSACTION_CFG,
10424 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010425 }
Michael Chan79381092005-04-21 17:13:59 -070010426 }
10427
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010428 tg3_phy_start(tp);
10429
Michael Chanaed93e02012-07-16 16:24:02 +000010430 tg3_hwmon_open(tp);
10431
David S. Millerf47c11e2005-06-24 20:18:35 -070010432 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010433
Matt Carlson21f76382012-02-22 12:35:21 +000010434 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010435 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010436 tg3_enable_ints(tp);
10437
David S. Millerf47c11e2005-06-24 20:18:35 -070010438 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010439
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010440 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010441
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010442 /*
10443 * Reset loopback feature if it was turned on while the device was down
10444 * make sure that it's installed properly now.
10445 */
10446 if (dev->features & NETIF_F_LOOPBACK)
10447 tg3_set_loopback(dev, dev->features);
10448
Linus Torvalds1da177e2005-04-16 15:20:36 -070010449 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010450
Matt Carlson679563f2009-09-01 12:55:46 +000010451err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010452 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10453 struct tg3_napi *tnapi = &tp->napi[i];
10454 free_irq(tnapi->irq_vec, tnapi);
10455 }
Matt Carlson07b01732009-08-28 14:01:15 +000010456
Matt Carlson679563f2009-09-01 12:55:46 +000010457err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010458 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010459 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010460 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010461
10462err_out1:
10463 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010464
Matt Carlson07b01732009-08-28 14:01:15 +000010465 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010466}
10467
Michael Chan65138592012-09-28 07:12:41 +000010468static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010469{
Matt Carlson4f125f42009-09-01 12:55:02 +000010470 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010471
Matt Carlsondb219972011-11-04 09:15:03 +000010472 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000010473 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010474
Matt Carlson21f76382012-02-22 12:35:21 +000010475 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010476
Michael Chanaed93e02012-07-16 16:24:02 +000010477 tg3_hwmon_close(tp);
10478
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010479 tg3_phy_stop(tp);
10480
David S. Millerf47c11e2005-06-24 20:18:35 -070010481 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482
10483 tg3_disable_ints(tp);
10484
Michael Chan944d9802005-05-29 14:57:48 -070010485 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010486 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010487 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010488
David S. Millerf47c11e2005-06-24 20:18:35 -070010489 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010490
Matt Carlson4f125f42009-09-01 12:55:02 +000010491 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10492 struct tg3_napi *tnapi = &tp->napi[i];
10493 free_irq(tnapi->irq_vec, tnapi);
10494 }
Matt Carlson07b01732009-08-28 14:01:15 +000010495
10496 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010497
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010498 tg3_napi_fini(tp);
10499
Linus Torvalds1da177e2005-04-16 15:20:36 -070010500 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000010501}
10502
Michael Chand8f4cd32012-09-28 07:12:40 +000010503static int tg3_open(struct net_device *dev)
10504{
10505 struct tg3 *tp = netdev_priv(dev);
10506 int err;
10507
10508 if (tp->fw_needed) {
10509 err = tg3_request_firmware(tp);
10510 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
10511 if (err)
10512 return err;
10513 } else if (err) {
10514 netdev_warn(tp->dev, "TSO capability disabled\n");
10515 tg3_flag_clear(tp, TSO_CAPABLE);
10516 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
10517 netdev_notice(tp->dev, "TSO capability restored\n");
10518 tg3_flag_set(tp, TSO_CAPABLE);
10519 }
10520 }
10521
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010522 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010523
10524 err = tg3_power_up(tp);
10525 if (err)
10526 return err;
10527
10528 tg3_full_lock(tp, 0);
10529
10530 tg3_disable_ints(tp);
10531 tg3_flag_clear(tp, INIT_COMPLETE);
10532
10533 tg3_full_unlock(tp);
10534
10535 err = tg3_start(tp, true, true);
10536 if (err) {
10537 tg3_frob_aux_power(tp, false);
10538 pci_set_power_state(tp->pdev, PCI_D3hot);
10539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010540 return err;
10541}
10542
10543static int tg3_close(struct net_device *dev)
10544{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010545 struct tg3 *tp = netdev_priv(dev);
10546
Michael Chan65138592012-09-28 07:12:41 +000010547 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010548
10549 /* Clear stats across close / open calls */
10550 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10551 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010552
10553 tg3_power_down(tp);
10554
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010555 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010556
10557 return 0;
10558}
10559
10560static inline u64 get_stat64(tg3_stat64_t *val)
10561{
10562 return ((u64)val->high << 32) | ((u64)val->low);
10563}
10564
10565static u64 tg3_calc_crc_errors(struct tg3 *tp)
10566{
10567 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10568
10569 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10570 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10571 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
10572 u32 val;
10573
10574 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10575 tg3_writephy(tp, MII_TG3_TEST1,
10576 val | MII_TG3_TEST1_CRC_EN);
10577 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
10578 } else
10579 val = 0;
10580
10581 tp->phy_crc_errors += val;
10582
10583 return tp->phy_crc_errors;
10584 }
10585
10586 return get_stat64(&hw_stats->rx_fcs_errors);
10587}
10588
10589#define ESTAT_ADD(member) \
10590 estats->member = old_estats->member + \
10591 get_stat64(&hw_stats->member)
10592
10593static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
10594{
10595 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10596 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10597
10598 ESTAT_ADD(rx_octets);
10599 ESTAT_ADD(rx_fragments);
10600 ESTAT_ADD(rx_ucast_packets);
10601 ESTAT_ADD(rx_mcast_packets);
10602 ESTAT_ADD(rx_bcast_packets);
10603 ESTAT_ADD(rx_fcs_errors);
10604 ESTAT_ADD(rx_align_errors);
10605 ESTAT_ADD(rx_xon_pause_rcvd);
10606 ESTAT_ADD(rx_xoff_pause_rcvd);
10607 ESTAT_ADD(rx_mac_ctrl_rcvd);
10608 ESTAT_ADD(rx_xoff_entered);
10609 ESTAT_ADD(rx_frame_too_long_errors);
10610 ESTAT_ADD(rx_jabbers);
10611 ESTAT_ADD(rx_undersize_packets);
10612 ESTAT_ADD(rx_in_length_errors);
10613 ESTAT_ADD(rx_out_length_errors);
10614 ESTAT_ADD(rx_64_or_less_octet_packets);
10615 ESTAT_ADD(rx_65_to_127_octet_packets);
10616 ESTAT_ADD(rx_128_to_255_octet_packets);
10617 ESTAT_ADD(rx_256_to_511_octet_packets);
10618 ESTAT_ADD(rx_512_to_1023_octet_packets);
10619 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10620 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10621 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10622 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10623 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10624
10625 ESTAT_ADD(tx_octets);
10626 ESTAT_ADD(tx_collisions);
10627 ESTAT_ADD(tx_xon_sent);
10628 ESTAT_ADD(tx_xoff_sent);
10629 ESTAT_ADD(tx_flow_control);
10630 ESTAT_ADD(tx_mac_errors);
10631 ESTAT_ADD(tx_single_collisions);
10632 ESTAT_ADD(tx_mult_collisions);
10633 ESTAT_ADD(tx_deferred);
10634 ESTAT_ADD(tx_excessive_collisions);
10635 ESTAT_ADD(tx_late_collisions);
10636 ESTAT_ADD(tx_collide_2times);
10637 ESTAT_ADD(tx_collide_3times);
10638 ESTAT_ADD(tx_collide_4times);
10639 ESTAT_ADD(tx_collide_5times);
10640 ESTAT_ADD(tx_collide_6times);
10641 ESTAT_ADD(tx_collide_7times);
10642 ESTAT_ADD(tx_collide_8times);
10643 ESTAT_ADD(tx_collide_9times);
10644 ESTAT_ADD(tx_collide_10times);
10645 ESTAT_ADD(tx_collide_11times);
10646 ESTAT_ADD(tx_collide_12times);
10647 ESTAT_ADD(tx_collide_13times);
10648 ESTAT_ADD(tx_collide_14times);
10649 ESTAT_ADD(tx_collide_15times);
10650 ESTAT_ADD(tx_ucast_packets);
10651 ESTAT_ADD(tx_mcast_packets);
10652 ESTAT_ADD(tx_bcast_packets);
10653 ESTAT_ADD(tx_carrier_sense_errors);
10654 ESTAT_ADD(tx_discards);
10655 ESTAT_ADD(tx_errors);
10656
10657 ESTAT_ADD(dma_writeq_full);
10658 ESTAT_ADD(dma_write_prioq_full);
10659 ESTAT_ADD(rxbds_empty);
10660 ESTAT_ADD(rx_discards);
10661 ESTAT_ADD(rx_errors);
10662 ESTAT_ADD(rx_threshold_hit);
10663
10664 ESTAT_ADD(dma_readq_full);
10665 ESTAT_ADD(dma_read_prioq_full);
10666 ESTAT_ADD(tx_comp_queue_full);
10667
10668 ESTAT_ADD(ring_set_send_prod_index);
10669 ESTAT_ADD(ring_status_update);
10670 ESTAT_ADD(nic_irqs);
10671 ESTAT_ADD(nic_avoided_irqs);
10672 ESTAT_ADD(nic_tx_threshold_hit);
10673
Matt Carlson4452d092011-05-19 12:12:51 +000010674 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010675}
10676
Matt Carlson65ec6982012-02-28 23:33:37 +000010677static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010678{
Eric Dumazet511d2222010-07-07 20:44:24 +000010679 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010680 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10681
Linus Torvalds1da177e2005-04-16 15:20:36 -070010682 stats->rx_packets = old_stats->rx_packets +
10683 get_stat64(&hw_stats->rx_ucast_packets) +
10684 get_stat64(&hw_stats->rx_mcast_packets) +
10685 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010686
Linus Torvalds1da177e2005-04-16 15:20:36 -070010687 stats->tx_packets = old_stats->tx_packets +
10688 get_stat64(&hw_stats->tx_ucast_packets) +
10689 get_stat64(&hw_stats->tx_mcast_packets) +
10690 get_stat64(&hw_stats->tx_bcast_packets);
10691
10692 stats->rx_bytes = old_stats->rx_bytes +
10693 get_stat64(&hw_stats->rx_octets);
10694 stats->tx_bytes = old_stats->tx_bytes +
10695 get_stat64(&hw_stats->tx_octets);
10696
10697 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010698 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010699 stats->tx_errors = old_stats->tx_errors +
10700 get_stat64(&hw_stats->tx_errors) +
10701 get_stat64(&hw_stats->tx_mac_errors) +
10702 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10703 get_stat64(&hw_stats->tx_discards);
10704
10705 stats->multicast = old_stats->multicast +
10706 get_stat64(&hw_stats->rx_mcast_packets);
10707 stats->collisions = old_stats->collisions +
10708 get_stat64(&hw_stats->tx_collisions);
10709
10710 stats->rx_length_errors = old_stats->rx_length_errors +
10711 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10712 get_stat64(&hw_stats->rx_undersize_packets);
10713
10714 stats->rx_over_errors = old_stats->rx_over_errors +
10715 get_stat64(&hw_stats->rxbds_empty);
10716 stats->rx_frame_errors = old_stats->rx_frame_errors +
10717 get_stat64(&hw_stats->rx_align_errors);
10718 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10719 get_stat64(&hw_stats->tx_discards);
10720 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10721 get_stat64(&hw_stats->tx_carrier_sense_errors);
10722
10723 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000010724 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010725
John W. Linville4f63b872005-09-12 14:43:18 -070010726 stats->rx_missed_errors = old_stats->rx_missed_errors +
10727 get_stat64(&hw_stats->rx_discards);
10728
Eric Dumazetb0057c52010-10-10 19:55:52 +000010729 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010730 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010731}
10732
Linus Torvalds1da177e2005-04-16 15:20:36 -070010733static int tg3_get_regs_len(struct net_device *dev)
10734{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010735 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010736}
10737
10738static void tg3_get_regs(struct net_device *dev,
10739 struct ethtool_regs *regs, void *_p)
10740{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010741 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010742
10743 regs->version = 0;
10744
Matt Carlson97bd8e42011-04-13 11:05:04 +000010745 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010746
Matt Carlson800960682010-08-02 11:26:06 +000010747 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010748 return;
10749
David S. Millerf47c11e2005-06-24 20:18:35 -070010750 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010751
Matt Carlson97bd8e42011-04-13 11:05:04 +000010752 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010753
David S. Millerf47c11e2005-06-24 20:18:35 -070010754 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010755}
10756
10757static int tg3_get_eeprom_len(struct net_device *dev)
10758{
10759 struct tg3 *tp = netdev_priv(dev);
10760
10761 return tp->nvram_size;
10762}
10763
Linus Torvalds1da177e2005-04-16 15:20:36 -070010764static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10765{
10766 struct tg3 *tp = netdev_priv(dev);
10767 int ret;
10768 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010769 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010770 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010771
Joe Perches63c3a662011-04-26 08:12:10 +000010772 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010773 return -EINVAL;
10774
Matt Carlson800960682010-08-02 11:26:06 +000010775 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010776 return -EAGAIN;
10777
Linus Torvalds1da177e2005-04-16 15:20:36 -070010778 offset = eeprom->offset;
10779 len = eeprom->len;
10780 eeprom->len = 0;
10781
10782 eeprom->magic = TG3_EEPROM_MAGIC;
10783
10784 if (offset & 3) {
10785 /* adjustments to start on required 4 byte boundary */
10786 b_offset = offset & 3;
10787 b_count = 4 - b_offset;
10788 if (b_count > len) {
10789 /* i.e. offset=1 len=2 */
10790 b_count = len;
10791 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010792 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010793 if (ret)
10794 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010795 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010796 len -= b_count;
10797 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010798 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010799 }
10800
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010801 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010802 pd = &data[eeprom->len];
10803 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010804 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010805 if (ret) {
10806 eeprom->len += i;
10807 return ret;
10808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010809 memcpy(pd + i, &val, 4);
10810 }
10811 eeprom->len += i;
10812
10813 if (len & 3) {
10814 /* read last bytes not ending on 4 byte boundary */
10815 pd = &data[eeprom->len];
10816 b_count = len & 3;
10817 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010818 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010819 if (ret)
10820 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010821 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010822 eeprom->len += b_count;
10823 }
10824 return 0;
10825}
10826
Linus Torvalds1da177e2005-04-16 15:20:36 -070010827static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10828{
10829 struct tg3 *tp = netdev_priv(dev);
10830 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010831 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010832 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010833 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010834
Matt Carlson800960682010-08-02 11:26:06 +000010835 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010836 return -EAGAIN;
10837
Joe Perches63c3a662011-04-26 08:12:10 +000010838 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010839 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010840 return -EINVAL;
10841
10842 offset = eeprom->offset;
10843 len = eeprom->len;
10844
10845 if ((b_offset = (offset & 3))) {
10846 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010847 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010848 if (ret)
10849 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010850 len += b_offset;
10851 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010852 if (len < 4)
10853 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010854 }
10855
10856 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010857 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010858 /* adjustments to end on required 4 byte boundary */
10859 odd_len = 1;
10860 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010861 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010862 if (ret)
10863 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010864 }
10865
10866 buf = data;
10867 if (b_offset || odd_len) {
10868 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010869 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010870 return -ENOMEM;
10871 if (b_offset)
10872 memcpy(buf, &start, 4);
10873 if (odd_len)
10874 memcpy(buf+len-4, &end, 4);
10875 memcpy(buf + b_offset, data, eeprom->len);
10876 }
10877
10878 ret = tg3_nvram_write_block(tp, offset, len, buf);
10879
10880 if (buf != data)
10881 kfree(buf);
10882
10883 return ret;
10884}
10885
10886static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10887{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010888 struct tg3 *tp = netdev_priv(dev);
10889
Joe Perches63c3a662011-04-26 08:12:10 +000010890 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010891 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010892 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010893 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010894 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10895 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010896 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010897
Linus Torvalds1da177e2005-04-16 15:20:36 -070010898 cmd->supported = (SUPPORTED_Autoneg);
10899
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010900 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010901 cmd->supported |= (SUPPORTED_1000baseT_Half |
10902 SUPPORTED_1000baseT_Full);
10903
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010904 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010905 cmd->supported |= (SUPPORTED_100baseT_Half |
10906 SUPPORTED_100baseT_Full |
10907 SUPPORTED_10baseT_Half |
10908 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010909 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010910 cmd->port = PORT_TP;
10911 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010912 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010913 cmd->port = PORT_FIBRE;
10914 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010915
Linus Torvalds1da177e2005-04-16 15:20:36 -070010916 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010917 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10918 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10919 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10920 cmd->advertising |= ADVERTISED_Pause;
10921 } else {
10922 cmd->advertising |= ADVERTISED_Pause |
10923 ADVERTISED_Asym_Pause;
10924 }
10925 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10926 cmd->advertising |= ADVERTISED_Asym_Pause;
10927 }
10928 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010929 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000010930 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010931 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010932 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010933 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10934 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10935 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10936 else
10937 cmd->eth_tp_mdix = ETH_TP_MDI;
10938 }
Matt Carlson64c22182010-10-14 10:37:44 +000010939 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000010940 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
10941 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010942 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010943 }
Matt Carlson882e9792009-09-01 13:21:36 +000010944 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010945 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010946 cmd->autoneg = tp->link_config.autoneg;
10947 cmd->maxtxpkt = 0;
10948 cmd->maxrxpkt = 0;
10949 return 0;
10950}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010951
Linus Torvalds1da177e2005-04-16 15:20:36 -070010952static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10953{
10954 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010955 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010956
Joe Perches63c3a662011-04-26 08:12:10 +000010957 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010958 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010959 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010960 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010961 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10962 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010963 }
10964
Matt Carlson7e5856b2009-02-25 14:23:01 +000010965 if (cmd->autoneg != AUTONEG_ENABLE &&
10966 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010967 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010968
10969 if (cmd->autoneg == AUTONEG_DISABLE &&
10970 cmd->duplex != DUPLEX_FULL &&
10971 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010972 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010973
Matt Carlson7e5856b2009-02-25 14:23:01 +000010974 if (cmd->autoneg == AUTONEG_ENABLE) {
10975 u32 mask = ADVERTISED_Autoneg |
10976 ADVERTISED_Pause |
10977 ADVERTISED_Asym_Pause;
10978
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010979 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010980 mask |= ADVERTISED_1000baseT_Half |
10981 ADVERTISED_1000baseT_Full;
10982
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010983 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010984 mask |= ADVERTISED_100baseT_Half |
10985 ADVERTISED_100baseT_Full |
10986 ADVERTISED_10baseT_Half |
10987 ADVERTISED_10baseT_Full |
10988 ADVERTISED_TP;
10989 else
10990 mask |= ADVERTISED_FIBRE;
10991
10992 if (cmd->advertising & ~mask)
10993 return -EINVAL;
10994
10995 mask &= (ADVERTISED_1000baseT_Half |
10996 ADVERTISED_1000baseT_Full |
10997 ADVERTISED_100baseT_Half |
10998 ADVERTISED_100baseT_Full |
10999 ADVERTISED_10baseT_Half |
11000 ADVERTISED_10baseT_Full);
11001
11002 cmd->advertising &= mask;
11003 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011004 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011005 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011006 return -EINVAL;
11007
11008 if (cmd->duplex != DUPLEX_FULL)
11009 return -EINVAL;
11010 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011011 if (speed != SPEED_100 &&
11012 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011013 return -EINVAL;
11014 }
11015 }
11016
David S. Millerf47c11e2005-06-24 20:18:35 -070011017 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011018
11019 tp->link_config.autoneg = cmd->autoneg;
11020 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011021 tp->link_config.advertising = (cmd->advertising |
11022 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011023 tp->link_config.speed = SPEED_UNKNOWN;
11024 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011025 } else {
11026 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011027 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011028 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011029 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011030
Linus Torvalds1da177e2005-04-16 15:20:36 -070011031 if (netif_running(dev))
11032 tg3_setup_phy(tp, 1);
11033
David S. Millerf47c11e2005-06-24 20:18:35 -070011034 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011035
Linus Torvalds1da177e2005-04-16 15:20:36 -070011036 return 0;
11037}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011038
Linus Torvalds1da177e2005-04-16 15:20:36 -070011039static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11040{
11041 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011042
Rick Jones68aad782011-11-07 13:29:27 +000011043 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11044 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11045 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11046 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011047}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011048
Linus Torvalds1da177e2005-04-16 15:20:36 -070011049static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11050{
11051 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011052
Joe Perches63c3a662011-04-26 08:12:10 +000011053 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011054 wol->supported = WAKE_MAGIC;
11055 else
11056 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011057 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011058 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011059 wol->wolopts = WAKE_MAGIC;
11060 memset(&wol->sopass, 0, sizeof(wol->sopass));
11061}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011062
Linus Torvalds1da177e2005-04-16 15:20:36 -070011063static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11064{
11065 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011066 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011067
Linus Torvalds1da177e2005-04-16 15:20:36 -070011068 if (wol->wolopts & ~WAKE_MAGIC)
11069 return -EINVAL;
11070 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011071 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011072 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011073
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011074 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11075
David S. Millerf47c11e2005-06-24 20:18:35 -070011076 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011077 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011078 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011079 else
Joe Perches63c3a662011-04-26 08:12:10 +000011080 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011081 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011082
Linus Torvalds1da177e2005-04-16 15:20:36 -070011083 return 0;
11084}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011085
Linus Torvalds1da177e2005-04-16 15:20:36 -070011086static u32 tg3_get_msglevel(struct net_device *dev)
11087{
11088 struct tg3 *tp = netdev_priv(dev);
11089 return tp->msg_enable;
11090}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011091
Linus Torvalds1da177e2005-04-16 15:20:36 -070011092static void tg3_set_msglevel(struct net_device *dev, u32 value)
11093{
11094 struct tg3 *tp = netdev_priv(dev);
11095 tp->msg_enable = value;
11096}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011097
Linus Torvalds1da177e2005-04-16 15:20:36 -070011098static int tg3_nway_reset(struct net_device *dev)
11099{
11100 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011101 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011102
Linus Torvalds1da177e2005-04-16 15:20:36 -070011103 if (!netif_running(dev))
11104 return -EAGAIN;
11105
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011106 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011107 return -EINVAL;
11108
Joe Perches63c3a662011-04-26 08:12:10 +000011109 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011110 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011111 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011112 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011113 } else {
11114 u32 bmcr;
11115
11116 spin_lock_bh(&tp->lock);
11117 r = -EINVAL;
11118 tg3_readphy(tp, MII_BMCR, &bmcr);
11119 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11120 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011121 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011122 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11123 BMCR_ANENABLE);
11124 r = 0;
11125 }
11126 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011127 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011128
Linus Torvalds1da177e2005-04-16 15:20:36 -070011129 return r;
11130}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011131
Linus Torvalds1da177e2005-04-16 15:20:36 -070011132static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11133{
11134 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011135
Matt Carlson2c49a442010-09-30 10:34:35 +000011136 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011137 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011138 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011139 else
11140 ering->rx_jumbo_max_pending = 0;
11141
11142 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011143
11144 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011145 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011146 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11147 else
11148 ering->rx_jumbo_pending = 0;
11149
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011150 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011151}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011152
Linus Torvalds1da177e2005-04-16 15:20:36 -070011153static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11154{
11155 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000011156 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011157
Matt Carlson2c49a442010-09-30 10:34:35 +000011158 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
11159 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070011160 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
11161 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011162 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070011163 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011164 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011165
Michael Chanbbe832c2005-06-24 20:20:04 -070011166 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011167 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011168 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011169 irq_sync = 1;
11170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011171
Michael Chanbbe832c2005-06-24 20:20:04 -070011172 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011173
Linus Torvalds1da177e2005-04-16 15:20:36 -070011174 tp->rx_pending = ering->rx_pending;
11175
Joe Perches63c3a662011-04-26 08:12:10 +000011176 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011177 tp->rx_pending > 63)
11178 tp->rx_pending = 63;
11179 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000011180
Matt Carlson6fd45cb2010-09-15 08:59:57 +000011181 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000011182 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011183
11184 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070011185 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070011186 err = tg3_restart_hw(tp, 1);
11187 if (!err)
11188 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011189 }
11190
David S. Millerf47c11e2005-06-24 20:18:35 -070011191 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011192
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011193 if (irq_sync && !err)
11194 tg3_phy_start(tp);
11195
Michael Chanb9ec6c12006-07-25 16:37:27 -070011196 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011197}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011198
Linus Torvalds1da177e2005-04-16 15:20:36 -070011199static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11200{
11201 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011202
Joe Perches63c3a662011-04-26 08:12:10 +000011203 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080011204
Matt Carlson4a2db502011-12-08 14:40:17 +000011205 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080011206 epause->rx_pause = 1;
11207 else
11208 epause->rx_pause = 0;
11209
Matt Carlson4a2db502011-12-08 14:40:17 +000011210 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080011211 epause->tx_pause = 1;
11212 else
11213 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011214}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011215
Linus Torvalds1da177e2005-04-16 15:20:36 -070011216static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11217{
11218 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011219 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011220
Joe Perches63c3a662011-04-26 08:12:10 +000011221 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000011222 u32 newadv;
11223 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011224
Matt Carlson27121682010-02-17 15:16:57 +000011225 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011226
Matt Carlson27121682010-02-17 15:16:57 +000011227 if (!(phydev->supported & SUPPORTED_Pause) ||
11228 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000011229 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000011230 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011231
Matt Carlson27121682010-02-17 15:16:57 +000011232 tp->link_config.flowctrl = 0;
11233 if (epause->rx_pause) {
11234 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011235
Matt Carlson27121682010-02-17 15:16:57 +000011236 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080011237 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000011238 newadv = ADVERTISED_Pause;
11239 } else
11240 newadv = ADVERTISED_Pause |
11241 ADVERTISED_Asym_Pause;
11242 } else if (epause->tx_pause) {
11243 tp->link_config.flowctrl |= FLOW_CTRL_TX;
11244 newadv = ADVERTISED_Asym_Pause;
11245 } else
11246 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011247
Matt Carlson27121682010-02-17 15:16:57 +000011248 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011249 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011250 else
Joe Perches63c3a662011-04-26 08:12:10 +000011251 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011252
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011253 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000011254 u32 oldadv = phydev->advertising &
11255 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
11256 if (oldadv != newadv) {
11257 phydev->advertising &=
11258 ~(ADVERTISED_Pause |
11259 ADVERTISED_Asym_Pause);
11260 phydev->advertising |= newadv;
11261 if (phydev->autoneg) {
11262 /*
11263 * Always renegotiate the link to
11264 * inform our link partner of our
11265 * flow control settings, even if the
11266 * flow control is forced. Let
11267 * tg3_adjust_link() do the final
11268 * flow control setup.
11269 */
11270 return phy_start_aneg(phydev);
11271 }
11272 }
11273
11274 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011275 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000011276 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011277 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000011278 ~(ADVERTISED_Pause |
11279 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011280 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011281 }
11282 } else {
11283 int irq_sync = 0;
11284
11285 if (netif_running(dev)) {
11286 tg3_netif_stop(tp);
11287 irq_sync = 1;
11288 }
11289
11290 tg3_full_lock(tp, irq_sync);
11291
11292 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011293 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011294 else
Joe Perches63c3a662011-04-26 08:12:10 +000011295 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011296 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011297 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011298 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011299 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011300 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011301 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011302 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011303 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011304
11305 if (netif_running(dev)) {
11306 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11307 err = tg3_restart_hw(tp, 1);
11308 if (!err)
11309 tg3_netif_start(tp);
11310 }
11311
11312 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011314
Michael Chanb9ec6c12006-07-25 16:37:27 -070011315 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011316}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011317
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011318static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011319{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011320 switch (sset) {
11321 case ETH_SS_TEST:
11322 return TG3_NUM_TEST;
11323 case ETH_SS_STATS:
11324 return TG3_NUM_STATS;
11325 default:
11326 return -EOPNOTSUPP;
11327 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070011328}
11329
Matt Carlson90415472011-12-16 13:33:23 +000011330static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
11331 u32 *rules __always_unused)
11332{
11333 struct tg3 *tp = netdev_priv(dev);
11334
11335 if (!tg3_flag(tp, SUPPORT_MSIX))
11336 return -EOPNOTSUPP;
11337
11338 switch (info->cmd) {
11339 case ETHTOOL_GRXRINGS:
11340 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000011341 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000011342 else {
11343 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000011344 if (info->data > TG3_RSS_MAX_NUM_QS)
11345 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000011346 }
11347
11348 /* The first interrupt vector only
11349 * handles link interrupts.
11350 */
11351 info->data -= 1;
11352 return 0;
11353
11354 default:
11355 return -EOPNOTSUPP;
11356 }
11357}
11358
11359static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
11360{
11361 u32 size = 0;
11362 struct tg3 *tp = netdev_priv(dev);
11363
11364 if (tg3_flag(tp, SUPPORT_MSIX))
11365 size = TG3_RSS_INDIR_TBL_SIZE;
11366
11367 return size;
11368}
11369
11370static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
11371{
11372 struct tg3 *tp = netdev_priv(dev);
11373 int i;
11374
11375 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11376 indir[i] = tp->rss_ind_tbl[i];
11377
11378 return 0;
11379}
11380
11381static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11382{
11383 struct tg3 *tp = netdev_priv(dev);
11384 size_t i;
11385
11386 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11387 tp->rss_ind_tbl[i] = indir[i];
11388
11389 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11390 return 0;
11391
11392 /* It is legal to write the indirection
11393 * table while the device is running.
11394 */
11395 tg3_full_lock(tp, 0);
11396 tg3_rss_write_indir_tbl(tp);
11397 tg3_full_unlock(tp);
11398
11399 return 0;
11400}
11401
Michael Chan09681692012-09-28 07:12:42 +000011402static void tg3_get_channels(struct net_device *dev,
11403 struct ethtool_channels *channel)
11404{
11405 struct tg3 *tp = netdev_priv(dev);
11406 u32 deflt_qs = netif_get_num_default_rss_queues();
11407
11408 channel->max_rx = tp->rxq_max;
11409 channel->max_tx = tp->txq_max;
11410
11411 if (netif_running(dev)) {
11412 channel->rx_count = tp->rxq_cnt;
11413 channel->tx_count = tp->txq_cnt;
11414 } else {
11415 if (tp->rxq_req)
11416 channel->rx_count = tp->rxq_req;
11417 else
11418 channel->rx_count = min(deflt_qs, tp->rxq_max);
11419
11420 if (tp->txq_req)
11421 channel->tx_count = tp->txq_req;
11422 else
11423 channel->tx_count = min(deflt_qs, tp->txq_max);
11424 }
11425}
11426
11427static int tg3_set_channels(struct net_device *dev,
11428 struct ethtool_channels *channel)
11429{
11430 struct tg3 *tp = netdev_priv(dev);
11431
11432 if (!tg3_flag(tp, SUPPORT_MSIX))
11433 return -EOPNOTSUPP;
11434
11435 if (channel->rx_count > tp->rxq_max ||
11436 channel->tx_count > tp->txq_max)
11437 return -EINVAL;
11438
11439 tp->rxq_req = channel->rx_count;
11440 tp->txq_req = channel->tx_count;
11441
11442 if (!netif_running(dev))
11443 return 0;
11444
11445 tg3_stop(tp);
11446
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011447 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000011448
11449 tg3_start(tp, true, false);
11450
11451 return 0;
11452}
11453
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011454static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011455{
11456 switch (stringset) {
11457 case ETH_SS_STATS:
11458 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11459 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011460 case ETH_SS_TEST:
11461 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011463 default:
11464 WARN_ON(1); /* we need a WARN() */
11465 break;
11466 }
11467}
11468
stephen hemminger81b87092011-04-04 08:43:50 +000011469static int tg3_set_phys_id(struct net_device *dev,
11470 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011471{
11472 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011473
11474 if (!netif_running(tp->dev))
11475 return -EAGAIN;
11476
stephen hemminger81b87092011-04-04 08:43:50 +000011477 switch (state) {
11478 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011479 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011480
stephen hemminger81b87092011-04-04 08:43:50 +000011481 case ETHTOOL_ID_ON:
11482 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11483 LED_CTRL_1000MBPS_ON |
11484 LED_CTRL_100MBPS_ON |
11485 LED_CTRL_10MBPS_ON |
11486 LED_CTRL_TRAFFIC_OVERRIDE |
11487 LED_CTRL_TRAFFIC_BLINK |
11488 LED_CTRL_TRAFFIC_LED);
11489 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011490
stephen hemminger81b87092011-04-04 08:43:50 +000011491 case ETHTOOL_ID_OFF:
11492 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11493 LED_CTRL_TRAFFIC_OVERRIDE);
11494 break;
Michael Chan4009a932005-09-05 17:52:54 -070011495
stephen hemminger81b87092011-04-04 08:43:50 +000011496 case ETHTOOL_ID_INACTIVE:
11497 tw32(MAC_LED_CTRL, tp->led_ctrl);
11498 break;
Michael Chan4009a932005-09-05 17:52:54 -070011499 }
stephen hemminger81b87092011-04-04 08:43:50 +000011500
Michael Chan4009a932005-09-05 17:52:54 -070011501 return 0;
11502}
11503
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011504static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011505 struct ethtool_stats *estats, u64 *tmp_stats)
11506{
11507 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011508
Matt Carlsonb546e462012-02-13 15:20:09 +000011509 if (tp->hw_stats)
11510 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11511 else
11512 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011513}
11514
Matt Carlson535a4902011-07-20 10:20:56 +000011515static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011516{
11517 int i;
11518 __be32 *buf;
11519 u32 offset = 0, len = 0;
11520 u32 magic, val;
11521
Joe Perches63c3a662011-04-26 08:12:10 +000011522 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011523 return NULL;
11524
11525 if (magic == TG3_EEPROM_MAGIC) {
11526 for (offset = TG3_NVM_DIR_START;
11527 offset < TG3_NVM_DIR_END;
11528 offset += TG3_NVM_DIRENT_SIZE) {
11529 if (tg3_nvram_read(tp, offset, &val))
11530 return NULL;
11531
11532 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11533 TG3_NVM_DIRTYPE_EXTVPD)
11534 break;
11535 }
11536
11537 if (offset != TG3_NVM_DIR_END) {
11538 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11539 if (tg3_nvram_read(tp, offset + 4, &offset))
11540 return NULL;
11541
11542 offset = tg3_nvram_logical_addr(tp, offset);
11543 }
11544 }
11545
11546 if (!offset || !len) {
11547 offset = TG3_NVM_VPD_OFF;
11548 len = TG3_NVM_VPD_LEN;
11549 }
11550
11551 buf = kmalloc(len, GFP_KERNEL);
11552 if (buf == NULL)
11553 return NULL;
11554
11555 if (magic == TG3_EEPROM_MAGIC) {
11556 for (i = 0; i < len; i += 4) {
11557 /* The data is in little-endian format in NVRAM.
11558 * Use the big-endian read routines to preserve
11559 * the byte order as it exists in NVRAM.
11560 */
11561 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11562 goto error;
11563 }
11564 } else {
11565 u8 *ptr;
11566 ssize_t cnt;
11567 unsigned int pos = 0;
11568
11569 ptr = (u8 *)&buf[0];
11570 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11571 cnt = pci_read_vpd(tp->pdev, pos,
11572 len - pos, ptr);
11573 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11574 cnt = 0;
11575 else if (cnt < 0)
11576 goto error;
11577 }
11578 if (pos != len)
11579 goto error;
11580 }
11581
Matt Carlson535a4902011-07-20 10:20:56 +000011582 *vpdlen = len;
11583
Matt Carlsonc3e94502011-04-13 11:05:08 +000011584 return buf;
11585
11586error:
11587 kfree(buf);
11588 return NULL;
11589}
11590
Michael Chan566f86a2005-05-29 14:56:58 -070011591#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011592#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11593#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11594#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011595#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11596#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011597#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011598#define NVRAM_SELFBOOT_HW_SIZE 0x20
11599#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011600
11601static int tg3_test_nvram(struct tg3 *tp)
11602{
Matt Carlson535a4902011-07-20 10:20:56 +000011603 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011604 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011605 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011606
Joe Perches63c3a662011-04-26 08:12:10 +000011607 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011608 return 0;
11609
Matt Carlsone4f34112009-02-25 14:25:00 +000011610 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011611 return -EIO;
11612
Michael Chan1b277772006-03-20 22:27:48 -080011613 if (magic == TG3_EEPROM_MAGIC)
11614 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011615 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011616 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11617 TG3_EEPROM_SB_FORMAT_1) {
11618 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11619 case TG3_EEPROM_SB_REVISION_0:
11620 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11621 break;
11622 case TG3_EEPROM_SB_REVISION_2:
11623 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11624 break;
11625 case TG3_EEPROM_SB_REVISION_3:
11626 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11627 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011628 case TG3_EEPROM_SB_REVISION_4:
11629 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11630 break;
11631 case TG3_EEPROM_SB_REVISION_5:
11632 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11633 break;
11634 case TG3_EEPROM_SB_REVISION_6:
11635 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11636 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011637 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011638 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011639 }
11640 } else
Michael Chan1b277772006-03-20 22:27:48 -080011641 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011642 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11643 size = NVRAM_SELFBOOT_HW_SIZE;
11644 else
Michael Chan1b277772006-03-20 22:27:48 -080011645 return -EIO;
11646
11647 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011648 if (buf == NULL)
11649 return -ENOMEM;
11650
Michael Chan1b277772006-03-20 22:27:48 -080011651 err = -EIO;
11652 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011653 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11654 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011655 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011656 }
Michael Chan1b277772006-03-20 22:27:48 -080011657 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011658 goto out;
11659
Michael Chan1b277772006-03-20 22:27:48 -080011660 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011661 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011662 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011663 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011664 u8 *buf8 = (u8 *) buf, csum8 = 0;
11665
Al Virob9fc7dc2007-12-17 22:59:57 -080011666 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011667 TG3_EEPROM_SB_REVISION_2) {
11668 /* For rev 2, the csum doesn't include the MBA. */
11669 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11670 csum8 += buf8[i];
11671 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11672 csum8 += buf8[i];
11673 } else {
11674 for (i = 0; i < size; i++)
11675 csum8 += buf8[i];
11676 }
Michael Chan1b277772006-03-20 22:27:48 -080011677
Adrian Bunkad96b482006-04-05 22:21:04 -070011678 if (csum8 == 0) {
11679 err = 0;
11680 goto out;
11681 }
11682
11683 err = -EIO;
11684 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011685 }
Michael Chan566f86a2005-05-29 14:56:58 -070011686
Al Virob9fc7dc2007-12-17 22:59:57 -080011687 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011688 TG3_EEPROM_MAGIC_HW) {
11689 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011690 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011691 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011692
11693 /* Separate the parity bits and the data bytes. */
11694 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11695 if ((i == 0) || (i == 8)) {
11696 int l;
11697 u8 msk;
11698
11699 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11700 parity[k++] = buf8[i] & msk;
11701 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011702 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011703 int l;
11704 u8 msk;
11705
11706 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11707 parity[k++] = buf8[i] & msk;
11708 i++;
11709
11710 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11711 parity[k++] = buf8[i] & msk;
11712 i++;
11713 }
11714 data[j++] = buf8[i];
11715 }
11716
11717 err = -EIO;
11718 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11719 u8 hw8 = hweight8(data[i]);
11720
11721 if ((hw8 & 0x1) && parity[i])
11722 goto out;
11723 else if (!(hw8 & 0x1) && !parity[i])
11724 goto out;
11725 }
11726 err = 0;
11727 goto out;
11728 }
11729
Matt Carlson01c3a392011-03-09 16:58:20 +000011730 err = -EIO;
11731
Michael Chan566f86a2005-05-29 14:56:58 -070011732 /* Bootstrap checksum at offset 0x10 */
11733 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011734 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011735 goto out;
11736
11737 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11738 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011739 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011740 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011741
Matt Carlsonc3e94502011-04-13 11:05:08 +000011742 kfree(buf);
11743
Matt Carlson535a4902011-07-20 10:20:56 +000011744 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011745 if (!buf)
11746 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011747
Matt Carlson535a4902011-07-20 10:20:56 +000011748 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011749 if (i > 0) {
11750 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11751 if (j < 0)
11752 goto out;
11753
Matt Carlson535a4902011-07-20 10:20:56 +000011754 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011755 goto out;
11756
11757 i += PCI_VPD_LRDT_TAG_SIZE;
11758 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11759 PCI_VPD_RO_KEYWORD_CHKSUM);
11760 if (j > 0) {
11761 u8 csum8 = 0;
11762
11763 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11764
11765 for (i = 0; i <= j; i++)
11766 csum8 += ((u8 *)buf)[i];
11767
11768 if (csum8)
11769 goto out;
11770 }
11771 }
11772
Michael Chan566f86a2005-05-29 14:56:58 -070011773 err = 0;
11774
11775out:
11776 kfree(buf);
11777 return err;
11778}
11779
Michael Chanca430072005-05-29 14:57:23 -070011780#define TG3_SERDES_TIMEOUT_SEC 2
11781#define TG3_COPPER_TIMEOUT_SEC 6
11782
11783static int tg3_test_link(struct tg3 *tp)
11784{
11785 int i, max;
11786
11787 if (!netif_running(tp->dev))
11788 return -ENODEV;
11789
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011790 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011791 max = TG3_SERDES_TIMEOUT_SEC;
11792 else
11793 max = TG3_COPPER_TIMEOUT_SEC;
11794
11795 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011796 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070011797 return 0;
11798
11799 if (msleep_interruptible(1000))
11800 break;
11801 }
11802
11803 return -EIO;
11804}
11805
Michael Chana71116d2005-05-29 14:58:11 -070011806/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011807static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011808{
Michael Chanb16250e2006-09-27 16:10:14 -070011809 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011810 u32 offset, read_mask, write_mask, val, save_val, read_val;
11811 static struct {
11812 u16 offset;
11813 u16 flags;
11814#define TG3_FL_5705 0x1
11815#define TG3_FL_NOT_5705 0x2
11816#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011817#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011818 u32 read_mask;
11819 u32 write_mask;
11820 } reg_tbl[] = {
11821 /* MAC Control Registers */
11822 { MAC_MODE, TG3_FL_NOT_5705,
11823 0x00000000, 0x00ef6f8c },
11824 { MAC_MODE, TG3_FL_5705,
11825 0x00000000, 0x01ef6b8c },
11826 { MAC_STATUS, TG3_FL_NOT_5705,
11827 0x03800107, 0x00000000 },
11828 { MAC_STATUS, TG3_FL_5705,
11829 0x03800100, 0x00000000 },
11830 { MAC_ADDR_0_HIGH, 0x0000,
11831 0x00000000, 0x0000ffff },
11832 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011833 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011834 { MAC_RX_MTU_SIZE, 0x0000,
11835 0x00000000, 0x0000ffff },
11836 { MAC_TX_MODE, 0x0000,
11837 0x00000000, 0x00000070 },
11838 { MAC_TX_LENGTHS, 0x0000,
11839 0x00000000, 0x00003fff },
11840 { MAC_RX_MODE, TG3_FL_NOT_5705,
11841 0x00000000, 0x000007fc },
11842 { MAC_RX_MODE, TG3_FL_5705,
11843 0x00000000, 0x000007dc },
11844 { MAC_HASH_REG_0, 0x0000,
11845 0x00000000, 0xffffffff },
11846 { MAC_HASH_REG_1, 0x0000,
11847 0x00000000, 0xffffffff },
11848 { MAC_HASH_REG_2, 0x0000,
11849 0x00000000, 0xffffffff },
11850 { MAC_HASH_REG_3, 0x0000,
11851 0x00000000, 0xffffffff },
11852
11853 /* Receive Data and Receive BD Initiator Control Registers. */
11854 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11855 0x00000000, 0xffffffff },
11856 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11857 0x00000000, 0xffffffff },
11858 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11859 0x00000000, 0x00000003 },
11860 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11861 0x00000000, 0xffffffff },
11862 { RCVDBDI_STD_BD+0, 0x0000,
11863 0x00000000, 0xffffffff },
11864 { RCVDBDI_STD_BD+4, 0x0000,
11865 0x00000000, 0xffffffff },
11866 { RCVDBDI_STD_BD+8, 0x0000,
11867 0x00000000, 0xffff0002 },
11868 { RCVDBDI_STD_BD+0xc, 0x0000,
11869 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011870
Michael Chana71116d2005-05-29 14:58:11 -070011871 /* Receive BD Initiator Control Registers. */
11872 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11873 0x00000000, 0xffffffff },
11874 { RCVBDI_STD_THRESH, TG3_FL_5705,
11875 0x00000000, 0x000003ff },
11876 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11877 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011878
Michael Chana71116d2005-05-29 14:58:11 -070011879 /* Host Coalescing Control Registers. */
11880 { HOSTCC_MODE, TG3_FL_NOT_5705,
11881 0x00000000, 0x00000004 },
11882 { HOSTCC_MODE, TG3_FL_5705,
11883 0x00000000, 0x000000f6 },
11884 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11885 0x00000000, 0xffffffff },
11886 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11887 0x00000000, 0x000003ff },
11888 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11889 0x00000000, 0xffffffff },
11890 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11891 0x00000000, 0x000003ff },
11892 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11893 0x00000000, 0xffffffff },
11894 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11895 0x00000000, 0x000000ff },
11896 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11897 0x00000000, 0xffffffff },
11898 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11899 0x00000000, 0x000000ff },
11900 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11901 0x00000000, 0xffffffff },
11902 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11903 0x00000000, 0xffffffff },
11904 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11905 0x00000000, 0xffffffff },
11906 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11907 0x00000000, 0x000000ff },
11908 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11909 0x00000000, 0xffffffff },
11910 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11911 0x00000000, 0x000000ff },
11912 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11913 0x00000000, 0xffffffff },
11914 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11915 0x00000000, 0xffffffff },
11916 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11917 0x00000000, 0xffffffff },
11918 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11919 0x00000000, 0xffffffff },
11920 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11921 0x00000000, 0xffffffff },
11922 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11923 0xffffffff, 0x00000000 },
11924 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11925 0xffffffff, 0x00000000 },
11926
11927 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011928 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011929 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011930 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011931 0x00000000, 0x007fffff },
11932 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11933 0x00000000, 0x0000003f },
11934 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11935 0x00000000, 0x000001ff },
11936 { BUFMGR_MB_HIGH_WATER, 0x0000,
11937 0x00000000, 0x000001ff },
11938 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11939 0xffffffff, 0x00000000 },
11940 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11941 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011942
Michael Chana71116d2005-05-29 14:58:11 -070011943 /* Mailbox Registers */
11944 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11945 0x00000000, 0x000001ff },
11946 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11947 0x00000000, 0x000001ff },
11948 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11949 0x00000000, 0x000007ff },
11950 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11951 0x00000000, 0x000001ff },
11952
11953 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11954 };
11955
Michael Chanb16250e2006-09-27 16:10:14 -070011956 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011957 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011958 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011959 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011960 is_5750 = 1;
11961 }
Michael Chana71116d2005-05-29 14:58:11 -070011962
11963 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11964 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11965 continue;
11966
11967 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11968 continue;
11969
Joe Perches63c3a662011-04-26 08:12:10 +000011970 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011971 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11972 continue;
11973
Michael Chanb16250e2006-09-27 16:10:14 -070011974 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11975 continue;
11976
Michael Chana71116d2005-05-29 14:58:11 -070011977 offset = (u32) reg_tbl[i].offset;
11978 read_mask = reg_tbl[i].read_mask;
11979 write_mask = reg_tbl[i].write_mask;
11980
11981 /* Save the original register content */
11982 save_val = tr32(offset);
11983
11984 /* Determine the read-only value. */
11985 read_val = save_val & read_mask;
11986
11987 /* Write zero to the register, then make sure the read-only bits
11988 * are not changed and the read/write bits are all zeros.
11989 */
11990 tw32(offset, 0);
11991
11992 val = tr32(offset);
11993
11994 /* Test the read-only and read/write bits. */
11995 if (((val & read_mask) != read_val) || (val & write_mask))
11996 goto out;
11997
11998 /* Write ones to all the bits defined by RdMask and WrMask, then
11999 * make sure the read-only bits are not changed and the
12000 * read/write bits are all ones.
12001 */
12002 tw32(offset, read_mask | write_mask);
12003
12004 val = tr32(offset);
12005
12006 /* Test the read-only bits. */
12007 if ((val & read_mask) != read_val)
12008 goto out;
12009
12010 /* Test the read/write bits. */
12011 if ((val & write_mask) != write_mask)
12012 goto out;
12013
12014 tw32(offset, save_val);
12015 }
12016
12017 return 0;
12018
12019out:
Michael Chan9f88f292006-12-07 00:22:54 -080012020 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012021 netdev_err(tp->dev,
12022 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012023 tw32(offset, save_val);
12024 return -EIO;
12025}
12026
Michael Chan7942e1d2005-05-29 14:58:36 -070012027static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12028{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012029 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012030 int i;
12031 u32 j;
12032
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012033 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012034 for (j = 0; j < len; j += 4) {
12035 u32 val;
12036
12037 tg3_write_mem(tp, offset + j, test_pattern[i]);
12038 tg3_read_mem(tp, offset + j, &val);
12039 if (val != test_pattern[i])
12040 return -EIO;
12041 }
12042 }
12043 return 0;
12044}
12045
12046static int tg3_test_memory(struct tg3 *tp)
12047{
12048 static struct mem_entry {
12049 u32 offset;
12050 u32 len;
12051 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012052 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012053 { 0x00002000, 0x1c000},
12054 { 0xffffffff, 0x00000}
12055 }, mem_tbl_5705[] = {
12056 { 0x00000100, 0x0000c},
12057 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012058 { 0x00004000, 0x00800},
12059 { 0x00006000, 0x01000},
12060 { 0x00008000, 0x02000},
12061 { 0x00010000, 0x0e000},
12062 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012063 }, mem_tbl_5755[] = {
12064 { 0x00000200, 0x00008},
12065 { 0x00004000, 0x00800},
12066 { 0x00006000, 0x00800},
12067 { 0x00008000, 0x02000},
12068 { 0x00010000, 0x0c000},
12069 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012070 }, mem_tbl_5906[] = {
12071 { 0x00000200, 0x00008},
12072 { 0x00004000, 0x00400},
12073 { 0x00006000, 0x00400},
12074 { 0x00008000, 0x01000},
12075 { 0x00010000, 0x01000},
12076 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012077 }, mem_tbl_5717[] = {
12078 { 0x00000200, 0x00008},
12079 { 0x00010000, 0x0a000},
12080 { 0x00020000, 0x13c00},
12081 { 0xffffffff, 0x00000}
12082 }, mem_tbl_57765[] = {
12083 { 0x00000200, 0x00008},
12084 { 0x00004000, 0x00800},
12085 { 0x00006000, 0x09800},
12086 { 0x00010000, 0x0a000},
12087 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012088 };
12089 struct mem_entry *mem_tbl;
12090 int err = 0;
12091 int i;
12092
Joe Perches63c3a662011-04-26 08:12:10 +000012093 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012094 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000012095 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012096 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012097 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012098 mem_tbl = mem_tbl_5755;
12099 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12100 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012101 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012102 mem_tbl = mem_tbl_5705;
12103 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012104 mem_tbl = mem_tbl_570x;
12105
12106 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012107 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12108 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012109 break;
12110 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012111
Michael Chan7942e1d2005-05-29 14:58:36 -070012112 return err;
12113}
12114
Matt Carlsonbb158d62011-04-25 12:42:47 +000012115#define TG3_TSO_MSS 500
12116
12117#define TG3_TSO_IP_HDR_LEN 20
12118#define TG3_TSO_TCP_HDR_LEN 20
12119#define TG3_TSO_TCP_OPT_LEN 12
12120
12121static const u8 tg3_tso_header[] = {
121220x08, 0x00,
121230x45, 0x00, 0x00, 0x00,
121240x00, 0x00, 0x40, 0x00,
121250x40, 0x06, 0x00, 0x00,
121260x0a, 0x00, 0x00, 0x01,
121270x0a, 0x00, 0x00, 0x02,
121280x0d, 0x00, 0xe0, 0x00,
121290x00, 0x00, 0x01, 0x00,
121300x00, 0x00, 0x02, 0x00,
121310x80, 0x10, 0x10, 0x00,
121320x14, 0x09, 0x00, 0x00,
121330x01, 0x01, 0x08, 0x0a,
121340x11, 0x11, 0x11, 0x11,
121350x11, 0x11, 0x11, 0x11,
12136};
Michael Chan9f40dea2005-09-05 17:53:06 -070012137
Matt Carlson28a45952011-08-19 13:58:22 +000012138static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012139{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012140 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012141 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012142 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012143 struct sk_buff *skb;
12144 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012145 dma_addr_t map;
12146 int num_pkts, tx_len, rx_len, i, err;
12147 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012148 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000012149 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070012150
Matt Carlsonc8873402010-02-12 14:47:11 +000012151 tnapi = &tp->napi[0];
12152 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012153 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000012154 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000012155 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000012156 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000012157 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012158 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012159 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000012160
Michael Chanc76949a2005-05-29 14:58:59 -070012161 err = -EIO;
12162
Matt Carlson4852a862011-04-13 11:05:07 +000012163 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070012164 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070012165 if (!skb)
12166 return -ENOMEM;
12167
Michael Chanc76949a2005-05-29 14:58:59 -070012168 tx_data = skb_put(skb, tx_len);
12169 memcpy(tx_data, tp->dev->dev_addr, 6);
12170 memset(tx_data + 6, 0x0, 8);
12171
Matt Carlson4852a862011-04-13 11:05:07 +000012172 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070012173
Matt Carlson28a45952011-08-19 13:58:22 +000012174 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012175 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
12176
12177 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
12178 TG3_TSO_TCP_OPT_LEN;
12179
12180 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
12181 sizeof(tg3_tso_header));
12182 mss = TG3_TSO_MSS;
12183
12184 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
12185 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
12186
12187 /* Set the total length field in the IP header */
12188 iph->tot_len = htons((u16)(mss + hdr_len));
12189
12190 base_flags = (TXD_FLAG_CPU_PRE_DMA |
12191 TXD_FLAG_CPU_POST_DMA);
12192
Joe Perches63c3a662011-04-26 08:12:10 +000012193 if (tg3_flag(tp, HW_TSO_1) ||
12194 tg3_flag(tp, HW_TSO_2) ||
12195 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012196 struct tcphdr *th;
12197 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
12198 th = (struct tcphdr *)&tx_data[val];
12199 th->check = 0;
12200 } else
12201 base_flags |= TXD_FLAG_TCPUDP_CSUM;
12202
Joe Perches63c3a662011-04-26 08:12:10 +000012203 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012204 mss |= (hdr_len & 0xc) << 12;
12205 if (hdr_len & 0x10)
12206 base_flags |= 0x00000010;
12207 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000012208 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012209 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000012210 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000012211 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
12212 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
12213 } else {
12214 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
12215 }
12216
12217 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
12218 } else {
12219 num_pkts = 1;
12220 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000012221
12222 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
12223 tx_len > VLAN_ETH_FRAME_LEN)
12224 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012225 }
12226
12227 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070012228 tx_data[i] = (u8) (i & 0xff);
12229
Alexander Duyckf4188d82009-12-02 16:48:38 +000012230 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
12231 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000012232 dev_kfree_skb(skb);
12233 return -EIO;
12234 }
Michael Chanc76949a2005-05-29 14:58:59 -070012235
Matt Carlson0d681b22011-07-27 14:20:49 +000012236 val = tnapi->tx_prod;
12237 tnapi->tx_buffers[val].skb = skb;
12238 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
12239
Michael Chanc76949a2005-05-29 14:58:59 -070012240 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012241 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012242
12243 udelay(10);
12244
Matt Carlson898a56f2009-08-28 14:02:40 +000012245 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070012246
Matt Carlson84b67b22011-07-27 14:20:52 +000012247 budget = tg3_tx_avail(tnapi);
12248 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000012249 base_flags | TXD_FLAG_END, mss, 0)) {
12250 tnapi->tx_buffers[val].skb = NULL;
12251 dev_kfree_skb(skb);
12252 return -EIO;
12253 }
Michael Chanc76949a2005-05-29 14:58:59 -070012254
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012255 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070012256
Michael Chan6541b802012-03-04 14:48:14 +000012257 /* Sync BD data before updating mailbox */
12258 wmb();
12259
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012260 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
12261 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070012262
12263 udelay(10);
12264
Matt Carlson303fc922009-11-02 14:27:34 +000012265 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
12266 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070012267 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012268 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012269
12270 udelay(10);
12271
Matt Carlson898a56f2009-08-28 14:02:40 +000012272 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
12273 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012274 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070012275 (rx_idx == (rx_start_idx + num_pkts)))
12276 break;
12277 }
12278
Matt Carlsonba1142e2011-11-04 09:15:00 +000012279 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070012280 dev_kfree_skb(skb);
12281
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012282 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070012283 goto out;
12284
12285 if (rx_idx != rx_start_idx + num_pkts)
12286 goto out;
12287
Matt Carlsonbb158d62011-04-25 12:42:47 +000012288 val = data_off;
12289 while (rx_idx != rx_start_idx) {
12290 desc = &rnapi->rx_rcb[rx_start_idx++];
12291 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
12292 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070012293
Matt Carlsonbb158d62011-04-25 12:42:47 +000012294 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
12295 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000012296 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070012297
Matt Carlsonbb158d62011-04-25 12:42:47 +000012298 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
12299 - ETH_FCS_LEN;
12300
Matt Carlson28a45952011-08-19 13:58:22 +000012301 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012302 if (rx_len != tx_len)
12303 goto out;
12304
12305 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
12306 if (opaque_key != RXD_OPAQUE_RING_STD)
12307 goto out;
12308 } else {
12309 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
12310 goto out;
12311 }
12312 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
12313 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000012314 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012315 goto out;
12316 }
12317
12318 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012319 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012320 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
12321 mapping);
12322 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012323 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012324 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
12325 mapping);
12326 } else
Matt Carlson4852a862011-04-13 11:05:07 +000012327 goto out;
12328
Matt Carlsonbb158d62011-04-25 12:42:47 +000012329 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
12330 PCI_DMA_FROMDEVICE);
12331
Eric Dumazet9205fd92011-11-18 06:47:01 +000012332 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000012333 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012334 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012335 goto out;
12336 }
Matt Carlson4852a862011-04-13 11:05:07 +000012337 }
12338
Michael Chanc76949a2005-05-29 14:58:59 -070012339 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012340
Eric Dumazet9205fd92011-11-18 06:47:01 +000012341 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070012342out:
12343 return err;
12344}
12345
Matt Carlson00c266b2011-04-25 12:42:46 +000012346#define TG3_STD_LOOPBACK_FAILED 1
12347#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000012348#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000012349#define TG3_LOOPBACK_FAILED \
12350 (TG3_STD_LOOPBACK_FAILED | \
12351 TG3_JMB_LOOPBACK_FAILED | \
12352 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000012353
Matt Carlson941ec902011-08-19 13:58:23 +000012354static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070012355{
Matt Carlson28a45952011-08-19 13:58:22 +000012356 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000012357 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000012358 u32 jmb_pkt_sz = 9000;
12359
12360 if (tp->dma_limit)
12361 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070012362
Matt Carlsonab789042011-01-25 15:58:54 +000012363 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
12364 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
12365
Matt Carlson28a45952011-08-19 13:58:22 +000012366 if (!netif_running(tp->dev)) {
12367 data[0] = TG3_LOOPBACK_FAILED;
12368 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012369 if (do_extlpbk)
12370 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000012371 goto done;
12372 }
12373
Michael Chanb9ec6c12006-07-25 16:37:27 -070012374 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000012375 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000012376 data[0] = TG3_LOOPBACK_FAILED;
12377 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012378 if (do_extlpbk)
12379 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000012380 goto done;
12381 }
Michael Chan9f40dea2005-09-05 17:53:06 -070012382
Joe Perches63c3a662011-04-26 08:12:10 +000012383 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000012384 int i;
12385
12386 /* Reroute all rx packets to the 1st queue */
12387 for (i = MAC_RSS_INDIR_TBL_0;
12388 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
12389 tw32(i, 0x0);
12390 }
12391
Matt Carlson6e01b202011-08-19 13:58:20 +000012392 /* HW errata - mac loopback fails in some cases on 5780.
12393 * Normal traffic and PHY loopback are not affected by
12394 * errata. Also, the MAC loopback test is deprecated for
12395 * all newer ASIC revisions.
12396 */
12397 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
12398 !tg3_flag(tp, CPMU_PRESENT)) {
12399 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070012400
Matt Carlson28a45952011-08-19 13:58:22 +000012401 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
12402 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012403
12404 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012405 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000012406 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012407
12408 tg3_mac_loopback(tp, false);
12409 }
Matt Carlson4852a862011-04-13 11:05:07 +000012410
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012411 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012412 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012413 int i;
12414
Matt Carlson941ec902011-08-19 13:58:23 +000012415 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012416
12417 /* Wait for link */
12418 for (i = 0; i < 100; i++) {
12419 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
12420 break;
12421 mdelay(1);
12422 }
12423
Matt Carlson28a45952011-08-19 13:58:22 +000012424 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
12425 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012426 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012427 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
12428 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012429 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012430 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000012431 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012432
Matt Carlson941ec902011-08-19 13:58:23 +000012433 if (do_extlpbk) {
12434 tg3_phy_lpbk_set(tp, 0, true);
12435
12436 /* All link indications report up, but the hardware
12437 * isn't really ready for about 20 msec. Double it
12438 * to be sure.
12439 */
12440 mdelay(40);
12441
12442 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
12443 data[2] |= TG3_STD_LOOPBACK_FAILED;
12444 if (tg3_flag(tp, TSO_CAPABLE) &&
12445 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
12446 data[2] |= TG3_TSO_LOOPBACK_FAILED;
12447 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012448 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson941ec902011-08-19 13:58:23 +000012449 data[2] |= TG3_JMB_LOOPBACK_FAILED;
12450 }
12451
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012452 /* Re-enable gphy autopowerdown. */
12453 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12454 tg3_phy_toggle_apd(tp, true);
12455 }
Matt Carlson6833c042008-11-21 17:18:59 -080012456
Matt Carlson941ec902011-08-19 13:58:23 +000012457 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000012458
Matt Carlsonab789042011-01-25 15:58:54 +000012459done:
12460 tp->phy_flags |= eee_cap;
12461
Michael Chan9f40dea2005-09-05 17:53:06 -070012462 return err;
12463}
12464
Michael Chan4cafd3f2005-05-29 14:56:34 -070012465static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12466 u64 *data)
12467{
Michael Chan566f86a2005-05-29 14:56:58 -070012468 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012469 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012470
Matt Carlsonbed98292011-07-13 09:27:29 +000012471 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12472 tg3_power_up(tp)) {
12473 etest->flags |= ETH_TEST_FL_FAILED;
12474 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12475 return;
12476 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012477
Michael Chan566f86a2005-05-29 14:56:58 -070012478 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12479
12480 if (tg3_test_nvram(tp) != 0) {
12481 etest->flags |= ETH_TEST_FL_FAILED;
12482 data[0] = 1;
12483 }
Matt Carlson941ec902011-08-19 13:58:23 +000012484 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012485 etest->flags |= ETH_TEST_FL_FAILED;
12486 data[1] = 1;
12487 }
Michael Chana71116d2005-05-29 14:58:11 -070012488 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012489 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012490
Michael Chanbbe832c2005-06-24 20:20:04 -070012491 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012492 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012493 tg3_netif_stop(tp);
12494 irq_sync = 1;
12495 }
12496
12497 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012498
12499 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012500 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012501 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012502 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012503 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012504 if (!err)
12505 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012506
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012507 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080012508 tg3_phy_reset(tp);
12509
Michael Chana71116d2005-05-29 14:58:11 -070012510 if (tg3_test_registers(tp) != 0) {
12511 etest->flags |= ETH_TEST_FL_FAILED;
12512 data[2] = 1;
12513 }
Matt Carlson28a45952011-08-19 13:58:22 +000012514
Michael Chan7942e1d2005-05-29 14:58:36 -070012515 if (tg3_test_memory(tp) != 0) {
12516 etest->flags |= ETH_TEST_FL_FAILED;
12517 data[3] = 1;
12518 }
Matt Carlson28a45952011-08-19 13:58:22 +000012519
Matt Carlson941ec902011-08-19 13:58:23 +000012520 if (doextlpbk)
12521 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12522
12523 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012524 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012525
David S. Millerf47c11e2005-06-24 20:18:35 -070012526 tg3_full_unlock(tp);
12527
Michael Chand4bc3922005-05-29 14:59:20 -070012528 if (tg3_test_interrupt(tp) != 0) {
12529 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012530 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012531 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012532
12533 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012534
Michael Chana71116d2005-05-29 14:58:11 -070012535 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12536 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012537 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012538 err2 = tg3_restart_hw(tp, 1);
12539 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012540 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012541 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012542
12543 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012544
12545 if (irq_sync && !err2)
12546 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012547 }
Matt Carlson800960682010-08-02 11:26:06 +000012548 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012549 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012550
Michael Chan4cafd3f2005-05-29 14:56:34 -070012551}
12552
Linus Torvalds1da177e2005-04-16 15:20:36 -070012553static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12554{
12555 struct mii_ioctl_data *data = if_mii(ifr);
12556 struct tg3 *tp = netdev_priv(dev);
12557 int err;
12558
Joe Perches63c3a662011-04-26 08:12:10 +000012559 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012560 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012561 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012562 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012563 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012564 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012565 }
12566
Matt Carlson33f401a2010-04-05 10:19:27 +000012567 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012568 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012569 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012570
12571 /* fallthru */
12572 case SIOCGMIIREG: {
12573 u32 mii_regval;
12574
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012575 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012576 break; /* We have no PHY */
12577
Matt Carlson34eea5a2011-04-20 07:57:38 +000012578 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012579 return -EAGAIN;
12580
David S. Millerf47c11e2005-06-24 20:18:35 -070012581 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012582 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012583 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012584
12585 data->val_out = mii_regval;
12586
12587 return err;
12588 }
12589
12590 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012591 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012592 break; /* We have no PHY */
12593
Matt Carlson34eea5a2011-04-20 07:57:38 +000012594 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012595 return -EAGAIN;
12596
David S. Millerf47c11e2005-06-24 20:18:35 -070012597 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012598 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012599 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012600
12601 return err;
12602
12603 default:
12604 /* do nothing */
12605 break;
12606 }
12607 return -EOPNOTSUPP;
12608}
12609
David S. Miller15f98502005-05-18 22:49:26 -070012610static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12611{
12612 struct tg3 *tp = netdev_priv(dev);
12613
12614 memcpy(ec, &tp->coal, sizeof(*ec));
12615 return 0;
12616}
12617
Michael Chand244c892005-07-05 14:42:33 -070012618static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12619{
12620 struct tg3 *tp = netdev_priv(dev);
12621 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12622 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12623
Joe Perches63c3a662011-04-26 08:12:10 +000012624 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012625 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12626 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12627 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12628 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12629 }
12630
12631 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12632 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12633 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12634 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12635 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12636 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12637 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12638 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12639 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12640 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12641 return -EINVAL;
12642
12643 /* No rx interrupts will be generated if both are zero */
12644 if ((ec->rx_coalesce_usecs == 0) &&
12645 (ec->rx_max_coalesced_frames == 0))
12646 return -EINVAL;
12647
12648 /* No tx interrupts will be generated if both are zero */
12649 if ((ec->tx_coalesce_usecs == 0) &&
12650 (ec->tx_max_coalesced_frames == 0))
12651 return -EINVAL;
12652
12653 /* Only copy relevant parameters, ignore all others. */
12654 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
12655 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12656 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12657 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12658 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12659 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12660 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12661 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12662 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12663
12664 if (netif_running(dev)) {
12665 tg3_full_lock(tp, 0);
12666 __tg3_set_coalesce(tp, &tp->coal);
12667 tg3_full_unlock(tp);
12668 }
12669 return 0;
12670}
12671
Jeff Garzik7282d492006-09-13 14:30:00 -040012672static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012673 .get_settings = tg3_get_settings,
12674 .set_settings = tg3_set_settings,
12675 .get_drvinfo = tg3_get_drvinfo,
12676 .get_regs_len = tg3_get_regs_len,
12677 .get_regs = tg3_get_regs,
12678 .get_wol = tg3_get_wol,
12679 .set_wol = tg3_set_wol,
12680 .get_msglevel = tg3_get_msglevel,
12681 .set_msglevel = tg3_set_msglevel,
12682 .nway_reset = tg3_nway_reset,
12683 .get_link = ethtool_op_get_link,
12684 .get_eeprom_len = tg3_get_eeprom_len,
12685 .get_eeprom = tg3_get_eeprom,
12686 .set_eeprom = tg3_set_eeprom,
12687 .get_ringparam = tg3_get_ringparam,
12688 .set_ringparam = tg3_set_ringparam,
12689 .get_pauseparam = tg3_get_pauseparam,
12690 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012691 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012692 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012693 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012694 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012695 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012696 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012697 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012698 .get_rxnfc = tg3_get_rxnfc,
12699 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12700 .get_rxfh_indir = tg3_get_rxfh_indir,
12701 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000012702 .get_channels = tg3_get_channels,
12703 .set_channels = tg3_set_channels,
Richard Cochran3f847492012-04-03 22:59:39 +000012704 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012705};
12706
David S. Millerb4017c52012-03-01 17:57:40 -050012707static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
12708 struct rtnl_link_stats64 *stats)
12709{
12710 struct tg3 *tp = netdev_priv(dev);
12711
David S. Millerb4017c52012-03-01 17:57:40 -050012712 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000012713 if (!tp->hw_stats) {
12714 spin_unlock_bh(&tp->lock);
12715 return &tp->net_stats_prev;
12716 }
12717
David S. Millerb4017c52012-03-01 17:57:40 -050012718 tg3_get_nstats(tp, stats);
12719 spin_unlock_bh(&tp->lock);
12720
12721 return stats;
12722}
12723
Matt Carlsonccd5ba92012-02-13 10:20:08 +000012724static void tg3_set_rx_mode(struct net_device *dev)
12725{
12726 struct tg3 *tp = netdev_priv(dev);
12727
12728 if (!netif_running(dev))
12729 return;
12730
12731 tg3_full_lock(tp, 0);
12732 __tg3_set_rx_mode(dev);
12733 tg3_full_unlock(tp);
12734}
12735
Matt Carlsonfaf16272012-02-13 10:20:07 +000012736static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
12737 int new_mtu)
12738{
12739 dev->mtu = new_mtu;
12740
12741 if (new_mtu > ETH_DATA_LEN) {
12742 if (tg3_flag(tp, 5780_CLASS)) {
12743 netdev_update_features(dev);
12744 tg3_flag_clear(tp, TSO_CAPABLE);
12745 } else {
12746 tg3_flag_set(tp, JUMBO_RING_ENABLE);
12747 }
12748 } else {
12749 if (tg3_flag(tp, 5780_CLASS)) {
12750 tg3_flag_set(tp, TSO_CAPABLE);
12751 netdev_update_features(dev);
12752 }
12753 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
12754 }
12755}
12756
12757static int tg3_change_mtu(struct net_device *dev, int new_mtu)
12758{
12759 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000012760 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000012761
12762 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
12763 return -EINVAL;
12764
12765 if (!netif_running(dev)) {
12766 /* We'll just catch it later when the
12767 * device is up'd.
12768 */
12769 tg3_set_mtu(dev, tp, new_mtu);
12770 return 0;
12771 }
12772
12773 tg3_phy_stop(tp);
12774
12775 tg3_netif_stop(tp);
12776
12777 tg3_full_lock(tp, 1);
12778
12779 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12780
12781 tg3_set_mtu(dev, tp, new_mtu);
12782
Michael Chan2fae5e32012-03-04 14:48:15 +000012783 /* Reset PHY, otherwise the read DMA engine will be in a mode that
12784 * breaks all requests to 256 bytes.
12785 */
12786 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
12787 reset_phy = 1;
12788
12789 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000012790
12791 if (!err)
12792 tg3_netif_start(tp);
12793
12794 tg3_full_unlock(tp);
12795
12796 if (!err)
12797 tg3_phy_start(tp);
12798
12799 return err;
12800}
12801
12802static const struct net_device_ops tg3_netdev_ops = {
12803 .ndo_open = tg3_open,
12804 .ndo_stop = tg3_close,
12805 .ndo_start_xmit = tg3_start_xmit,
12806 .ndo_get_stats64 = tg3_get_stats64,
12807 .ndo_validate_addr = eth_validate_addr,
12808 .ndo_set_rx_mode = tg3_set_rx_mode,
12809 .ndo_set_mac_address = tg3_set_mac_addr,
12810 .ndo_do_ioctl = tg3_ioctl,
12811 .ndo_tx_timeout = tg3_tx_timeout,
12812 .ndo_change_mtu = tg3_change_mtu,
12813 .ndo_fix_features = tg3_fix_features,
12814 .ndo_set_features = tg3_set_features,
12815#ifdef CONFIG_NET_POLL_CONTROLLER
12816 .ndo_poll_controller = tg3_poll_controller,
12817#endif
12818};
12819
Linus Torvalds1da177e2005-04-16 15:20:36 -070012820static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
12821{
Michael Chan1b277772006-03-20 22:27:48 -080012822 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012823
12824 tp->nvram_size = EEPROM_CHIP_SIZE;
12825
Matt Carlsone4f34112009-02-25 14:25:00 +000012826 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012827 return;
12828
Michael Chanb16250e2006-09-27 16:10:14 -070012829 if ((magic != TG3_EEPROM_MAGIC) &&
12830 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12831 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012832 return;
12833
12834 /*
12835 * Size the chip by reading offsets at increasing powers of two.
12836 * When we encounter our validation signature, we know the addressing
12837 * has wrapped around, and thus have our chip size.
12838 */
Michael Chan1b277772006-03-20 22:27:48 -080012839 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012840
12841 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012842 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012843 return;
12844
Michael Chan18201802006-03-20 22:29:15 -080012845 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012846 break;
12847
12848 cursize <<= 1;
12849 }
12850
12851 tp->nvram_size = cursize;
12852}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012853
Linus Torvalds1da177e2005-04-16 15:20:36 -070012854static void __devinit tg3_get_nvram_size(struct tg3 *tp)
12855{
12856 u32 val;
12857
Joe Perches63c3a662011-04-26 08:12:10 +000012858 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012859 return;
12860
12861 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012862 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012863 tg3_get_eeprom_size(tp);
12864 return;
12865 }
12866
Matt Carlson6d348f22009-02-25 14:25:52 +000012867 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012868 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012869 /* This is confusing. We want to operate on the
12870 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12871 * call will read from NVRAM and byteswap the data
12872 * according to the byteswapping settings for all
12873 * other register accesses. This ensures the data we
12874 * want will always reside in the lower 16-bits.
12875 * However, the data in NVRAM is in LE format, which
12876 * means the data from the NVRAM read will always be
12877 * opposite the endianness of the CPU. The 16-bit
12878 * byteswap then brings the data to CPU endianness.
12879 */
12880 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012881 return;
12882 }
12883 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012884 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012885}
12886
12887static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12888{
12889 u32 nvcfg1;
12890
12891 nvcfg1 = tr32(NVRAM_CFG1);
12892 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012893 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012894 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012895 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12896 tw32(NVRAM_CFG1, nvcfg1);
12897 }
12898
Matt Carlson6ff6f812011-05-19 12:12:54 +000012899 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012900 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012901 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012902 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12903 tp->nvram_jedecnum = JEDEC_ATMEL;
12904 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012905 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012906 break;
12907 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12908 tp->nvram_jedecnum = JEDEC_ATMEL;
12909 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12910 break;
12911 case FLASH_VENDOR_ATMEL_EEPROM:
12912 tp->nvram_jedecnum = JEDEC_ATMEL;
12913 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012914 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012915 break;
12916 case FLASH_VENDOR_ST:
12917 tp->nvram_jedecnum = JEDEC_ST;
12918 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012919 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012920 break;
12921 case FLASH_VENDOR_SAIFUN:
12922 tp->nvram_jedecnum = JEDEC_SAIFUN;
12923 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12924 break;
12925 case FLASH_VENDOR_SST_SMALL:
12926 case FLASH_VENDOR_SST_LARGE:
12927 tp->nvram_jedecnum = JEDEC_SST;
12928 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12929 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012930 }
Matt Carlson8590a602009-08-28 12:29:16 +000012931 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012932 tp->nvram_jedecnum = JEDEC_ATMEL;
12933 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012934 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012935 }
12936}
12937
Matt Carlsona1b950d2009-09-01 13:20:17 +000012938static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12939{
12940 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12941 case FLASH_5752PAGE_SIZE_256:
12942 tp->nvram_pagesize = 256;
12943 break;
12944 case FLASH_5752PAGE_SIZE_512:
12945 tp->nvram_pagesize = 512;
12946 break;
12947 case FLASH_5752PAGE_SIZE_1K:
12948 tp->nvram_pagesize = 1024;
12949 break;
12950 case FLASH_5752PAGE_SIZE_2K:
12951 tp->nvram_pagesize = 2048;
12952 break;
12953 case FLASH_5752PAGE_SIZE_4K:
12954 tp->nvram_pagesize = 4096;
12955 break;
12956 case FLASH_5752PAGE_SIZE_264:
12957 tp->nvram_pagesize = 264;
12958 break;
12959 case FLASH_5752PAGE_SIZE_528:
12960 tp->nvram_pagesize = 528;
12961 break;
12962 }
12963}
12964
Michael Chan361b4ac2005-04-21 17:11:21 -070012965static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12966{
12967 u32 nvcfg1;
12968
12969 nvcfg1 = tr32(NVRAM_CFG1);
12970
Michael Chane6af3012005-04-21 17:12:05 -070012971 /* NVRAM protection for TPM */
12972 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012973 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012974
Michael Chan361b4ac2005-04-21 17:11:21 -070012975 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012976 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12977 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12978 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012979 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012980 break;
12981 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12982 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012983 tg3_flag_set(tp, NVRAM_BUFFERED);
12984 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012985 break;
12986 case FLASH_5752VENDOR_ST_M45PE10:
12987 case FLASH_5752VENDOR_ST_M45PE20:
12988 case FLASH_5752VENDOR_ST_M45PE40:
12989 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012990 tg3_flag_set(tp, NVRAM_BUFFERED);
12991 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012992 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012993 }
12994
Joe Perches63c3a662011-04-26 08:12:10 +000012995 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012996 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012997 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012998 /* For eeprom, set pagesize to maximum eeprom size */
12999 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13000
13001 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13002 tw32(NVRAM_CFG1, nvcfg1);
13003 }
13004}
13005
Michael Chand3c7b882006-03-23 01:28:25 -080013006static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
13007{
Matt Carlson989a9d22007-05-05 11:51:05 -070013008 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080013009
13010 nvcfg1 = tr32(NVRAM_CFG1);
13011
13012 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070013013 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013014 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070013015 protect = 1;
13016 }
Michael Chand3c7b882006-03-23 01:28:25 -080013017
Matt Carlson989a9d22007-05-05 11:51:05 -070013018 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13019 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013020 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13021 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13022 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13023 case FLASH_5755VENDOR_ATMEL_FLASH_5:
13024 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013025 tg3_flag_set(tp, NVRAM_BUFFERED);
13026 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013027 tp->nvram_pagesize = 264;
13028 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
13029 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
13030 tp->nvram_size = (protect ? 0x3e200 :
13031 TG3_NVRAM_SIZE_512KB);
13032 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
13033 tp->nvram_size = (protect ? 0x1f200 :
13034 TG3_NVRAM_SIZE_256KB);
13035 else
13036 tp->nvram_size = (protect ? 0x1f200 :
13037 TG3_NVRAM_SIZE_128KB);
13038 break;
13039 case FLASH_5752VENDOR_ST_M45PE10:
13040 case FLASH_5752VENDOR_ST_M45PE20:
13041 case FLASH_5752VENDOR_ST_M45PE40:
13042 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013043 tg3_flag_set(tp, NVRAM_BUFFERED);
13044 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013045 tp->nvram_pagesize = 256;
13046 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
13047 tp->nvram_size = (protect ?
13048 TG3_NVRAM_SIZE_64KB :
13049 TG3_NVRAM_SIZE_128KB);
13050 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
13051 tp->nvram_size = (protect ?
13052 TG3_NVRAM_SIZE_64KB :
13053 TG3_NVRAM_SIZE_256KB);
13054 else
13055 tp->nvram_size = (protect ?
13056 TG3_NVRAM_SIZE_128KB :
13057 TG3_NVRAM_SIZE_512KB);
13058 break;
Michael Chand3c7b882006-03-23 01:28:25 -080013059 }
13060}
13061
Michael Chan1b277772006-03-20 22:27:48 -080013062static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
13063{
13064 u32 nvcfg1;
13065
13066 nvcfg1 = tr32(NVRAM_CFG1);
13067
13068 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013069 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
13070 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13071 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
13072 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13073 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013074 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013075 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080013076
Matt Carlson8590a602009-08-28 12:29:16 +000013077 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13078 tw32(NVRAM_CFG1, nvcfg1);
13079 break;
13080 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13081 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13082 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13083 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13084 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013085 tg3_flag_set(tp, NVRAM_BUFFERED);
13086 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013087 tp->nvram_pagesize = 264;
13088 break;
13089 case FLASH_5752VENDOR_ST_M45PE10:
13090 case FLASH_5752VENDOR_ST_M45PE20:
13091 case FLASH_5752VENDOR_ST_M45PE40:
13092 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013093 tg3_flag_set(tp, NVRAM_BUFFERED);
13094 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013095 tp->nvram_pagesize = 256;
13096 break;
Michael Chan1b277772006-03-20 22:27:48 -080013097 }
13098}
13099
Matt Carlson6b91fa02007-10-10 18:01:09 -070013100static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
13101{
13102 u32 nvcfg1, protect = 0;
13103
13104 nvcfg1 = tr32(NVRAM_CFG1);
13105
13106 /* NVRAM protection for TPM */
13107 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013108 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013109 protect = 1;
13110 }
13111
13112 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13113 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013114 case FLASH_5761VENDOR_ATMEL_ADB021D:
13115 case FLASH_5761VENDOR_ATMEL_ADB041D:
13116 case FLASH_5761VENDOR_ATMEL_ADB081D:
13117 case FLASH_5761VENDOR_ATMEL_ADB161D:
13118 case FLASH_5761VENDOR_ATMEL_MDB021D:
13119 case FLASH_5761VENDOR_ATMEL_MDB041D:
13120 case FLASH_5761VENDOR_ATMEL_MDB081D:
13121 case FLASH_5761VENDOR_ATMEL_MDB161D:
13122 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013123 tg3_flag_set(tp, NVRAM_BUFFERED);
13124 tg3_flag_set(tp, FLASH);
13125 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000013126 tp->nvram_pagesize = 256;
13127 break;
13128 case FLASH_5761VENDOR_ST_A_M45PE20:
13129 case FLASH_5761VENDOR_ST_A_M45PE40:
13130 case FLASH_5761VENDOR_ST_A_M45PE80:
13131 case FLASH_5761VENDOR_ST_A_M45PE16:
13132 case FLASH_5761VENDOR_ST_M_M45PE20:
13133 case FLASH_5761VENDOR_ST_M_M45PE40:
13134 case FLASH_5761VENDOR_ST_M_M45PE80:
13135 case FLASH_5761VENDOR_ST_M_M45PE16:
13136 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013137 tg3_flag_set(tp, NVRAM_BUFFERED);
13138 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013139 tp->nvram_pagesize = 256;
13140 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013141 }
13142
13143 if (protect) {
13144 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
13145 } else {
13146 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013147 case FLASH_5761VENDOR_ATMEL_ADB161D:
13148 case FLASH_5761VENDOR_ATMEL_MDB161D:
13149 case FLASH_5761VENDOR_ST_A_M45PE16:
13150 case FLASH_5761VENDOR_ST_M_M45PE16:
13151 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
13152 break;
13153 case FLASH_5761VENDOR_ATMEL_ADB081D:
13154 case FLASH_5761VENDOR_ATMEL_MDB081D:
13155 case FLASH_5761VENDOR_ST_A_M45PE80:
13156 case FLASH_5761VENDOR_ST_M_M45PE80:
13157 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13158 break;
13159 case FLASH_5761VENDOR_ATMEL_ADB041D:
13160 case FLASH_5761VENDOR_ATMEL_MDB041D:
13161 case FLASH_5761VENDOR_ST_A_M45PE40:
13162 case FLASH_5761VENDOR_ST_M_M45PE40:
13163 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13164 break;
13165 case FLASH_5761VENDOR_ATMEL_ADB021D:
13166 case FLASH_5761VENDOR_ATMEL_MDB021D:
13167 case FLASH_5761VENDOR_ST_A_M45PE20:
13168 case FLASH_5761VENDOR_ST_M_M45PE20:
13169 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13170 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013171 }
13172 }
13173}
13174
Michael Chanb5d37722006-09-27 16:06:21 -070013175static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
13176{
13177 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013178 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070013179 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13180}
13181
Matt Carlson321d32a2008-11-21 17:22:19 -080013182static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
13183{
13184 u32 nvcfg1;
13185
13186 nvcfg1 = tr32(NVRAM_CFG1);
13187
13188 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13189 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13190 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13191 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013192 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080013193 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13194
13195 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13196 tw32(NVRAM_CFG1, nvcfg1);
13197 return;
13198 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13199 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13200 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13201 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13202 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13203 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13204 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13205 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013206 tg3_flag_set(tp, NVRAM_BUFFERED);
13207 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013208
13209 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13210 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13211 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13212 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13213 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13214 break;
13215 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13216 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13217 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13218 break;
13219 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13220 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13221 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13222 break;
13223 }
13224 break;
13225 case FLASH_5752VENDOR_ST_M45PE10:
13226 case FLASH_5752VENDOR_ST_M45PE20:
13227 case FLASH_5752VENDOR_ST_M45PE40:
13228 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013229 tg3_flag_set(tp, NVRAM_BUFFERED);
13230 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013231
13232 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13233 case FLASH_5752VENDOR_ST_M45PE10:
13234 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13235 break;
13236 case FLASH_5752VENDOR_ST_M45PE20:
13237 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13238 break;
13239 case FLASH_5752VENDOR_ST_M45PE40:
13240 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13241 break;
13242 }
13243 break;
13244 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013245 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080013246 return;
13247 }
13248
Matt Carlsona1b950d2009-09-01 13:20:17 +000013249 tg3_nvram_get_pagesize(tp, nvcfg1);
13250 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013251 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013252}
13253
13254
13255static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
13256{
13257 u32 nvcfg1;
13258
13259 nvcfg1 = tr32(NVRAM_CFG1);
13260
13261 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13262 case FLASH_5717VENDOR_ATMEL_EEPROM:
13263 case FLASH_5717VENDOR_MICRO_EEPROM:
13264 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013265 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013266 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13267
13268 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13269 tw32(NVRAM_CFG1, nvcfg1);
13270 return;
13271 case FLASH_5717VENDOR_ATMEL_MDB011D:
13272 case FLASH_5717VENDOR_ATMEL_ADB011B:
13273 case FLASH_5717VENDOR_ATMEL_ADB011D:
13274 case FLASH_5717VENDOR_ATMEL_MDB021D:
13275 case FLASH_5717VENDOR_ATMEL_ADB021B:
13276 case FLASH_5717VENDOR_ATMEL_ADB021D:
13277 case FLASH_5717VENDOR_ATMEL_45USPT:
13278 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013279 tg3_flag_set(tp, NVRAM_BUFFERED);
13280 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013281
13282 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13283 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013284 /* Detect size with tg3_nvram_get_size() */
13285 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013286 case FLASH_5717VENDOR_ATMEL_ADB021B:
13287 case FLASH_5717VENDOR_ATMEL_ADB021D:
13288 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13289 break;
13290 default:
13291 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13292 break;
13293 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013294 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013295 case FLASH_5717VENDOR_ST_M_M25PE10:
13296 case FLASH_5717VENDOR_ST_A_M25PE10:
13297 case FLASH_5717VENDOR_ST_M_M45PE10:
13298 case FLASH_5717VENDOR_ST_A_M45PE10:
13299 case FLASH_5717VENDOR_ST_M_M25PE20:
13300 case FLASH_5717VENDOR_ST_A_M25PE20:
13301 case FLASH_5717VENDOR_ST_M_M45PE20:
13302 case FLASH_5717VENDOR_ST_A_M45PE20:
13303 case FLASH_5717VENDOR_ST_25USPT:
13304 case FLASH_5717VENDOR_ST_45USPT:
13305 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013306 tg3_flag_set(tp, NVRAM_BUFFERED);
13307 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013308
13309 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13310 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013311 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013312 /* Detect size with tg3_nvram_get_size() */
13313 break;
13314 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013315 case FLASH_5717VENDOR_ST_A_M45PE20:
13316 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13317 break;
13318 default:
13319 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13320 break;
13321 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013322 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013323 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013324 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013325 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080013326 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000013327
13328 tg3_nvram_get_pagesize(tp, nvcfg1);
13329 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013330 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013331}
13332
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013333static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
13334{
13335 u32 nvcfg1, nvmpinstrp;
13336
13337 nvcfg1 = tr32(NVRAM_CFG1);
13338 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
13339
13340 switch (nvmpinstrp) {
13341 case FLASH_5720_EEPROM_HD:
13342 case FLASH_5720_EEPROM_LD:
13343 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013344 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013345
13346 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13347 tw32(NVRAM_CFG1, nvcfg1);
13348 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
13349 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13350 else
13351 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
13352 return;
13353 case FLASH_5720VENDOR_M_ATMEL_DB011D:
13354 case FLASH_5720VENDOR_A_ATMEL_DB011B:
13355 case FLASH_5720VENDOR_A_ATMEL_DB011D:
13356 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13357 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13358 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13359 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13360 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13361 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13362 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13363 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13364 case FLASH_5720VENDOR_ATMEL_45USPT:
13365 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013366 tg3_flag_set(tp, NVRAM_BUFFERED);
13367 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013368
13369 switch (nvmpinstrp) {
13370 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13371 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13372 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13373 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13374 break;
13375 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13376 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13377 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13378 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13379 break;
13380 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13381 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13382 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13383 break;
13384 default:
13385 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13386 break;
13387 }
13388 break;
13389 case FLASH_5720VENDOR_M_ST_M25PE10:
13390 case FLASH_5720VENDOR_M_ST_M45PE10:
13391 case FLASH_5720VENDOR_A_ST_M25PE10:
13392 case FLASH_5720VENDOR_A_ST_M45PE10:
13393 case FLASH_5720VENDOR_M_ST_M25PE20:
13394 case FLASH_5720VENDOR_M_ST_M45PE20:
13395 case FLASH_5720VENDOR_A_ST_M25PE20:
13396 case FLASH_5720VENDOR_A_ST_M45PE20:
13397 case FLASH_5720VENDOR_M_ST_M25PE40:
13398 case FLASH_5720VENDOR_M_ST_M45PE40:
13399 case FLASH_5720VENDOR_A_ST_M25PE40:
13400 case FLASH_5720VENDOR_A_ST_M45PE40:
13401 case FLASH_5720VENDOR_M_ST_M25PE80:
13402 case FLASH_5720VENDOR_M_ST_M45PE80:
13403 case FLASH_5720VENDOR_A_ST_M25PE80:
13404 case FLASH_5720VENDOR_A_ST_M45PE80:
13405 case FLASH_5720VENDOR_ST_25USPT:
13406 case FLASH_5720VENDOR_ST_45USPT:
13407 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013408 tg3_flag_set(tp, NVRAM_BUFFERED);
13409 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013410
13411 switch (nvmpinstrp) {
13412 case FLASH_5720VENDOR_M_ST_M25PE20:
13413 case FLASH_5720VENDOR_M_ST_M45PE20:
13414 case FLASH_5720VENDOR_A_ST_M25PE20:
13415 case FLASH_5720VENDOR_A_ST_M45PE20:
13416 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13417 break;
13418 case FLASH_5720VENDOR_M_ST_M25PE40:
13419 case FLASH_5720VENDOR_M_ST_M45PE40:
13420 case FLASH_5720VENDOR_A_ST_M25PE40:
13421 case FLASH_5720VENDOR_A_ST_M45PE40:
13422 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13423 break;
13424 case FLASH_5720VENDOR_M_ST_M25PE80:
13425 case FLASH_5720VENDOR_M_ST_M45PE80:
13426 case FLASH_5720VENDOR_A_ST_M25PE80:
13427 case FLASH_5720VENDOR_A_ST_M45PE80:
13428 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13429 break;
13430 default:
13431 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13432 break;
13433 }
13434 break;
13435 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013436 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013437 return;
13438 }
13439
13440 tg3_nvram_get_pagesize(tp, nvcfg1);
13441 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013442 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013443}
13444
Linus Torvalds1da177e2005-04-16 15:20:36 -070013445/* Chips other than 5700/5701 use the NVRAM for fetching info. */
13446static void __devinit tg3_nvram_init(struct tg3 *tp)
13447{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013448 tw32_f(GRC_EEPROM_ADDR,
13449 (EEPROM_ADDR_FSM_RESET |
13450 (EEPROM_DEFAULT_CLOCK_PERIOD <<
13451 EEPROM_ADDR_CLKPERD_SHIFT)));
13452
Michael Chan9d57f012006-12-07 00:23:25 -080013453 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013454
13455 /* Enable seeprom accesses. */
13456 tw32_f(GRC_LOCAL_CTRL,
13457 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
13458 udelay(100);
13459
13460 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13461 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000013462 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013463
Michael Chanec41c7d2006-01-17 02:40:55 -080013464 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000013465 netdev_warn(tp->dev,
13466 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000013467 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080013468 return;
13469 }
Michael Chane6af3012005-04-21 17:12:05 -070013470 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013471
Matt Carlson989a9d22007-05-05 11:51:05 -070013472 tp->nvram_size = 0;
13473
Michael Chan361b4ac2005-04-21 17:11:21 -070013474 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13475 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080013476 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13477 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013478 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013479 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13480 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080013481 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013482 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
13483 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070013484 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13485 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000013486 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013487 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013488 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013489 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13490 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013491 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013492 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13493 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070013494 else
13495 tg3_get_nvram_info(tp);
13496
Matt Carlson989a9d22007-05-05 11:51:05 -070013497 if (tp->nvram_size == 0)
13498 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013499
Michael Chane6af3012005-04-21 17:12:05 -070013500 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080013501 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013502
13503 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013504 tg3_flag_clear(tp, NVRAM);
13505 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013506
13507 tg3_get_eeprom_size(tp);
13508 }
13509}
13510
Linus Torvalds1da177e2005-04-16 15:20:36 -070013511struct subsys_tbl_ent {
13512 u16 subsys_vendor, subsys_devid;
13513 u32 phy_id;
13514};
13515
Matt Carlson24daf2b2010-02-17 15:17:02 +000013516static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013517 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013518 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013519 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013520 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013521 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013522 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013523 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013524 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13525 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13526 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013527 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013528 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013529 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013530 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13531 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13532 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013533 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013534 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013535 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013536 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013537 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013538 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013539 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013540
13541 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013542 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013543 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013544 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013545 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013546 { TG3PCI_SUBVENDOR_ID_3COM,
13547 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13548 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013549 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013550 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013551 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013552
13553 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013554 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013555 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013556 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013557 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013558 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013559 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013560 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013561 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013562
13563 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013564 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013565 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013566 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013567 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013568 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13569 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13570 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013571 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013572 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013573 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013574
13575 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013576 { TG3PCI_SUBVENDOR_ID_IBM,
13577 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013578};
13579
Matt Carlson24daf2b2010-02-17 15:17:02 +000013580static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013581{
13582 int i;
13583
13584 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13585 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13586 tp->pdev->subsystem_vendor) &&
13587 (subsys_id_to_phy_id[i].subsys_devid ==
13588 tp->pdev->subsystem_device))
13589 return &subsys_id_to_phy_id[i];
13590 }
13591 return NULL;
13592}
13593
Michael Chan7d0c41e2005-04-21 17:06:20 -070013594static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013595{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013596 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013597
Matt Carlson79eb6902010-02-17 15:17:03 +000013598 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013599 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13600
Gary Zambranoa85feb82007-05-05 11:52:19 -070013601 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013602 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13603 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013604
Michael Chanb5d37722006-09-27 16:06:21 -070013605 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013606 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013607 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13608 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013609 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013610 val = tr32(VCPU_CFGSHDW);
13611 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013612 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013613 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013614 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013615 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013616 device_set_wakeup_enable(&tp->pdev->dev, true);
13617 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013618 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013619 }
13620
Linus Torvalds1da177e2005-04-16 15:20:36 -070013621 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13622 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13623 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013624 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013625 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013626
13627 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13628 tp->nic_sram_data_cfg = nic_cfg;
13629
13630 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13631 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013632 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13633 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13634 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013635 (ver > 0) && (ver < 0x100))
13636 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13637
Matt Carlsona9daf362008-05-25 23:49:44 -070013638 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13639 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13640
Linus Torvalds1da177e2005-04-16 15:20:36 -070013641 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13642 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13643 eeprom_phy_serdes = 1;
13644
13645 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13646 if (nic_phy_id != 0) {
13647 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13648 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13649
13650 eeprom_phy_id = (id1 >> 16) << 10;
13651 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13652 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13653 } else
13654 eeprom_phy_id = 0;
13655
Michael Chan7d0c41e2005-04-21 17:06:20 -070013656 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013657 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013658 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013659 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013660 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013661 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013662 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013663
Joe Perches63c3a662011-04-26 08:12:10 +000013664 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013665 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13666 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013667 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013668 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13669
13670 switch (led_cfg) {
13671 default:
13672 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13673 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13674 break;
13675
13676 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13677 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13678 break;
13679
13680 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13681 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013682
13683 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13684 * read on some older 5700/5701 bootcode.
13685 */
13686 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13687 ASIC_REV_5700 ||
13688 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13689 ASIC_REV_5701)
13690 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13691
Linus Torvalds1da177e2005-04-16 15:20:36 -070013692 break;
13693
13694 case SHASTA_EXT_LED_SHARED:
13695 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13696 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13697 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13698 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13699 LED_CTRL_MODE_PHY_2);
13700 break;
13701
13702 case SHASTA_EXT_LED_MAC:
13703 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13704 break;
13705
13706 case SHASTA_EXT_LED_COMBO:
13707 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13708 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13709 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13710 LED_CTRL_MODE_PHY_2);
13711 break;
13712
Stephen Hemminger855e1112008-04-16 16:37:28 -070013713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013714
13715 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13716 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13717 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13718 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13719
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013720 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13721 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013722
Michael Chan9d26e212006-12-07 00:21:14 -080013723 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013724 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013725 if ((tp->pdev->subsystem_vendor ==
13726 PCI_VENDOR_ID_ARIMA) &&
13727 (tp->pdev->subsystem_device == 0x205a ||
13728 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013729 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013730 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013731 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13732 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013734
13735 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013736 tg3_flag_set(tp, ENABLE_ASF);
13737 if (tg3_flag(tp, 5750_PLUS))
13738 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013739 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013740
13741 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013742 tg3_flag(tp, 5750_PLUS))
13743 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013744
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013745 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013746 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013747 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013748
Joe Perches63c3a662011-04-26 08:12:10 +000013749 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013750 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013751 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013752 device_set_wakeup_enable(&tp->pdev->dev, true);
13753 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013754
Linus Torvalds1da177e2005-04-16 15:20:36 -070013755 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013756 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013757
13758 /* serdes signal pre-emphasis in register 0x590 set by */
13759 /* bootcode if bit 18 is set */
13760 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013761 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013762
Joe Perches63c3a662011-04-26 08:12:10 +000013763 if ((tg3_flag(tp, 57765_PLUS) ||
13764 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13765 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013766 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013767 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013768
Joe Perches63c3a662011-04-26 08:12:10 +000013769 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013770 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013771 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013772 u32 cfg3;
13773
13774 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13775 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013776 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013777 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013778
Matt Carlson14417062010-02-17 15:16:59 +000013779 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013780 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013781 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013782 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013783 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013784 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013785 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013786done:
Joe Perches63c3a662011-04-26 08:12:10 +000013787 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013788 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013789 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013790 else
13791 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013792}
13793
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013794static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13795{
13796 int i;
13797 u32 val;
13798
13799 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13800 tw32(OTP_CTRL, cmd);
13801
13802 /* Wait for up to 1 ms for command to execute. */
13803 for (i = 0; i < 100; i++) {
13804 val = tr32(OTP_STATUS);
13805 if (val & OTP_STATUS_CMD_DONE)
13806 break;
13807 udelay(10);
13808 }
13809
13810 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13811}
13812
13813/* Read the gphy configuration from the OTP region of the chip. The gphy
13814 * configuration is a 32-bit value that straddles the alignment boundary.
13815 * We do two 32-bit reads and then shift and merge the results.
13816 */
13817static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13818{
13819 u32 bhalf_otp, thalf_otp;
13820
13821 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13822
13823 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13824 return 0;
13825
13826 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13827
13828 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13829 return 0;
13830
13831 thalf_otp = tr32(OTP_READ_DATA);
13832
13833 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13834
13835 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13836 return 0;
13837
13838 bhalf_otp = tr32(OTP_READ_DATA);
13839
13840 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13841}
13842
Matt Carlsone256f8a2011-03-09 16:58:24 +000013843static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13844{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013845 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013846
13847 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13848 adv |= ADVERTISED_1000baseT_Half |
13849 ADVERTISED_1000baseT_Full;
13850
13851 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13852 adv |= ADVERTISED_100baseT_Half |
13853 ADVERTISED_100baseT_Full |
13854 ADVERTISED_10baseT_Half |
13855 ADVERTISED_10baseT_Full |
13856 ADVERTISED_TP;
13857 else
13858 adv |= ADVERTISED_FIBRE;
13859
13860 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000013861 tp->link_config.speed = SPEED_UNKNOWN;
13862 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013863 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000013864 tp->link_config.active_speed = SPEED_UNKNOWN;
13865 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000013866
13867 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013868}
13869
Michael Chan7d0c41e2005-04-21 17:06:20 -070013870static int __devinit tg3_phy_probe(struct tg3 *tp)
13871{
13872 u32 hw_phy_id_1, hw_phy_id_2;
13873 u32 hw_phy_id, hw_phy_id_masked;
13874 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013875
Matt Carlsone256f8a2011-03-09 16:58:24 +000013876 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013877 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013878 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13879
Michael Chan8151ad52012-07-29 19:15:41 +000013880 if (tg3_flag(tp, ENABLE_APE)) {
13881 switch (tp->pci_fn) {
13882 case 0:
13883 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
13884 break;
13885 case 1:
13886 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
13887 break;
13888 case 2:
13889 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
13890 break;
13891 case 3:
13892 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
13893 break;
13894 }
13895 }
13896
Joe Perches63c3a662011-04-26 08:12:10 +000013897 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013898 return tg3_phy_init(tp);
13899
Linus Torvalds1da177e2005-04-16 15:20:36 -070013900 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013901 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013902 */
13903 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013904 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013905 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013906 } else {
13907 /* Now read the physical PHY_ID from the chip and verify
13908 * that it is sane. If it doesn't look good, we fall back
13909 * to either the hard-coded table based PHY_ID and failing
13910 * that the value found in the eeprom area.
13911 */
13912 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13913 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13914
13915 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13916 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13917 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13918
Matt Carlson79eb6902010-02-17 15:17:03 +000013919 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013920 }
13921
Matt Carlson79eb6902010-02-17 15:17:03 +000013922 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013923 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013924 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013925 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013926 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013927 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013928 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013929 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013930 /* Do nothing, phy ID already set up in
13931 * tg3_get_eeprom_hw_cfg().
13932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013933 } else {
13934 struct subsys_tbl_ent *p;
13935
13936 /* No eeprom signature? Try the hardcoded
13937 * subsys device table.
13938 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013939 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013940 if (!p)
13941 return -ENODEV;
13942
13943 tp->phy_id = p->phy_id;
13944 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013945 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013946 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013947 }
13948 }
13949
Matt Carlsona6b68da2010-12-06 08:28:52 +000013950 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013951 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13952 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13953 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013954 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13955 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13956 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013957 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13958
Matt Carlsone256f8a2011-03-09 16:58:24 +000013959 tg3_phy_init_link_config(tp);
13960
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013961 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013962 !tg3_flag(tp, ENABLE_APE) &&
13963 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013964 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013965
13966 tg3_readphy(tp, MII_BMSR, &bmsr);
13967 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13968 (bmsr & BMSR_LSTATUS))
13969 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013970
Linus Torvalds1da177e2005-04-16 15:20:36 -070013971 err = tg3_phy_reset(tp);
13972 if (err)
13973 return err;
13974
Matt Carlson42b64a42011-05-19 12:12:49 +000013975 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013976
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013977 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013978 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13979 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013980
13981 tg3_writephy(tp, MII_BMCR,
13982 BMCR_ANENABLE | BMCR_ANRESTART);
13983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013984 }
13985
13986skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013987 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013988 err = tg3_init_5401phy_dsp(tp);
13989 if (err)
13990 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013991
Linus Torvalds1da177e2005-04-16 15:20:36 -070013992 err = tg3_init_5401phy_dsp(tp);
13993 }
13994
Linus Torvalds1da177e2005-04-16 15:20:36 -070013995 return err;
13996}
13997
Matt Carlson184b8902010-04-05 10:19:25 +000013998static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013999{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014000 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014001 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000014002 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000014003 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014004
Matt Carlson535a4902011-07-20 10:20:56 +000014005 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014006 if (!vpd_data)
14007 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014008
Matt Carlson535a4902011-07-20 10:20:56 +000014009 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000014010 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014011 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014012
14013 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
14014 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
14015 i += PCI_VPD_LRDT_TAG_SIZE;
14016
Matt Carlson535a4902011-07-20 10:20:56 +000014017 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014018 goto out_not_found;
14019
Matt Carlson184b8902010-04-05 10:19:25 +000014020 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14021 PCI_VPD_RO_KEYWORD_MFR_ID);
14022 if (j > 0) {
14023 len = pci_vpd_info_field_size(&vpd_data[j]);
14024
14025 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14026 if (j + len > block_end || len != 4 ||
14027 memcmp(&vpd_data[j], "1028", 4))
14028 goto partno;
14029
14030 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14031 PCI_VPD_RO_KEYWORD_VENDOR0);
14032 if (j < 0)
14033 goto partno;
14034
14035 len = pci_vpd_info_field_size(&vpd_data[j]);
14036
14037 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14038 if (j + len > block_end)
14039 goto partno;
14040
14041 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000014042 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000014043 }
14044
14045partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000014046 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14047 PCI_VPD_RO_KEYWORD_PARTNO);
14048 if (i < 0)
14049 goto out_not_found;
14050
14051 len = pci_vpd_info_field_size(&vpd_data[i]);
14052
14053 i += PCI_VPD_INFO_FLD_HDR_SIZE;
14054 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000014055 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014056 goto out_not_found;
14057
14058 memcpy(tp->board_part_number, &vpd_data[i], len);
14059
Linus Torvalds1da177e2005-04-16 15:20:36 -070014060out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014061 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000014062 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014063 return;
14064
14065out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000014066 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000014067 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
14068 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000014069 strcpy(tp->board_part_number, "BCM5717");
14070 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
14071 strcpy(tp->board_part_number, "BCM5718");
14072 else
14073 goto nomatch;
14074 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
14075 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
14076 strcpy(tp->board_part_number, "BCM57780");
14077 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
14078 strcpy(tp->board_part_number, "BCM57760");
14079 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
14080 strcpy(tp->board_part_number, "BCM57790");
14081 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
14082 strcpy(tp->board_part_number, "BCM57788");
14083 else
14084 goto nomatch;
14085 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
14086 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
14087 strcpy(tp->board_part_number, "BCM57761");
14088 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
14089 strcpy(tp->board_part_number, "BCM57765");
14090 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
14091 strcpy(tp->board_part_number, "BCM57781");
14092 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
14093 strcpy(tp->board_part_number, "BCM57785");
14094 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
14095 strcpy(tp->board_part_number, "BCM57791");
14096 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
14097 strcpy(tp->board_part_number, "BCM57795");
14098 else
14099 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000014100 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
14101 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
14102 strcpy(tp->board_part_number, "BCM57762");
14103 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
14104 strcpy(tp->board_part_number, "BCM57766");
14105 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
14106 strcpy(tp->board_part_number, "BCM57782");
14107 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14108 strcpy(tp->board_part_number, "BCM57786");
14109 else
14110 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000014111 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070014112 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000014113 } else {
14114nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070014115 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000014116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014117}
14118
Matt Carlson9c8a6202007-10-21 16:16:08 -070014119static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
14120{
14121 u32 val;
14122
Matt Carlsone4f34112009-02-25 14:25:00 +000014123 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014124 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014125 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014126 val != 0)
14127 return 0;
14128
14129 return 1;
14130}
14131
Matt Carlsonacd9c112009-02-25 14:26:33 +000014132static void __devinit tg3_read_bc_ver(struct tg3 *tp)
14133{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014134 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000014135 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014136 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014137
14138 if (tg3_nvram_read(tp, 0xc, &offset) ||
14139 tg3_nvram_read(tp, 0x4, &start))
14140 return;
14141
14142 offset = tg3_nvram_logical_addr(tp, offset);
14143
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014144 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014145 return;
14146
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014147 if ((val & 0xfc000000) == 0x0c000000) {
14148 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014149 return;
14150
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014151 if (val == 0)
14152 newver = true;
14153 }
14154
Matt Carlson75f99362010-04-05 10:19:24 +000014155 dst_off = strlen(tp->fw_ver);
14156
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014157 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000014158 if (TG3_VER_SIZE - dst_off < 16 ||
14159 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014160 return;
14161
14162 offset = offset + ver_offset - start;
14163 for (i = 0; i < 16; i += 4) {
14164 __be32 v;
14165 if (tg3_nvram_read_be32(tp, offset + i, &v))
14166 return;
14167
Matt Carlson75f99362010-04-05 10:19:24 +000014168 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014169 }
14170 } else {
14171 u32 major, minor;
14172
14173 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
14174 return;
14175
14176 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
14177 TG3_NVM_BCVER_MAJSFT;
14178 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000014179 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
14180 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014181 }
14182}
14183
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014184static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
14185{
14186 u32 val, major, minor;
14187
14188 /* Use native endian representation */
14189 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
14190 return;
14191
14192 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
14193 TG3_NVM_HWSB_CFG1_MAJSFT;
14194 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
14195 TG3_NVM_HWSB_CFG1_MINSFT;
14196
14197 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
14198}
14199
Matt Carlsondfe00d72008-11-21 17:19:41 -080014200static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
14201{
14202 u32 offset, major, minor, build;
14203
Matt Carlson75f99362010-04-05 10:19:24 +000014204 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014205
14206 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
14207 return;
14208
14209 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
14210 case TG3_EEPROM_SB_REVISION_0:
14211 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
14212 break;
14213 case TG3_EEPROM_SB_REVISION_2:
14214 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
14215 break;
14216 case TG3_EEPROM_SB_REVISION_3:
14217 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
14218 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000014219 case TG3_EEPROM_SB_REVISION_4:
14220 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
14221 break;
14222 case TG3_EEPROM_SB_REVISION_5:
14223 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
14224 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000014225 case TG3_EEPROM_SB_REVISION_6:
14226 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
14227 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014228 default:
14229 return;
14230 }
14231
Matt Carlsone4f34112009-02-25 14:25:00 +000014232 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080014233 return;
14234
14235 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
14236 TG3_EEPROM_SB_EDH_BLD_SHFT;
14237 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
14238 TG3_EEPROM_SB_EDH_MAJ_SHFT;
14239 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
14240
14241 if (minor > 99 || build > 26)
14242 return;
14243
Matt Carlson75f99362010-04-05 10:19:24 +000014244 offset = strlen(tp->fw_ver);
14245 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
14246 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014247
14248 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000014249 offset = strlen(tp->fw_ver);
14250 if (offset < TG3_VER_SIZE - 1)
14251 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014252 }
14253}
14254
Matt Carlsonacd9c112009-02-25 14:26:33 +000014255static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080014256{
14257 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014258 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070014259
14260 for (offset = TG3_NVM_DIR_START;
14261 offset < TG3_NVM_DIR_END;
14262 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014263 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014264 return;
14265
14266 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
14267 break;
14268 }
14269
14270 if (offset == TG3_NVM_DIR_END)
14271 return;
14272
Joe Perches63c3a662011-04-26 08:12:10 +000014273 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014274 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000014275 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014276 return;
14277
Matt Carlsone4f34112009-02-25 14:25:00 +000014278 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014279 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014280 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014281 return;
14282
14283 offset += val - start;
14284
Matt Carlsonacd9c112009-02-25 14:26:33 +000014285 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014286
Matt Carlsonacd9c112009-02-25 14:26:33 +000014287 tp->fw_ver[vlen++] = ',';
14288 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070014289
14290 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000014291 __be32 v;
14292 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014293 return;
14294
Al Virob9fc7dc2007-12-17 22:59:57 -080014295 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014296
Matt Carlsonacd9c112009-02-25 14:26:33 +000014297 if (vlen > TG3_VER_SIZE - sizeof(v)) {
14298 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014299 break;
14300 }
14301
Matt Carlsonacd9c112009-02-25 14:26:33 +000014302 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
14303 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014304 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000014305}
14306
Michael Chan165f4d12012-07-16 16:23:59 +000014307static void __devinit tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000014308{
Matt Carlson7fd76442009-02-25 14:27:20 +000014309 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000014310
14311 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
14312 if (apedata != APE_SEG_SIG_MAGIC)
14313 return;
14314
14315 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
14316 if (!(apedata & APE_FW_STATUS_READY))
14317 return;
14318
Michael Chan165f4d12012-07-16 16:23:59 +000014319 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
14320 tg3_flag_set(tp, APE_HAS_NCSI);
14321}
14322
14323static void __devinit tg3_read_dash_ver(struct tg3 *tp)
14324{
14325 int vlen;
14326 u32 apedata;
14327 char *fwtype;
14328
Matt Carlson7fd76442009-02-25 14:27:20 +000014329 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
14330
Michael Chan165f4d12012-07-16 16:23:59 +000014331 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000014332 fwtype = "NCSI";
Michael Chan165f4d12012-07-16 16:23:59 +000014333 else
Matt Carlsonecc79642010-08-02 11:26:01 +000014334 fwtype = "DASH";
14335
Matt Carlson7fd76442009-02-25 14:27:20 +000014336 vlen = strlen(tp->fw_ver);
14337
Matt Carlsonecc79642010-08-02 11:26:01 +000014338 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
14339 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000014340 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
14341 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
14342 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
14343 (apedata & APE_FW_VERSION_BLDMSK));
14344}
14345
Matt Carlsonacd9c112009-02-25 14:26:33 +000014346static void __devinit tg3_read_fw_ver(struct tg3 *tp)
14347{
14348 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000014349 bool vpd_vers = false;
14350
14351 if (tp->fw_ver[0] != 0)
14352 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014353
Joe Perches63c3a662011-04-26 08:12:10 +000014354 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000014355 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000014356 return;
14357 }
14358
Matt Carlsonacd9c112009-02-25 14:26:33 +000014359 if (tg3_nvram_read(tp, 0, &val))
14360 return;
14361
14362 if (val == TG3_EEPROM_MAGIC)
14363 tg3_read_bc_ver(tp);
14364 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
14365 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014366 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
14367 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014368
Michael Chan165f4d12012-07-16 16:23:59 +000014369 if (tg3_flag(tp, ENABLE_ASF)) {
14370 if (tg3_flag(tp, ENABLE_APE)) {
14371 tg3_probe_ncsi(tp);
14372 if (!vpd_vers)
14373 tg3_read_dash_ver(tp);
14374 } else if (!vpd_vers) {
14375 tg3_read_mgmtfw_ver(tp);
14376 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000014377 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070014378
14379 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080014380}
14381
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014382static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
14383{
Joe Perches63c3a662011-04-26 08:12:10 +000014384 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014385 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000014386 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014387 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014388 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000014389 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014390}
14391
Matt Carlson41434702011-03-09 16:58:22 +000014392static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014393 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
14394 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
14395 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
14396 { },
14397};
14398
Matt Carlson16c7fa72012-02-13 10:20:10 +000014399static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
14400{
14401 struct pci_dev *peer;
14402 unsigned int func, devnr = tp->pdev->devfn & ~7;
14403
14404 for (func = 0; func < 8; func++) {
14405 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14406 if (peer && peer != tp->pdev)
14407 break;
14408 pci_dev_put(peer);
14409 }
14410 /* 5704 can be configured in single-port mode, set peer to
14411 * tp->pdev in that case.
14412 */
14413 if (!peer) {
14414 peer = tp->pdev;
14415 return peer;
14416 }
14417
14418 /*
14419 * We don't need to keep the refcount elevated; there's no way
14420 * to remove one half of this device without removing the other
14421 */
14422 pci_dev_put(peer);
14423
14424 return peer;
14425}
14426
Matt Carlson42b123b2012-02-13 15:20:13 +000014427static void __devinit tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
14428{
14429 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
14430 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
14431 u32 reg;
14432
14433 /* All devices that use the alternate
14434 * ASIC REV location have a CPMU.
14435 */
14436 tg3_flag_set(tp, CPMU_PRESENT);
14437
14438 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000014439 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000014440 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
14441 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
14442 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
14443 reg = TG3PCI_GEN2_PRODID_ASICREV;
14444 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
14445 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
14446 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
14447 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
14448 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14449 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
14450 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
14451 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
14452 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
14453 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14454 reg = TG3PCI_GEN15_PRODID_ASICREV;
14455 else
14456 reg = TG3PCI_PRODID_ASICREV;
14457
14458 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
14459 }
14460
14461 /* Wrong chip ID in 5752 A0. This code can be removed later
14462 * as A0 is not in production.
14463 */
14464 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
14465 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
14466
Michael Chan79d49692012-11-05 14:26:29 +000014467 if (tp->pci_chip_rev_id == CHIPREV_ID_5717_C0)
14468 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
14469
Matt Carlson42b123b2012-02-13 15:20:13 +000014470 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14471 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14472 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14473 tg3_flag_set(tp, 5717_PLUS);
14474
14475 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
14476 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14477 tg3_flag_set(tp, 57765_CLASS);
14478
14479 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
14480 tg3_flag_set(tp, 57765_PLUS);
14481
14482 /* Intentionally exclude ASIC_REV_5906 */
14483 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
14484 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
14485 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14486 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14487 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14488 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14489 tg3_flag(tp, 57765_PLUS))
14490 tg3_flag_set(tp, 5755_PLUS);
14491
14492 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14493 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
14494 tg3_flag_set(tp, 5780_CLASS);
14495
14496 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14497 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14498 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
14499 tg3_flag(tp, 5755_PLUS) ||
14500 tg3_flag(tp, 5780_CLASS))
14501 tg3_flag_set(tp, 5750_PLUS);
14502
14503 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14504 tg3_flag(tp, 5750_PLUS))
14505 tg3_flag_set(tp, 5705_PLUS);
14506}
14507
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000014508static bool tg3_10_100_only_device(struct tg3 *tp,
14509 const struct pci_device_id *ent)
14510{
14511 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
14512
14513 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14514 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14515 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14516 return true;
14517
14518 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
14519 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
14520 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
14521 return true;
14522 } else {
14523 return true;
14524 }
14525 }
14526
14527 return false;
14528}
14529
14530static int __devinit tg3_get_invariants(struct tg3 *tp,
14531 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014532{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014533 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534 u32 pci_state_reg, grc_misc_cfg;
14535 u32 val;
14536 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014537 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014538
Linus Torvalds1da177e2005-04-16 15:20:36 -070014539 /* Force memory write invalidate off. If we leave it on,
14540 * then on 5700_BX chips we have to enable a workaround.
14541 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
14542 * to match the cacheline size. The Broadcom driver have this
14543 * workaround but turns MWI off all the times so never uses
14544 * it. This seems to suggest that the workaround is insufficient.
14545 */
14546 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14547 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
14548 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14549
Matt Carlson16821282011-07-13 09:27:28 +000014550 /* Important! -- Make sure register accesses are byteswapped
14551 * correctly. Also, for those chips that require it, make
14552 * sure that indirect register accesses are enabled before
14553 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014554 */
14555 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14556 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000014557 tp->misc_host_ctrl |= (misc_ctrl_reg &
14558 MISC_HOST_CTRL_CHIPREV);
14559 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14560 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014561
Matt Carlson42b123b2012-02-13 15:20:13 +000014562 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070014563
Michael Chan68929142005-08-09 20:17:14 -070014564 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
14565 * we need to disable memory and use config. cycles
14566 * only to access all registers. The 5702/03 chips
14567 * can mistakenly decode the special cycles from the
14568 * ICH chipsets as memory write cycles, causing corruption
14569 * of register and memory space. Only certain ICH bridges
14570 * will drive special cycles with non-zero data during the
14571 * address phase which can fall within the 5703's address
14572 * range. This is not an ICH bug as the PCI spec allows
14573 * non-zero address during special cycles. However, only
14574 * these ICH bridges are known to drive non-zero addresses
14575 * during special cycles.
14576 *
14577 * Since special cycles do not cross PCI bridges, we only
14578 * enable this workaround if the 5703 is on the secondary
14579 * bus of these ICH bridges.
14580 */
14581 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
14582 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
14583 static struct tg3_dev_id {
14584 u32 vendor;
14585 u32 device;
14586 u32 rev;
14587 } ich_chipsets[] = {
14588 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
14589 PCI_ANY_ID },
14590 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14591 PCI_ANY_ID },
14592 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14593 0xa },
14594 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14595 PCI_ANY_ID },
14596 { },
14597 };
14598 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14599 struct pci_dev *bridge = NULL;
14600
14601 while (pci_id->vendor != 0) {
14602 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14603 bridge);
14604 if (!bridge) {
14605 pci_id++;
14606 continue;
14607 }
14608 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014609 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014610 continue;
14611 }
14612 if (bridge->subordinate &&
14613 (bridge->subordinate->number ==
14614 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014615 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014616 pci_dev_put(bridge);
14617 break;
14618 }
14619 }
14620 }
14621
Matt Carlson6ff6f812011-05-19 12:12:54 +000014622 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070014623 static struct tg3_dev_id {
14624 u32 vendor;
14625 u32 device;
14626 } bridge_chipsets[] = {
14627 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14628 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14629 { },
14630 };
14631 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14632 struct pci_dev *bridge = NULL;
14633
14634 while (pci_id->vendor != 0) {
14635 bridge = pci_get_device(pci_id->vendor,
14636 pci_id->device,
14637 bridge);
14638 if (!bridge) {
14639 pci_id++;
14640 continue;
14641 }
14642 if (bridge->subordinate &&
14643 (bridge->subordinate->number <=
14644 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070014645 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070014646 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014647 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070014648 pci_dev_put(bridge);
14649 break;
14650 }
14651 }
14652 }
14653
Michael Chan4a29cc22006-03-19 13:21:12 -080014654 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14655 * DMA addresses > 40-bit. This bridge may have other additional
14656 * 57xx devices behind it in some 4-port NIC designs for example.
14657 * Any tg3 device found behind the bridge will also need the 40-bit
14658 * DMA workaround.
14659 */
Matt Carlson42b123b2012-02-13 15:20:13 +000014660 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014661 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014662 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014663 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014664 struct pci_dev *bridge = NULL;
14665
14666 do {
14667 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14668 PCI_DEVICE_ID_SERVERWORKS_EPB,
14669 bridge);
14670 if (bridge && bridge->subordinate &&
14671 (bridge->subordinate->number <=
14672 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070014673 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080014674 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014675 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014676 pci_dev_put(bridge);
14677 break;
14678 }
14679 } while (bridge);
14680 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014681
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014682 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014683 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014684 tp->pdev_peer = tg3_find_peer(tp);
14685
Matt Carlson507399f2009-11-13 13:03:37 +000014686 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014687 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014688 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014689 else if (tg3_flag(tp, 57765_PLUS))
14690 tg3_flag_set(tp, HW_TSO_3);
14691 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014692 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014693 tg3_flag_set(tp, HW_TSO_2);
14694 else if (tg3_flag(tp, 5750_PLUS)) {
14695 tg3_flag_set(tp, HW_TSO_1);
14696 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014697 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14698 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014699 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014700 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14701 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14702 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014703 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014704 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14705 tp->fw_needed = FIRMWARE_TG3TSO5;
14706 else
14707 tp->fw_needed = FIRMWARE_TG3TSO;
14708 }
14709
Matt Carlsondabc5c62011-05-19 12:12:52 +000014710 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014711 if (tg3_flag(tp, HW_TSO_1) ||
14712 tg3_flag(tp, HW_TSO_2) ||
14713 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014714 tp->fw_needed) {
14715 /* For firmware TSO, assume ASF is disabled.
14716 * We'll disable TSO later if we discover ASF
14717 * is enabled in tg3_get_eeprom_hw_cfg().
14718 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014719 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014720 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014721 tg3_flag_clear(tp, TSO_CAPABLE);
14722 tg3_flag_clear(tp, TSO_BUG);
14723 tp->fw_needed = NULL;
14724 }
14725
14726 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14727 tp->fw_needed = FIRMWARE_TG3;
14728
Matt Carlson507399f2009-11-13 13:03:37 +000014729 tp->irq_max = 1;
14730
Joe Perches63c3a662011-04-26 08:12:10 +000014731 if (tg3_flag(tp, 5750_PLUS)) {
14732 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014733 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14734 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14735 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14736 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14737 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014738 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014739
Joe Perches63c3a662011-04-26 08:12:10 +000014740 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014741 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014742 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014744
Joe Perches63c3a662011-04-26 08:12:10 +000014745 if (tg3_flag(tp, 57765_PLUS)) {
14746 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014747 tp->irq_max = TG3_IRQ_MAX_VECS;
14748 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014749 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014750
Michael Chan91024262012-09-28 07:12:38 +000014751 tp->txq_max = 1;
14752 tp->rxq_max = 1;
14753 if (tp->irq_max > 1) {
14754 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
14755 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
14756
14757 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14758 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14759 tp->txq_max = tp->irq_max - 1;
14760 }
14761
Matt Carlsonb7abee62012-06-07 12:56:54 +000014762 if (tg3_flag(tp, 5755_PLUS) ||
14763 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014764 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014765
Matt Carlsone31aa982011-07-27 14:20:53 +000014766 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014767 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014768
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014769 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14770 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14771 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014772 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014773
Joe Perches63c3a662011-04-26 08:12:10 +000014774 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014775 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014776 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014777
Joe Perches63c3a662011-04-26 08:12:10 +000014778 if (!tg3_flag(tp, 5705_PLUS) ||
14779 tg3_flag(tp, 5780_CLASS) ||
14780 tg3_flag(tp, USE_JUMBO_BDFLAG))
14781 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014782
Matt Carlson52f44902008-11-21 17:17:04 -080014783 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14784 &pci_state_reg);
14785
Jon Mason708ebb32011-06-27 12:56:50 +000014786 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014787 u16 lnkctl;
14788
Joe Perches63c3a662011-04-26 08:12:10 +000014789 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014790
Jiang Liu0f49bfb2012-08-20 13:28:20 -060014791 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014792 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014793 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14794 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014795 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014796 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014797 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014798 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014799 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014800 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14801 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014802 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014803 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014804 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014805 }
Matt Carlson52f44902008-11-21 17:17:04 -080014806 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014807 /* BCM5785 devices are effectively PCIe devices, and should
14808 * follow PCIe codepaths, but do not have a PCIe capabilities
14809 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014810 */
Joe Perches63c3a662011-04-26 08:12:10 +000014811 tg3_flag_set(tp, PCI_EXPRESS);
14812 } else if (!tg3_flag(tp, 5705_PLUS) ||
14813 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014814 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14815 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014816 dev_err(&tp->pdev->dev,
14817 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014818 return -EIO;
14819 }
14820
14821 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014822 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014824
Michael Chan399de502005-10-03 14:02:39 -070014825 /* If we have an AMD 762 or VIA K8T800 chipset, write
14826 * reordering to the mailbox registers done by the host
14827 * controller can cause major troubles. We read back from
14828 * every mailbox register write to force the writes to be
14829 * posted to the chip in order.
14830 */
Matt Carlson41434702011-03-09 16:58:22 +000014831 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014832 !tg3_flag(tp, PCI_EXPRESS))
14833 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014834
Matt Carlson69fc4052008-12-21 20:19:57 -080014835 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14836 &tp->pci_cacheline_sz);
14837 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14838 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014839 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14840 tp->pci_lat_timer < 64) {
14841 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014842 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14843 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014844 }
14845
Matt Carlson16821282011-07-13 09:27:28 +000014846 /* Important! -- It is critical that the PCI-X hw workaround
14847 * situation is decided before the first MMIO register access.
14848 */
Matt Carlson52f44902008-11-21 17:17:04 -080014849 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14850 /* 5700 BX chips need to have their TX producer index
14851 * mailboxes written twice to workaround a bug.
14852 */
Joe Perches63c3a662011-04-26 08:12:10 +000014853 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014854
Matt Carlson52f44902008-11-21 17:17:04 -080014855 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014856 *
14857 * The workaround is to use indirect register accesses
14858 * for all chip writes not to mailbox registers.
14859 */
Joe Perches63c3a662011-04-26 08:12:10 +000014860 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014861 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014862
Joe Perches63c3a662011-04-26 08:12:10 +000014863 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014864
14865 /* The chip can have it's power management PCI config
14866 * space registers clobbered due to this bug.
14867 * So explicitly force the chip into D0 here.
14868 */
Matt Carlson9974a352007-10-07 23:27:28 -070014869 pci_read_config_dword(tp->pdev,
14870 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014871 &pm_reg);
14872 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14873 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014874 pci_write_config_dword(tp->pdev,
14875 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014876 pm_reg);
14877
14878 /* Also, force SERR#/PERR# in PCI command. */
14879 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14880 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14881 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14882 }
14883 }
14884
Linus Torvalds1da177e2005-04-16 15:20:36 -070014885 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014886 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014887 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014888 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014889
14890 /* Chip-specific fixup from Broadcom driver */
14891 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14892 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14893 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14894 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14895 }
14896
Michael Chan1ee582d2005-08-09 20:16:46 -070014897 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014898 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014899 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014900 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014901 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014902 tp->write32_tx_mbox = tg3_write32;
14903 tp->write32_rx_mbox = tg3_write32;
14904
14905 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014906 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014907 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014908 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014909 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014910 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14911 /*
14912 * Back to back register writes can cause problems on these
14913 * chips, the workaround is to read back all reg writes
14914 * except those to mailbox regs.
14915 *
14916 * See tg3_write_indirect_reg32().
14917 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014918 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014919 }
14920
Joe Perches63c3a662011-04-26 08:12:10 +000014921 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014922 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014923 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014924 tp->write32_rx_mbox = tg3_write_flush_reg32;
14925 }
Michael Chan20094932005-08-09 20:16:32 -070014926
Joe Perches63c3a662011-04-26 08:12:10 +000014927 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014928 tp->read32 = tg3_read_indirect_reg32;
14929 tp->write32 = tg3_write_indirect_reg32;
14930 tp->read32_mbox = tg3_read_indirect_mbox;
14931 tp->write32_mbox = tg3_write_indirect_mbox;
14932 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14933 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14934
14935 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014936 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014937
14938 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14939 pci_cmd &= ~PCI_COMMAND_MEMORY;
14940 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14941 }
Michael Chanb5d37722006-09-27 16:06:21 -070014942 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14943 tp->read32_mbox = tg3_read32_mbox_5906;
14944 tp->write32_mbox = tg3_write32_mbox_5906;
14945 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14946 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14947 }
Michael Chan68929142005-08-09 20:17:14 -070014948
Michael Chanbbadf502006-04-06 21:46:34 -070014949 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014950 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014951 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014952 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014953 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014954
Matt Carlson16821282011-07-13 09:27:28 +000014955 /* The memory arbiter has to be enabled in order for SRAM accesses
14956 * to succeed. Normally on powerup the tg3 chip firmware will make
14957 * sure it is enabled, but other entities such as system netboot
14958 * code might disable it.
14959 */
14960 val = tr32(MEMARB_MODE);
14961 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14962
Matt Carlson9dc5e342011-11-04 09:15:02 +000014963 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14965 tg3_flag(tp, 5780_CLASS)) {
14966 if (tg3_flag(tp, PCIX_MODE)) {
14967 pci_read_config_dword(tp->pdev,
14968 tp->pcix_cap + PCI_X_STATUS,
14969 &val);
14970 tp->pci_fn = val & 0x7;
14971 }
14972 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14973 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14974 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14975 NIC_SRAM_CPMUSTAT_SIG) {
14976 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14977 tp->pci_fn = tp->pci_fn ? 1 : 0;
14978 }
14979 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14980 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14981 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14982 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14983 NIC_SRAM_CPMUSTAT_SIG) {
14984 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14985 TG3_CPMU_STATUS_FSHFT_5719;
14986 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014987 }
14988
Michael Chan7d0c41e2005-04-21 17:06:20 -070014989 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014990 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014991 * determined before calling tg3_set_power_state() so that
14992 * we know whether or not to switch out of Vaux power.
14993 * When the flag is set, it means that GPIO1 is used for eeprom
14994 * write protect and also implies that it is a LOM where GPIOs
14995 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014996 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014997 tg3_get_eeprom_hw_cfg(tp);
14998
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014999 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
15000 tg3_flag_clear(tp, TSO_CAPABLE);
15001 tg3_flag_clear(tp, TSO_BUG);
15002 tp->fw_needed = NULL;
15003 }
15004
Joe Perches63c3a662011-04-26 08:12:10 +000015005 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070015006 /* Allow reads and writes to the
15007 * APE register and memory space.
15008 */
15009 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000015010 PCISTATE_ALLOW_APE_SHMEM_WR |
15011 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015012 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
15013 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000015014
15015 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015016 }
15017
Matt Carlson16821282011-07-13 09:27:28 +000015018 /* Set up tp->grc_local_ctrl before calling
15019 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
15020 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070015021 * It is also used as eeprom write protect on LOMs.
15022 */
15023 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000015024 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015025 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070015026 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
15027 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070015028 /* Unused GPIO3 must be driven as output on 5752 because there
15029 * are no pull-up resistors on unused GPIO pins.
15030 */
15031 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
15032 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070015033
Matt Carlson321d32a2008-11-21 17:22:19 -080015034 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000015035 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015036 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080015037 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
15038
Matt Carlson8d519ab2009-04-20 06:58:01 +000015039 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15040 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015041 /* Turn off the debug UART. */
15042 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000015043 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015044 /* Keep VMain power. */
15045 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
15046 GRC_LCLCTRL_GPIO_OUTPUT0;
15047 }
15048
Matt Carlson16821282011-07-13 09:27:28 +000015049 /* Switch out of Vaux if it is a NIC */
15050 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015051
Linus Torvalds1da177e2005-04-16 15:20:36 -070015052 /* Derive initial jumbo mode from MTU assigned in
15053 * ether_setup() via the alloc_etherdev() call
15054 */
Joe Perches63c3a662011-04-26 08:12:10 +000015055 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
15056 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015057
15058 /* Determine WakeOnLan speed to use. */
15059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15060 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
15061 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
15062 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000015063 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015064 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015065 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015066 }
15067
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015068 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015069 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015070
Linus Torvalds1da177e2005-04-16 15:20:36 -070015071 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015072 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15073 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015074 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070015075 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015076 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
15077 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15078 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015079
15080 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
15081 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015082 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015083 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015084 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015085
Joe Perches63c3a662011-04-26 08:12:10 +000015086 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015087 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080015088 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015089 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015090 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070015091 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070015092 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070015093 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
15094 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080015095 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
15096 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015097 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080015098 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015099 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080015100 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015101 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070015102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015103
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015104 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15105 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
15106 tp->phy_otp = tg3_read_otp_phycfg(tp);
15107 if (tp->phy_otp == 0)
15108 tp->phy_otp = TG3_OTP_DEFAULT;
15109 }
15110
Joe Perches63c3a662011-04-26 08:12:10 +000015111 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070015112 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
15113 else
15114 tp->mi_mode = MAC_MI_MODE_BASE;
15115
Linus Torvalds1da177e2005-04-16 15:20:36 -070015116 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015117 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
15118 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
15119 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
15120
Matt Carlson4d958472011-04-20 07:57:35 +000015121 /* Set these bits to enable statistics workaround. */
15122 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
15123 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
15124 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
15125 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
15126 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
15127 }
15128
Matt Carlson321d32a2008-11-21 17:22:19 -080015129 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
15130 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000015131 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070015132
Matt Carlson158d7ab2008-05-29 01:37:54 -070015133 err = tg3_mdio_init(tp);
15134 if (err)
15135 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015136
15137 /* Initialize data/descriptor byte/word swapping. */
15138 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000015139 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
15140 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
15141 GRC_MODE_WORD_SWAP_B2HRX_DATA |
15142 GRC_MODE_B2HRX_ENABLE |
15143 GRC_MODE_HTX2B_ENABLE |
15144 GRC_MODE_HOST_STACKUP);
15145 else
15146 val &= GRC_MODE_HOST_STACKUP;
15147
Linus Torvalds1da177e2005-04-16 15:20:36 -070015148 tw32(GRC_MODE, val | tp->grc_mode);
15149
15150 tg3_switch_clocks(tp);
15151
15152 /* Clear this out for sanity. */
15153 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
15154
15155 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15156 &pci_state_reg);
15157 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015158 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015159 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
15160
15161 if (chiprevid == CHIPREV_ID_5701_A0 ||
15162 chiprevid == CHIPREV_ID_5701_B0 ||
15163 chiprevid == CHIPREV_ID_5701_B2 ||
15164 chiprevid == CHIPREV_ID_5701_B5) {
15165 void __iomem *sram_base;
15166
15167 /* Write some dummy words into the SRAM status block
15168 * area, see if it reads back correctly. If the return
15169 * value is bad, force enable the PCIX workaround.
15170 */
15171 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
15172
15173 writel(0x00000000, sram_base);
15174 writel(0x00000000, sram_base + 4);
15175 writel(0xffffffff, sram_base + 4);
15176 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000015177 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015178 }
15179 }
15180
15181 udelay(50);
15182 tg3_nvram_init(tp);
15183
15184 grc_misc_cfg = tr32(GRC_MISC_CFG);
15185 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
15186
Linus Torvalds1da177e2005-04-16 15:20:36 -070015187 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
15188 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
15189 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000015190 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015191
Joe Perches63c3a662011-04-26 08:12:10 +000015192 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000015193 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015194 tg3_flag_set(tp, TAGGED_STATUS);
15195 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070015196 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
15197 HOSTCC_MODE_CLRTICK_TXBD);
15198
15199 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
15200 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15201 tp->misc_host_ctrl);
15202 }
15203
Matt Carlson3bda1252008-08-15 14:08:22 -070015204 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000015205 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000015206 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070015207 else
Matt Carlson6e01b202011-08-19 13:58:20 +000015208 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070015209
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015210 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015211 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015212
15213 err = tg3_phy_probe(tp);
15214 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015215 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015216 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015217 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015218 }
15219
Matt Carlson184b8902010-04-05 10:19:25 +000015220 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080015221 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015222
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015223 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
15224 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015225 } else {
15226 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015227 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015228 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015229 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015230 }
15231
15232 /* 5700 {AX,BX} chips have a broken status block link
15233 * change bit implementation, so we must use the
15234 * status register in those cases.
15235 */
15236 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015237 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015238 else
Joe Perches63c3a662011-04-26 08:12:10 +000015239 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015240
15241 /* The led_ctrl is set during tg3_phy_probe, here we might
15242 * have to force the link status polling mechanism based
15243 * upon subsystem IDs.
15244 */
15245 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070015246 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015247 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
15248 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000015249 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015250 }
15251
15252 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015253 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000015254 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015255 else
Joe Perches63c3a662011-04-26 08:12:10 +000015256 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015257
Eric Dumazet9205fd92011-11-18 06:47:01 +000015258 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015259 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015260 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015261 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000015262 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015263#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000015264 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015265#endif
15266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015267
Matt Carlson2c49a442010-09-30 10:34:35 +000015268 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
15269 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015270 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
15271
Matt Carlson2c49a442010-09-30 10:34:35 +000015272 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070015273
15274 /* Increment the rx prod index on the rx std ring by at most
15275 * 8 for these chips to workaround hw errata.
15276 */
15277 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
15278 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
15279 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
15280 tp->rx_std_max_post = 8;
15281
Joe Perches63c3a662011-04-26 08:12:10 +000015282 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070015283 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
15284 PCIE_PWR_MGMT_L1_THRESH_MSK;
15285
Linus Torvalds1da177e2005-04-16 15:20:36 -070015286 return err;
15287}
15288
David S. Miller49b6e95f2007-03-29 01:38:42 -070015289#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015290static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
15291{
15292 struct net_device *dev = tp->dev;
15293 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015294 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070015295 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015296 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015297
David S. Miller49b6e95f2007-03-29 01:38:42 -070015298 addr = of_get_property(dp, "local-mac-address", &len);
15299 if (addr && len == 6) {
15300 memcpy(dev->dev_addr, addr, 6);
15301 memcpy(dev->perm_addr, dev->dev_addr, 6);
15302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015303 }
15304 return -ENODEV;
15305}
15306
15307static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
15308{
15309 struct net_device *dev = tp->dev;
15310
15311 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070015312 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015313 return 0;
15314}
15315#endif
15316
15317static int __devinit tg3_get_device_address(struct tg3 *tp)
15318{
15319 struct net_device *dev = tp->dev;
15320 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080015321 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015322
David S. Miller49b6e95f2007-03-29 01:38:42 -070015323#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015324 if (!tg3_get_macaddr_sparc(tp))
15325 return 0;
15326#endif
15327
15328 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000015329 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015330 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015331 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
15332 mac_offset = 0xcc;
15333 if (tg3_nvram_lock(tp))
15334 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
15335 else
15336 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015337 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000015338 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000015339 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000015340 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000015341 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000015342 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070015343 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015344
15345 /* First try to get it from MAC address mailbox. */
15346 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
15347 if ((hi >> 16) == 0x484b) {
15348 dev->dev_addr[0] = (hi >> 8) & 0xff;
15349 dev->dev_addr[1] = (hi >> 0) & 0xff;
15350
15351 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
15352 dev->dev_addr[2] = (lo >> 24) & 0xff;
15353 dev->dev_addr[3] = (lo >> 16) & 0xff;
15354 dev->dev_addr[4] = (lo >> 8) & 0xff;
15355 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015356
Michael Chan008652b2006-03-27 23:14:53 -080015357 /* Some old bootcode may report a 0 MAC address in SRAM */
15358 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
15359 }
15360 if (!addr_ok) {
15361 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000015362 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000015363 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000015364 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070015365 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
15366 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080015367 }
15368 /* Finally just fetch it out of the MAC control regs. */
15369 else {
15370 hi = tr32(MAC_ADDR_0_HIGH);
15371 lo = tr32(MAC_ADDR_0_LOW);
15372
15373 dev->dev_addr[5] = lo & 0xff;
15374 dev->dev_addr[4] = (lo >> 8) & 0xff;
15375 dev->dev_addr[3] = (lo >> 16) & 0xff;
15376 dev->dev_addr[2] = (lo >> 24) & 0xff;
15377 dev->dev_addr[1] = hi & 0xff;
15378 dev->dev_addr[0] = (hi >> 8) & 0xff;
15379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015380 }
15381
15382 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070015383#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015384 if (!tg3_get_default_macaddr_sparc(tp))
15385 return 0;
15386#endif
15387 return -EINVAL;
15388 }
John W. Linville2ff43692005-09-12 14:44:20 -070015389 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015390 return 0;
15391}
15392
David S. Miller59e6b432005-05-18 22:50:10 -070015393#define BOUNDARY_SINGLE_CACHELINE 1
15394#define BOUNDARY_MULTI_CACHELINE 2
15395
15396static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
15397{
15398 int cacheline_size;
15399 u8 byte;
15400 int goal;
15401
15402 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
15403 if (byte == 0)
15404 cacheline_size = 1024;
15405 else
15406 cacheline_size = (int) byte * 4;
15407
15408 /* On 5703 and later chips, the boundary bits have no
15409 * effect.
15410 */
15411 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15412 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015413 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070015414 goto out;
15415
15416#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
15417 goal = BOUNDARY_MULTI_CACHELINE;
15418#else
15419#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
15420 goal = BOUNDARY_SINGLE_CACHELINE;
15421#else
15422 goal = 0;
15423#endif
15424#endif
15425
Joe Perches63c3a662011-04-26 08:12:10 +000015426 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015427 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
15428 goto out;
15429 }
15430
David S. Miller59e6b432005-05-18 22:50:10 -070015431 if (!goal)
15432 goto out;
15433
15434 /* PCI controllers on most RISC systems tend to disconnect
15435 * when a device tries to burst across a cache-line boundary.
15436 * Therefore, letting tg3 do so just wastes PCI bandwidth.
15437 *
15438 * Unfortunately, for PCI-E there are only limited
15439 * write-side controls for this, and thus for reads
15440 * we will still get the disconnects. We'll also waste
15441 * these PCI cycles for both read and write for chips
15442 * other than 5700 and 5701 which do not implement the
15443 * boundary bits.
15444 */
Joe Perches63c3a662011-04-26 08:12:10 +000015445 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015446 switch (cacheline_size) {
15447 case 16:
15448 case 32:
15449 case 64:
15450 case 128:
15451 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15452 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
15453 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
15454 } else {
15455 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15456 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15457 }
15458 break;
15459
15460 case 256:
15461 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
15462 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
15463 break;
15464
15465 default:
15466 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15467 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15468 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015469 }
Joe Perches63c3a662011-04-26 08:12:10 +000015470 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015471 switch (cacheline_size) {
15472 case 16:
15473 case 32:
15474 case 64:
15475 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15476 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15477 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
15478 break;
15479 }
15480 /* fallthrough */
15481 case 128:
15482 default:
15483 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15484 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
15485 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015486 }
David S. Miller59e6b432005-05-18 22:50:10 -070015487 } else {
15488 switch (cacheline_size) {
15489 case 16:
15490 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15491 val |= (DMA_RWCTRL_READ_BNDRY_16 |
15492 DMA_RWCTRL_WRITE_BNDRY_16);
15493 break;
15494 }
15495 /* fallthrough */
15496 case 32:
15497 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15498 val |= (DMA_RWCTRL_READ_BNDRY_32 |
15499 DMA_RWCTRL_WRITE_BNDRY_32);
15500 break;
15501 }
15502 /* fallthrough */
15503 case 64:
15504 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15505 val |= (DMA_RWCTRL_READ_BNDRY_64 |
15506 DMA_RWCTRL_WRITE_BNDRY_64);
15507 break;
15508 }
15509 /* fallthrough */
15510 case 128:
15511 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15512 val |= (DMA_RWCTRL_READ_BNDRY_128 |
15513 DMA_RWCTRL_WRITE_BNDRY_128);
15514 break;
15515 }
15516 /* fallthrough */
15517 case 256:
15518 val |= (DMA_RWCTRL_READ_BNDRY_256 |
15519 DMA_RWCTRL_WRITE_BNDRY_256);
15520 break;
15521 case 512:
15522 val |= (DMA_RWCTRL_READ_BNDRY_512 |
15523 DMA_RWCTRL_WRITE_BNDRY_512);
15524 break;
15525 case 1024:
15526 default:
15527 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
15528 DMA_RWCTRL_WRITE_BNDRY_1024);
15529 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015530 }
David S. Miller59e6b432005-05-18 22:50:10 -070015531 }
15532
15533out:
15534 return val;
15535}
15536
Linus Torvalds1da177e2005-04-16 15:20:36 -070015537static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
15538{
15539 struct tg3_internal_buffer_desc test_desc;
15540 u32 sram_dma_descs;
15541 int i, ret;
15542
15543 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
15544
15545 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15546 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15547 tw32(RDMAC_STATUS, 0);
15548 tw32(WDMAC_STATUS, 0);
15549
15550 tw32(BUFMGR_MODE, 0);
15551 tw32(FTQ_RESET, 0);
15552
15553 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15554 test_desc.addr_lo = buf_dma & 0xffffffff;
15555 test_desc.nic_mbuf = 0x00002100;
15556 test_desc.len = size;
15557
15558 /*
15559 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15560 * the *second* time the tg3 driver was getting loaded after an
15561 * initial scan.
15562 *
15563 * Broadcom tells me:
15564 * ...the DMA engine is connected to the GRC block and a DMA
15565 * reset may affect the GRC block in some unpredictable way...
15566 * The behavior of resets to individual blocks has not been tested.
15567 *
15568 * Broadcom noted the GRC reset will also reset all sub-components.
15569 */
15570 if (to_device) {
15571 test_desc.cqid_sqid = (13 << 8) | 2;
15572
15573 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15574 udelay(40);
15575 } else {
15576 test_desc.cqid_sqid = (16 << 8) | 7;
15577
15578 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15579 udelay(40);
15580 }
15581 test_desc.flags = 0x00000005;
15582
15583 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15584 u32 val;
15585
15586 val = *(((u32 *)&test_desc) + i);
15587 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15588 sram_dma_descs + (i * sizeof(u32)));
15589 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15590 }
15591 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15592
Matt Carlson859a588792010-04-05 10:19:28 +000015593 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015594 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015595 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015596 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015597
15598 ret = -ENODEV;
15599 for (i = 0; i < 40; i++) {
15600 u32 val;
15601
15602 if (to_device)
15603 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15604 else
15605 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15606 if ((val & 0xffff) == sram_dma_descs) {
15607 ret = 0;
15608 break;
15609 }
15610
15611 udelay(100);
15612 }
15613
15614 return ret;
15615}
15616
David S. Millerded73402005-05-23 13:59:47 -070015617#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015618
Matt Carlson41434702011-03-09 16:58:22 +000015619static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015620 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15621 { },
15622};
15623
Linus Torvalds1da177e2005-04-16 15:20:36 -070015624static int __devinit tg3_test_dma(struct tg3 *tp)
15625{
15626 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015627 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015628 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015629
Matt Carlson4bae65c2010-11-24 08:31:52 +000015630 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15631 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015632 if (!buf) {
15633 ret = -ENOMEM;
15634 goto out_nofree;
15635 }
15636
15637 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15638 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15639
David S. Miller59e6b432005-05-18 22:50:10 -070015640 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015641
Joe Perches63c3a662011-04-26 08:12:10 +000015642 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015643 goto out;
15644
Joe Perches63c3a662011-04-26 08:12:10 +000015645 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015646 /* DMA read watermark not used on PCIE */
15647 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015648 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015649 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15650 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015651 tp->dma_rwctrl |= 0x003f0000;
15652 else
15653 tp->dma_rwctrl |= 0x003f000f;
15654 } else {
15655 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15656 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15657 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015658 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015659
Michael Chan4a29cc22006-03-19 13:21:12 -080015660 /* If the 5704 is behind the EPB bridge, we can
15661 * do the less restrictive ONE_DMA workaround for
15662 * better performance.
15663 */
Joe Perches63c3a662011-04-26 08:12:10 +000015664 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015665 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15666 tp->dma_rwctrl |= 0x8000;
15667 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015668 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15669
Michael Chan49afdeb2007-02-13 12:17:03 -080015670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15671 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015672 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015673 tp->dma_rwctrl |=
15674 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15675 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15676 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015677 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15678 /* 5780 always in PCIX mode */
15679 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015680 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15681 /* 5714 always in PCIX mode */
15682 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015683 } else {
15684 tp->dma_rwctrl |= 0x001b000f;
15685 }
15686 }
15687
15688 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15689 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15690 tp->dma_rwctrl &= 0xfffffff0;
15691
15692 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15693 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15694 /* Remove this if it causes problems for some boards. */
15695 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15696
15697 /* On 5700/5701 chips, we need to set this bit.
15698 * Otherwise the chip will issue cacheline transactions
15699 * to streamable DMA memory with not all the byte
15700 * enables turned on. This is an error on several
15701 * RISC PCI controllers, in particular sparc64.
15702 *
15703 * On 5703/5704 chips, this bit has been reassigned
15704 * a different meaning. In particular, it is used
15705 * on those chips to enable a PCI-X workaround.
15706 */
15707 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15708 }
15709
15710 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15711
15712#if 0
15713 /* Unneeded, already done by tg3_get_invariants. */
15714 tg3_switch_clocks(tp);
15715#endif
15716
Linus Torvalds1da177e2005-04-16 15:20:36 -070015717 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15718 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15719 goto out;
15720
David S. Miller59e6b432005-05-18 22:50:10 -070015721 /* It is best to perform DMA test with maximum write burst size
15722 * to expose the 5700/5701 write DMA bug.
15723 */
15724 saved_dma_rwctrl = tp->dma_rwctrl;
15725 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15726 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15727
Linus Torvalds1da177e2005-04-16 15:20:36 -070015728 while (1) {
15729 u32 *p = buf, i;
15730
15731 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15732 p[i] = i;
15733
15734 /* Send the buffer to the chip. */
15735 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15736 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015737 dev_err(&tp->pdev->dev,
15738 "%s: Buffer write failed. err = %d\n",
15739 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015740 break;
15741 }
15742
15743#if 0
15744 /* validate data reached card RAM correctly. */
15745 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15746 u32 val;
15747 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15748 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015749 dev_err(&tp->pdev->dev,
15750 "%s: Buffer corrupted on device! "
15751 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015752 /* ret = -ENODEV here? */
15753 }
15754 p[i] = 0;
15755 }
15756#endif
15757 /* Now read it back. */
15758 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15759 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015760 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15761 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015762 break;
15763 }
15764
15765 /* Verify it. */
15766 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15767 if (p[i] == i)
15768 continue;
15769
David S. Miller59e6b432005-05-18 22:50:10 -070015770 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15771 DMA_RWCTRL_WRITE_BNDRY_16) {
15772 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015773 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15774 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15775 break;
15776 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015777 dev_err(&tp->pdev->dev,
15778 "%s: Buffer corrupted on read back! "
15779 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015780 ret = -ENODEV;
15781 goto out;
15782 }
15783 }
15784
15785 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15786 /* Success. */
15787 ret = 0;
15788 break;
15789 }
15790 }
David S. Miller59e6b432005-05-18 22:50:10 -070015791 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15792 DMA_RWCTRL_WRITE_BNDRY_16) {
15793 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015794 * now look for chipsets that are known to expose the
15795 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015796 */
Matt Carlson41434702011-03-09 16:58:22 +000015797 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015798 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15799 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015800 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015801 /* Safe to use the calculated DMA boundary. */
15802 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015803 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015804
David S. Miller59e6b432005-05-18 22:50:10 -070015805 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015807
15808out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015809 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015810out_nofree:
15811 return ret;
15812}
15813
Linus Torvalds1da177e2005-04-16 15:20:36 -070015814static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15815{
Joe Perches63c3a662011-04-26 08:12:10 +000015816 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015817 tp->bufmgr_config.mbuf_read_dma_low_water =
15818 DEFAULT_MB_RDMA_LOW_WATER_5705;
15819 tp->bufmgr_config.mbuf_mac_rx_low_water =
15820 DEFAULT_MB_MACRX_LOW_WATER_57765;
15821 tp->bufmgr_config.mbuf_high_water =
15822 DEFAULT_MB_HIGH_WATER_57765;
15823
15824 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15825 DEFAULT_MB_RDMA_LOW_WATER_5705;
15826 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15827 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15828 tp->bufmgr_config.mbuf_high_water_jumbo =
15829 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015830 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015831 tp->bufmgr_config.mbuf_read_dma_low_water =
15832 DEFAULT_MB_RDMA_LOW_WATER_5705;
15833 tp->bufmgr_config.mbuf_mac_rx_low_water =
15834 DEFAULT_MB_MACRX_LOW_WATER_5705;
15835 tp->bufmgr_config.mbuf_high_water =
15836 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015837 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15838 tp->bufmgr_config.mbuf_mac_rx_low_water =
15839 DEFAULT_MB_MACRX_LOW_WATER_5906;
15840 tp->bufmgr_config.mbuf_high_water =
15841 DEFAULT_MB_HIGH_WATER_5906;
15842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015843
Michael Chanfdfec1722005-07-25 12:31:48 -070015844 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15845 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15846 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15847 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15848 tp->bufmgr_config.mbuf_high_water_jumbo =
15849 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15850 } else {
15851 tp->bufmgr_config.mbuf_read_dma_low_water =
15852 DEFAULT_MB_RDMA_LOW_WATER;
15853 tp->bufmgr_config.mbuf_mac_rx_low_water =
15854 DEFAULT_MB_MACRX_LOW_WATER;
15855 tp->bufmgr_config.mbuf_high_water =
15856 DEFAULT_MB_HIGH_WATER;
15857
15858 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15859 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15860 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15861 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15862 tp->bufmgr_config.mbuf_high_water_jumbo =
15863 DEFAULT_MB_HIGH_WATER_JUMBO;
15864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015865
15866 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15867 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15868}
15869
15870static char * __devinit tg3_phy_string(struct tg3 *tp)
15871{
Matt Carlson79eb6902010-02-17 15:17:03 +000015872 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15873 case TG3_PHY_ID_BCM5400: return "5400";
15874 case TG3_PHY_ID_BCM5401: return "5401";
15875 case TG3_PHY_ID_BCM5411: return "5411";
15876 case TG3_PHY_ID_BCM5701: return "5701";
15877 case TG3_PHY_ID_BCM5703: return "5703";
15878 case TG3_PHY_ID_BCM5704: return "5704";
15879 case TG3_PHY_ID_BCM5705: return "5705";
15880 case TG3_PHY_ID_BCM5750: return "5750";
15881 case TG3_PHY_ID_BCM5752: return "5752";
15882 case TG3_PHY_ID_BCM5714: return "5714";
15883 case TG3_PHY_ID_BCM5780: return "5780";
15884 case TG3_PHY_ID_BCM5755: return "5755";
15885 case TG3_PHY_ID_BCM5787: return "5787";
15886 case TG3_PHY_ID_BCM5784: return "5784";
15887 case TG3_PHY_ID_BCM5756: return "5722/5756";
15888 case TG3_PHY_ID_BCM5906: return "5906";
15889 case TG3_PHY_ID_BCM5761: return "5761";
15890 case TG3_PHY_ID_BCM5718C: return "5718C";
15891 case TG3_PHY_ID_BCM5718S: return "5718S";
15892 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015893 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015894 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015895 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015896 case 0: return "serdes";
15897 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015899}
15900
Michael Chanf9804dd2005-09-27 12:13:10 -070015901static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15902{
Joe Perches63c3a662011-04-26 08:12:10 +000015903 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015904 strcpy(str, "PCI Express");
15905 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015906 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015907 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15908
15909 strcpy(str, "PCIX:");
15910
15911 if ((clock_ctrl == 7) ||
15912 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15913 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15914 strcat(str, "133MHz");
15915 else if (clock_ctrl == 0)
15916 strcat(str, "33MHz");
15917 else if (clock_ctrl == 2)
15918 strcat(str, "50MHz");
15919 else if (clock_ctrl == 4)
15920 strcat(str, "66MHz");
15921 else if (clock_ctrl == 6)
15922 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015923 } else {
15924 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015925 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015926 strcat(str, "66MHz");
15927 else
15928 strcat(str, "33MHz");
15929 }
Joe Perches63c3a662011-04-26 08:12:10 +000015930 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015931 strcat(str, ":32-bit");
15932 else
15933 strcat(str, ":64-bit");
15934 return str;
15935}
15936
David S. Miller15f98502005-05-18 22:49:26 -070015937static void __devinit tg3_init_coal(struct tg3 *tp)
15938{
15939 struct ethtool_coalesce *ec = &tp->coal;
15940
15941 memset(ec, 0, sizeof(*ec));
15942 ec->cmd = ETHTOOL_GCOALESCE;
15943 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15944 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15945 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15946 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15947 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15948 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15949 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15950 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15951 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15952
15953 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15954 HOSTCC_MODE_CLRTICK_TXBD)) {
15955 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15956 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15957 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15958 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15959 }
Michael Chand244c892005-07-05 14:42:33 -070015960
Joe Perches63c3a662011-04-26 08:12:10 +000015961 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015962 ec->rx_coalesce_usecs_irq = 0;
15963 ec->tx_coalesce_usecs_irq = 0;
15964 ec->stats_block_coalesce_usecs = 0;
15965 }
David S. Miller15f98502005-05-18 22:49:26 -070015966}
15967
Linus Torvalds1da177e2005-04-16 15:20:36 -070015968static int __devinit tg3_init_one(struct pci_dev *pdev,
15969 const struct pci_device_id *ent)
15970{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015971 struct net_device *dev;
15972 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015973 int i, err, pm_cap;
15974 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015975 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015976 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015977 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015978
Joe Perches05dbe002010-02-17 19:44:19 +000015979 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015980
15981 err = pci_enable_device(pdev);
15982 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015983 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015984 return err;
15985 }
15986
Linus Torvalds1da177e2005-04-16 15:20:36 -070015987 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15988 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015989 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015990 goto err_out_disable_pdev;
15991 }
15992
15993 pci_set_master(pdev);
15994
15995 /* Find power-management capability. */
15996 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15997 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015998 dev_err(&pdev->dev,
15999 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016000 err = -EIO;
16001 goto err_out_free_res;
16002 }
16003
Matt Carlson16821282011-07-13 09:27:28 +000016004 err = pci_set_power_state(pdev, PCI_D0);
16005 if (err) {
16006 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
16007 goto err_out_free_res;
16008 }
16009
Matt Carlsonfe5f5782009-09-01 13:09:39 +000016010 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016011 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016012 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000016013 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016014 }
16015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016016 SET_NETDEV_DEV(dev, &pdev->dev);
16017
Linus Torvalds1da177e2005-04-16 15:20:36 -070016018 tp = netdev_priv(dev);
16019 tp->pdev = pdev;
16020 tp->dev = dev;
16021 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016022 tp->rx_mode = TG3_DEF_RX_MODE;
16023 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070016024
Linus Torvalds1da177e2005-04-16 15:20:36 -070016025 if (tg3_debug > 0)
16026 tp->msg_enable = tg3_debug;
16027 else
16028 tp->msg_enable = TG3_DEF_MSG_ENABLE;
16029
16030 /* The word/byte swap controls here control register access byte
16031 * swapping. DMA data byte swapping is controlled in the GRC_MODE
16032 * setting below.
16033 */
16034 tp->misc_host_ctrl =
16035 MISC_HOST_CTRL_MASK_PCI_INT |
16036 MISC_HOST_CTRL_WORD_SWAP |
16037 MISC_HOST_CTRL_INDIR_ACCESS |
16038 MISC_HOST_CTRL_PCISTATE_RW;
16039
16040 /* The NONFRM (non-frame) byte/word swap controls take effect
16041 * on descriptor entries, anything which isn't packet data.
16042 *
16043 * The StrongARM chips on the board (one for tx, one for rx)
16044 * are running in big-endian mode.
16045 */
16046 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
16047 GRC_MODE_WSWAP_NONFRM_DATA);
16048#ifdef __BIG_ENDIAN
16049 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
16050#endif
16051 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016052 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000016053 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016054
Matt Carlsond5fe4882008-11-21 17:20:32 -080016055 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010016056 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016057 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016058 err = -ENOMEM;
16059 goto err_out_free_dev;
16060 }
16061
Matt Carlsonc9cab242011-07-13 09:27:27 +000016062 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16063 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
16064 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
16065 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
16066 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016067 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000016068 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16069 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
16070 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
16071 tg3_flag_set(tp, ENABLE_APE);
16072 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
16073 if (!tp->aperegs) {
16074 dev_err(&pdev->dev,
16075 "Cannot map APE registers, aborting\n");
16076 err = -ENOMEM;
16077 goto err_out_iounmap;
16078 }
16079 }
16080
Linus Torvalds1da177e2005-04-16 15:20:36 -070016081 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
16082 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016083
Linus Torvalds1da177e2005-04-16 15:20:36 -070016084 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016085 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000016086 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016087 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016088
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016089 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016090 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016091 dev_err(&pdev->dev,
16092 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016093 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016094 }
16095
Michael Chan4a29cc22006-03-19 13:21:12 -080016096 /* The EPB bridge inside 5714, 5715, and 5780 and any
16097 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080016098 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
16099 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
16100 * do DMA address check in tg3_start_xmit().
16101 */
Joe Perches63c3a662011-04-26 08:12:10 +000016102 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070016103 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000016104 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070016105 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080016106#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070016107 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016108#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080016109 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070016110 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016111
16112 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070016113 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080016114 err = pci_set_dma_mask(pdev, dma_mask);
16115 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000016116 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080016117 err = pci_set_consistent_dma_mask(pdev,
16118 persist_dma_mask);
16119 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016120 dev_err(&pdev->dev, "Unable to obtain 64 bit "
16121 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016122 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016123 }
16124 }
16125 }
Yang Hongyang284901a2009-04-06 19:01:15 -070016126 if (err || dma_mask == DMA_BIT_MASK(32)) {
16127 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080016128 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016129 dev_err(&pdev->dev,
16130 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016131 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016132 }
16133 }
16134
Michael Chanfdfec1722005-07-25 12:31:48 -070016135 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016136
Matt Carlson0da06062011-05-19 12:12:53 +000016137 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
16138
16139 /* 5700 B0 chips do not support checksumming correctly due
16140 * to hardware bugs.
16141 */
16142 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
16143 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
16144
16145 if (tg3_flag(tp, 5755_PLUS))
16146 features |= NETIF_F_IPV6_CSUM;
16147 }
16148
Michael Chan4e3a7aa2006-03-20 17:47:44 -080016149 /* TSO is on by default on chips that support hardware TSO.
16150 * Firmware TSO on older chips gives lower performance, so it
16151 * is off by default, but can be enabled using ethtool.
16152 */
Joe Perches63c3a662011-04-26 08:12:10 +000016153 if ((tg3_flag(tp, HW_TSO_1) ||
16154 tg3_flag(tp, HW_TSO_2) ||
16155 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000016156 (features & NETIF_F_IP_CSUM))
16157 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000016158 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000016159 if (features & NETIF_F_IPV6_CSUM)
16160 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000016161 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000016162 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070016163 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
16164 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000016165 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000016166 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000016167 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070016168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016169
Matt Carlsond542fe22011-05-19 16:02:43 +000016170 dev->features |= features;
16171 dev->vlan_features |= features;
16172
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016173 /*
16174 * Add loopback capability only for a subset of devices that support
16175 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
16176 * loopback for the remaining devices.
16177 */
16178 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
16179 !tg3_flag(tp, CPMU_PRESENT))
16180 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000016181 features |= NETIF_F_LOOPBACK;
16182
Matt Carlson0da06062011-05-19 12:12:53 +000016183 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016184
Linus Torvalds1da177e2005-04-16 15:20:36 -070016185 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016186 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016187 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016188 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016189 tp->rx_pending = 63;
16190 }
16191
Linus Torvalds1da177e2005-04-16 15:20:36 -070016192 err = tg3_get_device_address(tp);
16193 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016194 dev_err(&pdev->dev,
16195 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016196 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016197 }
16198
Matt Carlsonc88864d2007-11-12 21:07:01 -080016199 /*
16200 * Reset chip in case UNDI or EFI driver did not shutdown
16201 * DMA self test will enable WDMAC and we'll see (spurious)
16202 * pending DMA on the PCI bus at that point.
16203 */
16204 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
16205 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
16206 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
16207 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
16208 }
16209
16210 err = tg3_test_dma(tp);
16211 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016212 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080016213 goto err_out_apeunmap;
16214 }
16215
Matt Carlson78f90dc2009-11-13 13:03:42 +000016216 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
16217 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
16218 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000016219 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000016220 struct tg3_napi *tnapi = &tp->napi[i];
16221
16222 tnapi->tp = tp;
16223 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
16224
16225 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000016226 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016227 intmbx += 0x8;
16228 else
16229 intmbx += 0x4;
16230
16231 tnapi->consmbox = rcvmbx;
16232 tnapi->prodmbox = sndmbx;
16233
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016234 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016235 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016236 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000016237 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000016238
Joe Perches63c3a662011-04-26 08:12:10 +000016239 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000016240 break;
16241
16242 /*
16243 * If we support MSIX, we'll be using RSS. If we're using
16244 * RSS, the first vector only handles link interrupts and the
16245 * remaining vectors handle rx and tx interrupts. Reuse the
16246 * mailbox values for the next iteration. The values we setup
16247 * above are still useful for the single vectored mode.
16248 */
16249 if (!i)
16250 continue;
16251
16252 rcvmbx += 0x8;
16253
16254 if (sndmbx & 0x4)
16255 sndmbx -= 0x4;
16256 else
16257 sndmbx += 0xc;
16258 }
16259
Matt Carlsonc88864d2007-11-12 21:07:01 -080016260 tg3_init_coal(tp);
16261
Michael Chanc49a1562006-12-17 17:07:29 -080016262 pci_set_drvdata(pdev, dev);
16263
Matt Carlsoncd0d7222011-07-13 09:27:33 +000016264 if (tg3_flag(tp, 5717_PLUS)) {
16265 /* Resume a low-power mode */
16266 tg3_frob_aux_power(tp, false);
16267 }
16268
Matt Carlson21f76382012-02-22 12:35:21 +000016269 tg3_timer_init(tp);
16270
Linus Torvalds1da177e2005-04-16 15:20:36 -070016271 err = register_netdev(dev);
16272 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016273 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070016274 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016275 }
16276
Joe Perches05dbe002010-02-17 19:44:19 +000016277 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
16278 tp->board_part_number,
16279 tp->pci_chip_rev_id,
16280 tg3_bus_string(tp, str),
16281 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016282
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016283 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000016284 struct phy_device *phydev;
16285 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000016286 netdev_info(dev,
16287 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000016288 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016289 } else {
16290 char *ethtype;
16291
16292 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
16293 ethtype = "10/100Base-TX";
16294 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
16295 ethtype = "1000Base-SX";
16296 else
16297 ethtype = "10/100/1000Base-T";
16298
Matt Carlson5129c3a2010-04-05 10:19:23 +000016299 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000016300 "(WireSpeed[%d], EEE[%d])\n",
16301 tg3_phy_string(tp), ethtype,
16302 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
16303 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016304 }
Matt Carlsondf59c942008-11-03 16:52:56 -080016305
Joe Perches05dbe002010-02-17 19:44:19 +000016306 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000016307 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016308 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016309 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016310 tg3_flag(tp, ENABLE_ASF) != 0,
16311 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000016312 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
16313 tp->dma_rwctrl,
16314 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
16315 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016316
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016317 pci_save_state(pdev);
16318
Linus Torvalds1da177e2005-04-16 15:20:36 -070016319 return 0;
16320
Matt Carlson0d3031d2007-10-10 18:02:43 -070016321err_out_apeunmap:
16322 if (tp->aperegs) {
16323 iounmap(tp->aperegs);
16324 tp->aperegs = NULL;
16325 }
16326
Linus Torvalds1da177e2005-04-16 15:20:36 -070016327err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070016328 if (tp->regs) {
16329 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016330 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016332
16333err_out_free_dev:
16334 free_netdev(dev);
16335
Matt Carlson16821282011-07-13 09:27:28 +000016336err_out_power_down:
16337 pci_set_power_state(pdev, PCI_D3hot);
16338
Linus Torvalds1da177e2005-04-16 15:20:36 -070016339err_out_free_res:
16340 pci_release_regions(pdev);
16341
16342err_out_disable_pdev:
16343 pci_disable_device(pdev);
16344 pci_set_drvdata(pdev, NULL);
16345 return err;
16346}
16347
16348static void __devexit tg3_remove_one(struct pci_dev *pdev)
16349{
16350 struct net_device *dev = pci_get_drvdata(pdev);
16351
16352 if (dev) {
16353 struct tg3 *tp = netdev_priv(dev);
16354
Jesper Juhle3c55302012-04-09 22:50:15 +020016355 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080016356
Matt Carlsondb219972011-11-04 09:15:03 +000016357 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016358
David S. Miller1805b2f2011-10-24 18:18:09 -040016359 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016360 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016361 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016362 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070016363
Linus Torvalds1da177e2005-04-16 15:20:36 -070016364 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016365 if (tp->aperegs) {
16366 iounmap(tp->aperegs);
16367 tp->aperegs = NULL;
16368 }
Michael Chan68929142005-08-09 20:17:14 -070016369 if (tp->regs) {
16370 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016371 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016373 free_netdev(dev);
16374 pci_release_regions(pdev);
16375 pci_disable_device(pdev);
16376 pci_set_drvdata(pdev, NULL);
16377 }
16378}
16379
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016380#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016381static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016382{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016383 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016384 struct net_device *dev = pci_get_drvdata(pdev);
16385 struct tg3 *tp = netdev_priv(dev);
16386 int err;
16387
16388 if (!netif_running(dev))
16389 return 0;
16390
Matt Carlsondb219972011-11-04 09:15:03 +000016391 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016392 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016393 tg3_netif_stop(tp);
16394
Matt Carlson21f76382012-02-22 12:35:21 +000016395 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016396
David S. Millerf47c11e2005-06-24 20:18:35 -070016397 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016398 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070016399 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016400
16401 netif_device_detach(dev);
16402
David S. Millerf47c11e2005-06-24 20:18:35 -070016403 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070016404 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000016405 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070016406 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016407
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016408 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016409 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016410 int err2;
16411
David S. Millerf47c11e2005-06-24 20:18:35 -070016412 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016413
Joe Perches63c3a662011-04-26 08:12:10 +000016414 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016415 err2 = tg3_restart_hw(tp, 1);
16416 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070016417 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016418
Matt Carlson21f76382012-02-22 12:35:21 +000016419 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016420
16421 netif_device_attach(dev);
16422 tg3_netif_start(tp);
16423
Michael Chanb9ec6c12006-07-25 16:37:27 -070016424out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016425 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016426
16427 if (!err2)
16428 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016429 }
16430
16431 return err;
16432}
16433
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016434static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016435{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016436 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016437 struct net_device *dev = pci_get_drvdata(pdev);
16438 struct tg3 *tp = netdev_priv(dev);
16439 int err;
16440
16441 if (!netif_running(dev))
16442 return 0;
16443
Linus Torvalds1da177e2005-04-16 15:20:36 -070016444 netif_device_attach(dev);
16445
David S. Millerf47c11e2005-06-24 20:18:35 -070016446 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016447
Joe Perches63c3a662011-04-26 08:12:10 +000016448 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070016449 err = tg3_restart_hw(tp, 1);
16450 if (err)
16451 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016452
Matt Carlson21f76382012-02-22 12:35:21 +000016453 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016454
Linus Torvalds1da177e2005-04-16 15:20:36 -070016455 tg3_netif_start(tp);
16456
Michael Chanb9ec6c12006-07-25 16:37:27 -070016457out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016458 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016459
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016460 if (!err)
16461 tg3_phy_start(tp);
16462
Michael Chanb9ec6c12006-07-25 16:37:27 -070016463 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016464}
16465
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016466static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016467#define TG3_PM_OPS (&tg3_pm_ops)
16468
16469#else
16470
16471#define TG3_PM_OPS NULL
16472
16473#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016474
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016475/**
16476 * tg3_io_error_detected - called when PCI error is detected
16477 * @pdev: Pointer to PCI device
16478 * @state: The current pci connection state
16479 *
16480 * This function is called after a PCI bus error affecting
16481 * this device has been detected.
16482 */
16483static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
16484 pci_channel_state_t state)
16485{
16486 struct net_device *netdev = pci_get_drvdata(pdev);
16487 struct tg3 *tp = netdev_priv(netdev);
16488 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
16489
16490 netdev_info(netdev, "PCI I/O error detected\n");
16491
16492 rtnl_lock();
16493
16494 if (!netif_running(netdev))
16495 goto done;
16496
16497 tg3_phy_stop(tp);
16498
16499 tg3_netif_stop(tp);
16500
Matt Carlson21f76382012-02-22 12:35:21 +000016501 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016502
16503 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000016504 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016505
16506 netif_device_detach(netdev);
16507
16508 /* Clean up software state, even if MMIO is blocked */
16509 tg3_full_lock(tp, 0);
16510 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
16511 tg3_full_unlock(tp);
16512
16513done:
16514 if (state == pci_channel_io_perm_failure)
16515 err = PCI_ERS_RESULT_DISCONNECT;
16516 else
16517 pci_disable_device(pdev);
16518
16519 rtnl_unlock();
16520
16521 return err;
16522}
16523
16524/**
16525 * tg3_io_slot_reset - called after the pci bus has been reset.
16526 * @pdev: Pointer to PCI device
16527 *
16528 * Restart the card from scratch, as if from a cold-boot.
16529 * At this point, the card has exprienced a hard reset,
16530 * followed by fixups by BIOS, and has its config space
16531 * set up identically to what it was at cold boot.
16532 */
16533static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
16534{
16535 struct net_device *netdev = pci_get_drvdata(pdev);
16536 struct tg3 *tp = netdev_priv(netdev);
16537 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
16538 int err;
16539
16540 rtnl_lock();
16541
16542 if (pci_enable_device(pdev)) {
16543 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16544 goto done;
16545 }
16546
16547 pci_set_master(pdev);
16548 pci_restore_state(pdev);
16549 pci_save_state(pdev);
16550
16551 if (!netif_running(netdev)) {
16552 rc = PCI_ERS_RESULT_RECOVERED;
16553 goto done;
16554 }
16555
16556 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016557 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016558 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016559
16560 rc = PCI_ERS_RESULT_RECOVERED;
16561
16562done:
16563 rtnl_unlock();
16564
16565 return rc;
16566}
16567
16568/**
16569 * tg3_io_resume - called when traffic can start flowing again.
16570 * @pdev: Pointer to PCI device
16571 *
16572 * This callback is called when the error recovery driver tells
16573 * us that its OK to resume normal operation.
16574 */
16575static void tg3_io_resume(struct pci_dev *pdev)
16576{
16577 struct net_device *netdev = pci_get_drvdata(pdev);
16578 struct tg3 *tp = netdev_priv(netdev);
16579 int err;
16580
16581 rtnl_lock();
16582
16583 if (!netif_running(netdev))
16584 goto done;
16585
16586 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016587 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016588 err = tg3_restart_hw(tp, 1);
16589 tg3_full_unlock(tp);
16590 if (err) {
16591 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16592 goto done;
16593 }
16594
16595 netif_device_attach(netdev);
16596
Matt Carlson21f76382012-02-22 12:35:21 +000016597 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016598
16599 tg3_netif_start(tp);
16600
16601 tg3_phy_start(tp);
16602
16603done:
16604 rtnl_unlock();
16605}
16606
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070016607static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016608 .error_detected = tg3_io_error_detected,
16609 .slot_reset = tg3_io_slot_reset,
16610 .resume = tg3_io_resume
16611};
16612
Linus Torvalds1da177e2005-04-16 15:20:36 -070016613static struct pci_driver tg3_driver = {
16614 .name = DRV_MODULE_NAME,
16615 .id_table = tg3_pci_tbl,
16616 .probe = tg3_init_one,
16617 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016618 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016619 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016620};
16621
16622static int __init tg3_init(void)
16623{
Jeff Garzik29917622006-08-19 17:48:59 -040016624 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016625}
16626
16627static void __exit tg3_cleanup(void)
16628{
16629 pci_unregister_driver(&tg3_driver);
16630}
16631
16632module_init(tg3_init);
16633module_exit(tg3_cleanup);