tg3: Move phy accessor functions higher
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / tg3.c
blobea41d76a70d3e895f1dea9700589925585146ead
1 /*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
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.
7 * Copyright (C) 2005-2011 Broadcom Corporation.
9 * Firmware is:
10 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
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.
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/stringify.h>
22 #include <linux/kernel.h>
23 #include <linux/types.h>
24 #include <linux/compiler.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/in.h>
28 #include <linux/init.h>
29 #include <linux/ioport.h>
30 #include <linux/pci.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/ethtool.h>
35 #include <linux/mdio.h>
36 #include <linux/mii.h>
37 #include <linux/phy.h>
38 #include <linux/brcmphy.h>
39 #include <linux/if_vlan.h>
40 #include <linux/ip.h>
41 #include <linux/tcp.h>
42 #include <linux/workqueue.h>
43 #include <linux/prefetch.h>
44 #include <linux/dma-mapping.h>
45 #include <linux/firmware.h>
47 #include <net/checksum.h>
48 #include <net/ip.h>
50 #include <asm/system.h>
51 #include <linux/io.h>
52 #include <asm/byteorder.h>
53 #include <linux/uaccess.h>
55 #ifdef CONFIG_SPARC
56 #include <asm/idprom.h>
57 #include <asm/prom.h>
58 #endif
60 #define BAR_0 0
61 #define BAR_2 2
63 #include "tg3.h"
65 #define DRV_MODULE_NAME "tg3"
66 #define TG3_MAJ_NUM 3
67 #define TG3_MIN_NUM 117
68 #define DRV_MODULE_VERSION \
69 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
70 #define DRV_MODULE_RELDATE "January 25, 2011"
72 #define TG3_DEF_MAC_MODE 0
73 #define TG3_DEF_RX_MODE 0
74 #define TG3_DEF_TX_MODE 0
75 #define TG3_DEF_MSG_ENABLE \
76 (NETIF_MSG_DRV | \
77 NETIF_MSG_PROBE | \
78 NETIF_MSG_LINK | \
79 NETIF_MSG_TIMER | \
80 NETIF_MSG_IFDOWN | \
81 NETIF_MSG_IFUP | \
82 NETIF_MSG_RX_ERR | \
83 NETIF_MSG_TX_ERR)
85 /* length of time before we decide the hardware is borked,
86 * and dev->tx_timeout() should be called to fix the problem
88 #define TG3_TX_TIMEOUT (5 * HZ)
90 /* hardware minimum and maximum for a single frame's data payload */
91 #define TG3_MIN_MTU 60
92 #define TG3_MAX_MTU(tp) \
93 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
95 /* These numbers seem to be hard coded in the NIC firmware somehow.
96 * You can't change the ring sizes, but you can change where you place
97 * them in the NIC onboard memory.
99 #define TG3_RX_STD_RING_SIZE(tp) \
100 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
101 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
102 #define TG3_DEF_RX_RING_PENDING 200
103 #define TG3_RX_JMB_RING_SIZE(tp) \
104 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
105 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
106 #define TG3_DEF_RX_JUMBO_RING_PENDING 100
107 #define TG3_RSS_INDIR_TBL_SIZE 128
109 /* Do not place this n-ring entries value into the tp struct itself,
110 * we really want to expose these constants to GCC so that modulo et
111 * al. operations are done with shifts and masks instead of with
112 * hw multiply/modulo instructions. Another solution would be to
113 * replace things like '% foo' with '& (foo - 1)'.
116 #define TG3_TX_RING_SIZE 512
117 #define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
119 #define TG3_RX_STD_RING_BYTES(tp) \
120 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
121 #define TG3_RX_JMB_RING_BYTES(tp) \
122 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
123 #define TG3_RX_RCB_RING_BYTES(tp) \
124 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
125 #define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
126 TG3_TX_RING_SIZE)
127 #define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
129 #define TG3_DMA_BYTE_ENAB 64
131 #define TG3_RX_STD_DMA_SZ 1536
132 #define TG3_RX_JMB_DMA_SZ 9046
134 #define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
136 #define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
137 #define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
139 #define TG3_RX_STD_BUFF_RING_SIZE(tp) \
140 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
142 #define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
143 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
145 /* Due to a hardware bug, the 5701 can only DMA to memory addresses
146 * that are at least dword aligned when used in PCIX mode. The driver
147 * works around this bug by double copying the packet. This workaround
148 * is built into the normal double copy length check for efficiency.
150 * However, the double copy is only necessary on those architectures
151 * where unaligned memory accesses are inefficient. For those architectures
152 * where unaligned memory accesses incur little penalty, we can reintegrate
153 * the 5701 in the normal rx path. Doing so saves a device structure
154 * dereference by hardcoding the double copy threshold in place.
156 #define TG3_RX_COPY_THRESHOLD 256
157 #if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
158 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
159 #else
160 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
161 #endif
163 /* minimum number of free TX descriptors required to wake up TX process */
164 #define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
166 #define TG3_RAW_IP_ALIGN 2
168 #define TG3_FW_UPDATE_TIMEOUT_SEC 5
170 #define FIRMWARE_TG3 "tigon/tg3.bin"
171 #define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
172 #define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
174 static char version[] __devinitdata =
175 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
177 MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
178 MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
179 MODULE_LICENSE("GPL");
180 MODULE_VERSION(DRV_MODULE_VERSION);
181 MODULE_FIRMWARE(FIRMWARE_TG3);
182 MODULE_FIRMWARE(FIRMWARE_TG3TSO);
183 MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
185 static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
186 module_param(tg3_debug, int, 0);
187 MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
189 static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
190 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
191 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
192 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
193 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
194 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
263 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
264 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
265 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
266 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
267 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
268 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
269 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
273 MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
275 static const struct {
276 const char string[ETH_GSTRING_LEN];
277 } ethtool_stats_keys[] = {
278 { "rx_octets" },
279 { "rx_fragments" },
280 { "rx_ucast_packets" },
281 { "rx_mcast_packets" },
282 { "rx_bcast_packets" },
283 { "rx_fcs_errors" },
284 { "rx_align_errors" },
285 { "rx_xon_pause_rcvd" },
286 { "rx_xoff_pause_rcvd" },
287 { "rx_mac_ctrl_rcvd" },
288 { "rx_xoff_entered" },
289 { "rx_frame_too_long_errors" },
290 { "rx_jabbers" },
291 { "rx_undersize_packets" },
292 { "rx_in_length_errors" },
293 { "rx_out_length_errors" },
294 { "rx_64_or_less_octet_packets" },
295 { "rx_65_to_127_octet_packets" },
296 { "rx_128_to_255_octet_packets" },
297 { "rx_256_to_511_octet_packets" },
298 { "rx_512_to_1023_octet_packets" },
299 { "rx_1024_to_1522_octet_packets" },
300 { "rx_1523_to_2047_octet_packets" },
301 { "rx_2048_to_4095_octet_packets" },
302 { "rx_4096_to_8191_octet_packets" },
303 { "rx_8192_to_9022_octet_packets" },
305 { "tx_octets" },
306 { "tx_collisions" },
308 { "tx_xon_sent" },
309 { "tx_xoff_sent" },
310 { "tx_flow_control" },
311 { "tx_mac_errors" },
312 { "tx_single_collisions" },
313 { "tx_mult_collisions" },
314 { "tx_deferred" },
315 { "tx_excessive_collisions" },
316 { "tx_late_collisions" },
317 { "tx_collide_2times" },
318 { "tx_collide_3times" },
319 { "tx_collide_4times" },
320 { "tx_collide_5times" },
321 { "tx_collide_6times" },
322 { "tx_collide_7times" },
323 { "tx_collide_8times" },
324 { "tx_collide_9times" },
325 { "tx_collide_10times" },
326 { "tx_collide_11times" },
327 { "tx_collide_12times" },
328 { "tx_collide_13times" },
329 { "tx_collide_14times" },
330 { "tx_collide_15times" },
331 { "tx_ucast_packets" },
332 { "tx_mcast_packets" },
333 { "tx_bcast_packets" },
334 { "tx_carrier_sense_errors" },
335 { "tx_discards" },
336 { "tx_errors" },
338 { "dma_writeq_full" },
339 { "dma_write_prioq_full" },
340 { "rxbds_empty" },
341 { "rx_discards" },
342 { "mbuf_lwm_thresh_hit" },
343 { "rx_errors" },
344 { "rx_threshold_hit" },
346 { "dma_readq_full" },
347 { "dma_read_prioq_full" },
348 { "tx_comp_queue_full" },
350 { "ring_set_send_prod_index" },
351 { "ring_status_update" },
352 { "nic_irqs" },
353 { "nic_avoided_irqs" },
354 { "nic_tx_threshold_hit" }
357 #define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
360 static const struct {
361 const char string[ETH_GSTRING_LEN];
362 } ethtool_test_keys[] = {
363 { "nvram test (online) " },
364 { "link test (online) " },
365 { "register test (offline)" },
366 { "memory test (offline)" },
367 { "loopback test (offline)" },
368 { "interrupt test (offline)" },
371 #define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
374 static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
376 writel(val, tp->regs + off);
379 static u32 tg3_read32(struct tg3 *tp, u32 off)
381 return readl(tp->regs + off);
384 static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
386 writel(val, tp->aperegs + off);
389 static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
391 return readl(tp->aperegs + off);
394 static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
396 unsigned long flags;
398 spin_lock_irqsave(&tp->indirect_lock, flags);
399 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
400 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
401 spin_unlock_irqrestore(&tp->indirect_lock, flags);
404 static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
406 writel(val, tp->regs + off);
407 readl(tp->regs + off);
410 static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
412 unsigned long flags;
413 u32 val;
415 spin_lock_irqsave(&tp->indirect_lock, flags);
416 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
417 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
418 spin_unlock_irqrestore(&tp->indirect_lock, flags);
419 return val;
422 static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
424 unsigned long flags;
426 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
427 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
428 TG3_64BIT_REG_LOW, val);
429 return;
431 if (off == TG3_RX_STD_PROD_IDX_REG) {
432 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
433 TG3_64BIT_REG_LOW, val);
434 return;
437 spin_lock_irqsave(&tp->indirect_lock, flags);
438 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
439 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
440 spin_unlock_irqrestore(&tp->indirect_lock, flags);
442 /* In indirect mode when disabling interrupts, we also need
443 * to clear the interrupt bit in the GRC local ctrl register.
445 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
446 (val == 0x1)) {
447 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
448 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
452 static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
454 unsigned long flags;
455 u32 val;
457 spin_lock_irqsave(&tp->indirect_lock, flags);
458 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
459 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
460 spin_unlock_irqrestore(&tp->indirect_lock, flags);
461 return val;
464 /* usec_wait specifies the wait time in usec when writing to certain registers
465 * where it is unsafe to read back the register without some delay.
466 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
467 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
469 static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
471 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
472 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
473 /* Non-posted methods */
474 tp->write32(tp, off, val);
475 else {
476 /* Posted method */
477 tg3_write32(tp, off, val);
478 if (usec_wait)
479 udelay(usec_wait);
480 tp->read32(tp, off);
482 /* Wait again after the read for the posted method to guarantee that
483 * the wait time is met.
485 if (usec_wait)
486 udelay(usec_wait);
489 static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
491 tp->write32_mbox(tp, off, val);
492 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
493 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
494 tp->read32_mbox(tp, off);
497 static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
499 void __iomem *mbox = tp->regs + off;
500 writel(val, mbox);
501 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
502 writel(val, mbox);
503 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
504 readl(mbox);
507 static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
509 return readl(tp->regs + off + GRCMBOX_BASE);
512 static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
514 writel(val, tp->regs + off + GRCMBOX_BASE);
517 #define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
518 #define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
519 #define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
520 #define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
521 #define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
523 #define tw32(reg, val) tp->write32(tp, reg, val)
524 #define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
525 #define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
526 #define tr32(reg) tp->read32(tp, reg)
528 static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
530 unsigned long flags;
532 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
533 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
534 return;
536 spin_lock_irqsave(&tp->indirect_lock, flags);
537 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
538 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
539 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
541 /* Always leave this as zero. */
542 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
543 } else {
544 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
545 tw32_f(TG3PCI_MEM_WIN_DATA, val);
547 /* Always leave this as zero. */
548 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
550 spin_unlock_irqrestore(&tp->indirect_lock, flags);
553 static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
555 unsigned long flags;
557 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
558 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
559 *val = 0;
560 return;
563 spin_lock_irqsave(&tp->indirect_lock, flags);
564 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
565 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
566 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
568 /* Always leave this as zero. */
569 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
570 } else {
571 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
572 *val = tr32(TG3PCI_MEM_WIN_DATA);
574 /* Always leave this as zero. */
575 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
577 spin_unlock_irqrestore(&tp->indirect_lock, flags);
580 static void tg3_ape_lock_init(struct tg3 *tp)
582 int i;
583 u32 regbase;
585 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
586 regbase = TG3_APE_LOCK_GRANT;
587 else
588 regbase = TG3_APE_PER_LOCK_GRANT;
590 /* Make sure the driver hasn't any stale locks. */
591 for (i = 0; i < 8; i++)
592 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
595 static int tg3_ape_lock(struct tg3 *tp, int locknum)
597 int i, off;
598 int ret = 0;
599 u32 status, req, gnt;
601 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
602 return 0;
604 switch (locknum) {
605 case TG3_APE_LOCK_GRC:
606 case TG3_APE_LOCK_MEM:
607 break;
608 default:
609 return -EINVAL;
612 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
613 req = TG3_APE_LOCK_REQ;
614 gnt = TG3_APE_LOCK_GRANT;
615 } else {
616 req = TG3_APE_PER_LOCK_REQ;
617 gnt = TG3_APE_PER_LOCK_GRANT;
620 off = 4 * locknum;
622 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
624 /* Wait for up to 1 millisecond to acquire lock. */
625 for (i = 0; i < 100; i++) {
626 status = tg3_ape_read32(tp, gnt + off);
627 if (status == APE_LOCK_GRANT_DRIVER)
628 break;
629 udelay(10);
632 if (status != APE_LOCK_GRANT_DRIVER) {
633 /* Revoke the lock request. */
634 tg3_ape_write32(tp, gnt + off,
635 APE_LOCK_GRANT_DRIVER);
637 ret = -EBUSY;
640 return ret;
643 static void tg3_ape_unlock(struct tg3 *tp, int locknum)
645 u32 gnt;
647 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
648 return;
650 switch (locknum) {
651 case TG3_APE_LOCK_GRC:
652 case TG3_APE_LOCK_MEM:
653 break;
654 default:
655 return;
658 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
659 gnt = TG3_APE_LOCK_GRANT;
660 else
661 gnt = TG3_APE_PER_LOCK_GRANT;
663 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
666 static void tg3_disable_ints(struct tg3 *tp)
668 int i;
670 tw32(TG3PCI_MISC_HOST_CTRL,
671 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
672 for (i = 0; i < tp->irq_max; i++)
673 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
676 static void tg3_enable_ints(struct tg3 *tp)
678 int i;
680 tp->irq_sync = 0;
681 wmb();
683 tw32(TG3PCI_MISC_HOST_CTRL,
684 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
686 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
687 for (i = 0; i < tp->irq_cnt; i++) {
688 struct tg3_napi *tnapi = &tp->napi[i];
690 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
691 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
692 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
694 tp->coal_now |= tnapi->coal_now;
697 /* Force an initial interrupt */
698 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
699 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
700 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
701 else
702 tw32(HOSTCC_MODE, tp->coal_now);
704 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
707 static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
709 struct tg3 *tp = tnapi->tp;
710 struct tg3_hw_status *sblk = tnapi->hw_status;
711 unsigned int work_exists = 0;
713 /* check for phy events */
714 if (!(tp->tg3_flags &
715 (TG3_FLAG_USE_LINKCHG_REG |
716 TG3_FLAG_POLL_SERDES))) {
717 if (sblk->status & SD_STATUS_LINK_CHG)
718 work_exists = 1;
720 /* check for RX/TX work to do */
721 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
722 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
723 work_exists = 1;
725 return work_exists;
728 /* tg3_int_reenable
729 * similar to tg3_enable_ints, but it accurately determines whether there
730 * is new work pending and can return without flushing the PIO write
731 * which reenables interrupts
733 static void tg3_int_reenable(struct tg3_napi *tnapi)
735 struct tg3 *tp = tnapi->tp;
737 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
738 mmiowb();
740 /* When doing tagged status, this work check is unnecessary.
741 * The last_tag we write above tells the chip which piece of
742 * work we've completed.
744 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
745 tg3_has_work(tnapi))
746 tw32(HOSTCC_MODE, tp->coalesce_mode |
747 HOSTCC_MODE_ENABLE | tnapi->coal_now);
750 static void tg3_switch_clocks(struct tg3 *tp)
752 u32 clock_ctrl;
753 u32 orig_clock_ctrl;
755 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
756 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
757 return;
759 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
761 orig_clock_ctrl = clock_ctrl;
762 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
763 CLOCK_CTRL_CLKRUN_OENABLE |
764 0x1f);
765 tp->pci_clock_ctrl = clock_ctrl;
767 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
768 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
769 tw32_wait_f(TG3PCI_CLOCK_CTRL,
770 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
772 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
773 tw32_wait_f(TG3PCI_CLOCK_CTRL,
774 clock_ctrl |
775 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
776 40);
777 tw32_wait_f(TG3PCI_CLOCK_CTRL,
778 clock_ctrl | (CLOCK_CTRL_ALTCLK),
779 40);
781 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
784 #define PHY_BUSY_LOOPS 5000
786 static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
788 u32 frame_val;
789 unsigned int loops;
790 int ret;
792 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
793 tw32_f(MAC_MI_MODE,
794 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
795 udelay(80);
798 *val = 0x0;
800 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
801 MI_COM_PHY_ADDR_MASK);
802 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
803 MI_COM_REG_ADDR_MASK);
804 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
806 tw32_f(MAC_MI_COM, frame_val);
808 loops = PHY_BUSY_LOOPS;
809 while (loops != 0) {
810 udelay(10);
811 frame_val = tr32(MAC_MI_COM);
813 if ((frame_val & MI_COM_BUSY) == 0) {
814 udelay(5);
815 frame_val = tr32(MAC_MI_COM);
816 break;
818 loops -= 1;
821 ret = -EBUSY;
822 if (loops != 0) {
823 *val = frame_val & MI_COM_DATA_MASK;
824 ret = 0;
827 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
828 tw32_f(MAC_MI_MODE, tp->mi_mode);
829 udelay(80);
832 return ret;
835 static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
837 u32 frame_val;
838 unsigned int loops;
839 int ret;
841 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
842 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
843 return 0;
845 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
846 tw32_f(MAC_MI_MODE,
847 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
848 udelay(80);
851 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
852 MI_COM_PHY_ADDR_MASK);
853 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
854 MI_COM_REG_ADDR_MASK);
855 frame_val |= (val & MI_COM_DATA_MASK);
856 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
858 tw32_f(MAC_MI_COM, frame_val);
860 loops = PHY_BUSY_LOOPS;
861 while (loops != 0) {
862 udelay(10);
863 frame_val = tr32(MAC_MI_COM);
864 if ((frame_val & MI_COM_BUSY) == 0) {
865 udelay(5);
866 frame_val = tr32(MAC_MI_COM);
867 break;
869 loops -= 1;
872 ret = -EBUSY;
873 if (loops != 0)
874 ret = 0;
876 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
877 tw32_f(MAC_MI_MODE, tp->mi_mode);
878 udelay(80);
881 return ret;
884 static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
886 int err;
888 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
889 if (err)
890 goto done;
892 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
893 if (err)
894 goto done;
896 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
897 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
898 if (err)
899 goto done;
901 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
903 done:
904 return err;
907 static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
909 int err;
911 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
912 if (err)
913 goto done;
915 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
916 if (err)
917 goto done;
919 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
920 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
921 if (err)
922 goto done;
924 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
926 done:
927 return err;
930 static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
932 int err;
934 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
935 if (!err)
936 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
938 return err;
941 static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
943 int err;
945 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
946 if (!err)
947 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
949 return err;
952 static int tg3_bmcr_reset(struct tg3 *tp)
954 u32 phy_control;
955 int limit, err;
957 /* OK, reset it, and poll the BMCR_RESET bit until it
958 * clears or we time out.
960 phy_control = BMCR_RESET;
961 err = tg3_writephy(tp, MII_BMCR, phy_control);
962 if (err != 0)
963 return -EBUSY;
965 limit = 5000;
966 while (limit--) {
967 err = tg3_readphy(tp, MII_BMCR, &phy_control);
968 if (err != 0)
969 return -EBUSY;
971 if ((phy_control & BMCR_RESET) == 0) {
972 udelay(40);
973 break;
975 udelay(10);
977 if (limit < 0)
978 return -EBUSY;
980 return 0;
983 static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
985 struct tg3 *tp = bp->priv;
986 u32 val;
988 spin_lock_bh(&tp->lock);
990 if (tg3_readphy(tp, reg, &val))
991 val = -EIO;
993 spin_unlock_bh(&tp->lock);
995 return val;
998 static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1000 struct tg3 *tp = bp->priv;
1001 u32 ret = 0;
1003 spin_lock_bh(&tp->lock);
1005 if (tg3_writephy(tp, reg, val))
1006 ret = -EIO;
1008 spin_unlock_bh(&tp->lock);
1010 return ret;
1013 static int tg3_mdio_reset(struct mii_bus *bp)
1015 return 0;
1018 static void tg3_mdio_config_5785(struct tg3 *tp)
1020 u32 val;
1021 struct phy_device *phydev;
1023 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1024 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
1025 case PHY_ID_BCM50610:
1026 case PHY_ID_BCM50610M:
1027 val = MAC_PHYCFG2_50610_LED_MODES;
1028 break;
1029 case PHY_ID_BCMAC131:
1030 val = MAC_PHYCFG2_AC131_LED_MODES;
1031 break;
1032 case PHY_ID_RTL8211C:
1033 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1034 break;
1035 case PHY_ID_RTL8201E:
1036 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1037 break;
1038 default:
1039 return;
1042 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1043 tw32(MAC_PHYCFG2, val);
1045 val = tr32(MAC_PHYCFG1);
1046 val &= ~(MAC_PHYCFG1_RGMII_INT |
1047 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1048 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
1049 tw32(MAC_PHYCFG1, val);
1051 return;
1054 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
1055 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1056 MAC_PHYCFG2_FMODE_MASK_MASK |
1057 MAC_PHYCFG2_GMODE_MASK_MASK |
1058 MAC_PHYCFG2_ACT_MASK_MASK |
1059 MAC_PHYCFG2_QUAL_MASK_MASK |
1060 MAC_PHYCFG2_INBAND_ENABLE;
1062 tw32(MAC_PHYCFG2, val);
1064 val = tr32(MAC_PHYCFG1);
1065 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1066 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
1067 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
1068 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1069 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1070 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1071 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1073 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1074 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1075 tw32(MAC_PHYCFG1, val);
1077 val = tr32(MAC_EXT_RGMII_MODE);
1078 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1079 MAC_RGMII_MODE_RX_QUALITY |
1080 MAC_RGMII_MODE_RX_ACTIVITY |
1081 MAC_RGMII_MODE_RX_ENG_DET |
1082 MAC_RGMII_MODE_TX_ENABLE |
1083 MAC_RGMII_MODE_TX_LOWPWR |
1084 MAC_RGMII_MODE_TX_RESET);
1085 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
1086 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1087 val |= MAC_RGMII_MODE_RX_INT_B |
1088 MAC_RGMII_MODE_RX_QUALITY |
1089 MAC_RGMII_MODE_RX_ACTIVITY |
1090 MAC_RGMII_MODE_RX_ENG_DET;
1091 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1092 val |= MAC_RGMII_MODE_TX_ENABLE |
1093 MAC_RGMII_MODE_TX_LOWPWR |
1094 MAC_RGMII_MODE_TX_RESET;
1096 tw32(MAC_EXT_RGMII_MODE, val);
1099 static void tg3_mdio_start(struct tg3 *tp)
1101 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1102 tw32_f(MAC_MI_MODE, tp->mi_mode);
1103 udelay(80);
1105 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1106 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1107 tg3_mdio_config_5785(tp);
1110 static int tg3_mdio_init(struct tg3 *tp)
1112 int i;
1113 u32 reg;
1114 struct phy_device *phydev;
1116 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
1117 u32 is_serdes;
1119 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
1121 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1122 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1123 else
1124 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1125 TG3_CPMU_PHY_STRAP_IS_SERDES;
1126 if (is_serdes)
1127 tp->phy_addr += 7;
1128 } else
1129 tp->phy_addr = TG3_PHY_MII_ADDR;
1131 tg3_mdio_start(tp);
1133 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1134 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1135 return 0;
1137 tp->mdio_bus = mdiobus_alloc();
1138 if (tp->mdio_bus == NULL)
1139 return -ENOMEM;
1141 tp->mdio_bus->name = "tg3 mdio bus";
1142 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
1143 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
1144 tp->mdio_bus->priv = tp;
1145 tp->mdio_bus->parent = &tp->pdev->dev;
1146 tp->mdio_bus->read = &tg3_mdio_read;
1147 tp->mdio_bus->write = &tg3_mdio_write;
1148 tp->mdio_bus->reset = &tg3_mdio_reset;
1149 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
1150 tp->mdio_bus->irq = &tp->mdio_irq[0];
1152 for (i = 0; i < PHY_MAX_ADDR; i++)
1153 tp->mdio_bus->irq[i] = PHY_POLL;
1155 /* The bus registration will look for all the PHYs on the mdio bus.
1156 * Unfortunately, it does not ensure the PHY is powered up before
1157 * accessing the PHY ID registers. A chip reset is the
1158 * quickest way to bring the device back to an operational state..
1160 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1161 tg3_bmcr_reset(tp);
1163 i = mdiobus_register(tp->mdio_bus);
1164 if (i) {
1165 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
1166 mdiobus_free(tp->mdio_bus);
1167 return i;
1170 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1172 if (!phydev || !phydev->drv) {
1173 dev_warn(&tp->pdev->dev, "No PHY devices\n");
1174 mdiobus_unregister(tp->mdio_bus);
1175 mdiobus_free(tp->mdio_bus);
1176 return -ENODEV;
1179 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
1180 case PHY_ID_BCM57780:
1181 phydev->interface = PHY_INTERFACE_MODE_GMII;
1182 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
1183 break;
1184 case PHY_ID_BCM50610:
1185 case PHY_ID_BCM50610M:
1186 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
1187 PHY_BRCM_RX_REFCLK_UNUSED |
1188 PHY_BRCM_DIS_TXCRXC_NOENRGY |
1189 PHY_BRCM_AUTO_PWRDWN_ENABLE;
1190 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
1191 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1192 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1193 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1194 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1195 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
1196 /* fallthru */
1197 case PHY_ID_RTL8211C:
1198 phydev->interface = PHY_INTERFACE_MODE_RGMII;
1199 break;
1200 case PHY_ID_RTL8201E:
1201 case PHY_ID_BCMAC131:
1202 phydev->interface = PHY_INTERFACE_MODE_MII;
1203 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
1204 tp->phy_flags |= TG3_PHYFLG_IS_FET;
1205 break;
1208 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1210 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1211 tg3_mdio_config_5785(tp);
1213 return 0;
1216 static void tg3_mdio_fini(struct tg3 *tp)
1218 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1219 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
1220 mdiobus_unregister(tp->mdio_bus);
1221 mdiobus_free(tp->mdio_bus);
1225 /* tp->lock is held. */
1226 static inline void tg3_generate_fw_event(struct tg3 *tp)
1228 u32 val;
1230 val = tr32(GRC_RX_CPU_EVENT);
1231 val |= GRC_RX_CPU_DRIVER_EVENT;
1232 tw32_f(GRC_RX_CPU_EVENT, val);
1234 tp->last_event_jiffies = jiffies;
1237 #define TG3_FW_EVENT_TIMEOUT_USEC 2500
1239 /* tp->lock is held. */
1240 static void tg3_wait_for_event_ack(struct tg3 *tp)
1242 int i;
1243 unsigned int delay_cnt;
1244 long time_remain;
1246 /* If enough time has passed, no wait is necessary. */
1247 time_remain = (long)(tp->last_event_jiffies + 1 +
1248 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1249 (long)jiffies;
1250 if (time_remain < 0)
1251 return;
1253 /* Check if we can shorten the wait time. */
1254 delay_cnt = jiffies_to_usecs(time_remain);
1255 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1256 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1257 delay_cnt = (delay_cnt >> 3) + 1;
1259 for (i = 0; i < delay_cnt; i++) {
1260 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1261 break;
1262 udelay(8);
1266 /* tp->lock is held. */
1267 static void tg3_ump_link_report(struct tg3 *tp)
1269 u32 reg;
1270 u32 val;
1272 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1273 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1274 return;
1276 tg3_wait_for_event_ack(tp);
1278 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1280 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1282 val = 0;
1283 if (!tg3_readphy(tp, MII_BMCR, &reg))
1284 val = reg << 16;
1285 if (!tg3_readphy(tp, MII_BMSR, &reg))
1286 val |= (reg & 0xffff);
1287 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1289 val = 0;
1290 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1291 val = reg << 16;
1292 if (!tg3_readphy(tp, MII_LPA, &reg))
1293 val |= (reg & 0xffff);
1294 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1296 val = 0;
1297 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
1298 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1299 val = reg << 16;
1300 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1301 val |= (reg & 0xffff);
1303 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1305 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1306 val = reg << 16;
1307 else
1308 val = 0;
1309 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1311 tg3_generate_fw_event(tp);
1314 static void tg3_link_report(struct tg3 *tp)
1316 if (!netif_carrier_ok(tp->dev)) {
1317 netif_info(tp, link, tp->dev, "Link is down\n");
1318 tg3_ump_link_report(tp);
1319 } else if (netif_msg_link(tp)) {
1320 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1321 (tp->link_config.active_speed == SPEED_1000 ?
1322 1000 :
1323 (tp->link_config.active_speed == SPEED_100 ?
1324 100 : 10)),
1325 (tp->link_config.active_duplex == DUPLEX_FULL ?
1326 "full" : "half"));
1328 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1329 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1330 "on" : "off",
1331 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1332 "on" : "off");
1333 tg3_ump_link_report(tp);
1337 static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1339 u16 miireg;
1341 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
1342 miireg = ADVERTISE_PAUSE_CAP;
1343 else if (flow_ctrl & FLOW_CTRL_TX)
1344 miireg = ADVERTISE_PAUSE_ASYM;
1345 else if (flow_ctrl & FLOW_CTRL_RX)
1346 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1347 else
1348 miireg = 0;
1350 return miireg;
1353 static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1355 u16 miireg;
1357 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
1358 miireg = ADVERTISE_1000XPAUSE;
1359 else if (flow_ctrl & FLOW_CTRL_TX)
1360 miireg = ADVERTISE_1000XPSE_ASYM;
1361 else if (flow_ctrl & FLOW_CTRL_RX)
1362 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1363 else
1364 miireg = 0;
1366 return miireg;
1369 static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1371 u8 cap = 0;
1373 if (lcladv & ADVERTISE_1000XPAUSE) {
1374 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1375 if (rmtadv & LPA_1000XPAUSE)
1376 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1377 else if (rmtadv & LPA_1000XPAUSE_ASYM)
1378 cap = FLOW_CTRL_RX;
1379 } else {
1380 if (rmtadv & LPA_1000XPAUSE)
1381 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1383 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1384 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
1385 cap = FLOW_CTRL_TX;
1388 return cap;
1391 static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
1393 u8 autoneg;
1394 u8 flowctrl = 0;
1395 u32 old_rx_mode = tp->rx_mode;
1396 u32 old_tx_mode = tp->tx_mode;
1398 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
1399 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
1400 else
1401 autoneg = tp->link_config.autoneg;
1403 if (autoneg == AUTONEG_ENABLE &&
1404 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1405 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
1406 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
1407 else
1408 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
1409 } else
1410 flowctrl = tp->link_config.flowctrl;
1412 tp->link_config.active_flowctrl = flowctrl;
1414 if (flowctrl & FLOW_CTRL_RX)
1415 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1416 else
1417 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1419 if (old_rx_mode != tp->rx_mode)
1420 tw32_f(MAC_RX_MODE, tp->rx_mode);
1422 if (flowctrl & FLOW_CTRL_TX)
1423 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1424 else
1425 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1427 if (old_tx_mode != tp->tx_mode)
1428 tw32_f(MAC_TX_MODE, tp->tx_mode);
1431 static void tg3_adjust_link(struct net_device *dev)
1433 u8 oldflowctrl, linkmesg = 0;
1434 u32 mac_mode, lcl_adv, rmt_adv;
1435 struct tg3 *tp = netdev_priv(dev);
1436 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1438 spin_lock_bh(&tp->lock);
1440 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1441 MAC_MODE_HALF_DUPLEX);
1443 oldflowctrl = tp->link_config.active_flowctrl;
1445 if (phydev->link) {
1446 lcl_adv = 0;
1447 rmt_adv = 0;
1449 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1450 mac_mode |= MAC_MODE_PORT_MODE_MII;
1451 else if (phydev->speed == SPEED_1000 ||
1452 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
1453 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1454 else
1455 mac_mode |= MAC_MODE_PORT_MODE_MII;
1457 if (phydev->duplex == DUPLEX_HALF)
1458 mac_mode |= MAC_MODE_HALF_DUPLEX;
1459 else {
1460 lcl_adv = tg3_advert_flowctrl_1000T(
1461 tp->link_config.flowctrl);
1463 if (phydev->pause)
1464 rmt_adv = LPA_PAUSE_CAP;
1465 if (phydev->asym_pause)
1466 rmt_adv |= LPA_PAUSE_ASYM;
1469 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1470 } else
1471 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1473 if (mac_mode != tp->mac_mode) {
1474 tp->mac_mode = mac_mode;
1475 tw32_f(MAC_MODE, tp->mac_mode);
1476 udelay(40);
1479 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1480 if (phydev->speed == SPEED_10)
1481 tw32(MAC_MI_STAT,
1482 MAC_MI_STAT_10MBPS_MODE |
1483 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1484 else
1485 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1488 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1489 tw32(MAC_TX_LENGTHS,
1490 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1491 (6 << TX_LENGTHS_IPG_SHIFT) |
1492 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1493 else
1494 tw32(MAC_TX_LENGTHS,
1495 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1496 (6 << TX_LENGTHS_IPG_SHIFT) |
1497 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1499 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1500 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1501 phydev->speed != tp->link_config.active_speed ||
1502 phydev->duplex != tp->link_config.active_duplex ||
1503 oldflowctrl != tp->link_config.active_flowctrl)
1504 linkmesg = 1;
1506 tp->link_config.active_speed = phydev->speed;
1507 tp->link_config.active_duplex = phydev->duplex;
1509 spin_unlock_bh(&tp->lock);
1511 if (linkmesg)
1512 tg3_link_report(tp);
1515 static int tg3_phy_init(struct tg3 *tp)
1517 struct phy_device *phydev;
1519 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
1520 return 0;
1522 /* Bring the PHY back to a known state. */
1523 tg3_bmcr_reset(tp);
1525 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1527 /* Attach the MAC to the PHY. */
1528 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
1529 phydev->dev_flags, phydev->interface);
1530 if (IS_ERR(phydev)) {
1531 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
1532 return PTR_ERR(phydev);
1535 /* Mask with MAC supported features. */
1536 switch (phydev->interface) {
1537 case PHY_INTERFACE_MODE_GMII:
1538 case PHY_INTERFACE_MODE_RGMII:
1539 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
1540 phydev->supported &= (PHY_GBIT_FEATURES |
1541 SUPPORTED_Pause |
1542 SUPPORTED_Asym_Pause);
1543 break;
1545 /* fallthru */
1546 case PHY_INTERFACE_MODE_MII:
1547 phydev->supported &= (PHY_BASIC_FEATURES |
1548 SUPPORTED_Pause |
1549 SUPPORTED_Asym_Pause);
1550 break;
1551 default:
1552 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
1553 return -EINVAL;
1556 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
1558 phydev->advertising = phydev->supported;
1560 return 0;
1563 static void tg3_phy_start(struct tg3 *tp)
1565 struct phy_device *phydev;
1567 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
1568 return;
1570 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1572 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1573 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
1574 phydev->speed = tp->link_config.orig_speed;
1575 phydev->duplex = tp->link_config.orig_duplex;
1576 phydev->autoneg = tp->link_config.orig_autoneg;
1577 phydev->advertising = tp->link_config.orig_advertising;
1580 phy_start(phydev);
1582 phy_start_aneg(phydev);
1585 static void tg3_phy_stop(struct tg3 *tp)
1587 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
1588 return;
1590 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
1593 static void tg3_phy_fini(struct tg3 *tp)
1595 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
1596 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
1597 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
1601 static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1603 u32 phytest;
1605 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1606 u32 phy;
1608 tg3_writephy(tp, MII_TG3_FET_TEST,
1609 phytest | MII_TG3_FET_SHADOW_EN);
1610 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1611 if (enable)
1612 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1613 else
1614 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1615 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1617 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1621 static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1623 u32 reg;
1625 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1626 ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
1627 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
1628 return;
1630 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
1631 tg3_phy_fet_toggle_apd(tp, enable);
1632 return;
1635 reg = MII_TG3_MISC_SHDW_WREN |
1636 MII_TG3_MISC_SHDW_SCR5_SEL |
1637 MII_TG3_MISC_SHDW_SCR5_LPED |
1638 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1639 MII_TG3_MISC_SHDW_SCR5_SDTL |
1640 MII_TG3_MISC_SHDW_SCR5_C125OE;
1641 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1642 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1644 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1647 reg = MII_TG3_MISC_SHDW_WREN |
1648 MII_TG3_MISC_SHDW_APD_SEL |
1649 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1650 if (enable)
1651 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1653 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1656 static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1658 u32 phy;
1660 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1661 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
1662 return;
1664 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
1665 u32 ephy;
1667 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1668 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1670 tg3_writephy(tp, MII_TG3_FET_TEST,
1671 ephy | MII_TG3_FET_SHADOW_EN);
1672 if (!tg3_readphy(tp, reg, &phy)) {
1673 if (enable)
1674 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1675 else
1676 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1677 tg3_writephy(tp, reg, phy);
1679 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
1681 } else {
1682 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1683 MII_TG3_AUXCTL_SHDWSEL_MISC;
1684 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1685 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1686 if (enable)
1687 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1688 else
1689 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1690 phy |= MII_TG3_AUXCTL_MISC_WREN;
1691 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1696 static void tg3_phy_set_wirespeed(struct tg3 *tp)
1698 u32 val;
1700 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
1701 return;
1703 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1704 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1705 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1706 (val | (1 << 15) | (1 << 4)));
1709 static void tg3_phy_apply_otp(struct tg3 *tp)
1711 u32 otp, phy;
1713 if (!tp->phy_otp)
1714 return;
1716 otp = tp->phy_otp;
1718 /* Enable SM_DSP clock and tx 6dB coding. */
1719 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1720 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1721 MII_TG3_AUXCTL_ACTL_TX_6DB;
1722 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1724 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1725 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1726 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1728 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1729 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1730 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1732 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1733 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1734 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1736 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1737 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1739 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1740 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1742 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1743 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1744 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1746 /* Turn off SM_DSP clock. */
1747 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1748 MII_TG3_AUXCTL_ACTL_TX_6DB;
1749 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1752 static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1754 u32 val;
1756 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1757 return;
1759 tp->setlpicnt = 0;
1761 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1762 current_link_up == 1 &&
1763 tp->link_config.active_duplex == DUPLEX_FULL &&
1764 (tp->link_config.active_speed == SPEED_100 ||
1765 tp->link_config.active_speed == SPEED_1000)) {
1766 u32 eeectl;
1768 if (tp->link_config.active_speed == SPEED_1000)
1769 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1770 else
1771 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1773 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1775 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1776 TG3_CL45_D7_EEERES_STAT, &val);
1778 switch (val) {
1779 case TG3_CL45_D7_EEERES_STAT_LP_1000T:
1780 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
1781 case ASIC_REV_5717:
1782 case ASIC_REV_5719:
1783 case ASIC_REV_57765:
1784 /* Enable SM_DSP clock and tx 6dB coding. */
1785 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1786 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1787 MII_TG3_AUXCTL_ACTL_TX_6DB;
1788 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1790 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
1792 /* Turn off SM_DSP clock. */
1793 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1794 MII_TG3_AUXCTL_ACTL_TX_6DB;
1795 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1797 /* Fallthrough */
1798 case TG3_CL45_D7_EEERES_STAT_LP_100TX:
1799 tp->setlpicnt = 2;
1803 if (!tp->setlpicnt) {
1804 val = tr32(TG3_CPMU_EEE_MODE);
1805 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1809 static int tg3_wait_macro_done(struct tg3 *tp)
1811 int limit = 100;
1813 while (limit--) {
1814 u32 tmp32;
1816 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
1817 if ((tmp32 & 0x1000) == 0)
1818 break;
1821 if (limit < 0)
1822 return -EBUSY;
1824 return 0;
1827 static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1829 static const u32 test_pat[4][6] = {
1830 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1831 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1832 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1833 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1835 int chan;
1837 for (chan = 0; chan < 4; chan++) {
1838 int i;
1840 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1841 (chan * 0x2000) | 0x0200);
1842 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
1844 for (i = 0; i < 6; i++)
1845 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1846 test_pat[chan][i]);
1848 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
1849 if (tg3_wait_macro_done(tp)) {
1850 *resetp = 1;
1851 return -EBUSY;
1854 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1855 (chan * 0x2000) | 0x0200);
1856 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
1857 if (tg3_wait_macro_done(tp)) {
1858 *resetp = 1;
1859 return -EBUSY;
1862 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
1863 if (tg3_wait_macro_done(tp)) {
1864 *resetp = 1;
1865 return -EBUSY;
1868 for (i = 0; i < 6; i += 2) {
1869 u32 low, high;
1871 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1872 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1873 tg3_wait_macro_done(tp)) {
1874 *resetp = 1;
1875 return -EBUSY;
1877 low &= 0x7fff;
1878 high &= 0x000f;
1879 if (low != test_pat[chan][i] ||
1880 high != test_pat[chan][i+1]) {
1881 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1882 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1883 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1885 return -EBUSY;
1890 return 0;
1893 static int tg3_phy_reset_chanpat(struct tg3 *tp)
1895 int chan;
1897 for (chan = 0; chan < 4; chan++) {
1898 int i;
1900 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1901 (chan * 0x2000) | 0x0200);
1902 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
1903 for (i = 0; i < 6; i++)
1904 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1905 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
1906 if (tg3_wait_macro_done(tp))
1907 return -EBUSY;
1910 return 0;
1913 static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1915 u32 reg32, phy9_orig;
1916 int retries, do_phy_reset, err;
1918 retries = 10;
1919 do_phy_reset = 1;
1920 do {
1921 if (do_phy_reset) {
1922 err = tg3_bmcr_reset(tp);
1923 if (err)
1924 return err;
1925 do_phy_reset = 0;
1928 /* Disable transmitter and interrupt. */
1929 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1930 continue;
1932 reg32 |= 0x3000;
1933 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1935 /* Set full-duplex, 1000 mbps. */
1936 tg3_writephy(tp, MII_BMCR,
1937 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1939 /* Set to master mode. */
1940 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1941 continue;
1943 tg3_writephy(tp, MII_TG3_CTRL,
1944 (MII_TG3_CTRL_AS_MASTER |
1945 MII_TG3_CTRL_ENABLE_AS_MASTER));
1947 /* Enable SM_DSP_CLOCK and 6dB. */
1948 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1950 /* Block the PHY control access. */
1951 tg3_phydsp_write(tp, 0x8005, 0x0800);
1953 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1954 if (!err)
1955 break;
1956 } while (--retries);
1958 err = tg3_phy_reset_chanpat(tp);
1959 if (err)
1960 return err;
1962 tg3_phydsp_write(tp, 0x8005, 0x0000);
1964 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1965 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
1967 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1968 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1969 /* Set Extended packet length bit for jumbo frames */
1970 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
1971 } else {
1972 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1975 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1977 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1978 reg32 &= ~0x3000;
1979 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1980 } else if (!err)
1981 err = -EBUSY;
1983 return err;
1986 /* This will reset the tigon3 PHY if there is no valid
1987 * link unless the FORCE argument is non-zero.
1989 static int tg3_phy_reset(struct tg3 *tp)
1991 u32 val, cpmuctrl;
1992 int err;
1994 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1995 val = tr32(GRC_MISC_CFG);
1996 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1997 udelay(40);
1999 err = tg3_readphy(tp, MII_BMSR, &val);
2000 err |= tg3_readphy(tp, MII_BMSR, &val);
2001 if (err != 0)
2002 return -EBUSY;
2004 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2005 netif_carrier_off(tp->dev);
2006 tg3_link_report(tp);
2009 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2011 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2012 err = tg3_phy_reset_5703_4_5(tp);
2013 if (err)
2014 return err;
2015 goto out;
2018 cpmuctrl = 0;
2019 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2020 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2021 cpmuctrl = tr32(TG3_CPMU_CTRL);
2022 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2023 tw32(TG3_CPMU_CTRL,
2024 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2027 err = tg3_bmcr_reset(tp);
2028 if (err)
2029 return err;
2031 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
2032 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2033 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
2035 tw32(TG3_CPMU_CTRL, cpmuctrl);
2038 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2039 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
2040 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2041 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2042 CPMU_LSPD_1000MB_MACCLK_12_5) {
2043 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2044 udelay(40);
2045 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2049 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
2050 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
2051 return 0;
2053 tg3_phy_apply_otp(tp);
2055 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
2056 tg3_phy_toggle_apd(tp, true);
2057 else
2058 tg3_phy_toggle_apd(tp, false);
2060 out:
2061 if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
2062 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2063 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2064 tg3_phydsp_write(tp, 0x000a, 0x0323);
2065 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2067 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
2068 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2069 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2071 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
2072 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2073 tg3_phydsp_write(tp, 0x000a, 0x310b);
2074 tg3_phydsp_write(tp, 0x201f, 0x9506);
2075 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2076 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2077 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
2078 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2079 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2080 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2081 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2082 tg3_writephy(tp, MII_TG3_TEST1,
2083 MII_TG3_TEST1_TRIM_EN | 0x4);
2084 } else
2085 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2086 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2088 /* Set Extended packet length bit (bit 14) on all chips that */
2089 /* support jumbo frames */
2090 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2091 /* Cannot do read-modify-write on 5401 */
2092 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2093 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
2094 /* Set bit 14 with read-modify-write to preserve other bits */
2095 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
2096 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
2097 tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
2100 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2101 * jumbo frames transmission.
2103 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
2104 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
2105 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2106 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
2109 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2110 /* adjust output voltage */
2111 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
2114 tg3_phy_toggle_automdix(tp, 1);
2115 tg3_phy_set_wirespeed(tp);
2116 return 0;
2119 static void tg3_frob_aux_power(struct tg3 *tp)
2121 bool need_vaux = false;
2123 /* The GPIOs do something completely different on 57765. */
2124 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
2125 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2126 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
2127 return;
2129 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2130 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2131 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2132 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
2133 tp->pdev_peer != tp->pdev) {
2134 struct net_device *dev_peer;
2136 dev_peer = pci_get_drvdata(tp->pdev_peer);
2138 /* remove_one() may have been run on the peer. */
2139 if (dev_peer) {
2140 struct tg3 *tp_peer = netdev_priv(dev_peer);
2142 if (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE)
2143 return;
2145 if ((tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2146 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF))
2147 need_vaux = true;
2151 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2152 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
2153 need_vaux = true;
2155 if (need_vaux) {
2156 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2158 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2159 (GRC_LCLCTRL_GPIO_OE0 |
2160 GRC_LCLCTRL_GPIO_OE1 |
2161 GRC_LCLCTRL_GPIO_OE2 |
2162 GRC_LCLCTRL_GPIO_OUTPUT0 |
2163 GRC_LCLCTRL_GPIO_OUTPUT1),
2164 100);
2165 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2166 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2167 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2168 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2169 GRC_LCLCTRL_GPIO_OE1 |
2170 GRC_LCLCTRL_GPIO_OE2 |
2171 GRC_LCLCTRL_GPIO_OUTPUT0 |
2172 GRC_LCLCTRL_GPIO_OUTPUT1 |
2173 tp->grc_local_ctrl;
2174 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2176 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2177 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2179 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2180 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2181 } else {
2182 u32 no_gpio2;
2183 u32 grc_local_ctrl = 0;
2185 /* Workaround to prevent overdrawing Amps. */
2186 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2187 ASIC_REV_5714) {
2188 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2189 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2190 grc_local_ctrl, 100);
2193 /* On 5753 and variants, GPIO2 cannot be used. */
2194 no_gpio2 = tp->nic_sram_data_cfg &
2195 NIC_SRAM_DATA_CFG_NO_GPIO2;
2197 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2198 GRC_LCLCTRL_GPIO_OE1 |
2199 GRC_LCLCTRL_GPIO_OE2 |
2200 GRC_LCLCTRL_GPIO_OUTPUT1 |
2201 GRC_LCLCTRL_GPIO_OUTPUT2;
2202 if (no_gpio2) {
2203 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2204 GRC_LCLCTRL_GPIO_OUTPUT2);
2206 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2207 grc_local_ctrl, 100);
2209 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2211 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2212 grc_local_ctrl, 100);
2214 if (!no_gpio2) {
2215 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2216 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2217 grc_local_ctrl, 100);
2220 } else {
2221 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2222 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2223 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2224 (GRC_LCLCTRL_GPIO_OE1 |
2225 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
2227 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2228 GRC_LCLCTRL_GPIO_OE1, 100);
2230 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2231 (GRC_LCLCTRL_GPIO_OE1 |
2232 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
2237 static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2239 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2240 return 1;
2241 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
2242 if (speed != SPEED_10)
2243 return 1;
2244 } else if (speed == SPEED_10)
2245 return 1;
2247 return 0;
2250 static int tg3_setup_phy(struct tg3 *, int);
2252 #define RESET_KIND_SHUTDOWN 0
2253 #define RESET_KIND_INIT 1
2254 #define RESET_KIND_SUSPEND 2
2256 static void tg3_write_sig_post_reset(struct tg3 *, int);
2257 static int tg3_halt_cpu(struct tg3 *, u32);
2259 static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
2261 u32 val;
2263 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
2264 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2265 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2266 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2268 sg_dig_ctrl |=
2269 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2270 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2271 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2273 return;
2276 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2277 tg3_bmcr_reset(tp);
2278 val = tr32(GRC_MISC_CFG);
2279 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2280 udelay(40);
2281 return;
2282 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
2283 u32 phytest;
2284 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2285 u32 phy;
2287 tg3_writephy(tp, MII_ADVERTISE, 0);
2288 tg3_writephy(tp, MII_BMCR,
2289 BMCR_ANENABLE | BMCR_ANRESTART);
2291 tg3_writephy(tp, MII_TG3_FET_TEST,
2292 phytest | MII_TG3_FET_SHADOW_EN);
2293 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2294 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2295 tg3_writephy(tp,
2296 MII_TG3_FET_SHDW_AUXMODE4,
2297 phy);
2299 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2301 return;
2302 } else if (do_low_power) {
2303 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2304 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
2306 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2307 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2308 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2309 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2310 MII_TG3_AUXCTL_PCTL_VREG_11V);
2313 /* The PHY should not be powered down on some chips because
2314 * of bugs.
2316 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2317 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2318 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2319 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
2320 return;
2322 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2323 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
2324 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2325 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2326 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2327 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2330 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2333 /* tp->lock is held. */
2334 static int tg3_nvram_lock(struct tg3 *tp)
2336 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2337 int i;
2339 if (tp->nvram_lock_cnt == 0) {
2340 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2341 for (i = 0; i < 8000; i++) {
2342 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2343 break;
2344 udelay(20);
2346 if (i == 8000) {
2347 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2348 return -ENODEV;
2351 tp->nvram_lock_cnt++;
2353 return 0;
2356 /* tp->lock is held. */
2357 static void tg3_nvram_unlock(struct tg3 *tp)
2359 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2360 if (tp->nvram_lock_cnt > 0)
2361 tp->nvram_lock_cnt--;
2362 if (tp->nvram_lock_cnt == 0)
2363 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2367 /* tp->lock is held. */
2368 static void tg3_enable_nvram_access(struct tg3 *tp)
2370 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2371 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
2372 u32 nvaccess = tr32(NVRAM_ACCESS);
2374 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2378 /* tp->lock is held. */
2379 static void tg3_disable_nvram_access(struct tg3 *tp)
2381 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2382 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
2383 u32 nvaccess = tr32(NVRAM_ACCESS);
2385 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2389 static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2390 u32 offset, u32 *val)
2392 u32 tmp;
2393 int i;
2395 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2396 return -EINVAL;
2398 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2399 EEPROM_ADDR_DEVID_MASK |
2400 EEPROM_ADDR_READ);
2401 tw32(GRC_EEPROM_ADDR,
2402 tmp |
2403 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2404 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2405 EEPROM_ADDR_ADDR_MASK) |
2406 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2408 for (i = 0; i < 1000; i++) {
2409 tmp = tr32(GRC_EEPROM_ADDR);
2411 if (tmp & EEPROM_ADDR_COMPLETE)
2412 break;
2413 msleep(1);
2415 if (!(tmp & EEPROM_ADDR_COMPLETE))
2416 return -EBUSY;
2418 tmp = tr32(GRC_EEPROM_DATA);
2421 * The data will always be opposite the native endian
2422 * format. Perform a blind byteswap to compensate.
2424 *val = swab32(tmp);
2426 return 0;
2429 #define NVRAM_CMD_TIMEOUT 10000
2431 static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2433 int i;
2435 tw32(NVRAM_CMD, nvram_cmd);
2436 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2437 udelay(10);
2438 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2439 udelay(10);
2440 break;
2444 if (i == NVRAM_CMD_TIMEOUT)
2445 return -EBUSY;
2447 return 0;
2450 static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2452 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2453 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2454 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2455 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2456 (tp->nvram_jedecnum == JEDEC_ATMEL))
2458 addr = ((addr / tp->nvram_pagesize) <<
2459 ATMEL_AT45DB0X1B_PAGE_POS) +
2460 (addr % tp->nvram_pagesize);
2462 return addr;
2465 static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2467 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2468 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2469 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2470 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2471 (tp->nvram_jedecnum == JEDEC_ATMEL))
2473 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2474 tp->nvram_pagesize) +
2475 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2477 return addr;
2480 /* NOTE: Data read in from NVRAM is byteswapped according to
2481 * the byteswapping settings for all other register accesses.
2482 * tg3 devices are BE devices, so on a BE machine, the data
2483 * returned will be exactly as it is seen in NVRAM. On a LE
2484 * machine, the 32-bit value will be byteswapped.
2486 static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2488 int ret;
2490 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2491 return tg3_nvram_read_using_eeprom(tp, offset, val);
2493 offset = tg3_nvram_phys_addr(tp, offset);
2495 if (offset > NVRAM_ADDR_MSK)
2496 return -EINVAL;
2498 ret = tg3_nvram_lock(tp);
2499 if (ret)
2500 return ret;
2502 tg3_enable_nvram_access(tp);
2504 tw32(NVRAM_ADDR, offset);
2505 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2506 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2508 if (ret == 0)
2509 *val = tr32(NVRAM_RDDATA);
2511 tg3_disable_nvram_access(tp);
2513 tg3_nvram_unlock(tp);
2515 return ret;
2518 /* Ensures NVRAM data is in bytestream format. */
2519 static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
2521 u32 v;
2522 int res = tg3_nvram_read(tp, offset, &v);
2523 if (!res)
2524 *val = cpu_to_be32(v);
2525 return res;
2528 /* tp->lock is held. */
2529 static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2531 u32 addr_high, addr_low;
2532 int i;
2534 addr_high = ((tp->dev->dev_addr[0] << 8) |
2535 tp->dev->dev_addr[1]);
2536 addr_low = ((tp->dev->dev_addr[2] << 24) |
2537 (tp->dev->dev_addr[3] << 16) |
2538 (tp->dev->dev_addr[4] << 8) |
2539 (tp->dev->dev_addr[5] << 0));
2540 for (i = 0; i < 4; i++) {
2541 if (i == 1 && skip_mac_1)
2542 continue;
2543 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2544 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2547 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2548 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2549 for (i = 0; i < 12; i++) {
2550 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2551 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2555 addr_high = (tp->dev->dev_addr[0] +
2556 tp->dev->dev_addr[1] +
2557 tp->dev->dev_addr[2] +
2558 tp->dev->dev_addr[3] +
2559 tp->dev->dev_addr[4] +
2560 tp->dev->dev_addr[5]) &
2561 TX_BACKOFF_SEED_MASK;
2562 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2565 static void tg3_enable_register_access(struct tg3 *tp)
2568 * Make sure register accesses (indirect or otherwise) will function
2569 * correctly.
2571 pci_write_config_dword(tp->pdev,
2572 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2575 static int tg3_power_up(struct tg3 *tp)
2577 tg3_enable_register_access(tp);
2579 pci_set_power_state(tp->pdev, PCI_D0);
2581 /* Switch out of Vaux if it is a NIC */
2582 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
2583 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2585 return 0;
2588 static int tg3_power_down_prepare(struct tg3 *tp)
2590 u32 misc_host_ctrl;
2591 bool device_should_wake, do_low_power;
2593 tg3_enable_register_access(tp);
2595 /* Restore the CLKREQ setting. */
2596 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2597 u16 lnkctl;
2599 pci_read_config_word(tp->pdev,
2600 tp->pcie_cap + PCI_EXP_LNKCTL,
2601 &lnkctl);
2602 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2603 pci_write_config_word(tp->pdev,
2604 tp->pcie_cap + PCI_EXP_LNKCTL,
2605 lnkctl);
2608 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2609 tw32(TG3PCI_MISC_HOST_CTRL,
2610 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2612 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
2613 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2615 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
2616 do_low_power = false;
2617 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
2618 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2619 struct phy_device *phydev;
2620 u32 phyid, advertising;
2622 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
2624 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
2626 tp->link_config.orig_speed = phydev->speed;
2627 tp->link_config.orig_duplex = phydev->duplex;
2628 tp->link_config.orig_autoneg = phydev->autoneg;
2629 tp->link_config.orig_advertising = phydev->advertising;
2631 advertising = ADVERTISED_TP |
2632 ADVERTISED_Pause |
2633 ADVERTISED_Autoneg |
2634 ADVERTISED_10baseT_Half;
2636 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2637 device_should_wake) {
2638 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2639 advertising |=
2640 ADVERTISED_100baseT_Half |
2641 ADVERTISED_100baseT_Full |
2642 ADVERTISED_10baseT_Full;
2643 else
2644 advertising |= ADVERTISED_10baseT_Full;
2647 phydev->advertising = advertising;
2649 phy_start_aneg(phydev);
2651 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
2652 if (phyid != PHY_ID_BCMAC131) {
2653 phyid &= PHY_BCM_OUI_MASK;
2654 if (phyid == PHY_BCM_OUI_1 ||
2655 phyid == PHY_BCM_OUI_2 ||
2656 phyid == PHY_BCM_OUI_3)
2657 do_low_power = true;
2660 } else {
2661 do_low_power = true;
2663 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2664 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
2665 tp->link_config.orig_speed = tp->link_config.speed;
2666 tp->link_config.orig_duplex = tp->link_config.duplex;
2667 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2670 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
2671 tp->link_config.speed = SPEED_10;
2672 tp->link_config.duplex = DUPLEX_HALF;
2673 tp->link_config.autoneg = AUTONEG_ENABLE;
2674 tg3_setup_phy(tp, 0);
2678 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2679 u32 val;
2681 val = tr32(GRC_VCPU_EXT_CTRL);
2682 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2683 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
2684 int i;
2685 u32 val;
2687 for (i = 0; i < 200; i++) {
2688 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2689 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2690 break;
2691 msleep(1);
2694 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2695 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2696 WOL_DRV_STATE_SHUTDOWN |
2697 WOL_DRV_WOL |
2698 WOL_SET_MAGIC_PKT);
2700 if (device_should_wake) {
2701 u32 mac_mode;
2703 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
2704 if (do_low_power) {
2705 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2706 udelay(40);
2709 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
2710 mac_mode = MAC_MODE_PORT_MODE_GMII;
2711 else
2712 mac_mode = MAC_MODE_PORT_MODE_MII;
2714 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2715 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2716 ASIC_REV_5700) {
2717 u32 speed = (tp->tg3_flags &
2718 TG3_FLAG_WOL_SPEED_100MB) ?
2719 SPEED_100 : SPEED_10;
2720 if (tg3_5700_link_polarity(tp, speed))
2721 mac_mode |= MAC_MODE_LINK_POLARITY;
2722 else
2723 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2725 } else {
2726 mac_mode = MAC_MODE_PORT_MODE_TBI;
2729 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
2730 tw32(MAC_LED_CTRL, tp->led_ctrl);
2732 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2733 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2734 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2735 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2736 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2737 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
2739 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
2740 mac_mode |= MAC_MODE_APE_TX_EN |
2741 MAC_MODE_APE_RX_EN |
2742 MAC_MODE_TDE_ENABLE;
2744 tw32_f(MAC_MODE, mac_mode);
2745 udelay(100);
2747 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2748 udelay(10);
2751 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2752 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2753 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2754 u32 base_val;
2756 base_val = tp->pci_clock_ctrl;
2757 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2758 CLOCK_CTRL_TXCLK_DISABLE);
2760 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2761 CLOCK_CTRL_PWRDOWN_PLL133, 40);
2762 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
2763 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
2764 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
2765 /* do nothing */
2766 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2767 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2768 u32 newbits1, newbits2;
2770 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2771 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2772 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2773 CLOCK_CTRL_TXCLK_DISABLE |
2774 CLOCK_CTRL_ALTCLK);
2775 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2776 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2777 newbits1 = CLOCK_CTRL_625_CORE;
2778 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2779 } else {
2780 newbits1 = CLOCK_CTRL_ALTCLK;
2781 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2784 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2785 40);
2787 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2788 40);
2790 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2791 u32 newbits3;
2793 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2794 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2795 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2796 CLOCK_CTRL_TXCLK_DISABLE |
2797 CLOCK_CTRL_44MHZ_CORE);
2798 } else {
2799 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2802 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2803 tp->pci_clock_ctrl | newbits3, 40);
2807 if (!(device_should_wake) &&
2808 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
2809 tg3_power_down_phy(tp, do_low_power);
2811 tg3_frob_aux_power(tp);
2813 /* Workaround for unstable PLL clock */
2814 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2815 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2816 u32 val = tr32(0x7d00);
2818 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2819 tw32(0x7d00, val);
2820 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
2821 int err;
2823 err = tg3_nvram_lock(tp);
2824 tg3_halt_cpu(tp, RX_CPU_BASE);
2825 if (!err)
2826 tg3_nvram_unlock(tp);
2830 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2832 return 0;
2835 static void tg3_power_down(struct tg3 *tp)
2837 tg3_power_down_prepare(tp);
2839 pci_wake_from_d3(tp->pdev, tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2840 pci_set_power_state(tp->pdev, PCI_D3hot);
2843 static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2845 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2846 case MII_TG3_AUX_STAT_10HALF:
2847 *speed = SPEED_10;
2848 *duplex = DUPLEX_HALF;
2849 break;
2851 case MII_TG3_AUX_STAT_10FULL:
2852 *speed = SPEED_10;
2853 *duplex = DUPLEX_FULL;
2854 break;
2856 case MII_TG3_AUX_STAT_100HALF:
2857 *speed = SPEED_100;
2858 *duplex = DUPLEX_HALF;
2859 break;
2861 case MII_TG3_AUX_STAT_100FULL:
2862 *speed = SPEED_100;
2863 *duplex = DUPLEX_FULL;
2864 break;
2866 case MII_TG3_AUX_STAT_1000HALF:
2867 *speed = SPEED_1000;
2868 *duplex = DUPLEX_HALF;
2869 break;
2871 case MII_TG3_AUX_STAT_1000FULL:
2872 *speed = SPEED_1000;
2873 *duplex = DUPLEX_FULL;
2874 break;
2876 default:
2877 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
2878 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2879 SPEED_10;
2880 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2881 DUPLEX_HALF;
2882 break;
2884 *speed = SPEED_INVALID;
2885 *duplex = DUPLEX_INVALID;
2886 break;
2890 static void tg3_phy_copper_begin(struct tg3 *tp)
2892 u32 new_adv;
2893 int i;
2895 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2896 /* Entering low power mode. Disable gigabit and
2897 * 100baseT advertisements.
2899 tg3_writephy(tp, MII_TG3_CTRL, 0);
2901 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2902 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2903 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2904 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2906 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2907 } else if (tp->link_config.speed == SPEED_INVALID) {
2908 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
2909 tp->link_config.advertising &=
2910 ~(ADVERTISED_1000baseT_Half |
2911 ADVERTISED_1000baseT_Full);
2913 new_adv = ADVERTISE_CSMA;
2914 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2915 new_adv |= ADVERTISE_10HALF;
2916 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2917 new_adv |= ADVERTISE_10FULL;
2918 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2919 new_adv |= ADVERTISE_100HALF;
2920 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2921 new_adv |= ADVERTISE_100FULL;
2923 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2925 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2927 if (tp->link_config.advertising &
2928 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2929 new_adv = 0;
2930 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2931 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2932 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2933 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2934 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
2935 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2936 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2937 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2938 MII_TG3_CTRL_ENABLE_AS_MASTER);
2939 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2940 } else {
2941 tg3_writephy(tp, MII_TG3_CTRL, 0);
2943 } else {
2944 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2945 new_adv |= ADVERTISE_CSMA;
2947 /* Asking for a specific link mode. */
2948 if (tp->link_config.speed == SPEED_1000) {
2949 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2951 if (tp->link_config.duplex == DUPLEX_FULL)
2952 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2953 else
2954 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2955 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2956 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2957 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2958 MII_TG3_CTRL_ENABLE_AS_MASTER);
2959 } else {
2960 if (tp->link_config.speed == SPEED_100) {
2961 if (tp->link_config.duplex == DUPLEX_FULL)
2962 new_adv |= ADVERTISE_100FULL;
2963 else
2964 new_adv |= ADVERTISE_100HALF;
2965 } else {
2966 if (tp->link_config.duplex == DUPLEX_FULL)
2967 new_adv |= ADVERTISE_10FULL;
2968 else
2969 new_adv |= ADVERTISE_10HALF;
2971 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2973 new_adv = 0;
2976 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2979 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
2980 u32 val;
2982 tw32(TG3_CPMU_EEE_MODE,
2983 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2985 /* Enable SM_DSP clock and tx 6dB coding. */
2986 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
2987 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
2988 MII_TG3_AUXCTL_ACTL_TX_6DB;
2989 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2991 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2992 case ASIC_REV_5717:
2993 case ASIC_REV_57765:
2994 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2995 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2996 MII_TG3_DSP_CH34TP2_HIBW01);
2997 /* Fall through */
2998 case ASIC_REV_5719:
2999 val = MII_TG3_DSP_TAP26_ALNOKO |
3000 MII_TG3_DSP_TAP26_RMRXSTO |
3001 MII_TG3_DSP_TAP26_OPCSINPT;
3002 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3005 val = 0;
3006 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3007 /* Advertise 100-BaseTX EEE ability */
3008 if (tp->link_config.advertising &
3009 ADVERTISED_100baseT_Full)
3010 val |= MDIO_AN_EEE_ADV_100TX;
3011 /* Advertise 1000-BaseT EEE ability */
3012 if (tp->link_config.advertising &
3013 ADVERTISED_1000baseT_Full)
3014 val |= MDIO_AN_EEE_ADV_1000T;
3016 tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
3018 /* Turn off SM_DSP clock. */
3019 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
3020 MII_TG3_AUXCTL_ACTL_TX_6DB;
3021 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3024 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3025 tp->link_config.speed != SPEED_INVALID) {
3026 u32 bmcr, orig_bmcr;
3028 tp->link_config.active_speed = tp->link_config.speed;
3029 tp->link_config.active_duplex = tp->link_config.duplex;
3031 bmcr = 0;
3032 switch (tp->link_config.speed) {
3033 default:
3034 case SPEED_10:
3035 break;
3037 case SPEED_100:
3038 bmcr |= BMCR_SPEED100;
3039 break;
3041 case SPEED_1000:
3042 bmcr |= TG3_BMCR_SPEED1000;
3043 break;
3046 if (tp->link_config.duplex == DUPLEX_FULL)
3047 bmcr |= BMCR_FULLDPLX;
3049 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3050 (bmcr != orig_bmcr)) {
3051 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3052 for (i = 0; i < 1500; i++) {
3053 u32 tmp;
3055 udelay(10);
3056 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3057 tg3_readphy(tp, MII_BMSR, &tmp))
3058 continue;
3059 if (!(tmp & BMSR_LSTATUS)) {
3060 udelay(40);
3061 break;
3064 tg3_writephy(tp, MII_BMCR, bmcr);
3065 udelay(40);
3067 } else {
3068 tg3_writephy(tp, MII_BMCR,
3069 BMCR_ANENABLE | BMCR_ANRESTART);
3073 static int tg3_init_5401phy_dsp(struct tg3 *tp)
3075 int err;
3077 /* Turn off tap power management. */
3078 /* Set Extended packet length bit */
3079 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
3081 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3082 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3083 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3084 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3085 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
3087 udelay(40);
3089 return err;
3092 static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
3094 u32 adv_reg, all_mask = 0;
3096 if (mask & ADVERTISED_10baseT_Half)
3097 all_mask |= ADVERTISE_10HALF;
3098 if (mask & ADVERTISED_10baseT_Full)
3099 all_mask |= ADVERTISE_10FULL;
3100 if (mask & ADVERTISED_100baseT_Half)
3101 all_mask |= ADVERTISE_100HALF;
3102 if (mask & ADVERTISED_100baseT_Full)
3103 all_mask |= ADVERTISE_100FULL;
3105 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3106 return 0;
3108 if ((adv_reg & all_mask) != all_mask)
3109 return 0;
3110 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3111 u32 tg3_ctrl;
3113 all_mask = 0;
3114 if (mask & ADVERTISED_1000baseT_Half)
3115 all_mask |= ADVERTISE_1000HALF;
3116 if (mask & ADVERTISED_1000baseT_Full)
3117 all_mask |= ADVERTISE_1000FULL;
3119 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3120 return 0;
3122 if ((tg3_ctrl & all_mask) != all_mask)
3123 return 0;
3125 return 1;
3128 static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3130 u32 curadv, reqadv;
3132 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3133 return 1;
3135 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3136 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3138 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3139 if (curadv != reqadv)
3140 return 0;
3142 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3143 tg3_readphy(tp, MII_LPA, rmtadv);
3144 } else {
3145 /* Reprogram the advertisement register, even if it
3146 * does not affect the current link. If the link
3147 * gets renegotiated in the future, we can save an
3148 * additional renegotiation cycle by advertising
3149 * it correctly in the first place.
3151 if (curadv != reqadv) {
3152 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3153 ADVERTISE_PAUSE_ASYM);
3154 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3158 return 1;
3161 static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3163 int current_link_up;
3164 u32 bmsr, val;
3165 u32 lcl_adv, rmt_adv;
3166 u16 current_speed;
3167 u8 current_duplex;
3168 int i, err;
3170 tw32(MAC_EVENT, 0);
3172 tw32_f(MAC_STATUS,
3173 (MAC_STATUS_SYNC_CHANGED |
3174 MAC_STATUS_CFG_CHANGED |
3175 MAC_STATUS_MI_COMPLETION |
3176 MAC_STATUS_LNKSTATE_CHANGED));
3177 udelay(40);
3179 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3180 tw32_f(MAC_MI_MODE,
3181 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3182 udelay(80);
3185 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3187 /* Some third-party PHYs need to be reset on link going
3188 * down.
3190 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3191 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3192 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3193 netif_carrier_ok(tp->dev)) {
3194 tg3_readphy(tp, MII_BMSR, &bmsr);
3195 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3196 !(bmsr & BMSR_LSTATUS))
3197 force_reset = 1;
3199 if (force_reset)
3200 tg3_phy_reset(tp);
3202 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
3203 tg3_readphy(tp, MII_BMSR, &bmsr);
3204 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3205 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3206 bmsr = 0;
3208 if (!(bmsr & BMSR_LSTATUS)) {
3209 err = tg3_init_5401phy_dsp(tp);
3210 if (err)
3211 return err;
3213 tg3_readphy(tp, MII_BMSR, &bmsr);
3214 for (i = 0; i < 1000; i++) {
3215 udelay(10);
3216 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3217 (bmsr & BMSR_LSTATUS)) {
3218 udelay(40);
3219 break;
3223 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3224 TG3_PHY_REV_BCM5401_B0 &&
3225 !(bmsr & BMSR_LSTATUS) &&
3226 tp->link_config.active_speed == SPEED_1000) {
3227 err = tg3_phy_reset(tp);
3228 if (!err)
3229 err = tg3_init_5401phy_dsp(tp);
3230 if (err)
3231 return err;
3234 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3235 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3236 /* 5701 {A0,B0} CRC bug workaround */
3237 tg3_writephy(tp, 0x15, 0x0a75);
3238 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3239 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3240 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3243 /* Clear pending interrupts... */
3244 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3245 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3247 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
3248 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
3249 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
3250 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3252 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3253 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3254 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3255 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3256 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3257 else
3258 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3261 current_link_up = 0;
3262 current_speed = SPEED_INVALID;
3263 current_duplex = DUPLEX_INVALID;
3265 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
3266 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3267 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3268 if (!(val & (1 << 10))) {
3269 val |= (1 << 10);
3270 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3271 goto relink;
3275 bmsr = 0;
3276 for (i = 0; i < 100; i++) {
3277 tg3_readphy(tp, MII_BMSR, &bmsr);
3278 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3279 (bmsr & BMSR_LSTATUS))
3280 break;
3281 udelay(40);
3284 if (bmsr & BMSR_LSTATUS) {
3285 u32 aux_stat, bmcr;
3287 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3288 for (i = 0; i < 2000; i++) {
3289 udelay(10);
3290 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3291 aux_stat)
3292 break;
3295 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3296 &current_speed,
3297 &current_duplex);
3299 bmcr = 0;
3300 for (i = 0; i < 200; i++) {
3301 tg3_readphy(tp, MII_BMCR, &bmcr);
3302 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3303 continue;
3304 if (bmcr && bmcr != 0x7fff)
3305 break;
3306 udelay(10);
3309 lcl_adv = 0;
3310 rmt_adv = 0;
3312 tp->link_config.active_speed = current_speed;
3313 tp->link_config.active_duplex = current_duplex;
3315 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3316 if ((bmcr & BMCR_ANENABLE) &&
3317 tg3_copper_is_advertising_all(tp,
3318 tp->link_config.advertising)) {
3319 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3320 &rmt_adv))
3321 current_link_up = 1;
3323 } else {
3324 if (!(bmcr & BMCR_ANENABLE) &&
3325 tp->link_config.speed == current_speed &&
3326 tp->link_config.duplex == current_duplex &&
3327 tp->link_config.flowctrl ==
3328 tp->link_config.active_flowctrl) {
3329 current_link_up = 1;
3333 if (current_link_up == 1 &&
3334 tp->link_config.active_duplex == DUPLEX_FULL)
3335 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
3338 relink:
3339 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3340 tg3_phy_copper_begin(tp);
3342 tg3_readphy(tp, MII_BMSR, &bmsr);
3343 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3344 (bmsr & BMSR_LSTATUS))
3345 current_link_up = 1;
3348 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3349 if (current_link_up == 1) {
3350 if (tp->link_config.active_speed == SPEED_100 ||
3351 tp->link_config.active_speed == SPEED_10)
3352 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3353 else
3354 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3355 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
3356 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3357 else
3358 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3360 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3361 if (tp->link_config.active_duplex == DUPLEX_HALF)
3362 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3364 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
3365 if (current_link_up == 1 &&
3366 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
3367 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
3368 else
3369 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
3372 /* ??? Without this setting Netgear GA302T PHY does not
3373 * ??? send/receive packets...
3375 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
3376 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3377 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3378 tw32_f(MAC_MI_MODE, tp->mi_mode);
3379 udelay(80);
3382 tw32_f(MAC_MODE, tp->mac_mode);
3383 udelay(40);
3385 tg3_phy_eee_adjust(tp, current_link_up);
3387 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3388 /* Polled via timer. */
3389 tw32_f(MAC_EVENT, 0);
3390 } else {
3391 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3393 udelay(40);
3395 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3396 current_link_up == 1 &&
3397 tp->link_config.active_speed == SPEED_1000 &&
3398 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3399 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3400 udelay(120);
3401 tw32_f(MAC_STATUS,
3402 (MAC_STATUS_SYNC_CHANGED |
3403 MAC_STATUS_CFG_CHANGED));
3404 udelay(40);
3405 tg3_write_mem(tp,
3406 NIC_SRAM_FIRMWARE_MBOX,
3407 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3410 /* Prevent send BD corruption. */
3411 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3412 u16 oldlnkctl, newlnkctl;
3414 pci_read_config_word(tp->pdev,
3415 tp->pcie_cap + PCI_EXP_LNKCTL,
3416 &oldlnkctl);
3417 if (tp->link_config.active_speed == SPEED_100 ||
3418 tp->link_config.active_speed == SPEED_10)
3419 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3420 else
3421 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3422 if (newlnkctl != oldlnkctl)
3423 pci_write_config_word(tp->pdev,
3424 tp->pcie_cap + PCI_EXP_LNKCTL,
3425 newlnkctl);
3428 if (current_link_up != netif_carrier_ok(tp->dev)) {
3429 if (current_link_up)
3430 netif_carrier_on(tp->dev);
3431 else
3432 netif_carrier_off(tp->dev);
3433 tg3_link_report(tp);
3436 return 0;
3439 struct tg3_fiber_aneginfo {
3440 int state;
3441 #define ANEG_STATE_UNKNOWN 0
3442 #define ANEG_STATE_AN_ENABLE 1
3443 #define ANEG_STATE_RESTART_INIT 2
3444 #define ANEG_STATE_RESTART 3
3445 #define ANEG_STATE_DISABLE_LINK_OK 4
3446 #define ANEG_STATE_ABILITY_DETECT_INIT 5
3447 #define ANEG_STATE_ABILITY_DETECT 6
3448 #define ANEG_STATE_ACK_DETECT_INIT 7
3449 #define ANEG_STATE_ACK_DETECT 8
3450 #define ANEG_STATE_COMPLETE_ACK_INIT 9
3451 #define ANEG_STATE_COMPLETE_ACK 10
3452 #define ANEG_STATE_IDLE_DETECT_INIT 11
3453 #define ANEG_STATE_IDLE_DETECT 12
3454 #define ANEG_STATE_LINK_OK 13
3455 #define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3456 #define ANEG_STATE_NEXT_PAGE_WAIT 15
3458 u32 flags;
3459 #define MR_AN_ENABLE 0x00000001
3460 #define MR_RESTART_AN 0x00000002
3461 #define MR_AN_COMPLETE 0x00000004
3462 #define MR_PAGE_RX 0x00000008
3463 #define MR_NP_LOADED 0x00000010
3464 #define MR_TOGGLE_TX 0x00000020
3465 #define MR_LP_ADV_FULL_DUPLEX 0x00000040
3466 #define MR_LP_ADV_HALF_DUPLEX 0x00000080
3467 #define MR_LP_ADV_SYM_PAUSE 0x00000100
3468 #define MR_LP_ADV_ASYM_PAUSE 0x00000200
3469 #define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3470 #define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3471 #define MR_LP_ADV_NEXT_PAGE 0x00001000
3472 #define MR_TOGGLE_RX 0x00002000
3473 #define MR_NP_RX 0x00004000
3475 #define MR_LINK_OK 0x80000000
3477 unsigned long link_time, cur_time;
3479 u32 ability_match_cfg;
3480 int ability_match_count;
3482 char ability_match, idle_match, ack_match;
3484 u32 txconfig, rxconfig;
3485 #define ANEG_CFG_NP 0x00000080
3486 #define ANEG_CFG_ACK 0x00000040
3487 #define ANEG_CFG_RF2 0x00000020
3488 #define ANEG_CFG_RF1 0x00000010
3489 #define ANEG_CFG_PS2 0x00000001
3490 #define ANEG_CFG_PS1 0x00008000
3491 #define ANEG_CFG_HD 0x00004000
3492 #define ANEG_CFG_FD 0x00002000
3493 #define ANEG_CFG_INVAL 0x00001f06
3496 #define ANEG_OK 0
3497 #define ANEG_DONE 1
3498 #define ANEG_TIMER_ENAB 2
3499 #define ANEG_FAILED -1
3501 #define ANEG_STATE_SETTLE_TIME 10000
3503 static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3504 struct tg3_fiber_aneginfo *ap)
3506 u16 flowctrl;
3507 unsigned long delta;
3508 u32 rx_cfg_reg;
3509 int ret;
3511 if (ap->state == ANEG_STATE_UNKNOWN) {
3512 ap->rxconfig = 0;
3513 ap->link_time = 0;
3514 ap->cur_time = 0;
3515 ap->ability_match_cfg = 0;
3516 ap->ability_match_count = 0;
3517 ap->ability_match = 0;
3518 ap->idle_match = 0;
3519 ap->ack_match = 0;
3521 ap->cur_time++;
3523 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3524 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3526 if (rx_cfg_reg != ap->ability_match_cfg) {
3527 ap->ability_match_cfg = rx_cfg_reg;
3528 ap->ability_match = 0;
3529 ap->ability_match_count = 0;
3530 } else {
3531 if (++ap->ability_match_count > 1) {
3532 ap->ability_match = 1;
3533 ap->ability_match_cfg = rx_cfg_reg;
3536 if (rx_cfg_reg & ANEG_CFG_ACK)
3537 ap->ack_match = 1;
3538 else
3539 ap->ack_match = 0;
3541 ap->idle_match = 0;
3542 } else {
3543 ap->idle_match = 1;
3544 ap->ability_match_cfg = 0;
3545 ap->ability_match_count = 0;
3546 ap->ability_match = 0;
3547 ap->ack_match = 0;
3549 rx_cfg_reg = 0;
3552 ap->rxconfig = rx_cfg_reg;
3553 ret = ANEG_OK;
3555 switch (ap->state) {
3556 case ANEG_STATE_UNKNOWN:
3557 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3558 ap->state = ANEG_STATE_AN_ENABLE;
3560 /* fallthru */
3561 case ANEG_STATE_AN_ENABLE:
3562 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3563 if (ap->flags & MR_AN_ENABLE) {
3564 ap->link_time = 0;
3565 ap->cur_time = 0;
3566 ap->ability_match_cfg = 0;
3567 ap->ability_match_count = 0;
3568 ap->ability_match = 0;
3569 ap->idle_match = 0;
3570 ap->ack_match = 0;
3572 ap->state = ANEG_STATE_RESTART_INIT;
3573 } else {
3574 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3576 break;
3578 case ANEG_STATE_RESTART_INIT:
3579 ap->link_time = ap->cur_time;
3580 ap->flags &= ~(MR_NP_LOADED);
3581 ap->txconfig = 0;
3582 tw32(MAC_TX_AUTO_NEG, 0);
3583 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3584 tw32_f(MAC_MODE, tp->mac_mode);
3585 udelay(40);
3587 ret = ANEG_TIMER_ENAB;
3588 ap->state = ANEG_STATE_RESTART;
3590 /* fallthru */
3591 case ANEG_STATE_RESTART:
3592 delta = ap->cur_time - ap->link_time;
3593 if (delta > ANEG_STATE_SETTLE_TIME)
3594 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3595 else
3596 ret = ANEG_TIMER_ENAB;
3597 break;
3599 case ANEG_STATE_DISABLE_LINK_OK:
3600 ret = ANEG_DONE;
3601 break;
3603 case ANEG_STATE_ABILITY_DETECT_INIT:
3604 ap->flags &= ~(MR_TOGGLE_TX);
3605 ap->txconfig = ANEG_CFG_FD;
3606 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3607 if (flowctrl & ADVERTISE_1000XPAUSE)
3608 ap->txconfig |= ANEG_CFG_PS1;
3609 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3610 ap->txconfig |= ANEG_CFG_PS2;
3611 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3612 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3613 tw32_f(MAC_MODE, tp->mac_mode);
3614 udelay(40);
3616 ap->state = ANEG_STATE_ABILITY_DETECT;
3617 break;
3619 case ANEG_STATE_ABILITY_DETECT:
3620 if (ap->ability_match != 0 && ap->rxconfig != 0)
3621 ap->state = ANEG_STATE_ACK_DETECT_INIT;
3622 break;
3624 case ANEG_STATE_ACK_DETECT_INIT:
3625 ap->txconfig |= ANEG_CFG_ACK;
3626 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3627 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3628 tw32_f(MAC_MODE, tp->mac_mode);
3629 udelay(40);
3631 ap->state = ANEG_STATE_ACK_DETECT;
3633 /* fallthru */
3634 case ANEG_STATE_ACK_DETECT:
3635 if (ap->ack_match != 0) {
3636 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3637 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3638 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3639 } else {
3640 ap->state = ANEG_STATE_AN_ENABLE;
3642 } else if (ap->ability_match != 0 &&
3643 ap->rxconfig == 0) {
3644 ap->state = ANEG_STATE_AN_ENABLE;
3646 break;
3648 case ANEG_STATE_COMPLETE_ACK_INIT:
3649 if (ap->rxconfig & ANEG_CFG_INVAL) {
3650 ret = ANEG_FAILED;
3651 break;
3653 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3654 MR_LP_ADV_HALF_DUPLEX |
3655 MR_LP_ADV_SYM_PAUSE |
3656 MR_LP_ADV_ASYM_PAUSE |
3657 MR_LP_ADV_REMOTE_FAULT1 |
3658 MR_LP_ADV_REMOTE_FAULT2 |
3659 MR_LP_ADV_NEXT_PAGE |
3660 MR_TOGGLE_RX |
3661 MR_NP_RX);
3662 if (ap->rxconfig & ANEG_CFG_FD)
3663 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3664 if (ap->rxconfig & ANEG_CFG_HD)
3665 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3666 if (ap->rxconfig & ANEG_CFG_PS1)
3667 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3668 if (ap->rxconfig & ANEG_CFG_PS2)
3669 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3670 if (ap->rxconfig & ANEG_CFG_RF1)
3671 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3672 if (ap->rxconfig & ANEG_CFG_RF2)
3673 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3674 if (ap->rxconfig & ANEG_CFG_NP)
3675 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3677 ap->link_time = ap->cur_time;
3679 ap->flags ^= (MR_TOGGLE_TX);
3680 if (ap->rxconfig & 0x0008)
3681 ap->flags |= MR_TOGGLE_RX;
3682 if (ap->rxconfig & ANEG_CFG_NP)
3683 ap->flags |= MR_NP_RX;
3684 ap->flags |= MR_PAGE_RX;
3686 ap->state = ANEG_STATE_COMPLETE_ACK;
3687 ret = ANEG_TIMER_ENAB;
3688 break;
3690 case ANEG_STATE_COMPLETE_ACK:
3691 if (ap->ability_match != 0 &&
3692 ap->rxconfig == 0) {
3693 ap->state = ANEG_STATE_AN_ENABLE;
3694 break;
3696 delta = ap->cur_time - ap->link_time;
3697 if (delta > ANEG_STATE_SETTLE_TIME) {
3698 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3699 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3700 } else {
3701 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3702 !(ap->flags & MR_NP_RX)) {
3703 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3704 } else {
3705 ret = ANEG_FAILED;
3709 break;
3711 case ANEG_STATE_IDLE_DETECT_INIT:
3712 ap->link_time = ap->cur_time;
3713 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3714 tw32_f(MAC_MODE, tp->mac_mode);
3715 udelay(40);
3717 ap->state = ANEG_STATE_IDLE_DETECT;
3718 ret = ANEG_TIMER_ENAB;
3719 break;
3721 case ANEG_STATE_IDLE_DETECT:
3722 if (ap->ability_match != 0 &&
3723 ap->rxconfig == 0) {
3724 ap->state = ANEG_STATE_AN_ENABLE;
3725 break;
3727 delta = ap->cur_time - ap->link_time;
3728 if (delta > ANEG_STATE_SETTLE_TIME) {
3729 /* XXX another gem from the Broadcom driver :( */
3730 ap->state = ANEG_STATE_LINK_OK;
3732 break;
3734 case ANEG_STATE_LINK_OK:
3735 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3736 ret = ANEG_DONE;
3737 break;
3739 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3740 /* ??? unimplemented */
3741 break;
3743 case ANEG_STATE_NEXT_PAGE_WAIT:
3744 /* ??? unimplemented */
3745 break;
3747 default:
3748 ret = ANEG_FAILED;
3749 break;
3752 return ret;
3755 static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
3757 int res = 0;
3758 struct tg3_fiber_aneginfo aninfo;
3759 int status = ANEG_FAILED;
3760 unsigned int tick;
3761 u32 tmp;
3763 tw32_f(MAC_TX_AUTO_NEG, 0);
3765 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3766 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3767 udelay(40);
3769 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3770 udelay(40);
3772 memset(&aninfo, 0, sizeof(aninfo));
3773 aninfo.flags |= MR_AN_ENABLE;
3774 aninfo.state = ANEG_STATE_UNKNOWN;
3775 aninfo.cur_time = 0;
3776 tick = 0;
3777 while (++tick < 195000) {
3778 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3779 if (status == ANEG_DONE || status == ANEG_FAILED)
3780 break;
3782 udelay(1);
3785 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3786 tw32_f(MAC_MODE, tp->mac_mode);
3787 udelay(40);
3789 *txflags = aninfo.txconfig;
3790 *rxflags = aninfo.flags;
3792 if (status == ANEG_DONE &&
3793 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3794 MR_LP_ADV_FULL_DUPLEX)))
3795 res = 1;
3797 return res;
3800 static void tg3_init_bcm8002(struct tg3 *tp)
3802 u32 mac_status = tr32(MAC_STATUS);
3803 int i;
3805 /* Reset when initting first time or we have a link. */
3806 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3807 !(mac_status & MAC_STATUS_PCS_SYNCED))
3808 return;
3810 /* Set PLL lock range. */
3811 tg3_writephy(tp, 0x16, 0x8007);
3813 /* SW reset */
3814 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3816 /* Wait for reset to complete. */
3817 /* XXX schedule_timeout() ... */
3818 for (i = 0; i < 500; i++)
3819 udelay(10);
3821 /* Config mode; select PMA/Ch 1 regs. */
3822 tg3_writephy(tp, 0x10, 0x8411);
3824 /* Enable auto-lock and comdet, select txclk for tx. */
3825 tg3_writephy(tp, 0x11, 0x0a10);
3827 tg3_writephy(tp, 0x18, 0x00a0);
3828 tg3_writephy(tp, 0x16, 0x41ff);
3830 /* Assert and deassert POR. */
3831 tg3_writephy(tp, 0x13, 0x0400);
3832 udelay(40);
3833 tg3_writephy(tp, 0x13, 0x0000);
3835 tg3_writephy(tp, 0x11, 0x0a50);
3836 udelay(40);
3837 tg3_writephy(tp, 0x11, 0x0a10);
3839 /* Wait for signal to stabilize */
3840 /* XXX schedule_timeout() ... */
3841 for (i = 0; i < 15000; i++)
3842 udelay(10);
3844 /* Deselect the channel register so we can read the PHYID
3845 * later.
3847 tg3_writephy(tp, 0x10, 0x8011);
3850 static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3852 u16 flowctrl;
3853 u32 sg_dig_ctrl, sg_dig_status;
3854 u32 serdes_cfg, expected_sg_dig_ctrl;
3855 int workaround, port_a;
3856 int current_link_up;
3858 serdes_cfg = 0;
3859 expected_sg_dig_ctrl = 0;
3860 workaround = 0;
3861 port_a = 1;
3862 current_link_up = 0;
3864 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3865 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3866 workaround = 1;
3867 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3868 port_a = 0;
3870 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3871 /* preserve bits 20-23 for voltage regulator */
3872 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3875 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3877 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
3878 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
3879 if (workaround) {
3880 u32 val = serdes_cfg;
3882 if (port_a)
3883 val |= 0xc010000;
3884 else
3885 val |= 0x4010000;
3886 tw32_f(MAC_SERDES_CFG, val);
3889 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
3891 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3892 tg3_setup_flow_control(tp, 0, 0);
3893 current_link_up = 1;
3895 goto out;
3898 /* Want auto-negotiation. */
3899 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
3901 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3902 if (flowctrl & ADVERTISE_1000XPAUSE)
3903 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3904 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3905 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
3907 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
3908 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
3909 tp->serdes_counter &&
3910 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3911 MAC_STATUS_RCVD_CFG)) ==
3912 MAC_STATUS_PCS_SYNCED)) {
3913 tp->serdes_counter--;
3914 current_link_up = 1;
3915 goto out;
3917 restart_autoneg:
3918 if (workaround)
3919 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
3920 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
3921 udelay(5);
3922 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3924 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3925 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
3926 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3927 MAC_STATUS_SIGNAL_DET)) {
3928 sg_dig_status = tr32(SG_DIG_STATUS);
3929 mac_status = tr32(MAC_STATUS);
3931 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
3932 (mac_status & MAC_STATUS_PCS_SYNCED)) {
3933 u32 local_adv = 0, remote_adv = 0;
3935 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3936 local_adv |= ADVERTISE_1000XPAUSE;
3937 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3938 local_adv |= ADVERTISE_1000XPSE_ASYM;
3940 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
3941 remote_adv |= LPA_1000XPAUSE;
3942 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
3943 remote_adv |= LPA_1000XPAUSE_ASYM;
3945 tg3_setup_flow_control(tp, local_adv, remote_adv);
3946 current_link_up = 1;
3947 tp->serdes_counter = 0;
3948 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
3949 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
3950 if (tp->serdes_counter)
3951 tp->serdes_counter--;
3952 else {
3953 if (workaround) {
3954 u32 val = serdes_cfg;
3956 if (port_a)
3957 val |= 0xc010000;
3958 else
3959 val |= 0x4010000;
3961 tw32_f(MAC_SERDES_CFG, val);
3964 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
3965 udelay(40);
3967 /* Link parallel detection - link is up */
3968 /* only if we have PCS_SYNC and not */
3969 /* receiving config code words */
3970 mac_status = tr32(MAC_STATUS);
3971 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3972 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3973 tg3_setup_flow_control(tp, 0, 0);
3974 current_link_up = 1;
3975 tp->phy_flags |=
3976 TG3_PHYFLG_PARALLEL_DETECT;
3977 tp->serdes_counter =
3978 SERDES_PARALLEL_DET_TIMEOUT;
3979 } else
3980 goto restart_autoneg;
3983 } else {
3984 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3985 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
3988 out:
3989 return current_link_up;
3992 static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3994 int current_link_up = 0;
3996 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
3997 goto out;
3999 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4000 u32 txflags, rxflags;
4001 int i;
4003 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4004 u32 local_adv = 0, remote_adv = 0;
4006 if (txflags & ANEG_CFG_PS1)
4007 local_adv |= ADVERTISE_1000XPAUSE;
4008 if (txflags & ANEG_CFG_PS2)
4009 local_adv |= ADVERTISE_1000XPSE_ASYM;
4011 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4012 remote_adv |= LPA_1000XPAUSE;
4013 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4014 remote_adv |= LPA_1000XPAUSE_ASYM;
4016 tg3_setup_flow_control(tp, local_adv, remote_adv);
4018 current_link_up = 1;
4020 for (i = 0; i < 30; i++) {
4021 udelay(20);
4022 tw32_f(MAC_STATUS,
4023 (MAC_STATUS_SYNC_CHANGED |
4024 MAC_STATUS_CFG_CHANGED));
4025 udelay(40);
4026 if ((tr32(MAC_STATUS) &
4027 (MAC_STATUS_SYNC_CHANGED |
4028 MAC_STATUS_CFG_CHANGED)) == 0)
4029 break;
4032 mac_status = tr32(MAC_STATUS);
4033 if (current_link_up == 0 &&
4034 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4035 !(mac_status & MAC_STATUS_RCVD_CFG))
4036 current_link_up = 1;
4037 } else {
4038 tg3_setup_flow_control(tp, 0, 0);
4040 /* Forcing 1000FD link up. */
4041 current_link_up = 1;
4043 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4044 udelay(40);
4046 tw32_f(MAC_MODE, tp->mac_mode);
4047 udelay(40);
4050 out:
4051 return current_link_up;
4054 static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4056 u32 orig_pause_cfg;
4057 u16 orig_active_speed;
4058 u8 orig_active_duplex;
4059 u32 mac_status;
4060 int current_link_up;
4061 int i;
4063 orig_pause_cfg = tp->link_config.active_flowctrl;
4064 orig_active_speed = tp->link_config.active_speed;
4065 orig_active_duplex = tp->link_config.active_duplex;
4067 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4068 netif_carrier_ok(tp->dev) &&
4069 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4070 mac_status = tr32(MAC_STATUS);
4071 mac_status &= (MAC_STATUS_PCS_SYNCED |
4072 MAC_STATUS_SIGNAL_DET |
4073 MAC_STATUS_CFG_CHANGED |
4074 MAC_STATUS_RCVD_CFG);
4075 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4076 MAC_STATUS_SIGNAL_DET)) {
4077 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4078 MAC_STATUS_CFG_CHANGED));
4079 return 0;
4083 tw32_f(MAC_TX_AUTO_NEG, 0);
4085 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4086 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4087 tw32_f(MAC_MODE, tp->mac_mode);
4088 udelay(40);
4090 if (tp->phy_id == TG3_PHY_ID_BCM8002)
4091 tg3_init_bcm8002(tp);
4093 /* Enable link change event even when serdes polling. */
4094 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4095 udelay(40);
4097 current_link_up = 0;
4098 mac_status = tr32(MAC_STATUS);
4100 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4101 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4102 else
4103 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4105 tp->napi[0].hw_status->status =
4106 (SD_STATUS_UPDATED |
4107 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
4109 for (i = 0; i < 100; i++) {
4110 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4111 MAC_STATUS_CFG_CHANGED));
4112 udelay(5);
4113 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
4114 MAC_STATUS_CFG_CHANGED |
4115 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
4116 break;
4119 mac_status = tr32(MAC_STATUS);
4120 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4121 current_link_up = 0;
4122 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4123 tp->serdes_counter == 0) {
4124 tw32_f(MAC_MODE, (tp->mac_mode |
4125 MAC_MODE_SEND_CONFIGS));
4126 udelay(1);
4127 tw32_f(MAC_MODE, tp->mac_mode);
4131 if (current_link_up == 1) {
4132 tp->link_config.active_speed = SPEED_1000;
4133 tp->link_config.active_duplex = DUPLEX_FULL;
4134 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4135 LED_CTRL_LNKLED_OVERRIDE |
4136 LED_CTRL_1000MBPS_ON));
4137 } else {
4138 tp->link_config.active_speed = SPEED_INVALID;
4139 tp->link_config.active_duplex = DUPLEX_INVALID;
4140 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4141 LED_CTRL_LNKLED_OVERRIDE |
4142 LED_CTRL_TRAFFIC_OVERRIDE));
4145 if (current_link_up != netif_carrier_ok(tp->dev)) {
4146 if (current_link_up)
4147 netif_carrier_on(tp->dev);
4148 else
4149 netif_carrier_off(tp->dev);
4150 tg3_link_report(tp);
4151 } else {
4152 u32 now_pause_cfg = tp->link_config.active_flowctrl;
4153 if (orig_pause_cfg != now_pause_cfg ||
4154 orig_active_speed != tp->link_config.active_speed ||
4155 orig_active_duplex != tp->link_config.active_duplex)
4156 tg3_link_report(tp);
4159 return 0;
4162 static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4164 int current_link_up, err = 0;
4165 u32 bmsr, bmcr;
4166 u16 current_speed;
4167 u8 current_duplex;
4168 u32 local_adv, remote_adv;
4170 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4171 tw32_f(MAC_MODE, tp->mac_mode);
4172 udelay(40);
4174 tw32(MAC_EVENT, 0);
4176 tw32_f(MAC_STATUS,
4177 (MAC_STATUS_SYNC_CHANGED |
4178 MAC_STATUS_CFG_CHANGED |
4179 MAC_STATUS_MI_COMPLETION |
4180 MAC_STATUS_LNKSTATE_CHANGED));
4181 udelay(40);
4183 if (force_reset)
4184 tg3_phy_reset(tp);
4186 current_link_up = 0;
4187 current_speed = SPEED_INVALID;
4188 current_duplex = DUPLEX_INVALID;
4190 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4191 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4192 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4193 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4194 bmsr |= BMSR_LSTATUS;
4195 else
4196 bmsr &= ~BMSR_LSTATUS;
4199 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4201 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
4202 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
4203 /* do nothing, just check for link up at the end */
4204 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4205 u32 adv, new_adv;
4207 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4208 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4209 ADVERTISE_1000XPAUSE |
4210 ADVERTISE_1000XPSE_ASYM |
4211 ADVERTISE_SLCT);
4213 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4215 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4216 new_adv |= ADVERTISE_1000XHALF;
4217 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4218 new_adv |= ADVERTISE_1000XFULL;
4220 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4221 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4222 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4223 tg3_writephy(tp, MII_BMCR, bmcr);
4225 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4226 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
4227 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4229 return err;
4231 } else {
4232 u32 new_bmcr;
4234 bmcr &= ~BMCR_SPEED1000;
4235 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4237 if (tp->link_config.duplex == DUPLEX_FULL)
4238 new_bmcr |= BMCR_FULLDPLX;
4240 if (new_bmcr != bmcr) {
4241 /* BMCR_SPEED1000 is a reserved bit that needs
4242 * to be set on write.
4244 new_bmcr |= BMCR_SPEED1000;
4246 /* Force a linkdown */
4247 if (netif_carrier_ok(tp->dev)) {
4248 u32 adv;
4250 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4251 adv &= ~(ADVERTISE_1000XFULL |
4252 ADVERTISE_1000XHALF |
4253 ADVERTISE_SLCT);
4254 tg3_writephy(tp, MII_ADVERTISE, adv);
4255 tg3_writephy(tp, MII_BMCR, bmcr |
4256 BMCR_ANRESTART |
4257 BMCR_ANENABLE);
4258 udelay(10);
4259 netif_carrier_off(tp->dev);
4261 tg3_writephy(tp, MII_BMCR, new_bmcr);
4262 bmcr = new_bmcr;
4263 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4264 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4265 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4266 ASIC_REV_5714) {
4267 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4268 bmsr |= BMSR_LSTATUS;
4269 else
4270 bmsr &= ~BMSR_LSTATUS;
4272 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4276 if (bmsr & BMSR_LSTATUS) {
4277 current_speed = SPEED_1000;
4278 current_link_up = 1;
4279 if (bmcr & BMCR_FULLDPLX)
4280 current_duplex = DUPLEX_FULL;
4281 else
4282 current_duplex = DUPLEX_HALF;
4284 local_adv = 0;
4285 remote_adv = 0;
4287 if (bmcr & BMCR_ANENABLE) {
4288 u32 common;
4290 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4291 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4292 common = local_adv & remote_adv;
4293 if (common & (ADVERTISE_1000XHALF |
4294 ADVERTISE_1000XFULL)) {
4295 if (common & ADVERTISE_1000XFULL)
4296 current_duplex = DUPLEX_FULL;
4297 else
4298 current_duplex = DUPLEX_HALF;
4299 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4300 /* Link is up via parallel detect */
4301 } else {
4302 current_link_up = 0;
4307 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4308 tg3_setup_flow_control(tp, local_adv, remote_adv);
4310 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4311 if (tp->link_config.active_duplex == DUPLEX_HALF)
4312 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4314 tw32_f(MAC_MODE, tp->mac_mode);
4315 udelay(40);
4317 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4319 tp->link_config.active_speed = current_speed;
4320 tp->link_config.active_duplex = current_duplex;
4322 if (current_link_up != netif_carrier_ok(tp->dev)) {
4323 if (current_link_up)
4324 netif_carrier_on(tp->dev);
4325 else {
4326 netif_carrier_off(tp->dev);
4327 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4329 tg3_link_report(tp);
4331 return err;
4334 static void tg3_serdes_parallel_detect(struct tg3 *tp)
4336 if (tp->serdes_counter) {
4337 /* Give autoneg time to complete. */
4338 tp->serdes_counter--;
4339 return;
4342 if (!netif_carrier_ok(tp->dev) &&
4343 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4344 u32 bmcr;
4346 tg3_readphy(tp, MII_BMCR, &bmcr);
4347 if (bmcr & BMCR_ANENABLE) {
4348 u32 phy1, phy2;
4350 /* Select shadow register 0x1f */
4351 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4352 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
4354 /* Select expansion interrupt status register */
4355 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4356 MII_TG3_DSP_EXP1_INT_STAT);
4357 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4358 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4360 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4361 /* We have signal detect and not receiving
4362 * config code words, link is up by parallel
4363 * detection.
4366 bmcr &= ~BMCR_ANENABLE;
4367 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4368 tg3_writephy(tp, MII_BMCR, bmcr);
4369 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
4372 } else if (netif_carrier_ok(tp->dev) &&
4373 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4374 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
4375 u32 phy2;
4377 /* Select expansion interrupt status register */
4378 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4379 MII_TG3_DSP_EXP1_INT_STAT);
4380 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4381 if (phy2 & 0x20) {
4382 u32 bmcr;
4384 /* Config code words received, turn on autoneg. */
4385 tg3_readphy(tp, MII_BMCR, &bmcr);
4386 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4388 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4394 static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4396 u32 val;
4397 int err;
4399 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
4400 err = tg3_setup_fiber_phy(tp, force_reset);
4401 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4402 err = tg3_setup_fiber_mii_phy(tp, force_reset);
4403 else
4404 err = tg3_setup_copper_phy(tp, force_reset);
4406 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
4407 u32 scale;
4409 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4410 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4411 scale = 65;
4412 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4413 scale = 6;
4414 else
4415 scale = 12;
4417 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4418 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4419 tw32(GRC_MISC_CFG, val);
4422 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4423 (6 << TX_LENGTHS_IPG_SHIFT);
4424 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4425 val |= tr32(MAC_TX_LENGTHS) &
4426 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4427 TX_LENGTHS_CNT_DWN_VAL_MSK);
4429 if (tp->link_config.active_speed == SPEED_1000 &&
4430 tp->link_config.active_duplex == DUPLEX_HALF)
4431 tw32(MAC_TX_LENGTHS, val |
4432 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
4433 else
4434 tw32(MAC_TX_LENGTHS, val |
4435 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
4437 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4438 if (netif_carrier_ok(tp->dev)) {
4439 tw32(HOSTCC_STAT_COAL_TICKS,
4440 tp->coal.stats_block_coalesce_usecs);
4441 } else {
4442 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4446 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4447 val = tr32(PCIE_PWR_MGMT_THRESH);
4448 if (!netif_carrier_ok(tp->dev))
4449 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4450 tp->pwrmgmt_thresh;
4451 else
4452 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4453 tw32(PCIE_PWR_MGMT_THRESH, val);
4456 return err;
4459 static inline int tg3_irq_sync(struct tg3 *tp)
4461 return tp->irq_sync;
4464 static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4466 int i;
4468 dst = (u32 *)((u8 *)dst + off);
4469 for (i = 0; i < len; i += sizeof(u32))
4470 *dst++ = tr32(off + i);
4473 static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4475 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4476 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4477 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4478 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4479 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4480 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4481 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4482 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4483 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4484 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4485 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4486 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4487 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4488 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4489 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4490 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4491 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4492 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4493 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4495 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX)
4496 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4498 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4499 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4500 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4501 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4502 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4503 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4504 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4505 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4507 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4508 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4509 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4510 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4513 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4514 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4515 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4516 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4517 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4519 if (tp->tg3_flags & TG3_FLAG_NVRAM)
4520 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4523 static void tg3_dump_state(struct tg3 *tp)
4525 int i;
4526 u32 *regs;
4528 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4529 if (!regs) {
4530 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4531 return;
4534 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
4535 /* Read up to but not including private PCI registers */
4536 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4537 regs[i / sizeof(u32)] = tr32(i);
4538 } else
4539 tg3_dump_legacy_regs(tp, regs);
4541 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4542 if (!regs[i + 0] && !regs[i + 1] &&
4543 !regs[i + 2] && !regs[i + 3])
4544 continue;
4546 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4547 i * 4,
4548 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4551 kfree(regs);
4553 for (i = 0; i < tp->irq_cnt; i++) {
4554 struct tg3_napi *tnapi = &tp->napi[i];
4556 /* SW status block */
4557 netdev_err(tp->dev,
4558 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4560 tnapi->hw_status->status,
4561 tnapi->hw_status->status_tag,
4562 tnapi->hw_status->rx_jumbo_consumer,
4563 tnapi->hw_status->rx_consumer,
4564 tnapi->hw_status->rx_mini_consumer,
4565 tnapi->hw_status->idx[0].rx_producer,
4566 tnapi->hw_status->idx[0].tx_consumer);
4568 netdev_err(tp->dev,
4569 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4571 tnapi->last_tag, tnapi->last_irq_tag,
4572 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4573 tnapi->rx_rcb_ptr,
4574 tnapi->prodring.rx_std_prod_idx,
4575 tnapi->prodring.rx_std_cons_idx,
4576 tnapi->prodring.rx_jmb_prod_idx,
4577 tnapi->prodring.rx_jmb_cons_idx);
4581 /* This is called whenever we suspect that the system chipset is re-
4582 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4583 * is bogus tx completions. We try to recover by setting the
4584 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4585 * in the workqueue.
4587 static void tg3_tx_recover(struct tg3 *tp)
4589 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4590 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4592 netdev_warn(tp->dev,
4593 "The system may be re-ordering memory-mapped I/O "
4594 "cycles to the network device, attempting to recover. "
4595 "Please report the problem to the driver maintainer "
4596 "and include system chipset information.\n");
4598 spin_lock(&tp->lock);
4599 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
4600 spin_unlock(&tp->lock);
4603 static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
4605 /* Tell compiler to fetch tx indices from memory. */
4606 barrier();
4607 return tnapi->tx_pending -
4608 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
4611 /* Tigon3 never reports partial packet sends. So we do not
4612 * need special logic to handle SKBs that have not had all
4613 * of their frags sent yet, like SunGEM does.
4615 static void tg3_tx(struct tg3_napi *tnapi)
4617 struct tg3 *tp = tnapi->tp;
4618 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
4619 u32 sw_idx = tnapi->tx_cons;
4620 struct netdev_queue *txq;
4621 int index = tnapi - tp->napi;
4623 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
4624 index--;
4626 txq = netdev_get_tx_queue(tp->dev, index);
4628 while (sw_idx != hw_idx) {
4629 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
4630 struct sk_buff *skb = ri->skb;
4631 int i, tx_bug = 0;
4633 if (unlikely(skb == NULL)) {
4634 tg3_tx_recover(tp);
4635 return;
4638 pci_unmap_single(tp->pdev,
4639 dma_unmap_addr(ri, mapping),
4640 skb_headlen(skb),
4641 PCI_DMA_TODEVICE);
4643 ri->skb = NULL;
4645 sw_idx = NEXT_TX(sw_idx);
4647 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4648 ri = &tnapi->tx_buffers[sw_idx];
4649 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4650 tx_bug = 1;
4652 pci_unmap_page(tp->pdev,
4653 dma_unmap_addr(ri, mapping),
4654 skb_shinfo(skb)->frags[i].size,
4655 PCI_DMA_TODEVICE);
4656 sw_idx = NEXT_TX(sw_idx);
4659 dev_kfree_skb(skb);
4661 if (unlikely(tx_bug)) {
4662 tg3_tx_recover(tp);
4663 return;
4667 tnapi->tx_cons = sw_idx;
4669 /* Need to make the tx_cons update visible to tg3_start_xmit()
4670 * before checking for netif_queue_stopped(). Without the
4671 * memory barrier, there is a small possibility that tg3_start_xmit()
4672 * will miss it and cause the queue to be stopped forever.
4674 smp_mb();
4676 if (unlikely(netif_tx_queue_stopped(txq) &&
4677 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
4678 __netif_tx_lock(txq, smp_processor_id());
4679 if (netif_tx_queue_stopped(txq) &&
4680 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
4681 netif_tx_wake_queue(txq);
4682 __netif_tx_unlock(txq);
4686 static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4688 if (!ri->skb)
4689 return;
4691 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
4692 map_sz, PCI_DMA_FROMDEVICE);
4693 dev_kfree_skb_any(ri->skb);
4694 ri->skb = NULL;
4697 /* Returns size of skb allocated or < 0 on error.
4699 * We only need to fill in the address because the other members
4700 * of the RX descriptor are invariant, see tg3_init_rings.
4702 * Note the purposeful assymetry of cpu vs. chip accesses. For
4703 * posting buffers we only dirty the first cache line of the RX
4704 * descriptor (containing the address). Whereas for the RX status
4705 * buffers the cpu only reads the last cacheline of the RX descriptor
4706 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4708 static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
4709 u32 opaque_key, u32 dest_idx_unmasked)
4711 struct tg3_rx_buffer_desc *desc;
4712 struct ring_info *map;
4713 struct sk_buff *skb;
4714 dma_addr_t mapping;
4715 int skb_size, dest_idx;
4717 switch (opaque_key) {
4718 case RXD_OPAQUE_RING_STD:
4719 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
4720 desc = &tpr->rx_std[dest_idx];
4721 map = &tpr->rx_std_buffers[dest_idx];
4722 skb_size = tp->rx_pkt_map_sz;
4723 break;
4725 case RXD_OPAQUE_RING_JUMBO:
4726 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
4727 desc = &tpr->rx_jmb[dest_idx].std;
4728 map = &tpr->rx_jmb_buffers[dest_idx];
4729 skb_size = TG3_RX_JMB_MAP_SZ;
4730 break;
4732 default:
4733 return -EINVAL;
4736 /* Do not overwrite any of the map or rp information
4737 * until we are sure we can commit to a new buffer.
4739 * Callers depend upon this behavior and assume that
4740 * we leave everything unchanged if we fail.
4742 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
4743 if (skb == NULL)
4744 return -ENOMEM;
4746 skb_reserve(skb, tp->rx_offset);
4748 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
4749 PCI_DMA_FROMDEVICE);
4750 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4751 dev_kfree_skb(skb);
4752 return -EIO;
4755 map->skb = skb;
4756 dma_unmap_addr_set(map, mapping, mapping);
4758 desc->addr_hi = ((u64)mapping >> 32);
4759 desc->addr_lo = ((u64)mapping & 0xffffffff);
4761 return skb_size;
4764 /* We only need to move over in the address because the other
4765 * members of the RX descriptor are invariant. See notes above
4766 * tg3_alloc_rx_skb for full details.
4768 static void tg3_recycle_rx(struct tg3_napi *tnapi,
4769 struct tg3_rx_prodring_set *dpr,
4770 u32 opaque_key, int src_idx,
4771 u32 dest_idx_unmasked)
4773 struct tg3 *tp = tnapi->tp;
4774 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4775 struct ring_info *src_map, *dest_map;
4776 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
4777 int dest_idx;
4779 switch (opaque_key) {
4780 case RXD_OPAQUE_RING_STD:
4781 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
4782 dest_desc = &dpr->rx_std[dest_idx];
4783 dest_map = &dpr->rx_std_buffers[dest_idx];
4784 src_desc = &spr->rx_std[src_idx];
4785 src_map = &spr->rx_std_buffers[src_idx];
4786 break;
4788 case RXD_OPAQUE_RING_JUMBO:
4789 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
4790 dest_desc = &dpr->rx_jmb[dest_idx].std;
4791 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4792 src_desc = &spr->rx_jmb[src_idx].std;
4793 src_map = &spr->rx_jmb_buffers[src_idx];
4794 break;
4796 default:
4797 return;
4800 dest_map->skb = src_map->skb;
4801 dma_unmap_addr_set(dest_map, mapping,
4802 dma_unmap_addr(src_map, mapping));
4803 dest_desc->addr_hi = src_desc->addr_hi;
4804 dest_desc->addr_lo = src_desc->addr_lo;
4806 /* Ensure that the update to the skb happens after the physical
4807 * addresses have been transferred to the new BD location.
4809 smp_wmb();
4811 src_map->skb = NULL;
4814 /* The RX ring scheme is composed of multiple rings which post fresh
4815 * buffers to the chip, and one special ring the chip uses to report
4816 * status back to the host.
4818 * The special ring reports the status of received packets to the
4819 * host. The chip does not write into the original descriptor the
4820 * RX buffer was obtained from. The chip simply takes the original
4821 * descriptor as provided by the host, updates the status and length
4822 * field, then writes this into the next status ring entry.
4824 * Each ring the host uses to post buffers to the chip is described
4825 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4826 * it is first placed into the on-chip ram. When the packet's length
4827 * is known, it walks down the TG3_BDINFO entries to select the ring.
4828 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4829 * which is within the range of the new packet's length is chosen.
4831 * The "separate ring for rx status" scheme may sound queer, but it makes
4832 * sense from a cache coherency perspective. If only the host writes
4833 * to the buffer post rings, and only the chip writes to the rx status
4834 * rings, then cache lines never move beyond shared-modified state.
4835 * If both the host and chip were to write into the same ring, cache line
4836 * eviction could occur since both entities want it in an exclusive state.
4838 static int tg3_rx(struct tg3_napi *tnapi, int budget)
4840 struct tg3 *tp = tnapi->tp;
4841 u32 work_mask, rx_std_posted = 0;
4842 u32 std_prod_idx, jmb_prod_idx;
4843 u32 sw_idx = tnapi->rx_rcb_ptr;
4844 u16 hw_idx;
4845 int received;
4846 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
4848 hw_idx = *(tnapi->rx_rcb_prod_idx);
4850 * We need to order the read of hw_idx and the read of
4851 * the opaque cookie.
4853 rmb();
4854 work_mask = 0;
4855 received = 0;
4856 std_prod_idx = tpr->rx_std_prod_idx;
4857 jmb_prod_idx = tpr->rx_jmb_prod_idx;
4858 while (sw_idx != hw_idx && budget > 0) {
4859 struct ring_info *ri;
4860 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
4861 unsigned int len;
4862 struct sk_buff *skb;
4863 dma_addr_t dma_addr;
4864 u32 opaque_key, desc_idx, *post_ptr;
4866 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4867 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4868 if (opaque_key == RXD_OPAQUE_RING_STD) {
4869 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
4870 dma_addr = dma_unmap_addr(ri, mapping);
4871 skb = ri->skb;
4872 post_ptr = &std_prod_idx;
4873 rx_std_posted++;
4874 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
4875 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
4876 dma_addr = dma_unmap_addr(ri, mapping);
4877 skb = ri->skb;
4878 post_ptr = &jmb_prod_idx;
4879 } else
4880 goto next_pkt_nopost;
4882 work_mask |= opaque_key;
4884 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4885 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4886 drop_it:
4887 tg3_recycle_rx(tnapi, tpr, opaque_key,
4888 desc_idx, *post_ptr);
4889 drop_it_no_recycle:
4890 /* Other statistics kept track of by card. */
4891 tp->rx_dropped++;
4892 goto next_pkt;
4895 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4896 ETH_FCS_LEN;
4898 if (len > TG3_RX_COPY_THRESH(tp)) {
4899 int skb_size;
4901 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
4902 *post_ptr);
4903 if (skb_size < 0)
4904 goto drop_it;
4906 pci_unmap_single(tp->pdev, dma_addr, skb_size,
4907 PCI_DMA_FROMDEVICE);
4909 /* Ensure that the update to the skb happens
4910 * after the usage of the old DMA mapping.
4912 smp_wmb();
4914 ri->skb = NULL;
4916 skb_put(skb, len);
4917 } else {
4918 struct sk_buff *copy_skb;
4920 tg3_recycle_rx(tnapi, tpr, opaque_key,
4921 desc_idx, *post_ptr);
4923 copy_skb = netdev_alloc_skb(tp->dev, len +
4924 TG3_RAW_IP_ALIGN);
4925 if (copy_skb == NULL)
4926 goto drop_it_no_recycle;
4928 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
4929 skb_put(copy_skb, len);
4930 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4931 skb_copy_from_linear_data(skb, copy_skb->data, len);
4932 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4934 /* We'll reuse the original ring buffer. */
4935 skb = copy_skb;
4938 if ((tp->dev->features & NETIF_F_RXCSUM) &&
4939 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4940 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4941 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4942 skb->ip_summed = CHECKSUM_UNNECESSARY;
4943 else
4944 skb_checksum_none_assert(skb);
4946 skb->protocol = eth_type_trans(skb, tp->dev);
4948 if (len > (tp->dev->mtu + ETH_HLEN) &&
4949 skb->protocol != htons(ETH_P_8021Q)) {
4950 dev_kfree_skb(skb);
4951 goto drop_it_no_recycle;
4954 if (desc->type_flags & RXD_FLAG_VLAN &&
4955 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4956 __vlan_hwaccel_put_tag(skb,
4957 desc->err_vlan & RXD_VLAN_MASK);
4959 napi_gro_receive(&tnapi->napi, skb);
4961 received++;
4962 budget--;
4964 next_pkt:
4965 (*post_ptr)++;
4967 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4968 tpr->rx_std_prod_idx = std_prod_idx &
4969 tp->rx_std_ring_mask;
4970 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4971 tpr->rx_std_prod_idx);
4972 work_mask &= ~RXD_OPAQUE_RING_STD;
4973 rx_std_posted = 0;
4975 next_pkt_nopost:
4976 sw_idx++;
4977 sw_idx &= tp->rx_ret_ring_mask;
4979 /* Refresh hw_idx to see if there is new work */
4980 if (sw_idx == hw_idx) {
4981 hw_idx = *(tnapi->rx_rcb_prod_idx);
4982 rmb();
4986 /* ACK the status ring. */
4987 tnapi->rx_rcb_ptr = sw_idx;
4988 tw32_rx_mbox(tnapi->consmbox, sw_idx);
4990 /* Refill RX ring(s). */
4991 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
4992 if (work_mask & RXD_OPAQUE_RING_STD) {
4993 tpr->rx_std_prod_idx = std_prod_idx &
4994 tp->rx_std_ring_mask;
4995 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4996 tpr->rx_std_prod_idx);
4998 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4999 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5000 tp->rx_jmb_ring_mask;
5001 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5002 tpr->rx_jmb_prod_idx);
5004 mmiowb();
5005 } else if (work_mask) {
5006 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5007 * updated before the producer indices can be updated.
5009 smp_wmb();
5011 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5012 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
5014 if (tnapi != &tp->napi[1])
5015 napi_schedule(&tp->napi[1].napi);
5018 return received;
5021 static void tg3_poll_link(struct tg3 *tp)
5023 /* handle link change and other phy events */
5024 if (!(tp->tg3_flags &
5025 (TG3_FLAG_USE_LINKCHG_REG |
5026 TG3_FLAG_POLL_SERDES))) {
5027 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5029 if (sblk->status & SD_STATUS_LINK_CHG) {
5030 sblk->status = SD_STATUS_UPDATED |
5031 (sblk->status & ~SD_STATUS_LINK_CHG);
5032 spin_lock(&tp->lock);
5033 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
5034 tw32_f(MAC_STATUS,
5035 (MAC_STATUS_SYNC_CHANGED |
5036 MAC_STATUS_CFG_CHANGED |
5037 MAC_STATUS_MI_COMPLETION |
5038 MAC_STATUS_LNKSTATE_CHANGED));
5039 udelay(40);
5040 } else
5041 tg3_setup_phy(tp, 0);
5042 spin_unlock(&tp->lock);
5047 static int tg3_rx_prodring_xfer(struct tg3 *tp,
5048 struct tg3_rx_prodring_set *dpr,
5049 struct tg3_rx_prodring_set *spr)
5051 u32 si, di, cpycnt, src_prod_idx;
5052 int i, err = 0;
5054 while (1) {
5055 src_prod_idx = spr->rx_std_prod_idx;
5057 /* Make sure updates to the rx_std_buffers[] entries and the
5058 * standard producer index are seen in the correct order.
5060 smp_rmb();
5062 if (spr->rx_std_cons_idx == src_prod_idx)
5063 break;
5065 if (spr->rx_std_cons_idx < src_prod_idx)
5066 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5067 else
5068 cpycnt = tp->rx_std_ring_mask + 1 -
5069 spr->rx_std_cons_idx;
5071 cpycnt = min(cpycnt,
5072 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
5074 si = spr->rx_std_cons_idx;
5075 di = dpr->rx_std_prod_idx;
5077 for (i = di; i < di + cpycnt; i++) {
5078 if (dpr->rx_std_buffers[i].skb) {
5079 cpycnt = i - di;
5080 err = -ENOSPC;
5081 break;
5085 if (!cpycnt)
5086 break;
5088 /* Ensure that updates to the rx_std_buffers ring and the
5089 * shadowed hardware producer ring from tg3_recycle_skb() are
5090 * ordered correctly WRT the skb check above.
5092 smp_rmb();
5094 memcpy(&dpr->rx_std_buffers[di],
5095 &spr->rx_std_buffers[si],
5096 cpycnt * sizeof(struct ring_info));
5098 for (i = 0; i < cpycnt; i++, di++, si++) {
5099 struct tg3_rx_buffer_desc *sbd, *dbd;
5100 sbd = &spr->rx_std[si];
5101 dbd = &dpr->rx_std[di];
5102 dbd->addr_hi = sbd->addr_hi;
5103 dbd->addr_lo = sbd->addr_lo;
5106 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5107 tp->rx_std_ring_mask;
5108 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5109 tp->rx_std_ring_mask;
5112 while (1) {
5113 src_prod_idx = spr->rx_jmb_prod_idx;
5115 /* Make sure updates to the rx_jmb_buffers[] entries and
5116 * the jumbo producer index are seen in the correct order.
5118 smp_rmb();
5120 if (spr->rx_jmb_cons_idx == src_prod_idx)
5121 break;
5123 if (spr->rx_jmb_cons_idx < src_prod_idx)
5124 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5125 else
5126 cpycnt = tp->rx_jmb_ring_mask + 1 -
5127 spr->rx_jmb_cons_idx;
5129 cpycnt = min(cpycnt,
5130 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
5132 si = spr->rx_jmb_cons_idx;
5133 di = dpr->rx_jmb_prod_idx;
5135 for (i = di; i < di + cpycnt; i++) {
5136 if (dpr->rx_jmb_buffers[i].skb) {
5137 cpycnt = i - di;
5138 err = -ENOSPC;
5139 break;
5143 if (!cpycnt)
5144 break;
5146 /* Ensure that updates to the rx_jmb_buffers ring and the
5147 * shadowed hardware producer ring from tg3_recycle_skb() are
5148 * ordered correctly WRT the skb check above.
5150 smp_rmb();
5152 memcpy(&dpr->rx_jmb_buffers[di],
5153 &spr->rx_jmb_buffers[si],
5154 cpycnt * sizeof(struct ring_info));
5156 for (i = 0; i < cpycnt; i++, di++, si++) {
5157 struct tg3_rx_buffer_desc *sbd, *dbd;
5158 sbd = &spr->rx_jmb[si].std;
5159 dbd = &dpr->rx_jmb[di].std;
5160 dbd->addr_hi = sbd->addr_hi;
5161 dbd->addr_lo = sbd->addr_lo;
5164 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5165 tp->rx_jmb_ring_mask;
5166 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5167 tp->rx_jmb_ring_mask;
5170 return err;
5173 static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5175 struct tg3 *tp = tnapi->tp;
5177 /* run TX completion thread */
5178 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
5179 tg3_tx(tnapi);
5180 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5181 return work_done;
5184 /* run RX thread, within the bounds set by NAPI.
5185 * All RX "locking" is done by ensuring outside
5186 * code synchronizes with tg3->napi.poll()
5188 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
5189 work_done += tg3_rx(tnapi, budget - work_done);
5191 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
5192 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
5193 int i, err = 0;
5194 u32 std_prod_idx = dpr->rx_std_prod_idx;
5195 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
5197 for (i = 1; i < tp->irq_cnt; i++)
5198 err |= tg3_rx_prodring_xfer(tp, dpr,
5199 &tp->napi[i].prodring);
5201 wmb();
5203 if (std_prod_idx != dpr->rx_std_prod_idx)
5204 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5205 dpr->rx_std_prod_idx);
5207 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5208 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5209 dpr->rx_jmb_prod_idx);
5211 mmiowb();
5213 if (err)
5214 tw32_f(HOSTCC_MODE, tp->coal_now);
5217 return work_done;
5220 static int tg3_poll_msix(struct napi_struct *napi, int budget)
5222 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5223 struct tg3 *tp = tnapi->tp;
5224 int work_done = 0;
5225 struct tg3_hw_status *sblk = tnapi->hw_status;
5227 while (1) {
5228 work_done = tg3_poll_work(tnapi, work_done, budget);
5230 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5231 goto tx_recovery;
5233 if (unlikely(work_done >= budget))
5234 break;
5236 /* tp->last_tag is used in tg3_int_reenable() below
5237 * to tell the hw how much work has been processed,
5238 * so we must read it before checking for more work.
5240 tnapi->last_tag = sblk->status_tag;
5241 tnapi->last_irq_tag = tnapi->last_tag;
5242 rmb();
5244 /* check for RX/TX work to do */
5245 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5246 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
5247 napi_complete(napi);
5248 /* Reenable interrupts. */
5249 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5250 mmiowb();
5251 break;
5255 return work_done;
5257 tx_recovery:
5258 /* work_done is guaranteed to be less than budget. */
5259 napi_complete(napi);
5260 schedule_work(&tp->reset_task);
5261 return work_done;
5264 static void tg3_process_error(struct tg3 *tp)
5266 u32 val;
5267 bool real_error = false;
5269 if (tp->tg3_flags & TG3_FLAG_ERROR_PROCESSED)
5270 return;
5272 /* Check Flow Attention register */
5273 val = tr32(HOSTCC_FLOW_ATTN);
5274 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5275 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5276 real_error = true;
5279 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5280 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5281 real_error = true;
5284 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5285 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5286 real_error = true;
5289 if (!real_error)
5290 return;
5292 tg3_dump_state(tp);
5294 tp->tg3_flags |= TG3_FLAG_ERROR_PROCESSED;
5295 schedule_work(&tp->reset_task);
5298 static int tg3_poll(struct napi_struct *napi, int budget)
5300 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5301 struct tg3 *tp = tnapi->tp;
5302 int work_done = 0;
5303 struct tg3_hw_status *sblk = tnapi->hw_status;
5305 while (1) {
5306 if (sblk->status & SD_STATUS_ERROR)
5307 tg3_process_error(tp);
5309 tg3_poll_link(tp);
5311 work_done = tg3_poll_work(tnapi, work_done, budget);
5313 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5314 goto tx_recovery;
5316 if (unlikely(work_done >= budget))
5317 break;
5319 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
5320 /* tp->last_tag is used in tg3_int_reenable() below
5321 * to tell the hw how much work has been processed,
5322 * so we must read it before checking for more work.
5324 tnapi->last_tag = sblk->status_tag;
5325 tnapi->last_irq_tag = tnapi->last_tag;
5326 rmb();
5327 } else
5328 sblk->status &= ~SD_STATUS_UPDATED;
5330 if (likely(!tg3_has_work(tnapi))) {
5331 napi_complete(napi);
5332 tg3_int_reenable(tnapi);
5333 break;
5337 return work_done;
5339 tx_recovery:
5340 /* work_done is guaranteed to be less than budget. */
5341 napi_complete(napi);
5342 schedule_work(&tp->reset_task);
5343 return work_done;
5346 static void tg3_napi_disable(struct tg3 *tp)
5348 int i;
5350 for (i = tp->irq_cnt - 1; i >= 0; i--)
5351 napi_disable(&tp->napi[i].napi);
5354 static void tg3_napi_enable(struct tg3 *tp)
5356 int i;
5358 for (i = 0; i < tp->irq_cnt; i++)
5359 napi_enable(&tp->napi[i].napi);
5362 static void tg3_napi_init(struct tg3 *tp)
5364 int i;
5366 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5367 for (i = 1; i < tp->irq_cnt; i++)
5368 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5371 static void tg3_napi_fini(struct tg3 *tp)
5373 int i;
5375 for (i = 0; i < tp->irq_cnt; i++)
5376 netif_napi_del(&tp->napi[i].napi);
5379 static inline void tg3_netif_stop(struct tg3 *tp)
5381 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5382 tg3_napi_disable(tp);
5383 netif_tx_disable(tp->dev);
5386 static inline void tg3_netif_start(struct tg3 *tp)
5388 /* NOTE: unconditional netif_tx_wake_all_queues is only
5389 * appropriate so long as all callers are assured to
5390 * have free tx slots (such as after tg3_init_hw)
5392 netif_tx_wake_all_queues(tp->dev);
5394 tg3_napi_enable(tp);
5395 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5396 tg3_enable_ints(tp);
5399 static void tg3_irq_quiesce(struct tg3 *tp)
5401 int i;
5403 BUG_ON(tp->irq_sync);
5405 tp->irq_sync = 1;
5406 smp_mb();
5408 for (i = 0; i < tp->irq_cnt; i++)
5409 synchronize_irq(tp->napi[i].irq_vec);
5412 /* Fully shutdown all tg3 driver activity elsewhere in the system.
5413 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5414 * with as well. Most of the time, this is not necessary except when
5415 * shutting down the device.
5417 static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5419 spin_lock_bh(&tp->lock);
5420 if (irq_sync)
5421 tg3_irq_quiesce(tp);
5424 static inline void tg3_full_unlock(struct tg3 *tp)
5426 spin_unlock_bh(&tp->lock);
5429 /* One-shot MSI handler - Chip automatically disables interrupt
5430 * after sending MSI so driver doesn't have to do it.
5432 static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
5434 struct tg3_napi *tnapi = dev_id;
5435 struct tg3 *tp = tnapi->tp;
5437 prefetch(tnapi->hw_status);
5438 if (tnapi->rx_rcb)
5439 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5441 if (likely(!tg3_irq_sync(tp)))
5442 napi_schedule(&tnapi->napi);
5444 return IRQ_HANDLED;
5447 /* MSI ISR - No need to check for interrupt sharing and no need to
5448 * flush status block and interrupt mailbox. PCI ordering rules
5449 * guarantee that MSI will arrive after the status block.
5451 static irqreturn_t tg3_msi(int irq, void *dev_id)
5453 struct tg3_napi *tnapi = dev_id;
5454 struct tg3 *tp = tnapi->tp;
5456 prefetch(tnapi->hw_status);
5457 if (tnapi->rx_rcb)
5458 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5460 * Writing any value to intr-mbox-0 clears PCI INTA# and
5461 * chip-internal interrupt pending events.
5462 * Writing non-zero to intr-mbox-0 additional tells the
5463 * NIC to stop sending us irqs, engaging "in-intr-handler"
5464 * event coalescing.
5466 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
5467 if (likely(!tg3_irq_sync(tp)))
5468 napi_schedule(&tnapi->napi);
5470 return IRQ_RETVAL(1);
5473 static irqreturn_t tg3_interrupt(int irq, void *dev_id)
5475 struct tg3_napi *tnapi = dev_id;
5476 struct tg3 *tp = tnapi->tp;
5477 struct tg3_hw_status *sblk = tnapi->hw_status;
5478 unsigned int handled = 1;
5480 /* In INTx mode, it is possible for the interrupt to arrive at
5481 * the CPU before the status block posted prior to the interrupt.
5482 * Reading the PCI State register will confirm whether the
5483 * interrupt is ours and will flush the status block.
5485 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5486 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5487 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5488 handled = 0;
5489 goto out;
5494 * Writing any value to intr-mbox-0 clears PCI INTA# and
5495 * chip-internal interrupt pending events.
5496 * Writing non-zero to intr-mbox-0 additional tells the
5497 * NIC to stop sending us irqs, engaging "in-intr-handler"
5498 * event coalescing.
5500 * Flush the mailbox to de-assert the IRQ immediately to prevent
5501 * spurious interrupts. The flush impacts performance but
5502 * excessive spurious interrupts can be worse in some cases.
5504 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
5505 if (tg3_irq_sync(tp))
5506 goto out;
5507 sblk->status &= ~SD_STATUS_UPDATED;
5508 if (likely(tg3_has_work(tnapi))) {
5509 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5510 napi_schedule(&tnapi->napi);
5511 } else {
5512 /* No work, shared interrupt perhaps? re-enable
5513 * interrupts, and flush that PCI write
5515 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5516 0x00000000);
5518 out:
5519 return IRQ_RETVAL(handled);
5522 static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
5524 struct tg3_napi *tnapi = dev_id;
5525 struct tg3 *tp = tnapi->tp;
5526 struct tg3_hw_status *sblk = tnapi->hw_status;
5527 unsigned int handled = 1;
5529 /* In INTx mode, it is possible for the interrupt to arrive at
5530 * the CPU before the status block posted prior to the interrupt.
5531 * Reading the PCI State register will confirm whether the
5532 * interrupt is ours and will flush the status block.
5534 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
5535 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5536 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5537 handled = 0;
5538 goto out;
5543 * writing any value to intr-mbox-0 clears PCI INTA# and
5544 * chip-internal interrupt pending events.
5545 * writing non-zero to intr-mbox-0 additional tells the
5546 * NIC to stop sending us irqs, engaging "in-intr-handler"
5547 * event coalescing.
5549 * Flush the mailbox to de-assert the IRQ immediately to prevent
5550 * spurious interrupts. The flush impacts performance but
5551 * excessive spurious interrupts can be worse in some cases.
5553 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
5556 * In a shared interrupt configuration, sometimes other devices'
5557 * interrupts will scream. We record the current status tag here
5558 * so that the above check can report that the screaming interrupts
5559 * are unhandled. Eventually they will be silenced.
5561 tnapi->last_irq_tag = sblk->status_tag;
5563 if (tg3_irq_sync(tp))
5564 goto out;
5566 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5568 napi_schedule(&tnapi->napi);
5570 out:
5571 return IRQ_RETVAL(handled);
5574 /* ISR for interrupt test */
5575 static irqreturn_t tg3_test_isr(int irq, void *dev_id)
5577 struct tg3_napi *tnapi = dev_id;
5578 struct tg3 *tp = tnapi->tp;
5579 struct tg3_hw_status *sblk = tnapi->hw_status;
5581 if ((sblk->status & SD_STATUS_UPDATED) ||
5582 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5583 tg3_disable_ints(tp);
5584 return IRQ_RETVAL(1);
5586 return IRQ_RETVAL(0);
5589 static int tg3_init_hw(struct tg3 *, int);
5590 static int tg3_halt(struct tg3 *, int, int);
5592 /* Restart hardware after configuration changes, self-test, etc.
5593 * Invoked with tp->lock held.
5595 static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
5596 __releases(tp->lock)
5597 __acquires(tp->lock)
5599 int err;
5601 err = tg3_init_hw(tp, reset_phy);
5602 if (err) {
5603 netdev_err(tp->dev,
5604 "Failed to re-initialize device, aborting\n");
5605 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5606 tg3_full_unlock(tp);
5607 del_timer_sync(&tp->timer);
5608 tp->irq_sync = 0;
5609 tg3_napi_enable(tp);
5610 dev_close(tp->dev);
5611 tg3_full_lock(tp, 0);
5613 return err;
5616 #ifdef CONFIG_NET_POLL_CONTROLLER
5617 static void tg3_poll_controller(struct net_device *dev)
5619 int i;
5620 struct tg3 *tp = netdev_priv(dev);
5622 for (i = 0; i < tp->irq_cnt; i++)
5623 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
5625 #endif
5627 static void tg3_reset_task(struct work_struct *work)
5629 struct tg3 *tp = container_of(work, struct tg3, reset_task);
5630 int err;
5631 unsigned int restart_timer;
5633 tg3_full_lock(tp, 0);
5635 if (!netif_running(tp->dev)) {
5636 tg3_full_unlock(tp);
5637 return;
5640 tg3_full_unlock(tp);
5642 tg3_phy_stop(tp);
5644 tg3_netif_stop(tp);
5646 tg3_full_lock(tp, 1);
5648 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5649 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5651 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5652 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5653 tp->write32_rx_mbox = tg3_write_flush_reg32;
5654 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5655 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5658 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
5659 err = tg3_init_hw(tp, 1);
5660 if (err)
5661 goto out;
5663 tg3_netif_start(tp);
5665 if (restart_timer)
5666 mod_timer(&tp->timer, jiffies + 1);
5668 out:
5669 tg3_full_unlock(tp);
5671 if (!err)
5672 tg3_phy_start(tp);
5675 static void tg3_tx_timeout(struct net_device *dev)
5677 struct tg3 *tp = netdev_priv(dev);
5679 if (netif_msg_tx_err(tp)) {
5680 netdev_err(dev, "transmit timed out, resetting\n");
5681 tg3_dump_state(tp);
5684 schedule_work(&tp->reset_task);
5687 /* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5688 static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5690 u32 base = (u32) mapping & 0xffffffff;
5692 return (base > 0xffffdcc0) && (base + len + 8 < base);
5695 /* Test for DMA addresses > 40-bit */
5696 static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5697 int len)
5699 #if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
5700 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
5701 return ((u64) mapping + len) > DMA_BIT_MASK(40);
5702 return 0;
5703 #else
5704 return 0;
5705 #endif
5708 static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
5710 /* Workaround 4GB and 40-bit hardware DMA bugs. */
5711 static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5712 struct sk_buff *skb, u32 last_plus_one,
5713 u32 *start, u32 base_flags, u32 mss)
5715 struct tg3 *tp = tnapi->tp;
5716 struct sk_buff *new_skb;
5717 dma_addr_t new_addr = 0;
5718 u32 entry = *start;
5719 int i, ret = 0;
5721 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5722 new_skb = skb_copy(skb, GFP_ATOMIC);
5723 else {
5724 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5726 new_skb = skb_copy_expand(skb,
5727 skb_headroom(skb) + more_headroom,
5728 skb_tailroom(skb), GFP_ATOMIC);
5731 if (!new_skb) {
5732 ret = -1;
5733 } else {
5734 /* New SKB is guaranteed to be linear. */
5735 entry = *start;
5736 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5737 PCI_DMA_TODEVICE);
5738 /* Make sure the mapping succeeded */
5739 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5740 ret = -1;
5741 dev_kfree_skb(new_skb);
5742 new_skb = NULL;
5744 /* Make sure new skb does not cross any 4G boundaries.
5745 * Drop the packet if it does.
5747 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5748 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5749 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5750 PCI_DMA_TODEVICE);
5751 ret = -1;
5752 dev_kfree_skb(new_skb);
5753 new_skb = NULL;
5754 } else {
5755 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
5756 base_flags, 1 | (mss << 1));
5757 *start = NEXT_TX(entry);
5761 /* Now clean up the sw ring entries. */
5762 i = 0;
5763 while (entry != last_plus_one) {
5764 int len;
5766 if (i == 0)
5767 len = skb_headlen(skb);
5768 else
5769 len = skb_shinfo(skb)->frags[i-1].size;
5771 pci_unmap_single(tp->pdev,
5772 dma_unmap_addr(&tnapi->tx_buffers[entry],
5773 mapping),
5774 len, PCI_DMA_TODEVICE);
5775 if (i == 0) {
5776 tnapi->tx_buffers[entry].skb = new_skb;
5777 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5778 new_addr);
5779 } else {
5780 tnapi->tx_buffers[entry].skb = NULL;
5782 entry = NEXT_TX(entry);
5783 i++;
5786 dev_kfree_skb(skb);
5788 return ret;
5791 static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5792 dma_addr_t mapping, int len, u32 flags,
5793 u32 mss_and_is_end)
5795 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5796 int is_end = (mss_and_is_end & 0x1);
5797 u32 mss = (mss_and_is_end >> 1);
5798 u32 vlan_tag = 0;
5800 if (is_end)
5801 flags |= TXD_FLAG_END;
5802 if (flags & TXD_FLAG_VLAN) {
5803 vlan_tag = flags >> 16;
5804 flags &= 0xffff;
5806 vlan_tag |= (mss << TXD_MSS_SHIFT);
5808 txd->addr_hi = ((u64) mapping >> 32);
5809 txd->addr_lo = ((u64) mapping & 0xffffffff);
5810 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5811 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5814 /* hard_start_xmit for devices that don't have any bugs and
5815 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
5817 static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5818 struct net_device *dev)
5820 struct tg3 *tp = netdev_priv(dev);
5821 u32 len, entry, base_flags, mss;
5822 dma_addr_t mapping;
5823 struct tg3_napi *tnapi;
5824 struct netdev_queue *txq;
5825 unsigned int i, last;
5827 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5828 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
5829 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
5830 tnapi++;
5832 /* We are running in BH disabled context with netif_tx_lock
5833 * and TX reclaim runs via tp->napi.poll inside of a software
5834 * interrupt. Furthermore, IRQ processing runs lockless so we have
5835 * no IRQ context deadlocks to worry about either. Rejoice!
5837 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
5838 if (!netif_tx_queue_stopped(txq)) {
5839 netif_tx_stop_queue(txq);
5841 /* This is a hard error, log it. */
5842 netdev_err(dev,
5843 "BUG! Tx Ring full when queue awake!\n");
5845 return NETDEV_TX_BUSY;
5848 entry = tnapi->tx_prod;
5849 base_flags = 0;
5850 mss = skb_shinfo(skb)->gso_size;
5851 if (mss) {
5852 int tcp_opt_len, ip_tcp_len;
5853 u32 hdrlen;
5855 if (skb_header_cloned(skb) &&
5856 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5857 dev_kfree_skb(skb);
5858 goto out_unlock;
5861 if (skb_is_gso_v6(skb)) {
5862 hdrlen = skb_headlen(skb) - ETH_HLEN;
5863 } else {
5864 struct iphdr *iph = ip_hdr(skb);
5866 tcp_opt_len = tcp_optlen(skb);
5867 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5869 iph->check = 0;
5870 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
5871 hdrlen = ip_tcp_len + tcp_opt_len;
5874 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5875 mss |= (hdrlen & 0xc) << 12;
5876 if (hdrlen & 0x10)
5877 base_flags |= 0x00000010;
5878 base_flags |= (hdrlen & 0x3e0) << 5;
5879 } else
5880 mss |= hdrlen << 9;
5882 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5883 TXD_FLAG_CPU_POST_DMA);
5885 tcp_hdr(skb)->check = 0;
5887 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5888 base_flags |= TXD_FLAG_TCPUDP_CSUM;
5891 if (vlan_tx_tag_present(skb))
5892 base_flags |= (TXD_FLAG_VLAN |
5893 (vlan_tx_tag_get(skb) << 16));
5895 len = skb_headlen(skb);
5897 /* Queue skb data, a.k.a. the main skb fragment. */
5898 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5899 if (pci_dma_mapping_error(tp->pdev, mapping)) {
5900 dev_kfree_skb(skb);
5901 goto out_unlock;
5904 tnapi->tx_buffers[entry].skb = skb;
5905 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
5907 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
5908 !mss && skb->len > VLAN_ETH_FRAME_LEN)
5909 base_flags |= TXD_FLAG_JMB_PKT;
5911 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
5912 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5914 entry = NEXT_TX(entry);
5916 /* Now loop through additional data fragments, and queue them. */
5917 if (skb_shinfo(skb)->nr_frags > 0) {
5918 last = skb_shinfo(skb)->nr_frags - 1;
5919 for (i = 0; i <= last; i++) {
5920 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5922 len = frag->size;
5923 mapping = pci_map_page(tp->pdev,
5924 frag->page,
5925 frag->page_offset,
5926 len, PCI_DMA_TODEVICE);
5927 if (pci_dma_mapping_error(tp->pdev, mapping))
5928 goto dma_error;
5930 tnapi->tx_buffers[entry].skb = NULL;
5931 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5932 mapping);
5934 tg3_set_txd(tnapi, entry, mapping, len,
5935 base_flags, (i == last) | (mss << 1));
5937 entry = NEXT_TX(entry);
5941 /* Packets are ready, update Tx producer idx local and on card. */
5942 tw32_tx_mbox(tnapi->prodmbox, entry);
5944 tnapi->tx_prod = entry;
5945 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
5946 netif_tx_stop_queue(txq);
5948 /* netif_tx_stop_queue() must be done before checking
5949 * checking tx index in tg3_tx_avail() below, because in
5950 * tg3_tx(), we update tx index before checking for
5951 * netif_tx_queue_stopped().
5953 smp_mb();
5954 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
5955 netif_tx_wake_queue(txq);
5958 out_unlock:
5959 mmiowb();
5961 return NETDEV_TX_OK;
5963 dma_error:
5964 last = i;
5965 entry = tnapi->tx_prod;
5966 tnapi->tx_buffers[entry].skb = NULL;
5967 pci_unmap_single(tp->pdev,
5968 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
5969 skb_headlen(skb),
5970 PCI_DMA_TODEVICE);
5971 for (i = 0; i <= last; i++) {
5972 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5973 entry = NEXT_TX(entry);
5975 pci_unmap_page(tp->pdev,
5976 dma_unmap_addr(&tnapi->tx_buffers[entry],
5977 mapping),
5978 frag->size, PCI_DMA_TODEVICE);
5981 dev_kfree_skb(skb);
5982 return NETDEV_TX_OK;
5985 static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5986 struct net_device *);
5988 /* Use GSO to workaround a rare TSO bug that may be triggered when the
5989 * TSO header is greater than 80 bytes.
5991 static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5993 struct sk_buff *segs, *nskb;
5994 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
5996 /* Estimate the number of fragments in the worst case */
5997 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
5998 netif_stop_queue(tp->dev);
6000 /* netif_tx_stop_queue() must be done before checking
6001 * checking tx index in tg3_tx_avail() below, because in
6002 * tg3_tx(), we update tx index before checking for
6003 * netif_tx_queue_stopped().
6005 smp_mb();
6006 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
6007 return NETDEV_TX_BUSY;
6009 netif_wake_queue(tp->dev);
6012 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
6013 if (IS_ERR(segs))
6014 goto tg3_tso_bug_end;
6016 do {
6017 nskb = segs;
6018 segs = segs->next;
6019 nskb->next = NULL;
6020 tg3_start_xmit_dma_bug(nskb, tp->dev);
6021 } while (segs);
6023 tg3_tso_bug_end:
6024 dev_kfree_skb(skb);
6026 return NETDEV_TX_OK;
6029 /* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
6030 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
6032 static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
6033 struct net_device *dev)
6035 struct tg3 *tp = netdev_priv(dev);
6036 u32 len, entry, base_flags, mss;
6037 int would_hit_hwbug;
6038 dma_addr_t mapping;
6039 struct tg3_napi *tnapi;
6040 struct netdev_queue *txq;
6041 unsigned int i, last;
6043 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6044 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
6045 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
6046 tnapi++;
6048 /* We are running in BH disabled context with netif_tx_lock
6049 * and TX reclaim runs via tp->napi.poll inside of a software
6050 * interrupt. Furthermore, IRQ processing runs lockless so we have
6051 * no IRQ context deadlocks to worry about either. Rejoice!
6053 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
6054 if (!netif_tx_queue_stopped(txq)) {
6055 netif_tx_stop_queue(txq);
6057 /* This is a hard error, log it. */
6058 netdev_err(dev,
6059 "BUG! Tx Ring full when queue awake!\n");
6061 return NETDEV_TX_BUSY;
6064 entry = tnapi->tx_prod;
6065 base_flags = 0;
6066 if (skb->ip_summed == CHECKSUM_PARTIAL)
6067 base_flags |= TXD_FLAG_TCPUDP_CSUM;
6069 mss = skb_shinfo(skb)->gso_size;
6070 if (mss) {
6071 struct iphdr *iph;
6072 u32 tcp_opt_len, hdr_len;
6074 if (skb_header_cloned(skb) &&
6075 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6076 dev_kfree_skb(skb);
6077 goto out_unlock;
6080 iph = ip_hdr(skb);
6081 tcp_opt_len = tcp_optlen(skb);
6083 if (skb_is_gso_v6(skb)) {
6084 hdr_len = skb_headlen(skb) - ETH_HLEN;
6085 } else {
6086 u32 ip_tcp_len;
6088 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6089 hdr_len = ip_tcp_len + tcp_opt_len;
6091 iph->check = 0;
6092 iph->tot_len = htons(mss + hdr_len);
6095 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
6096 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
6097 return tg3_tso_bug(tp, skb);
6099 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6100 TXD_FLAG_CPU_POST_DMA);
6102 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
6103 tcp_hdr(skb)->check = 0;
6104 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
6105 } else
6106 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6107 iph->daddr, 0,
6108 IPPROTO_TCP,
6111 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
6112 mss |= (hdr_len & 0xc) << 12;
6113 if (hdr_len & 0x10)
6114 base_flags |= 0x00000010;
6115 base_flags |= (hdr_len & 0x3e0) << 5;
6116 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
6117 mss |= hdr_len << 9;
6118 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
6119 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
6120 if (tcp_opt_len || iph->ihl > 5) {
6121 int tsflags;
6123 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
6124 mss |= (tsflags << 11);
6126 } else {
6127 if (tcp_opt_len || iph->ihl > 5) {
6128 int tsflags;
6130 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
6131 base_flags |= tsflags << 12;
6136 if (vlan_tx_tag_present(skb))
6137 base_flags |= (TXD_FLAG_VLAN |
6138 (vlan_tx_tag_get(skb) << 16));
6140 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
6141 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6142 base_flags |= TXD_FLAG_JMB_PKT;
6144 len = skb_headlen(skb);
6146 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6147 if (pci_dma_mapping_error(tp->pdev, mapping)) {
6148 dev_kfree_skb(skb);
6149 goto out_unlock;
6152 tnapi->tx_buffers[entry].skb = skb;
6153 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
6155 would_hit_hwbug = 0;
6157 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
6158 would_hit_hwbug = 1;
6160 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6161 tg3_4g_overflow_test(mapping, len))
6162 would_hit_hwbug = 1;
6164 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6165 tg3_40bit_overflow_test(tp, mapping, len))
6166 would_hit_hwbug = 1;
6168 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
6169 would_hit_hwbug = 1;
6171 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
6172 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6174 entry = NEXT_TX(entry);
6176 /* Now loop through additional data fragments, and queue them. */
6177 if (skb_shinfo(skb)->nr_frags > 0) {
6178 last = skb_shinfo(skb)->nr_frags - 1;
6179 for (i = 0; i <= last; i++) {
6180 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6182 len = frag->size;
6183 mapping = pci_map_page(tp->pdev,
6184 frag->page,
6185 frag->page_offset,
6186 len, PCI_DMA_TODEVICE);
6188 tnapi->tx_buffers[entry].skb = NULL;
6189 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
6190 mapping);
6191 if (pci_dma_mapping_error(tp->pdev, mapping))
6192 goto dma_error;
6194 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
6195 len <= 8)
6196 would_hit_hwbug = 1;
6198 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6199 tg3_4g_overflow_test(mapping, len))
6200 would_hit_hwbug = 1;
6202 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6203 tg3_40bit_overflow_test(tp, mapping, len))
6204 would_hit_hwbug = 1;
6206 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
6207 tg3_set_txd(tnapi, entry, mapping, len,
6208 base_flags, (i == last)|(mss << 1));
6209 else
6210 tg3_set_txd(tnapi, entry, mapping, len,
6211 base_flags, (i == last));
6213 entry = NEXT_TX(entry);
6217 if (would_hit_hwbug) {
6218 u32 last_plus_one = entry;
6219 u32 start;
6221 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6222 start &= (TG3_TX_RING_SIZE - 1);
6224 /* If the workaround fails due to memory/mapping
6225 * failure, silently drop this packet.
6227 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
6228 &start, base_flags, mss))
6229 goto out_unlock;
6231 entry = start;
6234 /* Packets are ready, update Tx producer idx local and on card. */
6235 tw32_tx_mbox(tnapi->prodmbox, entry);
6237 tnapi->tx_prod = entry;
6238 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
6239 netif_tx_stop_queue(txq);
6241 /* netif_tx_stop_queue() must be done before checking
6242 * checking tx index in tg3_tx_avail() below, because in
6243 * tg3_tx(), we update tx index before checking for
6244 * netif_tx_queue_stopped().
6246 smp_mb();
6247 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
6248 netif_tx_wake_queue(txq);
6251 out_unlock:
6252 mmiowb();
6254 return NETDEV_TX_OK;
6256 dma_error:
6257 last = i;
6258 entry = tnapi->tx_prod;
6259 tnapi->tx_buffers[entry].skb = NULL;
6260 pci_unmap_single(tp->pdev,
6261 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
6262 skb_headlen(skb),
6263 PCI_DMA_TODEVICE);
6264 for (i = 0; i <= last; i++) {
6265 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6266 entry = NEXT_TX(entry);
6268 pci_unmap_page(tp->pdev,
6269 dma_unmap_addr(&tnapi->tx_buffers[entry],
6270 mapping),
6271 frag->size, PCI_DMA_TODEVICE);
6274 dev_kfree_skb(skb);
6275 return NETDEV_TX_OK;
6278 static u32 tg3_fix_features(struct net_device *dev, u32 features)
6280 struct tg3 *tp = netdev_priv(dev);
6282 if (dev->mtu > ETH_DATA_LEN && (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
6283 features &= ~NETIF_F_ALL_TSO;
6285 return features;
6288 static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6289 int new_mtu)
6291 dev->mtu = new_mtu;
6293 if (new_mtu > ETH_DATA_LEN) {
6294 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
6295 netdev_update_features(dev);
6296 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
6297 } else {
6298 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
6300 } else {
6301 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
6302 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
6303 netdev_update_features(dev);
6305 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
6309 static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6311 struct tg3 *tp = netdev_priv(dev);
6312 int err;
6314 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6315 return -EINVAL;
6317 if (!netif_running(dev)) {
6318 /* We'll just catch it later when the
6319 * device is up'd.
6321 tg3_set_mtu(dev, tp, new_mtu);
6322 return 0;
6325 tg3_phy_stop(tp);
6327 tg3_netif_stop(tp);
6329 tg3_full_lock(tp, 1);
6331 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6333 tg3_set_mtu(dev, tp, new_mtu);
6335 err = tg3_restart_hw(tp, 0);
6337 if (!err)
6338 tg3_netif_start(tp);
6340 tg3_full_unlock(tp);
6342 if (!err)
6343 tg3_phy_start(tp);
6345 return err;
6348 static void tg3_rx_prodring_free(struct tg3 *tp,
6349 struct tg3_rx_prodring_set *tpr)
6351 int i;
6353 if (tpr != &tp->napi[0].prodring) {
6354 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
6355 i = (i + 1) & tp->rx_std_ring_mask)
6356 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6357 tp->rx_pkt_map_sz);
6359 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6360 for (i = tpr->rx_jmb_cons_idx;
6361 i != tpr->rx_jmb_prod_idx;
6362 i = (i + 1) & tp->rx_jmb_ring_mask) {
6363 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6364 TG3_RX_JMB_MAP_SZ);
6368 return;
6371 for (i = 0; i <= tp->rx_std_ring_mask; i++)
6372 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6373 tp->rx_pkt_map_sz);
6375 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6376 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
6377 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
6378 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6379 TG3_RX_JMB_MAP_SZ);
6383 /* Initialize rx rings for packet processing.
6385 * The chip has been shut down and the driver detached from
6386 * the networking, so no interrupts or new tx packets will
6387 * end up in the driver. tp->{tx,}lock are held and thus
6388 * we may not sleep.
6390 static int tg3_rx_prodring_alloc(struct tg3 *tp,
6391 struct tg3_rx_prodring_set *tpr)
6393 u32 i, rx_pkt_dma_sz;
6395 tpr->rx_std_cons_idx = 0;
6396 tpr->rx_std_prod_idx = 0;
6397 tpr->rx_jmb_cons_idx = 0;
6398 tpr->rx_jmb_prod_idx = 0;
6400 if (tpr != &tp->napi[0].prodring) {
6401 memset(&tpr->rx_std_buffers[0], 0,
6402 TG3_RX_STD_BUFF_RING_SIZE(tp));
6403 if (tpr->rx_jmb_buffers)
6404 memset(&tpr->rx_jmb_buffers[0], 0,
6405 TG3_RX_JMB_BUFF_RING_SIZE(tp));
6406 goto done;
6409 /* Zero out all descriptors. */
6410 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
6412 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
6413 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
6414 tp->dev->mtu > ETH_DATA_LEN)
6415 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6416 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
6418 /* Initialize invariants of the rings, we only set this
6419 * stuff once. This works because the card does not
6420 * write into the rx buffer posting rings.
6422 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
6423 struct tg3_rx_buffer_desc *rxd;
6425 rxd = &tpr->rx_std[i];
6426 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
6427 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6428 rxd->opaque = (RXD_OPAQUE_RING_STD |
6429 (i << RXD_OPAQUE_INDEX_SHIFT));
6432 /* Now allocate fresh SKBs for each rx ring. */
6433 for (i = 0; i < tp->rx_pending; i++) {
6434 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
6435 netdev_warn(tp->dev,
6436 "Using a smaller RX standard ring. Only "
6437 "%d out of %d buffers were allocated "
6438 "successfully\n", i, tp->rx_pending);
6439 if (i == 0)
6440 goto initfail;
6441 tp->rx_pending = i;
6442 break;
6446 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
6447 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
6448 goto done;
6450 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
6452 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6453 goto done;
6455 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
6456 struct tg3_rx_buffer_desc *rxd;
6458 rxd = &tpr->rx_jmb[i].std;
6459 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6460 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6461 RXD_FLAG_JUMBO;
6462 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6463 (i << RXD_OPAQUE_INDEX_SHIFT));
6466 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6467 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
6468 netdev_warn(tp->dev,
6469 "Using a smaller RX jumbo ring. Only %d "
6470 "out of %d buffers were allocated "
6471 "successfully\n", i, tp->rx_jumbo_pending);
6472 if (i == 0)
6473 goto initfail;
6474 tp->rx_jumbo_pending = i;
6475 break;
6479 done:
6480 return 0;
6482 initfail:
6483 tg3_rx_prodring_free(tp, tpr);
6484 return -ENOMEM;
6487 static void tg3_rx_prodring_fini(struct tg3 *tp,
6488 struct tg3_rx_prodring_set *tpr)
6490 kfree(tpr->rx_std_buffers);
6491 tpr->rx_std_buffers = NULL;
6492 kfree(tpr->rx_jmb_buffers);
6493 tpr->rx_jmb_buffers = NULL;
6494 if (tpr->rx_std) {
6495 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6496 tpr->rx_std, tpr->rx_std_mapping);
6497 tpr->rx_std = NULL;
6499 if (tpr->rx_jmb) {
6500 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6501 tpr->rx_jmb, tpr->rx_jmb_mapping);
6502 tpr->rx_jmb = NULL;
6506 static int tg3_rx_prodring_init(struct tg3 *tp,
6507 struct tg3_rx_prodring_set *tpr)
6509 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6510 GFP_KERNEL);
6511 if (!tpr->rx_std_buffers)
6512 return -ENOMEM;
6514 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6515 TG3_RX_STD_RING_BYTES(tp),
6516 &tpr->rx_std_mapping,
6517 GFP_KERNEL);
6518 if (!tpr->rx_std)
6519 goto err_out;
6521 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6522 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
6523 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
6524 GFP_KERNEL);
6525 if (!tpr->rx_jmb_buffers)
6526 goto err_out;
6528 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6529 TG3_RX_JMB_RING_BYTES(tp),
6530 &tpr->rx_jmb_mapping,
6531 GFP_KERNEL);
6532 if (!tpr->rx_jmb)
6533 goto err_out;
6536 return 0;
6538 err_out:
6539 tg3_rx_prodring_fini(tp, tpr);
6540 return -ENOMEM;
6543 /* Free up pending packets in all rx/tx rings.
6545 * The chip has been shut down and the driver detached from
6546 * the networking, so no interrupts or new tx packets will
6547 * end up in the driver. tp->{tx,}lock is not held and we are not
6548 * in an interrupt context and thus may sleep.
6550 static void tg3_free_rings(struct tg3 *tp)
6552 int i, j;
6554 for (j = 0; j < tp->irq_cnt; j++) {
6555 struct tg3_napi *tnapi = &tp->napi[j];
6557 tg3_rx_prodring_free(tp, &tnapi->prodring);
6559 if (!tnapi->tx_buffers)
6560 continue;
6562 for (i = 0; i < TG3_TX_RING_SIZE; ) {
6563 struct ring_info *txp;
6564 struct sk_buff *skb;
6565 unsigned int k;
6567 txp = &tnapi->tx_buffers[i];
6568 skb = txp->skb;
6570 if (skb == NULL) {
6571 i++;
6572 continue;
6575 pci_unmap_single(tp->pdev,
6576 dma_unmap_addr(txp, mapping),
6577 skb_headlen(skb),
6578 PCI_DMA_TODEVICE);
6579 txp->skb = NULL;
6581 i++;
6583 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6584 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6585 pci_unmap_page(tp->pdev,
6586 dma_unmap_addr(txp, mapping),
6587 skb_shinfo(skb)->frags[k].size,
6588 PCI_DMA_TODEVICE);
6589 i++;
6592 dev_kfree_skb_any(skb);
6597 /* Initialize tx/rx rings for packet processing.
6599 * The chip has been shut down and the driver detached from
6600 * the networking, so no interrupts or new tx packets will
6601 * end up in the driver. tp->{tx,}lock are held and thus
6602 * we may not sleep.
6604 static int tg3_init_rings(struct tg3 *tp)
6606 int i;
6608 /* Free up all the SKBs. */
6609 tg3_free_rings(tp);
6611 for (i = 0; i < tp->irq_cnt; i++) {
6612 struct tg3_napi *tnapi = &tp->napi[i];
6614 tnapi->last_tag = 0;
6615 tnapi->last_irq_tag = 0;
6616 tnapi->hw_status->status = 0;
6617 tnapi->hw_status->status_tag = 0;
6618 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6620 tnapi->tx_prod = 0;
6621 tnapi->tx_cons = 0;
6622 if (tnapi->tx_ring)
6623 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
6625 tnapi->rx_rcb_ptr = 0;
6626 if (tnapi->rx_rcb)
6627 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
6629 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
6630 tg3_free_rings(tp);
6631 return -ENOMEM;
6635 return 0;
6639 * Must not be invoked with interrupt sources disabled and
6640 * the hardware shutdown down.
6642 static void tg3_free_consistent(struct tg3 *tp)
6644 int i;
6646 for (i = 0; i < tp->irq_cnt; i++) {
6647 struct tg3_napi *tnapi = &tp->napi[i];
6649 if (tnapi->tx_ring) {
6650 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
6651 tnapi->tx_ring, tnapi->tx_desc_mapping);
6652 tnapi->tx_ring = NULL;
6655 kfree(tnapi->tx_buffers);
6656 tnapi->tx_buffers = NULL;
6658 if (tnapi->rx_rcb) {
6659 dma_free_coherent(&tp->pdev->dev,
6660 TG3_RX_RCB_RING_BYTES(tp),
6661 tnapi->rx_rcb,
6662 tnapi->rx_rcb_mapping);
6663 tnapi->rx_rcb = NULL;
6666 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6668 if (tnapi->hw_status) {
6669 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6670 tnapi->hw_status,
6671 tnapi->status_mapping);
6672 tnapi->hw_status = NULL;
6676 if (tp->hw_stats) {
6677 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6678 tp->hw_stats, tp->stats_mapping);
6679 tp->hw_stats = NULL;
6684 * Must not be invoked with interrupt sources disabled and
6685 * the hardware shutdown down. Can sleep.
6687 static int tg3_alloc_consistent(struct tg3 *tp)
6689 int i;
6691 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6692 sizeof(struct tg3_hw_stats),
6693 &tp->stats_mapping,
6694 GFP_KERNEL);
6695 if (!tp->hw_stats)
6696 goto err_out;
6698 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6700 for (i = 0; i < tp->irq_cnt; i++) {
6701 struct tg3_napi *tnapi = &tp->napi[i];
6702 struct tg3_hw_status *sblk;
6704 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6705 TG3_HW_STATUS_SIZE,
6706 &tnapi->status_mapping,
6707 GFP_KERNEL);
6708 if (!tnapi->hw_status)
6709 goto err_out;
6711 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6712 sblk = tnapi->hw_status;
6714 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6715 goto err_out;
6717 /* If multivector TSS is enabled, vector 0 does not handle
6718 * tx interrupts. Don't allocate any resources for it.
6720 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6721 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6722 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6723 TG3_TX_RING_SIZE,
6724 GFP_KERNEL);
6725 if (!tnapi->tx_buffers)
6726 goto err_out;
6728 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6729 TG3_TX_RING_BYTES,
6730 &tnapi->tx_desc_mapping,
6731 GFP_KERNEL);
6732 if (!tnapi->tx_ring)
6733 goto err_out;
6737 * When RSS is enabled, the status block format changes
6738 * slightly. The "rx_jumbo_consumer", "reserved",
6739 * and "rx_mini_consumer" members get mapped to the
6740 * other three rx return ring producer indexes.
6742 switch (i) {
6743 default:
6744 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6745 break;
6746 case 2:
6747 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6748 break;
6749 case 3:
6750 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6751 break;
6752 case 4:
6753 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6754 break;
6758 * If multivector RSS is enabled, vector 0 does not handle
6759 * rx or tx interrupts. Don't allocate any resources for it.
6761 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6762 continue;
6764 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6765 TG3_RX_RCB_RING_BYTES(tp),
6766 &tnapi->rx_rcb_mapping,
6767 GFP_KERNEL);
6768 if (!tnapi->rx_rcb)
6769 goto err_out;
6771 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
6774 return 0;
6776 err_out:
6777 tg3_free_consistent(tp);
6778 return -ENOMEM;
6781 #define MAX_WAIT_CNT 1000
6783 /* To stop a block, clear the enable bit and poll till it
6784 * clears. tp->lock is held.
6786 static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
6788 unsigned int i;
6789 u32 val;
6791 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6792 switch (ofs) {
6793 case RCVLSC_MODE:
6794 case DMAC_MODE:
6795 case MBFREE_MODE:
6796 case BUFMGR_MODE:
6797 case MEMARB_MODE:
6798 /* We can't enable/disable these bits of the
6799 * 5705/5750, just say success.
6801 return 0;
6803 default:
6804 break;
6808 val = tr32(ofs);
6809 val &= ~enable_bit;
6810 tw32_f(ofs, val);
6812 for (i = 0; i < MAX_WAIT_CNT; i++) {
6813 udelay(100);
6814 val = tr32(ofs);
6815 if ((val & enable_bit) == 0)
6816 break;
6819 if (i == MAX_WAIT_CNT && !silent) {
6820 dev_err(&tp->pdev->dev,
6821 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6822 ofs, enable_bit);
6823 return -ENODEV;
6826 return 0;
6829 /* tp->lock is held. */
6830 static int tg3_abort_hw(struct tg3 *tp, int silent)
6832 int i, err;
6834 tg3_disable_ints(tp);
6836 tp->rx_mode &= ~RX_MODE_ENABLE;
6837 tw32_f(MAC_RX_MODE, tp->rx_mode);
6838 udelay(10);
6840 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6841 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6842 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6843 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6844 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6845 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
6847 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6848 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6849 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6850 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6851 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6852 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6853 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
6855 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6856 tw32_f(MAC_MODE, tp->mac_mode);
6857 udelay(40);
6859 tp->tx_mode &= ~TX_MODE_ENABLE;
6860 tw32_f(MAC_TX_MODE, tp->tx_mode);
6862 for (i = 0; i < MAX_WAIT_CNT; i++) {
6863 udelay(100);
6864 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6865 break;
6867 if (i >= MAX_WAIT_CNT) {
6868 dev_err(&tp->pdev->dev,
6869 "%s timed out, TX_MODE_ENABLE will not clear "
6870 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
6871 err |= -ENODEV;
6874 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
6875 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6876 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
6878 tw32(FTQ_RESET, 0xffffffff);
6879 tw32(FTQ_RESET, 0x00000000);
6881 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6882 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
6884 for (i = 0; i < tp->irq_cnt; i++) {
6885 struct tg3_napi *tnapi = &tp->napi[i];
6886 if (tnapi->hw_status)
6887 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6889 if (tp->hw_stats)
6890 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6892 return err;
6895 static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6897 int i;
6898 u32 apedata;
6900 /* NCSI does not support APE events */
6901 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6902 return;
6904 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6905 if (apedata != APE_SEG_SIG_MAGIC)
6906 return;
6908 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
6909 if (!(apedata & APE_FW_STATUS_READY))
6910 return;
6912 /* Wait for up to 1 millisecond for APE to service previous event. */
6913 for (i = 0; i < 10; i++) {
6914 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6915 return;
6917 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6919 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6920 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6921 event | APE_EVENT_STATUS_EVENT_PENDING);
6923 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6925 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6926 break;
6928 udelay(100);
6931 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6932 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6935 static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6937 u32 event;
6938 u32 apedata;
6940 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6941 return;
6943 switch (kind) {
6944 case RESET_KIND_INIT:
6945 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6946 APE_HOST_SEG_SIG_MAGIC);
6947 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6948 APE_HOST_SEG_LEN_MAGIC);
6949 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6950 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6951 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6952 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
6953 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6954 APE_HOST_BEHAV_NO_PHYLOCK);
6955 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6956 TG3_APE_HOST_DRVR_STATE_START);
6958 event = APE_EVENT_STATUS_STATE_START;
6959 break;
6960 case RESET_KIND_SHUTDOWN:
6961 /* With the interface we are currently using,
6962 * APE does not track driver state. Wiping
6963 * out the HOST SEGMENT SIGNATURE forces
6964 * the APE to assume OS absent status.
6966 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
6968 if (device_may_wakeup(&tp->pdev->dev) &&
6969 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6970 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6971 TG3_APE_HOST_WOL_SPEED_AUTO);
6972 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6973 } else
6974 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6976 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6978 event = APE_EVENT_STATUS_STATE_UNLOAD;
6979 break;
6980 case RESET_KIND_SUSPEND:
6981 event = APE_EVENT_STATUS_STATE_SUSPEND;
6982 break;
6983 default:
6984 return;
6987 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6989 tg3_ape_send_event(tp, event);
6992 /* tp->lock is held. */
6993 static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6995 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6996 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
6998 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6999 switch (kind) {
7000 case RESET_KIND_INIT:
7001 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7002 DRV_STATE_START);
7003 break;
7005 case RESET_KIND_SHUTDOWN:
7006 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7007 DRV_STATE_UNLOAD);
7008 break;
7010 case RESET_KIND_SUSPEND:
7011 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7012 DRV_STATE_SUSPEND);
7013 break;
7015 default:
7016 break;
7020 if (kind == RESET_KIND_INIT ||
7021 kind == RESET_KIND_SUSPEND)
7022 tg3_ape_driver_state_change(tp, kind);
7025 /* tp->lock is held. */
7026 static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
7028 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
7029 switch (kind) {
7030 case RESET_KIND_INIT:
7031 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7032 DRV_STATE_START_DONE);
7033 break;
7035 case RESET_KIND_SHUTDOWN:
7036 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7037 DRV_STATE_UNLOAD_DONE);
7038 break;
7040 default:
7041 break;
7045 if (kind == RESET_KIND_SHUTDOWN)
7046 tg3_ape_driver_state_change(tp, kind);
7049 /* tp->lock is held. */
7050 static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7052 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
7053 switch (kind) {
7054 case RESET_KIND_INIT:
7055 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7056 DRV_STATE_START);
7057 break;
7059 case RESET_KIND_SHUTDOWN:
7060 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7061 DRV_STATE_UNLOAD);
7062 break;
7064 case RESET_KIND_SUSPEND:
7065 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7066 DRV_STATE_SUSPEND);
7067 break;
7069 default:
7070 break;
7075 static int tg3_poll_fw(struct tg3 *tp)
7077 int i;
7078 u32 val;
7080 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7081 /* Wait up to 20ms for init done. */
7082 for (i = 0; i < 200; i++) {
7083 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7084 return 0;
7085 udelay(100);
7087 return -ENODEV;
7090 /* Wait for firmware initialization to complete. */
7091 for (i = 0; i < 100000; i++) {
7092 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7093 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7094 break;
7095 udelay(10);
7098 /* Chip might not be fitted with firmware. Some Sun onboard
7099 * parts are configured like that. So don't signal the timeout
7100 * of the above loop as an error, but do report the lack of
7101 * running firmware once.
7103 if (i >= 100000 &&
7104 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
7105 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
7107 netdev_info(tp->dev, "No firmware running\n");
7110 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7111 /* The 57765 A0 needs a little more
7112 * time to do some important work.
7114 mdelay(10);
7117 return 0;
7120 /* Save PCI command register before chip reset */
7121 static void tg3_save_pci_state(struct tg3 *tp)
7123 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
7126 /* Restore PCI state after chip reset */
7127 static void tg3_restore_pci_state(struct tg3 *tp)
7129 u32 val;
7131 /* Re-enable indirect register accesses. */
7132 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7133 tp->misc_host_ctrl);
7135 /* Set MAX PCI retry to zero. */
7136 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7137 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7138 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
7139 val |= PCISTATE_RETRY_SAME_DMA;
7140 /* Allow reads and writes to the APE register and memory space. */
7141 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7142 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
7143 PCISTATE_ALLOW_APE_SHMEM_WR |
7144 PCISTATE_ALLOW_APE_PSPACE_WR;
7145 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7147 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
7149 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
7150 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7151 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
7152 else {
7153 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7154 tp->pci_cacheline_sz);
7155 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7156 tp->pci_lat_timer);
7160 /* Make sure PCI-X relaxed ordering bit is clear. */
7161 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
7162 u16 pcix_cmd;
7164 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7165 &pcix_cmd);
7166 pcix_cmd &= ~PCI_X_CMD_ERO;
7167 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7168 pcix_cmd);
7171 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
7173 /* Chip reset on 5780 will reset MSI enable bit,
7174 * so need to restore it.
7176 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7177 u16 ctrl;
7179 pci_read_config_word(tp->pdev,
7180 tp->msi_cap + PCI_MSI_FLAGS,
7181 &ctrl);
7182 pci_write_config_word(tp->pdev,
7183 tp->msi_cap + PCI_MSI_FLAGS,
7184 ctrl | PCI_MSI_FLAGS_ENABLE);
7185 val = tr32(MSGINT_MODE);
7186 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7191 static void tg3_stop_fw(struct tg3 *);
7193 /* tp->lock is held. */
7194 static int tg3_chip_reset(struct tg3 *tp)
7196 u32 val;
7197 void (*write_op)(struct tg3 *, u32, u32);
7198 int i, err;
7200 tg3_nvram_lock(tp);
7202 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7204 /* No matching tg3_nvram_unlock() after this because
7205 * chip reset below will undo the nvram lock.
7207 tp->nvram_lock_cnt = 0;
7209 /* GRC_MISC_CFG core clock reset will clear the memory
7210 * enable bit in PCI register 4 and the MSI enable bit
7211 * on some chips, so we save relevant registers here.
7213 tg3_save_pci_state(tp);
7215 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
7216 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
7217 tw32(GRC_FASTBOOT_PC, 0);
7220 * We must avoid the readl() that normally takes place.
7221 * It locks machines, causes machine checks, and other
7222 * fun things. So, temporarily disable the 5701
7223 * hardware workaround, while we do the reset.
7225 write_op = tp->write32;
7226 if (write_op == tg3_write_flush_reg32)
7227 tp->write32 = tg3_write32;
7229 /* Prevent the irq handler from reading or writing PCI registers
7230 * during chip reset when the memory enable bit in the PCI command
7231 * register may be cleared. The chip does not generate interrupt
7232 * at this time, but the irq handler may still be called due to irq
7233 * sharing or irqpoll.
7235 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
7236 for (i = 0; i < tp->irq_cnt; i++) {
7237 struct tg3_napi *tnapi = &tp->napi[i];
7238 if (tnapi->hw_status) {
7239 tnapi->hw_status->status = 0;
7240 tnapi->hw_status->status_tag = 0;
7242 tnapi->last_tag = 0;
7243 tnapi->last_irq_tag = 0;
7245 smp_mb();
7247 for (i = 0; i < tp->irq_cnt; i++)
7248 synchronize_irq(tp->napi[i].irq_vec);
7250 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7251 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7252 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7255 /* do the reset */
7256 val = GRC_MISC_CFG_CORECLK_RESET;
7258 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
7259 /* Force PCIe 1.0a mode */
7260 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
7261 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
7262 tr32(TG3_PCIE_PHY_TSTCTL) ==
7263 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7264 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7266 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7267 tw32(GRC_MISC_CFG, (1 << 29));
7268 val |= (1 << 29);
7272 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7273 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7274 tw32(GRC_VCPU_EXT_CTRL,
7275 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7278 /* Manage gphy power for all CPMU absent PCIe devices. */
7279 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7280 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
7281 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
7283 tw32(GRC_MISC_CFG, val);
7285 /* restore 5701 hardware bug workaround write method */
7286 tp->write32 = write_op;
7288 /* Unfortunately, we have to delay before the PCI read back.
7289 * Some 575X chips even will not respond to a PCI cfg access
7290 * when the reset command is given to the chip.
7292 * How do these hardware designers expect things to work
7293 * properly if the PCI write is posted for a long period
7294 * of time? It is always necessary to have some method by
7295 * which a register read back can occur to push the write
7296 * out which does the reset.
7298 * For most tg3 variants the trick below was working.
7299 * Ho hum...
7301 udelay(120);
7303 /* Flush PCI posted writes. The normal MMIO registers
7304 * are inaccessible at this time so this is the only
7305 * way to make this reliably (actually, this is no longer
7306 * the case, see above). I tried to use indirect
7307 * register read/write but this upset some 5701 variants.
7309 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7311 udelay(120);
7313 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
7314 u16 val16;
7316 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7317 int i;
7318 u32 cfg_val;
7320 /* Wait for link training to complete. */
7321 for (i = 0; i < 5000; i++)
7322 udelay(100);
7324 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7325 pci_write_config_dword(tp->pdev, 0xc4,
7326 cfg_val | (1 << 15));
7329 /* Clear the "no snoop" and "relaxed ordering" bits. */
7330 pci_read_config_word(tp->pdev,
7331 tp->pcie_cap + PCI_EXP_DEVCTL,
7332 &val16);
7333 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7334 PCI_EXP_DEVCTL_NOSNOOP_EN);
7336 * Older PCIe devices only support the 128 byte
7337 * MPS setting. Enforce the restriction.
7339 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
7340 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
7341 pci_write_config_word(tp->pdev,
7342 tp->pcie_cap + PCI_EXP_DEVCTL,
7343 val16);
7345 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
7347 /* Clear error status */
7348 pci_write_config_word(tp->pdev,
7349 tp->pcie_cap + PCI_EXP_DEVSTA,
7350 PCI_EXP_DEVSTA_CED |
7351 PCI_EXP_DEVSTA_NFED |
7352 PCI_EXP_DEVSTA_FED |
7353 PCI_EXP_DEVSTA_URD);
7356 tg3_restore_pci_state(tp);
7358 tp->tg3_flags &= ~(TG3_FLAG_CHIP_RESETTING |
7359 TG3_FLAG_ERROR_PROCESSED);
7361 val = 0;
7362 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
7363 val = tr32(MEMARB_MODE);
7364 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
7366 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7367 tg3_stop_fw(tp);
7368 tw32(0x5000, 0x400);
7371 tw32(GRC_MODE, tp->grc_mode);
7373 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
7374 val = tr32(0xc4);
7376 tw32(0xc4, val | (1 << 15));
7379 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7380 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7381 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7382 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7383 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7384 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7387 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7388 tp->mac_mode = MAC_MODE_APE_TX_EN |
7389 MAC_MODE_APE_RX_EN |
7390 MAC_MODE_TDE_ENABLE;
7392 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
7393 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7394 val = tp->mac_mode;
7395 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
7396 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7397 val = tp->mac_mode;
7398 } else
7399 val = 0;
7401 tw32_f(MAC_MODE, val);
7402 udelay(40);
7404 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7406 err = tg3_poll_fw(tp);
7407 if (err)
7408 return err;
7410 tg3_mdio_start(tp);
7412 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
7413 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7414 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
7415 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
7416 val = tr32(0x7c00);
7418 tw32(0x7c00, val | (1 << 25));
7421 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7422 val = tr32(TG3_CPMU_CLCK_ORIDE);
7423 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7426 /* Reprobe ASF enable state. */
7427 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7428 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7429 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7430 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7431 u32 nic_cfg;
7433 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7434 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7435 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
7436 tp->last_event_jiffies = jiffies;
7437 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
7438 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7442 return 0;
7445 /* tp->lock is held. */
7446 static void tg3_stop_fw(struct tg3 *tp)
7448 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7449 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
7450 /* Wait for RX cpu to ACK the previous event. */
7451 tg3_wait_for_event_ack(tp);
7453 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
7455 tg3_generate_fw_event(tp);
7457 /* Wait for RX cpu to ACK this event. */
7458 tg3_wait_for_event_ack(tp);
7462 /* tp->lock is held. */
7463 static int tg3_halt(struct tg3 *tp, int kind, int silent)
7465 int err;
7467 tg3_stop_fw(tp);
7469 tg3_write_sig_pre_reset(tp, kind);
7471 tg3_abort_hw(tp, silent);
7472 err = tg3_chip_reset(tp);
7474 __tg3_set_mac_addr(tp, 0);
7476 tg3_write_sig_legacy(tp, kind);
7477 tg3_write_sig_post_reset(tp, kind);
7479 if (err)
7480 return err;
7482 return 0;
7485 #define RX_CPU_SCRATCH_BASE 0x30000
7486 #define RX_CPU_SCRATCH_SIZE 0x04000
7487 #define TX_CPU_SCRATCH_BASE 0x34000
7488 #define TX_CPU_SCRATCH_SIZE 0x04000
7490 /* tp->lock is held. */
7491 static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7493 int i;
7495 BUG_ON(offset == TX_CPU_BASE &&
7496 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
7498 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7499 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7501 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7502 return 0;
7504 if (offset == RX_CPU_BASE) {
7505 for (i = 0; i < 10000; i++) {
7506 tw32(offset + CPU_STATE, 0xffffffff);
7507 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7508 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7509 break;
7512 tw32(offset + CPU_STATE, 0xffffffff);
7513 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7514 udelay(10);
7515 } else {
7516 for (i = 0; i < 10000; i++) {
7517 tw32(offset + CPU_STATE, 0xffffffff);
7518 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7519 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7520 break;
7524 if (i >= 10000) {
7525 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7526 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
7527 return -ENODEV;
7530 /* Clear firmware's nvram arbitration. */
7531 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7532 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
7533 return 0;
7536 struct fw_info {
7537 unsigned int fw_base;
7538 unsigned int fw_len;
7539 const __be32 *fw_data;
7542 /* tp->lock is held. */
7543 static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7544 int cpu_scratch_size, struct fw_info *info)
7546 int err, lock_err, i;
7547 void (*write_op)(struct tg3 *, u32, u32);
7549 if (cpu_base == TX_CPU_BASE &&
7550 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7551 netdev_err(tp->dev,
7552 "%s: Trying to load TX cpu firmware which is 5705\n",
7553 __func__);
7554 return -EINVAL;
7557 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7558 write_op = tg3_write_mem;
7559 else
7560 write_op = tg3_write_indirect_reg32;
7562 /* It is possible that bootcode is still loading at this point.
7563 * Get the nvram lock first before halting the cpu.
7565 lock_err = tg3_nvram_lock(tp);
7566 err = tg3_halt_cpu(tp, cpu_base);
7567 if (!lock_err)
7568 tg3_nvram_unlock(tp);
7569 if (err)
7570 goto out;
7572 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7573 write_op(tp, cpu_scratch_base + i, 0);
7574 tw32(cpu_base + CPU_STATE, 0xffffffff);
7575 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
7576 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
7577 write_op(tp, (cpu_scratch_base +
7578 (info->fw_base & 0xffff) +
7579 (i * sizeof(u32))),
7580 be32_to_cpu(info->fw_data[i]));
7582 err = 0;
7584 out:
7585 return err;
7588 /* tp->lock is held. */
7589 static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7591 struct fw_info info;
7592 const __be32 *fw_data;
7593 int err, i;
7595 fw_data = (void *)tp->fw->data;
7597 /* Firmware blob starts with version numbers, followed by
7598 start address and length. We are setting complete length.
7599 length = end_address_of_bss - start_address_of_text.
7600 Remainder is the blob to be loaded contiguously
7601 from start address. */
7603 info.fw_base = be32_to_cpu(fw_data[1]);
7604 info.fw_len = tp->fw->size - 12;
7605 info.fw_data = &fw_data[3];
7607 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7608 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7609 &info);
7610 if (err)
7611 return err;
7613 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7614 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7615 &info);
7616 if (err)
7617 return err;
7619 /* Now startup only the RX cpu. */
7620 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7621 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
7623 for (i = 0; i < 5; i++) {
7624 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
7625 break;
7626 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7627 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
7628 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
7629 udelay(1000);
7631 if (i >= 5) {
7632 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7633 "should be %08x\n", __func__,
7634 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
7635 return -ENODEV;
7637 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7638 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7640 return 0;
7643 /* 5705 needs a special version of the TSO firmware. */
7645 /* tp->lock is held. */
7646 static int tg3_load_tso_firmware(struct tg3 *tp)
7648 struct fw_info info;
7649 const __be32 *fw_data;
7650 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7651 int err, i;
7653 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7654 return 0;
7656 fw_data = (void *)tp->fw->data;
7658 /* Firmware blob starts with version numbers, followed by
7659 start address and length. We are setting complete length.
7660 length = end_address_of_bss - start_address_of_text.
7661 Remainder is the blob to be loaded contiguously
7662 from start address. */
7664 info.fw_base = be32_to_cpu(fw_data[1]);
7665 cpu_scratch_size = tp->fw_len;
7666 info.fw_len = tp->fw->size - 12;
7667 info.fw_data = &fw_data[3];
7669 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7670 cpu_base = RX_CPU_BASE;
7671 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
7672 } else {
7673 cpu_base = TX_CPU_BASE;
7674 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7675 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7678 err = tg3_load_firmware_cpu(tp, cpu_base,
7679 cpu_scratch_base, cpu_scratch_size,
7680 &info);
7681 if (err)
7682 return err;
7684 /* Now startup the cpu. */
7685 tw32(cpu_base + CPU_STATE, 0xffffffff);
7686 tw32_f(cpu_base + CPU_PC, info.fw_base);
7688 for (i = 0; i < 5; i++) {
7689 if (tr32(cpu_base + CPU_PC) == info.fw_base)
7690 break;
7691 tw32(cpu_base + CPU_STATE, 0xffffffff);
7692 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
7693 tw32_f(cpu_base + CPU_PC, info.fw_base);
7694 udelay(1000);
7696 if (i >= 5) {
7697 netdev_err(tp->dev,
7698 "%s fails to set CPU PC, is %08x should be %08x\n",
7699 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
7700 return -ENODEV;
7702 tw32(cpu_base + CPU_STATE, 0xffffffff);
7703 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7704 return 0;
7708 static int tg3_set_mac_addr(struct net_device *dev, void *p)
7710 struct tg3 *tp = netdev_priv(dev);
7711 struct sockaddr *addr = p;
7712 int err = 0, skip_mac_1 = 0;
7714 if (!is_valid_ether_addr(addr->sa_data))
7715 return -EINVAL;
7717 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7719 if (!netif_running(dev))
7720 return 0;
7722 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
7723 u32 addr0_high, addr0_low, addr1_high, addr1_low;
7725 addr0_high = tr32(MAC_ADDR_0_HIGH);
7726 addr0_low = tr32(MAC_ADDR_0_LOW);
7727 addr1_high = tr32(MAC_ADDR_1_HIGH);
7728 addr1_low = tr32(MAC_ADDR_1_LOW);
7730 /* Skip MAC addr 1 if ASF is using it. */
7731 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7732 !(addr1_high == 0 && addr1_low == 0))
7733 skip_mac_1 = 1;
7735 spin_lock_bh(&tp->lock);
7736 __tg3_set_mac_addr(tp, skip_mac_1);
7737 spin_unlock_bh(&tp->lock);
7739 return err;
7742 /* tp->lock is held. */
7743 static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7744 dma_addr_t mapping, u32 maxlen_flags,
7745 u32 nic_addr)
7747 tg3_write_mem(tp,
7748 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7749 ((u64) mapping >> 32));
7750 tg3_write_mem(tp,
7751 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7752 ((u64) mapping & 0xffffffff));
7753 tg3_write_mem(tp,
7754 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7755 maxlen_flags);
7757 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7758 tg3_write_mem(tp,
7759 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7760 nic_addr);
7763 static void __tg3_set_rx_mode(struct net_device *);
7764 static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
7766 int i;
7768 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
7769 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7770 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7771 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
7772 } else {
7773 tw32(HOSTCC_TXCOL_TICKS, 0);
7774 tw32(HOSTCC_TXMAX_FRAMES, 0);
7775 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
7778 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
7779 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7780 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7781 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7782 } else {
7783 tw32(HOSTCC_RXCOL_TICKS, 0);
7784 tw32(HOSTCC_RXMAX_FRAMES, 0);
7785 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
7788 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7789 u32 val = ec->stats_block_coalesce_usecs;
7791 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7792 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7794 if (!netif_carrier_ok(tp->dev))
7795 val = 0;
7797 tw32(HOSTCC_STAT_COAL_TICKS, val);
7800 for (i = 0; i < tp->irq_cnt - 1; i++) {
7801 u32 reg;
7803 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7804 tw32(reg, ec->rx_coalesce_usecs);
7805 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7806 tw32(reg, ec->rx_max_coalesced_frames);
7807 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7808 tw32(reg, ec->rx_max_coalesced_frames_irq);
7810 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7811 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7812 tw32(reg, ec->tx_coalesce_usecs);
7813 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7814 tw32(reg, ec->tx_max_coalesced_frames);
7815 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7816 tw32(reg, ec->tx_max_coalesced_frames_irq);
7820 for (; i < tp->irq_max - 1; i++) {
7821 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
7822 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
7823 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7825 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7826 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7827 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7828 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7833 /* tp->lock is held. */
7834 static void tg3_rings_reset(struct tg3 *tp)
7836 int i;
7837 u32 stblk, txrcb, rxrcb, limit;
7838 struct tg3_napi *tnapi = &tp->napi[0];
7840 /* Disable all transmit rings but the first. */
7841 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7842 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
7843 else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
7844 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
7845 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7846 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
7847 else
7848 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7850 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7851 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7852 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7853 BDINFO_FLAGS_DISABLED);
7856 /* Disable all receive return rings but the first. */
7857 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
7858 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7859 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7860 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
7861 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7862 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7863 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7864 else
7865 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7867 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7868 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7869 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7870 BDINFO_FLAGS_DISABLED);
7872 /* Disable interrupts */
7873 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7875 /* Zero mailbox registers. */
7876 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7877 for (i = 1; i < tp->irq_max; i++) {
7878 tp->napi[i].tx_prod = 0;
7879 tp->napi[i].tx_cons = 0;
7880 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7881 tw32_mailbox(tp->napi[i].prodmbox, 0);
7882 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7883 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7885 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7886 tw32_mailbox(tp->napi[0].prodmbox, 0);
7887 } else {
7888 tp->napi[0].tx_prod = 0;
7889 tp->napi[0].tx_cons = 0;
7890 tw32_mailbox(tp->napi[0].prodmbox, 0);
7891 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7894 /* Make sure the NIC-based send BD rings are disabled. */
7895 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7896 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7897 for (i = 0; i < 16; i++)
7898 tw32_tx_mbox(mbox + i * 8, 0);
7901 txrcb = NIC_SRAM_SEND_RCB;
7902 rxrcb = NIC_SRAM_RCV_RET_RCB;
7904 /* Clear status block in ram. */
7905 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7907 /* Set status block DMA address */
7908 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7909 ((u64) tnapi->status_mapping >> 32));
7910 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7911 ((u64) tnapi->status_mapping & 0xffffffff));
7913 if (tnapi->tx_ring) {
7914 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7915 (TG3_TX_RING_SIZE <<
7916 BDINFO_FLAGS_MAXLEN_SHIFT),
7917 NIC_SRAM_TX_BUFFER_DESC);
7918 txrcb += TG3_BDINFO_SIZE;
7921 if (tnapi->rx_rcb) {
7922 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7923 (tp->rx_ret_ring_mask + 1) <<
7924 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
7925 rxrcb += TG3_BDINFO_SIZE;
7928 stblk = HOSTCC_STATBLCK_RING1;
7930 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7931 u64 mapping = (u64)tnapi->status_mapping;
7932 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7933 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7935 /* Clear status block in ram. */
7936 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7938 if (tnapi->tx_ring) {
7939 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7940 (TG3_TX_RING_SIZE <<
7941 BDINFO_FLAGS_MAXLEN_SHIFT),
7942 NIC_SRAM_TX_BUFFER_DESC);
7943 txrcb += TG3_BDINFO_SIZE;
7946 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7947 ((tp->rx_ret_ring_mask + 1) <<
7948 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7950 stblk += 8;
7951 rxrcb += TG3_BDINFO_SIZE;
7955 static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7957 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7959 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS) ||
7960 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
7961 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7962 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7963 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7964 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7966 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7967 else
7968 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7970 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7971 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7973 val = min(nic_rep_thresh, host_rep_thresh);
7974 tw32(RCVBDI_STD_THRESH, val);
7976 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
7977 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
7979 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
7980 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
7981 return;
7983 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7984 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
7985 else
7986 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
7988 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
7990 val = min(bdcache_maxcnt / 2, host_rep_thresh);
7991 tw32(RCVBDI_JUMBO_THRESH, val);
7993 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
7994 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
7997 /* tp->lock is held. */
7998 static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
8000 u32 val, rdmac_mode;
8001 int i, err, limit;
8002 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
8004 tg3_disable_ints(tp);
8006 tg3_stop_fw(tp);
8008 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8010 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
8011 tg3_abort_hw(tp, 1);
8013 /* Enable MAC control of LPI */
8014 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8015 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8016 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8017 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8019 tw32_f(TG3_CPMU_EEE_CTRL,
8020 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8022 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8023 TG3_CPMU_EEEMD_LPI_IN_TX |
8024 TG3_CPMU_EEEMD_LPI_IN_RX |
8025 TG3_CPMU_EEEMD_EEE_ENABLE;
8027 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8028 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8030 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8031 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8033 tw32_f(TG3_CPMU_EEE_MODE, val);
8035 tw32_f(TG3_CPMU_EEE_DBTMR1,
8036 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8037 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8039 tw32_f(TG3_CPMU_EEE_DBTMR2,
8040 TG3_CPMU_DBTMR2_APE_TX_2047US |
8041 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
8044 if (reset_phy)
8045 tg3_phy_reset(tp);
8047 err = tg3_chip_reset(tp);
8048 if (err)
8049 return err;
8051 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8053 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
8054 val = tr32(TG3_CPMU_CTRL);
8055 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8056 tw32(TG3_CPMU_CTRL, val);
8058 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8059 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8060 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8061 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8063 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8064 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8065 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8066 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8068 val = tr32(TG3_CPMU_HST_ACC);
8069 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8070 val |= CPMU_HST_ACC_MACCLK_6_25;
8071 tw32(TG3_CPMU_HST_ACC, val);
8074 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8075 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8076 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8077 PCIE_PWR_MGMT_L1_THRESH_4MS;
8078 tw32(PCIE_PWR_MGMT_THRESH, val);
8080 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8081 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8083 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
8085 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8086 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8089 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
8090 u32 grc_mode = tr32(GRC_MODE);
8092 /* Access the lower 1K of PL PCIE block registers. */
8093 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8094 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8096 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8097 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8098 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8100 tw32(GRC_MODE, grc_mode);
8103 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8104 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8105 u32 grc_mode = tr32(GRC_MODE);
8107 /* Access the lower 1K of PL PCIE block registers. */
8108 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8109 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8111 val = tr32(TG3_PCIE_TLDLPL_PORT +
8112 TG3_PCIE_PL_LO_PHYCTL5);
8113 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8114 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
8116 tw32(GRC_MODE, grc_mode);
8119 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8120 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8121 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8122 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8125 /* This works around an issue with Athlon chipsets on
8126 * B3 tigon3 silicon. This bit has no effect on any
8127 * other revision. But do not set this on PCI Express
8128 * chips and don't even touch the clocks if the CPMU is present.
8130 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
8131 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
8132 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8133 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8136 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
8137 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
8138 val = tr32(TG3PCI_PCISTATE);
8139 val |= PCISTATE_RETRY_SAME_DMA;
8140 tw32(TG3PCI_PCISTATE, val);
8143 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
8144 /* Allow reads and writes to the
8145 * APE register and memory space.
8147 val = tr32(TG3PCI_PCISTATE);
8148 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
8149 PCISTATE_ALLOW_APE_SHMEM_WR |
8150 PCISTATE_ALLOW_APE_PSPACE_WR;
8151 tw32(TG3PCI_PCISTATE, val);
8154 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8155 /* Enable some hw fixes. */
8156 val = tr32(TG3PCI_MSI_DATA);
8157 val |= (1 << 26) | (1 << 28) | (1 << 29);
8158 tw32(TG3PCI_MSI_DATA, val);
8161 /* Descriptor ring init may make accesses to the
8162 * NIC SRAM area to setup the TX descriptors, so we
8163 * can only do this after the hardware has been
8164 * successfully reset.
8166 err = tg3_init_rings(tp);
8167 if (err)
8168 return err;
8170 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
8171 val = tr32(TG3PCI_DMA_RW_CTRL) &
8172 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
8173 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8174 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
8175 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8176 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8177 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
8178 /* This value is determined during the probe time DMA
8179 * engine test, tg3_test_dma.
8181 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8184 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8185 GRC_MODE_4X_NIC_SEND_RINGS |
8186 GRC_MODE_NO_TX_PHDR_CSUM |
8187 GRC_MODE_NO_RX_PHDR_CSUM);
8188 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
8190 /* Pseudo-header checksum is done by hardware logic and not
8191 * the offload processers, so make the chip do the pseudo-
8192 * header checksums on receive. For transmit it is more
8193 * convenient to do the pseudo-header checksum in software
8194 * as Linux does that on transmit for us in all cases.
8196 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
8198 tw32(GRC_MODE,
8199 tp->grc_mode |
8200 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8202 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8203 val = tr32(GRC_MISC_CFG);
8204 val &= ~0xff;
8205 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8206 tw32(GRC_MISC_CFG, val);
8208 /* Initialize MBUF/DESC pool. */
8209 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8210 /* Do nothing. */
8211 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8212 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8213 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8214 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8215 else
8216 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8217 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8218 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
8219 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8220 int fw_len;
8222 fw_len = tp->fw_len;
8223 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8224 tw32(BUFMGR_MB_POOL_ADDR,
8225 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8226 tw32(BUFMGR_MB_POOL_SIZE,
8227 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8230 if (tp->dev->mtu <= ETH_DATA_LEN) {
8231 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8232 tp->bufmgr_config.mbuf_read_dma_low_water);
8233 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8234 tp->bufmgr_config.mbuf_mac_rx_low_water);
8235 tw32(BUFMGR_MB_HIGH_WATER,
8236 tp->bufmgr_config.mbuf_high_water);
8237 } else {
8238 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8239 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8240 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8241 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8242 tw32(BUFMGR_MB_HIGH_WATER,
8243 tp->bufmgr_config.mbuf_high_water_jumbo);
8245 tw32(BUFMGR_DMA_LOW_WATER,
8246 tp->bufmgr_config.dma_low_water);
8247 tw32(BUFMGR_DMA_HIGH_WATER,
8248 tp->bufmgr_config.dma_high_water);
8250 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8251 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8252 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
8253 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8254 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8255 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8256 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
8257 tw32(BUFMGR_MODE, val);
8258 for (i = 0; i < 2000; i++) {
8259 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8260 break;
8261 udelay(10);
8263 if (i >= 2000) {
8264 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
8265 return -ENODEV;
8268 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8269 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
8271 tg3_setup_rxbd_thresholds(tp);
8273 /* Initialize TG3_BDINFO's at:
8274 * RCVDBDI_STD_BD: standard eth size rx ring
8275 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8276 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8278 * like so:
8279 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8280 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8281 * ring attribute flags
8282 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8284 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8285 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8287 * The size of each ring is fixed in the firmware, but the location is
8288 * configurable.
8290 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
8291 ((u64) tpr->rx_std_mapping >> 32));
8292 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
8293 ((u64) tpr->rx_std_mapping & 0xffffffff));
8294 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
8295 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8296 NIC_SRAM_RX_BUFFER_DESC);
8298 /* Disable the mini ring */
8299 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8300 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8301 BDINFO_FLAGS_DISABLED);
8303 /* Program the jumbo buffer descriptor ring control
8304 * blocks on those devices that have them.
8306 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8307 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
8308 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))) {
8310 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
8311 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
8312 ((u64) tpr->rx_jmb_mapping >> 32));
8313 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
8314 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
8315 val = TG3_RX_JMB_RING_SIZE(tp) <<
8316 BDINFO_FLAGS_MAXLEN_SHIFT;
8317 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8318 val | BDINFO_FLAGS_USE_EXT_RECV);
8319 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
8320 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8321 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8322 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
8323 } else {
8324 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8325 BDINFO_FLAGS_DISABLED);
8328 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
8329 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8330 val = TG3_RX_STD_MAX_SIZE_5700;
8331 else
8332 val = TG3_RX_STD_MAX_SIZE_5717;
8333 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8334 val |= (TG3_RX_STD_DMA_SZ << 2);
8335 } else
8336 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
8337 } else
8338 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
8340 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
8342 tpr->rx_std_prod_idx = tp->rx_pending;
8343 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
8345 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
8346 tp->rx_jumbo_pending : 0;
8347 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
8349 tg3_rings_reset(tp);
8351 /* Initialize MAC address and backoff seed. */
8352 __tg3_set_mac_addr(tp, 0);
8354 /* MTU + ethernet header + FCS + optional VLAN tag */
8355 tw32(MAC_RX_MTU_SIZE,
8356 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
8358 /* The slot time is changed by tg3_setup_phy if we
8359 * run at gigabit with half duplex.
8361 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8362 (6 << TX_LENGTHS_IPG_SHIFT) |
8363 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8365 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8366 val |= tr32(MAC_TX_LENGTHS) &
8367 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8368 TX_LENGTHS_CNT_DWN_VAL_MSK);
8370 tw32(MAC_TX_LENGTHS, val);
8372 /* Receive rules. */
8373 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8374 tw32(RCVLPC_CONFIG, 0x0181);
8376 /* Calculate RDMAC_MODE setting early, we need it to determine
8377 * the RCVLPC_STATE_ENABLE mask.
8379 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8380 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8381 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8382 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8383 RDMAC_MODE_LNGREAD_ENAB);
8385 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
8386 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8388 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8389 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8390 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8391 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8392 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8393 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8395 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8396 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
8397 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
8398 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
8399 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8400 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8401 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8402 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8406 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8407 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8409 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8410 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8412 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8413 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8414 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8415 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
8417 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8418 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8420 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8421 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8422 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8423 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
8424 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
8425 val = tr32(TG3_RDMA_RSRVCTRL_REG);
8426 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8427 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8428 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8429 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8430 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8431 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8432 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8433 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
8435 tw32(TG3_RDMA_RSRVCTRL_REG,
8436 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8439 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8440 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8441 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8442 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8443 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8444 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8447 /* Receive/send statistics. */
8448 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8449 val = tr32(RCVLPC_STATS_ENABLE);
8450 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8451 tw32(RCVLPC_STATS_ENABLE, val);
8452 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8453 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
8454 val = tr32(RCVLPC_STATS_ENABLE);
8455 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8456 tw32(RCVLPC_STATS_ENABLE, val);
8457 } else {
8458 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8460 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8461 tw32(SNDDATAI_STATSENAB, 0xffffff);
8462 tw32(SNDDATAI_STATSCTRL,
8463 (SNDDATAI_SCTRL_ENABLE |
8464 SNDDATAI_SCTRL_FASTUPD));
8466 /* Setup host coalescing engine. */
8467 tw32(HOSTCC_MODE, 0);
8468 for (i = 0; i < 2000; i++) {
8469 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8470 break;
8471 udelay(10);
8474 __tg3_set_coalesce(tp, &tp->coal);
8476 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8477 /* Status/statistics block address. See tg3_timer,
8478 * the tg3_periodic_fetch_stats call there, and
8479 * tg3_get_stats to see how this works for 5705/5750 chips.
8481 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8482 ((u64) tp->stats_mapping >> 32));
8483 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8484 ((u64) tp->stats_mapping & 0xffffffff));
8485 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
8487 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
8489 /* Clear statistics and status block memory areas */
8490 for (i = NIC_SRAM_STATS_BLK;
8491 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8492 i += sizeof(u32)) {
8493 tg3_write_mem(tp, i, 0);
8494 udelay(40);
8498 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8500 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8501 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8502 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8503 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8505 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8506 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
8507 /* reset to prevent losing 1st rx packet intermittently */
8508 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8509 udelay(10);
8512 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8513 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
8514 else
8515 tp->mac_mode = 0;
8516 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
8517 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
8518 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
8519 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8520 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8521 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8522 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8523 udelay(40);
8525 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
8526 * If TG3_FLG2_IS_NIC is zero, we should read the
8527 * register to preserve the GPIO settings for LOMs. The GPIOs,
8528 * whether used as inputs or outputs, are set by boot code after
8529 * reset.
8531 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
8532 u32 gpio_mask;
8534 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8535 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8536 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
8538 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8539 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8540 GRC_LCLCTRL_GPIO_OUTPUT3;
8542 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8543 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8545 tp->grc_local_ctrl &= ~gpio_mask;
8546 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8548 /* GPIO1 must be driven high for eeprom write protect */
8549 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8550 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8551 GRC_LCLCTRL_GPIO_OUTPUT1);
8553 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8554 udelay(100);
8556 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
8557 tp->irq_cnt > 1) {
8558 val = tr32(MSGINT_MODE);
8559 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8560 tw32(MSGINT_MODE, val);
8563 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8564 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8565 udelay(40);
8568 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8569 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8570 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8571 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8572 WDMAC_MODE_LNGREAD_ENAB);
8574 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8575 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
8576 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
8577 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8578 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8579 /* nothing */
8580 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8581 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8582 val |= WDMAC_MODE_RX_ACCEL;
8586 /* Enable host coalescing bug fix */
8587 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
8588 val |= WDMAC_MODE_STATUS_TAG_FIX;
8590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8591 val |= WDMAC_MODE_BURST_ALL_DATA;
8593 tw32_f(WDMAC_MODE, val);
8594 udelay(40);
8596 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8597 u16 pcix_cmd;
8599 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8600 &pcix_cmd);
8601 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
8602 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8603 pcix_cmd |= PCI_X_CMD_READ_2K;
8604 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
8605 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8606 pcix_cmd |= PCI_X_CMD_READ_2K;
8608 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8609 pcix_cmd);
8612 tw32_f(RDMAC_MODE, rdmac_mode);
8613 udelay(40);
8615 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8616 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8617 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
8619 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8620 tw32(SNDDATAC_MODE,
8621 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8622 else
8623 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8625 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8626 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8627 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
8628 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
8629 val |= RCVDBDI_MODE_LRG_RING_SZ;
8630 tw32(RCVDBDI_MODE, val);
8631 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
8632 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8633 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
8634 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
8635 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
8636 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8637 tw32(SNDBDI_MODE, val);
8638 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8640 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8641 err = tg3_load_5701_a0_firmware_fix(tp);
8642 if (err)
8643 return err;
8646 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8647 err = tg3_load_tso_firmware(tp);
8648 if (err)
8649 return err;
8652 tp->tx_mode = TX_MODE_ENABLE;
8654 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8655 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8656 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
8658 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8659 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8660 tp->tx_mode &= ~val;
8661 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8664 tw32_f(MAC_TX_MODE, tp->tx_mode);
8665 udelay(100);
8667 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8668 u32 reg = MAC_RSS_INDIR_TBL_0;
8669 u8 *ent = (u8 *)&val;
8671 /* Setup the indirection table */
8672 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8673 int idx = i % sizeof(val);
8675 ent[idx] = i % (tp->irq_cnt - 1);
8676 if (idx == sizeof(val) - 1) {
8677 tw32(reg, val);
8678 reg += 4;
8682 /* Setup the "secret" hash key. */
8683 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8684 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8685 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8686 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8687 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8688 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8689 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8690 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8691 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8692 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8695 tp->rx_mode = RX_MODE_ENABLE;
8696 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
8697 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8699 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8700 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8701 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8702 RX_MODE_RSS_IPV6_HASH_EN |
8703 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8704 RX_MODE_RSS_IPV4_HASH_EN |
8705 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8707 tw32_f(MAC_RX_MODE, tp->rx_mode);
8708 udelay(10);
8710 tw32(MAC_LED_CTRL, tp->led_ctrl);
8712 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
8713 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
8714 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8715 udelay(10);
8717 tw32_f(MAC_RX_MODE, tp->rx_mode);
8718 udelay(10);
8720 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
8721 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8722 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
8723 /* Set drive transmission level to 1.2V */
8724 /* only if the signal pre-emphasis bit is not set */
8725 val = tr32(MAC_SERDES_CFG);
8726 val &= 0xfffff000;
8727 val |= 0x880;
8728 tw32(MAC_SERDES_CFG, val);
8730 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8731 tw32(MAC_SERDES_CFG, 0x616000);
8734 /* Prevent chip from dropping frames when flow control
8735 * is enabled.
8737 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8738 val = 1;
8739 else
8740 val = 2;
8741 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
8743 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8744 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
8745 /* Use hardware link auto-negotiation */
8746 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8749 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8750 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8751 u32 tmp;
8753 tmp = tr32(SERDES_RX_CTRL);
8754 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8755 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8756 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8757 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8760 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8761 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8762 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
8763 tp->link_config.speed = tp->link_config.orig_speed;
8764 tp->link_config.duplex = tp->link_config.orig_duplex;
8765 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8768 err = tg3_setup_phy(tp, 0);
8769 if (err)
8770 return err;
8772 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8773 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8774 u32 tmp;
8776 /* Clear CRC stats. */
8777 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8778 tg3_writephy(tp, MII_TG3_TEST1,
8779 tmp | MII_TG3_TEST1_CRC_EN);
8780 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
8785 __tg3_set_rx_mode(tp->dev);
8787 /* Initialize receive rules. */
8788 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8789 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8790 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8791 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8793 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
8794 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
8795 limit = 8;
8796 else
8797 limit = 16;
8798 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8799 limit -= 4;
8800 switch (limit) {
8801 case 16:
8802 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8803 case 15:
8804 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8805 case 14:
8806 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8807 case 13:
8808 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8809 case 12:
8810 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8811 case 11:
8812 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8813 case 10:
8814 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8815 case 9:
8816 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8817 case 8:
8818 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8819 case 7:
8820 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8821 case 6:
8822 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8823 case 5:
8824 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8825 case 4:
8826 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8827 case 3:
8828 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8829 case 2:
8830 case 1:
8832 default:
8833 break;
8836 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8837 /* Write our heartbeat update interval to APE. */
8838 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8839 APE_HOST_HEARTBEAT_INT_DISABLE);
8841 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8843 return 0;
8846 /* Called at device open time to get the chip ready for
8847 * packet processing. Invoked with tp->lock held.
8849 static int tg3_init_hw(struct tg3 *tp, int reset_phy)
8851 tg3_switch_clocks(tp);
8853 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8855 return tg3_reset_hw(tp, reset_phy);
8858 #define TG3_STAT_ADD32(PSTAT, REG) \
8859 do { u32 __val = tr32(REG); \
8860 (PSTAT)->low += __val; \
8861 if ((PSTAT)->low < __val) \
8862 (PSTAT)->high += 1; \
8863 } while (0)
8865 static void tg3_periodic_fetch_stats(struct tg3 *tp)
8867 struct tg3_hw_stats *sp = tp->hw_stats;
8869 if (!netif_carrier_ok(tp->dev))
8870 return;
8872 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8873 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8874 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8875 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8876 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8877 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8878 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8879 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8880 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8881 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8882 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8883 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8884 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8886 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8887 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8888 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8889 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8890 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8891 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8892 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8893 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8894 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8895 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8896 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8897 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8898 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8899 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
8901 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8902 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
8903 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8904 } else {
8905 u32 val = tr32(HOSTCC_FLOW_ATTN);
8906 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8907 if (val) {
8908 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8909 sp->rx_discards.low += val;
8910 if (sp->rx_discards.low < val)
8911 sp->rx_discards.high += 1;
8913 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8915 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
8918 static void tg3_timer(unsigned long __opaque)
8920 struct tg3 *tp = (struct tg3 *) __opaque;
8922 if (tp->irq_sync)
8923 goto restart_timer;
8925 spin_lock(&tp->lock);
8927 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8928 /* All of this garbage is because when using non-tagged
8929 * IRQ status the mailbox/status_block protocol the chip
8930 * uses with the cpu is race prone.
8932 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
8933 tw32(GRC_LOCAL_CTRL,
8934 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8935 } else {
8936 tw32(HOSTCC_MODE, tp->coalesce_mode |
8937 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
8940 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8941 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
8942 spin_unlock(&tp->lock);
8943 schedule_work(&tp->reset_task);
8944 return;
8948 /* This part only runs once per second. */
8949 if (!--tp->timer_counter) {
8950 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8951 tg3_periodic_fetch_stats(tp);
8953 if (tp->setlpicnt && !--tp->setlpicnt) {
8954 u32 val = tr32(TG3_CPMU_EEE_MODE);
8955 tw32(TG3_CPMU_EEE_MODE,
8956 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8959 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8960 u32 mac_stat;
8961 int phy_event;
8963 mac_stat = tr32(MAC_STATUS);
8965 phy_event = 0;
8966 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
8967 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8968 phy_event = 1;
8969 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8970 phy_event = 1;
8972 if (phy_event)
8973 tg3_setup_phy(tp, 0);
8974 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8975 u32 mac_stat = tr32(MAC_STATUS);
8976 int need_setup = 0;
8978 if (netif_carrier_ok(tp->dev) &&
8979 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8980 need_setup = 1;
8982 if (!netif_carrier_ok(tp->dev) &&
8983 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8984 MAC_STATUS_SIGNAL_DET))) {
8985 need_setup = 1;
8987 if (need_setup) {
8988 if (!tp->serdes_counter) {
8989 tw32_f(MAC_MODE,
8990 (tp->mac_mode &
8991 ~MAC_MODE_PORT_MODE_MASK));
8992 udelay(40);
8993 tw32_f(MAC_MODE, tp->mac_mode);
8994 udelay(40);
8996 tg3_setup_phy(tp, 0);
8998 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8999 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
9000 tg3_serdes_parallel_detect(tp);
9003 tp->timer_counter = tp->timer_multiplier;
9006 /* Heartbeat is only sent once every 2 seconds.
9008 * The heartbeat is to tell the ASF firmware that the host
9009 * driver is still alive. In the event that the OS crashes,
9010 * ASF needs to reset the hardware to free up the FIFO space
9011 * that may be filled with rx packets destined for the host.
9012 * If the FIFO is full, ASF will no longer function properly.
9014 * Unintended resets have been reported on real time kernels
9015 * where the timer doesn't run on time. Netpoll will also have
9016 * same problem.
9018 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9019 * to check the ring condition when the heartbeat is expiring
9020 * before doing the reset. This will prevent most unintended
9021 * resets.
9023 if (!--tp->asf_counter) {
9024 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
9025 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
9026 tg3_wait_for_event_ack(tp);
9028 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
9029 FWCMD_NICDRV_ALIVE3);
9030 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
9031 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9032 TG3_FW_UPDATE_TIMEOUT_SEC);
9034 tg3_generate_fw_event(tp);
9036 tp->asf_counter = tp->asf_multiplier;
9039 spin_unlock(&tp->lock);
9041 restart_timer:
9042 tp->timer.expires = jiffies + tp->timer_offset;
9043 add_timer(&tp->timer);
9046 static int tg3_request_irq(struct tg3 *tp, int irq_num)
9048 irq_handler_t fn;
9049 unsigned long flags;
9050 char *name;
9051 struct tg3_napi *tnapi = &tp->napi[irq_num];
9053 if (tp->irq_cnt == 1)
9054 name = tp->dev->name;
9055 else {
9056 name = &tnapi->irq_lbl[0];
9057 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9058 name[IFNAMSIZ-1] = 0;
9061 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9062 fn = tg3_msi;
9063 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
9064 fn = tg3_msi_1shot;
9065 flags = 0;
9066 } else {
9067 fn = tg3_interrupt;
9068 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9069 fn = tg3_interrupt_tagged;
9070 flags = IRQF_SHARED;
9073 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
9076 static int tg3_test_interrupt(struct tg3 *tp)
9078 struct tg3_napi *tnapi = &tp->napi[0];
9079 struct net_device *dev = tp->dev;
9080 int err, i, intr_ok = 0;
9081 u32 val;
9083 if (!netif_running(dev))
9084 return -ENODEV;
9086 tg3_disable_ints(tp);
9088 free_irq(tnapi->irq_vec, tnapi);
9091 * Turn off MSI one shot mode. Otherwise this test has no
9092 * observable way to know whether the interrupt was delivered.
9094 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
9095 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9096 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9097 tw32(MSGINT_MODE, val);
9100 err = request_irq(tnapi->irq_vec, tg3_test_isr,
9101 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
9102 if (err)
9103 return err;
9105 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
9106 tg3_enable_ints(tp);
9108 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
9109 tnapi->coal_now);
9111 for (i = 0; i < 5; i++) {
9112 u32 int_mbox, misc_host_ctrl;
9114 int_mbox = tr32_mailbox(tnapi->int_mbox);
9115 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9117 if ((int_mbox != 0) ||
9118 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9119 intr_ok = 1;
9120 break;
9123 msleep(10);
9126 tg3_disable_ints(tp);
9128 free_irq(tnapi->irq_vec, tnapi);
9130 err = tg3_request_irq(tp, 0);
9132 if (err)
9133 return err;
9135 if (intr_ok) {
9136 /* Reenable MSI one shot mode. */
9137 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
9138 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9139 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9140 tw32(MSGINT_MODE, val);
9142 return 0;
9145 return -EIO;
9148 /* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9149 * successfully restored
9151 static int tg3_test_msi(struct tg3 *tp)
9153 int err;
9154 u16 pci_cmd;
9156 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
9157 return 0;
9159 /* Turn off SERR reporting in case MSI terminates with Master
9160 * Abort.
9162 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9163 pci_write_config_word(tp->pdev, PCI_COMMAND,
9164 pci_cmd & ~PCI_COMMAND_SERR);
9166 err = tg3_test_interrupt(tp);
9168 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9170 if (!err)
9171 return 0;
9173 /* other failures */
9174 if (err != -EIO)
9175 return err;
9177 /* MSI test failed, go back to INTx mode */
9178 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9179 "to INTx mode. Please report this failure to the PCI "
9180 "maintainer and include system chipset information\n");
9182 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
9184 pci_disable_msi(tp->pdev);
9186 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
9187 tp->napi[0].irq_vec = tp->pdev->irq;
9189 err = tg3_request_irq(tp, 0);
9190 if (err)
9191 return err;
9193 /* Need to reset the chip because the MSI cycle may have terminated
9194 * with Master Abort.
9196 tg3_full_lock(tp, 1);
9198 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9199 err = tg3_init_hw(tp, 1);
9201 tg3_full_unlock(tp);
9203 if (err)
9204 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
9206 return err;
9209 static int tg3_request_firmware(struct tg3 *tp)
9211 const __be32 *fw_data;
9213 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
9214 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9215 tp->fw_needed);
9216 return -ENOENT;
9219 fw_data = (void *)tp->fw->data;
9221 /* Firmware blob starts with version numbers, followed by
9222 * start address and _full_ length including BSS sections
9223 * (which must be longer than the actual data, of course
9226 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9227 if (tp->fw_len < (tp->fw->size - 12)) {
9228 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9229 tp->fw_len, tp->fw_needed);
9230 release_firmware(tp->fw);
9231 tp->fw = NULL;
9232 return -EINVAL;
9235 /* We no longer need firmware; we have it. */
9236 tp->fw_needed = NULL;
9237 return 0;
9240 static bool tg3_enable_msix(struct tg3 *tp)
9242 int i, rc, cpus = num_online_cpus();
9243 struct msix_entry msix_ent[tp->irq_max];
9245 if (cpus == 1)
9246 /* Just fallback to the simpler MSI mode. */
9247 return false;
9250 * We want as many rx rings enabled as there are cpus.
9251 * The first MSIX vector only deals with link interrupts, etc,
9252 * so we add one to the number of vectors we are requesting.
9254 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9256 for (i = 0; i < tp->irq_max; i++) {
9257 msix_ent[i].entry = i;
9258 msix_ent[i].vector = 0;
9261 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
9262 if (rc < 0) {
9263 return false;
9264 } else if (rc != 0) {
9265 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9266 return false;
9267 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9268 tp->irq_cnt, rc);
9269 tp->irq_cnt = rc;
9272 for (i = 0; i < tp->irq_max; i++)
9273 tp->napi[i].irq_vec = msix_ent[i].vector;
9275 netif_set_real_num_tx_queues(tp->dev, 1);
9276 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9277 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9278 pci_disable_msix(tp->pdev);
9279 return false;
9282 if (tp->irq_cnt > 1) {
9283 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
9285 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9286 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9287 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
9288 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9292 return true;
9295 static void tg3_ints_init(struct tg3 *tp)
9297 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
9298 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
9299 /* All MSI supporting chips should support tagged
9300 * status. Assert that this is the case.
9302 netdev_warn(tp->dev,
9303 "MSI without TAGGED_STATUS? Not using MSI\n");
9304 goto defcfg;
9307 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
9308 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
9309 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
9310 pci_enable_msi(tp->pdev) == 0)
9311 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
9313 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9314 u32 msi_mode = tr32(MSGINT_MODE);
9315 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
9316 tp->irq_cnt > 1)
9317 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
9318 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9320 defcfg:
9321 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
9322 tp->irq_cnt = 1;
9323 tp->napi[0].irq_vec = tp->pdev->irq;
9324 netif_set_real_num_tx_queues(tp->dev, 1);
9325 netif_set_real_num_rx_queues(tp->dev, 1);
9329 static void tg3_ints_fini(struct tg3 *tp)
9331 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9332 pci_disable_msix(tp->pdev);
9333 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
9334 pci_disable_msi(tp->pdev);
9335 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
9336 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
9339 static int tg3_open(struct net_device *dev)
9341 struct tg3 *tp = netdev_priv(dev);
9342 int i, err;
9344 if (tp->fw_needed) {
9345 err = tg3_request_firmware(tp);
9346 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9347 if (err)
9348 return err;
9349 } else if (err) {
9350 netdev_warn(tp->dev, "TSO capability disabled\n");
9351 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
9352 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9353 netdev_notice(tp->dev, "TSO capability restored\n");
9354 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
9358 netif_carrier_off(tp->dev);
9360 err = tg3_power_up(tp);
9361 if (err)
9362 return err;
9364 tg3_full_lock(tp, 0);
9366 tg3_disable_ints(tp);
9367 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9369 tg3_full_unlock(tp);
9372 * Setup interrupts first so we know how
9373 * many NAPI resources to allocate
9375 tg3_ints_init(tp);
9377 /* The placement of this call is tied
9378 * to the setup and use of Host TX descriptors.
9380 err = tg3_alloc_consistent(tp);
9381 if (err)
9382 goto err_out1;
9384 tg3_napi_init(tp);
9386 tg3_napi_enable(tp);
9388 for (i = 0; i < tp->irq_cnt; i++) {
9389 struct tg3_napi *tnapi = &tp->napi[i];
9390 err = tg3_request_irq(tp, i);
9391 if (err) {
9392 for (i--; i >= 0; i--)
9393 free_irq(tnapi->irq_vec, tnapi);
9394 break;
9398 if (err)
9399 goto err_out2;
9401 tg3_full_lock(tp, 0);
9403 err = tg3_init_hw(tp, 1);
9404 if (err) {
9405 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9406 tg3_free_rings(tp);
9407 } else {
9408 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9409 tp->timer_offset = HZ;
9410 else
9411 tp->timer_offset = HZ / 10;
9413 BUG_ON(tp->timer_offset > HZ);
9414 tp->timer_counter = tp->timer_multiplier =
9415 (HZ / tp->timer_offset);
9416 tp->asf_counter = tp->asf_multiplier =
9417 ((HZ / tp->timer_offset) * 2);
9419 init_timer(&tp->timer);
9420 tp->timer.expires = jiffies + tp->timer_offset;
9421 tp->timer.data = (unsigned long) tp;
9422 tp->timer.function = tg3_timer;
9425 tg3_full_unlock(tp);
9427 if (err)
9428 goto err_out3;
9430 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
9431 err = tg3_test_msi(tp);
9433 if (err) {
9434 tg3_full_lock(tp, 0);
9435 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9436 tg3_free_rings(tp);
9437 tg3_full_unlock(tp);
9439 goto err_out2;
9442 if (!(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
9443 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9444 u32 val = tr32(PCIE_TRANSACTION_CFG);
9446 tw32(PCIE_TRANSACTION_CFG,
9447 val | PCIE_TRANS_CFG_1SHOT_MSI);
9451 tg3_phy_start(tp);
9453 tg3_full_lock(tp, 0);
9455 add_timer(&tp->timer);
9456 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
9457 tg3_enable_ints(tp);
9459 tg3_full_unlock(tp);
9461 netif_tx_start_all_queues(dev);
9463 return 0;
9465 err_out3:
9466 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9467 struct tg3_napi *tnapi = &tp->napi[i];
9468 free_irq(tnapi->irq_vec, tnapi);
9471 err_out2:
9472 tg3_napi_disable(tp);
9473 tg3_napi_fini(tp);
9474 tg3_free_consistent(tp);
9476 err_out1:
9477 tg3_ints_fini(tp);
9478 return err;
9481 static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9482 struct rtnl_link_stats64 *);
9483 static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9485 static int tg3_close(struct net_device *dev)
9487 int i;
9488 struct tg3 *tp = netdev_priv(dev);
9490 tg3_napi_disable(tp);
9491 cancel_work_sync(&tp->reset_task);
9493 netif_tx_stop_all_queues(dev);
9495 del_timer_sync(&tp->timer);
9497 tg3_phy_stop(tp);
9499 tg3_full_lock(tp, 1);
9501 tg3_disable_ints(tp);
9503 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9504 tg3_free_rings(tp);
9505 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9507 tg3_full_unlock(tp);
9509 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9510 struct tg3_napi *tnapi = &tp->napi[i];
9511 free_irq(tnapi->irq_vec, tnapi);
9514 tg3_ints_fini(tp);
9516 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9518 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9519 sizeof(tp->estats_prev));
9521 tg3_napi_fini(tp);
9523 tg3_free_consistent(tp);
9525 tg3_power_down(tp);
9527 netif_carrier_off(tp->dev);
9529 return 0;
9532 static inline u64 get_stat64(tg3_stat64_t *val)
9534 return ((u64)val->high << 32) | ((u64)val->low);
9537 static u64 calc_crc_errors(struct tg3 *tp)
9539 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9541 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9542 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9543 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
9544 u32 val;
9546 spin_lock_bh(&tp->lock);
9547 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9548 tg3_writephy(tp, MII_TG3_TEST1,
9549 val | MII_TG3_TEST1_CRC_EN);
9550 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
9551 } else
9552 val = 0;
9553 spin_unlock_bh(&tp->lock);
9555 tp->phy_crc_errors += val;
9557 return tp->phy_crc_errors;
9560 return get_stat64(&hw_stats->rx_fcs_errors);
9563 #define ESTAT_ADD(member) \
9564 estats->member = old_estats->member + \
9565 get_stat64(&hw_stats->member)
9567 static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9569 struct tg3_ethtool_stats *estats = &tp->estats;
9570 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9571 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9573 if (!hw_stats)
9574 return old_estats;
9576 ESTAT_ADD(rx_octets);
9577 ESTAT_ADD(rx_fragments);
9578 ESTAT_ADD(rx_ucast_packets);
9579 ESTAT_ADD(rx_mcast_packets);
9580 ESTAT_ADD(rx_bcast_packets);
9581 ESTAT_ADD(rx_fcs_errors);
9582 ESTAT_ADD(rx_align_errors);
9583 ESTAT_ADD(rx_xon_pause_rcvd);
9584 ESTAT_ADD(rx_xoff_pause_rcvd);
9585 ESTAT_ADD(rx_mac_ctrl_rcvd);
9586 ESTAT_ADD(rx_xoff_entered);
9587 ESTAT_ADD(rx_frame_too_long_errors);
9588 ESTAT_ADD(rx_jabbers);
9589 ESTAT_ADD(rx_undersize_packets);
9590 ESTAT_ADD(rx_in_length_errors);
9591 ESTAT_ADD(rx_out_length_errors);
9592 ESTAT_ADD(rx_64_or_less_octet_packets);
9593 ESTAT_ADD(rx_65_to_127_octet_packets);
9594 ESTAT_ADD(rx_128_to_255_octet_packets);
9595 ESTAT_ADD(rx_256_to_511_octet_packets);
9596 ESTAT_ADD(rx_512_to_1023_octet_packets);
9597 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9598 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9599 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9600 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9601 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9603 ESTAT_ADD(tx_octets);
9604 ESTAT_ADD(tx_collisions);
9605 ESTAT_ADD(tx_xon_sent);
9606 ESTAT_ADD(tx_xoff_sent);
9607 ESTAT_ADD(tx_flow_control);
9608 ESTAT_ADD(tx_mac_errors);
9609 ESTAT_ADD(tx_single_collisions);
9610 ESTAT_ADD(tx_mult_collisions);
9611 ESTAT_ADD(tx_deferred);
9612 ESTAT_ADD(tx_excessive_collisions);
9613 ESTAT_ADD(tx_late_collisions);
9614 ESTAT_ADD(tx_collide_2times);
9615 ESTAT_ADD(tx_collide_3times);
9616 ESTAT_ADD(tx_collide_4times);
9617 ESTAT_ADD(tx_collide_5times);
9618 ESTAT_ADD(tx_collide_6times);
9619 ESTAT_ADD(tx_collide_7times);
9620 ESTAT_ADD(tx_collide_8times);
9621 ESTAT_ADD(tx_collide_9times);
9622 ESTAT_ADD(tx_collide_10times);
9623 ESTAT_ADD(tx_collide_11times);
9624 ESTAT_ADD(tx_collide_12times);
9625 ESTAT_ADD(tx_collide_13times);
9626 ESTAT_ADD(tx_collide_14times);
9627 ESTAT_ADD(tx_collide_15times);
9628 ESTAT_ADD(tx_ucast_packets);
9629 ESTAT_ADD(tx_mcast_packets);
9630 ESTAT_ADD(tx_bcast_packets);
9631 ESTAT_ADD(tx_carrier_sense_errors);
9632 ESTAT_ADD(tx_discards);
9633 ESTAT_ADD(tx_errors);
9635 ESTAT_ADD(dma_writeq_full);
9636 ESTAT_ADD(dma_write_prioq_full);
9637 ESTAT_ADD(rxbds_empty);
9638 ESTAT_ADD(rx_discards);
9639 ESTAT_ADD(rx_errors);
9640 ESTAT_ADD(rx_threshold_hit);
9642 ESTAT_ADD(dma_readq_full);
9643 ESTAT_ADD(dma_read_prioq_full);
9644 ESTAT_ADD(tx_comp_queue_full);
9646 ESTAT_ADD(ring_set_send_prod_index);
9647 ESTAT_ADD(ring_status_update);
9648 ESTAT_ADD(nic_irqs);
9649 ESTAT_ADD(nic_avoided_irqs);
9650 ESTAT_ADD(nic_tx_threshold_hit);
9652 return estats;
9655 static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9656 struct rtnl_link_stats64 *stats)
9658 struct tg3 *tp = netdev_priv(dev);
9659 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
9660 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9662 if (!hw_stats)
9663 return old_stats;
9665 stats->rx_packets = old_stats->rx_packets +
9666 get_stat64(&hw_stats->rx_ucast_packets) +
9667 get_stat64(&hw_stats->rx_mcast_packets) +
9668 get_stat64(&hw_stats->rx_bcast_packets);
9670 stats->tx_packets = old_stats->tx_packets +
9671 get_stat64(&hw_stats->tx_ucast_packets) +
9672 get_stat64(&hw_stats->tx_mcast_packets) +
9673 get_stat64(&hw_stats->tx_bcast_packets);
9675 stats->rx_bytes = old_stats->rx_bytes +
9676 get_stat64(&hw_stats->rx_octets);
9677 stats->tx_bytes = old_stats->tx_bytes +
9678 get_stat64(&hw_stats->tx_octets);
9680 stats->rx_errors = old_stats->rx_errors +
9681 get_stat64(&hw_stats->rx_errors);
9682 stats->tx_errors = old_stats->tx_errors +
9683 get_stat64(&hw_stats->tx_errors) +
9684 get_stat64(&hw_stats->tx_mac_errors) +
9685 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9686 get_stat64(&hw_stats->tx_discards);
9688 stats->multicast = old_stats->multicast +
9689 get_stat64(&hw_stats->rx_mcast_packets);
9690 stats->collisions = old_stats->collisions +
9691 get_stat64(&hw_stats->tx_collisions);
9693 stats->rx_length_errors = old_stats->rx_length_errors +
9694 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9695 get_stat64(&hw_stats->rx_undersize_packets);
9697 stats->rx_over_errors = old_stats->rx_over_errors +
9698 get_stat64(&hw_stats->rxbds_empty);
9699 stats->rx_frame_errors = old_stats->rx_frame_errors +
9700 get_stat64(&hw_stats->rx_align_errors);
9701 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9702 get_stat64(&hw_stats->tx_discards);
9703 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9704 get_stat64(&hw_stats->tx_carrier_sense_errors);
9706 stats->rx_crc_errors = old_stats->rx_crc_errors +
9707 calc_crc_errors(tp);
9709 stats->rx_missed_errors = old_stats->rx_missed_errors +
9710 get_stat64(&hw_stats->rx_discards);
9712 stats->rx_dropped = tp->rx_dropped;
9714 return stats;
9717 static inline u32 calc_crc(unsigned char *buf, int len)
9719 u32 reg;
9720 u32 tmp;
9721 int j, k;
9723 reg = 0xffffffff;
9725 for (j = 0; j < len; j++) {
9726 reg ^= buf[j];
9728 for (k = 0; k < 8; k++) {
9729 tmp = reg & 0x01;
9731 reg >>= 1;
9733 if (tmp)
9734 reg ^= 0xedb88320;
9738 return ~reg;
9741 static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9743 /* accept or reject all multicast frames */
9744 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9745 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9746 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9747 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9750 static void __tg3_set_rx_mode(struct net_device *dev)
9752 struct tg3 *tp = netdev_priv(dev);
9753 u32 rx_mode;
9755 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9756 RX_MODE_KEEP_VLAN_TAG);
9758 #if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9759 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9760 * flag clear.
9762 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9763 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9764 #endif
9766 if (dev->flags & IFF_PROMISC) {
9767 /* Promiscuous mode. */
9768 rx_mode |= RX_MODE_PROMISC;
9769 } else if (dev->flags & IFF_ALLMULTI) {
9770 /* Accept all multicast. */
9771 tg3_set_multi(tp, 1);
9772 } else if (netdev_mc_empty(dev)) {
9773 /* Reject all multicast. */
9774 tg3_set_multi(tp, 0);
9775 } else {
9776 /* Accept one or more multicast(s). */
9777 struct netdev_hw_addr *ha;
9778 u32 mc_filter[4] = { 0, };
9779 u32 regidx;
9780 u32 bit;
9781 u32 crc;
9783 netdev_for_each_mc_addr(ha, dev) {
9784 crc = calc_crc(ha->addr, ETH_ALEN);
9785 bit = ~crc & 0x7f;
9786 regidx = (bit & 0x60) >> 5;
9787 bit &= 0x1f;
9788 mc_filter[regidx] |= (1 << bit);
9791 tw32(MAC_HASH_REG_0, mc_filter[0]);
9792 tw32(MAC_HASH_REG_1, mc_filter[1]);
9793 tw32(MAC_HASH_REG_2, mc_filter[2]);
9794 tw32(MAC_HASH_REG_3, mc_filter[3]);
9797 if (rx_mode != tp->rx_mode) {
9798 tp->rx_mode = rx_mode;
9799 tw32_f(MAC_RX_MODE, rx_mode);
9800 udelay(10);
9804 static void tg3_set_rx_mode(struct net_device *dev)
9806 struct tg3 *tp = netdev_priv(dev);
9808 if (!netif_running(dev))
9809 return;
9811 tg3_full_lock(tp, 0);
9812 __tg3_set_rx_mode(dev);
9813 tg3_full_unlock(tp);
9816 static int tg3_get_regs_len(struct net_device *dev)
9818 return TG3_REG_BLK_SIZE;
9821 static void tg3_get_regs(struct net_device *dev,
9822 struct ethtool_regs *regs, void *_p)
9824 struct tg3 *tp = netdev_priv(dev);
9826 regs->version = 0;
9828 memset(_p, 0, TG3_REG_BLK_SIZE);
9830 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
9831 return;
9833 tg3_full_lock(tp, 0);
9835 tg3_dump_legacy_regs(tp, (u32 *)_p);
9837 tg3_full_unlock(tp);
9840 static int tg3_get_eeprom_len(struct net_device *dev)
9842 struct tg3 *tp = netdev_priv(dev);
9844 return tp->nvram_size;
9847 static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9849 struct tg3 *tp = netdev_priv(dev);
9850 int ret;
9851 u8 *pd;
9852 u32 i, offset, len, b_offset, b_count;
9853 __be32 val;
9855 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9856 return -EINVAL;
9858 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
9859 return -EAGAIN;
9861 offset = eeprom->offset;
9862 len = eeprom->len;
9863 eeprom->len = 0;
9865 eeprom->magic = TG3_EEPROM_MAGIC;
9867 if (offset & 3) {
9868 /* adjustments to start on required 4 byte boundary */
9869 b_offset = offset & 3;
9870 b_count = 4 - b_offset;
9871 if (b_count > len) {
9872 /* i.e. offset=1 len=2 */
9873 b_count = len;
9875 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
9876 if (ret)
9877 return ret;
9878 memcpy(data, ((char *)&val) + b_offset, b_count);
9879 len -= b_count;
9880 offset += b_count;
9881 eeprom->len += b_count;
9884 /* read bytes up to the last 4 byte boundary */
9885 pd = &data[eeprom->len];
9886 for (i = 0; i < (len - (len & 3)); i += 4) {
9887 ret = tg3_nvram_read_be32(tp, offset + i, &val);
9888 if (ret) {
9889 eeprom->len += i;
9890 return ret;
9892 memcpy(pd + i, &val, 4);
9894 eeprom->len += i;
9896 if (len & 3) {
9897 /* read last bytes not ending on 4 byte boundary */
9898 pd = &data[eeprom->len];
9899 b_count = len & 3;
9900 b_offset = offset + len - b_count;
9901 ret = tg3_nvram_read_be32(tp, b_offset, &val);
9902 if (ret)
9903 return ret;
9904 memcpy(pd, &val, b_count);
9905 eeprom->len += b_count;
9907 return 0;
9910 static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
9912 static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9914 struct tg3 *tp = netdev_priv(dev);
9915 int ret;
9916 u32 offset, len, b_offset, odd_len;
9917 u8 *buf;
9918 __be32 start, end;
9920 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
9921 return -EAGAIN;
9923 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9924 eeprom->magic != TG3_EEPROM_MAGIC)
9925 return -EINVAL;
9927 offset = eeprom->offset;
9928 len = eeprom->len;
9930 if ((b_offset = (offset & 3))) {
9931 /* adjustments to start on required 4 byte boundary */
9932 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
9933 if (ret)
9934 return ret;
9935 len += b_offset;
9936 offset &= ~3;
9937 if (len < 4)
9938 len = 4;
9941 odd_len = 0;
9942 if (len & 3) {
9943 /* adjustments to end on required 4 byte boundary */
9944 odd_len = 1;
9945 len = (len + 3) & ~3;
9946 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
9947 if (ret)
9948 return ret;
9951 buf = data;
9952 if (b_offset || odd_len) {
9953 buf = kmalloc(len, GFP_KERNEL);
9954 if (!buf)
9955 return -ENOMEM;
9956 if (b_offset)
9957 memcpy(buf, &start, 4);
9958 if (odd_len)
9959 memcpy(buf+len-4, &end, 4);
9960 memcpy(buf + b_offset, data, eeprom->len);
9963 ret = tg3_nvram_write_block(tp, offset, len, buf);
9965 if (buf != data)
9966 kfree(buf);
9968 return ret;
9971 static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9973 struct tg3 *tp = netdev_priv(dev);
9975 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9976 struct phy_device *phydev;
9977 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
9978 return -EAGAIN;
9979 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9980 return phy_ethtool_gset(phydev, cmd);
9983 cmd->supported = (SUPPORTED_Autoneg);
9985 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
9986 cmd->supported |= (SUPPORTED_1000baseT_Half |
9987 SUPPORTED_1000baseT_Full);
9989 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
9990 cmd->supported |= (SUPPORTED_100baseT_Half |
9991 SUPPORTED_100baseT_Full |
9992 SUPPORTED_10baseT_Half |
9993 SUPPORTED_10baseT_Full |
9994 SUPPORTED_TP);
9995 cmd->port = PORT_TP;
9996 } else {
9997 cmd->supported |= SUPPORTED_FIBRE;
9998 cmd->port = PORT_FIBRE;
10001 cmd->advertising = tp->link_config.advertising;
10002 if (netif_running(dev)) {
10003 cmd->speed = tp->link_config.active_speed;
10004 cmd->duplex = tp->link_config.active_duplex;
10005 } else {
10006 cmd->speed = SPEED_INVALID;
10007 cmd->duplex = DUPLEX_INVALID;
10009 cmd->phy_address = tp->phy_addr;
10010 cmd->transceiver = XCVR_INTERNAL;
10011 cmd->autoneg = tp->link_config.autoneg;
10012 cmd->maxtxpkt = 0;
10013 cmd->maxrxpkt = 0;
10014 return 0;
10017 static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10019 struct tg3 *tp = netdev_priv(dev);
10021 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
10022 struct phy_device *phydev;
10023 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
10024 return -EAGAIN;
10025 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10026 return phy_ethtool_sset(phydev, cmd);
10029 if (cmd->autoneg != AUTONEG_ENABLE &&
10030 cmd->autoneg != AUTONEG_DISABLE)
10031 return -EINVAL;
10033 if (cmd->autoneg == AUTONEG_DISABLE &&
10034 cmd->duplex != DUPLEX_FULL &&
10035 cmd->duplex != DUPLEX_HALF)
10036 return -EINVAL;
10038 if (cmd->autoneg == AUTONEG_ENABLE) {
10039 u32 mask = ADVERTISED_Autoneg |
10040 ADVERTISED_Pause |
10041 ADVERTISED_Asym_Pause;
10043 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
10044 mask |= ADVERTISED_1000baseT_Half |
10045 ADVERTISED_1000baseT_Full;
10047 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
10048 mask |= ADVERTISED_100baseT_Half |
10049 ADVERTISED_100baseT_Full |
10050 ADVERTISED_10baseT_Half |
10051 ADVERTISED_10baseT_Full |
10052 ADVERTISED_TP;
10053 else
10054 mask |= ADVERTISED_FIBRE;
10056 if (cmd->advertising & ~mask)
10057 return -EINVAL;
10059 mask &= (ADVERTISED_1000baseT_Half |
10060 ADVERTISED_1000baseT_Full |
10061 ADVERTISED_100baseT_Half |
10062 ADVERTISED_100baseT_Full |
10063 ADVERTISED_10baseT_Half |
10064 ADVERTISED_10baseT_Full);
10066 cmd->advertising &= mask;
10067 } else {
10068 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
10069 if (cmd->speed != SPEED_1000)
10070 return -EINVAL;
10072 if (cmd->duplex != DUPLEX_FULL)
10073 return -EINVAL;
10074 } else {
10075 if (cmd->speed != SPEED_100 &&
10076 cmd->speed != SPEED_10)
10077 return -EINVAL;
10081 tg3_full_lock(tp, 0);
10083 tp->link_config.autoneg = cmd->autoneg;
10084 if (cmd->autoneg == AUTONEG_ENABLE) {
10085 tp->link_config.advertising = (cmd->advertising |
10086 ADVERTISED_Autoneg);
10087 tp->link_config.speed = SPEED_INVALID;
10088 tp->link_config.duplex = DUPLEX_INVALID;
10089 } else {
10090 tp->link_config.advertising = 0;
10091 tp->link_config.speed = cmd->speed;
10092 tp->link_config.duplex = cmd->duplex;
10095 tp->link_config.orig_speed = tp->link_config.speed;
10096 tp->link_config.orig_duplex = tp->link_config.duplex;
10097 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10099 if (netif_running(dev))
10100 tg3_setup_phy(tp, 1);
10102 tg3_full_unlock(tp);
10104 return 0;
10107 static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10109 struct tg3 *tp = netdev_priv(dev);
10111 strcpy(info->driver, DRV_MODULE_NAME);
10112 strcpy(info->version, DRV_MODULE_VERSION);
10113 strcpy(info->fw_version, tp->fw_ver);
10114 strcpy(info->bus_info, pci_name(tp->pdev));
10117 static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10119 struct tg3 *tp = netdev_priv(dev);
10121 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
10122 device_can_wakeup(&tp->pdev->dev))
10123 wol->supported = WAKE_MAGIC;
10124 else
10125 wol->supported = 0;
10126 wol->wolopts = 0;
10127 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
10128 device_can_wakeup(&tp->pdev->dev))
10129 wol->wolopts = WAKE_MAGIC;
10130 memset(&wol->sopass, 0, sizeof(wol->sopass));
10133 static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10135 struct tg3 *tp = netdev_priv(dev);
10136 struct device *dp = &tp->pdev->dev;
10138 if (wol->wolopts & ~WAKE_MAGIC)
10139 return -EINVAL;
10140 if ((wol->wolopts & WAKE_MAGIC) &&
10141 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
10142 return -EINVAL;
10144 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10146 spin_lock_bh(&tp->lock);
10147 if (device_may_wakeup(dp))
10148 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
10149 else
10150 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
10151 spin_unlock_bh(&tp->lock);
10154 return 0;
10157 static u32 tg3_get_msglevel(struct net_device *dev)
10159 struct tg3 *tp = netdev_priv(dev);
10160 return tp->msg_enable;
10163 static void tg3_set_msglevel(struct net_device *dev, u32 value)
10165 struct tg3 *tp = netdev_priv(dev);
10166 tp->msg_enable = value;
10169 static int tg3_nway_reset(struct net_device *dev)
10171 struct tg3 *tp = netdev_priv(dev);
10172 int r;
10174 if (!netif_running(dev))
10175 return -EAGAIN;
10177 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
10178 return -EINVAL;
10180 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
10181 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
10182 return -EAGAIN;
10183 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
10184 } else {
10185 u32 bmcr;
10187 spin_lock_bh(&tp->lock);
10188 r = -EINVAL;
10189 tg3_readphy(tp, MII_BMCR, &bmcr);
10190 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10191 ((bmcr & BMCR_ANENABLE) ||
10192 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
10193 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10194 BMCR_ANENABLE);
10195 r = 0;
10197 spin_unlock_bh(&tp->lock);
10200 return r;
10203 static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10205 struct tg3 *tp = netdev_priv(dev);
10207 ering->rx_max_pending = tp->rx_std_ring_mask;
10208 ering->rx_mini_max_pending = 0;
10209 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10210 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
10211 else
10212 ering->rx_jumbo_max_pending = 0;
10214 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
10216 ering->rx_pending = tp->rx_pending;
10217 ering->rx_mini_pending = 0;
10218 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10219 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10220 else
10221 ering->rx_jumbo_pending = 0;
10223 ering->tx_pending = tp->napi[0].tx_pending;
10226 static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10228 struct tg3 *tp = netdev_priv(dev);
10229 int i, irq_sync = 0, err = 0;
10231 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10232 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
10233 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10234 (ering->tx_pending <= MAX_SKB_FRAGS) ||
10235 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
10236 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
10237 return -EINVAL;
10239 if (netif_running(dev)) {
10240 tg3_phy_stop(tp);
10241 tg3_netif_stop(tp);
10242 irq_sync = 1;
10245 tg3_full_lock(tp, irq_sync);
10247 tp->rx_pending = ering->rx_pending;
10249 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
10250 tp->rx_pending > 63)
10251 tp->rx_pending = 63;
10252 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
10254 for (i = 0; i < tp->irq_max; i++)
10255 tp->napi[i].tx_pending = ering->tx_pending;
10257 if (netif_running(dev)) {
10258 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10259 err = tg3_restart_hw(tp, 1);
10260 if (!err)
10261 tg3_netif_start(tp);
10264 tg3_full_unlock(tp);
10266 if (irq_sync && !err)
10267 tg3_phy_start(tp);
10269 return err;
10272 static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10274 struct tg3 *tp = netdev_priv(dev);
10276 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
10278 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
10279 epause->rx_pause = 1;
10280 else
10281 epause->rx_pause = 0;
10283 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
10284 epause->tx_pause = 1;
10285 else
10286 epause->tx_pause = 0;
10289 static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10291 struct tg3 *tp = netdev_priv(dev);
10292 int err = 0;
10294 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
10295 u32 newadv;
10296 struct phy_device *phydev;
10298 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10300 if (!(phydev->supported & SUPPORTED_Pause) ||
10301 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
10302 (epause->rx_pause != epause->tx_pause)))
10303 return -EINVAL;
10305 tp->link_config.flowctrl = 0;
10306 if (epause->rx_pause) {
10307 tp->link_config.flowctrl |= FLOW_CTRL_RX;
10309 if (epause->tx_pause) {
10310 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10311 newadv = ADVERTISED_Pause;
10312 } else
10313 newadv = ADVERTISED_Pause |
10314 ADVERTISED_Asym_Pause;
10315 } else if (epause->tx_pause) {
10316 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10317 newadv = ADVERTISED_Asym_Pause;
10318 } else
10319 newadv = 0;
10321 if (epause->autoneg)
10322 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10323 else
10324 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10326 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
10327 u32 oldadv = phydev->advertising &
10328 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10329 if (oldadv != newadv) {
10330 phydev->advertising &=
10331 ~(ADVERTISED_Pause |
10332 ADVERTISED_Asym_Pause);
10333 phydev->advertising |= newadv;
10334 if (phydev->autoneg) {
10336 * Always renegotiate the link to
10337 * inform our link partner of our
10338 * flow control settings, even if the
10339 * flow control is forced. Let
10340 * tg3_adjust_link() do the final
10341 * flow control setup.
10343 return phy_start_aneg(phydev);
10347 if (!epause->autoneg)
10348 tg3_setup_flow_control(tp, 0, 0);
10349 } else {
10350 tp->link_config.orig_advertising &=
10351 ~(ADVERTISED_Pause |
10352 ADVERTISED_Asym_Pause);
10353 tp->link_config.orig_advertising |= newadv;
10355 } else {
10356 int irq_sync = 0;
10358 if (netif_running(dev)) {
10359 tg3_netif_stop(tp);
10360 irq_sync = 1;
10363 tg3_full_lock(tp, irq_sync);
10365 if (epause->autoneg)
10366 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10367 else
10368 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10369 if (epause->rx_pause)
10370 tp->link_config.flowctrl |= FLOW_CTRL_RX;
10371 else
10372 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
10373 if (epause->tx_pause)
10374 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10375 else
10376 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
10378 if (netif_running(dev)) {
10379 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10380 err = tg3_restart_hw(tp, 1);
10381 if (!err)
10382 tg3_netif_start(tp);
10385 tg3_full_unlock(tp);
10388 return err;
10391 static int tg3_get_sset_count(struct net_device *dev, int sset)
10393 switch (sset) {
10394 case ETH_SS_TEST:
10395 return TG3_NUM_TEST;
10396 case ETH_SS_STATS:
10397 return TG3_NUM_STATS;
10398 default:
10399 return -EOPNOTSUPP;
10403 static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
10405 switch (stringset) {
10406 case ETH_SS_STATS:
10407 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10408 break;
10409 case ETH_SS_TEST:
10410 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10411 break;
10412 default:
10413 WARN_ON(1); /* we need a WARN() */
10414 break;
10418 static int tg3_set_phys_id(struct net_device *dev,
10419 enum ethtool_phys_id_state state)
10421 struct tg3 *tp = netdev_priv(dev);
10423 if (!netif_running(tp->dev))
10424 return -EAGAIN;
10426 switch (state) {
10427 case ETHTOOL_ID_ACTIVE:
10428 return 1; /* cycle on/off once per second */
10430 case ETHTOOL_ID_ON:
10431 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10432 LED_CTRL_1000MBPS_ON |
10433 LED_CTRL_100MBPS_ON |
10434 LED_CTRL_10MBPS_ON |
10435 LED_CTRL_TRAFFIC_OVERRIDE |
10436 LED_CTRL_TRAFFIC_BLINK |
10437 LED_CTRL_TRAFFIC_LED);
10438 break;
10440 case ETHTOOL_ID_OFF:
10441 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10442 LED_CTRL_TRAFFIC_OVERRIDE);
10443 break;
10445 case ETHTOOL_ID_INACTIVE:
10446 tw32(MAC_LED_CTRL, tp->led_ctrl);
10447 break;
10450 return 0;
10453 static void tg3_get_ethtool_stats(struct net_device *dev,
10454 struct ethtool_stats *estats, u64 *tmp_stats)
10456 struct tg3 *tp = netdev_priv(dev);
10457 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10460 static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10462 int i;
10463 __be32 *buf;
10464 u32 offset = 0, len = 0;
10465 u32 magic, val;
10467 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
10468 tg3_nvram_read(tp, 0, &magic))
10469 return NULL;
10471 if (magic == TG3_EEPROM_MAGIC) {
10472 for (offset = TG3_NVM_DIR_START;
10473 offset < TG3_NVM_DIR_END;
10474 offset += TG3_NVM_DIRENT_SIZE) {
10475 if (tg3_nvram_read(tp, offset, &val))
10476 return NULL;
10478 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10479 TG3_NVM_DIRTYPE_EXTVPD)
10480 break;
10483 if (offset != TG3_NVM_DIR_END) {
10484 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10485 if (tg3_nvram_read(tp, offset + 4, &offset))
10486 return NULL;
10488 offset = tg3_nvram_logical_addr(tp, offset);
10492 if (!offset || !len) {
10493 offset = TG3_NVM_VPD_OFF;
10494 len = TG3_NVM_VPD_LEN;
10497 buf = kmalloc(len, GFP_KERNEL);
10498 if (buf == NULL)
10499 return NULL;
10501 if (magic == TG3_EEPROM_MAGIC) {
10502 for (i = 0; i < len; i += 4) {
10503 /* The data is in little-endian format in NVRAM.
10504 * Use the big-endian read routines to preserve
10505 * the byte order as it exists in NVRAM.
10507 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10508 goto error;
10510 } else {
10511 u8 *ptr;
10512 ssize_t cnt;
10513 unsigned int pos = 0;
10515 ptr = (u8 *)&buf[0];
10516 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10517 cnt = pci_read_vpd(tp->pdev, pos,
10518 len - pos, ptr);
10519 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10520 cnt = 0;
10521 else if (cnt < 0)
10522 goto error;
10524 if (pos != len)
10525 goto error;
10528 return buf;
10530 error:
10531 kfree(buf);
10532 return NULL;
10535 #define NVRAM_TEST_SIZE 0x100
10536 #define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10537 #define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10538 #define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
10539 #define NVRAM_SELFBOOT_HW_SIZE 0x20
10540 #define NVRAM_SELFBOOT_DATA_SIZE 0x1c
10542 static int tg3_test_nvram(struct tg3 *tp)
10544 u32 csum, magic;
10545 __be32 *buf;
10546 int i, j, k, err = 0, size;
10548 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10549 return 0;
10551 if (tg3_nvram_read(tp, 0, &magic) != 0)
10552 return -EIO;
10554 if (magic == TG3_EEPROM_MAGIC)
10555 size = NVRAM_TEST_SIZE;
10556 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
10557 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10558 TG3_EEPROM_SB_FORMAT_1) {
10559 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10560 case TG3_EEPROM_SB_REVISION_0:
10561 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10562 break;
10563 case TG3_EEPROM_SB_REVISION_2:
10564 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10565 break;
10566 case TG3_EEPROM_SB_REVISION_3:
10567 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10568 break;
10569 default:
10570 return 0;
10572 } else
10573 return 0;
10574 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10575 size = NVRAM_SELFBOOT_HW_SIZE;
10576 else
10577 return -EIO;
10579 buf = kmalloc(size, GFP_KERNEL);
10580 if (buf == NULL)
10581 return -ENOMEM;
10583 err = -EIO;
10584 for (i = 0, j = 0; i < size; i += 4, j++) {
10585 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10586 if (err)
10587 break;
10589 if (i < size)
10590 goto out;
10592 /* Selfboot format */
10593 magic = be32_to_cpu(buf[0]);
10594 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
10595 TG3_EEPROM_MAGIC_FW) {
10596 u8 *buf8 = (u8 *) buf, csum8 = 0;
10598 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
10599 TG3_EEPROM_SB_REVISION_2) {
10600 /* For rev 2, the csum doesn't include the MBA. */
10601 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10602 csum8 += buf8[i];
10603 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10604 csum8 += buf8[i];
10605 } else {
10606 for (i = 0; i < size; i++)
10607 csum8 += buf8[i];
10610 if (csum8 == 0) {
10611 err = 0;
10612 goto out;
10615 err = -EIO;
10616 goto out;
10619 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
10620 TG3_EEPROM_MAGIC_HW) {
10621 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
10622 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
10623 u8 *buf8 = (u8 *) buf;
10625 /* Separate the parity bits and the data bytes. */
10626 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10627 if ((i == 0) || (i == 8)) {
10628 int l;
10629 u8 msk;
10631 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10632 parity[k++] = buf8[i] & msk;
10633 i++;
10634 } else if (i == 16) {
10635 int l;
10636 u8 msk;
10638 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10639 parity[k++] = buf8[i] & msk;
10640 i++;
10642 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10643 parity[k++] = buf8[i] & msk;
10644 i++;
10646 data[j++] = buf8[i];
10649 err = -EIO;
10650 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10651 u8 hw8 = hweight8(data[i]);
10653 if ((hw8 & 0x1) && parity[i])
10654 goto out;
10655 else if (!(hw8 & 0x1) && !parity[i])
10656 goto out;
10658 err = 0;
10659 goto out;
10662 err = -EIO;
10664 /* Bootstrap checksum at offset 0x10 */
10665 csum = calc_crc((unsigned char *) buf, 0x10);
10666 if (csum != le32_to_cpu(buf[0x10/4]))
10667 goto out;
10669 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10670 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
10671 if (csum != le32_to_cpu(buf[0xfc/4]))
10672 goto out;
10674 kfree(buf);
10676 buf = tg3_vpd_readblock(tp);
10677 if (!buf)
10678 return -ENOMEM;
10680 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10681 PCI_VPD_LRDT_RO_DATA);
10682 if (i > 0) {
10683 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10684 if (j < 0)
10685 goto out;
10687 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10688 goto out;
10690 i += PCI_VPD_LRDT_TAG_SIZE;
10691 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10692 PCI_VPD_RO_KEYWORD_CHKSUM);
10693 if (j > 0) {
10694 u8 csum8 = 0;
10696 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10698 for (i = 0; i <= j; i++)
10699 csum8 += ((u8 *)buf)[i];
10701 if (csum8)
10702 goto out;
10706 err = 0;
10708 out:
10709 kfree(buf);
10710 return err;
10713 #define TG3_SERDES_TIMEOUT_SEC 2
10714 #define TG3_COPPER_TIMEOUT_SEC 6
10716 static int tg3_test_link(struct tg3 *tp)
10718 int i, max;
10720 if (!netif_running(tp->dev))
10721 return -ENODEV;
10723 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
10724 max = TG3_SERDES_TIMEOUT_SEC;
10725 else
10726 max = TG3_COPPER_TIMEOUT_SEC;
10728 for (i = 0; i < max; i++) {
10729 if (netif_carrier_ok(tp->dev))
10730 return 0;
10732 if (msleep_interruptible(1000))
10733 break;
10736 return -EIO;
10739 /* Only test the commonly used registers */
10740 static int tg3_test_registers(struct tg3 *tp)
10742 int i, is_5705, is_5750;
10743 u32 offset, read_mask, write_mask, val, save_val, read_val;
10744 static struct {
10745 u16 offset;
10746 u16 flags;
10747 #define TG3_FL_5705 0x1
10748 #define TG3_FL_NOT_5705 0x2
10749 #define TG3_FL_NOT_5788 0x4
10750 #define TG3_FL_NOT_5750 0x8
10751 u32 read_mask;
10752 u32 write_mask;
10753 } reg_tbl[] = {
10754 /* MAC Control Registers */
10755 { MAC_MODE, TG3_FL_NOT_5705,
10756 0x00000000, 0x00ef6f8c },
10757 { MAC_MODE, TG3_FL_5705,
10758 0x00000000, 0x01ef6b8c },
10759 { MAC_STATUS, TG3_FL_NOT_5705,
10760 0x03800107, 0x00000000 },
10761 { MAC_STATUS, TG3_FL_5705,
10762 0x03800100, 0x00000000 },
10763 { MAC_ADDR_0_HIGH, 0x0000,
10764 0x00000000, 0x0000ffff },
10765 { MAC_ADDR_0_LOW, 0x0000,
10766 0x00000000, 0xffffffff },
10767 { MAC_RX_MTU_SIZE, 0x0000,
10768 0x00000000, 0x0000ffff },
10769 { MAC_TX_MODE, 0x0000,
10770 0x00000000, 0x00000070 },
10771 { MAC_TX_LENGTHS, 0x0000,
10772 0x00000000, 0x00003fff },
10773 { MAC_RX_MODE, TG3_FL_NOT_5705,
10774 0x00000000, 0x000007fc },
10775 { MAC_RX_MODE, TG3_FL_5705,
10776 0x00000000, 0x000007dc },
10777 { MAC_HASH_REG_0, 0x0000,
10778 0x00000000, 0xffffffff },
10779 { MAC_HASH_REG_1, 0x0000,
10780 0x00000000, 0xffffffff },
10781 { MAC_HASH_REG_2, 0x0000,
10782 0x00000000, 0xffffffff },
10783 { MAC_HASH_REG_3, 0x0000,
10784 0x00000000, 0xffffffff },
10786 /* Receive Data and Receive BD Initiator Control Registers. */
10787 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10788 0x00000000, 0xffffffff },
10789 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10790 0x00000000, 0xffffffff },
10791 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10792 0x00000000, 0x00000003 },
10793 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10794 0x00000000, 0xffffffff },
10795 { RCVDBDI_STD_BD+0, 0x0000,
10796 0x00000000, 0xffffffff },
10797 { RCVDBDI_STD_BD+4, 0x0000,
10798 0x00000000, 0xffffffff },
10799 { RCVDBDI_STD_BD+8, 0x0000,
10800 0x00000000, 0xffff0002 },
10801 { RCVDBDI_STD_BD+0xc, 0x0000,
10802 0x00000000, 0xffffffff },
10804 /* Receive BD Initiator Control Registers. */
10805 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10806 0x00000000, 0xffffffff },
10807 { RCVBDI_STD_THRESH, TG3_FL_5705,
10808 0x00000000, 0x000003ff },
10809 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10810 0x00000000, 0xffffffff },
10812 /* Host Coalescing Control Registers. */
10813 { HOSTCC_MODE, TG3_FL_NOT_5705,
10814 0x00000000, 0x00000004 },
10815 { HOSTCC_MODE, TG3_FL_5705,
10816 0x00000000, 0x000000f6 },
10817 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10818 0x00000000, 0xffffffff },
10819 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10820 0x00000000, 0x000003ff },
10821 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10822 0x00000000, 0xffffffff },
10823 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10824 0x00000000, 0x000003ff },
10825 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10826 0x00000000, 0xffffffff },
10827 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10828 0x00000000, 0x000000ff },
10829 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10830 0x00000000, 0xffffffff },
10831 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10832 0x00000000, 0x000000ff },
10833 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10834 0x00000000, 0xffffffff },
10835 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10836 0x00000000, 0xffffffff },
10837 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10838 0x00000000, 0xffffffff },
10839 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10840 0x00000000, 0x000000ff },
10841 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10842 0x00000000, 0xffffffff },
10843 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10844 0x00000000, 0x000000ff },
10845 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10846 0x00000000, 0xffffffff },
10847 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10848 0x00000000, 0xffffffff },
10849 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10850 0x00000000, 0xffffffff },
10851 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10852 0x00000000, 0xffffffff },
10853 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10854 0x00000000, 0xffffffff },
10855 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10856 0xffffffff, 0x00000000 },
10857 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10858 0xffffffff, 0x00000000 },
10860 /* Buffer Manager Control Registers. */
10861 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
10862 0x00000000, 0x007fff80 },
10863 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
10864 0x00000000, 0x007fffff },
10865 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10866 0x00000000, 0x0000003f },
10867 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10868 0x00000000, 0x000001ff },
10869 { BUFMGR_MB_HIGH_WATER, 0x0000,
10870 0x00000000, 0x000001ff },
10871 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10872 0xffffffff, 0x00000000 },
10873 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10874 0xffffffff, 0x00000000 },
10876 /* Mailbox Registers */
10877 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10878 0x00000000, 0x000001ff },
10879 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10880 0x00000000, 0x000001ff },
10881 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10882 0x00000000, 0x000007ff },
10883 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10884 0x00000000, 0x000001ff },
10886 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10889 is_5705 = is_5750 = 0;
10890 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
10891 is_5705 = 1;
10892 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10893 is_5750 = 1;
10896 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10897 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10898 continue;
10900 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10901 continue;
10903 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10904 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10905 continue;
10907 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10908 continue;
10910 offset = (u32) reg_tbl[i].offset;
10911 read_mask = reg_tbl[i].read_mask;
10912 write_mask = reg_tbl[i].write_mask;
10914 /* Save the original register content */
10915 save_val = tr32(offset);
10917 /* Determine the read-only value. */
10918 read_val = save_val & read_mask;
10920 /* Write zero to the register, then make sure the read-only bits
10921 * are not changed and the read/write bits are all zeros.
10923 tw32(offset, 0);
10925 val = tr32(offset);
10927 /* Test the read-only and read/write bits. */
10928 if (((val & read_mask) != read_val) || (val & write_mask))
10929 goto out;
10931 /* Write ones to all the bits defined by RdMask and WrMask, then
10932 * make sure the read-only bits are not changed and the
10933 * read/write bits are all ones.
10935 tw32(offset, read_mask | write_mask);
10937 val = tr32(offset);
10939 /* Test the read-only bits. */
10940 if ((val & read_mask) != read_val)
10941 goto out;
10943 /* Test the read/write bits. */
10944 if ((val & write_mask) != write_mask)
10945 goto out;
10947 tw32(offset, save_val);
10950 return 0;
10952 out:
10953 if (netif_msg_hw(tp))
10954 netdev_err(tp->dev,
10955 "Register test failed at offset %x\n", offset);
10956 tw32(offset, save_val);
10957 return -EIO;
10960 static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10962 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
10963 int i;
10964 u32 j;
10966 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
10967 for (j = 0; j < len; j += 4) {
10968 u32 val;
10970 tg3_write_mem(tp, offset + j, test_pattern[i]);
10971 tg3_read_mem(tp, offset + j, &val);
10972 if (val != test_pattern[i])
10973 return -EIO;
10976 return 0;
10979 static int tg3_test_memory(struct tg3 *tp)
10981 static struct mem_entry {
10982 u32 offset;
10983 u32 len;
10984 } mem_tbl_570x[] = {
10985 { 0x00000000, 0x00b50},
10986 { 0x00002000, 0x1c000},
10987 { 0xffffffff, 0x00000}
10988 }, mem_tbl_5705[] = {
10989 { 0x00000100, 0x0000c},
10990 { 0x00000200, 0x00008},
10991 { 0x00004000, 0x00800},
10992 { 0x00006000, 0x01000},
10993 { 0x00008000, 0x02000},
10994 { 0x00010000, 0x0e000},
10995 { 0xffffffff, 0x00000}
10996 }, mem_tbl_5755[] = {
10997 { 0x00000200, 0x00008},
10998 { 0x00004000, 0x00800},
10999 { 0x00006000, 0x00800},
11000 { 0x00008000, 0x02000},
11001 { 0x00010000, 0x0c000},
11002 { 0xffffffff, 0x00000}
11003 }, mem_tbl_5906[] = {
11004 { 0x00000200, 0x00008},
11005 { 0x00004000, 0x00400},
11006 { 0x00006000, 0x00400},
11007 { 0x00008000, 0x01000},
11008 { 0x00010000, 0x01000},
11009 { 0xffffffff, 0x00000}
11010 }, mem_tbl_5717[] = {
11011 { 0x00000200, 0x00008},
11012 { 0x00010000, 0x0a000},
11013 { 0x00020000, 0x13c00},
11014 { 0xffffffff, 0x00000}
11015 }, mem_tbl_57765[] = {
11016 { 0x00000200, 0x00008},
11017 { 0x00004000, 0x00800},
11018 { 0x00006000, 0x09800},
11019 { 0x00010000, 0x0a000},
11020 { 0xffffffff, 0x00000}
11022 struct mem_entry *mem_tbl;
11023 int err = 0;
11024 int i;
11026 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
11027 mem_tbl = mem_tbl_5717;
11028 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11029 mem_tbl = mem_tbl_57765;
11030 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
11031 mem_tbl = mem_tbl_5755;
11032 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11033 mem_tbl = mem_tbl_5906;
11034 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
11035 mem_tbl = mem_tbl_5705;
11036 else
11037 mem_tbl = mem_tbl_570x;
11039 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
11040 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11041 if (err)
11042 break;
11045 return err;
11048 #define TG3_MAC_LOOPBACK 0
11049 #define TG3_PHY_LOOPBACK 1
11051 static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
11053 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
11054 u32 desc_idx, coal_now;
11055 struct sk_buff *skb, *rx_skb;
11056 u8 *tx_data;
11057 dma_addr_t map;
11058 int num_pkts, tx_len, rx_len, i, err;
11059 struct tg3_rx_buffer_desc *desc;
11060 struct tg3_napi *tnapi, *rnapi;
11061 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
11063 tnapi = &tp->napi[0];
11064 rnapi = &tp->napi[0];
11065 if (tp->irq_cnt > 1) {
11066 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
11067 rnapi = &tp->napi[1];
11068 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
11069 tnapi = &tp->napi[1];
11071 coal_now = tnapi->coal_now | rnapi->coal_now;
11073 if (loopback_mode == TG3_MAC_LOOPBACK) {
11074 /* HW errata - mac loopback fails in some cases on 5780.
11075 * Normal traffic and PHY loopback are not affected by
11076 * errata. Also, the MAC loopback test is deprecated for
11077 * all newer ASIC revisions.
11079 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
11080 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
11081 return 0;
11083 mac_mode = tp->mac_mode &
11084 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11085 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
11086 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11087 mac_mode |= MAC_MODE_LINK_POLARITY;
11088 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
11089 mac_mode |= MAC_MODE_PORT_MODE_MII;
11090 else
11091 mac_mode |= MAC_MODE_PORT_MODE_GMII;
11092 tw32(MAC_MODE, mac_mode);
11093 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
11094 u32 val;
11096 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
11097 tg3_phy_fet_toggle_apd(tp, false);
11098 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11099 } else
11100 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
11102 tg3_phy_toggle_automdix(tp, 0);
11104 tg3_writephy(tp, MII_BMCR, val);
11105 udelay(40);
11107 mac_mode = tp->mac_mode &
11108 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11109 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
11110 tg3_writephy(tp, MII_TG3_FET_PTEST,
11111 MII_TG3_FET_PTEST_FRC_TX_LINK |
11112 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11113 /* The write needs to be flushed for the AC131 */
11114 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11115 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
11116 mac_mode |= MAC_MODE_PORT_MODE_MII;
11117 } else
11118 mac_mode |= MAC_MODE_PORT_MODE_GMII;
11120 /* reset to prevent losing 1st rx packet intermittently */
11121 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
11122 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11123 udelay(10);
11124 tw32_f(MAC_RX_MODE, tp->rx_mode);
11126 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
11127 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11128 if (masked_phy_id == TG3_PHY_ID_BCM5401)
11129 mac_mode &= ~MAC_MODE_LINK_POLARITY;
11130 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
11131 mac_mode |= MAC_MODE_LINK_POLARITY;
11132 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11133 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11135 tw32(MAC_MODE, mac_mode);
11137 /* Wait for link */
11138 for (i = 0; i < 100; i++) {
11139 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11140 break;
11141 mdelay(1);
11143 } else {
11144 return -EINVAL;
11147 err = -EIO;
11149 tx_len = pktsz;
11150 skb = netdev_alloc_skb(tp->dev, tx_len);
11151 if (!skb)
11152 return -ENOMEM;
11154 tx_data = skb_put(skb, tx_len);
11155 memcpy(tx_data, tp->dev->dev_addr, 6);
11156 memset(tx_data + 6, 0x0, 8);
11158 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
11160 for (i = 14; i < tx_len; i++)
11161 tx_data[i] = (u8) (i & 0xff);
11163 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11164 if (pci_dma_mapping_error(tp->pdev, map)) {
11165 dev_kfree_skb(skb);
11166 return -EIO;
11169 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
11170 rnapi->coal_now);
11172 udelay(10);
11174 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
11176 num_pkts = 0;
11178 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
11180 tnapi->tx_prod++;
11181 num_pkts++;
11183 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11184 tr32_mailbox(tnapi->prodmbox);
11186 udelay(10);
11188 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11189 for (i = 0; i < 35; i++) {
11190 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
11191 coal_now);
11193 udelay(10);
11195 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11196 rx_idx = rnapi->hw_status->idx[0].rx_producer;
11197 if ((tx_idx == tnapi->tx_prod) &&
11198 (rx_idx == (rx_start_idx + num_pkts)))
11199 break;
11202 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
11203 dev_kfree_skb(skb);
11205 if (tx_idx != tnapi->tx_prod)
11206 goto out;
11208 if (rx_idx != rx_start_idx + num_pkts)
11209 goto out;
11211 desc = &rnapi->rx_rcb[rx_start_idx];
11212 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11213 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
11215 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11216 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
11217 goto out;
11219 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
11220 if (rx_len != tx_len)
11221 goto out;
11223 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11224 if (opaque_key != RXD_OPAQUE_RING_STD)
11225 goto out;
11227 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11228 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
11229 } else {
11230 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11231 goto out;
11233 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11234 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx], mapping);
11237 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
11239 for (i = 14; i < tx_len; i++) {
11240 if (*(rx_skb->data + i) != (u8) (i & 0xff))
11241 goto out;
11243 err = 0;
11245 /* tg3_free_rings will unmap and free the rx_skb */
11246 out:
11247 return err;
11250 #define TG3_MAC_LOOPBACK_FAILED 1
11251 #define TG3_PHY_LOOPBACK_FAILED 2
11252 #define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
11253 TG3_PHY_LOOPBACK_FAILED)
11255 static int tg3_test_loopback(struct tg3 *tp)
11257 int err = 0;
11258 u32 eee_cap, cpmuctrl = 0;
11260 if (!netif_running(tp->dev))
11261 return TG3_LOOPBACK_FAILED;
11263 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11264 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11266 err = tg3_reset_hw(tp, 1);
11267 if (err) {
11268 err = TG3_LOOPBACK_FAILED;
11269 goto done;
11272 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
11273 int i;
11275 /* Reroute all rx packets to the 1st queue */
11276 for (i = MAC_RSS_INDIR_TBL_0;
11277 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11278 tw32(i, 0x0);
11281 /* Turn off gphy autopowerdown. */
11282 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11283 tg3_phy_toggle_apd(tp, false);
11285 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
11286 int i;
11287 u32 status;
11289 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11291 /* Wait for up to 40 microseconds to acquire lock. */
11292 for (i = 0; i < 4; i++) {
11293 status = tr32(TG3_CPMU_MUTEX_GNT);
11294 if (status == CPMU_MUTEX_GNT_DRIVER)
11295 break;
11296 udelay(10);
11299 if (status != CPMU_MUTEX_GNT_DRIVER) {
11300 err = TG3_LOOPBACK_FAILED;
11301 goto done;
11304 /* Turn off link-based power management. */
11305 cpmuctrl = tr32(TG3_CPMU_CTRL);
11306 tw32(TG3_CPMU_CTRL,
11307 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11308 CPMU_CTRL_LINK_AWARE_MODE));
11311 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
11312 err |= TG3_MAC_LOOPBACK_FAILED;
11314 if ((tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) &&
11315 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
11316 err |= (TG3_MAC_LOOPBACK_FAILED << 2);
11318 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
11319 tw32(TG3_CPMU_CTRL, cpmuctrl);
11321 /* Release the mutex */
11322 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11325 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
11326 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
11327 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
11328 err |= TG3_PHY_LOOPBACK_FAILED;
11329 if ((tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) &&
11330 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
11331 err |= (TG3_PHY_LOOPBACK_FAILED << 2);
11334 /* Re-enable gphy autopowerdown. */
11335 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11336 tg3_phy_toggle_apd(tp, true);
11338 done:
11339 tp->phy_flags |= eee_cap;
11341 return err;
11344 static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11345 u64 *data)
11347 struct tg3 *tp = netdev_priv(dev);
11349 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
11350 tg3_power_up(tp);
11352 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11354 if (tg3_test_nvram(tp) != 0) {
11355 etest->flags |= ETH_TEST_FL_FAILED;
11356 data[0] = 1;
11358 if (tg3_test_link(tp) != 0) {
11359 etest->flags |= ETH_TEST_FL_FAILED;
11360 data[1] = 1;
11362 if (etest->flags & ETH_TEST_FL_OFFLINE) {
11363 int err, err2 = 0, irq_sync = 0;
11365 if (netif_running(dev)) {
11366 tg3_phy_stop(tp);
11367 tg3_netif_stop(tp);
11368 irq_sync = 1;
11371 tg3_full_lock(tp, irq_sync);
11373 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
11374 err = tg3_nvram_lock(tp);
11375 tg3_halt_cpu(tp, RX_CPU_BASE);
11376 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11377 tg3_halt_cpu(tp, TX_CPU_BASE);
11378 if (!err)
11379 tg3_nvram_unlock(tp);
11381 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
11382 tg3_phy_reset(tp);
11384 if (tg3_test_registers(tp) != 0) {
11385 etest->flags |= ETH_TEST_FL_FAILED;
11386 data[2] = 1;
11388 if (tg3_test_memory(tp) != 0) {
11389 etest->flags |= ETH_TEST_FL_FAILED;
11390 data[3] = 1;
11392 if ((data[4] = tg3_test_loopback(tp)) != 0)
11393 etest->flags |= ETH_TEST_FL_FAILED;
11395 tg3_full_unlock(tp);
11397 if (tg3_test_interrupt(tp) != 0) {
11398 etest->flags |= ETH_TEST_FL_FAILED;
11399 data[5] = 1;
11402 tg3_full_lock(tp, 0);
11404 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11405 if (netif_running(dev)) {
11406 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
11407 err2 = tg3_restart_hw(tp, 1);
11408 if (!err2)
11409 tg3_netif_start(tp);
11412 tg3_full_unlock(tp);
11414 if (irq_sync && !err2)
11415 tg3_phy_start(tp);
11417 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
11418 tg3_power_down(tp);
11422 static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11424 struct mii_ioctl_data *data = if_mii(ifr);
11425 struct tg3 *tp = netdev_priv(dev);
11426 int err;
11428 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
11429 struct phy_device *phydev;
11430 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
11431 return -EAGAIN;
11432 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11433 return phy_mii_ioctl(phydev, ifr, cmd);
11436 switch (cmd) {
11437 case SIOCGMIIPHY:
11438 data->phy_id = tp->phy_addr;
11440 /* fallthru */
11441 case SIOCGMIIREG: {
11442 u32 mii_regval;
11444 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
11445 break; /* We have no PHY */
11447 if (!netif_running(dev))
11448 return -EAGAIN;
11450 spin_lock_bh(&tp->lock);
11451 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
11452 spin_unlock_bh(&tp->lock);
11454 data->val_out = mii_regval;
11456 return err;
11459 case SIOCSMIIREG:
11460 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
11461 break; /* We have no PHY */
11463 if (!netif_running(dev))
11464 return -EAGAIN;
11466 spin_lock_bh(&tp->lock);
11467 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
11468 spin_unlock_bh(&tp->lock);
11470 return err;
11472 default:
11473 /* do nothing */
11474 break;
11476 return -EOPNOTSUPP;
11479 static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11481 struct tg3 *tp = netdev_priv(dev);
11483 memcpy(ec, &tp->coal, sizeof(*ec));
11484 return 0;
11487 static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11489 struct tg3 *tp = netdev_priv(dev);
11490 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11491 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11493 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11494 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11495 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11496 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11497 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11500 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11501 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11502 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11503 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11504 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11505 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11506 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11507 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11508 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11509 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11510 return -EINVAL;
11512 /* No rx interrupts will be generated if both are zero */
11513 if ((ec->rx_coalesce_usecs == 0) &&
11514 (ec->rx_max_coalesced_frames == 0))
11515 return -EINVAL;
11517 /* No tx interrupts will be generated if both are zero */
11518 if ((ec->tx_coalesce_usecs == 0) &&
11519 (ec->tx_max_coalesced_frames == 0))
11520 return -EINVAL;
11522 /* Only copy relevant parameters, ignore all others. */
11523 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11524 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11525 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11526 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11527 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11528 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11529 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11530 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11531 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11533 if (netif_running(dev)) {
11534 tg3_full_lock(tp, 0);
11535 __tg3_set_coalesce(tp, &tp->coal);
11536 tg3_full_unlock(tp);
11538 return 0;
11541 static const struct ethtool_ops tg3_ethtool_ops = {
11542 .get_settings = tg3_get_settings,
11543 .set_settings = tg3_set_settings,
11544 .get_drvinfo = tg3_get_drvinfo,
11545 .get_regs_len = tg3_get_regs_len,
11546 .get_regs = tg3_get_regs,
11547 .get_wol = tg3_get_wol,
11548 .set_wol = tg3_set_wol,
11549 .get_msglevel = tg3_get_msglevel,
11550 .set_msglevel = tg3_set_msglevel,
11551 .nway_reset = tg3_nway_reset,
11552 .get_link = ethtool_op_get_link,
11553 .get_eeprom_len = tg3_get_eeprom_len,
11554 .get_eeprom = tg3_get_eeprom,
11555 .set_eeprom = tg3_set_eeprom,
11556 .get_ringparam = tg3_get_ringparam,
11557 .set_ringparam = tg3_set_ringparam,
11558 .get_pauseparam = tg3_get_pauseparam,
11559 .set_pauseparam = tg3_set_pauseparam,
11560 .self_test = tg3_self_test,
11561 .get_strings = tg3_get_strings,
11562 .set_phys_id = tg3_set_phys_id,
11563 .get_ethtool_stats = tg3_get_ethtool_stats,
11564 .get_coalesce = tg3_get_coalesce,
11565 .set_coalesce = tg3_set_coalesce,
11566 .get_sset_count = tg3_get_sset_count,
11569 static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11571 u32 cursize, val, magic;
11573 tp->nvram_size = EEPROM_CHIP_SIZE;
11575 if (tg3_nvram_read(tp, 0, &magic) != 0)
11576 return;
11578 if ((magic != TG3_EEPROM_MAGIC) &&
11579 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11580 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
11581 return;
11584 * Size the chip by reading offsets at increasing powers of two.
11585 * When we encounter our validation signature, we know the addressing
11586 * has wrapped around, and thus have our chip size.
11588 cursize = 0x10;
11590 while (cursize < tp->nvram_size) {
11591 if (tg3_nvram_read(tp, cursize, &val) != 0)
11592 return;
11594 if (val == magic)
11595 break;
11597 cursize <<= 1;
11600 tp->nvram_size = cursize;
11603 static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11605 u32 val;
11607 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11608 tg3_nvram_read(tp, 0, &val) != 0)
11609 return;
11611 /* Selfboot format */
11612 if (val != TG3_EEPROM_MAGIC) {
11613 tg3_get_eeprom_size(tp);
11614 return;
11617 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
11618 if (val != 0) {
11619 /* This is confusing. We want to operate on the
11620 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11621 * call will read from NVRAM and byteswap the data
11622 * according to the byteswapping settings for all
11623 * other register accesses. This ensures the data we
11624 * want will always reside in the lower 16-bits.
11625 * However, the data in NVRAM is in LE format, which
11626 * means the data from the NVRAM read will always be
11627 * opposite the endianness of the CPU. The 16-bit
11628 * byteswap then brings the data to CPU endianness.
11630 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
11631 return;
11634 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11637 static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11639 u32 nvcfg1;
11641 nvcfg1 = tr32(NVRAM_CFG1);
11642 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11643 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11644 } else {
11645 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11646 tw32(NVRAM_CFG1, nvcfg1);
11649 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
11650 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
11651 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
11652 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11653 tp->nvram_jedecnum = JEDEC_ATMEL;
11654 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11655 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11656 break;
11657 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11658 tp->nvram_jedecnum = JEDEC_ATMEL;
11659 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11660 break;
11661 case FLASH_VENDOR_ATMEL_EEPROM:
11662 tp->nvram_jedecnum = JEDEC_ATMEL;
11663 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11664 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11665 break;
11666 case FLASH_VENDOR_ST:
11667 tp->nvram_jedecnum = JEDEC_ST;
11668 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11669 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11670 break;
11671 case FLASH_VENDOR_SAIFUN:
11672 tp->nvram_jedecnum = JEDEC_SAIFUN;
11673 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11674 break;
11675 case FLASH_VENDOR_SST_SMALL:
11676 case FLASH_VENDOR_SST_LARGE:
11677 tp->nvram_jedecnum = JEDEC_SST;
11678 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11679 break;
11681 } else {
11682 tp->nvram_jedecnum = JEDEC_ATMEL;
11683 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11684 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11688 static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11690 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11691 case FLASH_5752PAGE_SIZE_256:
11692 tp->nvram_pagesize = 256;
11693 break;
11694 case FLASH_5752PAGE_SIZE_512:
11695 tp->nvram_pagesize = 512;
11696 break;
11697 case FLASH_5752PAGE_SIZE_1K:
11698 tp->nvram_pagesize = 1024;
11699 break;
11700 case FLASH_5752PAGE_SIZE_2K:
11701 tp->nvram_pagesize = 2048;
11702 break;
11703 case FLASH_5752PAGE_SIZE_4K:
11704 tp->nvram_pagesize = 4096;
11705 break;
11706 case FLASH_5752PAGE_SIZE_264:
11707 tp->nvram_pagesize = 264;
11708 break;
11709 case FLASH_5752PAGE_SIZE_528:
11710 tp->nvram_pagesize = 528;
11711 break;
11715 static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11717 u32 nvcfg1;
11719 nvcfg1 = tr32(NVRAM_CFG1);
11721 /* NVRAM protection for TPM */
11722 if (nvcfg1 & (1 << 27))
11723 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
11725 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11726 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11727 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11728 tp->nvram_jedecnum = JEDEC_ATMEL;
11729 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11730 break;
11731 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11732 tp->nvram_jedecnum = JEDEC_ATMEL;
11733 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11734 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11735 break;
11736 case FLASH_5752VENDOR_ST_M45PE10:
11737 case FLASH_5752VENDOR_ST_M45PE20:
11738 case FLASH_5752VENDOR_ST_M45PE40:
11739 tp->nvram_jedecnum = JEDEC_ST;
11740 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11741 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11742 break;
11745 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
11746 tg3_nvram_get_pagesize(tp, nvcfg1);
11747 } else {
11748 /* For eeprom, set pagesize to maximum eeprom size */
11749 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11751 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11752 tw32(NVRAM_CFG1, nvcfg1);
11756 static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11758 u32 nvcfg1, protect = 0;
11760 nvcfg1 = tr32(NVRAM_CFG1);
11762 /* NVRAM protection for TPM */
11763 if (nvcfg1 & (1 << 27)) {
11764 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
11765 protect = 1;
11768 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11769 switch (nvcfg1) {
11770 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11771 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11772 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11773 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11774 tp->nvram_jedecnum = JEDEC_ATMEL;
11775 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11776 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11777 tp->nvram_pagesize = 264;
11778 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11779 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11780 tp->nvram_size = (protect ? 0x3e200 :
11781 TG3_NVRAM_SIZE_512KB);
11782 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11783 tp->nvram_size = (protect ? 0x1f200 :
11784 TG3_NVRAM_SIZE_256KB);
11785 else
11786 tp->nvram_size = (protect ? 0x1f200 :
11787 TG3_NVRAM_SIZE_128KB);
11788 break;
11789 case FLASH_5752VENDOR_ST_M45PE10:
11790 case FLASH_5752VENDOR_ST_M45PE20:
11791 case FLASH_5752VENDOR_ST_M45PE40:
11792 tp->nvram_jedecnum = JEDEC_ST;
11793 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11794 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11795 tp->nvram_pagesize = 256;
11796 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11797 tp->nvram_size = (protect ?
11798 TG3_NVRAM_SIZE_64KB :
11799 TG3_NVRAM_SIZE_128KB);
11800 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11801 tp->nvram_size = (protect ?
11802 TG3_NVRAM_SIZE_64KB :
11803 TG3_NVRAM_SIZE_256KB);
11804 else
11805 tp->nvram_size = (protect ?
11806 TG3_NVRAM_SIZE_128KB :
11807 TG3_NVRAM_SIZE_512KB);
11808 break;
11812 static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11814 u32 nvcfg1;
11816 nvcfg1 = tr32(NVRAM_CFG1);
11818 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11819 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11820 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11821 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11822 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11823 tp->nvram_jedecnum = JEDEC_ATMEL;
11824 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11825 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11827 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11828 tw32(NVRAM_CFG1, nvcfg1);
11829 break;
11830 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11831 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11832 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11833 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11834 tp->nvram_jedecnum = JEDEC_ATMEL;
11835 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11836 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11837 tp->nvram_pagesize = 264;
11838 break;
11839 case FLASH_5752VENDOR_ST_M45PE10:
11840 case FLASH_5752VENDOR_ST_M45PE20:
11841 case FLASH_5752VENDOR_ST_M45PE40:
11842 tp->nvram_jedecnum = JEDEC_ST;
11843 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11844 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11845 tp->nvram_pagesize = 256;
11846 break;
11850 static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11852 u32 nvcfg1, protect = 0;
11854 nvcfg1 = tr32(NVRAM_CFG1);
11856 /* NVRAM protection for TPM */
11857 if (nvcfg1 & (1 << 27)) {
11858 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
11859 protect = 1;
11862 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11863 switch (nvcfg1) {
11864 case FLASH_5761VENDOR_ATMEL_ADB021D:
11865 case FLASH_5761VENDOR_ATMEL_ADB041D:
11866 case FLASH_5761VENDOR_ATMEL_ADB081D:
11867 case FLASH_5761VENDOR_ATMEL_ADB161D:
11868 case FLASH_5761VENDOR_ATMEL_MDB021D:
11869 case FLASH_5761VENDOR_ATMEL_MDB041D:
11870 case FLASH_5761VENDOR_ATMEL_MDB081D:
11871 case FLASH_5761VENDOR_ATMEL_MDB161D:
11872 tp->nvram_jedecnum = JEDEC_ATMEL;
11873 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11874 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11875 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11876 tp->nvram_pagesize = 256;
11877 break;
11878 case FLASH_5761VENDOR_ST_A_M45PE20:
11879 case FLASH_5761VENDOR_ST_A_M45PE40:
11880 case FLASH_5761VENDOR_ST_A_M45PE80:
11881 case FLASH_5761VENDOR_ST_A_M45PE16:
11882 case FLASH_5761VENDOR_ST_M_M45PE20:
11883 case FLASH_5761VENDOR_ST_M_M45PE40:
11884 case FLASH_5761VENDOR_ST_M_M45PE80:
11885 case FLASH_5761VENDOR_ST_M_M45PE16:
11886 tp->nvram_jedecnum = JEDEC_ST;
11887 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11888 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11889 tp->nvram_pagesize = 256;
11890 break;
11893 if (protect) {
11894 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11895 } else {
11896 switch (nvcfg1) {
11897 case FLASH_5761VENDOR_ATMEL_ADB161D:
11898 case FLASH_5761VENDOR_ATMEL_MDB161D:
11899 case FLASH_5761VENDOR_ST_A_M45PE16:
11900 case FLASH_5761VENDOR_ST_M_M45PE16:
11901 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11902 break;
11903 case FLASH_5761VENDOR_ATMEL_ADB081D:
11904 case FLASH_5761VENDOR_ATMEL_MDB081D:
11905 case FLASH_5761VENDOR_ST_A_M45PE80:
11906 case FLASH_5761VENDOR_ST_M_M45PE80:
11907 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11908 break;
11909 case FLASH_5761VENDOR_ATMEL_ADB041D:
11910 case FLASH_5761VENDOR_ATMEL_MDB041D:
11911 case FLASH_5761VENDOR_ST_A_M45PE40:
11912 case FLASH_5761VENDOR_ST_M_M45PE40:
11913 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11914 break;
11915 case FLASH_5761VENDOR_ATMEL_ADB021D:
11916 case FLASH_5761VENDOR_ATMEL_MDB021D:
11917 case FLASH_5761VENDOR_ST_A_M45PE20:
11918 case FLASH_5761VENDOR_ST_M_M45PE20:
11919 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11920 break;
11925 static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11927 tp->nvram_jedecnum = JEDEC_ATMEL;
11928 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11929 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11932 static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11934 u32 nvcfg1;
11936 nvcfg1 = tr32(NVRAM_CFG1);
11938 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11939 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11940 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11941 tp->nvram_jedecnum = JEDEC_ATMEL;
11942 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11943 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11945 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11946 tw32(NVRAM_CFG1, nvcfg1);
11947 return;
11948 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11949 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11950 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11951 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11952 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11953 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11954 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11955 tp->nvram_jedecnum = JEDEC_ATMEL;
11956 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11957 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11959 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11960 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11961 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11962 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11963 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11964 break;
11965 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11966 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11967 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11968 break;
11969 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11970 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11971 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11972 break;
11974 break;
11975 case FLASH_5752VENDOR_ST_M45PE10:
11976 case FLASH_5752VENDOR_ST_M45PE20:
11977 case FLASH_5752VENDOR_ST_M45PE40:
11978 tp->nvram_jedecnum = JEDEC_ST;
11979 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11980 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11982 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11983 case FLASH_5752VENDOR_ST_M45PE10:
11984 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11985 break;
11986 case FLASH_5752VENDOR_ST_M45PE20:
11987 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11988 break;
11989 case FLASH_5752VENDOR_ST_M45PE40:
11990 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11991 break;
11993 break;
11994 default:
11995 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11996 return;
11999 tg3_nvram_get_pagesize(tp, nvcfg1);
12000 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12001 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
12005 static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12007 u32 nvcfg1;
12009 nvcfg1 = tr32(NVRAM_CFG1);
12011 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12012 case FLASH_5717VENDOR_ATMEL_EEPROM:
12013 case FLASH_5717VENDOR_MICRO_EEPROM:
12014 tp->nvram_jedecnum = JEDEC_ATMEL;
12015 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12016 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12018 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12019 tw32(NVRAM_CFG1, nvcfg1);
12020 return;
12021 case FLASH_5717VENDOR_ATMEL_MDB011D:
12022 case FLASH_5717VENDOR_ATMEL_ADB011B:
12023 case FLASH_5717VENDOR_ATMEL_ADB011D:
12024 case FLASH_5717VENDOR_ATMEL_MDB021D:
12025 case FLASH_5717VENDOR_ATMEL_ADB021B:
12026 case FLASH_5717VENDOR_ATMEL_ADB021D:
12027 case FLASH_5717VENDOR_ATMEL_45USPT:
12028 tp->nvram_jedecnum = JEDEC_ATMEL;
12029 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12030 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12032 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12033 case FLASH_5717VENDOR_ATMEL_MDB021D:
12034 /* Detect size with tg3_nvram_get_size() */
12035 break;
12036 case FLASH_5717VENDOR_ATMEL_ADB021B:
12037 case FLASH_5717VENDOR_ATMEL_ADB021D:
12038 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12039 break;
12040 default:
12041 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12042 break;
12044 break;
12045 case FLASH_5717VENDOR_ST_M_M25PE10:
12046 case FLASH_5717VENDOR_ST_A_M25PE10:
12047 case FLASH_5717VENDOR_ST_M_M45PE10:
12048 case FLASH_5717VENDOR_ST_A_M45PE10:
12049 case FLASH_5717VENDOR_ST_M_M25PE20:
12050 case FLASH_5717VENDOR_ST_A_M25PE20:
12051 case FLASH_5717VENDOR_ST_M_M45PE20:
12052 case FLASH_5717VENDOR_ST_A_M45PE20:
12053 case FLASH_5717VENDOR_ST_25USPT:
12054 case FLASH_5717VENDOR_ST_45USPT:
12055 tp->nvram_jedecnum = JEDEC_ST;
12056 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12057 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12059 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12060 case FLASH_5717VENDOR_ST_M_M25PE20:
12061 case FLASH_5717VENDOR_ST_M_M45PE20:
12062 /* Detect size with tg3_nvram_get_size() */
12063 break;
12064 case FLASH_5717VENDOR_ST_A_M25PE20:
12065 case FLASH_5717VENDOR_ST_A_M45PE20:
12066 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12067 break;
12068 default:
12069 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12070 break;
12072 break;
12073 default:
12074 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
12075 return;
12078 tg3_nvram_get_pagesize(tp, nvcfg1);
12079 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12080 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
12083 static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12085 u32 nvcfg1, nvmpinstrp;
12087 nvcfg1 = tr32(NVRAM_CFG1);
12088 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12090 switch (nvmpinstrp) {
12091 case FLASH_5720_EEPROM_HD:
12092 case FLASH_5720_EEPROM_LD:
12093 tp->nvram_jedecnum = JEDEC_ATMEL;
12094 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12096 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12097 tw32(NVRAM_CFG1, nvcfg1);
12098 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12099 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12100 else
12101 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12102 return;
12103 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12104 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12105 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12106 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12107 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12108 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12109 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12110 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12111 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12112 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12113 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12114 case FLASH_5720VENDOR_ATMEL_45USPT:
12115 tp->nvram_jedecnum = JEDEC_ATMEL;
12116 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12117 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12119 switch (nvmpinstrp) {
12120 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12121 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12122 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12123 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12124 break;
12125 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12126 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12127 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12128 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12129 break;
12130 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12131 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12132 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12133 break;
12134 default:
12135 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12136 break;
12138 break;
12139 case FLASH_5720VENDOR_M_ST_M25PE10:
12140 case FLASH_5720VENDOR_M_ST_M45PE10:
12141 case FLASH_5720VENDOR_A_ST_M25PE10:
12142 case FLASH_5720VENDOR_A_ST_M45PE10:
12143 case FLASH_5720VENDOR_M_ST_M25PE20:
12144 case FLASH_5720VENDOR_M_ST_M45PE20:
12145 case FLASH_5720VENDOR_A_ST_M25PE20:
12146 case FLASH_5720VENDOR_A_ST_M45PE20:
12147 case FLASH_5720VENDOR_M_ST_M25PE40:
12148 case FLASH_5720VENDOR_M_ST_M45PE40:
12149 case FLASH_5720VENDOR_A_ST_M25PE40:
12150 case FLASH_5720VENDOR_A_ST_M45PE40:
12151 case FLASH_5720VENDOR_M_ST_M25PE80:
12152 case FLASH_5720VENDOR_M_ST_M45PE80:
12153 case FLASH_5720VENDOR_A_ST_M25PE80:
12154 case FLASH_5720VENDOR_A_ST_M45PE80:
12155 case FLASH_5720VENDOR_ST_25USPT:
12156 case FLASH_5720VENDOR_ST_45USPT:
12157 tp->nvram_jedecnum = JEDEC_ST;
12158 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12159 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12161 switch (nvmpinstrp) {
12162 case FLASH_5720VENDOR_M_ST_M25PE20:
12163 case FLASH_5720VENDOR_M_ST_M45PE20:
12164 case FLASH_5720VENDOR_A_ST_M25PE20:
12165 case FLASH_5720VENDOR_A_ST_M45PE20:
12166 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12167 break;
12168 case FLASH_5720VENDOR_M_ST_M25PE40:
12169 case FLASH_5720VENDOR_M_ST_M45PE40:
12170 case FLASH_5720VENDOR_A_ST_M25PE40:
12171 case FLASH_5720VENDOR_A_ST_M45PE40:
12172 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12173 break;
12174 case FLASH_5720VENDOR_M_ST_M25PE80:
12175 case FLASH_5720VENDOR_M_ST_M45PE80:
12176 case FLASH_5720VENDOR_A_ST_M25PE80:
12177 case FLASH_5720VENDOR_A_ST_M45PE80:
12178 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12179 break;
12180 default:
12181 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12182 break;
12184 break;
12185 default:
12186 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
12187 return;
12190 tg3_nvram_get_pagesize(tp, nvcfg1);
12191 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12192 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
12195 /* Chips other than 5700/5701 use the NVRAM for fetching info. */
12196 static void __devinit tg3_nvram_init(struct tg3 *tp)
12198 tw32_f(GRC_EEPROM_ADDR,
12199 (EEPROM_ADDR_FSM_RESET |
12200 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12201 EEPROM_ADDR_CLKPERD_SHIFT)));
12203 msleep(1);
12205 /* Enable seeprom accesses. */
12206 tw32_f(GRC_LOCAL_CTRL,
12207 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12208 udelay(100);
12210 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12211 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
12212 tp->tg3_flags |= TG3_FLAG_NVRAM;
12214 if (tg3_nvram_lock(tp)) {
12215 netdev_warn(tp->dev,
12216 "Cannot get nvram lock, %s failed\n",
12217 __func__);
12218 return;
12220 tg3_enable_nvram_access(tp);
12222 tp->nvram_size = 0;
12224 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12225 tg3_get_5752_nvram_info(tp);
12226 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12227 tg3_get_5755_nvram_info(tp);
12228 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
12229 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12230 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12231 tg3_get_5787_nvram_info(tp);
12232 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12233 tg3_get_5761_nvram_info(tp);
12234 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12235 tg3_get_5906_nvram_info(tp);
12236 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12237 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
12238 tg3_get_57780_nvram_info(tp);
12239 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12240 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
12241 tg3_get_5717_nvram_info(tp);
12242 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12243 tg3_get_5720_nvram_info(tp);
12244 else
12245 tg3_get_nvram_info(tp);
12247 if (tp->nvram_size == 0)
12248 tg3_get_nvram_size(tp);
12250 tg3_disable_nvram_access(tp);
12251 tg3_nvram_unlock(tp);
12253 } else {
12254 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
12256 tg3_get_eeprom_size(tp);
12260 static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12261 u32 offset, u32 len, u8 *buf)
12263 int i, j, rc = 0;
12264 u32 val;
12266 for (i = 0; i < len; i += 4) {
12267 u32 addr;
12268 __be32 data;
12270 addr = offset + i;
12272 memcpy(&data, buf + i, 4);
12275 * The SEEPROM interface expects the data to always be opposite
12276 * the native endian format. We accomplish this by reversing
12277 * all the operations that would have been performed on the
12278 * data from a call to tg3_nvram_read_be32().
12280 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
12282 val = tr32(GRC_EEPROM_ADDR);
12283 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12285 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12286 EEPROM_ADDR_READ);
12287 tw32(GRC_EEPROM_ADDR, val |
12288 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12289 (addr & EEPROM_ADDR_ADDR_MASK) |
12290 EEPROM_ADDR_START |
12291 EEPROM_ADDR_WRITE);
12293 for (j = 0; j < 1000; j++) {
12294 val = tr32(GRC_EEPROM_ADDR);
12296 if (val & EEPROM_ADDR_COMPLETE)
12297 break;
12298 msleep(1);
12300 if (!(val & EEPROM_ADDR_COMPLETE)) {
12301 rc = -EBUSY;
12302 break;
12306 return rc;
12309 /* offset and length are dword aligned */
12310 static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12311 u8 *buf)
12313 int ret = 0;
12314 u32 pagesize = tp->nvram_pagesize;
12315 u32 pagemask = pagesize - 1;
12316 u32 nvram_cmd;
12317 u8 *tmp;
12319 tmp = kmalloc(pagesize, GFP_KERNEL);
12320 if (tmp == NULL)
12321 return -ENOMEM;
12323 while (len) {
12324 int j;
12325 u32 phy_addr, page_off, size;
12327 phy_addr = offset & ~pagemask;
12329 for (j = 0; j < pagesize; j += 4) {
12330 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12331 (__be32 *) (tmp + j));
12332 if (ret)
12333 break;
12335 if (ret)
12336 break;
12338 page_off = offset & pagemask;
12339 size = pagesize;
12340 if (len < size)
12341 size = len;
12343 len -= size;
12345 memcpy(tmp + page_off, buf, size);
12347 offset = offset + (pagesize - page_off);
12349 tg3_enable_nvram_access(tp);
12352 * Before we can erase the flash page, we need
12353 * to issue a special "write enable" command.
12355 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12357 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12358 break;
12360 /* Erase the target page */
12361 tw32(NVRAM_ADDR, phy_addr);
12363 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12364 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12366 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12367 break;
12369 /* Issue another write enable to start the write. */
12370 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12372 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12373 break;
12375 for (j = 0; j < pagesize; j += 4) {
12376 __be32 data;
12378 data = *((__be32 *) (tmp + j));
12380 tw32(NVRAM_WRDATA, be32_to_cpu(data));
12382 tw32(NVRAM_ADDR, phy_addr + j);
12384 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12385 NVRAM_CMD_WR;
12387 if (j == 0)
12388 nvram_cmd |= NVRAM_CMD_FIRST;
12389 else if (j == (pagesize - 4))
12390 nvram_cmd |= NVRAM_CMD_LAST;
12392 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12393 break;
12395 if (ret)
12396 break;
12399 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12400 tg3_nvram_exec_cmd(tp, nvram_cmd);
12402 kfree(tmp);
12404 return ret;
12407 /* offset and length are dword aligned */
12408 static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12409 u8 *buf)
12411 int i, ret = 0;
12413 for (i = 0; i < len; i += 4, offset += 4) {
12414 u32 page_off, phy_addr, nvram_cmd;
12415 __be32 data;
12417 memcpy(&data, buf + i, 4);
12418 tw32(NVRAM_WRDATA, be32_to_cpu(data));
12420 page_off = offset % tp->nvram_pagesize;
12422 phy_addr = tg3_nvram_phys_addr(tp, offset);
12424 tw32(NVRAM_ADDR, phy_addr);
12426 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12428 if (page_off == 0 || i == 0)
12429 nvram_cmd |= NVRAM_CMD_FIRST;
12430 if (page_off == (tp->nvram_pagesize - 4))
12431 nvram_cmd |= NVRAM_CMD_LAST;
12433 if (i == (len - 4))
12434 nvram_cmd |= NVRAM_CMD_LAST;
12436 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
12437 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
12438 (tp->nvram_jedecnum == JEDEC_ST) &&
12439 (nvram_cmd & NVRAM_CMD_FIRST)) {
12441 if ((ret = tg3_nvram_exec_cmd(tp,
12442 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12443 NVRAM_CMD_DONE)))
12445 break;
12447 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12448 /* We always do complete word writes to eeprom. */
12449 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12452 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12453 break;
12455 return ret;
12458 /* offset and length are dword aligned */
12459 static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12461 int ret;
12463 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
12464 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12465 ~GRC_LCLCTRL_GPIO_OUTPUT1);
12466 udelay(40);
12469 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
12470 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
12471 } else {
12472 u32 grc_mode;
12474 ret = tg3_nvram_lock(tp);
12475 if (ret)
12476 return ret;
12478 tg3_enable_nvram_access(tp);
12479 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
12480 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
12481 tw32(NVRAM_WRITE1, 0x406);
12483 grc_mode = tr32(GRC_MODE);
12484 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12486 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12487 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12489 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12490 buf);
12491 } else {
12492 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12493 buf);
12496 grc_mode = tr32(GRC_MODE);
12497 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12499 tg3_disable_nvram_access(tp);
12500 tg3_nvram_unlock(tp);
12503 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
12504 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
12505 udelay(40);
12508 return ret;
12511 struct subsys_tbl_ent {
12512 u16 subsys_vendor, subsys_devid;
12513 u32 phy_id;
12516 static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
12517 /* Broadcom boards. */
12518 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12519 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
12520 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12521 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
12522 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12523 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
12524 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12525 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12526 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12527 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
12528 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12529 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
12530 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12531 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12532 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12533 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
12534 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12535 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
12536 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12537 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
12538 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12539 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
12541 /* 3com boards. */
12542 { TG3PCI_SUBVENDOR_ID_3COM,
12543 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
12544 { TG3PCI_SUBVENDOR_ID_3COM,
12545 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
12546 { TG3PCI_SUBVENDOR_ID_3COM,
12547 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12548 { TG3PCI_SUBVENDOR_ID_3COM,
12549 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
12550 { TG3PCI_SUBVENDOR_ID_3COM,
12551 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
12553 /* DELL boards. */
12554 { TG3PCI_SUBVENDOR_ID_DELL,
12555 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
12556 { TG3PCI_SUBVENDOR_ID_DELL,
12557 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
12558 { TG3PCI_SUBVENDOR_ID_DELL,
12559 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
12560 { TG3PCI_SUBVENDOR_ID_DELL,
12561 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
12563 /* Compaq boards. */
12564 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12565 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
12566 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12567 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
12568 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12569 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12570 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12571 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
12572 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12573 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
12575 /* IBM boards. */
12576 { TG3PCI_SUBVENDOR_ID_IBM,
12577 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
12580 static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
12582 int i;
12584 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12585 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12586 tp->pdev->subsystem_vendor) &&
12587 (subsys_id_to_phy_id[i].subsys_devid ==
12588 tp->pdev->subsystem_device))
12589 return &subsys_id_to_phy_id[i];
12591 return NULL;
12594 static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
12596 u32 val;
12597 u16 pmcsr;
12599 /* On some early chips the SRAM cannot be accessed in D3hot state,
12600 * so need make sure we're in D0.
12602 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12603 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12604 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12605 msleep(1);
12607 /* Make sure register accesses (indirect or otherwise)
12608 * will function correctly.
12610 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12611 tp->misc_host_ctrl);
12613 /* The memory arbiter has to be enabled in order for SRAM accesses
12614 * to succeed. Normally on powerup the tg3 chip firmware will make
12615 * sure it is enabled, but other entities such as system netboot
12616 * code might disable it.
12618 val = tr32(MEMARB_MODE);
12619 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12621 tp->phy_id = TG3_PHY_ID_INVALID;
12622 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12624 /* Assume an onboard device and WOL capable by default. */
12625 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
12627 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
12628 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
12629 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12630 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12632 val = tr32(VCPU_CFGSHDW);
12633 if (val & VCPU_CFGSHDW_ASPM_DBNC)
12634 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12635 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
12636 (val & VCPU_CFGSHDW_WOL_MAGPKT))
12637 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12638 goto done;
12641 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12642 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12643 u32 nic_cfg, led_cfg;
12644 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
12645 int eeprom_phy_serdes = 0;
12647 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12648 tp->nic_sram_data_cfg = nic_cfg;
12650 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12651 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12652 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12653 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12654 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12655 (ver > 0) && (ver < 0x100))
12656 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12658 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12659 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12661 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12662 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12663 eeprom_phy_serdes = 1;
12665 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12666 if (nic_phy_id != 0) {
12667 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12668 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12670 eeprom_phy_id = (id1 >> 16) << 10;
12671 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12672 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12673 } else
12674 eeprom_phy_id = 0;
12676 tp->phy_id = eeprom_phy_id;
12677 if (eeprom_phy_serdes) {
12678 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12679 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
12680 else
12681 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
12684 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
12685 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12686 SHASTA_EXT_LED_MODE_MASK);
12687 else
12688 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12690 switch (led_cfg) {
12691 default:
12692 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12693 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12694 break;
12696 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12697 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12698 break;
12700 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12701 tp->led_ctrl = LED_CTRL_MODE_MAC;
12703 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12704 * read on some older 5700/5701 bootcode.
12706 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12707 ASIC_REV_5700 ||
12708 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12709 ASIC_REV_5701)
12710 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12712 break;
12714 case SHASTA_EXT_LED_SHARED:
12715 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12716 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12717 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12718 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12719 LED_CTRL_MODE_PHY_2);
12720 break;
12722 case SHASTA_EXT_LED_MAC:
12723 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12724 break;
12726 case SHASTA_EXT_LED_COMBO:
12727 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12728 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12729 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12730 LED_CTRL_MODE_PHY_2);
12731 break;
12735 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12736 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12737 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12738 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12740 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12741 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12743 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
12744 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
12745 if ((tp->pdev->subsystem_vendor ==
12746 PCI_VENDOR_ID_ARIMA) &&
12747 (tp->pdev->subsystem_device == 0x205a ||
12748 tp->pdev->subsystem_device == 0x2063))
12749 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12750 } else {
12751 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12752 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12755 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12756 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
12757 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
12758 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12761 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12762 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
12763 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
12765 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
12766 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12767 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
12769 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
12770 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
12771 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12773 if (cfg2 & (1 << 17))
12774 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
12776 /* serdes signal pre-emphasis in register 0x590 set by */
12777 /* bootcode if bit 18 is set */
12778 if (cfg2 & (1 << 18))
12779 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
12781 if (((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) ||
12782 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12783 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX))) &&
12784 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12785 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
12787 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12788 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
12789 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
12790 u32 cfg3;
12792 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12793 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12794 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12797 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12798 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
12799 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12800 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12801 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12802 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
12804 done:
12805 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
12806 device_set_wakeup_enable(&tp->pdev->dev,
12807 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
12808 else
12809 device_set_wakeup_capable(&tp->pdev->dev, false);
12812 static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12814 int i;
12815 u32 val;
12817 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12818 tw32(OTP_CTRL, cmd);
12820 /* Wait for up to 1 ms for command to execute. */
12821 for (i = 0; i < 100; i++) {
12822 val = tr32(OTP_STATUS);
12823 if (val & OTP_STATUS_CMD_DONE)
12824 break;
12825 udelay(10);
12828 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12831 /* Read the gphy configuration from the OTP region of the chip. The gphy
12832 * configuration is a 32-bit value that straddles the alignment boundary.
12833 * We do two 32-bit reads and then shift and merge the results.
12835 static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12837 u32 bhalf_otp, thalf_otp;
12839 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12841 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12842 return 0;
12844 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12846 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12847 return 0;
12849 thalf_otp = tr32(OTP_READ_DATA);
12851 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12853 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12854 return 0;
12856 bhalf_otp = tr32(OTP_READ_DATA);
12858 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12861 static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12863 u32 adv = ADVERTISED_Autoneg |
12864 ADVERTISED_Pause;
12866 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12867 adv |= ADVERTISED_1000baseT_Half |
12868 ADVERTISED_1000baseT_Full;
12870 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12871 adv |= ADVERTISED_100baseT_Half |
12872 ADVERTISED_100baseT_Full |
12873 ADVERTISED_10baseT_Half |
12874 ADVERTISED_10baseT_Full |
12875 ADVERTISED_TP;
12876 else
12877 adv |= ADVERTISED_FIBRE;
12879 tp->link_config.advertising = adv;
12880 tp->link_config.speed = SPEED_INVALID;
12881 tp->link_config.duplex = DUPLEX_INVALID;
12882 tp->link_config.autoneg = AUTONEG_ENABLE;
12883 tp->link_config.active_speed = SPEED_INVALID;
12884 tp->link_config.active_duplex = DUPLEX_INVALID;
12885 tp->link_config.orig_speed = SPEED_INVALID;
12886 tp->link_config.orig_duplex = DUPLEX_INVALID;
12887 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12890 static int __devinit tg3_phy_probe(struct tg3 *tp)
12892 u32 hw_phy_id_1, hw_phy_id_2;
12893 u32 hw_phy_id, hw_phy_id_masked;
12894 int err;
12896 /* flow control autonegotiation is default behavior */
12897 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
12898 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12900 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12901 return tg3_phy_init(tp);
12903 /* Reading the PHY ID register can conflict with ASF
12904 * firmware access to the PHY hardware.
12906 err = 0;
12907 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12908 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
12909 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
12910 } else {
12911 /* Now read the physical PHY_ID from the chip and verify
12912 * that it is sane. If it doesn't look good, we fall back
12913 * to either the hard-coded table based PHY_ID and failing
12914 * that the value found in the eeprom area.
12916 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12917 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12919 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12920 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12921 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12923 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
12926 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
12927 tp->phy_id = hw_phy_id;
12928 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
12929 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
12930 else
12931 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
12932 } else {
12933 if (tp->phy_id != TG3_PHY_ID_INVALID) {
12934 /* Do nothing, phy ID already set up in
12935 * tg3_get_eeprom_hw_cfg().
12937 } else {
12938 struct subsys_tbl_ent *p;
12940 /* No eeprom signature? Try the hardcoded
12941 * subsys device table.
12943 p = tg3_lookup_by_subsys(tp);
12944 if (!p)
12945 return -ENODEV;
12947 tp->phy_id = p->phy_id;
12948 if (!tp->phy_id ||
12949 tp->phy_id == TG3_PHY_ID_BCM8002)
12950 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
12954 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12955 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12956 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12957 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12958 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
12959 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12961 tg3_phy_init_link_config(tp);
12963 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12964 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
12965 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
12966 u32 bmsr, adv_reg, tg3_ctrl, mask;
12968 tg3_readphy(tp, MII_BMSR, &bmsr);
12969 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12970 (bmsr & BMSR_LSTATUS))
12971 goto skip_phy_reset;
12973 err = tg3_phy_reset(tp);
12974 if (err)
12975 return err;
12977 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12978 ADVERTISE_100HALF | ADVERTISE_100FULL |
12979 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12980 tg3_ctrl = 0;
12981 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
12982 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12983 MII_TG3_CTRL_ADV_1000_FULL);
12984 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12985 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12986 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12987 MII_TG3_CTRL_ENABLE_AS_MASTER);
12990 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12991 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12992 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12993 if (!tg3_copper_is_advertising_all(tp, mask)) {
12994 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12996 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12997 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12999 tg3_writephy(tp, MII_BMCR,
13000 BMCR_ANENABLE | BMCR_ANRESTART);
13002 tg3_phy_set_wirespeed(tp);
13004 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
13005 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13006 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
13009 skip_phy_reset:
13010 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
13011 err = tg3_init_5401phy_dsp(tp);
13012 if (err)
13013 return err;
13015 err = tg3_init_5401phy_dsp(tp);
13018 return err;
13021 static void __devinit tg3_read_vpd(struct tg3 *tp)
13023 u8 *vpd_data;
13024 unsigned int block_end, rosize, len;
13025 int j, i = 0;
13027 vpd_data = (u8 *)tg3_vpd_readblock(tp);
13028 if (!vpd_data)
13029 goto out_no_vpd;
13031 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13032 PCI_VPD_LRDT_RO_DATA);
13033 if (i < 0)
13034 goto out_not_found;
13036 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13037 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13038 i += PCI_VPD_LRDT_TAG_SIZE;
13040 if (block_end > TG3_NVM_VPD_LEN)
13041 goto out_not_found;
13043 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13044 PCI_VPD_RO_KEYWORD_MFR_ID);
13045 if (j > 0) {
13046 len = pci_vpd_info_field_size(&vpd_data[j]);
13048 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13049 if (j + len > block_end || len != 4 ||
13050 memcmp(&vpd_data[j], "1028", 4))
13051 goto partno;
13053 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13054 PCI_VPD_RO_KEYWORD_VENDOR0);
13055 if (j < 0)
13056 goto partno;
13058 len = pci_vpd_info_field_size(&vpd_data[j]);
13060 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13061 if (j + len > block_end)
13062 goto partno;
13064 memcpy(tp->fw_ver, &vpd_data[j], len);
13065 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13068 partno:
13069 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13070 PCI_VPD_RO_KEYWORD_PARTNO);
13071 if (i < 0)
13072 goto out_not_found;
13074 len = pci_vpd_info_field_size(&vpd_data[i]);
13076 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13077 if (len > TG3_BPN_SIZE ||
13078 (len + i) > TG3_NVM_VPD_LEN)
13079 goto out_not_found;
13081 memcpy(tp->board_part_number, &vpd_data[i], len);
13083 out_not_found:
13084 kfree(vpd_data);
13085 if (tp->board_part_number[0])
13086 return;
13088 out_no_vpd:
13089 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13090 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13091 strcpy(tp->board_part_number, "BCM5717");
13092 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13093 strcpy(tp->board_part_number, "BCM5718");
13094 else
13095 goto nomatch;
13096 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13097 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13098 strcpy(tp->board_part_number, "BCM57780");
13099 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13100 strcpy(tp->board_part_number, "BCM57760");
13101 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13102 strcpy(tp->board_part_number, "BCM57790");
13103 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13104 strcpy(tp->board_part_number, "BCM57788");
13105 else
13106 goto nomatch;
13107 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13108 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13109 strcpy(tp->board_part_number, "BCM57761");
13110 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13111 strcpy(tp->board_part_number, "BCM57765");
13112 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13113 strcpy(tp->board_part_number, "BCM57781");
13114 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13115 strcpy(tp->board_part_number, "BCM57785");
13116 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13117 strcpy(tp->board_part_number, "BCM57791");
13118 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13119 strcpy(tp->board_part_number, "BCM57795");
13120 else
13121 goto nomatch;
13122 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13123 strcpy(tp->board_part_number, "BCM95906");
13124 } else {
13125 nomatch:
13126 strcpy(tp->board_part_number, "none");
13130 static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13132 u32 val;
13134 if (tg3_nvram_read(tp, offset, &val) ||
13135 (val & 0xfc000000) != 0x0c000000 ||
13136 tg3_nvram_read(tp, offset + 4, &val) ||
13137 val != 0)
13138 return 0;
13140 return 1;
13143 static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13145 u32 val, offset, start, ver_offset;
13146 int i, dst_off;
13147 bool newver = false;
13149 if (tg3_nvram_read(tp, 0xc, &offset) ||
13150 tg3_nvram_read(tp, 0x4, &start))
13151 return;
13153 offset = tg3_nvram_logical_addr(tp, offset);
13155 if (tg3_nvram_read(tp, offset, &val))
13156 return;
13158 if ((val & 0xfc000000) == 0x0c000000) {
13159 if (tg3_nvram_read(tp, offset + 4, &val))
13160 return;
13162 if (val == 0)
13163 newver = true;
13166 dst_off = strlen(tp->fw_ver);
13168 if (newver) {
13169 if (TG3_VER_SIZE - dst_off < 16 ||
13170 tg3_nvram_read(tp, offset + 8, &ver_offset))
13171 return;
13173 offset = offset + ver_offset - start;
13174 for (i = 0; i < 16; i += 4) {
13175 __be32 v;
13176 if (tg3_nvram_read_be32(tp, offset + i, &v))
13177 return;
13179 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
13181 } else {
13182 u32 major, minor;
13184 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13185 return;
13187 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13188 TG3_NVM_BCVER_MAJSFT;
13189 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
13190 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13191 "v%d.%02d", major, minor);
13195 static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13197 u32 val, major, minor;
13199 /* Use native endian representation */
13200 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13201 return;
13203 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13204 TG3_NVM_HWSB_CFG1_MAJSFT;
13205 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13206 TG3_NVM_HWSB_CFG1_MINSFT;
13208 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13211 static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13213 u32 offset, major, minor, build;
13215 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
13217 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13218 return;
13220 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13221 case TG3_EEPROM_SB_REVISION_0:
13222 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13223 break;
13224 case TG3_EEPROM_SB_REVISION_2:
13225 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13226 break;
13227 case TG3_EEPROM_SB_REVISION_3:
13228 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13229 break;
13230 case TG3_EEPROM_SB_REVISION_4:
13231 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13232 break;
13233 case TG3_EEPROM_SB_REVISION_5:
13234 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13235 break;
13236 case TG3_EEPROM_SB_REVISION_6:
13237 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13238 break;
13239 default:
13240 return;
13243 if (tg3_nvram_read(tp, offset, &val))
13244 return;
13246 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13247 TG3_EEPROM_SB_EDH_BLD_SHFT;
13248 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13249 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13250 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13252 if (minor > 99 || build > 26)
13253 return;
13255 offset = strlen(tp->fw_ver);
13256 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13257 " v%d.%02d", major, minor);
13259 if (build > 0) {
13260 offset = strlen(tp->fw_ver);
13261 if (offset < TG3_VER_SIZE - 1)
13262 tp->fw_ver[offset] = 'a' + build - 1;
13266 static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
13268 u32 val, offset, start;
13269 int i, vlen;
13271 for (offset = TG3_NVM_DIR_START;
13272 offset < TG3_NVM_DIR_END;
13273 offset += TG3_NVM_DIRENT_SIZE) {
13274 if (tg3_nvram_read(tp, offset, &val))
13275 return;
13277 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13278 break;
13281 if (offset == TG3_NVM_DIR_END)
13282 return;
13284 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
13285 start = 0x08000000;
13286 else if (tg3_nvram_read(tp, offset - 4, &start))
13287 return;
13289 if (tg3_nvram_read(tp, offset + 4, &offset) ||
13290 !tg3_fw_img_is_valid(tp, offset) ||
13291 tg3_nvram_read(tp, offset + 8, &val))
13292 return;
13294 offset += val - start;
13296 vlen = strlen(tp->fw_ver);
13298 tp->fw_ver[vlen++] = ',';
13299 tp->fw_ver[vlen++] = ' ';
13301 for (i = 0; i < 4; i++) {
13302 __be32 v;
13303 if (tg3_nvram_read_be32(tp, offset, &v))
13304 return;
13306 offset += sizeof(v);
13308 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13309 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
13310 break;
13313 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13314 vlen += sizeof(v);
13318 static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13320 int vlen;
13321 u32 apedata;
13322 char *fwtype;
13324 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
13325 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
13326 return;
13328 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13329 if (apedata != APE_SEG_SIG_MAGIC)
13330 return;
13332 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13333 if (!(apedata & APE_FW_STATUS_READY))
13334 return;
13336 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13338 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
13339 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
13340 fwtype = "NCSI";
13341 } else {
13342 fwtype = "DASH";
13345 vlen = strlen(tp->fw_ver);
13347 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13348 fwtype,
13349 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13350 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13351 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13352 (apedata & APE_FW_VERSION_BLDMSK));
13355 static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13357 u32 val;
13358 bool vpd_vers = false;
13360 if (tp->fw_ver[0] != 0)
13361 vpd_vers = true;
13363 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
13364 strcat(tp->fw_ver, "sb");
13365 return;
13368 if (tg3_nvram_read(tp, 0, &val))
13369 return;
13371 if (val == TG3_EEPROM_MAGIC)
13372 tg3_read_bc_ver(tp);
13373 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13374 tg3_read_sb_ver(tp, val);
13375 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13376 tg3_read_hwsb_ver(tp);
13377 else
13378 return;
13380 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
13381 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
13382 goto done;
13384 tg3_read_mgmtfw_ver(tp);
13386 done:
13387 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
13390 static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13392 static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13394 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
13395 return TG3_RX_RET_MAX_SIZE_5717;
13396 else if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
13397 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
13398 return TG3_RX_RET_MAX_SIZE_5700;
13399 else
13400 return TG3_RX_RET_MAX_SIZE_5705;
13403 static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
13404 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13405 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13406 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13407 { },
13410 static int __devinit tg3_get_invariants(struct tg3 *tp)
13412 u32 misc_ctrl_reg;
13413 u32 pci_state_reg, grc_misc_cfg;
13414 u32 val;
13415 u16 pci_cmd;
13416 int err;
13418 /* Force memory write invalidate off. If we leave it on,
13419 * then on 5700_BX chips we have to enable a workaround.
13420 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13421 * to match the cacheline size. The Broadcom driver have this
13422 * workaround but turns MWI off all the times so never uses
13423 * it. This seems to suggest that the workaround is insufficient.
13425 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13426 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13427 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13429 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13430 * has the register indirect write enable bit set before
13431 * we try to access any of the MMIO registers. It is also
13432 * critical that the PCI-X hw workaround situation is decided
13433 * before that as well.
13435 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13436 &misc_ctrl_reg);
13438 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13439 MISC_HOST_CTRL_CHIPREV_SHIFT);
13440 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13441 u32 prod_id_asic_rev;
13443 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13444 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
13445 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13446 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
13447 pci_read_config_dword(tp->pdev,
13448 TG3PCI_GEN2_PRODID_ASICREV,
13449 &prod_id_asic_rev);
13450 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13451 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13452 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13453 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13454 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13455 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13456 pci_read_config_dword(tp->pdev,
13457 TG3PCI_GEN15_PRODID_ASICREV,
13458 &prod_id_asic_rev);
13459 else
13460 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13461 &prod_id_asic_rev);
13463 tp->pci_chip_rev_id = prod_id_asic_rev;
13466 /* Wrong chip ID in 5752 A0. This code can be removed later
13467 * as A0 is not in production.
13469 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13470 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13472 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13473 * we need to disable memory and use config. cycles
13474 * only to access all registers. The 5702/03 chips
13475 * can mistakenly decode the special cycles from the
13476 * ICH chipsets as memory write cycles, causing corruption
13477 * of register and memory space. Only certain ICH bridges
13478 * will drive special cycles with non-zero data during the
13479 * address phase which can fall within the 5703's address
13480 * range. This is not an ICH bug as the PCI spec allows
13481 * non-zero address during special cycles. However, only
13482 * these ICH bridges are known to drive non-zero addresses
13483 * during special cycles.
13485 * Since special cycles do not cross PCI bridges, we only
13486 * enable this workaround if the 5703 is on the secondary
13487 * bus of these ICH bridges.
13489 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13490 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13491 static struct tg3_dev_id {
13492 u32 vendor;
13493 u32 device;
13494 u32 rev;
13495 } ich_chipsets[] = {
13496 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13497 PCI_ANY_ID },
13498 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13499 PCI_ANY_ID },
13500 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13501 0xa },
13502 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13503 PCI_ANY_ID },
13504 { },
13506 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13507 struct pci_dev *bridge = NULL;
13509 while (pci_id->vendor != 0) {
13510 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13511 bridge);
13512 if (!bridge) {
13513 pci_id++;
13514 continue;
13516 if (pci_id->rev != PCI_ANY_ID) {
13517 if (bridge->revision > pci_id->rev)
13518 continue;
13520 if (bridge->subordinate &&
13521 (bridge->subordinate->number ==
13522 tp->pdev->bus->number)) {
13524 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
13525 pci_dev_put(bridge);
13526 break;
13531 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13532 static struct tg3_dev_id {
13533 u32 vendor;
13534 u32 device;
13535 } bridge_chipsets[] = {
13536 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13537 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13538 { },
13540 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13541 struct pci_dev *bridge = NULL;
13543 while (pci_id->vendor != 0) {
13544 bridge = pci_get_device(pci_id->vendor,
13545 pci_id->device,
13546 bridge);
13547 if (!bridge) {
13548 pci_id++;
13549 continue;
13551 if (bridge->subordinate &&
13552 (bridge->subordinate->number <=
13553 tp->pdev->bus->number) &&
13554 (bridge->subordinate->subordinate >=
13555 tp->pdev->bus->number)) {
13556 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
13557 pci_dev_put(bridge);
13558 break;
13563 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13564 * DMA addresses > 40-bit. This bridge may have other additional
13565 * 57xx devices behind it in some 4-port NIC designs for example.
13566 * Any tg3 device found behind the bridge will also need the 40-bit
13567 * DMA workaround.
13569 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13570 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13571 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
13572 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13573 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
13574 } else {
13575 struct pci_dev *bridge = NULL;
13577 do {
13578 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13579 PCI_DEVICE_ID_SERVERWORKS_EPB,
13580 bridge);
13581 if (bridge && bridge->subordinate &&
13582 (bridge->subordinate->number <=
13583 tp->pdev->bus->number) &&
13584 (bridge->subordinate->subordinate >=
13585 tp->pdev->bus->number)) {
13586 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13587 pci_dev_put(bridge);
13588 break;
13590 } while (bridge);
13593 /* Initialize misc host control in PCI block. */
13594 tp->misc_host_ctrl |= (misc_ctrl_reg &
13595 MISC_HOST_CTRL_CHIPREV);
13596 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13597 tp->misc_host_ctrl);
13599 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13600 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
13601 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13602 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13603 tp->pdev_peer = tg3_find_peer(tp);
13605 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13606 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13607 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13608 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13610 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
13611 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
13612 tp->tg3_flags3 |= TG3_FLG3_57765_PLUS;
13614 /* Intentionally exclude ASIC_REV_5906 */
13615 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13616 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
13617 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13618 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13619 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13620 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13621 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
13622 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13624 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13625 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13626 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
13627 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13628 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
13629 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13632 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13633 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13634 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13636 /* 5700 B0 chips do not support checksumming correctly due
13637 * to hardware bugs.
13639 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
13640 u32 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
13642 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
13643 features |= NETIF_F_IPV6_CSUM;
13644 tp->dev->features |= features;
13645 tp->dev->hw_features |= features;
13646 tp->dev->vlan_features |= features;
13649 /* Determine TSO capabilities */
13650 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
13651 ; /* Do nothing. HW bug. */
13652 else if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
13653 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13654 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13655 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13656 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13657 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13658 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13659 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13660 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13661 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13662 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13663 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13664 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13665 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13666 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13667 tp->fw_needed = FIRMWARE_TG3TSO5;
13668 else
13669 tp->fw_needed = FIRMWARE_TG3TSO;
13672 tp->irq_max = 1;
13674 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13675 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13676 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13677 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13678 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13679 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13680 tp->pdev_peer == tp->pdev))
13681 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13683 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13684 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13685 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
13688 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
13689 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13690 tp->irq_max = TG3_IRQ_MAX_VECS;
13694 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13695 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13696 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13697 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13698 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13699 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13700 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
13703 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
13704 tp->tg3_flags3 |= TG3_FLG3_LRG_PROD_RING_CAP;
13706 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
13707 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
13708 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13710 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13711 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13712 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
13713 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
13715 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13716 &pci_state_reg);
13718 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13719 if (tp->pcie_cap != 0) {
13720 u16 lnkctl;
13722 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
13724 tp->pcie_readrq = 4096;
13725 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13726 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13727 tp->pcie_readrq = 2048;
13729 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
13731 pci_read_config_word(tp->pdev,
13732 tp->pcie_cap + PCI_EXP_LNKCTL,
13733 &lnkctl);
13734 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13735 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13736 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
13737 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13738 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13739 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13740 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
13741 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
13742 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13743 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
13745 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
13746 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
13747 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13748 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13749 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13750 if (!tp->pcix_cap) {
13751 dev_err(&tp->pdev->dev,
13752 "Cannot find PCI-X capability, aborting\n");
13753 return -EIO;
13756 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13757 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13760 /* If we have an AMD 762 or VIA K8T800 chipset, write
13761 * reordering to the mailbox registers done by the host
13762 * controller can cause major troubles. We read back from
13763 * every mailbox register write to force the writes to be
13764 * posted to the chip in order.
13766 if (pci_dev_present(tg3_write_reorder_chipsets) &&
13767 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13768 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13770 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13771 &tp->pci_cacheline_sz);
13772 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13773 &tp->pci_lat_timer);
13774 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13775 tp->pci_lat_timer < 64) {
13776 tp->pci_lat_timer = 64;
13777 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13778 tp->pci_lat_timer);
13781 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13782 /* 5700 BX chips need to have their TX producer index
13783 * mailboxes written twice to workaround a bug.
13785 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
13787 /* If we are in PCI-X mode, enable register write workaround.
13789 * The workaround is to use indirect register accesses
13790 * for all chip writes not to mailbox registers.
13792 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
13793 u32 pm_reg;
13795 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13797 /* The chip can have it's power management PCI config
13798 * space registers clobbered due to this bug.
13799 * So explicitly force the chip into D0 here.
13801 pci_read_config_dword(tp->pdev,
13802 tp->pm_cap + PCI_PM_CTRL,
13803 &pm_reg);
13804 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13805 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
13806 pci_write_config_dword(tp->pdev,
13807 tp->pm_cap + PCI_PM_CTRL,
13808 pm_reg);
13810 /* Also, force SERR#/PERR# in PCI command. */
13811 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13812 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13813 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13817 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13818 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13819 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13820 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13822 /* Chip-specific fixup from Broadcom driver */
13823 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13824 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13825 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13826 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13829 /* Default fast path register access methods */
13830 tp->read32 = tg3_read32;
13831 tp->write32 = tg3_write32;
13832 tp->read32_mbox = tg3_read32;
13833 tp->write32_mbox = tg3_write32;
13834 tp->write32_tx_mbox = tg3_write32;
13835 tp->write32_rx_mbox = tg3_write32;
13837 /* Various workaround register access methods */
13838 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13839 tp->write32 = tg3_write_indirect_reg32;
13840 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13841 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13842 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13844 * Back to back register writes can cause problems on these
13845 * chips, the workaround is to read back all reg writes
13846 * except those to mailbox regs.
13848 * See tg3_write_indirect_reg32().
13850 tp->write32 = tg3_write_flush_reg32;
13853 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13854 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13855 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13856 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13857 tp->write32_rx_mbox = tg3_write_flush_reg32;
13860 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13861 tp->read32 = tg3_read_indirect_reg32;
13862 tp->write32 = tg3_write_indirect_reg32;
13863 tp->read32_mbox = tg3_read_indirect_mbox;
13864 tp->write32_mbox = tg3_write_indirect_mbox;
13865 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13866 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13868 iounmap(tp->regs);
13869 tp->regs = NULL;
13871 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13872 pci_cmd &= ~PCI_COMMAND_MEMORY;
13873 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13875 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13876 tp->read32_mbox = tg3_read32_mbox_5906;
13877 tp->write32_mbox = tg3_write32_mbox_5906;
13878 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13879 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13882 if (tp->write32 == tg3_write_indirect_reg32 ||
13883 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13884 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13885 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
13886 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13888 /* Get eeprom hw config before calling tg3_set_power_state().
13889 * In particular, the TG3_FLG2_IS_NIC flag must be
13890 * determined before calling tg3_set_power_state() so that
13891 * we know whether or not to switch out of Vaux power.
13892 * When the flag is set, it means that GPIO1 is used for eeprom
13893 * write protect and also implies that it is a LOM where GPIOs
13894 * are not used to switch power.
13896 tg3_get_eeprom_hw_cfg(tp);
13898 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13899 /* Allow reads and writes to the
13900 * APE register and memory space.
13902 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
13903 PCISTATE_ALLOW_APE_SHMEM_WR |
13904 PCISTATE_ALLOW_APE_PSPACE_WR;
13905 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13906 pci_state_reg);
13909 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13910 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13911 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13912 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13913 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
13914 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13916 /* Set up tp->grc_local_ctrl before calling tg_power_up().
13917 * GPIO1 driven high will bring 5700's external PHY out of reset.
13918 * It is also used as eeprom write protect on LOMs.
13920 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13921 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13922 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13923 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13924 GRC_LCLCTRL_GPIO_OUTPUT1);
13925 /* Unused GPIO3 must be driven as output on 5752 because there
13926 * are no pull-up resistors on unused GPIO pins.
13928 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13929 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
13931 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13932 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13933 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13934 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13936 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13937 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
13938 /* Turn off the debug UART. */
13939 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13940 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13941 /* Keep VMain power. */
13942 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13943 GRC_LCLCTRL_GPIO_OUTPUT0;
13946 /* Force the chip into D0. */
13947 err = tg3_power_up(tp);
13948 if (err) {
13949 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
13950 return err;
13953 /* Derive initial jumbo mode from MTU assigned in
13954 * ether_setup() via the alloc_etherdev() call
13956 if (tp->dev->mtu > ETH_DATA_LEN &&
13957 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
13958 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
13960 /* Determine WakeOnLan speed to use. */
13961 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13962 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13963 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13964 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13965 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13966 } else {
13967 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13970 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13971 tp->phy_flags |= TG3_PHYFLG_IS_FET;
13973 /* A few boards don't want Ethernet@WireSpeed phy feature */
13974 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13975 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13976 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
13977 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
13978 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13979 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13980 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
13982 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13983 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13984 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
13985 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13986 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
13988 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
13989 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
13990 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
13991 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
13992 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
13993 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13994 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
13995 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13996 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
13997 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13998 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13999 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
14000 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
14001 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
14002 } else
14003 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
14006 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14007 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14008 tp->phy_otp = tg3_read_otp_phycfg(tp);
14009 if (tp->phy_otp == 0)
14010 tp->phy_otp = TG3_OTP_DEFAULT;
14013 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
14014 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14015 else
14016 tp->mi_mode = MAC_MI_MODE_BASE;
14018 tp->coalesce_mode = 0;
14019 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14020 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14021 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14023 /* Set these bits to enable statistics workaround. */
14024 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14025 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14026 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14027 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14028 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14031 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14032 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
14033 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
14035 err = tg3_mdio_init(tp);
14036 if (err)
14037 return err;
14039 /* Initialize data/descriptor byte/word swapping. */
14040 val = tr32(GRC_MODE);
14041 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14042 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14043 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14044 GRC_MODE_B2HRX_ENABLE |
14045 GRC_MODE_HTX2B_ENABLE |
14046 GRC_MODE_HOST_STACKUP);
14047 else
14048 val &= GRC_MODE_HOST_STACKUP;
14050 tw32(GRC_MODE, val | tp->grc_mode);
14052 tg3_switch_clocks(tp);
14054 /* Clear this out for sanity. */
14055 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14057 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14058 &pci_state_reg);
14059 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
14060 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
14061 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14063 if (chiprevid == CHIPREV_ID_5701_A0 ||
14064 chiprevid == CHIPREV_ID_5701_B0 ||
14065 chiprevid == CHIPREV_ID_5701_B2 ||
14066 chiprevid == CHIPREV_ID_5701_B5) {
14067 void __iomem *sram_base;
14069 /* Write some dummy words into the SRAM status block
14070 * area, see if it reads back correctly. If the return
14071 * value is bad, force enable the PCIX workaround.
14073 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14075 writel(0x00000000, sram_base);
14076 writel(0x00000000, sram_base + 4);
14077 writel(0xffffffff, sram_base + 4);
14078 if (readl(sram_base) != 0x00000000)
14079 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
14083 udelay(50);
14084 tg3_nvram_init(tp);
14086 grc_misc_cfg = tr32(GRC_MISC_CFG);
14087 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14089 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14090 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14091 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
14092 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
14094 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
14095 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
14096 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
14097 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
14098 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14099 HOSTCC_MODE_CLRTICK_TXBD);
14101 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14102 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14103 tp->misc_host_ctrl);
14106 /* Preserve the APE MAC_MODE bits */
14107 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
14108 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
14109 else
14110 tp->mac_mode = TG3_DEF_MAC_MODE;
14112 /* these are limited to 10/100 only */
14113 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14114 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14115 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14116 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14117 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14118 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14119 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14120 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14121 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
14122 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14123 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
14124 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
14125 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14126 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
14127 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14128 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
14130 err = tg3_phy_probe(tp);
14131 if (err) {
14132 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
14133 /* ... but do not return immediately ... */
14134 tg3_mdio_fini(tp);
14137 tg3_read_vpd(tp);
14138 tg3_read_fw_ver(tp);
14140 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14141 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
14142 } else {
14143 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
14144 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
14145 else
14146 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
14149 /* 5700 {AX,BX} chips have a broken status block link
14150 * change bit implementation, so we must use the
14151 * status register in those cases.
14153 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
14154 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
14155 else
14156 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
14158 /* The led_ctrl is set during tg3_phy_probe, here we might
14159 * have to force the link status polling mechanism based
14160 * upon subsystem IDs.
14162 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
14163 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
14164 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14165 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
14166 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
14169 /* For all SERDES we poll the MAC status register. */
14170 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
14171 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
14172 else
14173 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
14175 tp->rx_offset = NET_IP_ALIGN;
14176 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
14177 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
14178 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
14179 tp->rx_offset = 0;
14180 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
14181 tp->rx_copy_thresh = ~(u16)0;
14182 #endif
14185 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14186 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
14187 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14189 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
14191 /* Increment the rx prod index on the rx std ring by at most
14192 * 8 for these chips to workaround hw errata.
14194 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14195 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14196 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14197 tp->rx_std_max_post = 8;
14199 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
14200 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14201 PCIE_PWR_MGMT_L1_THRESH_MSK;
14203 return err;
14206 #ifdef CONFIG_SPARC
14207 static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14209 struct net_device *dev = tp->dev;
14210 struct pci_dev *pdev = tp->pdev;
14211 struct device_node *dp = pci_device_to_OF_node(pdev);
14212 const unsigned char *addr;
14213 int len;
14215 addr = of_get_property(dp, "local-mac-address", &len);
14216 if (addr && len == 6) {
14217 memcpy(dev->dev_addr, addr, 6);
14218 memcpy(dev->perm_addr, dev->dev_addr, 6);
14219 return 0;
14221 return -ENODEV;
14224 static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14226 struct net_device *dev = tp->dev;
14228 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
14229 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
14230 return 0;
14232 #endif
14234 static int __devinit tg3_get_device_address(struct tg3 *tp)
14236 struct net_device *dev = tp->dev;
14237 u32 hi, lo, mac_offset;
14238 int addr_ok = 0;
14240 #ifdef CONFIG_SPARC
14241 if (!tg3_get_macaddr_sparc(tp))
14242 return 0;
14243 #endif
14245 mac_offset = 0x7c;
14246 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
14247 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
14248 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14249 mac_offset = 0xcc;
14250 if (tg3_nvram_lock(tp))
14251 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14252 else
14253 tg3_nvram_unlock(tp);
14254 } else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
14255 if (PCI_FUNC(tp->pdev->devfn) & 1)
14256 mac_offset = 0xcc;
14257 if (PCI_FUNC(tp->pdev->devfn) > 1)
14258 mac_offset += 0x18c;
14259 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
14260 mac_offset = 0x10;
14262 /* First try to get it from MAC address mailbox. */
14263 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14264 if ((hi >> 16) == 0x484b) {
14265 dev->dev_addr[0] = (hi >> 8) & 0xff;
14266 dev->dev_addr[1] = (hi >> 0) & 0xff;
14268 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14269 dev->dev_addr[2] = (lo >> 24) & 0xff;
14270 dev->dev_addr[3] = (lo >> 16) & 0xff;
14271 dev->dev_addr[4] = (lo >> 8) & 0xff;
14272 dev->dev_addr[5] = (lo >> 0) & 0xff;
14274 /* Some old bootcode may report a 0 MAC address in SRAM */
14275 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14277 if (!addr_ok) {
14278 /* Next, try NVRAM. */
14279 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
14280 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
14281 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
14282 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14283 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
14285 /* Finally just fetch it out of the MAC control regs. */
14286 else {
14287 hi = tr32(MAC_ADDR_0_HIGH);
14288 lo = tr32(MAC_ADDR_0_LOW);
14290 dev->dev_addr[5] = lo & 0xff;
14291 dev->dev_addr[4] = (lo >> 8) & 0xff;
14292 dev->dev_addr[3] = (lo >> 16) & 0xff;
14293 dev->dev_addr[2] = (lo >> 24) & 0xff;
14294 dev->dev_addr[1] = hi & 0xff;
14295 dev->dev_addr[0] = (hi >> 8) & 0xff;
14299 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
14300 #ifdef CONFIG_SPARC
14301 if (!tg3_get_default_macaddr_sparc(tp))
14302 return 0;
14303 #endif
14304 return -EINVAL;
14306 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
14307 return 0;
14310 #define BOUNDARY_SINGLE_CACHELINE 1
14311 #define BOUNDARY_MULTI_CACHELINE 2
14313 static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14315 int cacheline_size;
14316 u8 byte;
14317 int goal;
14319 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14320 if (byte == 0)
14321 cacheline_size = 1024;
14322 else
14323 cacheline_size = (int) byte * 4;
14325 /* On 5703 and later chips, the boundary bits have no
14326 * effect.
14328 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14329 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14330 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
14331 goto out;
14333 #if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14334 goal = BOUNDARY_MULTI_CACHELINE;
14335 #else
14336 #if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14337 goal = BOUNDARY_SINGLE_CACHELINE;
14338 #else
14339 goal = 0;
14340 #endif
14341 #endif
14343 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
14344 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14345 goto out;
14348 if (!goal)
14349 goto out;
14351 /* PCI controllers on most RISC systems tend to disconnect
14352 * when a device tries to burst across a cache-line boundary.
14353 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14355 * Unfortunately, for PCI-E there are only limited
14356 * write-side controls for this, and thus for reads
14357 * we will still get the disconnects. We'll also waste
14358 * these PCI cycles for both read and write for chips
14359 * other than 5700 and 5701 which do not implement the
14360 * boundary bits.
14362 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
14363 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
14364 switch (cacheline_size) {
14365 case 16:
14366 case 32:
14367 case 64:
14368 case 128:
14369 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14370 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14371 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14372 } else {
14373 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14374 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14376 break;
14378 case 256:
14379 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14380 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14381 break;
14383 default:
14384 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14385 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14386 break;
14388 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14389 switch (cacheline_size) {
14390 case 16:
14391 case 32:
14392 case 64:
14393 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14394 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14395 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14396 break;
14398 /* fallthrough */
14399 case 128:
14400 default:
14401 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14402 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14403 break;
14405 } else {
14406 switch (cacheline_size) {
14407 case 16:
14408 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14409 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14410 DMA_RWCTRL_WRITE_BNDRY_16);
14411 break;
14413 /* fallthrough */
14414 case 32:
14415 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14416 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14417 DMA_RWCTRL_WRITE_BNDRY_32);
14418 break;
14420 /* fallthrough */
14421 case 64:
14422 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14423 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14424 DMA_RWCTRL_WRITE_BNDRY_64);
14425 break;
14427 /* fallthrough */
14428 case 128:
14429 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14430 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14431 DMA_RWCTRL_WRITE_BNDRY_128);
14432 break;
14434 /* fallthrough */
14435 case 256:
14436 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14437 DMA_RWCTRL_WRITE_BNDRY_256);
14438 break;
14439 case 512:
14440 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14441 DMA_RWCTRL_WRITE_BNDRY_512);
14442 break;
14443 case 1024:
14444 default:
14445 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14446 DMA_RWCTRL_WRITE_BNDRY_1024);
14447 break;
14451 out:
14452 return val;
14455 static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14457 struct tg3_internal_buffer_desc test_desc;
14458 u32 sram_dma_descs;
14459 int i, ret;
14461 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14463 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14464 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14465 tw32(RDMAC_STATUS, 0);
14466 tw32(WDMAC_STATUS, 0);
14468 tw32(BUFMGR_MODE, 0);
14469 tw32(FTQ_RESET, 0);
14471 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14472 test_desc.addr_lo = buf_dma & 0xffffffff;
14473 test_desc.nic_mbuf = 0x00002100;
14474 test_desc.len = size;
14477 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14478 * the *second* time the tg3 driver was getting loaded after an
14479 * initial scan.
14481 * Broadcom tells me:
14482 * ...the DMA engine is connected to the GRC block and a DMA
14483 * reset may affect the GRC block in some unpredictable way...
14484 * The behavior of resets to individual blocks has not been tested.
14486 * Broadcom noted the GRC reset will also reset all sub-components.
14488 if (to_device) {
14489 test_desc.cqid_sqid = (13 << 8) | 2;
14491 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14492 udelay(40);
14493 } else {
14494 test_desc.cqid_sqid = (16 << 8) | 7;
14496 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14497 udelay(40);
14499 test_desc.flags = 0x00000005;
14501 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14502 u32 val;
14504 val = *(((u32 *)&test_desc) + i);
14505 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14506 sram_dma_descs + (i * sizeof(u32)));
14507 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14509 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14511 if (to_device)
14512 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
14513 else
14514 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
14516 ret = -ENODEV;
14517 for (i = 0; i < 40; i++) {
14518 u32 val;
14520 if (to_device)
14521 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14522 else
14523 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14524 if ((val & 0xffff) == sram_dma_descs) {
14525 ret = 0;
14526 break;
14529 udelay(100);
14532 return ret;
14535 #define TEST_BUFFER_SIZE 0x2000
14537 static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
14538 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14539 { },
14542 static int __devinit tg3_test_dma(struct tg3 *tp)
14544 dma_addr_t buf_dma;
14545 u32 *buf, saved_dma_rwctrl;
14546 int ret = 0;
14548 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14549 &buf_dma, GFP_KERNEL);
14550 if (!buf) {
14551 ret = -ENOMEM;
14552 goto out_nofree;
14555 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14556 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14558 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
14560 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
14561 goto out;
14563 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14564 /* DMA read watermark not used on PCIE */
14565 tp->dma_rwctrl |= 0x00180000;
14566 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
14567 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14568 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
14569 tp->dma_rwctrl |= 0x003f0000;
14570 else
14571 tp->dma_rwctrl |= 0x003f000f;
14572 } else {
14573 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14574 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14575 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
14576 u32 read_water = 0x7;
14578 /* If the 5704 is behind the EPB bridge, we can
14579 * do the less restrictive ONE_DMA workaround for
14580 * better performance.
14582 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
14583 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14584 tp->dma_rwctrl |= 0x8000;
14585 else if (ccval == 0x6 || ccval == 0x7)
14586 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14588 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14589 read_water = 4;
14590 /* Set bit 23 to enable PCIX hw bug fix */
14591 tp->dma_rwctrl |=
14592 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14593 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14594 (1 << 23);
14595 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14596 /* 5780 always in PCIX mode */
14597 tp->dma_rwctrl |= 0x00144000;
14598 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14599 /* 5714 always in PCIX mode */
14600 tp->dma_rwctrl |= 0x00148000;
14601 } else {
14602 tp->dma_rwctrl |= 0x001b000f;
14606 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14607 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14608 tp->dma_rwctrl &= 0xfffffff0;
14610 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14611 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14612 /* Remove this if it causes problems for some boards. */
14613 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14615 /* On 5700/5701 chips, we need to set this bit.
14616 * Otherwise the chip will issue cacheline transactions
14617 * to streamable DMA memory with not all the byte
14618 * enables turned on. This is an error on several
14619 * RISC PCI controllers, in particular sparc64.
14621 * On 5703/5704 chips, this bit has been reassigned
14622 * a different meaning. In particular, it is used
14623 * on those chips to enable a PCI-X workaround.
14625 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14628 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14630 #if 0
14631 /* Unneeded, already done by tg3_get_invariants. */
14632 tg3_switch_clocks(tp);
14633 #endif
14635 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14636 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14637 goto out;
14639 /* It is best to perform DMA test with maximum write burst size
14640 * to expose the 5700/5701 write DMA bug.
14642 saved_dma_rwctrl = tp->dma_rwctrl;
14643 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14644 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14646 while (1) {
14647 u32 *p = buf, i;
14649 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14650 p[i] = i;
14652 /* Send the buffer to the chip. */
14653 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14654 if (ret) {
14655 dev_err(&tp->pdev->dev,
14656 "%s: Buffer write failed. err = %d\n",
14657 __func__, ret);
14658 break;
14661 #if 0
14662 /* validate data reached card RAM correctly. */
14663 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14664 u32 val;
14665 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14666 if (le32_to_cpu(val) != p[i]) {
14667 dev_err(&tp->pdev->dev,
14668 "%s: Buffer corrupted on device! "
14669 "(%d != %d)\n", __func__, val, i);
14670 /* ret = -ENODEV here? */
14672 p[i] = 0;
14674 #endif
14675 /* Now read it back. */
14676 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14677 if (ret) {
14678 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14679 "err = %d\n", __func__, ret);
14680 break;
14683 /* Verify it. */
14684 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14685 if (p[i] == i)
14686 continue;
14688 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14689 DMA_RWCTRL_WRITE_BNDRY_16) {
14690 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14691 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14692 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14693 break;
14694 } else {
14695 dev_err(&tp->pdev->dev,
14696 "%s: Buffer corrupted on read back! "
14697 "(%d != %d)\n", __func__, p[i], i);
14698 ret = -ENODEV;
14699 goto out;
14703 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14704 /* Success. */
14705 ret = 0;
14706 break;
14709 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14710 DMA_RWCTRL_WRITE_BNDRY_16) {
14712 /* DMA test passed without adjusting DMA boundary,
14713 * now look for chipsets that are known to expose the
14714 * DMA bug without failing the test.
14716 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
14717 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14718 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14719 } else {
14720 /* Safe to use the calculated DMA boundary. */
14721 tp->dma_rwctrl = saved_dma_rwctrl;
14724 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14727 out:
14728 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
14729 out_nofree:
14730 return ret;
14733 static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14735 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
14736 tp->bufmgr_config.mbuf_read_dma_low_water =
14737 DEFAULT_MB_RDMA_LOW_WATER_5705;
14738 tp->bufmgr_config.mbuf_mac_rx_low_water =
14739 DEFAULT_MB_MACRX_LOW_WATER_57765;
14740 tp->bufmgr_config.mbuf_high_water =
14741 DEFAULT_MB_HIGH_WATER_57765;
14743 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14744 DEFAULT_MB_RDMA_LOW_WATER_5705;
14745 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14746 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14747 tp->bufmgr_config.mbuf_high_water_jumbo =
14748 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14749 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14750 tp->bufmgr_config.mbuf_read_dma_low_water =
14751 DEFAULT_MB_RDMA_LOW_WATER_5705;
14752 tp->bufmgr_config.mbuf_mac_rx_low_water =
14753 DEFAULT_MB_MACRX_LOW_WATER_5705;
14754 tp->bufmgr_config.mbuf_high_water =
14755 DEFAULT_MB_HIGH_WATER_5705;
14756 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14757 tp->bufmgr_config.mbuf_mac_rx_low_water =
14758 DEFAULT_MB_MACRX_LOW_WATER_5906;
14759 tp->bufmgr_config.mbuf_high_water =
14760 DEFAULT_MB_HIGH_WATER_5906;
14763 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14764 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14765 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14766 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14767 tp->bufmgr_config.mbuf_high_water_jumbo =
14768 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14769 } else {
14770 tp->bufmgr_config.mbuf_read_dma_low_water =
14771 DEFAULT_MB_RDMA_LOW_WATER;
14772 tp->bufmgr_config.mbuf_mac_rx_low_water =
14773 DEFAULT_MB_MACRX_LOW_WATER;
14774 tp->bufmgr_config.mbuf_high_water =
14775 DEFAULT_MB_HIGH_WATER;
14777 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14778 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14779 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14780 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14781 tp->bufmgr_config.mbuf_high_water_jumbo =
14782 DEFAULT_MB_HIGH_WATER_JUMBO;
14785 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14786 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14789 static char * __devinit tg3_phy_string(struct tg3 *tp)
14791 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14792 case TG3_PHY_ID_BCM5400: return "5400";
14793 case TG3_PHY_ID_BCM5401: return "5401";
14794 case TG3_PHY_ID_BCM5411: return "5411";
14795 case TG3_PHY_ID_BCM5701: return "5701";
14796 case TG3_PHY_ID_BCM5703: return "5703";
14797 case TG3_PHY_ID_BCM5704: return "5704";
14798 case TG3_PHY_ID_BCM5705: return "5705";
14799 case TG3_PHY_ID_BCM5750: return "5750";
14800 case TG3_PHY_ID_BCM5752: return "5752";
14801 case TG3_PHY_ID_BCM5714: return "5714";
14802 case TG3_PHY_ID_BCM5780: return "5780";
14803 case TG3_PHY_ID_BCM5755: return "5755";
14804 case TG3_PHY_ID_BCM5787: return "5787";
14805 case TG3_PHY_ID_BCM5784: return "5784";
14806 case TG3_PHY_ID_BCM5756: return "5722/5756";
14807 case TG3_PHY_ID_BCM5906: return "5906";
14808 case TG3_PHY_ID_BCM5761: return "5761";
14809 case TG3_PHY_ID_BCM5718C: return "5718C";
14810 case TG3_PHY_ID_BCM5718S: return "5718S";
14811 case TG3_PHY_ID_BCM57765: return "57765";
14812 case TG3_PHY_ID_BCM5719C: return "5719C";
14813 case TG3_PHY_ID_BCM5720C: return "5720C";
14814 case TG3_PHY_ID_BCM8002: return "8002/serdes";
14815 case 0: return "serdes";
14816 default: return "unknown";
14820 static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14822 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14823 strcpy(str, "PCI Express");
14824 return str;
14825 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14826 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14828 strcpy(str, "PCIX:");
14830 if ((clock_ctrl == 7) ||
14831 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14832 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14833 strcat(str, "133MHz");
14834 else if (clock_ctrl == 0)
14835 strcat(str, "33MHz");
14836 else if (clock_ctrl == 2)
14837 strcat(str, "50MHz");
14838 else if (clock_ctrl == 4)
14839 strcat(str, "66MHz");
14840 else if (clock_ctrl == 6)
14841 strcat(str, "100MHz");
14842 } else {
14843 strcpy(str, "PCI:");
14844 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14845 strcat(str, "66MHz");
14846 else
14847 strcat(str, "33MHz");
14849 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14850 strcat(str, ":32-bit");
14851 else
14852 strcat(str, ":64-bit");
14853 return str;
14856 static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
14858 struct pci_dev *peer;
14859 unsigned int func, devnr = tp->pdev->devfn & ~7;
14861 for (func = 0; func < 8; func++) {
14862 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14863 if (peer && peer != tp->pdev)
14864 break;
14865 pci_dev_put(peer);
14867 /* 5704 can be configured in single-port mode, set peer to
14868 * tp->pdev in that case.
14870 if (!peer) {
14871 peer = tp->pdev;
14872 return peer;
14876 * We don't need to keep the refcount elevated; there's no way
14877 * to remove one half of this device without removing the other
14879 pci_dev_put(peer);
14881 return peer;
14884 static void __devinit tg3_init_coal(struct tg3 *tp)
14886 struct ethtool_coalesce *ec = &tp->coal;
14888 memset(ec, 0, sizeof(*ec));
14889 ec->cmd = ETHTOOL_GCOALESCE;
14890 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14891 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14892 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14893 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14894 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14895 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14896 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14897 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14898 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14900 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14901 HOSTCC_MODE_CLRTICK_TXBD)) {
14902 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14903 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14904 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14905 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14908 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14909 ec->rx_coalesce_usecs_irq = 0;
14910 ec->tx_coalesce_usecs_irq = 0;
14911 ec->stats_block_coalesce_usecs = 0;
14915 static const struct net_device_ops tg3_netdev_ops = {
14916 .ndo_open = tg3_open,
14917 .ndo_stop = tg3_close,
14918 .ndo_start_xmit = tg3_start_xmit,
14919 .ndo_get_stats64 = tg3_get_stats64,
14920 .ndo_validate_addr = eth_validate_addr,
14921 .ndo_set_multicast_list = tg3_set_rx_mode,
14922 .ndo_set_mac_address = tg3_set_mac_addr,
14923 .ndo_do_ioctl = tg3_ioctl,
14924 .ndo_tx_timeout = tg3_tx_timeout,
14925 .ndo_change_mtu = tg3_change_mtu,
14926 .ndo_fix_features = tg3_fix_features,
14927 #ifdef CONFIG_NET_POLL_CONTROLLER
14928 .ndo_poll_controller = tg3_poll_controller,
14929 #endif
14932 static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14933 .ndo_open = tg3_open,
14934 .ndo_stop = tg3_close,
14935 .ndo_start_xmit = tg3_start_xmit_dma_bug,
14936 .ndo_get_stats64 = tg3_get_stats64,
14937 .ndo_validate_addr = eth_validate_addr,
14938 .ndo_set_multicast_list = tg3_set_rx_mode,
14939 .ndo_set_mac_address = tg3_set_mac_addr,
14940 .ndo_do_ioctl = tg3_ioctl,
14941 .ndo_tx_timeout = tg3_tx_timeout,
14942 .ndo_change_mtu = tg3_change_mtu,
14943 #ifdef CONFIG_NET_POLL_CONTROLLER
14944 .ndo_poll_controller = tg3_poll_controller,
14945 #endif
14948 static int __devinit tg3_init_one(struct pci_dev *pdev,
14949 const struct pci_device_id *ent)
14951 struct net_device *dev;
14952 struct tg3 *tp;
14953 int i, err, pm_cap;
14954 u32 sndmbx, rcvmbx, intmbx;
14955 char str[40];
14956 u64 dma_mask, persist_dma_mask;
14957 u32 hw_features = 0;
14959 printk_once(KERN_INFO "%s\n", version);
14961 err = pci_enable_device(pdev);
14962 if (err) {
14963 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14964 return err;
14967 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14968 if (err) {
14969 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14970 goto err_out_disable_pdev;
14973 pci_set_master(pdev);
14975 /* Find power-management capability. */
14976 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14977 if (pm_cap == 0) {
14978 dev_err(&pdev->dev,
14979 "Cannot find Power Management capability, aborting\n");
14980 err = -EIO;
14981 goto err_out_free_res;
14984 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
14985 if (!dev) {
14986 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
14987 err = -ENOMEM;
14988 goto err_out_free_res;
14991 SET_NETDEV_DEV(dev, &pdev->dev);
14993 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
14995 tp = netdev_priv(dev);
14996 tp->pdev = pdev;
14997 tp->dev = dev;
14998 tp->pm_cap = pm_cap;
14999 tp->rx_mode = TG3_DEF_RX_MODE;
15000 tp->tx_mode = TG3_DEF_TX_MODE;
15002 if (tg3_debug > 0)
15003 tp->msg_enable = tg3_debug;
15004 else
15005 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15007 /* The word/byte swap controls here control register access byte
15008 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15009 * setting below.
15011 tp->misc_host_ctrl =
15012 MISC_HOST_CTRL_MASK_PCI_INT |
15013 MISC_HOST_CTRL_WORD_SWAP |
15014 MISC_HOST_CTRL_INDIR_ACCESS |
15015 MISC_HOST_CTRL_PCISTATE_RW;
15017 /* The NONFRM (non-frame) byte/word swap controls take effect
15018 * on descriptor entries, anything which isn't packet data.
15020 * The StrongARM chips on the board (one for tx, one for rx)
15021 * are running in big-endian mode.
15023 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15024 GRC_MODE_WSWAP_NONFRM_DATA);
15025 #ifdef __BIG_ENDIAN
15026 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15027 #endif
15028 spin_lock_init(&tp->lock);
15029 spin_lock_init(&tp->indirect_lock);
15030 INIT_WORK(&tp->reset_task, tg3_reset_task);
15032 tp->regs = pci_ioremap_bar(pdev, BAR_0);
15033 if (!tp->regs) {
15034 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15035 err = -ENOMEM;
15036 goto err_out_free_dev;
15039 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15040 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
15042 dev->ethtool_ops = &tg3_ethtool_ops;
15043 dev->watchdog_timeo = TG3_TX_TIMEOUT;
15044 dev->irq = pdev->irq;
15046 err = tg3_get_invariants(tp);
15047 if (err) {
15048 dev_err(&pdev->dev,
15049 "Problem fetching invariants of chip, aborting\n");
15050 goto err_out_iounmap;
15053 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
15054 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
15055 dev->netdev_ops = &tg3_netdev_ops;
15056 else
15057 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
15060 /* The EPB bridge inside 5714, 5715, and 5780 and any
15061 * device behind the EPB cannot support DMA addresses > 40-bit.
15062 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15063 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15064 * do DMA address check in tg3_start_xmit().
15066 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
15067 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
15068 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
15069 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
15070 #ifdef CONFIG_HIGHMEM
15071 dma_mask = DMA_BIT_MASK(64);
15072 #endif
15073 } else
15074 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
15076 /* Configure DMA attributes. */
15077 if (dma_mask > DMA_BIT_MASK(32)) {
15078 err = pci_set_dma_mask(pdev, dma_mask);
15079 if (!err) {
15080 dev->features |= NETIF_F_HIGHDMA;
15081 err = pci_set_consistent_dma_mask(pdev,
15082 persist_dma_mask);
15083 if (err < 0) {
15084 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15085 "DMA for consistent allocations\n");
15086 goto err_out_iounmap;
15090 if (err || dma_mask == DMA_BIT_MASK(32)) {
15091 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
15092 if (err) {
15093 dev_err(&pdev->dev,
15094 "No usable DMA configuration, aborting\n");
15095 goto err_out_iounmap;
15099 tg3_init_bufmgr_config(tp);
15101 /* Selectively allow TSO based on operating conditions */
15102 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
15103 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
15104 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
15105 else {
15106 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
15107 tp->fw_needed = NULL;
15110 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
15111 tp->fw_needed = FIRMWARE_TG3;
15113 /* TSO is on by default on chips that support hardware TSO.
15114 * Firmware TSO on older chips gives lower performance, so it
15115 * is off by default, but can be enabled using ethtool.
15117 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
15118 (dev->features & NETIF_F_IP_CSUM))
15119 hw_features |= NETIF_F_TSO;
15120 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
15121 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
15122 if (dev->features & NETIF_F_IPV6_CSUM)
15123 hw_features |= NETIF_F_TSO6;
15124 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
15125 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
15126 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15127 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
15128 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
15129 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
15130 hw_features |= NETIF_F_TSO_ECN;
15133 dev->hw_features |= hw_features;
15134 dev->features |= hw_features;
15135 dev->vlan_features |= hw_features;
15137 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
15138 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
15139 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
15140 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
15141 tp->rx_pending = 63;
15144 err = tg3_get_device_address(tp);
15145 if (err) {
15146 dev_err(&pdev->dev,
15147 "Could not obtain valid ethernet address, aborting\n");
15148 goto err_out_iounmap;
15151 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
15152 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15153 if (!tp->aperegs) {
15154 dev_err(&pdev->dev,
15155 "Cannot map APE registers, aborting\n");
15156 err = -ENOMEM;
15157 goto err_out_iounmap;
15160 tg3_ape_lock_init(tp);
15162 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
15163 tg3_read_dash_ver(tp);
15167 * Reset chip in case UNDI or EFI driver did not shutdown
15168 * DMA self test will enable WDMAC and we'll see (spurious)
15169 * pending DMA on the PCI bus at that point.
15171 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15172 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15173 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15174 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15177 err = tg3_test_dma(tp);
15178 if (err) {
15179 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
15180 goto err_out_apeunmap;
15183 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15184 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15185 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
15186 for (i = 0; i < tp->irq_max; i++) {
15187 struct tg3_napi *tnapi = &tp->napi[i];
15189 tnapi->tp = tp;
15190 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15192 tnapi->int_mbox = intmbx;
15193 if (i < 4)
15194 intmbx += 0x8;
15195 else
15196 intmbx += 0x4;
15198 tnapi->consmbox = rcvmbx;
15199 tnapi->prodmbox = sndmbx;
15201 if (i)
15202 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
15203 else
15204 tnapi->coal_now = HOSTCC_MODE_NOW;
15206 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
15207 break;
15210 * If we support MSIX, we'll be using RSS. If we're using
15211 * RSS, the first vector only handles link interrupts and the
15212 * remaining vectors handle rx and tx interrupts. Reuse the
15213 * mailbox values for the next iteration. The values we setup
15214 * above are still useful for the single vectored mode.
15216 if (!i)
15217 continue;
15219 rcvmbx += 0x8;
15221 if (sndmbx & 0x4)
15222 sndmbx -= 0x4;
15223 else
15224 sndmbx += 0xc;
15227 tg3_init_coal(tp);
15229 pci_set_drvdata(pdev, dev);
15231 err = register_netdev(dev);
15232 if (err) {
15233 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
15234 goto err_out_apeunmap;
15237 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15238 tp->board_part_number,
15239 tp->pci_chip_rev_id,
15240 tg3_bus_string(tp, str),
15241 dev->dev_addr);
15243 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
15244 struct phy_device *phydev;
15245 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
15246 netdev_info(dev,
15247 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
15248 phydev->drv->name, dev_name(&phydev->dev));
15249 } else {
15250 char *ethtype;
15252 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15253 ethtype = "10/100Base-TX";
15254 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15255 ethtype = "1000Base-SX";
15256 else
15257 ethtype = "10/100/1000Base-T";
15259 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
15260 "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
15261 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
15264 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
15265 (dev->features & NETIF_F_RXCSUM) != 0,
15266 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
15267 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
15268 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
15269 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
15270 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15271 tp->dma_rwctrl,
15272 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15273 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
15275 return 0;
15277 err_out_apeunmap:
15278 if (tp->aperegs) {
15279 iounmap(tp->aperegs);
15280 tp->aperegs = NULL;
15283 err_out_iounmap:
15284 if (tp->regs) {
15285 iounmap(tp->regs);
15286 tp->regs = NULL;
15289 err_out_free_dev:
15290 free_netdev(dev);
15292 err_out_free_res:
15293 pci_release_regions(pdev);
15295 err_out_disable_pdev:
15296 pci_disable_device(pdev);
15297 pci_set_drvdata(pdev, NULL);
15298 return err;
15301 static void __devexit tg3_remove_one(struct pci_dev *pdev)
15303 struct net_device *dev = pci_get_drvdata(pdev);
15305 if (dev) {
15306 struct tg3 *tp = netdev_priv(dev);
15308 if (tp->fw)
15309 release_firmware(tp->fw);
15311 cancel_work_sync(&tp->reset_task);
15313 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
15314 tg3_phy_fini(tp);
15315 tg3_mdio_fini(tp);
15318 unregister_netdev(dev);
15319 if (tp->aperegs) {
15320 iounmap(tp->aperegs);
15321 tp->aperegs = NULL;
15323 if (tp->regs) {
15324 iounmap(tp->regs);
15325 tp->regs = NULL;
15327 free_netdev(dev);
15328 pci_release_regions(pdev);
15329 pci_disable_device(pdev);
15330 pci_set_drvdata(pdev, NULL);
15334 #ifdef CONFIG_PM_SLEEP
15335 static int tg3_suspend(struct device *device)
15337 struct pci_dev *pdev = to_pci_dev(device);
15338 struct net_device *dev = pci_get_drvdata(pdev);
15339 struct tg3 *tp = netdev_priv(dev);
15340 int err;
15342 if (!netif_running(dev))
15343 return 0;
15345 flush_work_sync(&tp->reset_task);
15346 tg3_phy_stop(tp);
15347 tg3_netif_stop(tp);
15349 del_timer_sync(&tp->timer);
15351 tg3_full_lock(tp, 1);
15352 tg3_disable_ints(tp);
15353 tg3_full_unlock(tp);
15355 netif_device_detach(dev);
15357 tg3_full_lock(tp, 0);
15358 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15359 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
15360 tg3_full_unlock(tp);
15362 err = tg3_power_down_prepare(tp);
15363 if (err) {
15364 int err2;
15366 tg3_full_lock(tp, 0);
15368 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
15369 err2 = tg3_restart_hw(tp, 1);
15370 if (err2)
15371 goto out;
15373 tp->timer.expires = jiffies + tp->timer_offset;
15374 add_timer(&tp->timer);
15376 netif_device_attach(dev);
15377 tg3_netif_start(tp);
15379 out:
15380 tg3_full_unlock(tp);
15382 if (!err2)
15383 tg3_phy_start(tp);
15386 return err;
15389 static int tg3_resume(struct device *device)
15391 struct pci_dev *pdev = to_pci_dev(device);
15392 struct net_device *dev = pci_get_drvdata(pdev);
15393 struct tg3 *tp = netdev_priv(dev);
15394 int err;
15396 if (!netif_running(dev))
15397 return 0;
15399 netif_device_attach(dev);
15401 tg3_full_lock(tp, 0);
15403 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
15404 err = tg3_restart_hw(tp, 1);
15405 if (err)
15406 goto out;
15408 tp->timer.expires = jiffies + tp->timer_offset;
15409 add_timer(&tp->timer);
15411 tg3_netif_start(tp);
15413 out:
15414 tg3_full_unlock(tp);
15416 if (!err)
15417 tg3_phy_start(tp);
15419 return err;
15422 static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
15423 #define TG3_PM_OPS (&tg3_pm_ops)
15425 #else
15427 #define TG3_PM_OPS NULL
15429 #endif /* CONFIG_PM_SLEEP */
15431 static struct pci_driver tg3_driver = {
15432 .name = DRV_MODULE_NAME,
15433 .id_table = tg3_pci_tbl,
15434 .probe = tg3_init_one,
15435 .remove = __devexit_p(tg3_remove_one),
15436 .driver.pm = TG3_PM_OPS,
15439 static int __init tg3_init(void)
15441 return pci_register_driver(&tg3_driver);
15444 static void __exit tg3_cleanup(void)
15446 pci_unregister_driver(&tg3_driver);
15449 module_init(tg3_init);
15450 module_exit(tg3_cleanup);