tg3: Disable multivec mode for 1 MSIX vector
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / tg3.c
blobb2a16b4e3caf67e7ea30f451f5c47a7ac41db4f2
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-2010 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 <asm/io.h>
52 #include <asm/byteorder.h>
53 #include <asm/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 116
68 #define DRV_MODULE_VERSION \
69 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
70 #define DRV_MODULE_RELDATE "December 3, 2010"
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 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 || \
101 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) ? \
102 RX_STD_MAX_SIZE_5717 : 512)
103 #define TG3_DEF_RX_RING_PENDING 200
104 #define TG3_RX_JMB_RING_SIZE(tp) \
105 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 || \
106 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) ? \
107 1024 : 256)
108 #define TG3_DEF_RX_JUMBO_RING_PENDING 100
109 #define TG3_RSS_INDIR_TBL_SIZE 128
111 /* Do not place this n-ring entries value into the tp struct itself,
112 * we really want to expose these constants to GCC so that modulo et
113 * al. operations are done with shifts and masks instead of with
114 * hw multiply/modulo instructions. Another solution would be to
115 * replace things like '% foo' with '& (foo - 1)'.
118 #define TG3_TX_RING_SIZE 512
119 #define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
121 #define TG3_RX_STD_RING_BYTES(tp) \
122 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
123 #define TG3_RX_JMB_RING_BYTES(tp) \
124 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
125 #define TG3_RX_RCB_RING_BYTES(tp) \
126 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
127 #define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
128 TG3_TX_RING_SIZE)
129 #define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
131 #define TG3_DMA_BYTE_ENAB 64
133 #define TG3_RX_STD_DMA_SZ 1536
134 #define TG3_RX_JMB_DMA_SZ 9046
136 #define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
138 #define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
139 #define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
141 #define TG3_RX_STD_BUFF_RING_SIZE(tp) \
142 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
144 #define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
145 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
147 /* Due to a hardware bug, the 5701 can only DMA to memory addresses
148 * that are at least dword aligned when used in PCIX mode. The driver
149 * works around this bug by double copying the packet. This workaround
150 * is built into the normal double copy length check for efficiency.
152 * However, the double copy is only necessary on those architectures
153 * where unaligned memory accesses are inefficient. For those architectures
154 * where unaligned memory accesses incur little penalty, we can reintegrate
155 * the 5701 in the normal rx path. Doing so saves a device structure
156 * dereference by hardcoding the double copy threshold in place.
158 #define TG3_RX_COPY_THRESHOLD 256
159 #if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
160 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
161 #else
162 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
163 #endif
165 /* minimum number of free TX descriptors required to wake up TX process */
166 #define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
168 #define TG3_RAW_IP_ALIGN 2
170 /* number of ETHTOOL_GSTATS u64's */
171 #define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
173 #define TG3_NUM_TEST 6
175 #define TG3_FW_UPDATE_TIMEOUT_SEC 5
177 #define FIRMWARE_TG3 "tigon/tg3.bin"
178 #define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
179 #define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
181 static char version[] __devinitdata =
182 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
184 MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
185 MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
186 MODULE_LICENSE("GPL");
187 MODULE_VERSION(DRV_MODULE_VERSION);
188 MODULE_FIRMWARE(FIRMWARE_TG3);
189 MODULE_FIRMWARE(FIRMWARE_TG3TSO);
190 MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
192 static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
193 module_param(tg3_debug, int, 0);
194 MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
196 static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
269 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
270 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
271 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
272 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
273 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
274 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
275 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
279 MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
281 static const struct {
282 const char string[ETH_GSTRING_LEN];
283 } ethtool_stats_keys[TG3_NUM_STATS] = {
284 { "rx_octets" },
285 { "rx_fragments" },
286 { "rx_ucast_packets" },
287 { "rx_mcast_packets" },
288 { "rx_bcast_packets" },
289 { "rx_fcs_errors" },
290 { "rx_align_errors" },
291 { "rx_xon_pause_rcvd" },
292 { "rx_xoff_pause_rcvd" },
293 { "rx_mac_ctrl_rcvd" },
294 { "rx_xoff_entered" },
295 { "rx_frame_too_long_errors" },
296 { "rx_jabbers" },
297 { "rx_undersize_packets" },
298 { "rx_in_length_errors" },
299 { "rx_out_length_errors" },
300 { "rx_64_or_less_octet_packets" },
301 { "rx_65_to_127_octet_packets" },
302 { "rx_128_to_255_octet_packets" },
303 { "rx_256_to_511_octet_packets" },
304 { "rx_512_to_1023_octet_packets" },
305 { "rx_1024_to_1522_octet_packets" },
306 { "rx_1523_to_2047_octet_packets" },
307 { "rx_2048_to_4095_octet_packets" },
308 { "rx_4096_to_8191_octet_packets" },
309 { "rx_8192_to_9022_octet_packets" },
311 { "tx_octets" },
312 { "tx_collisions" },
314 { "tx_xon_sent" },
315 { "tx_xoff_sent" },
316 { "tx_flow_control" },
317 { "tx_mac_errors" },
318 { "tx_single_collisions" },
319 { "tx_mult_collisions" },
320 { "tx_deferred" },
321 { "tx_excessive_collisions" },
322 { "tx_late_collisions" },
323 { "tx_collide_2times" },
324 { "tx_collide_3times" },
325 { "tx_collide_4times" },
326 { "tx_collide_5times" },
327 { "tx_collide_6times" },
328 { "tx_collide_7times" },
329 { "tx_collide_8times" },
330 { "tx_collide_9times" },
331 { "tx_collide_10times" },
332 { "tx_collide_11times" },
333 { "tx_collide_12times" },
334 { "tx_collide_13times" },
335 { "tx_collide_14times" },
336 { "tx_collide_15times" },
337 { "tx_ucast_packets" },
338 { "tx_mcast_packets" },
339 { "tx_bcast_packets" },
340 { "tx_carrier_sense_errors" },
341 { "tx_discards" },
342 { "tx_errors" },
344 { "dma_writeq_full" },
345 { "dma_write_prioq_full" },
346 { "rxbds_empty" },
347 { "rx_discards" },
348 { "rx_errors" },
349 { "rx_threshold_hit" },
351 { "dma_readq_full" },
352 { "dma_read_prioq_full" },
353 { "tx_comp_queue_full" },
355 { "ring_set_send_prod_index" },
356 { "ring_status_update" },
357 { "nic_irqs" },
358 { "nic_avoided_irqs" },
359 { "nic_tx_threshold_hit" }
362 static const struct {
363 const char string[ETH_GSTRING_LEN];
364 } ethtool_test_keys[TG3_NUM_TEST] = {
365 { "nvram test (online) " },
366 { "link test (online) " },
367 { "register test (offline)" },
368 { "memory test (offline)" },
369 { "loopback test (offline)" },
370 { "interrupt test (offline)" },
373 static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
375 writel(val, tp->regs + off);
378 static u32 tg3_read32(struct tg3 *tp, u32 off)
380 return readl(tp->regs + off);
383 static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
385 writel(val, tp->aperegs + off);
388 static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
390 return readl(tp->aperegs + off);
393 static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
395 unsigned long flags;
397 spin_lock_irqsave(&tp->indirect_lock, flags);
398 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
399 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
400 spin_unlock_irqrestore(&tp->indirect_lock, flags);
403 static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
405 writel(val, tp->regs + off);
406 readl(tp->regs + off);
409 static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
411 unsigned long flags;
412 u32 val;
414 spin_lock_irqsave(&tp->indirect_lock, flags);
415 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
416 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
417 spin_unlock_irqrestore(&tp->indirect_lock, flags);
418 return val;
421 static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
423 unsigned long flags;
425 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
426 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
427 TG3_64BIT_REG_LOW, val);
428 return;
430 if (off == TG3_RX_STD_PROD_IDX_REG) {
431 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
432 TG3_64BIT_REG_LOW, val);
433 return;
436 spin_lock_irqsave(&tp->indirect_lock, flags);
437 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
438 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
439 spin_unlock_irqrestore(&tp->indirect_lock, flags);
441 /* In indirect mode when disabling interrupts, we also need
442 * to clear the interrupt bit in the GRC local ctrl register.
444 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
445 (val == 0x1)) {
446 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
447 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
451 static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
453 unsigned long flags;
454 u32 val;
456 spin_lock_irqsave(&tp->indirect_lock, flags);
457 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
458 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
459 spin_unlock_irqrestore(&tp->indirect_lock, flags);
460 return val;
463 /* usec_wait specifies the wait time in usec when writing to certain registers
464 * where it is unsafe to read back the register without some delay.
465 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
466 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
468 static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
470 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
471 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
472 /* Non-posted methods */
473 tp->write32(tp, off, val);
474 else {
475 /* Posted method */
476 tg3_write32(tp, off, val);
477 if (usec_wait)
478 udelay(usec_wait);
479 tp->read32(tp, off);
481 /* Wait again after the read for the posted method to guarantee that
482 * the wait time is met.
484 if (usec_wait)
485 udelay(usec_wait);
488 static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
490 tp->write32_mbox(tp, off, val);
491 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
492 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
493 tp->read32_mbox(tp, off);
496 static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
498 void __iomem *mbox = tp->regs + off;
499 writel(val, mbox);
500 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
501 writel(val, mbox);
502 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
503 readl(mbox);
506 static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
508 return readl(tp->regs + off + GRCMBOX_BASE);
511 static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
513 writel(val, tp->regs + off + GRCMBOX_BASE);
516 #define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
517 #define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
518 #define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
519 #define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
520 #define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
522 #define tw32(reg, val) tp->write32(tp, reg, val)
523 #define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
524 #define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
525 #define tr32(reg) tp->read32(tp, reg)
527 static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
529 unsigned long flags;
531 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
532 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
533 return;
535 spin_lock_irqsave(&tp->indirect_lock, flags);
536 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
537 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
538 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
540 /* Always leave this as zero. */
541 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
542 } else {
543 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
544 tw32_f(TG3PCI_MEM_WIN_DATA, val);
546 /* Always leave this as zero. */
547 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
549 spin_unlock_irqrestore(&tp->indirect_lock, flags);
552 static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
554 unsigned long flags;
556 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
557 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
558 *val = 0;
559 return;
562 spin_lock_irqsave(&tp->indirect_lock, flags);
563 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
564 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
565 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
567 /* Always leave this as zero. */
568 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
569 } else {
570 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
571 *val = tr32(TG3PCI_MEM_WIN_DATA);
573 /* Always leave this as zero. */
574 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
576 spin_unlock_irqrestore(&tp->indirect_lock, flags);
579 static void tg3_ape_lock_init(struct tg3 *tp)
581 int i;
582 u32 regbase;
584 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
585 regbase = TG3_APE_LOCK_GRANT;
586 else
587 regbase = TG3_APE_PER_LOCK_GRANT;
589 /* Make sure the driver hasn't any stale locks. */
590 for (i = 0; i < 8; i++)
591 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
594 static int tg3_ape_lock(struct tg3 *tp, int locknum)
596 int i, off;
597 int ret = 0;
598 u32 status, req, gnt;
600 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
601 return 0;
603 switch (locknum) {
604 case TG3_APE_LOCK_GRC:
605 case TG3_APE_LOCK_MEM:
606 break;
607 default:
608 return -EINVAL;
611 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
612 req = TG3_APE_LOCK_REQ;
613 gnt = TG3_APE_LOCK_GRANT;
614 } else {
615 req = TG3_APE_PER_LOCK_REQ;
616 gnt = TG3_APE_PER_LOCK_GRANT;
619 off = 4 * locknum;
621 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
623 /* Wait for up to 1 millisecond to acquire lock. */
624 for (i = 0; i < 100; i++) {
625 status = tg3_ape_read32(tp, gnt + off);
626 if (status == APE_LOCK_GRANT_DRIVER)
627 break;
628 udelay(10);
631 if (status != APE_LOCK_GRANT_DRIVER) {
632 /* Revoke the lock request. */
633 tg3_ape_write32(tp, gnt + off,
634 APE_LOCK_GRANT_DRIVER);
636 ret = -EBUSY;
639 return ret;
642 static void tg3_ape_unlock(struct tg3 *tp, int locknum)
644 u32 gnt;
646 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
647 return;
649 switch (locknum) {
650 case TG3_APE_LOCK_GRC:
651 case TG3_APE_LOCK_MEM:
652 break;
653 default:
654 return;
657 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
658 gnt = TG3_APE_LOCK_GRANT;
659 else
660 gnt = TG3_APE_PER_LOCK_GRANT;
662 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
665 static void tg3_disable_ints(struct tg3 *tp)
667 int i;
669 tw32(TG3PCI_MISC_HOST_CTRL,
670 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
671 for (i = 0; i < tp->irq_max; i++)
672 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
675 static void tg3_enable_ints(struct tg3 *tp)
677 int i;
679 tp->irq_sync = 0;
680 wmb();
682 tw32(TG3PCI_MISC_HOST_CTRL,
683 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
685 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
686 for (i = 0; i < tp->irq_cnt; i++) {
687 struct tg3_napi *tnapi = &tp->napi[i];
689 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
690 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
691 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
693 tp->coal_now |= tnapi->coal_now;
696 /* Force an initial interrupt */
697 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
698 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
699 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
700 else
701 tw32(HOSTCC_MODE, tp->coal_now);
703 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
706 static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
708 struct tg3 *tp = tnapi->tp;
709 struct tg3_hw_status *sblk = tnapi->hw_status;
710 unsigned int work_exists = 0;
712 /* check for phy events */
713 if (!(tp->tg3_flags &
714 (TG3_FLAG_USE_LINKCHG_REG |
715 TG3_FLAG_POLL_SERDES))) {
716 if (sblk->status & SD_STATUS_LINK_CHG)
717 work_exists = 1;
719 /* check for RX/TX work to do */
720 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
721 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
722 work_exists = 1;
724 return work_exists;
727 /* tg3_int_reenable
728 * similar to tg3_enable_ints, but it accurately determines whether there
729 * is new work pending and can return without flushing the PIO write
730 * which reenables interrupts
732 static void tg3_int_reenable(struct tg3_napi *tnapi)
734 struct tg3 *tp = tnapi->tp;
736 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
737 mmiowb();
739 /* When doing tagged status, this work check is unnecessary.
740 * The last_tag we write above tells the chip which piece of
741 * work we've completed.
743 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
744 tg3_has_work(tnapi))
745 tw32(HOSTCC_MODE, tp->coalesce_mode |
746 HOSTCC_MODE_ENABLE | tnapi->coal_now);
749 static void tg3_switch_clocks(struct tg3 *tp)
751 u32 clock_ctrl;
752 u32 orig_clock_ctrl;
754 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
755 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
756 return;
758 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
760 orig_clock_ctrl = clock_ctrl;
761 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
762 CLOCK_CTRL_CLKRUN_OENABLE |
763 0x1f);
764 tp->pci_clock_ctrl = clock_ctrl;
766 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
767 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
768 tw32_wait_f(TG3PCI_CLOCK_CTRL,
769 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
771 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
772 tw32_wait_f(TG3PCI_CLOCK_CTRL,
773 clock_ctrl |
774 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
775 40);
776 tw32_wait_f(TG3PCI_CLOCK_CTRL,
777 clock_ctrl | (CLOCK_CTRL_ALTCLK),
778 40);
780 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
783 #define PHY_BUSY_LOOPS 5000
785 static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
787 u32 frame_val;
788 unsigned int loops;
789 int ret;
791 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
792 tw32_f(MAC_MI_MODE,
793 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
794 udelay(80);
797 *val = 0x0;
799 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
800 MI_COM_PHY_ADDR_MASK);
801 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
802 MI_COM_REG_ADDR_MASK);
803 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
805 tw32_f(MAC_MI_COM, frame_val);
807 loops = PHY_BUSY_LOOPS;
808 while (loops != 0) {
809 udelay(10);
810 frame_val = tr32(MAC_MI_COM);
812 if ((frame_val & MI_COM_BUSY) == 0) {
813 udelay(5);
814 frame_val = tr32(MAC_MI_COM);
815 break;
817 loops -= 1;
820 ret = -EBUSY;
821 if (loops != 0) {
822 *val = frame_val & MI_COM_DATA_MASK;
823 ret = 0;
826 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
827 tw32_f(MAC_MI_MODE, tp->mi_mode);
828 udelay(80);
831 return ret;
834 static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
836 u32 frame_val;
837 unsigned int loops;
838 int ret;
840 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
841 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
842 return 0;
844 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
845 tw32_f(MAC_MI_MODE,
846 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
847 udelay(80);
850 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
851 MI_COM_PHY_ADDR_MASK);
852 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
853 MI_COM_REG_ADDR_MASK);
854 frame_val |= (val & MI_COM_DATA_MASK);
855 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
857 tw32_f(MAC_MI_COM, frame_val);
859 loops = PHY_BUSY_LOOPS;
860 while (loops != 0) {
861 udelay(10);
862 frame_val = tr32(MAC_MI_COM);
863 if ((frame_val & MI_COM_BUSY) == 0) {
864 udelay(5);
865 frame_val = tr32(MAC_MI_COM);
866 break;
868 loops -= 1;
871 ret = -EBUSY;
872 if (loops != 0)
873 ret = 0;
875 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
876 tw32_f(MAC_MI_MODE, tp->mi_mode);
877 udelay(80);
880 return ret;
883 static int tg3_bmcr_reset(struct tg3 *tp)
885 u32 phy_control;
886 int limit, err;
888 /* OK, reset it, and poll the BMCR_RESET bit until it
889 * clears or we time out.
891 phy_control = BMCR_RESET;
892 err = tg3_writephy(tp, MII_BMCR, phy_control);
893 if (err != 0)
894 return -EBUSY;
896 limit = 5000;
897 while (limit--) {
898 err = tg3_readphy(tp, MII_BMCR, &phy_control);
899 if (err != 0)
900 return -EBUSY;
902 if ((phy_control & BMCR_RESET) == 0) {
903 udelay(40);
904 break;
906 udelay(10);
908 if (limit < 0)
909 return -EBUSY;
911 return 0;
914 static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
916 struct tg3 *tp = bp->priv;
917 u32 val;
919 spin_lock_bh(&tp->lock);
921 if (tg3_readphy(tp, reg, &val))
922 val = -EIO;
924 spin_unlock_bh(&tp->lock);
926 return val;
929 static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
931 struct tg3 *tp = bp->priv;
932 u32 ret = 0;
934 spin_lock_bh(&tp->lock);
936 if (tg3_writephy(tp, reg, val))
937 ret = -EIO;
939 spin_unlock_bh(&tp->lock);
941 return ret;
944 static int tg3_mdio_reset(struct mii_bus *bp)
946 return 0;
949 static void tg3_mdio_config_5785(struct tg3 *tp)
951 u32 val;
952 struct phy_device *phydev;
954 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
955 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
956 case PHY_ID_BCM50610:
957 case PHY_ID_BCM50610M:
958 val = MAC_PHYCFG2_50610_LED_MODES;
959 break;
960 case PHY_ID_BCMAC131:
961 val = MAC_PHYCFG2_AC131_LED_MODES;
962 break;
963 case PHY_ID_RTL8211C:
964 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
965 break;
966 case PHY_ID_RTL8201E:
967 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
968 break;
969 default:
970 return;
973 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
974 tw32(MAC_PHYCFG2, val);
976 val = tr32(MAC_PHYCFG1);
977 val &= ~(MAC_PHYCFG1_RGMII_INT |
978 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
979 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
980 tw32(MAC_PHYCFG1, val);
982 return;
985 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
986 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
987 MAC_PHYCFG2_FMODE_MASK_MASK |
988 MAC_PHYCFG2_GMODE_MASK_MASK |
989 MAC_PHYCFG2_ACT_MASK_MASK |
990 MAC_PHYCFG2_QUAL_MASK_MASK |
991 MAC_PHYCFG2_INBAND_ENABLE;
993 tw32(MAC_PHYCFG2, val);
995 val = tr32(MAC_PHYCFG1);
996 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
997 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
998 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
999 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1000 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1001 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1002 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1004 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1005 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1006 tw32(MAC_PHYCFG1, val);
1008 val = tr32(MAC_EXT_RGMII_MODE);
1009 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1010 MAC_RGMII_MODE_RX_QUALITY |
1011 MAC_RGMII_MODE_RX_ACTIVITY |
1012 MAC_RGMII_MODE_RX_ENG_DET |
1013 MAC_RGMII_MODE_TX_ENABLE |
1014 MAC_RGMII_MODE_TX_LOWPWR |
1015 MAC_RGMII_MODE_TX_RESET);
1016 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
1017 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1018 val |= MAC_RGMII_MODE_RX_INT_B |
1019 MAC_RGMII_MODE_RX_QUALITY |
1020 MAC_RGMII_MODE_RX_ACTIVITY |
1021 MAC_RGMII_MODE_RX_ENG_DET;
1022 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1023 val |= MAC_RGMII_MODE_TX_ENABLE |
1024 MAC_RGMII_MODE_TX_LOWPWR |
1025 MAC_RGMII_MODE_TX_RESET;
1027 tw32(MAC_EXT_RGMII_MODE, val);
1030 static void tg3_mdio_start(struct tg3 *tp)
1032 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1033 tw32_f(MAC_MI_MODE, tp->mi_mode);
1034 udelay(80);
1036 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1037 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1038 tg3_mdio_config_5785(tp);
1041 static int tg3_mdio_init(struct tg3 *tp)
1043 int i;
1044 u32 reg;
1045 struct phy_device *phydev;
1047 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1048 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
1049 u32 is_serdes;
1051 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
1053 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1054 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1055 else
1056 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1057 TG3_CPMU_PHY_STRAP_IS_SERDES;
1058 if (is_serdes)
1059 tp->phy_addr += 7;
1060 } else
1061 tp->phy_addr = TG3_PHY_MII_ADDR;
1063 tg3_mdio_start(tp);
1065 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1066 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1067 return 0;
1069 tp->mdio_bus = mdiobus_alloc();
1070 if (tp->mdio_bus == NULL)
1071 return -ENOMEM;
1073 tp->mdio_bus->name = "tg3 mdio bus";
1074 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
1075 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
1076 tp->mdio_bus->priv = tp;
1077 tp->mdio_bus->parent = &tp->pdev->dev;
1078 tp->mdio_bus->read = &tg3_mdio_read;
1079 tp->mdio_bus->write = &tg3_mdio_write;
1080 tp->mdio_bus->reset = &tg3_mdio_reset;
1081 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
1082 tp->mdio_bus->irq = &tp->mdio_irq[0];
1084 for (i = 0; i < PHY_MAX_ADDR; i++)
1085 tp->mdio_bus->irq[i] = PHY_POLL;
1087 /* The bus registration will look for all the PHYs on the mdio bus.
1088 * Unfortunately, it does not ensure the PHY is powered up before
1089 * accessing the PHY ID registers. A chip reset is the
1090 * quickest way to bring the device back to an operational state..
1092 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1093 tg3_bmcr_reset(tp);
1095 i = mdiobus_register(tp->mdio_bus);
1096 if (i) {
1097 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
1098 mdiobus_free(tp->mdio_bus);
1099 return i;
1102 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1104 if (!phydev || !phydev->drv) {
1105 dev_warn(&tp->pdev->dev, "No PHY devices\n");
1106 mdiobus_unregister(tp->mdio_bus);
1107 mdiobus_free(tp->mdio_bus);
1108 return -ENODEV;
1111 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
1112 case PHY_ID_BCM57780:
1113 phydev->interface = PHY_INTERFACE_MODE_GMII;
1114 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
1115 break;
1116 case PHY_ID_BCM50610:
1117 case PHY_ID_BCM50610M:
1118 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
1119 PHY_BRCM_RX_REFCLK_UNUSED |
1120 PHY_BRCM_DIS_TXCRXC_NOENRGY |
1121 PHY_BRCM_AUTO_PWRDWN_ENABLE;
1122 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
1123 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1124 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1125 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1126 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1127 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
1128 /* fallthru */
1129 case PHY_ID_RTL8211C:
1130 phydev->interface = PHY_INTERFACE_MODE_RGMII;
1131 break;
1132 case PHY_ID_RTL8201E:
1133 case PHY_ID_BCMAC131:
1134 phydev->interface = PHY_INTERFACE_MODE_MII;
1135 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
1136 tp->phy_flags |= TG3_PHYFLG_IS_FET;
1137 break;
1140 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1142 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1143 tg3_mdio_config_5785(tp);
1145 return 0;
1148 static void tg3_mdio_fini(struct tg3 *tp)
1150 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1151 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
1152 mdiobus_unregister(tp->mdio_bus);
1153 mdiobus_free(tp->mdio_bus);
1157 static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1159 int err;
1161 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1162 if (err)
1163 goto done;
1165 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1166 if (err)
1167 goto done;
1169 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1170 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1171 if (err)
1172 goto done;
1174 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1176 done:
1177 return err;
1180 static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1182 int err;
1184 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1185 if (err)
1186 goto done;
1188 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1189 if (err)
1190 goto done;
1192 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1193 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1194 if (err)
1195 goto done;
1197 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1199 done:
1200 return err;
1203 /* tp->lock is held. */
1204 static inline void tg3_generate_fw_event(struct tg3 *tp)
1206 u32 val;
1208 val = tr32(GRC_RX_CPU_EVENT);
1209 val |= GRC_RX_CPU_DRIVER_EVENT;
1210 tw32_f(GRC_RX_CPU_EVENT, val);
1212 tp->last_event_jiffies = jiffies;
1215 #define TG3_FW_EVENT_TIMEOUT_USEC 2500
1217 /* tp->lock is held. */
1218 static void tg3_wait_for_event_ack(struct tg3 *tp)
1220 int i;
1221 unsigned int delay_cnt;
1222 long time_remain;
1224 /* If enough time has passed, no wait is necessary. */
1225 time_remain = (long)(tp->last_event_jiffies + 1 +
1226 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1227 (long)jiffies;
1228 if (time_remain < 0)
1229 return;
1231 /* Check if we can shorten the wait time. */
1232 delay_cnt = jiffies_to_usecs(time_remain);
1233 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1234 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1235 delay_cnt = (delay_cnt >> 3) + 1;
1237 for (i = 0; i < delay_cnt; i++) {
1238 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1239 break;
1240 udelay(8);
1244 /* tp->lock is held. */
1245 static void tg3_ump_link_report(struct tg3 *tp)
1247 u32 reg;
1248 u32 val;
1250 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1251 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1252 return;
1254 tg3_wait_for_event_ack(tp);
1256 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1258 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1260 val = 0;
1261 if (!tg3_readphy(tp, MII_BMCR, &reg))
1262 val = reg << 16;
1263 if (!tg3_readphy(tp, MII_BMSR, &reg))
1264 val |= (reg & 0xffff);
1265 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1267 val = 0;
1268 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1269 val = reg << 16;
1270 if (!tg3_readphy(tp, MII_LPA, &reg))
1271 val |= (reg & 0xffff);
1272 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1274 val = 0;
1275 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
1276 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1277 val = reg << 16;
1278 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1279 val |= (reg & 0xffff);
1281 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1283 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1284 val = reg << 16;
1285 else
1286 val = 0;
1287 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1289 tg3_generate_fw_event(tp);
1292 static void tg3_link_report(struct tg3 *tp)
1294 if (!netif_carrier_ok(tp->dev)) {
1295 netif_info(tp, link, tp->dev, "Link is down\n");
1296 tg3_ump_link_report(tp);
1297 } else if (netif_msg_link(tp)) {
1298 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1299 (tp->link_config.active_speed == SPEED_1000 ?
1300 1000 :
1301 (tp->link_config.active_speed == SPEED_100 ?
1302 100 : 10)),
1303 (tp->link_config.active_duplex == DUPLEX_FULL ?
1304 "full" : "half"));
1306 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1307 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1308 "on" : "off",
1309 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1310 "on" : "off");
1311 tg3_ump_link_report(tp);
1315 static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1317 u16 miireg;
1319 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
1320 miireg = ADVERTISE_PAUSE_CAP;
1321 else if (flow_ctrl & FLOW_CTRL_TX)
1322 miireg = ADVERTISE_PAUSE_ASYM;
1323 else if (flow_ctrl & FLOW_CTRL_RX)
1324 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1325 else
1326 miireg = 0;
1328 return miireg;
1331 static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1333 u16 miireg;
1335 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
1336 miireg = ADVERTISE_1000XPAUSE;
1337 else if (flow_ctrl & FLOW_CTRL_TX)
1338 miireg = ADVERTISE_1000XPSE_ASYM;
1339 else if (flow_ctrl & FLOW_CTRL_RX)
1340 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1341 else
1342 miireg = 0;
1344 return miireg;
1347 static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1349 u8 cap = 0;
1351 if (lcladv & ADVERTISE_1000XPAUSE) {
1352 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1353 if (rmtadv & LPA_1000XPAUSE)
1354 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1355 else if (rmtadv & LPA_1000XPAUSE_ASYM)
1356 cap = FLOW_CTRL_RX;
1357 } else {
1358 if (rmtadv & LPA_1000XPAUSE)
1359 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1361 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1362 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
1363 cap = FLOW_CTRL_TX;
1366 return cap;
1369 static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
1371 u8 autoneg;
1372 u8 flowctrl = 0;
1373 u32 old_rx_mode = tp->rx_mode;
1374 u32 old_tx_mode = tp->tx_mode;
1376 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
1377 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
1378 else
1379 autoneg = tp->link_config.autoneg;
1381 if (autoneg == AUTONEG_ENABLE &&
1382 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1383 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
1384 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
1385 else
1386 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
1387 } else
1388 flowctrl = tp->link_config.flowctrl;
1390 tp->link_config.active_flowctrl = flowctrl;
1392 if (flowctrl & FLOW_CTRL_RX)
1393 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1394 else
1395 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1397 if (old_rx_mode != tp->rx_mode)
1398 tw32_f(MAC_RX_MODE, tp->rx_mode);
1400 if (flowctrl & FLOW_CTRL_TX)
1401 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1402 else
1403 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1405 if (old_tx_mode != tp->tx_mode)
1406 tw32_f(MAC_TX_MODE, tp->tx_mode);
1409 static void tg3_adjust_link(struct net_device *dev)
1411 u8 oldflowctrl, linkmesg = 0;
1412 u32 mac_mode, lcl_adv, rmt_adv;
1413 struct tg3 *tp = netdev_priv(dev);
1414 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1416 spin_lock_bh(&tp->lock);
1418 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1419 MAC_MODE_HALF_DUPLEX);
1421 oldflowctrl = tp->link_config.active_flowctrl;
1423 if (phydev->link) {
1424 lcl_adv = 0;
1425 rmt_adv = 0;
1427 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1428 mac_mode |= MAC_MODE_PORT_MODE_MII;
1429 else if (phydev->speed == SPEED_1000 ||
1430 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
1431 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1432 else
1433 mac_mode |= MAC_MODE_PORT_MODE_MII;
1435 if (phydev->duplex == DUPLEX_HALF)
1436 mac_mode |= MAC_MODE_HALF_DUPLEX;
1437 else {
1438 lcl_adv = tg3_advert_flowctrl_1000T(
1439 tp->link_config.flowctrl);
1441 if (phydev->pause)
1442 rmt_adv = LPA_PAUSE_CAP;
1443 if (phydev->asym_pause)
1444 rmt_adv |= LPA_PAUSE_ASYM;
1447 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1448 } else
1449 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1451 if (mac_mode != tp->mac_mode) {
1452 tp->mac_mode = mac_mode;
1453 tw32_f(MAC_MODE, tp->mac_mode);
1454 udelay(40);
1457 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1458 if (phydev->speed == SPEED_10)
1459 tw32(MAC_MI_STAT,
1460 MAC_MI_STAT_10MBPS_MODE |
1461 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1462 else
1463 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1466 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1467 tw32(MAC_TX_LENGTHS,
1468 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1469 (6 << TX_LENGTHS_IPG_SHIFT) |
1470 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1471 else
1472 tw32(MAC_TX_LENGTHS,
1473 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1474 (6 << TX_LENGTHS_IPG_SHIFT) |
1475 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1477 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1478 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1479 phydev->speed != tp->link_config.active_speed ||
1480 phydev->duplex != tp->link_config.active_duplex ||
1481 oldflowctrl != tp->link_config.active_flowctrl)
1482 linkmesg = 1;
1484 tp->link_config.active_speed = phydev->speed;
1485 tp->link_config.active_duplex = phydev->duplex;
1487 spin_unlock_bh(&tp->lock);
1489 if (linkmesg)
1490 tg3_link_report(tp);
1493 static int tg3_phy_init(struct tg3 *tp)
1495 struct phy_device *phydev;
1497 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
1498 return 0;
1500 /* Bring the PHY back to a known state. */
1501 tg3_bmcr_reset(tp);
1503 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1505 /* Attach the MAC to the PHY. */
1506 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
1507 phydev->dev_flags, phydev->interface);
1508 if (IS_ERR(phydev)) {
1509 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
1510 return PTR_ERR(phydev);
1513 /* Mask with MAC supported features. */
1514 switch (phydev->interface) {
1515 case PHY_INTERFACE_MODE_GMII:
1516 case PHY_INTERFACE_MODE_RGMII:
1517 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
1518 phydev->supported &= (PHY_GBIT_FEATURES |
1519 SUPPORTED_Pause |
1520 SUPPORTED_Asym_Pause);
1521 break;
1523 /* fallthru */
1524 case PHY_INTERFACE_MODE_MII:
1525 phydev->supported &= (PHY_BASIC_FEATURES |
1526 SUPPORTED_Pause |
1527 SUPPORTED_Asym_Pause);
1528 break;
1529 default:
1530 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
1531 return -EINVAL;
1534 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
1536 phydev->advertising = phydev->supported;
1538 return 0;
1541 static void tg3_phy_start(struct tg3 *tp)
1543 struct phy_device *phydev;
1545 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
1546 return;
1548 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1550 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1551 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
1552 phydev->speed = tp->link_config.orig_speed;
1553 phydev->duplex = tp->link_config.orig_duplex;
1554 phydev->autoneg = tp->link_config.orig_autoneg;
1555 phydev->advertising = tp->link_config.orig_advertising;
1558 phy_start(phydev);
1560 phy_start_aneg(phydev);
1563 static void tg3_phy_stop(struct tg3 *tp)
1565 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
1566 return;
1568 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
1571 static void tg3_phy_fini(struct tg3 *tp)
1573 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
1574 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
1575 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
1579 static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1581 int err;
1583 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1584 if (!err)
1585 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1587 return err;
1590 static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1592 int err;
1594 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1595 if (!err)
1596 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1598 return err;
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 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1627 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
1628 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
1629 return;
1631 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
1632 tg3_phy_fet_toggle_apd(tp, enable);
1633 return;
1636 reg = MII_TG3_MISC_SHDW_WREN |
1637 MII_TG3_MISC_SHDW_SCR5_SEL |
1638 MII_TG3_MISC_SHDW_SCR5_LPED |
1639 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1640 MII_TG3_MISC_SHDW_SCR5_SDTL |
1641 MII_TG3_MISC_SHDW_SCR5_C125OE;
1642 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1643 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1645 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1648 reg = MII_TG3_MISC_SHDW_WREN |
1649 MII_TG3_MISC_SHDW_APD_SEL |
1650 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1651 if (enable)
1652 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1654 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1657 static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1659 u32 phy;
1661 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1662 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
1663 return;
1665 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
1666 u32 ephy;
1668 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1669 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1671 tg3_writephy(tp, MII_TG3_FET_TEST,
1672 ephy | MII_TG3_FET_SHADOW_EN);
1673 if (!tg3_readphy(tp, reg, &phy)) {
1674 if (enable)
1675 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1676 else
1677 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1678 tg3_writephy(tp, reg, phy);
1680 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
1682 } else {
1683 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1684 MII_TG3_AUXCTL_SHDWSEL_MISC;
1685 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1686 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1687 if (enable)
1688 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1689 else
1690 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1691 phy |= MII_TG3_AUXCTL_MISC_WREN;
1692 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1697 static void tg3_phy_set_wirespeed(struct tg3 *tp)
1699 u32 val;
1701 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
1702 return;
1704 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1705 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1706 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1707 (val | (1 << 15) | (1 << 4)));
1710 static void tg3_phy_apply_otp(struct tg3 *tp)
1712 u32 otp, phy;
1714 if (!tp->phy_otp)
1715 return;
1717 otp = tp->phy_otp;
1719 /* Enable SM_DSP clock and tx 6dB coding. */
1720 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1721 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1722 MII_TG3_AUXCTL_ACTL_TX_6DB;
1723 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1725 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1726 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1727 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1729 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1730 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1731 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1733 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1734 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1735 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1737 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1738 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1740 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1741 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1743 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1744 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1745 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1747 /* Turn off SM_DSP clock. */
1748 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1749 MII_TG3_AUXCTL_ACTL_TX_6DB;
1750 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1753 static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1755 u32 val;
1757 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1758 return;
1760 tp->setlpicnt = 0;
1762 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1763 current_link_up == 1 &&
1764 tp->link_config.active_duplex == DUPLEX_FULL &&
1765 (tp->link_config.active_speed == SPEED_100 ||
1766 tp->link_config.active_speed == SPEED_1000)) {
1767 u32 eeectl;
1769 if (tp->link_config.active_speed == SPEED_1000)
1770 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1771 else
1772 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1774 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1776 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1777 TG3_CL45_D7_EEERES_STAT, &val);
1779 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1780 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
1781 tp->setlpicnt = 2;
1784 if (!tp->setlpicnt) {
1785 val = tr32(TG3_CPMU_EEE_MODE);
1786 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1790 static int tg3_wait_macro_done(struct tg3 *tp)
1792 int limit = 100;
1794 while (limit--) {
1795 u32 tmp32;
1797 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
1798 if ((tmp32 & 0x1000) == 0)
1799 break;
1802 if (limit < 0)
1803 return -EBUSY;
1805 return 0;
1808 static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1810 static const u32 test_pat[4][6] = {
1811 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1812 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1813 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1814 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1816 int chan;
1818 for (chan = 0; chan < 4; chan++) {
1819 int i;
1821 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1822 (chan * 0x2000) | 0x0200);
1823 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
1825 for (i = 0; i < 6; i++)
1826 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1827 test_pat[chan][i]);
1829 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
1830 if (tg3_wait_macro_done(tp)) {
1831 *resetp = 1;
1832 return -EBUSY;
1835 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1836 (chan * 0x2000) | 0x0200);
1837 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
1838 if (tg3_wait_macro_done(tp)) {
1839 *resetp = 1;
1840 return -EBUSY;
1843 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
1844 if (tg3_wait_macro_done(tp)) {
1845 *resetp = 1;
1846 return -EBUSY;
1849 for (i = 0; i < 6; i += 2) {
1850 u32 low, high;
1852 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1853 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1854 tg3_wait_macro_done(tp)) {
1855 *resetp = 1;
1856 return -EBUSY;
1858 low &= 0x7fff;
1859 high &= 0x000f;
1860 if (low != test_pat[chan][i] ||
1861 high != test_pat[chan][i+1]) {
1862 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1863 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1864 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1866 return -EBUSY;
1871 return 0;
1874 static int tg3_phy_reset_chanpat(struct tg3 *tp)
1876 int chan;
1878 for (chan = 0; chan < 4; chan++) {
1879 int i;
1881 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1882 (chan * 0x2000) | 0x0200);
1883 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
1884 for (i = 0; i < 6; i++)
1885 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1886 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
1887 if (tg3_wait_macro_done(tp))
1888 return -EBUSY;
1891 return 0;
1894 static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1896 u32 reg32, phy9_orig;
1897 int retries, do_phy_reset, err;
1899 retries = 10;
1900 do_phy_reset = 1;
1901 do {
1902 if (do_phy_reset) {
1903 err = tg3_bmcr_reset(tp);
1904 if (err)
1905 return err;
1906 do_phy_reset = 0;
1909 /* Disable transmitter and interrupt. */
1910 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1911 continue;
1913 reg32 |= 0x3000;
1914 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1916 /* Set full-duplex, 1000 mbps. */
1917 tg3_writephy(tp, MII_BMCR,
1918 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1920 /* Set to master mode. */
1921 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1922 continue;
1924 tg3_writephy(tp, MII_TG3_CTRL,
1925 (MII_TG3_CTRL_AS_MASTER |
1926 MII_TG3_CTRL_ENABLE_AS_MASTER));
1928 /* Enable SM_DSP_CLOCK and 6dB. */
1929 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1931 /* Block the PHY control access. */
1932 tg3_phydsp_write(tp, 0x8005, 0x0800);
1934 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1935 if (!err)
1936 break;
1937 } while (--retries);
1939 err = tg3_phy_reset_chanpat(tp);
1940 if (err)
1941 return err;
1943 tg3_phydsp_write(tp, 0x8005, 0x0000);
1945 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1946 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
1948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1950 /* Set Extended packet length bit for jumbo frames */
1951 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
1952 } else {
1953 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1956 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1958 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1959 reg32 &= ~0x3000;
1960 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1961 } else if (!err)
1962 err = -EBUSY;
1964 return err;
1967 /* This will reset the tigon3 PHY if there is no valid
1968 * link unless the FORCE argument is non-zero.
1970 static int tg3_phy_reset(struct tg3 *tp)
1972 u32 val, cpmuctrl;
1973 int err;
1975 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1976 val = tr32(GRC_MISC_CFG);
1977 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1978 udelay(40);
1980 err = tg3_readphy(tp, MII_BMSR, &val);
1981 err |= tg3_readphy(tp, MII_BMSR, &val);
1982 if (err != 0)
1983 return -EBUSY;
1985 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1986 netif_carrier_off(tp->dev);
1987 tg3_link_report(tp);
1990 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1991 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1992 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1993 err = tg3_phy_reset_5703_4_5(tp);
1994 if (err)
1995 return err;
1996 goto out;
1999 cpmuctrl = 0;
2000 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2001 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2002 cpmuctrl = tr32(TG3_CPMU_CTRL);
2003 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2004 tw32(TG3_CPMU_CTRL,
2005 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2008 err = tg3_bmcr_reset(tp);
2009 if (err)
2010 return err;
2012 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
2013 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2014 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
2016 tw32(TG3_CPMU_CTRL, cpmuctrl);
2019 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2020 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
2021 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2022 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2023 CPMU_LSPD_1000MB_MACCLK_12_5) {
2024 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2025 udelay(40);
2026 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2030 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2031 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
2032 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
2033 return 0;
2035 tg3_phy_apply_otp(tp);
2037 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
2038 tg3_phy_toggle_apd(tp, true);
2039 else
2040 tg3_phy_toggle_apd(tp, false);
2042 out:
2043 if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
2044 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2045 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2046 tg3_phydsp_write(tp, 0x000a, 0x0323);
2047 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2049 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
2050 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2051 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2053 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
2054 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2055 tg3_phydsp_write(tp, 0x000a, 0x310b);
2056 tg3_phydsp_write(tp, 0x201f, 0x9506);
2057 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2058 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2059 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
2060 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2061 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2062 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2063 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2064 tg3_writephy(tp, MII_TG3_TEST1,
2065 MII_TG3_TEST1_TRIM_EN | 0x4);
2066 } else
2067 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2068 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2070 /* Set Extended packet length bit (bit 14) on all chips that */
2071 /* support jumbo frames */
2072 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2073 /* Cannot do read-modify-write on 5401 */
2074 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2075 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
2076 /* Set bit 14 with read-modify-write to preserve other bits */
2077 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
2078 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
2079 tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
2082 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2083 * jumbo frames transmission.
2085 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
2086 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
2087 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2088 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
2091 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2092 /* adjust output voltage */
2093 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
2096 tg3_phy_toggle_automdix(tp, 1);
2097 tg3_phy_set_wirespeed(tp);
2098 return 0;
2101 static void tg3_frob_aux_power(struct tg3 *tp)
2103 struct tg3 *tp_peer = tp;
2105 /* The GPIOs do something completely different on 57765. */
2106 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
2107 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2108 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
2109 return;
2111 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2112 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2113 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
2114 struct net_device *dev_peer;
2116 dev_peer = pci_get_drvdata(tp->pdev_peer);
2117 /* remove_one() may have been run on the peer. */
2118 if (!dev_peer)
2119 tp_peer = tp;
2120 else
2121 tp_peer = netdev_priv(dev_peer);
2124 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2125 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2126 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2127 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
2128 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2129 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2130 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2131 (GRC_LCLCTRL_GPIO_OE0 |
2132 GRC_LCLCTRL_GPIO_OE1 |
2133 GRC_LCLCTRL_GPIO_OE2 |
2134 GRC_LCLCTRL_GPIO_OUTPUT0 |
2135 GRC_LCLCTRL_GPIO_OUTPUT1),
2136 100);
2137 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2138 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2139 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2140 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2141 GRC_LCLCTRL_GPIO_OE1 |
2142 GRC_LCLCTRL_GPIO_OE2 |
2143 GRC_LCLCTRL_GPIO_OUTPUT0 |
2144 GRC_LCLCTRL_GPIO_OUTPUT1 |
2145 tp->grc_local_ctrl;
2146 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2148 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2149 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2151 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2152 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2153 } else {
2154 u32 no_gpio2;
2155 u32 grc_local_ctrl = 0;
2157 if (tp_peer != tp &&
2158 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2159 return;
2161 /* Workaround to prevent overdrawing Amps. */
2162 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2163 ASIC_REV_5714) {
2164 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2165 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2166 grc_local_ctrl, 100);
2169 /* On 5753 and variants, GPIO2 cannot be used. */
2170 no_gpio2 = tp->nic_sram_data_cfg &
2171 NIC_SRAM_DATA_CFG_NO_GPIO2;
2173 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2174 GRC_LCLCTRL_GPIO_OE1 |
2175 GRC_LCLCTRL_GPIO_OE2 |
2176 GRC_LCLCTRL_GPIO_OUTPUT1 |
2177 GRC_LCLCTRL_GPIO_OUTPUT2;
2178 if (no_gpio2) {
2179 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2180 GRC_LCLCTRL_GPIO_OUTPUT2);
2182 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2183 grc_local_ctrl, 100);
2185 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2187 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2188 grc_local_ctrl, 100);
2190 if (!no_gpio2) {
2191 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2192 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2193 grc_local_ctrl, 100);
2196 } else {
2197 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2198 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2199 if (tp_peer != tp &&
2200 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2201 return;
2203 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2204 (GRC_LCLCTRL_GPIO_OE1 |
2205 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
2207 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2208 GRC_LCLCTRL_GPIO_OE1, 100);
2210 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2211 (GRC_LCLCTRL_GPIO_OE1 |
2212 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
2217 static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2219 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2220 return 1;
2221 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
2222 if (speed != SPEED_10)
2223 return 1;
2224 } else if (speed == SPEED_10)
2225 return 1;
2227 return 0;
2230 static int tg3_setup_phy(struct tg3 *, int);
2232 #define RESET_KIND_SHUTDOWN 0
2233 #define RESET_KIND_INIT 1
2234 #define RESET_KIND_SUSPEND 2
2236 static void tg3_write_sig_post_reset(struct tg3 *, int);
2237 static int tg3_halt_cpu(struct tg3 *, u32);
2239 static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
2241 u32 val;
2243 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
2244 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2245 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2246 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2248 sg_dig_ctrl |=
2249 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2250 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2251 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2253 return;
2256 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2257 tg3_bmcr_reset(tp);
2258 val = tr32(GRC_MISC_CFG);
2259 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2260 udelay(40);
2261 return;
2262 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
2263 u32 phytest;
2264 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2265 u32 phy;
2267 tg3_writephy(tp, MII_ADVERTISE, 0);
2268 tg3_writephy(tp, MII_BMCR,
2269 BMCR_ANENABLE | BMCR_ANRESTART);
2271 tg3_writephy(tp, MII_TG3_FET_TEST,
2272 phytest | MII_TG3_FET_SHADOW_EN);
2273 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2274 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2275 tg3_writephy(tp,
2276 MII_TG3_FET_SHDW_AUXMODE4,
2277 phy);
2279 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2281 return;
2282 } else if (do_low_power) {
2283 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2284 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
2286 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2287 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2288 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2289 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2290 MII_TG3_AUXCTL_PCTL_VREG_11V);
2293 /* The PHY should not be powered down on some chips because
2294 * of bugs.
2296 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2297 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2298 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2299 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
2300 return;
2302 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2303 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
2304 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2305 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2306 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2307 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2310 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2313 /* tp->lock is held. */
2314 static int tg3_nvram_lock(struct tg3 *tp)
2316 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2317 int i;
2319 if (tp->nvram_lock_cnt == 0) {
2320 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2321 for (i = 0; i < 8000; i++) {
2322 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2323 break;
2324 udelay(20);
2326 if (i == 8000) {
2327 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2328 return -ENODEV;
2331 tp->nvram_lock_cnt++;
2333 return 0;
2336 /* tp->lock is held. */
2337 static void tg3_nvram_unlock(struct tg3 *tp)
2339 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2340 if (tp->nvram_lock_cnt > 0)
2341 tp->nvram_lock_cnt--;
2342 if (tp->nvram_lock_cnt == 0)
2343 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2347 /* tp->lock is held. */
2348 static void tg3_enable_nvram_access(struct tg3 *tp)
2350 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2351 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
2352 u32 nvaccess = tr32(NVRAM_ACCESS);
2354 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2358 /* tp->lock is held. */
2359 static void tg3_disable_nvram_access(struct tg3 *tp)
2361 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2362 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
2363 u32 nvaccess = tr32(NVRAM_ACCESS);
2365 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2369 static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2370 u32 offset, u32 *val)
2372 u32 tmp;
2373 int i;
2375 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2376 return -EINVAL;
2378 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2379 EEPROM_ADDR_DEVID_MASK |
2380 EEPROM_ADDR_READ);
2381 tw32(GRC_EEPROM_ADDR,
2382 tmp |
2383 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2384 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2385 EEPROM_ADDR_ADDR_MASK) |
2386 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2388 for (i = 0; i < 1000; i++) {
2389 tmp = tr32(GRC_EEPROM_ADDR);
2391 if (tmp & EEPROM_ADDR_COMPLETE)
2392 break;
2393 msleep(1);
2395 if (!(tmp & EEPROM_ADDR_COMPLETE))
2396 return -EBUSY;
2398 tmp = tr32(GRC_EEPROM_DATA);
2401 * The data will always be opposite the native endian
2402 * format. Perform a blind byteswap to compensate.
2404 *val = swab32(tmp);
2406 return 0;
2409 #define NVRAM_CMD_TIMEOUT 10000
2411 static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2413 int i;
2415 tw32(NVRAM_CMD, nvram_cmd);
2416 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2417 udelay(10);
2418 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2419 udelay(10);
2420 break;
2424 if (i == NVRAM_CMD_TIMEOUT)
2425 return -EBUSY;
2427 return 0;
2430 static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2432 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2433 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2434 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2435 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2436 (tp->nvram_jedecnum == JEDEC_ATMEL))
2438 addr = ((addr / tp->nvram_pagesize) <<
2439 ATMEL_AT45DB0X1B_PAGE_POS) +
2440 (addr % tp->nvram_pagesize);
2442 return addr;
2445 static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2447 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2448 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2449 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2450 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2451 (tp->nvram_jedecnum == JEDEC_ATMEL))
2453 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2454 tp->nvram_pagesize) +
2455 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2457 return addr;
2460 /* NOTE: Data read in from NVRAM is byteswapped according to
2461 * the byteswapping settings for all other register accesses.
2462 * tg3 devices are BE devices, so on a BE machine, the data
2463 * returned will be exactly as it is seen in NVRAM. On a LE
2464 * machine, the 32-bit value will be byteswapped.
2466 static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2468 int ret;
2470 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2471 return tg3_nvram_read_using_eeprom(tp, offset, val);
2473 offset = tg3_nvram_phys_addr(tp, offset);
2475 if (offset > NVRAM_ADDR_MSK)
2476 return -EINVAL;
2478 ret = tg3_nvram_lock(tp);
2479 if (ret)
2480 return ret;
2482 tg3_enable_nvram_access(tp);
2484 tw32(NVRAM_ADDR, offset);
2485 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2486 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2488 if (ret == 0)
2489 *val = tr32(NVRAM_RDDATA);
2491 tg3_disable_nvram_access(tp);
2493 tg3_nvram_unlock(tp);
2495 return ret;
2498 /* Ensures NVRAM data is in bytestream format. */
2499 static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
2501 u32 v;
2502 int res = tg3_nvram_read(tp, offset, &v);
2503 if (!res)
2504 *val = cpu_to_be32(v);
2505 return res;
2508 /* tp->lock is held. */
2509 static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2511 u32 addr_high, addr_low;
2512 int i;
2514 addr_high = ((tp->dev->dev_addr[0] << 8) |
2515 tp->dev->dev_addr[1]);
2516 addr_low = ((tp->dev->dev_addr[2] << 24) |
2517 (tp->dev->dev_addr[3] << 16) |
2518 (tp->dev->dev_addr[4] << 8) |
2519 (tp->dev->dev_addr[5] << 0));
2520 for (i = 0; i < 4; i++) {
2521 if (i == 1 && skip_mac_1)
2522 continue;
2523 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2524 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2527 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2528 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2529 for (i = 0; i < 12; i++) {
2530 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2531 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2535 addr_high = (tp->dev->dev_addr[0] +
2536 tp->dev->dev_addr[1] +
2537 tp->dev->dev_addr[2] +
2538 tp->dev->dev_addr[3] +
2539 tp->dev->dev_addr[4] +
2540 tp->dev->dev_addr[5]) &
2541 TX_BACKOFF_SEED_MASK;
2542 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2545 static void tg3_enable_register_access(struct tg3 *tp)
2548 * Make sure register accesses (indirect or otherwise) will function
2549 * correctly.
2551 pci_write_config_dword(tp->pdev,
2552 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2555 static int tg3_power_up(struct tg3 *tp)
2557 tg3_enable_register_access(tp);
2559 pci_set_power_state(tp->pdev, PCI_D0);
2561 /* Switch out of Vaux if it is a NIC */
2562 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
2563 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2565 return 0;
2568 static int tg3_power_down_prepare(struct tg3 *tp)
2570 u32 misc_host_ctrl;
2571 bool device_should_wake, do_low_power;
2573 tg3_enable_register_access(tp);
2575 /* Restore the CLKREQ setting. */
2576 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2577 u16 lnkctl;
2579 pci_read_config_word(tp->pdev,
2580 tp->pcie_cap + PCI_EXP_LNKCTL,
2581 &lnkctl);
2582 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2583 pci_write_config_word(tp->pdev,
2584 tp->pcie_cap + PCI_EXP_LNKCTL,
2585 lnkctl);
2588 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2589 tw32(TG3PCI_MISC_HOST_CTRL,
2590 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2592 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
2593 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2595 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
2596 do_low_power = false;
2597 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
2598 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2599 struct phy_device *phydev;
2600 u32 phyid, advertising;
2602 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
2604 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
2606 tp->link_config.orig_speed = phydev->speed;
2607 tp->link_config.orig_duplex = phydev->duplex;
2608 tp->link_config.orig_autoneg = phydev->autoneg;
2609 tp->link_config.orig_advertising = phydev->advertising;
2611 advertising = ADVERTISED_TP |
2612 ADVERTISED_Pause |
2613 ADVERTISED_Autoneg |
2614 ADVERTISED_10baseT_Half;
2616 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2617 device_should_wake) {
2618 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2619 advertising |=
2620 ADVERTISED_100baseT_Half |
2621 ADVERTISED_100baseT_Full |
2622 ADVERTISED_10baseT_Full;
2623 else
2624 advertising |= ADVERTISED_10baseT_Full;
2627 phydev->advertising = advertising;
2629 phy_start_aneg(phydev);
2631 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
2632 if (phyid != PHY_ID_BCMAC131) {
2633 phyid &= PHY_BCM_OUI_MASK;
2634 if (phyid == PHY_BCM_OUI_1 ||
2635 phyid == PHY_BCM_OUI_2 ||
2636 phyid == PHY_BCM_OUI_3)
2637 do_low_power = true;
2640 } else {
2641 do_low_power = true;
2643 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2644 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
2645 tp->link_config.orig_speed = tp->link_config.speed;
2646 tp->link_config.orig_duplex = tp->link_config.duplex;
2647 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2650 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
2651 tp->link_config.speed = SPEED_10;
2652 tp->link_config.duplex = DUPLEX_HALF;
2653 tp->link_config.autoneg = AUTONEG_ENABLE;
2654 tg3_setup_phy(tp, 0);
2658 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2659 u32 val;
2661 val = tr32(GRC_VCPU_EXT_CTRL);
2662 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2663 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
2664 int i;
2665 u32 val;
2667 for (i = 0; i < 200; i++) {
2668 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2669 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2670 break;
2671 msleep(1);
2674 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2675 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2676 WOL_DRV_STATE_SHUTDOWN |
2677 WOL_DRV_WOL |
2678 WOL_SET_MAGIC_PKT);
2680 if (device_should_wake) {
2681 u32 mac_mode;
2683 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
2684 if (do_low_power) {
2685 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2686 udelay(40);
2689 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
2690 mac_mode = MAC_MODE_PORT_MODE_GMII;
2691 else
2692 mac_mode = MAC_MODE_PORT_MODE_MII;
2694 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2695 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2696 ASIC_REV_5700) {
2697 u32 speed = (tp->tg3_flags &
2698 TG3_FLAG_WOL_SPEED_100MB) ?
2699 SPEED_100 : SPEED_10;
2700 if (tg3_5700_link_polarity(tp, speed))
2701 mac_mode |= MAC_MODE_LINK_POLARITY;
2702 else
2703 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2705 } else {
2706 mac_mode = MAC_MODE_PORT_MODE_TBI;
2709 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
2710 tw32(MAC_LED_CTRL, tp->led_ctrl);
2712 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2713 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2714 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2715 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2716 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2717 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
2719 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
2720 mac_mode |= MAC_MODE_APE_TX_EN |
2721 MAC_MODE_APE_RX_EN |
2722 MAC_MODE_TDE_ENABLE;
2724 tw32_f(MAC_MODE, mac_mode);
2725 udelay(100);
2727 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2728 udelay(10);
2731 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2732 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2733 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2734 u32 base_val;
2736 base_val = tp->pci_clock_ctrl;
2737 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2738 CLOCK_CTRL_TXCLK_DISABLE);
2740 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2741 CLOCK_CTRL_PWRDOWN_PLL133, 40);
2742 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
2743 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
2744 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
2745 /* do nothing */
2746 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2747 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2748 u32 newbits1, newbits2;
2750 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2751 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2752 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2753 CLOCK_CTRL_TXCLK_DISABLE |
2754 CLOCK_CTRL_ALTCLK);
2755 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2756 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2757 newbits1 = CLOCK_CTRL_625_CORE;
2758 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2759 } else {
2760 newbits1 = CLOCK_CTRL_ALTCLK;
2761 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2764 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2765 40);
2767 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2768 40);
2770 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2771 u32 newbits3;
2773 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2774 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2775 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2776 CLOCK_CTRL_TXCLK_DISABLE |
2777 CLOCK_CTRL_44MHZ_CORE);
2778 } else {
2779 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2782 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2783 tp->pci_clock_ctrl | newbits3, 40);
2787 if (!(device_should_wake) &&
2788 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
2789 tg3_power_down_phy(tp, do_low_power);
2791 tg3_frob_aux_power(tp);
2793 /* Workaround for unstable PLL clock */
2794 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2795 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2796 u32 val = tr32(0x7d00);
2798 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2799 tw32(0x7d00, val);
2800 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
2801 int err;
2803 err = tg3_nvram_lock(tp);
2804 tg3_halt_cpu(tp, RX_CPU_BASE);
2805 if (!err)
2806 tg3_nvram_unlock(tp);
2810 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2812 return 0;
2815 static void tg3_power_down(struct tg3 *tp)
2817 tg3_power_down_prepare(tp);
2819 pci_wake_from_d3(tp->pdev, tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2820 pci_set_power_state(tp->pdev, PCI_D3hot);
2823 static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2825 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2826 case MII_TG3_AUX_STAT_10HALF:
2827 *speed = SPEED_10;
2828 *duplex = DUPLEX_HALF;
2829 break;
2831 case MII_TG3_AUX_STAT_10FULL:
2832 *speed = SPEED_10;
2833 *duplex = DUPLEX_FULL;
2834 break;
2836 case MII_TG3_AUX_STAT_100HALF:
2837 *speed = SPEED_100;
2838 *duplex = DUPLEX_HALF;
2839 break;
2841 case MII_TG3_AUX_STAT_100FULL:
2842 *speed = SPEED_100;
2843 *duplex = DUPLEX_FULL;
2844 break;
2846 case MII_TG3_AUX_STAT_1000HALF:
2847 *speed = SPEED_1000;
2848 *duplex = DUPLEX_HALF;
2849 break;
2851 case MII_TG3_AUX_STAT_1000FULL:
2852 *speed = SPEED_1000;
2853 *duplex = DUPLEX_FULL;
2854 break;
2856 default:
2857 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
2858 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2859 SPEED_10;
2860 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2861 DUPLEX_HALF;
2862 break;
2864 *speed = SPEED_INVALID;
2865 *duplex = DUPLEX_INVALID;
2866 break;
2870 static void tg3_phy_copper_begin(struct tg3 *tp)
2872 u32 new_adv;
2873 int i;
2875 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2876 /* Entering low power mode. Disable gigabit and
2877 * 100baseT advertisements.
2879 tg3_writephy(tp, MII_TG3_CTRL, 0);
2881 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2882 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2883 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2884 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2886 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2887 } else if (tp->link_config.speed == SPEED_INVALID) {
2888 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
2889 tp->link_config.advertising &=
2890 ~(ADVERTISED_1000baseT_Half |
2891 ADVERTISED_1000baseT_Full);
2893 new_adv = ADVERTISE_CSMA;
2894 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2895 new_adv |= ADVERTISE_10HALF;
2896 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2897 new_adv |= ADVERTISE_10FULL;
2898 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2899 new_adv |= ADVERTISE_100HALF;
2900 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2901 new_adv |= ADVERTISE_100FULL;
2903 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2905 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2907 if (tp->link_config.advertising &
2908 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2909 new_adv = 0;
2910 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2911 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2912 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2913 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2914 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
2915 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2916 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2917 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2918 MII_TG3_CTRL_ENABLE_AS_MASTER);
2919 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2920 } else {
2921 tg3_writephy(tp, MII_TG3_CTRL, 0);
2923 } else {
2924 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2925 new_adv |= ADVERTISE_CSMA;
2927 /* Asking for a specific link mode. */
2928 if (tp->link_config.speed == SPEED_1000) {
2929 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2931 if (tp->link_config.duplex == DUPLEX_FULL)
2932 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2933 else
2934 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2935 if (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 } else {
2940 if (tp->link_config.speed == SPEED_100) {
2941 if (tp->link_config.duplex == DUPLEX_FULL)
2942 new_adv |= ADVERTISE_100FULL;
2943 else
2944 new_adv |= ADVERTISE_100HALF;
2945 } else {
2946 if (tp->link_config.duplex == DUPLEX_FULL)
2947 new_adv |= ADVERTISE_10FULL;
2948 else
2949 new_adv |= ADVERTISE_10HALF;
2951 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2953 new_adv = 0;
2956 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2959 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
2960 u32 val;
2962 tw32(TG3_CPMU_EEE_MODE,
2963 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2965 /* Enable SM_DSP clock and tx 6dB coding. */
2966 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
2967 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
2968 MII_TG3_AUXCTL_ACTL_TX_6DB;
2969 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2971 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
2973 !tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2974 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2,
2975 val | MII_TG3_DSP_CH34TP2_HIBW01);
2977 val = 0;
2978 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
2979 /* Advertise 100-BaseTX EEE ability */
2980 if (tp->link_config.advertising &
2981 ADVERTISED_100baseT_Full)
2982 val |= MDIO_AN_EEE_ADV_100TX;
2983 /* Advertise 1000-BaseT EEE ability */
2984 if (tp->link_config.advertising &
2985 ADVERTISED_1000baseT_Full)
2986 val |= MDIO_AN_EEE_ADV_1000T;
2988 tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
2990 /* Turn off SM_DSP clock. */
2991 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
2992 MII_TG3_AUXCTL_ACTL_TX_6DB;
2993 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2996 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2997 tp->link_config.speed != SPEED_INVALID) {
2998 u32 bmcr, orig_bmcr;
3000 tp->link_config.active_speed = tp->link_config.speed;
3001 tp->link_config.active_duplex = tp->link_config.duplex;
3003 bmcr = 0;
3004 switch (tp->link_config.speed) {
3005 default:
3006 case SPEED_10:
3007 break;
3009 case SPEED_100:
3010 bmcr |= BMCR_SPEED100;
3011 break;
3013 case SPEED_1000:
3014 bmcr |= TG3_BMCR_SPEED1000;
3015 break;
3018 if (tp->link_config.duplex == DUPLEX_FULL)
3019 bmcr |= BMCR_FULLDPLX;
3021 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3022 (bmcr != orig_bmcr)) {
3023 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3024 for (i = 0; i < 1500; i++) {
3025 u32 tmp;
3027 udelay(10);
3028 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3029 tg3_readphy(tp, MII_BMSR, &tmp))
3030 continue;
3031 if (!(tmp & BMSR_LSTATUS)) {
3032 udelay(40);
3033 break;
3036 tg3_writephy(tp, MII_BMCR, bmcr);
3037 udelay(40);
3039 } else {
3040 tg3_writephy(tp, MII_BMCR,
3041 BMCR_ANENABLE | BMCR_ANRESTART);
3045 static int tg3_init_5401phy_dsp(struct tg3 *tp)
3047 int err;
3049 /* Turn off tap power management. */
3050 /* Set Extended packet length bit */
3051 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
3053 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3054 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3055 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3056 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3057 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
3059 udelay(40);
3061 return err;
3064 static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
3066 u32 adv_reg, all_mask = 0;
3068 if (mask & ADVERTISED_10baseT_Half)
3069 all_mask |= ADVERTISE_10HALF;
3070 if (mask & ADVERTISED_10baseT_Full)
3071 all_mask |= ADVERTISE_10FULL;
3072 if (mask & ADVERTISED_100baseT_Half)
3073 all_mask |= ADVERTISE_100HALF;
3074 if (mask & ADVERTISED_100baseT_Full)
3075 all_mask |= ADVERTISE_100FULL;
3077 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3078 return 0;
3080 if ((adv_reg & all_mask) != all_mask)
3081 return 0;
3082 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3083 u32 tg3_ctrl;
3085 all_mask = 0;
3086 if (mask & ADVERTISED_1000baseT_Half)
3087 all_mask |= ADVERTISE_1000HALF;
3088 if (mask & ADVERTISED_1000baseT_Full)
3089 all_mask |= ADVERTISE_1000FULL;
3091 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3092 return 0;
3094 if ((tg3_ctrl & all_mask) != all_mask)
3095 return 0;
3097 return 1;
3100 static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3102 u32 curadv, reqadv;
3104 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3105 return 1;
3107 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3108 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3110 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3111 if (curadv != reqadv)
3112 return 0;
3114 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3115 tg3_readphy(tp, MII_LPA, rmtadv);
3116 } else {
3117 /* Reprogram the advertisement register, even if it
3118 * does not affect the current link. If the link
3119 * gets renegotiated in the future, we can save an
3120 * additional renegotiation cycle by advertising
3121 * it correctly in the first place.
3123 if (curadv != reqadv) {
3124 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3125 ADVERTISE_PAUSE_ASYM);
3126 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3130 return 1;
3133 static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3135 int current_link_up;
3136 u32 bmsr, val;
3137 u32 lcl_adv, rmt_adv;
3138 u16 current_speed;
3139 u8 current_duplex;
3140 int i, err;
3142 tw32(MAC_EVENT, 0);
3144 tw32_f(MAC_STATUS,
3145 (MAC_STATUS_SYNC_CHANGED |
3146 MAC_STATUS_CFG_CHANGED |
3147 MAC_STATUS_MI_COMPLETION |
3148 MAC_STATUS_LNKSTATE_CHANGED));
3149 udelay(40);
3151 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3152 tw32_f(MAC_MI_MODE,
3153 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3154 udelay(80);
3157 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3159 /* Some third-party PHYs need to be reset on link going
3160 * down.
3162 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3163 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3164 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3165 netif_carrier_ok(tp->dev)) {
3166 tg3_readphy(tp, MII_BMSR, &bmsr);
3167 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3168 !(bmsr & BMSR_LSTATUS))
3169 force_reset = 1;
3171 if (force_reset)
3172 tg3_phy_reset(tp);
3174 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
3175 tg3_readphy(tp, MII_BMSR, &bmsr);
3176 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3177 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3178 bmsr = 0;
3180 if (!(bmsr & BMSR_LSTATUS)) {
3181 err = tg3_init_5401phy_dsp(tp);
3182 if (err)
3183 return err;
3185 tg3_readphy(tp, MII_BMSR, &bmsr);
3186 for (i = 0; i < 1000; i++) {
3187 udelay(10);
3188 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3189 (bmsr & BMSR_LSTATUS)) {
3190 udelay(40);
3191 break;
3195 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3196 TG3_PHY_REV_BCM5401_B0 &&
3197 !(bmsr & BMSR_LSTATUS) &&
3198 tp->link_config.active_speed == SPEED_1000) {
3199 err = tg3_phy_reset(tp);
3200 if (!err)
3201 err = tg3_init_5401phy_dsp(tp);
3202 if (err)
3203 return err;
3206 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3207 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3208 /* 5701 {A0,B0} CRC bug workaround */
3209 tg3_writephy(tp, 0x15, 0x0a75);
3210 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3211 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3212 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3215 /* Clear pending interrupts... */
3216 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3217 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3219 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
3220 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
3221 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
3222 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3224 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3225 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3226 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3227 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3228 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3229 else
3230 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3233 current_link_up = 0;
3234 current_speed = SPEED_INVALID;
3235 current_duplex = DUPLEX_INVALID;
3237 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
3238 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3239 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3240 if (!(val & (1 << 10))) {
3241 val |= (1 << 10);
3242 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3243 goto relink;
3247 bmsr = 0;
3248 for (i = 0; i < 100; i++) {
3249 tg3_readphy(tp, MII_BMSR, &bmsr);
3250 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3251 (bmsr & BMSR_LSTATUS))
3252 break;
3253 udelay(40);
3256 if (bmsr & BMSR_LSTATUS) {
3257 u32 aux_stat, bmcr;
3259 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3260 for (i = 0; i < 2000; i++) {
3261 udelay(10);
3262 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3263 aux_stat)
3264 break;
3267 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3268 &current_speed,
3269 &current_duplex);
3271 bmcr = 0;
3272 for (i = 0; i < 200; i++) {
3273 tg3_readphy(tp, MII_BMCR, &bmcr);
3274 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3275 continue;
3276 if (bmcr && bmcr != 0x7fff)
3277 break;
3278 udelay(10);
3281 lcl_adv = 0;
3282 rmt_adv = 0;
3284 tp->link_config.active_speed = current_speed;
3285 tp->link_config.active_duplex = current_duplex;
3287 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3288 if ((bmcr & BMCR_ANENABLE) &&
3289 tg3_copper_is_advertising_all(tp,
3290 tp->link_config.advertising)) {
3291 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3292 &rmt_adv))
3293 current_link_up = 1;
3295 } else {
3296 if (!(bmcr & BMCR_ANENABLE) &&
3297 tp->link_config.speed == current_speed &&
3298 tp->link_config.duplex == current_duplex &&
3299 tp->link_config.flowctrl ==
3300 tp->link_config.active_flowctrl) {
3301 current_link_up = 1;
3305 if (current_link_up == 1 &&
3306 tp->link_config.active_duplex == DUPLEX_FULL)
3307 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
3310 relink:
3311 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3312 tg3_phy_copper_begin(tp);
3314 tg3_readphy(tp, MII_BMSR, &bmsr);
3315 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3316 (bmsr & BMSR_LSTATUS))
3317 current_link_up = 1;
3320 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3321 if (current_link_up == 1) {
3322 if (tp->link_config.active_speed == SPEED_100 ||
3323 tp->link_config.active_speed == SPEED_10)
3324 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3325 else
3326 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3327 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
3328 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3329 else
3330 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3332 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3333 if (tp->link_config.active_duplex == DUPLEX_HALF)
3334 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3336 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
3337 if (current_link_up == 1 &&
3338 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
3339 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
3340 else
3341 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
3344 /* ??? Without this setting Netgear GA302T PHY does not
3345 * ??? send/receive packets...
3347 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
3348 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3349 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3350 tw32_f(MAC_MI_MODE, tp->mi_mode);
3351 udelay(80);
3354 tw32_f(MAC_MODE, tp->mac_mode);
3355 udelay(40);
3357 tg3_phy_eee_adjust(tp, current_link_up);
3359 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3360 /* Polled via timer. */
3361 tw32_f(MAC_EVENT, 0);
3362 } else {
3363 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3365 udelay(40);
3367 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3368 current_link_up == 1 &&
3369 tp->link_config.active_speed == SPEED_1000 &&
3370 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3371 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3372 udelay(120);
3373 tw32_f(MAC_STATUS,
3374 (MAC_STATUS_SYNC_CHANGED |
3375 MAC_STATUS_CFG_CHANGED));
3376 udelay(40);
3377 tg3_write_mem(tp,
3378 NIC_SRAM_FIRMWARE_MBOX,
3379 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3382 /* Prevent send BD corruption. */
3383 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3384 u16 oldlnkctl, newlnkctl;
3386 pci_read_config_word(tp->pdev,
3387 tp->pcie_cap + PCI_EXP_LNKCTL,
3388 &oldlnkctl);
3389 if (tp->link_config.active_speed == SPEED_100 ||
3390 tp->link_config.active_speed == SPEED_10)
3391 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3392 else
3393 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3394 if (newlnkctl != oldlnkctl)
3395 pci_write_config_word(tp->pdev,
3396 tp->pcie_cap + PCI_EXP_LNKCTL,
3397 newlnkctl);
3400 if (current_link_up != netif_carrier_ok(tp->dev)) {
3401 if (current_link_up)
3402 netif_carrier_on(tp->dev);
3403 else
3404 netif_carrier_off(tp->dev);
3405 tg3_link_report(tp);
3408 return 0;
3411 struct tg3_fiber_aneginfo {
3412 int state;
3413 #define ANEG_STATE_UNKNOWN 0
3414 #define ANEG_STATE_AN_ENABLE 1
3415 #define ANEG_STATE_RESTART_INIT 2
3416 #define ANEG_STATE_RESTART 3
3417 #define ANEG_STATE_DISABLE_LINK_OK 4
3418 #define ANEG_STATE_ABILITY_DETECT_INIT 5
3419 #define ANEG_STATE_ABILITY_DETECT 6
3420 #define ANEG_STATE_ACK_DETECT_INIT 7
3421 #define ANEG_STATE_ACK_DETECT 8
3422 #define ANEG_STATE_COMPLETE_ACK_INIT 9
3423 #define ANEG_STATE_COMPLETE_ACK 10
3424 #define ANEG_STATE_IDLE_DETECT_INIT 11
3425 #define ANEG_STATE_IDLE_DETECT 12
3426 #define ANEG_STATE_LINK_OK 13
3427 #define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3428 #define ANEG_STATE_NEXT_PAGE_WAIT 15
3430 u32 flags;
3431 #define MR_AN_ENABLE 0x00000001
3432 #define MR_RESTART_AN 0x00000002
3433 #define MR_AN_COMPLETE 0x00000004
3434 #define MR_PAGE_RX 0x00000008
3435 #define MR_NP_LOADED 0x00000010
3436 #define MR_TOGGLE_TX 0x00000020
3437 #define MR_LP_ADV_FULL_DUPLEX 0x00000040
3438 #define MR_LP_ADV_HALF_DUPLEX 0x00000080
3439 #define MR_LP_ADV_SYM_PAUSE 0x00000100
3440 #define MR_LP_ADV_ASYM_PAUSE 0x00000200
3441 #define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3442 #define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3443 #define MR_LP_ADV_NEXT_PAGE 0x00001000
3444 #define MR_TOGGLE_RX 0x00002000
3445 #define MR_NP_RX 0x00004000
3447 #define MR_LINK_OK 0x80000000
3449 unsigned long link_time, cur_time;
3451 u32 ability_match_cfg;
3452 int ability_match_count;
3454 char ability_match, idle_match, ack_match;
3456 u32 txconfig, rxconfig;
3457 #define ANEG_CFG_NP 0x00000080
3458 #define ANEG_CFG_ACK 0x00000040
3459 #define ANEG_CFG_RF2 0x00000020
3460 #define ANEG_CFG_RF1 0x00000010
3461 #define ANEG_CFG_PS2 0x00000001
3462 #define ANEG_CFG_PS1 0x00008000
3463 #define ANEG_CFG_HD 0x00004000
3464 #define ANEG_CFG_FD 0x00002000
3465 #define ANEG_CFG_INVAL 0x00001f06
3468 #define ANEG_OK 0
3469 #define ANEG_DONE 1
3470 #define ANEG_TIMER_ENAB 2
3471 #define ANEG_FAILED -1
3473 #define ANEG_STATE_SETTLE_TIME 10000
3475 static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3476 struct tg3_fiber_aneginfo *ap)
3478 u16 flowctrl;
3479 unsigned long delta;
3480 u32 rx_cfg_reg;
3481 int ret;
3483 if (ap->state == ANEG_STATE_UNKNOWN) {
3484 ap->rxconfig = 0;
3485 ap->link_time = 0;
3486 ap->cur_time = 0;
3487 ap->ability_match_cfg = 0;
3488 ap->ability_match_count = 0;
3489 ap->ability_match = 0;
3490 ap->idle_match = 0;
3491 ap->ack_match = 0;
3493 ap->cur_time++;
3495 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3496 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3498 if (rx_cfg_reg != ap->ability_match_cfg) {
3499 ap->ability_match_cfg = rx_cfg_reg;
3500 ap->ability_match = 0;
3501 ap->ability_match_count = 0;
3502 } else {
3503 if (++ap->ability_match_count > 1) {
3504 ap->ability_match = 1;
3505 ap->ability_match_cfg = rx_cfg_reg;
3508 if (rx_cfg_reg & ANEG_CFG_ACK)
3509 ap->ack_match = 1;
3510 else
3511 ap->ack_match = 0;
3513 ap->idle_match = 0;
3514 } else {
3515 ap->idle_match = 1;
3516 ap->ability_match_cfg = 0;
3517 ap->ability_match_count = 0;
3518 ap->ability_match = 0;
3519 ap->ack_match = 0;
3521 rx_cfg_reg = 0;
3524 ap->rxconfig = rx_cfg_reg;
3525 ret = ANEG_OK;
3527 switch (ap->state) {
3528 case ANEG_STATE_UNKNOWN:
3529 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3530 ap->state = ANEG_STATE_AN_ENABLE;
3532 /* fallthru */
3533 case ANEG_STATE_AN_ENABLE:
3534 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3535 if (ap->flags & MR_AN_ENABLE) {
3536 ap->link_time = 0;
3537 ap->cur_time = 0;
3538 ap->ability_match_cfg = 0;
3539 ap->ability_match_count = 0;
3540 ap->ability_match = 0;
3541 ap->idle_match = 0;
3542 ap->ack_match = 0;
3544 ap->state = ANEG_STATE_RESTART_INIT;
3545 } else {
3546 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3548 break;
3550 case ANEG_STATE_RESTART_INIT:
3551 ap->link_time = ap->cur_time;
3552 ap->flags &= ~(MR_NP_LOADED);
3553 ap->txconfig = 0;
3554 tw32(MAC_TX_AUTO_NEG, 0);
3555 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3556 tw32_f(MAC_MODE, tp->mac_mode);
3557 udelay(40);
3559 ret = ANEG_TIMER_ENAB;
3560 ap->state = ANEG_STATE_RESTART;
3562 /* fallthru */
3563 case ANEG_STATE_RESTART:
3564 delta = ap->cur_time - ap->link_time;
3565 if (delta > ANEG_STATE_SETTLE_TIME)
3566 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3567 else
3568 ret = ANEG_TIMER_ENAB;
3569 break;
3571 case ANEG_STATE_DISABLE_LINK_OK:
3572 ret = ANEG_DONE;
3573 break;
3575 case ANEG_STATE_ABILITY_DETECT_INIT:
3576 ap->flags &= ~(MR_TOGGLE_TX);
3577 ap->txconfig = ANEG_CFG_FD;
3578 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3579 if (flowctrl & ADVERTISE_1000XPAUSE)
3580 ap->txconfig |= ANEG_CFG_PS1;
3581 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3582 ap->txconfig |= ANEG_CFG_PS2;
3583 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3584 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3585 tw32_f(MAC_MODE, tp->mac_mode);
3586 udelay(40);
3588 ap->state = ANEG_STATE_ABILITY_DETECT;
3589 break;
3591 case ANEG_STATE_ABILITY_DETECT:
3592 if (ap->ability_match != 0 && ap->rxconfig != 0)
3593 ap->state = ANEG_STATE_ACK_DETECT_INIT;
3594 break;
3596 case ANEG_STATE_ACK_DETECT_INIT:
3597 ap->txconfig |= ANEG_CFG_ACK;
3598 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3599 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3600 tw32_f(MAC_MODE, tp->mac_mode);
3601 udelay(40);
3603 ap->state = ANEG_STATE_ACK_DETECT;
3605 /* fallthru */
3606 case ANEG_STATE_ACK_DETECT:
3607 if (ap->ack_match != 0) {
3608 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3609 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3610 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3611 } else {
3612 ap->state = ANEG_STATE_AN_ENABLE;
3614 } else if (ap->ability_match != 0 &&
3615 ap->rxconfig == 0) {
3616 ap->state = ANEG_STATE_AN_ENABLE;
3618 break;
3620 case ANEG_STATE_COMPLETE_ACK_INIT:
3621 if (ap->rxconfig & ANEG_CFG_INVAL) {
3622 ret = ANEG_FAILED;
3623 break;
3625 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3626 MR_LP_ADV_HALF_DUPLEX |
3627 MR_LP_ADV_SYM_PAUSE |
3628 MR_LP_ADV_ASYM_PAUSE |
3629 MR_LP_ADV_REMOTE_FAULT1 |
3630 MR_LP_ADV_REMOTE_FAULT2 |
3631 MR_LP_ADV_NEXT_PAGE |
3632 MR_TOGGLE_RX |
3633 MR_NP_RX);
3634 if (ap->rxconfig & ANEG_CFG_FD)
3635 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3636 if (ap->rxconfig & ANEG_CFG_HD)
3637 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3638 if (ap->rxconfig & ANEG_CFG_PS1)
3639 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3640 if (ap->rxconfig & ANEG_CFG_PS2)
3641 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3642 if (ap->rxconfig & ANEG_CFG_RF1)
3643 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3644 if (ap->rxconfig & ANEG_CFG_RF2)
3645 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3646 if (ap->rxconfig & ANEG_CFG_NP)
3647 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3649 ap->link_time = ap->cur_time;
3651 ap->flags ^= (MR_TOGGLE_TX);
3652 if (ap->rxconfig & 0x0008)
3653 ap->flags |= MR_TOGGLE_RX;
3654 if (ap->rxconfig & ANEG_CFG_NP)
3655 ap->flags |= MR_NP_RX;
3656 ap->flags |= MR_PAGE_RX;
3658 ap->state = ANEG_STATE_COMPLETE_ACK;
3659 ret = ANEG_TIMER_ENAB;
3660 break;
3662 case ANEG_STATE_COMPLETE_ACK:
3663 if (ap->ability_match != 0 &&
3664 ap->rxconfig == 0) {
3665 ap->state = ANEG_STATE_AN_ENABLE;
3666 break;
3668 delta = ap->cur_time - ap->link_time;
3669 if (delta > ANEG_STATE_SETTLE_TIME) {
3670 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3671 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3672 } else {
3673 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3674 !(ap->flags & MR_NP_RX)) {
3675 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3676 } else {
3677 ret = ANEG_FAILED;
3681 break;
3683 case ANEG_STATE_IDLE_DETECT_INIT:
3684 ap->link_time = ap->cur_time;
3685 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3686 tw32_f(MAC_MODE, tp->mac_mode);
3687 udelay(40);
3689 ap->state = ANEG_STATE_IDLE_DETECT;
3690 ret = ANEG_TIMER_ENAB;
3691 break;
3693 case ANEG_STATE_IDLE_DETECT:
3694 if (ap->ability_match != 0 &&
3695 ap->rxconfig == 0) {
3696 ap->state = ANEG_STATE_AN_ENABLE;
3697 break;
3699 delta = ap->cur_time - ap->link_time;
3700 if (delta > ANEG_STATE_SETTLE_TIME) {
3701 /* XXX another gem from the Broadcom driver :( */
3702 ap->state = ANEG_STATE_LINK_OK;
3704 break;
3706 case ANEG_STATE_LINK_OK:
3707 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3708 ret = ANEG_DONE;
3709 break;
3711 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3712 /* ??? unimplemented */
3713 break;
3715 case ANEG_STATE_NEXT_PAGE_WAIT:
3716 /* ??? unimplemented */
3717 break;
3719 default:
3720 ret = ANEG_FAILED;
3721 break;
3724 return ret;
3727 static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
3729 int res = 0;
3730 struct tg3_fiber_aneginfo aninfo;
3731 int status = ANEG_FAILED;
3732 unsigned int tick;
3733 u32 tmp;
3735 tw32_f(MAC_TX_AUTO_NEG, 0);
3737 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3738 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3739 udelay(40);
3741 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3742 udelay(40);
3744 memset(&aninfo, 0, sizeof(aninfo));
3745 aninfo.flags |= MR_AN_ENABLE;
3746 aninfo.state = ANEG_STATE_UNKNOWN;
3747 aninfo.cur_time = 0;
3748 tick = 0;
3749 while (++tick < 195000) {
3750 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3751 if (status == ANEG_DONE || status == ANEG_FAILED)
3752 break;
3754 udelay(1);
3757 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3758 tw32_f(MAC_MODE, tp->mac_mode);
3759 udelay(40);
3761 *txflags = aninfo.txconfig;
3762 *rxflags = aninfo.flags;
3764 if (status == ANEG_DONE &&
3765 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3766 MR_LP_ADV_FULL_DUPLEX)))
3767 res = 1;
3769 return res;
3772 static void tg3_init_bcm8002(struct tg3 *tp)
3774 u32 mac_status = tr32(MAC_STATUS);
3775 int i;
3777 /* Reset when initting first time or we have a link. */
3778 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3779 !(mac_status & MAC_STATUS_PCS_SYNCED))
3780 return;
3782 /* Set PLL lock range. */
3783 tg3_writephy(tp, 0x16, 0x8007);
3785 /* SW reset */
3786 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3788 /* Wait for reset to complete. */
3789 /* XXX schedule_timeout() ... */
3790 for (i = 0; i < 500; i++)
3791 udelay(10);
3793 /* Config mode; select PMA/Ch 1 regs. */
3794 tg3_writephy(tp, 0x10, 0x8411);
3796 /* Enable auto-lock and comdet, select txclk for tx. */
3797 tg3_writephy(tp, 0x11, 0x0a10);
3799 tg3_writephy(tp, 0x18, 0x00a0);
3800 tg3_writephy(tp, 0x16, 0x41ff);
3802 /* Assert and deassert POR. */
3803 tg3_writephy(tp, 0x13, 0x0400);
3804 udelay(40);
3805 tg3_writephy(tp, 0x13, 0x0000);
3807 tg3_writephy(tp, 0x11, 0x0a50);
3808 udelay(40);
3809 tg3_writephy(tp, 0x11, 0x0a10);
3811 /* Wait for signal to stabilize */
3812 /* XXX schedule_timeout() ... */
3813 for (i = 0; i < 15000; i++)
3814 udelay(10);
3816 /* Deselect the channel register so we can read the PHYID
3817 * later.
3819 tg3_writephy(tp, 0x10, 0x8011);
3822 static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3824 u16 flowctrl;
3825 u32 sg_dig_ctrl, sg_dig_status;
3826 u32 serdes_cfg, expected_sg_dig_ctrl;
3827 int workaround, port_a;
3828 int current_link_up;
3830 serdes_cfg = 0;
3831 expected_sg_dig_ctrl = 0;
3832 workaround = 0;
3833 port_a = 1;
3834 current_link_up = 0;
3836 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3837 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3838 workaround = 1;
3839 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3840 port_a = 0;
3842 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3843 /* preserve bits 20-23 for voltage regulator */
3844 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3847 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3849 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
3850 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
3851 if (workaround) {
3852 u32 val = serdes_cfg;
3854 if (port_a)
3855 val |= 0xc010000;
3856 else
3857 val |= 0x4010000;
3858 tw32_f(MAC_SERDES_CFG, val);
3861 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
3863 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3864 tg3_setup_flow_control(tp, 0, 0);
3865 current_link_up = 1;
3867 goto out;
3870 /* Want auto-negotiation. */
3871 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
3873 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3874 if (flowctrl & ADVERTISE_1000XPAUSE)
3875 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3876 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3877 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
3879 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
3880 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
3881 tp->serdes_counter &&
3882 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3883 MAC_STATUS_RCVD_CFG)) ==
3884 MAC_STATUS_PCS_SYNCED)) {
3885 tp->serdes_counter--;
3886 current_link_up = 1;
3887 goto out;
3889 restart_autoneg:
3890 if (workaround)
3891 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
3892 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
3893 udelay(5);
3894 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3896 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3897 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
3898 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3899 MAC_STATUS_SIGNAL_DET)) {
3900 sg_dig_status = tr32(SG_DIG_STATUS);
3901 mac_status = tr32(MAC_STATUS);
3903 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
3904 (mac_status & MAC_STATUS_PCS_SYNCED)) {
3905 u32 local_adv = 0, remote_adv = 0;
3907 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3908 local_adv |= ADVERTISE_1000XPAUSE;
3909 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3910 local_adv |= ADVERTISE_1000XPSE_ASYM;
3912 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
3913 remote_adv |= LPA_1000XPAUSE;
3914 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
3915 remote_adv |= LPA_1000XPAUSE_ASYM;
3917 tg3_setup_flow_control(tp, local_adv, remote_adv);
3918 current_link_up = 1;
3919 tp->serdes_counter = 0;
3920 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
3921 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
3922 if (tp->serdes_counter)
3923 tp->serdes_counter--;
3924 else {
3925 if (workaround) {
3926 u32 val = serdes_cfg;
3928 if (port_a)
3929 val |= 0xc010000;
3930 else
3931 val |= 0x4010000;
3933 tw32_f(MAC_SERDES_CFG, val);
3936 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
3937 udelay(40);
3939 /* Link parallel detection - link is up */
3940 /* only if we have PCS_SYNC and not */
3941 /* receiving config code words */
3942 mac_status = tr32(MAC_STATUS);
3943 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3944 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3945 tg3_setup_flow_control(tp, 0, 0);
3946 current_link_up = 1;
3947 tp->phy_flags |=
3948 TG3_PHYFLG_PARALLEL_DETECT;
3949 tp->serdes_counter =
3950 SERDES_PARALLEL_DET_TIMEOUT;
3951 } else
3952 goto restart_autoneg;
3955 } else {
3956 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3957 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
3960 out:
3961 return current_link_up;
3964 static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3966 int current_link_up = 0;
3968 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
3969 goto out;
3971 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3972 u32 txflags, rxflags;
3973 int i;
3975 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3976 u32 local_adv = 0, remote_adv = 0;
3978 if (txflags & ANEG_CFG_PS1)
3979 local_adv |= ADVERTISE_1000XPAUSE;
3980 if (txflags & ANEG_CFG_PS2)
3981 local_adv |= ADVERTISE_1000XPSE_ASYM;
3983 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3984 remote_adv |= LPA_1000XPAUSE;
3985 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3986 remote_adv |= LPA_1000XPAUSE_ASYM;
3988 tg3_setup_flow_control(tp, local_adv, remote_adv);
3990 current_link_up = 1;
3992 for (i = 0; i < 30; i++) {
3993 udelay(20);
3994 tw32_f(MAC_STATUS,
3995 (MAC_STATUS_SYNC_CHANGED |
3996 MAC_STATUS_CFG_CHANGED));
3997 udelay(40);
3998 if ((tr32(MAC_STATUS) &
3999 (MAC_STATUS_SYNC_CHANGED |
4000 MAC_STATUS_CFG_CHANGED)) == 0)
4001 break;
4004 mac_status = tr32(MAC_STATUS);
4005 if (current_link_up == 0 &&
4006 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4007 !(mac_status & MAC_STATUS_RCVD_CFG))
4008 current_link_up = 1;
4009 } else {
4010 tg3_setup_flow_control(tp, 0, 0);
4012 /* Forcing 1000FD link up. */
4013 current_link_up = 1;
4015 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4016 udelay(40);
4018 tw32_f(MAC_MODE, tp->mac_mode);
4019 udelay(40);
4022 out:
4023 return current_link_up;
4026 static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4028 u32 orig_pause_cfg;
4029 u16 orig_active_speed;
4030 u8 orig_active_duplex;
4031 u32 mac_status;
4032 int current_link_up;
4033 int i;
4035 orig_pause_cfg = tp->link_config.active_flowctrl;
4036 orig_active_speed = tp->link_config.active_speed;
4037 orig_active_duplex = tp->link_config.active_duplex;
4039 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4040 netif_carrier_ok(tp->dev) &&
4041 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4042 mac_status = tr32(MAC_STATUS);
4043 mac_status &= (MAC_STATUS_PCS_SYNCED |
4044 MAC_STATUS_SIGNAL_DET |
4045 MAC_STATUS_CFG_CHANGED |
4046 MAC_STATUS_RCVD_CFG);
4047 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4048 MAC_STATUS_SIGNAL_DET)) {
4049 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4050 MAC_STATUS_CFG_CHANGED));
4051 return 0;
4055 tw32_f(MAC_TX_AUTO_NEG, 0);
4057 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4058 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4059 tw32_f(MAC_MODE, tp->mac_mode);
4060 udelay(40);
4062 if (tp->phy_id == TG3_PHY_ID_BCM8002)
4063 tg3_init_bcm8002(tp);
4065 /* Enable link change event even when serdes polling. */
4066 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4067 udelay(40);
4069 current_link_up = 0;
4070 mac_status = tr32(MAC_STATUS);
4072 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4073 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4074 else
4075 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4077 tp->napi[0].hw_status->status =
4078 (SD_STATUS_UPDATED |
4079 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
4081 for (i = 0; i < 100; i++) {
4082 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4083 MAC_STATUS_CFG_CHANGED));
4084 udelay(5);
4085 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
4086 MAC_STATUS_CFG_CHANGED |
4087 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
4088 break;
4091 mac_status = tr32(MAC_STATUS);
4092 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4093 current_link_up = 0;
4094 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4095 tp->serdes_counter == 0) {
4096 tw32_f(MAC_MODE, (tp->mac_mode |
4097 MAC_MODE_SEND_CONFIGS));
4098 udelay(1);
4099 tw32_f(MAC_MODE, tp->mac_mode);
4103 if (current_link_up == 1) {
4104 tp->link_config.active_speed = SPEED_1000;
4105 tp->link_config.active_duplex = DUPLEX_FULL;
4106 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4107 LED_CTRL_LNKLED_OVERRIDE |
4108 LED_CTRL_1000MBPS_ON));
4109 } else {
4110 tp->link_config.active_speed = SPEED_INVALID;
4111 tp->link_config.active_duplex = DUPLEX_INVALID;
4112 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4113 LED_CTRL_LNKLED_OVERRIDE |
4114 LED_CTRL_TRAFFIC_OVERRIDE));
4117 if (current_link_up != netif_carrier_ok(tp->dev)) {
4118 if (current_link_up)
4119 netif_carrier_on(tp->dev);
4120 else
4121 netif_carrier_off(tp->dev);
4122 tg3_link_report(tp);
4123 } else {
4124 u32 now_pause_cfg = tp->link_config.active_flowctrl;
4125 if (orig_pause_cfg != now_pause_cfg ||
4126 orig_active_speed != tp->link_config.active_speed ||
4127 orig_active_duplex != tp->link_config.active_duplex)
4128 tg3_link_report(tp);
4131 return 0;
4134 static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4136 int current_link_up, err = 0;
4137 u32 bmsr, bmcr;
4138 u16 current_speed;
4139 u8 current_duplex;
4140 u32 local_adv, remote_adv;
4142 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4143 tw32_f(MAC_MODE, tp->mac_mode);
4144 udelay(40);
4146 tw32(MAC_EVENT, 0);
4148 tw32_f(MAC_STATUS,
4149 (MAC_STATUS_SYNC_CHANGED |
4150 MAC_STATUS_CFG_CHANGED |
4151 MAC_STATUS_MI_COMPLETION |
4152 MAC_STATUS_LNKSTATE_CHANGED));
4153 udelay(40);
4155 if (force_reset)
4156 tg3_phy_reset(tp);
4158 current_link_up = 0;
4159 current_speed = SPEED_INVALID;
4160 current_duplex = DUPLEX_INVALID;
4162 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4163 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4164 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4165 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4166 bmsr |= BMSR_LSTATUS;
4167 else
4168 bmsr &= ~BMSR_LSTATUS;
4171 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4173 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
4174 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
4175 /* do nothing, just check for link up at the end */
4176 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4177 u32 adv, new_adv;
4179 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4180 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4181 ADVERTISE_1000XPAUSE |
4182 ADVERTISE_1000XPSE_ASYM |
4183 ADVERTISE_SLCT);
4185 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4187 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4188 new_adv |= ADVERTISE_1000XHALF;
4189 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4190 new_adv |= ADVERTISE_1000XFULL;
4192 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4193 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4194 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4195 tg3_writephy(tp, MII_BMCR, bmcr);
4197 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4198 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
4199 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4201 return err;
4203 } else {
4204 u32 new_bmcr;
4206 bmcr &= ~BMCR_SPEED1000;
4207 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4209 if (tp->link_config.duplex == DUPLEX_FULL)
4210 new_bmcr |= BMCR_FULLDPLX;
4212 if (new_bmcr != bmcr) {
4213 /* BMCR_SPEED1000 is a reserved bit that needs
4214 * to be set on write.
4216 new_bmcr |= BMCR_SPEED1000;
4218 /* Force a linkdown */
4219 if (netif_carrier_ok(tp->dev)) {
4220 u32 adv;
4222 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4223 adv &= ~(ADVERTISE_1000XFULL |
4224 ADVERTISE_1000XHALF |
4225 ADVERTISE_SLCT);
4226 tg3_writephy(tp, MII_ADVERTISE, adv);
4227 tg3_writephy(tp, MII_BMCR, bmcr |
4228 BMCR_ANRESTART |
4229 BMCR_ANENABLE);
4230 udelay(10);
4231 netif_carrier_off(tp->dev);
4233 tg3_writephy(tp, MII_BMCR, new_bmcr);
4234 bmcr = new_bmcr;
4235 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4236 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4237 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4238 ASIC_REV_5714) {
4239 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4240 bmsr |= BMSR_LSTATUS;
4241 else
4242 bmsr &= ~BMSR_LSTATUS;
4244 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4248 if (bmsr & BMSR_LSTATUS) {
4249 current_speed = SPEED_1000;
4250 current_link_up = 1;
4251 if (bmcr & BMCR_FULLDPLX)
4252 current_duplex = DUPLEX_FULL;
4253 else
4254 current_duplex = DUPLEX_HALF;
4256 local_adv = 0;
4257 remote_adv = 0;
4259 if (bmcr & BMCR_ANENABLE) {
4260 u32 common;
4262 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4263 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4264 common = local_adv & remote_adv;
4265 if (common & (ADVERTISE_1000XHALF |
4266 ADVERTISE_1000XFULL)) {
4267 if (common & ADVERTISE_1000XFULL)
4268 current_duplex = DUPLEX_FULL;
4269 else
4270 current_duplex = DUPLEX_HALF;
4271 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4272 /* Link is up via parallel detect */
4273 } else {
4274 current_link_up = 0;
4279 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4280 tg3_setup_flow_control(tp, local_adv, remote_adv);
4282 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4283 if (tp->link_config.active_duplex == DUPLEX_HALF)
4284 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4286 tw32_f(MAC_MODE, tp->mac_mode);
4287 udelay(40);
4289 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4291 tp->link_config.active_speed = current_speed;
4292 tp->link_config.active_duplex = current_duplex;
4294 if (current_link_up != netif_carrier_ok(tp->dev)) {
4295 if (current_link_up)
4296 netif_carrier_on(tp->dev);
4297 else {
4298 netif_carrier_off(tp->dev);
4299 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4301 tg3_link_report(tp);
4303 return err;
4306 static void tg3_serdes_parallel_detect(struct tg3 *tp)
4308 if (tp->serdes_counter) {
4309 /* Give autoneg time to complete. */
4310 tp->serdes_counter--;
4311 return;
4314 if (!netif_carrier_ok(tp->dev) &&
4315 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4316 u32 bmcr;
4318 tg3_readphy(tp, MII_BMCR, &bmcr);
4319 if (bmcr & BMCR_ANENABLE) {
4320 u32 phy1, phy2;
4322 /* Select shadow register 0x1f */
4323 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4324 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
4326 /* Select expansion interrupt status register */
4327 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4328 MII_TG3_DSP_EXP1_INT_STAT);
4329 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4330 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4332 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4333 /* We have signal detect and not receiving
4334 * config code words, link is up by parallel
4335 * detection.
4338 bmcr &= ~BMCR_ANENABLE;
4339 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4340 tg3_writephy(tp, MII_BMCR, bmcr);
4341 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
4344 } else if (netif_carrier_ok(tp->dev) &&
4345 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4346 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
4347 u32 phy2;
4349 /* Select expansion interrupt status register */
4350 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4351 MII_TG3_DSP_EXP1_INT_STAT);
4352 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4353 if (phy2 & 0x20) {
4354 u32 bmcr;
4356 /* Config code words received, turn on autoneg. */
4357 tg3_readphy(tp, MII_BMCR, &bmcr);
4358 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4360 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4366 static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4368 int err;
4370 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
4371 err = tg3_setup_fiber_phy(tp, force_reset);
4372 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4373 err = tg3_setup_fiber_mii_phy(tp, force_reset);
4374 else
4375 err = tg3_setup_copper_phy(tp, force_reset);
4377 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
4378 u32 val, scale;
4380 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4381 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4382 scale = 65;
4383 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4384 scale = 6;
4385 else
4386 scale = 12;
4388 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4389 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4390 tw32(GRC_MISC_CFG, val);
4393 if (tp->link_config.active_speed == SPEED_1000 &&
4394 tp->link_config.active_duplex == DUPLEX_HALF)
4395 tw32(MAC_TX_LENGTHS,
4396 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4397 (6 << TX_LENGTHS_IPG_SHIFT) |
4398 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4399 else
4400 tw32(MAC_TX_LENGTHS,
4401 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4402 (6 << TX_LENGTHS_IPG_SHIFT) |
4403 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4405 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4406 if (netif_carrier_ok(tp->dev)) {
4407 tw32(HOSTCC_STAT_COAL_TICKS,
4408 tp->coal.stats_block_coalesce_usecs);
4409 } else {
4410 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4414 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4415 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4416 if (!netif_carrier_ok(tp->dev))
4417 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4418 tp->pwrmgmt_thresh;
4419 else
4420 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4421 tw32(PCIE_PWR_MGMT_THRESH, val);
4424 return err;
4427 static inline int tg3_irq_sync(struct tg3 *tp)
4429 return tp->irq_sync;
4432 /* This is called whenever we suspect that the system chipset is re-
4433 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4434 * is bogus tx completions. We try to recover by setting the
4435 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4436 * in the workqueue.
4438 static void tg3_tx_recover(struct tg3 *tp)
4440 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4441 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4443 netdev_warn(tp->dev,
4444 "The system may be re-ordering memory-mapped I/O "
4445 "cycles to the network device, attempting to recover. "
4446 "Please report the problem to the driver maintainer "
4447 "and include system chipset information.\n");
4449 spin_lock(&tp->lock);
4450 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
4451 spin_unlock(&tp->lock);
4454 static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
4456 /* Tell compiler to fetch tx indices from memory. */
4457 barrier();
4458 return tnapi->tx_pending -
4459 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
4462 /* Tigon3 never reports partial packet sends. So we do not
4463 * need special logic to handle SKBs that have not had all
4464 * of their frags sent yet, like SunGEM does.
4466 static void tg3_tx(struct tg3_napi *tnapi)
4468 struct tg3 *tp = tnapi->tp;
4469 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
4470 u32 sw_idx = tnapi->tx_cons;
4471 struct netdev_queue *txq;
4472 int index = tnapi - tp->napi;
4474 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
4475 index--;
4477 txq = netdev_get_tx_queue(tp->dev, index);
4479 while (sw_idx != hw_idx) {
4480 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
4481 struct sk_buff *skb = ri->skb;
4482 int i, tx_bug = 0;
4484 if (unlikely(skb == NULL)) {
4485 tg3_tx_recover(tp);
4486 return;
4489 pci_unmap_single(tp->pdev,
4490 dma_unmap_addr(ri, mapping),
4491 skb_headlen(skb),
4492 PCI_DMA_TODEVICE);
4494 ri->skb = NULL;
4496 sw_idx = NEXT_TX(sw_idx);
4498 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4499 ri = &tnapi->tx_buffers[sw_idx];
4500 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4501 tx_bug = 1;
4503 pci_unmap_page(tp->pdev,
4504 dma_unmap_addr(ri, mapping),
4505 skb_shinfo(skb)->frags[i].size,
4506 PCI_DMA_TODEVICE);
4507 sw_idx = NEXT_TX(sw_idx);
4510 dev_kfree_skb(skb);
4512 if (unlikely(tx_bug)) {
4513 tg3_tx_recover(tp);
4514 return;
4518 tnapi->tx_cons = sw_idx;
4520 /* Need to make the tx_cons update visible to tg3_start_xmit()
4521 * before checking for netif_queue_stopped(). Without the
4522 * memory barrier, there is a small possibility that tg3_start_xmit()
4523 * will miss it and cause the queue to be stopped forever.
4525 smp_mb();
4527 if (unlikely(netif_tx_queue_stopped(txq) &&
4528 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
4529 __netif_tx_lock(txq, smp_processor_id());
4530 if (netif_tx_queue_stopped(txq) &&
4531 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
4532 netif_tx_wake_queue(txq);
4533 __netif_tx_unlock(txq);
4537 static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4539 if (!ri->skb)
4540 return;
4542 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
4543 map_sz, PCI_DMA_FROMDEVICE);
4544 dev_kfree_skb_any(ri->skb);
4545 ri->skb = NULL;
4548 /* Returns size of skb allocated or < 0 on error.
4550 * We only need to fill in the address because the other members
4551 * of the RX descriptor are invariant, see tg3_init_rings.
4553 * Note the purposeful assymetry of cpu vs. chip accesses. For
4554 * posting buffers we only dirty the first cache line of the RX
4555 * descriptor (containing the address). Whereas for the RX status
4556 * buffers the cpu only reads the last cacheline of the RX descriptor
4557 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4559 static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
4560 u32 opaque_key, u32 dest_idx_unmasked)
4562 struct tg3_rx_buffer_desc *desc;
4563 struct ring_info *map;
4564 struct sk_buff *skb;
4565 dma_addr_t mapping;
4566 int skb_size, dest_idx;
4568 switch (opaque_key) {
4569 case RXD_OPAQUE_RING_STD:
4570 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
4571 desc = &tpr->rx_std[dest_idx];
4572 map = &tpr->rx_std_buffers[dest_idx];
4573 skb_size = tp->rx_pkt_map_sz;
4574 break;
4576 case RXD_OPAQUE_RING_JUMBO:
4577 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
4578 desc = &tpr->rx_jmb[dest_idx].std;
4579 map = &tpr->rx_jmb_buffers[dest_idx];
4580 skb_size = TG3_RX_JMB_MAP_SZ;
4581 break;
4583 default:
4584 return -EINVAL;
4587 /* Do not overwrite any of the map or rp information
4588 * until we are sure we can commit to a new buffer.
4590 * Callers depend upon this behavior and assume that
4591 * we leave everything unchanged if we fail.
4593 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
4594 if (skb == NULL)
4595 return -ENOMEM;
4597 skb_reserve(skb, tp->rx_offset);
4599 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
4600 PCI_DMA_FROMDEVICE);
4601 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4602 dev_kfree_skb(skb);
4603 return -EIO;
4606 map->skb = skb;
4607 dma_unmap_addr_set(map, mapping, mapping);
4609 desc->addr_hi = ((u64)mapping >> 32);
4610 desc->addr_lo = ((u64)mapping & 0xffffffff);
4612 return skb_size;
4615 /* We only need to move over in the address because the other
4616 * members of the RX descriptor are invariant. See notes above
4617 * tg3_alloc_rx_skb for full details.
4619 static void tg3_recycle_rx(struct tg3_napi *tnapi,
4620 struct tg3_rx_prodring_set *dpr,
4621 u32 opaque_key, int src_idx,
4622 u32 dest_idx_unmasked)
4624 struct tg3 *tp = tnapi->tp;
4625 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4626 struct ring_info *src_map, *dest_map;
4627 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
4628 int dest_idx;
4630 switch (opaque_key) {
4631 case RXD_OPAQUE_RING_STD:
4632 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
4633 dest_desc = &dpr->rx_std[dest_idx];
4634 dest_map = &dpr->rx_std_buffers[dest_idx];
4635 src_desc = &spr->rx_std[src_idx];
4636 src_map = &spr->rx_std_buffers[src_idx];
4637 break;
4639 case RXD_OPAQUE_RING_JUMBO:
4640 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
4641 dest_desc = &dpr->rx_jmb[dest_idx].std;
4642 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4643 src_desc = &spr->rx_jmb[src_idx].std;
4644 src_map = &spr->rx_jmb_buffers[src_idx];
4645 break;
4647 default:
4648 return;
4651 dest_map->skb = src_map->skb;
4652 dma_unmap_addr_set(dest_map, mapping,
4653 dma_unmap_addr(src_map, mapping));
4654 dest_desc->addr_hi = src_desc->addr_hi;
4655 dest_desc->addr_lo = src_desc->addr_lo;
4657 /* Ensure that the update to the skb happens after the physical
4658 * addresses have been transferred to the new BD location.
4660 smp_wmb();
4662 src_map->skb = NULL;
4665 /* The RX ring scheme is composed of multiple rings which post fresh
4666 * buffers to the chip, and one special ring the chip uses to report
4667 * status back to the host.
4669 * The special ring reports the status of received packets to the
4670 * host. The chip does not write into the original descriptor the
4671 * RX buffer was obtained from. The chip simply takes the original
4672 * descriptor as provided by the host, updates the status and length
4673 * field, then writes this into the next status ring entry.
4675 * Each ring the host uses to post buffers to the chip is described
4676 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4677 * it is first placed into the on-chip ram. When the packet's length
4678 * is known, it walks down the TG3_BDINFO entries to select the ring.
4679 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4680 * which is within the range of the new packet's length is chosen.
4682 * The "separate ring for rx status" scheme may sound queer, but it makes
4683 * sense from a cache coherency perspective. If only the host writes
4684 * to the buffer post rings, and only the chip writes to the rx status
4685 * rings, then cache lines never move beyond shared-modified state.
4686 * If both the host and chip were to write into the same ring, cache line
4687 * eviction could occur since both entities want it in an exclusive state.
4689 static int tg3_rx(struct tg3_napi *tnapi, int budget)
4691 struct tg3 *tp = tnapi->tp;
4692 u32 work_mask, rx_std_posted = 0;
4693 u32 std_prod_idx, jmb_prod_idx;
4694 u32 sw_idx = tnapi->rx_rcb_ptr;
4695 u16 hw_idx;
4696 int received;
4697 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
4699 hw_idx = *(tnapi->rx_rcb_prod_idx);
4701 * We need to order the read of hw_idx and the read of
4702 * the opaque cookie.
4704 rmb();
4705 work_mask = 0;
4706 received = 0;
4707 std_prod_idx = tpr->rx_std_prod_idx;
4708 jmb_prod_idx = tpr->rx_jmb_prod_idx;
4709 while (sw_idx != hw_idx && budget > 0) {
4710 struct ring_info *ri;
4711 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
4712 unsigned int len;
4713 struct sk_buff *skb;
4714 dma_addr_t dma_addr;
4715 u32 opaque_key, desc_idx, *post_ptr;
4717 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4718 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4719 if (opaque_key == RXD_OPAQUE_RING_STD) {
4720 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
4721 dma_addr = dma_unmap_addr(ri, mapping);
4722 skb = ri->skb;
4723 post_ptr = &std_prod_idx;
4724 rx_std_posted++;
4725 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
4726 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
4727 dma_addr = dma_unmap_addr(ri, mapping);
4728 skb = ri->skb;
4729 post_ptr = &jmb_prod_idx;
4730 } else
4731 goto next_pkt_nopost;
4733 work_mask |= opaque_key;
4735 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4736 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4737 drop_it:
4738 tg3_recycle_rx(tnapi, tpr, opaque_key,
4739 desc_idx, *post_ptr);
4740 drop_it_no_recycle:
4741 /* Other statistics kept track of by card. */
4742 tp->rx_dropped++;
4743 goto next_pkt;
4746 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4747 ETH_FCS_LEN;
4749 if (len > TG3_RX_COPY_THRESH(tp)) {
4750 int skb_size;
4752 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
4753 *post_ptr);
4754 if (skb_size < 0)
4755 goto drop_it;
4757 pci_unmap_single(tp->pdev, dma_addr, skb_size,
4758 PCI_DMA_FROMDEVICE);
4760 /* Ensure that the update to the skb happens
4761 * after the usage of the old DMA mapping.
4763 smp_wmb();
4765 ri->skb = NULL;
4767 skb_put(skb, len);
4768 } else {
4769 struct sk_buff *copy_skb;
4771 tg3_recycle_rx(tnapi, tpr, opaque_key,
4772 desc_idx, *post_ptr);
4774 copy_skb = netdev_alloc_skb(tp->dev, len +
4775 TG3_RAW_IP_ALIGN);
4776 if (copy_skb == NULL)
4777 goto drop_it_no_recycle;
4779 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
4780 skb_put(copy_skb, len);
4781 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4782 skb_copy_from_linear_data(skb, copy_skb->data, len);
4783 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4785 /* We'll reuse the original ring buffer. */
4786 skb = copy_skb;
4789 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4790 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4791 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4792 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4793 skb->ip_summed = CHECKSUM_UNNECESSARY;
4794 else
4795 skb_checksum_none_assert(skb);
4797 skb->protocol = eth_type_trans(skb, tp->dev);
4799 if (len > (tp->dev->mtu + ETH_HLEN) &&
4800 skb->protocol != htons(ETH_P_8021Q)) {
4801 dev_kfree_skb(skb);
4802 goto drop_it_no_recycle;
4805 if (desc->type_flags & RXD_FLAG_VLAN &&
4806 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4807 __vlan_hwaccel_put_tag(skb,
4808 desc->err_vlan & RXD_VLAN_MASK);
4810 napi_gro_receive(&tnapi->napi, skb);
4812 received++;
4813 budget--;
4815 next_pkt:
4816 (*post_ptr)++;
4818 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4819 tpr->rx_std_prod_idx = std_prod_idx &
4820 tp->rx_std_ring_mask;
4821 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4822 tpr->rx_std_prod_idx);
4823 work_mask &= ~RXD_OPAQUE_RING_STD;
4824 rx_std_posted = 0;
4826 next_pkt_nopost:
4827 sw_idx++;
4828 sw_idx &= tp->rx_ret_ring_mask;
4830 /* Refresh hw_idx to see if there is new work */
4831 if (sw_idx == hw_idx) {
4832 hw_idx = *(tnapi->rx_rcb_prod_idx);
4833 rmb();
4837 /* ACK the status ring. */
4838 tnapi->rx_rcb_ptr = sw_idx;
4839 tw32_rx_mbox(tnapi->consmbox, sw_idx);
4841 /* Refill RX ring(s). */
4842 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
4843 if (work_mask & RXD_OPAQUE_RING_STD) {
4844 tpr->rx_std_prod_idx = std_prod_idx &
4845 tp->rx_std_ring_mask;
4846 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4847 tpr->rx_std_prod_idx);
4849 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4850 tpr->rx_jmb_prod_idx = jmb_prod_idx &
4851 tp->rx_jmb_ring_mask;
4852 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4853 tpr->rx_jmb_prod_idx);
4855 mmiowb();
4856 } else if (work_mask) {
4857 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4858 * updated before the producer indices can be updated.
4860 smp_wmb();
4862 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
4863 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
4865 if (tnapi != &tp->napi[1])
4866 napi_schedule(&tp->napi[1].napi);
4869 return received;
4872 static void tg3_poll_link(struct tg3 *tp)
4874 /* handle link change and other phy events */
4875 if (!(tp->tg3_flags &
4876 (TG3_FLAG_USE_LINKCHG_REG |
4877 TG3_FLAG_POLL_SERDES))) {
4878 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4880 if (sblk->status & SD_STATUS_LINK_CHG) {
4881 sblk->status = SD_STATUS_UPDATED |
4882 (sblk->status & ~SD_STATUS_LINK_CHG);
4883 spin_lock(&tp->lock);
4884 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4885 tw32_f(MAC_STATUS,
4886 (MAC_STATUS_SYNC_CHANGED |
4887 MAC_STATUS_CFG_CHANGED |
4888 MAC_STATUS_MI_COMPLETION |
4889 MAC_STATUS_LNKSTATE_CHANGED));
4890 udelay(40);
4891 } else
4892 tg3_setup_phy(tp, 0);
4893 spin_unlock(&tp->lock);
4898 static int tg3_rx_prodring_xfer(struct tg3 *tp,
4899 struct tg3_rx_prodring_set *dpr,
4900 struct tg3_rx_prodring_set *spr)
4902 u32 si, di, cpycnt, src_prod_idx;
4903 int i, err = 0;
4905 while (1) {
4906 src_prod_idx = spr->rx_std_prod_idx;
4908 /* Make sure updates to the rx_std_buffers[] entries and the
4909 * standard producer index are seen in the correct order.
4911 smp_rmb();
4913 if (spr->rx_std_cons_idx == src_prod_idx)
4914 break;
4916 if (spr->rx_std_cons_idx < src_prod_idx)
4917 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4918 else
4919 cpycnt = tp->rx_std_ring_mask + 1 -
4920 spr->rx_std_cons_idx;
4922 cpycnt = min(cpycnt,
4923 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
4925 si = spr->rx_std_cons_idx;
4926 di = dpr->rx_std_prod_idx;
4928 for (i = di; i < di + cpycnt; i++) {
4929 if (dpr->rx_std_buffers[i].skb) {
4930 cpycnt = i - di;
4931 err = -ENOSPC;
4932 break;
4936 if (!cpycnt)
4937 break;
4939 /* Ensure that updates to the rx_std_buffers ring and the
4940 * shadowed hardware producer ring from tg3_recycle_skb() are
4941 * ordered correctly WRT the skb check above.
4943 smp_rmb();
4945 memcpy(&dpr->rx_std_buffers[di],
4946 &spr->rx_std_buffers[si],
4947 cpycnt * sizeof(struct ring_info));
4949 for (i = 0; i < cpycnt; i++, di++, si++) {
4950 struct tg3_rx_buffer_desc *sbd, *dbd;
4951 sbd = &spr->rx_std[si];
4952 dbd = &dpr->rx_std[di];
4953 dbd->addr_hi = sbd->addr_hi;
4954 dbd->addr_lo = sbd->addr_lo;
4957 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
4958 tp->rx_std_ring_mask;
4959 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
4960 tp->rx_std_ring_mask;
4963 while (1) {
4964 src_prod_idx = spr->rx_jmb_prod_idx;
4966 /* Make sure updates to the rx_jmb_buffers[] entries and
4967 * the jumbo producer index are seen in the correct order.
4969 smp_rmb();
4971 if (spr->rx_jmb_cons_idx == src_prod_idx)
4972 break;
4974 if (spr->rx_jmb_cons_idx < src_prod_idx)
4975 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
4976 else
4977 cpycnt = tp->rx_jmb_ring_mask + 1 -
4978 spr->rx_jmb_cons_idx;
4980 cpycnt = min(cpycnt,
4981 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
4983 si = spr->rx_jmb_cons_idx;
4984 di = dpr->rx_jmb_prod_idx;
4986 for (i = di; i < di + cpycnt; i++) {
4987 if (dpr->rx_jmb_buffers[i].skb) {
4988 cpycnt = i - di;
4989 err = -ENOSPC;
4990 break;
4994 if (!cpycnt)
4995 break;
4997 /* Ensure that updates to the rx_jmb_buffers ring and the
4998 * shadowed hardware producer ring from tg3_recycle_skb() are
4999 * ordered correctly WRT the skb check above.
5001 smp_rmb();
5003 memcpy(&dpr->rx_jmb_buffers[di],
5004 &spr->rx_jmb_buffers[si],
5005 cpycnt * sizeof(struct ring_info));
5007 for (i = 0; i < cpycnt; i++, di++, si++) {
5008 struct tg3_rx_buffer_desc *sbd, *dbd;
5009 sbd = &spr->rx_jmb[si].std;
5010 dbd = &dpr->rx_jmb[di].std;
5011 dbd->addr_hi = sbd->addr_hi;
5012 dbd->addr_lo = sbd->addr_lo;
5015 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5016 tp->rx_jmb_ring_mask;
5017 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5018 tp->rx_jmb_ring_mask;
5021 return err;
5024 static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5026 struct tg3 *tp = tnapi->tp;
5028 /* run TX completion thread */
5029 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
5030 tg3_tx(tnapi);
5031 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5032 return work_done;
5035 /* run RX thread, within the bounds set by NAPI.
5036 * All RX "locking" is done by ensuring outside
5037 * code synchronizes with tg3->napi.poll()
5039 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
5040 work_done += tg3_rx(tnapi, budget - work_done);
5042 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
5043 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
5044 int i, err = 0;
5045 u32 std_prod_idx = dpr->rx_std_prod_idx;
5046 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
5048 for (i = 1; i < tp->irq_cnt; i++)
5049 err |= tg3_rx_prodring_xfer(tp, dpr,
5050 &tp->napi[i].prodring);
5052 wmb();
5054 if (std_prod_idx != dpr->rx_std_prod_idx)
5055 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5056 dpr->rx_std_prod_idx);
5058 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5059 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5060 dpr->rx_jmb_prod_idx);
5062 mmiowb();
5064 if (err)
5065 tw32_f(HOSTCC_MODE, tp->coal_now);
5068 return work_done;
5071 static int tg3_poll_msix(struct napi_struct *napi, int budget)
5073 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5074 struct tg3 *tp = tnapi->tp;
5075 int work_done = 0;
5076 struct tg3_hw_status *sblk = tnapi->hw_status;
5078 while (1) {
5079 work_done = tg3_poll_work(tnapi, work_done, budget);
5081 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5082 goto tx_recovery;
5084 if (unlikely(work_done >= budget))
5085 break;
5087 /* tp->last_tag is used in tg3_int_reenable() below
5088 * to tell the hw how much work has been processed,
5089 * so we must read it before checking for more work.
5091 tnapi->last_tag = sblk->status_tag;
5092 tnapi->last_irq_tag = tnapi->last_tag;
5093 rmb();
5095 /* check for RX/TX work to do */
5096 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5097 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
5098 napi_complete(napi);
5099 /* Reenable interrupts. */
5100 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5101 mmiowb();
5102 break;
5106 return work_done;
5108 tx_recovery:
5109 /* work_done is guaranteed to be less than budget. */
5110 napi_complete(napi);
5111 schedule_work(&tp->reset_task);
5112 return work_done;
5115 static int tg3_poll(struct napi_struct *napi, int budget)
5117 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5118 struct tg3 *tp = tnapi->tp;
5119 int work_done = 0;
5120 struct tg3_hw_status *sblk = tnapi->hw_status;
5122 while (1) {
5123 tg3_poll_link(tp);
5125 work_done = tg3_poll_work(tnapi, work_done, budget);
5127 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5128 goto tx_recovery;
5130 if (unlikely(work_done >= budget))
5131 break;
5133 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
5134 /* tp->last_tag is used in tg3_int_reenable() below
5135 * to tell the hw how much work has been processed,
5136 * so we must read it before checking for more work.
5138 tnapi->last_tag = sblk->status_tag;
5139 tnapi->last_irq_tag = tnapi->last_tag;
5140 rmb();
5141 } else
5142 sblk->status &= ~SD_STATUS_UPDATED;
5144 if (likely(!tg3_has_work(tnapi))) {
5145 napi_complete(napi);
5146 tg3_int_reenable(tnapi);
5147 break;
5151 return work_done;
5153 tx_recovery:
5154 /* work_done is guaranteed to be less than budget. */
5155 napi_complete(napi);
5156 schedule_work(&tp->reset_task);
5157 return work_done;
5160 static void tg3_napi_disable(struct tg3 *tp)
5162 int i;
5164 for (i = tp->irq_cnt - 1; i >= 0; i--)
5165 napi_disable(&tp->napi[i].napi);
5168 static void tg3_napi_enable(struct tg3 *tp)
5170 int i;
5172 for (i = 0; i < tp->irq_cnt; i++)
5173 napi_enable(&tp->napi[i].napi);
5176 static void tg3_napi_init(struct tg3 *tp)
5178 int i;
5180 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5181 for (i = 1; i < tp->irq_cnt; i++)
5182 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5185 static void tg3_napi_fini(struct tg3 *tp)
5187 int i;
5189 for (i = 0; i < tp->irq_cnt; i++)
5190 netif_napi_del(&tp->napi[i].napi);
5193 static inline void tg3_netif_stop(struct tg3 *tp)
5195 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5196 tg3_napi_disable(tp);
5197 netif_tx_disable(tp->dev);
5200 static inline void tg3_netif_start(struct tg3 *tp)
5202 /* NOTE: unconditional netif_tx_wake_all_queues is only
5203 * appropriate so long as all callers are assured to
5204 * have free tx slots (such as after tg3_init_hw)
5206 netif_tx_wake_all_queues(tp->dev);
5208 tg3_napi_enable(tp);
5209 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5210 tg3_enable_ints(tp);
5213 static void tg3_irq_quiesce(struct tg3 *tp)
5215 int i;
5217 BUG_ON(tp->irq_sync);
5219 tp->irq_sync = 1;
5220 smp_mb();
5222 for (i = 0; i < tp->irq_cnt; i++)
5223 synchronize_irq(tp->napi[i].irq_vec);
5226 /* Fully shutdown all tg3 driver activity elsewhere in the system.
5227 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5228 * with as well. Most of the time, this is not necessary except when
5229 * shutting down the device.
5231 static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5233 spin_lock_bh(&tp->lock);
5234 if (irq_sync)
5235 tg3_irq_quiesce(tp);
5238 static inline void tg3_full_unlock(struct tg3 *tp)
5240 spin_unlock_bh(&tp->lock);
5243 /* One-shot MSI handler - Chip automatically disables interrupt
5244 * after sending MSI so driver doesn't have to do it.
5246 static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
5248 struct tg3_napi *tnapi = dev_id;
5249 struct tg3 *tp = tnapi->tp;
5251 prefetch(tnapi->hw_status);
5252 if (tnapi->rx_rcb)
5253 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5255 if (likely(!tg3_irq_sync(tp)))
5256 napi_schedule(&tnapi->napi);
5258 return IRQ_HANDLED;
5261 /* MSI ISR - No need to check for interrupt sharing and no need to
5262 * flush status block and interrupt mailbox. PCI ordering rules
5263 * guarantee that MSI will arrive after the status block.
5265 static irqreturn_t tg3_msi(int irq, void *dev_id)
5267 struct tg3_napi *tnapi = dev_id;
5268 struct tg3 *tp = tnapi->tp;
5270 prefetch(tnapi->hw_status);
5271 if (tnapi->rx_rcb)
5272 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5274 * Writing any value to intr-mbox-0 clears PCI INTA# and
5275 * chip-internal interrupt pending events.
5276 * Writing non-zero to intr-mbox-0 additional tells the
5277 * NIC to stop sending us irqs, engaging "in-intr-handler"
5278 * event coalescing.
5280 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
5281 if (likely(!tg3_irq_sync(tp)))
5282 napi_schedule(&tnapi->napi);
5284 return IRQ_RETVAL(1);
5287 static irqreturn_t tg3_interrupt(int irq, void *dev_id)
5289 struct tg3_napi *tnapi = dev_id;
5290 struct tg3 *tp = tnapi->tp;
5291 struct tg3_hw_status *sblk = tnapi->hw_status;
5292 unsigned int handled = 1;
5294 /* In INTx mode, it is possible for the interrupt to arrive at
5295 * the CPU before the status block posted prior to the interrupt.
5296 * Reading the PCI State register will confirm whether the
5297 * interrupt is ours and will flush the status block.
5299 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5300 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5301 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5302 handled = 0;
5303 goto out;
5308 * Writing any value to intr-mbox-0 clears PCI INTA# and
5309 * chip-internal interrupt pending events.
5310 * Writing non-zero to intr-mbox-0 additional tells the
5311 * NIC to stop sending us irqs, engaging "in-intr-handler"
5312 * event coalescing.
5314 * Flush the mailbox to de-assert the IRQ immediately to prevent
5315 * spurious interrupts. The flush impacts performance but
5316 * excessive spurious interrupts can be worse in some cases.
5318 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
5319 if (tg3_irq_sync(tp))
5320 goto out;
5321 sblk->status &= ~SD_STATUS_UPDATED;
5322 if (likely(tg3_has_work(tnapi))) {
5323 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5324 napi_schedule(&tnapi->napi);
5325 } else {
5326 /* No work, shared interrupt perhaps? re-enable
5327 * interrupts, and flush that PCI write
5329 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5330 0x00000000);
5332 out:
5333 return IRQ_RETVAL(handled);
5336 static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
5338 struct tg3_napi *tnapi = dev_id;
5339 struct tg3 *tp = tnapi->tp;
5340 struct tg3_hw_status *sblk = tnapi->hw_status;
5341 unsigned int handled = 1;
5343 /* In INTx mode, it is possible for the interrupt to arrive at
5344 * the CPU before the status block posted prior to the interrupt.
5345 * Reading the PCI State register will confirm whether the
5346 * interrupt is ours and will flush the status block.
5348 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
5349 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5350 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5351 handled = 0;
5352 goto out;
5357 * writing any value to intr-mbox-0 clears PCI INTA# and
5358 * chip-internal interrupt pending events.
5359 * writing non-zero to intr-mbox-0 additional tells the
5360 * NIC to stop sending us irqs, engaging "in-intr-handler"
5361 * event coalescing.
5363 * Flush the mailbox to de-assert the IRQ immediately to prevent
5364 * spurious interrupts. The flush impacts performance but
5365 * excessive spurious interrupts can be worse in some cases.
5367 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
5370 * In a shared interrupt configuration, sometimes other devices'
5371 * interrupts will scream. We record the current status tag here
5372 * so that the above check can report that the screaming interrupts
5373 * are unhandled. Eventually they will be silenced.
5375 tnapi->last_irq_tag = sblk->status_tag;
5377 if (tg3_irq_sync(tp))
5378 goto out;
5380 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5382 napi_schedule(&tnapi->napi);
5384 out:
5385 return IRQ_RETVAL(handled);
5388 /* ISR for interrupt test */
5389 static irqreturn_t tg3_test_isr(int irq, void *dev_id)
5391 struct tg3_napi *tnapi = dev_id;
5392 struct tg3 *tp = tnapi->tp;
5393 struct tg3_hw_status *sblk = tnapi->hw_status;
5395 if ((sblk->status & SD_STATUS_UPDATED) ||
5396 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5397 tg3_disable_ints(tp);
5398 return IRQ_RETVAL(1);
5400 return IRQ_RETVAL(0);
5403 static int tg3_init_hw(struct tg3 *, int);
5404 static int tg3_halt(struct tg3 *, int, int);
5406 /* Restart hardware after configuration changes, self-test, etc.
5407 * Invoked with tp->lock held.
5409 static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
5410 __releases(tp->lock)
5411 __acquires(tp->lock)
5413 int err;
5415 err = tg3_init_hw(tp, reset_phy);
5416 if (err) {
5417 netdev_err(tp->dev,
5418 "Failed to re-initialize device, aborting\n");
5419 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5420 tg3_full_unlock(tp);
5421 del_timer_sync(&tp->timer);
5422 tp->irq_sync = 0;
5423 tg3_napi_enable(tp);
5424 dev_close(tp->dev);
5425 tg3_full_lock(tp, 0);
5427 return err;
5430 #ifdef CONFIG_NET_POLL_CONTROLLER
5431 static void tg3_poll_controller(struct net_device *dev)
5433 int i;
5434 struct tg3 *tp = netdev_priv(dev);
5436 for (i = 0; i < tp->irq_cnt; i++)
5437 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
5439 #endif
5441 static void tg3_reset_task(struct work_struct *work)
5443 struct tg3 *tp = container_of(work, struct tg3, reset_task);
5444 int err;
5445 unsigned int restart_timer;
5447 tg3_full_lock(tp, 0);
5449 if (!netif_running(tp->dev)) {
5450 tg3_full_unlock(tp);
5451 return;
5454 tg3_full_unlock(tp);
5456 tg3_phy_stop(tp);
5458 tg3_netif_stop(tp);
5460 tg3_full_lock(tp, 1);
5462 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5463 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5465 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5466 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5467 tp->write32_rx_mbox = tg3_write_flush_reg32;
5468 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5469 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5472 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
5473 err = tg3_init_hw(tp, 1);
5474 if (err)
5475 goto out;
5477 tg3_netif_start(tp);
5479 if (restart_timer)
5480 mod_timer(&tp->timer, jiffies + 1);
5482 out:
5483 tg3_full_unlock(tp);
5485 if (!err)
5486 tg3_phy_start(tp);
5489 static void tg3_dump_short_state(struct tg3 *tp)
5491 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5492 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5493 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5494 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
5497 static void tg3_tx_timeout(struct net_device *dev)
5499 struct tg3 *tp = netdev_priv(dev);
5501 if (netif_msg_tx_err(tp)) {
5502 netdev_err(dev, "transmit timed out, resetting\n");
5503 tg3_dump_short_state(tp);
5506 schedule_work(&tp->reset_task);
5509 /* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5510 static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5512 u32 base = (u32) mapping & 0xffffffff;
5514 return (base > 0xffffdcc0) && (base + len + 8 < base);
5517 /* Test for DMA addresses > 40-bit */
5518 static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5519 int len)
5521 #if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
5522 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
5523 return ((u64) mapping + len) > DMA_BIT_MASK(40);
5524 return 0;
5525 #else
5526 return 0;
5527 #endif
5530 static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
5532 /* Workaround 4GB and 40-bit hardware DMA bugs. */
5533 static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5534 struct sk_buff *skb, u32 last_plus_one,
5535 u32 *start, u32 base_flags, u32 mss)
5537 struct tg3 *tp = tnapi->tp;
5538 struct sk_buff *new_skb;
5539 dma_addr_t new_addr = 0;
5540 u32 entry = *start;
5541 int i, ret = 0;
5543 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5544 new_skb = skb_copy(skb, GFP_ATOMIC);
5545 else {
5546 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5548 new_skb = skb_copy_expand(skb,
5549 skb_headroom(skb) + more_headroom,
5550 skb_tailroom(skb), GFP_ATOMIC);
5553 if (!new_skb) {
5554 ret = -1;
5555 } else {
5556 /* New SKB is guaranteed to be linear. */
5557 entry = *start;
5558 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5559 PCI_DMA_TODEVICE);
5560 /* Make sure the mapping succeeded */
5561 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5562 ret = -1;
5563 dev_kfree_skb(new_skb);
5564 new_skb = NULL;
5566 /* Make sure new skb does not cross any 4G boundaries.
5567 * Drop the packet if it does.
5569 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5570 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5571 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5572 PCI_DMA_TODEVICE);
5573 ret = -1;
5574 dev_kfree_skb(new_skb);
5575 new_skb = NULL;
5576 } else {
5577 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
5578 base_flags, 1 | (mss << 1));
5579 *start = NEXT_TX(entry);
5583 /* Now clean up the sw ring entries. */
5584 i = 0;
5585 while (entry != last_plus_one) {
5586 int len;
5588 if (i == 0)
5589 len = skb_headlen(skb);
5590 else
5591 len = skb_shinfo(skb)->frags[i-1].size;
5593 pci_unmap_single(tp->pdev,
5594 dma_unmap_addr(&tnapi->tx_buffers[entry],
5595 mapping),
5596 len, PCI_DMA_TODEVICE);
5597 if (i == 0) {
5598 tnapi->tx_buffers[entry].skb = new_skb;
5599 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5600 new_addr);
5601 } else {
5602 tnapi->tx_buffers[entry].skb = NULL;
5604 entry = NEXT_TX(entry);
5605 i++;
5608 dev_kfree_skb(skb);
5610 return ret;
5613 static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5614 dma_addr_t mapping, int len, u32 flags,
5615 u32 mss_and_is_end)
5617 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5618 int is_end = (mss_and_is_end & 0x1);
5619 u32 mss = (mss_and_is_end >> 1);
5620 u32 vlan_tag = 0;
5622 if (is_end)
5623 flags |= TXD_FLAG_END;
5624 if (flags & TXD_FLAG_VLAN) {
5625 vlan_tag = flags >> 16;
5626 flags &= 0xffff;
5628 vlan_tag |= (mss << TXD_MSS_SHIFT);
5630 txd->addr_hi = ((u64) mapping >> 32);
5631 txd->addr_lo = ((u64) mapping & 0xffffffff);
5632 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5633 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5636 /* hard_start_xmit for devices that don't have any bugs and
5637 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
5639 static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5640 struct net_device *dev)
5642 struct tg3 *tp = netdev_priv(dev);
5643 u32 len, entry, base_flags, mss;
5644 dma_addr_t mapping;
5645 struct tg3_napi *tnapi;
5646 struct netdev_queue *txq;
5647 unsigned int i, last;
5649 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5650 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
5651 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
5652 tnapi++;
5654 /* We are running in BH disabled context with netif_tx_lock
5655 * and TX reclaim runs via tp->napi.poll inside of a software
5656 * interrupt. Furthermore, IRQ processing runs lockless so we have
5657 * no IRQ context deadlocks to worry about either. Rejoice!
5659 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
5660 if (!netif_tx_queue_stopped(txq)) {
5661 netif_tx_stop_queue(txq);
5663 /* This is a hard error, log it. */
5664 netdev_err(dev,
5665 "BUG! Tx Ring full when queue awake!\n");
5667 return NETDEV_TX_BUSY;
5670 entry = tnapi->tx_prod;
5671 base_flags = 0;
5672 mss = skb_shinfo(skb)->gso_size;
5673 if (mss) {
5674 int tcp_opt_len, ip_tcp_len;
5675 u32 hdrlen;
5677 if (skb_header_cloned(skb) &&
5678 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5679 dev_kfree_skb(skb);
5680 goto out_unlock;
5683 if (skb_is_gso_v6(skb)) {
5684 hdrlen = skb_headlen(skb) - ETH_HLEN;
5685 } else {
5686 struct iphdr *iph = ip_hdr(skb);
5688 tcp_opt_len = tcp_optlen(skb);
5689 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5691 iph->check = 0;
5692 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
5693 hdrlen = ip_tcp_len + tcp_opt_len;
5696 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5697 mss |= (hdrlen & 0xc) << 12;
5698 if (hdrlen & 0x10)
5699 base_flags |= 0x00000010;
5700 base_flags |= (hdrlen & 0x3e0) << 5;
5701 } else
5702 mss |= hdrlen << 9;
5704 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5705 TXD_FLAG_CPU_POST_DMA);
5707 tcp_hdr(skb)->check = 0;
5709 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5710 base_flags |= TXD_FLAG_TCPUDP_CSUM;
5713 if (vlan_tx_tag_present(skb))
5714 base_flags |= (TXD_FLAG_VLAN |
5715 (vlan_tx_tag_get(skb) << 16));
5717 len = skb_headlen(skb);
5719 /* Queue skb data, a.k.a. the main skb fragment. */
5720 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5721 if (pci_dma_mapping_error(tp->pdev, mapping)) {
5722 dev_kfree_skb(skb);
5723 goto out_unlock;
5726 tnapi->tx_buffers[entry].skb = skb;
5727 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
5729 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
5730 !mss && skb->len > VLAN_ETH_FRAME_LEN)
5731 base_flags |= TXD_FLAG_JMB_PKT;
5733 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
5734 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5736 entry = NEXT_TX(entry);
5738 /* Now loop through additional data fragments, and queue them. */
5739 if (skb_shinfo(skb)->nr_frags > 0) {
5740 last = skb_shinfo(skb)->nr_frags - 1;
5741 for (i = 0; i <= last; i++) {
5742 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5744 len = frag->size;
5745 mapping = pci_map_page(tp->pdev,
5746 frag->page,
5747 frag->page_offset,
5748 len, PCI_DMA_TODEVICE);
5749 if (pci_dma_mapping_error(tp->pdev, mapping))
5750 goto dma_error;
5752 tnapi->tx_buffers[entry].skb = NULL;
5753 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5754 mapping);
5756 tg3_set_txd(tnapi, entry, mapping, len,
5757 base_flags, (i == last) | (mss << 1));
5759 entry = NEXT_TX(entry);
5763 /* Packets are ready, update Tx producer idx local and on card. */
5764 tw32_tx_mbox(tnapi->prodmbox, entry);
5766 tnapi->tx_prod = entry;
5767 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
5768 netif_tx_stop_queue(txq);
5770 /* netif_tx_stop_queue() must be done before checking
5771 * checking tx index in tg3_tx_avail() below, because in
5772 * tg3_tx(), we update tx index before checking for
5773 * netif_tx_queue_stopped().
5775 smp_mb();
5776 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
5777 netif_tx_wake_queue(txq);
5780 out_unlock:
5781 mmiowb();
5783 return NETDEV_TX_OK;
5785 dma_error:
5786 last = i;
5787 entry = tnapi->tx_prod;
5788 tnapi->tx_buffers[entry].skb = NULL;
5789 pci_unmap_single(tp->pdev,
5790 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
5791 skb_headlen(skb),
5792 PCI_DMA_TODEVICE);
5793 for (i = 0; i <= last; i++) {
5794 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5795 entry = NEXT_TX(entry);
5797 pci_unmap_page(tp->pdev,
5798 dma_unmap_addr(&tnapi->tx_buffers[entry],
5799 mapping),
5800 frag->size, PCI_DMA_TODEVICE);
5803 dev_kfree_skb(skb);
5804 return NETDEV_TX_OK;
5807 static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5808 struct net_device *);
5810 /* Use GSO to workaround a rare TSO bug that may be triggered when the
5811 * TSO header is greater than 80 bytes.
5813 static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5815 struct sk_buff *segs, *nskb;
5816 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
5818 /* Estimate the number of fragments in the worst case */
5819 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
5820 netif_stop_queue(tp->dev);
5822 /* netif_tx_stop_queue() must be done before checking
5823 * checking tx index in tg3_tx_avail() below, because in
5824 * tg3_tx(), we update tx index before checking for
5825 * netif_tx_queue_stopped().
5827 smp_mb();
5828 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
5829 return NETDEV_TX_BUSY;
5831 netif_wake_queue(tp->dev);
5834 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
5835 if (IS_ERR(segs))
5836 goto tg3_tso_bug_end;
5838 do {
5839 nskb = segs;
5840 segs = segs->next;
5841 nskb->next = NULL;
5842 tg3_start_xmit_dma_bug(nskb, tp->dev);
5843 } while (segs);
5845 tg3_tso_bug_end:
5846 dev_kfree_skb(skb);
5848 return NETDEV_TX_OK;
5851 /* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5852 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5854 static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5855 struct net_device *dev)
5857 struct tg3 *tp = netdev_priv(dev);
5858 u32 len, entry, base_flags, mss;
5859 int would_hit_hwbug;
5860 dma_addr_t mapping;
5861 struct tg3_napi *tnapi;
5862 struct netdev_queue *txq;
5863 unsigned int i, last;
5865 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5866 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
5867 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
5868 tnapi++;
5870 /* We are running in BH disabled context with netif_tx_lock
5871 * and TX reclaim runs via tp->napi.poll inside of a software
5872 * interrupt. Furthermore, IRQ processing runs lockless so we have
5873 * no IRQ context deadlocks to worry about either. Rejoice!
5875 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
5876 if (!netif_tx_queue_stopped(txq)) {
5877 netif_tx_stop_queue(txq);
5879 /* This is a hard error, log it. */
5880 netdev_err(dev,
5881 "BUG! Tx Ring full when queue awake!\n");
5883 return NETDEV_TX_BUSY;
5886 entry = tnapi->tx_prod;
5887 base_flags = 0;
5888 if (skb->ip_summed == CHECKSUM_PARTIAL)
5889 base_flags |= TXD_FLAG_TCPUDP_CSUM;
5891 mss = skb_shinfo(skb)->gso_size;
5892 if (mss) {
5893 struct iphdr *iph;
5894 u32 tcp_opt_len, hdr_len;
5896 if (skb_header_cloned(skb) &&
5897 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5898 dev_kfree_skb(skb);
5899 goto out_unlock;
5902 iph = ip_hdr(skb);
5903 tcp_opt_len = tcp_optlen(skb);
5905 if (skb_is_gso_v6(skb)) {
5906 hdr_len = skb_headlen(skb) - ETH_HLEN;
5907 } else {
5908 u32 ip_tcp_len;
5910 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5911 hdr_len = ip_tcp_len + tcp_opt_len;
5913 iph->check = 0;
5914 iph->tot_len = htons(mss + hdr_len);
5917 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
5918 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
5919 return tg3_tso_bug(tp, skb);
5921 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5922 TXD_FLAG_CPU_POST_DMA);
5924 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
5925 tcp_hdr(skb)->check = 0;
5926 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
5927 } else
5928 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5929 iph->daddr, 0,
5930 IPPROTO_TCP,
5933 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5934 mss |= (hdr_len & 0xc) << 12;
5935 if (hdr_len & 0x10)
5936 base_flags |= 0x00000010;
5937 base_flags |= (hdr_len & 0x3e0) << 5;
5938 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
5939 mss |= hdr_len << 9;
5940 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5941 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
5942 if (tcp_opt_len || iph->ihl > 5) {
5943 int tsflags;
5945 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
5946 mss |= (tsflags << 11);
5948 } else {
5949 if (tcp_opt_len || iph->ihl > 5) {
5950 int tsflags;
5952 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
5953 base_flags |= tsflags << 12;
5958 if (vlan_tx_tag_present(skb))
5959 base_flags |= (TXD_FLAG_VLAN |
5960 (vlan_tx_tag_get(skb) << 16));
5962 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
5963 !mss && skb->len > VLAN_ETH_FRAME_LEN)
5964 base_flags |= TXD_FLAG_JMB_PKT;
5966 len = skb_headlen(skb);
5968 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5969 if (pci_dma_mapping_error(tp->pdev, mapping)) {
5970 dev_kfree_skb(skb);
5971 goto out_unlock;
5974 tnapi->tx_buffers[entry].skb = skb;
5975 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
5977 would_hit_hwbug = 0;
5979 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5980 would_hit_hwbug = 1;
5982 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5983 tg3_4g_overflow_test(mapping, len))
5984 would_hit_hwbug = 1;
5986 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5987 tg3_40bit_overflow_test(tp, mapping, len))
5988 would_hit_hwbug = 1;
5990 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
5991 would_hit_hwbug = 1;
5993 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
5994 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5996 entry = NEXT_TX(entry);
5998 /* Now loop through additional data fragments, and queue them. */
5999 if (skb_shinfo(skb)->nr_frags > 0) {
6000 last = skb_shinfo(skb)->nr_frags - 1;
6001 for (i = 0; i <= last; i++) {
6002 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6004 len = frag->size;
6005 mapping = pci_map_page(tp->pdev,
6006 frag->page,
6007 frag->page_offset,
6008 len, PCI_DMA_TODEVICE);
6010 tnapi->tx_buffers[entry].skb = NULL;
6011 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
6012 mapping);
6013 if (pci_dma_mapping_error(tp->pdev, mapping))
6014 goto dma_error;
6016 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
6017 len <= 8)
6018 would_hit_hwbug = 1;
6020 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6021 tg3_4g_overflow_test(mapping, len))
6022 would_hit_hwbug = 1;
6024 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6025 tg3_40bit_overflow_test(tp, mapping, len))
6026 would_hit_hwbug = 1;
6028 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
6029 tg3_set_txd(tnapi, entry, mapping, len,
6030 base_flags, (i == last)|(mss << 1));
6031 else
6032 tg3_set_txd(tnapi, entry, mapping, len,
6033 base_flags, (i == last));
6035 entry = NEXT_TX(entry);
6039 if (would_hit_hwbug) {
6040 u32 last_plus_one = entry;
6041 u32 start;
6043 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6044 start &= (TG3_TX_RING_SIZE - 1);
6046 /* If the workaround fails due to memory/mapping
6047 * failure, silently drop this packet.
6049 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
6050 &start, base_flags, mss))
6051 goto out_unlock;
6053 entry = start;
6056 /* Packets are ready, update Tx producer idx local and on card. */
6057 tw32_tx_mbox(tnapi->prodmbox, entry);
6059 tnapi->tx_prod = entry;
6060 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
6061 netif_tx_stop_queue(txq);
6063 /* netif_tx_stop_queue() must be done before checking
6064 * checking tx index in tg3_tx_avail() below, because in
6065 * tg3_tx(), we update tx index before checking for
6066 * netif_tx_queue_stopped().
6068 smp_mb();
6069 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
6070 netif_tx_wake_queue(txq);
6073 out_unlock:
6074 mmiowb();
6076 return NETDEV_TX_OK;
6078 dma_error:
6079 last = i;
6080 entry = tnapi->tx_prod;
6081 tnapi->tx_buffers[entry].skb = NULL;
6082 pci_unmap_single(tp->pdev,
6083 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
6084 skb_headlen(skb),
6085 PCI_DMA_TODEVICE);
6086 for (i = 0; i <= last; i++) {
6087 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6088 entry = NEXT_TX(entry);
6090 pci_unmap_page(tp->pdev,
6091 dma_unmap_addr(&tnapi->tx_buffers[entry],
6092 mapping),
6093 frag->size, PCI_DMA_TODEVICE);
6096 dev_kfree_skb(skb);
6097 return NETDEV_TX_OK;
6100 static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6101 int new_mtu)
6103 dev->mtu = new_mtu;
6105 if (new_mtu > ETH_DATA_LEN) {
6106 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
6107 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
6108 ethtool_op_set_tso(dev, 0);
6109 } else {
6110 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
6112 } else {
6113 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
6114 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
6115 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
6119 static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6121 struct tg3 *tp = netdev_priv(dev);
6122 int err;
6124 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6125 return -EINVAL;
6127 if (!netif_running(dev)) {
6128 /* We'll just catch it later when the
6129 * device is up'd.
6131 tg3_set_mtu(dev, tp, new_mtu);
6132 return 0;
6135 tg3_phy_stop(tp);
6137 tg3_netif_stop(tp);
6139 tg3_full_lock(tp, 1);
6141 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6143 tg3_set_mtu(dev, tp, new_mtu);
6145 err = tg3_restart_hw(tp, 0);
6147 if (!err)
6148 tg3_netif_start(tp);
6150 tg3_full_unlock(tp);
6152 if (!err)
6153 tg3_phy_start(tp);
6155 return err;
6158 static void tg3_rx_prodring_free(struct tg3 *tp,
6159 struct tg3_rx_prodring_set *tpr)
6161 int i;
6163 if (tpr != &tp->napi[0].prodring) {
6164 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
6165 i = (i + 1) & tp->rx_std_ring_mask)
6166 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6167 tp->rx_pkt_map_sz);
6169 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6170 for (i = tpr->rx_jmb_cons_idx;
6171 i != tpr->rx_jmb_prod_idx;
6172 i = (i + 1) & tp->rx_jmb_ring_mask) {
6173 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6174 TG3_RX_JMB_MAP_SZ);
6178 return;
6181 for (i = 0; i <= tp->rx_std_ring_mask; i++)
6182 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6183 tp->rx_pkt_map_sz);
6185 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6186 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
6187 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
6188 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6189 TG3_RX_JMB_MAP_SZ);
6193 /* Initialize rx rings for packet processing.
6195 * The chip has been shut down and the driver detached from
6196 * the networking, so no interrupts or new tx packets will
6197 * end up in the driver. tp->{tx,}lock are held and thus
6198 * we may not sleep.
6200 static int tg3_rx_prodring_alloc(struct tg3 *tp,
6201 struct tg3_rx_prodring_set *tpr)
6203 u32 i, rx_pkt_dma_sz;
6205 tpr->rx_std_cons_idx = 0;
6206 tpr->rx_std_prod_idx = 0;
6207 tpr->rx_jmb_cons_idx = 0;
6208 tpr->rx_jmb_prod_idx = 0;
6210 if (tpr != &tp->napi[0].prodring) {
6211 memset(&tpr->rx_std_buffers[0], 0,
6212 TG3_RX_STD_BUFF_RING_SIZE(tp));
6213 if (tpr->rx_jmb_buffers)
6214 memset(&tpr->rx_jmb_buffers[0], 0,
6215 TG3_RX_JMB_BUFF_RING_SIZE(tp));
6216 goto done;
6219 /* Zero out all descriptors. */
6220 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
6222 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
6223 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
6224 tp->dev->mtu > ETH_DATA_LEN)
6225 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6226 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
6228 /* Initialize invariants of the rings, we only set this
6229 * stuff once. This works because the card does not
6230 * write into the rx buffer posting rings.
6232 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
6233 struct tg3_rx_buffer_desc *rxd;
6235 rxd = &tpr->rx_std[i];
6236 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
6237 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6238 rxd->opaque = (RXD_OPAQUE_RING_STD |
6239 (i << RXD_OPAQUE_INDEX_SHIFT));
6242 /* Now allocate fresh SKBs for each rx ring. */
6243 for (i = 0; i < tp->rx_pending; i++) {
6244 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
6245 netdev_warn(tp->dev,
6246 "Using a smaller RX standard ring. Only "
6247 "%d out of %d buffers were allocated "
6248 "successfully\n", i, tp->rx_pending);
6249 if (i == 0)
6250 goto initfail;
6251 tp->rx_pending = i;
6252 break;
6256 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
6257 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
6258 goto done;
6260 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
6262 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6263 goto done;
6265 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
6266 struct tg3_rx_buffer_desc *rxd;
6268 rxd = &tpr->rx_jmb[i].std;
6269 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6270 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6271 RXD_FLAG_JUMBO;
6272 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6273 (i << RXD_OPAQUE_INDEX_SHIFT));
6276 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6277 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
6278 netdev_warn(tp->dev,
6279 "Using a smaller RX jumbo ring. Only %d "
6280 "out of %d buffers were allocated "
6281 "successfully\n", i, tp->rx_jumbo_pending);
6282 if (i == 0)
6283 goto initfail;
6284 tp->rx_jumbo_pending = i;
6285 break;
6289 done:
6290 return 0;
6292 initfail:
6293 tg3_rx_prodring_free(tp, tpr);
6294 return -ENOMEM;
6297 static void tg3_rx_prodring_fini(struct tg3 *tp,
6298 struct tg3_rx_prodring_set *tpr)
6300 kfree(tpr->rx_std_buffers);
6301 tpr->rx_std_buffers = NULL;
6302 kfree(tpr->rx_jmb_buffers);
6303 tpr->rx_jmb_buffers = NULL;
6304 if (tpr->rx_std) {
6305 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6306 tpr->rx_std, tpr->rx_std_mapping);
6307 tpr->rx_std = NULL;
6309 if (tpr->rx_jmb) {
6310 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6311 tpr->rx_jmb, tpr->rx_jmb_mapping);
6312 tpr->rx_jmb = NULL;
6316 static int tg3_rx_prodring_init(struct tg3 *tp,
6317 struct tg3_rx_prodring_set *tpr)
6319 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6320 GFP_KERNEL);
6321 if (!tpr->rx_std_buffers)
6322 return -ENOMEM;
6324 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6325 TG3_RX_STD_RING_BYTES(tp),
6326 &tpr->rx_std_mapping,
6327 GFP_KERNEL);
6328 if (!tpr->rx_std)
6329 goto err_out;
6331 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6332 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
6333 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
6334 GFP_KERNEL);
6335 if (!tpr->rx_jmb_buffers)
6336 goto err_out;
6338 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6339 TG3_RX_JMB_RING_BYTES(tp),
6340 &tpr->rx_jmb_mapping,
6341 GFP_KERNEL);
6342 if (!tpr->rx_jmb)
6343 goto err_out;
6346 return 0;
6348 err_out:
6349 tg3_rx_prodring_fini(tp, tpr);
6350 return -ENOMEM;
6353 /* Free up pending packets in all rx/tx rings.
6355 * The chip has been shut down and the driver detached from
6356 * the networking, so no interrupts or new tx packets will
6357 * end up in the driver. tp->{tx,}lock is not held and we are not
6358 * in an interrupt context and thus may sleep.
6360 static void tg3_free_rings(struct tg3 *tp)
6362 int i, j;
6364 for (j = 0; j < tp->irq_cnt; j++) {
6365 struct tg3_napi *tnapi = &tp->napi[j];
6367 tg3_rx_prodring_free(tp, &tnapi->prodring);
6369 if (!tnapi->tx_buffers)
6370 continue;
6372 for (i = 0; i < TG3_TX_RING_SIZE; ) {
6373 struct ring_info *txp;
6374 struct sk_buff *skb;
6375 unsigned int k;
6377 txp = &tnapi->tx_buffers[i];
6378 skb = txp->skb;
6380 if (skb == NULL) {
6381 i++;
6382 continue;
6385 pci_unmap_single(tp->pdev,
6386 dma_unmap_addr(txp, mapping),
6387 skb_headlen(skb),
6388 PCI_DMA_TODEVICE);
6389 txp->skb = NULL;
6391 i++;
6393 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6394 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6395 pci_unmap_page(tp->pdev,
6396 dma_unmap_addr(txp, mapping),
6397 skb_shinfo(skb)->frags[k].size,
6398 PCI_DMA_TODEVICE);
6399 i++;
6402 dev_kfree_skb_any(skb);
6407 /* Initialize tx/rx rings for packet processing.
6409 * The chip has been shut down and the driver detached from
6410 * the networking, so no interrupts or new tx packets will
6411 * end up in the driver. tp->{tx,}lock are held and thus
6412 * we may not sleep.
6414 static int tg3_init_rings(struct tg3 *tp)
6416 int i;
6418 /* Free up all the SKBs. */
6419 tg3_free_rings(tp);
6421 for (i = 0; i < tp->irq_cnt; i++) {
6422 struct tg3_napi *tnapi = &tp->napi[i];
6424 tnapi->last_tag = 0;
6425 tnapi->last_irq_tag = 0;
6426 tnapi->hw_status->status = 0;
6427 tnapi->hw_status->status_tag = 0;
6428 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6430 tnapi->tx_prod = 0;
6431 tnapi->tx_cons = 0;
6432 if (tnapi->tx_ring)
6433 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
6435 tnapi->rx_rcb_ptr = 0;
6436 if (tnapi->rx_rcb)
6437 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
6439 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
6440 tg3_free_rings(tp);
6441 return -ENOMEM;
6445 return 0;
6449 * Must not be invoked with interrupt sources disabled and
6450 * the hardware shutdown down.
6452 static void tg3_free_consistent(struct tg3 *tp)
6454 int i;
6456 for (i = 0; i < tp->irq_cnt; i++) {
6457 struct tg3_napi *tnapi = &tp->napi[i];
6459 if (tnapi->tx_ring) {
6460 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
6461 tnapi->tx_ring, tnapi->tx_desc_mapping);
6462 tnapi->tx_ring = NULL;
6465 kfree(tnapi->tx_buffers);
6466 tnapi->tx_buffers = NULL;
6468 if (tnapi->rx_rcb) {
6469 dma_free_coherent(&tp->pdev->dev,
6470 TG3_RX_RCB_RING_BYTES(tp),
6471 tnapi->rx_rcb,
6472 tnapi->rx_rcb_mapping);
6473 tnapi->rx_rcb = NULL;
6476 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6478 if (tnapi->hw_status) {
6479 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6480 tnapi->hw_status,
6481 tnapi->status_mapping);
6482 tnapi->hw_status = NULL;
6486 if (tp->hw_stats) {
6487 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6488 tp->hw_stats, tp->stats_mapping);
6489 tp->hw_stats = NULL;
6494 * Must not be invoked with interrupt sources disabled and
6495 * the hardware shutdown down. Can sleep.
6497 static int tg3_alloc_consistent(struct tg3 *tp)
6499 int i;
6501 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6502 sizeof(struct tg3_hw_stats),
6503 &tp->stats_mapping,
6504 GFP_KERNEL);
6505 if (!tp->hw_stats)
6506 goto err_out;
6508 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6510 for (i = 0; i < tp->irq_cnt; i++) {
6511 struct tg3_napi *tnapi = &tp->napi[i];
6512 struct tg3_hw_status *sblk;
6514 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6515 TG3_HW_STATUS_SIZE,
6516 &tnapi->status_mapping,
6517 GFP_KERNEL);
6518 if (!tnapi->hw_status)
6519 goto err_out;
6521 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6522 sblk = tnapi->hw_status;
6524 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6525 goto err_out;
6527 /* If multivector TSS is enabled, vector 0 does not handle
6528 * tx interrupts. Don't allocate any resources for it.
6530 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6531 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6532 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6533 TG3_TX_RING_SIZE,
6534 GFP_KERNEL);
6535 if (!tnapi->tx_buffers)
6536 goto err_out;
6538 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6539 TG3_TX_RING_BYTES,
6540 &tnapi->tx_desc_mapping,
6541 GFP_KERNEL);
6542 if (!tnapi->tx_ring)
6543 goto err_out;
6547 * When RSS is enabled, the status block format changes
6548 * slightly. The "rx_jumbo_consumer", "reserved",
6549 * and "rx_mini_consumer" members get mapped to the
6550 * other three rx return ring producer indexes.
6552 switch (i) {
6553 default:
6554 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6555 break;
6556 case 2:
6557 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6558 break;
6559 case 3:
6560 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6561 break;
6562 case 4:
6563 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6564 break;
6568 * If multivector RSS is enabled, vector 0 does not handle
6569 * rx or tx interrupts. Don't allocate any resources for it.
6571 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6572 continue;
6574 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6575 TG3_RX_RCB_RING_BYTES(tp),
6576 &tnapi->rx_rcb_mapping,
6577 GFP_KERNEL);
6578 if (!tnapi->rx_rcb)
6579 goto err_out;
6581 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
6584 return 0;
6586 err_out:
6587 tg3_free_consistent(tp);
6588 return -ENOMEM;
6591 #define MAX_WAIT_CNT 1000
6593 /* To stop a block, clear the enable bit and poll till it
6594 * clears. tp->lock is held.
6596 static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
6598 unsigned int i;
6599 u32 val;
6601 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6602 switch (ofs) {
6603 case RCVLSC_MODE:
6604 case DMAC_MODE:
6605 case MBFREE_MODE:
6606 case BUFMGR_MODE:
6607 case MEMARB_MODE:
6608 /* We can't enable/disable these bits of the
6609 * 5705/5750, just say success.
6611 return 0;
6613 default:
6614 break;
6618 val = tr32(ofs);
6619 val &= ~enable_bit;
6620 tw32_f(ofs, val);
6622 for (i = 0; i < MAX_WAIT_CNT; i++) {
6623 udelay(100);
6624 val = tr32(ofs);
6625 if ((val & enable_bit) == 0)
6626 break;
6629 if (i == MAX_WAIT_CNT && !silent) {
6630 dev_err(&tp->pdev->dev,
6631 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6632 ofs, enable_bit);
6633 return -ENODEV;
6636 return 0;
6639 /* tp->lock is held. */
6640 static int tg3_abort_hw(struct tg3 *tp, int silent)
6642 int i, err;
6644 tg3_disable_ints(tp);
6646 tp->rx_mode &= ~RX_MODE_ENABLE;
6647 tw32_f(MAC_RX_MODE, tp->rx_mode);
6648 udelay(10);
6650 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6651 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6652 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6653 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6654 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6655 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
6657 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6658 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6659 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6660 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6661 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6662 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6663 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
6665 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6666 tw32_f(MAC_MODE, tp->mac_mode);
6667 udelay(40);
6669 tp->tx_mode &= ~TX_MODE_ENABLE;
6670 tw32_f(MAC_TX_MODE, tp->tx_mode);
6672 for (i = 0; i < MAX_WAIT_CNT; i++) {
6673 udelay(100);
6674 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6675 break;
6677 if (i >= MAX_WAIT_CNT) {
6678 dev_err(&tp->pdev->dev,
6679 "%s timed out, TX_MODE_ENABLE will not clear "
6680 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
6681 err |= -ENODEV;
6684 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
6685 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6686 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
6688 tw32(FTQ_RESET, 0xffffffff);
6689 tw32(FTQ_RESET, 0x00000000);
6691 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6692 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
6694 for (i = 0; i < tp->irq_cnt; i++) {
6695 struct tg3_napi *tnapi = &tp->napi[i];
6696 if (tnapi->hw_status)
6697 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6699 if (tp->hw_stats)
6700 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6702 return err;
6705 static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6707 int i;
6708 u32 apedata;
6710 /* NCSI does not support APE events */
6711 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6712 return;
6714 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6715 if (apedata != APE_SEG_SIG_MAGIC)
6716 return;
6718 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
6719 if (!(apedata & APE_FW_STATUS_READY))
6720 return;
6722 /* Wait for up to 1 millisecond for APE to service previous event. */
6723 for (i = 0; i < 10; i++) {
6724 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6725 return;
6727 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6729 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6730 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6731 event | APE_EVENT_STATUS_EVENT_PENDING);
6733 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6735 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6736 break;
6738 udelay(100);
6741 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6742 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6745 static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6747 u32 event;
6748 u32 apedata;
6750 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6751 return;
6753 switch (kind) {
6754 case RESET_KIND_INIT:
6755 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6756 APE_HOST_SEG_SIG_MAGIC);
6757 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6758 APE_HOST_SEG_LEN_MAGIC);
6759 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6760 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6761 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6762 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
6763 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6764 APE_HOST_BEHAV_NO_PHYLOCK);
6765 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6766 TG3_APE_HOST_DRVR_STATE_START);
6768 event = APE_EVENT_STATUS_STATE_START;
6769 break;
6770 case RESET_KIND_SHUTDOWN:
6771 /* With the interface we are currently using,
6772 * APE does not track driver state. Wiping
6773 * out the HOST SEGMENT SIGNATURE forces
6774 * the APE to assume OS absent status.
6776 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
6778 if (device_may_wakeup(&tp->pdev->dev) &&
6779 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6780 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6781 TG3_APE_HOST_WOL_SPEED_AUTO);
6782 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6783 } else
6784 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6786 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6788 event = APE_EVENT_STATUS_STATE_UNLOAD;
6789 break;
6790 case RESET_KIND_SUSPEND:
6791 event = APE_EVENT_STATUS_STATE_SUSPEND;
6792 break;
6793 default:
6794 return;
6797 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6799 tg3_ape_send_event(tp, event);
6802 /* tp->lock is held. */
6803 static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6805 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6806 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
6808 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6809 switch (kind) {
6810 case RESET_KIND_INIT:
6811 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6812 DRV_STATE_START);
6813 break;
6815 case RESET_KIND_SHUTDOWN:
6816 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6817 DRV_STATE_UNLOAD);
6818 break;
6820 case RESET_KIND_SUSPEND:
6821 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6822 DRV_STATE_SUSPEND);
6823 break;
6825 default:
6826 break;
6830 if (kind == RESET_KIND_INIT ||
6831 kind == RESET_KIND_SUSPEND)
6832 tg3_ape_driver_state_change(tp, kind);
6835 /* tp->lock is held. */
6836 static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6838 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6839 switch (kind) {
6840 case RESET_KIND_INIT:
6841 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6842 DRV_STATE_START_DONE);
6843 break;
6845 case RESET_KIND_SHUTDOWN:
6846 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6847 DRV_STATE_UNLOAD_DONE);
6848 break;
6850 default:
6851 break;
6855 if (kind == RESET_KIND_SHUTDOWN)
6856 tg3_ape_driver_state_change(tp, kind);
6859 /* tp->lock is held. */
6860 static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6862 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6863 switch (kind) {
6864 case RESET_KIND_INIT:
6865 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6866 DRV_STATE_START);
6867 break;
6869 case RESET_KIND_SHUTDOWN:
6870 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6871 DRV_STATE_UNLOAD);
6872 break;
6874 case RESET_KIND_SUSPEND:
6875 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6876 DRV_STATE_SUSPEND);
6877 break;
6879 default:
6880 break;
6885 static int tg3_poll_fw(struct tg3 *tp)
6887 int i;
6888 u32 val;
6890 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6891 /* Wait up to 20ms for init done. */
6892 for (i = 0; i < 200; i++) {
6893 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6894 return 0;
6895 udelay(100);
6897 return -ENODEV;
6900 /* Wait for firmware initialization to complete. */
6901 for (i = 0; i < 100000; i++) {
6902 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6903 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6904 break;
6905 udelay(10);
6908 /* Chip might not be fitted with firmware. Some Sun onboard
6909 * parts are configured like that. So don't signal the timeout
6910 * of the above loop as an error, but do report the lack of
6911 * running firmware once.
6913 if (i >= 100000 &&
6914 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6915 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6917 netdev_info(tp->dev, "No firmware running\n");
6920 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6921 /* The 57765 A0 needs a little more
6922 * time to do some important work.
6924 mdelay(10);
6927 return 0;
6930 /* Save PCI command register before chip reset */
6931 static void tg3_save_pci_state(struct tg3 *tp)
6933 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
6936 /* Restore PCI state after chip reset */
6937 static void tg3_restore_pci_state(struct tg3 *tp)
6939 u32 val;
6941 /* Re-enable indirect register accesses. */
6942 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6943 tp->misc_host_ctrl);
6945 /* Set MAX PCI retry to zero. */
6946 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6947 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6948 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6949 val |= PCISTATE_RETRY_SAME_DMA;
6950 /* Allow reads and writes to the APE register and memory space. */
6951 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6952 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6953 PCISTATE_ALLOW_APE_SHMEM_WR |
6954 PCISTATE_ALLOW_APE_PSPACE_WR;
6955 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6957 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
6959 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6960 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6961 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
6962 else {
6963 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6964 tp->pci_cacheline_sz);
6965 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6966 tp->pci_lat_timer);
6970 /* Make sure PCI-X relaxed ordering bit is clear. */
6971 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
6972 u16 pcix_cmd;
6974 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6975 &pcix_cmd);
6976 pcix_cmd &= ~PCI_X_CMD_ERO;
6977 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6978 pcix_cmd);
6981 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
6983 /* Chip reset on 5780 will reset MSI enable bit,
6984 * so need to restore it.
6986 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6987 u16 ctrl;
6989 pci_read_config_word(tp->pdev,
6990 tp->msi_cap + PCI_MSI_FLAGS,
6991 &ctrl);
6992 pci_write_config_word(tp->pdev,
6993 tp->msi_cap + PCI_MSI_FLAGS,
6994 ctrl | PCI_MSI_FLAGS_ENABLE);
6995 val = tr32(MSGINT_MODE);
6996 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7001 static void tg3_stop_fw(struct tg3 *);
7003 /* tp->lock is held. */
7004 static int tg3_chip_reset(struct tg3 *tp)
7006 u32 val;
7007 void (*write_op)(struct tg3 *, u32, u32);
7008 int i, err;
7010 tg3_nvram_lock(tp);
7012 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7014 /* No matching tg3_nvram_unlock() after this because
7015 * chip reset below will undo the nvram lock.
7017 tp->nvram_lock_cnt = 0;
7019 /* GRC_MISC_CFG core clock reset will clear the memory
7020 * enable bit in PCI register 4 and the MSI enable bit
7021 * on some chips, so we save relevant registers here.
7023 tg3_save_pci_state(tp);
7025 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
7026 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
7027 tw32(GRC_FASTBOOT_PC, 0);
7030 * We must avoid the readl() that normally takes place.
7031 * It locks machines, causes machine checks, and other
7032 * fun things. So, temporarily disable the 5701
7033 * hardware workaround, while we do the reset.
7035 write_op = tp->write32;
7036 if (write_op == tg3_write_flush_reg32)
7037 tp->write32 = tg3_write32;
7039 /* Prevent the irq handler from reading or writing PCI registers
7040 * during chip reset when the memory enable bit in the PCI command
7041 * register may be cleared. The chip does not generate interrupt
7042 * at this time, but the irq handler may still be called due to irq
7043 * sharing or irqpoll.
7045 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
7046 for (i = 0; i < tp->irq_cnt; i++) {
7047 struct tg3_napi *tnapi = &tp->napi[i];
7048 if (tnapi->hw_status) {
7049 tnapi->hw_status->status = 0;
7050 tnapi->hw_status->status_tag = 0;
7052 tnapi->last_tag = 0;
7053 tnapi->last_irq_tag = 0;
7055 smp_mb();
7057 for (i = 0; i < tp->irq_cnt; i++)
7058 synchronize_irq(tp->napi[i].irq_vec);
7060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7061 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7062 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7065 /* do the reset */
7066 val = GRC_MISC_CFG_CORECLK_RESET;
7068 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
7069 /* Force PCIe 1.0a mode */
7070 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
7071 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
7072 tr32(TG3_PCIE_PHY_TSTCTL) ==
7073 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7074 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7076 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7077 tw32(GRC_MISC_CFG, (1 << 29));
7078 val |= (1 << 29);
7082 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7083 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7084 tw32(GRC_VCPU_EXT_CTRL,
7085 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7088 /* Manage gphy power for all CPMU absent PCIe devices. */
7089 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7090 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
7091 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
7093 tw32(GRC_MISC_CFG, val);
7095 /* restore 5701 hardware bug workaround write method */
7096 tp->write32 = write_op;
7098 /* Unfortunately, we have to delay before the PCI read back.
7099 * Some 575X chips even will not respond to a PCI cfg access
7100 * when the reset command is given to the chip.
7102 * How do these hardware designers expect things to work
7103 * properly if the PCI write is posted for a long period
7104 * of time? It is always necessary to have some method by
7105 * which a register read back can occur to push the write
7106 * out which does the reset.
7108 * For most tg3 variants the trick below was working.
7109 * Ho hum...
7111 udelay(120);
7113 /* Flush PCI posted writes. The normal MMIO registers
7114 * are inaccessible at this time so this is the only
7115 * way to make this reliably (actually, this is no longer
7116 * the case, see above). I tried to use indirect
7117 * register read/write but this upset some 5701 variants.
7119 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7121 udelay(120);
7123 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
7124 u16 val16;
7126 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7127 int i;
7128 u32 cfg_val;
7130 /* Wait for link training to complete. */
7131 for (i = 0; i < 5000; i++)
7132 udelay(100);
7134 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7135 pci_write_config_dword(tp->pdev, 0xc4,
7136 cfg_val | (1 << 15));
7139 /* Clear the "no snoop" and "relaxed ordering" bits. */
7140 pci_read_config_word(tp->pdev,
7141 tp->pcie_cap + PCI_EXP_DEVCTL,
7142 &val16);
7143 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7144 PCI_EXP_DEVCTL_NOSNOOP_EN);
7146 * Older PCIe devices only support the 128 byte
7147 * MPS setting. Enforce the restriction.
7149 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
7150 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
7151 pci_write_config_word(tp->pdev,
7152 tp->pcie_cap + PCI_EXP_DEVCTL,
7153 val16);
7155 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
7157 /* Clear error status */
7158 pci_write_config_word(tp->pdev,
7159 tp->pcie_cap + PCI_EXP_DEVSTA,
7160 PCI_EXP_DEVSTA_CED |
7161 PCI_EXP_DEVSTA_NFED |
7162 PCI_EXP_DEVSTA_FED |
7163 PCI_EXP_DEVSTA_URD);
7166 tg3_restore_pci_state(tp);
7168 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7170 val = 0;
7171 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
7172 val = tr32(MEMARB_MODE);
7173 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
7175 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7176 tg3_stop_fw(tp);
7177 tw32(0x5000, 0x400);
7180 tw32(GRC_MODE, tp->grc_mode);
7182 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
7183 val = tr32(0xc4);
7185 tw32(0xc4, val | (1 << 15));
7188 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7189 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7190 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7191 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7192 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7193 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7196 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7197 tp->mac_mode = MAC_MODE_APE_TX_EN |
7198 MAC_MODE_APE_RX_EN |
7199 MAC_MODE_TDE_ENABLE;
7201 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
7202 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7203 val = tp->mac_mode;
7204 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
7205 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7206 val = tp->mac_mode;
7207 } else
7208 val = 0;
7210 tw32_f(MAC_MODE, val);
7211 udelay(40);
7213 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7215 err = tg3_poll_fw(tp);
7216 if (err)
7217 return err;
7219 tg3_mdio_start(tp);
7221 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
7222 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7223 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
7224 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
7225 val = tr32(0x7c00);
7227 tw32(0x7c00, val | (1 << 25));
7230 /* Reprobe ASF enable state. */
7231 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7232 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7233 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7234 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7235 u32 nic_cfg;
7237 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7238 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7239 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
7240 tp->last_event_jiffies = jiffies;
7241 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
7242 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7246 return 0;
7249 /* tp->lock is held. */
7250 static void tg3_stop_fw(struct tg3 *tp)
7252 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7253 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
7254 /* Wait for RX cpu to ACK the previous event. */
7255 tg3_wait_for_event_ack(tp);
7257 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
7259 tg3_generate_fw_event(tp);
7261 /* Wait for RX cpu to ACK this event. */
7262 tg3_wait_for_event_ack(tp);
7266 /* tp->lock is held. */
7267 static int tg3_halt(struct tg3 *tp, int kind, int silent)
7269 int err;
7271 tg3_stop_fw(tp);
7273 tg3_write_sig_pre_reset(tp, kind);
7275 tg3_abort_hw(tp, silent);
7276 err = tg3_chip_reset(tp);
7278 __tg3_set_mac_addr(tp, 0);
7280 tg3_write_sig_legacy(tp, kind);
7281 tg3_write_sig_post_reset(tp, kind);
7283 if (err)
7284 return err;
7286 return 0;
7289 #define RX_CPU_SCRATCH_BASE 0x30000
7290 #define RX_CPU_SCRATCH_SIZE 0x04000
7291 #define TX_CPU_SCRATCH_BASE 0x34000
7292 #define TX_CPU_SCRATCH_SIZE 0x04000
7294 /* tp->lock is held. */
7295 static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7297 int i;
7299 BUG_ON(offset == TX_CPU_BASE &&
7300 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
7302 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7303 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7305 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7306 return 0;
7308 if (offset == RX_CPU_BASE) {
7309 for (i = 0; i < 10000; i++) {
7310 tw32(offset + CPU_STATE, 0xffffffff);
7311 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7312 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7313 break;
7316 tw32(offset + CPU_STATE, 0xffffffff);
7317 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7318 udelay(10);
7319 } else {
7320 for (i = 0; i < 10000; i++) {
7321 tw32(offset + CPU_STATE, 0xffffffff);
7322 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7323 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7324 break;
7328 if (i >= 10000) {
7329 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7330 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
7331 return -ENODEV;
7334 /* Clear firmware's nvram arbitration. */
7335 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7336 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
7337 return 0;
7340 struct fw_info {
7341 unsigned int fw_base;
7342 unsigned int fw_len;
7343 const __be32 *fw_data;
7346 /* tp->lock is held. */
7347 static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7348 int cpu_scratch_size, struct fw_info *info)
7350 int err, lock_err, i;
7351 void (*write_op)(struct tg3 *, u32, u32);
7353 if (cpu_base == TX_CPU_BASE &&
7354 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7355 netdev_err(tp->dev,
7356 "%s: Trying to load TX cpu firmware which is 5705\n",
7357 __func__);
7358 return -EINVAL;
7361 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7362 write_op = tg3_write_mem;
7363 else
7364 write_op = tg3_write_indirect_reg32;
7366 /* It is possible that bootcode is still loading at this point.
7367 * Get the nvram lock first before halting the cpu.
7369 lock_err = tg3_nvram_lock(tp);
7370 err = tg3_halt_cpu(tp, cpu_base);
7371 if (!lock_err)
7372 tg3_nvram_unlock(tp);
7373 if (err)
7374 goto out;
7376 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7377 write_op(tp, cpu_scratch_base + i, 0);
7378 tw32(cpu_base + CPU_STATE, 0xffffffff);
7379 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
7380 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
7381 write_op(tp, (cpu_scratch_base +
7382 (info->fw_base & 0xffff) +
7383 (i * sizeof(u32))),
7384 be32_to_cpu(info->fw_data[i]));
7386 err = 0;
7388 out:
7389 return err;
7392 /* tp->lock is held. */
7393 static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7395 struct fw_info info;
7396 const __be32 *fw_data;
7397 int err, i;
7399 fw_data = (void *)tp->fw->data;
7401 /* Firmware blob starts with version numbers, followed by
7402 start address and length. We are setting complete length.
7403 length = end_address_of_bss - start_address_of_text.
7404 Remainder is the blob to be loaded contiguously
7405 from start address. */
7407 info.fw_base = be32_to_cpu(fw_data[1]);
7408 info.fw_len = tp->fw->size - 12;
7409 info.fw_data = &fw_data[3];
7411 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7412 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7413 &info);
7414 if (err)
7415 return err;
7417 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7418 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7419 &info);
7420 if (err)
7421 return err;
7423 /* Now startup only the RX cpu. */
7424 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7425 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
7427 for (i = 0; i < 5; i++) {
7428 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
7429 break;
7430 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7431 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
7432 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
7433 udelay(1000);
7435 if (i >= 5) {
7436 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7437 "should be %08x\n", __func__,
7438 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
7439 return -ENODEV;
7441 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7442 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7444 return 0;
7447 /* 5705 needs a special version of the TSO firmware. */
7449 /* tp->lock is held. */
7450 static int tg3_load_tso_firmware(struct tg3 *tp)
7452 struct fw_info info;
7453 const __be32 *fw_data;
7454 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7455 int err, i;
7457 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7458 return 0;
7460 fw_data = (void *)tp->fw->data;
7462 /* Firmware blob starts with version numbers, followed by
7463 start address and length. We are setting complete length.
7464 length = end_address_of_bss - start_address_of_text.
7465 Remainder is the blob to be loaded contiguously
7466 from start address. */
7468 info.fw_base = be32_to_cpu(fw_data[1]);
7469 cpu_scratch_size = tp->fw_len;
7470 info.fw_len = tp->fw->size - 12;
7471 info.fw_data = &fw_data[3];
7473 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7474 cpu_base = RX_CPU_BASE;
7475 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
7476 } else {
7477 cpu_base = TX_CPU_BASE;
7478 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7479 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7482 err = tg3_load_firmware_cpu(tp, cpu_base,
7483 cpu_scratch_base, cpu_scratch_size,
7484 &info);
7485 if (err)
7486 return err;
7488 /* Now startup the cpu. */
7489 tw32(cpu_base + CPU_STATE, 0xffffffff);
7490 tw32_f(cpu_base + CPU_PC, info.fw_base);
7492 for (i = 0; i < 5; i++) {
7493 if (tr32(cpu_base + CPU_PC) == info.fw_base)
7494 break;
7495 tw32(cpu_base + CPU_STATE, 0xffffffff);
7496 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
7497 tw32_f(cpu_base + CPU_PC, info.fw_base);
7498 udelay(1000);
7500 if (i >= 5) {
7501 netdev_err(tp->dev,
7502 "%s fails to set CPU PC, is %08x should be %08x\n",
7503 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
7504 return -ENODEV;
7506 tw32(cpu_base + CPU_STATE, 0xffffffff);
7507 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7508 return 0;
7512 static int tg3_set_mac_addr(struct net_device *dev, void *p)
7514 struct tg3 *tp = netdev_priv(dev);
7515 struct sockaddr *addr = p;
7516 int err = 0, skip_mac_1 = 0;
7518 if (!is_valid_ether_addr(addr->sa_data))
7519 return -EINVAL;
7521 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7523 if (!netif_running(dev))
7524 return 0;
7526 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
7527 u32 addr0_high, addr0_low, addr1_high, addr1_low;
7529 addr0_high = tr32(MAC_ADDR_0_HIGH);
7530 addr0_low = tr32(MAC_ADDR_0_LOW);
7531 addr1_high = tr32(MAC_ADDR_1_HIGH);
7532 addr1_low = tr32(MAC_ADDR_1_LOW);
7534 /* Skip MAC addr 1 if ASF is using it. */
7535 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7536 !(addr1_high == 0 && addr1_low == 0))
7537 skip_mac_1 = 1;
7539 spin_lock_bh(&tp->lock);
7540 __tg3_set_mac_addr(tp, skip_mac_1);
7541 spin_unlock_bh(&tp->lock);
7543 return err;
7546 /* tp->lock is held. */
7547 static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7548 dma_addr_t mapping, u32 maxlen_flags,
7549 u32 nic_addr)
7551 tg3_write_mem(tp,
7552 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7553 ((u64) mapping >> 32));
7554 tg3_write_mem(tp,
7555 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7556 ((u64) mapping & 0xffffffff));
7557 tg3_write_mem(tp,
7558 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7559 maxlen_flags);
7561 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7562 tg3_write_mem(tp,
7563 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7564 nic_addr);
7567 static void __tg3_set_rx_mode(struct net_device *);
7568 static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
7570 int i;
7572 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
7573 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7574 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7575 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
7576 } else {
7577 tw32(HOSTCC_TXCOL_TICKS, 0);
7578 tw32(HOSTCC_TXMAX_FRAMES, 0);
7579 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
7582 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
7583 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7584 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7585 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7586 } else {
7587 tw32(HOSTCC_RXCOL_TICKS, 0);
7588 tw32(HOSTCC_RXMAX_FRAMES, 0);
7589 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
7592 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7593 u32 val = ec->stats_block_coalesce_usecs;
7595 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7596 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7598 if (!netif_carrier_ok(tp->dev))
7599 val = 0;
7601 tw32(HOSTCC_STAT_COAL_TICKS, val);
7604 for (i = 0; i < tp->irq_cnt - 1; i++) {
7605 u32 reg;
7607 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7608 tw32(reg, ec->rx_coalesce_usecs);
7609 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7610 tw32(reg, ec->rx_max_coalesced_frames);
7611 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7612 tw32(reg, ec->rx_max_coalesced_frames_irq);
7614 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7615 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7616 tw32(reg, ec->tx_coalesce_usecs);
7617 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7618 tw32(reg, ec->tx_max_coalesced_frames);
7619 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7620 tw32(reg, ec->tx_max_coalesced_frames_irq);
7624 for (; i < tp->irq_max - 1; i++) {
7625 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
7626 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
7627 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7629 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7630 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7631 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7632 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7637 /* tp->lock is held. */
7638 static void tg3_rings_reset(struct tg3 *tp)
7640 int i;
7641 u32 stblk, txrcb, rxrcb, limit;
7642 struct tg3_napi *tnapi = &tp->napi[0];
7644 /* Disable all transmit rings but the first. */
7645 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7646 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
7647 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7648 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
7649 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
7650 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7651 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
7652 else
7653 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7655 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7656 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7657 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7658 BDINFO_FLAGS_DISABLED);
7661 /* Disable all receive return rings but the first. */
7662 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7663 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
7664 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7665 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7666 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
7667 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7668 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7669 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7670 else
7671 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7673 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7674 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7675 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7676 BDINFO_FLAGS_DISABLED);
7678 /* Disable interrupts */
7679 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7681 /* Zero mailbox registers. */
7682 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7683 for (i = 1; i < tp->irq_max; i++) {
7684 tp->napi[i].tx_prod = 0;
7685 tp->napi[i].tx_cons = 0;
7686 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7687 tw32_mailbox(tp->napi[i].prodmbox, 0);
7688 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7689 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7691 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7692 tw32_mailbox(tp->napi[0].prodmbox, 0);
7693 } else {
7694 tp->napi[0].tx_prod = 0;
7695 tp->napi[0].tx_cons = 0;
7696 tw32_mailbox(tp->napi[0].prodmbox, 0);
7697 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7700 /* Make sure the NIC-based send BD rings are disabled. */
7701 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7702 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7703 for (i = 0; i < 16; i++)
7704 tw32_tx_mbox(mbox + i * 8, 0);
7707 txrcb = NIC_SRAM_SEND_RCB;
7708 rxrcb = NIC_SRAM_RCV_RET_RCB;
7710 /* Clear status block in ram. */
7711 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7713 /* Set status block DMA address */
7714 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7715 ((u64) tnapi->status_mapping >> 32));
7716 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7717 ((u64) tnapi->status_mapping & 0xffffffff));
7719 if (tnapi->tx_ring) {
7720 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7721 (TG3_TX_RING_SIZE <<
7722 BDINFO_FLAGS_MAXLEN_SHIFT),
7723 NIC_SRAM_TX_BUFFER_DESC);
7724 txrcb += TG3_BDINFO_SIZE;
7727 if (tnapi->rx_rcb) {
7728 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7729 (tp->rx_ret_ring_mask + 1) <<
7730 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
7731 rxrcb += TG3_BDINFO_SIZE;
7734 stblk = HOSTCC_STATBLCK_RING1;
7736 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7737 u64 mapping = (u64)tnapi->status_mapping;
7738 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7739 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7741 /* Clear status block in ram. */
7742 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7744 if (tnapi->tx_ring) {
7745 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7746 (TG3_TX_RING_SIZE <<
7747 BDINFO_FLAGS_MAXLEN_SHIFT),
7748 NIC_SRAM_TX_BUFFER_DESC);
7749 txrcb += TG3_BDINFO_SIZE;
7752 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7753 ((tp->rx_ret_ring_mask + 1) <<
7754 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7756 stblk += 8;
7757 rxrcb += TG3_BDINFO_SIZE;
7761 /* tp->lock is held. */
7762 static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
7764 u32 val, rdmac_mode;
7765 int i, err, limit;
7766 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
7768 tg3_disable_ints(tp);
7770 tg3_stop_fw(tp);
7772 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7774 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
7775 tg3_abort_hw(tp, 1);
7777 /* Enable MAC control of LPI */
7778 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7779 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7780 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7781 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7783 tw32_f(TG3_CPMU_EEE_CTRL,
7784 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7786 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7787 TG3_CPMU_EEEMD_LPI_IN_TX |
7788 TG3_CPMU_EEEMD_LPI_IN_RX |
7789 TG3_CPMU_EEEMD_EEE_ENABLE;
7791 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7792 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7794 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7795 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7797 tw32_f(TG3_CPMU_EEE_MODE, val);
7799 tw32_f(TG3_CPMU_EEE_DBTMR1,
7800 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7801 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7803 tw32_f(TG3_CPMU_EEE_DBTMR2,
7804 TG3_CPMU_DBTMR1_APE_TX_2047US |
7805 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
7808 if (reset_phy)
7809 tg3_phy_reset(tp);
7811 err = tg3_chip_reset(tp);
7812 if (err)
7813 return err;
7815 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7817 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
7818 val = tr32(TG3_CPMU_CTRL);
7819 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7820 tw32(TG3_CPMU_CTRL, val);
7822 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7823 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7824 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7825 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7827 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7828 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7829 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7830 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7832 val = tr32(TG3_CPMU_HST_ACC);
7833 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7834 val |= CPMU_HST_ACC_MACCLK_6_25;
7835 tw32(TG3_CPMU_HST_ACC, val);
7838 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7839 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7840 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7841 PCIE_PWR_MGMT_L1_THRESH_4MS;
7842 tw32(PCIE_PWR_MGMT_THRESH, val);
7844 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7845 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7847 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
7849 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7850 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7853 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7854 u32 grc_mode = tr32(GRC_MODE);
7856 /* Access the lower 1K of PL PCIE block registers. */
7857 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7858 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7860 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7861 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7862 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7864 tw32(GRC_MODE, grc_mode);
7867 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
7868 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7869 u32 grc_mode = tr32(GRC_MODE);
7871 /* Access the lower 1K of PL PCIE block registers. */
7872 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7873 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7875 val = tr32(TG3_PCIE_TLDLPL_PORT +
7876 TG3_PCIE_PL_LO_PHYCTL5);
7877 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7878 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
7880 tw32(GRC_MODE, grc_mode);
7883 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7884 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7885 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7886 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7889 /* This works around an issue with Athlon chipsets on
7890 * B3 tigon3 silicon. This bit has no effect on any
7891 * other revision. But do not set this on PCI Express
7892 * chips and don't even touch the clocks if the CPMU is present.
7894 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7895 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7896 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7897 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7900 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7901 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7902 val = tr32(TG3PCI_PCISTATE);
7903 val |= PCISTATE_RETRY_SAME_DMA;
7904 tw32(TG3PCI_PCISTATE, val);
7907 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7908 /* Allow reads and writes to the
7909 * APE register and memory space.
7911 val = tr32(TG3PCI_PCISTATE);
7912 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
7913 PCISTATE_ALLOW_APE_SHMEM_WR |
7914 PCISTATE_ALLOW_APE_PSPACE_WR;
7915 tw32(TG3PCI_PCISTATE, val);
7918 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7919 /* Enable some hw fixes. */
7920 val = tr32(TG3PCI_MSI_DATA);
7921 val |= (1 << 26) | (1 << 28) | (1 << 29);
7922 tw32(TG3PCI_MSI_DATA, val);
7925 /* Descriptor ring init may make accesses to the
7926 * NIC SRAM area to setup the TX descriptors, so we
7927 * can only do this after the hardware has been
7928 * successfully reset.
7930 err = tg3_init_rings(tp);
7931 if (err)
7932 return err;
7934 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
7935 val = tr32(TG3PCI_DMA_RW_CTRL) &
7936 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
7937 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7938 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
7939 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7940 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7941 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
7942 /* This value is determined during the probe time DMA
7943 * engine test, tg3_test_dma.
7945 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7948 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7949 GRC_MODE_4X_NIC_SEND_RINGS |
7950 GRC_MODE_NO_TX_PHDR_CSUM |
7951 GRC_MODE_NO_RX_PHDR_CSUM);
7952 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
7954 /* Pseudo-header checksum is done by hardware logic and not
7955 * the offload processers, so make the chip do the pseudo-
7956 * header checksums on receive. For transmit it is more
7957 * convenient to do the pseudo-header checksum in software
7958 * as Linux does that on transmit for us in all cases.
7960 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
7962 tw32(GRC_MODE,
7963 tp->grc_mode |
7964 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7966 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7967 val = tr32(GRC_MISC_CFG);
7968 val &= ~0xff;
7969 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7970 tw32(GRC_MISC_CFG, val);
7972 /* Initialize MBUF/DESC pool. */
7973 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
7974 /* Do nothing. */
7975 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7976 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7978 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7979 else
7980 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7981 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7982 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
7983 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7984 int fw_len;
7986 fw_len = tp->fw_len;
7987 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7988 tw32(BUFMGR_MB_POOL_ADDR,
7989 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7990 tw32(BUFMGR_MB_POOL_SIZE,
7991 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7994 if (tp->dev->mtu <= ETH_DATA_LEN) {
7995 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7996 tp->bufmgr_config.mbuf_read_dma_low_water);
7997 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7998 tp->bufmgr_config.mbuf_mac_rx_low_water);
7999 tw32(BUFMGR_MB_HIGH_WATER,
8000 tp->bufmgr_config.mbuf_high_water);
8001 } else {
8002 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8003 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8004 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8005 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8006 tw32(BUFMGR_MB_HIGH_WATER,
8007 tp->bufmgr_config.mbuf_high_water_jumbo);
8009 tw32(BUFMGR_DMA_LOW_WATER,
8010 tp->bufmgr_config.dma_low_water);
8011 tw32(BUFMGR_DMA_HIGH_WATER,
8012 tp->bufmgr_config.dma_high_water);
8014 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8015 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8016 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
8017 tw32(BUFMGR_MODE, val);
8018 for (i = 0; i < 2000; i++) {
8019 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8020 break;
8021 udelay(10);
8023 if (i >= 2000) {
8024 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
8025 return -ENODEV;
8028 /* Setup replenish threshold. */
8029 val = tp->rx_pending / 8;
8030 if (val == 0)
8031 val = 1;
8032 else if (val > tp->rx_std_max_post)
8033 val = tp->rx_std_max_post;
8034 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8035 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8036 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
8038 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
8039 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
8042 tw32(RCVBDI_STD_THRESH, val);
8044 /* Initialize TG3_BDINFO's at:
8045 * RCVDBDI_STD_BD: standard eth size rx ring
8046 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8047 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8049 * like so:
8050 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8051 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8052 * ring attribute flags
8053 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8055 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8056 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8058 * The size of each ring is fixed in the firmware, but the location is
8059 * configurable.
8061 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
8062 ((u64) tpr->rx_std_mapping >> 32));
8063 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
8064 ((u64) tpr->rx_std_mapping & 0xffffffff));
8065 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8066 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
8067 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8068 NIC_SRAM_RX_BUFFER_DESC);
8070 /* Disable the mini ring */
8071 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8072 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8073 BDINFO_FLAGS_DISABLED);
8075 /* Program the jumbo buffer descriptor ring control
8076 * blocks on those devices that have them.
8078 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8079 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
8080 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))) {
8081 /* Setup replenish threshold. */
8082 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
8084 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
8085 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
8086 ((u64) tpr->rx_jmb_mapping >> 32));
8087 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
8088 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
8089 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8090 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
8091 BDINFO_FLAGS_USE_EXT_RECV);
8092 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
8093 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8094 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8095 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
8096 } else {
8097 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8098 BDINFO_FLAGS_DISABLED);
8101 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
8102 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8103 val = RX_STD_MAX_SIZE_5705;
8104 else
8105 val = RX_STD_MAX_SIZE_5717;
8106 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8107 val |= (TG3_RX_STD_DMA_SZ << 2);
8108 } else
8109 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
8110 } else
8111 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
8113 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
8115 tpr->rx_std_prod_idx = tp->rx_pending;
8116 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
8118 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
8119 tp->rx_jumbo_pending : 0;
8120 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
8122 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
8123 tw32(STD_REPLENISH_LWM, 32);
8124 tw32(JMB_REPLENISH_LWM, 16);
8127 tg3_rings_reset(tp);
8129 /* Initialize MAC address and backoff seed. */
8130 __tg3_set_mac_addr(tp, 0);
8132 /* MTU + ethernet header + FCS + optional VLAN tag */
8133 tw32(MAC_RX_MTU_SIZE,
8134 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
8136 /* The slot time is changed by tg3_setup_phy if we
8137 * run at gigabit with half duplex.
8139 tw32(MAC_TX_LENGTHS,
8140 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8141 (6 << TX_LENGTHS_IPG_SHIFT) |
8142 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
8144 /* Receive rules. */
8145 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8146 tw32(RCVLPC_CONFIG, 0x0181);
8148 /* Calculate RDMAC_MODE setting early, we need it to determine
8149 * the RCVLPC_STATE_ENABLE mask.
8151 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8152 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8153 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8154 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8155 RDMAC_MODE_LNGREAD_ENAB);
8157 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
8158 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8160 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8161 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8162 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8163 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8164 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8165 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8167 /* If statement applies to 5705 and 5750 PCI devices only */
8168 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8169 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8170 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
8171 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
8172 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
8173 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8174 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8175 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8176 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8180 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8181 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8183 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8184 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8186 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8187 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8188 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8189 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
8191 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8192 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8193 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8194 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
8195 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
8196 val = tr32(TG3_RDMA_RSRVCTRL_REG);
8197 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
8198 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8199 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8200 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8201 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8202 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8203 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
8205 tw32(TG3_RDMA_RSRVCTRL_REG,
8206 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8209 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
8210 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8211 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8212 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8213 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8216 /* Receive/send statistics. */
8217 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8218 val = tr32(RCVLPC_STATS_ENABLE);
8219 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8220 tw32(RCVLPC_STATS_ENABLE, val);
8221 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8222 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
8223 val = tr32(RCVLPC_STATS_ENABLE);
8224 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8225 tw32(RCVLPC_STATS_ENABLE, val);
8226 } else {
8227 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8229 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8230 tw32(SNDDATAI_STATSENAB, 0xffffff);
8231 tw32(SNDDATAI_STATSCTRL,
8232 (SNDDATAI_SCTRL_ENABLE |
8233 SNDDATAI_SCTRL_FASTUPD));
8235 /* Setup host coalescing engine. */
8236 tw32(HOSTCC_MODE, 0);
8237 for (i = 0; i < 2000; i++) {
8238 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8239 break;
8240 udelay(10);
8243 __tg3_set_coalesce(tp, &tp->coal);
8245 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8246 /* Status/statistics block address. See tg3_timer,
8247 * the tg3_periodic_fetch_stats call there, and
8248 * tg3_get_stats to see how this works for 5705/5750 chips.
8250 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8251 ((u64) tp->stats_mapping >> 32));
8252 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8253 ((u64) tp->stats_mapping & 0xffffffff));
8254 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
8256 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
8258 /* Clear statistics and status block memory areas */
8259 for (i = NIC_SRAM_STATS_BLK;
8260 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8261 i += sizeof(u32)) {
8262 tg3_write_mem(tp, i, 0);
8263 udelay(40);
8267 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8269 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8270 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8271 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8272 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8274 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8275 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
8276 /* reset to prevent losing 1st rx packet intermittently */
8277 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8278 udelay(10);
8281 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8282 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
8283 else
8284 tp->mac_mode = 0;
8285 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
8286 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
8287 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
8288 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8289 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8290 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8291 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8292 udelay(40);
8294 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
8295 * If TG3_FLG2_IS_NIC is zero, we should read the
8296 * register to preserve the GPIO settings for LOMs. The GPIOs,
8297 * whether used as inputs or outputs, are set by boot code after
8298 * reset.
8300 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
8301 u32 gpio_mask;
8303 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8304 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8305 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
8307 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8308 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8309 GRC_LCLCTRL_GPIO_OUTPUT3;
8311 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8312 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8314 tp->grc_local_ctrl &= ~gpio_mask;
8315 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8317 /* GPIO1 must be driven high for eeprom write protect */
8318 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8319 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8320 GRC_LCLCTRL_GPIO_OUTPUT1);
8322 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8323 udelay(100);
8325 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
8326 tp->irq_cnt > 1) {
8327 val = tr32(MSGINT_MODE);
8328 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8329 tw32(MSGINT_MODE, val);
8332 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8333 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8334 udelay(40);
8337 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8338 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8339 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8340 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8341 WDMAC_MODE_LNGREAD_ENAB);
8343 /* If statement applies to 5705 and 5750 PCI devices only */
8344 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8345 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8346 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
8347 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
8348 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8349 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8350 /* nothing */
8351 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8352 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8353 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8354 val |= WDMAC_MODE_RX_ACCEL;
8358 /* Enable host coalescing bug fix */
8359 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
8360 val |= WDMAC_MODE_STATUS_TAG_FIX;
8362 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8363 val |= WDMAC_MODE_BURST_ALL_DATA;
8365 tw32_f(WDMAC_MODE, val);
8366 udelay(40);
8368 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8369 u16 pcix_cmd;
8371 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8372 &pcix_cmd);
8373 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
8374 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8375 pcix_cmd |= PCI_X_CMD_READ_2K;
8376 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
8377 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8378 pcix_cmd |= PCI_X_CMD_READ_2K;
8380 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8381 pcix_cmd);
8384 tw32_f(RDMAC_MODE, rdmac_mode);
8385 udelay(40);
8387 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8388 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8389 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
8391 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8392 tw32(SNDDATAC_MODE,
8393 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8394 else
8395 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8397 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8398 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8399 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
8400 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8401 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8402 val |= RCVDBDI_MODE_LRG_RING_SZ;
8403 tw32(RCVDBDI_MODE, val);
8404 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
8405 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8406 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
8407 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
8408 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
8409 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8410 tw32(SNDBDI_MODE, val);
8411 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8413 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8414 err = tg3_load_5701_a0_firmware_fix(tp);
8415 if (err)
8416 return err;
8419 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8420 err = tg3_load_tso_firmware(tp);
8421 if (err)
8422 return err;
8425 tp->tx_mode = TX_MODE_ENABLE;
8426 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8427 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8428 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
8429 tw32_f(MAC_TX_MODE, tp->tx_mode);
8430 udelay(100);
8432 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8433 u32 reg = MAC_RSS_INDIR_TBL_0;
8434 u8 *ent = (u8 *)&val;
8436 /* Setup the indirection table */
8437 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8438 int idx = i % sizeof(val);
8440 ent[idx] = i % (tp->irq_cnt - 1);
8441 if (idx == sizeof(val) - 1) {
8442 tw32(reg, val);
8443 reg += 4;
8447 /* Setup the "secret" hash key. */
8448 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8449 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8450 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8451 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8452 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8453 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8454 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8455 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8456 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8457 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8460 tp->rx_mode = RX_MODE_ENABLE;
8461 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
8462 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8464 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8465 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8466 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8467 RX_MODE_RSS_IPV6_HASH_EN |
8468 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8469 RX_MODE_RSS_IPV4_HASH_EN |
8470 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8472 tw32_f(MAC_RX_MODE, tp->rx_mode);
8473 udelay(10);
8475 tw32(MAC_LED_CTRL, tp->led_ctrl);
8477 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
8478 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
8479 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8480 udelay(10);
8482 tw32_f(MAC_RX_MODE, tp->rx_mode);
8483 udelay(10);
8485 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
8486 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8487 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
8488 /* Set drive transmission level to 1.2V */
8489 /* only if the signal pre-emphasis bit is not set */
8490 val = tr32(MAC_SERDES_CFG);
8491 val &= 0xfffff000;
8492 val |= 0x880;
8493 tw32(MAC_SERDES_CFG, val);
8495 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8496 tw32(MAC_SERDES_CFG, 0x616000);
8499 /* Prevent chip from dropping frames when flow control
8500 * is enabled.
8502 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8503 val = 1;
8504 else
8505 val = 2;
8506 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
8508 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8509 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
8510 /* Use hardware link auto-negotiation */
8511 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8514 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8515 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8516 u32 tmp;
8518 tmp = tr32(SERDES_RX_CTRL);
8519 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8520 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8521 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8522 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8525 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8526 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8527 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
8528 tp->link_config.speed = tp->link_config.orig_speed;
8529 tp->link_config.duplex = tp->link_config.orig_duplex;
8530 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8533 err = tg3_setup_phy(tp, 0);
8534 if (err)
8535 return err;
8537 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8538 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8539 u32 tmp;
8541 /* Clear CRC stats. */
8542 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8543 tg3_writephy(tp, MII_TG3_TEST1,
8544 tmp | MII_TG3_TEST1_CRC_EN);
8545 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
8550 __tg3_set_rx_mode(tp->dev);
8552 /* Initialize receive rules. */
8553 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8554 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8555 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8556 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8558 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
8559 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
8560 limit = 8;
8561 else
8562 limit = 16;
8563 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8564 limit -= 4;
8565 switch (limit) {
8566 case 16:
8567 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8568 case 15:
8569 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8570 case 14:
8571 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8572 case 13:
8573 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8574 case 12:
8575 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8576 case 11:
8577 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8578 case 10:
8579 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8580 case 9:
8581 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8582 case 8:
8583 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8584 case 7:
8585 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8586 case 6:
8587 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8588 case 5:
8589 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8590 case 4:
8591 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8592 case 3:
8593 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8594 case 2:
8595 case 1:
8597 default:
8598 break;
8601 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8602 /* Write our heartbeat update interval to APE. */
8603 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8604 APE_HOST_HEARTBEAT_INT_DISABLE);
8606 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8608 return 0;
8611 /* Called at device open time to get the chip ready for
8612 * packet processing. Invoked with tp->lock held.
8614 static int tg3_init_hw(struct tg3 *tp, int reset_phy)
8616 tg3_switch_clocks(tp);
8618 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8620 return tg3_reset_hw(tp, reset_phy);
8623 #define TG3_STAT_ADD32(PSTAT, REG) \
8624 do { u32 __val = tr32(REG); \
8625 (PSTAT)->low += __val; \
8626 if ((PSTAT)->low < __val) \
8627 (PSTAT)->high += 1; \
8628 } while (0)
8630 static void tg3_periodic_fetch_stats(struct tg3 *tp)
8632 struct tg3_hw_stats *sp = tp->hw_stats;
8634 if (!netif_carrier_ok(tp->dev))
8635 return;
8637 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8638 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8639 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8640 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8641 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8642 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8643 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8644 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8645 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8646 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8647 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8648 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8649 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8651 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8652 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8653 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8654 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8655 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8656 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8657 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8658 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8659 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8660 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8661 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8662 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8663 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8664 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
8666 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8667 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8668 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
8671 static void tg3_timer(unsigned long __opaque)
8673 struct tg3 *tp = (struct tg3 *) __opaque;
8675 if (tp->irq_sync)
8676 goto restart_timer;
8678 spin_lock(&tp->lock);
8680 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8681 /* All of this garbage is because when using non-tagged
8682 * IRQ status the mailbox/status_block protocol the chip
8683 * uses with the cpu is race prone.
8685 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
8686 tw32(GRC_LOCAL_CTRL,
8687 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8688 } else {
8689 tw32(HOSTCC_MODE, tp->coalesce_mode |
8690 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
8693 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8694 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
8695 spin_unlock(&tp->lock);
8696 schedule_work(&tp->reset_task);
8697 return;
8701 /* This part only runs once per second. */
8702 if (!--tp->timer_counter) {
8703 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8704 tg3_periodic_fetch_stats(tp);
8706 if (tp->setlpicnt && !--tp->setlpicnt) {
8707 u32 val = tr32(TG3_CPMU_EEE_MODE);
8708 tw32(TG3_CPMU_EEE_MODE,
8709 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8712 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8713 u32 mac_stat;
8714 int phy_event;
8716 mac_stat = tr32(MAC_STATUS);
8718 phy_event = 0;
8719 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
8720 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8721 phy_event = 1;
8722 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8723 phy_event = 1;
8725 if (phy_event)
8726 tg3_setup_phy(tp, 0);
8727 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8728 u32 mac_stat = tr32(MAC_STATUS);
8729 int need_setup = 0;
8731 if (netif_carrier_ok(tp->dev) &&
8732 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8733 need_setup = 1;
8735 if (!netif_carrier_ok(tp->dev) &&
8736 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8737 MAC_STATUS_SIGNAL_DET))) {
8738 need_setup = 1;
8740 if (need_setup) {
8741 if (!tp->serdes_counter) {
8742 tw32_f(MAC_MODE,
8743 (tp->mac_mode &
8744 ~MAC_MODE_PORT_MODE_MASK));
8745 udelay(40);
8746 tw32_f(MAC_MODE, tp->mac_mode);
8747 udelay(40);
8749 tg3_setup_phy(tp, 0);
8751 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8752 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
8753 tg3_serdes_parallel_detect(tp);
8756 tp->timer_counter = tp->timer_multiplier;
8759 /* Heartbeat is only sent once every 2 seconds.
8761 * The heartbeat is to tell the ASF firmware that the host
8762 * driver is still alive. In the event that the OS crashes,
8763 * ASF needs to reset the hardware to free up the FIFO space
8764 * that may be filled with rx packets destined for the host.
8765 * If the FIFO is full, ASF will no longer function properly.
8767 * Unintended resets have been reported on real time kernels
8768 * where the timer doesn't run on time. Netpoll will also have
8769 * same problem.
8771 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8772 * to check the ring condition when the heartbeat is expiring
8773 * before doing the reset. This will prevent most unintended
8774 * resets.
8776 if (!--tp->asf_counter) {
8777 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8778 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
8779 tg3_wait_for_event_ack(tp);
8781 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
8782 FWCMD_NICDRV_ALIVE3);
8783 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
8784 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8785 TG3_FW_UPDATE_TIMEOUT_SEC);
8787 tg3_generate_fw_event(tp);
8789 tp->asf_counter = tp->asf_multiplier;
8792 spin_unlock(&tp->lock);
8794 restart_timer:
8795 tp->timer.expires = jiffies + tp->timer_offset;
8796 add_timer(&tp->timer);
8799 static int tg3_request_irq(struct tg3 *tp, int irq_num)
8801 irq_handler_t fn;
8802 unsigned long flags;
8803 char *name;
8804 struct tg3_napi *tnapi = &tp->napi[irq_num];
8806 if (tp->irq_cnt == 1)
8807 name = tp->dev->name;
8808 else {
8809 name = &tnapi->irq_lbl[0];
8810 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8811 name[IFNAMSIZ-1] = 0;
8814 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8815 fn = tg3_msi;
8816 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8817 fn = tg3_msi_1shot;
8818 flags = IRQF_SAMPLE_RANDOM;
8819 } else {
8820 fn = tg3_interrupt;
8821 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8822 fn = tg3_interrupt_tagged;
8823 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
8826 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
8829 static int tg3_test_interrupt(struct tg3 *tp)
8831 struct tg3_napi *tnapi = &tp->napi[0];
8832 struct net_device *dev = tp->dev;
8833 int err, i, intr_ok = 0;
8834 u32 val;
8836 if (!netif_running(dev))
8837 return -ENODEV;
8839 tg3_disable_ints(tp);
8841 free_irq(tnapi->irq_vec, tnapi);
8844 * Turn off MSI one shot mode. Otherwise this test has no
8845 * observable way to know whether the interrupt was delivered.
8847 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
8848 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8849 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8850 tw32(MSGINT_MODE, val);
8853 err = request_irq(tnapi->irq_vec, tg3_test_isr,
8854 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
8855 if (err)
8856 return err;
8858 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
8859 tg3_enable_ints(tp);
8861 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
8862 tnapi->coal_now);
8864 for (i = 0; i < 5; i++) {
8865 u32 int_mbox, misc_host_ctrl;
8867 int_mbox = tr32_mailbox(tnapi->int_mbox);
8868 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8870 if ((int_mbox != 0) ||
8871 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8872 intr_ok = 1;
8873 break;
8876 msleep(10);
8879 tg3_disable_ints(tp);
8881 free_irq(tnapi->irq_vec, tnapi);
8883 err = tg3_request_irq(tp, 0);
8885 if (err)
8886 return err;
8888 if (intr_ok) {
8889 /* Reenable MSI one shot mode. */
8890 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
8891 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8892 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8893 tw32(MSGINT_MODE, val);
8895 return 0;
8898 return -EIO;
8901 /* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8902 * successfully restored
8904 static int tg3_test_msi(struct tg3 *tp)
8906 int err;
8907 u16 pci_cmd;
8909 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8910 return 0;
8912 /* Turn off SERR reporting in case MSI terminates with Master
8913 * Abort.
8915 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8916 pci_write_config_word(tp->pdev, PCI_COMMAND,
8917 pci_cmd & ~PCI_COMMAND_SERR);
8919 err = tg3_test_interrupt(tp);
8921 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8923 if (!err)
8924 return 0;
8926 /* other failures */
8927 if (err != -EIO)
8928 return err;
8930 /* MSI test failed, go back to INTx mode */
8931 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8932 "to INTx mode. Please report this failure to the PCI "
8933 "maintainer and include system chipset information\n");
8935 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
8937 pci_disable_msi(tp->pdev);
8939 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
8940 tp->napi[0].irq_vec = tp->pdev->irq;
8942 err = tg3_request_irq(tp, 0);
8943 if (err)
8944 return err;
8946 /* Need to reset the chip because the MSI cycle may have terminated
8947 * with Master Abort.
8949 tg3_full_lock(tp, 1);
8951 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
8952 err = tg3_init_hw(tp, 1);
8954 tg3_full_unlock(tp);
8956 if (err)
8957 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
8959 return err;
8962 static int tg3_request_firmware(struct tg3 *tp)
8964 const __be32 *fw_data;
8966 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
8967 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8968 tp->fw_needed);
8969 return -ENOENT;
8972 fw_data = (void *)tp->fw->data;
8974 /* Firmware blob starts with version numbers, followed by
8975 * start address and _full_ length including BSS sections
8976 * (which must be longer than the actual data, of course
8979 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8980 if (tp->fw_len < (tp->fw->size - 12)) {
8981 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
8982 tp->fw_len, tp->fw_needed);
8983 release_firmware(tp->fw);
8984 tp->fw = NULL;
8985 return -EINVAL;
8988 /* We no longer need firmware; we have it. */
8989 tp->fw_needed = NULL;
8990 return 0;
8993 static bool tg3_enable_msix(struct tg3 *tp)
8995 int i, rc, cpus = num_online_cpus();
8996 struct msix_entry msix_ent[tp->irq_max];
8998 if (cpus == 1)
8999 /* Just fallback to the simpler MSI mode. */
9000 return false;
9003 * We want as many rx rings enabled as there are cpus.
9004 * The first MSIX vector only deals with link interrupts, etc,
9005 * so we add one to the number of vectors we are requesting.
9007 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9009 for (i = 0; i < tp->irq_max; i++) {
9010 msix_ent[i].entry = i;
9011 msix_ent[i].vector = 0;
9014 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
9015 if (rc < 0) {
9016 return false;
9017 } else if (rc != 0) {
9018 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9019 return false;
9020 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9021 tp->irq_cnt, rc);
9022 tp->irq_cnt = rc;
9025 for (i = 0; i < tp->irq_max; i++)
9026 tp->napi[i].irq_vec = msix_ent[i].vector;
9028 netif_set_real_num_tx_queues(tp->dev, 1);
9029 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9030 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9031 pci_disable_msix(tp->pdev);
9032 return false;
9035 if (tp->irq_cnt > 1) {
9036 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
9037 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
9038 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
9039 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9043 return true;
9046 static void tg3_ints_init(struct tg3 *tp)
9048 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
9049 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
9050 /* All MSI supporting chips should support tagged
9051 * status. Assert that this is the case.
9053 netdev_warn(tp->dev,
9054 "MSI without TAGGED_STATUS? Not using MSI\n");
9055 goto defcfg;
9058 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
9059 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
9060 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
9061 pci_enable_msi(tp->pdev) == 0)
9062 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
9064 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9065 u32 msi_mode = tr32(MSGINT_MODE);
9066 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
9067 tp->irq_cnt > 1)
9068 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
9069 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9071 defcfg:
9072 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
9073 tp->irq_cnt = 1;
9074 tp->napi[0].irq_vec = tp->pdev->irq;
9075 netif_set_real_num_tx_queues(tp->dev, 1);
9076 netif_set_real_num_rx_queues(tp->dev, 1);
9080 static void tg3_ints_fini(struct tg3 *tp)
9082 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9083 pci_disable_msix(tp->pdev);
9084 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
9085 pci_disable_msi(tp->pdev);
9086 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
9087 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
9090 static int tg3_open(struct net_device *dev)
9092 struct tg3 *tp = netdev_priv(dev);
9093 int i, err;
9095 if (tp->fw_needed) {
9096 err = tg3_request_firmware(tp);
9097 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9098 if (err)
9099 return err;
9100 } else if (err) {
9101 netdev_warn(tp->dev, "TSO capability disabled\n");
9102 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
9103 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9104 netdev_notice(tp->dev, "TSO capability restored\n");
9105 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
9109 netif_carrier_off(tp->dev);
9111 err = tg3_power_up(tp);
9112 if (err)
9113 return err;
9115 tg3_full_lock(tp, 0);
9117 tg3_disable_ints(tp);
9118 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9120 tg3_full_unlock(tp);
9123 * Setup interrupts first so we know how
9124 * many NAPI resources to allocate
9126 tg3_ints_init(tp);
9128 /* The placement of this call is tied
9129 * to the setup and use of Host TX descriptors.
9131 err = tg3_alloc_consistent(tp);
9132 if (err)
9133 goto err_out1;
9135 tg3_napi_init(tp);
9137 tg3_napi_enable(tp);
9139 for (i = 0; i < tp->irq_cnt; i++) {
9140 struct tg3_napi *tnapi = &tp->napi[i];
9141 err = tg3_request_irq(tp, i);
9142 if (err) {
9143 for (i--; i >= 0; i--)
9144 free_irq(tnapi->irq_vec, tnapi);
9145 break;
9149 if (err)
9150 goto err_out2;
9152 tg3_full_lock(tp, 0);
9154 err = tg3_init_hw(tp, 1);
9155 if (err) {
9156 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9157 tg3_free_rings(tp);
9158 } else {
9159 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9160 tp->timer_offset = HZ;
9161 else
9162 tp->timer_offset = HZ / 10;
9164 BUG_ON(tp->timer_offset > HZ);
9165 tp->timer_counter = tp->timer_multiplier =
9166 (HZ / tp->timer_offset);
9167 tp->asf_counter = tp->asf_multiplier =
9168 ((HZ / tp->timer_offset) * 2);
9170 init_timer(&tp->timer);
9171 tp->timer.expires = jiffies + tp->timer_offset;
9172 tp->timer.data = (unsigned long) tp;
9173 tp->timer.function = tg3_timer;
9176 tg3_full_unlock(tp);
9178 if (err)
9179 goto err_out3;
9181 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
9182 err = tg3_test_msi(tp);
9184 if (err) {
9185 tg3_full_lock(tp, 0);
9186 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9187 tg3_free_rings(tp);
9188 tg3_full_unlock(tp);
9190 goto err_out2;
9193 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
9194 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9195 u32 val = tr32(PCIE_TRANSACTION_CFG);
9197 tw32(PCIE_TRANSACTION_CFG,
9198 val | PCIE_TRANS_CFG_1SHOT_MSI);
9202 tg3_phy_start(tp);
9204 tg3_full_lock(tp, 0);
9206 add_timer(&tp->timer);
9207 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
9208 tg3_enable_ints(tp);
9210 tg3_full_unlock(tp);
9212 netif_tx_start_all_queues(dev);
9214 return 0;
9216 err_out3:
9217 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9218 struct tg3_napi *tnapi = &tp->napi[i];
9219 free_irq(tnapi->irq_vec, tnapi);
9222 err_out2:
9223 tg3_napi_disable(tp);
9224 tg3_napi_fini(tp);
9225 tg3_free_consistent(tp);
9227 err_out1:
9228 tg3_ints_fini(tp);
9229 return err;
9232 static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9233 struct rtnl_link_stats64 *);
9234 static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9236 static int tg3_close(struct net_device *dev)
9238 int i;
9239 struct tg3 *tp = netdev_priv(dev);
9241 tg3_napi_disable(tp);
9242 cancel_work_sync(&tp->reset_task);
9244 netif_tx_stop_all_queues(dev);
9246 del_timer_sync(&tp->timer);
9248 tg3_phy_stop(tp);
9250 tg3_full_lock(tp, 1);
9252 tg3_disable_ints(tp);
9254 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9255 tg3_free_rings(tp);
9256 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9258 tg3_full_unlock(tp);
9260 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9261 struct tg3_napi *tnapi = &tp->napi[i];
9262 free_irq(tnapi->irq_vec, tnapi);
9265 tg3_ints_fini(tp);
9267 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9269 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9270 sizeof(tp->estats_prev));
9272 tg3_napi_fini(tp);
9274 tg3_free_consistent(tp);
9276 tg3_power_down(tp);
9278 netif_carrier_off(tp->dev);
9280 return 0;
9283 static inline u64 get_stat64(tg3_stat64_t *val)
9285 return ((u64)val->high << 32) | ((u64)val->low);
9288 static u64 calc_crc_errors(struct tg3 *tp)
9290 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9292 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9293 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9294 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
9295 u32 val;
9297 spin_lock_bh(&tp->lock);
9298 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9299 tg3_writephy(tp, MII_TG3_TEST1,
9300 val | MII_TG3_TEST1_CRC_EN);
9301 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
9302 } else
9303 val = 0;
9304 spin_unlock_bh(&tp->lock);
9306 tp->phy_crc_errors += val;
9308 return tp->phy_crc_errors;
9311 return get_stat64(&hw_stats->rx_fcs_errors);
9314 #define ESTAT_ADD(member) \
9315 estats->member = old_estats->member + \
9316 get_stat64(&hw_stats->member)
9318 static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9320 struct tg3_ethtool_stats *estats = &tp->estats;
9321 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9322 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9324 if (!hw_stats)
9325 return old_estats;
9327 ESTAT_ADD(rx_octets);
9328 ESTAT_ADD(rx_fragments);
9329 ESTAT_ADD(rx_ucast_packets);
9330 ESTAT_ADD(rx_mcast_packets);
9331 ESTAT_ADD(rx_bcast_packets);
9332 ESTAT_ADD(rx_fcs_errors);
9333 ESTAT_ADD(rx_align_errors);
9334 ESTAT_ADD(rx_xon_pause_rcvd);
9335 ESTAT_ADD(rx_xoff_pause_rcvd);
9336 ESTAT_ADD(rx_mac_ctrl_rcvd);
9337 ESTAT_ADD(rx_xoff_entered);
9338 ESTAT_ADD(rx_frame_too_long_errors);
9339 ESTAT_ADD(rx_jabbers);
9340 ESTAT_ADD(rx_undersize_packets);
9341 ESTAT_ADD(rx_in_length_errors);
9342 ESTAT_ADD(rx_out_length_errors);
9343 ESTAT_ADD(rx_64_or_less_octet_packets);
9344 ESTAT_ADD(rx_65_to_127_octet_packets);
9345 ESTAT_ADD(rx_128_to_255_octet_packets);
9346 ESTAT_ADD(rx_256_to_511_octet_packets);
9347 ESTAT_ADD(rx_512_to_1023_octet_packets);
9348 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9349 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9350 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9351 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9352 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9354 ESTAT_ADD(tx_octets);
9355 ESTAT_ADD(tx_collisions);
9356 ESTAT_ADD(tx_xon_sent);
9357 ESTAT_ADD(tx_xoff_sent);
9358 ESTAT_ADD(tx_flow_control);
9359 ESTAT_ADD(tx_mac_errors);
9360 ESTAT_ADD(tx_single_collisions);
9361 ESTAT_ADD(tx_mult_collisions);
9362 ESTAT_ADD(tx_deferred);
9363 ESTAT_ADD(tx_excessive_collisions);
9364 ESTAT_ADD(tx_late_collisions);
9365 ESTAT_ADD(tx_collide_2times);
9366 ESTAT_ADD(tx_collide_3times);
9367 ESTAT_ADD(tx_collide_4times);
9368 ESTAT_ADD(tx_collide_5times);
9369 ESTAT_ADD(tx_collide_6times);
9370 ESTAT_ADD(tx_collide_7times);
9371 ESTAT_ADD(tx_collide_8times);
9372 ESTAT_ADD(tx_collide_9times);
9373 ESTAT_ADD(tx_collide_10times);
9374 ESTAT_ADD(tx_collide_11times);
9375 ESTAT_ADD(tx_collide_12times);
9376 ESTAT_ADD(tx_collide_13times);
9377 ESTAT_ADD(tx_collide_14times);
9378 ESTAT_ADD(tx_collide_15times);
9379 ESTAT_ADD(tx_ucast_packets);
9380 ESTAT_ADD(tx_mcast_packets);
9381 ESTAT_ADD(tx_bcast_packets);
9382 ESTAT_ADD(tx_carrier_sense_errors);
9383 ESTAT_ADD(tx_discards);
9384 ESTAT_ADD(tx_errors);
9386 ESTAT_ADD(dma_writeq_full);
9387 ESTAT_ADD(dma_write_prioq_full);
9388 ESTAT_ADD(rxbds_empty);
9389 ESTAT_ADD(rx_discards);
9390 ESTAT_ADD(rx_errors);
9391 ESTAT_ADD(rx_threshold_hit);
9393 ESTAT_ADD(dma_readq_full);
9394 ESTAT_ADD(dma_read_prioq_full);
9395 ESTAT_ADD(tx_comp_queue_full);
9397 ESTAT_ADD(ring_set_send_prod_index);
9398 ESTAT_ADD(ring_status_update);
9399 ESTAT_ADD(nic_irqs);
9400 ESTAT_ADD(nic_avoided_irqs);
9401 ESTAT_ADD(nic_tx_threshold_hit);
9403 return estats;
9406 static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9407 struct rtnl_link_stats64 *stats)
9409 struct tg3 *tp = netdev_priv(dev);
9410 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
9411 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9413 if (!hw_stats)
9414 return old_stats;
9416 stats->rx_packets = old_stats->rx_packets +
9417 get_stat64(&hw_stats->rx_ucast_packets) +
9418 get_stat64(&hw_stats->rx_mcast_packets) +
9419 get_stat64(&hw_stats->rx_bcast_packets);
9421 stats->tx_packets = old_stats->tx_packets +
9422 get_stat64(&hw_stats->tx_ucast_packets) +
9423 get_stat64(&hw_stats->tx_mcast_packets) +
9424 get_stat64(&hw_stats->tx_bcast_packets);
9426 stats->rx_bytes = old_stats->rx_bytes +
9427 get_stat64(&hw_stats->rx_octets);
9428 stats->tx_bytes = old_stats->tx_bytes +
9429 get_stat64(&hw_stats->tx_octets);
9431 stats->rx_errors = old_stats->rx_errors +
9432 get_stat64(&hw_stats->rx_errors);
9433 stats->tx_errors = old_stats->tx_errors +
9434 get_stat64(&hw_stats->tx_errors) +
9435 get_stat64(&hw_stats->tx_mac_errors) +
9436 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9437 get_stat64(&hw_stats->tx_discards);
9439 stats->multicast = old_stats->multicast +
9440 get_stat64(&hw_stats->rx_mcast_packets);
9441 stats->collisions = old_stats->collisions +
9442 get_stat64(&hw_stats->tx_collisions);
9444 stats->rx_length_errors = old_stats->rx_length_errors +
9445 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9446 get_stat64(&hw_stats->rx_undersize_packets);
9448 stats->rx_over_errors = old_stats->rx_over_errors +
9449 get_stat64(&hw_stats->rxbds_empty);
9450 stats->rx_frame_errors = old_stats->rx_frame_errors +
9451 get_stat64(&hw_stats->rx_align_errors);
9452 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9453 get_stat64(&hw_stats->tx_discards);
9454 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9455 get_stat64(&hw_stats->tx_carrier_sense_errors);
9457 stats->rx_crc_errors = old_stats->rx_crc_errors +
9458 calc_crc_errors(tp);
9460 stats->rx_missed_errors = old_stats->rx_missed_errors +
9461 get_stat64(&hw_stats->rx_discards);
9463 stats->rx_dropped = tp->rx_dropped;
9465 return stats;
9468 static inline u32 calc_crc(unsigned char *buf, int len)
9470 u32 reg;
9471 u32 tmp;
9472 int j, k;
9474 reg = 0xffffffff;
9476 for (j = 0; j < len; j++) {
9477 reg ^= buf[j];
9479 for (k = 0; k < 8; k++) {
9480 tmp = reg & 0x01;
9482 reg >>= 1;
9484 if (tmp)
9485 reg ^= 0xedb88320;
9489 return ~reg;
9492 static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9494 /* accept or reject all multicast frames */
9495 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9496 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9497 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9498 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9501 static void __tg3_set_rx_mode(struct net_device *dev)
9503 struct tg3 *tp = netdev_priv(dev);
9504 u32 rx_mode;
9506 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9507 RX_MODE_KEEP_VLAN_TAG);
9509 #if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9510 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9511 * flag clear.
9513 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9514 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9515 #endif
9517 if (dev->flags & IFF_PROMISC) {
9518 /* Promiscuous mode. */
9519 rx_mode |= RX_MODE_PROMISC;
9520 } else if (dev->flags & IFF_ALLMULTI) {
9521 /* Accept all multicast. */
9522 tg3_set_multi(tp, 1);
9523 } else if (netdev_mc_empty(dev)) {
9524 /* Reject all multicast. */
9525 tg3_set_multi(tp, 0);
9526 } else {
9527 /* Accept one or more multicast(s). */
9528 struct netdev_hw_addr *ha;
9529 u32 mc_filter[4] = { 0, };
9530 u32 regidx;
9531 u32 bit;
9532 u32 crc;
9534 netdev_for_each_mc_addr(ha, dev) {
9535 crc = calc_crc(ha->addr, ETH_ALEN);
9536 bit = ~crc & 0x7f;
9537 regidx = (bit & 0x60) >> 5;
9538 bit &= 0x1f;
9539 mc_filter[regidx] |= (1 << bit);
9542 tw32(MAC_HASH_REG_0, mc_filter[0]);
9543 tw32(MAC_HASH_REG_1, mc_filter[1]);
9544 tw32(MAC_HASH_REG_2, mc_filter[2]);
9545 tw32(MAC_HASH_REG_3, mc_filter[3]);
9548 if (rx_mode != tp->rx_mode) {
9549 tp->rx_mode = rx_mode;
9550 tw32_f(MAC_RX_MODE, rx_mode);
9551 udelay(10);
9555 static void tg3_set_rx_mode(struct net_device *dev)
9557 struct tg3 *tp = netdev_priv(dev);
9559 if (!netif_running(dev))
9560 return;
9562 tg3_full_lock(tp, 0);
9563 __tg3_set_rx_mode(dev);
9564 tg3_full_unlock(tp);
9567 #define TG3_REGDUMP_LEN (32 * 1024)
9569 static int tg3_get_regs_len(struct net_device *dev)
9571 return TG3_REGDUMP_LEN;
9574 static void tg3_get_regs(struct net_device *dev,
9575 struct ethtool_regs *regs, void *_p)
9577 u32 *p = _p;
9578 struct tg3 *tp = netdev_priv(dev);
9579 u8 *orig_p = _p;
9580 int i;
9582 regs->version = 0;
9584 memset(p, 0, TG3_REGDUMP_LEN);
9586 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
9587 return;
9589 tg3_full_lock(tp, 0);
9591 #define __GET_REG32(reg) (*(p)++ = tr32(reg))
9592 #define GET_REG32_LOOP(base, len) \
9593 do { p = (u32 *)(orig_p + (base)); \
9594 for (i = 0; i < len; i += 4) \
9595 __GET_REG32((base) + i); \
9596 } while (0)
9597 #define GET_REG32_1(reg) \
9598 do { p = (u32 *)(orig_p + (reg)); \
9599 __GET_REG32((reg)); \
9600 } while (0)
9602 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9603 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9604 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9605 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9606 GET_REG32_1(SNDDATAC_MODE);
9607 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9608 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9609 GET_REG32_1(SNDBDC_MODE);
9610 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9611 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9612 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9613 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9614 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9615 GET_REG32_1(RCVDCC_MODE);
9616 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9617 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9618 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9619 GET_REG32_1(MBFREE_MODE);
9620 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9621 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9622 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9623 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9624 GET_REG32_LOOP(WDMAC_MODE, 0x08);
9625 GET_REG32_1(RX_CPU_MODE);
9626 GET_REG32_1(RX_CPU_STATE);
9627 GET_REG32_1(RX_CPU_PGMCTR);
9628 GET_REG32_1(RX_CPU_HWBKPT);
9629 GET_REG32_1(TX_CPU_MODE);
9630 GET_REG32_1(TX_CPU_STATE);
9631 GET_REG32_1(TX_CPU_PGMCTR);
9632 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9633 GET_REG32_LOOP(FTQ_RESET, 0x120);
9634 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9635 GET_REG32_1(DMAC_MODE);
9636 GET_REG32_LOOP(GRC_MODE, 0x4c);
9637 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9638 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9640 #undef __GET_REG32
9641 #undef GET_REG32_LOOP
9642 #undef GET_REG32_1
9644 tg3_full_unlock(tp);
9647 static int tg3_get_eeprom_len(struct net_device *dev)
9649 struct tg3 *tp = netdev_priv(dev);
9651 return tp->nvram_size;
9654 static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9656 struct tg3 *tp = netdev_priv(dev);
9657 int ret;
9658 u8 *pd;
9659 u32 i, offset, len, b_offset, b_count;
9660 __be32 val;
9662 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9663 return -EINVAL;
9665 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
9666 return -EAGAIN;
9668 offset = eeprom->offset;
9669 len = eeprom->len;
9670 eeprom->len = 0;
9672 eeprom->magic = TG3_EEPROM_MAGIC;
9674 if (offset & 3) {
9675 /* adjustments to start on required 4 byte boundary */
9676 b_offset = offset & 3;
9677 b_count = 4 - b_offset;
9678 if (b_count > len) {
9679 /* i.e. offset=1 len=2 */
9680 b_count = len;
9682 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
9683 if (ret)
9684 return ret;
9685 memcpy(data, ((char *)&val) + b_offset, b_count);
9686 len -= b_count;
9687 offset += b_count;
9688 eeprom->len += b_count;
9691 /* read bytes upto the last 4 byte boundary */
9692 pd = &data[eeprom->len];
9693 for (i = 0; i < (len - (len & 3)); i += 4) {
9694 ret = tg3_nvram_read_be32(tp, offset + i, &val);
9695 if (ret) {
9696 eeprom->len += i;
9697 return ret;
9699 memcpy(pd + i, &val, 4);
9701 eeprom->len += i;
9703 if (len & 3) {
9704 /* read last bytes not ending on 4 byte boundary */
9705 pd = &data[eeprom->len];
9706 b_count = len & 3;
9707 b_offset = offset + len - b_count;
9708 ret = tg3_nvram_read_be32(tp, b_offset, &val);
9709 if (ret)
9710 return ret;
9711 memcpy(pd, &val, b_count);
9712 eeprom->len += b_count;
9714 return 0;
9717 static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
9719 static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9721 struct tg3 *tp = netdev_priv(dev);
9722 int ret;
9723 u32 offset, len, b_offset, odd_len;
9724 u8 *buf;
9725 __be32 start, end;
9727 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
9728 return -EAGAIN;
9730 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9731 eeprom->magic != TG3_EEPROM_MAGIC)
9732 return -EINVAL;
9734 offset = eeprom->offset;
9735 len = eeprom->len;
9737 if ((b_offset = (offset & 3))) {
9738 /* adjustments to start on required 4 byte boundary */
9739 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
9740 if (ret)
9741 return ret;
9742 len += b_offset;
9743 offset &= ~3;
9744 if (len < 4)
9745 len = 4;
9748 odd_len = 0;
9749 if (len & 3) {
9750 /* adjustments to end on required 4 byte boundary */
9751 odd_len = 1;
9752 len = (len + 3) & ~3;
9753 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
9754 if (ret)
9755 return ret;
9758 buf = data;
9759 if (b_offset || odd_len) {
9760 buf = kmalloc(len, GFP_KERNEL);
9761 if (!buf)
9762 return -ENOMEM;
9763 if (b_offset)
9764 memcpy(buf, &start, 4);
9765 if (odd_len)
9766 memcpy(buf+len-4, &end, 4);
9767 memcpy(buf + b_offset, data, eeprom->len);
9770 ret = tg3_nvram_write_block(tp, offset, len, buf);
9772 if (buf != data)
9773 kfree(buf);
9775 return ret;
9778 static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9780 struct tg3 *tp = netdev_priv(dev);
9782 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9783 struct phy_device *phydev;
9784 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
9785 return -EAGAIN;
9786 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9787 return phy_ethtool_gset(phydev, cmd);
9790 cmd->supported = (SUPPORTED_Autoneg);
9792 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
9793 cmd->supported |= (SUPPORTED_1000baseT_Half |
9794 SUPPORTED_1000baseT_Full);
9796 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
9797 cmd->supported |= (SUPPORTED_100baseT_Half |
9798 SUPPORTED_100baseT_Full |
9799 SUPPORTED_10baseT_Half |
9800 SUPPORTED_10baseT_Full |
9801 SUPPORTED_TP);
9802 cmd->port = PORT_TP;
9803 } else {
9804 cmd->supported |= SUPPORTED_FIBRE;
9805 cmd->port = PORT_FIBRE;
9808 cmd->advertising = tp->link_config.advertising;
9809 if (netif_running(dev)) {
9810 cmd->speed = tp->link_config.active_speed;
9811 cmd->duplex = tp->link_config.active_duplex;
9812 } else {
9813 cmd->speed = SPEED_INVALID;
9814 cmd->duplex = DUPLEX_INVALID;
9816 cmd->phy_address = tp->phy_addr;
9817 cmd->transceiver = XCVR_INTERNAL;
9818 cmd->autoneg = tp->link_config.autoneg;
9819 cmd->maxtxpkt = 0;
9820 cmd->maxrxpkt = 0;
9821 return 0;
9824 static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9826 struct tg3 *tp = netdev_priv(dev);
9828 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9829 struct phy_device *phydev;
9830 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
9831 return -EAGAIN;
9832 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9833 return phy_ethtool_sset(phydev, cmd);
9836 if (cmd->autoneg != AUTONEG_ENABLE &&
9837 cmd->autoneg != AUTONEG_DISABLE)
9838 return -EINVAL;
9840 if (cmd->autoneg == AUTONEG_DISABLE &&
9841 cmd->duplex != DUPLEX_FULL &&
9842 cmd->duplex != DUPLEX_HALF)
9843 return -EINVAL;
9845 if (cmd->autoneg == AUTONEG_ENABLE) {
9846 u32 mask = ADVERTISED_Autoneg |
9847 ADVERTISED_Pause |
9848 ADVERTISED_Asym_Pause;
9850 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
9851 mask |= ADVERTISED_1000baseT_Half |
9852 ADVERTISED_1000baseT_Full;
9854 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
9855 mask |= ADVERTISED_100baseT_Half |
9856 ADVERTISED_100baseT_Full |
9857 ADVERTISED_10baseT_Half |
9858 ADVERTISED_10baseT_Full |
9859 ADVERTISED_TP;
9860 else
9861 mask |= ADVERTISED_FIBRE;
9863 if (cmd->advertising & ~mask)
9864 return -EINVAL;
9866 mask &= (ADVERTISED_1000baseT_Half |
9867 ADVERTISED_1000baseT_Full |
9868 ADVERTISED_100baseT_Half |
9869 ADVERTISED_100baseT_Full |
9870 ADVERTISED_10baseT_Half |
9871 ADVERTISED_10baseT_Full);
9873 cmd->advertising &= mask;
9874 } else {
9875 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
9876 if (cmd->speed != SPEED_1000)
9877 return -EINVAL;
9879 if (cmd->duplex != DUPLEX_FULL)
9880 return -EINVAL;
9881 } else {
9882 if (cmd->speed != SPEED_100 &&
9883 cmd->speed != SPEED_10)
9884 return -EINVAL;
9888 tg3_full_lock(tp, 0);
9890 tp->link_config.autoneg = cmd->autoneg;
9891 if (cmd->autoneg == AUTONEG_ENABLE) {
9892 tp->link_config.advertising = (cmd->advertising |
9893 ADVERTISED_Autoneg);
9894 tp->link_config.speed = SPEED_INVALID;
9895 tp->link_config.duplex = DUPLEX_INVALID;
9896 } else {
9897 tp->link_config.advertising = 0;
9898 tp->link_config.speed = cmd->speed;
9899 tp->link_config.duplex = cmd->duplex;
9902 tp->link_config.orig_speed = tp->link_config.speed;
9903 tp->link_config.orig_duplex = tp->link_config.duplex;
9904 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9906 if (netif_running(dev))
9907 tg3_setup_phy(tp, 1);
9909 tg3_full_unlock(tp);
9911 return 0;
9914 static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9916 struct tg3 *tp = netdev_priv(dev);
9918 strcpy(info->driver, DRV_MODULE_NAME);
9919 strcpy(info->version, DRV_MODULE_VERSION);
9920 strcpy(info->fw_version, tp->fw_ver);
9921 strcpy(info->bus_info, pci_name(tp->pdev));
9924 static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9926 struct tg3 *tp = netdev_priv(dev);
9928 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9929 device_can_wakeup(&tp->pdev->dev))
9930 wol->supported = WAKE_MAGIC;
9931 else
9932 wol->supported = 0;
9933 wol->wolopts = 0;
9934 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9935 device_can_wakeup(&tp->pdev->dev))
9936 wol->wolopts = WAKE_MAGIC;
9937 memset(&wol->sopass, 0, sizeof(wol->sopass));
9940 static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9942 struct tg3 *tp = netdev_priv(dev);
9943 struct device *dp = &tp->pdev->dev;
9945 if (wol->wolopts & ~WAKE_MAGIC)
9946 return -EINVAL;
9947 if ((wol->wolopts & WAKE_MAGIC) &&
9948 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
9949 return -EINVAL;
9951 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
9953 spin_lock_bh(&tp->lock);
9954 if (device_may_wakeup(dp))
9955 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
9956 else
9957 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
9958 spin_unlock_bh(&tp->lock);
9961 return 0;
9964 static u32 tg3_get_msglevel(struct net_device *dev)
9966 struct tg3 *tp = netdev_priv(dev);
9967 return tp->msg_enable;
9970 static void tg3_set_msglevel(struct net_device *dev, u32 value)
9972 struct tg3 *tp = netdev_priv(dev);
9973 tp->msg_enable = value;
9976 static int tg3_set_tso(struct net_device *dev, u32 value)
9978 struct tg3 *tp = netdev_priv(dev);
9980 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9981 if (value)
9982 return -EINVAL;
9983 return 0;
9985 if ((dev->features & NETIF_F_IPV6_CSUM) &&
9986 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9987 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
9988 if (value) {
9989 dev->features |= NETIF_F_TSO6;
9990 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9991 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
9992 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9993 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
9994 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9995 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
9996 dev->features |= NETIF_F_TSO_ECN;
9997 } else
9998 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
10000 return ethtool_op_set_tso(dev, value);
10003 static int tg3_nway_reset(struct net_device *dev)
10005 struct tg3 *tp = netdev_priv(dev);
10006 int r;
10008 if (!netif_running(dev))
10009 return -EAGAIN;
10011 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
10012 return -EINVAL;
10014 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
10015 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
10016 return -EAGAIN;
10017 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
10018 } else {
10019 u32 bmcr;
10021 spin_lock_bh(&tp->lock);
10022 r = -EINVAL;
10023 tg3_readphy(tp, MII_BMCR, &bmcr);
10024 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10025 ((bmcr & BMCR_ANENABLE) ||
10026 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
10027 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10028 BMCR_ANENABLE);
10029 r = 0;
10031 spin_unlock_bh(&tp->lock);
10034 return r;
10037 static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10039 struct tg3 *tp = netdev_priv(dev);
10041 ering->rx_max_pending = tp->rx_std_ring_mask;
10042 ering->rx_mini_max_pending = 0;
10043 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10044 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
10045 else
10046 ering->rx_jumbo_max_pending = 0;
10048 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
10050 ering->rx_pending = tp->rx_pending;
10051 ering->rx_mini_pending = 0;
10052 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10053 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10054 else
10055 ering->rx_jumbo_pending = 0;
10057 ering->tx_pending = tp->napi[0].tx_pending;
10060 static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10062 struct tg3 *tp = netdev_priv(dev);
10063 int i, irq_sync = 0, err = 0;
10065 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10066 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
10067 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10068 (ering->tx_pending <= MAX_SKB_FRAGS) ||
10069 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
10070 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
10071 return -EINVAL;
10073 if (netif_running(dev)) {
10074 tg3_phy_stop(tp);
10075 tg3_netif_stop(tp);
10076 irq_sync = 1;
10079 tg3_full_lock(tp, irq_sync);
10081 tp->rx_pending = ering->rx_pending;
10083 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
10084 tp->rx_pending > 63)
10085 tp->rx_pending = 63;
10086 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
10088 for (i = 0; i < tp->irq_max; i++)
10089 tp->napi[i].tx_pending = ering->tx_pending;
10091 if (netif_running(dev)) {
10092 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10093 err = tg3_restart_hw(tp, 1);
10094 if (!err)
10095 tg3_netif_start(tp);
10098 tg3_full_unlock(tp);
10100 if (irq_sync && !err)
10101 tg3_phy_start(tp);
10103 return err;
10106 static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10108 struct tg3 *tp = netdev_priv(dev);
10110 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
10112 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
10113 epause->rx_pause = 1;
10114 else
10115 epause->rx_pause = 0;
10117 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
10118 epause->tx_pause = 1;
10119 else
10120 epause->tx_pause = 0;
10123 static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10125 struct tg3 *tp = netdev_priv(dev);
10126 int err = 0;
10128 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
10129 u32 newadv;
10130 struct phy_device *phydev;
10132 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10134 if (!(phydev->supported & SUPPORTED_Pause) ||
10135 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
10136 (epause->rx_pause != epause->tx_pause)))
10137 return -EINVAL;
10139 tp->link_config.flowctrl = 0;
10140 if (epause->rx_pause) {
10141 tp->link_config.flowctrl |= FLOW_CTRL_RX;
10143 if (epause->tx_pause) {
10144 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10145 newadv = ADVERTISED_Pause;
10146 } else
10147 newadv = ADVERTISED_Pause |
10148 ADVERTISED_Asym_Pause;
10149 } else if (epause->tx_pause) {
10150 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10151 newadv = ADVERTISED_Asym_Pause;
10152 } else
10153 newadv = 0;
10155 if (epause->autoneg)
10156 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10157 else
10158 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10160 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
10161 u32 oldadv = phydev->advertising &
10162 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10163 if (oldadv != newadv) {
10164 phydev->advertising &=
10165 ~(ADVERTISED_Pause |
10166 ADVERTISED_Asym_Pause);
10167 phydev->advertising |= newadv;
10168 if (phydev->autoneg) {
10170 * Always renegotiate the link to
10171 * inform our link partner of our
10172 * flow control settings, even if the
10173 * flow control is forced. Let
10174 * tg3_adjust_link() do the final
10175 * flow control setup.
10177 return phy_start_aneg(phydev);
10181 if (!epause->autoneg)
10182 tg3_setup_flow_control(tp, 0, 0);
10183 } else {
10184 tp->link_config.orig_advertising &=
10185 ~(ADVERTISED_Pause |
10186 ADVERTISED_Asym_Pause);
10187 tp->link_config.orig_advertising |= newadv;
10189 } else {
10190 int irq_sync = 0;
10192 if (netif_running(dev)) {
10193 tg3_netif_stop(tp);
10194 irq_sync = 1;
10197 tg3_full_lock(tp, irq_sync);
10199 if (epause->autoneg)
10200 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10201 else
10202 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10203 if (epause->rx_pause)
10204 tp->link_config.flowctrl |= FLOW_CTRL_RX;
10205 else
10206 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
10207 if (epause->tx_pause)
10208 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10209 else
10210 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
10212 if (netif_running(dev)) {
10213 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10214 err = tg3_restart_hw(tp, 1);
10215 if (!err)
10216 tg3_netif_start(tp);
10219 tg3_full_unlock(tp);
10222 return err;
10225 static u32 tg3_get_rx_csum(struct net_device *dev)
10227 struct tg3 *tp = netdev_priv(dev);
10228 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
10231 static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10233 struct tg3 *tp = netdev_priv(dev);
10235 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10236 if (data != 0)
10237 return -EINVAL;
10238 return 0;
10241 spin_lock_bh(&tp->lock);
10242 if (data)
10243 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10244 else
10245 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
10246 spin_unlock_bh(&tp->lock);
10248 return 0;
10251 static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10253 struct tg3 *tp = netdev_priv(dev);
10255 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10256 if (data != 0)
10257 return -EINVAL;
10258 return 0;
10261 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
10262 ethtool_op_set_tx_ipv6_csum(dev, data);
10263 else
10264 ethtool_op_set_tx_csum(dev, data);
10266 return 0;
10269 static int tg3_get_sset_count(struct net_device *dev, int sset)
10271 switch (sset) {
10272 case ETH_SS_TEST:
10273 return TG3_NUM_TEST;
10274 case ETH_SS_STATS:
10275 return TG3_NUM_STATS;
10276 default:
10277 return -EOPNOTSUPP;
10281 static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
10283 switch (stringset) {
10284 case ETH_SS_STATS:
10285 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10286 break;
10287 case ETH_SS_TEST:
10288 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10289 break;
10290 default:
10291 WARN_ON(1); /* we need a WARN() */
10292 break;
10296 static int tg3_phys_id(struct net_device *dev, u32 data)
10298 struct tg3 *tp = netdev_priv(dev);
10299 int i;
10301 if (!netif_running(tp->dev))
10302 return -EAGAIN;
10304 if (data == 0)
10305 data = UINT_MAX / 2;
10307 for (i = 0; i < (data * 2); i++) {
10308 if ((i % 2) == 0)
10309 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10310 LED_CTRL_1000MBPS_ON |
10311 LED_CTRL_100MBPS_ON |
10312 LED_CTRL_10MBPS_ON |
10313 LED_CTRL_TRAFFIC_OVERRIDE |
10314 LED_CTRL_TRAFFIC_BLINK |
10315 LED_CTRL_TRAFFIC_LED);
10317 else
10318 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10319 LED_CTRL_TRAFFIC_OVERRIDE);
10321 if (msleep_interruptible(500))
10322 break;
10324 tw32(MAC_LED_CTRL, tp->led_ctrl);
10325 return 0;
10328 static void tg3_get_ethtool_stats(struct net_device *dev,
10329 struct ethtool_stats *estats, u64 *tmp_stats)
10331 struct tg3 *tp = netdev_priv(dev);
10332 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10335 #define NVRAM_TEST_SIZE 0x100
10336 #define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10337 #define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10338 #define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
10339 #define NVRAM_SELFBOOT_HW_SIZE 0x20
10340 #define NVRAM_SELFBOOT_DATA_SIZE 0x1c
10342 static int tg3_test_nvram(struct tg3 *tp)
10344 u32 csum, magic;
10345 __be32 *buf;
10346 int i, j, k, err = 0, size;
10348 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10349 return 0;
10351 if (tg3_nvram_read(tp, 0, &magic) != 0)
10352 return -EIO;
10354 if (magic == TG3_EEPROM_MAGIC)
10355 size = NVRAM_TEST_SIZE;
10356 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
10357 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10358 TG3_EEPROM_SB_FORMAT_1) {
10359 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10360 case TG3_EEPROM_SB_REVISION_0:
10361 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10362 break;
10363 case TG3_EEPROM_SB_REVISION_2:
10364 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10365 break;
10366 case TG3_EEPROM_SB_REVISION_3:
10367 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10368 break;
10369 default:
10370 return 0;
10372 } else
10373 return 0;
10374 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10375 size = NVRAM_SELFBOOT_HW_SIZE;
10376 else
10377 return -EIO;
10379 buf = kmalloc(size, GFP_KERNEL);
10380 if (buf == NULL)
10381 return -ENOMEM;
10383 err = -EIO;
10384 for (i = 0, j = 0; i < size; i += 4, j++) {
10385 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10386 if (err)
10387 break;
10389 if (i < size)
10390 goto out;
10392 /* Selfboot format */
10393 magic = be32_to_cpu(buf[0]);
10394 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
10395 TG3_EEPROM_MAGIC_FW) {
10396 u8 *buf8 = (u8 *) buf, csum8 = 0;
10398 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
10399 TG3_EEPROM_SB_REVISION_2) {
10400 /* For rev 2, the csum doesn't include the MBA. */
10401 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10402 csum8 += buf8[i];
10403 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10404 csum8 += buf8[i];
10405 } else {
10406 for (i = 0; i < size; i++)
10407 csum8 += buf8[i];
10410 if (csum8 == 0) {
10411 err = 0;
10412 goto out;
10415 err = -EIO;
10416 goto out;
10419 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
10420 TG3_EEPROM_MAGIC_HW) {
10421 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
10422 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
10423 u8 *buf8 = (u8 *) buf;
10425 /* Separate the parity bits and the data bytes. */
10426 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10427 if ((i == 0) || (i == 8)) {
10428 int l;
10429 u8 msk;
10431 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10432 parity[k++] = buf8[i] & msk;
10433 i++;
10434 } else if (i == 16) {
10435 int l;
10436 u8 msk;
10438 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10439 parity[k++] = buf8[i] & msk;
10440 i++;
10442 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10443 parity[k++] = buf8[i] & msk;
10444 i++;
10446 data[j++] = buf8[i];
10449 err = -EIO;
10450 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10451 u8 hw8 = hweight8(data[i]);
10453 if ((hw8 & 0x1) && parity[i])
10454 goto out;
10455 else if (!(hw8 & 0x1) && !parity[i])
10456 goto out;
10458 err = 0;
10459 goto out;
10462 /* Bootstrap checksum at offset 0x10 */
10463 csum = calc_crc((unsigned char *) buf, 0x10);
10464 if (csum != be32_to_cpu(buf[0x10/4]))
10465 goto out;
10467 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10468 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
10469 if (csum != be32_to_cpu(buf[0xfc/4]))
10470 goto out;
10472 err = 0;
10474 out:
10475 kfree(buf);
10476 return err;
10479 #define TG3_SERDES_TIMEOUT_SEC 2
10480 #define TG3_COPPER_TIMEOUT_SEC 6
10482 static int tg3_test_link(struct tg3 *tp)
10484 int i, max;
10486 if (!netif_running(tp->dev))
10487 return -ENODEV;
10489 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
10490 max = TG3_SERDES_TIMEOUT_SEC;
10491 else
10492 max = TG3_COPPER_TIMEOUT_SEC;
10494 for (i = 0; i < max; i++) {
10495 if (netif_carrier_ok(tp->dev))
10496 return 0;
10498 if (msleep_interruptible(1000))
10499 break;
10502 return -EIO;
10505 /* Only test the commonly used registers */
10506 static int tg3_test_registers(struct tg3 *tp)
10508 int i, is_5705, is_5750;
10509 u32 offset, read_mask, write_mask, val, save_val, read_val;
10510 static struct {
10511 u16 offset;
10512 u16 flags;
10513 #define TG3_FL_5705 0x1
10514 #define TG3_FL_NOT_5705 0x2
10515 #define TG3_FL_NOT_5788 0x4
10516 #define TG3_FL_NOT_5750 0x8
10517 u32 read_mask;
10518 u32 write_mask;
10519 } reg_tbl[] = {
10520 /* MAC Control Registers */
10521 { MAC_MODE, TG3_FL_NOT_5705,
10522 0x00000000, 0x00ef6f8c },
10523 { MAC_MODE, TG3_FL_5705,
10524 0x00000000, 0x01ef6b8c },
10525 { MAC_STATUS, TG3_FL_NOT_5705,
10526 0x03800107, 0x00000000 },
10527 { MAC_STATUS, TG3_FL_5705,
10528 0x03800100, 0x00000000 },
10529 { MAC_ADDR_0_HIGH, 0x0000,
10530 0x00000000, 0x0000ffff },
10531 { MAC_ADDR_0_LOW, 0x0000,
10532 0x00000000, 0xffffffff },
10533 { MAC_RX_MTU_SIZE, 0x0000,
10534 0x00000000, 0x0000ffff },
10535 { MAC_TX_MODE, 0x0000,
10536 0x00000000, 0x00000070 },
10537 { MAC_TX_LENGTHS, 0x0000,
10538 0x00000000, 0x00003fff },
10539 { MAC_RX_MODE, TG3_FL_NOT_5705,
10540 0x00000000, 0x000007fc },
10541 { MAC_RX_MODE, TG3_FL_5705,
10542 0x00000000, 0x000007dc },
10543 { MAC_HASH_REG_0, 0x0000,
10544 0x00000000, 0xffffffff },
10545 { MAC_HASH_REG_1, 0x0000,
10546 0x00000000, 0xffffffff },
10547 { MAC_HASH_REG_2, 0x0000,
10548 0x00000000, 0xffffffff },
10549 { MAC_HASH_REG_3, 0x0000,
10550 0x00000000, 0xffffffff },
10552 /* Receive Data and Receive BD Initiator Control Registers. */
10553 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10554 0x00000000, 0xffffffff },
10555 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10556 0x00000000, 0xffffffff },
10557 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10558 0x00000000, 0x00000003 },
10559 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10560 0x00000000, 0xffffffff },
10561 { RCVDBDI_STD_BD+0, 0x0000,
10562 0x00000000, 0xffffffff },
10563 { RCVDBDI_STD_BD+4, 0x0000,
10564 0x00000000, 0xffffffff },
10565 { RCVDBDI_STD_BD+8, 0x0000,
10566 0x00000000, 0xffff0002 },
10567 { RCVDBDI_STD_BD+0xc, 0x0000,
10568 0x00000000, 0xffffffff },
10570 /* Receive BD Initiator Control Registers. */
10571 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10572 0x00000000, 0xffffffff },
10573 { RCVBDI_STD_THRESH, TG3_FL_5705,
10574 0x00000000, 0x000003ff },
10575 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10576 0x00000000, 0xffffffff },
10578 /* Host Coalescing Control Registers. */
10579 { HOSTCC_MODE, TG3_FL_NOT_5705,
10580 0x00000000, 0x00000004 },
10581 { HOSTCC_MODE, TG3_FL_5705,
10582 0x00000000, 0x000000f6 },
10583 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10584 0x00000000, 0xffffffff },
10585 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10586 0x00000000, 0x000003ff },
10587 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10588 0x00000000, 0xffffffff },
10589 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10590 0x00000000, 0x000003ff },
10591 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10592 0x00000000, 0xffffffff },
10593 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10594 0x00000000, 0x000000ff },
10595 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10596 0x00000000, 0xffffffff },
10597 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10598 0x00000000, 0x000000ff },
10599 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10600 0x00000000, 0xffffffff },
10601 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10602 0x00000000, 0xffffffff },
10603 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10604 0x00000000, 0xffffffff },
10605 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10606 0x00000000, 0x000000ff },
10607 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10608 0x00000000, 0xffffffff },
10609 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10610 0x00000000, 0x000000ff },
10611 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10612 0x00000000, 0xffffffff },
10613 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10614 0x00000000, 0xffffffff },
10615 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10616 0x00000000, 0xffffffff },
10617 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10618 0x00000000, 0xffffffff },
10619 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10620 0x00000000, 0xffffffff },
10621 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10622 0xffffffff, 0x00000000 },
10623 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10624 0xffffffff, 0x00000000 },
10626 /* Buffer Manager Control Registers. */
10627 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
10628 0x00000000, 0x007fff80 },
10629 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
10630 0x00000000, 0x007fffff },
10631 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10632 0x00000000, 0x0000003f },
10633 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10634 0x00000000, 0x000001ff },
10635 { BUFMGR_MB_HIGH_WATER, 0x0000,
10636 0x00000000, 0x000001ff },
10637 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10638 0xffffffff, 0x00000000 },
10639 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10640 0xffffffff, 0x00000000 },
10642 /* Mailbox Registers */
10643 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10644 0x00000000, 0x000001ff },
10645 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10646 0x00000000, 0x000001ff },
10647 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10648 0x00000000, 0x000007ff },
10649 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10650 0x00000000, 0x000001ff },
10652 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10655 is_5705 = is_5750 = 0;
10656 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
10657 is_5705 = 1;
10658 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10659 is_5750 = 1;
10662 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10663 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10664 continue;
10666 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10667 continue;
10669 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10670 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10671 continue;
10673 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10674 continue;
10676 offset = (u32) reg_tbl[i].offset;
10677 read_mask = reg_tbl[i].read_mask;
10678 write_mask = reg_tbl[i].write_mask;
10680 /* Save the original register content */
10681 save_val = tr32(offset);
10683 /* Determine the read-only value. */
10684 read_val = save_val & read_mask;
10686 /* Write zero to the register, then make sure the read-only bits
10687 * are not changed and the read/write bits are all zeros.
10689 tw32(offset, 0);
10691 val = tr32(offset);
10693 /* Test the read-only and read/write bits. */
10694 if (((val & read_mask) != read_val) || (val & write_mask))
10695 goto out;
10697 /* Write ones to all the bits defined by RdMask and WrMask, then
10698 * make sure the read-only bits are not changed and the
10699 * read/write bits are all ones.
10701 tw32(offset, read_mask | write_mask);
10703 val = tr32(offset);
10705 /* Test the read-only bits. */
10706 if ((val & read_mask) != read_val)
10707 goto out;
10709 /* Test the read/write bits. */
10710 if ((val & write_mask) != write_mask)
10711 goto out;
10713 tw32(offset, save_val);
10716 return 0;
10718 out:
10719 if (netif_msg_hw(tp))
10720 netdev_err(tp->dev,
10721 "Register test failed at offset %x\n", offset);
10722 tw32(offset, save_val);
10723 return -EIO;
10726 static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10728 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
10729 int i;
10730 u32 j;
10732 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
10733 for (j = 0; j < len; j += 4) {
10734 u32 val;
10736 tg3_write_mem(tp, offset + j, test_pattern[i]);
10737 tg3_read_mem(tp, offset + j, &val);
10738 if (val != test_pattern[i])
10739 return -EIO;
10742 return 0;
10745 static int tg3_test_memory(struct tg3 *tp)
10747 static struct mem_entry {
10748 u32 offset;
10749 u32 len;
10750 } mem_tbl_570x[] = {
10751 { 0x00000000, 0x00b50},
10752 { 0x00002000, 0x1c000},
10753 { 0xffffffff, 0x00000}
10754 }, mem_tbl_5705[] = {
10755 { 0x00000100, 0x0000c},
10756 { 0x00000200, 0x00008},
10757 { 0x00004000, 0x00800},
10758 { 0x00006000, 0x01000},
10759 { 0x00008000, 0x02000},
10760 { 0x00010000, 0x0e000},
10761 { 0xffffffff, 0x00000}
10762 }, mem_tbl_5755[] = {
10763 { 0x00000200, 0x00008},
10764 { 0x00004000, 0x00800},
10765 { 0x00006000, 0x00800},
10766 { 0x00008000, 0x02000},
10767 { 0x00010000, 0x0c000},
10768 { 0xffffffff, 0x00000}
10769 }, mem_tbl_5906[] = {
10770 { 0x00000200, 0x00008},
10771 { 0x00004000, 0x00400},
10772 { 0x00006000, 0x00400},
10773 { 0x00008000, 0x01000},
10774 { 0x00010000, 0x01000},
10775 { 0xffffffff, 0x00000}
10776 }, mem_tbl_5717[] = {
10777 { 0x00000200, 0x00008},
10778 { 0x00010000, 0x0a000},
10779 { 0x00020000, 0x13c00},
10780 { 0xffffffff, 0x00000}
10781 }, mem_tbl_57765[] = {
10782 { 0x00000200, 0x00008},
10783 { 0x00004000, 0x00800},
10784 { 0x00006000, 0x09800},
10785 { 0x00010000, 0x0a000},
10786 { 0xffffffff, 0x00000}
10788 struct mem_entry *mem_tbl;
10789 int err = 0;
10790 int i;
10792 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
10793 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
10794 mem_tbl = mem_tbl_5717;
10795 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10796 mem_tbl = mem_tbl_57765;
10797 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
10798 mem_tbl = mem_tbl_5755;
10799 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10800 mem_tbl = mem_tbl_5906;
10801 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10802 mem_tbl = mem_tbl_5705;
10803 else
10804 mem_tbl = mem_tbl_570x;
10806 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10807 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10808 if (err)
10809 break;
10812 return err;
10815 #define TG3_MAC_LOOPBACK 0
10816 #define TG3_PHY_LOOPBACK 1
10818 static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
10820 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
10821 u32 desc_idx, coal_now;
10822 struct sk_buff *skb, *rx_skb;
10823 u8 *tx_data;
10824 dma_addr_t map;
10825 int num_pkts, tx_len, rx_len, i, err;
10826 struct tg3_rx_buffer_desc *desc;
10827 struct tg3_napi *tnapi, *rnapi;
10828 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
10830 tnapi = &tp->napi[0];
10831 rnapi = &tp->napi[0];
10832 if (tp->irq_cnt > 1) {
10833 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
10834 rnapi = &tp->napi[1];
10835 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10836 tnapi = &tp->napi[1];
10838 coal_now = tnapi->coal_now | rnapi->coal_now;
10840 if (loopback_mode == TG3_MAC_LOOPBACK) {
10841 /* HW errata - mac loopback fails in some cases on 5780.
10842 * Normal traffic and PHY loopback are not affected by
10843 * errata.
10845 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10846 return 0;
10848 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
10849 MAC_MODE_PORT_INT_LPBACK;
10850 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10851 mac_mode |= MAC_MODE_LINK_POLARITY;
10852 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
10853 mac_mode |= MAC_MODE_PORT_MODE_MII;
10854 else
10855 mac_mode |= MAC_MODE_PORT_MODE_GMII;
10856 tw32(MAC_MODE, mac_mode);
10857 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
10858 u32 val;
10860 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
10861 tg3_phy_fet_toggle_apd(tp, false);
10862 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10863 } else
10864 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
10866 tg3_phy_toggle_automdix(tp, 0);
10868 tg3_writephy(tp, MII_BMCR, val);
10869 udelay(40);
10871 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
10872 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
10873 tg3_writephy(tp, MII_TG3_FET_PTEST,
10874 MII_TG3_FET_PTEST_FRC_TX_LINK |
10875 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10876 /* The write needs to be flushed for the AC131 */
10877 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10878 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
10879 mac_mode |= MAC_MODE_PORT_MODE_MII;
10880 } else
10881 mac_mode |= MAC_MODE_PORT_MODE_GMII;
10883 /* reset to prevent losing 1st rx packet intermittently */
10884 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10885 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10886 udelay(10);
10887 tw32_f(MAC_RX_MODE, tp->rx_mode);
10889 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
10890 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10891 if (masked_phy_id == TG3_PHY_ID_BCM5401)
10892 mac_mode &= ~MAC_MODE_LINK_POLARITY;
10893 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
10894 mac_mode |= MAC_MODE_LINK_POLARITY;
10895 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10896 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10898 tw32(MAC_MODE, mac_mode);
10899 } else {
10900 return -EINVAL;
10903 err = -EIO;
10905 tx_len = 1514;
10906 skb = netdev_alloc_skb(tp->dev, tx_len);
10907 if (!skb)
10908 return -ENOMEM;
10910 tx_data = skb_put(skb, tx_len);
10911 memcpy(tx_data, tp->dev->dev_addr, 6);
10912 memset(tx_data + 6, 0x0, 8);
10914 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10916 for (i = 14; i < tx_len; i++)
10917 tx_data[i] = (u8) (i & 0xff);
10919 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10920 if (pci_dma_mapping_error(tp->pdev, map)) {
10921 dev_kfree_skb(skb);
10922 return -EIO;
10925 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
10926 rnapi->coal_now);
10928 udelay(10);
10930 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
10932 num_pkts = 0;
10934 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
10936 tnapi->tx_prod++;
10937 num_pkts++;
10939 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10940 tr32_mailbox(tnapi->prodmbox);
10942 udelay(10);
10944 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10945 for (i = 0; i < 35; i++) {
10946 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
10947 coal_now);
10949 udelay(10);
10951 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10952 rx_idx = rnapi->hw_status->idx[0].rx_producer;
10953 if ((tx_idx == tnapi->tx_prod) &&
10954 (rx_idx == (rx_start_idx + num_pkts)))
10955 break;
10958 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
10959 dev_kfree_skb(skb);
10961 if (tx_idx != tnapi->tx_prod)
10962 goto out;
10964 if (rx_idx != rx_start_idx + num_pkts)
10965 goto out;
10967 desc = &rnapi->rx_rcb[rx_start_idx];
10968 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10969 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10970 if (opaque_key != RXD_OPAQUE_RING_STD)
10971 goto out;
10973 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10974 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10975 goto out;
10977 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10978 if (rx_len != tx_len)
10979 goto out;
10981 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
10983 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
10984 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10986 for (i = 14; i < tx_len; i++) {
10987 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10988 goto out;
10990 err = 0;
10992 /* tg3_free_rings will unmap and free the rx_skb */
10993 out:
10994 return err;
10997 #define TG3_MAC_LOOPBACK_FAILED 1
10998 #define TG3_PHY_LOOPBACK_FAILED 2
10999 #define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
11000 TG3_PHY_LOOPBACK_FAILED)
11002 static int tg3_test_loopback(struct tg3 *tp)
11004 int err = 0;
11005 u32 cpmuctrl = 0;
11007 if (!netif_running(tp->dev))
11008 return TG3_LOOPBACK_FAILED;
11010 err = tg3_reset_hw(tp, 1);
11011 if (err)
11012 return TG3_LOOPBACK_FAILED;
11014 /* Turn off gphy autopowerdown. */
11015 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11016 tg3_phy_toggle_apd(tp, false);
11018 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
11019 int i;
11020 u32 status;
11022 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11024 /* Wait for up to 40 microseconds to acquire lock. */
11025 for (i = 0; i < 4; i++) {
11026 status = tr32(TG3_CPMU_MUTEX_GNT);
11027 if (status == CPMU_MUTEX_GNT_DRIVER)
11028 break;
11029 udelay(10);
11032 if (status != CPMU_MUTEX_GNT_DRIVER)
11033 return TG3_LOOPBACK_FAILED;
11035 /* Turn off link-based power management. */
11036 cpmuctrl = tr32(TG3_CPMU_CTRL);
11037 tw32(TG3_CPMU_CTRL,
11038 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11039 CPMU_CTRL_LINK_AWARE_MODE));
11042 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
11043 err |= TG3_MAC_LOOPBACK_FAILED;
11045 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
11046 tw32(TG3_CPMU_CTRL, cpmuctrl);
11048 /* Release the mutex */
11049 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11052 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
11053 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
11054 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
11055 err |= TG3_PHY_LOOPBACK_FAILED;
11058 /* Re-enable gphy autopowerdown. */
11059 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11060 tg3_phy_toggle_apd(tp, true);
11062 return err;
11065 static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11066 u64 *data)
11068 struct tg3 *tp = netdev_priv(dev);
11070 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
11071 tg3_power_up(tp);
11073 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11075 if (tg3_test_nvram(tp) != 0) {
11076 etest->flags |= ETH_TEST_FL_FAILED;
11077 data[0] = 1;
11079 if (tg3_test_link(tp) != 0) {
11080 etest->flags |= ETH_TEST_FL_FAILED;
11081 data[1] = 1;
11083 if (etest->flags & ETH_TEST_FL_OFFLINE) {
11084 int err, err2 = 0, irq_sync = 0;
11086 if (netif_running(dev)) {
11087 tg3_phy_stop(tp);
11088 tg3_netif_stop(tp);
11089 irq_sync = 1;
11092 tg3_full_lock(tp, irq_sync);
11094 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
11095 err = tg3_nvram_lock(tp);
11096 tg3_halt_cpu(tp, RX_CPU_BASE);
11097 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11098 tg3_halt_cpu(tp, TX_CPU_BASE);
11099 if (!err)
11100 tg3_nvram_unlock(tp);
11102 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
11103 tg3_phy_reset(tp);
11105 if (tg3_test_registers(tp) != 0) {
11106 etest->flags |= ETH_TEST_FL_FAILED;
11107 data[2] = 1;
11109 if (tg3_test_memory(tp) != 0) {
11110 etest->flags |= ETH_TEST_FL_FAILED;
11111 data[3] = 1;
11113 if ((data[4] = tg3_test_loopback(tp)) != 0)
11114 etest->flags |= ETH_TEST_FL_FAILED;
11116 tg3_full_unlock(tp);
11118 if (tg3_test_interrupt(tp) != 0) {
11119 etest->flags |= ETH_TEST_FL_FAILED;
11120 data[5] = 1;
11123 tg3_full_lock(tp, 0);
11125 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11126 if (netif_running(dev)) {
11127 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
11128 err2 = tg3_restart_hw(tp, 1);
11129 if (!err2)
11130 tg3_netif_start(tp);
11133 tg3_full_unlock(tp);
11135 if (irq_sync && !err2)
11136 tg3_phy_start(tp);
11138 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
11139 tg3_power_down(tp);
11143 static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11145 struct mii_ioctl_data *data = if_mii(ifr);
11146 struct tg3 *tp = netdev_priv(dev);
11147 int err;
11149 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
11150 struct phy_device *phydev;
11151 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
11152 return -EAGAIN;
11153 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11154 return phy_mii_ioctl(phydev, ifr, cmd);
11157 switch (cmd) {
11158 case SIOCGMIIPHY:
11159 data->phy_id = tp->phy_addr;
11161 /* fallthru */
11162 case SIOCGMIIREG: {
11163 u32 mii_regval;
11165 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
11166 break; /* We have no PHY */
11168 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
11169 return -EAGAIN;
11171 spin_lock_bh(&tp->lock);
11172 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
11173 spin_unlock_bh(&tp->lock);
11175 data->val_out = mii_regval;
11177 return err;
11180 case SIOCSMIIREG:
11181 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
11182 break; /* We have no PHY */
11184 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
11185 return -EAGAIN;
11187 spin_lock_bh(&tp->lock);
11188 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
11189 spin_unlock_bh(&tp->lock);
11191 return err;
11193 default:
11194 /* do nothing */
11195 break;
11197 return -EOPNOTSUPP;
11200 static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11202 struct tg3 *tp = netdev_priv(dev);
11204 memcpy(ec, &tp->coal, sizeof(*ec));
11205 return 0;
11208 static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11210 struct tg3 *tp = netdev_priv(dev);
11211 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11212 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11214 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11215 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11216 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11217 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11218 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11221 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11222 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11223 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11224 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11225 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11226 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11227 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11228 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11229 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11230 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11231 return -EINVAL;
11233 /* No rx interrupts will be generated if both are zero */
11234 if ((ec->rx_coalesce_usecs == 0) &&
11235 (ec->rx_max_coalesced_frames == 0))
11236 return -EINVAL;
11238 /* No tx interrupts will be generated if both are zero */
11239 if ((ec->tx_coalesce_usecs == 0) &&
11240 (ec->tx_max_coalesced_frames == 0))
11241 return -EINVAL;
11243 /* Only copy relevant parameters, ignore all others. */
11244 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11245 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11246 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11247 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11248 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11249 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11250 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11251 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11252 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11254 if (netif_running(dev)) {
11255 tg3_full_lock(tp, 0);
11256 __tg3_set_coalesce(tp, &tp->coal);
11257 tg3_full_unlock(tp);
11259 return 0;
11262 static const struct ethtool_ops tg3_ethtool_ops = {
11263 .get_settings = tg3_get_settings,
11264 .set_settings = tg3_set_settings,
11265 .get_drvinfo = tg3_get_drvinfo,
11266 .get_regs_len = tg3_get_regs_len,
11267 .get_regs = tg3_get_regs,
11268 .get_wol = tg3_get_wol,
11269 .set_wol = tg3_set_wol,
11270 .get_msglevel = tg3_get_msglevel,
11271 .set_msglevel = tg3_set_msglevel,
11272 .nway_reset = tg3_nway_reset,
11273 .get_link = ethtool_op_get_link,
11274 .get_eeprom_len = tg3_get_eeprom_len,
11275 .get_eeprom = tg3_get_eeprom,
11276 .set_eeprom = tg3_set_eeprom,
11277 .get_ringparam = tg3_get_ringparam,
11278 .set_ringparam = tg3_set_ringparam,
11279 .get_pauseparam = tg3_get_pauseparam,
11280 .set_pauseparam = tg3_set_pauseparam,
11281 .get_rx_csum = tg3_get_rx_csum,
11282 .set_rx_csum = tg3_set_rx_csum,
11283 .set_tx_csum = tg3_set_tx_csum,
11284 .set_sg = ethtool_op_set_sg,
11285 .set_tso = tg3_set_tso,
11286 .self_test = tg3_self_test,
11287 .get_strings = tg3_get_strings,
11288 .phys_id = tg3_phys_id,
11289 .get_ethtool_stats = tg3_get_ethtool_stats,
11290 .get_coalesce = tg3_get_coalesce,
11291 .set_coalesce = tg3_set_coalesce,
11292 .get_sset_count = tg3_get_sset_count,
11295 static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11297 u32 cursize, val, magic;
11299 tp->nvram_size = EEPROM_CHIP_SIZE;
11301 if (tg3_nvram_read(tp, 0, &magic) != 0)
11302 return;
11304 if ((magic != TG3_EEPROM_MAGIC) &&
11305 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11306 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
11307 return;
11310 * Size the chip by reading offsets at increasing powers of two.
11311 * When we encounter our validation signature, we know the addressing
11312 * has wrapped around, and thus have our chip size.
11314 cursize = 0x10;
11316 while (cursize < tp->nvram_size) {
11317 if (tg3_nvram_read(tp, cursize, &val) != 0)
11318 return;
11320 if (val == magic)
11321 break;
11323 cursize <<= 1;
11326 tp->nvram_size = cursize;
11329 static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11331 u32 val;
11333 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11334 tg3_nvram_read(tp, 0, &val) != 0)
11335 return;
11337 /* Selfboot format */
11338 if (val != TG3_EEPROM_MAGIC) {
11339 tg3_get_eeprom_size(tp);
11340 return;
11343 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
11344 if (val != 0) {
11345 /* This is confusing. We want to operate on the
11346 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11347 * call will read from NVRAM and byteswap the data
11348 * according to the byteswapping settings for all
11349 * other register accesses. This ensures the data we
11350 * want will always reside in the lower 16-bits.
11351 * However, the data in NVRAM is in LE format, which
11352 * means the data from the NVRAM read will always be
11353 * opposite the endianness of the CPU. The 16-bit
11354 * byteswap then brings the data to CPU endianness.
11356 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
11357 return;
11360 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11363 static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11365 u32 nvcfg1;
11367 nvcfg1 = tr32(NVRAM_CFG1);
11368 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11369 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11370 } else {
11371 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11372 tw32(NVRAM_CFG1, nvcfg1);
11375 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
11376 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
11377 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
11378 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11379 tp->nvram_jedecnum = JEDEC_ATMEL;
11380 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11381 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11382 break;
11383 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11384 tp->nvram_jedecnum = JEDEC_ATMEL;
11385 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11386 break;
11387 case FLASH_VENDOR_ATMEL_EEPROM:
11388 tp->nvram_jedecnum = JEDEC_ATMEL;
11389 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11390 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11391 break;
11392 case FLASH_VENDOR_ST:
11393 tp->nvram_jedecnum = JEDEC_ST;
11394 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11395 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11396 break;
11397 case FLASH_VENDOR_SAIFUN:
11398 tp->nvram_jedecnum = JEDEC_SAIFUN;
11399 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11400 break;
11401 case FLASH_VENDOR_SST_SMALL:
11402 case FLASH_VENDOR_SST_LARGE:
11403 tp->nvram_jedecnum = JEDEC_SST;
11404 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11405 break;
11407 } else {
11408 tp->nvram_jedecnum = JEDEC_ATMEL;
11409 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11410 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11414 static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11416 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11417 case FLASH_5752PAGE_SIZE_256:
11418 tp->nvram_pagesize = 256;
11419 break;
11420 case FLASH_5752PAGE_SIZE_512:
11421 tp->nvram_pagesize = 512;
11422 break;
11423 case FLASH_5752PAGE_SIZE_1K:
11424 tp->nvram_pagesize = 1024;
11425 break;
11426 case FLASH_5752PAGE_SIZE_2K:
11427 tp->nvram_pagesize = 2048;
11428 break;
11429 case FLASH_5752PAGE_SIZE_4K:
11430 tp->nvram_pagesize = 4096;
11431 break;
11432 case FLASH_5752PAGE_SIZE_264:
11433 tp->nvram_pagesize = 264;
11434 break;
11435 case FLASH_5752PAGE_SIZE_528:
11436 tp->nvram_pagesize = 528;
11437 break;
11441 static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11443 u32 nvcfg1;
11445 nvcfg1 = tr32(NVRAM_CFG1);
11447 /* NVRAM protection for TPM */
11448 if (nvcfg1 & (1 << 27))
11449 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
11451 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11452 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11453 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11454 tp->nvram_jedecnum = JEDEC_ATMEL;
11455 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11456 break;
11457 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11458 tp->nvram_jedecnum = JEDEC_ATMEL;
11459 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11460 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11461 break;
11462 case FLASH_5752VENDOR_ST_M45PE10:
11463 case FLASH_5752VENDOR_ST_M45PE20:
11464 case FLASH_5752VENDOR_ST_M45PE40:
11465 tp->nvram_jedecnum = JEDEC_ST;
11466 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11467 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11468 break;
11471 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
11472 tg3_nvram_get_pagesize(tp, nvcfg1);
11473 } else {
11474 /* For eeprom, set pagesize to maximum eeprom size */
11475 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11477 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11478 tw32(NVRAM_CFG1, nvcfg1);
11482 static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11484 u32 nvcfg1, protect = 0;
11486 nvcfg1 = tr32(NVRAM_CFG1);
11488 /* NVRAM protection for TPM */
11489 if (nvcfg1 & (1 << 27)) {
11490 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
11491 protect = 1;
11494 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11495 switch (nvcfg1) {
11496 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11497 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11498 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11499 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11500 tp->nvram_jedecnum = JEDEC_ATMEL;
11501 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11502 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11503 tp->nvram_pagesize = 264;
11504 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11505 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11506 tp->nvram_size = (protect ? 0x3e200 :
11507 TG3_NVRAM_SIZE_512KB);
11508 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11509 tp->nvram_size = (protect ? 0x1f200 :
11510 TG3_NVRAM_SIZE_256KB);
11511 else
11512 tp->nvram_size = (protect ? 0x1f200 :
11513 TG3_NVRAM_SIZE_128KB);
11514 break;
11515 case FLASH_5752VENDOR_ST_M45PE10:
11516 case FLASH_5752VENDOR_ST_M45PE20:
11517 case FLASH_5752VENDOR_ST_M45PE40:
11518 tp->nvram_jedecnum = JEDEC_ST;
11519 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11520 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11521 tp->nvram_pagesize = 256;
11522 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11523 tp->nvram_size = (protect ?
11524 TG3_NVRAM_SIZE_64KB :
11525 TG3_NVRAM_SIZE_128KB);
11526 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11527 tp->nvram_size = (protect ?
11528 TG3_NVRAM_SIZE_64KB :
11529 TG3_NVRAM_SIZE_256KB);
11530 else
11531 tp->nvram_size = (protect ?
11532 TG3_NVRAM_SIZE_128KB :
11533 TG3_NVRAM_SIZE_512KB);
11534 break;
11538 static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11540 u32 nvcfg1;
11542 nvcfg1 = tr32(NVRAM_CFG1);
11544 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11545 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11546 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11547 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11548 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11549 tp->nvram_jedecnum = JEDEC_ATMEL;
11550 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11551 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11553 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11554 tw32(NVRAM_CFG1, nvcfg1);
11555 break;
11556 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11557 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11558 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11559 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11560 tp->nvram_jedecnum = JEDEC_ATMEL;
11561 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11562 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11563 tp->nvram_pagesize = 264;
11564 break;
11565 case FLASH_5752VENDOR_ST_M45PE10:
11566 case FLASH_5752VENDOR_ST_M45PE20:
11567 case FLASH_5752VENDOR_ST_M45PE40:
11568 tp->nvram_jedecnum = JEDEC_ST;
11569 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11570 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11571 tp->nvram_pagesize = 256;
11572 break;
11576 static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11578 u32 nvcfg1, protect = 0;
11580 nvcfg1 = tr32(NVRAM_CFG1);
11582 /* NVRAM protection for TPM */
11583 if (nvcfg1 & (1 << 27)) {
11584 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
11585 protect = 1;
11588 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11589 switch (nvcfg1) {
11590 case FLASH_5761VENDOR_ATMEL_ADB021D:
11591 case FLASH_5761VENDOR_ATMEL_ADB041D:
11592 case FLASH_5761VENDOR_ATMEL_ADB081D:
11593 case FLASH_5761VENDOR_ATMEL_ADB161D:
11594 case FLASH_5761VENDOR_ATMEL_MDB021D:
11595 case FLASH_5761VENDOR_ATMEL_MDB041D:
11596 case FLASH_5761VENDOR_ATMEL_MDB081D:
11597 case FLASH_5761VENDOR_ATMEL_MDB161D:
11598 tp->nvram_jedecnum = JEDEC_ATMEL;
11599 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11600 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11601 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11602 tp->nvram_pagesize = 256;
11603 break;
11604 case FLASH_5761VENDOR_ST_A_M45PE20:
11605 case FLASH_5761VENDOR_ST_A_M45PE40:
11606 case FLASH_5761VENDOR_ST_A_M45PE80:
11607 case FLASH_5761VENDOR_ST_A_M45PE16:
11608 case FLASH_5761VENDOR_ST_M_M45PE20:
11609 case FLASH_5761VENDOR_ST_M_M45PE40:
11610 case FLASH_5761VENDOR_ST_M_M45PE80:
11611 case FLASH_5761VENDOR_ST_M_M45PE16:
11612 tp->nvram_jedecnum = JEDEC_ST;
11613 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11614 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11615 tp->nvram_pagesize = 256;
11616 break;
11619 if (protect) {
11620 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11621 } else {
11622 switch (nvcfg1) {
11623 case FLASH_5761VENDOR_ATMEL_ADB161D:
11624 case FLASH_5761VENDOR_ATMEL_MDB161D:
11625 case FLASH_5761VENDOR_ST_A_M45PE16:
11626 case FLASH_5761VENDOR_ST_M_M45PE16:
11627 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11628 break;
11629 case FLASH_5761VENDOR_ATMEL_ADB081D:
11630 case FLASH_5761VENDOR_ATMEL_MDB081D:
11631 case FLASH_5761VENDOR_ST_A_M45PE80:
11632 case FLASH_5761VENDOR_ST_M_M45PE80:
11633 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11634 break;
11635 case FLASH_5761VENDOR_ATMEL_ADB041D:
11636 case FLASH_5761VENDOR_ATMEL_MDB041D:
11637 case FLASH_5761VENDOR_ST_A_M45PE40:
11638 case FLASH_5761VENDOR_ST_M_M45PE40:
11639 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11640 break;
11641 case FLASH_5761VENDOR_ATMEL_ADB021D:
11642 case FLASH_5761VENDOR_ATMEL_MDB021D:
11643 case FLASH_5761VENDOR_ST_A_M45PE20:
11644 case FLASH_5761VENDOR_ST_M_M45PE20:
11645 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11646 break;
11651 static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11653 tp->nvram_jedecnum = JEDEC_ATMEL;
11654 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11655 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11658 static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11660 u32 nvcfg1;
11662 nvcfg1 = tr32(NVRAM_CFG1);
11664 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11665 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11666 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11667 tp->nvram_jedecnum = JEDEC_ATMEL;
11668 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11669 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11671 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11672 tw32(NVRAM_CFG1, nvcfg1);
11673 return;
11674 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11675 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11676 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11677 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11678 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11679 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11680 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11681 tp->nvram_jedecnum = JEDEC_ATMEL;
11682 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11683 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11685 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11686 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11687 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11688 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11689 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11690 break;
11691 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11692 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11693 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11694 break;
11695 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11696 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11697 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11698 break;
11700 break;
11701 case FLASH_5752VENDOR_ST_M45PE10:
11702 case FLASH_5752VENDOR_ST_M45PE20:
11703 case FLASH_5752VENDOR_ST_M45PE40:
11704 tp->nvram_jedecnum = JEDEC_ST;
11705 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11706 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11708 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11709 case FLASH_5752VENDOR_ST_M45PE10:
11710 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11711 break;
11712 case FLASH_5752VENDOR_ST_M45PE20:
11713 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11714 break;
11715 case FLASH_5752VENDOR_ST_M45PE40:
11716 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11717 break;
11719 break;
11720 default:
11721 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11722 return;
11725 tg3_nvram_get_pagesize(tp, nvcfg1);
11726 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11727 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11731 static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11733 u32 nvcfg1;
11735 nvcfg1 = tr32(NVRAM_CFG1);
11737 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11738 case FLASH_5717VENDOR_ATMEL_EEPROM:
11739 case FLASH_5717VENDOR_MICRO_EEPROM:
11740 tp->nvram_jedecnum = JEDEC_ATMEL;
11741 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11742 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11744 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11745 tw32(NVRAM_CFG1, nvcfg1);
11746 return;
11747 case FLASH_5717VENDOR_ATMEL_MDB011D:
11748 case FLASH_5717VENDOR_ATMEL_ADB011B:
11749 case FLASH_5717VENDOR_ATMEL_ADB011D:
11750 case FLASH_5717VENDOR_ATMEL_MDB021D:
11751 case FLASH_5717VENDOR_ATMEL_ADB021B:
11752 case FLASH_5717VENDOR_ATMEL_ADB021D:
11753 case FLASH_5717VENDOR_ATMEL_45USPT:
11754 tp->nvram_jedecnum = JEDEC_ATMEL;
11755 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11756 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11758 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11759 case FLASH_5717VENDOR_ATMEL_MDB021D:
11760 case FLASH_5717VENDOR_ATMEL_ADB021B:
11761 case FLASH_5717VENDOR_ATMEL_ADB021D:
11762 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11763 break;
11764 default:
11765 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11766 break;
11768 break;
11769 case FLASH_5717VENDOR_ST_M_M25PE10:
11770 case FLASH_5717VENDOR_ST_A_M25PE10:
11771 case FLASH_5717VENDOR_ST_M_M45PE10:
11772 case FLASH_5717VENDOR_ST_A_M45PE10:
11773 case FLASH_5717VENDOR_ST_M_M25PE20:
11774 case FLASH_5717VENDOR_ST_A_M25PE20:
11775 case FLASH_5717VENDOR_ST_M_M45PE20:
11776 case FLASH_5717VENDOR_ST_A_M45PE20:
11777 case FLASH_5717VENDOR_ST_25USPT:
11778 case FLASH_5717VENDOR_ST_45USPT:
11779 tp->nvram_jedecnum = JEDEC_ST;
11780 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11781 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11783 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11784 case FLASH_5717VENDOR_ST_M_M25PE20:
11785 case FLASH_5717VENDOR_ST_A_M25PE20:
11786 case FLASH_5717VENDOR_ST_M_M45PE20:
11787 case FLASH_5717VENDOR_ST_A_M45PE20:
11788 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11789 break;
11790 default:
11791 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11792 break;
11794 break;
11795 default:
11796 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11797 return;
11800 tg3_nvram_get_pagesize(tp, nvcfg1);
11801 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11802 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11805 /* Chips other than 5700/5701 use the NVRAM for fetching info. */
11806 static void __devinit tg3_nvram_init(struct tg3 *tp)
11808 tw32_f(GRC_EEPROM_ADDR,
11809 (EEPROM_ADDR_FSM_RESET |
11810 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11811 EEPROM_ADDR_CLKPERD_SHIFT)));
11813 msleep(1);
11815 /* Enable seeprom accesses. */
11816 tw32_f(GRC_LOCAL_CTRL,
11817 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11818 udelay(100);
11820 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11821 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11822 tp->tg3_flags |= TG3_FLAG_NVRAM;
11824 if (tg3_nvram_lock(tp)) {
11825 netdev_warn(tp->dev,
11826 "Cannot get nvram lock, %s failed\n",
11827 __func__);
11828 return;
11830 tg3_enable_nvram_access(tp);
11832 tp->nvram_size = 0;
11834 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11835 tg3_get_5752_nvram_info(tp);
11836 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11837 tg3_get_5755_nvram_info(tp);
11838 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
11839 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11840 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11841 tg3_get_5787_nvram_info(tp);
11842 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11843 tg3_get_5761_nvram_info(tp);
11844 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11845 tg3_get_5906_nvram_info(tp);
11846 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11847 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11848 tg3_get_57780_nvram_info(tp);
11849 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
11850 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
11851 tg3_get_5717_nvram_info(tp);
11852 else
11853 tg3_get_nvram_info(tp);
11855 if (tp->nvram_size == 0)
11856 tg3_get_nvram_size(tp);
11858 tg3_disable_nvram_access(tp);
11859 tg3_nvram_unlock(tp);
11861 } else {
11862 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11864 tg3_get_eeprom_size(tp);
11868 static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11869 u32 offset, u32 len, u8 *buf)
11871 int i, j, rc = 0;
11872 u32 val;
11874 for (i = 0; i < len; i += 4) {
11875 u32 addr;
11876 __be32 data;
11878 addr = offset + i;
11880 memcpy(&data, buf + i, 4);
11883 * The SEEPROM interface expects the data to always be opposite
11884 * the native endian format. We accomplish this by reversing
11885 * all the operations that would have been performed on the
11886 * data from a call to tg3_nvram_read_be32().
11888 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
11890 val = tr32(GRC_EEPROM_ADDR);
11891 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11893 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11894 EEPROM_ADDR_READ);
11895 tw32(GRC_EEPROM_ADDR, val |
11896 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11897 (addr & EEPROM_ADDR_ADDR_MASK) |
11898 EEPROM_ADDR_START |
11899 EEPROM_ADDR_WRITE);
11901 for (j = 0; j < 1000; j++) {
11902 val = tr32(GRC_EEPROM_ADDR);
11904 if (val & EEPROM_ADDR_COMPLETE)
11905 break;
11906 msleep(1);
11908 if (!(val & EEPROM_ADDR_COMPLETE)) {
11909 rc = -EBUSY;
11910 break;
11914 return rc;
11917 /* offset and length are dword aligned */
11918 static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11919 u8 *buf)
11921 int ret = 0;
11922 u32 pagesize = tp->nvram_pagesize;
11923 u32 pagemask = pagesize - 1;
11924 u32 nvram_cmd;
11925 u8 *tmp;
11927 tmp = kmalloc(pagesize, GFP_KERNEL);
11928 if (tmp == NULL)
11929 return -ENOMEM;
11931 while (len) {
11932 int j;
11933 u32 phy_addr, page_off, size;
11935 phy_addr = offset & ~pagemask;
11937 for (j = 0; j < pagesize; j += 4) {
11938 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11939 (__be32 *) (tmp + j));
11940 if (ret)
11941 break;
11943 if (ret)
11944 break;
11946 page_off = offset & pagemask;
11947 size = pagesize;
11948 if (len < size)
11949 size = len;
11951 len -= size;
11953 memcpy(tmp + page_off, buf, size);
11955 offset = offset + (pagesize - page_off);
11957 tg3_enable_nvram_access(tp);
11960 * Before we can erase the flash page, we need
11961 * to issue a special "write enable" command.
11963 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11965 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11966 break;
11968 /* Erase the target page */
11969 tw32(NVRAM_ADDR, phy_addr);
11971 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11972 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11974 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11975 break;
11977 /* Issue another write enable to start the write. */
11978 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11980 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11981 break;
11983 for (j = 0; j < pagesize; j += 4) {
11984 __be32 data;
11986 data = *((__be32 *) (tmp + j));
11988 tw32(NVRAM_WRDATA, be32_to_cpu(data));
11990 tw32(NVRAM_ADDR, phy_addr + j);
11992 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11993 NVRAM_CMD_WR;
11995 if (j == 0)
11996 nvram_cmd |= NVRAM_CMD_FIRST;
11997 else if (j == (pagesize - 4))
11998 nvram_cmd |= NVRAM_CMD_LAST;
12000 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12001 break;
12003 if (ret)
12004 break;
12007 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12008 tg3_nvram_exec_cmd(tp, nvram_cmd);
12010 kfree(tmp);
12012 return ret;
12015 /* offset and length are dword aligned */
12016 static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12017 u8 *buf)
12019 int i, ret = 0;
12021 for (i = 0; i < len; i += 4, offset += 4) {
12022 u32 page_off, phy_addr, nvram_cmd;
12023 __be32 data;
12025 memcpy(&data, buf + i, 4);
12026 tw32(NVRAM_WRDATA, be32_to_cpu(data));
12028 page_off = offset % tp->nvram_pagesize;
12030 phy_addr = tg3_nvram_phys_addr(tp, offset);
12032 tw32(NVRAM_ADDR, phy_addr);
12034 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12036 if (page_off == 0 || i == 0)
12037 nvram_cmd |= NVRAM_CMD_FIRST;
12038 if (page_off == (tp->nvram_pagesize - 4))
12039 nvram_cmd |= NVRAM_CMD_LAST;
12041 if (i == (len - 4))
12042 nvram_cmd |= NVRAM_CMD_LAST;
12044 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
12045 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
12046 (tp->nvram_jedecnum == JEDEC_ST) &&
12047 (nvram_cmd & NVRAM_CMD_FIRST)) {
12049 if ((ret = tg3_nvram_exec_cmd(tp,
12050 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12051 NVRAM_CMD_DONE)))
12053 break;
12055 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12056 /* We always do complete word writes to eeprom. */
12057 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12060 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12061 break;
12063 return ret;
12066 /* offset and length are dword aligned */
12067 static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12069 int ret;
12071 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
12072 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12073 ~GRC_LCLCTRL_GPIO_OUTPUT1);
12074 udelay(40);
12077 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
12078 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
12079 } else {
12080 u32 grc_mode;
12082 ret = tg3_nvram_lock(tp);
12083 if (ret)
12084 return ret;
12086 tg3_enable_nvram_access(tp);
12087 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
12088 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
12089 tw32(NVRAM_WRITE1, 0x406);
12091 grc_mode = tr32(GRC_MODE);
12092 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12094 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12095 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12097 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12098 buf);
12099 } else {
12100 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12101 buf);
12104 grc_mode = tr32(GRC_MODE);
12105 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12107 tg3_disable_nvram_access(tp);
12108 tg3_nvram_unlock(tp);
12111 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
12112 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
12113 udelay(40);
12116 return ret;
12119 struct subsys_tbl_ent {
12120 u16 subsys_vendor, subsys_devid;
12121 u32 phy_id;
12124 static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
12125 /* Broadcom boards. */
12126 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12127 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
12128 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12129 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
12130 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12131 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
12132 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12133 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12134 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12135 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
12136 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12137 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
12138 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12139 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12140 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12141 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
12142 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12143 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
12144 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12145 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
12146 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12147 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
12149 /* 3com boards. */
12150 { TG3PCI_SUBVENDOR_ID_3COM,
12151 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
12152 { TG3PCI_SUBVENDOR_ID_3COM,
12153 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
12154 { TG3PCI_SUBVENDOR_ID_3COM,
12155 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12156 { TG3PCI_SUBVENDOR_ID_3COM,
12157 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
12158 { TG3PCI_SUBVENDOR_ID_3COM,
12159 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
12161 /* DELL boards. */
12162 { TG3PCI_SUBVENDOR_ID_DELL,
12163 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
12164 { TG3PCI_SUBVENDOR_ID_DELL,
12165 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
12166 { TG3PCI_SUBVENDOR_ID_DELL,
12167 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
12168 { TG3PCI_SUBVENDOR_ID_DELL,
12169 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
12171 /* Compaq boards. */
12172 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12173 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
12174 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12175 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
12176 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12177 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12178 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12179 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
12180 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12181 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
12183 /* IBM boards. */
12184 { TG3PCI_SUBVENDOR_ID_IBM,
12185 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
12188 static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
12190 int i;
12192 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12193 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12194 tp->pdev->subsystem_vendor) &&
12195 (subsys_id_to_phy_id[i].subsys_devid ==
12196 tp->pdev->subsystem_device))
12197 return &subsys_id_to_phy_id[i];
12199 return NULL;
12202 static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
12204 u32 val;
12205 u16 pmcsr;
12207 /* On some early chips the SRAM cannot be accessed in D3hot state,
12208 * so need make sure we're in D0.
12210 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12211 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12212 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12213 msleep(1);
12215 /* Make sure register accesses (indirect or otherwise)
12216 * will function correctly.
12218 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12219 tp->misc_host_ctrl);
12221 /* The memory arbiter has to be enabled in order for SRAM accesses
12222 * to succeed. Normally on powerup the tg3 chip firmware will make
12223 * sure it is enabled, but other entities such as system netboot
12224 * code might disable it.
12226 val = tr32(MEMARB_MODE);
12227 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12229 tp->phy_id = TG3_PHY_ID_INVALID;
12230 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12232 /* Assume an onboard device and WOL capable by default. */
12233 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
12235 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
12236 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
12237 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12238 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12240 val = tr32(VCPU_CFGSHDW);
12241 if (val & VCPU_CFGSHDW_ASPM_DBNC)
12242 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12243 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
12244 (val & VCPU_CFGSHDW_WOL_MAGPKT))
12245 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12246 goto done;
12249 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12250 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12251 u32 nic_cfg, led_cfg;
12252 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
12253 int eeprom_phy_serdes = 0;
12255 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12256 tp->nic_sram_data_cfg = nic_cfg;
12258 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12259 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12260 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12261 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12262 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12263 (ver > 0) && (ver < 0x100))
12264 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12266 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12267 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12269 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12270 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12271 eeprom_phy_serdes = 1;
12273 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12274 if (nic_phy_id != 0) {
12275 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12276 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12278 eeprom_phy_id = (id1 >> 16) << 10;
12279 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12280 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12281 } else
12282 eeprom_phy_id = 0;
12284 tp->phy_id = eeprom_phy_id;
12285 if (eeprom_phy_serdes) {
12286 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12287 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
12288 else
12289 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
12292 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
12293 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12294 SHASTA_EXT_LED_MODE_MASK);
12295 else
12296 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12298 switch (led_cfg) {
12299 default:
12300 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12301 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12302 break;
12304 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12305 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12306 break;
12308 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12309 tp->led_ctrl = LED_CTRL_MODE_MAC;
12311 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12312 * read on some older 5700/5701 bootcode.
12314 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12315 ASIC_REV_5700 ||
12316 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12317 ASIC_REV_5701)
12318 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12320 break;
12322 case SHASTA_EXT_LED_SHARED:
12323 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12324 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12325 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12326 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12327 LED_CTRL_MODE_PHY_2);
12328 break;
12330 case SHASTA_EXT_LED_MAC:
12331 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12332 break;
12334 case SHASTA_EXT_LED_COMBO:
12335 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12336 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12337 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12338 LED_CTRL_MODE_PHY_2);
12339 break;
12343 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12344 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12345 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12346 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12348 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12349 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12351 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
12352 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
12353 if ((tp->pdev->subsystem_vendor ==
12354 PCI_VENDOR_ID_ARIMA) &&
12355 (tp->pdev->subsystem_device == 0x205a ||
12356 tp->pdev->subsystem_device == 0x2063))
12357 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12358 } else {
12359 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12360 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12363 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12364 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
12365 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
12366 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12369 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12370 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
12371 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
12373 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
12374 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12375 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
12377 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
12378 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
12379 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12381 if (cfg2 & (1 << 17))
12382 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
12384 /* serdes signal pre-emphasis in register 0x590 set by */
12385 /* bootcode if bit 18 is set */
12386 if (cfg2 & (1 << 18))
12387 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
12389 if (((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) ||
12390 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12391 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX))) &&
12392 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12393 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
12395 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12396 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
12397 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
12398 u32 cfg3;
12400 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12401 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12402 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12405 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12406 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
12407 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12408 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12409 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12410 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
12412 done:
12413 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12414 device_set_wakeup_enable(&tp->pdev->dev,
12415 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
12418 static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12420 int i;
12421 u32 val;
12423 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12424 tw32(OTP_CTRL, cmd);
12426 /* Wait for up to 1 ms for command to execute. */
12427 for (i = 0; i < 100; i++) {
12428 val = tr32(OTP_STATUS);
12429 if (val & OTP_STATUS_CMD_DONE)
12430 break;
12431 udelay(10);
12434 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12437 /* Read the gphy configuration from the OTP region of the chip. The gphy
12438 * configuration is a 32-bit value that straddles the alignment boundary.
12439 * We do two 32-bit reads and then shift and merge the results.
12441 static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12443 u32 bhalf_otp, thalf_otp;
12445 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12447 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12448 return 0;
12450 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12452 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12453 return 0;
12455 thalf_otp = tr32(OTP_READ_DATA);
12457 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12459 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12460 return 0;
12462 bhalf_otp = tr32(OTP_READ_DATA);
12464 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12467 static int __devinit tg3_phy_probe(struct tg3 *tp)
12469 u32 hw_phy_id_1, hw_phy_id_2;
12470 u32 hw_phy_id, hw_phy_id_masked;
12471 int err;
12473 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12474 return tg3_phy_init(tp);
12476 /* Reading the PHY ID register can conflict with ASF
12477 * firmware access to the PHY hardware.
12479 err = 0;
12480 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12481 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
12482 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
12483 } else {
12484 /* Now read the physical PHY_ID from the chip and verify
12485 * that it is sane. If it doesn't look good, we fall back
12486 * to either the hard-coded table based PHY_ID and failing
12487 * that the value found in the eeprom area.
12489 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12490 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12492 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12493 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12494 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12496 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
12499 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
12500 tp->phy_id = hw_phy_id;
12501 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
12502 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
12503 else
12504 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
12505 } else {
12506 if (tp->phy_id != TG3_PHY_ID_INVALID) {
12507 /* Do nothing, phy ID already set up in
12508 * tg3_get_eeprom_hw_cfg().
12510 } else {
12511 struct subsys_tbl_ent *p;
12513 /* No eeprom signature? Try the hardcoded
12514 * subsys device table.
12516 p = tg3_lookup_by_subsys(tp);
12517 if (!p)
12518 return -ENODEV;
12520 tp->phy_id = p->phy_id;
12521 if (!tp->phy_id ||
12522 tp->phy_id == TG3_PHY_ID_BCM8002)
12523 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
12527 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12528 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12529 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12530 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12531 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
12532 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12534 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12535 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
12536 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
12537 u32 bmsr, adv_reg, tg3_ctrl, mask;
12539 tg3_readphy(tp, MII_BMSR, &bmsr);
12540 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12541 (bmsr & BMSR_LSTATUS))
12542 goto skip_phy_reset;
12544 err = tg3_phy_reset(tp);
12545 if (err)
12546 return err;
12548 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12549 ADVERTISE_100HALF | ADVERTISE_100FULL |
12550 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12551 tg3_ctrl = 0;
12552 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
12553 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12554 MII_TG3_CTRL_ADV_1000_FULL);
12555 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12556 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12557 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12558 MII_TG3_CTRL_ENABLE_AS_MASTER);
12561 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12562 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12563 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12564 if (!tg3_copper_is_advertising_all(tp, mask)) {
12565 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12567 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12568 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12570 tg3_writephy(tp, MII_BMCR,
12571 BMCR_ANENABLE | BMCR_ANRESTART);
12573 tg3_phy_set_wirespeed(tp);
12575 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12576 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12577 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12580 skip_phy_reset:
12581 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
12582 err = tg3_init_5401phy_dsp(tp);
12583 if (err)
12584 return err;
12586 err = tg3_init_5401phy_dsp(tp);
12589 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
12590 tp->link_config.advertising =
12591 (ADVERTISED_1000baseT_Half |
12592 ADVERTISED_1000baseT_Full |
12593 ADVERTISED_Autoneg |
12594 ADVERTISED_FIBRE);
12595 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
12596 tp->link_config.advertising &=
12597 ~(ADVERTISED_1000baseT_Half |
12598 ADVERTISED_1000baseT_Full);
12600 return err;
12603 static void __devinit tg3_read_vpd(struct tg3 *tp)
12605 u8 *vpd_data;
12606 unsigned int block_end, rosize, len;
12607 int j, i = 0;
12608 u32 magic;
12610 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12611 tg3_nvram_read(tp, 0x0, &magic))
12612 goto out_no_vpd;
12614 vpd_data = kmalloc(TG3_NVM_VPD_LEN, GFP_KERNEL);
12615 if (!vpd_data)
12616 goto out_no_vpd;
12618 if (magic == TG3_EEPROM_MAGIC) {
12619 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
12620 u32 tmp;
12622 /* The data is in little-endian format in NVRAM.
12623 * Use the big-endian read routines to preserve
12624 * the byte order as it exists in NVRAM.
12626 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
12627 goto out_not_found;
12629 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
12631 } else {
12632 ssize_t cnt;
12633 unsigned int pos = 0;
12635 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12636 cnt = pci_read_vpd(tp->pdev, pos,
12637 TG3_NVM_VPD_LEN - pos,
12638 &vpd_data[pos]);
12639 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12640 cnt = 0;
12641 else if (cnt < 0)
12642 goto out_not_found;
12644 if (pos != TG3_NVM_VPD_LEN)
12645 goto out_not_found;
12648 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12649 PCI_VPD_LRDT_RO_DATA);
12650 if (i < 0)
12651 goto out_not_found;
12653 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12654 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12655 i += PCI_VPD_LRDT_TAG_SIZE;
12657 if (block_end > TG3_NVM_VPD_LEN)
12658 goto out_not_found;
12660 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12661 PCI_VPD_RO_KEYWORD_MFR_ID);
12662 if (j > 0) {
12663 len = pci_vpd_info_field_size(&vpd_data[j]);
12665 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12666 if (j + len > block_end || len != 4 ||
12667 memcmp(&vpd_data[j], "1028", 4))
12668 goto partno;
12670 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12671 PCI_VPD_RO_KEYWORD_VENDOR0);
12672 if (j < 0)
12673 goto partno;
12675 len = pci_vpd_info_field_size(&vpd_data[j]);
12677 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12678 if (j + len > block_end)
12679 goto partno;
12681 memcpy(tp->fw_ver, &vpd_data[j], len);
12682 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12685 partno:
12686 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12687 PCI_VPD_RO_KEYWORD_PARTNO);
12688 if (i < 0)
12689 goto out_not_found;
12691 len = pci_vpd_info_field_size(&vpd_data[i]);
12693 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12694 if (len > TG3_BPN_SIZE ||
12695 (len + i) > TG3_NVM_VPD_LEN)
12696 goto out_not_found;
12698 memcpy(tp->board_part_number, &vpd_data[i], len);
12700 out_not_found:
12701 kfree(vpd_data);
12702 if (tp->board_part_number[0])
12703 return;
12705 out_no_vpd:
12706 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12707 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
12708 strcpy(tp->board_part_number, "BCM5717");
12709 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
12710 strcpy(tp->board_part_number, "BCM5718");
12711 else
12712 goto nomatch;
12713 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
12714 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12715 strcpy(tp->board_part_number, "BCM57780");
12716 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12717 strcpy(tp->board_part_number, "BCM57760");
12718 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12719 strcpy(tp->board_part_number, "BCM57790");
12720 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12721 strcpy(tp->board_part_number, "BCM57788");
12722 else
12723 goto nomatch;
12724 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
12725 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12726 strcpy(tp->board_part_number, "BCM57761");
12727 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
12728 strcpy(tp->board_part_number, "BCM57765");
12729 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12730 strcpy(tp->board_part_number, "BCM57781");
12731 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12732 strcpy(tp->board_part_number, "BCM57785");
12733 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12734 strcpy(tp->board_part_number, "BCM57791");
12735 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12736 strcpy(tp->board_part_number, "BCM57795");
12737 else
12738 goto nomatch;
12739 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
12740 strcpy(tp->board_part_number, "BCM95906");
12741 } else {
12742 nomatch:
12743 strcpy(tp->board_part_number, "none");
12747 static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12749 u32 val;
12751 if (tg3_nvram_read(tp, offset, &val) ||
12752 (val & 0xfc000000) != 0x0c000000 ||
12753 tg3_nvram_read(tp, offset + 4, &val) ||
12754 val != 0)
12755 return 0;
12757 return 1;
12760 static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12762 u32 val, offset, start, ver_offset;
12763 int i, dst_off;
12764 bool newver = false;
12766 if (tg3_nvram_read(tp, 0xc, &offset) ||
12767 tg3_nvram_read(tp, 0x4, &start))
12768 return;
12770 offset = tg3_nvram_logical_addr(tp, offset);
12772 if (tg3_nvram_read(tp, offset, &val))
12773 return;
12775 if ((val & 0xfc000000) == 0x0c000000) {
12776 if (tg3_nvram_read(tp, offset + 4, &val))
12777 return;
12779 if (val == 0)
12780 newver = true;
12783 dst_off = strlen(tp->fw_ver);
12785 if (newver) {
12786 if (TG3_VER_SIZE - dst_off < 16 ||
12787 tg3_nvram_read(tp, offset + 8, &ver_offset))
12788 return;
12790 offset = offset + ver_offset - start;
12791 for (i = 0; i < 16; i += 4) {
12792 __be32 v;
12793 if (tg3_nvram_read_be32(tp, offset + i, &v))
12794 return;
12796 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
12798 } else {
12799 u32 major, minor;
12801 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12802 return;
12804 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12805 TG3_NVM_BCVER_MAJSFT;
12806 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
12807 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12808 "v%d.%02d", major, minor);
12812 static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12814 u32 val, major, minor;
12816 /* Use native endian representation */
12817 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12818 return;
12820 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12821 TG3_NVM_HWSB_CFG1_MAJSFT;
12822 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12823 TG3_NVM_HWSB_CFG1_MINSFT;
12825 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12828 static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12830 u32 offset, major, minor, build;
12832 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
12834 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12835 return;
12837 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12838 case TG3_EEPROM_SB_REVISION_0:
12839 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12840 break;
12841 case TG3_EEPROM_SB_REVISION_2:
12842 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12843 break;
12844 case TG3_EEPROM_SB_REVISION_3:
12845 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12846 break;
12847 case TG3_EEPROM_SB_REVISION_4:
12848 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12849 break;
12850 case TG3_EEPROM_SB_REVISION_5:
12851 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12852 break;
12853 case TG3_EEPROM_SB_REVISION_6:
12854 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
12855 break;
12856 default:
12857 return;
12860 if (tg3_nvram_read(tp, offset, &val))
12861 return;
12863 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12864 TG3_EEPROM_SB_EDH_BLD_SHFT;
12865 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12866 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12867 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12869 if (minor > 99 || build > 26)
12870 return;
12872 offset = strlen(tp->fw_ver);
12873 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12874 " v%d.%02d", major, minor);
12876 if (build > 0) {
12877 offset = strlen(tp->fw_ver);
12878 if (offset < TG3_VER_SIZE - 1)
12879 tp->fw_ver[offset] = 'a' + build - 1;
12883 static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
12885 u32 val, offset, start;
12886 int i, vlen;
12888 for (offset = TG3_NVM_DIR_START;
12889 offset < TG3_NVM_DIR_END;
12890 offset += TG3_NVM_DIRENT_SIZE) {
12891 if (tg3_nvram_read(tp, offset, &val))
12892 return;
12894 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12895 break;
12898 if (offset == TG3_NVM_DIR_END)
12899 return;
12901 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12902 start = 0x08000000;
12903 else if (tg3_nvram_read(tp, offset - 4, &start))
12904 return;
12906 if (tg3_nvram_read(tp, offset + 4, &offset) ||
12907 !tg3_fw_img_is_valid(tp, offset) ||
12908 tg3_nvram_read(tp, offset + 8, &val))
12909 return;
12911 offset += val - start;
12913 vlen = strlen(tp->fw_ver);
12915 tp->fw_ver[vlen++] = ',';
12916 tp->fw_ver[vlen++] = ' ';
12918 for (i = 0; i < 4; i++) {
12919 __be32 v;
12920 if (tg3_nvram_read_be32(tp, offset, &v))
12921 return;
12923 offset += sizeof(v);
12925 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12926 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
12927 break;
12930 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12931 vlen += sizeof(v);
12935 static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12937 int vlen;
12938 u32 apedata;
12939 char *fwtype;
12941 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12942 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12943 return;
12945 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12946 if (apedata != APE_SEG_SIG_MAGIC)
12947 return;
12949 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12950 if (!(apedata & APE_FW_STATUS_READY))
12951 return;
12953 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12955 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
12956 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
12957 fwtype = "NCSI";
12958 } else {
12959 fwtype = "DASH";
12962 vlen = strlen(tp->fw_ver);
12964 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
12965 fwtype,
12966 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12967 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12968 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12969 (apedata & APE_FW_VERSION_BLDMSK));
12972 static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12974 u32 val;
12975 bool vpd_vers = false;
12977 if (tp->fw_ver[0] != 0)
12978 vpd_vers = true;
12980 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
12981 strcat(tp->fw_ver, "sb");
12982 return;
12985 if (tg3_nvram_read(tp, 0, &val))
12986 return;
12988 if (val == TG3_EEPROM_MAGIC)
12989 tg3_read_bc_ver(tp);
12990 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12991 tg3_read_sb_ver(tp, val);
12992 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12993 tg3_read_hwsb_ver(tp);
12994 else
12995 return;
12997 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12998 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
12999 goto done;
13001 tg3_read_mgmtfw_ver(tp);
13003 done:
13004 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
13007 static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13009 static void inline vlan_features_add(struct net_device *dev, unsigned long flags)
13011 dev->vlan_features |= flags;
13014 static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13016 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
13018 return 4096;
13019 else if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
13020 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
13021 return 1024;
13022 else
13023 return 512;
13026 DEFINE_PCI_DEVICE_TABLE(write_reorder_chipsets) = {
13027 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13028 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13029 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13030 { },
13033 static int __devinit tg3_get_invariants(struct tg3 *tp)
13035 u32 misc_ctrl_reg;
13036 u32 pci_state_reg, grc_misc_cfg;
13037 u32 val;
13038 u16 pci_cmd;
13039 int err;
13041 /* Force memory write invalidate off. If we leave it on,
13042 * then on 5700_BX chips we have to enable a workaround.
13043 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13044 * to match the cacheline size. The Broadcom driver have this
13045 * workaround but turns MWI off all the times so never uses
13046 * it. This seems to suggest that the workaround is insufficient.
13048 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13049 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13050 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13052 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13053 * has the register indirect write enable bit set before
13054 * we try to access any of the MMIO registers. It is also
13055 * critical that the PCI-X hw workaround situation is decided
13056 * before that as well.
13058 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13059 &misc_ctrl_reg);
13061 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13062 MISC_HOST_CTRL_CHIPREV_SHIFT);
13063 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13064 u32 prod_id_asic_rev;
13066 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13067 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
13068 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719)
13069 pci_read_config_dword(tp->pdev,
13070 TG3PCI_GEN2_PRODID_ASICREV,
13071 &prod_id_asic_rev);
13072 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13073 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13074 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13075 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13076 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13077 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13078 pci_read_config_dword(tp->pdev,
13079 TG3PCI_GEN15_PRODID_ASICREV,
13080 &prod_id_asic_rev);
13081 else
13082 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13083 &prod_id_asic_rev);
13085 tp->pci_chip_rev_id = prod_id_asic_rev;
13088 /* Wrong chip ID in 5752 A0. This code can be removed later
13089 * as A0 is not in production.
13091 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13092 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13094 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13095 * we need to disable memory and use config. cycles
13096 * only to access all registers. The 5702/03 chips
13097 * can mistakenly decode the special cycles from the
13098 * ICH chipsets as memory write cycles, causing corruption
13099 * of register and memory space. Only certain ICH bridges
13100 * will drive special cycles with non-zero data during the
13101 * address phase which can fall within the 5703's address
13102 * range. This is not an ICH bug as the PCI spec allows
13103 * non-zero address during special cycles. However, only
13104 * these ICH bridges are known to drive non-zero addresses
13105 * during special cycles.
13107 * Since special cycles do not cross PCI bridges, we only
13108 * enable this workaround if the 5703 is on the secondary
13109 * bus of these ICH bridges.
13111 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13112 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13113 static struct tg3_dev_id {
13114 u32 vendor;
13115 u32 device;
13116 u32 rev;
13117 } ich_chipsets[] = {
13118 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13119 PCI_ANY_ID },
13120 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13121 PCI_ANY_ID },
13122 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13123 0xa },
13124 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13125 PCI_ANY_ID },
13126 { },
13128 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13129 struct pci_dev *bridge = NULL;
13131 while (pci_id->vendor != 0) {
13132 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13133 bridge);
13134 if (!bridge) {
13135 pci_id++;
13136 continue;
13138 if (pci_id->rev != PCI_ANY_ID) {
13139 if (bridge->revision > pci_id->rev)
13140 continue;
13142 if (bridge->subordinate &&
13143 (bridge->subordinate->number ==
13144 tp->pdev->bus->number)) {
13146 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
13147 pci_dev_put(bridge);
13148 break;
13153 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13154 static struct tg3_dev_id {
13155 u32 vendor;
13156 u32 device;
13157 } bridge_chipsets[] = {
13158 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13159 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13160 { },
13162 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13163 struct pci_dev *bridge = NULL;
13165 while (pci_id->vendor != 0) {
13166 bridge = pci_get_device(pci_id->vendor,
13167 pci_id->device,
13168 bridge);
13169 if (!bridge) {
13170 pci_id++;
13171 continue;
13173 if (bridge->subordinate &&
13174 (bridge->subordinate->number <=
13175 tp->pdev->bus->number) &&
13176 (bridge->subordinate->subordinate >=
13177 tp->pdev->bus->number)) {
13178 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
13179 pci_dev_put(bridge);
13180 break;
13185 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13186 * DMA addresses > 40-bit. This bridge may have other additional
13187 * 57xx devices behind it in some 4-port NIC designs for example.
13188 * Any tg3 device found behind the bridge will also need the 40-bit
13189 * DMA workaround.
13191 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13192 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13193 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
13194 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13195 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
13196 } else {
13197 struct pci_dev *bridge = NULL;
13199 do {
13200 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13201 PCI_DEVICE_ID_SERVERWORKS_EPB,
13202 bridge);
13203 if (bridge && bridge->subordinate &&
13204 (bridge->subordinate->number <=
13205 tp->pdev->bus->number) &&
13206 (bridge->subordinate->subordinate >=
13207 tp->pdev->bus->number)) {
13208 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13209 pci_dev_put(bridge);
13210 break;
13212 } while (bridge);
13215 /* Initialize misc host control in PCI block. */
13216 tp->misc_host_ctrl |= (misc_ctrl_reg &
13217 MISC_HOST_CTRL_CHIPREV);
13218 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13219 tp->misc_host_ctrl);
13221 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13222 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
13223 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
13224 tp->pdev_peer = tg3_find_peer(tp);
13226 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13228 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13229 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13231 /* Intentionally exclude ASIC_REV_5906 */
13232 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13233 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
13234 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13235 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13236 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13237 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13238 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
13239 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13241 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13242 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13243 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
13244 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13245 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
13246 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13248 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13249 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13250 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13252 /* 5700 B0 chips do not support checksumming correctly due
13253 * to hardware bugs.
13255 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13256 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13257 else {
13258 unsigned long features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_GRO;
13260 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
13261 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
13262 features |= NETIF_F_IPV6_CSUM;
13263 tp->dev->features |= features;
13264 vlan_features_add(tp->dev, features);
13267 /* Determine TSO capabilities */
13268 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
13269 ; /* Do nothing. HW bug. */
13270 else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
13271 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13272 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13273 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13274 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13275 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13276 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13277 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13278 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13279 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13280 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13281 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13282 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13283 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13284 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13285 tp->fw_needed = FIRMWARE_TG3TSO5;
13286 else
13287 tp->fw_needed = FIRMWARE_TG3TSO;
13290 tp->irq_max = 1;
13292 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13293 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13294 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13295 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13296 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13297 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13298 tp->pdev_peer == tp->pdev))
13299 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13301 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13302 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13303 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
13306 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
13307 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13308 tp->irq_max = TG3_IRQ_MAX_VECS;
13312 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13313 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13314 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13315 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13316 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13317 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13318 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
13321 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
13322 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
13323 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13325 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13326 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13327 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
13328 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
13330 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13331 &pci_state_reg);
13333 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13334 if (tp->pcie_cap != 0) {
13335 u16 lnkctl;
13337 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
13339 tp->pcie_readrq = 4096;
13340 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
13341 tp->pcie_readrq = 2048;
13343 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
13345 pci_read_config_word(tp->pdev,
13346 tp->pcie_cap + PCI_EXP_LNKCTL,
13347 &lnkctl);
13348 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13349 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13350 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
13351 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13352 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13353 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13354 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
13355 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
13356 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13357 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
13359 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
13360 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
13361 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13362 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13363 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13364 if (!tp->pcix_cap) {
13365 dev_err(&tp->pdev->dev,
13366 "Cannot find PCI-X capability, aborting\n");
13367 return -EIO;
13370 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13371 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13374 /* If we have an AMD 762 or VIA K8T800 chipset, write
13375 * reordering to the mailbox registers done by the host
13376 * controller can cause major troubles. We read back from
13377 * every mailbox register write to force the writes to be
13378 * posted to the chip in order.
13380 if (pci_dev_present(write_reorder_chipsets) &&
13381 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13382 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13384 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13385 &tp->pci_cacheline_sz);
13386 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13387 &tp->pci_lat_timer);
13388 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13389 tp->pci_lat_timer < 64) {
13390 tp->pci_lat_timer = 64;
13391 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13392 tp->pci_lat_timer);
13395 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13396 /* 5700 BX chips need to have their TX producer index
13397 * mailboxes written twice to workaround a bug.
13399 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
13401 /* If we are in PCI-X mode, enable register write workaround.
13403 * The workaround is to use indirect register accesses
13404 * for all chip writes not to mailbox registers.
13406 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
13407 u32 pm_reg;
13409 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13411 /* The chip can have it's power management PCI config
13412 * space registers clobbered due to this bug.
13413 * So explicitly force the chip into D0 here.
13415 pci_read_config_dword(tp->pdev,
13416 tp->pm_cap + PCI_PM_CTRL,
13417 &pm_reg);
13418 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13419 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
13420 pci_write_config_dword(tp->pdev,
13421 tp->pm_cap + PCI_PM_CTRL,
13422 pm_reg);
13424 /* Also, force SERR#/PERR# in PCI command. */
13425 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13426 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13427 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13431 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13432 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13433 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13434 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13436 /* Chip-specific fixup from Broadcom driver */
13437 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13438 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13439 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13440 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13443 /* Default fast path register access methods */
13444 tp->read32 = tg3_read32;
13445 tp->write32 = tg3_write32;
13446 tp->read32_mbox = tg3_read32;
13447 tp->write32_mbox = tg3_write32;
13448 tp->write32_tx_mbox = tg3_write32;
13449 tp->write32_rx_mbox = tg3_write32;
13451 /* Various workaround register access methods */
13452 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13453 tp->write32 = tg3_write_indirect_reg32;
13454 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13455 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13456 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13458 * Back to back register writes can cause problems on these
13459 * chips, the workaround is to read back all reg writes
13460 * except those to mailbox regs.
13462 * See tg3_write_indirect_reg32().
13464 tp->write32 = tg3_write_flush_reg32;
13467 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13468 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13469 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13470 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13471 tp->write32_rx_mbox = tg3_write_flush_reg32;
13474 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13475 tp->read32 = tg3_read_indirect_reg32;
13476 tp->write32 = tg3_write_indirect_reg32;
13477 tp->read32_mbox = tg3_read_indirect_mbox;
13478 tp->write32_mbox = tg3_write_indirect_mbox;
13479 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13480 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13482 iounmap(tp->regs);
13483 tp->regs = NULL;
13485 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13486 pci_cmd &= ~PCI_COMMAND_MEMORY;
13487 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13489 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13490 tp->read32_mbox = tg3_read32_mbox_5906;
13491 tp->write32_mbox = tg3_write32_mbox_5906;
13492 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13493 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13496 if (tp->write32 == tg3_write_indirect_reg32 ||
13497 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13498 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13499 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
13500 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13502 /* Get eeprom hw config before calling tg3_set_power_state().
13503 * In particular, the TG3_FLG2_IS_NIC flag must be
13504 * determined before calling tg3_set_power_state() so that
13505 * we know whether or not to switch out of Vaux power.
13506 * When the flag is set, it means that GPIO1 is used for eeprom
13507 * write protect and also implies that it is a LOM where GPIOs
13508 * are not used to switch power.
13510 tg3_get_eeprom_hw_cfg(tp);
13512 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13513 /* Allow reads and writes to the
13514 * APE register and memory space.
13516 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
13517 PCISTATE_ALLOW_APE_SHMEM_WR |
13518 PCISTATE_ALLOW_APE_PSPACE_WR;
13519 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13520 pci_state_reg);
13523 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13524 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13525 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13526 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13527 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
13528 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13530 /* Set up tp->grc_local_ctrl before calling tg_power_up().
13531 * GPIO1 driven high will bring 5700's external PHY out of reset.
13532 * It is also used as eeprom write protect on LOMs.
13534 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13535 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13536 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13537 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13538 GRC_LCLCTRL_GPIO_OUTPUT1);
13539 /* Unused GPIO3 must be driven as output on 5752 because there
13540 * are no pull-up resistors on unused GPIO pins.
13542 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13543 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
13545 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13546 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13547 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13548 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13550 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13551 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
13552 /* Turn off the debug UART. */
13553 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13554 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13555 /* Keep VMain power. */
13556 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13557 GRC_LCLCTRL_GPIO_OUTPUT0;
13560 /* Force the chip into D0. */
13561 err = tg3_power_up(tp);
13562 if (err) {
13563 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
13564 return err;
13567 /* Derive initial jumbo mode from MTU assigned in
13568 * ether_setup() via the alloc_etherdev() call
13570 if (tp->dev->mtu > ETH_DATA_LEN &&
13571 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
13572 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
13574 /* Determine WakeOnLan speed to use. */
13575 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13576 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13577 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13578 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13579 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13580 } else {
13581 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13584 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13585 tp->phy_flags |= TG3_PHYFLG_IS_FET;
13587 /* A few boards don't want Ethernet@WireSpeed phy feature */
13588 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13589 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13590 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
13591 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
13592 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13593 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13594 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
13596 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13597 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13598 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
13599 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13600 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
13602 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
13603 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
13604 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
13605 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
13606 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
13607 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
13608 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
13609 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13610 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
13611 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13612 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13613 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
13614 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
13615 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
13616 } else
13617 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
13620 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13621 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13622 tp->phy_otp = tg3_read_otp_phycfg(tp);
13623 if (tp->phy_otp == 0)
13624 tp->phy_otp = TG3_OTP_DEFAULT;
13627 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
13628 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13629 else
13630 tp->mi_mode = MAC_MI_MODE_BASE;
13632 tp->coalesce_mode = 0;
13633 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13634 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13635 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13638 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
13639 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13641 err = tg3_mdio_init(tp);
13642 if (err)
13643 return err;
13645 /* Initialize data/descriptor byte/word swapping. */
13646 val = tr32(GRC_MODE);
13647 val &= GRC_MODE_HOST_STACKUP;
13648 tw32(GRC_MODE, val | tp->grc_mode);
13650 tg3_switch_clocks(tp);
13652 /* Clear this out for sanity. */
13653 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13655 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13656 &pci_state_reg);
13657 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13658 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13659 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13661 if (chiprevid == CHIPREV_ID_5701_A0 ||
13662 chiprevid == CHIPREV_ID_5701_B0 ||
13663 chiprevid == CHIPREV_ID_5701_B2 ||
13664 chiprevid == CHIPREV_ID_5701_B5) {
13665 void __iomem *sram_base;
13667 /* Write some dummy words into the SRAM status block
13668 * area, see if it reads back correctly. If the return
13669 * value is bad, force enable the PCIX workaround.
13671 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13673 writel(0x00000000, sram_base);
13674 writel(0x00000000, sram_base + 4);
13675 writel(0xffffffff, sram_base + 4);
13676 if (readl(sram_base) != 0x00000000)
13677 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13681 udelay(50);
13682 tg3_nvram_init(tp);
13684 grc_misc_cfg = tr32(GRC_MISC_CFG);
13685 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13687 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13688 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13689 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13690 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13692 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13693 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13694 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13695 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13696 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13697 HOSTCC_MODE_CLRTICK_TXBD);
13699 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13700 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13701 tp->misc_host_ctrl);
13704 /* Preserve the APE MAC_MODE bits */
13705 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13706 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13707 else
13708 tp->mac_mode = TG3_DEF_MAC_MODE;
13710 /* these are limited to 10/100 only */
13711 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13712 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13713 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13714 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13715 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13716 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13717 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13718 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13719 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
13720 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13721 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
13722 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
13723 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13724 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
13725 (tp->phy_flags & TG3_PHYFLG_IS_FET))
13726 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
13728 err = tg3_phy_probe(tp);
13729 if (err) {
13730 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
13731 /* ... but do not return immediately ... */
13732 tg3_mdio_fini(tp);
13735 tg3_read_vpd(tp);
13736 tg3_read_fw_ver(tp);
13738 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
13739 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
13740 } else {
13741 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13742 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
13743 else
13744 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
13747 /* 5700 {AX,BX} chips have a broken status block link
13748 * change bit implementation, so we must use the
13749 * status register in those cases.
13751 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13752 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13753 else
13754 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13756 /* The led_ctrl is set during tg3_phy_probe, here we might
13757 * have to force the link status polling mechanism based
13758 * upon subsystem IDs.
13760 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
13761 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
13762 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
13763 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
13764 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13767 /* For all SERDES we poll the MAC status register. */
13768 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
13769 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13770 else
13771 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13773 tp->rx_offset = NET_IP_ALIGN;
13774 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
13775 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
13776 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
13777 tp->rx_offset = 0;
13778 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
13779 tp->rx_copy_thresh = ~(u16)0;
13780 #endif
13783 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
13784 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
13785 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
13787 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
13789 /* Increment the rx prod index on the rx std ring by at most
13790 * 8 for these chips to workaround hw errata.
13792 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13793 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13794 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13795 tp->rx_std_max_post = 8;
13797 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13798 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13799 PCIE_PWR_MGMT_L1_THRESH_MSK;
13801 return err;
13804 #ifdef CONFIG_SPARC
13805 static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13807 struct net_device *dev = tp->dev;
13808 struct pci_dev *pdev = tp->pdev;
13809 struct device_node *dp = pci_device_to_OF_node(pdev);
13810 const unsigned char *addr;
13811 int len;
13813 addr = of_get_property(dp, "local-mac-address", &len);
13814 if (addr && len == 6) {
13815 memcpy(dev->dev_addr, addr, 6);
13816 memcpy(dev->perm_addr, dev->dev_addr, 6);
13817 return 0;
13819 return -ENODEV;
13822 static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13824 struct net_device *dev = tp->dev;
13826 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
13827 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
13828 return 0;
13830 #endif
13832 static int __devinit tg3_get_device_address(struct tg3 *tp)
13834 struct net_device *dev = tp->dev;
13835 u32 hi, lo, mac_offset;
13836 int addr_ok = 0;
13838 #ifdef CONFIG_SPARC
13839 if (!tg3_get_macaddr_sparc(tp))
13840 return 0;
13841 #endif
13843 mac_offset = 0x7c;
13844 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
13845 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13846 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13847 mac_offset = 0xcc;
13848 if (tg3_nvram_lock(tp))
13849 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13850 else
13851 tg3_nvram_unlock(tp);
13852 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13853 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
13854 if (PCI_FUNC(tp->pdev->devfn) & 1)
13855 mac_offset = 0xcc;
13856 if (PCI_FUNC(tp->pdev->devfn) > 1)
13857 mac_offset += 0x18c;
13858 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13859 mac_offset = 0x10;
13861 /* First try to get it from MAC address mailbox. */
13862 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13863 if ((hi >> 16) == 0x484b) {
13864 dev->dev_addr[0] = (hi >> 8) & 0xff;
13865 dev->dev_addr[1] = (hi >> 0) & 0xff;
13867 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13868 dev->dev_addr[2] = (lo >> 24) & 0xff;
13869 dev->dev_addr[3] = (lo >> 16) & 0xff;
13870 dev->dev_addr[4] = (lo >> 8) & 0xff;
13871 dev->dev_addr[5] = (lo >> 0) & 0xff;
13873 /* Some old bootcode may report a 0 MAC address in SRAM */
13874 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13876 if (!addr_ok) {
13877 /* Next, try NVRAM. */
13878 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13879 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
13880 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
13881 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13882 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
13884 /* Finally just fetch it out of the MAC control regs. */
13885 else {
13886 hi = tr32(MAC_ADDR_0_HIGH);
13887 lo = tr32(MAC_ADDR_0_LOW);
13889 dev->dev_addr[5] = lo & 0xff;
13890 dev->dev_addr[4] = (lo >> 8) & 0xff;
13891 dev->dev_addr[3] = (lo >> 16) & 0xff;
13892 dev->dev_addr[2] = (lo >> 24) & 0xff;
13893 dev->dev_addr[1] = hi & 0xff;
13894 dev->dev_addr[0] = (hi >> 8) & 0xff;
13898 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
13899 #ifdef CONFIG_SPARC
13900 if (!tg3_get_default_macaddr_sparc(tp))
13901 return 0;
13902 #endif
13903 return -EINVAL;
13905 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
13906 return 0;
13909 #define BOUNDARY_SINGLE_CACHELINE 1
13910 #define BOUNDARY_MULTI_CACHELINE 2
13912 static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13914 int cacheline_size;
13915 u8 byte;
13916 int goal;
13918 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13919 if (byte == 0)
13920 cacheline_size = 1024;
13921 else
13922 cacheline_size = (int) byte * 4;
13924 /* On 5703 and later chips, the boundary bits have no
13925 * effect.
13927 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13928 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13929 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13930 goto out;
13932 #if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13933 goal = BOUNDARY_MULTI_CACHELINE;
13934 #else
13935 #if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13936 goal = BOUNDARY_SINGLE_CACHELINE;
13937 #else
13938 goal = 0;
13939 #endif
13940 #endif
13942 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
13943 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13944 goto out;
13947 if (!goal)
13948 goto out;
13950 /* PCI controllers on most RISC systems tend to disconnect
13951 * when a device tries to burst across a cache-line boundary.
13952 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13954 * Unfortunately, for PCI-E there are only limited
13955 * write-side controls for this, and thus for reads
13956 * we will still get the disconnects. We'll also waste
13957 * these PCI cycles for both read and write for chips
13958 * other than 5700 and 5701 which do not implement the
13959 * boundary bits.
13961 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13962 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13963 switch (cacheline_size) {
13964 case 16:
13965 case 32:
13966 case 64:
13967 case 128:
13968 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13969 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13970 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13971 } else {
13972 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13973 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13975 break;
13977 case 256:
13978 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13979 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13980 break;
13982 default:
13983 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13984 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13985 break;
13987 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13988 switch (cacheline_size) {
13989 case 16:
13990 case 32:
13991 case 64:
13992 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13993 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13994 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13995 break;
13997 /* fallthrough */
13998 case 128:
13999 default:
14000 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14001 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14002 break;
14004 } else {
14005 switch (cacheline_size) {
14006 case 16:
14007 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14008 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14009 DMA_RWCTRL_WRITE_BNDRY_16);
14010 break;
14012 /* fallthrough */
14013 case 32:
14014 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14015 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14016 DMA_RWCTRL_WRITE_BNDRY_32);
14017 break;
14019 /* fallthrough */
14020 case 64:
14021 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14022 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14023 DMA_RWCTRL_WRITE_BNDRY_64);
14024 break;
14026 /* fallthrough */
14027 case 128:
14028 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14029 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14030 DMA_RWCTRL_WRITE_BNDRY_128);
14031 break;
14033 /* fallthrough */
14034 case 256:
14035 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14036 DMA_RWCTRL_WRITE_BNDRY_256);
14037 break;
14038 case 512:
14039 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14040 DMA_RWCTRL_WRITE_BNDRY_512);
14041 break;
14042 case 1024:
14043 default:
14044 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14045 DMA_RWCTRL_WRITE_BNDRY_1024);
14046 break;
14050 out:
14051 return val;
14054 static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14056 struct tg3_internal_buffer_desc test_desc;
14057 u32 sram_dma_descs;
14058 int i, ret;
14060 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14062 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14063 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14064 tw32(RDMAC_STATUS, 0);
14065 tw32(WDMAC_STATUS, 0);
14067 tw32(BUFMGR_MODE, 0);
14068 tw32(FTQ_RESET, 0);
14070 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14071 test_desc.addr_lo = buf_dma & 0xffffffff;
14072 test_desc.nic_mbuf = 0x00002100;
14073 test_desc.len = size;
14076 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14077 * the *second* time the tg3 driver was getting loaded after an
14078 * initial scan.
14080 * Broadcom tells me:
14081 * ...the DMA engine is connected to the GRC block and a DMA
14082 * reset may affect the GRC block in some unpredictable way...
14083 * The behavior of resets to individual blocks has not been tested.
14085 * Broadcom noted the GRC reset will also reset all sub-components.
14087 if (to_device) {
14088 test_desc.cqid_sqid = (13 << 8) | 2;
14090 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14091 udelay(40);
14092 } else {
14093 test_desc.cqid_sqid = (16 << 8) | 7;
14095 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14096 udelay(40);
14098 test_desc.flags = 0x00000005;
14100 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14101 u32 val;
14103 val = *(((u32 *)&test_desc) + i);
14104 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14105 sram_dma_descs + (i * sizeof(u32)));
14106 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14108 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14110 if (to_device)
14111 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
14112 else
14113 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
14115 ret = -ENODEV;
14116 for (i = 0; i < 40; i++) {
14117 u32 val;
14119 if (to_device)
14120 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14121 else
14122 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14123 if ((val & 0xffff) == sram_dma_descs) {
14124 ret = 0;
14125 break;
14128 udelay(100);
14131 return ret;
14134 #define TEST_BUFFER_SIZE 0x2000
14136 DEFINE_PCI_DEVICE_TABLE(dma_wait_state_chipsets) = {
14137 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14138 { },
14141 static int __devinit tg3_test_dma(struct tg3 *tp)
14143 dma_addr_t buf_dma;
14144 u32 *buf, saved_dma_rwctrl;
14145 int ret = 0;
14147 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14148 &buf_dma, GFP_KERNEL);
14149 if (!buf) {
14150 ret = -ENOMEM;
14151 goto out_nofree;
14154 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14155 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14157 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
14159 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
14160 goto out;
14162 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14163 /* DMA read watermark not used on PCIE */
14164 tp->dma_rwctrl |= 0x00180000;
14165 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
14166 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14167 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
14168 tp->dma_rwctrl |= 0x003f0000;
14169 else
14170 tp->dma_rwctrl |= 0x003f000f;
14171 } else {
14172 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14174 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
14175 u32 read_water = 0x7;
14177 /* If the 5704 is behind the EPB bridge, we can
14178 * do the less restrictive ONE_DMA workaround for
14179 * better performance.
14181 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
14182 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14183 tp->dma_rwctrl |= 0x8000;
14184 else if (ccval == 0x6 || ccval == 0x7)
14185 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14187 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14188 read_water = 4;
14189 /* Set bit 23 to enable PCIX hw bug fix */
14190 tp->dma_rwctrl |=
14191 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14192 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14193 (1 << 23);
14194 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14195 /* 5780 always in PCIX mode */
14196 tp->dma_rwctrl |= 0x00144000;
14197 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14198 /* 5714 always in PCIX mode */
14199 tp->dma_rwctrl |= 0x00148000;
14200 } else {
14201 tp->dma_rwctrl |= 0x001b000f;
14205 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14206 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14207 tp->dma_rwctrl &= 0xfffffff0;
14209 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14210 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14211 /* Remove this if it causes problems for some boards. */
14212 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14214 /* On 5700/5701 chips, we need to set this bit.
14215 * Otherwise the chip will issue cacheline transactions
14216 * to streamable DMA memory with not all the byte
14217 * enables turned on. This is an error on several
14218 * RISC PCI controllers, in particular sparc64.
14220 * On 5703/5704 chips, this bit has been reassigned
14221 * a different meaning. In particular, it is used
14222 * on those chips to enable a PCI-X workaround.
14224 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14227 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14229 #if 0
14230 /* Unneeded, already done by tg3_get_invariants. */
14231 tg3_switch_clocks(tp);
14232 #endif
14234 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14235 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14236 goto out;
14238 /* It is best to perform DMA test with maximum write burst size
14239 * to expose the 5700/5701 write DMA bug.
14241 saved_dma_rwctrl = tp->dma_rwctrl;
14242 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14243 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14245 while (1) {
14246 u32 *p = buf, i;
14248 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14249 p[i] = i;
14251 /* Send the buffer to the chip. */
14252 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14253 if (ret) {
14254 dev_err(&tp->pdev->dev,
14255 "%s: Buffer write failed. err = %d\n",
14256 __func__, ret);
14257 break;
14260 #if 0
14261 /* validate data reached card RAM correctly. */
14262 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14263 u32 val;
14264 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14265 if (le32_to_cpu(val) != p[i]) {
14266 dev_err(&tp->pdev->dev,
14267 "%s: Buffer corrupted on device! "
14268 "(%d != %d)\n", __func__, val, i);
14269 /* ret = -ENODEV here? */
14271 p[i] = 0;
14273 #endif
14274 /* Now read it back. */
14275 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14276 if (ret) {
14277 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14278 "err = %d\n", __func__, ret);
14279 break;
14282 /* Verify it. */
14283 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14284 if (p[i] == i)
14285 continue;
14287 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14288 DMA_RWCTRL_WRITE_BNDRY_16) {
14289 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14290 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14291 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14292 break;
14293 } else {
14294 dev_err(&tp->pdev->dev,
14295 "%s: Buffer corrupted on read back! "
14296 "(%d != %d)\n", __func__, p[i], i);
14297 ret = -ENODEV;
14298 goto out;
14302 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14303 /* Success. */
14304 ret = 0;
14305 break;
14308 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14309 DMA_RWCTRL_WRITE_BNDRY_16) {
14311 /* DMA test passed without adjusting DMA boundary,
14312 * now look for chipsets that are known to expose the
14313 * DMA bug without failing the test.
14315 if (pci_dev_present(dma_wait_state_chipsets)) {
14316 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14317 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14318 } else {
14319 /* Safe to use the calculated DMA boundary. */
14320 tp->dma_rwctrl = saved_dma_rwctrl;
14323 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14326 out:
14327 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
14328 out_nofree:
14329 return ret;
14332 static void __devinit tg3_init_link_config(struct tg3 *tp)
14334 tp->link_config.advertising =
14335 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14336 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14337 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14338 ADVERTISED_Autoneg | ADVERTISED_MII);
14339 tp->link_config.speed = SPEED_INVALID;
14340 tp->link_config.duplex = DUPLEX_INVALID;
14341 tp->link_config.autoneg = AUTONEG_ENABLE;
14342 tp->link_config.active_speed = SPEED_INVALID;
14343 tp->link_config.active_duplex = DUPLEX_INVALID;
14344 tp->link_config.orig_speed = SPEED_INVALID;
14345 tp->link_config.orig_duplex = DUPLEX_INVALID;
14346 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14349 static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14351 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
14352 tp->bufmgr_config.mbuf_read_dma_low_water =
14353 DEFAULT_MB_RDMA_LOW_WATER_5705;
14354 tp->bufmgr_config.mbuf_mac_rx_low_water =
14355 DEFAULT_MB_MACRX_LOW_WATER_57765;
14356 tp->bufmgr_config.mbuf_high_water =
14357 DEFAULT_MB_HIGH_WATER_57765;
14359 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14360 DEFAULT_MB_RDMA_LOW_WATER_5705;
14361 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14362 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14363 tp->bufmgr_config.mbuf_high_water_jumbo =
14364 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14365 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14366 tp->bufmgr_config.mbuf_read_dma_low_water =
14367 DEFAULT_MB_RDMA_LOW_WATER_5705;
14368 tp->bufmgr_config.mbuf_mac_rx_low_water =
14369 DEFAULT_MB_MACRX_LOW_WATER_5705;
14370 tp->bufmgr_config.mbuf_high_water =
14371 DEFAULT_MB_HIGH_WATER_5705;
14372 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14373 tp->bufmgr_config.mbuf_mac_rx_low_water =
14374 DEFAULT_MB_MACRX_LOW_WATER_5906;
14375 tp->bufmgr_config.mbuf_high_water =
14376 DEFAULT_MB_HIGH_WATER_5906;
14379 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14380 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14381 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14382 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14383 tp->bufmgr_config.mbuf_high_water_jumbo =
14384 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14385 } else {
14386 tp->bufmgr_config.mbuf_read_dma_low_water =
14387 DEFAULT_MB_RDMA_LOW_WATER;
14388 tp->bufmgr_config.mbuf_mac_rx_low_water =
14389 DEFAULT_MB_MACRX_LOW_WATER;
14390 tp->bufmgr_config.mbuf_high_water =
14391 DEFAULT_MB_HIGH_WATER;
14393 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14394 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14395 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14396 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14397 tp->bufmgr_config.mbuf_high_water_jumbo =
14398 DEFAULT_MB_HIGH_WATER_JUMBO;
14401 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14402 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14405 static char * __devinit tg3_phy_string(struct tg3 *tp)
14407 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14408 case TG3_PHY_ID_BCM5400: return "5400";
14409 case TG3_PHY_ID_BCM5401: return "5401";
14410 case TG3_PHY_ID_BCM5411: return "5411";
14411 case TG3_PHY_ID_BCM5701: return "5701";
14412 case TG3_PHY_ID_BCM5703: return "5703";
14413 case TG3_PHY_ID_BCM5704: return "5704";
14414 case TG3_PHY_ID_BCM5705: return "5705";
14415 case TG3_PHY_ID_BCM5750: return "5750";
14416 case TG3_PHY_ID_BCM5752: return "5752";
14417 case TG3_PHY_ID_BCM5714: return "5714";
14418 case TG3_PHY_ID_BCM5780: return "5780";
14419 case TG3_PHY_ID_BCM5755: return "5755";
14420 case TG3_PHY_ID_BCM5787: return "5787";
14421 case TG3_PHY_ID_BCM5784: return "5784";
14422 case TG3_PHY_ID_BCM5756: return "5722/5756";
14423 case TG3_PHY_ID_BCM5906: return "5906";
14424 case TG3_PHY_ID_BCM5761: return "5761";
14425 case TG3_PHY_ID_BCM5718C: return "5718C";
14426 case TG3_PHY_ID_BCM5718S: return "5718S";
14427 case TG3_PHY_ID_BCM57765: return "57765";
14428 case TG3_PHY_ID_BCM5719C: return "5719C";
14429 case TG3_PHY_ID_BCM8002: return "8002/serdes";
14430 case 0: return "serdes";
14431 default: return "unknown";
14435 static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14437 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14438 strcpy(str, "PCI Express");
14439 return str;
14440 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14441 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14443 strcpy(str, "PCIX:");
14445 if ((clock_ctrl == 7) ||
14446 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14447 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14448 strcat(str, "133MHz");
14449 else if (clock_ctrl == 0)
14450 strcat(str, "33MHz");
14451 else if (clock_ctrl == 2)
14452 strcat(str, "50MHz");
14453 else if (clock_ctrl == 4)
14454 strcat(str, "66MHz");
14455 else if (clock_ctrl == 6)
14456 strcat(str, "100MHz");
14457 } else {
14458 strcpy(str, "PCI:");
14459 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14460 strcat(str, "66MHz");
14461 else
14462 strcat(str, "33MHz");
14464 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14465 strcat(str, ":32-bit");
14466 else
14467 strcat(str, ":64-bit");
14468 return str;
14471 static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
14473 struct pci_dev *peer;
14474 unsigned int func, devnr = tp->pdev->devfn & ~7;
14476 for (func = 0; func < 8; func++) {
14477 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14478 if (peer && peer != tp->pdev)
14479 break;
14480 pci_dev_put(peer);
14482 /* 5704 can be configured in single-port mode, set peer to
14483 * tp->pdev in that case.
14485 if (!peer) {
14486 peer = tp->pdev;
14487 return peer;
14491 * We don't need to keep the refcount elevated; there's no way
14492 * to remove one half of this device without removing the other
14494 pci_dev_put(peer);
14496 return peer;
14499 static void __devinit tg3_init_coal(struct tg3 *tp)
14501 struct ethtool_coalesce *ec = &tp->coal;
14503 memset(ec, 0, sizeof(*ec));
14504 ec->cmd = ETHTOOL_GCOALESCE;
14505 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14506 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14507 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14508 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14509 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14510 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14511 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14512 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14513 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14515 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14516 HOSTCC_MODE_CLRTICK_TXBD)) {
14517 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14518 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14519 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14520 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14523 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14524 ec->rx_coalesce_usecs_irq = 0;
14525 ec->tx_coalesce_usecs_irq = 0;
14526 ec->stats_block_coalesce_usecs = 0;
14530 static const struct net_device_ops tg3_netdev_ops = {
14531 .ndo_open = tg3_open,
14532 .ndo_stop = tg3_close,
14533 .ndo_start_xmit = tg3_start_xmit,
14534 .ndo_get_stats64 = tg3_get_stats64,
14535 .ndo_validate_addr = eth_validate_addr,
14536 .ndo_set_multicast_list = tg3_set_rx_mode,
14537 .ndo_set_mac_address = tg3_set_mac_addr,
14538 .ndo_do_ioctl = tg3_ioctl,
14539 .ndo_tx_timeout = tg3_tx_timeout,
14540 .ndo_change_mtu = tg3_change_mtu,
14541 #ifdef CONFIG_NET_POLL_CONTROLLER
14542 .ndo_poll_controller = tg3_poll_controller,
14543 #endif
14546 static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14547 .ndo_open = tg3_open,
14548 .ndo_stop = tg3_close,
14549 .ndo_start_xmit = tg3_start_xmit_dma_bug,
14550 .ndo_get_stats64 = tg3_get_stats64,
14551 .ndo_validate_addr = eth_validate_addr,
14552 .ndo_set_multicast_list = tg3_set_rx_mode,
14553 .ndo_set_mac_address = tg3_set_mac_addr,
14554 .ndo_do_ioctl = tg3_ioctl,
14555 .ndo_tx_timeout = tg3_tx_timeout,
14556 .ndo_change_mtu = tg3_change_mtu,
14557 #ifdef CONFIG_NET_POLL_CONTROLLER
14558 .ndo_poll_controller = tg3_poll_controller,
14559 #endif
14562 static int __devinit tg3_init_one(struct pci_dev *pdev,
14563 const struct pci_device_id *ent)
14565 struct net_device *dev;
14566 struct tg3 *tp;
14567 int i, err, pm_cap;
14568 u32 sndmbx, rcvmbx, intmbx;
14569 char str[40];
14570 u64 dma_mask, persist_dma_mask;
14572 printk_once(KERN_INFO "%s\n", version);
14574 err = pci_enable_device(pdev);
14575 if (err) {
14576 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14577 return err;
14580 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14581 if (err) {
14582 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14583 goto err_out_disable_pdev;
14586 pci_set_master(pdev);
14588 /* Find power-management capability. */
14589 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14590 if (pm_cap == 0) {
14591 dev_err(&pdev->dev,
14592 "Cannot find Power Management capability, aborting\n");
14593 err = -EIO;
14594 goto err_out_free_res;
14597 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
14598 if (!dev) {
14599 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
14600 err = -ENOMEM;
14601 goto err_out_free_res;
14604 SET_NETDEV_DEV(dev, &pdev->dev);
14606 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
14608 tp = netdev_priv(dev);
14609 tp->pdev = pdev;
14610 tp->dev = dev;
14611 tp->pm_cap = pm_cap;
14612 tp->rx_mode = TG3_DEF_RX_MODE;
14613 tp->tx_mode = TG3_DEF_TX_MODE;
14615 if (tg3_debug > 0)
14616 tp->msg_enable = tg3_debug;
14617 else
14618 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14620 /* The word/byte swap controls here control register access byte
14621 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14622 * setting below.
14624 tp->misc_host_ctrl =
14625 MISC_HOST_CTRL_MASK_PCI_INT |
14626 MISC_HOST_CTRL_WORD_SWAP |
14627 MISC_HOST_CTRL_INDIR_ACCESS |
14628 MISC_HOST_CTRL_PCISTATE_RW;
14630 /* The NONFRM (non-frame) byte/word swap controls take effect
14631 * on descriptor entries, anything which isn't packet data.
14633 * The StrongARM chips on the board (one for tx, one for rx)
14634 * are running in big-endian mode.
14636 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14637 GRC_MODE_WSWAP_NONFRM_DATA);
14638 #ifdef __BIG_ENDIAN
14639 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14640 #endif
14641 spin_lock_init(&tp->lock);
14642 spin_lock_init(&tp->indirect_lock);
14643 INIT_WORK(&tp->reset_task, tg3_reset_task);
14645 tp->regs = pci_ioremap_bar(pdev, BAR_0);
14646 if (!tp->regs) {
14647 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14648 err = -ENOMEM;
14649 goto err_out_free_dev;
14652 tg3_init_link_config(tp);
14654 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14655 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
14657 dev->ethtool_ops = &tg3_ethtool_ops;
14658 dev->watchdog_timeo = TG3_TX_TIMEOUT;
14659 dev->irq = pdev->irq;
14661 err = tg3_get_invariants(tp);
14662 if (err) {
14663 dev_err(&pdev->dev,
14664 "Problem fetching invariants of chip, aborting\n");
14665 goto err_out_iounmap;
14668 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
14669 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
14670 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
14671 dev->netdev_ops = &tg3_netdev_ops;
14672 else
14673 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14676 /* The EPB bridge inside 5714, 5715, and 5780 and any
14677 * device behind the EPB cannot support DMA addresses > 40-bit.
14678 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14679 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14680 * do DMA address check in tg3_start_xmit().
14682 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
14683 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
14684 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
14685 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
14686 #ifdef CONFIG_HIGHMEM
14687 dma_mask = DMA_BIT_MASK(64);
14688 #endif
14689 } else
14690 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
14692 /* Configure DMA attributes. */
14693 if (dma_mask > DMA_BIT_MASK(32)) {
14694 err = pci_set_dma_mask(pdev, dma_mask);
14695 if (!err) {
14696 dev->features |= NETIF_F_HIGHDMA;
14697 err = pci_set_consistent_dma_mask(pdev,
14698 persist_dma_mask);
14699 if (err < 0) {
14700 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14701 "DMA for consistent allocations\n");
14702 goto err_out_iounmap;
14706 if (err || dma_mask == DMA_BIT_MASK(32)) {
14707 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
14708 if (err) {
14709 dev_err(&pdev->dev,
14710 "No usable DMA configuration, aborting\n");
14711 goto err_out_iounmap;
14715 tg3_init_bufmgr_config(tp);
14717 /* Selectively allow TSO based on operating conditions */
14718 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14719 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14720 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14721 else {
14722 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14723 tp->fw_needed = NULL;
14726 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14727 tp->fw_needed = FIRMWARE_TG3;
14729 /* TSO is on by default on chips that support hardware TSO.
14730 * Firmware TSO on older chips gives lower performance, so it
14731 * is off by default, but can be enabled using ethtool.
14733 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
14734 (dev->features & NETIF_F_IP_CSUM)) {
14735 dev->features |= NETIF_F_TSO;
14736 vlan_features_add(dev, NETIF_F_TSO);
14738 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14739 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
14740 if (dev->features & NETIF_F_IPV6_CSUM) {
14741 dev->features |= NETIF_F_TSO6;
14742 vlan_features_add(dev, NETIF_F_TSO6);
14744 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14745 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14746 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14747 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
14748 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14749 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
14750 dev->features |= NETIF_F_TSO_ECN;
14751 vlan_features_add(dev, NETIF_F_TSO_ECN);
14755 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14756 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14757 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14758 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14759 tp->rx_pending = 63;
14762 err = tg3_get_device_address(tp);
14763 if (err) {
14764 dev_err(&pdev->dev,
14765 "Could not obtain valid ethernet address, aborting\n");
14766 goto err_out_iounmap;
14769 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
14770 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
14771 if (!tp->aperegs) {
14772 dev_err(&pdev->dev,
14773 "Cannot map APE registers, aborting\n");
14774 err = -ENOMEM;
14775 goto err_out_iounmap;
14778 tg3_ape_lock_init(tp);
14780 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14781 tg3_read_dash_ver(tp);
14785 * Reset chip in case UNDI or EFI driver did not shutdown
14786 * DMA self test will enable WDMAC and we'll see (spurious)
14787 * pending DMA on the PCI bus at that point.
14789 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14790 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14791 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14792 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14795 err = tg3_test_dma(tp);
14796 if (err) {
14797 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
14798 goto err_out_apeunmap;
14801 /* flow control autonegotiation is default behavior */
14802 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
14803 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
14805 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14806 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14807 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
14808 for (i = 0; i < tp->irq_max; i++) {
14809 struct tg3_napi *tnapi = &tp->napi[i];
14811 tnapi->tp = tp;
14812 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14814 tnapi->int_mbox = intmbx;
14815 if (i < 4)
14816 intmbx += 0x8;
14817 else
14818 intmbx += 0x4;
14820 tnapi->consmbox = rcvmbx;
14821 tnapi->prodmbox = sndmbx;
14823 if (i)
14824 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14825 else
14826 tnapi->coal_now = HOSTCC_MODE_NOW;
14828 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14829 break;
14832 * If we support MSIX, we'll be using RSS. If we're using
14833 * RSS, the first vector only handles link interrupts and the
14834 * remaining vectors handle rx and tx interrupts. Reuse the
14835 * mailbox values for the next iteration. The values we setup
14836 * above are still useful for the single vectored mode.
14838 if (!i)
14839 continue;
14841 rcvmbx += 0x8;
14843 if (sndmbx & 0x4)
14844 sndmbx -= 0x4;
14845 else
14846 sndmbx += 0xc;
14849 tg3_init_coal(tp);
14851 pci_set_drvdata(pdev, dev);
14853 err = register_netdev(dev);
14854 if (err) {
14855 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
14856 goto err_out_apeunmap;
14859 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14860 tp->board_part_number,
14861 tp->pci_chip_rev_id,
14862 tg3_bus_string(tp, str),
14863 dev->dev_addr);
14865 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
14866 struct phy_device *phydev;
14867 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
14868 netdev_info(dev,
14869 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
14870 phydev->drv->name, dev_name(&phydev->dev));
14871 } else {
14872 char *ethtype;
14874 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
14875 ethtype = "10/100Base-TX";
14876 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
14877 ethtype = "1000Base-SX";
14878 else
14879 ethtype = "10/100/1000Base-T";
14881 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
14882 "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
14883 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
14886 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
14887 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14888 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14889 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
14890 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
14891 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
14892 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14893 tp->dma_rwctrl,
14894 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
14895 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
14897 return 0;
14899 err_out_apeunmap:
14900 if (tp->aperegs) {
14901 iounmap(tp->aperegs);
14902 tp->aperegs = NULL;
14905 err_out_iounmap:
14906 if (tp->regs) {
14907 iounmap(tp->regs);
14908 tp->regs = NULL;
14911 err_out_free_dev:
14912 free_netdev(dev);
14914 err_out_free_res:
14915 pci_release_regions(pdev);
14917 err_out_disable_pdev:
14918 pci_disable_device(pdev);
14919 pci_set_drvdata(pdev, NULL);
14920 return err;
14923 static void __devexit tg3_remove_one(struct pci_dev *pdev)
14925 struct net_device *dev = pci_get_drvdata(pdev);
14927 if (dev) {
14928 struct tg3 *tp = netdev_priv(dev);
14930 if (tp->fw)
14931 release_firmware(tp->fw);
14933 cancel_work_sync(&tp->reset_task);
14935 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14936 tg3_phy_fini(tp);
14937 tg3_mdio_fini(tp);
14940 unregister_netdev(dev);
14941 if (tp->aperegs) {
14942 iounmap(tp->aperegs);
14943 tp->aperegs = NULL;
14945 if (tp->regs) {
14946 iounmap(tp->regs);
14947 tp->regs = NULL;
14949 free_netdev(dev);
14950 pci_release_regions(pdev);
14951 pci_disable_device(pdev);
14952 pci_set_drvdata(pdev, NULL);
14956 #ifdef CONFIG_PM_SLEEP
14957 static int tg3_suspend(struct device *device)
14959 struct pci_dev *pdev = to_pci_dev(device);
14960 struct net_device *dev = pci_get_drvdata(pdev);
14961 struct tg3 *tp = netdev_priv(dev);
14962 int err;
14964 if (!netif_running(dev))
14965 return 0;
14967 flush_work_sync(&tp->reset_task);
14968 tg3_phy_stop(tp);
14969 tg3_netif_stop(tp);
14971 del_timer_sync(&tp->timer);
14973 tg3_full_lock(tp, 1);
14974 tg3_disable_ints(tp);
14975 tg3_full_unlock(tp);
14977 netif_device_detach(dev);
14979 tg3_full_lock(tp, 0);
14980 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14981 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
14982 tg3_full_unlock(tp);
14984 err = tg3_power_down_prepare(tp);
14985 if (err) {
14986 int err2;
14988 tg3_full_lock(tp, 0);
14990 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
14991 err2 = tg3_restart_hw(tp, 1);
14992 if (err2)
14993 goto out;
14995 tp->timer.expires = jiffies + tp->timer_offset;
14996 add_timer(&tp->timer);
14998 netif_device_attach(dev);
14999 tg3_netif_start(tp);
15001 out:
15002 tg3_full_unlock(tp);
15004 if (!err2)
15005 tg3_phy_start(tp);
15008 return err;
15011 static int tg3_resume(struct device *device)
15013 struct pci_dev *pdev = to_pci_dev(device);
15014 struct net_device *dev = pci_get_drvdata(pdev);
15015 struct tg3 *tp = netdev_priv(dev);
15016 int err;
15018 if (!netif_running(dev))
15019 return 0;
15021 netif_device_attach(dev);
15023 tg3_full_lock(tp, 0);
15025 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
15026 err = tg3_restart_hw(tp, 1);
15027 if (err)
15028 goto out;
15030 tp->timer.expires = jiffies + tp->timer_offset;
15031 add_timer(&tp->timer);
15033 tg3_netif_start(tp);
15035 out:
15036 tg3_full_unlock(tp);
15038 if (!err)
15039 tg3_phy_start(tp);
15041 return err;
15044 static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
15045 #define TG3_PM_OPS (&tg3_pm_ops)
15047 #else
15049 #define TG3_PM_OPS NULL
15051 #endif /* CONFIG_PM_SLEEP */
15053 static struct pci_driver tg3_driver = {
15054 .name = DRV_MODULE_NAME,
15055 .id_table = tg3_pci_tbl,
15056 .probe = tg3_init_one,
15057 .remove = __devexit_p(tg3_remove_one),
15058 .driver.pm = TG3_PM_OPS,
15061 static int __init tg3_init(void)
15063 return pci_register_driver(&tg3_driver);
15066 static void __exit tg3_cleanup(void)
15068 pci_unregister_driver(&tg3_driver);
15071 module_init(tg3_init);
15072 module_exit(tg3_cleanup);