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