blob: 71d4a0329d0298c910bf781e6275f2a7fd08eda9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: sungem.c,v 1.44.2.22 2002/03/13 01:18:12 davem Exp $
2 * sungem.c: Sun GEM ethernet driver.
3 *
4 * Copyright (C) 2000, 2001, 2002, 2003 David S. Miller (davem@redhat.com)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Support for Apple GMAC and assorted PHYs, WOL, Power Management
7 * (C) 2001,2002,2003 Benjamin Herrenscmidt (benh@kernel.crashing.org)
8 * (C) 2004,2005 Benjamin Herrenscmidt, IBM Corp.
9 *
10 * NAPI and NETPOLL support
11 * (C) 2004 by Eric Lemoine (eric.lemoine@gmail.com)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
Joe Perchesc6c75982010-08-17 07:55:04 +000015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/types.h>
20#include <linux/fcntl.h>
21#include <linux/interrupt.h>
22#include <linux/ioport.h>
23#include <linux/in.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040024#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
26#include <linux/delay.h>
27#include <linux/init.h>
28#include <linux/errno.h>
29#include <linux/pci.h>
Domen Puncer1e7f0bd2005-06-26 18:22:14 -040030#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/mii.h>
35#include <linux/ethtool.h>
36#include <linux/crc32.h>
37#include <linux/random.h>
38#include <linux/workqueue.h>
39#include <linux/if_vlan.h>
40#include <linux/bitops.h>
Al Virod7fe0f22006-12-03 23:15:30 -050041#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/system.h>
45#include <asm/io.h>
46#include <asm/byteorder.h>
47#include <asm/uaccess.h>
48#include <asm/irq.h>
49
David S. Millerdadb8302007-02-28 15:42:50 -080050#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/idprom.h>
David S. Miller457e1a82007-03-29 01:36:44 -070052#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#endif
54
55#ifdef CONFIG_PPC_PMAC
56#include <asm/pci-bridge.h>
57#include <asm/prom.h>
58#include <asm/machdep.h>
59#include <asm/pmac_feature.h>
60#endif
61
62#include "sungem_phy.h"
63#include "sungem.h"
64
65/* Stripping FCS is causing problems, disabled for now */
66#undef STRIP_FCS
67
68#define DEFAULT_MSG (NETIF_MSG_DRV | \
69 NETIF_MSG_PROBE | \
70 NETIF_MSG_LINK)
71
72#define ADVERTISE_MASK (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | \
73 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | \
Benjamin Herrenschmidt63ea9982007-01-03 18:54:43 -080074 SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full | \
75 SUPPORTED_Pause | SUPPORTED_Autoneg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#define DRV_NAME "sungem"
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +100078#define DRV_VERSION "1.0"
79#define DRV_AUTHOR "David S. Miller <davem@redhat.com>"
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static char version[] __devinitdata =
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +100082 DRV_NAME ".c:v" DRV_VERSION " " DRV_AUTHOR "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84MODULE_AUTHOR(DRV_AUTHOR);
85MODULE_DESCRIPTION("Sun GEM Gbit ethernet driver");
86MODULE_LICENSE("GPL");
87
88#define GEM_MODULE_NAME "gem"
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000090static DEFINE_PCI_DEVICE_TABLE(gem_pci_tbl) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_GEM,
92 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
93
94 /* These models only differ from the original GEM in
95 * that their tx/rx fifos are of a different size and
96 * they only support 10/100 speeds. -DaveM
Jeff Garzik6aa20a22006-09-13 13:24:59 -040097 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * Apple's GMAC does support gigabit on machines with
99 * the BCM54xx PHYs. -BenH
100 */
101 { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_RIO_GEM,
102 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
103 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMAC,
104 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
105 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMACP,
106 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
107 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMAC2,
108 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
109 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_K2_GMAC,
110 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
111 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_SH_SUNGEM,
112 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
Olof Johansson7fce2602005-11-13 16:06:48 -0800113 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_IPID2_GMAC,
114 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 {0, }
116};
117
118MODULE_DEVICE_TABLE(pci, gem_pci_tbl);
119
120static u16 __phy_read(struct gem *gp, int phy_addr, int reg)
121{
122 u32 cmd;
123 int limit = 10000;
124
125 cmd = (1 << 30);
126 cmd |= (2 << 28);
127 cmd |= (phy_addr << 23) & MIF_FRAME_PHYAD;
128 cmd |= (reg << 18) & MIF_FRAME_REGAD;
129 cmd |= (MIF_FRAME_TAMSB);
130 writel(cmd, gp->regs + MIF_FRAME);
131
Roel Kluin46578a692009-02-02 21:39:02 -0800132 while (--limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 cmd = readl(gp->regs + MIF_FRAME);
134 if (cmd & MIF_FRAME_TALSB)
135 break;
136
137 udelay(10);
138 }
139
140 if (!limit)
141 cmd = 0xffff;
142
143 return cmd & MIF_FRAME_DATA;
144}
145
146static inline int _phy_read(struct net_device *dev, int mii_id, int reg)
147{
Wang Chen8f15ea42008-11-12 23:38:36 -0800148 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return __phy_read(gp, mii_id, reg);
150}
151
152static inline u16 phy_read(struct gem *gp, int reg)
153{
154 return __phy_read(gp, gp->mii_phy_addr, reg);
155}
156
157static void __phy_write(struct gem *gp, int phy_addr, int reg, u16 val)
158{
159 u32 cmd;
160 int limit = 10000;
161
162 cmd = (1 << 30);
163 cmd |= (1 << 28);
164 cmd |= (phy_addr << 23) & MIF_FRAME_PHYAD;
165 cmd |= (reg << 18) & MIF_FRAME_REGAD;
166 cmd |= (MIF_FRAME_TAMSB);
167 cmd |= (val & MIF_FRAME_DATA);
168 writel(cmd, gp->regs + MIF_FRAME);
169
170 while (limit--) {
171 cmd = readl(gp->regs + MIF_FRAME);
172 if (cmd & MIF_FRAME_TALSB)
173 break;
174
175 udelay(10);
176 }
177}
178
179static inline void _phy_write(struct net_device *dev, int mii_id, int reg, int val)
180{
Wang Chen8f15ea42008-11-12 23:38:36 -0800181 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 __phy_write(gp, mii_id, reg, val & 0xffff);
183}
184
185static inline void phy_write(struct gem *gp, int reg, u16 val)
186{
187 __phy_write(gp, gp->mii_phy_addr, reg, val);
188}
189
190static inline void gem_enable_ints(struct gem *gp)
191{
192 /* Enable all interrupts but TXDONE */
193 writel(GREG_STAT_TXDONE, gp->regs + GREG_IMASK);
194}
195
196static inline void gem_disable_ints(struct gem *gp)
197{
198 /* Disable all interrupts, including TXDONE */
199 writel(GREG_STAT_NAPI | GREG_STAT_TXDONE, gp->regs + GREG_IMASK);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000200 (void)readl(gp->regs + GREG_IMASK); /* write posting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203static void gem_get_cell(struct gem *gp)
204{
205 BUG_ON(gp->cell_enabled < 0);
206 gp->cell_enabled++;
207#ifdef CONFIG_PPC_PMAC
208 if (gp->cell_enabled == 1) {
209 mb();
210 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, gp->of_node, 0, 1);
211 udelay(10);
212 }
213#endif /* CONFIG_PPC_PMAC */
214}
215
216/* Turn off the chip's clock */
217static void gem_put_cell(struct gem *gp)
218{
219 BUG_ON(gp->cell_enabled <= 0);
220 gp->cell_enabled--;
221#ifdef CONFIG_PPC_PMAC
222 if (gp->cell_enabled == 0) {
223 mb();
224 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, gp->of_node, 0, 0);
225 udelay(10);
226 }
227#endif /* CONFIG_PPC_PMAC */
228}
229
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000230static inline void gem_netif_stop(struct gem *gp)
231{
232 gp->dev->trans_start = jiffies; /* prevent tx timeout */
233 napi_disable(&gp->napi);
234 netif_tx_disable(gp->dev);
235}
236
237static inline void gem_netif_start(struct gem *gp)
238{
239 /* NOTE: unconditional netif_wake_queue is only
240 * appropriate so long as all callers are assured to
241 * have free tx slots.
242 */
243 netif_wake_queue(gp->dev);
244 napi_enable(&gp->napi);
245}
246
247static void gem_schedule_reset(struct gem *gp)
248{
249 gp->reset_task_pending = 1;
250 schedule_work(&gp->reset_task);
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static void gem_handle_mif_event(struct gem *gp, u32 reg_val, u32 changed_bits)
254{
255 if (netif_msg_intr(gp))
256 printk(KERN_DEBUG "%s: mif interrupt\n", gp->dev->name);
257}
258
259static int gem_pcs_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
260{
261 u32 pcs_istat = readl(gp->regs + PCS_ISTAT);
262 u32 pcs_miistat;
263
264 if (netif_msg_intr(gp))
265 printk(KERN_DEBUG "%s: pcs interrupt, pcs_istat: 0x%x\n",
266 gp->dev->name, pcs_istat);
267
268 if (!(pcs_istat & PCS_ISTAT_LSC)) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000269 netdev_err(dev, "PCS irq but no link status change???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 0;
271 }
272
273 /* The link status bit latches on zero, so you must
274 * read it twice in such a case to see a transition
275 * to the link being up.
276 */
277 pcs_miistat = readl(gp->regs + PCS_MIISTAT);
278 if (!(pcs_miistat & PCS_MIISTAT_LS))
279 pcs_miistat |=
280 (readl(gp->regs + PCS_MIISTAT) &
281 PCS_MIISTAT_LS);
282
283 if (pcs_miistat & PCS_MIISTAT_ANC) {
284 /* The remote-fault indication is only valid
285 * when autoneg has completed.
286 */
287 if (pcs_miistat & PCS_MIISTAT_RF)
Joe Perchesc6c75982010-08-17 07:55:04 +0000288 netdev_info(dev, "PCS AutoNEG complete, RemoteFault\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 else
Joe Perchesc6c75982010-08-17 07:55:04 +0000290 netdev_info(dev, "PCS AutoNEG complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
293 if (pcs_miistat & PCS_MIISTAT_LS) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000294 netdev_info(dev, "PCS link is now up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 netif_carrier_on(gp->dev);
296 } else {
Joe Perchesc6c75982010-08-17 07:55:04 +0000297 netdev_info(dev, "PCS link is now down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 netif_carrier_off(gp->dev);
299 /* If this happens and the link timer is not running,
300 * reset so we re-negotiate.
301 */
302 if (!timer_pending(&gp->link_timer))
303 return 1;
304 }
305
306 return 0;
307}
308
309static int gem_txmac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
310{
311 u32 txmac_stat = readl(gp->regs + MAC_TXSTAT);
312
313 if (netif_msg_intr(gp))
314 printk(KERN_DEBUG "%s: txmac interrupt, txmac_stat: 0x%x\n",
315 gp->dev->name, txmac_stat);
316
317 /* Defer timer expiration is quite normal,
318 * don't even log the event.
319 */
320 if ((txmac_stat & MAC_TXSTAT_DTE) &&
321 !(txmac_stat & ~MAC_TXSTAT_DTE))
322 return 0;
323
324 if (txmac_stat & MAC_TXSTAT_URUN) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000325 netdev_err(dev, "TX MAC xmit underrun\n");
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000326 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
329 if (txmac_stat & MAC_TXSTAT_MPE) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000330 netdev_err(dev, "TX MAC max packet size error\n");
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000331 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
334 /* The rest are all cases of one of the 16-bit TX
335 * counters expiring.
336 */
337 if (txmac_stat & MAC_TXSTAT_NCE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000338 dev->stats.collisions += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 if (txmac_stat & MAC_TXSTAT_ECE) {
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000341 dev->stats.tx_aborted_errors += 0x10000;
342 dev->stats.collisions += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 if (txmac_stat & MAC_TXSTAT_LCE) {
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000346 dev->stats.tx_aborted_errors += 0x10000;
347 dev->stats.collisions += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
350 /* We do not keep track of MAC_TXSTAT_FCE and
351 * MAC_TXSTAT_PCE events.
352 */
353 return 0;
354}
355
356/* When we get a RX fifo overflow, the RX unit in GEM is probably hung
357 * so we do the following.
358 *
359 * If any part of the reset goes wrong, we return 1 and that causes the
360 * whole chip to be reset.
361 */
362static int gem_rxmac_reset(struct gem *gp)
363{
364 struct net_device *dev = gp->dev;
365 int limit, i;
366 u64 desc_dma;
367 u32 val;
368
369 /* First, reset & disable MAC RX. */
370 writel(MAC_RXRST_CMD, gp->regs + MAC_RXRST);
371 for (limit = 0; limit < 5000; limit++) {
372 if (!(readl(gp->regs + MAC_RXRST) & MAC_RXRST_CMD))
373 break;
374 udelay(10);
375 }
376 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000377 netdev_err(dev, "RX MAC will not reset, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 1;
379 }
380
381 writel(gp->mac_rx_cfg & ~MAC_RXCFG_ENAB,
382 gp->regs + MAC_RXCFG);
383 for (limit = 0; limit < 5000; limit++) {
384 if (!(readl(gp->regs + MAC_RXCFG) & MAC_RXCFG_ENAB))
385 break;
386 udelay(10);
387 }
388 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000389 netdev_err(dev, "RX MAC will not disable, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 1;
391 }
392
393 /* Second, disable RX DMA. */
394 writel(0, gp->regs + RXDMA_CFG);
395 for (limit = 0; limit < 5000; limit++) {
396 if (!(readl(gp->regs + RXDMA_CFG) & RXDMA_CFG_ENABLE))
397 break;
398 udelay(10);
399 }
400 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000401 netdev_err(dev, "RX DMA will not disable, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 1;
403 }
404
405 udelay(5000);
406
407 /* Execute RX reset command. */
408 writel(gp->swrst_base | GREG_SWRST_RXRST,
409 gp->regs + GREG_SWRST);
410 for (limit = 0; limit < 5000; limit++) {
411 if (!(readl(gp->regs + GREG_SWRST) & GREG_SWRST_RXRST))
412 break;
413 udelay(10);
414 }
415 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000416 netdev_err(dev, "RX reset command will not execute, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return 1;
418 }
419
420 /* Refresh the RX ring. */
421 for (i = 0; i < RX_RING_SIZE; i++) {
422 struct gem_rxd *rxd = &gp->init_block->rxd[i];
423
424 if (gp->rx_skbs[i] == NULL) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000425 netdev_err(dev, "Parts of RX ring empty, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 1;
427 }
428
429 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
430 }
431 gp->rx_new = gp->rx_old = 0;
432
433 /* Now we must reprogram the rest of RX unit. */
434 desc_dma = (u64) gp->gblock_dvma;
435 desc_dma += (INIT_BLOCK_TX_RING_SIZE * sizeof(struct gem_txd));
436 writel(desc_dma >> 32, gp->regs + RXDMA_DBHI);
437 writel(desc_dma & 0xffffffff, gp->regs + RXDMA_DBLOW);
438 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
439 val = (RXDMA_CFG_BASE | (RX_OFFSET << 10) |
440 ((14 / 2) << 13) | RXDMA_CFG_FTHRESH_128);
441 writel(val, gp->regs + RXDMA_CFG);
442 if (readl(gp->regs + GREG_BIFCFG) & GREG_BIFCFG_M66EN)
443 writel(((5 & RXDMA_BLANK_IPKTS) |
444 ((8 << 12) & RXDMA_BLANK_ITIME)),
445 gp->regs + RXDMA_BLANK);
446 else
447 writel(((5 & RXDMA_BLANK_IPKTS) |
448 ((4 << 12) & RXDMA_BLANK_ITIME)),
449 gp->regs + RXDMA_BLANK);
450 val = (((gp->rx_pause_off / 64) << 0) & RXDMA_PTHRESH_OFF);
451 val |= (((gp->rx_pause_on / 64) << 12) & RXDMA_PTHRESH_ON);
452 writel(val, gp->regs + RXDMA_PTHRESH);
453 val = readl(gp->regs + RXDMA_CFG);
454 writel(val | RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
455 writel(MAC_RXSTAT_RCV, gp->regs + MAC_RXMASK);
456 val = readl(gp->regs + MAC_RXCFG);
457 writel(val | MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
458
459 return 0;
460}
461
462static int gem_rxmac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
463{
464 u32 rxmac_stat = readl(gp->regs + MAC_RXSTAT);
465 int ret = 0;
466
467 if (netif_msg_intr(gp))
468 printk(KERN_DEBUG "%s: rxmac interrupt, rxmac_stat: 0x%x\n",
469 gp->dev->name, rxmac_stat);
470
471 if (rxmac_stat & MAC_RXSTAT_OFLW) {
472 u32 smac = readl(gp->regs + MAC_SMACHINE);
473
Joe Perchesc6c75982010-08-17 07:55:04 +0000474 netdev_err(dev, "RX MAC fifo overflow smac[%08x]\n", smac);
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000475 dev->stats.rx_over_errors++;
476 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 ret = gem_rxmac_reset(gp);
479 }
480
481 if (rxmac_stat & MAC_RXSTAT_ACE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000482 dev->stats.rx_frame_errors += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if (rxmac_stat & MAC_RXSTAT_CCE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000485 dev->stats.rx_crc_errors += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if (rxmac_stat & MAC_RXSTAT_LCE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000488 dev->stats.rx_length_errors += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* We do not track MAC_RXSTAT_FCE and MAC_RXSTAT_VCE
491 * events.
492 */
493 return ret;
494}
495
496static int gem_mac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
497{
498 u32 mac_cstat = readl(gp->regs + MAC_CSTAT);
499
500 if (netif_msg_intr(gp))
501 printk(KERN_DEBUG "%s: mac interrupt, mac_cstat: 0x%x\n",
502 gp->dev->name, mac_cstat);
503
504 /* This interrupt is just for pause frame and pause
505 * tracking. It is useful for diagnostics and debug
506 * but probably by default we will mask these events.
507 */
508 if (mac_cstat & MAC_CSTAT_PS)
509 gp->pause_entered++;
510
511 if (mac_cstat & MAC_CSTAT_PRCV)
512 gp->pause_last_time_recvd = (mac_cstat >> 16);
513
514 return 0;
515}
516
517static int gem_mif_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
518{
519 u32 mif_status = readl(gp->regs + MIF_STATUS);
520 u32 reg_val, changed_bits;
521
522 reg_val = (mif_status & MIF_STATUS_DATA) >> 16;
523 changed_bits = (mif_status & MIF_STATUS_STAT);
524
525 gem_handle_mif_event(gp, reg_val, changed_bits);
526
527 return 0;
528}
529
530static int gem_pci_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
531{
532 u32 pci_estat = readl(gp->regs + GREG_PCIESTAT);
533
534 if (gp->pdev->vendor == PCI_VENDOR_ID_SUN &&
535 gp->pdev->device == PCI_DEVICE_ID_SUN_GEM) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000536 netdev_err(dev, "PCI error [%04x]", pci_estat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (pci_estat & GREG_PCIESTAT_BADACK)
Joe Perchesc6c75982010-08-17 07:55:04 +0000539 pr_cont(" <No ACK64# during ABS64 cycle>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (pci_estat & GREG_PCIESTAT_DTRTO)
Joe Perchesc6c75982010-08-17 07:55:04 +0000541 pr_cont(" <Delayed transaction timeout>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (pci_estat & GREG_PCIESTAT_OTHER)
Joe Perchesc6c75982010-08-17 07:55:04 +0000543 pr_cont(" <other>");
544 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 } else {
546 pci_estat |= GREG_PCIESTAT_OTHER;
Joe Perchesc6c75982010-08-17 07:55:04 +0000547 netdev_err(dev, "PCI error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
550 if (pci_estat & GREG_PCIESTAT_OTHER) {
551 u16 pci_cfg_stat;
552
553 /* Interrogate PCI config space for the
554 * true cause.
555 */
556 pci_read_config_word(gp->pdev, PCI_STATUS,
557 &pci_cfg_stat);
Joe Perchesc6c75982010-08-17 07:55:04 +0000558 netdev_err(dev, "Read PCI cfg space status [%04x]\n",
559 pci_cfg_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (pci_cfg_stat & PCI_STATUS_PARITY)
Joe Perchesc6c75982010-08-17 07:55:04 +0000561 netdev_err(dev, "PCI parity error detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (pci_cfg_stat & PCI_STATUS_SIG_TARGET_ABORT)
Joe Perchesc6c75982010-08-17 07:55:04 +0000563 netdev_err(dev, "PCI target abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (pci_cfg_stat & PCI_STATUS_REC_TARGET_ABORT)
Joe Perchesc6c75982010-08-17 07:55:04 +0000565 netdev_err(dev, "PCI master acks target abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (pci_cfg_stat & PCI_STATUS_REC_MASTER_ABORT)
Joe Perchesc6c75982010-08-17 07:55:04 +0000567 netdev_err(dev, "PCI master abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (pci_cfg_stat & PCI_STATUS_SIG_SYSTEM_ERROR)
Joe Perchesc6c75982010-08-17 07:55:04 +0000569 netdev_err(dev, "PCI system error SERR#\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (pci_cfg_stat & PCI_STATUS_DETECTED_PARITY)
Joe Perchesc6c75982010-08-17 07:55:04 +0000571 netdev_err(dev, "PCI parity error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* Write the error bits back to clear them. */
574 pci_cfg_stat &= (PCI_STATUS_PARITY |
575 PCI_STATUS_SIG_TARGET_ABORT |
576 PCI_STATUS_REC_TARGET_ABORT |
577 PCI_STATUS_REC_MASTER_ABORT |
578 PCI_STATUS_SIG_SYSTEM_ERROR |
579 PCI_STATUS_DETECTED_PARITY);
580 pci_write_config_word(gp->pdev,
581 PCI_STATUS, pci_cfg_stat);
582 }
583
584 /* For all PCI errors, we should reset the chip. */
585 return 1;
586}
587
588/* All non-normal interrupt conditions get serviced here.
589 * Returns non-zero if we should just exit the interrupt
590 * handler right now (ie. if we reset the card which invalidates
591 * all of the other original irq status bits).
592 */
593static int gem_abnormal_irq(struct net_device *dev, struct gem *gp, u32 gem_status)
594{
595 if (gem_status & GREG_STAT_RXNOBUF) {
596 /* Frame arrived, no free RX buffers available. */
597 if (netif_msg_rx_err(gp))
598 printk(KERN_DEBUG "%s: no buffer for rx frame\n",
599 gp->dev->name);
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000600 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
603 if (gem_status & GREG_STAT_RXTAGERR) {
604 /* corrupt RX tag framing */
605 if (netif_msg_rx_err(gp))
606 printk(KERN_DEBUG "%s: corrupt rx tag framing\n",
607 gp->dev->name);
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000608 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000610 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
613 if (gem_status & GREG_STAT_PCS) {
614 if (gem_pcs_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000615 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
618 if (gem_status & GREG_STAT_TXMAC) {
619 if (gem_txmac_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000620 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
623 if (gem_status & GREG_STAT_RXMAC) {
624 if (gem_rxmac_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000625 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 if (gem_status & GREG_STAT_MAC) {
629 if (gem_mac_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000630 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
633 if (gem_status & GREG_STAT_MIF) {
634 if (gem_mif_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000635 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
638 if (gem_status & GREG_STAT_PCIERR) {
639 if (gem_pci_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000640 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646static __inline__ void gem_tx(struct net_device *dev, struct gem *gp, u32 gem_status)
647{
648 int entry, limit;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 entry = gp->tx_old;
651 limit = ((gem_status & GREG_STAT_TXNR) >> GREG_STAT_TXNR_SHIFT);
652 while (entry != limit) {
653 struct sk_buff *skb;
654 struct gem_txd *txd;
655 dma_addr_t dma_addr;
656 u32 dma_len;
657 int frag;
658
659 if (netif_msg_tx_done(gp))
660 printk(KERN_DEBUG "%s: tx done, slot %d\n",
661 gp->dev->name, entry);
662 skb = gp->tx_skbs[entry];
663 if (skb_shinfo(skb)->nr_frags) {
664 int last = entry + skb_shinfo(skb)->nr_frags;
665 int walk = entry;
666 int incomplete = 0;
667
668 last &= (TX_RING_SIZE - 1);
669 for (;;) {
670 walk = NEXT_TX(walk);
671 if (walk == limit)
672 incomplete = 1;
673 if (walk == last)
674 break;
675 }
676 if (incomplete)
677 break;
678 }
679 gp->tx_skbs[entry] = NULL;
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000680 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
683 txd = &gp->init_block->txd[entry];
684
685 dma_addr = le64_to_cpu(txd->buffer);
686 dma_len = le64_to_cpu(txd->control_word) & TXDCTRL_BUFSZ;
687
688 pci_unmap_page(gp->pdev, dma_addr, dma_len, PCI_DMA_TODEVICE);
689 entry = NEXT_TX(entry);
690 }
691
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000692 dev->stats.tx_packets++;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000693 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695 gp->tx_old = entry;
696
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000697 /* Need to make the tx_old update visible to gem_start_xmit()
698 * before checking for netif_queue_stopped(). Without the
699 * memory barrier, there is a small possibility that gem_start_xmit()
700 * will miss it and cause the queue to be stopped forever.
701 */
702 smp_mb();
703
704 if (unlikely(netif_queue_stopped(dev) &&
705 TX_BUFFS_AVAIL(gp) > (MAX_SKB_FRAGS + 1))) {
706 struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
707
708 __netif_tx_lock(txq, smp_processor_id());
709 if (netif_queue_stopped(dev) &&
710 TX_BUFFS_AVAIL(gp) > (MAX_SKB_FRAGS + 1))
711 netif_wake_queue(dev);
712 __netif_tx_unlock(txq);
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716static __inline__ void gem_post_rxds(struct gem *gp, int limit)
717{
718 int cluster_start, curr, count, kick;
719
720 cluster_start = curr = (gp->rx_new & ~(4 - 1));
721 count = 0;
722 kick = -1;
723 wmb();
724 while (curr != limit) {
725 curr = NEXT_RX(curr);
726 if (++count == 4) {
727 struct gem_rxd *rxd =
728 &gp->init_block->rxd[cluster_start];
729 for (;;) {
730 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
731 rxd++;
732 cluster_start = NEXT_RX(cluster_start);
733 if (cluster_start == curr)
734 break;
735 }
736 kick = curr;
737 count = 0;
738 }
739 }
740 if (kick >= 0) {
741 mb();
742 writel(kick, gp->regs + RXDMA_KICK);
743 }
744}
745
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000746#define ALIGNED_RX_SKB_ADDR(addr) \
747 ((((unsigned long)(addr) + (64UL - 1UL)) & ~(64UL - 1UL)) - (unsigned long)(addr))
748static __inline__ struct sk_buff *gem_alloc_skb(struct net_device *dev, int size,
749 gfp_t gfp_flags)
750{
751 struct sk_buff *skb = alloc_skb(size + 64, gfp_flags);
752
753 if (likely(skb)) {
754 unsigned long offset = ALIGNED_RX_SKB_ADDR(skb->data);
755 skb_reserve(skb, offset);
756 skb->dev = dev;
757 }
758 return skb;
759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static int gem_rx(struct gem *gp, int work_to_do)
762{
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000763 struct net_device *dev = gp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int entry, drops, work_done = 0;
765 u32 done;
Al Viro439104b2007-12-17 06:48:04 +0000766 __sum16 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (netif_msg_rx_status(gp))
769 printk(KERN_DEBUG "%s: rx interrupt, done: %d, rx_new: %d\n",
770 gp->dev->name, readl(gp->regs + RXDMA_DONE), gp->rx_new);
771
772 entry = gp->rx_new;
773 drops = 0;
774 done = readl(gp->regs + RXDMA_DONE);
775 for (;;) {
776 struct gem_rxd *rxd = &gp->init_block->rxd[entry];
777 struct sk_buff *skb;
Al Viro439104b2007-12-17 06:48:04 +0000778 u64 status = le64_to_cpu(rxd->status_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 dma_addr_t dma_addr;
780 int len;
781
782 if ((status & RXDCTRL_OWN) != 0)
783 break;
784
785 if (work_done >= RX_RING_SIZE || work_done >= work_to_do)
786 break;
787
788 /* When writing back RX descriptor, GEM writes status
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800789 * then buffer address, possibly in separate transactions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * If we don't wait for the chip to write both, we could
791 * post a new buffer to this descriptor then have GEM spam
792 * on the buffer address. We sync on the RX completion
793 * register to prevent this from happening.
794 */
795 if (entry == done) {
796 done = readl(gp->regs + RXDMA_DONE);
797 if (entry == done)
798 break;
799 }
800
801 /* We can now account for the work we're about to do */
802 work_done++;
803
804 skb = gp->rx_skbs[entry];
805
806 len = (status & RXDCTRL_BUFSZ) >> 16;
807 if ((len < ETH_ZLEN) || (status & RXDCTRL_BAD)) {
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000808 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (len < ETH_ZLEN)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000810 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (len & RXDCTRL_BAD)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000812 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /* We'll just return it to GEM. */
815 drop_it:
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000816 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 goto next;
818 }
819
Al Viro439104b2007-12-17 06:48:04 +0000820 dma_addr = le64_to_cpu(rxd->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (len > RX_COPY_THRESHOLD) {
822 struct sk_buff *new_skb;
823
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000824 new_skb = gem_alloc_skb(dev, RX_BUF_ALLOC_SIZE(gp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (new_skb == NULL) {
826 drops++;
827 goto drop_it;
828 }
829 pci_unmap_page(gp->pdev, dma_addr,
830 RX_BUF_ALLOC_SIZE(gp),
831 PCI_DMA_FROMDEVICE);
832 gp->rx_skbs[entry] = new_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 skb_put(new_skb, (gp->rx_buf_sz + RX_OFFSET));
834 rxd->buffer = cpu_to_le64(pci_map_page(gp->pdev,
835 virt_to_page(new_skb->data),
836 offset_in_page(new_skb->data),
837 RX_BUF_ALLOC_SIZE(gp),
838 PCI_DMA_FROMDEVICE));
839 skb_reserve(new_skb, RX_OFFSET);
840
841 /* Trim the original skb for the netif. */
842 skb_trim(skb, len);
843 } else {
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000844 struct sk_buff *copy_skb = netdev_alloc_skb(dev, len + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 if (copy_skb == NULL) {
847 drops++;
848 goto drop_it;
849 }
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 skb_reserve(copy_skb, 2);
852 skb_put(copy_skb, len);
853 pci_dma_sync_single_for_cpu(gp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300854 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 pci_dma_sync_single_for_device(gp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
856
857 /* We'll reuse the original ring buffer. */
858 skb = copy_skb;
859 }
860
Al Viro439104b2007-12-17 06:48:04 +0000861 csum = (__force __sum16)htons((status & RXDCTRL_TCPCSUM) ^ 0xffff);
862 skb->csum = csum_unfold(csum);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700863 skb->ip_summed = CHECKSUM_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 skb->protocol = eth_type_trans(skb, gp->dev);
865
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000866 napi_gro_receive(&gp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000868 dev->stats.rx_packets++;
869 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 next:
872 entry = NEXT_RX(entry);
873 }
874
875 gem_post_rxds(gp, entry);
876
877 gp->rx_new = entry;
878
879 if (drops)
Joe Perchesc6c75982010-08-17 07:55:04 +0000880 netdev_info(gp->dev, "Memory squeeze, deferring packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 return work_done;
883}
884
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700885static int gem_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700887 struct gem *gp = container_of(napi, struct gem, napi);
888 struct net_device *dev = gp->dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700889 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700891 work_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* Handle anomalies */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000894 if (unlikely(gp->status & GREG_STAT_ABNORMAL)) {
895 struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
896 int reset;
897
898 /* We run the abnormal interrupt handling code with
899 * the Tx lock. It only resets the Rx portion of the
900 * chip, but we need to guard it against DMA being
901 * restarted by the link poll timer
902 */
903 __netif_tx_lock(txq, smp_processor_id());
904 reset = gem_abnormal_irq(dev, gp, gp->status);
905 __netif_tx_unlock(txq);
906 if (reset) {
907 gem_schedule_reset(gp);
908 napi_complete(napi);
909 return work_done;
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912
913 /* Run TX completion thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 gem_tx(dev, gp, gp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400916 /* Run RX thread. We don't use any locking here,
917 * code willing to do bad things - like cleaning the
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700918 * rx ring - must call napi_disable(), which
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * schedule_timeout()'s if polling is already disabled.
920 */
David S. Millerda990a22008-03-23 03:35:12 -0700921 work_done += gem_rx(gp, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700923 if (work_done >= budget)
924 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 gp->status = readl(gp->regs + GREG_STAT);
927 } while (gp->status & GREG_STAT_NAPI);
928
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000929 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 gem_enable_ints(gp);
931
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700932 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
David Howells7d12e782006-10-05 14:55:46 +0100935static irqreturn_t gem_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct net_device *dev = dev_id;
Wang Chen8f15ea42008-11-12 23:38:36 -0800938 struct gem *gp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400939
Ben Hutchings288379f2009-01-19 16:43:59 -0800940 if (napi_schedule_prep(&gp->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 u32 gem_status = readl(gp->regs + GREG_STAT);
942
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000943 if (unlikely(gem_status == 0)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700944 napi_enable(&gp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return IRQ_NONE;
946 }
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000947 if (netif_msg_intr(gp))
948 printk(KERN_DEBUG "%s: gem_interrupt() gem_status: 0x%x\n",
949 gp->dev->name, gem_status);
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 gp->status = gem_status;
952 gem_disable_ints(gp);
Ben Hutchings288379f2009-01-19 16:43:59 -0800953 __napi_schedule(&gp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* If polling was disabled at the time we received that
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400957 * interrupt, we may return IRQ_HANDLED here while we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * should return IRQ_NONE. No big deal...
959 */
960 return IRQ_HANDLED;
961}
962
963#ifdef CONFIG_NET_POLL_CONTROLLER
964static void gem_poll_controller(struct net_device *dev)
965{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000966 struct gem *gp = netdev_priv(dev);
967
968 disable_irq(gp->pdev->irq);
969 gem_interrupt(gp->pdev->irq, dev);
970 enable_irq(gp->pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972#endif
973
974static void gem_tx_timeout(struct net_device *dev)
975{
Wang Chen8f15ea42008-11-12 23:38:36 -0800976 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Joe Perchesc6c75982010-08-17 07:55:04 +0000978 netdev_err(dev, "transmit timed out, resetting\n");
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000979
Joe Perchesc6c75982010-08-17 07:55:04 +0000980 netdev_err(dev, "TX_STATE[%08x:%08x:%08x]\n",
981 readl(gp->regs + TXDMA_CFG),
982 readl(gp->regs + MAC_TXSTAT),
983 readl(gp->regs + MAC_TXCFG));
984 netdev_err(dev, "RX_STATE[%08x:%08x:%08x]\n",
985 readl(gp->regs + RXDMA_CFG),
986 readl(gp->regs + MAC_RXSTAT),
987 readl(gp->regs + MAC_RXCFG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000989 gem_schedule_reset(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992static __inline__ int gem_intme(int entry)
993{
994 /* Algorithm: IRQ every 1/2 of descriptors. */
995 if (!(entry & ((TX_RING_SIZE>>1)-1)))
996 return 1;
997
998 return 0;
999}
1000
Stephen Hemminger613573252009-08-31 19:50:58 +00001001static netdev_tx_t gem_start_xmit(struct sk_buff *skb,
1002 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Wang Chen8f15ea42008-11-12 23:38:36 -08001004 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int entry;
1006 u64 ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 ctrl = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07001009 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +00001010 const u64 csum_start_off = skb_checksum_start_offset(skb);
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07001011 const u64 csum_stuff_off = csum_start_off + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 ctrl = (TXDCTRL_CENAB |
1014 (csum_start_off << 15) |
1015 (csum_stuff_off << 21));
1016 }
1017
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001018 if (unlikely(TX_BUFFS_AVAIL(gp) <= (skb_shinfo(skb)->nr_frags + 1))) {
1019 /* This is a hard error, log it. */
1020 if (!netif_queue_stopped(dev)) {
1021 netif_stop_queue(dev);
1022 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return NETDEV_TX_BUSY;
1025 }
1026
1027 entry = gp->tx_new;
1028 gp->tx_skbs[entry] = skb;
1029
1030 if (skb_shinfo(skb)->nr_frags == 0) {
1031 struct gem_txd *txd = &gp->init_block->txd[entry];
1032 dma_addr_t mapping;
1033 u32 len;
1034
1035 len = skb->len;
1036 mapping = pci_map_page(gp->pdev,
1037 virt_to_page(skb->data),
1038 offset_in_page(skb->data),
1039 len, PCI_DMA_TODEVICE);
1040 ctrl |= TXDCTRL_SOF | TXDCTRL_EOF | len;
1041 if (gem_intme(entry))
1042 ctrl |= TXDCTRL_INTME;
1043 txd->buffer = cpu_to_le64(mapping);
1044 wmb();
1045 txd->control_word = cpu_to_le64(ctrl);
1046 entry = NEXT_TX(entry);
1047 } else {
1048 struct gem_txd *txd;
1049 u32 first_len;
1050 u64 intme;
1051 dma_addr_t first_mapping;
1052 int frag, first_entry = entry;
1053
1054 intme = 0;
1055 if (gem_intme(entry))
1056 intme |= TXDCTRL_INTME;
1057
1058 /* We must give this initial chunk to the device last.
1059 * Otherwise we could race with the device.
1060 */
1061 first_len = skb_headlen(skb);
1062 first_mapping = pci_map_page(gp->pdev, virt_to_page(skb->data),
1063 offset_in_page(skb->data),
1064 first_len, PCI_DMA_TODEVICE);
1065 entry = NEXT_TX(entry);
1066
1067 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
1068 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
1069 u32 len;
1070 dma_addr_t mapping;
1071 u64 this_ctrl;
1072
1073 len = this_frag->size;
1074 mapping = pci_map_page(gp->pdev,
1075 this_frag->page,
1076 this_frag->page_offset,
1077 len, PCI_DMA_TODEVICE);
1078 this_ctrl = ctrl;
1079 if (frag == skb_shinfo(skb)->nr_frags - 1)
1080 this_ctrl |= TXDCTRL_EOF;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 txd = &gp->init_block->txd[entry];
1083 txd->buffer = cpu_to_le64(mapping);
1084 wmb();
1085 txd->control_word = cpu_to_le64(this_ctrl | len);
1086
1087 if (gem_intme(entry))
1088 intme |= TXDCTRL_INTME;
1089
1090 entry = NEXT_TX(entry);
1091 }
1092 txd = &gp->init_block->txd[first_entry];
1093 txd->buffer = cpu_to_le64(first_mapping);
1094 wmb();
1095 txd->control_word =
1096 cpu_to_le64(ctrl | TXDCTRL_SOF | intme | first_len);
1097 }
1098
1099 gp->tx_new = entry;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001100 if (unlikely(TX_BUFFS_AVAIL(gp) <= (MAX_SKB_FRAGS + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 netif_stop_queue(dev);
1102
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001103 /* netif_stop_queue() must be done before checking
1104 * checking tx index in TX_BUFFS_AVAIL() below, because
1105 * in gem_tx(), we update tx_old before checking for
1106 * netif_queue_stopped().
1107 */
1108 smp_mb();
1109 if (TX_BUFFS_AVAIL(gp) > (MAX_SKB_FRAGS + 1))
1110 netif_wake_queue(dev);
1111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (netif_msg_tx_queued(gp))
1113 printk(KERN_DEBUG "%s: tx queued, slot %d, skblen %d\n",
1114 dev->name, entry, skb->len);
1115 mb();
1116 writel(gp->tx_new, gp->regs + TXDMA_KICK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 return NETDEV_TX_OK;
1119}
1120
David S. Miller8c83f802008-12-09 00:10:11 -08001121static void gem_pcs_reset(struct gem *gp)
1122{
1123 int limit;
1124 u32 val;
1125
1126 /* Reset PCS unit. */
1127 val = readl(gp->regs + PCS_MIICTRL);
1128 val |= PCS_MIICTRL_RST;
1129 writel(val, gp->regs + PCS_MIICTRL);
1130
1131 limit = 32;
1132 while (readl(gp->regs + PCS_MIICTRL) & PCS_MIICTRL_RST) {
1133 udelay(100);
1134 if (limit-- <= 0)
1135 break;
1136 }
roel kluind13c11f2009-02-16 04:02:04 +00001137 if (limit < 0)
Joe Perchesc6c75982010-08-17 07:55:04 +00001138 netdev_warn(gp->dev, "PCS reset bit would not clear\n");
David S. Miller8c83f802008-12-09 00:10:11 -08001139}
1140
1141static void gem_pcs_reinit_adv(struct gem *gp)
1142{
1143 u32 val;
1144
1145 /* Make sure PCS is disabled while changing advertisement
1146 * configuration.
1147 */
1148 val = readl(gp->regs + PCS_CFG);
1149 val &= ~(PCS_CFG_ENABLE | PCS_CFG_TO);
1150 writel(val, gp->regs + PCS_CFG);
1151
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001152 /* Advertise all capabilities except asymmetric
David S. Miller8c83f802008-12-09 00:10:11 -08001153 * pause.
1154 */
1155 val = readl(gp->regs + PCS_MIIADV);
1156 val |= (PCS_MIIADV_FD | PCS_MIIADV_HD |
1157 PCS_MIIADV_SP | PCS_MIIADV_AP);
1158 writel(val, gp->regs + PCS_MIIADV);
1159
1160 /* Enable and restart auto-negotiation, disable wrapback/loopback,
1161 * and re-enable PCS.
1162 */
1163 val = readl(gp->regs + PCS_MIICTRL);
1164 val |= (PCS_MIICTRL_RAN | PCS_MIICTRL_ANE);
1165 val &= ~PCS_MIICTRL_WB;
1166 writel(val, gp->regs + PCS_MIICTRL);
1167
1168 val = readl(gp->regs + PCS_CFG);
1169 val |= PCS_CFG_ENABLE;
1170 writel(val, gp->regs + PCS_CFG);
1171
1172 /* Make sure serialink loopback is off. The meaning
1173 * of this bit is logically inverted based upon whether
1174 * you are in Serialink or SERDES mode.
1175 */
1176 val = readl(gp->regs + PCS_SCTRL);
1177 if (gp->phy_type == phy_serialink)
1178 val &= ~PCS_SCTRL_LOOP;
1179 else
1180 val |= PCS_SCTRL_LOOP;
1181 writel(val, gp->regs + PCS_SCTRL);
1182}
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184#define STOP_TRIES 32
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186static void gem_reset(struct gem *gp)
1187{
1188 int limit;
1189 u32 val;
1190
1191 /* Make sure we won't get any more interrupts */
1192 writel(0xffffffff, gp->regs + GREG_IMASK);
1193
1194 /* Reset the chip */
1195 writel(gp->swrst_base | GREG_SWRST_TXRST | GREG_SWRST_RXRST,
1196 gp->regs + GREG_SWRST);
1197
1198 limit = STOP_TRIES;
1199
1200 do {
1201 udelay(20);
1202 val = readl(gp->regs + GREG_SWRST);
1203 if (limit-- <= 0)
1204 break;
1205 } while (val & (GREG_SWRST_TXRST | GREG_SWRST_RXRST));
1206
Roel Kluin4a8fd2c2009-03-04 00:08:39 -08001207 if (limit < 0)
Joe Perchesc6c75982010-08-17 07:55:04 +00001208 netdev_err(gp->dev, "SW reset is ghetto\n");
David S. Miller8c83f802008-12-09 00:10:11 -08001209
1210 if (gp->phy_type == phy_serialink || gp->phy_type == phy_serdes)
1211 gem_pcs_reinit_adv(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214static void gem_start_dma(struct gem *gp)
1215{
1216 u32 val;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 /* We are ready to rock, turn everything on. */
1219 val = readl(gp->regs + TXDMA_CFG);
1220 writel(val | TXDMA_CFG_ENABLE, gp->regs + TXDMA_CFG);
1221 val = readl(gp->regs + RXDMA_CFG);
1222 writel(val | RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
1223 val = readl(gp->regs + MAC_TXCFG);
1224 writel(val | MAC_TXCFG_ENAB, gp->regs + MAC_TXCFG);
1225 val = readl(gp->regs + MAC_RXCFG);
1226 writel(val | MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
1227
1228 (void) readl(gp->regs + MAC_RXCFG);
1229 udelay(100);
1230
1231 gem_enable_ints(gp);
1232
1233 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
1234}
1235
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001236/* DMA won't be actually stopped before about 4ms tho ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 */
1238static void gem_stop_dma(struct gem *gp)
1239{
1240 u32 val;
1241
1242 /* We are done rocking, turn everything off. */
1243 val = readl(gp->regs + TXDMA_CFG);
1244 writel(val & ~TXDMA_CFG_ENABLE, gp->regs + TXDMA_CFG);
1245 val = readl(gp->regs + RXDMA_CFG);
1246 writel(val & ~RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
1247 val = readl(gp->regs + MAC_TXCFG);
1248 writel(val & ~MAC_TXCFG_ENAB, gp->regs + MAC_TXCFG);
1249 val = readl(gp->regs + MAC_RXCFG);
1250 writel(val & ~MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
1251
1252 (void) readl(gp->regs + MAC_RXCFG);
1253
1254 /* Need to wait a bit ... done by the caller */
1255}
1256
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258// XXX dbl check what that function should do when called on PCS PHY
1259static void gem_begin_auto_negotiation(struct gem *gp, struct ethtool_cmd *ep)
1260{
1261 u32 advertise, features;
1262 int autoneg;
1263 int speed;
1264 int duplex;
1265
1266 if (gp->phy_type != phy_mii_mdio0 &&
1267 gp->phy_type != phy_mii_mdio1)
1268 goto non_mii;
1269
1270 /* Setup advertise */
1271 if (found_mii_phy(gp))
1272 features = gp->phy_mii.def->features;
1273 else
1274 features = 0;
1275
1276 advertise = features & ADVERTISE_MASK;
1277 if (gp->phy_mii.advertising != 0)
1278 advertise &= gp->phy_mii.advertising;
1279
1280 autoneg = gp->want_autoneg;
1281 speed = gp->phy_mii.speed;
1282 duplex = gp->phy_mii.duplex;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /* Setup link parameters */
1285 if (!ep)
1286 goto start_aneg;
1287 if (ep->autoneg == AUTONEG_ENABLE) {
1288 advertise = ep->advertising;
1289 autoneg = 1;
1290 } else {
1291 autoneg = 0;
David Decotigny25db0332011-04-27 18:32:39 +00001292 speed = ethtool_cmd_speed(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 duplex = ep->duplex;
1294 }
1295
1296start_aneg:
1297 /* Sanitize settings based on PHY capabilities */
1298 if ((features & SUPPORTED_Autoneg) == 0)
1299 autoneg = 0;
1300 if (speed == SPEED_1000 &&
1301 !(features & (SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full)))
1302 speed = SPEED_100;
1303 if (speed == SPEED_100 &&
1304 !(features & (SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full)))
1305 speed = SPEED_10;
1306 if (duplex == DUPLEX_FULL &&
1307 !(features & (SUPPORTED_1000baseT_Full |
1308 SUPPORTED_100baseT_Full |
1309 SUPPORTED_10baseT_Full)))
1310 duplex = DUPLEX_HALF;
1311 if (speed == 0)
1312 speed = SPEED_10;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /* If we are asleep, we don't try to actually setup the PHY, we
1315 * just store the settings
1316 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001317 if (!netif_device_present(gp->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 gp->phy_mii.autoneg = gp->want_autoneg = autoneg;
1319 gp->phy_mii.speed = speed;
1320 gp->phy_mii.duplex = duplex;
1321 return;
1322 }
1323
1324 /* Configure PHY & start aneg */
1325 gp->want_autoneg = autoneg;
1326 if (autoneg) {
1327 if (found_mii_phy(gp))
1328 gp->phy_mii.def->ops->setup_aneg(&gp->phy_mii, advertise);
1329 gp->lstate = link_aneg;
1330 } else {
1331 if (found_mii_phy(gp))
1332 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, speed, duplex);
1333 gp->lstate = link_force_ok;
1334 }
1335
1336non_mii:
1337 gp->timer_ticks = 0;
1338 mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
1339}
1340
1341/* A link-up condition has occurred, initialize and enable the
1342 * rest of the chip.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
1344static int gem_set_link_modes(struct gem *gp)
1345{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001346 struct netdev_queue *txq = netdev_get_tx_queue(gp->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int full_duplex, speed, pause;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001348 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 full_duplex = 0;
1351 speed = SPEED_10;
1352 pause = 0;
1353
1354 if (found_mii_phy(gp)) {
1355 if (gp->phy_mii.def->ops->read_link(&gp->phy_mii))
1356 return 1;
1357 full_duplex = (gp->phy_mii.duplex == DUPLEX_FULL);
1358 speed = gp->phy_mii.speed;
1359 pause = gp->phy_mii.pause;
1360 } else if (gp->phy_type == phy_serialink ||
1361 gp->phy_type == phy_serdes) {
1362 u32 pcs_lpa = readl(gp->regs + PCS_MIILP);
1363
David S. Miller8c83f802008-12-09 00:10:11 -08001364 if ((pcs_lpa & PCS_MIIADV_FD) || gp->phy_type == phy_serdes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 full_duplex = 1;
1366 speed = SPEED_1000;
1367 }
1368
Joe Perchesc6c75982010-08-17 07:55:04 +00001369 netif_info(gp, link, gp->dev, "Link is up at %d Mbps, %s-duplex\n",
1370 speed, (full_duplex ? "full" : "half"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001372
1373 /* We take the tx queue lock to avoid collisions between
1374 * this code, the tx path and the NAPI-driven error path
1375 */
1376 __netif_tx_lock(txq, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 val = (MAC_TXCFG_EIPG0 | MAC_TXCFG_NGU);
1379 if (full_duplex) {
1380 val |= (MAC_TXCFG_ICS | MAC_TXCFG_ICOLL);
1381 } else {
1382 /* MAC_TXCFG_NBO must be zero. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 writel(val, gp->regs + MAC_TXCFG);
1385
1386 val = (MAC_XIFCFG_OE | MAC_XIFCFG_LLED);
1387 if (!full_duplex &&
1388 (gp->phy_type == phy_mii_mdio0 ||
1389 gp->phy_type == phy_mii_mdio1)) {
1390 val |= MAC_XIFCFG_DISE;
1391 } else if (full_duplex) {
1392 val |= MAC_XIFCFG_FLED;
1393 }
1394
1395 if (speed == SPEED_1000)
1396 val |= (MAC_XIFCFG_GMII);
1397
1398 writel(val, gp->regs + MAC_XIFCFG);
1399
1400 /* If gigabit and half-duplex, enable carrier extension
1401 * mode. Else, disable it.
1402 */
1403 if (speed == SPEED_1000 && !full_duplex) {
1404 val = readl(gp->regs + MAC_TXCFG);
1405 writel(val | MAC_TXCFG_TCE, gp->regs + MAC_TXCFG);
1406
1407 val = readl(gp->regs + MAC_RXCFG);
1408 writel(val | MAC_RXCFG_RCE, gp->regs + MAC_RXCFG);
1409 } else {
1410 val = readl(gp->regs + MAC_TXCFG);
1411 writel(val & ~MAC_TXCFG_TCE, gp->regs + MAC_TXCFG);
1412
1413 val = readl(gp->regs + MAC_RXCFG);
1414 writel(val & ~MAC_RXCFG_RCE, gp->regs + MAC_RXCFG);
1415 }
1416
1417 if (gp->phy_type == phy_serialink ||
1418 gp->phy_type == phy_serdes) {
1419 u32 pcs_lpa = readl(gp->regs + PCS_MIILP);
1420
1421 if (pcs_lpa & (PCS_MIIADV_SP | PCS_MIIADV_AP))
1422 pause = 1;
1423 }
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (!full_duplex)
1426 writel(512, gp->regs + MAC_STIME);
1427 else
1428 writel(64, gp->regs + MAC_STIME);
1429 val = readl(gp->regs + MAC_MCCFG);
1430 if (pause)
1431 val |= (MAC_MCCFG_SPE | MAC_MCCFG_RPE);
1432 else
1433 val &= ~(MAC_MCCFG_SPE | MAC_MCCFG_RPE);
1434 writel(val, gp->regs + MAC_MCCFG);
1435
1436 gem_start_dma(gp);
1437
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001438 __netif_tx_unlock(txq);
1439
1440 if (netif_msg_link(gp)) {
1441 if (pause) {
1442 netdev_info(gp->dev,
1443 "Pause is enabled (rxfifo: %d off: %d on: %d)\n",
1444 gp->rx_fifo_sz,
1445 gp->rx_pause_off,
1446 gp->rx_pause_on);
1447 } else {
1448 netdev_info(gp->dev, "Pause is disabled\n");
1449 }
1450 }
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return 0;
1453}
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455static int gem_mdio_link_not_up(struct gem *gp)
1456{
1457 switch (gp->lstate) {
1458 case link_force_ret:
Joe Perchesc6c75982010-08-17 07:55:04 +00001459 netif_info(gp, link, gp->dev,
1460 "Autoneg failed again, keeping forced mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii,
1462 gp->last_forced_speed, DUPLEX_HALF);
1463 gp->timer_ticks = 5;
1464 gp->lstate = link_force_ok;
1465 return 0;
1466 case link_aneg:
1467 /* We try forced modes after a failed aneg only on PHYs that don't
1468 * have "magic_aneg" bit set, which means they internally do the
1469 * while forced-mode thingy. On these, we just restart aneg
1470 */
1471 if (gp->phy_mii.def->magic_aneg)
1472 return 1;
Joe Perchesc6c75982010-08-17 07:55:04 +00001473 netif_info(gp, link, gp->dev, "switching to forced 100bt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 /* Try forced modes. */
1475 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, SPEED_100,
1476 DUPLEX_HALF);
1477 gp->timer_ticks = 5;
1478 gp->lstate = link_force_try;
1479 return 0;
1480 case link_force_try:
1481 /* Downgrade from 100 to 10 Mbps if necessary.
1482 * If already at 10Mbps, warn user about the
1483 * situation every 10 ticks.
1484 */
1485 if (gp->phy_mii.speed == SPEED_100) {
1486 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, SPEED_10,
1487 DUPLEX_HALF);
1488 gp->timer_ticks = 5;
Joe Perchesc6c75982010-08-17 07:55:04 +00001489 netif_info(gp, link, gp->dev,
1490 "switching to forced 10bt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return 0;
1492 } else
1493 return 1;
1494 default:
1495 return 0;
1496 }
1497}
1498
1499static void gem_link_timer(unsigned long data)
1500{
1501 struct gem *gp = (struct gem *) data;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001502 struct net_device *dev = gp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int restart_aneg = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001504
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001505 /* There's no point doing anything if we're going to be reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (gp->reset_task_pending)
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001507 return;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (gp->phy_type == phy_serialink ||
1510 gp->phy_type == phy_serdes) {
1511 u32 val = readl(gp->regs + PCS_MIISTAT);
1512
1513 if (!(val & PCS_MIISTAT_LS))
1514 val = readl(gp->regs + PCS_MIISTAT);
1515
1516 if ((val & PCS_MIISTAT_LS) != 0) {
David S. Miller8c83f802008-12-09 00:10:11 -08001517 if (gp->lstate == link_up)
1518 goto restart;
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 gp->lstate = link_up;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001521 netif_carrier_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 (void)gem_set_link_modes(gp);
1523 }
1524 goto restart;
1525 }
1526 if (found_mii_phy(gp) && gp->phy_mii.def->ops->poll_link(&gp->phy_mii)) {
1527 /* Ok, here we got a link. If we had it due to a forced
1528 * fallback, and we were configured for autoneg, we do
1529 * retry a short autoneg pass. If you know your hub is
1530 * broken, use ethtool ;)
1531 */
1532 if (gp->lstate == link_force_try && gp->want_autoneg) {
1533 gp->lstate = link_force_ret;
1534 gp->last_forced_speed = gp->phy_mii.speed;
1535 gp->timer_ticks = 5;
1536 if (netif_msg_link(gp))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001537 netdev_info(dev,
Joe Perchesc6c75982010-08-17 07:55:04 +00001538 "Got link after fallback, retrying autoneg once...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 gp->phy_mii.def->ops->setup_aneg(&gp->phy_mii, gp->phy_mii.advertising);
1540 } else if (gp->lstate != link_up) {
1541 gp->lstate = link_up;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001542 netif_carrier_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (gem_set_link_modes(gp))
1544 restart_aneg = 1;
1545 }
1546 } else {
1547 /* If the link was previously up, we restart the
1548 * whole process
1549 */
1550 if (gp->lstate == link_up) {
1551 gp->lstate = link_down;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001552 netif_info(gp, link, dev, "Link down\n");
1553 netif_carrier_off(dev);
1554 gem_schedule_reset(gp);
1555 /* The reset task will restart the timer */
1556 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 } else if (++gp->timer_ticks > 10) {
1558 if (found_mii_phy(gp))
1559 restart_aneg = gem_mdio_link_not_up(gp);
1560 else
1561 restart_aneg = 1;
1562 }
1563 }
1564 if (restart_aneg) {
1565 gem_begin_auto_negotiation(gp, NULL);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001566 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568restart:
1569 mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572static void gem_clean_rings(struct gem *gp)
1573{
1574 struct gem_init_block *gb = gp->init_block;
1575 struct sk_buff *skb;
1576 int i;
1577 dma_addr_t dma_addr;
1578
1579 for (i = 0; i < RX_RING_SIZE; i++) {
1580 struct gem_rxd *rxd;
1581
1582 rxd = &gb->rxd[i];
1583 if (gp->rx_skbs[i] != NULL) {
1584 skb = gp->rx_skbs[i];
1585 dma_addr = le64_to_cpu(rxd->buffer);
1586 pci_unmap_page(gp->pdev, dma_addr,
1587 RX_BUF_ALLOC_SIZE(gp),
1588 PCI_DMA_FROMDEVICE);
1589 dev_kfree_skb_any(skb);
1590 gp->rx_skbs[i] = NULL;
1591 }
1592 rxd->status_word = 0;
1593 wmb();
1594 rxd->buffer = 0;
1595 }
1596
1597 for (i = 0; i < TX_RING_SIZE; i++) {
1598 if (gp->tx_skbs[i] != NULL) {
1599 struct gem_txd *txd;
1600 int frag;
1601
1602 skb = gp->tx_skbs[i];
1603 gp->tx_skbs[i] = NULL;
1604
1605 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1606 int ent = i & (TX_RING_SIZE - 1);
1607
1608 txd = &gb->txd[ent];
1609 dma_addr = le64_to_cpu(txd->buffer);
1610 pci_unmap_page(gp->pdev, dma_addr,
1611 le64_to_cpu(txd->control_word) &
1612 TXDCTRL_BUFSZ, PCI_DMA_TODEVICE);
1613
1614 if (frag != skb_shinfo(skb)->nr_frags)
1615 i++;
1616 }
1617 dev_kfree_skb_any(skb);
1618 }
1619 }
1620}
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622static void gem_init_rings(struct gem *gp)
1623{
1624 struct gem_init_block *gb = gp->init_block;
1625 struct net_device *dev = gp->dev;
1626 int i;
1627 dma_addr_t dma_addr;
1628
1629 gp->rx_new = gp->rx_old = gp->tx_new = gp->tx_old = 0;
1630
1631 gem_clean_rings(gp);
1632
1633 gp->rx_buf_sz = max(dev->mtu + ETH_HLEN + VLAN_HLEN,
1634 (unsigned)VLAN_ETH_FRAME_LEN);
1635
1636 for (i = 0; i < RX_RING_SIZE; i++) {
1637 struct sk_buff *skb;
1638 struct gem_rxd *rxd = &gb->rxd[i];
1639
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001640 skb = gem_alloc_skb(dev, RX_BUF_ALLOC_SIZE(gp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (!skb) {
1642 rxd->buffer = 0;
1643 rxd->status_word = 0;
1644 continue;
1645 }
1646
1647 gp->rx_skbs[i] = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 skb_put(skb, (gp->rx_buf_sz + RX_OFFSET));
1649 dma_addr = pci_map_page(gp->pdev,
1650 virt_to_page(skb->data),
1651 offset_in_page(skb->data),
1652 RX_BUF_ALLOC_SIZE(gp),
1653 PCI_DMA_FROMDEVICE);
1654 rxd->buffer = cpu_to_le64(dma_addr);
1655 wmb();
1656 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
1657 skb_reserve(skb, RX_OFFSET);
1658 }
1659
1660 for (i = 0; i < TX_RING_SIZE; i++) {
1661 struct gem_txd *txd = &gb->txd[i];
1662
1663 txd->control_word = 0;
1664 wmb();
1665 txd->buffer = 0;
1666 }
1667 wmb();
1668}
1669
1670/* Init PHY interface and start link poll state machine */
1671static void gem_init_phy(struct gem *gp)
1672{
David S. Miller7fb76aa2006-01-31 17:09:20 -08001673 u32 mifcfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 /* Revert MIF CFG setting done on stop_phy */
David S. Miller7fb76aa2006-01-31 17:09:20 -08001676 mifcfg = readl(gp->regs + MIF_CFG);
1677 mifcfg &= ~MIF_CFG_BBMODE;
1678 writel(mifcfg, gp->regs + MIF_CFG);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (gp->pdev->vendor == PCI_VENDOR_ID_APPLE) {
1681 int i;
1682
David S. Miller7fb76aa2006-01-31 17:09:20 -08001683 /* Those delay sucks, the HW seem to love them though, I'll
1684 * serisouly consider breaking some locks here to be able
1685 * to schedule instead
Benjamin Herrenschmidt40727192006-01-23 16:30:04 -08001686 */
David S. Miller7fb76aa2006-01-31 17:09:20 -08001687 for (i = 0; i < 3; i++) {
1688#ifdef CONFIG_PPC_PMAC
1689 pmac_call_feature(PMAC_FTR_GMAC_PHY_RESET, gp->of_node, 0, 0);
1690 msleep(20);
1691#endif
1692 /* Some PHYs used by apple have problem getting back to us,
1693 * we do an additional reset here
1694 */
1695 phy_write(gp, MII_BMCR, BMCR_RESET);
1696 msleep(20);
1697 if (phy_read(gp, MII_BMCR) != 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 break;
David S. Miller7fb76aa2006-01-31 17:09:20 -08001699 if (i == 2)
Joe Perchesc6c75982010-08-17 07:55:04 +00001700 netdev_warn(gp->dev, "GMAC PHY not responding !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
1702 }
1703
1704 if (gp->pdev->vendor == PCI_VENDOR_ID_SUN &&
1705 gp->pdev->device == PCI_DEVICE_ID_SUN_GEM) {
1706 u32 val;
1707
1708 /* Init datapath mode register. */
1709 if (gp->phy_type == phy_mii_mdio0 ||
1710 gp->phy_type == phy_mii_mdio1) {
1711 val = PCS_DMODE_MGM;
1712 } else if (gp->phy_type == phy_serialink) {
1713 val = PCS_DMODE_SM | PCS_DMODE_GMOE;
1714 } else {
1715 val = PCS_DMODE_ESM;
1716 }
1717
1718 writel(val, gp->regs + PCS_DMODE);
1719 }
1720
1721 if (gp->phy_type == phy_mii_mdio0 ||
1722 gp->phy_type == phy_mii_mdio1) {
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001723 /* Reset and detect MII PHY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 mii_phy_probe(&gp->phy_mii, gp->mii_phy_addr);
1725
1726 /* Init PHY */
1727 if (gp->phy_mii.def && gp->phy_mii.def->ops->init)
1728 gp->phy_mii.def->ops->init(&gp->phy_mii);
1729 } else {
David S. Miller8c83f802008-12-09 00:10:11 -08001730 gem_pcs_reset(gp);
1731 gem_pcs_reinit_adv(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
1734 /* Default aneg parameters */
1735 gp->timer_ticks = 0;
1736 gp->lstate = link_down;
1737 netif_carrier_off(gp->dev);
1738
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001739 /* Print things out */
1740 if (gp->phy_type == phy_mii_mdio0 ||
1741 gp->phy_type == phy_mii_mdio1)
1742 netdev_info(gp->dev, "Found %s PHY\n",
1743 gp->phy_mii.def ? gp->phy_mii.def->name : "no");
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 gem_begin_auto_negotiation(gp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static void gem_init_dma(struct gem *gp)
1749{
1750 u64 desc_dma = (u64) gp->gblock_dvma;
1751 u32 val;
1752
1753 val = (TXDMA_CFG_BASE | (0x7ff << 10) | TXDMA_CFG_PMODE);
1754 writel(val, gp->regs + TXDMA_CFG);
1755
1756 writel(desc_dma >> 32, gp->regs + TXDMA_DBHI);
1757 writel(desc_dma & 0xffffffff, gp->regs + TXDMA_DBLOW);
1758 desc_dma += (INIT_BLOCK_TX_RING_SIZE * sizeof(struct gem_txd));
1759
1760 writel(0, gp->regs + TXDMA_KICK);
1761
1762 val = (RXDMA_CFG_BASE | (RX_OFFSET << 10) |
1763 ((14 / 2) << 13) | RXDMA_CFG_FTHRESH_128);
1764 writel(val, gp->regs + RXDMA_CFG);
1765
1766 writel(desc_dma >> 32, gp->regs + RXDMA_DBHI);
1767 writel(desc_dma & 0xffffffff, gp->regs + RXDMA_DBLOW);
1768
1769 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
1770
1771 val = (((gp->rx_pause_off / 64) << 0) & RXDMA_PTHRESH_OFF);
1772 val |= (((gp->rx_pause_on / 64) << 12) & RXDMA_PTHRESH_ON);
1773 writel(val, gp->regs + RXDMA_PTHRESH);
1774
1775 if (readl(gp->regs + GREG_BIFCFG) & GREG_BIFCFG_M66EN)
1776 writel(((5 & RXDMA_BLANK_IPKTS) |
1777 ((8 << 12) & RXDMA_BLANK_ITIME)),
1778 gp->regs + RXDMA_BLANK);
1779 else
1780 writel(((5 & RXDMA_BLANK_IPKTS) |
1781 ((4 << 12) & RXDMA_BLANK_ITIME)),
1782 gp->regs + RXDMA_BLANK);
1783}
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785static u32 gem_setup_multicast(struct gem *gp)
1786{
1787 u32 rxcfg = 0;
1788 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if ((gp->dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001791 (netdev_mc_count(gp->dev) > 256)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 for (i=0; i<16; i++)
1793 writel(0xffff, gp->regs + MAC_HASH0 + (i << 2));
1794 rxcfg |= MAC_RXCFG_HFE;
1795 } else if (gp->dev->flags & IFF_PROMISC) {
1796 rxcfg |= MAC_RXCFG_PROM;
1797 } else {
1798 u16 hash_table[16];
1799 u32 crc;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001800 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 int i;
1802
Jiri Pirko55085902010-02-18 00:42:54 +00001803 memset(hash_table, 0, sizeof(hash_table));
Jiri Pirko22bedad32010-04-01 21:22:57 +00001804 netdev_for_each_mc_addr(ha, gp->dev) {
1805 char *addrs = ha->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (!(*addrs & 1))
1808 continue;
1809
1810 crc = ether_crc_le(6, addrs);
1811 crc >>= 24;
1812 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
1813 }
1814 for (i=0; i<16; i++)
1815 writel(hash_table[i], gp->regs + MAC_HASH0 + (i << 2));
1816 rxcfg |= MAC_RXCFG_HFE;
1817 }
1818
1819 return rxcfg;
1820}
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822static void gem_init_mac(struct gem *gp)
1823{
1824 unsigned char *e = &gp->dev->dev_addr[0];
1825
1826 writel(0x1bf0, gp->regs + MAC_SNDPAUSE);
1827
1828 writel(0x00, gp->regs + MAC_IPG0);
1829 writel(0x08, gp->regs + MAC_IPG1);
1830 writel(0x04, gp->regs + MAC_IPG2);
1831 writel(0x40, gp->regs + MAC_STIME);
1832 writel(0x40, gp->regs + MAC_MINFSZ);
1833
1834 /* Ethernet payload + header + FCS + optional VLAN tag. */
1835 writel(0x20000000 | (gp->rx_buf_sz + 4), gp->regs + MAC_MAXFSZ);
1836
1837 writel(0x07, gp->regs + MAC_PASIZE);
1838 writel(0x04, gp->regs + MAC_JAMSIZE);
1839 writel(0x10, gp->regs + MAC_ATTLIM);
1840 writel(0x8808, gp->regs + MAC_MCTYPE);
1841
1842 writel((e[5] | (e[4] << 8)) & 0x3ff, gp->regs + MAC_RANDSEED);
1843
1844 writel((e[4] << 8) | e[5], gp->regs + MAC_ADDR0);
1845 writel((e[2] << 8) | e[3], gp->regs + MAC_ADDR1);
1846 writel((e[0] << 8) | e[1], gp->regs + MAC_ADDR2);
1847
1848 writel(0, gp->regs + MAC_ADDR3);
1849 writel(0, gp->regs + MAC_ADDR4);
1850 writel(0, gp->regs + MAC_ADDR5);
1851
1852 writel(0x0001, gp->regs + MAC_ADDR6);
1853 writel(0xc200, gp->regs + MAC_ADDR7);
1854 writel(0x0180, gp->regs + MAC_ADDR8);
1855
1856 writel(0, gp->regs + MAC_AFILT0);
1857 writel(0, gp->regs + MAC_AFILT1);
1858 writel(0, gp->regs + MAC_AFILT2);
1859 writel(0, gp->regs + MAC_AF21MSK);
1860 writel(0, gp->regs + MAC_AF0MSK);
1861
1862 gp->mac_rx_cfg = gem_setup_multicast(gp);
1863#ifdef STRIP_FCS
1864 gp->mac_rx_cfg |= MAC_RXCFG_SFCS;
1865#endif
1866 writel(0, gp->regs + MAC_NCOLL);
1867 writel(0, gp->regs + MAC_FASUCC);
1868 writel(0, gp->regs + MAC_ECOLL);
1869 writel(0, gp->regs + MAC_LCOLL);
1870 writel(0, gp->regs + MAC_DTIMER);
1871 writel(0, gp->regs + MAC_PATMPS);
1872 writel(0, gp->regs + MAC_RFCTR);
1873 writel(0, gp->regs + MAC_LERR);
1874 writel(0, gp->regs + MAC_AERR);
1875 writel(0, gp->regs + MAC_FCSERR);
1876 writel(0, gp->regs + MAC_RXCVERR);
1877
1878 /* Clear RX/TX/MAC/XIF config, we will set these up and enable
1879 * them once a link is established.
1880 */
1881 writel(0, gp->regs + MAC_TXCFG);
1882 writel(gp->mac_rx_cfg, gp->regs + MAC_RXCFG);
1883 writel(0, gp->regs + MAC_MCCFG);
1884 writel(0, gp->regs + MAC_XIFCFG);
1885
1886 /* Setup MAC interrupts. We want to get all of the interesting
1887 * counter expiration events, but we do not want to hear about
1888 * normal rx/tx as the DMA engine tells us that.
1889 */
1890 writel(MAC_TXSTAT_XMIT, gp->regs + MAC_TXMASK);
1891 writel(MAC_RXSTAT_RCV, gp->regs + MAC_RXMASK);
1892
1893 /* Don't enable even the PAUSE interrupts for now, we
1894 * make no use of those events other than to record them.
1895 */
1896 writel(0xffffffff, gp->regs + MAC_MCMASK);
1897
1898 /* Don't enable GEM's WOL in normal operations
1899 */
1900 if (gp->has_wol)
1901 writel(0, gp->regs + WOL_WAKECSR);
1902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904static void gem_init_pause_thresholds(struct gem *gp)
1905{
1906 u32 cfg;
1907
1908 /* Calculate pause thresholds. Setting the OFF threshold to the
1909 * full RX fifo size effectively disables PAUSE generation which
1910 * is what we do for 10/100 only GEMs which have FIFOs too small
1911 * to make real gains from PAUSE.
1912 */
1913 if (gp->rx_fifo_sz <= (2 * 1024)) {
1914 gp->rx_pause_off = gp->rx_pause_on = gp->rx_fifo_sz;
1915 } else {
1916 int max_frame = (gp->rx_buf_sz + 4 + 64) & ~63;
1917 int off = (gp->rx_fifo_sz - (max_frame * 2));
1918 int on = off - max_frame;
1919
1920 gp->rx_pause_off = off;
1921 gp->rx_pause_on = on;
1922 }
1923
1924
1925 /* Configure the chip "burst" DMA mode & enable some
1926 * HW bug fixes on Apple version
1927 */
1928 cfg = 0;
1929 if (gp->pdev->vendor == PCI_VENDOR_ID_APPLE)
1930 cfg |= GREG_CFG_RONPAULBIT | GREG_CFG_ENBUG2FIX;
1931#if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
1932 cfg |= GREG_CFG_IBURST;
1933#endif
1934 cfg |= ((31 << 1) & GREG_CFG_TXDMALIM);
1935 cfg |= ((31 << 6) & GREG_CFG_RXDMALIM);
1936 writel(cfg, gp->regs + GREG_CFG);
1937
1938 /* If Infinite Burst didn't stick, then use different
1939 * thresholds (and Apple bug fixes don't exist)
1940 */
1941 if (!(readl(gp->regs + GREG_CFG) & GREG_CFG_IBURST)) {
1942 cfg = ((2 << 1) & GREG_CFG_TXDMALIM);
1943 cfg |= ((8 << 6) & GREG_CFG_RXDMALIM);
1944 writel(cfg, gp->regs + GREG_CFG);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
1948static int gem_check_invariants(struct gem *gp)
1949{
1950 struct pci_dev *pdev = gp->pdev;
1951 u32 mif_cfg;
1952
1953 /* On Apple's sungem, we can't rely on registers as the chip
1954 * was been powered down by the firmware. The PHY is looked
1955 * up later on.
1956 */
1957 if (pdev->vendor == PCI_VENDOR_ID_APPLE) {
1958 gp->phy_type = phy_mii_mdio0;
1959 gp->tx_fifo_sz = readl(gp->regs + TXDMA_FSZ) * 64;
1960 gp->rx_fifo_sz = readl(gp->regs + RXDMA_FSZ) * 64;
1961 gp->swrst_base = 0;
1962
1963 mif_cfg = readl(gp->regs + MIF_CFG);
1964 mif_cfg &= ~(MIF_CFG_PSELECT|MIF_CFG_POLL|MIF_CFG_BBMODE|MIF_CFG_MDI1);
1965 mif_cfg |= MIF_CFG_MDI0;
1966 writel(mif_cfg, gp->regs + MIF_CFG);
1967 writel(PCS_DMODE_MGM, gp->regs + PCS_DMODE);
1968 writel(MAC_XIFCFG_OE, gp->regs + MAC_XIFCFG);
1969
1970 /* We hard-code the PHY address so we can properly bring it out of
1971 * reset later on, we can't really probe it at this point, though
1972 * that isn't an issue.
1973 */
1974 if (gp->pdev->device == PCI_DEVICE_ID_APPLE_K2_GMAC)
1975 gp->mii_phy_addr = 1;
1976 else
1977 gp->mii_phy_addr = 0;
1978
1979 return 0;
1980 }
1981
1982 mif_cfg = readl(gp->regs + MIF_CFG);
1983
1984 if (pdev->vendor == PCI_VENDOR_ID_SUN &&
1985 pdev->device == PCI_DEVICE_ID_SUN_RIO_GEM) {
1986 /* One of the MII PHYs _must_ be present
1987 * as this chip has no gigabit PHY.
1988 */
1989 if ((mif_cfg & (MIF_CFG_MDI0 | MIF_CFG_MDI1)) == 0) {
Joe Perchesc6c75982010-08-17 07:55:04 +00001990 pr_err("RIO GEM lacks MII phy, mif_cfg[%08x]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 mif_cfg);
1992 return -1;
1993 }
1994 }
1995
1996 /* Determine initial PHY interface type guess. MDIO1 is the
1997 * external PHY and thus takes precedence over MDIO0.
1998 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (mif_cfg & MIF_CFG_MDI1) {
2001 gp->phy_type = phy_mii_mdio1;
2002 mif_cfg |= MIF_CFG_PSELECT;
2003 writel(mif_cfg, gp->regs + MIF_CFG);
2004 } else if (mif_cfg & MIF_CFG_MDI0) {
2005 gp->phy_type = phy_mii_mdio0;
2006 mif_cfg &= ~MIF_CFG_PSELECT;
2007 writel(mif_cfg, gp->regs + MIF_CFG);
2008 } else {
David S. Millere54d5512009-11-17 04:46:18 -08002009#ifdef CONFIG_SPARC
2010 const char *p;
2011
2012 p = of_get_property(gp->of_node, "shared-pins", NULL);
2013 if (p && !strcmp(p, "serdes"))
2014 gp->phy_type = phy_serdes;
2015 else
2016#endif
2017 gp->phy_type = phy_serialink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
2019 if (gp->phy_type == phy_mii_mdio1 ||
2020 gp->phy_type == phy_mii_mdio0) {
2021 int i;
2022
2023 for (i = 0; i < 32; i++) {
2024 gp->mii_phy_addr = i;
2025 if (phy_read(gp, MII_BMCR) != 0xffff)
2026 break;
2027 }
2028 if (i == 32) {
2029 if (pdev->device != PCI_DEVICE_ID_SUN_GEM) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002030 pr_err("RIO MII phy will not respond\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 return -1;
2032 }
2033 gp->phy_type = phy_serdes;
2034 }
2035 }
2036
2037 /* Fetch the FIFO configurations now too. */
2038 gp->tx_fifo_sz = readl(gp->regs + TXDMA_FSZ) * 64;
2039 gp->rx_fifo_sz = readl(gp->regs + RXDMA_FSZ) * 64;
2040
2041 if (pdev->vendor == PCI_VENDOR_ID_SUN) {
2042 if (pdev->device == PCI_DEVICE_ID_SUN_GEM) {
2043 if (gp->tx_fifo_sz != (9 * 1024) ||
2044 gp->rx_fifo_sz != (20 * 1024)) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002045 pr_err("GEM has bogus fifo sizes tx(%d) rx(%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 gp->tx_fifo_sz, gp->rx_fifo_sz);
2047 return -1;
2048 }
2049 gp->swrst_base = 0;
2050 } else {
2051 if (gp->tx_fifo_sz != (2 * 1024) ||
2052 gp->rx_fifo_sz != (2 * 1024)) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002053 pr_err("RIO GEM has bogus fifo sizes tx(%d) rx(%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 gp->tx_fifo_sz, gp->rx_fifo_sz);
2055 return -1;
2056 }
2057 gp->swrst_base = (64 / 4) << GREG_SWRST_CACHE_SHIFT;
2058 }
2059 }
2060
2061 return 0;
2062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064static void gem_reinit_chip(struct gem *gp)
2065{
2066 /* Reset the chip */
2067 gem_reset(gp);
2068
2069 /* Make sure ints are disabled */
2070 gem_disable_ints(gp);
2071
2072 /* Allocate & setup ring buffers */
2073 gem_init_rings(gp);
2074
2075 /* Configure pause thresholds */
2076 gem_init_pause_thresholds(gp);
2077
2078 /* Init DMA & MAC engines */
2079 gem_init_dma(gp);
2080 gem_init_mac(gp);
2081}
2082
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084static void gem_stop_phy(struct gem *gp, int wol)
2085{
David S. Miller7fb76aa2006-01-31 17:09:20 -08002086 u32 mifcfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 /* Let the chip settle down a bit, it seems that helps
2089 * for sleep mode on some models
2090 */
2091 msleep(10);
2092
2093 /* Make sure we aren't polling PHY status change. We
2094 * don't currently use that feature though
2095 */
David S. Miller7fb76aa2006-01-31 17:09:20 -08002096 mifcfg = readl(gp->regs + MIF_CFG);
2097 mifcfg &= ~MIF_CFG_POLL;
2098 writel(mifcfg, gp->regs + MIF_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 if (wol && gp->has_wol) {
2101 unsigned char *e = &gp->dev->dev_addr[0];
2102 u32 csr;
2103
2104 /* Setup wake-on-lan for MAGIC packet */
2105 writel(MAC_RXCFG_HFE | MAC_RXCFG_SFCS | MAC_RXCFG_ENAB,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002106 gp->regs + MAC_RXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 writel((e[4] << 8) | e[5], gp->regs + WOL_MATCH0);
2108 writel((e[2] << 8) | e[3], gp->regs + WOL_MATCH1);
2109 writel((e[0] << 8) | e[1], gp->regs + WOL_MATCH2);
2110
2111 writel(WOL_MCOUNT_N | WOL_MCOUNT_M, gp->regs + WOL_MCOUNT);
2112 csr = WOL_WAKECSR_ENABLE;
2113 if ((readl(gp->regs + MAC_XIFCFG) & MAC_XIFCFG_GMII) == 0)
2114 csr |= WOL_WAKECSR_MII;
2115 writel(csr, gp->regs + WOL_WAKECSR);
2116 } else {
2117 writel(0, gp->regs + MAC_RXCFG);
2118 (void)readl(gp->regs + MAC_RXCFG);
2119 /* Machine sleep will die in strange ways if we
2120 * dont wait a bit here, looks like the chip takes
2121 * some time to really shut down
2122 */
2123 msleep(10);
2124 }
2125
2126 writel(0, gp->regs + MAC_TXCFG);
2127 writel(0, gp->regs + MAC_XIFCFG);
2128 writel(0, gp->regs + TXDMA_CFG);
2129 writel(0, gp->regs + RXDMA_CFG);
2130
2131 if (!wol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 gem_reset(gp);
2133 writel(MAC_TXRST_CMD, gp->regs + MAC_TXRST);
2134 writel(MAC_RXRST_CMD, gp->regs + MAC_RXRST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 if (found_mii_phy(gp) && gp->phy_mii.def->ops->suspend)
2137 gp->phy_mii.def->ops->suspend(&gp->phy_mii);
2138
2139 /* According to Apple, we must set the MDIO pins to this begnign
2140 * state or we may 1) eat more current, 2) damage some PHYs
2141 */
David S. Miller7fb76aa2006-01-31 17:09:20 -08002142 writel(mifcfg | MIF_CFG_BBMODE, gp->regs + MIF_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 writel(0, gp->regs + MIF_BBCLK);
2144 writel(0, gp->regs + MIF_BBDATA);
2145 writel(0, gp->regs + MIF_BBOENAB);
2146 writel(MAC_XIFCFG_GMII | MAC_XIFCFG_LBCK, gp->regs + MAC_XIFCFG);
2147 (void) readl(gp->regs + MAC_XIFCFG);
2148 }
2149}
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151static int gem_do_start(struct net_device *dev)
2152{
Wang Chen8f15ea42008-11-12 23:38:36 -08002153 struct gem *gp = netdev_priv(dev);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002154 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 /* Enable the cell */
2157 gem_get_cell(gp);
2158
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002159 /* Make sure PCI access and bus master are enabled */
2160 rc = pci_enable_device(gp->pdev);
2161 if (rc) {
2162 netdev_err(dev, "Failed to enable chip on PCI bus !\n");
2163
2164 /* Put cell and forget it for now, it will be considered as
2165 * still asleep, a new sleep cycle may bring it back
2166 */
2167 gem_put_cell(gp);
2168 return -ENXIO;
2169 }
2170 pci_set_master(gp->pdev);
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* Init & setup chip hardware */
2173 gem_reinit_chip(gp);
2174
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002175 /* An interrupt might come in handy */
2176 rc = request_irq(gp->pdev->irq, gem_interrupt,
2177 IRQF_SHARED, dev->name, (void *)dev);
2178 if (rc) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002179 netdev_err(dev, "failed to request irq !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 gem_reset(gp);
2182 gem_clean_rings(gp);
2183 gem_put_cell(gp);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002184 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002187 /* Mark us as attached again if we come from resume(), this has
2188 * no effect if we weren't detatched and needs to be done now.
2189 */
2190 netif_device_attach(dev);
2191
2192 /* Restart NAPI & queues */
2193 gem_netif_start(gp);
2194
2195 /* Detect & init PHY, start autoneg etc... this will
2196 * eventually result in starting DMA operations when
2197 * the link is up
2198 */
2199 gem_init_phy(gp);
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return 0;
2202}
2203
2204static void gem_do_stop(struct net_device *dev, int wol)
2205{
Wang Chen8f15ea42008-11-12 23:38:36 -08002206 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002208 /* Stop NAPI and stop tx queue */
2209 gem_netif_stop(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002211 /* Make sure ints are disabled. We don't care about
2212 * synchronizing as NAPI is disabled, thus a stray
2213 * interrupt will do nothing bad (our irq handler
2214 * just schedules NAPI)
2215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 gem_disable_ints(gp);
2217
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002218 /* Stop the link timer */
2219 del_timer_sync(&gp->link_timer);
2220
2221 /* We cannot cancel the reset task while holding the
2222 * rtnl lock, we'd get an A->B / B->A deadlock stituation
2223 * if we did. This is not an issue however as the reset
2224 * task is synchronized vs. us (rtnl_lock) and will do
2225 * nothing if the device is down or suspended. We do
2226 * still clear reset_task_pending to avoid a spurrious
2227 * reset later on in case we do resume before it gets
2228 * scheduled.
2229 */
2230 gp->reset_task_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 /* If we are going to sleep with WOL */
2233 gem_stop_dma(gp);
2234 msleep(10);
2235 if (!wol)
2236 gem_reset(gp);
2237 msleep(10);
2238
2239 /* Get rid of rings */
2240 gem_clean_rings(gp);
2241
2242 /* No irq needed anymore */
2243 free_irq(gp->pdev->irq, (void *) dev);
2244
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002245 /* Shut the PHY down eventually and setup WOL */
2246 gem_stop_phy(gp, wol);
2247
2248 /* Make sure bus master is disabled */
2249 pci_disable_device(gp->pdev);
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 /* Cell not needed neither if no WOL */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002252 if (!wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 gem_put_cell(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
David Howellsc4028952006-11-22 14:57:56 +00002256static void gem_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
David Howellsc4028952006-11-22 14:57:56 +00002258 struct gem *gp = container_of(work, struct gem, reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002260 /* Lock out the network stack (essentially shield ourselves
2261 * against a racing open, close, control call, or suspend
2262 */
2263 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002265 /* Skip the reset task if suspended or closed, or if it's
2266 * been cancelled by gem_do_stop (see comment there)
2267 */
2268 if (!netif_device_present(gp->dev) ||
2269 !netif_running(gp->dev) ||
2270 !gp->reset_task_pending) {
2271 rtnl_unlock();
2272 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
Johannes Bergdde655c92007-11-29 21:51:36 +11002274
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002275 /* Stop the link timer */
2276 del_timer_sync(&gp->link_timer);
2277
2278 /* Stop NAPI and tx */
2279 gem_netif_stop(gp);
2280
2281 /* Reset the chip & rings */
2282 gem_reinit_chip(gp);
2283 if (gp->lstate == link_up)
2284 gem_set_link_modes(gp);
2285
2286 /* Restart NAPI and Tx */
2287 gem_netif_start(gp);
2288
2289 /* We are back ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 gp->reset_task_pending = 0;
2291
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002292 /* If the link is not up, restart autoneg, else restart the
2293 * polling timer
2294 */
2295 if (gp->lstate != link_up)
2296 gem_begin_auto_negotiation(gp, NULL);
2297 else
2298 mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002300 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303static int gem_open(struct net_device *dev)
2304{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002305 /* We allow open while suspended, we just do nothing,
2306 * the chip will be initialized in resume()
2307 */
2308 if (netif_device_present(dev))
2309 return gem_do_start(dev);
2310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
2313static int gem_close(struct net_device *dev)
2314{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002315 if (netif_device_present(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 gem_do_stop(dev, 0);
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return 0;
2319}
2320
2321#ifdef CONFIG_PM
2322static int gem_suspend(struct pci_dev *pdev, pm_message_t state)
2323{
2324 struct net_device *dev = pci_get_drvdata(pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002325 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002327 /* Lock the network stack first to avoid racing with open/close,
2328 * reset task and setting calls
2329 */
2330 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002332 /* Not running, mark ourselves non-present, no need for
2333 * a lock here
2334 */
2335 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 netif_device_detach(dev);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002337 rtnl_unlock();
2338 return 0;
2339 }
2340 netdev_info(dev, "suspending, WakeOnLan %s\n",
2341 (gp->wake_on_lan && netif_running(dev)) ?
2342 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002344 /* Tell the network stack we're gone. gem_do_stop() below will
2345 * synchronize with TX, stop NAPI etc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002347 netif_device_detach(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002349 /* Switch off chip, remember WOL setting */
2350 gp->asleep_wol = gp->wake_on_lan;
2351 gem_do_stop(dev, gp->asleep_wol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002353 /* Unlock the network stack */
2354 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 return 0;
2357}
2358
2359static int gem_resume(struct pci_dev *pdev)
2360{
2361 struct net_device *dev = pci_get_drvdata(pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002362 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002364 /* See locking comment in gem_suspend */
2365 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002367 /* Not running, mark ourselves present, no need for
2368 * a lock here
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002370 if (!netif_running(dev)) {
2371 netif_device_attach(dev);
2372 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return 0;
2374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002376 /* Restart chip. If that fails there isn't much we can do, we
2377 * leave things stopped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002379 gem_do_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 /* If we had WOL enabled, the cell clock was never turned off during
2382 * sleep, so we end up beeing unbalanced. Fix that here
2383 */
2384 if (gp->asleep_wol)
2385 gem_put_cell(gp);
2386
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002387 /* Unlock the network stack */
2388 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 return 0;
2391}
2392#endif /* CONFIG_PM */
2393
2394static struct net_device_stats *gem_get_stats(struct net_device *dev)
2395{
Wang Chen8f15ea42008-11-12 23:38:36 -08002396 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 /* I have seen this being called while the PM was in progress,
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002399 * so we shield against this. Let's also not poke at registers
2400 * while the reset task is going on.
2401 *
2402 * TODO: Move stats collection elsewhere (link timer ?) and
2403 * make this a nop to avoid all those synchro issues
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002405 if (!netif_device_present(dev) || !netif_running(dev))
2406 goto bail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002408 /* Better safe than sorry... */
2409 if (WARN_ON(!gp->cell_enabled))
2410 goto bail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002412 dev->stats.rx_crc_errors += readl(gp->regs + MAC_FCSERR);
2413 writel(0, gp->regs + MAC_FCSERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002415 dev->stats.rx_frame_errors += readl(gp->regs + MAC_AERR);
2416 writel(0, gp->regs + MAC_AERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002418 dev->stats.rx_length_errors += readl(gp->regs + MAC_LERR);
2419 writel(0, gp->regs + MAC_LERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002421 dev->stats.tx_aborted_errors += readl(gp->regs + MAC_ECOLL);
2422 dev->stats.collisions +=
2423 (readl(gp->regs + MAC_ECOLL) + readl(gp->regs + MAC_LCOLL));
2424 writel(0, gp->regs + MAC_ECOLL);
2425 writel(0, gp->regs + MAC_LCOLL);
2426 bail:
Denis Kirjanovaae7c472011-01-27 09:54:12 +00002427 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002430static int gem_set_mac_address(struct net_device *dev, void *addr)
2431{
2432 struct sockaddr *macaddr = (struct sockaddr *) addr;
Wang Chen8f15ea42008-11-12 23:38:36 -08002433 struct gem *gp = netdev_priv(dev);
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002434 unsigned char *e = &dev->dev_addr[0];
2435
2436 if (!is_valid_ether_addr(macaddr->sa_data))
2437 return -EADDRNOTAVAIL;
2438
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002439 memcpy(dev->dev_addr, macaddr->sa_data, dev->addr_len);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002440
2441 /* We'll just catch it later when the device is up'd or resumed */
2442 if (!netif_running(dev) || !netif_device_present(dev))
2443 return 0;
2444
2445 /* Better safe than sorry... */
2446 if (WARN_ON(!gp->cell_enabled))
2447 return 0;
2448
2449 writel((e[4] << 8) | e[5], gp->regs + MAC_ADDR0);
2450 writel((e[2] << 8) | e[3], gp->regs + MAC_ADDR1);
2451 writel((e[0] << 8) | e[1], gp->regs + MAC_ADDR2);
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002452
2453 return 0;
2454}
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456static void gem_set_multicast(struct net_device *dev)
2457{
Wang Chen8f15ea42008-11-12 23:38:36 -08002458 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 u32 rxcfg, rxcfg_new;
2460 int limit = 10000;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002461
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002462 if (!netif_running(dev) || !netif_device_present(dev))
2463 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002465 /* Better safe than sorry... */
2466 if (gp->reset_task_pending || WARN_ON(!gp->cell_enabled))
2467 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 rxcfg = readl(gp->regs + MAC_RXCFG);
2470 rxcfg_new = gem_setup_multicast(gp);
2471#ifdef STRIP_FCS
2472 rxcfg_new |= MAC_RXCFG_SFCS;
2473#endif
2474 gp->mac_rx_cfg = rxcfg_new;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 writel(rxcfg & ~MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
2477 while (readl(gp->regs + MAC_RXCFG) & MAC_RXCFG_ENAB) {
2478 if (!limit--)
2479 break;
2480 udelay(10);
2481 }
2482
2483 rxcfg &= ~(MAC_RXCFG_PROM | MAC_RXCFG_HFE);
2484 rxcfg |= rxcfg_new;
2485
2486 writel(rxcfg, gp->regs + MAC_RXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
2489/* Jumbo-grams don't seem to work :-( */
2490#define GEM_MIN_MTU 68
2491#if 1
2492#define GEM_MAX_MTU 1500
2493#else
2494#define GEM_MAX_MTU 9000
2495#endif
2496
2497static int gem_change_mtu(struct net_device *dev, int new_mtu)
2498{
Wang Chen8f15ea42008-11-12 23:38:36 -08002499 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 if (new_mtu < GEM_MIN_MTU || new_mtu > GEM_MAX_MTU)
2502 return -EINVAL;
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 dev->mtu = new_mtu;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002505
2506 /* We'll just catch it later when the device is up'd or resumed */
2507 if (!netif_running(dev) || !netif_device_present(dev))
2508 return 0;
2509
2510 /* Better safe than sorry... */
2511 if (WARN_ON(!gp->cell_enabled))
2512 return 0;
2513
2514 gem_netif_stop(gp);
2515 gem_reinit_chip(gp);
2516 if (gp->lstate == link_up)
2517 gem_set_link_modes(gp);
2518 gem_netif_start(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 return 0;
2521}
2522
2523static void gem_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2524{
Wang Chen8f15ea42008-11-12 23:38:36 -08002525 struct gem *gp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 strcpy(info->driver, DRV_NAME);
2528 strcpy(info->version, DRV_VERSION);
2529 strcpy(info->bus_info, pci_name(gp->pdev));
2530}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532static int gem_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2533{
Wang Chen8f15ea42008-11-12 23:38:36 -08002534 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 if (gp->phy_type == phy_mii_mdio0 ||
2537 gp->phy_type == phy_mii_mdio1) {
2538 if (gp->phy_mii.def)
2539 cmd->supported = gp->phy_mii.def->features;
2540 else
2541 cmd->supported = (SUPPORTED_10baseT_Half |
2542 SUPPORTED_10baseT_Full);
2543
2544 /* XXX hardcoded stuff for now */
2545 cmd->port = PORT_MII;
2546 cmd->transceiver = XCVR_EXTERNAL;
2547 cmd->phy_address = 0; /* XXX fixed PHYAD */
2548
2549 /* Return current PHY settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 cmd->autoneg = gp->want_autoneg;
David Decotigny70739492011-04-27 18:32:40 +00002551 ethtool_cmd_speed_set(cmd, gp->phy_mii.speed);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002552 cmd->duplex = gp->phy_mii.duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 cmd->advertising = gp->phy_mii.advertising;
2554
2555 /* If we started with a forced mode, we don't have a default
2556 * advertise set, we need to return something sensible so
2557 * userland can re-enable autoneg properly.
2558 */
2559 if (cmd->advertising == 0)
2560 cmd->advertising = cmd->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 } else { // XXX PCS ?
2562 cmd->supported =
2563 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2564 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2565 SUPPORTED_Autoneg);
2566 cmd->advertising = cmd->supported;
David Decotigny70739492011-04-27 18:32:40 +00002567 ethtool_cmd_speed_set(cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 cmd->duplex = cmd->port = cmd->phy_address =
2569 cmd->transceiver = cmd->autoneg = 0;
Hermann Lauerfbf02292008-12-09 15:39:14 -08002570
2571 /* serdes means usually a Fibre connector, with most fixed */
2572 if (gp->phy_type == phy_serdes) {
2573 cmd->port = PORT_FIBRE;
2574 cmd->supported = (SUPPORTED_1000baseT_Half |
2575 SUPPORTED_1000baseT_Full |
2576 SUPPORTED_FIBRE | SUPPORTED_Autoneg |
2577 SUPPORTED_Pause | SUPPORTED_Asym_Pause);
2578 cmd->advertising = cmd->supported;
2579 cmd->transceiver = XCVR_INTERNAL;
2580 if (gp->lstate == link_up)
David Decotigny70739492011-04-27 18:32:40 +00002581 ethtool_cmd_speed_set(cmd, SPEED_1000);
Hermann Lauerfbf02292008-12-09 15:39:14 -08002582 cmd->duplex = DUPLEX_FULL;
2583 cmd->autoneg = 1;
2584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
2586 cmd->maxtxpkt = cmd->maxrxpkt = 0;
2587
2588 return 0;
2589}
2590
2591static int gem_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2592{
Wang Chen8f15ea42008-11-12 23:38:36 -08002593 struct gem *gp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +00002594 u32 speed = ethtool_cmd_speed(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 /* Verify the settings we care about. */
2597 if (cmd->autoneg != AUTONEG_ENABLE &&
2598 cmd->autoneg != AUTONEG_DISABLE)
2599 return -EINVAL;
2600
2601 if (cmd->autoneg == AUTONEG_ENABLE &&
2602 cmd->advertising == 0)
2603 return -EINVAL;
2604
2605 if (cmd->autoneg == AUTONEG_DISABLE &&
David Decotigny25db0332011-04-27 18:32:39 +00002606 ((speed != SPEED_1000 &&
2607 speed != SPEED_100 &&
2608 speed != SPEED_10) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 (cmd->duplex != DUPLEX_HALF &&
2610 cmd->duplex != DUPLEX_FULL)))
2611 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /* Apply settings and restart link process. */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002614 if (netif_device_present(gp->dev)) {
2615 del_timer_sync(&gp->link_timer);
2616 gem_begin_auto_negotiation(gp, cmd);
2617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619 return 0;
2620}
2621
2622static int gem_nway_reset(struct net_device *dev)
2623{
Wang Chen8f15ea42008-11-12 23:38:36 -08002624 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 if (!gp->want_autoneg)
2627 return -EINVAL;
2628
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002629 /* Restart link process */
2630 if (netif_device_present(gp->dev)) {
2631 del_timer_sync(&gp->link_timer);
2632 gem_begin_auto_negotiation(gp, NULL);
2633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 return 0;
2636}
2637
2638static u32 gem_get_msglevel(struct net_device *dev)
2639{
Wang Chen8f15ea42008-11-12 23:38:36 -08002640 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return gp->msg_enable;
2642}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644static void gem_set_msglevel(struct net_device *dev, u32 value)
2645{
Wang Chen8f15ea42008-11-12 23:38:36 -08002646 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 gp->msg_enable = value;
2648}
2649
2650
2651/* Add more when I understand how to program the chip */
2652/* like WAKE_UCAST | WAKE_MCAST | WAKE_BCAST */
2653
2654#define WOL_SUPPORTED_MASK (WAKE_MAGIC)
2655
2656static void gem_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2657{
Wang Chen8f15ea42008-11-12 23:38:36 -08002658 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 /* Add more when I understand how to program the chip */
2661 if (gp->has_wol) {
2662 wol->supported = WOL_SUPPORTED_MASK;
2663 wol->wolopts = gp->wake_on_lan;
2664 } else {
2665 wol->supported = 0;
2666 wol->wolopts = 0;
2667 }
2668}
2669
2670static int gem_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2671{
Wang Chen8f15ea42008-11-12 23:38:36 -08002672 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674 if (!gp->has_wol)
2675 return -EOPNOTSUPP;
2676 gp->wake_on_lan = wol->wolopts & WOL_SUPPORTED_MASK;
2677 return 0;
2678}
2679
Jeff Garzik7282d492006-09-13 14:30:00 -04002680static const struct ethtool_ops gem_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 .get_drvinfo = gem_get_drvinfo,
2682 .get_link = ethtool_op_get_link,
2683 .get_settings = gem_get_settings,
2684 .set_settings = gem_set_settings,
2685 .nway_reset = gem_nway_reset,
2686 .get_msglevel = gem_get_msglevel,
2687 .set_msglevel = gem_set_msglevel,
2688 .get_wol = gem_get_wol,
2689 .set_wol = gem_set_wol,
2690};
2691
2692static int gem_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2693{
Wang Chen8f15ea42008-11-12 23:38:36 -08002694 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 struct mii_ioctl_data *data = if_mii(ifr);
2696 int rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002698 /* For SIOCGMIIREG and SIOCSMIIREG the core checks for us that
2699 * netif_device_present() is true and holds rtnl_lock for us
2700 * so we have nothing to worry about
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 switch (cmd) {
2704 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2705 data->phy_id = gp->mii_phy_addr;
2706 /* Fallthrough... */
2707
2708 case SIOCGMIIREG: /* Read MII PHY register. */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002709 data->val_out = __phy_read(gp, data->phy_id & 0x1f,
2710 data->reg_num & 0x1f);
2711 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 break;
2713
2714 case SIOCSMIIREG: /* Write MII PHY register. */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002715 __phy_write(gp, data->phy_id & 0x1f, data->reg_num & 0x1f,
2716 data->val_in);
2717 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 break;
Joe Perches6403eab2011-06-03 11:51:20 +00002719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return rc;
2721}
2722
David S. Millerdadb8302007-02-28 15:42:50 -08002723#if (!defined(CONFIG_SPARC) && !defined(CONFIG_PPC_PMAC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724/* Fetch MAC address from vital product data of PCI ROM. */
Linus Torvalds4120b022005-09-11 09:26:20 -07002725static int find_eth_addr_in_vpd(void __iomem *rom_base, int len, unsigned char *dev_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 int this_offset;
2728
2729 for (this_offset = 0x20; this_offset < len; this_offset++) {
2730 void __iomem *p = rom_base + this_offset;
2731 int i;
2732
2733 if (readb(p + 0) != 0x90 ||
2734 readb(p + 1) != 0x00 ||
2735 readb(p + 2) != 0x09 ||
2736 readb(p + 3) != 0x4e ||
2737 readb(p + 4) != 0x41 ||
2738 readb(p + 5) != 0x06)
2739 continue;
2740
2741 this_offset += 6;
2742 p += 6;
2743
2744 for (i = 0; i < 6; i++)
2745 dev_addr[i] = readb(p + i);
Linus Torvalds4120b022005-09-11 09:26:20 -07002746 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
Linus Torvalds4120b022005-09-11 09:26:20 -07002748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
2751static void get_gem_mac_nonobp(struct pci_dev *pdev, unsigned char *dev_addr)
2752{
Linus Torvalds4120b022005-09-11 09:26:20 -07002753 size_t size;
2754 void __iomem *p = pci_map_rom(pdev, &size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Linus Torvalds4120b022005-09-11 09:26:20 -07002756 if (p) {
2757 int found;
2758
2759 found = readb(p) == 0x55 &&
2760 readb(p + 1) == 0xaa &&
2761 find_eth_addr_in_vpd(p, (64 * 1024), dev_addr);
2762 pci_unmap_rom(pdev, p);
2763 if (found)
2764 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 }
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 /* Sun MAC prefix then 3 random bytes. */
2768 dev_addr[0] = 0x08;
2769 dev_addr[1] = 0x00;
2770 dev_addr[2] = 0x20;
2771 get_random_bytes(dev_addr + 3, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772}
2773#endif /* not Sparc and not PPC */
2774
2775static int __devinit gem_get_device_address(struct gem *gp)
2776{
David S. Millerdadb8302007-02-28 15:42:50 -08002777#if defined(CONFIG_SPARC) || defined(CONFIG_PPC_PMAC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 struct net_device *dev = gp->dev;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002779 const unsigned char *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10002781 addr = of_get_property(gp->of_node, "local-mac-address", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (addr == NULL) {
David S. Miller457e1a82007-03-29 01:36:44 -07002783#ifdef CONFIG_SPARC
2784 addr = idprom->id_ethaddr;
2785#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 printk("\n");
Joe Perchesc6c75982010-08-17 07:55:04 +00002787 pr_err("%s: can't get mac-address\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return -1;
David S. Miller457e1a82007-03-29 01:36:44 -07002789#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791 memcpy(dev->dev_addr, addr, 6);
2792#else
2793 get_gem_mac_nonobp(gp->pdev, gp->dev->dev_addr);
2794#endif
2795 return 0;
2796}
2797
Adrian Bunk14904392005-12-21 18:50:12 -08002798static void gem_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 struct net_device *dev = pci_get_drvdata(pdev);
2801
2802 if (dev) {
Wang Chen8f15ea42008-11-12 23:38:36 -08002803 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 unregister_netdev(dev);
2806
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002807 /* Ensure reset task is truely gone */
Tejun Heofe8998c2010-12-12 16:45:14 +01002808 cancel_work_sync(&gp->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 /* Free resources */
2811 pci_free_consistent(pdev,
2812 sizeof(struct gem_init_block),
2813 gp->init_block,
2814 gp->gblock_dvma);
2815 iounmap(gp->regs);
2816 pci_release_regions(pdev);
2817 free_netdev(dev);
2818
2819 pci_set_drvdata(pdev, NULL);
2820 }
2821}
2822
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002823static const struct net_device_ops gem_netdev_ops = {
2824 .ndo_open = gem_open,
2825 .ndo_stop = gem_close,
2826 .ndo_start_xmit = gem_start_xmit,
2827 .ndo_get_stats = gem_get_stats,
2828 .ndo_set_multicast_list = gem_set_multicast,
2829 .ndo_do_ioctl = gem_ioctl,
2830 .ndo_tx_timeout = gem_tx_timeout,
2831 .ndo_change_mtu = gem_change_mtu,
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002832 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger5ed01022009-03-19 23:58:01 -07002833 .ndo_set_mac_address = gem_set_mac_address,
2834#ifdef CONFIG_NET_POLL_CONTROLLER
2835 .ndo_poll_controller = gem_poll_controller,
2836#endif
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002837};
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839static int __devinit gem_init_one(struct pci_dev *pdev,
2840 const struct pci_device_id *ent)
2841{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 unsigned long gemreg_base, gemreg_len;
2843 struct net_device *dev;
2844 struct gem *gp;
Joe Perches0795af52007-10-03 17:59:30 -07002845 int err, pci_using_dac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Joe Perchesc6c75982010-08-17 07:55:04 +00002847 printk_once(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 /* Apple gmac note: during probe, the chip is powered up by
2850 * the arch code to allow the code below to work (and to let
2851 * the chip be probed on the config space. It won't stay powered
2852 * up until the interface is brought up however, so we can't rely
2853 * on register configuration done at this point.
2854 */
2855 err = pci_enable_device(pdev);
2856 if (err) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002857 pr_err("Cannot enable MMIO operation, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return err;
2859 }
2860 pci_set_master(pdev);
2861
2862 /* Configure DMA attributes. */
2863
2864 /* All of the GEM documentation states that 64-bit DMA addressing
2865 * is fully supported and should work just fine. However the
2866 * front end for RIO based GEMs is different and only supports
2867 * 32-bit addressing.
2868 *
2869 * For now we assume the various PPC GEMs are 32-bit only as well.
2870 */
2871 if (pdev->vendor == PCI_VENDOR_ID_SUN &&
2872 pdev->device == PCI_DEVICE_ID_SUN_GEM &&
Yang Hongyang6a355282009-04-06 19:01:13 -07002873 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 pci_using_dac = 1;
2875 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07002876 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (err) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002878 pr_err("No usable DMA configuration, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 goto err_disable_device;
2880 }
2881 pci_using_dac = 0;
2882 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 gemreg_base = pci_resource_start(pdev, 0);
2885 gemreg_len = pci_resource_len(pdev, 0);
2886
2887 if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002888 pr_err("Cannot find proper PCI device base address, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 err = -ENODEV;
2890 goto err_disable_device;
2891 }
2892
2893 dev = alloc_etherdev(sizeof(*gp));
2894 if (!dev) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002895 pr_err("Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 err = -ENOMEM;
2897 goto err_disable_device;
2898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 SET_NETDEV_DEV(dev, &pdev->dev);
2900
Wang Chen8f15ea42008-11-12 23:38:36 -08002901 gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 err = pci_request_regions(pdev, DRV_NAME);
2904 if (err) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002905 pr_err("Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 goto err_out_free_netdev;
2907 }
2908
2909 gp->pdev = pdev;
2910 dev->base_addr = (long) pdev;
2911 gp->dev = dev;
2912
2913 gp->msg_enable = DEFAULT_MSG;
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 init_timer(&gp->link_timer);
2916 gp->link_timer.function = gem_link_timer;
2917 gp->link_timer.data = (unsigned long) gp;
2918
David Howellsc4028952006-11-22 14:57:56 +00002919 INIT_WORK(&gp->reset_task, gem_reset_task);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 gp->lstate = link_down;
2922 gp->timer_ticks = 0;
2923 netif_carrier_off(dev);
2924
2925 gp->regs = ioremap(gemreg_base, gemreg_len);
Al Viro79ea13c2008-01-24 02:06:46 -08002926 if (!gp->regs) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002927 pr_err("Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 err = -EIO;
2929 goto err_out_free_res;
2930 }
2931
2932 /* On Apple, we want a reference to the Open Firmware device-tree
2933 * node. We use it for clock control.
2934 */
David S. Miller457e1a82007-03-29 01:36:44 -07002935#if defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 gp->of_node = pci_device_to_OF_node(pdev);
2937#endif
2938
2939 /* Only Apple version supports WOL afaik */
2940 if (pdev->vendor == PCI_VENDOR_ID_APPLE)
2941 gp->has_wol = 1;
2942
2943 /* Make sure cell is enabled */
2944 gem_get_cell(gp);
2945
2946 /* Make sure everything is stopped and in init state */
2947 gem_reset(gp);
2948
2949 /* Fill up the mii_phy structure (even if we won't use it) */
2950 gp->phy_mii.dev = dev;
2951 gp->phy_mii.mdio_read = _phy_read;
2952 gp->phy_mii.mdio_write = _phy_write;
Benjamin Herrenschmidt3c326fe2005-07-07 17:56:09 -07002953#ifdef CONFIG_PPC_PMAC
2954 gp->phy_mii.platform_data = gp->of_node;
2955#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 /* By default, we start with autoneg */
2957 gp->want_autoneg = 1;
2958
2959 /* Check fifo sizes, PHY type, etc... */
2960 if (gem_check_invariants(gp)) {
2961 err = -ENODEV;
2962 goto err_out_iounmap;
2963 }
2964
2965 /* It is guaranteed that the returned buffer will be at least
2966 * PAGE_SIZE aligned.
2967 */
2968 gp->init_block = (struct gem_init_block *)
2969 pci_alloc_consistent(pdev, sizeof(struct gem_init_block),
2970 &gp->gblock_dvma);
2971 if (!gp->init_block) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002972 pr_err("Cannot allocate init block, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 err = -ENOMEM;
2974 goto err_out_iounmap;
2975 }
2976
2977 if (gem_get_device_address(gp))
2978 goto err_out_free_consistent;
2979
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002980 dev->netdev_ops = &gem_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002981 netif_napi_add(dev, &gp->napi, gem_poll, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 dev->ethtool_ops = &gem_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 dev->watchdog_timeo = 5 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 dev->irq = pdev->irq;
2985 dev->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 /* Set that now, in case PM kicks in now */
2988 pci_set_drvdata(pdev, dev);
2989
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002990 /* We can do scatter/gather and HW checksum */
2991 dev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM;
2992 dev->features |= dev->hw_features | NETIF_F_RXCSUM;
2993 if (pci_using_dac)
2994 dev->features |= NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 /* Register with kernel */
2997 if (register_netdev(dev)) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002998 pr_err("Cannot register net device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 err = -ENOMEM;
3000 goto err_out_free_consistent;
3001 }
3002
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10003003 /* Undo the get_cell with appropriate locking (we could use
3004 * ndo_init/uninit but that would be even more clumsy imho)
3005 */
3006 rtnl_lock();
3007 gem_put_cell(gp);
3008 rtnl_unlock();
3009
Joe Perchesc6c75982010-08-17 07:55:04 +00003010 netdev_info(dev, "Sun GEM (PCI) 10/100/1000BaseT Ethernet %pM\n",
3011 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return 0;
3013
3014err_out_free_consistent:
3015 gem_remove_one(pdev);
3016err_out_iounmap:
3017 gem_put_cell(gp);
3018 iounmap(gp->regs);
3019
3020err_out_free_res:
3021 pci_release_regions(pdev);
3022
3023err_out_free_netdev:
3024 free_netdev(dev);
3025err_disable_device:
3026 pci_disable_device(pdev);
3027 return err;
3028
3029}
3030
3031
3032static struct pci_driver gem_driver = {
3033 .name = GEM_MODULE_NAME,
3034 .id_table = gem_pci_tbl,
3035 .probe = gem_init_one,
Adrian Bunk14904392005-12-21 18:50:12 -08003036 .remove = gem_remove_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037#ifdef CONFIG_PM
3038 .suspend = gem_suspend,
3039 .resume = gem_resume,
3040#endif /* CONFIG_PM */
3041};
3042
3043static int __init gem_init(void)
3044{
Jeff Garzik29917622006-08-19 17:48:59 -04003045 return pci_register_driver(&gem_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
3048static void __exit gem_cleanup(void)
3049{
3050 pci_unregister_driver(&gem_driver);
3051}
3052
3053module_init(gem_init);
3054module_exit(gem_cleanup);