1 /* $Id: sungem.c,v 1.44.2.22 2002/03/13 01:18:12 davem Exp $
2 * sungem.c: Sun GEM ethernet driver.
4 * Copyright (C) 2000, 2001, 2002, 2003 David S. Miller (davem@redhat.com)
6 * 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.
10 * NAPI and NETPOLL support
11 * (C) 2004 by Eric Lemoine (eric.lemoine@gmail.com)
14 * - Now that the driver was significantly simplified, I need to rework
15 * the locking. I'm sure we don't need _2_ spinlocks, and we probably
16 * can avoid taking most of them for so long period of time (and schedule
17 * instead). The main issues at this point are caused by the netdev layer
20 * gem_change_mtu() and gem_set_multicast() are called with a read_lock()
21 * help by net/core/dev.c, thus they can't schedule. That means they can't
22 * call napi_disable() neither, thus force gem_poll() to keep a spinlock
23 * where it could have been dropped. change_mtu especially would love also to
24 * be able to msleep instead of horrid locked delays when resetting the HW,
25 * but that read_lock() makes it impossible, unless I defer it's action to
26 * the reset task, which means it'll be asynchronous (won't take effect until
27 * the system schedules a bit).
29 * Also, it would probably be possible to also remove most of the long-life
30 * locking in open/resume code path (gem_reinit_chip) by beeing more careful
31 * about when we can start taking interrupts or get xmit() called...
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/fcntl.h>
38 #include <linux/interrupt.h>
39 #include <linux/ioport.h>
41 #include <linux/slab.h>
42 #include <linux/string.h>
43 #include <linux/delay.h>
44 #include <linux/init.h>
45 #include <linux/errno.h>
46 #include <linux/pci.h>
47 #include <linux/dma-mapping.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/skbuff.h>
51 #include <linux/mii.h>
52 #include <linux/ethtool.h>
53 #include <linux/crc32.h>
54 #include <linux/random.h>
55 #include <linux/workqueue.h>
56 #include <linux/if_vlan.h>
57 #include <linux/bitops.h>
58 #include <linux/mutex.h>
61 #include <asm/system.h>
63 #include <asm/byteorder.h>
64 #include <asm/uaccess.h>
68 #include <asm/idprom.h>
72 #ifdef CONFIG_PPC_PMAC
73 #include <asm/pci-bridge.h>
75 #include <asm/machdep.h>
76 #include <asm/pmac_feature.h>
79 #include "sungem_phy.h"
82 /* Stripping FCS is causing problems, disabled for now */
85 #define DEFAULT_MSG (NETIF_MSG_DRV | \
89 #define ADVERTISE_MASK (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | \
90 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | \
91 SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full | \
92 SUPPORTED_Pause | SUPPORTED_Autoneg)
94 #define DRV_NAME "sungem"
95 #define DRV_VERSION "0.98"
96 #define DRV_RELDATE "8/24/03"
97 #define DRV_AUTHOR "David S. Miller (davem@redhat.com)"
99 static char version[] __devinitdata =
100 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
102 MODULE_AUTHOR(DRV_AUTHOR);
103 MODULE_DESCRIPTION("Sun GEM Gbit ethernet driver");
104 MODULE_LICENSE("GPL");
106 #define GEM_MODULE_NAME "gem"
107 #define PFX GEM_MODULE_NAME ": "
109 static struct pci_device_id gem_pci_tbl[] = {
110 { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_GEM,
111 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
113 /* These models only differ from the original GEM in
114 * that their tx/rx fifos are of a different size and
115 * they only support 10/100 speeds. -DaveM
117 * Apple's GMAC does support gigabit on machines with
118 * the BCM54xx PHYs. -BenH
120 { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_RIO_GEM,
121 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
122 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMAC,
123 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
124 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMACP,
125 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
126 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMAC2,
127 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
128 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_K2_GMAC,
129 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
130 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_SH_SUNGEM,
131 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
132 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_IPID2_GMAC,
133 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
137 MODULE_DEVICE_TABLE(pci, gem_pci_tbl);
139 static u16 __phy_read(struct gem *gp, int phy_addr, int reg)
146 cmd |= (phy_addr << 23) & MIF_FRAME_PHYAD;
147 cmd |= (reg << 18) & MIF_FRAME_REGAD;
148 cmd |= (MIF_FRAME_TAMSB);
149 writel(cmd, gp->regs + MIF_FRAME);
152 cmd = readl(gp->regs + MIF_FRAME);
153 if (cmd & MIF_FRAME_TALSB)
162 return cmd & MIF_FRAME_DATA;
165 static inline int _phy_read(struct net_device *dev, int mii_id, int reg)
167 struct gem *gp = netdev_priv(dev);
168 return __phy_read(gp, mii_id, reg);
171 static inline u16 phy_read(struct gem *gp, int reg)
173 return __phy_read(gp, gp->mii_phy_addr, reg);
176 static void __phy_write(struct gem *gp, int phy_addr, int reg, u16 val)
183 cmd |= (phy_addr << 23) & MIF_FRAME_PHYAD;
184 cmd |= (reg << 18) & MIF_FRAME_REGAD;
185 cmd |= (MIF_FRAME_TAMSB);
186 cmd |= (val & MIF_FRAME_DATA);
187 writel(cmd, gp->regs + MIF_FRAME);
190 cmd = readl(gp->regs + MIF_FRAME);
191 if (cmd & MIF_FRAME_TALSB)
198 static inline void _phy_write(struct net_device *dev, int mii_id, int reg, int val)
200 struct gem *gp = netdev_priv(dev);
201 __phy_write(gp, mii_id, reg, val & 0xffff);
204 static inline void phy_write(struct gem *gp, int reg, u16 val)
206 __phy_write(gp, gp->mii_phy_addr, reg, val);
209 static inline void gem_enable_ints(struct gem *gp)
211 /* Enable all interrupts but TXDONE */
212 writel(GREG_STAT_TXDONE, gp->regs + GREG_IMASK);
215 static inline void gem_disable_ints(struct gem *gp)
217 /* Disable all interrupts, including TXDONE */
218 writel(GREG_STAT_NAPI | GREG_STAT_TXDONE, gp->regs + GREG_IMASK);
221 static void gem_get_cell(struct gem *gp)
223 BUG_ON(gp->cell_enabled < 0);
225 #ifdef CONFIG_PPC_PMAC
226 if (gp->cell_enabled == 1) {
228 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, gp->of_node, 0, 1);
231 #endif /* CONFIG_PPC_PMAC */
234 /* Turn off the chip's clock */
235 static void gem_put_cell(struct gem *gp)
237 BUG_ON(gp->cell_enabled <= 0);
239 #ifdef CONFIG_PPC_PMAC
240 if (gp->cell_enabled == 0) {
242 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, gp->of_node, 0, 0);
245 #endif /* CONFIG_PPC_PMAC */
248 static void gem_handle_mif_event(struct gem *gp, u32 reg_val, u32 changed_bits)
250 if (netif_msg_intr(gp))
251 printk(KERN_DEBUG "%s: mif interrupt\n", gp->dev->name);
254 static int gem_pcs_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
256 u32 pcs_istat = readl(gp->regs + PCS_ISTAT);
259 if (netif_msg_intr(gp))
260 printk(KERN_DEBUG "%s: pcs interrupt, pcs_istat: 0x%x\n",
261 gp->dev->name, pcs_istat);
263 if (!(pcs_istat & PCS_ISTAT_LSC)) {
264 printk(KERN_ERR "%s: PCS irq but no link status change???\n",
269 /* The link status bit latches on zero, so you must
270 * read it twice in such a case to see a transition
271 * to the link being up.
273 pcs_miistat = readl(gp->regs + PCS_MIISTAT);
274 if (!(pcs_miistat & PCS_MIISTAT_LS))
276 (readl(gp->regs + PCS_MIISTAT) &
279 if (pcs_miistat & PCS_MIISTAT_ANC) {
280 /* The remote-fault indication is only valid
281 * when autoneg has completed.
283 if (pcs_miistat & PCS_MIISTAT_RF)
284 printk(KERN_INFO "%s: PCS AutoNEG complete, "
285 "RemoteFault\n", dev->name);
287 printk(KERN_INFO "%s: PCS AutoNEG complete.\n",
291 if (pcs_miistat & PCS_MIISTAT_LS) {
292 printk(KERN_INFO "%s: PCS link is now up.\n",
294 netif_carrier_on(gp->dev);
296 printk(KERN_INFO "%s: PCS link is now down.\n",
298 netif_carrier_off(gp->dev);
299 /* If this happens and the link timer is not running,
300 * reset so we re-negotiate.
302 if (!timer_pending(&gp->link_timer))
309 static int gem_txmac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
311 u32 txmac_stat = readl(gp->regs + MAC_TXSTAT);
313 if (netif_msg_intr(gp))
314 printk(KERN_DEBUG "%s: txmac interrupt, txmac_stat: 0x%x\n",
315 gp->dev->name, txmac_stat);
317 /* Defer timer expiration is quite normal,
318 * don't even log the event.
320 if ((txmac_stat & MAC_TXSTAT_DTE) &&
321 !(txmac_stat & ~MAC_TXSTAT_DTE))
324 if (txmac_stat & MAC_TXSTAT_URUN) {
325 printk(KERN_ERR "%s: TX MAC xmit underrun.\n",
327 gp->net_stats.tx_fifo_errors++;
330 if (txmac_stat & MAC_TXSTAT_MPE) {
331 printk(KERN_ERR "%s: TX MAC max packet size error.\n",
333 gp->net_stats.tx_errors++;
336 /* The rest are all cases of one of the 16-bit TX
339 if (txmac_stat & MAC_TXSTAT_NCE)
340 gp->net_stats.collisions += 0x10000;
342 if (txmac_stat & MAC_TXSTAT_ECE) {
343 gp->net_stats.tx_aborted_errors += 0x10000;
344 gp->net_stats.collisions += 0x10000;
347 if (txmac_stat & MAC_TXSTAT_LCE) {
348 gp->net_stats.tx_aborted_errors += 0x10000;
349 gp->net_stats.collisions += 0x10000;
352 /* We do not keep track of MAC_TXSTAT_FCE and
353 * MAC_TXSTAT_PCE events.
358 /* When we get a RX fifo overflow, the RX unit in GEM is probably hung
359 * so we do the following.
361 * If any part of the reset goes wrong, we return 1 and that causes the
362 * whole chip to be reset.
364 static int gem_rxmac_reset(struct gem *gp)
366 struct net_device *dev = gp->dev;
371 /* First, reset & disable MAC RX. */
372 writel(MAC_RXRST_CMD, gp->regs + MAC_RXRST);
373 for (limit = 0; limit < 5000; limit++) {
374 if (!(readl(gp->regs + MAC_RXRST) & MAC_RXRST_CMD))
379 printk(KERN_ERR "%s: RX MAC will not reset, resetting whole "
380 "chip.\n", dev->name);
384 writel(gp->mac_rx_cfg & ~MAC_RXCFG_ENAB,
385 gp->regs + MAC_RXCFG);
386 for (limit = 0; limit < 5000; limit++) {
387 if (!(readl(gp->regs + MAC_RXCFG) & MAC_RXCFG_ENAB))
392 printk(KERN_ERR "%s: RX MAC will not disable, resetting whole "
393 "chip.\n", dev->name);
397 /* Second, disable RX DMA. */
398 writel(0, gp->regs + RXDMA_CFG);
399 for (limit = 0; limit < 5000; limit++) {
400 if (!(readl(gp->regs + RXDMA_CFG) & RXDMA_CFG_ENABLE))
405 printk(KERN_ERR "%s: RX DMA will not disable, resetting whole "
406 "chip.\n", dev->name);
412 /* Execute RX reset command. */
413 writel(gp->swrst_base | GREG_SWRST_RXRST,
414 gp->regs + GREG_SWRST);
415 for (limit = 0; limit < 5000; limit++) {
416 if (!(readl(gp->regs + GREG_SWRST) & GREG_SWRST_RXRST))
421 printk(KERN_ERR "%s: RX reset command will not execute, resetting "
422 "whole chip.\n", dev->name);
426 /* Refresh the RX ring. */
427 for (i = 0; i < RX_RING_SIZE; i++) {
428 struct gem_rxd *rxd = &gp->init_block->rxd[i];
430 if (gp->rx_skbs[i] == NULL) {
431 printk(KERN_ERR "%s: Parts of RX ring empty, resetting "
432 "whole chip.\n", dev->name);
436 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
438 gp->rx_new = gp->rx_old = 0;
440 /* Now we must reprogram the rest of RX unit. */
441 desc_dma = (u64) gp->gblock_dvma;
442 desc_dma += (INIT_BLOCK_TX_RING_SIZE * sizeof(struct gem_txd));
443 writel(desc_dma >> 32, gp->regs + RXDMA_DBHI);
444 writel(desc_dma & 0xffffffff, gp->regs + RXDMA_DBLOW);
445 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
446 val = (RXDMA_CFG_BASE | (RX_OFFSET << 10) |
447 ((14 / 2) << 13) | RXDMA_CFG_FTHRESH_128);
448 writel(val, gp->regs + RXDMA_CFG);
449 if (readl(gp->regs + GREG_BIFCFG) & GREG_BIFCFG_M66EN)
450 writel(((5 & RXDMA_BLANK_IPKTS) |
451 ((8 << 12) & RXDMA_BLANK_ITIME)),
452 gp->regs + RXDMA_BLANK);
454 writel(((5 & RXDMA_BLANK_IPKTS) |
455 ((4 << 12) & RXDMA_BLANK_ITIME)),
456 gp->regs + RXDMA_BLANK);
457 val = (((gp->rx_pause_off / 64) << 0) & RXDMA_PTHRESH_OFF);
458 val |= (((gp->rx_pause_on / 64) << 12) & RXDMA_PTHRESH_ON);
459 writel(val, gp->regs + RXDMA_PTHRESH);
460 val = readl(gp->regs + RXDMA_CFG);
461 writel(val | RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
462 writel(MAC_RXSTAT_RCV, gp->regs + MAC_RXMASK);
463 val = readl(gp->regs + MAC_RXCFG);
464 writel(val | MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
469 static int gem_rxmac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
471 u32 rxmac_stat = readl(gp->regs + MAC_RXSTAT);
474 if (netif_msg_intr(gp))
475 printk(KERN_DEBUG "%s: rxmac interrupt, rxmac_stat: 0x%x\n",
476 gp->dev->name, rxmac_stat);
478 if (rxmac_stat & MAC_RXSTAT_OFLW) {
479 u32 smac = readl(gp->regs + MAC_SMACHINE);
481 printk(KERN_ERR "%s: RX MAC fifo overflow smac[%08x].\n",
483 gp->net_stats.rx_over_errors++;
484 gp->net_stats.rx_fifo_errors++;
486 ret = gem_rxmac_reset(gp);
489 if (rxmac_stat & MAC_RXSTAT_ACE)
490 gp->net_stats.rx_frame_errors += 0x10000;
492 if (rxmac_stat & MAC_RXSTAT_CCE)
493 gp->net_stats.rx_crc_errors += 0x10000;
495 if (rxmac_stat & MAC_RXSTAT_LCE)
496 gp->net_stats.rx_length_errors += 0x10000;
498 /* We do not track MAC_RXSTAT_FCE and MAC_RXSTAT_VCE
504 static int gem_mac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
506 u32 mac_cstat = readl(gp->regs + MAC_CSTAT);
508 if (netif_msg_intr(gp))
509 printk(KERN_DEBUG "%s: mac interrupt, mac_cstat: 0x%x\n",
510 gp->dev->name, mac_cstat);
512 /* This interrupt is just for pause frame and pause
513 * tracking. It is useful for diagnostics and debug
514 * but probably by default we will mask these events.
516 if (mac_cstat & MAC_CSTAT_PS)
519 if (mac_cstat & MAC_CSTAT_PRCV)
520 gp->pause_last_time_recvd = (mac_cstat >> 16);
525 static int gem_mif_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
527 u32 mif_status = readl(gp->regs + MIF_STATUS);
528 u32 reg_val, changed_bits;
530 reg_val = (mif_status & MIF_STATUS_DATA) >> 16;
531 changed_bits = (mif_status & MIF_STATUS_STAT);
533 gem_handle_mif_event(gp, reg_val, changed_bits);
538 static int gem_pci_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
540 u32 pci_estat = readl(gp->regs + GREG_PCIESTAT);
542 if (gp->pdev->vendor == PCI_VENDOR_ID_SUN &&
543 gp->pdev->device == PCI_DEVICE_ID_SUN_GEM) {
544 printk(KERN_ERR "%s: PCI error [%04x] ",
545 dev->name, pci_estat);
547 if (pci_estat & GREG_PCIESTAT_BADACK)
548 printk("<No ACK64# during ABS64 cycle> ");
549 if (pci_estat & GREG_PCIESTAT_DTRTO)
550 printk("<Delayed transaction timeout> ");
551 if (pci_estat & GREG_PCIESTAT_OTHER)
555 pci_estat |= GREG_PCIESTAT_OTHER;
556 printk(KERN_ERR "%s: PCI error\n", dev->name);
559 if (pci_estat & GREG_PCIESTAT_OTHER) {
562 /* Interrogate PCI config space for the
565 pci_read_config_word(gp->pdev, PCI_STATUS,
567 printk(KERN_ERR "%s: Read PCI cfg space status [%04x]\n",
568 dev->name, pci_cfg_stat);
569 if (pci_cfg_stat & PCI_STATUS_PARITY)
570 printk(KERN_ERR "%s: PCI parity error detected.\n",
572 if (pci_cfg_stat & PCI_STATUS_SIG_TARGET_ABORT)
573 printk(KERN_ERR "%s: PCI target abort.\n",
575 if (pci_cfg_stat & PCI_STATUS_REC_TARGET_ABORT)
576 printk(KERN_ERR "%s: PCI master acks target abort.\n",
578 if (pci_cfg_stat & PCI_STATUS_REC_MASTER_ABORT)
579 printk(KERN_ERR "%s: PCI master abort.\n",
581 if (pci_cfg_stat & PCI_STATUS_SIG_SYSTEM_ERROR)
582 printk(KERN_ERR "%s: PCI system error SERR#.\n",
584 if (pci_cfg_stat & PCI_STATUS_DETECTED_PARITY)
585 printk(KERN_ERR "%s: PCI parity error.\n",
588 /* Write the error bits back to clear them. */
589 pci_cfg_stat &= (PCI_STATUS_PARITY |
590 PCI_STATUS_SIG_TARGET_ABORT |
591 PCI_STATUS_REC_TARGET_ABORT |
592 PCI_STATUS_REC_MASTER_ABORT |
593 PCI_STATUS_SIG_SYSTEM_ERROR |
594 PCI_STATUS_DETECTED_PARITY);
595 pci_write_config_word(gp->pdev,
596 PCI_STATUS, pci_cfg_stat);
599 /* For all PCI errors, we should reset the chip. */
603 /* All non-normal interrupt conditions get serviced here.
604 * Returns non-zero if we should just exit the interrupt
605 * handler right now (ie. if we reset the card which invalidates
606 * all of the other original irq status bits).
608 static int gem_abnormal_irq(struct net_device *dev, struct gem *gp, u32 gem_status)
610 if (gem_status & GREG_STAT_RXNOBUF) {
611 /* Frame arrived, no free RX buffers available. */
612 if (netif_msg_rx_err(gp))
613 printk(KERN_DEBUG "%s: no buffer for rx frame\n",
615 gp->net_stats.rx_dropped++;
618 if (gem_status & GREG_STAT_RXTAGERR) {
619 /* corrupt RX tag framing */
620 if (netif_msg_rx_err(gp))
621 printk(KERN_DEBUG "%s: corrupt rx tag framing\n",
623 gp->net_stats.rx_errors++;
628 if (gem_status & GREG_STAT_PCS) {
629 if (gem_pcs_interrupt(dev, gp, gem_status))
633 if (gem_status & GREG_STAT_TXMAC) {
634 if (gem_txmac_interrupt(dev, gp, gem_status))
638 if (gem_status & GREG_STAT_RXMAC) {
639 if (gem_rxmac_interrupt(dev, gp, gem_status))
643 if (gem_status & GREG_STAT_MAC) {
644 if (gem_mac_interrupt(dev, gp, gem_status))
648 if (gem_status & GREG_STAT_MIF) {
649 if (gem_mif_interrupt(dev, gp, gem_status))
653 if (gem_status & GREG_STAT_PCIERR) {
654 if (gem_pci_interrupt(dev, gp, gem_status))
661 gp->reset_task_pending = 1;
662 schedule_work(&gp->reset_task);
667 static __inline__ void gem_tx(struct net_device *dev, struct gem *gp, u32 gem_status)
671 if (netif_msg_intr(gp))
672 printk(KERN_DEBUG "%s: tx interrupt, gem_status: 0x%x\n",
673 gp->dev->name, gem_status);
676 limit = ((gem_status & GREG_STAT_TXNR) >> GREG_STAT_TXNR_SHIFT);
677 while (entry != limit) {
684 if (netif_msg_tx_done(gp))
685 printk(KERN_DEBUG "%s: tx done, slot %d\n",
686 gp->dev->name, entry);
687 skb = gp->tx_skbs[entry];
688 if (skb_shinfo(skb)->nr_frags) {
689 int last = entry + skb_shinfo(skb)->nr_frags;
693 last &= (TX_RING_SIZE - 1);
695 walk = NEXT_TX(walk);
704 gp->tx_skbs[entry] = NULL;
705 gp->net_stats.tx_bytes += skb->len;
707 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
708 txd = &gp->init_block->txd[entry];
710 dma_addr = le64_to_cpu(txd->buffer);
711 dma_len = le64_to_cpu(txd->control_word) & TXDCTRL_BUFSZ;
713 pci_unmap_page(gp->pdev, dma_addr, dma_len, PCI_DMA_TODEVICE);
714 entry = NEXT_TX(entry);
717 gp->net_stats.tx_packets++;
718 dev_kfree_skb_irq(skb);
722 if (netif_queue_stopped(dev) &&
723 TX_BUFFS_AVAIL(gp) > (MAX_SKB_FRAGS + 1))
724 netif_wake_queue(dev);
727 static __inline__ void gem_post_rxds(struct gem *gp, int limit)
729 int cluster_start, curr, count, kick;
731 cluster_start = curr = (gp->rx_new & ~(4 - 1));
735 while (curr != limit) {
736 curr = NEXT_RX(curr);
738 struct gem_rxd *rxd =
739 &gp->init_block->rxd[cluster_start];
741 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
743 cluster_start = NEXT_RX(cluster_start);
744 if (cluster_start == curr)
753 writel(kick, gp->regs + RXDMA_KICK);
757 static int gem_rx(struct gem *gp, int work_to_do)
759 int entry, drops, work_done = 0;
763 if (netif_msg_rx_status(gp))
764 printk(KERN_DEBUG "%s: rx interrupt, done: %d, rx_new: %d\n",
765 gp->dev->name, readl(gp->regs + RXDMA_DONE), gp->rx_new);
769 done = readl(gp->regs + RXDMA_DONE);
771 struct gem_rxd *rxd = &gp->init_block->rxd[entry];
773 u64 status = le64_to_cpu(rxd->status_word);
777 if ((status & RXDCTRL_OWN) != 0)
780 if (work_done >= RX_RING_SIZE || work_done >= work_to_do)
783 /* When writing back RX descriptor, GEM writes status
784 * then buffer address, possibly in seperate transactions.
785 * If we don't wait for the chip to write both, we could
786 * post a new buffer to this descriptor then have GEM spam
787 * on the buffer address. We sync on the RX completion
788 * register to prevent this from happening.
791 done = readl(gp->regs + RXDMA_DONE);
796 /* We can now account for the work we're about to do */
799 skb = gp->rx_skbs[entry];
801 len = (status & RXDCTRL_BUFSZ) >> 16;
802 if ((len < ETH_ZLEN) || (status & RXDCTRL_BAD)) {
803 gp->net_stats.rx_errors++;
805 gp->net_stats.rx_length_errors++;
806 if (len & RXDCTRL_BAD)
807 gp->net_stats.rx_crc_errors++;
809 /* We'll just return it to GEM. */
811 gp->net_stats.rx_dropped++;
815 dma_addr = le64_to_cpu(rxd->buffer);
816 if (len > RX_COPY_THRESHOLD) {
817 struct sk_buff *new_skb;
819 new_skb = gem_alloc_skb(RX_BUF_ALLOC_SIZE(gp), GFP_ATOMIC);
820 if (new_skb == NULL) {
824 pci_unmap_page(gp->pdev, dma_addr,
825 RX_BUF_ALLOC_SIZE(gp),
827 gp->rx_skbs[entry] = new_skb;
828 new_skb->dev = gp->dev;
829 skb_put(new_skb, (gp->rx_buf_sz + RX_OFFSET));
830 rxd->buffer = cpu_to_le64(pci_map_page(gp->pdev,
831 virt_to_page(new_skb->data),
832 offset_in_page(new_skb->data),
833 RX_BUF_ALLOC_SIZE(gp),
834 PCI_DMA_FROMDEVICE));
835 skb_reserve(new_skb, RX_OFFSET);
837 /* Trim the original skb for the netif. */
840 struct sk_buff *copy_skb = dev_alloc_skb(len + 2);
842 if (copy_skb == NULL) {
847 skb_reserve(copy_skb, 2);
848 skb_put(copy_skb, len);
849 pci_dma_sync_single_for_cpu(gp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
850 skb_copy_from_linear_data(skb, copy_skb->data, len);
851 pci_dma_sync_single_for_device(gp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
853 /* We'll reuse the original ring buffer. */
857 csum = (__force __sum16)htons((status & RXDCTRL_TCPCSUM) ^ 0xffff);
858 skb->csum = csum_unfold(csum);
859 skb->ip_summed = CHECKSUM_COMPLETE;
860 skb->protocol = eth_type_trans(skb, gp->dev);
862 netif_receive_skb(skb);
864 gp->net_stats.rx_packets++;
865 gp->net_stats.rx_bytes += len;
868 entry = NEXT_RX(entry);
871 gem_post_rxds(gp, entry);
876 printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n",
882 static int gem_poll(struct napi_struct *napi, int budget)
884 struct gem *gp = container_of(napi, struct gem, napi);
885 struct net_device *dev = gp->dev;
890 * NAPI locking nightmare: See comment at head of driver
892 spin_lock_irqsave(&gp->lock, flags);
896 /* Handle anomalies */
897 if (gp->status & GREG_STAT_ABNORMAL) {
898 if (gem_abnormal_irq(dev, gp, gp->status))
902 /* Run TX completion thread */
903 spin_lock(&gp->tx_lock);
904 gem_tx(dev, gp, gp->status);
905 spin_unlock(&gp->tx_lock);
907 spin_unlock_irqrestore(&gp->lock, flags);
909 /* Run RX thread. We don't use any locking here,
910 * code willing to do bad things - like cleaning the
911 * rx ring - must call napi_disable(), which
912 * schedule_timeout()'s if polling is already disabled.
914 work_done += gem_rx(gp, budget - work_done);
916 if (work_done >= budget)
919 spin_lock_irqsave(&gp->lock, flags);
921 gp->status = readl(gp->regs + GREG_STAT);
922 } while (gp->status & GREG_STAT_NAPI);
924 __netif_rx_complete(dev, napi);
927 spin_unlock_irqrestore(&gp->lock, flags);
932 static irqreturn_t gem_interrupt(int irq, void *dev_id)
934 struct net_device *dev = dev_id;
935 struct gem *gp = netdev_priv(dev);
938 /* Swallow interrupts when shutting the chip down, though
939 * that shouldn't happen, we should have done free_irq() at
945 spin_lock_irqsave(&gp->lock, flags);
947 if (netif_rx_schedule_prep(dev, &gp->napi)) {
948 u32 gem_status = readl(gp->regs + GREG_STAT);
950 if (gem_status == 0) {
951 napi_enable(&gp->napi);
952 spin_unlock_irqrestore(&gp->lock, flags);
955 gp->status = gem_status;
956 gem_disable_ints(gp);
957 __netif_rx_schedule(dev, &gp->napi);
960 spin_unlock_irqrestore(&gp->lock, flags);
962 /* If polling was disabled at the time we received that
963 * interrupt, we may return IRQ_HANDLED here while we
964 * should return IRQ_NONE. No big deal...
969 #ifdef CONFIG_NET_POLL_CONTROLLER
970 static void gem_poll_controller(struct net_device *dev)
972 /* gem_interrupt is safe to reentrance so no need
973 * to disable_irq here.
975 gem_interrupt(dev->irq, dev);
979 static void gem_tx_timeout(struct net_device *dev)
981 struct gem *gp = netdev_priv(dev);
983 printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
985 printk("%s: hrm.. hw not running !\n", dev->name);
988 printk(KERN_ERR "%s: TX_STATE[%08x:%08x:%08x]\n",
990 readl(gp->regs + TXDMA_CFG),
991 readl(gp->regs + MAC_TXSTAT),
992 readl(gp->regs + MAC_TXCFG));
993 printk(KERN_ERR "%s: RX_STATE[%08x:%08x:%08x]\n",
995 readl(gp->regs + RXDMA_CFG),
996 readl(gp->regs + MAC_RXSTAT),
997 readl(gp->regs + MAC_RXCFG));
999 spin_lock_irq(&gp->lock);
1000 spin_lock(&gp->tx_lock);
1002 gp->reset_task_pending = 1;
1003 schedule_work(&gp->reset_task);
1005 spin_unlock(&gp->tx_lock);
1006 spin_unlock_irq(&gp->lock);
1009 static __inline__ int gem_intme(int entry)
1011 /* Algorithm: IRQ every 1/2 of descriptors. */
1012 if (!(entry & ((TX_RING_SIZE>>1)-1)))
1018 static int gem_start_xmit(struct sk_buff *skb, struct net_device *dev)
1020 struct gem *gp = netdev_priv(dev);
1023 unsigned long flags;
1026 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1027 const u64 csum_start_off = skb_transport_offset(skb);
1028 const u64 csum_stuff_off = csum_start_off + skb->csum_offset;
1030 ctrl = (TXDCTRL_CENAB |
1031 (csum_start_off << 15) |
1032 (csum_stuff_off << 21));
1035 local_irq_save(flags);
1036 if (!spin_trylock(&gp->tx_lock)) {
1037 /* Tell upper layer to requeue */
1038 local_irq_restore(flags);
1039 return NETDEV_TX_LOCKED;
1041 /* We raced with gem_do_stop() */
1043 spin_unlock_irqrestore(&gp->tx_lock, flags);
1044 return NETDEV_TX_BUSY;
1047 /* This is a hard error, log it. */
1048 if (TX_BUFFS_AVAIL(gp) <= (skb_shinfo(skb)->nr_frags + 1)) {
1049 netif_stop_queue(dev);
1050 spin_unlock_irqrestore(&gp->tx_lock, flags);
1051 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when queue awake!\n",
1053 return NETDEV_TX_BUSY;
1057 gp->tx_skbs[entry] = skb;
1059 if (skb_shinfo(skb)->nr_frags == 0) {
1060 struct gem_txd *txd = &gp->init_block->txd[entry];
1065 mapping = pci_map_page(gp->pdev,
1066 virt_to_page(skb->data),
1067 offset_in_page(skb->data),
1068 len, PCI_DMA_TODEVICE);
1069 ctrl |= TXDCTRL_SOF | TXDCTRL_EOF | len;
1070 if (gem_intme(entry))
1071 ctrl |= TXDCTRL_INTME;
1072 txd->buffer = cpu_to_le64(mapping);
1074 txd->control_word = cpu_to_le64(ctrl);
1075 entry = NEXT_TX(entry);
1077 struct gem_txd *txd;
1080 dma_addr_t first_mapping;
1081 int frag, first_entry = entry;
1084 if (gem_intme(entry))
1085 intme |= TXDCTRL_INTME;
1087 /* We must give this initial chunk to the device last.
1088 * Otherwise we could race with the device.
1090 first_len = skb_headlen(skb);
1091 first_mapping = pci_map_page(gp->pdev, virt_to_page(skb->data),
1092 offset_in_page(skb->data),
1093 first_len, PCI_DMA_TODEVICE);
1094 entry = NEXT_TX(entry);
1096 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
1097 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
1102 len = this_frag->size;
1103 mapping = pci_map_page(gp->pdev,
1105 this_frag->page_offset,
1106 len, PCI_DMA_TODEVICE);
1108 if (frag == skb_shinfo(skb)->nr_frags - 1)
1109 this_ctrl |= TXDCTRL_EOF;
1111 txd = &gp->init_block->txd[entry];
1112 txd->buffer = cpu_to_le64(mapping);
1114 txd->control_word = cpu_to_le64(this_ctrl | len);
1116 if (gem_intme(entry))
1117 intme |= TXDCTRL_INTME;
1119 entry = NEXT_TX(entry);
1121 txd = &gp->init_block->txd[first_entry];
1122 txd->buffer = cpu_to_le64(first_mapping);
1125 cpu_to_le64(ctrl | TXDCTRL_SOF | intme | first_len);
1129 if (TX_BUFFS_AVAIL(gp) <= (MAX_SKB_FRAGS + 1))
1130 netif_stop_queue(dev);
1132 if (netif_msg_tx_queued(gp))
1133 printk(KERN_DEBUG "%s: tx queued, slot %d, skblen %d\n",
1134 dev->name, entry, skb->len);
1136 writel(gp->tx_new, gp->regs + TXDMA_KICK);
1137 spin_unlock_irqrestore(&gp->tx_lock, flags);
1139 dev->trans_start = jiffies;
1141 return NETDEV_TX_OK;
1144 #define STOP_TRIES 32
1146 /* Must be invoked under gp->lock and gp->tx_lock. */
1147 static void gem_reset(struct gem *gp)
1152 /* Make sure we won't get any more interrupts */
1153 writel(0xffffffff, gp->regs + GREG_IMASK);
1155 /* Reset the chip */
1156 writel(gp->swrst_base | GREG_SWRST_TXRST | GREG_SWRST_RXRST,
1157 gp->regs + GREG_SWRST);
1163 val = readl(gp->regs + GREG_SWRST);
1166 } while (val & (GREG_SWRST_TXRST | GREG_SWRST_RXRST));
1169 printk(KERN_ERR "%s: SW reset is ghetto.\n", gp->dev->name);
1172 /* Must be invoked under gp->lock and gp->tx_lock. */
1173 static void gem_start_dma(struct gem *gp)
1177 /* We are ready to rock, turn everything on. */
1178 val = readl(gp->regs + TXDMA_CFG);
1179 writel(val | TXDMA_CFG_ENABLE, gp->regs + TXDMA_CFG);
1180 val = readl(gp->regs + RXDMA_CFG);
1181 writel(val | RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
1182 val = readl(gp->regs + MAC_TXCFG);
1183 writel(val | MAC_TXCFG_ENAB, gp->regs + MAC_TXCFG);
1184 val = readl(gp->regs + MAC_RXCFG);
1185 writel(val | MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
1187 (void) readl(gp->regs + MAC_RXCFG);
1190 gem_enable_ints(gp);
1192 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
1195 /* Must be invoked under gp->lock and gp->tx_lock. DMA won't be
1196 * actually stopped before about 4ms tho ...
1198 static void gem_stop_dma(struct gem *gp)
1202 /* We are done rocking, turn everything off. */
1203 val = readl(gp->regs + TXDMA_CFG);
1204 writel(val & ~TXDMA_CFG_ENABLE, gp->regs + TXDMA_CFG);
1205 val = readl(gp->regs + RXDMA_CFG);
1206 writel(val & ~RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
1207 val = readl(gp->regs + MAC_TXCFG);
1208 writel(val & ~MAC_TXCFG_ENAB, gp->regs + MAC_TXCFG);
1209 val = readl(gp->regs + MAC_RXCFG);
1210 writel(val & ~MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
1212 (void) readl(gp->regs + MAC_RXCFG);
1214 /* Need to wait a bit ... done by the caller */
1218 /* Must be invoked under gp->lock and gp->tx_lock. */
1219 // XXX dbl check what that function should do when called on PCS PHY
1220 static void gem_begin_auto_negotiation(struct gem *gp, struct ethtool_cmd *ep)
1222 u32 advertise, features;
1227 if (gp->phy_type != phy_mii_mdio0 &&
1228 gp->phy_type != phy_mii_mdio1)
1231 /* Setup advertise */
1232 if (found_mii_phy(gp))
1233 features = gp->phy_mii.def->features;
1237 advertise = features & ADVERTISE_MASK;
1238 if (gp->phy_mii.advertising != 0)
1239 advertise &= gp->phy_mii.advertising;
1241 autoneg = gp->want_autoneg;
1242 speed = gp->phy_mii.speed;
1243 duplex = gp->phy_mii.duplex;
1245 /* Setup link parameters */
1248 if (ep->autoneg == AUTONEG_ENABLE) {
1249 advertise = ep->advertising;
1254 duplex = ep->duplex;
1258 /* Sanitize settings based on PHY capabilities */
1259 if ((features & SUPPORTED_Autoneg) == 0)
1261 if (speed == SPEED_1000 &&
1262 !(features & (SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full)))
1264 if (speed == SPEED_100 &&
1265 !(features & (SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full)))
1267 if (duplex == DUPLEX_FULL &&
1268 !(features & (SUPPORTED_1000baseT_Full |
1269 SUPPORTED_100baseT_Full |
1270 SUPPORTED_10baseT_Full)))
1271 duplex = DUPLEX_HALF;
1275 /* If we are asleep, we don't try to actually setup the PHY, we
1276 * just store the settings
1279 gp->phy_mii.autoneg = gp->want_autoneg = autoneg;
1280 gp->phy_mii.speed = speed;
1281 gp->phy_mii.duplex = duplex;
1285 /* Configure PHY & start aneg */
1286 gp->want_autoneg = autoneg;
1288 if (found_mii_phy(gp))
1289 gp->phy_mii.def->ops->setup_aneg(&gp->phy_mii, advertise);
1290 gp->lstate = link_aneg;
1292 if (found_mii_phy(gp))
1293 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, speed, duplex);
1294 gp->lstate = link_force_ok;
1298 gp->timer_ticks = 0;
1299 mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
1302 /* A link-up condition has occurred, initialize and enable the
1305 * Must be invoked under gp->lock and gp->tx_lock.
1307 static int gem_set_link_modes(struct gem *gp)
1310 int full_duplex, speed, pause;
1316 if (found_mii_phy(gp)) {
1317 if (gp->phy_mii.def->ops->read_link(&gp->phy_mii))
1319 full_duplex = (gp->phy_mii.duplex == DUPLEX_FULL);
1320 speed = gp->phy_mii.speed;
1321 pause = gp->phy_mii.pause;
1322 } else if (gp->phy_type == phy_serialink ||
1323 gp->phy_type == phy_serdes) {
1324 u32 pcs_lpa = readl(gp->regs + PCS_MIILP);
1326 if (pcs_lpa & PCS_MIIADV_FD)
1331 if (netif_msg_link(gp))
1332 printk(KERN_INFO "%s: Link is up at %d Mbps, %s-duplex.\n",
1333 gp->dev->name, speed, (full_duplex ? "full" : "half"));
1338 val = (MAC_TXCFG_EIPG0 | MAC_TXCFG_NGU);
1340 val |= (MAC_TXCFG_ICS | MAC_TXCFG_ICOLL);
1342 /* MAC_TXCFG_NBO must be zero. */
1344 writel(val, gp->regs + MAC_TXCFG);
1346 val = (MAC_XIFCFG_OE | MAC_XIFCFG_LLED);
1348 (gp->phy_type == phy_mii_mdio0 ||
1349 gp->phy_type == phy_mii_mdio1)) {
1350 val |= MAC_XIFCFG_DISE;
1351 } else if (full_duplex) {
1352 val |= MAC_XIFCFG_FLED;
1355 if (speed == SPEED_1000)
1356 val |= (MAC_XIFCFG_GMII);
1358 writel(val, gp->regs + MAC_XIFCFG);
1360 /* If gigabit and half-duplex, enable carrier extension
1361 * mode. Else, disable it.
1363 if (speed == SPEED_1000 && !full_duplex) {
1364 val = readl(gp->regs + MAC_TXCFG);
1365 writel(val | MAC_TXCFG_TCE, gp->regs + MAC_TXCFG);
1367 val = readl(gp->regs + MAC_RXCFG);
1368 writel(val | MAC_RXCFG_RCE, gp->regs + MAC_RXCFG);
1370 val = readl(gp->regs + MAC_TXCFG);
1371 writel(val & ~MAC_TXCFG_TCE, gp->regs + MAC_TXCFG);
1373 val = readl(gp->regs + MAC_RXCFG);
1374 writel(val & ~MAC_RXCFG_RCE, gp->regs + MAC_RXCFG);
1377 if (gp->phy_type == phy_serialink ||
1378 gp->phy_type == phy_serdes) {
1379 u32 pcs_lpa = readl(gp->regs + PCS_MIILP);
1381 if (pcs_lpa & (PCS_MIIADV_SP | PCS_MIIADV_AP))
1385 if (netif_msg_link(gp)) {
1387 printk(KERN_INFO "%s: Pause is enabled "
1388 "(rxfifo: %d off: %d on: %d)\n",
1394 printk(KERN_INFO "%s: Pause is disabled\n",
1400 writel(512, gp->regs + MAC_STIME);
1402 writel(64, gp->regs + MAC_STIME);
1403 val = readl(gp->regs + MAC_MCCFG);
1405 val |= (MAC_MCCFG_SPE | MAC_MCCFG_RPE);
1407 val &= ~(MAC_MCCFG_SPE | MAC_MCCFG_RPE);
1408 writel(val, gp->regs + MAC_MCCFG);
1415 /* Must be invoked under gp->lock and gp->tx_lock. */
1416 static int gem_mdio_link_not_up(struct gem *gp)
1418 switch (gp->lstate) {
1419 case link_force_ret:
1420 if (netif_msg_link(gp))
1421 printk(KERN_INFO "%s: Autoneg failed again, keeping"
1422 " forced mode\n", gp->dev->name);
1423 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii,
1424 gp->last_forced_speed, DUPLEX_HALF);
1425 gp->timer_ticks = 5;
1426 gp->lstate = link_force_ok;
1429 /* We try forced modes after a failed aneg only on PHYs that don't
1430 * have "magic_aneg" bit set, which means they internally do the
1431 * while forced-mode thingy. On these, we just restart aneg
1433 if (gp->phy_mii.def->magic_aneg)
1435 if (netif_msg_link(gp))
1436 printk(KERN_INFO "%s: switching to forced 100bt\n",
1438 /* Try forced modes. */
1439 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, SPEED_100,
1441 gp->timer_ticks = 5;
1442 gp->lstate = link_force_try;
1444 case link_force_try:
1445 /* Downgrade from 100 to 10 Mbps if necessary.
1446 * If already at 10Mbps, warn user about the
1447 * situation every 10 ticks.
1449 if (gp->phy_mii.speed == SPEED_100) {
1450 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, SPEED_10,
1452 gp->timer_ticks = 5;
1453 if (netif_msg_link(gp))
1454 printk(KERN_INFO "%s: switching to forced 10bt\n",
1464 static void gem_link_timer(unsigned long data)
1466 struct gem *gp = (struct gem *) data;
1467 int restart_aneg = 0;
1472 spin_lock_irq(&gp->lock);
1473 spin_lock(&gp->tx_lock);
1476 /* If the reset task is still pending, we just
1477 * reschedule the link timer
1479 if (gp->reset_task_pending)
1482 if (gp->phy_type == phy_serialink ||
1483 gp->phy_type == phy_serdes) {
1484 u32 val = readl(gp->regs + PCS_MIISTAT);
1486 if (!(val & PCS_MIISTAT_LS))
1487 val = readl(gp->regs + PCS_MIISTAT);
1489 if ((val & PCS_MIISTAT_LS) != 0) {
1490 gp->lstate = link_up;
1491 netif_carrier_on(gp->dev);
1492 (void)gem_set_link_modes(gp);
1496 if (found_mii_phy(gp) && gp->phy_mii.def->ops->poll_link(&gp->phy_mii)) {
1497 /* Ok, here we got a link. If we had it due to a forced
1498 * fallback, and we were configured for autoneg, we do
1499 * retry a short autoneg pass. If you know your hub is
1500 * broken, use ethtool ;)
1502 if (gp->lstate == link_force_try && gp->want_autoneg) {
1503 gp->lstate = link_force_ret;
1504 gp->last_forced_speed = gp->phy_mii.speed;
1505 gp->timer_ticks = 5;
1506 if (netif_msg_link(gp))
1507 printk(KERN_INFO "%s: Got link after fallback, retrying"
1508 " autoneg once...\n", gp->dev->name);
1509 gp->phy_mii.def->ops->setup_aneg(&gp->phy_mii, gp->phy_mii.advertising);
1510 } else if (gp->lstate != link_up) {
1511 gp->lstate = link_up;
1512 netif_carrier_on(gp->dev);
1513 if (gem_set_link_modes(gp))
1517 /* If the link was previously up, we restart the
1520 if (gp->lstate == link_up) {
1521 gp->lstate = link_down;
1522 if (netif_msg_link(gp))
1523 printk(KERN_INFO "%s: Link down\n",
1525 netif_carrier_off(gp->dev);
1526 gp->reset_task_pending = 1;
1527 schedule_work(&gp->reset_task);
1529 } else if (++gp->timer_ticks > 10) {
1530 if (found_mii_phy(gp))
1531 restart_aneg = gem_mdio_link_not_up(gp);
1537 gem_begin_auto_negotiation(gp, NULL);
1541 mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
1544 spin_unlock(&gp->tx_lock);
1545 spin_unlock_irq(&gp->lock);
1548 /* Must be invoked under gp->lock and gp->tx_lock. */
1549 static void gem_clean_rings(struct gem *gp)
1551 struct gem_init_block *gb = gp->init_block;
1552 struct sk_buff *skb;
1554 dma_addr_t dma_addr;
1556 for (i = 0; i < RX_RING_SIZE; i++) {
1557 struct gem_rxd *rxd;
1560 if (gp->rx_skbs[i] != NULL) {
1561 skb = gp->rx_skbs[i];
1562 dma_addr = le64_to_cpu(rxd->buffer);
1563 pci_unmap_page(gp->pdev, dma_addr,
1564 RX_BUF_ALLOC_SIZE(gp),
1565 PCI_DMA_FROMDEVICE);
1566 dev_kfree_skb_any(skb);
1567 gp->rx_skbs[i] = NULL;
1569 rxd->status_word = 0;
1574 for (i = 0; i < TX_RING_SIZE; i++) {
1575 if (gp->tx_skbs[i] != NULL) {
1576 struct gem_txd *txd;
1579 skb = gp->tx_skbs[i];
1580 gp->tx_skbs[i] = NULL;
1582 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1583 int ent = i & (TX_RING_SIZE - 1);
1585 txd = &gb->txd[ent];
1586 dma_addr = le64_to_cpu(txd->buffer);
1587 pci_unmap_page(gp->pdev, dma_addr,
1588 le64_to_cpu(txd->control_word) &
1589 TXDCTRL_BUFSZ, PCI_DMA_TODEVICE);
1591 if (frag != skb_shinfo(skb)->nr_frags)
1594 dev_kfree_skb_any(skb);
1599 /* Must be invoked under gp->lock and gp->tx_lock. */
1600 static void gem_init_rings(struct gem *gp)
1602 struct gem_init_block *gb = gp->init_block;
1603 struct net_device *dev = gp->dev;
1605 dma_addr_t dma_addr;
1607 gp->rx_new = gp->rx_old = gp->tx_new = gp->tx_old = 0;
1609 gem_clean_rings(gp);
1611 gp->rx_buf_sz = max(dev->mtu + ETH_HLEN + VLAN_HLEN,
1612 (unsigned)VLAN_ETH_FRAME_LEN);
1614 for (i = 0; i < RX_RING_SIZE; i++) {
1615 struct sk_buff *skb;
1616 struct gem_rxd *rxd = &gb->rxd[i];
1618 skb = gem_alloc_skb(RX_BUF_ALLOC_SIZE(gp), GFP_ATOMIC);
1621 rxd->status_word = 0;
1625 gp->rx_skbs[i] = skb;
1627 skb_put(skb, (gp->rx_buf_sz + RX_OFFSET));
1628 dma_addr = pci_map_page(gp->pdev,
1629 virt_to_page(skb->data),
1630 offset_in_page(skb->data),
1631 RX_BUF_ALLOC_SIZE(gp),
1632 PCI_DMA_FROMDEVICE);
1633 rxd->buffer = cpu_to_le64(dma_addr);
1635 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
1636 skb_reserve(skb, RX_OFFSET);
1639 for (i = 0; i < TX_RING_SIZE; i++) {
1640 struct gem_txd *txd = &gb->txd[i];
1642 txd->control_word = 0;
1649 /* Init PHY interface and start link poll state machine */
1650 static void gem_init_phy(struct gem *gp)
1654 /* Revert MIF CFG setting done on stop_phy */
1655 mifcfg = readl(gp->regs + MIF_CFG);
1656 mifcfg &= ~MIF_CFG_BBMODE;
1657 writel(mifcfg, gp->regs + MIF_CFG);
1659 if (gp->pdev->vendor == PCI_VENDOR_ID_APPLE) {
1662 /* Those delay sucks, the HW seem to love them though, I'll
1663 * serisouly consider breaking some locks here to be able
1664 * to schedule instead
1666 for (i = 0; i < 3; i++) {
1667 #ifdef CONFIG_PPC_PMAC
1668 pmac_call_feature(PMAC_FTR_GMAC_PHY_RESET, gp->of_node, 0, 0);
1671 /* Some PHYs used by apple have problem getting back to us,
1672 * we do an additional reset here
1674 phy_write(gp, MII_BMCR, BMCR_RESET);
1676 if (phy_read(gp, MII_BMCR) != 0xffff)
1679 printk(KERN_WARNING "%s: GMAC PHY not responding !\n",
1684 if (gp->pdev->vendor == PCI_VENDOR_ID_SUN &&
1685 gp->pdev->device == PCI_DEVICE_ID_SUN_GEM) {
1688 /* Init datapath mode register. */
1689 if (gp->phy_type == phy_mii_mdio0 ||
1690 gp->phy_type == phy_mii_mdio1) {
1691 val = PCS_DMODE_MGM;
1692 } else if (gp->phy_type == phy_serialink) {
1693 val = PCS_DMODE_SM | PCS_DMODE_GMOE;
1695 val = PCS_DMODE_ESM;
1698 writel(val, gp->regs + PCS_DMODE);
1701 if (gp->phy_type == phy_mii_mdio0 ||
1702 gp->phy_type == phy_mii_mdio1) {
1703 // XXX check for errors
1704 mii_phy_probe(&gp->phy_mii, gp->mii_phy_addr);
1707 if (gp->phy_mii.def && gp->phy_mii.def->ops->init)
1708 gp->phy_mii.def->ops->init(&gp->phy_mii);
1713 /* Reset PCS unit. */
1714 val = readl(gp->regs + PCS_MIICTRL);
1715 val |= PCS_MIICTRL_RST;
1716 writeb(val, gp->regs + PCS_MIICTRL);
1719 while (readl(gp->regs + PCS_MIICTRL) & PCS_MIICTRL_RST) {
1725 printk(KERN_WARNING "%s: PCS reset bit would not clear.\n",
1728 /* Make sure PCS is disabled while changing advertisement
1731 val = readl(gp->regs + PCS_CFG);
1732 val &= ~(PCS_CFG_ENABLE | PCS_CFG_TO);
1733 writel(val, gp->regs + PCS_CFG);
1735 /* Advertise all capabilities except assymetric
1738 val = readl(gp->regs + PCS_MIIADV);
1739 val |= (PCS_MIIADV_FD | PCS_MIIADV_HD |
1740 PCS_MIIADV_SP | PCS_MIIADV_AP);
1741 writel(val, gp->regs + PCS_MIIADV);
1743 /* Enable and restart auto-negotiation, disable wrapback/loopback,
1744 * and re-enable PCS.
1746 val = readl(gp->regs + PCS_MIICTRL);
1747 val |= (PCS_MIICTRL_RAN | PCS_MIICTRL_ANE);
1748 val &= ~PCS_MIICTRL_WB;
1749 writel(val, gp->regs + PCS_MIICTRL);
1751 val = readl(gp->regs + PCS_CFG);
1752 val |= PCS_CFG_ENABLE;
1753 writel(val, gp->regs + PCS_CFG);
1755 /* Make sure serialink loopback is off. The meaning
1756 * of this bit is logically inverted based upon whether
1757 * you are in Serialink or SERDES mode.
1759 val = readl(gp->regs + PCS_SCTRL);
1760 if (gp->phy_type == phy_serialink)
1761 val &= ~PCS_SCTRL_LOOP;
1763 val |= PCS_SCTRL_LOOP;
1764 writel(val, gp->regs + PCS_SCTRL);
1767 /* Default aneg parameters */
1768 gp->timer_ticks = 0;
1769 gp->lstate = link_down;
1770 netif_carrier_off(gp->dev);
1772 /* Can I advertise gigabit here ? I'd need BCM PHY docs... */
1773 spin_lock_irq(&gp->lock);
1774 gem_begin_auto_negotiation(gp, NULL);
1775 spin_unlock_irq(&gp->lock);
1778 /* Must be invoked under gp->lock and gp->tx_lock. */
1779 static void gem_init_dma(struct gem *gp)
1781 u64 desc_dma = (u64) gp->gblock_dvma;
1784 val = (TXDMA_CFG_BASE | (0x7ff << 10) | TXDMA_CFG_PMODE);
1785 writel(val, gp->regs + TXDMA_CFG);
1787 writel(desc_dma >> 32, gp->regs + TXDMA_DBHI);
1788 writel(desc_dma & 0xffffffff, gp->regs + TXDMA_DBLOW);
1789 desc_dma += (INIT_BLOCK_TX_RING_SIZE * sizeof(struct gem_txd));
1791 writel(0, gp->regs + TXDMA_KICK);
1793 val = (RXDMA_CFG_BASE | (RX_OFFSET << 10) |
1794 ((14 / 2) << 13) | RXDMA_CFG_FTHRESH_128);
1795 writel(val, gp->regs + RXDMA_CFG);
1797 writel(desc_dma >> 32, gp->regs + RXDMA_DBHI);
1798 writel(desc_dma & 0xffffffff, gp->regs + RXDMA_DBLOW);
1800 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
1802 val = (((gp->rx_pause_off / 64) << 0) & RXDMA_PTHRESH_OFF);
1803 val |= (((gp->rx_pause_on / 64) << 12) & RXDMA_PTHRESH_ON);
1804 writel(val, gp->regs + RXDMA_PTHRESH);
1806 if (readl(gp->regs + GREG_BIFCFG) & GREG_BIFCFG_M66EN)
1807 writel(((5 & RXDMA_BLANK_IPKTS) |
1808 ((8 << 12) & RXDMA_BLANK_ITIME)),
1809 gp->regs + RXDMA_BLANK);
1811 writel(((5 & RXDMA_BLANK_IPKTS) |
1812 ((4 << 12) & RXDMA_BLANK_ITIME)),
1813 gp->regs + RXDMA_BLANK);
1816 /* Must be invoked under gp->lock and gp->tx_lock. */
1817 static u32 gem_setup_multicast(struct gem *gp)
1822 if ((gp->dev->flags & IFF_ALLMULTI) ||
1823 (gp->dev->mc_count > 256)) {
1824 for (i=0; i<16; i++)
1825 writel(0xffff, gp->regs + MAC_HASH0 + (i << 2));
1826 rxcfg |= MAC_RXCFG_HFE;
1827 } else if (gp->dev->flags & IFF_PROMISC) {
1828 rxcfg |= MAC_RXCFG_PROM;
1832 struct dev_mc_list *dmi = gp->dev->mc_list;
1835 for (i = 0; i < 16; i++)
1838 for (i = 0; i < gp->dev->mc_count; i++) {
1839 char *addrs = dmi->dmi_addr;
1846 crc = ether_crc_le(6, addrs);
1848 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
1850 for (i=0; i<16; i++)
1851 writel(hash_table[i], gp->regs + MAC_HASH0 + (i << 2));
1852 rxcfg |= MAC_RXCFG_HFE;
1858 /* Must be invoked under gp->lock and gp->tx_lock. */
1859 static void gem_init_mac(struct gem *gp)
1861 unsigned char *e = &gp->dev->dev_addr[0];
1863 writel(0x1bf0, gp->regs + MAC_SNDPAUSE);
1865 writel(0x00, gp->regs + MAC_IPG0);
1866 writel(0x08, gp->regs + MAC_IPG1);
1867 writel(0x04, gp->regs + MAC_IPG2);
1868 writel(0x40, gp->regs + MAC_STIME);
1869 writel(0x40, gp->regs + MAC_MINFSZ);
1871 /* Ethernet payload + header + FCS + optional VLAN tag. */
1872 writel(0x20000000 | (gp->rx_buf_sz + 4), gp->regs + MAC_MAXFSZ);
1874 writel(0x07, gp->regs + MAC_PASIZE);
1875 writel(0x04, gp->regs + MAC_JAMSIZE);
1876 writel(0x10, gp->regs + MAC_ATTLIM);
1877 writel(0x8808, gp->regs + MAC_MCTYPE);
1879 writel((e[5] | (e[4] << 8)) & 0x3ff, gp->regs + MAC_RANDSEED);
1881 writel((e[4] << 8) | e[5], gp->regs + MAC_ADDR0);
1882 writel((e[2] << 8) | e[3], gp->regs + MAC_ADDR1);
1883 writel((e[0] << 8) | e[1], gp->regs + MAC_ADDR2);
1885 writel(0, gp->regs + MAC_ADDR3);
1886 writel(0, gp->regs + MAC_ADDR4);
1887 writel(0, gp->regs + MAC_ADDR5);
1889 writel(0x0001, gp->regs + MAC_ADDR6);
1890 writel(0xc200, gp->regs + MAC_ADDR7);
1891 writel(0x0180, gp->regs + MAC_ADDR8);
1893 writel(0, gp->regs + MAC_AFILT0);
1894 writel(0, gp->regs + MAC_AFILT1);
1895 writel(0, gp->regs + MAC_AFILT2);
1896 writel(0, gp->regs + MAC_AF21MSK);
1897 writel(0, gp->regs + MAC_AF0MSK);
1899 gp->mac_rx_cfg = gem_setup_multicast(gp);
1901 gp->mac_rx_cfg |= MAC_RXCFG_SFCS;
1903 writel(0, gp->regs + MAC_NCOLL);
1904 writel(0, gp->regs + MAC_FASUCC);
1905 writel(0, gp->regs + MAC_ECOLL);
1906 writel(0, gp->regs + MAC_LCOLL);
1907 writel(0, gp->regs + MAC_DTIMER);
1908 writel(0, gp->regs + MAC_PATMPS);
1909 writel(0, gp->regs + MAC_RFCTR);
1910 writel(0, gp->regs + MAC_LERR);
1911 writel(0, gp->regs + MAC_AERR);
1912 writel(0, gp->regs + MAC_FCSERR);
1913 writel(0, gp->regs + MAC_RXCVERR);
1915 /* Clear RX/TX/MAC/XIF config, we will set these up and enable
1916 * them once a link is established.
1918 writel(0, gp->regs + MAC_TXCFG);
1919 writel(gp->mac_rx_cfg, gp->regs + MAC_RXCFG);
1920 writel(0, gp->regs + MAC_MCCFG);
1921 writel(0, gp->regs + MAC_XIFCFG);
1923 /* Setup MAC interrupts. We want to get all of the interesting
1924 * counter expiration events, but we do not want to hear about
1925 * normal rx/tx as the DMA engine tells us that.
1927 writel(MAC_TXSTAT_XMIT, gp->regs + MAC_TXMASK);
1928 writel(MAC_RXSTAT_RCV, gp->regs + MAC_RXMASK);
1930 /* Don't enable even the PAUSE interrupts for now, we
1931 * make no use of those events other than to record them.
1933 writel(0xffffffff, gp->regs + MAC_MCMASK);
1935 /* Don't enable GEM's WOL in normal operations
1938 writel(0, gp->regs + WOL_WAKECSR);
1941 /* Must be invoked under gp->lock and gp->tx_lock. */
1942 static void gem_init_pause_thresholds(struct gem *gp)
1946 /* Calculate pause thresholds. Setting the OFF threshold to the
1947 * full RX fifo size effectively disables PAUSE generation which
1948 * is what we do for 10/100 only GEMs which have FIFOs too small
1949 * to make real gains from PAUSE.
1951 if (gp->rx_fifo_sz <= (2 * 1024)) {
1952 gp->rx_pause_off = gp->rx_pause_on = gp->rx_fifo_sz;
1954 int max_frame = (gp->rx_buf_sz + 4 + 64) & ~63;
1955 int off = (gp->rx_fifo_sz - (max_frame * 2));
1956 int on = off - max_frame;
1958 gp->rx_pause_off = off;
1959 gp->rx_pause_on = on;
1963 /* Configure the chip "burst" DMA mode & enable some
1964 * HW bug fixes on Apple version
1967 if (gp->pdev->vendor == PCI_VENDOR_ID_APPLE)
1968 cfg |= GREG_CFG_RONPAULBIT | GREG_CFG_ENBUG2FIX;
1969 #if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
1970 cfg |= GREG_CFG_IBURST;
1972 cfg |= ((31 << 1) & GREG_CFG_TXDMALIM);
1973 cfg |= ((31 << 6) & GREG_CFG_RXDMALIM);
1974 writel(cfg, gp->regs + GREG_CFG);
1976 /* If Infinite Burst didn't stick, then use different
1977 * thresholds (and Apple bug fixes don't exist)
1979 if (!(readl(gp->regs + GREG_CFG) & GREG_CFG_IBURST)) {
1980 cfg = ((2 << 1) & GREG_CFG_TXDMALIM);
1981 cfg |= ((8 << 6) & GREG_CFG_RXDMALIM);
1982 writel(cfg, gp->regs + GREG_CFG);
1986 static int gem_check_invariants(struct gem *gp)
1988 struct pci_dev *pdev = gp->pdev;
1991 /* On Apple's sungem, we can't rely on registers as the chip
1992 * was been powered down by the firmware. The PHY is looked
1995 if (pdev->vendor == PCI_VENDOR_ID_APPLE) {
1996 gp->phy_type = phy_mii_mdio0;
1997 gp->tx_fifo_sz = readl(gp->regs + TXDMA_FSZ) * 64;
1998 gp->rx_fifo_sz = readl(gp->regs + RXDMA_FSZ) * 64;
2001 mif_cfg = readl(gp->regs + MIF_CFG);
2002 mif_cfg &= ~(MIF_CFG_PSELECT|MIF_CFG_POLL|MIF_CFG_BBMODE|MIF_CFG_MDI1);
2003 mif_cfg |= MIF_CFG_MDI0;
2004 writel(mif_cfg, gp->regs + MIF_CFG);
2005 writel(PCS_DMODE_MGM, gp->regs + PCS_DMODE);
2006 writel(MAC_XIFCFG_OE, gp->regs + MAC_XIFCFG);
2008 /* We hard-code the PHY address so we can properly bring it out of
2009 * reset later on, we can't really probe it at this point, though
2010 * that isn't an issue.
2012 if (gp->pdev->device == PCI_DEVICE_ID_APPLE_K2_GMAC)
2013 gp->mii_phy_addr = 1;
2015 gp->mii_phy_addr = 0;
2020 mif_cfg = readl(gp->regs + MIF_CFG);
2022 if (pdev->vendor == PCI_VENDOR_ID_SUN &&
2023 pdev->device == PCI_DEVICE_ID_SUN_RIO_GEM) {
2024 /* One of the MII PHYs _must_ be present
2025 * as this chip has no gigabit PHY.
2027 if ((mif_cfg & (MIF_CFG_MDI0 | MIF_CFG_MDI1)) == 0) {
2028 printk(KERN_ERR PFX "RIO GEM lacks MII phy, mif_cfg[%08x]\n",
2034 /* Determine initial PHY interface type guess. MDIO1 is the
2035 * external PHY and thus takes precedence over MDIO0.
2038 if (mif_cfg & MIF_CFG_MDI1) {
2039 gp->phy_type = phy_mii_mdio1;
2040 mif_cfg |= MIF_CFG_PSELECT;
2041 writel(mif_cfg, gp->regs + MIF_CFG);
2042 } else if (mif_cfg & MIF_CFG_MDI0) {
2043 gp->phy_type = phy_mii_mdio0;
2044 mif_cfg &= ~MIF_CFG_PSELECT;
2045 writel(mif_cfg, gp->regs + MIF_CFG);
2047 gp->phy_type = phy_serialink;
2049 if (gp->phy_type == phy_mii_mdio1 ||
2050 gp->phy_type == phy_mii_mdio0) {
2053 for (i = 0; i < 32; i++) {
2054 gp->mii_phy_addr = i;
2055 if (phy_read(gp, MII_BMCR) != 0xffff)
2059 if (pdev->device != PCI_DEVICE_ID_SUN_GEM) {
2060 printk(KERN_ERR PFX "RIO MII phy will not respond.\n");
2063 gp->phy_type = phy_serdes;
2067 /* Fetch the FIFO configurations now too. */
2068 gp->tx_fifo_sz = readl(gp->regs + TXDMA_FSZ) * 64;
2069 gp->rx_fifo_sz = readl(gp->regs + RXDMA_FSZ) * 64;
2071 if (pdev->vendor == PCI_VENDOR_ID_SUN) {
2072 if (pdev->device == PCI_DEVICE_ID_SUN_GEM) {
2073 if (gp->tx_fifo_sz != (9 * 1024) ||
2074 gp->rx_fifo_sz != (20 * 1024)) {
2075 printk(KERN_ERR PFX "GEM has bogus fifo sizes tx(%d) rx(%d)\n",
2076 gp->tx_fifo_sz, gp->rx_fifo_sz);
2081 if (gp->tx_fifo_sz != (2 * 1024) ||
2082 gp->rx_fifo_sz != (2 * 1024)) {
2083 printk(KERN_ERR PFX "RIO GEM has bogus fifo sizes tx(%d) rx(%d)\n",
2084 gp->tx_fifo_sz, gp->rx_fifo_sz);
2087 gp->swrst_base = (64 / 4) << GREG_SWRST_CACHE_SHIFT;
2094 /* Must be invoked under gp->lock and gp->tx_lock. */
2095 static void gem_reinit_chip(struct gem *gp)
2097 /* Reset the chip */
2100 /* Make sure ints are disabled */
2101 gem_disable_ints(gp);
2103 /* Allocate & setup ring buffers */
2106 /* Configure pause thresholds */
2107 gem_init_pause_thresholds(gp);
2109 /* Init DMA & MAC engines */
2115 /* Must be invoked with no lock held. */
2116 static void gem_stop_phy(struct gem *gp, int wol)
2119 unsigned long flags;
2121 /* Let the chip settle down a bit, it seems that helps
2122 * for sleep mode on some models
2126 /* Make sure we aren't polling PHY status change. We
2127 * don't currently use that feature though
2129 mifcfg = readl(gp->regs + MIF_CFG);
2130 mifcfg &= ~MIF_CFG_POLL;
2131 writel(mifcfg, gp->regs + MIF_CFG);
2133 if (wol && gp->has_wol) {
2134 unsigned char *e = &gp->dev->dev_addr[0];
2137 /* Setup wake-on-lan for MAGIC packet */
2138 writel(MAC_RXCFG_HFE | MAC_RXCFG_SFCS | MAC_RXCFG_ENAB,
2139 gp->regs + MAC_RXCFG);
2140 writel((e[4] << 8) | e[5], gp->regs + WOL_MATCH0);
2141 writel((e[2] << 8) | e[3], gp->regs + WOL_MATCH1);
2142 writel((e[0] << 8) | e[1], gp->regs + WOL_MATCH2);
2144 writel(WOL_MCOUNT_N | WOL_MCOUNT_M, gp->regs + WOL_MCOUNT);
2145 csr = WOL_WAKECSR_ENABLE;
2146 if ((readl(gp->regs + MAC_XIFCFG) & MAC_XIFCFG_GMII) == 0)
2147 csr |= WOL_WAKECSR_MII;
2148 writel(csr, gp->regs + WOL_WAKECSR);
2150 writel(0, gp->regs + MAC_RXCFG);
2151 (void)readl(gp->regs + MAC_RXCFG);
2152 /* Machine sleep will die in strange ways if we
2153 * dont wait a bit here, looks like the chip takes
2154 * some time to really shut down
2159 writel(0, gp->regs + MAC_TXCFG);
2160 writel(0, gp->regs + MAC_XIFCFG);
2161 writel(0, gp->regs + TXDMA_CFG);
2162 writel(0, gp->regs + RXDMA_CFG);
2165 spin_lock_irqsave(&gp->lock, flags);
2166 spin_lock(&gp->tx_lock);
2168 writel(MAC_TXRST_CMD, gp->regs + MAC_TXRST);
2169 writel(MAC_RXRST_CMD, gp->regs + MAC_RXRST);
2170 spin_unlock(&gp->tx_lock);
2171 spin_unlock_irqrestore(&gp->lock, flags);
2173 /* No need to take the lock here */
2175 if (found_mii_phy(gp) && gp->phy_mii.def->ops->suspend)
2176 gp->phy_mii.def->ops->suspend(&gp->phy_mii);
2178 /* According to Apple, we must set the MDIO pins to this begnign
2179 * state or we may 1) eat more current, 2) damage some PHYs
2181 writel(mifcfg | MIF_CFG_BBMODE, gp->regs + MIF_CFG);
2182 writel(0, gp->regs + MIF_BBCLK);
2183 writel(0, gp->regs + MIF_BBDATA);
2184 writel(0, gp->regs + MIF_BBOENAB);
2185 writel(MAC_XIFCFG_GMII | MAC_XIFCFG_LBCK, gp->regs + MAC_XIFCFG);
2186 (void) readl(gp->regs + MAC_XIFCFG);
2191 static int gem_do_start(struct net_device *dev)
2193 struct gem *gp = netdev_priv(dev);
2194 unsigned long flags;
2196 spin_lock_irqsave(&gp->lock, flags);
2197 spin_lock(&gp->tx_lock);
2199 /* Enable the cell */
2202 /* Init & setup chip hardware */
2203 gem_reinit_chip(gp);
2207 if (gp->lstate == link_up) {
2208 netif_carrier_on(gp->dev);
2209 gem_set_link_modes(gp);
2212 netif_wake_queue(gp->dev);
2214 spin_unlock(&gp->tx_lock);
2215 spin_unlock_irqrestore(&gp->lock, flags);
2217 if (request_irq(gp->pdev->irq, gem_interrupt,
2218 IRQF_SHARED, dev->name, (void *)dev)) {
2219 printk(KERN_ERR "%s: failed to request irq !\n", gp->dev->name);
2221 spin_lock_irqsave(&gp->lock, flags);
2222 spin_lock(&gp->tx_lock);
2226 gem_clean_rings(gp);
2229 spin_unlock(&gp->tx_lock);
2230 spin_unlock_irqrestore(&gp->lock, flags);
2238 static void gem_do_stop(struct net_device *dev, int wol)
2240 struct gem *gp = netdev_priv(dev);
2241 unsigned long flags;
2243 spin_lock_irqsave(&gp->lock, flags);
2244 spin_lock(&gp->tx_lock);
2248 /* Stop netif queue */
2249 netif_stop_queue(dev);
2251 /* Make sure ints are disabled */
2252 gem_disable_ints(gp);
2254 /* We can drop the lock now */
2255 spin_unlock(&gp->tx_lock);
2256 spin_unlock_irqrestore(&gp->lock, flags);
2258 /* If we are going to sleep with WOL */
2265 /* Get rid of rings */
2266 gem_clean_rings(gp);
2268 /* No irq needed anymore */
2269 free_irq(gp->pdev->irq, (void *) dev);
2271 /* Cell not needed neither if no WOL */
2273 spin_lock_irqsave(&gp->lock, flags);
2275 spin_unlock_irqrestore(&gp->lock, flags);
2279 static void gem_reset_task(struct work_struct *work)
2281 struct gem *gp = container_of(work, struct gem, reset_task);
2283 mutex_lock(&gp->pm_mutex);
2286 napi_disable(&gp->napi);
2288 spin_lock_irq(&gp->lock);
2289 spin_lock(&gp->tx_lock);
2292 netif_stop_queue(gp->dev);
2294 /* Reset the chip & rings */
2295 gem_reinit_chip(gp);
2296 if (gp->lstate == link_up)
2297 gem_set_link_modes(gp);
2298 netif_wake_queue(gp->dev);
2301 gp->reset_task_pending = 0;
2303 spin_unlock(&gp->tx_lock);
2304 spin_unlock_irq(&gp->lock);
2307 napi_enable(&gp->napi);
2309 mutex_unlock(&gp->pm_mutex);
2313 static int gem_open(struct net_device *dev)
2315 struct gem *gp = netdev_priv(dev);
2318 mutex_lock(&gp->pm_mutex);
2320 /* We need the cell enabled */
2322 rc = gem_do_start(dev);
2323 gp->opened = (rc == 0);
2325 napi_enable(&gp->napi);
2327 mutex_unlock(&gp->pm_mutex);
2332 static int gem_close(struct net_device *dev)
2334 struct gem *gp = netdev_priv(dev);
2336 mutex_lock(&gp->pm_mutex);
2338 napi_disable(&gp->napi);
2342 gem_do_stop(dev, 0);
2344 mutex_unlock(&gp->pm_mutex);
2350 static int gem_suspend(struct pci_dev *pdev, pm_message_t state)
2352 struct net_device *dev = pci_get_drvdata(pdev);
2353 struct gem *gp = netdev_priv(dev);
2354 unsigned long flags;
2356 mutex_lock(&gp->pm_mutex);
2358 printk(KERN_INFO "%s: suspending, WakeOnLan %s\n",
2360 (gp->wake_on_lan && gp->opened) ? "enabled" : "disabled");
2362 /* Keep the cell enabled during the entire operation */
2363 spin_lock_irqsave(&gp->lock, flags);
2364 spin_lock(&gp->tx_lock);
2366 spin_unlock(&gp->tx_lock);
2367 spin_unlock_irqrestore(&gp->lock, flags);
2369 /* If the driver is opened, we stop the MAC */
2371 napi_disable(&gp->napi);
2373 /* Stop traffic, mark us closed */
2374 netif_device_detach(dev);
2376 /* Switch off MAC, remember WOL setting */
2377 gp->asleep_wol = gp->wake_on_lan;
2378 gem_do_stop(dev, gp->asleep_wol);
2382 /* Mark us asleep */
2386 /* Stop the link timer */
2387 del_timer_sync(&gp->link_timer);
2389 /* Now we release the mutex to not block the reset task who
2390 * can take it too. We are marked asleep, so there will be no
2393 mutex_unlock(&gp->pm_mutex);
2395 /* Wait for a pending reset task to complete */
2396 while (gp->reset_task_pending)
2398 flush_scheduled_work();
2400 /* Shut the PHY down eventually and setup WOL */
2401 gem_stop_phy(gp, gp->asleep_wol);
2403 /* Make sure bus master is disabled */
2404 pci_disable_device(gp->pdev);
2406 /* Release the cell, no need to take a lock at this point since
2407 * nothing else can happen now
2414 static int gem_resume(struct pci_dev *pdev)
2416 struct net_device *dev = pci_get_drvdata(pdev);
2417 struct gem *gp = netdev_priv(dev);
2418 unsigned long flags;
2420 printk(KERN_INFO "%s: resuming\n", dev->name);
2422 mutex_lock(&gp->pm_mutex);
2424 /* Keep the cell enabled during the entire operation, no need to
2425 * take a lock here tho since nothing else can happen while we are
2430 /* Make sure PCI access and bus master are enabled */
2431 if (pci_enable_device(gp->pdev)) {
2432 printk(KERN_ERR "%s: Can't re-enable chip !\n",
2434 /* Put cell and forget it for now, it will be considered as
2435 * still asleep, a new sleep cycle may bring it back
2438 mutex_unlock(&gp->pm_mutex);
2441 pci_set_master(gp->pdev);
2443 /* Reset everything */
2446 /* Mark us woken up */
2450 /* Bring the PHY back. Again, lock is useless at this point as
2451 * nothing can be happening until we restart the whole thing
2455 /* If we were opened, bring everything back */
2460 /* Re-attach net device */
2461 netif_device_attach(dev);
2463 napi_enable(&gp->napi);
2466 spin_lock_irqsave(&gp->lock, flags);
2467 spin_lock(&gp->tx_lock);
2469 /* If we had WOL enabled, the cell clock was never turned off during
2470 * sleep, so we end up beeing unbalanced. Fix that here
2475 /* This function doesn't need to hold the cell, it will be held if the
2476 * driver is open by gem_do_start().
2480 spin_unlock(&gp->tx_lock);
2481 spin_unlock_irqrestore(&gp->lock, flags);
2483 mutex_unlock(&gp->pm_mutex);
2487 #endif /* CONFIG_PM */
2489 static struct net_device_stats *gem_get_stats(struct net_device *dev)
2491 struct gem *gp = netdev_priv(dev);
2492 struct net_device_stats *stats = &gp->net_stats;
2494 spin_lock_irq(&gp->lock);
2495 spin_lock(&gp->tx_lock);
2497 /* I have seen this being called while the PM was in progress,
2498 * so we shield against this
2501 stats->rx_crc_errors += readl(gp->regs + MAC_FCSERR);
2502 writel(0, gp->regs + MAC_FCSERR);
2504 stats->rx_frame_errors += readl(gp->regs + MAC_AERR);
2505 writel(0, gp->regs + MAC_AERR);
2507 stats->rx_length_errors += readl(gp->regs + MAC_LERR);
2508 writel(0, gp->regs + MAC_LERR);
2510 stats->tx_aborted_errors += readl(gp->regs + MAC_ECOLL);
2511 stats->collisions +=
2512 (readl(gp->regs + MAC_ECOLL) +
2513 readl(gp->regs + MAC_LCOLL));
2514 writel(0, gp->regs + MAC_ECOLL);
2515 writel(0, gp->regs + MAC_LCOLL);
2518 spin_unlock(&gp->tx_lock);
2519 spin_unlock_irq(&gp->lock);
2521 return &gp->net_stats;
2524 static int gem_set_mac_address(struct net_device *dev, void *addr)
2526 struct sockaddr *macaddr = (struct sockaddr *) addr;
2527 struct gem *gp = netdev_priv(dev);
2528 unsigned char *e = &dev->dev_addr[0];
2530 if (!is_valid_ether_addr(macaddr->sa_data))
2531 return -EADDRNOTAVAIL;
2533 if (!netif_running(dev) || !netif_device_present(dev)) {
2534 /* We'll just catch it later when the
2535 * device is up'd or resumed.
2537 memcpy(dev->dev_addr, macaddr->sa_data, dev->addr_len);
2541 mutex_lock(&gp->pm_mutex);
2542 memcpy(dev->dev_addr, macaddr->sa_data, dev->addr_len);
2544 writel((e[4] << 8) | e[5], gp->regs + MAC_ADDR0);
2545 writel((e[2] << 8) | e[3], gp->regs + MAC_ADDR1);
2546 writel((e[0] << 8) | e[1], gp->regs + MAC_ADDR2);
2548 mutex_unlock(&gp->pm_mutex);
2553 static void gem_set_multicast(struct net_device *dev)
2555 struct gem *gp = netdev_priv(dev);
2556 u32 rxcfg, rxcfg_new;
2560 spin_lock_irq(&gp->lock);
2561 spin_lock(&gp->tx_lock);
2566 netif_stop_queue(dev);
2568 rxcfg = readl(gp->regs + MAC_RXCFG);
2569 rxcfg_new = gem_setup_multicast(gp);
2571 rxcfg_new |= MAC_RXCFG_SFCS;
2573 gp->mac_rx_cfg = rxcfg_new;
2575 writel(rxcfg & ~MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
2576 while (readl(gp->regs + MAC_RXCFG) & MAC_RXCFG_ENAB) {
2582 rxcfg &= ~(MAC_RXCFG_PROM | MAC_RXCFG_HFE);
2585 writel(rxcfg, gp->regs + MAC_RXCFG);
2587 netif_wake_queue(dev);
2590 spin_unlock(&gp->tx_lock);
2591 spin_unlock_irq(&gp->lock);
2594 /* Jumbo-grams don't seem to work :-( */
2595 #define GEM_MIN_MTU 68
2597 #define GEM_MAX_MTU 1500
2599 #define GEM_MAX_MTU 9000
2602 static int gem_change_mtu(struct net_device *dev, int new_mtu)
2604 struct gem *gp = netdev_priv(dev);
2606 if (new_mtu < GEM_MIN_MTU || new_mtu > GEM_MAX_MTU)
2609 if (!netif_running(dev) || !netif_device_present(dev)) {
2610 /* We'll just catch it later when the
2611 * device is up'd or resumed.
2617 mutex_lock(&gp->pm_mutex);
2618 spin_lock_irq(&gp->lock);
2619 spin_lock(&gp->tx_lock);
2622 gem_reinit_chip(gp);
2623 if (gp->lstate == link_up)
2624 gem_set_link_modes(gp);
2626 spin_unlock(&gp->tx_lock);
2627 spin_unlock_irq(&gp->lock);
2628 mutex_unlock(&gp->pm_mutex);
2633 static void gem_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2635 struct gem *gp = netdev_priv(dev);
2637 strcpy(info->driver, DRV_NAME);
2638 strcpy(info->version, DRV_VERSION);
2639 strcpy(info->bus_info, pci_name(gp->pdev));
2642 static int gem_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2644 struct gem *gp = netdev_priv(dev);
2646 if (gp->phy_type == phy_mii_mdio0 ||
2647 gp->phy_type == phy_mii_mdio1) {
2648 if (gp->phy_mii.def)
2649 cmd->supported = gp->phy_mii.def->features;
2651 cmd->supported = (SUPPORTED_10baseT_Half |
2652 SUPPORTED_10baseT_Full);
2654 /* XXX hardcoded stuff for now */
2655 cmd->port = PORT_MII;
2656 cmd->transceiver = XCVR_EXTERNAL;
2657 cmd->phy_address = 0; /* XXX fixed PHYAD */
2659 /* Return current PHY settings */
2660 spin_lock_irq(&gp->lock);
2661 cmd->autoneg = gp->want_autoneg;
2662 cmd->speed = gp->phy_mii.speed;
2663 cmd->duplex = gp->phy_mii.duplex;
2664 cmd->advertising = gp->phy_mii.advertising;
2666 /* If we started with a forced mode, we don't have a default
2667 * advertise set, we need to return something sensible so
2668 * userland can re-enable autoneg properly.
2670 if (cmd->advertising == 0)
2671 cmd->advertising = cmd->supported;
2672 spin_unlock_irq(&gp->lock);
2673 } else { // XXX PCS ?
2675 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2676 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2678 cmd->advertising = cmd->supported;
2680 cmd->duplex = cmd->port = cmd->phy_address =
2681 cmd->transceiver = cmd->autoneg = 0;
2683 cmd->maxtxpkt = cmd->maxrxpkt = 0;
2688 static int gem_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2690 struct gem *gp = netdev_priv(dev);
2692 /* Verify the settings we care about. */
2693 if (cmd->autoneg != AUTONEG_ENABLE &&
2694 cmd->autoneg != AUTONEG_DISABLE)
2697 if (cmd->autoneg == AUTONEG_ENABLE &&
2698 cmd->advertising == 0)
2701 if (cmd->autoneg == AUTONEG_DISABLE &&
2702 ((cmd->speed != SPEED_1000 &&
2703 cmd->speed != SPEED_100 &&
2704 cmd->speed != SPEED_10) ||
2705 (cmd->duplex != DUPLEX_HALF &&
2706 cmd->duplex != DUPLEX_FULL)))
2709 /* Apply settings and restart link process. */
2710 spin_lock_irq(&gp->lock);
2712 gem_begin_auto_negotiation(gp, cmd);
2714 spin_unlock_irq(&gp->lock);
2719 static int gem_nway_reset(struct net_device *dev)
2721 struct gem *gp = netdev_priv(dev);
2723 if (!gp->want_autoneg)
2726 /* Restart link process. */
2727 spin_lock_irq(&gp->lock);
2729 gem_begin_auto_negotiation(gp, NULL);
2731 spin_unlock_irq(&gp->lock);
2736 static u32 gem_get_msglevel(struct net_device *dev)
2738 struct gem *gp = netdev_priv(dev);
2739 return gp->msg_enable;
2742 static void gem_set_msglevel(struct net_device *dev, u32 value)
2744 struct gem *gp = netdev_priv(dev);
2745 gp->msg_enable = value;
2749 /* Add more when I understand how to program the chip */
2750 /* like WAKE_UCAST | WAKE_MCAST | WAKE_BCAST */
2752 #define WOL_SUPPORTED_MASK (WAKE_MAGIC)
2754 static void gem_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2756 struct gem *gp = netdev_priv(dev);
2758 /* Add more when I understand how to program the chip */
2760 wol->supported = WOL_SUPPORTED_MASK;
2761 wol->wolopts = gp->wake_on_lan;
2768 static int gem_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2770 struct gem *gp = netdev_priv(dev);
2774 gp->wake_on_lan = wol->wolopts & WOL_SUPPORTED_MASK;
2778 static const struct ethtool_ops gem_ethtool_ops = {
2779 .get_drvinfo = gem_get_drvinfo,
2780 .get_link = ethtool_op_get_link,
2781 .get_settings = gem_get_settings,
2782 .set_settings = gem_set_settings,
2783 .nway_reset = gem_nway_reset,
2784 .get_msglevel = gem_get_msglevel,
2785 .set_msglevel = gem_set_msglevel,
2786 .get_wol = gem_get_wol,
2787 .set_wol = gem_set_wol,
2790 static int gem_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2792 struct gem *gp = netdev_priv(dev);
2793 struct mii_ioctl_data *data = if_mii(ifr);
2794 int rc = -EOPNOTSUPP;
2795 unsigned long flags;
2797 /* Hold the PM mutex while doing ioctl's or we may collide
2798 * with power management.
2800 mutex_lock(&gp->pm_mutex);
2802 spin_lock_irqsave(&gp->lock, flags);
2804 spin_unlock_irqrestore(&gp->lock, flags);
2807 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2808 data->phy_id = gp->mii_phy_addr;
2809 /* Fallthrough... */
2811 case SIOCGMIIREG: /* Read MII PHY register. */
2815 data->val_out = __phy_read(gp, data->phy_id & 0x1f,
2816 data->reg_num & 0x1f);
2821 case SIOCSMIIREG: /* Write MII PHY register. */
2822 if (!capable(CAP_NET_ADMIN))
2824 else if (!gp->running)
2827 __phy_write(gp, data->phy_id & 0x1f, data->reg_num & 0x1f,
2834 spin_lock_irqsave(&gp->lock, flags);
2836 spin_unlock_irqrestore(&gp->lock, flags);
2838 mutex_unlock(&gp->pm_mutex);
2843 #if (!defined(CONFIG_SPARC) && !defined(CONFIG_PPC_PMAC))
2844 /* Fetch MAC address from vital product data of PCI ROM. */
2845 static int find_eth_addr_in_vpd(void __iomem *rom_base, int len, unsigned char *dev_addr)
2849 for (this_offset = 0x20; this_offset < len; this_offset++) {
2850 void __iomem *p = rom_base + this_offset;
2853 if (readb(p + 0) != 0x90 ||
2854 readb(p + 1) != 0x00 ||
2855 readb(p + 2) != 0x09 ||
2856 readb(p + 3) != 0x4e ||
2857 readb(p + 4) != 0x41 ||
2858 readb(p + 5) != 0x06)
2864 for (i = 0; i < 6; i++)
2865 dev_addr[i] = readb(p + i);
2871 static void get_gem_mac_nonobp(struct pci_dev *pdev, unsigned char *dev_addr)
2874 void __iomem *p = pci_map_rom(pdev, &size);
2879 found = readb(p) == 0x55 &&
2880 readb(p + 1) == 0xaa &&
2881 find_eth_addr_in_vpd(p, (64 * 1024), dev_addr);
2882 pci_unmap_rom(pdev, p);
2887 /* Sun MAC prefix then 3 random bytes. */
2891 get_random_bytes(dev_addr + 3, 3);
2894 #endif /* not Sparc and not PPC */
2896 static int __devinit gem_get_device_address(struct gem *gp)
2898 #if defined(CONFIG_SPARC) || defined(CONFIG_PPC_PMAC)
2899 struct net_device *dev = gp->dev;
2900 const unsigned char *addr;
2902 addr = of_get_property(gp->of_node, "local-mac-address", NULL);
2905 addr = idprom->id_ethaddr;
2908 printk(KERN_ERR "%s: can't get mac-address\n", dev->name);
2912 memcpy(dev->dev_addr, addr, 6);
2914 get_gem_mac_nonobp(gp->pdev, gp->dev->dev_addr);
2919 static void gem_remove_one(struct pci_dev *pdev)
2921 struct net_device *dev = pci_get_drvdata(pdev);
2924 struct gem *gp = netdev_priv(dev);
2926 unregister_netdev(dev);
2928 /* Stop the link timer */
2929 del_timer_sync(&gp->link_timer);
2931 /* We shouldn't need any locking here */
2934 /* Wait for a pending reset task to complete */
2935 while (gp->reset_task_pending)
2937 flush_scheduled_work();
2939 /* Shut the PHY down */
2940 gem_stop_phy(gp, 0);
2944 /* Make sure bus master is disabled */
2945 pci_disable_device(gp->pdev);
2947 /* Free resources */
2948 pci_free_consistent(pdev,
2949 sizeof(struct gem_init_block),
2953 pci_release_regions(pdev);
2956 pci_set_drvdata(pdev, NULL);
2960 static int __devinit gem_init_one(struct pci_dev *pdev,
2961 const struct pci_device_id *ent)
2963 static int gem_version_printed = 0;
2964 unsigned long gemreg_base, gemreg_len;
2965 struct net_device *dev;
2967 int err, pci_using_dac;
2969 if (gem_version_printed++ == 0)
2970 printk(KERN_INFO "%s", version);
2972 /* Apple gmac note: during probe, the chip is powered up by
2973 * the arch code to allow the code below to work (and to let
2974 * the chip be probed on the config space. It won't stay powered
2975 * up until the interface is brought up however, so we can't rely
2976 * on register configuration done at this point.
2978 err = pci_enable_device(pdev);
2980 printk(KERN_ERR PFX "Cannot enable MMIO operation, "
2984 pci_set_master(pdev);
2986 /* Configure DMA attributes. */
2988 /* All of the GEM documentation states that 64-bit DMA addressing
2989 * is fully supported and should work just fine. However the
2990 * front end for RIO based GEMs is different and only supports
2991 * 32-bit addressing.
2993 * For now we assume the various PPC GEMs are 32-bit only as well.
2995 if (pdev->vendor == PCI_VENDOR_ID_SUN &&
2996 pdev->device == PCI_DEVICE_ID_SUN_GEM &&
2997 !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
3000 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3002 printk(KERN_ERR PFX "No usable DMA configuration, "
3004 goto err_disable_device;
3009 gemreg_base = pci_resource_start(pdev, 0);
3010 gemreg_len = pci_resource_len(pdev, 0);
3012 if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0) {
3013 printk(KERN_ERR PFX "Cannot find proper PCI device "
3014 "base address, aborting.\n");
3016 goto err_disable_device;
3019 dev = alloc_etherdev(sizeof(*gp));
3021 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
3023 goto err_disable_device;
3025 SET_NETDEV_DEV(dev, &pdev->dev);
3027 gp = netdev_priv(dev);
3029 err = pci_request_regions(pdev, DRV_NAME);
3031 printk(KERN_ERR PFX "Cannot obtain PCI resources, "
3033 goto err_out_free_netdev;
3037 dev->base_addr = (long) pdev;
3040 gp->msg_enable = DEFAULT_MSG;
3042 spin_lock_init(&gp->lock);
3043 spin_lock_init(&gp->tx_lock);
3044 mutex_init(&gp->pm_mutex);
3046 init_timer(&gp->link_timer);
3047 gp->link_timer.function = gem_link_timer;
3048 gp->link_timer.data = (unsigned long) gp;
3050 INIT_WORK(&gp->reset_task, gem_reset_task);
3052 gp->lstate = link_down;
3053 gp->timer_ticks = 0;
3054 netif_carrier_off(dev);
3056 gp->regs = ioremap(gemreg_base, gemreg_len);
3058 printk(KERN_ERR PFX "Cannot map device registers, "
3061 goto err_out_free_res;
3064 /* On Apple, we want a reference to the Open Firmware device-tree
3065 * node. We use it for clock control.
3067 #if defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC)
3068 gp->of_node = pci_device_to_OF_node(pdev);
3071 /* Only Apple version supports WOL afaik */
3072 if (pdev->vendor == PCI_VENDOR_ID_APPLE)
3075 /* Make sure cell is enabled */
3078 /* Make sure everything is stopped and in init state */
3081 /* Fill up the mii_phy structure (even if we won't use it) */
3082 gp->phy_mii.dev = dev;
3083 gp->phy_mii.mdio_read = _phy_read;
3084 gp->phy_mii.mdio_write = _phy_write;
3085 #ifdef CONFIG_PPC_PMAC
3086 gp->phy_mii.platform_data = gp->of_node;
3088 /* By default, we start with autoneg */
3089 gp->want_autoneg = 1;
3091 /* Check fifo sizes, PHY type, etc... */
3092 if (gem_check_invariants(gp)) {
3094 goto err_out_iounmap;
3097 /* It is guaranteed that the returned buffer will be at least
3098 * PAGE_SIZE aligned.
3100 gp->init_block = (struct gem_init_block *)
3101 pci_alloc_consistent(pdev, sizeof(struct gem_init_block),
3103 if (!gp->init_block) {
3104 printk(KERN_ERR PFX "Cannot allocate init block, "
3107 goto err_out_iounmap;
3110 if (gem_get_device_address(gp))
3111 goto err_out_free_consistent;
3113 dev->open = gem_open;
3114 dev->stop = gem_close;
3115 dev->hard_start_xmit = gem_start_xmit;
3116 dev->get_stats = gem_get_stats;
3117 dev->set_multicast_list = gem_set_multicast;
3118 dev->do_ioctl = gem_ioctl;
3119 netif_napi_add(dev, &gp->napi, gem_poll, 64);
3120 dev->ethtool_ops = &gem_ethtool_ops;
3121 dev->tx_timeout = gem_tx_timeout;
3122 dev->watchdog_timeo = 5 * HZ;
3123 dev->change_mtu = gem_change_mtu;
3124 dev->irq = pdev->irq;
3126 dev->set_mac_address = gem_set_mac_address;
3127 #ifdef CONFIG_NET_POLL_CONTROLLER
3128 dev->poll_controller = gem_poll_controller;
3131 /* Set that now, in case PM kicks in now */
3132 pci_set_drvdata(pdev, dev);
3134 /* Detect & init PHY, start autoneg, we release the cell now
3135 * too, it will be managed by whoever needs it
3139 spin_lock_irq(&gp->lock);
3141 spin_unlock_irq(&gp->lock);
3143 /* Register with kernel */
3144 if (register_netdev(dev)) {
3145 printk(KERN_ERR PFX "Cannot register net device, "
3148 goto err_out_free_consistent;
3151 printk(KERN_INFO "%s: Sun GEM (PCI) 10/100/1000BaseT Ethernet %pM\n",
3152 dev->name, dev->dev_addr);
3154 if (gp->phy_type == phy_mii_mdio0 ||
3155 gp->phy_type == phy_mii_mdio1)
3156 printk(KERN_INFO "%s: Found %s PHY\n", dev->name,
3157 gp->phy_mii.def ? gp->phy_mii.def->name : "no");
3159 /* GEM can do it all... */
3160 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_LLTX;
3162 dev->features |= NETIF_F_HIGHDMA;
3166 err_out_free_consistent:
3167 gem_remove_one(pdev);
3173 pci_release_regions(pdev);
3175 err_out_free_netdev:
3178 pci_disable_device(pdev);
3184 static struct pci_driver gem_driver = {
3185 .name = GEM_MODULE_NAME,
3186 .id_table = gem_pci_tbl,
3187 .probe = gem_init_one,
3188 .remove = gem_remove_one,
3190 .suspend = gem_suspend,
3191 .resume = gem_resume,
3192 #endif /* CONFIG_PM */
3195 static int __init gem_init(void)
3197 return pci_register_driver(&gem_driver);
3200 static void __exit gem_cleanup(void)
3202 pci_unregister_driver(&gem_driver);
3205 module_init(gem_init);
3206 module_exit(gem_cleanup);