2  * Network device driver for the MACE ethernet controller on
 
   3  * Apple Powermacs.  Assumes it's under a DBDMA controller.
 
   5  * Copyright (C) 1996 Paul Mackerras.
 
   8 #include <linux/config.h>
 
   9 #include <linux/module.h>
 
  10 #include <linux/kernel.h>
 
  11 #include <linux/netdevice.h>
 
  12 #include <linux/etherdevice.h>
 
  13 #include <linux/delay.h>
 
  14 #include <linux/string.h>
 
  15 #include <linux/timer.h>
 
  16 #include <linux/init.h>
 
  17 #include <linux/crc32.h>
 
  18 #include <linux/spinlock.h>
 
  20 #include <asm/dbdma.h>
 
  22 #include <asm/pgtable.h>
 
  23 #include <asm/macio.h>
 
  27 static int port_aaui = -1;
 
  31 #define MAX_TX_ACTIVE   1
 
  32 #define NCMDS_TX        1       /* dma commands per element in tx ring */
 
  33 #define RX_BUFLEN       (ETH_FRAME_LEN + 8)
 
  34 #define TX_TIMEOUT      HZ      /* 1 second */
 
  36 /* Chip rev needs workaround on HW & multicast addr change */
 
  37 #define BROKEN_ADDRCHG_REV      0x0941
 
  39 /* Bits in transmit DMA status */
 
  40 #define TX_DMA_ERR      0x80
 
  43     volatile struct mace __iomem *mace;
 
  44     volatile struct dbdma_regs __iomem *tx_dma;
 
  46     volatile struct dbdma_regs __iomem *rx_dma;
 
  48     volatile struct dbdma_cmd *tx_cmds; /* xmit dma command list */
 
  49     volatile struct dbdma_cmd *rx_cmds; /* recv dma command list */
 
  50     struct sk_buff *rx_bufs[N_RX_RING];
 
  53     struct sk_buff *tx_bufs[N_TX_RING];
 
  57     unsigned char tx_fullup;
 
  58     unsigned char tx_active;
 
  59     unsigned char tx_bad_runt;
 
  60     struct net_device_stats stats;
 
  61     struct timer_list tx_timeout;
 
  65     struct macio_dev *mdev;
 
  70  * Number of bytes of private data per MACE: allow enough for
 
  71  * the rx and tx dma commands plus a branch dma command each,
 
  72  * and another 16 bytes to allow us to align the dma command
 
  73  * buffers on a 16 byte boundary.
 
  75 #define PRIV_BYTES      (sizeof(struct mace_data) \
 
  76         + (N_RX_RING + NCMDS_TX * N_TX_RING + 3) * sizeof(struct dbdma_cmd))
 
  78 static int bitrev(int);
 
  79 static int mace_open(struct net_device *dev);
 
  80 static int mace_close(struct net_device *dev);
 
  81 static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev);
 
  82 static struct net_device_stats *mace_stats(struct net_device *dev);
 
  83 static void mace_set_multicast(struct net_device *dev);
 
  84 static void mace_reset(struct net_device *dev);
 
  85 static int mace_set_address(struct net_device *dev, void *addr);
 
  86 static irqreturn_t mace_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 
  87 static irqreturn_t mace_txdma_intr(int irq, void *dev_id, struct pt_regs *regs);
 
  88 static irqreturn_t mace_rxdma_intr(int irq, void *dev_id, struct pt_regs *regs);
 
  89 static void mace_set_timeout(struct net_device *dev);
 
  90 static void mace_tx_timeout(unsigned long data);
 
  91 static inline void dbdma_reset(volatile struct dbdma_regs __iomem *dma);
 
  92 static inline void mace_clean_rings(struct mace_data *mp);
 
  93 static void __mace_set_address(struct net_device *dev, void *addr);
 
  96  * If we can't get a skbuff when we need it, we use this area for DMA.
 
  98 static unsigned char *dummy_buf;
 
 100 /* Bit-reverse one byte of an ethernet hardware address. */
 
 106     for (i = 0; i < 8; ++i, b >>= 1)
 
 107         d = (d << 1) | (b & 1);
 
 112 static int __devinit mace_probe(struct macio_dev *mdev, const struct of_device_id *match)
 
 114         struct device_node *mace = macio_get_of_node(mdev);
 
 115         struct net_device *dev;
 
 116         struct mace_data *mp;
 
 118         int j, rev, rc = -EBUSY;
 
 120         if (macio_resource_count(mdev) != 3 || macio_irq_count(mdev) != 3) {
 
 121                 printk(KERN_ERR "can't use MACE %s: need 3 addrs and 3 irqs\n",
 
 126         addr = get_property(mace, "mac-address", NULL);
 
 128                 addr = get_property(mace, "local-mac-address", NULL);
 
 130                         printk(KERN_ERR "Can't get mac-address for MACE %s\n",
 
 137          * lazy allocate the driver-wide dummy buffer. (Note that we
 
 138          * never have more than one MACE in the system anyway)
 
 140         if (dummy_buf == NULL) {
 
 141                 dummy_buf = kmalloc(RX_BUFLEN+2, GFP_KERNEL);
 
 142                 if (dummy_buf == NULL) {
 
 143                         printk(KERN_ERR "MACE: couldn't allocate dummy buffer\n");
 
 148         if (macio_request_resources(mdev, "mace")) {
 
 149                 printk(KERN_ERR "MACE: can't request IO resources !\n");
 
 153         dev = alloc_etherdev(PRIV_BYTES);
 
 155                 printk(KERN_ERR "MACE: can't allocate ethernet device !\n");
 
 159         SET_MODULE_OWNER(dev);
 
 160         SET_NETDEV_DEV(dev, &mdev->ofdev.dev);
 
 164         macio_set_drvdata(mdev, dev);
 
 166         dev->base_addr = macio_resource_start(mdev, 0);
 
 167         mp->mace = ioremap(dev->base_addr, 0x1000);
 
 168         if (mp->mace == NULL) {
 
 169                 printk(KERN_ERR "MACE: can't map IO resources !\n");
 
 173         dev->irq = macio_irq(mdev, 0);
 
 175         rev = addr[0] == 0 && addr[1] == 0xA0;
 
 176         for (j = 0; j < 6; ++j) {
 
 177                 dev->dev_addr[j] = rev? bitrev(addr[j]): addr[j];
 
 179         mp->chipid = (in_8(&mp->mace->chipid_hi) << 8) |
 
 180                         in_8(&mp->mace->chipid_lo);
 
 183         mp = (struct mace_data *) dev->priv;
 
 184         mp->maccc = ENXMT | ENRCV;
 
 186         mp->tx_dma = ioremap(macio_resource_start(mdev, 1), 0x1000);
 
 187         if (mp->tx_dma == NULL) {
 
 188                 printk(KERN_ERR "MACE: can't map TX DMA resources !\n");
 
 192         mp->tx_dma_intr = macio_irq(mdev, 1);
 
 194         mp->rx_dma = ioremap(macio_resource_start(mdev, 2), 0x1000);
 
 195         if (mp->rx_dma == NULL) {
 
 196                 printk(KERN_ERR "MACE: can't map RX DMA resources !\n");
 
 198                 goto err_unmap_tx_dma;
 
 200         mp->rx_dma_intr = macio_irq(mdev, 2);
 
 202         mp->tx_cmds = (volatile struct dbdma_cmd *) DBDMA_ALIGN(mp + 1);
 
 203         mp->rx_cmds = mp->tx_cmds + NCMDS_TX * N_TX_RING + 1;
 
 205         memset(&mp->stats, 0, sizeof(mp->stats));
 
 206         memset((char *) mp->tx_cmds, 0,
 
 207                (NCMDS_TX*N_TX_RING + N_RX_RING + 2) * sizeof(struct dbdma_cmd));
 
 208         init_timer(&mp->tx_timeout);
 
 209         spin_lock_init(&mp->lock);
 
 210         mp->timeout_active = 0;
 
 213                 mp->port_aaui = port_aaui;
 
 215                 /* Apple Network Server uses the AAUI port */
 
 216                 if (machine_is_compatible("AAPL,ShinerESB"))
 
 219 #ifdef CONFIG_MACE_AAUI_PORT
 
 227         dev->open = mace_open;
 
 228         dev->stop = mace_close;
 
 229         dev->hard_start_xmit = mace_xmit_start;
 
 230         dev->get_stats = mace_stats;
 
 231         dev->set_multicast_list = mace_set_multicast;
 
 232         dev->set_mac_address = mace_set_address;
 
 235          * Most of what is below could be moved to mace_open()
 
 239         rc = request_irq(dev->irq, mace_interrupt, 0, "MACE", dev);
 
 241                 printk(KERN_ERR "MACE: can't get irq %d\n", dev->irq);
 
 242                 goto err_unmap_rx_dma;
 
 244         rc = request_irq(mp->tx_dma_intr, mace_txdma_intr, 0, "MACE-txdma", dev);
 
 246                 printk(KERN_ERR "MACE: can't get irq %d\n", mace->intrs[1].line);
 
 249         rc = request_irq(mp->rx_dma_intr, mace_rxdma_intr, 0, "MACE-rxdma", dev);
 
 251                 printk(KERN_ERR "MACE: can't get irq %d\n", mace->intrs[2].line);
 
 252                 goto err_free_tx_irq;
 
 255         rc = register_netdev(dev);
 
 257                 printk(KERN_ERR "MACE: Cannot register net device, aborting.\n");
 
 258                 goto err_free_rx_irq;
 
 261         printk(KERN_INFO "%s: MACE at", dev->name);
 
 262         for (j = 0; j < 6; ++j) {
 
 263                 printk("%c%.2x", (j? ':': ' '), dev->dev_addr[j]);
 
 265         printk(", chip revision %d.%d\n", mp->chipid >> 8, mp->chipid & 0xff);
 
 270         free_irq(macio_irq(mdev, 2), dev);
 
 272         free_irq(macio_irq(mdev, 1), dev);
 
 274         free_irq(macio_irq(mdev, 0), dev);
 
 284         macio_release_resources(mdev);
 
 289 static int __devexit mace_remove(struct macio_dev *mdev)
 
 291         struct net_device *dev = macio_get_drvdata(mdev);
 
 292         struct mace_data *mp;
 
 296         macio_set_drvdata(mdev, NULL);
 
 300         unregister_netdev(dev);
 
 302         free_irq(dev->irq, dev);
 
 303         free_irq(mp->tx_dma_intr, dev);
 
 304         free_irq(mp->rx_dma_intr, dev);
 
 312         macio_release_resources(mdev);
 
 317 static void dbdma_reset(volatile struct dbdma_regs __iomem *dma)
 
 321     out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16);
 
 324      * Yes this looks peculiar, but apparently it needs to be this
 
 325      * way on some machines.
 
 327     for (i = 200; i > 0; --i)
 
 328         if (ld_le32(&dma->control) & RUN)
 
 332 static void mace_reset(struct net_device *dev)
 
 334     struct mace_data *mp = (struct mace_data *) dev->priv;
 
 335     volatile struct mace __iomem *mb = mp->mace;
 
 338     /* soft-reset the chip */
 
 341         out_8(&mb->biucc, SWRST);
 
 342         if (in_8(&mb->biucc) & SWRST) {
 
 349         printk(KERN_ERR "mace: cannot reset chip!\n");
 
 353     out_8(&mb->imr, 0xff);      /* disable all intrs for now */
 
 355     out_8(&mb->maccc, 0);       /* turn off tx, rx */
 
 357     out_8(&mb->biucc, XMTSP_64);
 
 358     out_8(&mb->utr, RTRD);
 
 359     out_8(&mb->fifocc, RCVFW_32 | XMTFW_16 | XMTFWU | RCVFWU | XMTBRST);
 
 360     out_8(&mb->xmtfc, AUTO_PAD_XMIT); /* auto-pad short frames */
 
 361     out_8(&mb->rcvfc, 0);
 
 363     /* load up the hardware address */
 
 364     __mace_set_address(dev, dev->dev_addr);
 
 366     /* clear the multicast filter */
 
 367     if (mp->chipid == BROKEN_ADDRCHG_REV)
 
 368         out_8(&mb->iac, LOGADDR);
 
 370         out_8(&mb->iac, ADDRCHG | LOGADDR);
 
 371         while ((in_8(&mb->iac) & ADDRCHG) != 0)
 
 374     for (i = 0; i < 8; ++i)
 
 375         out_8(&mb->ladrf, 0);
 
 377     /* done changing address */
 
 378     if (mp->chipid != BROKEN_ADDRCHG_REV)
 
 382         out_8(&mb->plscc, PORTSEL_AUI + ENPLSIO);
 
 384         out_8(&mb->plscc, PORTSEL_GPSI + ENPLSIO);
 
 387 static void __mace_set_address(struct net_device *dev, void *addr)
 
 389     struct mace_data *mp = (struct mace_data *) dev->priv;
 
 390     volatile struct mace __iomem *mb = mp->mace;
 
 391     unsigned char *p = addr;
 
 394     /* load up the hardware address */
 
 395     if (mp->chipid == BROKEN_ADDRCHG_REV)
 
 396         out_8(&mb->iac, PHYADDR);
 
 398         out_8(&mb->iac, ADDRCHG | PHYADDR);
 
 399         while ((in_8(&mb->iac) & ADDRCHG) != 0)
 
 402     for (i = 0; i < 6; ++i)
 
 403         out_8(&mb->padr, dev->dev_addr[i] = p[i]);
 
 404     if (mp->chipid != BROKEN_ADDRCHG_REV)
 
 408 static int mace_set_address(struct net_device *dev, void *addr)
 
 410     struct mace_data *mp = (struct mace_data *) dev->priv;
 
 411     volatile struct mace __iomem *mb = mp->mace;
 
 414     spin_lock_irqsave(&mp->lock, flags);
 
 416     __mace_set_address(dev, addr);
 
 418     /* note: setting ADDRCHG clears ENRCV */
 
 419     out_8(&mb->maccc, mp->maccc);
 
 421     spin_unlock_irqrestore(&mp->lock, flags);
 
 425 static inline void mace_clean_rings(struct mace_data *mp)
 
 429     /* free some skb's */
 
 430     for (i = 0; i < N_RX_RING; ++i) {
 
 431         if (mp->rx_bufs[i] != 0) {
 
 432             dev_kfree_skb(mp->rx_bufs[i]);
 
 433             mp->rx_bufs[i] = NULL;
 
 436     for (i = mp->tx_empty; i != mp->tx_fill; ) {
 
 437         dev_kfree_skb(mp->tx_bufs[i]);
 
 438         if (++i >= N_TX_RING)
 
 443 static int mace_open(struct net_device *dev)
 
 445     struct mace_data *mp = (struct mace_data *) dev->priv;
 
 446     volatile struct mace __iomem *mb = mp->mace;
 
 447     volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
 
 448     volatile struct dbdma_regs __iomem *td = mp->tx_dma;
 
 449     volatile struct dbdma_cmd *cp;
 
 457     /* initialize list of sk_buffs for receiving and set up recv dma */
 
 458     mace_clean_rings(mp);
 
 459     memset((char *)mp->rx_cmds, 0, N_RX_RING * sizeof(struct dbdma_cmd));
 
 461     for (i = 0; i < N_RX_RING - 1; ++i) {
 
 462         skb = dev_alloc_skb(RX_BUFLEN + 2);
 
 466             skb_reserve(skb, 2);        /* so IP header lands on 4-byte bdry */
 
 469         mp->rx_bufs[i] = skb;
 
 470         st_le16(&cp->req_count, RX_BUFLEN);
 
 471         st_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
 
 472         st_le32(&cp->phy_addr, virt_to_bus(data));
 
 476     mp->rx_bufs[i] = NULL;
 
 477     st_le16(&cp->command, DBDMA_STOP);
 
 481     /* Put a branch back to the beginning of the receive command list */
 
 483     st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
 
 484     st_le32(&cp->cmd_dep, virt_to_bus(mp->rx_cmds));
 
 487     out_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
 
 488     out_le32(&rd->cmdptr, virt_to_bus(mp->rx_cmds));
 
 489     out_le32(&rd->control, (RUN << 16) | RUN);
 
 491     /* put a branch at the end of the tx command list */
 
 492     cp = mp->tx_cmds + NCMDS_TX * N_TX_RING;
 
 493     st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
 
 494     st_le32(&cp->cmd_dep, virt_to_bus(mp->tx_cmds));
 
 497     out_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
 
 498     out_le32(&td->cmdptr, virt_to_bus(mp->tx_cmds));
 
 506     out_8(&mb->maccc, mp->maccc);
 
 507     /* enable all interrupts except receive interrupts */
 
 508     out_8(&mb->imr, RCVINT);
 
 513 static int mace_close(struct net_device *dev)
 
 515     struct mace_data *mp = (struct mace_data *) dev->priv;
 
 516     volatile struct mace __iomem *mb = mp->mace;
 
 517     volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
 
 518     volatile struct dbdma_regs __iomem *td = mp->tx_dma;
 
 520     /* disable rx and tx */
 
 521     out_8(&mb->maccc, 0);
 
 522     out_8(&mb->imr, 0xff);              /* disable all intrs */
 
 524     /* disable rx and tx dma */
 
 525     st_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
 
 526     st_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
 
 528     mace_clean_rings(mp);
 
 533 static inline void mace_set_timeout(struct net_device *dev)
 
 535     struct mace_data *mp = (struct mace_data *) dev->priv;
 
 537     if (mp->timeout_active)
 
 538         del_timer(&mp->tx_timeout);
 
 539     mp->tx_timeout.expires = jiffies + TX_TIMEOUT;
 
 540     mp->tx_timeout.function = mace_tx_timeout;
 
 541     mp->tx_timeout.data = (unsigned long) dev;
 
 542     add_timer(&mp->tx_timeout);
 
 543     mp->timeout_active = 1;
 
 546 static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev)
 
 548     struct mace_data *mp = (struct mace_data *) dev->priv;
 
 549     volatile struct dbdma_regs __iomem *td = mp->tx_dma;
 
 550     volatile struct dbdma_cmd *cp, *np;
 
 554     /* see if there's a free slot in the tx ring */
 
 555     spin_lock_irqsave(&mp->lock, flags);
 
 558     if (next >= N_TX_RING)
 
 560     if (next == mp->tx_empty) {
 
 561         netif_stop_queue(dev);
 
 563         spin_unlock_irqrestore(&mp->lock, flags);
 
 564         return 1;               /* can't take it at the moment */
 
 566     spin_unlock_irqrestore(&mp->lock, flags);
 
 568     /* partially fill in the dma command block */
 
 570     if (len > ETH_FRAME_LEN) {
 
 571         printk(KERN_DEBUG "mace: xmit frame too long (%d)\n", len);
 
 574     mp->tx_bufs[fill] = skb;
 
 575     cp = mp->tx_cmds + NCMDS_TX * fill;
 
 576     st_le16(&cp->req_count, len);
 
 577     st_le32(&cp->phy_addr, virt_to_bus(skb->data));
 
 579     np = mp->tx_cmds + NCMDS_TX * next;
 
 580     out_le16(&np->command, DBDMA_STOP);
 
 582     /* poke the tx dma channel */
 
 583     spin_lock_irqsave(&mp->lock, flags);
 
 585     if (!mp->tx_bad_runt && mp->tx_active < MAX_TX_ACTIVE) {
 
 586         out_le16(&cp->xfer_status, 0);
 
 587         out_le16(&cp->command, OUTPUT_LAST);
 
 588         out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
 
 590         mace_set_timeout(dev);
 
 592     if (++next >= N_TX_RING)
 
 594     if (next == mp->tx_empty)
 
 595         netif_stop_queue(dev);
 
 596     spin_unlock_irqrestore(&mp->lock, flags);
 
 601 static struct net_device_stats *mace_stats(struct net_device *dev)
 
 603     struct mace_data *p = (struct mace_data *) dev->priv;
 
 608 static void mace_set_multicast(struct net_device *dev)
 
 610     struct mace_data *mp = (struct mace_data *) dev->priv;
 
 611     volatile struct mace __iomem *mb = mp->mace;
 
 616     spin_lock_irqsave(&mp->lock, flags);
 
 618     if (dev->flags & IFF_PROMISC) {
 
 621         unsigned char multicast_filter[8];
 
 622         struct dev_mc_list *dmi = dev->mc_list;
 
 624         if (dev->flags & IFF_ALLMULTI) {
 
 625             for (i = 0; i < 8; i++)
 
 626                 multicast_filter[i] = 0xff;
 
 628             for (i = 0; i < 8; i++)
 
 629                 multicast_filter[i] = 0;
 
 630             for (i = 0; i < dev->mc_count; i++) {
 
 631                 crc = ether_crc_le(6, dmi->dmi_addr);
 
 632                 j = crc >> 26;  /* bit number in multicast_filter */
 
 633                 multicast_filter[j >> 3] |= 1 << (j & 7);
 
 638         printk("Multicast filter :");
 
 639         for (i = 0; i < 8; i++)
 
 640             printk("%02x ", multicast_filter[i]);
 
 644         if (mp->chipid == BROKEN_ADDRCHG_REV)
 
 645             out_8(&mb->iac, LOGADDR);
 
 647             out_8(&mb->iac, ADDRCHG | LOGADDR);
 
 648             while ((in_8(&mb->iac) & ADDRCHG) != 0)
 
 651         for (i = 0; i < 8; ++i)
 
 652             out_8(&mb->ladrf, multicast_filter[i]);
 
 653         if (mp->chipid != BROKEN_ADDRCHG_REV)
 
 657     out_8(&mb->maccc, mp->maccc);
 
 658     spin_unlock_irqrestore(&mp->lock, flags);
 
 661 static void mace_handle_misc_intrs(struct mace_data *mp, int intr)
 
 663     volatile struct mace __iomem *mb = mp->mace;
 
 664     static int mace_babbles, mace_jabbers;
 
 667         mp->stats.rx_missed_errors += 256;
 
 668     mp->stats.rx_missed_errors += in_8(&mb->mpc);   /* reading clears it */
 
 670         mp->stats.rx_length_errors += 256;
 
 671     mp->stats.rx_length_errors += in_8(&mb->rntpc); /* reading clears it */
 
 673         ++mp->stats.tx_heartbeat_errors;
 
 675         if (mace_babbles++ < 4)
 
 676             printk(KERN_DEBUG "mace: babbling transmitter\n");
 
 678         if (mace_jabbers++ < 4)
 
 679             printk(KERN_DEBUG "mace: jabbering transceiver\n");
 
 682 static irqreturn_t mace_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 
 684     struct net_device *dev = (struct net_device *) dev_id;
 
 685     struct mace_data *mp = (struct mace_data *) dev->priv;
 
 686     volatile struct mace __iomem *mb = mp->mace;
 
 687     volatile struct dbdma_regs __iomem *td = mp->tx_dma;
 
 688     volatile struct dbdma_cmd *cp;
 
 689     int intr, fs, i, stat, x;
 
 692     /* static int mace_last_fs, mace_last_xcount; */
 
 694     spin_lock_irqsave(&mp->lock, flags);
 
 695     intr = in_8(&mb->ir);               /* read interrupt register */
 
 696     in_8(&mb->xmtrc);                   /* get retries */
 
 697     mace_handle_misc_intrs(mp, intr);
 
 700     while (in_8(&mb->pr) & XMTSV) {
 
 701         del_timer(&mp->tx_timeout);
 
 702         mp->timeout_active = 0;
 
 704          * Clear any interrupt indication associated with this status
 
 705          * word.  This appears to unlatch any error indication from
 
 706          * the DMA controller.
 
 708         intr = in_8(&mb->ir);
 
 710             mace_handle_misc_intrs(mp, intr);
 
 711         if (mp->tx_bad_runt) {
 
 712             fs = in_8(&mb->xmtfs);
 
 714             out_8(&mb->xmtfc, AUTO_PAD_XMIT);
 
 717         dstat = ld_le32(&td->status);
 
 718         /* stop DMA controller */
 
 719         out_le32(&td->control, RUN << 16);
 
 721          * xcount is the number of complete frames which have been
 
 722          * written to the fifo but for which status has not been read.
 
 724         xcount = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
 
 725         if (xcount == 0 || (dstat & DEAD)) {
 
 727              * If a packet was aborted before the DMA controller has
 
 728              * finished transferring it, it seems that there are 2 bytes
 
 729              * which are stuck in some buffer somewhere.  These will get
 
 730              * transmitted as soon as we read the frame status (which
 
 731              * reenables the transmit data transfer request).  Turning
 
 732              * off the DMA controller and/or resetting the MACE doesn't
 
 733              * help.  So we disable auto-padding and FCS transmission
 
 734              * so the two bytes will only be a runt packet which should
 
 735              * be ignored by other stations.
 
 737             out_8(&mb->xmtfc, DXMTFCS);
 
 739         fs = in_8(&mb->xmtfs);
 
 740         if ((fs & XMTSV) == 0) {
 
 741             printk(KERN_ERR "mace: xmtfs not valid! (fs=%x xc=%d ds=%x)\n",
 
 745                  * XXX mace likes to hang the machine after a xmtfs error.
 
 746                  * This is hard to reproduce, reseting *may* help
 
 749         cp = mp->tx_cmds + NCMDS_TX * i;
 
 750         stat = ld_le16(&cp->xfer_status);
 
 751         if ((fs & (UFLO|LCOL|LCAR|RTRY)) || (dstat & DEAD) || xcount == 0) {
 
 753              * Check whether there were in fact 2 bytes written to
 
 757             x = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
 
 759                 /* there were two bytes with an end-of-packet indication */
 
 761                 mace_set_timeout(dev);
 
 764                  * Either there weren't the two bytes buffered up, or they
 
 765                  * didn't have an end-of-packet indication.
 
 766                  * We flush the transmit FIFO just in case (by setting the
 
 767                  * XMTFWU bit with the transmitter disabled).
 
 769                 out_8(&mb->maccc, in_8(&mb->maccc) & ~ENXMT);
 
 770                 out_8(&mb->fifocc, in_8(&mb->fifocc) | XMTFWU);
 
 772                 out_8(&mb->maccc, in_8(&mb->maccc) | ENXMT);
 
 773                 out_8(&mb->xmtfc, AUTO_PAD_XMIT);
 
 776         /* dma should have finished */
 
 777         if (i == mp->tx_fill) {
 
 778             printk(KERN_DEBUG "mace: tx ring ran out? (fs=%x xc=%d ds=%x)\n",
 
 783         if (fs & (UFLO|LCOL|LCAR|RTRY)) {
 
 784             ++mp->stats.tx_errors;
 
 786                 ++mp->stats.tx_carrier_errors;
 
 787             if (fs & (UFLO|LCOL|RTRY))
 
 788                 ++mp->stats.tx_aborted_errors;
 
 790             mp->stats.tx_bytes += mp->tx_bufs[i]->len;
 
 791             ++mp->stats.tx_packets;
 
 793         dev_kfree_skb_irq(mp->tx_bufs[i]);
 
 795         if (++i >= N_TX_RING)
 
 799         mace_last_xcount = xcount;
 
 803     if (i != mp->tx_empty) {
 
 805         netif_wake_queue(dev);
 
 811     if (!mp->tx_bad_runt && i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE) {
 
 813             /* set up the next one */
 
 814             cp = mp->tx_cmds + NCMDS_TX * i;
 
 815             out_le16(&cp->xfer_status, 0);
 
 816             out_le16(&cp->command, OUTPUT_LAST);
 
 818             if (++i >= N_TX_RING)
 
 820         } while (i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE);
 
 821         out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
 
 822         mace_set_timeout(dev);
 
 824     spin_unlock_irqrestore(&mp->lock, flags);
 
 828 static void mace_tx_timeout(unsigned long data)
 
 830     struct net_device *dev = (struct net_device *) data;
 
 831     struct mace_data *mp = (struct mace_data *) dev->priv;
 
 832     volatile struct mace __iomem *mb = mp->mace;
 
 833     volatile struct dbdma_regs __iomem *td = mp->tx_dma;
 
 834     volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
 
 835     volatile struct dbdma_cmd *cp;
 
 839     spin_lock_irqsave(&mp->lock, flags);
 
 840     mp->timeout_active = 0;
 
 841     if (mp->tx_active == 0 && !mp->tx_bad_runt)
 
 844     /* update various counters */
 
 845     mace_handle_misc_intrs(mp, in_8(&mb->ir));
 
 847     cp = mp->tx_cmds + NCMDS_TX * mp->tx_empty;
 
 849     /* turn off both tx and rx and reset the chip */
 
 850     out_8(&mb->maccc, 0);
 
 851     printk(KERN_ERR "mace: transmit timeout - resetting\n");
 
 856     cp = bus_to_virt(ld_le32(&rd->cmdptr));
 
 858     out_le16(&cp->xfer_status, 0);
 
 859     out_le32(&rd->cmdptr, virt_to_bus(cp));
 
 860     out_le32(&rd->control, (RUN << 16) | RUN);
 
 862     /* fix up the transmit side */
 
 865     ++mp->stats.tx_errors;
 
 866     if (mp->tx_bad_runt) {
 
 868     } else if (i != mp->tx_fill) {
 
 869         dev_kfree_skb(mp->tx_bufs[i]);
 
 870         if (++i >= N_TX_RING)
 
 875     netif_wake_queue(dev);
 
 876     if (i != mp->tx_fill) {
 
 877         cp = mp->tx_cmds + NCMDS_TX * i;
 
 878         out_le16(&cp->xfer_status, 0);
 
 879         out_le16(&cp->command, OUTPUT_LAST);
 
 880         out_le32(&td->cmdptr, virt_to_bus(cp));
 
 881         out_le32(&td->control, (RUN << 16) | RUN);
 
 883         mace_set_timeout(dev);
 
 886     /* turn it back on */
 
 887     out_8(&mb->imr, RCVINT);
 
 888     out_8(&mb->maccc, mp->maccc);
 
 891     spin_unlock_irqrestore(&mp->lock, flags);
 
 894 static irqreturn_t mace_txdma_intr(int irq, void *dev_id, struct pt_regs *regs)
 
 899 static irqreturn_t mace_rxdma_intr(int irq, void *dev_id, struct pt_regs *regs)
 
 901     struct net_device *dev = (struct net_device *) dev_id;
 
 902     struct mace_data *mp = (struct mace_data *) dev->priv;
 
 903     volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
 
 904     volatile struct dbdma_cmd *cp, *np;
 
 905     int i, nb, stat, next;
 
 907     unsigned frame_status;
 
 908     static int mace_lost_status;
 
 912     spin_lock_irqsave(&mp->lock, flags);
 
 913     for (i = mp->rx_empty; i != mp->rx_fill; ) {
 
 914         cp = mp->rx_cmds + i;
 
 915         stat = ld_le16(&cp->xfer_status);
 
 916         if ((stat & ACTIVE) == 0) {
 
 918             if (next >= N_RX_RING)
 
 920             np = mp->rx_cmds + next;
 
 921             if (next != mp->rx_fill
 
 922                 && (ld_le16(&np->xfer_status) & ACTIVE) != 0) {
 
 923                 printk(KERN_DEBUG "mace: lost a status word\n");
 
 928         nb = ld_le16(&cp->req_count) - ld_le16(&cp->res_count);
 
 929         out_le16(&cp->command, DBDMA_STOP);
 
 930         /* got a packet, have a look at it */
 
 931         skb = mp->rx_bufs[i];
 
 933             ++mp->stats.rx_dropped;
 
 936             frame_status = (data[nb-3] << 8) + data[nb-4];
 
 937             if (frame_status & (RS_OFLO|RS_CLSN|RS_FRAMERR|RS_FCSERR)) {
 
 938                 ++mp->stats.rx_errors;
 
 939                 if (frame_status & RS_OFLO)
 
 940                     ++mp->stats.rx_over_errors;
 
 941                 if (frame_status & RS_FRAMERR)
 
 942                     ++mp->stats.rx_frame_errors;
 
 943                 if (frame_status & RS_FCSERR)
 
 944                     ++mp->stats.rx_crc_errors;
 
 946                 /* Mace feature AUTO_STRIP_RCV is on by default, dropping the
 
 947                  * FCS on frames with 802.3 headers. This means that Ethernet
 
 948                  * frames have 8 extra octets at the end, while 802.3 frames
 
 949                  * have only 4. We need to correctly account for this. */
 
 950                 if (*(unsigned short *)(data+12) < 1536) /* 802.3 header */
 
 952                 else    /* Ethernet header; mace includes FCS */
 
 956                 skb->protocol = eth_type_trans(skb, dev);
 
 957                 mp->stats.rx_bytes += skb->len;
 
 959                 dev->last_rx = jiffies;
 
 960                 mp->rx_bufs[i] = NULL;
 
 961                 ++mp->stats.rx_packets;
 
 964             ++mp->stats.rx_errors;
 
 965             ++mp->stats.rx_length_errors;
 
 968         /* advance to next */
 
 969         if (++i >= N_RX_RING)
 
 977         if (next >= N_RX_RING)
 
 979         if (next == mp->rx_empty)
 
 981         cp = mp->rx_cmds + i;
 
 982         skb = mp->rx_bufs[i];
 
 984             skb = dev_alloc_skb(RX_BUFLEN + 2);
 
 987                 mp->rx_bufs[i] = skb;
 
 990         st_le16(&cp->req_count, RX_BUFLEN);
 
 991         data = skb? skb->data: dummy_buf;
 
 992         st_le32(&cp->phy_addr, virt_to_bus(data));
 
 993         out_le16(&cp->xfer_status, 0);
 
 994         out_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
 
 996         if ((ld_le32(&rd->status) & ACTIVE) != 0) {
 
 997             out_le32(&rd->control, (PAUSE << 16) | PAUSE);
 
 998             while ((in_le32(&rd->status) & ACTIVE) != 0)
 
1004     if (i != mp->rx_fill) {
 
1005         out_le32(&rd->control, ((RUN|WAKE) << 16) | (RUN|WAKE));
 
1008     spin_unlock_irqrestore(&mp->lock, flags);
 
1012 static struct of_device_id mace_match[] = 
 
1020 static struct macio_driver mace_driver = 
 
1023         .match_table    = mace_match,
 
1024         .probe          = mace_probe,
 
1025         .remove         = mace_remove,
 
1029 static int __init mace_init(void)
 
1031         return macio_register_driver(&mace_driver);
 
1034 static void __exit mace_cleanup(void)
 
1036         macio_unregister_driver(&mace_driver);
 
1044 MODULE_AUTHOR("Paul Mackerras");
 
1045 MODULE_DESCRIPTION("PowerMac MACE driver.");
 
1046 MODULE_PARM(port_aaui, "i");
 
1047 MODULE_PARM_DESC(port_aaui, "MACE uses AAUI port (0-1)");
 
1048 MODULE_LICENSE("GPL");
 
1050 module_init(mace_init);
 
1051 module_exit(mace_cleanup);