// SPDX-License-Identifier: GPL-2.0 /* $Id: sungem.c,v 1.44.2.22 2002/03/13 01:18:12 davem Exp $ * sungem.c: Sun GEM ethernet driver. * * Copyright (C) 2000, 2001, 2002, 2003 David S. Miller (davem@redhat.com) * * Support for Apple GMAC and assorted PHYs, WOL, Power Management * (C) 2001,2002,2003 Benjamin Herrenscmidt (benh@kernel.crashing.org) * (C) 2004,2005 Benjamin Herrenscmidt, IBM Corp. * * NAPI and NETPOLL support * (C) 2004 by Eric Lemoine (eric.lemoine@gmail.com) *
*/
staticinlinevoid gem_netif_start(struct gem *gp)
{ /* NOTE: unconditional netif_wake_queue is only * appropriate so long as all callers are assured to * have free tx slots.
*/
netif_wake_queue(gp->dev);
napi_enable(&gp->napi);
}
if (netif_msg_intr(gp))
printk(KERN_DEBUG "%s: pcs interrupt, pcs_istat: 0x%x\n",
gp->dev->name, pcs_istat);
if (!(pcs_istat & PCS_ISTAT_LSC)) {
netdev_err(dev, "PCS irq but no link status change???\n"); return 0;
}
/* The link status bit latches on zero, so you must * read it twice in such a case to see a transition * to the link being up.
*/
pcs_miistat = readl(gp->regs + PCS_MIISTAT); if (!(pcs_miistat & PCS_MIISTAT_LS))
pcs_miistat |=
(readl(gp->regs + PCS_MIISTAT) &
PCS_MIISTAT_LS);
if (pcs_miistat & PCS_MIISTAT_ANC) { /* The remote-fault indication is only valid * when autoneg has completed.
*/ if (pcs_miistat & PCS_MIISTAT_RF)
netdev_info(dev, "PCS AutoNEG complete, RemoteFault\n"); else
netdev_info(dev, "PCS AutoNEG complete\n");
}
if (pcs_miistat & PCS_MIISTAT_LS) {
netdev_info(dev, "PCS link is now up\n");
netif_carrier_on(gp->dev);
} else {
netdev_info(dev, "PCS link is now down\n");
netif_carrier_off(gp->dev); /* If this happens and the link timer is not running, * reset so we re-negotiate.
*/ if (!timer_pending(&gp->link_timer)) return 1;
}
/* We do not keep track of MAC_TXSTAT_FCE and * MAC_TXSTAT_PCE events.
*/ return 0;
}
/* When we get a RX fifo overflow, the RX unit in GEM is probably hung * so we do the following. * * If any part of the reset goes wrong, we return 1 and that causes the * whole chip to be reset.
*/ staticint gem_rxmac_reset(struct gem *gp)
{ struct net_device *dev = gp->dev; int limit, i;
u64 desc_dma;
u32 val;
/* First, reset & disable MAC RX. */
writel(MAC_RXRST_CMD, gp->regs + MAC_RXRST); for (limit = 0; limit < 5000; limit++) { if (!(readl(gp->regs + MAC_RXRST) & MAC_RXRST_CMD)) break;
udelay(10);
} if (limit == 5000) {
netdev_err(dev, "RX MAC will not reset, resetting whole chip\n"); return 1;
}
writel(gp->mac_rx_cfg & ~MAC_RXCFG_ENAB,
gp->regs + MAC_RXCFG); for (limit = 0; limit < 5000; limit++) { if (!(readl(gp->regs + MAC_RXCFG) & MAC_RXCFG_ENAB)) break;
udelay(10);
} if (limit == 5000) {
netdev_err(dev, "RX MAC will not disable, resetting whole chip\n"); return 1;
}
/* Second, disable RX DMA. */
writel(0, gp->regs + RXDMA_CFG); for (limit = 0; limit < 5000; limit++) { if (!(readl(gp->regs + RXDMA_CFG) & RXDMA_CFG_ENABLE)) break;
udelay(10);
} if (limit == 5000) {
netdev_err(dev, "RX DMA will not disable, resetting whole chip\n"); return 1;
}
mdelay(5);
/* Execute RX reset command. */
writel(gp->swrst_base | GREG_SWRST_RXRST,
gp->regs + GREG_SWRST); for (limit = 0; limit < 5000; limit++) { if (!(readl(gp->regs + GREG_SWRST) & GREG_SWRST_RXRST)) break;
udelay(10);
} if (limit == 5000) {
netdev_err(dev, "RX reset command will not execute, resetting whole chip\n"); return 1;
}
/* Refresh the RX ring. */ for (i = 0; i < RX_RING_SIZE; i++) { struct gem_rxd *rxd = &gp->init_block->rxd[i];
if (gp->rx_skbs[i] == NULL) {
netdev_err(dev, "Parts of RX ring empty, resetting whole chip\n"); return 1;
}
if (netif_msg_intr(gp))
printk(KERN_DEBUG "%s: mac interrupt, mac_cstat: 0x%x\n",
gp->dev->name, mac_cstat);
/* This interrupt is just for pause frame and pause * tracking. It is useful for diagnostics and debug * but probably by default we will mask these events.
*/ if (mac_cstat & MAC_CSTAT_PS)
gp->pause_entered++;
if (mac_cstat & MAC_CSTAT_PRCV)
gp->pause_last_time_recvd = (mac_cstat >> 16);
if (pci_estat & GREG_PCIESTAT_BADACK)
pr_cont(" "); if (pci_estat & GREG_PCIESTAT_DTRTO)
pr_cont(" "); if (pci_estat & GREG_PCIESTAT_OTHER)
pr_cont(" ");
pr_cont("\n");
} else {
pci_estat |= GREG_PCIESTAT_OTHER;
netdev_err(dev, "PCI error\n");
}
if (pci_estat & GREG_PCIESTAT_OTHER) { int pci_errs;
/* Interrogate PCI config space for the * true cause.
*/
pci_errs = pci_status_get_and_clear_errors(gp->pdev);
netdev_err(dev, "PCI status errors[%04x]\n", pci_errs); if (pci_errs & PCI_STATUS_PARITY)
netdev_err(dev, "PCI parity error detected\n"); if (pci_errs & PCI_STATUS_SIG_TARGET_ABORT)
netdev_err(dev, "PCI target abort\n"); if (pci_errs & PCI_STATUS_REC_TARGET_ABORT)
netdev_err(dev, "PCI master acks target abort\n"); if (pci_errs & PCI_STATUS_REC_MASTER_ABORT)
netdev_err(dev, "PCI master abort\n"); if (pci_errs & PCI_STATUS_SIG_SYSTEM_ERROR)
netdev_err(dev, "PCI system error SERR#\n"); if (pci_errs & PCI_STATUS_DETECTED_PARITY)
netdev_err(dev, "PCI parity error\n");
}
/* For all PCI errors, we should reset the chip. */ return 1;
}
/* All non-normal interrupt conditions get serviced here. * Returns non-zero if we should just exit the interrupt * handler right now (ie. if we reset the card which invalidates * all of the other original irq status bits).
*/ staticint gem_abnormal_irq(struct net_device *dev, struct gem *gp, u32 gem_status)
{ if (gem_status & GREG_STAT_RXNOBUF) { /* Frame arrived, no free RX buffers available. */ if (netif_msg_rx_err(gp))
printk(KERN_DEBUG "%s: no buffer for rx frame\n",
gp->dev->name);
dev->stats.rx_dropped++;
}
if (gem_status & GREG_STAT_RXTAGERR) { /* corrupt RX tag framing */ if (netif_msg_rx_err(gp))
printk(KERN_DEBUG "%s: corrupt rx tag framing\n",
gp->dev->name);
dev->stats.rx_errors++;
return 1;
}
if (gem_status & GREG_STAT_PCS) { if (gem_pcs_interrupt(dev, gp, gem_status)) return 1;
}
if (gem_status & GREG_STAT_TXMAC) { if (gem_txmac_interrupt(dev, gp, gem_status)) return 1;
}
if (gem_status & GREG_STAT_RXMAC) { if (gem_rxmac_interrupt(dev, gp, gem_status)) return 1;
}
if (gem_status & GREG_STAT_MAC) { if (gem_mac_interrupt(dev, gp, gem_status)) return 1;
}
if (gem_status & GREG_STAT_MIF) { if (gem_mif_interrupt(dev, gp, gem_status)) return 1;
}
if (gem_status & GREG_STAT_PCIERR) { if (gem_pci_interrupt(dev, gp, gem_status)) return 1;
}
if (netif_msg_tx_done(gp))
printk(KERN_DEBUG "%s: tx done, slot %d\n",
gp->dev->name, entry);
skb = gp->tx_skbs[entry]; if (skb_shinfo(skb)->nr_frags) { int last = entry + skb_shinfo(skb)->nr_frags; int walk = entry; int incomplete = 0;
last &= (TX_RING_SIZE - 1); for (;;) {
walk = NEXT_TX(walk); if (walk == limit)
incomplete = 1; if (walk == last) break;
} if (incomplete) break;
}
gp->tx_skbs[entry] = NULL;
dev->stats.tx_bytes += skb->len;
/* Need to make the tx_old update visible to gem_start_xmit() * before checking for netif_queue_stopped(). Without the * memory barrier, there is a small possibility that gem_start_xmit() * will miss it and cause the queue to be stopped forever.
*/
smp_mb();
entry = gp->rx_new;
drops = 0;
done = readl(gp->regs + RXDMA_DONE); for (;;) { struct gem_rxd *rxd = &gp->init_block->rxd[entry]; struct sk_buff *skb;
u64 status = le64_to_cpu(rxd->status_word);
dma_addr_t dma_addr; int len;
if ((status & RXDCTRL_OWN) != 0) break;
if (work_done >= RX_RING_SIZE || work_done >= work_to_do) break;
/* When writing back RX descriptor, GEM writes status * then buffer address, possibly in separate transactions. * If we don't wait for the chip to write both, we could * post a new buffer to this descriptor then have GEM spam * on the buffer address. We sync on the RX completion * register to prevent this from happening.
*/ if (entry == done) {
done = readl(gp->regs + RXDMA_DONE); if (entry == done) break;
}
/* We can now account for the work we're about to do */
work_done++;
skb = gp->rx_skbs[entry];
len = (status & RXDCTRL_BUFSZ) >> 16; if ((len < ETH_ZLEN) || (status & RXDCTRL_BAD)) {
dev->stats.rx_errors++; if (len < ETH_ZLEN)
dev->stats.rx_length_errors++; if (len & RXDCTRL_BAD)
dev->stats.rx_crc_errors++;
/* We'll just return it to GEM. */
drop_it:
dev->stats.rx_dropped++; goto next;
}
work_done = 0; do { /* Handle anomalies */ if (unlikely(gp->status & GREG_STAT_ABNORMAL)) { struct netdev_queue *txq = netdev_get_tx_queue(dev, 0); int reset;
/* We run the abnormal interrupt handling code with * the Tx lock. It only resets the Rx portion of the * chip, but we need to guard it against DMA being * restarted by the link poll timer
*/
__netif_tx_lock(txq, smp_processor_id());
reset = gem_abnormal_irq(dev, gp, gp->status);
__netif_tx_unlock(txq); if (reset) {
gem_schedule_reset(gp);
napi_complete(napi); return work_done;
}
}
/* Run TX completion thread */
gem_tx(dev, gp, gp->status);
/* Run RX thread. We don't use any locking here, * code willing to do bad things - like cleaning the * rx ring - must call napi_disable(), which * schedule_timeout()'s if polling is already disabled.
*/
work_done += gem_rx(gp, budget - work_done);
if (work_done >= budget) return work_done;
gp->status = readl(gp->regs + GREG_STAT);
} while (gp->status & GREG_STAT_NAPI);
/* If polling was disabled at the time we received that * interrupt, we may return IRQ_HANDLED here while we * should return IRQ_NONE. No big deal...
*/ return IRQ_HANDLED;
}
if (unlikely(TX_BUFFS_AVAIL(gp) <= (skb_shinfo(skb)->nr_frags + 1))) { /* This is a hard error, log it. */ if (!netif_queue_stopped(dev)) {
netif_stop_queue(dev);
netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
} return NETDEV_TX_BUSY;
}
intme = 0; if (gem_intme(entry))
intme |= TXDCTRL_INTME;
/* We must give this initial chunk to the device last. * Otherwise we could race with the device.
*/
first_len = skb_headlen(skb);
first_mapping = dma_map_page(&gp->pdev->dev,
virt_to_page(skb->data),
offset_in_page(skb->data),
first_len, DMA_TO_DEVICE);
entry = NEXT_TX(entry);
/* netif_stop_queue() must be done before checking * tx index in TX_BUFFS_AVAIL() below, because * in gem_tx(), we update tx_old before checking for * netif_queue_stopped().
*/
smp_mb(); if (TX_BUFFS_AVAIL(gp) > (MAX_SKB_FRAGS + 1))
netif_wake_queue(dev);
} if (netif_msg_tx_queued(gp))
printk(KERN_DEBUG "%s: tx queued, slot %d, skblen %d\n",
dev->name, entry, skb->len);
mb();
writel(gp->tx_new, gp->regs + TXDMA_KICK);
return NETDEV_TX_OK;
}
staticvoid gem_pcs_reset(struct gem *gp)
{ int limit;
u32 val;
/* Reset PCS unit. */
val = readl(gp->regs + PCS_MIICTRL);
val |= PCS_MIICTRL_RST;
writel(val, gp->regs + PCS_MIICTRL);
limit = 32; while (readl(gp->regs + PCS_MIICTRL) & PCS_MIICTRL_RST) {
udelay(100); if (limit-- <= 0) break;
} if (limit < 0)
netdev_warn(gp->dev, "PCS reset bit would not clear\n");
}
/* Make sure PCS is disabled while changing advertisement * configuration.
*/
val = readl(gp->regs + PCS_CFG);
val &= ~(PCS_CFG_ENABLE | PCS_CFG_TO);
writel(val, gp->regs + PCS_CFG);
/* Advertise all capabilities except asymmetric * pause.
*/
val = readl(gp->regs + PCS_MIIADV);
val |= (PCS_MIIADV_FD | PCS_MIIADV_HD |
PCS_MIIADV_SP | PCS_MIIADV_AP);
writel(val, gp->regs + PCS_MIIADV);
/* Enable and restart auto-negotiation, disable wrapback/loopback, * and re-enable PCS.
*/
val = readl(gp->regs + PCS_MIICTRL);
val |= (PCS_MIICTRL_RAN | PCS_MIICTRL_ANE);
val &= ~PCS_MIICTRL_WB;
writel(val, gp->regs + PCS_MIICTRL);
val = readl(gp->regs + PCS_CFG);
val |= PCS_CFG_ENABLE;
writel(val, gp->regs + PCS_CFG);
/* Make sure serialink loopback is off. The meaning * of this bit is logically inverted based upon whether * you are in Serialink or SERDES mode.
*/
val = readl(gp->regs + PCS_SCTRL); if (gp->phy_type == phy_serialink)
val &= ~PCS_SCTRL_LOOP; else
val |= PCS_SCTRL_LOOP;
writel(val, gp->regs + PCS_SCTRL);
}
#define STOP_TRIES 32
staticvoid gem_reset(struct gem *gp)
{ int limit;
u32 val;
/* Make sure we won't get any more interrupts */
writel(0xffffffff, gp->regs + GREG_IMASK);
/* DMA won't be actually stopped before about 4ms tho ...
*/ staticvoid gem_stop_dma(struct gem *gp)
{
u32 val;
/* We are done rocking, turn everything off. */
val = readl(gp->regs + TXDMA_CFG);
writel(val & ~TXDMA_CFG_ENABLE, gp->regs + TXDMA_CFG);
val = readl(gp->regs + RXDMA_CFG);
writel(val & ~RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
val = readl(gp->regs + MAC_TXCFG);
writel(val & ~MAC_TXCFG_ENAB, gp->regs + MAC_TXCFG);
val = readl(gp->regs + MAC_RXCFG);
writel(val & ~MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
(void) readl(gp->regs + MAC_RXCFG);
/* Need to wait a bit ... done by the caller */
}
// XXX dbl check what that function should do when called on PCS PHY staticvoid gem_begin_auto_negotiation(struct gem *gp, conststruct ethtool_link_ksettings *ep)
{
u32 advertise, features; int autoneg; int speed; int duplex;
u32 advertising;
if (ep)
ethtool_convert_link_mode_to_legacy_u32(
&advertising, ep->link_modes.advertising);
if (gp->phy_type != phy_mii_mdio0 &&
gp->phy_type != phy_mii_mdio1) goto non_mii;
/* Setup advertise */ if (found_mii_phy(gp))
features = gp->phy_mii.def->features; else
features = 0;
advertise = features & ADVERTISE_MASK; if (gp->phy_mii.advertising != 0)
advertise &= gp->phy_mii.advertising;
start_aneg: /* Sanitize settings based on PHY capabilities */ if ((features & SUPPORTED_Autoneg) == 0)
autoneg = 0; if (speed == SPEED_1000 &&
!(features & (SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full)))
speed = SPEED_100; if (speed == SPEED_100 &&
!(features & (SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full)))
speed = SPEED_10; if (duplex == DUPLEX_FULL &&
!(features & (SUPPORTED_1000baseT_Full |
SUPPORTED_100baseT_Full |
SUPPORTED_10baseT_Full)))
duplex = DUPLEX_HALF; if (speed == 0)
speed = SPEED_10;
/* If we are asleep, we don't try to actually setup the PHY, we * just store the settings
*/ if (!netif_device_present(gp->dev)) {
gp->phy_mii.autoneg = gp->want_autoneg = autoneg;
gp->phy_mii.speed = speed;
gp->phy_mii.duplex = duplex; return;
}
/* A link-up condition has occurred, initialize and enable the * rest of the chip.
*/ staticint gem_set_link_modes(struct gem *gp)
{ struct netdev_queue *txq = netdev_get_tx_queue(gp->dev, 0); int full_duplex, speed, pause;
u32 val;
netif_info(gp, link, gp->dev, "Link is up at %d Mbps, %s-duplex\n",
speed, (full_duplex ? "full" : "half"));
/* We take the tx queue lock to avoid collisions between * this code, the tx path and the NAPI-driven error path
*/
__netif_tx_lock(txq, smp_processor_id());
val = (MAC_TXCFG_EIPG0 | MAC_TXCFG_NGU); if (full_duplex) {
val |= (MAC_TXCFG_ICS | MAC_TXCFG_ICOLL);
} else { /* MAC_TXCFG_NBO must be zero. */
}
writel(val, gp->regs + MAC_TXCFG);
val = (MAC_XIFCFG_OE | MAC_XIFCFG_LLED); if (!full_duplex &&
(gp->phy_type == phy_mii_mdio0 ||
gp->phy_type == phy_mii_mdio1)) {
val |= MAC_XIFCFG_DISE;
} elseif (full_duplex) {
val |= MAC_XIFCFG_FLED;
}
if (speed == SPEED_1000)
val |= (MAC_XIFCFG_GMII);
writel(val, gp->regs + MAC_XIFCFG);
/* If gigabit and half-duplex, enable carrier extension * mode. Else, disable it.
*/ if (speed == SPEED_1000 && !full_duplex) {
val = readl(gp->regs + MAC_TXCFG);
writel(val | MAC_TXCFG_TCE, gp->regs + MAC_TXCFG);
if (pcs_lpa & (PCS_MIIADV_SP | PCS_MIIADV_AP))
pause = 1;
}
if (!full_duplex)
writel(512, gp->regs + MAC_STIME); else
writel(64, gp->regs + MAC_STIME);
val = readl(gp->regs + MAC_MCCFG); if (pause)
val |= (MAC_MCCFG_SPE | MAC_MCCFG_RPE); else
val &= ~(MAC_MCCFG_SPE | MAC_MCCFG_RPE);
writel(val, gp->regs + MAC_MCCFG);
gem_start_dma(gp);
__netif_tx_unlock(txq);
if (netif_msg_link(gp)) { if (pause) {
netdev_info(gp->dev, "Pause is enabled (rxfifo: %d off: %d on: %d)\n",
gp->rx_fifo_sz,
gp->rx_pause_off,
gp->rx_pause_on);
} else {
netdev_info(gp->dev, "Pause is disabled\n");
}
}
return 0;
}
staticint gem_mdio_link_not_up(struct gem *gp)
{ switch (gp->lstate) { case link_force_ret:
netif_info(gp, link, gp->dev, "Autoneg failed again, keeping forced mode\n");
gp->phy_mii.def->ops->setup_forced(&gp->phy_mii,
gp->last_forced_speed, DUPLEX_HALF);
gp->timer_ticks = 5;
gp->lstate = link_force_ok; return 0; case link_aneg: /* We try forced modes after a failed aneg only on PHYs that don't * have "magic_aneg" bit set, which means they internally do the * while forced-mode thingy. On these, we just restart aneg
*/ if (gp->phy_mii.def->magic_aneg) return 1;
netif_info(gp, link, gp->dev, "switching to forced 100bt\n"); /* Try forced modes. */
gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, SPEED_100,
DUPLEX_HALF);
gp->timer_ticks = 5;
gp->lstate = link_force_try; return 0; case link_force_try: /* Downgrade from 100 to 10 Mbps if necessary. * If already at 10Mbps, warn user about the * situation every 10 ticks.
*/ if (gp->phy_mii.speed == SPEED_100) {
gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, SPEED_10,
DUPLEX_HALF);
gp->timer_ticks = 5;
netif_info(gp, link, gp->dev, "switching to forced 10bt\n"); return 0;
} else return 1; default: return 0;
}
}
/* There's no point doing anything if we're going to be reset */ if (gp->reset_task_pending) return;
if (gp->phy_type == phy_serialink ||
gp->phy_type == phy_serdes) {
u32 val = readl(gp->regs + PCS_MIISTAT);
if (!(val & PCS_MIISTAT_LS))
val = readl(gp->regs + PCS_MIISTAT);
if ((val & PCS_MIISTAT_LS) != 0) { if (gp->lstate == link_up) goto restart;
gp->lstate = link_up;
netif_carrier_on(dev);
(void)gem_set_link_modes(gp);
} goto restart;
} if (found_mii_phy(gp) && gp->phy_mii.def->ops->poll_link(&gp->phy_mii)) { /* Ok, here we got a link. If we had it due to a forced * fallback, and we were configured for autoneg, we do * retry a short autoneg pass. If you know your hub is * broken, use ethtool ;)
*/ if (gp->lstate == link_force_try && gp->want_autoneg) {
gp->lstate = link_force_ret;
gp->last_forced_speed = gp->phy_mii.speed;
gp->timer_ticks = 5; if (netif_msg_link(gp))
netdev_info(dev, "Got link after fallback, retrying autoneg once...\n");
gp->phy_mii.def->ops->setup_aneg(&gp->phy_mii, gp->phy_mii.advertising);
} elseif (gp->lstate != link_up) {
gp->lstate = link_up;
netif_carrier_on(dev); if (gem_set_link_modes(gp))
restart_aneg = 1;
}
} else { /* If the link was previously up, we restart the * whole process
*/ if (gp->lstate == link_up) {
gp->lstate = link_down;
netif_info(gp, link, dev, "Link down\n");
netif_carrier_off(dev);
gem_schedule_reset(gp); /* The reset task will restart the timer */ return;
} elseif (++gp->timer_ticks > 10) { if (found_mii_phy(gp))
restart_aneg = gem_mdio_link_not_up(gp); else
restart_aneg = 1;
}
} if (restart_aneg) {
gem_begin_auto_negotiation(gp, NULL); return;
}
restart:
mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
}
if (gp->pdev->vendor == PCI_VENDOR_ID_APPLE) { int i;
/* Those delays sucks, the HW seems to love them though, I'll * seriously consider breaking some locks here to be able * to schedule instead
*/ for (i = 0; i < 3; i++) { #ifdef CONFIG_PPC_PMAC
pmac_call_feature(PMAC_FTR_GMAC_PHY_RESET, gp->of_node, 0, 0);
msleep(20); #endif /* Some PHYs used by apple have problem getting back to us, * we do an additional reset here
*/
sungem_phy_write(gp, MII_BMCR, BMCR_RESET);
msleep(20); if (sungem_phy_read(gp, MII_BMCR) != 0xffff) break; if (i == 2)
netdev_warn(gp->dev, "GMAC PHY not responding !\n");
}
}
/* Clear RX/TX/MAC/XIF config, we will set these up and enable * them once a link is established.
*/
writel(0, gp->regs + MAC_TXCFG);
writel(gp->mac_rx_cfg, gp->regs + MAC_RXCFG);
writel(0, gp->regs + MAC_MCCFG);
writel(0, gp->regs + MAC_XIFCFG);
/* Setup MAC interrupts. We want to get all of the interesting * counter expiration events, but we do not want to hear about * normal rx/tx as the DMA engine tells us that.
*/
writel(MAC_TXSTAT_XMIT, gp->regs + MAC_TXMASK);
writel(MAC_RXSTAT_RCV, gp->regs + MAC_RXMASK);
/* Don't enable even the PAUSE interrupts for now, we * make no use of those events other than to record them.
*/
writel(0xffffffff, gp->regs + MAC_MCMASK);
/* Don't enable GEM's WOL in normal operations
*/ if (gp->has_wol)
writel(0, gp->regs + WOL_WAKECSR);
}
/* Calculate pause thresholds. Setting the OFF threshold to the * full RX fifo size effectively disables PAUSE generation which * is what we do for 10/100 only GEMs which have FIFOs too small * to make real gains from PAUSE.
*/ if (gp->rx_fifo_sz <= (2 * 1024)) {
gp->rx_pause_off = gp->rx_pause_on = gp->rx_fifo_sz;
} else { int max_frame = (gp->rx_buf_sz + 4 + 64) & ~63; int off = (gp->rx_fifo_sz - (max_frame * 2)); int on = off - max_frame;
gp->rx_pause_off = off;
gp->rx_pause_on = on;
}
/* Configure the chip "burst" DMA mode & enable some * HW bug fixes on Apple version
*/
cfg = 0; if (gp->pdev->vendor == PCI_VENDOR_ID_APPLE)
cfg |= GREG_CFG_RONPAULBIT | GREG_CFG_ENBUG2FIX; #if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
cfg |= GREG_CFG_IBURST; #endif
cfg |= ((31 << 1) & GREG_CFG_TXDMALIM);
cfg |= ((31 << 6) & GREG_CFG_RXDMALIM);
writel(cfg, gp->regs + GREG_CFG);
/* If Infinite Burst didn't stick, then use different * thresholds (and Apple bug fixes don't exist)
*/ if (!(readl(gp->regs + GREG_CFG) & GREG_CFG_IBURST)) {
cfg = ((2 << 1) & GREG_CFG_TXDMALIM);
cfg |= ((8 << 6) & GREG_CFG_RXDMALIM);
writel(cfg, gp->regs + GREG_CFG);
}
}
/* On Apple's sungem, we can't rely on registers as the chip * was been powered down by the firmware. The PHY is looked * up later on.
*/ if (pdev->vendor == PCI_VENDOR_ID_APPLE) {
gp->phy_type = phy_mii_mdio0;
gp->tx_fifo_sz = readl(gp->regs + TXDMA_FSZ) * 64;
gp->rx_fifo_sz = readl(gp->regs + RXDMA_FSZ) * 64;
gp->swrst_base = 0;
/* We hard-code the PHY address so we can properly bring it out of * reset later on, we can't really probe it at this point, though * that isn't an issue.
*/ if (gp->pdev->device == PCI_DEVICE_ID_APPLE_K2_GMAC)
gp->mii_phy_addr = 1; else
gp->mii_phy_addr = 0;
return 0;
}
mif_cfg = readl(gp->regs + MIF_CFG);
if (pdev->vendor == PCI_VENDOR_ID_SUN &&
pdev->device == PCI_DEVICE_ID_SUN_RIO_GEM) { /* One of the MII PHYs _must_ be present * as this chip has no gigabit PHY.
*/ if ((mif_cfg & (MIF_CFG_MDI0 | MIF_CFG_MDI1)) == 0) {
pr_err("RIO GEM lacks MII phy, mif_cfg[%08x]\n",
mif_cfg); return -1;
}
}
/* Determine initial PHY interface type guess. MDIO1 is the * external PHY and thus takes precedence over MDIO0.
*/
p = of_get_property(gp->of_node, "shared-pins", NULL); if (p && !strcmp(p, "serdes"))
gp->phy_type = phy_serdes; else #endif
gp->phy_type = phy_serialink;
} if (gp->phy_type == phy_mii_mdio1 ||
gp->phy_type == phy_mii_mdio0) { int i;
for (i = 0; i < 32; i++) {
gp->mii_phy_addr = i; if (sungem_phy_read(gp, MII_BMCR) != 0xffff) break;
} if (i == 32) { if (pdev->device != PCI_DEVICE_ID_SUN_GEM) {
pr_err("RIO MII phy will not respond\n"); return -1;
}
gp->phy_type = phy_serdes;
}
}
/* Init DMA & MAC engines */
gem_init_dma(gp);
gem_init_mac(gp);
}
staticvoid gem_stop_phy(struct gem *gp, int wol)
{
u32 mifcfg;
/* Let the chip settle down a bit, it seems that helps * for sleep mode on some models
*/
msleep(10);
/* Make sure we aren't polling PHY status change. We * don't currently use that feature though
*/
mifcfg = readl(gp->regs + MIF_CFG);
mifcfg &= ~MIF_CFG_POLL;
writel(mifcfg, gp->regs + MIF_CFG);
writel(WOL_MCOUNT_N | WOL_MCOUNT_M, gp->regs + WOL_MCOUNT);
csr = WOL_WAKECSR_ENABLE; if ((readl(gp->regs + MAC_XIFCFG) & MAC_XIFCFG_GMII) == 0)
csr |= WOL_WAKECSR_MII;
writel(csr, gp->regs + WOL_WAKECSR);
} else {
writel(0, gp->regs + MAC_RXCFG);
(void)readl(gp->regs + MAC_RXCFG); /* Machine sleep will die in strange ways if we * dont wait a bit here, looks like the chip takes * some time to really shut down
*/
msleep(10);
}
if (found_mii_phy(gp) && gp->phy_mii.def->ops->suspend)
gp->phy_mii.def->ops->suspend(&gp->phy_mii);
/* According to Apple, we must set the MDIO pins to this begnign * state or we may 1) eat more current, 2) damage some PHYs
*/
writel(mifcfg | MIF_CFG_BBMODE, gp->regs + MIF_CFG);
writel(0, gp->regs + MIF_BBCLK);
writel(0, gp->regs + MIF_BBDATA);
writel(0, gp->regs + MIF_BBOENAB);
writel(MAC_XIFCFG_GMII | MAC_XIFCFG_LBCK, gp->regs + MAC_XIFCFG);
(void) readl(gp->regs + MAC_XIFCFG);
}
}
/* An interrupt might come in handy */
rc = request_irq(gp->pdev->irq, gem_interrupt,
IRQF_SHARED, dev->name, (void *)dev); if (rc) {
netdev_err(dev, "failed to request irq !\n");
/* Mark us as attached again if we come from resume(), this has * no effect if we weren't detached and needs to be done now.
*/
netif_device_attach(dev);
/* Restart NAPI & queues */
gem_netif_start(gp);
/* Detect & init PHY, start autoneg etc... this will * eventually result in starting DMA operations when * the link is up
*/
gem_init_phy(gp);
/* Stop NAPI and stop tx queue */
gem_netif_stop(gp);
/* Make sure ints are disabled. We don't care about * synchronizing as NAPI is disabled, thus a stray * interrupt will do nothing bad (our irq handler * just schedules NAPI)
*/
gem_disable_ints(gp);
/* Stop the link timer */
timer_delete_sync(&gp->link_timer);
/* We cannot cancel the reset task while holding the * rtnl lock, we'd get an A->B / B->A deadlock stituation * if we did. This is not an issue however as the reset * task is synchronized vs. us (rtnl_lock) and will do * nothing if the device is down or suspended. We do * still clear reset_task_pending to avoid a spurrious * reset later on in case we do resume before it gets * scheduled.
*/
gp->reset_task_pending = 0;
/* If we are going to sleep with WOL */
gem_stop_dma(gp);
msleep(10); if (!wol)
gem_reset(gp);
msleep(10);
/* Get rid of rings */
gem_clean_rings(gp);
/* No irq needed anymore */
free_irq(gp->pdev->irq, (void *) dev);
/* Shut the PHY down eventually and setup WOL */
gem_stop_phy(gp, wol);
}
/* Lock out the network stack (essentially shield ourselves * against a racing open, close, control call, or suspend
*/
rtnl_lock();
/* Skip the reset task if suspended or closed, or if it's * been cancelled by gem_do_stop (see comment there)
*/ if (!netif_device_present(gp->dev) ||
!netif_running(gp->dev) ||
!gp->reset_task_pending) {
rtnl_unlock(); return;
}
/* Stop the link timer */
timer_delete_sync(&gp->link_timer);
/* Stop NAPI and tx */
gem_netif_stop(gp);
/* Reset the chip & rings */
gem_reinit_chip(gp); if (gp->lstate == link_up)
gem_set_link_modes(gp);
/* Restart NAPI and Tx */
gem_netif_start(gp);
/* We are back ! */
gp->reset_task_pending = 0;
/* If the link is not up, restart autoneg, else restart the * polling timer
*/ if (gp->lstate != link_up)
gem_begin_auto_negotiation(gp, NULL); else
mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
/* We allow open while suspended, we just do nothing, * the chip will be initialized in resume()
*/ if (netif_device_present(dev)) { /* Enable the cell */
gem_get_cell(gp);
/* Make sure PCI access and bus master are enabled */
rc = pci_enable_device(gp->pdev); if (rc) {
netdev_err(dev, "Failed to enable chip on PCI bus !\n");
/* Put cell and forget it for now, it will be considered *as still asleep, a new sleep cycle may bring it back
*/
gem_put_cell(gp); return -ENXIO;
} return gem_do_start(dev);
}
/* Lock the network stack first to avoid racing with open/close, * reset task and setting calls
*/
rtnl_lock();
/* Not running, mark ourselves non-present, no need for * a lock here
*/ if (!netif_running(dev)) {
netif_device_detach(dev);
rtnl_unlock(); return 0;
}
netdev_info(dev, "suspending, WakeOnLan %s\n",
(gp->wake_on_lan && netif_running(dev)) ? "enabled" : "disabled");
/* Tell the network stack we're gone. gem_do_stop() below will * synchronize with TX, stop NAPI etc...
*/
netif_device_detach(dev);
/* Switch off chip, remember WOL setting */
gp->asleep_wol = !!gp->wake_on_lan;
gem_do_stop(dev, gp->asleep_wol);
/* Cell not needed neither if no WOL */ if (!gp->asleep_wol)
gem_put_cell(gp);
/* See locking comment in gem_suspend */
rtnl_lock();
/* Not running, mark ourselves present, no need for * a lock here
*/ if (!netif_running(dev)) {
netif_device_attach(dev);
rtnl_unlock(); return 0;
}
/* Enable the cell */
gem_get_cell(gp);
/* Restart chip. If that fails there isn't much we can do, we * leave things stopped.
*/
gem_do_start(dev);
/* If we had WOL enabled, the cell clock was never turned off during * sleep, so we end up beeing unbalanced. Fix that here
*/ if (gp->asleep_wol)
gem_put_cell(gp);
/* I have seen this being called while the PM was in progress, * so we shield against this. Let's also not poke at registers * while the reset task is going on. * * TODO: Move stats collection elsewhere (link timer ?) and * make this a nop to avoid all those synchro issues
*/ if (!netif_device_present(dev) || !netif_running(dev)) goto bail;
/* Better safe than sorry... */ if (WARN_ON(!gp->cell_enabled)) goto bail;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.