7841a8f69998d5351bad0745350edcfa65972208
[linux-2.6.git] / drivers / net / tg3.c
1 /*
2  * tg3.c: Broadcom Tigon3 ethernet driver.
3  *
4  * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5  * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6  * Copyright (C) 2004 Sun Microsystems Inc.
7  * Copyright (C) 2005-2010 Broadcom Corporation.
8  *
9  * Firmware is:
10  *      Derived from proprietary unpublished source code,
11  *      Copyright (C) 2000-2003 Broadcom Corporation.
12  *
13  *      Permission is hereby granted for the distribution of this firmware
14  *      data in hexadecimal or equivalent format, provided this copyright
15  *      notice is accompanying it.
16  */
17
18
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>
46
47 #include <net/checksum.h>
48 #include <net/ip.h>
49
50 #include <asm/system.h>
51 #include <asm/io.h>
52 #include <asm/byteorder.h>
53 #include <asm/uaccess.h>
54
55 #ifdef CONFIG_SPARC
56 #include <asm/idprom.h>
57 #include <asm/prom.h>
58 #endif
59
60 #define BAR_0   0
61 #define BAR_2   2
62
63 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
64 #define TG3_VLAN_TAG_USED 1
65 #else
66 #define TG3_VLAN_TAG_USED 0
67 #endif
68
69 #include "tg3.h"
70
71 #define DRV_MODULE_NAME         "tg3"
72 #define TG3_MAJ_NUM                     3
73 #define TG3_MIN_NUM                     116
74 #define DRV_MODULE_VERSION      \
75         __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
76 #define DRV_MODULE_RELDATE      "December 3, 2010"
77
78 #define TG3_DEF_MAC_MODE        0
79 #define TG3_DEF_RX_MODE         0
80 #define TG3_DEF_TX_MODE         0
81 #define TG3_DEF_MSG_ENABLE        \
82         (NETIF_MSG_DRV          | \
83          NETIF_MSG_PROBE        | \
84          NETIF_MSG_LINK         | \
85          NETIF_MSG_TIMER        | \
86          NETIF_MSG_IFDOWN       | \
87          NETIF_MSG_IFUP         | \
88          NETIF_MSG_RX_ERR       | \
89          NETIF_MSG_TX_ERR)
90
91 /* length of time before we decide the hardware is borked,
92  * and dev->tx_timeout() should be called to fix the problem
93  */
94 #define TG3_TX_TIMEOUT                  (5 * HZ)
95
96 /* hardware minimum and maximum for a single frame's data payload */
97 #define TG3_MIN_MTU                     60
98 #define TG3_MAX_MTU(tp) \
99         ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
100
101 /* These numbers seem to be hard coded in the NIC firmware somehow.
102  * You can't change the ring sizes, but you can change where you place
103  * them in the NIC onboard memory.
104  */
105 #define TG3_RX_STD_RING_SIZE(tp) \
106         ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 || \
107           GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) ? \
108          RX_STD_MAX_SIZE_5717 : 512)
109 #define TG3_DEF_RX_RING_PENDING         200
110 #define TG3_RX_JMB_RING_SIZE(tp) \
111         ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 || \
112           GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) ? \
113          1024 : 256)
114 #define TG3_DEF_RX_JUMBO_RING_PENDING   100
115 #define TG3_RSS_INDIR_TBL_SIZE          128
116
117 /* Do not place this n-ring entries value into the tp struct itself,
118  * we really want to expose these constants to GCC so that modulo et
119  * al.  operations are done with shifts and masks instead of with
120  * hw multiply/modulo instructions.  Another solution would be to
121  * replace things like '% foo' with '& (foo - 1)'.
122  */
123
124 #define TG3_TX_RING_SIZE                512
125 #define TG3_DEF_TX_RING_PENDING         (TG3_TX_RING_SIZE - 1)
126
127 #define TG3_RX_STD_RING_BYTES(tp) \
128         (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
129 #define TG3_RX_JMB_RING_BYTES(tp) \
130         (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
131 #define TG3_RX_RCB_RING_BYTES(tp) \
132         (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
133 #define TG3_TX_RING_BYTES       (sizeof(struct tg3_tx_buffer_desc) * \
134                                  TG3_TX_RING_SIZE)
135 #define NEXT_TX(N)              (((N) + 1) & (TG3_TX_RING_SIZE - 1))
136
137 #define TG3_RX_DMA_ALIGN                16
138 #define TG3_RX_HEADROOM                 ALIGN(VLAN_HLEN, TG3_RX_DMA_ALIGN)
139
140 #define TG3_DMA_BYTE_ENAB               64
141
142 #define TG3_RX_STD_DMA_SZ               1536
143 #define TG3_RX_JMB_DMA_SZ               9046
144
145 #define TG3_RX_DMA_TO_MAP_SZ(x)         ((x) + TG3_DMA_BYTE_ENAB)
146
147 #define TG3_RX_STD_MAP_SZ               TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
148 #define TG3_RX_JMB_MAP_SZ               TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
149
150 #define TG3_RX_STD_BUFF_RING_SIZE(tp) \
151         (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
152
153 #define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
154         (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
155
156 /* Due to a hardware bug, the 5701 can only DMA to memory addresses
157  * that are at least dword aligned when used in PCIX mode.  The driver
158  * works around this bug by double copying the packet.  This workaround
159  * is built into the normal double copy length check for efficiency.
160  *
161  * However, the double copy is only necessary on those architectures
162  * where unaligned memory accesses are inefficient.  For those architectures
163  * where unaligned memory accesses incur little penalty, we can reintegrate
164  * the 5701 in the normal rx path.  Doing so saves a device structure
165  * dereference by hardcoding the double copy threshold in place.
166  */
167 #define TG3_RX_COPY_THRESHOLD           256
168 #if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
169         #define TG3_RX_COPY_THRESH(tp)  TG3_RX_COPY_THRESHOLD
170 #else
171         #define TG3_RX_COPY_THRESH(tp)  ((tp)->rx_copy_thresh)
172 #endif
173
174 /* minimum number of free TX descriptors required to wake up TX process */
175 #define TG3_TX_WAKEUP_THRESH(tnapi)             ((tnapi)->tx_pending / 4)
176
177 #define TG3_RAW_IP_ALIGN 2
178
179 /* number of ETHTOOL_GSTATS u64's */
180 #define TG3_NUM_STATS           (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
181
182 #define TG3_NUM_TEST            6
183
184 #define TG3_FW_UPDATE_TIMEOUT_SEC       5
185
186 #define FIRMWARE_TG3            "tigon/tg3.bin"
187 #define FIRMWARE_TG3TSO         "tigon/tg3_tso.bin"
188 #define FIRMWARE_TG3TSO5        "tigon/tg3_tso5.bin"
189
190 static char version[] __devinitdata =
191         DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
192
193 MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
194 MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
195 MODULE_LICENSE("GPL");
196 MODULE_VERSION(DRV_MODULE_VERSION);
197 MODULE_FIRMWARE(FIRMWARE_TG3);
198 MODULE_FIRMWARE(FIRMWARE_TG3TSO);
199 MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
200
201 static int tg3_debug = -1;      /* -1 == use TG3_DEF_MSG_ENABLE as value */
202 module_param(tg3_debug, int, 0);
203 MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
204
205 static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
206         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
207         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
208         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
209         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
210         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
211         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
212         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
213         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
214         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
215         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
216         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
217         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
218         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
219         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
220         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
221         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
222         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
223         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
224         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
225         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
226         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
227         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
228         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
229         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
230         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
231         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
232         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
233         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
234         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
235         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
236         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
237         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
238         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
239         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
240         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
241         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
242         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
243         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
244         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
245         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
246         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
247         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
248         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
249         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
250         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
251         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
252         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
253         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
254         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
255         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
256         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
257         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
258         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
259         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
260         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
261         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
262         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
263         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
264         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
265         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
266         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
267         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
268         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
269         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
270         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
271         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
272         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
273         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
274         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
275         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
276         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
277         {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
278         {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
279         {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
280         {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
281         {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
282         {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
283         {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
284         {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
285         {}
286 };
287
288 MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
289
290 static const struct {
291         const char string[ETH_GSTRING_LEN];
292 } ethtool_stats_keys[TG3_NUM_STATS] = {
293         { "rx_octets" },
294         { "rx_fragments" },
295         { "rx_ucast_packets" },
296         { "rx_mcast_packets" },
297         { "rx_bcast_packets" },
298         { "rx_fcs_errors" },
299         { "rx_align_errors" },
300         { "rx_xon_pause_rcvd" },
301         { "rx_xoff_pause_rcvd" },
302         { "rx_mac_ctrl_rcvd" },
303         { "rx_xoff_entered" },
304         { "rx_frame_too_long_errors" },
305         { "rx_jabbers" },
306         { "rx_undersize_packets" },
307         { "rx_in_length_errors" },
308         { "rx_out_length_errors" },
309         { "rx_64_or_less_octet_packets" },
310         { "rx_65_to_127_octet_packets" },
311         { "rx_128_to_255_octet_packets" },
312         { "rx_256_to_511_octet_packets" },
313         { "rx_512_to_1023_octet_packets" },
314         { "rx_1024_to_1522_octet_packets" },
315         { "rx_1523_to_2047_octet_packets" },
316         { "rx_2048_to_4095_octet_packets" },
317         { "rx_4096_to_8191_octet_packets" },
318         { "rx_8192_to_9022_octet_packets" },
319
320         { "tx_octets" },
321         { "tx_collisions" },
322
323         { "tx_xon_sent" },
324         { "tx_xoff_sent" },
325         { "tx_flow_control" },
326         { "tx_mac_errors" },
327         { "tx_single_collisions" },
328         { "tx_mult_collisions" },
329         { "tx_deferred" },
330         { "tx_excessive_collisions" },
331         { "tx_late_collisions" },
332         { "tx_collide_2times" },
333         { "tx_collide_3times" },
334         { "tx_collide_4times" },
335         { "tx_collide_5times" },
336         { "tx_collide_6times" },
337         { "tx_collide_7times" },
338         { "tx_collide_8times" },
339         { "tx_collide_9times" },
340         { "tx_collide_10times" },
341         { "tx_collide_11times" },
342         { "tx_collide_12times" },
343         { "tx_collide_13times" },
344         { "tx_collide_14times" },
345         { "tx_collide_15times" },
346         { "tx_ucast_packets" },
347         { "tx_mcast_packets" },
348         { "tx_bcast_packets" },
349         { "tx_carrier_sense_errors" },
350         { "tx_discards" },
351         { "tx_errors" },
352
353         { "dma_writeq_full" },
354         { "dma_write_prioq_full" },
355         { "rxbds_empty" },
356         { "rx_discards" },
357         { "rx_errors" },
358         { "rx_threshold_hit" },
359
360         { "dma_readq_full" },
361         { "dma_read_prioq_full" },
362         { "tx_comp_queue_full" },
363
364         { "ring_set_send_prod_index" },
365         { "ring_status_update" },
366         { "nic_irqs" },
367         { "nic_avoided_irqs" },
368         { "nic_tx_threshold_hit" }
369 };
370
371 static const struct {
372         const char string[ETH_GSTRING_LEN];
373 } ethtool_test_keys[TG3_NUM_TEST] = {
374         { "nvram test     (online) " },
375         { "link test      (online) " },
376         { "register test  (offline)" },
377         { "memory test    (offline)" },
378         { "loopback test  (offline)" },
379         { "interrupt test (offline)" },
380 };
381
382 static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
383 {
384         writel(val, tp->regs + off);
385 }
386
387 static u32 tg3_read32(struct tg3 *tp, u32 off)
388 {
389         return readl(tp->regs + off);
390 }
391
392 static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
393 {
394         writel(val, tp->aperegs + off);
395 }
396
397 static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
398 {
399         return readl(tp->aperegs + off);
400 }
401
402 static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
403 {
404         unsigned long flags;
405
406         spin_lock_irqsave(&tp->indirect_lock, flags);
407         pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
408         pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
409         spin_unlock_irqrestore(&tp->indirect_lock, flags);
410 }
411
412 static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
413 {
414         writel(val, tp->regs + off);
415         readl(tp->regs + off);
416 }
417
418 static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
419 {
420         unsigned long flags;
421         u32 val;
422
423         spin_lock_irqsave(&tp->indirect_lock, flags);
424         pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
425         pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
426         spin_unlock_irqrestore(&tp->indirect_lock, flags);
427         return val;
428 }
429
430 static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
431 {
432         unsigned long flags;
433
434         if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
435                 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
436                                        TG3_64BIT_REG_LOW, val);
437                 return;
438         }
439         if (off == TG3_RX_STD_PROD_IDX_REG) {
440                 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
441                                        TG3_64BIT_REG_LOW, val);
442                 return;
443         }
444
445         spin_lock_irqsave(&tp->indirect_lock, flags);
446         pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
447         pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
448         spin_unlock_irqrestore(&tp->indirect_lock, flags);
449
450         /* In indirect mode when disabling interrupts, we also need
451          * to clear the interrupt bit in the GRC local ctrl register.
452          */
453         if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
454             (val == 0x1)) {
455                 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
456                                        tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
457         }
458 }
459
460 static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
461 {
462         unsigned long flags;
463         u32 val;
464
465         spin_lock_irqsave(&tp->indirect_lock, flags);
466         pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
467         pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
468         spin_unlock_irqrestore(&tp->indirect_lock, flags);
469         return val;
470 }
471
472 /* usec_wait specifies the wait time in usec when writing to certain registers
473  * where it is unsafe to read back the register without some delay.
474  * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
475  * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
476  */
477 static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
478 {
479         if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
480             (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
481                 /* Non-posted methods */
482                 tp->write32(tp, off, val);
483         else {
484                 /* Posted method */
485                 tg3_write32(tp, off, val);
486                 if (usec_wait)
487                         udelay(usec_wait);
488                 tp->read32(tp, off);
489         }
490         /* Wait again after the read for the posted method to guarantee that
491          * the wait time is met.
492          */
493         if (usec_wait)
494                 udelay(usec_wait);
495 }
496
497 static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
498 {
499         tp->write32_mbox(tp, off, val);
500         if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
501             !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
502                 tp->read32_mbox(tp, off);
503 }
504
505 static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
506 {
507         void __iomem *mbox = tp->regs + off;
508         writel(val, mbox);
509         if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
510                 writel(val, mbox);
511         if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
512                 readl(mbox);
513 }
514
515 static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
516 {
517         return readl(tp->regs + off + GRCMBOX_BASE);
518 }
519
520 static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
521 {
522         writel(val, tp->regs + off + GRCMBOX_BASE);
523 }
524
525 #define tw32_mailbox(reg, val)          tp->write32_mbox(tp, reg, val)
526 #define tw32_mailbox_f(reg, val)        tw32_mailbox_flush(tp, (reg), (val))
527 #define tw32_rx_mbox(reg, val)          tp->write32_rx_mbox(tp, reg, val)
528 #define tw32_tx_mbox(reg, val)          tp->write32_tx_mbox(tp, reg, val)
529 #define tr32_mailbox(reg)               tp->read32_mbox(tp, reg)
530
531 #define tw32(reg, val)                  tp->write32(tp, reg, val)
532 #define tw32_f(reg, val)                _tw32_flush(tp, (reg), (val), 0)
533 #define tw32_wait_f(reg, val, us)       _tw32_flush(tp, (reg), (val), (us))
534 #define tr32(reg)                       tp->read32(tp, reg)
535
536 static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
537 {
538         unsigned long flags;
539
540         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
541             (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
542                 return;
543
544         spin_lock_irqsave(&tp->indirect_lock, flags);
545         if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
546                 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
547                 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
548
549                 /* Always leave this as zero. */
550                 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
551         } else {
552                 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
553                 tw32_f(TG3PCI_MEM_WIN_DATA, val);
554
555                 /* Always leave this as zero. */
556                 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
557         }
558         spin_unlock_irqrestore(&tp->indirect_lock, flags);
559 }
560
561 static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
562 {
563         unsigned long flags;
564
565         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
566             (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
567                 *val = 0;
568                 return;
569         }
570
571         spin_lock_irqsave(&tp->indirect_lock, flags);
572         if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
573                 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
574                 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
575
576                 /* Always leave this as zero. */
577                 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
578         } else {
579                 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
580                 *val = tr32(TG3PCI_MEM_WIN_DATA);
581
582                 /* Always leave this as zero. */
583                 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
584         }
585         spin_unlock_irqrestore(&tp->indirect_lock, flags);
586 }
587
588 static void tg3_ape_lock_init(struct tg3 *tp)
589 {
590         int i;
591         u32 regbase;
592
593         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
594                 regbase = TG3_APE_LOCK_GRANT;
595         else
596                 regbase = TG3_APE_PER_LOCK_GRANT;
597
598         /* Make sure the driver hasn't any stale locks. */
599         for (i = 0; i < 8; i++)
600                 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
601 }
602
603 static int tg3_ape_lock(struct tg3 *tp, int locknum)
604 {
605         int i, off;
606         int ret = 0;
607         u32 status, req, gnt;
608
609         if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
610                 return 0;
611
612         switch (locknum) {
613         case TG3_APE_LOCK_GRC:
614         case TG3_APE_LOCK_MEM:
615                 break;
616         default:
617                 return -EINVAL;
618         }
619
620         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
621                 req = TG3_APE_LOCK_REQ;
622                 gnt = TG3_APE_LOCK_GRANT;
623         } else {
624                 req = TG3_APE_PER_LOCK_REQ;
625                 gnt = TG3_APE_PER_LOCK_GRANT;
626         }
627
628         off = 4 * locknum;
629
630         tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
631
632         /* Wait for up to 1 millisecond to acquire lock. */
633         for (i = 0; i < 100; i++) {
634                 status = tg3_ape_read32(tp, gnt + off);
635                 if (status == APE_LOCK_GRANT_DRIVER)
636                         break;
637                 udelay(10);
638         }
639
640         if (status != APE_LOCK_GRANT_DRIVER) {
641                 /* Revoke the lock request. */
642                 tg3_ape_write32(tp, gnt + off,
643                                 APE_LOCK_GRANT_DRIVER);
644
645                 ret = -EBUSY;
646         }
647
648         return ret;
649 }
650
651 static void tg3_ape_unlock(struct tg3 *tp, int locknum)
652 {
653         u32 gnt;
654
655         if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
656                 return;
657
658         switch (locknum) {
659         case TG3_APE_LOCK_GRC:
660         case TG3_APE_LOCK_MEM:
661                 break;
662         default:
663                 return;
664         }
665
666         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
667                 gnt = TG3_APE_LOCK_GRANT;
668         else
669                 gnt = TG3_APE_PER_LOCK_GRANT;
670
671         tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
672 }
673
674 static void tg3_disable_ints(struct tg3 *tp)
675 {
676         int i;
677
678         tw32(TG3PCI_MISC_HOST_CTRL,
679              (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
680         for (i = 0; i < tp->irq_max; i++)
681                 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
682 }
683
684 static void tg3_enable_ints(struct tg3 *tp)
685 {
686         int i;
687
688         tp->irq_sync = 0;
689         wmb();
690
691         tw32(TG3PCI_MISC_HOST_CTRL,
692              (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
693
694         tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
695         for (i = 0; i < tp->irq_cnt; i++) {
696                 struct tg3_napi *tnapi = &tp->napi[i];
697
698                 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
699                 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
700                         tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
701
702                 tp->coal_now |= tnapi->coal_now;
703         }
704
705         /* Force an initial interrupt */
706         if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
707             (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
708                 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
709         else
710                 tw32(HOSTCC_MODE, tp->coal_now);
711
712         tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
713 }
714
715 static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
716 {
717         struct tg3 *tp = tnapi->tp;
718         struct tg3_hw_status *sblk = tnapi->hw_status;
719         unsigned int work_exists = 0;
720
721         /* check for phy events */
722         if (!(tp->tg3_flags &
723               (TG3_FLAG_USE_LINKCHG_REG |
724                TG3_FLAG_POLL_SERDES))) {
725                 if (sblk->status & SD_STATUS_LINK_CHG)
726                         work_exists = 1;
727         }
728         /* check for RX/TX work to do */
729         if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
730             *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
731                 work_exists = 1;
732
733         return work_exists;
734 }
735
736 /* tg3_int_reenable
737  *  similar to tg3_enable_ints, but it accurately determines whether there
738  *  is new work pending and can return without flushing the PIO write
739  *  which reenables interrupts
740  */
741 static void tg3_int_reenable(struct tg3_napi *tnapi)
742 {
743         struct tg3 *tp = tnapi->tp;
744
745         tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
746         mmiowb();
747
748         /* When doing tagged status, this work check is unnecessary.
749          * The last_tag we write above tells the chip which piece of
750          * work we've completed.
751          */
752         if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
753             tg3_has_work(tnapi))
754                 tw32(HOSTCC_MODE, tp->coalesce_mode |
755                      HOSTCC_MODE_ENABLE | tnapi->coal_now);
756 }
757
758 static void tg3_switch_clocks(struct tg3 *tp)
759 {
760         u32 clock_ctrl;
761         u32 orig_clock_ctrl;
762
763         if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
764             (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
765                 return;
766
767         clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
768
769         orig_clock_ctrl = clock_ctrl;
770         clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
771                        CLOCK_CTRL_CLKRUN_OENABLE |
772                        0x1f);
773         tp->pci_clock_ctrl = clock_ctrl;
774
775         if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
776                 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
777                         tw32_wait_f(TG3PCI_CLOCK_CTRL,
778                                     clock_ctrl | CLOCK_CTRL_625_CORE, 40);
779                 }
780         } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
781                 tw32_wait_f(TG3PCI_CLOCK_CTRL,
782                             clock_ctrl |
783                             (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
784                             40);
785                 tw32_wait_f(TG3PCI_CLOCK_CTRL,
786                             clock_ctrl | (CLOCK_CTRL_ALTCLK),
787                             40);
788         }
789         tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
790 }
791
792 #define PHY_BUSY_LOOPS  5000
793
794 static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
795 {
796         u32 frame_val;
797         unsigned int loops;
798         int ret;
799
800         if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
801                 tw32_f(MAC_MI_MODE,
802                      (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
803                 udelay(80);
804         }
805
806         *val = 0x0;
807
808         frame_val  = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
809                       MI_COM_PHY_ADDR_MASK);
810         frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
811                       MI_COM_REG_ADDR_MASK);
812         frame_val |= (MI_COM_CMD_READ | MI_COM_START);
813
814         tw32_f(MAC_MI_COM, frame_val);
815
816         loops = PHY_BUSY_LOOPS;
817         while (loops != 0) {
818                 udelay(10);
819                 frame_val = tr32(MAC_MI_COM);
820
821                 if ((frame_val & MI_COM_BUSY) == 0) {
822                         udelay(5);
823                         frame_val = tr32(MAC_MI_COM);
824                         break;
825                 }
826                 loops -= 1;
827         }
828
829         ret = -EBUSY;
830         if (loops != 0) {
831                 *val = frame_val & MI_COM_DATA_MASK;
832                 ret = 0;
833         }
834
835         if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
836                 tw32_f(MAC_MI_MODE, tp->mi_mode);
837                 udelay(80);
838         }
839
840         return ret;
841 }
842
843 static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
844 {
845         u32 frame_val;
846         unsigned int loops;
847         int ret;
848
849         if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
850             (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
851                 return 0;
852
853         if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
854                 tw32_f(MAC_MI_MODE,
855                      (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
856                 udelay(80);
857         }
858
859         frame_val  = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
860                       MI_COM_PHY_ADDR_MASK);
861         frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
862                       MI_COM_REG_ADDR_MASK);
863         frame_val |= (val & MI_COM_DATA_MASK);
864         frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
865
866         tw32_f(MAC_MI_COM, frame_val);
867
868         loops = PHY_BUSY_LOOPS;
869         while (loops != 0) {
870                 udelay(10);
871                 frame_val = tr32(MAC_MI_COM);
872                 if ((frame_val & MI_COM_BUSY) == 0) {
873                         udelay(5);
874                         frame_val = tr32(MAC_MI_COM);
875                         break;
876                 }
877                 loops -= 1;
878         }
879
880         ret = -EBUSY;
881         if (loops != 0)
882                 ret = 0;
883
884         if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
885                 tw32_f(MAC_MI_MODE, tp->mi_mode);
886                 udelay(80);
887         }
888
889         return ret;
890 }
891
892 static int tg3_bmcr_reset(struct tg3 *tp)
893 {
894         u32 phy_control;
895         int limit, err;
896
897         /* OK, reset it, and poll the BMCR_RESET bit until it
898          * clears or we time out.
899          */
900         phy_control = BMCR_RESET;
901         err = tg3_writephy(tp, MII_BMCR, phy_control);
902         if (err != 0)
903                 return -EBUSY;
904
905         limit = 5000;
906         while (limit--) {
907                 err = tg3_readphy(tp, MII_BMCR, &phy_control);
908                 if (err != 0)
909                         return -EBUSY;
910
911                 if ((phy_control & BMCR_RESET) == 0) {
912                         udelay(40);
913                         break;
914                 }
915                 udelay(10);
916         }
917         if (limit < 0)
918                 return -EBUSY;
919
920         return 0;
921 }
922
923 static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
924 {
925         struct tg3 *tp = bp->priv;
926         u32 val;
927
928         spin_lock_bh(&tp->lock);
929
930         if (tg3_readphy(tp, reg, &val))
931                 val = -EIO;
932
933         spin_unlock_bh(&tp->lock);
934
935         return val;
936 }
937
938 static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
939 {
940         struct tg3 *tp = bp->priv;
941         u32 ret = 0;
942
943         spin_lock_bh(&tp->lock);
944
945         if (tg3_writephy(tp, reg, val))
946                 ret = -EIO;
947
948         spin_unlock_bh(&tp->lock);
949
950         return ret;
951 }
952
953 static int tg3_mdio_reset(struct mii_bus *bp)
954 {
955         return 0;
956 }
957
958 static void tg3_mdio_config_5785(struct tg3 *tp)
959 {
960         u32 val;
961         struct phy_device *phydev;
962
963         phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
964         switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
965         case PHY_ID_BCM50610:
966         case PHY_ID_BCM50610M:
967                 val = MAC_PHYCFG2_50610_LED_MODES;
968                 break;
969         case PHY_ID_BCMAC131:
970                 val = MAC_PHYCFG2_AC131_LED_MODES;
971                 break;
972         case PHY_ID_RTL8211C:
973                 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
974                 break;
975         case PHY_ID_RTL8201E:
976                 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
977                 break;
978         default:
979                 return;
980         }
981
982         if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
983                 tw32(MAC_PHYCFG2, val);
984
985                 val = tr32(MAC_PHYCFG1);
986                 val &= ~(MAC_PHYCFG1_RGMII_INT |
987                          MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
988                 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
989                 tw32(MAC_PHYCFG1, val);
990
991                 return;
992         }
993
994         if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
995                 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
996                        MAC_PHYCFG2_FMODE_MASK_MASK |
997                        MAC_PHYCFG2_GMODE_MASK_MASK |
998                        MAC_PHYCFG2_ACT_MASK_MASK   |
999                        MAC_PHYCFG2_QUAL_MASK_MASK |
1000                        MAC_PHYCFG2_INBAND_ENABLE;
1001
1002         tw32(MAC_PHYCFG2, val);
1003
1004         val = tr32(MAC_PHYCFG1);
1005         val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1006                  MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
1007         if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
1008                 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1009                         val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1010                 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1011                         val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1012         }
1013         val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1014                MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1015         tw32(MAC_PHYCFG1, val);
1016
1017         val = tr32(MAC_EXT_RGMII_MODE);
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                  MAC_RGMII_MODE_TX_ENABLE |
1023                  MAC_RGMII_MODE_TX_LOWPWR |
1024                  MAC_RGMII_MODE_TX_RESET);
1025         if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
1026                 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1027                         val |= MAC_RGMII_MODE_RX_INT_B |
1028                                MAC_RGMII_MODE_RX_QUALITY |
1029                                MAC_RGMII_MODE_RX_ACTIVITY |
1030                                MAC_RGMII_MODE_RX_ENG_DET;
1031                 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1032                         val |= MAC_RGMII_MODE_TX_ENABLE |
1033                                MAC_RGMII_MODE_TX_LOWPWR |
1034                                MAC_RGMII_MODE_TX_RESET;
1035         }
1036         tw32(MAC_EXT_RGMII_MODE, val);
1037 }
1038
1039 static void tg3_mdio_start(struct tg3 *tp)
1040 {
1041         tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1042         tw32_f(MAC_MI_MODE, tp->mi_mode);
1043         udelay(80);
1044
1045         if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1046             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1047                 tg3_mdio_config_5785(tp);
1048 }
1049
1050 static int tg3_mdio_init(struct tg3 *tp)
1051 {
1052         int i;
1053         u32 reg;
1054         struct phy_device *phydev;
1055
1056         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1057             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
1058                 u32 is_serdes;
1059
1060                 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
1061
1062                 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1063                         is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1064                 else
1065                         is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1066                                     TG3_CPMU_PHY_STRAP_IS_SERDES;
1067                 if (is_serdes)
1068                         tp->phy_addr += 7;
1069         } else
1070                 tp->phy_addr = TG3_PHY_MII_ADDR;
1071
1072         tg3_mdio_start(tp);
1073
1074         if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1075             (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1076                 return 0;
1077
1078         tp->mdio_bus = mdiobus_alloc();
1079         if (tp->mdio_bus == NULL)
1080                 return -ENOMEM;
1081
1082         tp->mdio_bus->name     = "tg3 mdio bus";
1083         snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
1084                  (tp->pdev->bus->number << 8) | tp->pdev->devfn);
1085         tp->mdio_bus->priv     = tp;
1086         tp->mdio_bus->parent   = &tp->pdev->dev;
1087         tp->mdio_bus->read     = &tg3_mdio_read;
1088         tp->mdio_bus->write    = &tg3_mdio_write;
1089         tp->mdio_bus->reset    = &tg3_mdio_reset;
1090         tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
1091         tp->mdio_bus->irq      = &tp->mdio_irq[0];
1092
1093         for (i = 0; i < PHY_MAX_ADDR; i++)
1094                 tp->mdio_bus->irq[i] = PHY_POLL;
1095
1096         /* The bus registration will look for all the PHYs on the mdio bus.
1097          * Unfortunately, it does not ensure the PHY is powered up before
1098          * accessing the PHY ID registers.  A chip reset is the
1099          * quickest way to bring the device back to an operational state..
1100          */
1101         if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1102                 tg3_bmcr_reset(tp);
1103
1104         i = mdiobus_register(tp->mdio_bus);
1105         if (i) {
1106                 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
1107                 mdiobus_free(tp->mdio_bus);
1108                 return i;
1109         }
1110
1111         phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1112
1113         if (!phydev || !phydev->drv) {
1114                 dev_warn(&tp->pdev->dev, "No PHY devices\n");
1115                 mdiobus_unregister(tp->mdio_bus);
1116                 mdiobus_free(tp->mdio_bus);
1117                 return -ENODEV;
1118         }
1119
1120         switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
1121         case PHY_ID_BCM57780:
1122                 phydev->interface = PHY_INTERFACE_MODE_GMII;
1123                 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
1124                 break;
1125         case PHY_ID_BCM50610:
1126         case PHY_ID_BCM50610M:
1127                 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
1128                                      PHY_BRCM_RX_REFCLK_UNUSED |
1129                                      PHY_BRCM_DIS_TXCRXC_NOENRGY |
1130                                      PHY_BRCM_AUTO_PWRDWN_ENABLE;
1131                 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
1132                         phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1133                 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1134                         phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1135                 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1136                         phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
1137                 /* fallthru */
1138         case PHY_ID_RTL8211C:
1139                 phydev->interface = PHY_INTERFACE_MODE_RGMII;
1140                 break;
1141         case PHY_ID_RTL8201E:
1142         case PHY_ID_BCMAC131:
1143                 phydev->interface = PHY_INTERFACE_MODE_MII;
1144                 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
1145                 tp->phy_flags |= TG3_PHYFLG_IS_FET;
1146                 break;
1147         }
1148
1149         tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1150
1151         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1152                 tg3_mdio_config_5785(tp);
1153
1154         return 0;
1155 }
1156
1157 static void tg3_mdio_fini(struct tg3 *tp)
1158 {
1159         if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1160                 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
1161                 mdiobus_unregister(tp->mdio_bus);
1162                 mdiobus_free(tp->mdio_bus);
1163         }
1164 }
1165
1166 static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1167 {
1168         int err;
1169
1170         err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1171         if (err)
1172                 goto done;
1173
1174         err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1175         if (err)
1176                 goto done;
1177
1178         err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1179                            MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1180         if (err)
1181                 goto done;
1182
1183         err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1184
1185 done:
1186         return err;
1187 }
1188
1189 static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1190 {
1191         int err;
1192
1193         err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1194         if (err)
1195                 goto done;
1196
1197         err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1198         if (err)
1199                 goto done;
1200
1201         err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1202                            MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1203         if (err)
1204                 goto done;
1205
1206         err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1207
1208 done:
1209         return err;
1210 }
1211
1212 /* tp->lock is held. */
1213 static inline void tg3_generate_fw_event(struct tg3 *tp)
1214 {
1215         u32 val;
1216
1217         val = tr32(GRC_RX_CPU_EVENT);
1218         val |= GRC_RX_CPU_DRIVER_EVENT;
1219         tw32_f(GRC_RX_CPU_EVENT, val);
1220
1221         tp->last_event_jiffies = jiffies;
1222 }
1223
1224 #define TG3_FW_EVENT_TIMEOUT_USEC 2500
1225
1226 /* tp->lock is held. */
1227 static void tg3_wait_for_event_ack(struct tg3 *tp)
1228 {
1229         int i;
1230         unsigned int delay_cnt;
1231         long time_remain;
1232
1233         /* If enough time has passed, no wait is necessary. */
1234         time_remain = (long)(tp->last_event_jiffies + 1 +
1235                       usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1236                       (long)jiffies;
1237         if (time_remain < 0)
1238                 return;
1239
1240         /* Check if we can shorten the wait time. */
1241         delay_cnt = jiffies_to_usecs(time_remain);
1242         if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1243                 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1244         delay_cnt = (delay_cnt >> 3) + 1;
1245
1246         for (i = 0; i < delay_cnt; i++) {
1247                 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1248                         break;
1249                 udelay(8);
1250         }
1251 }
1252
1253 /* tp->lock is held. */
1254 static void tg3_ump_link_report(struct tg3 *tp)
1255 {
1256         u32 reg;
1257         u32 val;
1258
1259         if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1260             !(tp->tg3_flags  & TG3_FLAG_ENABLE_ASF))
1261                 return;
1262
1263         tg3_wait_for_event_ack(tp);
1264
1265         tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1266
1267         tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1268
1269         val = 0;
1270         if (!tg3_readphy(tp, MII_BMCR, &reg))
1271                 val = reg << 16;
1272         if (!tg3_readphy(tp, MII_BMSR, &reg))
1273                 val |= (reg & 0xffff);
1274         tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1275
1276         val = 0;
1277         if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1278                 val = reg << 16;
1279         if (!tg3_readphy(tp, MII_LPA, &reg))
1280                 val |= (reg & 0xffff);
1281         tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1282
1283         val = 0;
1284         if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
1285                 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1286                         val = reg << 16;
1287                 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1288                         val |= (reg & 0xffff);
1289         }
1290         tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1291
1292         if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1293                 val = reg << 16;
1294         else
1295                 val = 0;
1296         tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1297
1298         tg3_generate_fw_event(tp);
1299 }
1300
1301 static void tg3_link_report(struct tg3 *tp)
1302 {
1303         if (!netif_carrier_ok(tp->dev)) {
1304                 netif_info(tp, link, tp->dev, "Link is down\n");
1305                 tg3_ump_link_report(tp);
1306         } else if (netif_msg_link(tp)) {
1307                 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1308                             (tp->link_config.active_speed == SPEED_1000 ?
1309                              1000 :
1310                              (tp->link_config.active_speed == SPEED_100 ?
1311                               100 : 10)),
1312                             (tp->link_config.active_duplex == DUPLEX_FULL ?
1313                              "full" : "half"));
1314
1315                 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1316                             (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1317                             "on" : "off",
1318                             (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1319                             "on" : "off");
1320                 tg3_ump_link_report(tp);
1321         }
1322 }
1323
1324 static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1325 {
1326         u16 miireg;
1327
1328         if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
1329                 miireg = ADVERTISE_PAUSE_CAP;
1330         else if (flow_ctrl & FLOW_CTRL_TX)
1331                 miireg = ADVERTISE_PAUSE_ASYM;
1332         else if (flow_ctrl & FLOW_CTRL_RX)
1333                 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1334         else
1335                 miireg = 0;
1336
1337         return miireg;
1338 }
1339
1340 static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1341 {
1342         u16 miireg;
1343
1344         if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
1345                 miireg = ADVERTISE_1000XPAUSE;
1346         else if (flow_ctrl & FLOW_CTRL_TX)
1347                 miireg = ADVERTISE_1000XPSE_ASYM;
1348         else if (flow_ctrl & FLOW_CTRL_RX)
1349                 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1350         else
1351                 miireg = 0;
1352
1353         return miireg;
1354 }
1355
1356 static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1357 {
1358         u8 cap = 0;
1359
1360         if (lcladv & ADVERTISE_1000XPAUSE) {
1361                 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1362                         if (rmtadv & LPA_1000XPAUSE)
1363                                 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1364                         else if (rmtadv & LPA_1000XPAUSE_ASYM)
1365                                 cap = FLOW_CTRL_RX;
1366                 } else {
1367                         if (rmtadv & LPA_1000XPAUSE)
1368                                 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1369                 }
1370         } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1371                 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
1372                         cap = FLOW_CTRL_TX;
1373         }
1374
1375         return cap;
1376 }
1377
1378 static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
1379 {
1380         u8 autoneg;
1381         u8 flowctrl = 0;
1382         u32 old_rx_mode = tp->rx_mode;
1383         u32 old_tx_mode = tp->tx_mode;
1384
1385         if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
1386                 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
1387         else
1388                 autoneg = tp->link_config.autoneg;
1389
1390         if (autoneg == AUTONEG_ENABLE &&
1391             (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1392                 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
1393                         flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
1394                 else
1395                         flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
1396         } else
1397                 flowctrl = tp->link_config.flowctrl;
1398
1399         tp->link_config.active_flowctrl = flowctrl;
1400
1401         if (flowctrl & FLOW_CTRL_RX)
1402                 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1403         else
1404                 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1405
1406         if (old_rx_mode != tp->rx_mode)
1407                 tw32_f(MAC_RX_MODE, tp->rx_mode);
1408
1409         if (flowctrl & FLOW_CTRL_TX)
1410                 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1411         else
1412                 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1413
1414         if (old_tx_mode != tp->tx_mode)
1415                 tw32_f(MAC_TX_MODE, tp->tx_mode);
1416 }
1417
1418 static void tg3_adjust_link(struct net_device *dev)
1419 {
1420         u8 oldflowctrl, linkmesg = 0;
1421         u32 mac_mode, lcl_adv, rmt_adv;
1422         struct tg3 *tp = netdev_priv(dev);
1423         struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1424
1425         spin_lock_bh(&tp->lock);
1426
1427         mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1428                                     MAC_MODE_HALF_DUPLEX);
1429
1430         oldflowctrl = tp->link_config.active_flowctrl;
1431
1432         if (phydev->link) {
1433                 lcl_adv = 0;
1434                 rmt_adv = 0;
1435
1436                 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1437                         mac_mode |= MAC_MODE_PORT_MODE_MII;
1438                 else if (phydev->speed == SPEED_1000 ||
1439                          GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
1440                         mac_mode |= MAC_MODE_PORT_MODE_GMII;
1441                 else
1442                         mac_mode |= MAC_MODE_PORT_MODE_MII;
1443
1444                 if (phydev->duplex == DUPLEX_HALF)
1445                         mac_mode |= MAC_MODE_HALF_DUPLEX;
1446                 else {
1447                         lcl_adv = tg3_advert_flowctrl_1000T(
1448                                   tp->link_config.flowctrl);
1449
1450                         if (phydev->pause)
1451                                 rmt_adv = LPA_PAUSE_CAP;
1452                         if (phydev->asym_pause)
1453                                 rmt_adv |= LPA_PAUSE_ASYM;
1454                 }
1455
1456                 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1457         } else
1458                 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1459
1460         if (mac_mode != tp->mac_mode) {
1461                 tp->mac_mode = mac_mode;
1462                 tw32_f(MAC_MODE, tp->mac_mode);
1463                 udelay(40);
1464         }
1465
1466         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1467                 if (phydev->speed == SPEED_10)
1468                         tw32(MAC_MI_STAT,
1469                              MAC_MI_STAT_10MBPS_MODE |
1470                              MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1471                 else
1472                         tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1473         }
1474
1475         if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1476                 tw32(MAC_TX_LENGTHS,
1477                      ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1478                       (6 << TX_LENGTHS_IPG_SHIFT) |
1479                       (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1480         else
1481                 tw32(MAC_TX_LENGTHS,
1482                      ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1483                       (6 << TX_LENGTHS_IPG_SHIFT) |
1484                       (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1485
1486         if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1487             (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1488             phydev->speed != tp->link_config.active_speed ||
1489             phydev->duplex != tp->link_config.active_duplex ||
1490             oldflowctrl != tp->link_config.active_flowctrl)
1491                 linkmesg = 1;
1492
1493         tp->link_config.active_speed = phydev->speed;
1494         tp->link_config.active_duplex = phydev->duplex;
1495
1496         spin_unlock_bh(&tp->lock);
1497
1498         if (linkmesg)
1499                 tg3_link_report(tp);
1500 }
1501
1502 static int tg3_phy_init(struct tg3 *tp)
1503 {
1504         struct phy_device *phydev;
1505
1506         if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
1507                 return 0;
1508
1509         /* Bring the PHY back to a known state. */
1510         tg3_bmcr_reset(tp);
1511
1512         phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1513
1514         /* Attach the MAC to the PHY. */
1515         phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
1516                              phydev->dev_flags, phydev->interface);
1517         if (IS_ERR(phydev)) {
1518                 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
1519                 return PTR_ERR(phydev);
1520         }
1521
1522         /* Mask with MAC supported features. */
1523         switch (phydev->interface) {
1524         case PHY_INTERFACE_MODE_GMII:
1525         case PHY_INTERFACE_MODE_RGMII:
1526                 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
1527                         phydev->supported &= (PHY_GBIT_FEATURES |
1528                                               SUPPORTED_Pause |
1529                                               SUPPORTED_Asym_Pause);
1530                         break;
1531                 }
1532                 /* fallthru */
1533         case PHY_INTERFACE_MODE_MII:
1534                 phydev->supported &= (PHY_BASIC_FEATURES |
1535                                       SUPPORTED_Pause |
1536                                       SUPPORTED_Asym_Pause);
1537                 break;
1538         default:
1539                 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
1540                 return -EINVAL;
1541         }
1542
1543         tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
1544
1545         phydev->advertising = phydev->supported;
1546
1547         return 0;
1548 }
1549
1550 static void tg3_phy_start(struct tg3 *tp)
1551 {
1552         struct phy_device *phydev;
1553
1554         if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
1555                 return;
1556
1557         phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
1558
1559         if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1560                 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
1561                 phydev->speed = tp->link_config.orig_speed;
1562                 phydev->duplex = tp->link_config.orig_duplex;
1563                 phydev->autoneg = tp->link_config.orig_autoneg;
1564                 phydev->advertising = tp->link_config.orig_advertising;
1565         }
1566
1567         phy_start(phydev);
1568
1569         phy_start_aneg(phydev);
1570 }
1571
1572 static void tg3_phy_stop(struct tg3 *tp)
1573 {
1574         if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
1575                 return;
1576
1577         phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
1578 }
1579
1580 static void tg3_phy_fini(struct tg3 *tp)
1581 {
1582         if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
1583                 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
1584                 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
1585         }
1586 }
1587
1588 static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1589 {
1590         int err;
1591
1592         err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1593         if (!err)
1594                 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1595
1596         return err;
1597 }
1598
1599 static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1600 {
1601         int err;
1602
1603         err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1604         if (!err)
1605                 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1606
1607         return err;
1608 }
1609
1610 static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1611 {
1612         u32 phytest;
1613
1614         if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1615                 u32 phy;
1616
1617                 tg3_writephy(tp, MII_TG3_FET_TEST,
1618                              phytest | MII_TG3_FET_SHADOW_EN);
1619                 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1620                         if (enable)
1621                                 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1622                         else
1623                                 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1624                         tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1625                 }
1626                 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1627         }
1628 }
1629
1630 static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1631 {
1632         u32 reg;
1633
1634         if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1635             ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1636               GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
1637              (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
1638                 return;
1639
1640         if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
1641                 tg3_phy_fet_toggle_apd(tp, enable);
1642                 return;
1643         }
1644
1645         reg = MII_TG3_MISC_SHDW_WREN |
1646               MII_TG3_MISC_SHDW_SCR5_SEL |
1647               MII_TG3_MISC_SHDW_SCR5_LPED |
1648               MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1649               MII_TG3_MISC_SHDW_SCR5_SDTL |
1650               MII_TG3_MISC_SHDW_SCR5_C125OE;
1651         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1652                 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1653
1654         tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1655
1656
1657         reg = MII_TG3_MISC_SHDW_WREN |
1658               MII_TG3_MISC_SHDW_APD_SEL |
1659               MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1660         if (enable)
1661                 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1662
1663         tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1664 }
1665
1666 static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1667 {
1668         u32 phy;
1669
1670         if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1671             (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
1672                 return;
1673
1674         if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
1675                 u32 ephy;
1676
1677                 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1678                         u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1679
1680                         tg3_writephy(tp, MII_TG3_FET_TEST,
1681                                      ephy | MII_TG3_FET_SHADOW_EN);
1682                         if (!tg3_readphy(tp, reg, &phy)) {
1683                                 if (enable)
1684                                         phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1685                                 else
1686                                         phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1687                                 tg3_writephy(tp, reg, phy);
1688                         }
1689                         tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
1690                 }
1691         } else {
1692                 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1693                       MII_TG3_AUXCTL_SHDWSEL_MISC;
1694                 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1695                     !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1696                         if (enable)
1697                                 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1698                         else
1699                                 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1700                         phy |= MII_TG3_AUXCTL_MISC_WREN;
1701                         tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1702                 }
1703         }
1704 }
1705
1706 static void tg3_phy_set_wirespeed(struct tg3 *tp)
1707 {
1708         u32 val;
1709
1710         if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
1711                 return;
1712
1713         if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1714             !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1715                 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1716                              (val | (1 << 15) | (1 << 4)));
1717 }
1718
1719 static void tg3_phy_apply_otp(struct tg3 *tp)
1720 {
1721         u32 otp, phy;
1722
1723         if (!tp->phy_otp)
1724                 return;
1725
1726         otp = tp->phy_otp;
1727
1728         /* Enable SM_DSP clock and tx 6dB coding. */
1729         phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1730               MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1731               MII_TG3_AUXCTL_ACTL_TX_6DB;
1732         tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1733
1734         phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1735         phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1736         tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1737
1738         phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1739               ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1740         tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1741
1742         phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1743         phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1744         tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1745
1746         phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1747         tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1748
1749         phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1750         tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1751
1752         phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1753               ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1754         tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1755
1756         /* Turn off SM_DSP clock. */
1757         phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1758               MII_TG3_AUXCTL_ACTL_TX_6DB;
1759         tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1760 }
1761
1762 static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1763 {
1764         u32 val;
1765
1766         if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1767                 return;
1768
1769         tp->setlpicnt = 0;
1770
1771         if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1772             current_link_up == 1 &&
1773             tp->link_config.active_duplex == DUPLEX_FULL &&
1774             (tp->link_config.active_speed == SPEED_100 ||
1775              tp->link_config.active_speed == SPEED_1000)) {
1776                 u32 eeectl;
1777
1778                 if (tp->link_config.active_speed == SPEED_1000)
1779                         eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1780                 else
1781                         eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1782
1783                 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1784
1785                 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1786                                   TG3_CL45_D7_EEERES_STAT, &val);
1787
1788                 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1789                     val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
1790                         tp->setlpicnt = 2;
1791         }
1792
1793         if (!tp->setlpicnt) {
1794                 val = tr32(TG3_CPMU_EEE_MODE);
1795                 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1796         }
1797 }
1798
1799 static int tg3_wait_macro_done(struct tg3 *tp)
1800 {
1801         int limit = 100;
1802
1803         while (limit--) {
1804                 u32 tmp32;
1805
1806                 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
1807                         if ((tmp32 & 0x1000) == 0)
1808                                 break;
1809                 }
1810         }
1811         if (limit < 0)
1812                 return -EBUSY;
1813
1814         return 0;
1815 }
1816
1817 static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1818 {
1819         static const u32 test_pat[4][6] = {
1820         { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1821         { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1822         { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1823         { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1824         };
1825         int chan;
1826
1827         for (chan = 0; chan < 4; chan++) {
1828                 int i;
1829
1830                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1831                              (chan * 0x2000) | 0x0200);
1832                 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
1833
1834                 for (i = 0; i < 6; i++)
1835                         tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1836                                      test_pat[chan][i]);
1837
1838                 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
1839                 if (tg3_wait_macro_done(tp)) {
1840                         *resetp = 1;
1841                         return -EBUSY;
1842                 }
1843
1844                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1845                              (chan * 0x2000) | 0x0200);
1846                 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
1847                 if (tg3_wait_macro_done(tp)) {
1848                         *resetp = 1;
1849                         return -EBUSY;
1850                 }
1851
1852                 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
1853                 if (tg3_wait_macro_done(tp)) {
1854                         *resetp = 1;
1855                         return -EBUSY;
1856                 }
1857
1858                 for (i = 0; i < 6; i += 2) {
1859                         u32 low, high;
1860
1861                         if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1862                             tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1863                             tg3_wait_macro_done(tp)) {
1864                                 *resetp = 1;
1865                                 return -EBUSY;
1866                         }
1867                         low &= 0x7fff;
1868                         high &= 0x000f;
1869                         if (low != test_pat[chan][i] ||
1870                             high != test_pat[chan][i+1]) {
1871                                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1872                                 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1873                                 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1874
1875                                 return -EBUSY;
1876                         }
1877                 }
1878         }
1879
1880         return 0;
1881 }
1882
1883 static int tg3_phy_reset_chanpat(struct tg3 *tp)
1884 {
1885         int chan;
1886
1887         for (chan = 0; chan < 4; chan++) {
1888                 int i;
1889
1890                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1891                              (chan * 0x2000) | 0x0200);
1892                 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
1893                 for (i = 0; i < 6; i++)
1894                         tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1895                 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
1896                 if (tg3_wait_macro_done(tp))
1897                         return -EBUSY;
1898         }
1899
1900         return 0;
1901 }
1902
1903 static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1904 {
1905         u32 reg32, phy9_orig;
1906         int retries, do_phy_reset, err;
1907
1908         retries = 10;
1909         do_phy_reset = 1;
1910         do {
1911                 if (do_phy_reset) {
1912                         err = tg3_bmcr_reset(tp);
1913                         if (err)
1914                                 return err;
1915                         do_phy_reset = 0;
1916                 }
1917
1918                 /* Disable transmitter and interrupt.  */
1919                 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1920                         continue;
1921
1922                 reg32 |= 0x3000;
1923                 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1924
1925                 /* Set full-duplex, 1000 mbps.  */
1926                 tg3_writephy(tp, MII_BMCR,
1927                              BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1928
1929                 /* Set to master mode.  */
1930                 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1931                         continue;
1932
1933                 tg3_writephy(tp, MII_TG3_CTRL,
1934                              (MII_TG3_CTRL_AS_MASTER |
1935                               MII_TG3_CTRL_ENABLE_AS_MASTER));
1936
1937                 /* Enable SM_DSP_CLOCK and 6dB.  */
1938                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1939
1940                 /* Block the PHY control access.  */
1941                 tg3_phydsp_write(tp, 0x8005, 0x0800);
1942
1943                 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1944                 if (!err)
1945                         break;
1946         } while (--retries);
1947
1948         err = tg3_phy_reset_chanpat(tp);
1949         if (err)
1950                 return err;
1951
1952         tg3_phydsp_write(tp, 0x8005, 0x0000);
1953
1954         tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1955         tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
1956
1957         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1958             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1959                 /* Set Extended packet length bit for jumbo frames */
1960                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
1961         } else {
1962                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1963         }
1964
1965         tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1966
1967         if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1968                 reg32 &= ~0x3000;
1969                 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1970         } else if (!err)
1971                 err = -EBUSY;
1972
1973         return err;
1974 }
1975
1976 /* This will reset the tigon3 PHY if there is no valid
1977  * link unless the FORCE argument is non-zero.
1978  */
1979 static int tg3_phy_reset(struct tg3 *tp)
1980 {
1981         u32 val, cpmuctrl;
1982         int err;
1983
1984         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1985                 val = tr32(GRC_MISC_CFG);
1986                 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1987                 udelay(40);
1988         }
1989         err  = tg3_readphy(tp, MII_BMSR, &val);
1990         err |= tg3_readphy(tp, MII_BMSR, &val);
1991         if (err != 0)
1992                 return -EBUSY;
1993
1994         if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1995                 netif_carrier_off(tp->dev);
1996                 tg3_link_report(tp);
1997         }
1998
1999         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2000             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2001             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2002                 err = tg3_phy_reset_5703_4_5(tp);
2003                 if (err)
2004                         return err;
2005                 goto out;
2006         }
2007
2008         cpmuctrl = 0;
2009         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2010             GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2011                 cpmuctrl = tr32(TG3_CPMU_CTRL);
2012                 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2013                         tw32(TG3_CPMU_CTRL,
2014                              cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2015         }
2016
2017         err = tg3_bmcr_reset(tp);
2018         if (err)
2019                 return err;
2020
2021         if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
2022                 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2023                 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
2024
2025                 tw32(TG3_CPMU_CTRL, cpmuctrl);
2026         }
2027
2028         if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2029             GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
2030                 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2031                 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2032                     CPMU_LSPD_1000MB_MACCLK_12_5) {
2033                         val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2034                         udelay(40);
2035                         tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2036                 }
2037         }
2038
2039         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2040              GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
2041             (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
2042                 return 0;
2043
2044         tg3_phy_apply_otp(tp);
2045
2046         if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
2047                 tg3_phy_toggle_apd(tp, true);
2048         else
2049                 tg3_phy_toggle_apd(tp, false);
2050
2051 out:
2052         if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
2053                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2054                 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2055                 tg3_phydsp_write(tp, 0x000a, 0x0323);
2056                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2057         }
2058         if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
2059                 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2060                 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2061         }
2062         if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
2063                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2064                 tg3_phydsp_write(tp, 0x000a, 0x310b);
2065                 tg3_phydsp_write(tp, 0x201f, 0x9506);
2066                 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2067                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2068         } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
2069                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2070                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2071                 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2072                         tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2073                         tg3_writephy(tp, MII_TG3_TEST1,
2074                                      MII_TG3_TEST1_TRIM_EN | 0x4);
2075                 } else
2076                         tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2077                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2078         }
2079         /* Set Extended packet length bit (bit 14) on all chips that */
2080         /* support jumbo frames */
2081         if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2082                 /* Cannot do read-modify-write on 5401 */
2083                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2084         } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
2085                 /* Set bit 14 with read-modify-write to preserve other bits */
2086                 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
2087                     !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
2088                         tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
2089         }
2090
2091         /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2092          * jumbo frames transmission.
2093          */
2094         if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
2095                 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
2096                         tg3_writephy(tp, MII_TG3_EXT_CTRL,
2097                                      val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
2098         }
2099
2100         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2101                 /* adjust output voltage */
2102                 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
2103         }
2104
2105         tg3_phy_toggle_automdix(tp, 1);
2106         tg3_phy_set_wirespeed(tp);
2107         return 0;
2108 }
2109
2110 static void tg3_frob_aux_power(struct tg3 *tp)
2111 {
2112         struct tg3 *tp_peer = tp;
2113
2114         /* The GPIOs do something completely different on 57765. */
2115         if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
2116             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2117             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
2118                 return;
2119
2120         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2121             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2122             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
2123                 struct net_device *dev_peer;
2124
2125                 dev_peer = pci_get_drvdata(tp->pdev_peer);
2126                 /* remove_one() may have been run on the peer. */
2127                 if (!dev_peer)
2128                         tp_peer = tp;
2129                 else
2130                         tp_peer = netdev_priv(dev_peer);
2131         }
2132
2133         if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2134             (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2135             (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2136             (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
2137                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2138                     GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2139                         tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2140                                     (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                                     100);
2146                 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2147                            tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2148                         /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2149                         u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2150                                              GRC_LCLCTRL_GPIO_OE1 |
2151                                              GRC_LCLCTRL_GPIO_OE2 |
2152                                              GRC_LCLCTRL_GPIO_OUTPUT0 |
2153                                              GRC_LCLCTRL_GPIO_OUTPUT1 |
2154                                              tp->grc_local_ctrl;
2155                         tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2156
2157                         grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2158                         tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2159
2160                         grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2161                         tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2162                 } else {
2163                         u32 no_gpio2;
2164                         u32 grc_local_ctrl = 0;
2165
2166                         if (tp_peer != tp &&
2167                             (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2168                                 return;
2169
2170                         /* Workaround to prevent overdrawing Amps. */
2171                         if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2172                             ASIC_REV_5714) {
2173                                 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2174                                 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2175                                             grc_local_ctrl, 100);
2176                         }
2177
2178                         /* On 5753 and variants, GPIO2 cannot be used. */
2179                         no_gpio2 = tp->nic_sram_data_cfg &
2180                                     NIC_SRAM_DATA_CFG_NO_GPIO2;
2181
2182                         grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2183                                          GRC_LCLCTRL_GPIO_OE1 |
2184                                          GRC_LCLCTRL_GPIO_OE2 |
2185                                          GRC_LCLCTRL_GPIO_OUTPUT1 |
2186                                          GRC_LCLCTRL_GPIO_OUTPUT2;
2187                         if (no_gpio2) {
2188                                 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2189                                                     GRC_LCLCTRL_GPIO_OUTPUT2);
2190                         }
2191                         tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2192                                                     grc_local_ctrl, 100);
2193
2194                         grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2195
2196                         tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2197                                                     grc_local_ctrl, 100);
2198
2199                         if (!no_gpio2) {
2200                                 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2201                                 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2202                                             grc_local_ctrl, 100);
2203                         }
2204                 }
2205         } else {
2206                 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2207                     GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2208                         if (tp_peer != tp &&
2209                             (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2210                                 return;
2211
2212                         tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2213                                     (GRC_LCLCTRL_GPIO_OE1 |
2214                                      GRC_LCLCTRL_GPIO_OUTPUT1), 100);
2215
2216                         tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2217                                     GRC_LCLCTRL_GPIO_OE1, 100);
2218
2219                         tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2220                                     (GRC_LCLCTRL_GPIO_OE1 |
2221                                      GRC_LCLCTRL_GPIO_OUTPUT1), 100);
2222                 }
2223         }
2224 }
2225
2226 static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2227 {
2228         if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2229                 return 1;
2230         else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
2231                 if (speed != SPEED_10)
2232                         return 1;
2233         } else if (speed == SPEED_10)
2234                 return 1;
2235
2236         return 0;
2237 }
2238
2239 static int tg3_setup_phy(struct tg3 *, int);
2240
2241 #define RESET_KIND_SHUTDOWN     0
2242 #define RESET_KIND_INIT         1
2243 #define RESET_KIND_SUSPEND      2
2244
2245 static void tg3_write_sig_post_reset(struct tg3 *, int);
2246 static int tg3_halt_cpu(struct tg3 *, u32);
2247
2248 static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
2249 {
2250         u32 val;
2251
2252         if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
2253                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2254                         u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2255                         u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2256
2257                         sg_dig_ctrl |=
2258                                 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2259                         tw32(SG_DIG_CTRL, sg_dig_ctrl);
2260                         tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2261                 }
2262                 return;
2263         }
2264
2265         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2266                 tg3_bmcr_reset(tp);
2267                 val = tr32(GRC_MISC_CFG);
2268                 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2269                 udelay(40);
2270                 return;
2271         } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
2272                 u32 phytest;
2273                 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2274                         u32 phy;
2275
2276                         tg3_writephy(tp, MII_ADVERTISE, 0);
2277                         tg3_writephy(tp, MII_BMCR,
2278                                      BMCR_ANENABLE | BMCR_ANRESTART);
2279
2280                         tg3_writephy(tp, MII_TG3_FET_TEST,
2281                                      phytest | MII_TG3_FET_SHADOW_EN);
2282                         if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2283                                 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2284                                 tg3_writephy(tp,
2285                                              MII_TG3_FET_SHDW_AUXMODE4,
2286                                              phy);
2287                         }
2288                         tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2289                 }
2290                 return;
2291         } else if (do_low_power) {
2292                 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2293                              MII_TG3_EXT_CTRL_FORCE_LED_OFF);
2294
2295                 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2296                              MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2297                              MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2298                              MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2299                              MII_TG3_AUXCTL_PCTL_VREG_11V);
2300         }
2301
2302         /* The PHY should not be powered down on some chips because
2303          * of bugs.
2304          */
2305         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2306             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2307             (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2308              (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
2309                 return;
2310
2311         if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2312             GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
2313                 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2314                 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2315                 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2316                 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2317         }
2318
2319         tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2320 }
2321
2322 /* tp->lock is held. */
2323 static int tg3_nvram_lock(struct tg3 *tp)
2324 {
2325         if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2326                 int i;
2327
2328                 if (tp->nvram_lock_cnt == 0) {
2329                         tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2330                         for (i = 0; i < 8000; i++) {
2331                                 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2332                                         break;
2333                                 udelay(20);
2334                         }
2335                         if (i == 8000) {
2336                                 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2337                                 return -ENODEV;
2338                         }
2339                 }
2340                 tp->nvram_lock_cnt++;
2341         }
2342         return 0;
2343 }
2344
2345 /* tp->lock is held. */
2346 static void tg3_nvram_unlock(struct tg3 *tp)
2347 {
2348         if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2349                 if (tp->nvram_lock_cnt > 0)
2350                         tp->nvram_lock_cnt--;
2351                 if (tp->nvram_lock_cnt == 0)
2352                         tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2353         }
2354 }
2355
2356 /* tp->lock is held. */
2357 static void tg3_enable_nvram_access(struct tg3 *tp)
2358 {
2359         if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2360             !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
2361                 u32 nvaccess = tr32(NVRAM_ACCESS);
2362
2363                 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2364         }
2365 }
2366
2367 /* tp->lock is held. */
2368 static void tg3_disable_nvram_access(struct tg3 *tp)
2369 {
2370         if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2371             !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
2372                 u32 nvaccess = tr32(NVRAM_ACCESS);
2373
2374                 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2375         }
2376 }
2377
2378 static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2379                                         u32 offset, u32 *val)
2380 {
2381         u32 tmp;
2382         int i;
2383
2384         if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2385                 return -EINVAL;
2386
2387         tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2388                                         EEPROM_ADDR_DEVID_MASK |
2389                                         EEPROM_ADDR_READ);
2390         tw32(GRC_EEPROM_ADDR,
2391              tmp |
2392              (0 << EEPROM_ADDR_DEVID_SHIFT) |
2393              ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2394               EEPROM_ADDR_ADDR_MASK) |
2395              EEPROM_ADDR_READ | EEPROM_ADDR_START);
2396
2397         for (i = 0; i < 1000; i++) {
2398                 tmp = tr32(GRC_EEPROM_ADDR);
2399
2400                 if (tmp & EEPROM_ADDR_COMPLETE)
2401                         break;
2402                 msleep(1);
2403         }
2404         if (!(tmp & EEPROM_ADDR_COMPLETE))
2405                 return -EBUSY;
2406
2407         tmp = tr32(GRC_EEPROM_DATA);
2408
2409         /*
2410          * The data will always be opposite the native endian
2411          * format.  Perform a blind byteswap to compensate.
2412          */
2413         *val = swab32(tmp);
2414
2415         return 0;
2416 }
2417
2418 #define NVRAM_CMD_TIMEOUT 10000
2419
2420 static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2421 {
2422         int i;
2423
2424         tw32(NVRAM_CMD, nvram_cmd);
2425         for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2426                 udelay(10);
2427                 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2428                         udelay(10);
2429                         break;
2430                 }
2431         }
2432
2433         if (i == NVRAM_CMD_TIMEOUT)
2434                 return -EBUSY;
2435
2436         return 0;
2437 }
2438
2439 static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2440 {
2441         if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2442             (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2443             (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2444            !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2445             (tp->nvram_jedecnum == JEDEC_ATMEL))
2446
2447                 addr = ((addr / tp->nvram_pagesize) <<
2448                         ATMEL_AT45DB0X1B_PAGE_POS) +
2449                        (addr % tp->nvram_pagesize);
2450
2451         return addr;
2452 }
2453
2454 static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2455 {
2456         if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2457             (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2458             (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2459            !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2460             (tp->nvram_jedecnum == JEDEC_ATMEL))
2461
2462                 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2463                         tp->nvram_pagesize) +
2464                        (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2465
2466         return addr;
2467 }
2468
2469 /* NOTE: Data read in from NVRAM is byteswapped according to
2470  * the byteswapping settings for all other register accesses.
2471  * tg3 devices are BE devices, so on a BE machine, the data
2472  * returned will be exactly as it is seen in NVRAM.  On a LE
2473  * machine, the 32-bit value will be byteswapped.
2474  */
2475 static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2476 {
2477         int ret;
2478
2479         if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2480                 return tg3_nvram_read_using_eeprom(tp, offset, val);
2481
2482         offset = tg3_nvram_phys_addr(tp, offset);
2483
2484         if (offset > NVRAM_ADDR_MSK)
2485                 return -EINVAL;
2486
2487         ret = tg3_nvram_lock(tp);
2488         if (ret)
2489                 return ret;
2490
2491         tg3_enable_nvram_access(tp);
2492
2493         tw32(NVRAM_ADDR, offset);
2494         ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2495                 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2496
2497         if (ret == 0)
2498                 *val = tr32(NVRAM_RDDATA);
2499
2500         tg3_disable_nvram_access(tp);
2501
2502         tg3_nvram_unlock(tp);
2503
2504         return ret;
2505 }
2506
2507 /* Ensures NVRAM data is in bytestream format. */
2508 static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
2509 {
2510         u32 v;
2511         int res = tg3_nvram_read(tp, offset, &v);
2512         if (!res)
2513                 *val = cpu_to_be32(v);
2514         return res;
2515 }
2516
2517 /* tp->lock is held. */
2518 static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2519 {
2520         u32 addr_high, addr_low;
2521         int i;
2522
2523         addr_high = ((tp->dev->dev_addr[0] << 8) |
2524                      tp->dev->dev_addr[1]);
2525         addr_low = ((tp->dev->dev_addr[2] << 24) |
2526                     (tp->dev->dev_addr[3] << 16) |
2527                     (tp->dev->dev_addr[4] <<  8) |
2528                     (tp->dev->dev_addr[5] <<  0));
2529         for (i = 0; i < 4; i++) {
2530                 if (i == 1 && skip_mac_1)
2531                         continue;
2532                 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2533                 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2534         }
2535
2536         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2537             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2538                 for (i = 0; i < 12; i++) {
2539                         tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2540                         tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2541                 }
2542         }
2543
2544         addr_high = (tp->dev->dev_addr[0] +
2545                      tp->dev->dev_addr[1] +
2546                      tp->dev->dev_addr[2] +
2547                      tp->dev->dev_addr[3] +
2548                      tp->dev->dev_addr[4] +
2549                      tp->dev->dev_addr[5]) &
2550                 TX_BACKOFF_SEED_MASK;
2551         tw32(MAC_TX_BACKOFF_SEED, addr_high);
2552 }
2553
2554 static void tg3_enable_register_access(struct tg3 *tp)
2555 {
2556         /*
2557          * Make sure register accesses (indirect or otherwise) will function
2558          * correctly.
2559          */
2560         pci_write_config_dword(tp->pdev,
2561                                TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2562 }
2563
2564 static int tg3_power_up(struct tg3 *tp)
2565 {
2566         tg3_enable_register_access(tp);
2567
2568         pci_set_power_state(tp->pdev, PCI_D0);
2569
2570         /* Switch out of Vaux if it is a NIC */
2571         if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
2572                 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2573
2574         return 0;
2575 }
2576
2577 static int tg3_power_down_prepare(struct tg3 *tp)
2578 {
2579         u32 misc_host_ctrl;
2580         bool device_should_wake, do_low_power;
2581
2582         tg3_enable_register_access(tp);
2583
2584         /* Restore the CLKREQ setting. */
2585         if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2586                 u16 lnkctl;
2587
2588                 pci_read_config_word(tp->pdev,
2589                                      tp->pcie_cap + PCI_EXP_LNKCTL,
2590                                      &lnkctl);
2591                 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2592                 pci_write_config_word(tp->pdev,
2593                                       tp->pcie_cap + PCI_EXP_LNKCTL,
2594                                       lnkctl);
2595         }
2596
2597         misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2598         tw32(TG3PCI_MISC_HOST_CTRL,
2599              misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2600
2601         device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
2602                              (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2603
2604         if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
2605                 do_low_power = false;
2606                 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
2607                     !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2608                         struct phy_device *phydev;
2609                         u32 phyid, advertising;
2610
2611                         phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
2612
2613                         tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
2614
2615                         tp->link_config.orig_speed = phydev->speed;
2616                         tp->link_config.orig_duplex = phydev->duplex;
2617                         tp->link_config.orig_autoneg = phydev->autoneg;
2618                         tp->link_config.orig_advertising = phydev->advertising;
2619
2620                         advertising = ADVERTISED_TP |
2621                                       ADVERTISED_Pause |
2622                                       ADVERTISED_Autoneg |
2623                                       ADVERTISED_10baseT_Half;
2624
2625                         if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2626                             device_should_wake) {
2627                                 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2628                                         advertising |=
2629                                                 ADVERTISED_100baseT_Half |
2630                                                 ADVERTISED_100baseT_Full |
2631                                                 ADVERTISED_10baseT_Full;
2632                                 else
2633                                         advertising |= ADVERTISED_10baseT_Full;
2634                         }
2635
2636                         phydev->advertising = advertising;
2637
2638                         phy_start_aneg(phydev);
2639
2640                         phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
2641                         if (phyid != PHY_ID_BCMAC131) {
2642                                 phyid &= PHY_BCM_OUI_MASK;
2643                                 if (phyid == PHY_BCM_OUI_1 ||
2644                                     phyid == PHY_BCM_OUI_2 ||
2645                                     phyid == PHY_BCM_OUI_3)
2646                                         do_low_power = true;
2647                         }
2648                 }
2649         } else {
2650                 do_low_power = true;
2651
2652                 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2653                         tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
2654                         tp->link_config.orig_speed = tp->link_config.speed;
2655                         tp->link_config.orig_duplex = tp->link_config.duplex;
2656                         tp->link_config.orig_autoneg = tp->link_config.autoneg;
2657                 }
2658
2659                 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
2660                         tp->link_config.speed = SPEED_10;
2661                         tp->link_config.duplex = DUPLEX_HALF;
2662                         tp->link_config.autoneg = AUTONEG_ENABLE;
2663                         tg3_setup_phy(tp, 0);
2664                 }
2665         }
2666
2667         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2668                 u32 val;
2669
2670                 val = tr32(GRC_VCPU_EXT_CTRL);
2671                 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2672         } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
2673                 int i;
2674                 u32 val;
2675
2676                 for (i = 0; i < 200; i++) {
2677                         tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2678                         if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2679                                 break;
2680                         msleep(1);
2681                 }
2682         }
2683         if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2684                 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2685                                                      WOL_DRV_STATE_SHUTDOWN |
2686                                                      WOL_DRV_WOL |
2687                                                      WOL_SET_MAGIC_PKT);
2688
2689         if (device_should_wake) {
2690                 u32 mac_mode;
2691
2692                 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
2693                         if (do_low_power) {
2694                                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2695                                 udelay(40);
2696                         }
2697
2698                         if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
2699                                 mac_mode = MAC_MODE_PORT_MODE_GMII;
2700                         else
2701                                 mac_mode = MAC_MODE_PORT_MODE_MII;
2702
2703                         mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2704                         if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2705                             ASIC_REV_5700) {
2706                                 u32 speed = (tp->tg3_flags &
2707                                              TG3_FLAG_WOL_SPEED_100MB) ?
2708                                              SPEED_100 : SPEED_10;
2709                                 if (tg3_5700_link_polarity(tp, speed))
2710                                         mac_mode |= MAC_MODE_LINK_POLARITY;
2711                                 else
2712                                         mac_mode &= ~MAC_MODE_LINK_POLARITY;
2713                         }
2714                 } else {
2715                         mac_mode = MAC_MODE_PORT_MODE_TBI;
2716                 }
2717
2718                 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
2719                         tw32(MAC_LED_CTRL, tp->led_ctrl);
2720
2721                 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2722                 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2723                     !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2724                     ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2725                      (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2726                         mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
2727
2728                 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
2729                         mac_mode |= MAC_MODE_APE_TX_EN |
2730                                     MAC_MODE_APE_RX_EN |
2731                                     MAC_MODE_TDE_ENABLE;
2732
2733                 tw32_f(MAC_MODE, mac_mode);
2734                 udelay(100);
2735
2736                 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2737                 udelay(10);
2738         }
2739
2740         if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2741             (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2742              GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2743                 u32 base_val;
2744
2745                 base_val = tp->pci_clock_ctrl;
2746                 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2747                              CLOCK_CTRL_TXCLK_DISABLE);
2748
2749                 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2750                             CLOCK_CTRL_PWRDOWN_PLL133, 40);
2751         } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
2752                    (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
2753                    (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
2754                 /* do nothing */
2755         } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2756                      (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2757                 u32 newbits1, newbits2;
2758
2759                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2760                     GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2761                         newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2762                                     CLOCK_CTRL_TXCLK_DISABLE |
2763                                     CLOCK_CTRL_ALTCLK);
2764                         newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2765                 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2766                         newbits1 = CLOCK_CTRL_625_CORE;
2767                         newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2768                 } else {
2769                         newbits1 = CLOCK_CTRL_ALTCLK;
2770                         newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2771                 }
2772
2773                 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2774                             40);
2775
2776                 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2777                             40);
2778
2779                 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2780                         u32 newbits3;
2781
2782                         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2783                             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2784                                 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2785                                             CLOCK_CTRL_TXCLK_DISABLE |
2786                                             CLOCK_CTRL_44MHZ_CORE);
2787                         } else {
2788                                 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2789                         }
2790
2791                         tw32_wait_f(TG3PCI_CLOCK_CTRL,
2792                                     tp->pci_clock_ctrl | newbits3, 40);
2793                 }
2794         }
2795
2796         if (!(device_should_wake) &&
2797             !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
2798                 tg3_power_down_phy(tp, do_low_power);
2799
2800         tg3_frob_aux_power(tp);
2801
2802         /* Workaround for unstable PLL clock */
2803         if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2804             (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2805                 u32 val = tr32(0x7d00);
2806
2807                 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2808                 tw32(0x7d00, val);
2809                 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
2810                         int err;
2811
2812                         err = tg3_nvram_lock(tp);
2813                         tg3_halt_cpu(tp, RX_CPU_BASE);
2814                         if (!err)
2815                                 tg3_nvram_unlock(tp);
2816                 }
2817         }
2818
2819         tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2820
2821         return 0;
2822 }
2823
2824 static void tg3_power_down(struct tg3 *tp)
2825 {
2826         tg3_power_down_prepare(tp);
2827
2828         pci_wake_from_d3(tp->pdev, tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2829         pci_set_power_state(tp->pdev, PCI_D3hot);
2830 }
2831
2832 static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2833 {
2834         switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2835         case MII_TG3_AUX_STAT_10HALF:
2836                 *speed = SPEED_10;
2837                 *duplex = DUPLEX_HALF;
2838                 break;
2839
2840         case MII_TG3_AUX_STAT_10FULL:
2841                 *speed = SPEED_10;
2842                 *duplex = DUPLEX_FULL;
2843                 break;
2844
2845         case MII_TG3_AUX_STAT_100HALF:
2846                 *speed = SPEED_100;
2847                 *duplex = DUPLEX_HALF;
2848                 break;
2849
2850         case MII_TG3_AUX_STAT_100FULL:
2851                 *speed = SPEED_100;
2852                 *duplex = DUPLEX_FULL;
2853                 break;
2854
2855         case MII_TG3_AUX_STAT_1000HALF:
2856                 *speed = SPEED_1000;
2857                 *duplex = DUPLEX_HALF;
2858                 break;
2859
2860         case MII_TG3_AUX_STAT_1000FULL:
2861                 *speed = SPEED_1000;
2862                 *duplex = DUPLEX_FULL;
2863                 break;
2864
2865         default:
2866                 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
2867                         *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2868                                  SPEED_10;
2869                         *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2870                                   DUPLEX_HALF;
2871                         break;
2872                 }
2873                 *speed = SPEED_INVALID;
2874                 *duplex = DUPLEX_INVALID;
2875                 break;
2876         }
2877 }
2878
2879 static void tg3_phy_copper_begin(struct tg3 *tp)
2880 {
2881         u32 new_adv;
2882         int i;
2883
2884         if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2885                 /* Entering low power mode.  Disable gigabit and
2886                  * 100baseT advertisements.
2887                  */
2888                 tg3_writephy(tp, MII_TG3_CTRL, 0);
2889
2890                 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2891                            ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2892                 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2893                         new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2894
2895                 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2896         } else if (tp->link_config.speed == SPEED_INVALID) {
2897                 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
2898                         tp->link_config.advertising &=
2899                                 ~(ADVERTISED_1000baseT_Half |
2900                                   ADVERTISED_1000baseT_Full);
2901
2902                 new_adv = ADVERTISE_CSMA;
2903                 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2904                         new_adv |= ADVERTISE_10HALF;
2905                 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2906                         new_adv |= ADVERTISE_10FULL;
2907                 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2908                         new_adv |= ADVERTISE_100HALF;
2909                 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2910                         new_adv |= ADVERTISE_100FULL;
2911
2912                 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2913
2914                 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2915
2916                 if (tp->link_config.advertising &
2917                     (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2918                         new_adv = 0;
2919                         if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2920                                 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2921                         if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2922                                 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2923                         if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
2924                             (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2925                              tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2926                                 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2927                                             MII_TG3_CTRL_ENABLE_AS_MASTER);
2928                         tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2929                 } else {
2930                         tg3_writephy(tp, MII_TG3_CTRL, 0);
2931                 }
2932         } else {
2933                 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2934                 new_adv |= ADVERTISE_CSMA;
2935
2936                 /* Asking for a specific link mode. */
2937                 if (tp->link_config.speed == SPEED_1000) {
2938                         tg3_writephy(tp, MII_ADVERTISE, new_adv);
2939
2940                         if (tp->link_config.duplex == DUPLEX_FULL)
2941                                 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2942                         else
2943                                 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2944                         if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2945                             tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2946                                 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2947                                             MII_TG3_CTRL_ENABLE_AS_MASTER);
2948                 } else {
2949                         if (tp->link_config.speed == SPEED_100) {
2950                                 if (tp->link_config.duplex == DUPLEX_FULL)
2951                                         new_adv |= ADVERTISE_100FULL;
2952                                 else
2953                                         new_adv |= ADVERTISE_100HALF;
2954                         } else {
2955                                 if (tp->link_config.duplex == DUPLEX_FULL)
2956                                         new_adv |= ADVERTISE_10FULL;
2957                                 else
2958                                         new_adv |= ADVERTISE_10HALF;
2959                         }
2960                         tg3_writephy(tp, MII_ADVERTISE, new_adv);
2961
2962                         new_adv = 0;
2963                 }
2964
2965                 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2966         }
2967
2968         if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
2969                 u32 val;
2970
2971                 tw32(TG3_CPMU_EEE_MODE,
2972                      tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2973
2974                 /* Enable SM_DSP clock and tx 6dB coding. */
2975                 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
2976                       MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
2977                       MII_TG3_AUXCTL_ACTL_TX_6DB;
2978                 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2979
2980                 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2981                      GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
2982                     !tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2983                         tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2,
2984                                          val | MII_TG3_DSP_CH34TP2_HIBW01);
2985
2986                 val = 0;
2987                 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
2988                         /* Advertise 100-BaseTX EEE ability */
2989                         if (tp->link_config.advertising &
2990                             ADVERTISED_100baseT_Full)
2991                                 val |= MDIO_AN_EEE_ADV_100TX;
2992                         /* Advertise 1000-BaseT EEE ability */
2993                         if (tp->link_config.advertising &
2994                             ADVERTISED_1000baseT_Full)
2995                                 val |= MDIO_AN_EEE_ADV_1000T;
2996                 }
2997                 tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
2998
2999                 /* Turn off SM_DSP clock. */
3000                 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
3001                       MII_TG3_AUXCTL_ACTL_TX_6DB;
3002                 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3003         }
3004
3005         if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3006             tp->link_config.speed != SPEED_INVALID) {
3007                 u32 bmcr, orig_bmcr;
3008
3009                 tp->link_config.active_speed = tp->link_config.speed;
3010                 tp->link_config.active_duplex = tp->link_config.duplex;
3011
3012                 bmcr = 0;
3013                 switch (tp->link_config.speed) {
3014                 default:
3015                 case SPEED_10:
3016                         break;
3017
3018                 case SPEED_100:
3019                         bmcr |= BMCR_SPEED100;
3020                         break;
3021
3022                 case SPEED_1000:
3023                         bmcr |= TG3_BMCR_SPEED1000;
3024                         break;
3025                 }
3026
3027                 if (tp->link_config.duplex == DUPLEX_FULL)
3028                         bmcr |= BMCR_FULLDPLX;
3029
3030                 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3031                     (bmcr != orig_bmcr)) {
3032                         tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3033                         for (i = 0; i < 1500; i++) {
3034                                 u32 tmp;
3035
3036                                 udelay(10);
3037                                 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3038                                     tg3_readphy(tp, MII_BMSR, &tmp))
3039                                         continue;
3040                                 if (!(tmp & BMSR_LSTATUS)) {
3041                                         udelay(40);
3042                                         break;
3043                                 }
3044                         }
3045                         tg3_writephy(tp, MII_BMCR, bmcr);
3046                         udelay(40);
3047                 }
3048         } else {
3049                 tg3_writephy(tp, MII_BMCR,
3050                              BMCR_ANENABLE | BMCR_ANRESTART);
3051         }
3052 }
3053
3054 static int tg3_init_5401phy_dsp(struct tg3 *tp)
3055 {
3056         int err;
3057
3058         /* Turn off tap power management. */
3059         /* Set Extended packet length bit */
3060         err  = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
3061
3062         err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3063         err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3064         err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3065         err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3066         err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
3067
3068         udelay(40);
3069
3070         return err;
3071 }
3072
3073 static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
3074 {
3075         u32 adv_reg, all_mask = 0;
3076
3077         if (mask & ADVERTISED_10baseT_Half)
3078                 all_mask |= ADVERTISE_10HALF;
3079         if (mask & ADVERTISED_10baseT_Full)
3080                 all_mask |= ADVERTISE_10FULL;
3081         if (mask & ADVERTISED_100baseT_Half)
3082                 all_mask |= ADVERTISE_100HALF;
3083         if (mask & ADVERTISED_100baseT_Full)
3084                 all_mask |= ADVERTISE_100FULL;
3085
3086         if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3087                 return 0;
3088
3089         if ((adv_reg & all_mask) != all_mask)
3090                 return 0;
3091         if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3092                 u32 tg3_ctrl;
3093
3094                 all_mask = 0;
3095                 if (mask & ADVERTISED_1000baseT_Half)
3096                         all_mask |= ADVERTISE_1000HALF;
3097                 if (mask & ADVERTISED_1000baseT_Full)
3098                         all_mask |= ADVERTISE_1000FULL;
3099
3100                 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3101                         return 0;
3102
3103                 if ((tg3_ctrl & all_mask) != all_mask)
3104                         return 0;
3105         }
3106         return 1;
3107 }
3108
3109 static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3110 {
3111         u32 curadv, reqadv;
3112
3113         if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3114                 return 1;
3115
3116         curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3117         reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3118
3119         if (tp->link_config.active_duplex == DUPLEX_FULL) {
3120                 if (curadv != reqadv)
3121                         return 0;
3122
3123                 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3124                         tg3_readphy(tp, MII_LPA, rmtadv);
3125         } else {
3126                 /* Reprogram the advertisement register, even if it
3127                  * does not affect the current link.  If the link
3128                  * gets renegotiated in the future, we can save an
3129                  * additional renegotiation cycle by advertising
3130                  * it correctly in the first place.
3131                  */
3132                 if (curadv != reqadv) {
3133                         *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3134                                      ADVERTISE_PAUSE_ASYM);
3135                         tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3136                 }
3137         }
3138
3139         return 1;
3140 }
3141
3142 static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3143 {
3144         int current_link_up;
3145         u32 bmsr, val;
3146         u32 lcl_adv, rmt_adv;
3147         u16 current_speed;
3148         u8 current_duplex;
3149         int i, err;
3150
3151         tw32(MAC_EVENT, 0);
3152
3153         tw32_f(MAC_STATUS,
3154              (MAC_STATUS_SYNC_CHANGED |
3155               MAC_STATUS_CFG_CHANGED |
3156               MAC_STATUS_MI_COMPLETION |
3157               MAC_STATUS_LNKSTATE_CHANGED));
3158         udelay(40);
3159
3160         if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3161                 tw32_f(MAC_MI_MODE,
3162                      (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3163                 udelay(80);
3164         }
3165
3166         tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3167
3168         /* Some third-party PHYs need to be reset on link going
3169          * down.
3170          */
3171         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3172              GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3173              GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3174             netif_carrier_ok(tp->dev)) {
3175                 tg3_readphy(tp, MII_BMSR, &bmsr);
3176                 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3177                     !(bmsr & BMSR_LSTATUS))
3178                         force_reset = 1;
3179         }
3180         if (force_reset)
3181                 tg3_phy_reset(tp);
3182
3183         if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
3184                 tg3_readphy(tp, MII_BMSR, &bmsr);
3185                 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3186                     !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3187                         bmsr = 0;
3188
3189                 if (!(bmsr & BMSR_LSTATUS)) {
3190                         err = tg3_init_5401phy_dsp(tp);
3191                         if (err)
3192                                 return err;
3193
3194                         tg3_readphy(tp, MII_BMSR, &bmsr);
3195                         for (i = 0; i < 1000; i++) {
3196                                 udelay(10);
3197                                 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3198                                     (bmsr & BMSR_LSTATUS)) {
3199                                         udelay(40);
3200                                         break;
3201                                 }
3202                         }
3203
3204                         if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3205                             TG3_PHY_REV_BCM5401_B0 &&
3206                             !(bmsr & BMSR_LSTATUS) &&
3207                             tp->link_config.active_speed == SPEED_1000) {
3208                                 err = tg3_phy_reset(tp);
3209                                 if (!err)
3210                                         err = tg3_init_5401phy_dsp(tp);
3211                                 if (err)
3212                                         return err;
3213                         }
3214                 }
3215         } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3216                    tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3217                 /* 5701 {A0,B0} CRC bug workaround */
3218                 tg3_writephy(tp, 0x15, 0x0a75);
3219                 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3220                 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3221                 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3222         }
3223
3224         /* Clear pending interrupts... */
3225         tg3_readphy(tp, MII_TG3_ISTAT, &val);
3226         tg3_readphy(tp, MII_TG3_ISTAT, &val);
3227
3228         if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
3229                 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
3230         else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
3231                 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3232
3233         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3234             GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3235                 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3236                         tg3_writephy(tp, MII_TG3_EXT_CTRL,
3237                                      MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3238                 else
3239                         tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3240         }
3241
3242         current_link_up = 0;
3243         current_speed = SPEED_INVALID;
3244         current_duplex = DUPLEX_INVALID;
3245
3246         if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
3247                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3248                 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3249                 if (!(val & (1 << 10))) {
3250                         val |= (1 << 10);
3251                         tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3252                         goto relink;
3253                 }
3254         }
3255
3256         bmsr = 0;
3257         for (i = 0; i < 100; i++) {
3258                 tg3_readphy(tp, MII_BMSR, &bmsr);
3259                 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3260                     (bmsr & BMSR_LSTATUS))
3261                         break;
3262                 udelay(40);
3263         }
3264
3265         if (bmsr & BMSR_LSTATUS) {
3266                 u32 aux_stat, bmcr;
3267
3268                 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3269                 for (i = 0; i < 2000; i++) {
3270                         udelay(10);
3271                         if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3272                             aux_stat)
3273                                 break;
3274                 }
3275
3276                 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3277                                              &current_speed,
3278                                              &current_duplex);
3279
3280                 bmcr = 0;
3281                 for (i = 0; i < 200; i++) {
3282                         tg3_readphy(tp, MII_BMCR, &bmcr);
3283                         if (tg3_readphy(tp, MII_BMCR, &bmcr))
3284                                 continue;
3285                         if (bmcr && bmcr != 0x7fff)
3286                                 break;
3287                         udelay(10);
3288                 }
3289
3290                 lcl_adv = 0;
3291                 rmt_adv = 0;
3292
3293                 tp->link_config.active_speed = current_speed;
3294                 tp->link_config.active_duplex = current_duplex;
3295
3296                 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3297                         if ((bmcr & BMCR_ANENABLE) &&
3298                             tg3_copper_is_advertising_all(tp,
3299                                                 tp->link_config.advertising)) {
3300                                 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3301                                                                   &rmt_adv))
3302                                         current_link_up = 1;
3303                         }
3304                 } else {
3305                         if (!(bmcr & BMCR_ANENABLE) &&
3306                             tp->link_config.speed == current_speed &&
3307                             tp->link_config.duplex == current_duplex &&
3308                             tp->link_config.flowctrl ==
3309                             tp->link_config.active_flowctrl) {
3310                                 current_link_up = 1;
3311                         }
3312                 }
3313
3314                 if (current_link_up == 1 &&
3315                     tp->link_config.active_duplex == DUPLEX_FULL)
3316                         tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
3317         }
3318
3319 relink:
3320         if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3321                 tg3_phy_copper_begin(tp);
3322
3323                 tg3_readphy(tp, MII_BMSR, &bmsr);
3324                 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3325                     (bmsr & BMSR_LSTATUS))
3326                         current_link_up = 1;
3327         }
3328
3329         tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3330         if (current_link_up == 1) {
3331                 if (tp->link_config.active_speed == SPEED_100 ||
3332                     tp->link_config.active_speed == SPEED_10)
3333                         tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3334                 else
3335                         tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3336         } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
3337                 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3338         else
3339                 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3340
3341         tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3342         if (tp->link_config.active_duplex == DUPLEX_HALF)
3343                 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3344
3345         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
3346                 if (current_link_up == 1 &&
3347                     tg3_5700_link_polarity(tp, tp->link_config.active_speed))
3348                         tp->mac_mode |= MAC_MODE_LINK_POLARITY;
3349                 else
3350                         tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
3351         }
3352
3353         /* ??? Without this setting Netgear GA302T PHY does not
3354          * ??? send/receive packets...
3355          */
3356         if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
3357             tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3358                 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3359                 tw32_f(MAC_MI_MODE, tp->mi_mode);
3360                 udelay(80);
3361         }
3362
3363         tw32_f(MAC_MODE, tp->mac_mode);
3364         udelay(40);
3365
3366         tg3_phy_eee_adjust(tp, current_link_up);
3367
3368         if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3369                 /* Polled via timer. */
3370                 tw32_f(MAC_EVENT, 0);
3371         } else {
3372                 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3373         }
3374         udelay(40);
3375
3376         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3377             current_link_up == 1 &&
3378             tp->link_config.active_speed == SPEED_1000 &&
3379             ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3380              (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3381                 udelay(120);
3382                 tw32_f(MAC_STATUS,
3383                      (MAC_STATUS_SYNC_CHANGED |
3384                       MAC_STATUS_CFG_CHANGED));
3385                 udelay(40);
3386                 tg3_write_mem(tp,
3387                               NIC_SRAM_FIRMWARE_MBOX,
3388                               NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3389         }
3390
3391         /* Prevent send BD corruption. */
3392         if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3393                 u16 oldlnkctl, newlnkctl;
3394
3395                 pci_read_config_word(tp->pdev,
3396                                      tp->pcie_cap + PCI_EXP_LNKCTL,
3397                                      &oldlnkctl);
3398                 if (tp->link_config.active_speed == SPEED_100 ||
3399                     tp->link_config.active_speed == SPEED_10)
3400                         newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3401                 else
3402                         newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3403                 if (newlnkctl != oldlnkctl)
3404                         pci_write_config_word(tp->pdev,
3405                                               tp->pcie_cap + PCI_EXP_LNKCTL,
3406                                               newlnkctl);
3407         }
3408
3409         if (current_link_up != netif_carrier_ok(tp->dev)) {
3410                 if (current_link_up)
3411                         netif_carrier_on(tp->dev);
3412                 else
3413                         netif_carrier_off(tp->dev);
3414                 tg3_link_report(tp);
3415         }
3416
3417         return 0;
3418 }
3419
3420 struct tg3_fiber_aneginfo {
3421         int state;
3422 #define ANEG_STATE_UNKNOWN              0
3423 #define ANEG_STATE_AN_ENABLE            1
3424 #define ANEG_STATE_RESTART_INIT         2
3425 #define ANEG_STATE_RESTART              3
3426 #define ANEG_STATE_DISABLE_LINK_OK      4
3427 #define ANEG_STATE_ABILITY_DETECT_INIT  5
3428 #define ANEG_STATE_ABILITY_DETECT       6
3429 #define ANEG_STATE_ACK_DETECT_INIT      7
3430 #define ANEG_STATE_ACK_DETECT           8
3431 #define ANEG_STATE_COMPLETE_ACK_INIT    9
3432 #define ANEG_STATE_COMPLETE_ACK         10
3433 #define ANEG_STATE_IDLE_DETECT_INIT     11
3434 #define ANEG_STATE_IDLE_DETECT          12
3435 #define ANEG_STATE_LINK_OK              13
3436 #define ANEG_STATE_NEXT_PAGE_WAIT_INIT  14
3437 #define ANEG_STATE_NEXT_PAGE_WAIT       15
3438
3439         u32 flags;
3440 #define MR_AN_ENABLE            0x00000001
3441 #define MR_RESTART_AN           0x00000002
3442 #define MR_AN_COMPLETE          0x00000004
3443 #define MR_PAGE_RX              0x00000008
3444 #define MR_NP_LOADED            0x00000010
3445 #define MR_TOGGLE_TX            0x00000020
3446 #define MR_LP_ADV_FULL_DUPLEX   0x00000040
3447 #define MR_LP_ADV_HALF_DUPLEX   0x00000080
3448 #define MR_LP_ADV_SYM_PAUSE     0x00000100
3449 #define MR_LP_ADV_ASYM_PAUSE    0x00000200
3450 #define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3451 #define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3452 #define MR_LP_ADV_NEXT_PAGE     0x00001000
3453 #define MR_TOGGLE_RX            0x00002000
3454 #define MR_NP_RX                0x00004000
3455
3456 #define MR_LINK_OK              0x80000000
3457
3458         unsigned long link_time, cur_time;
3459
3460         u32 ability_match_cfg;
3461         int ability_match_count;
3462
3463         char ability_match, idle_match, ack_match;
3464
3465         u32 txconfig, rxconfig;
3466 #define ANEG_CFG_NP             0x00000080
3467 #define ANEG_CFG_ACK            0x00000040
3468 #define ANEG_CFG_RF2            0x00000020
3469 #define ANEG_CFG_RF1            0x00000010
3470 #define ANEG_CFG_PS2            0x00000001
3471 #define ANEG_CFG_PS1            0x00008000
3472 #define ANEG_CFG_HD             0x00004000
3473 #define ANEG_CFG_FD             0x00002000
3474 #define ANEG_CFG_INVAL          0x00001f06
3475
3476 };
3477 #define ANEG_OK         0
3478 #define ANEG_DONE       1
3479 #define ANEG_TIMER_ENAB 2
3480 #define ANEG_FAILED     -1
3481
3482 #define ANEG_STATE_SETTLE_TIME  10000
3483
3484 static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3485                                    struct tg3_fiber_aneginfo *ap)
3486 {
3487         u16 flowctrl;
3488         unsigned long delta;
3489         u32 rx_cfg_reg;
3490         int ret;
3491
3492         if (ap->state == ANEG_STATE_UNKNOWN) {
3493                 ap->rxconfig = 0;
3494                 ap->link_time = 0;
3495                 ap->cur_time = 0;
3496                 ap->ability_match_cfg = 0;
3497                 ap->ability_match_count = 0;
3498                 ap->ability_match = 0;
3499                 ap->idle_match = 0;
3500                 ap->ack_match = 0;
3501         }
3502         ap->cur_time++;
3503
3504         if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3505                 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3506
3507                 if (rx_cfg_reg != ap->ability_match_cfg) {
3508                         ap->ability_match_cfg = rx_cfg_reg;
3509                         ap->ability_match = 0;
3510                         ap->ability_match_count = 0;
3511                 } else {
3512                         if (++ap->ability_match_count > 1) {
3513                                 ap->ability_match = 1;
3514                                 ap->ability_match_cfg = rx_cfg_reg;
3515                         }
3516                 }
3517                 if (rx_cfg_reg & ANEG_CFG_ACK)
3518                         ap->ack_match = 1;
3519                 else
3520                         ap->ack_match = 0;
3521
3522                 ap->idle_match = 0;
3523         } else {
3524                 ap->idle_match = 1;
3525                 ap->ability_match_cfg = 0;
3526                 ap->ability_match_count = 0;
3527                 ap->ability_match = 0;
3528                 ap->ack_match = 0;
3529
3530                 rx_cfg_reg = 0;
3531         }
3532
3533         ap->rxconfig = rx_cfg_reg;
3534         ret = ANEG_OK;
3535
3536         switch (ap->state) {
3537         case ANEG_STATE_UNKNOWN:
3538                 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3539                         ap->state = ANEG_STATE_AN_ENABLE;
3540
3541                 /* fallthru */
3542         case ANEG_STATE_AN_ENABLE:
3543                 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3544                 if (ap->flags & MR_AN_ENABLE) {
3545                         ap->link_time = 0;
3546                         ap->cur_time = 0;
3547                         ap->ability_match_cfg = 0;
3548                         ap->ability_match_count = 0;
3549                         ap->ability_match = 0;
3550                         ap->idle_match = 0;
3551                         ap->ack_match = 0;
3552
3553                         ap->state = ANEG_STATE_RESTART_INIT;
3554                 } else {
3555                         ap->state = ANEG_STATE_DISABLE_LINK_OK;
3556                 }
3557                 break;
3558
3559         case ANEG_STATE_RESTART_INIT:
3560                 ap->link_time = ap->cur_time;
3561                 ap->flags &= ~(MR_NP_LOADED);
3562                 ap->txconfig = 0;
3563                 tw32(MAC_TX_AUTO_NEG, 0);
3564                 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3565                 tw32_f(MAC_MODE, tp->mac_mode);
3566                 udelay(40);
3567
3568                 ret = ANEG_TIMER_ENAB;
3569                 ap->state = ANEG_STATE_RESTART;
3570
3571                 /* fallthru */
3572         case ANEG_STATE_RESTART:
3573                 delta = ap->cur_time - ap->link_time;
3574                 if (delta > ANEG_STATE_SETTLE_TIME)
3575                         ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3576                 else
3577                         ret = ANEG_TIMER_ENAB;
3578                 break;
3579
3580         case ANEG_STATE_DISABLE_LINK_OK:
3581                 ret = ANEG_DONE;
3582                 break;
3583
3584         case ANEG_STATE_ABILITY_DETECT_INIT:
3585                 ap->flags &= ~(MR_TOGGLE_TX);
3586                 ap->txconfig = ANEG_CFG_FD;
3587                 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3588                 if (flowctrl & ADVERTISE_1000XPAUSE)
3589                         ap->txconfig |= ANEG_CFG_PS1;
3590                 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3591                         ap->txconfig |= ANEG_CFG_PS2;
3592                 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3593                 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3594                 tw32_f(MAC_MODE, tp->mac_mode);
3595                 udelay(40);
3596
3597                 ap->state = ANEG_STATE_ABILITY_DETECT;
3598                 break;
3599
3600         case ANEG_STATE_ABILITY_DETECT:
3601                 if (ap->ability_match != 0 && ap->rxconfig != 0)
3602                         ap->state = ANEG_STATE_ACK_DETECT_INIT;
3603                 break;
3604
3605         case ANEG_STATE_ACK_DETECT_INIT:
3606                 ap->txconfig |= ANEG_CFG_ACK;
3607                 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3608                 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3609                 tw32_f(MAC_MODE, tp->mac_mode);
3610                 udelay(40);
3611
3612                 ap->state = ANEG_STATE_ACK_DETECT;
3613
3614                 /* fallthru */
3615         case ANEG_STATE_ACK_DETECT:
3616                 if (ap->ack_match != 0) {
3617                         if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3618                             (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3619                                 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3620                         } else {
3621                                 ap->state = ANEG_STATE_AN_ENABLE;
3622                         }
3623                 } else if (ap->ability_match != 0 &&
3624                            ap->rxconfig == 0) {
3625                         ap->state = ANEG_STATE_AN_ENABLE;
3626                 }
3627                 break;
3628
3629         case ANEG_STATE_COMPLETE_ACK_INIT:
3630                 if (ap->rxconfig & ANEG_CFG_INVAL) {
3631                         ret = ANEG_FAILED;
3632                         break;
3633                 }
3634                 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3635                                MR_LP_ADV_HALF_DUPLEX |
3636                                MR_LP_ADV_SYM_PAUSE |
3637                                MR_LP_ADV_ASYM_PAUSE |
3638                                MR_LP_ADV_REMOTE_FAULT1 |
3639                                MR_LP_ADV_REMOTE_FAULT2 |
3640                                MR_LP_ADV_NEXT_PAGE |
3641                                MR_TOGGLE_RX |
3642                                MR_NP_RX);
3643                 if (ap->rxconfig & ANEG_CFG_FD)
3644                         ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3645                 if (ap->rxconfig & ANEG_CFG_HD)
3646                         ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3647                 if (ap->rxconfig & ANEG_CFG_PS1)
3648                         ap->flags |= MR_LP_ADV_SYM_PAUSE;
3649                 if (ap->rxconfig & ANEG_CFG_PS2)
3650                         ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3651                 if (ap->rxconfig & ANEG_CFG_RF1)
3652                         ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3653                 if (ap->rxconfig & ANEG_CFG_RF2)
3654                         ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3655                 if (ap->rxconfig & ANEG_CFG_NP)
3656                         ap->flags |= MR_LP_ADV_NEXT_PAGE;
3657
3658                 ap->link_time = ap->cur_time;
3659
3660                 ap->flags ^= (MR_TOGGLE_TX);
3661                 if (ap->rxconfig & 0x0008)
3662                         ap->flags |= MR_TOGGLE_RX;
3663                 if (ap->rxconfig & ANEG_CFG_NP)
3664                         ap->flags |= MR_NP_RX;
3665                 ap->flags |= MR_PAGE_RX;
3666
3667                 ap->state = ANEG_STATE_COMPLETE_ACK;
3668                 ret = ANEG_TIMER_ENAB;
3669                 break;
3670
3671         case ANEG_STATE_COMPLETE_ACK:
3672                 if (ap->ability_match != 0 &&
3673                     ap->rxconfig == 0) {
3674                         ap->state = ANEG_STATE_AN_ENABLE;
3675                         break;
3676                 }
3677                 delta = ap->cur_time - ap->link_time;
3678                 if (delta > ANEG_STATE_SETTLE_TIME) {
3679                         if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3680                                 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3681                         } else {
3682                                 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3683                                     !(ap->flags & MR_NP_RX)) {
3684                                         ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3685                                 } else {
3686                                         ret = ANEG_FAILED;
3687                                 }
3688                         }
3689                 }
3690                 break;
3691
3692         case ANEG_STATE_IDLE_DETECT_INIT:
3693                 ap->link_time = ap->cur_time;
3694                 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3695                 tw32_f(MAC_MODE, tp->mac_mode);
3696                 udelay(40);
3697
3698                 ap->state = ANEG_STATE_IDLE_DETECT;
3699                 ret = ANEG_TIMER_ENAB;
3700                 break;
3701
3702         case ANEG_STATE_IDLE_DETECT:
3703                 if (ap->ability_match != 0 &&
3704                     ap->rxconfig == 0) {
3705                         ap->state = ANEG_STATE_AN_ENABLE;
3706                         break;
3707                 }
3708                 delta = ap->cur_time - ap->link_time;
3709                 if (delta > ANEG_STATE_SETTLE_TIME) {
3710                         /* XXX another gem from the Broadcom driver :( */
3711                         ap->state = ANEG_STATE_LINK_OK;
3712                 }
3713                 break;
3714
3715         case ANEG_STATE_LINK_OK:
3716                 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3717                 ret = ANEG_DONE;
3718                 break;
3719
3720         case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3721                 /* ??? unimplemented */
3722                 break;
3723
3724         case ANEG_STATE_NEXT_PAGE_WAIT:
3725                 /* ??? unimplemented */
3726                 break;
3727
3728         default:
3729                 ret = ANEG_FAILED;
3730                 break;
3731         }
3732
3733         return ret;
3734 }
3735
3736 static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
3737 {
3738         int res = 0;
3739         struct tg3_fiber_aneginfo aninfo;
3740         int status = ANEG_FAILED;
3741         unsigned int tick;
3742         u32 tmp;
3743
3744         tw32_f(MAC_TX_AUTO_NEG, 0);
3745
3746         tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3747         tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3748         udelay(40);
3749
3750         tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3751         udelay(40);
3752
3753         memset(&aninfo, 0, sizeof(aninfo));
3754         aninfo.flags |= MR_AN_ENABLE;
3755         aninfo.state = ANEG_STATE_UNKNOWN;
3756         aninfo.cur_time = 0;
3757         tick = 0;
3758         while (++tick < 195000) {
3759                 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3760                 if (status == ANEG_DONE || status == ANEG_FAILED)
3761                         break;
3762
3763                 udelay(1);
3764         }
3765
3766         tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3767         tw32_f(MAC_MODE, tp->mac_mode);
3768         udelay(40);
3769
3770         *txflags = aninfo.txconfig;
3771         *rxflags = aninfo.flags;
3772
3773         if (status == ANEG_DONE &&
3774             (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3775                              MR_LP_ADV_FULL_DUPLEX)))
3776                 res = 1;
3777
3778         return res;
3779 }
3780
3781 static void tg3_init_bcm8002(struct tg3 *tp)
3782 {
3783         u32 mac_status = tr32(MAC_STATUS);
3784         int i;
3785
3786         /* Reset when initting first time or we have a link. */
3787         if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3788             !(mac_status & MAC_STATUS_PCS_SYNCED))
3789                 return;
3790
3791         /* Set PLL lock range. */
3792         tg3_writephy(tp, 0x16, 0x8007);
3793
3794         /* SW reset */
3795         tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3796
3797         /* Wait for reset to complete. */
3798         /* XXX schedule_timeout() ... */
3799         for (i = 0; i < 500; i++)
3800                 udelay(10);
3801
3802         /* Config mode; select PMA/Ch 1 regs. */
3803         tg3_writephy(tp, 0x10, 0x8411);
3804
3805         /* Enable auto-lock and comdet, select txclk for tx. */
3806         tg3_writephy(tp, 0x11, 0x0a10);
3807
3808         tg3_writephy(tp, 0x18, 0x00a0);
3809         tg3_writephy(tp, 0x16, 0x41ff);
3810
3811         /* Assert and deassert POR. */
3812         tg3_writephy(tp, 0x13, 0x0400);
3813         udelay(40);
3814         tg3_writephy(tp, 0x13, 0x0000);
3815
3816         tg3_writephy(tp, 0x11, 0x0a50);
3817         udelay(40);
3818         tg3_writephy(tp, 0x11, 0x0a10);
3819
3820         /* Wait for signal to stabilize */
3821         /* XXX schedule_timeout() ... */
3822         for (i = 0; i < 15000; i++)
3823                 udelay(10);
3824
3825         /* Deselect the channel register so we can read the PHYID
3826          * later.
3827          */
3828         tg3_writephy(tp, 0x10, 0x8011);
3829 }
3830
3831 static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3832 {
3833         u16 flowctrl;
3834         u32 sg_dig_ctrl, sg_dig_status;
3835         u32 serdes_cfg, expected_sg_dig_ctrl;
3836         int workaround, port_a;
3837         int current_link_up;
3838
3839         serdes_cfg = 0;
3840         expected_sg_dig_ctrl = 0;
3841         workaround = 0;
3842         port_a = 1;
3843         current_link_up = 0;
3844
3845         if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3846             tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3847                 workaround = 1;
3848                 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3849                         port_a = 0;
3850
3851                 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3852                 /* preserve bits 20-23 for voltage regulator */
3853                 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3854         }
3855
3856         sg_dig_ctrl = tr32(SG_DIG_CTRL);
3857
3858         if (tp->link_config.autoneg != AUTONEG_ENABLE) {
3859                 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
3860                         if (workaround) {
3861                                 u32 val = serdes_cfg;
3862
3863                                 if (port_a)
3864                                         val |= 0xc010000;
3865                                 else
3866                                         val |= 0x4010000;
3867                                 tw32_f(MAC_SERDES_CFG, val);
3868                         }
3869
3870                         tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
3871                 }
3872                 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3873                         tg3_setup_flow_control(tp, 0, 0);
3874                         current_link_up = 1;
3875                 }
3876                 goto out;
3877         }
3878
3879         /* Want auto-negotiation.  */
3880         expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
3881
3882         flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3883         if (flowctrl & ADVERTISE_1000XPAUSE)
3884                 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3885         if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3886                 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
3887
3888         if (sg_dig_ctrl != expected_sg_dig_ctrl) {
3889                 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
3890                     tp->serdes_counter &&
3891                     ((mac_status & (MAC_STATUS_PCS_SYNCED |
3892                                     MAC_STATUS_RCVD_CFG)) ==
3893                      MAC_STATUS_PCS_SYNCED)) {
3894                         tp->serdes_counter--;
3895                         current_link_up = 1;
3896                         goto out;
3897                 }
3898 restart_autoneg:
3899                 if (workaround)
3900                         tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
3901                 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
3902                 udelay(5);
3903                 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3904
3905                 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3906                 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
3907         } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3908                                  MAC_STATUS_SIGNAL_DET)) {
3909                 sg_dig_status = tr32(SG_DIG_STATUS);
3910                 mac_status = tr32(MAC_STATUS);
3911
3912                 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
3913                     (mac_status & MAC_STATUS_PCS_SYNCED)) {
3914                         u32 local_adv = 0, remote_adv = 0;
3915
3916                         if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3917                                 local_adv |= ADVERTISE_1000XPAUSE;
3918                         if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3919                                 local_adv |= ADVERTISE_1000XPSE_ASYM;
3920
3921                         if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
3922                                 remote_adv |= LPA_1000XPAUSE;
3923                         if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
3924                                 remote_adv |= LPA_1000XPAUSE_ASYM;
3925
3926                         tg3_setup_flow_control(tp, local_adv, remote_adv);
3927                         current_link_up = 1;
3928                         tp->serdes_counter = 0;
3929                         tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
3930                 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
3931                         if (tp->serdes_counter)
3932                                 tp->serdes_counter--;
3933                         else {
3934                                 if (workaround) {
3935                                         u32 val = serdes_cfg;
3936
3937                                         if (port_a)
3938                                                 val |= 0xc010000;
3939                                         else
3940                                                 val |= 0x4010000;
3941
3942                                         tw32_f(MAC_SERDES_CFG, val);
3943                                 }
3944
3945                                 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
3946                                 udelay(40);
3947
3948                                 /* Link parallel detection - link is up */
3949                                 /* only if we have PCS_SYNC and not */
3950                                 /* receiving config code words */
3951                                 mac_status = tr32(MAC_STATUS);
3952                                 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3953                                     !(mac_status & MAC_STATUS_RCVD_CFG)) {
3954                                         tg3_setup_flow_control(tp, 0, 0);
3955                                         current_link_up = 1;
3956                                         tp->phy_flags |=
3957                                                 TG3_PHYFLG_PARALLEL_DETECT;
3958                                         tp->serdes_counter =
3959                                                 SERDES_PARALLEL_DET_TIMEOUT;
3960                                 } else
3961                                         goto restart_autoneg;
3962                         }
3963                 }
3964         } else {
3965                 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3966                 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
3967         }
3968
3969 out:
3970         return current_link_up;
3971 }
3972
3973 static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3974 {
3975         int current_link_up = 0;
3976
3977         if (!(mac_status & MAC_STATUS_PCS_SYNCED))
3978                 goto out;
3979
3980         if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3981                 u32 txflags, rxflags;
3982                 int i;
3983
3984                 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3985                         u32 local_adv = 0, remote_adv = 0;
3986
3987                         if (txflags & ANEG_CFG_PS1)
3988                                 local_adv |= ADVERTISE_1000XPAUSE;
3989                         if (txflags & ANEG_CFG_PS2)
3990                                 local_adv |= ADVERTISE_1000XPSE_ASYM;
3991
3992                         if (rxflags & MR_LP_ADV_SYM_PAUSE)
3993                                 remote_adv |= LPA_1000XPAUSE;
3994                         if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3995                                 remote_adv |= LPA_1000XPAUSE_ASYM;
3996
3997                         tg3_setup_flow_control(tp, local_adv, remote_adv);
3998
3999                         current_link_up = 1;
4000                 }
4001                 for (i = 0; i < 30; i++) {
4002                         udelay(20);
4003                         tw32_f(MAC_STATUS,
4004                                (MAC_STATUS_SYNC_CHANGED |
4005                                 MAC_STATUS_CFG_CHANGED));
4006                         udelay(40);
4007                         if ((tr32(MAC_STATUS) &
4008                              (MAC_STATUS_SYNC_CHANGED |
4009                               MAC_STATUS_CFG_CHANGED)) == 0)
4010                                 break;
4011                 }
4012
4013                 mac_status = tr32(MAC_STATUS);
4014                 if (current_link_up == 0 &&
4015                     (mac_status & MAC_STATUS_PCS_SYNCED) &&
4016                     !(mac_status & MAC_STATUS_RCVD_CFG))
4017                         current_link_up = 1;
4018         } else {
4019                 tg3_setup_flow_control(tp, 0, 0);
4020
4021                 /* Forcing 1000FD link up. */
4022                 current_link_up = 1;
4023
4024                 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4025                 udelay(40);
4026
4027                 tw32_f(MAC_MODE, tp->mac_mode);
4028                 udelay(40);
4029         }
4030
4031 out:
4032         return current_link_up;
4033 }
4034
4035 static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4036 {
4037         u32 orig_pause_cfg;
4038         u16 orig_active_speed;
4039         u8 orig_active_duplex;
4040         u32 mac_status;
4041         int current_link_up;
4042         int i;
4043
4044         orig_pause_cfg = tp->link_config.active_flowctrl;
4045         orig_active_speed = tp->link_config.active_speed;
4046         orig_active_duplex = tp->link_config.active_duplex;
4047
4048         if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4049             netif_carrier_ok(tp->dev) &&
4050             (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4051                 mac_status = tr32(MAC_STATUS);
4052                 mac_status &= (MAC_STATUS_PCS_SYNCED |
4053                                MAC_STATUS_SIGNAL_DET |
4054                                MAC_STATUS_CFG_CHANGED |
4055                                MAC_STATUS_RCVD_CFG);
4056                 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4057                                    MAC_STATUS_SIGNAL_DET)) {
4058                         tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4059                                             MAC_STATUS_CFG_CHANGED));
4060                         return 0;
4061                 }
4062         }
4063
4064         tw32_f(MAC_TX_AUTO_NEG, 0);
4065
4066         tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4067         tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4068         tw32_f(MAC_MODE, tp->mac_mode);
4069         udelay(40);
4070
4071         if (tp->phy_id == TG3_PHY_ID_BCM8002)
4072                 tg3_init_bcm8002(tp);
4073
4074         /* Enable link change event even when serdes polling.  */
4075         tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4076         udelay(40);
4077
4078         current_link_up = 0;
4079         mac_status = tr32(MAC_STATUS);
4080
4081         if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4082                 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4083         else
4084                 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4085
4086         tp->napi[0].hw_status->status =
4087                 (SD_STATUS_UPDATED |
4088                  (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
4089
4090         for (i = 0; i < 100; i++) {
4091                 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4092                                     MAC_STATUS_CFG_CHANGED));
4093                 udelay(5);
4094                 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
4095                                          MAC_STATUS_CFG_CHANGED |
4096                                          MAC_STATUS_LNKSTATE_CHANGED)) == 0)
4097                         break;
4098         }
4099
4100         mac_status = tr32(MAC_STATUS);
4101         if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4102                 current_link_up = 0;
4103                 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4104                     tp->serdes_counter == 0) {
4105                         tw32_f(MAC_MODE, (tp->mac_mode |
4106                                           MAC_MODE_SEND_CONFIGS));
4107                         udelay(1);
4108                         tw32_f(MAC_MODE, tp->mac_mode);
4109                 }
4110         }
4111
4112         if (current_link_up == 1) {
4113                 tp->link_config.active_speed = SPEED_1000;
4114                 tp->link_config.active_duplex = DUPLEX_FULL;
4115                 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4116                                     LED_CTRL_LNKLED_OVERRIDE |
4117                                     LED_CTRL_1000MBPS_ON));
4118         } else {
4119                 tp->link_config.active_speed = SPEED_INVALID;
4120                 tp->link_config.active_duplex = DUPLEX_INVALID;
4121                 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4122                                     LED_CTRL_LNKLED_OVERRIDE |
4123                                     LED_CTRL_TRAFFIC_OVERRIDE));
4124         }
4125
4126         if (current_link_up != netif_carrier_ok(tp->dev)) {
4127                 if (current_link_up)
4128                         netif_carrier_on(tp->dev);
4129                 else
4130                         netif_carrier_off(tp->dev);
4131                 tg3_link_report(tp);
4132         } else {
4133                 u32 now_pause_cfg = tp->link_config.active_flowctrl;
4134                 if (orig_pause_cfg != now_pause_cfg ||
4135                     orig_active_speed != tp->link_config.active_speed ||
4136                     orig_active_duplex != tp->link_config.active_duplex)
4137                         tg3_link_report(tp);
4138         }
4139
4140         return 0;
4141 }
4142
4143 static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4144 {
4145         int current_link_up, err = 0;
4146         u32 bmsr, bmcr;
4147         u16 current_speed;
4148         u8 current_duplex;
4149         u32 local_adv, remote_adv;
4150
4151         tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4152         tw32_f(MAC_MODE, tp->mac_mode);
4153         udelay(40);
4154
4155         tw32(MAC_EVENT, 0);
4156
4157         tw32_f(MAC_STATUS,
4158              (MAC_STATUS_SYNC_CHANGED |
4159               MAC_STATUS_CFG_CHANGED |
4160               MAC_STATUS_MI_COMPLETION |
4161               MAC_STATUS_LNKSTATE_CHANGED));
4162         udelay(40);
4163
4164         if (force_reset)
4165                 tg3_phy_reset(tp);
4166
4167         current_link_up = 0;
4168         current_speed = SPEED_INVALID;
4169         current_duplex = DUPLEX_INVALID;
4170
4171         err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4172         err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4173         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4174                 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4175                         bmsr |= BMSR_LSTATUS;
4176                 else
4177                         bmsr &= ~BMSR_LSTATUS;
4178         }
4179
4180         err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4181
4182         if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
4183             (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
4184                 /* do nothing, just check for link up at the end */
4185         } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4186                 u32 adv, new_adv;
4187
4188                 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4189                 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4190                                   ADVERTISE_1000XPAUSE |
4191                                   ADVERTISE_1000XPSE_ASYM |
4192                                   ADVERTISE_SLCT);
4193
4194                 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4195
4196                 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4197                         new_adv |= ADVERTISE_1000XHALF;
4198                 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4199                         new_adv |= ADVERTISE_1000XFULL;
4200
4201                 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4202                         tg3_writephy(tp, MII_ADVERTISE, new_adv);
4203                         bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4204                         tg3_writephy(tp, MII_BMCR, bmcr);
4205
4206                         tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4207                         tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
4208                         tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4209
4210                         return err;
4211                 }
4212         } else {
4213                 u32 new_bmcr;
4214
4215                 bmcr &= ~BMCR_SPEED1000;
4216                 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4217
4218                 if (tp->link_config.duplex == DUPLEX_FULL)
4219                         new_bmcr |= BMCR_FULLDPLX;
4220
4221                 if (new_bmcr != bmcr) {
4222                         /* BMCR_SPEED1000 is a reserved bit that needs
4223                          * to be set on write.
4224                          */
4225                         new_bmcr |= BMCR_SPEED1000;
4226
4227                         /* Force a linkdown */
4228                         if (netif_carrier_ok(tp->dev)) {
4229                                 u32 adv;
4230
4231                                 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4232                                 adv &= ~(ADVERTISE_1000XFULL |
4233                                          ADVERTISE_1000XHALF |
4234                                          ADVERTISE_SLCT);
4235                                 tg3_writephy(tp, MII_ADVERTISE, adv);
4236                                 tg3_writephy(tp, MII_BMCR, bmcr |
4237                                                            BMCR_ANRESTART |
4238                                                            BMCR_ANENABLE);
4239                                 udelay(10);
4240                                 netif_carrier_off(tp->dev);
4241                         }
4242                         tg3_writephy(tp, MII_BMCR, new_bmcr);
4243                         bmcr = new_bmcr;
4244                         err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4245                         err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4246                         if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4247                             ASIC_REV_5714) {
4248                                 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4249                                         bmsr |= BMSR_LSTATUS;
4250                                 else
4251                                         bmsr &= ~BMSR_LSTATUS;
4252                         }
4253                         tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4254                 }
4255         }
4256
4257         if (bmsr & BMSR_LSTATUS) {
4258                 current_speed = SPEED_1000;
4259                 current_link_up = 1;
4260                 if (bmcr & BMCR_FULLDPLX)
4261                         current_duplex = DUPLEX_FULL;
4262                 else
4263                         current_duplex = DUPLEX_HALF;
4264
4265                 local_adv = 0;
4266                 remote_adv = 0;
4267
4268                 if (bmcr & BMCR_ANENABLE) {
4269                         u32 common;
4270
4271                         err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4272                         err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4273                         common = local_adv & remote_adv;
4274                         if (common & (ADVERTISE_1000XHALF |
4275                                       ADVERTISE_1000XFULL)) {
4276                                 if (common & ADVERTISE_1000XFULL)
4277                                         current_duplex = DUPLEX_FULL;
4278                                 else
4279                                         current_duplex = DUPLEX_HALF;
4280                         } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4281                                 /* Link is up via parallel detect */
4282                         } else {
4283                                 current_link_up = 0;
4284                         }
4285                 }
4286         }
4287
4288         if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4289                 tg3_setup_flow_control(tp, local_adv, remote_adv);
4290
4291         tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4292         if (tp->link_config.active_duplex == DUPLEX_HALF)
4293                 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4294
4295         tw32_f(MAC_MODE, tp->mac_mode);
4296         udelay(40);
4297
4298         tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4299
4300         tp->link_config.active_speed = current_speed;
4301         tp->link_config.active_duplex = current_duplex;
4302
4303         if (current_link_up != netif_carrier_ok(tp->dev)) {
4304                 if (current_link_up)
4305                         netif_carrier_on(tp->dev);
4306                 else {
4307                         netif_carrier_off(tp->dev);
4308                         tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4309                 }
4310                 tg3_link_report(tp);
4311         }
4312         return err;
4313 }
4314
4315 static void tg3_serdes_parallel_detect(struct tg3 *tp)
4316 {
4317         if (tp->serdes_counter) {
4318                 /* Give autoneg time to complete. */
4319                 tp->serdes_counter--;
4320                 return;
4321         }
4322
4323         if (!netif_carrier_ok(tp->dev) &&
4324             (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4325                 u32 bmcr;
4326
4327                 tg3_readphy(tp, MII_BMCR, &bmcr);
4328                 if (bmcr & BMCR_ANENABLE) {
4329                         u32 phy1, phy2;
4330
4331                         /* Select shadow register 0x1f */
4332                         tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4333                         tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
4334
4335                         /* Select expansion interrupt status register */
4336                         tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4337                                          MII_TG3_DSP_EXP1_INT_STAT);
4338                         tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4339                         tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4340
4341                         if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4342                                 /* We have signal detect and not receiving
4343                                  * config code words, link is up by parallel
4344                                  * detection.
4345                                  */
4346
4347                                 bmcr &= ~BMCR_ANENABLE;
4348                                 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4349                                 tg3_writephy(tp, MII_BMCR, bmcr);
4350                                 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
4351                         }
4352                 }
4353         } else if (netif_carrier_ok(tp->dev) &&
4354                    (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4355                    (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
4356                 u32 phy2;
4357
4358                 /* Select expansion interrupt status register */
4359                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4360                                  MII_TG3_DSP_EXP1_INT_STAT);
4361                 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4362                 if (phy2 & 0x20) {
4363                         u32 bmcr;
4364
4365                         /* Config code words received, turn on autoneg. */
4366                         tg3_readphy(tp, MII_BMCR, &bmcr);
4367                         tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4368
4369                         tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4370
4371                 }
4372         }
4373 }
4374
4375 static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4376 {
4377         int err;
4378
4379         if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
4380                 err = tg3_setup_fiber_phy(tp, force_reset);
4381         else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4382                 err = tg3_setup_fiber_mii_phy(tp, force_reset);
4383         else
4384                 err = tg3_setup_copper_phy(tp, force_reset);
4385
4386         if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
4387                 u32 val, scale;
4388
4389                 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4390                 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4391                         scale = 65;
4392                 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4393                         scale = 6;
4394                 else
4395                         scale = 12;
4396
4397                 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4398                 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4399                 tw32(GRC_MISC_CFG, val);
4400         }
4401
4402         if (tp->link_config.active_speed == SPEED_1000 &&
4403             tp->link_config.active_duplex == DUPLEX_HALF)
4404                 tw32(MAC_TX_LENGTHS,
4405                      ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4406                       (6 << TX_LENGTHS_IPG_SHIFT) |
4407                       (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4408         else
4409                 tw32(MAC_TX_LENGTHS,
4410                      ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4411                       (6 << TX_LENGTHS_IPG_SHIFT) |
4412                       (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4413
4414         if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4415                 if (netif_carrier_ok(tp->dev)) {
4416                         tw32(HOSTCC_STAT_COAL_TICKS,
4417                              tp->coal.stats_block_coalesce_usecs);
4418                 } else {
4419                         tw32(HOSTCC_STAT_COAL_TICKS, 0);
4420                 }
4421         }
4422
4423         if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4424                 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4425                 if (!netif_carrier_ok(tp->dev))
4426                         val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4427                               tp->pwrmgmt_thresh;
4428                 else
4429                         val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4430                 tw32(PCIE_PWR_MGMT_THRESH, val);
4431         }
4432
4433         return err;
4434 }
4435
4436 static inline int tg3_irq_sync(struct tg3 *tp)
4437 {
4438         return tp->irq_sync;
4439 }
4440
4441 /* This is called whenever we suspect that the system chipset is re-
4442  * ordering the sequence of MMIO to the tx send mailbox. The symptom
4443  * is bogus tx completions. We try to recover by setting the
4444  * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4445  * in the workqueue.
4446  */
4447 static void tg3_tx_recover(struct tg3 *tp)
4448 {
4449         BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4450                tp->write32_tx_mbox == tg3_write_indirect_mbox);
4451
4452         netdev_warn(tp->dev,
4453                     "The system may be re-ordering memory-mapped I/O "
4454                     "cycles to the network device, attempting to recover. "
4455                     "Please report the problem to the driver maintainer "
4456                     "and include system chipset information.\n");
4457
4458         spin_lock(&tp->lock);
4459         tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
4460         spin_unlock(&tp->lock);
4461 }
4462
4463 static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
4464 {
4465         /* Tell compiler to fetch tx indices from memory. */
4466         barrier();
4467         return tnapi->tx_pending -
4468                ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
4469 }
4470
4471 /* Tigon3 never reports partial packet sends.  So we do not
4472  * need special logic to handle SKBs that have not had all
4473  * of their frags sent yet, like SunGEM does.
4474  */
4475 static void tg3_tx(struct tg3_napi *tnapi)
4476 {
4477         struct tg3 *tp = tnapi->tp;
4478         u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
4479         u32 sw_idx = tnapi->tx_cons;
4480         struct netdev_queue *txq;
4481         int index = tnapi - tp->napi;
4482
4483         if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
4484                 index--;
4485
4486         txq = netdev_get_tx_queue(tp->dev, index);
4487
4488         while (sw_idx != hw_idx) {
4489                 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
4490                 struct sk_buff *skb = ri->skb;
4491                 int i, tx_bug = 0;
4492
4493                 if (unlikely(skb == NULL)) {
4494                         tg3_tx_recover(tp);
4495                         return;
4496                 }
4497
4498                 pci_unmap_single(tp->pdev,
4499                                  dma_unmap_addr(ri, mapping),
4500                                  skb_headlen(skb),
4501                                  PCI_DMA_TODEVICE);
4502
4503                 ri->skb = NULL;
4504
4505                 sw_idx = NEXT_TX(sw_idx);
4506
4507                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4508                         ri = &tnapi->tx_buffers[sw_idx];
4509                         if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4510                                 tx_bug = 1;
4511
4512                         pci_unmap_page(tp->pdev,
4513                                        dma_unmap_addr(ri, mapping),
4514                                        skb_shinfo(skb)->frags[i].size,
4515                                        PCI_DMA_TODEVICE);
4516                         sw_idx = NEXT_TX(sw_idx);
4517                 }
4518
4519                 dev_kfree_skb(skb);
4520
4521                 if (unlikely(tx_bug)) {
4522                         tg3_tx_recover(tp);
4523                         return;
4524                 }
4525         }
4526
4527         tnapi->tx_cons = sw_idx;
4528
4529         /* Need to make the tx_cons update visible to tg3_start_xmit()
4530          * before checking for netif_queue_stopped().  Without the
4531          * memory barrier, there is a small possibility that tg3_start_xmit()
4532          * will miss it and cause the queue to be stopped forever.
4533          */
4534         smp_mb();
4535
4536         if (unlikely(netif_tx_queue_stopped(txq) &&
4537                      (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
4538                 __netif_tx_lock(txq, smp_processor_id());
4539                 if (netif_tx_queue_stopped(txq) &&
4540                     (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
4541                         netif_tx_wake_queue(txq);
4542                 __netif_tx_unlock(txq);
4543         }
4544 }
4545
4546 static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4547 {
4548         if (!ri->skb)
4549                 return;
4550
4551         pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
4552                          map_sz, PCI_DMA_FROMDEVICE);
4553         dev_kfree_skb_any(ri->skb);
4554         ri->skb = NULL;
4555 }
4556
4557 /* Returns size of skb allocated or < 0 on error.
4558  *
4559  * We only need to fill in the address because the other members
4560  * of the RX descriptor are invariant, see tg3_init_rings.
4561  *
4562  * Note the purposeful assymetry of cpu vs. chip accesses.  For
4563  * posting buffers we only dirty the first cache line of the RX
4564  * descriptor (containing the address).  Whereas for the RX status
4565  * buffers the cpu only reads the last cacheline of the RX descriptor
4566  * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4567  */
4568 static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
4569                             u32 opaque_key, u32 dest_idx_unmasked)
4570 {
4571         struct tg3_rx_buffer_desc *desc;
4572         struct ring_info *map;
4573         struct sk_buff *skb;
4574         dma_addr_t mapping;
4575         int skb_size, dest_idx;
4576
4577         switch (opaque_key) {
4578         case RXD_OPAQUE_RING_STD:
4579                 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
4580                 desc = &tpr->rx_std[dest_idx];
4581                 map = &tpr->rx_std_buffers[dest_idx];
4582                 skb_size = tp->rx_pkt_map_sz;
4583                 break;
4584
4585         case RXD_OPAQUE_RING_JUMBO:
4586                 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
4587                 desc = &tpr->rx_jmb[dest_idx].std;
4588                 map = &tpr->rx_jmb_buffers[dest_idx];
4589                 skb_size = TG3_RX_JMB_MAP_SZ;
4590                 break;
4591
4592         default:
4593                 return -EINVAL;
4594         }
4595
4596         /* Do not overwrite any of the map or rp information
4597          * until we are sure we can commit to a new buffer.
4598          *
4599          * Callers depend upon this behavior and assume that
4600          * we leave everything unchanged if we fail.
4601          */
4602         skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
4603         if (skb == NULL)
4604                 return -ENOMEM;
4605
4606         skb_reserve(skb, tp->rx_offset);
4607
4608         mapping = pci_map_single(tp->pdev, skb->data, skb_size,
4609                                  PCI_DMA_FROMDEVICE);
4610         if (pci_dma_mapping_error(tp->pdev, mapping)) {
4611                 dev_kfree_skb(skb);
4612                 return -EIO;
4613         }
4614
4615         map->skb = skb;
4616         dma_unmap_addr_set(map, mapping, mapping);
4617
4618         desc->addr_hi = ((u64)mapping >> 32);
4619         desc->addr_lo = ((u64)mapping & 0xffffffff);
4620
4621         return skb_size;
4622 }
4623
4624 /* We only need to move over in the address because the other
4625  * members of the RX descriptor are invariant.  See notes above
4626  * tg3_alloc_rx_skb for full details.
4627  */
4628 static void tg3_recycle_rx(struct tg3_napi *tnapi,
4629                            struct tg3_rx_prodring_set *dpr,
4630                            u32 opaque_key, int src_idx,
4631                            u32 dest_idx_unmasked)
4632 {
4633         struct tg3 *tp = tnapi->tp;
4634         struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4635         struct ring_info *src_map, *dest_map;
4636         struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
4637         int dest_idx;
4638
4639         switch (opaque_key) {
4640         case RXD_OPAQUE_RING_STD:
4641                 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
4642                 dest_desc = &dpr->rx_std[dest_idx];
4643                 dest_map = &dpr->rx_std_buffers[dest_idx];
4644                 src_desc = &spr->rx_std[src_idx];
4645                 src_map = &spr->rx_std_buffers[src_idx];
4646                 break;
4647
4648         case RXD_OPAQUE_RING_JUMBO:
4649                 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
4650                 dest_desc = &dpr->rx_jmb[dest_idx].std;
4651                 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4652                 src_desc = &spr->rx_jmb[src_idx].std;
4653                 src_map = &spr->rx_jmb_buffers[src_idx];
4654                 break;
4655
4656         default:
4657                 return;
4658         }
4659
4660         dest_map->skb = src_map->skb;
4661         dma_unmap_addr_set(dest_map, mapping,
4662                            dma_unmap_addr(src_map, mapping));
4663         dest_desc->addr_hi = src_desc->addr_hi;
4664         dest_desc->addr_lo = src_desc->addr_lo;
4665
4666         /* Ensure that the update to the skb happens after the physical
4667          * addresses have been transferred to the new BD location.
4668          */
4669         smp_wmb();
4670
4671         src_map->skb = NULL;
4672 }
4673
4674 /* The RX ring scheme is composed of multiple rings which post fresh
4675  * buffers to the chip, and one special ring the chip uses to report
4676  * status back to the host.
4677  *
4678  * The special ring reports the status of received packets to the
4679  * host.  The chip does not write into the original descriptor the
4680  * RX buffer was obtained from.  The chip simply takes the original
4681  * descriptor as provided by the host, updates the status and length
4682  * field, then writes this into the next status ring entry.
4683  *
4684  * Each ring the host uses to post buffers to the chip is described
4685  * by a TG3_BDINFO entry in the chips SRAM area.  When a packet arrives,
4686  * it is first placed into the on-chip ram.  When the packet's length
4687  * is known, it walks down the TG3_BDINFO entries to select the ring.
4688  * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4689  * which is within the range of the new packet's length is chosen.
4690  *
4691  * The "separate ring for rx status" scheme may sound queer, but it makes
4692  * sense from a cache coherency perspective.  If only the host writes
4693  * to the buffer post rings, and only the chip writes to the rx status
4694  * rings, then cache lines never move beyond shared-modified state.
4695  * If both the host and chip were to write into the same ring, cache line
4696  * eviction could occur since both entities want it in an exclusive state.
4697  */
4698 static int tg3_rx(struct tg3_napi *tnapi, int budget)
4699 {
4700         struct tg3 *tp = tnapi->tp;
4701         u32 work_mask, rx_std_posted = 0;
4702         u32 std_prod_idx, jmb_prod_idx;
4703         u32 sw_idx = tnapi->rx_rcb_ptr;
4704         u16 hw_idx;
4705         int received;
4706         struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
4707
4708         hw_idx = *(tnapi->rx_rcb_prod_idx);
4709         /*
4710          * We need to order the read of hw_idx and the read of
4711          * the opaque cookie.
4712          */
4713         rmb();
4714         work_mask = 0;
4715         received = 0;
4716         std_prod_idx = tpr->rx_std_prod_idx;
4717         jmb_prod_idx = tpr->rx_jmb_prod_idx;
4718         while (sw_idx != hw_idx && budget > 0) {
4719                 struct ring_info *ri;
4720                 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
4721                 unsigned int len;
4722                 struct sk_buff *skb;
4723                 dma_addr_t dma_addr;
4724                 u32 opaque_key, desc_idx, *post_ptr;
4725                 bool hw_vlan __maybe_unused = false;
4726                 u16 vtag __maybe_unused = 0;
4727
4728                 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4729                 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4730                 if (opaque_key == RXD_OPAQUE_RING_STD) {
4731                         ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
4732                         dma_addr = dma_unmap_addr(ri, mapping);
4733                         skb = ri->skb;
4734                         post_ptr = &std_prod_idx;
4735                         rx_std_posted++;
4736                 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
4737                         ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
4738                         dma_addr = dma_unmap_addr(ri, mapping);
4739                         skb = ri->skb;
4740                         post_ptr = &jmb_prod_idx;
4741                 } else
4742                         goto next_pkt_nopost;
4743
4744                 work_mask |= opaque_key;
4745
4746                 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4747                     (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4748                 drop_it:
4749                         tg3_recycle_rx(tnapi, tpr, opaque_key,
4750                                        desc_idx, *post_ptr);
4751                 drop_it_no_recycle:
4752                         /* Other statistics kept track of by card. */
4753                         tp->rx_dropped++;
4754                         goto next_pkt;
4755                 }
4756
4757                 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4758                       ETH_FCS_LEN;
4759
4760                 if (len > TG3_RX_COPY_THRESH(tp)) {
4761                         int skb_size;
4762
4763                         skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
4764                                                     *post_ptr);
4765                         if (skb_size < 0)
4766                                 goto drop_it;
4767
4768                         pci_unmap_single(tp->pdev, dma_addr, skb_size,
4769                                          PCI_DMA_FROMDEVICE);
4770
4771                         /* Ensure that the update to the skb happens
4772                          * after the usage of the old DMA mapping.
4773                          */
4774                         smp_wmb();
4775
4776                         ri->skb = NULL;
4777
4778                         skb_put(skb, len);
4779                 } else {
4780                         struct sk_buff *copy_skb;
4781
4782                         tg3_recycle_rx(tnapi, tpr, opaque_key,
4783                                        desc_idx, *post_ptr);
4784
4785                         copy_skb = netdev_alloc_skb(tp->dev, len + VLAN_HLEN +
4786                                                     TG3_RAW_IP_ALIGN);
4787                         if (copy_skb == NULL)
4788                                 goto drop_it_no_recycle;
4789
4790                         skb_reserve(copy_skb, TG3_RAW_IP_ALIGN + VLAN_HLEN);
4791                         skb_put(copy_skb, len);
4792                         pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4793                         skb_copy_from_linear_data(skb, copy_skb->data, len);
4794                         pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4795
4796                         /* We'll reuse the original ring buffer. */
4797                         skb = copy_skb;
4798                 }
4799
4800                 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4801                     (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4802                     (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4803                       >> RXD_TCPCSUM_SHIFT) == 0xffff))
4804                         skb->ip_summed = CHECKSUM_UNNECESSARY;
4805                 else
4806                         skb_checksum_none_assert(skb);
4807
4808                 skb->protocol = eth_type_trans(skb, tp->dev);
4809
4810                 if (len > (tp->dev->mtu + ETH_HLEN) &&
4811                     skb->protocol != htons(ETH_P_8021Q)) {
4812                         dev_kfree_skb(skb);
4813                         goto drop_it_no_recycle;
4814                 }
4815
4816                 if (desc->type_flags & RXD_FLAG_VLAN &&
4817                     !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG)) {
4818                         vtag = desc->err_vlan & RXD_VLAN_MASK;
4819 #if TG3_VLAN_TAG_USED
4820                         if (tp->vlgrp)
4821                                 hw_vlan = true;
4822                         else
4823 #endif
4824                         {
4825                                 struct vlan_ethhdr *ve = (struct vlan_ethhdr *)
4826                                                     __skb_push(skb, VLAN_HLEN);
4827
4828                                 memmove(ve, skb->data + VLAN_HLEN,
4829                                         ETH_ALEN * 2);
4830                                 ve->h_vlan_proto = htons(ETH_P_8021Q);
4831                                 ve->h_vlan_TCI = htons(vtag);
4832                         }
4833                 }
4834
4835 #if TG3_VLAN_TAG_USED
4836                 if (hw_vlan)
4837                         vlan_gro_receive(&tnapi->napi, tp->vlgrp, vtag, skb);
4838                 else
4839 #endif
4840                         napi_gro_receive(&tnapi->napi, skb);
4841
4842                 received++;
4843                 budget--;
4844
4845 next_pkt:
4846                 (*post_ptr)++;
4847
4848                 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4849                         tpr->rx_std_prod_idx = std_prod_idx &
4850                                                tp->rx_std_ring_mask;
4851                         tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4852                                      tpr->rx_std_prod_idx);
4853                         work_mask &= ~RXD_OPAQUE_RING_STD;
4854                         rx_std_posted = 0;
4855                 }
4856 next_pkt_nopost:
4857                 sw_idx++;
4858                 sw_idx &= tp->rx_ret_ring_mask;
4859
4860                 /* Refresh hw_idx to see if there is new work */
4861                 if (sw_idx == hw_idx) {
4862                         hw_idx = *(tnapi->rx_rcb_prod_idx);
4863                         rmb();
4864                 }
4865         }
4866
4867         /* ACK the status ring. */
4868         tnapi->rx_rcb_ptr = sw_idx;
4869         tw32_rx_mbox(tnapi->consmbox, sw_idx);
4870
4871         /* Refill RX ring(s). */
4872         if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
4873                 if (work_mask & RXD_OPAQUE_RING_STD) {
4874                         tpr->rx_std_prod_idx = std_prod_idx &
4875                                                tp->rx_std_ring_mask;
4876                         tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4877                                      tpr->rx_std_prod_idx);
4878                 }
4879                 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4880                         tpr->rx_jmb_prod_idx = jmb_prod_idx &
4881                                                tp->rx_jmb_ring_mask;
4882                         tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4883                                      tpr->rx_jmb_prod_idx);
4884                 }
4885                 mmiowb();
4886         } else if (work_mask) {
4887                 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4888                  * updated before the producer indices can be updated.
4889                  */
4890                 smp_wmb();
4891
4892                 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
4893                 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
4894
4895                 if (tnapi != &tp->napi[1])
4896                         napi_schedule(&tp->napi[1].napi);
4897         }
4898
4899         return received;
4900 }
4901
4902 static void tg3_poll_link(struct tg3 *tp)
4903 {
4904         /* handle link change and other phy events */
4905         if (!(tp->tg3_flags &
4906               (TG3_FLAG_USE_LINKCHG_REG |
4907                TG3_FLAG_POLL_SERDES))) {
4908                 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4909
4910                 if (sblk->status & SD_STATUS_LINK_CHG) {
4911                         sblk->status = SD_STATUS_UPDATED |
4912                                        (sblk->status & ~SD_STATUS_LINK_CHG);
4913                         spin_lock(&tp->lock);
4914                         if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4915                                 tw32_f(MAC_STATUS,
4916                                      (MAC_STATUS_SYNC_CHANGED |
4917                                       MAC_STATUS_CFG_CHANGED |
4918                                       MAC_STATUS_MI_COMPLETION |
4919                                       MAC_STATUS_LNKSTATE_CHANGED));
4920                                 udelay(40);
4921                         } else
4922                                 tg3_setup_phy(tp, 0);
4923                         spin_unlock(&tp->lock);
4924                 }
4925         }
4926 }
4927
4928 static int tg3_rx_prodring_xfer(struct tg3 *tp,
4929                                 struct tg3_rx_prodring_set *dpr,
4930                                 struct tg3_rx_prodring_set *spr)
4931 {
4932         u32 si, di, cpycnt, src_prod_idx;
4933         int i, err = 0;
4934
4935         while (1) {
4936                 src_prod_idx = spr->rx_std_prod_idx;
4937
4938                 /* Make sure updates to the rx_std_buffers[] entries and the
4939                  * standard producer index are seen in the correct order.
4940                  */
4941                 smp_rmb();
4942
4943                 if (spr->rx_std_cons_idx == src_prod_idx)
4944                         break;
4945
4946                 if (spr->rx_std_cons_idx < src_prod_idx)
4947                         cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4948                 else
4949                         cpycnt = tp->rx_std_ring_mask + 1 -
4950                                  spr->rx_std_cons_idx;
4951
4952                 cpycnt = min(cpycnt,
4953                              tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
4954
4955                 si = spr->rx_std_cons_idx;
4956                 di = dpr->rx_std_prod_idx;
4957
4958                 for (i = di; i < di + cpycnt; i++) {
4959                         if (dpr->rx_std_buffers[i].skb) {
4960                                 cpycnt = i - di;
4961                                 err = -ENOSPC;
4962                                 break;
4963                         }
4964                 }
4965
4966                 if (!cpycnt)
4967                         break;
4968
4969                 /* Ensure that updates to the rx_std_buffers ring and the
4970                  * shadowed hardware producer ring from tg3_recycle_skb() are
4971                  * ordered correctly WRT the skb check above.
4972                  */
4973                 smp_rmb();
4974
4975                 memcpy(&dpr->rx_std_buffers[di],
4976                        &spr->rx_std_buffers[si],
4977                        cpycnt * sizeof(struct ring_info));
4978
4979                 for (i = 0; i < cpycnt; i++, di++, si++) {
4980                         struct tg3_rx_buffer_desc *sbd, *dbd;
4981                         sbd = &spr->rx_std[si];
4982                         dbd = &dpr->rx_std[di];
4983                         dbd->addr_hi = sbd->addr_hi;
4984                         dbd->addr_lo = sbd->addr_lo;
4985                 }
4986
4987                 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
4988                                        tp->rx_std_ring_mask;
4989                 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
4990                                        tp->rx_std_ring_mask;
4991         }
4992
4993         while (1) {
4994                 src_prod_idx = spr->rx_jmb_prod_idx;
4995
4996                 /* Make sure updates to the rx_jmb_buffers[] entries and
4997                  * the jumbo producer index are seen in the correct order.
4998                  */
4999                 smp_rmb();
5000
5001                 if (spr->rx_jmb_cons_idx == src_prod_idx)
5002                         break;
5003
5004                 if (spr->rx_jmb_cons_idx < src_prod_idx)
5005                         cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5006                 else
5007                         cpycnt = tp->rx_jmb_ring_mask + 1 -
5008                                  spr->rx_jmb_cons_idx;
5009
5010                 cpycnt = min(cpycnt,
5011                              tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
5012
5013                 si = spr->rx_jmb_cons_idx;
5014                 di = dpr->rx_jmb_prod_idx;
5015
5016                 for (i = di; i < di + cpycnt; i++) {
5017                         if (dpr->rx_jmb_buffers[i].skb) {
5018                                 cpycnt = i - di;
5019                                 err = -ENOSPC;
5020                                 break;
5021                         }
5022                 }
5023
5024                 if (!cpycnt)
5025                         break;
5026
5027                 /* Ensure that updates to the rx_jmb_buffers ring and the
5028                  * shadowed hardware producer ring from tg3_recycle_skb() are
5029                  * ordered correctly WRT the skb check above.
5030                  */
5031                 smp_rmb();
5032
5033                 memcpy(&dpr->rx_jmb_buffers[di],
5034                        &spr->rx_jmb_buffers[si],
5035                        cpycnt * sizeof(struct ring_info));
5036
5037                 for (i = 0; i < cpycnt; i++, di++, si++) {
5038                         struct tg3_rx_buffer_desc *sbd, *dbd;
5039                         sbd = &spr->rx_jmb[si].std;
5040                         dbd = &dpr->rx_jmb[di].std;
5041                         dbd->addr_hi = sbd->addr_hi;
5042                         dbd->addr_lo = sbd->addr_lo;
5043                 }
5044
5045                 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5046                                        tp->rx_jmb_ring_mask;
5047                 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5048                                        tp->rx_jmb_ring_mask;
5049         }
5050
5051         return err;
5052 }
5053
5054 static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5055 {
5056         struct tg3 *tp = tnapi->tp;
5057
5058         /* run TX completion thread */
5059         if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
5060                 tg3_tx(tnapi);
5061                 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5062                         return work_done;
5063         }
5064
5065         /* run RX thread, within the bounds set by NAPI.
5066          * All RX "locking" is done by ensuring outside
5067          * code synchronizes with tg3->napi.poll()
5068          */
5069         if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
5070                 work_done += tg3_rx(tnapi, budget - work_done);
5071
5072         if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
5073                 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
5074                 int i, err = 0;
5075                 u32 std_prod_idx = dpr->rx_std_prod_idx;
5076                 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
5077
5078                 for (i = 1; i < tp->irq_cnt; i++)
5079                         err |= tg3_rx_prodring_xfer(tp, dpr,
5080                                                     &tp->napi[i].prodring);
5081
5082                 wmb();
5083
5084                 if (std_prod_idx != dpr->rx_std_prod_idx)
5085                         tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5086                                      dpr->rx_std_prod_idx);
5087
5088                 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5089                         tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5090                                      dpr->rx_jmb_prod_idx);
5091
5092                 mmiowb();
5093
5094                 if (err)
5095                         tw32_f(HOSTCC_MODE, tp->coal_now);
5096         }
5097
5098         return work_done;
5099 }
5100
5101 static int tg3_poll_msix(struct napi_struct *napi, int budget)
5102 {
5103         struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5104         struct tg3 *tp = tnapi->tp;
5105         int work_done = 0;
5106         struct tg3_hw_status *sblk = tnapi->hw_status;
5107
5108         while (1) {
5109                 work_done = tg3_poll_work(tnapi, work_done, budget);
5110
5111                 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5112                         goto tx_recovery;
5113
5114                 if (unlikely(work_done >= budget))
5115                         break;
5116
5117                 /* tp->last_tag is used in tg3_int_reenable() below
5118                  * to tell the hw how much work has been processed,
5119                  * so we must read it before checking for more work.
5120                  */
5121                 tnapi->last_tag = sblk->status_tag;
5122                 tnapi->last_irq_tag = tnapi->last_tag;
5123                 rmb();
5124
5125                 /* check for RX/TX work to do */
5126                 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5127                            *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
5128                         napi_complete(napi);
5129                         /* Reenable interrupts. */
5130                         tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5131                         mmiowb();
5132                         break;
5133                 }
5134         }
5135
5136         return work_done;
5137
5138 tx_recovery:
5139         /* work_done is guaranteed to be less than budget. */
5140         napi_complete(napi);
5141         schedule_work(&tp->reset_task);
5142         return work_done;
5143 }
5144
5145 static int tg3_poll(struct napi_struct *napi, int budget)
5146 {
5147         struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5148         struct tg3 *tp = tnapi->tp;
5149         int work_done = 0;
5150         struct tg3_hw_status *sblk = tnapi->hw_status;
5151
5152         while (1) {
5153                 tg3_poll_link(tp);
5154
5155                 work_done = tg3_poll_work(tnapi, work_done, budget);
5156
5157                 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5158                         goto tx_recovery;
5159
5160                 if (unlikely(work_done >= budget))
5161                         break;
5162
5163                 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
5164                         /* tp->last_tag is used in tg3_int_reenable() below
5165                          * to tell the hw how much work has been processed,
5166                          * so we must read it before checking for more work.
5167                          */
5168                         tnapi->last_tag = sblk->status_tag;
5169                         tnapi->last_irq_tag = tnapi->last_tag;
5170                         rmb();
5171                 } else
5172                         sblk->status &= ~SD_STATUS_UPDATED;
5173
5174                 if (likely(!tg3_has_work(tnapi))) {
5175                         napi_complete(napi);
5176                         tg3_int_reenable(tnapi);
5177                         break;
5178                 }
5179         }
5180
5181         return work_done;
5182
5183 tx_recovery:
5184         /* work_done is guaranteed to be less than budget. */
5185         napi_complete(napi);
5186         schedule_work(&tp->reset_task);
5187         return work_done;
5188 }
5189
5190 static void tg3_napi_disable(struct tg3 *tp)
5191 {
5192         int i;
5193
5194         for (i = tp->irq_cnt - 1; i >= 0; i--)
5195                 napi_disable(&tp->napi[i].napi);
5196 }
5197
5198 static void tg3_napi_enable(struct tg3 *tp)
5199 {
5200         int i;
5201
5202         for (i = 0; i < tp->irq_cnt; i++)
5203                 napi_enable(&tp->napi[i].napi);
5204 }
5205
5206 static void tg3_napi_init(struct tg3 *tp)
5207 {
5208         int i;
5209
5210         netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5211         for (i = 1; i < tp->irq_cnt; i++)
5212                 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5213 }
5214
5215 static void tg3_napi_fini(struct tg3 *tp)
5216 {
5217         int i;
5218
5219         for (i = 0; i < tp->irq_cnt; i++)
5220                 netif_napi_del(&tp->napi[i].napi);
5221 }
5222
5223 static inline void tg3_netif_stop(struct tg3 *tp)
5224 {
5225         tp->dev->trans_start = jiffies; /* prevent tx timeout */
5226         tg3_napi_disable(tp);
5227         netif_tx_disable(tp->dev);
5228 }
5229
5230 static inline void tg3_netif_start(struct tg3 *tp)
5231 {
5232         /* NOTE: unconditional netif_tx_wake_all_queues is only
5233          * appropriate so long as all callers are assured to
5234          * have free tx slots (such as after tg3_init_hw)
5235          */
5236         netif_tx_wake_all_queues(tp->dev);
5237
5238         tg3_napi_enable(tp);
5239         tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5240         tg3_enable_ints(tp);
5241 }
5242
5243 static void tg3_irq_quiesce(struct tg3 *tp)
5244 {
5245         int i;
5246
5247         BUG_ON(tp->irq_sync);
5248
5249         tp->irq_sync = 1;
5250         smp_mb();
5251
5252         for (i = 0; i < tp->irq_cnt; i++)
5253                 synchronize_irq(tp->napi[i].irq_vec);
5254 }
5255
5256 /* Fully shutdown all tg3 driver activity elsewhere in the system.
5257  * If irq_sync is non-zero, then the IRQ handler must be synchronized
5258  * with as well.  Most of the time, this is not necessary except when
5259  * shutting down the device.
5260  */
5261 static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5262 {
5263         spin_lock_bh(&tp->lock);
5264         if (irq_sync)
5265                 tg3_irq_quiesce(tp);
5266 }
5267
5268 static inline void tg3_full_unlock(struct tg3 *tp)
5269 {
5270         spin_unlock_bh(&tp->lock);
5271 }
5272
5273 /* One-shot MSI handler - Chip automatically disables interrupt
5274  * after sending MSI so driver doesn't have to do it.
5275  */
5276 static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
5277 {
5278         struct tg3_napi *tnapi = dev_id;
5279         struct tg3 *tp = tnapi->tp;
5280
5281         prefetch(tnapi->hw_status);
5282         if (tnapi->rx_rcb)
5283                 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5284
5285         if (likely(!tg3_irq_sync(tp)))
5286                 napi_schedule(&tnapi->napi);
5287
5288         return IRQ_HANDLED;
5289 }
5290
5291 /* MSI ISR - No need to check for interrupt sharing and no need to
5292  * flush status block and interrupt mailbox. PCI ordering rules
5293  * guarantee that MSI will arrive after the status block.
5294  */
5295 static irqreturn_t tg3_msi(int irq, void *dev_id)
5296 {
5297         struct tg3_napi *tnapi = dev_id;
5298         struct tg3 *tp = tnapi->tp;
5299
5300         prefetch(tnapi->hw_status);
5301         if (tnapi->rx_rcb)
5302                 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5303         /*
5304          * Writing any value to intr-mbox-0 clears PCI INTA# and
5305          * chip-internal interrupt pending events.
5306          * Writing non-zero to intr-mbox-0 additional tells the
5307          * NIC to stop sending us irqs, engaging "in-intr-handler"
5308          * event coalescing.
5309          */
5310         tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
5311         if (likely(!tg3_irq_sync(tp)))
5312                 napi_schedule(&tnapi->napi);
5313
5314         return IRQ_RETVAL(1);
5315 }
5316
5317 static irqreturn_t tg3_interrupt(int irq, void *dev_id)
5318 {
5319         struct tg3_napi *tnapi = dev_id;
5320         struct tg3 *tp = tnapi->tp;
5321         struct tg3_hw_status *sblk = tnapi->hw_status;
5322         unsigned int handled = 1;
5323
5324         /* In INTx mode, it is possible for the interrupt to arrive at
5325          * the CPU before the status block posted prior to the interrupt.
5326          * Reading the PCI State register will confirm whether the
5327          * interrupt is ours and will flush the status block.
5328          */
5329         if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5330                 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5331                     (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5332                         handled = 0;
5333                         goto out;
5334                 }
5335         }
5336
5337         /*
5338          * Writing any value to intr-mbox-0 clears PCI INTA# and
5339          * chip-internal interrupt pending events.
5340          * Writing non-zero to intr-mbox-0 additional tells the
5341          * NIC to stop sending us irqs, engaging "in-intr-handler"
5342          * event coalescing.
5343          *
5344          * Flush the mailbox to de-assert the IRQ immediately to prevent
5345          * spurious interrupts.  The flush impacts performance but
5346          * excessive spurious interrupts can be worse in some cases.
5347          */
5348         tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
5349         if (tg3_irq_sync(tp))
5350                 goto out;
5351         sblk->status &= ~SD_STATUS_UPDATED;
5352         if (likely(tg3_has_work(tnapi))) {
5353                 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5354                 napi_schedule(&tnapi->napi);
5355         } else {
5356                 /* No work, shared interrupt perhaps?  re-enable
5357                  * interrupts, and flush that PCI write
5358                  */
5359                 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5360                                0x00000000);
5361         }
5362 out:
5363         return IRQ_RETVAL(handled);
5364 }
5365
5366 static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
5367 {
5368         struct tg3_napi *tnapi = dev_id;
5369         struct tg3 *tp = tnapi->tp;
5370         struct tg3_hw_status *sblk = tnapi->hw_status;
5371         unsigned int handled = 1;
5372
5373         /* In INTx mode, it is possible for the interrupt to arrive at
5374          * the CPU before the status block posted prior to the interrupt.
5375          * Reading the PCI State register will confirm whether the
5376          * interrupt is ours and will flush the status block.
5377          */
5378         if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
5379                 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5380                     (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5381                         handled = 0;
5382                         goto out;
5383                 }
5384         }
5385
5386         /*
5387          * writing any value to intr-mbox-0 clears PCI INTA# and
5388          * chip-internal interrupt pending events.
5389          * writing non-zero to intr-mbox-0 additional tells the
5390          * NIC to stop sending us irqs, engaging "in-intr-handler"
5391          * event coalescing.
5392          *
5393          * Flush the mailbox to de-assert the IRQ immediately to prevent
5394          * spurious interrupts.  The flush impacts performance but
5395          * excessive spurious interrupts can be worse in some cases.
5396          */
5397         tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
5398
5399         /*
5400          * In a shared interrupt configuration, sometimes other devices'
5401          * interrupts will scream.  We record the current status tag here
5402          * so that the above check can report that the screaming interrupts
5403          * are unhandled.  Eventually they will be silenced.
5404          */
5405         tnapi->last_irq_tag = sblk->status_tag;
5406
5407         if (tg3_irq_sync(tp))
5408                 goto out;
5409
5410         prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
5411
5412         napi_schedule(&tnapi->napi);
5413
5414 out:
5415         return IRQ_RETVAL(handled);
5416 }
5417
5418 /* ISR for interrupt test */
5419 static irqreturn_t tg3_test_isr(int irq, void *dev_id)
5420 {
5421         struct tg3_napi *tnapi = dev_id;
5422         struct tg3 *tp = tnapi->tp;
5423         struct tg3_hw_status *sblk = tnapi->hw_status;
5424
5425         if ((sblk->status & SD_STATUS_UPDATED) ||
5426             !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5427                 tg3_disable_ints(tp);
5428                 return IRQ_RETVAL(1);
5429         }
5430         return IRQ_RETVAL(0);
5431 }
5432
5433 static int tg3_init_hw(struct tg3 *, int);
5434 static int tg3_halt(struct tg3 *, int, int);
5435
5436 /* Restart hardware after configuration changes, self-test, etc.
5437  * Invoked with tp->lock held.
5438  */
5439 static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
5440         __releases(tp->lock)
5441         __acquires(tp->lock)
5442 {
5443         int err;
5444
5445         err = tg3_init_hw(tp, reset_phy);
5446         if (err) {
5447                 netdev_err(tp->dev,
5448                            "Failed to re-initialize device, aborting\n");
5449                 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5450                 tg3_full_unlock(tp);
5451                 del_timer_sync(&tp->timer);
5452                 tp->irq_sync = 0;
5453                 tg3_napi_enable(tp);
5454                 dev_close(tp->dev);
5455                 tg3_full_lock(tp, 0);
5456         }
5457         return err;
5458 }
5459
5460 #ifdef CONFIG_NET_POLL_CONTROLLER
5461 static void tg3_poll_controller(struct net_device *dev)
5462 {
5463         int i;
5464         struct tg3 *tp = netdev_priv(dev);
5465
5466         for (i = 0; i < tp->irq_cnt; i++)
5467                 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
5468 }
5469 #endif
5470
5471 static void tg3_reset_task(struct work_struct *work)
5472 {
5473         struct tg3 *tp = container_of(work, struct tg3, reset_task);
5474         int err;
5475         unsigned int restart_timer;
5476
5477         tg3_full_lock(tp, 0);
5478
5479         if (!netif_running(tp->dev)) {
5480                 tg3_full_unlock(tp);
5481                 return;
5482         }
5483
5484         tg3_full_unlock(tp);
5485
5486         tg3_phy_stop(tp);
5487
5488         tg3_netif_stop(tp);
5489
5490         tg3_full_lock(tp, 1);
5491
5492         restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5493         tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5494
5495         if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5496                 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5497                 tp->write32_rx_mbox = tg3_write_flush_reg32;
5498                 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5499                 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5500         }
5501
5502         tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
5503         err = tg3_init_hw(tp, 1);
5504         if (err)
5505                 goto out;
5506
5507         tg3_netif_start(tp);
5508
5509         if (restart_timer)
5510                 mod_timer(&tp->timer, jiffies + 1);
5511
5512 out:
5513         tg3_full_unlock(tp);
5514
5515         if (!err)
5516                 tg3_phy_start(tp);
5517 }
5518
5519 static void tg3_dump_short_state(struct tg3 *tp)
5520 {
5521         netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5522                    tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5523         netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5524                    tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
5525 }
5526
5527 static void tg3_tx_timeout(struct net_device *dev)
5528 {
5529         struct tg3 *tp = netdev_priv(dev);
5530
5531         if (netif_msg_tx_err(tp)) {
5532                 netdev_err(dev, "transmit timed out, resetting\n");
5533                 tg3_dump_short_state(tp);
5534         }
5535
5536         schedule_work(&tp->reset_task);
5537 }
5538
5539 /* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5540 static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5541 {
5542         u32 base = (u32) mapping & 0xffffffff;
5543
5544         return (base > 0xffffdcc0) && (base + len + 8 < base);
5545 }
5546
5547 /* Test for DMA addresses > 40-bit */
5548 static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5549                                           int len)
5550 {
5551 #if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
5552         if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
5553                 return ((u64) mapping + len) > DMA_BIT_MASK(40);
5554         return 0;
5555 #else
5556         return 0;
5557 #endif
5558 }
5559
5560 static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
5561
5562 /* Workaround 4GB and 40-bit hardware DMA bugs. */
5563 static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5564                                        struct sk_buff *skb, u32 last_plus_one,
5565                                        u32 *start, u32 base_flags, u32 mss)
5566 {
5567         struct tg3 *tp = tnapi->tp;
5568         struct sk_buff *new_skb;
5569         dma_addr_t new_addr = 0;
5570         u32 entry = *start;
5571         int i, ret = 0;
5572
5573         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5574                 new_skb = skb_copy(skb, GFP_ATOMIC);
5575         else {
5576                 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5577
5578                 new_skb = skb_copy_expand(skb,
5579                                           skb_headroom(skb) + more_headroom,
5580                                           skb_tailroom(skb), GFP_ATOMIC);
5581         }
5582
5583         if (!new_skb) {
5584                 ret = -1;
5585         } else {
5586                 /* New SKB is guaranteed to be linear. */
5587                 entry = *start;
5588                 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5589                                           PCI_DMA_TODEVICE);
5590                 /* Make sure the mapping succeeded */
5591                 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5592                         ret = -1;
5593                         dev_kfree_skb(new_skb);
5594                         new_skb = NULL;
5595
5596                 /* Make sure new skb does not cross any 4G boundaries.
5597                  * Drop the packet if it does.
5598                  */
5599                 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5600                             tg3_4g_overflow_test(new_addr, new_skb->len)) {
5601                         pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5602                                          PCI_DMA_TODEVICE);
5603                         ret = -1;
5604                         dev_kfree_skb(new_skb);
5605                         new_skb = NULL;
5606                 } else {
5607                         tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
5608                                     base_flags, 1 | (mss << 1));
5609                         *start = NEXT_TX(entry);
5610                 }
5611         }
5612
5613         /* Now clean up the sw ring entries. */
5614         i = 0;
5615         while (entry != last_plus_one) {
5616                 int len;
5617
5618                 if (i == 0)
5619                         len = skb_headlen(skb);
5620                 else
5621                         len = skb_shinfo(skb)->frags[i-1].size;
5622
5623                 pci_unmap_single(tp->pdev,
5624                                  dma_unmap_addr(&tnapi->tx_buffers[entry],
5625                                                 mapping),
5626                                  len, PCI_DMA_TODEVICE);
5627                 if (i == 0) {
5628                         tnapi->tx_buffers[entry].skb = new_skb;
5629                         dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
5630                                            new_addr);
5631                 } else {
5632                         tnapi->tx_buffers[entry].skb = NULL;
5633                 }
5634                 entry = NEXT_TX(entry);
5635                 i++;
5636         }
5637
5638         dev_kfree_skb(skb);
5639
5640         return ret;
5641 }
5642
5643 static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5644                         dma_addr_t mapping, int len, u32 flags,
5645                         u32 mss_and_is_end)
5646 {
5647         struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5648         int is_end = (mss_and_is_end & 0x1);
5649         u32 mss = (mss_and_is_end >> 1);
5650         u32 vlan_tag = 0;
5651
5652         if (is_end)
5653                 flags |= TXD_FLAG_END;
5654         if (flags & TXD_FLAG_VLAN) {
5655                 vlan_tag = flags >> 16;
5656                 flags &= 0xffff;
5657         }
5658         vlan_tag |= (mss << TXD_MSS_SHIFT);
5659
5660         txd->addr_hi = ((u64) mapping >> 32);
5661         txd->addr_lo = ((u64) mapping & 0xffffffff);
5662         txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5663         txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5664 }
5665
5666 /* hard_start_xmit for devices that don't have any bugs and
5667  * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
5668  */
5669 static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5670                                   struct net_device *dev)
5671 {
5672         struct tg3 *tp = netdev_priv(dev);
5673         u32 len, entry, base_flags, mss;
5674         dma_addr_t mapping;
5675         struct tg3_napi *tnapi;
5676         struct netdev_queue *txq;
5677         unsigned int i, last;
5678
5679         txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5680         tnapi = &tp->napi[skb_get_queue_mapping(skb)];
5681         if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
5682                 tnapi++;
5683
5684         /* We are running in BH disabled context with netif_tx_lock
5685          * and TX reclaim runs via tp->napi.poll inside of a software
5686          * interrupt.  Furthermore, IRQ processing runs lockless so we have
5687          * no IRQ context deadlocks to worry about either.  Rejoice!
5688          */
5689         if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
5690                 if (!netif_tx_queue_stopped(txq)) {
5691                         netif_tx_stop_queue(txq);
5692
5693                         /* This is a hard error, log it. */
5694                         netdev_err(dev,
5695                                    "BUG! Tx Ring full when queue awake!\n");
5696                 }
5697                 return NETDEV_TX_BUSY;
5698         }
5699
5700         entry = tnapi->tx_prod;
5701         base_flags = 0;
5702         mss = skb_shinfo(skb)->gso_size;
5703         if (mss) {
5704                 int tcp_opt_len, ip_tcp_len;
5705                 u32 hdrlen;
5706
5707                 if (skb_header_cloned(skb) &&
5708                     pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5709                         dev_kfree_skb(skb);
5710                         goto out_unlock;
5711                 }
5712
5713                 if (skb_is_gso_v6(skb)) {
5714                         hdrlen = skb_headlen(skb) - ETH_HLEN;
5715                 } else {
5716                         struct iphdr *iph = ip_hdr(skb);
5717
5718                         tcp_opt_len = tcp_optlen(skb);
5719                         ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5720
5721                         iph->check = 0;
5722                         iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
5723                         hdrlen = ip_tcp_len + tcp_opt_len;
5724                 }
5725
5726                 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5727                         mss |= (hdrlen & 0xc) << 12;
5728                         if (hdrlen & 0x10)
5729                                 base_flags |= 0x00000010;
5730                         base_flags |= (hdrlen & 0x3e0) << 5;
5731                 } else
5732                         mss |= hdrlen << 9;
5733
5734                 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5735                                TXD_FLAG_CPU_POST_DMA);
5736
5737                 tcp_hdr(skb)->check = 0;
5738
5739         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5740                 base_flags |= TXD_FLAG_TCPUDP_CSUM;
5741         }
5742
5743 #if TG3_VLAN_TAG_USED
5744         if (vlan_tx_tag_present(skb))
5745                 base_flags |= (TXD_FLAG_VLAN |
5746                                (vlan_tx_tag_get(skb) << 16));
5747 #endif
5748
5749         len = skb_headlen(skb);
5750
5751         /* Queue skb data, a.k.a. the main skb fragment. */
5752         mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5753         if (pci_dma_mapping_error(tp->pdev, mapping)) {
5754                 dev_kfree_skb(skb);
5755                 goto out_unlock;
5756         }
5757
5758         tnapi->tx_buffers[entry].skb = skb;
5759         dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
5760
5761         if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
5762             !mss && skb->len > VLAN_ETH_FRAME_LEN)
5763                 base_flags |= TXD_FLAG_JMB_PKT;
5764
5765         tg3_set_txd(tnapi, entry, mapping, len, base_flags,
5766                     (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5767
5768         entry = NEXT_TX(entry);
5769
5770         /* Now loop through additional data fragments, and queue them. */
5771         if (skb_shinfo(skb)->nr_frags > 0) {
5772                 last = skb_shinfo(skb)->nr_frags - 1;
5773                 for (i = 0; i <= last; i++) {
5774                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5775
5776                         len = frag->size;
5777