linux-bk/drivers/net/mace.c
<<
>>
Prefs
   1/*
   2 * Network device driver for the MACE ethernet controller on
   3 * Apple Powermacs.  Assumes it's under a DBDMA controller.
   4 *
   5 * Copyright (C) 1996 Paul Mackerras.
   6 */
   7
   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>
  19#include <asm/prom.h>
  20#include <asm/dbdma.h>
  21#include <asm/io.h>
  22#include <asm/pgtable.h>
  23#include "mace.h"
  24
  25static struct net_device *mace_devs;
  26static int port_aaui = -1;
  27
  28#define N_RX_RING       8
  29#define N_TX_RING       6
  30#define MAX_TX_ACTIVE   1
  31#define NCMDS_TX        1       /* dma commands per element in tx ring */
  32#define RX_BUFLEN       (ETH_FRAME_LEN + 8)
  33#define TX_TIMEOUT      HZ      /* 1 second */
  34
  35/* Chip rev needs workaround on HW & multicast addr change */
  36#define BROKEN_ADDRCHG_REV      0x0941
  37
  38/* Bits in transmit DMA status */
  39#define TX_DMA_ERR      0x80
  40
  41struct mace_data {
  42    volatile struct mace *mace;
  43    volatile struct dbdma_regs *tx_dma;
  44    int tx_dma_intr;
  45    volatile struct dbdma_regs *rx_dma;
  46    int rx_dma_intr;
  47    volatile struct dbdma_cmd *tx_cmds; /* xmit dma command list */
  48    volatile struct dbdma_cmd *rx_cmds; /* recv dma command list */
  49    struct sk_buff *rx_bufs[N_RX_RING];
  50    int rx_fill;
  51    int rx_empty;
  52    struct sk_buff *tx_bufs[N_TX_RING];
  53    int tx_fill;
  54    int tx_empty;
  55    unsigned char maccc;
  56    unsigned char tx_fullup;
  57    unsigned char tx_active;
  58    unsigned char tx_bad_runt;
  59    struct net_device_stats stats;
  60    struct timer_list tx_timeout;
  61    int timeout_active;
  62    int port_aaui;
  63    int chipid;
  64    struct device_node* of_node;
  65    struct net_device *next_mace;
  66    spinlock_t lock;
  67};
  68
  69/*
  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.
  74 */
  75#define PRIV_BYTES      (sizeof(struct mace_data) \
  76        + (N_RX_RING + NCMDS_TX * N_TX_RING + 3) * sizeof(struct dbdma_cmd))
  77
  78static int bitrev(int);
  79static int mace_probe(void);
  80static void mace_probe1(struct device_node *mace);
  81static int mace_open(struct net_device *dev);
  82static int mace_close(struct net_device *dev);
  83static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev);
  84static struct net_device_stats *mace_stats(struct net_device *dev);
  85static void mace_set_multicast(struct net_device *dev);
  86static void mace_reset(struct net_device *dev);
  87static int mace_set_address(struct net_device *dev, void *addr);
  88static irqreturn_t mace_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  89static irqreturn_t mace_txdma_intr(int irq, void *dev_id, struct pt_regs *regs);
  90static irqreturn_t mace_rxdma_intr(int irq, void *dev_id, struct pt_regs *regs);
  91static void mace_set_timeout(struct net_device *dev);
  92static void mace_tx_timeout(unsigned long data);
  93static inline void dbdma_reset(volatile struct dbdma_regs *dma);
  94static inline void mace_clean_rings(struct mace_data *mp);
  95static void __mace_set_address(struct net_device *dev, void *addr);
  96
  97/*
  98 * If we can't get a skbuff when we need it, we use this area for DMA.
  99 */
 100static unsigned char *dummy_buf;
 101
 102/* Bit-reverse one byte of an ethernet hardware address. */
 103static inline int
 104bitrev(int b)
 105{
 106    int d = 0, i;
 107
 108    for (i = 0; i < 8; ++i, b >>= 1)
 109        d = (d << 1) | (b & 1);
 110    return d;
 111}
 112
 113static int __init mace_probe(void)
 114{
 115        struct device_node *mace;
 116
 117        for (mace = find_devices("mace"); mace != NULL; mace = mace->next)
 118                mace_probe1(mace);
 119        return mace_devs? 0: -ENODEV;
 120}
 121
 122static void __init mace_probe1(struct device_node *mace)
 123{
 124        int j, rev;
 125        struct net_device *dev;
 126        struct mace_data *mp;
 127        unsigned char *addr;
 128
 129        if (mace->n_addrs != 3 || mace->n_intrs != 3) {
 130                printk(KERN_ERR "can't use MACE %s: need 3 addrs and 3 irqs\n",
 131                       mace->full_name);
 132                return;
 133        }
 134
 135        addr = get_property(mace, "mac-address", NULL);
 136        if (addr == NULL) {
 137                addr = get_property(mace, "local-mac-address", NULL);
 138                if (addr == NULL) {
 139                        printk(KERN_ERR "Can't get mac-address for MACE %s\n",
 140                               mace->full_name);
 141                        return;
 142                }
 143        }
 144
 145        if (dummy_buf == NULL) {
 146                dummy_buf = kmalloc(RX_BUFLEN+2, GFP_KERNEL);
 147                if (dummy_buf == NULL) {
 148                        printk(KERN_ERR "MACE: couldn't allocate dummy buffer\n");
 149                        return;
 150                }
 151        }
 152
 153        dev = init_etherdev(0, PRIV_BYTES);
 154        if (!dev)
 155                return;
 156        SET_MODULE_OWNER(dev);
 157
 158        mp = dev->priv;
 159        mp->of_node = mace;
 160        
 161        if (!request_OF_resource(mace, 0, " (mace)")) {
 162                printk(KERN_ERR "MACE: can't request IO resource !\n");
 163                goto err_out;
 164        }
 165        if (!request_OF_resource(mace, 1, " (mace tx dma)")) {
 166                printk(KERN_ERR "MACE: can't request TX DMA resource !\n");
 167                goto err_out;
 168        }
 169
 170        if (!request_OF_resource(mace, 2, " (mace tx dma)")) {
 171                printk(KERN_ERR "MACE: can't request RX DMA resource !\n");
 172                goto err_out;
 173        }
 174
 175        dev->base_addr = mace->addrs[0].address;
 176        mp->mace = (volatile struct mace *)
 177                ioremap(mace->addrs[0].address, 0x1000);
 178        dev->irq = mace->intrs[0].line;
 179
 180        printk(KERN_INFO "%s: MACE at", dev->name);
 181        rev = addr[0] == 0 && addr[1] == 0xA0;
 182        for (j = 0; j < 6; ++j) {
 183                dev->dev_addr[j] = rev? bitrev(addr[j]): addr[j];
 184                printk("%c%.2x", (j? ':': ' '), dev->dev_addr[j]);
 185        }
 186        mp->chipid = (in_8(&mp->mace->chipid_hi) << 8) |
 187                        in_8(&mp->mace->chipid_lo);
 188        printk(", chip revision %d.%d\n", mp->chipid >> 8, mp->chipid & 0xff);
 189                
 190
 191        mp = (struct mace_data *) dev->priv;
 192        mp->maccc = ENXMT | ENRCV;
 193        mp->tx_dma = (volatile struct dbdma_regs *)
 194                ioremap(mace->addrs[1].address, 0x1000);
 195        mp->tx_dma_intr = mace->intrs[1].line;
 196        mp->rx_dma = (volatile struct dbdma_regs *)
 197                ioremap(mace->addrs[2].address, 0x1000);
 198        mp->rx_dma_intr = mace->intrs[2].line;
 199
 200        mp->tx_cmds = (volatile struct dbdma_cmd *) DBDMA_ALIGN(mp + 1);
 201        mp->rx_cmds = mp->tx_cmds + NCMDS_TX * N_TX_RING + 1;
 202
 203        memset(&mp->stats, 0, sizeof(mp->stats));
 204        memset((char *) mp->tx_cmds, 0,
 205               (NCMDS_TX*N_TX_RING + N_RX_RING + 2) * sizeof(struct dbdma_cmd));
 206        init_timer(&mp->tx_timeout);
 207        spin_lock_init(&mp->lock);
 208        mp->timeout_active = 0;
 209
 210        if (port_aaui >= 0)
 211                mp->port_aaui = port_aaui;
 212        else {
 213                /* Apple Network Server uses the AAUI port */
 214                if (machine_is_compatible("AAPL,ShinerESB"))
 215                        mp->port_aaui = 1;
 216                else {
 217#ifdef CONFIG_MACE_AAUI_PORT
 218                        mp->port_aaui = 1;
 219#else
 220                        mp->port_aaui = 0;
 221#endif                  
 222                }
 223        }
 224
 225        dev->open = mace_open;
 226        dev->stop = mace_close;
 227        dev->hard_start_xmit = mace_xmit_start;
 228        dev->get_stats = mace_stats;
 229        dev->set_multicast_list = mace_set_multicast;
 230        dev->set_mac_address = mace_set_address;
 231
 232        ether_setup(dev);
 233
 234        mace_reset(dev);
 235
 236        if (request_irq(dev->irq, mace_interrupt, 0, "MACE", dev))
 237                printk(KERN_ERR "MACE: can't get irq %d\n", dev->irq);
 238        if (request_irq(mace->intrs[1].line, mace_txdma_intr, 0, "MACE-txdma",
 239                        dev))
 240                printk(KERN_ERR "MACE: can't get irq %d\n", mace->intrs[1].line);
 241        if (request_irq(mace->intrs[2].line, mace_rxdma_intr, 0, "MACE-rxdma",
 242                        dev))
 243                printk(KERN_ERR "MACE: can't get irq %d\n", mace->intrs[2].line);
 244
 245        mp->next_mace = mace_devs;
 246        mace_devs = dev;
 247        return;
 248        
 249err_out:
 250        unregister_netdev(dev);
 251        if (mp->of_node) {
 252                release_OF_resource(mp->of_node, 0);
 253                release_OF_resource(mp->of_node, 1);
 254                release_OF_resource(mp->of_node, 2);
 255        }
 256        free_netdev(dev);
 257}
 258
 259static void dbdma_reset(volatile struct dbdma_regs *dma)
 260{
 261    int i;
 262
 263    out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16);
 264
 265    /*
 266     * Yes this looks peculiar, but apparently it needs to be this
 267     * way on some machines.
 268     */
 269    for (i = 200; i > 0; --i)
 270        if (ld_le32(&dma->control) & RUN)
 271            udelay(1);
 272}
 273
 274static void mace_reset(struct net_device *dev)
 275{
 276    struct mace_data *mp = (struct mace_data *) dev->priv;
 277    volatile struct mace *mb = mp->mace;
 278    int i;
 279
 280    /* soft-reset the chip */
 281    i = 200;
 282    while (--i) {
 283        out_8(&mb->biucc, SWRST);
 284        if (in_8(&mb->biucc) & SWRST) {
 285            udelay(10);
 286            continue;
 287        }
 288        break;
 289    }
 290    if (!i) {
 291        printk(KERN_ERR "mace: cannot reset chip!\n");
 292        return;
 293    }
 294
 295    out_8(&mb->imr, 0xff);      /* disable all intrs for now */
 296    i = in_8(&mb->ir);
 297    out_8(&mb->maccc, 0);       /* turn off tx, rx */
 298
 299    out_8(&mb->biucc, XMTSP_64);
 300    out_8(&mb->utr, RTRD);
 301    out_8(&mb->fifocc, RCVFW_32 | XMTFW_16 | XMTFWU | RCVFWU | XMTBRST);
 302    out_8(&mb->xmtfc, AUTO_PAD_XMIT); /* auto-pad short frames */
 303    out_8(&mb->rcvfc, 0);
 304
 305    /* load up the hardware address */
 306    __mace_set_address(dev, dev->dev_addr);
 307
 308    /* clear the multicast filter */
 309    if (mp->chipid == BROKEN_ADDRCHG_REV)
 310        out_8(&mb->iac, LOGADDR);
 311    else {
 312        out_8(&mb->iac, ADDRCHG | LOGADDR);
 313        while ((in_8(&mb->iac) & ADDRCHG) != 0)
 314                ;
 315    }
 316    for (i = 0; i < 8; ++i)
 317        out_8(&mb->ladrf, 0);
 318
 319    /* done changing address */
 320    if (mp->chipid != BROKEN_ADDRCHG_REV)
 321        out_8(&mb->iac, 0);
 322
 323    if (mp->port_aaui)
 324        out_8(&mb->plscc, PORTSEL_AUI + ENPLSIO);
 325    else
 326        out_8(&mb->plscc, PORTSEL_GPSI + ENPLSIO);
 327}
 328
 329static void __mace_set_address(struct net_device *dev, void *addr)
 330{
 331    struct mace_data *mp = (struct mace_data *) dev->priv;
 332    volatile struct mace *mb = mp->mace;
 333    unsigned char *p = addr;
 334    int i;
 335
 336    /* load up the hardware address */
 337    if (mp->chipid == BROKEN_ADDRCHG_REV)
 338        out_8(&mb->iac, PHYADDR);
 339    else {
 340        out_8(&mb->iac, ADDRCHG | PHYADDR);
 341        while ((in_8(&mb->iac) & ADDRCHG) != 0)
 342            ;
 343    }
 344    for (i = 0; i < 6; ++i)
 345        out_8(&mb->padr, dev->dev_addr[i] = p[i]);
 346    if (mp->chipid != BROKEN_ADDRCHG_REV)
 347        out_8(&mb->iac, 0);
 348}
 349
 350static int mace_set_address(struct net_device *dev, void *addr)
 351{
 352    struct mace_data *mp = (struct mace_data *) dev->priv;
 353    volatile struct mace *mb = mp->mace;
 354    unsigned long flags;
 355
 356    spin_lock_irqsave(&mp->lock, flags);
 357
 358    __mace_set_address(dev, addr);
 359
 360    /* note: setting ADDRCHG clears ENRCV */
 361    out_8(&mb->maccc, mp->maccc);
 362
 363    spin_unlock_irqrestore(&mp->lock, flags);
 364    return 0;
 365}
 366
 367static inline void mace_clean_rings(struct mace_data *mp)
 368{
 369    int i;
 370
 371    /* free some skb's */
 372    for (i = 0; i < N_RX_RING; ++i) {
 373        if (mp->rx_bufs[i] != 0) {
 374            dev_kfree_skb(mp->rx_bufs[i]);
 375            mp->rx_bufs[i] = 0;
 376        }
 377    }
 378    for (i = mp->tx_empty; i != mp->tx_fill; ) {
 379        dev_kfree_skb(mp->tx_bufs[i]);
 380        if (++i >= N_TX_RING)
 381            i = 0;
 382    }
 383}
 384
 385static int mace_open(struct net_device *dev)
 386{
 387    struct mace_data *mp = (struct mace_data *) dev->priv;
 388    volatile struct mace *mb = mp->mace;
 389    volatile struct dbdma_regs *rd = mp->rx_dma;
 390    volatile struct dbdma_regs *td = mp->tx_dma;
 391    volatile struct dbdma_cmd *cp;
 392    int i;
 393    struct sk_buff *skb;
 394    unsigned char *data;
 395
 396    /* reset the chip */
 397    mace_reset(dev);
 398
 399    /* initialize list of sk_buffs for receiving and set up recv dma */
 400    mace_clean_rings(mp);
 401    memset((char *)mp->rx_cmds, 0, N_RX_RING * sizeof(struct dbdma_cmd));
 402    cp = mp->rx_cmds;
 403    for (i = 0; i < N_RX_RING - 1; ++i) {
 404        skb = dev_alloc_skb(RX_BUFLEN + 2);
 405        if (skb == 0) {
 406            data = dummy_buf;
 407        } else {
 408            skb_reserve(skb, 2);        /* so IP header lands on 4-byte bdry */
 409            data = skb->data;
 410        }
 411        mp->rx_bufs[i] = skb;
 412        st_le16(&cp->req_count, RX_BUFLEN);
 413        st_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
 414        st_le32(&cp->phy_addr, virt_to_bus(data));
 415        cp->xfer_status = 0;
 416        ++cp;
 417    }
 418    mp->rx_bufs[i] = 0;
 419    st_le16(&cp->command, DBDMA_STOP);
 420    mp->rx_fill = i;
 421    mp->rx_empty = 0;
 422
 423    /* Put a branch back to the beginning of the receive command list */
 424    ++cp;
 425    st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
 426    st_le32(&cp->cmd_dep, virt_to_bus(mp->rx_cmds));
 427
 428    /* start rx dma */
 429    out_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
 430    out_le32(&rd->cmdptr, virt_to_bus(mp->rx_cmds));
 431    out_le32(&rd->control, (RUN << 16) | RUN);
 432
 433    /* put a branch at the end of the tx command list */
 434    cp = mp->tx_cmds + NCMDS_TX * N_TX_RING;
 435    st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
 436    st_le32(&cp->cmd_dep, virt_to_bus(mp->tx_cmds));
 437
 438    /* reset tx dma */
 439    out_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
 440    out_le32(&td->cmdptr, virt_to_bus(mp->tx_cmds));
 441    mp->tx_fill = 0;
 442    mp->tx_empty = 0;
 443    mp->tx_fullup = 0;
 444    mp->tx_active = 0;
 445    mp->tx_bad_runt = 0;
 446
 447    /* turn it on! */
 448    out_8(&mb->maccc, mp->maccc);
 449    /* enable all interrupts except receive interrupts */
 450    out_8(&mb->imr, RCVINT);
 451
 452    return 0;
 453}
 454
 455static int mace_close(struct net_device *dev)
 456{
 457    struct mace_data *mp = (struct mace_data *) dev->priv;
 458    volatile struct mace *mb = mp->mace;
 459    volatile struct dbdma_regs *rd = mp->rx_dma;
 460    volatile struct dbdma_regs *td = mp->tx_dma;
 461
 462    /* disable rx and tx */
 463    out_8(&mb->maccc, 0);
 464    out_8(&mb->imr, 0xff);              /* disable all intrs */
 465
 466    /* disable rx and tx dma */
 467    st_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
 468    st_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
 469
 470    mace_clean_rings(mp);
 471
 472    return 0;
 473}
 474
 475static inline void mace_set_timeout(struct net_device *dev)
 476{
 477    struct mace_data *mp = (struct mace_data *) dev->priv;
 478
 479    if (mp->timeout_active)
 480        del_timer(&mp->tx_timeout);
 481    mp->tx_timeout.expires = jiffies + TX_TIMEOUT;
 482    mp->tx_timeout.function = mace_tx_timeout;
 483    mp->tx_timeout.data = (unsigned long) dev;
 484    add_timer(&mp->tx_timeout);
 485    mp->timeout_active = 1;
 486}
 487
 488static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev)
 489{
 490    struct mace_data *mp = (struct mace_data *) dev->priv;
 491    volatile struct dbdma_regs *td = mp->tx_dma;
 492    volatile struct dbdma_cmd *cp, *np;
 493    unsigned long flags;
 494    int fill, next, len;
 495
 496    /* see if there's a free slot in the tx ring */
 497    spin_lock_irqsave(&mp->lock, flags);
 498    fill = mp->tx_fill;
 499    next = fill + 1;
 500    if (next >= N_TX_RING)
 501        next = 0;
 502    if (next == mp->tx_empty) {
 503        netif_stop_queue(dev);
 504        mp->tx_fullup = 1;
 505        spin_unlock_irqrestore(&mp->lock, flags);
 506        return 1;               /* can't take it at the moment */
 507    }
 508    spin_unlock_irqrestore(&mp->lock, flags);
 509
 510    /* partially fill in the dma command block */
 511    len = skb->len;
 512    if (len > ETH_FRAME_LEN) {
 513        printk(KERN_DEBUG "mace: xmit frame too long (%d)\n", len);
 514        len = ETH_FRAME_LEN;
 515    }
 516    mp->tx_bufs[fill] = skb;
 517    cp = mp->tx_cmds + NCMDS_TX * fill;
 518    st_le16(&cp->req_count, len);
 519    st_le32(&cp->phy_addr, virt_to_bus(skb->data));
 520
 521    np = mp->tx_cmds + NCMDS_TX * next;
 522    out_le16(&np->command, DBDMA_STOP);
 523
 524    /* poke the tx dma channel */
 525    spin_lock_irqsave(&mp->lock, flags);
 526    mp->tx_fill = next;
 527    if (!mp->tx_bad_runt && mp->tx_active < MAX_TX_ACTIVE) {
 528        out_le16(&cp->xfer_status, 0);
 529        out_le16(&cp->command, OUTPUT_LAST);
 530        out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
 531        ++mp->tx_active;
 532        mace_set_timeout(dev);
 533    }
 534    if (++next >= N_TX_RING)
 535        next = 0;
 536    if (next == mp->tx_empty)
 537        netif_stop_queue(dev);
 538    spin_unlock_irqrestore(&mp->lock, flags);
 539
 540    return 0;
 541}
 542
 543static struct net_device_stats *mace_stats(struct net_device *dev)
 544{
 545    struct mace_data *p = (struct mace_data *) dev->priv;
 546
 547    return &p->stats;
 548}
 549
 550static void mace_set_multicast(struct net_device *dev)
 551{
 552    struct mace_data *mp = (struct mace_data *) dev->priv;
 553    volatile struct mace *mb = mp->mace;
 554    int i, j;
 555    u32 crc;
 556    unsigned long flags;
 557
 558    spin_lock_irqsave(&mp->lock, flags);
 559    mp->maccc &= ~PROM;
 560    if (dev->flags & IFF_PROMISC) {
 561        mp->maccc |= PROM;
 562    } else {
 563        unsigned char multicast_filter[8];
 564        struct dev_mc_list *dmi = dev->mc_list;
 565
 566        if (dev->flags & IFF_ALLMULTI) {
 567            for (i = 0; i < 8; i++)
 568                multicast_filter[i] = 0xff;
 569        } else {
 570            for (i = 0; i < 8; i++)
 571                multicast_filter[i] = 0;
 572            for (i = 0; i < dev->mc_count; i++) {
 573                crc = ether_crc_le(6, dmi->dmi_addr);
 574                j = crc >> 26;  /* bit number in multicast_filter */
 575                multicast_filter[j >> 3] |= 1 << (j & 7);
 576                dmi = dmi->next;
 577            }
 578        }
 579#if 0
 580        printk("Multicast filter :");
 581        for (i = 0; i < 8; i++)
 582            printk("%02x ", multicast_filter[i]);
 583        printk("\n");
 584#endif
 585
 586        if (mp->chipid == BROKEN_ADDRCHG_REV)
 587            out_8(&mb->iac, LOGADDR);
 588        else {
 589            out_8(&mb->iac, ADDRCHG | LOGADDR);
 590            while ((in_8(&mb->iac) & ADDRCHG) != 0)
 591                ;
 592        }
 593        for (i = 0; i < 8; ++i)
 594            out_8(&mb->ladrf, multicast_filter[i]);
 595        if (mp->chipid != BROKEN_ADDRCHG_REV)
 596            out_8(&mb->iac, 0);
 597    }
 598    /* reset maccc */
 599    out_8(&mb->maccc, mp->maccc);
 600    spin_unlock_irqrestore(&mp->lock, flags);
 601}
 602
 603static void mace_handle_misc_intrs(struct mace_data *mp, int intr)
 604{
 605    volatile struct mace *mb = mp->mace;
 606    static int mace_babbles, mace_jabbers;
 607
 608    if (intr & MPCO)
 609        mp->stats.rx_missed_errors += 256;
 610    mp->stats.rx_missed_errors += in_8(&mb->mpc);   /* reading clears it */
 611    if (intr & RNTPCO)
 612        mp->stats.rx_length_errors += 256;
 613    mp->stats.rx_length_errors += in_8(&mb->rntpc); /* reading clears it */
 614    if (intr & CERR)
 615        ++mp->stats.tx_heartbeat_errors;
 616    if (intr & BABBLE)
 617        if (mace_babbles++ < 4)
 618            printk(KERN_DEBUG "mace: babbling transmitter\n");
 619    if (intr & JABBER)
 620        if (mace_jabbers++ < 4)
 621            printk(KERN_DEBUG "mace: jabbering transceiver\n");
 622}
 623
 624static irqreturn_t mace_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 625{
 626    struct net_device *dev = (struct net_device *) dev_id;
 627    struct mace_data *mp = (struct mace_data *) dev->priv;
 628    volatile struct mace *mb = mp->mace;
 629    volatile struct dbdma_regs *td = mp->tx_dma;
 630    volatile struct dbdma_cmd *cp;
 631    int intr, fs, i, stat, x;
 632    int xcount, dstat;
 633    unsigned long flags;
 634    /* static int mace_last_fs, mace_last_xcount; */
 635
 636    spin_lock_irqsave(&mp->lock, flags);
 637    intr = in_8(&mb->ir);               /* read interrupt register */
 638    in_8(&mb->xmtrc);                   /* get retries */
 639    mace_handle_misc_intrs(mp, intr);
 640
 641    i = mp->tx_empty;
 642    while (in_8(&mb->pr) & XMTSV) {
 643        del_timer(&mp->tx_timeout);
 644        mp->timeout_active = 0;
 645        /*
 646         * Clear any interrupt indication associated with this status
 647         * word.  This appears to unlatch any error indication from
 648         * the DMA controller.
 649         */
 650        intr = in_8(&mb->ir);
 651        if (intr != 0)
 652            mace_handle_misc_intrs(mp, intr);
 653        if (mp->tx_bad_runt) {
 654            fs = in_8(&mb->xmtfs);
 655            mp->tx_bad_runt = 0;
 656            out_8(&mb->xmtfc, AUTO_PAD_XMIT);
 657            continue;
 658        }
 659        dstat = ld_le32(&td->status);
 660        /* stop DMA controller */
 661        out_le32(&td->control, RUN << 16);
 662        /*
 663         * xcount is the number of complete frames which have been
 664         * written to the fifo but for which status has not been read.
 665         */
 666        xcount = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
 667        if (xcount == 0 || (dstat & DEAD)) {
 668            /*
 669             * If a packet was aborted before the DMA controller has
 670             * finished transferring it, it seems that there are 2 bytes
 671             * which are stuck in some buffer somewhere.  These will get
 672             * transmitted as soon as we read the frame status (which
 673             * reenables the transmit data transfer request).  Turning
 674             * off the DMA controller and/or resetting the MACE doesn't
 675             * help.  So we disable auto-padding and FCS transmission
 676             * so the two bytes will only be a runt packet which should
 677             * be ignored by other stations.
 678             */
 679            out_8(&mb->xmtfc, DXMTFCS);
 680        }
 681        fs = in_8(&mb->xmtfs);
 682        if ((fs & XMTSV) == 0) {
 683            printk(KERN_ERR "mace: xmtfs not valid! (fs=%x xc=%d ds=%x)\n",
 684                   fs, xcount, dstat);
 685            mace_reset(dev);
 686                /*
 687                 * XXX mace likes to hang the machine after a xmtfs error.
 688                 * This is hard to reproduce, reseting *may* help
 689                 */
 690        }
 691        cp = mp->tx_cmds + NCMDS_TX * i;
 692        stat = ld_le16(&cp->xfer_status);
 693        if ((fs & (UFLO|LCOL|LCAR|RTRY)) || (dstat & DEAD) || xcount == 0) {
 694            /*
 695             * Check whether there were in fact 2 bytes written to
 696             * the transmit FIFO.
 697             */
 698            udelay(1);
 699            x = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
 700            if (x != 0) {
 701                /* there were two bytes with an end-of-packet indication */
 702                mp->tx_bad_runt = 1;
 703                mace_set_timeout(dev);
 704            } else {
 705                /*
 706                 * Either there weren't the two bytes buffered up, or they
 707                 * didn't have an end-of-packet indication.
 708                 * We flush the transmit FIFO just in case (by setting the
 709                 * XMTFWU bit with the transmitter disabled).
 710                 */
 711                out_8(&mb->maccc, in_8(&mb->maccc) & ~ENXMT);
 712                out_8(&mb->fifocc, in_8(&mb->fifocc) | XMTFWU);
 713                udelay(1);
 714                out_8(&mb->maccc, in_8(&mb->maccc) | ENXMT);
 715                out_8(&mb->xmtfc, AUTO_PAD_XMIT);
 716            }
 717        }
 718        /* dma should have finished */
 719        if (i == mp->tx_fill) {
 720            printk(KERN_DEBUG "mace: tx ring ran out? (fs=%x xc=%d ds=%x)\n",
 721                   fs, xcount, dstat);
 722            continue;
 723        }
 724        /* Update stats */
 725        if (fs & (UFLO|LCOL|LCAR|RTRY)) {
 726            ++mp->stats.tx_errors;
 727            if (fs & LCAR)
 728                ++mp->stats.tx_carrier_errors;
 729            if (fs & (UFLO|LCOL|RTRY))
 730                ++mp->stats.tx_aborted_errors;
 731        } else {
 732            mp->stats.tx_bytes += mp->tx_bufs[i]->len;
 733            ++mp->stats.tx_packets;
 734        }
 735        dev_kfree_skb_irq(mp->tx_bufs[i]);
 736        --mp->tx_active;
 737        if (++i >= N_TX_RING)
 738            i = 0;
 739#if 0
 740        mace_last_fs = fs;
 741        mace_last_xcount = xcount;
 742#endif
 743    }
 744
 745    if (i != mp->tx_empty) {
 746        mp->tx_fullup = 0;
 747        netif_wake_queue(dev);
 748    }
 749    mp->tx_empty = i;
 750    i += mp->tx_active;
 751    if (i >= N_TX_RING)
 752        i -= N_TX_RING;
 753    if (!mp->tx_bad_runt && i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE) {
 754        do {
 755            /* set up the next one */
 756            cp = mp->tx_cmds + NCMDS_TX * i;
 757            out_le16(&cp->xfer_status, 0);
 758            out_le16(&cp->command, OUTPUT_LAST);
 759            ++mp->tx_active;
 760            if (++i >= N_TX_RING)
 761                i = 0;
 762        } while (i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE);
 763        out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
 764        mace_set_timeout(dev);
 765    }
 766    spin_unlock_irqrestore(&mp->lock, flags);
 767    return IRQ_HANDLED;
 768}
 769
 770static void mace_tx_timeout(unsigned long data)
 771{
 772    struct net_device *dev = (struct net_device *) data;
 773    struct mace_data *mp = (struct mace_data *) dev->priv;
 774    volatile struct mace *mb = mp->mace;
 775    volatile struct dbdma_regs *td = mp->tx_dma;
 776    volatile struct dbdma_regs *rd = mp->rx_dma;
 777    volatile struct dbdma_cmd *cp;
 778    unsigned long flags;
 779    int i;
 780
 781    spin_lock_irqsave(&mp->lock, flags);
 782    mp->timeout_active = 0;
 783    if (mp->tx_active == 0 && !mp->tx_bad_runt)
 784        goto out;
 785
 786    /* update various counters */
 787    mace_handle_misc_intrs(mp, in_8(&mb->ir));
 788
 789    cp = mp->tx_cmds + NCMDS_TX * mp->tx_empty;
 790
 791    /* turn off both tx and rx and reset the chip */
 792    out_8(&mb->maccc, 0);
 793    printk(KERN_ERR "mace: transmit timeout - resetting\n");
 794    dbdma_reset(td);
 795    mace_reset(dev);
 796
 797    /* restart rx dma */
 798    cp = bus_to_virt(ld_le32(&rd->cmdptr));
 799    dbdma_reset(rd);
 800    out_le16(&cp->xfer_status, 0);
 801    out_le32(&rd->cmdptr, virt_to_bus(cp));
 802    out_le32(&rd->control, (RUN << 16) | RUN);
 803
 804    /* fix up the transmit side */
 805    i = mp->tx_empty;
 806    mp->tx_active = 0;
 807    ++mp->stats.tx_errors;
 808    if (mp->tx_bad_runt) {
 809        mp->tx_bad_runt = 0;
 810    } else if (i != mp->tx_fill) {
 811        dev_kfree_skb(mp->tx_bufs[i]);
 812        if (++i >= N_TX_RING)
 813            i = 0;
 814        mp->tx_empty = i;
 815    }
 816    mp->tx_fullup = 0;
 817    netif_wake_queue(dev);
 818    if (i != mp->tx_fill) {
 819        cp = mp->tx_cmds + NCMDS_TX * i;
 820        out_le16(&cp->xfer_status, 0);
 821        out_le16(&cp->command, OUTPUT_LAST);
 822        out_le32(&td->cmdptr, virt_to_bus(cp));
 823        out_le32(&td->control, (RUN << 16) | RUN);
 824        ++mp->tx_active;
 825        mace_set_timeout(dev);
 826    }
 827
 828    /* turn it back on */
 829    out_8(&mb->imr, RCVINT);
 830    out_8(&mb->maccc, mp->maccc);
 831
 832out:
 833    spin_unlock_irqrestore(&mp->lock, flags);
 834}
 835
 836static irqreturn_t mace_txdma_intr(int irq, void *dev_id, struct pt_regs *regs)
 837{
 838        return IRQ_HANDLED;
 839}
 840
 841static irqreturn_t mace_rxdma_intr(int irq, void *dev_id, struct pt_regs *regs)
 842{
 843    struct net_device *dev = (struct net_device *) dev_id;
 844    struct mace_data *mp = (struct mace_data *) dev->priv;
 845    volatile struct dbdma_regs *rd = mp->rx_dma;
 846    volatile struct dbdma_cmd *cp, *np;
 847    int i, nb, stat, next;
 848    struct sk_buff *skb;
 849    unsigned frame_status;
 850    static int mace_lost_status;
 851    unsigned char *data;
 852    unsigned long flags;
 853
 854    spin_lock_irqsave(&mp->lock, flags);
 855    for (i = mp->rx_empty; i != mp->rx_fill; ) {
 856        cp = mp->rx_cmds + i;
 857        stat = ld_le16(&cp->xfer_status);
 858        if ((stat & ACTIVE) == 0) {
 859            next = i + 1;
 860            if (next >= N_RX_RING)
 861                next = 0;
 862            np = mp->rx_cmds + next;
 863            if (next != mp->rx_fill
 864                && (ld_le16(&np->xfer_status) & ACTIVE) != 0) {
 865                printk(KERN_DEBUG "mace: lost a status word\n");
 866                ++mace_lost_status;
 867            } else
 868                break;
 869        }
 870        nb = ld_le16(&cp->req_count) - ld_le16(&cp->res_count);
 871        out_le16(&cp->command, DBDMA_STOP);
 872        /* got a packet, have a look at it */
 873        skb = mp->rx_bufs[i];
 874        if (skb == 0) {
 875            ++mp->stats.rx_dropped;
 876        } else if (nb > 8) {
 877            data = skb->data;
 878            frame_status = (data[nb-3] << 8) + data[nb-4];
 879            if (frame_status & (RS_OFLO|RS_CLSN|RS_FRAMERR|RS_FCSERR)) {
 880                ++mp->stats.rx_errors;
 881                if (frame_status & RS_OFLO)
 882                    ++mp->stats.rx_over_errors;
 883                if (frame_status & RS_FRAMERR)
 884                    ++mp->stats.rx_frame_errors;
 885                if (frame_status & RS_FCSERR)
 886                    ++mp->stats.rx_crc_errors;
 887            } else {
 888                /* Mace feature AUTO_STRIP_RCV is on by default, dropping the
 889                 * FCS on frames with 802.3 headers. This means that Ethernet
 890                 * frames have 8 extra octets at the end, while 802.3 frames
 891                 * have only 4. We need to correctly account for this. */
 892                if (*(unsigned short *)(data+12) < 1536) /* 802.3 header */
 893                    nb -= 4;
 894                else    /* Ethernet header; mace includes FCS */
 895                    nb -= 8;
 896                skb_put(skb, nb);
 897                skb->dev = dev;
 898                skb->protocol = eth_type_trans(skb, dev);
 899                mp->stats.rx_bytes += skb->len;
 900                netif_rx(skb);
 901                dev->last_rx = jiffies;
 902                mp->rx_bufs[i] = 0;
 903                ++mp->stats.rx_packets;
 904            }
 905        } else {
 906            ++mp->stats.rx_errors;
 907            ++mp->stats.rx_length_errors;
 908        }
 909
 910        /* advance to next */
 911        if (++i >= N_RX_RING)
 912            i = 0;
 913    }
 914    mp->rx_empty = i;
 915
 916    i = mp->rx_fill;
 917    for (;;) {
 918        next = i + 1;
 919        if (next >= N_RX_RING)
 920            next = 0;
 921        if (next == mp->rx_empty)
 922            break;
 923        cp = mp->rx_cmds + i;
 924        skb = mp->rx_bufs[i];
 925        if (skb == 0) {
 926            skb = dev_alloc_skb(RX_BUFLEN + 2);
 927            if (skb != 0) {
 928                skb_reserve(skb, 2);
 929                mp->rx_bufs[i] = skb;
 930            }
 931        }
 932        st_le16(&cp->req_count, RX_BUFLEN);
 933        data = skb? skb->data: dummy_buf;
 934        st_le32(&cp->phy_addr, virt_to_bus(data));
 935        out_le16(&cp->xfer_status, 0);
 936        out_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
 937#if 0
 938        if ((ld_le32(&rd->status) & ACTIVE) != 0) {
 939            out_le32(&rd->control, (PAUSE << 16) | PAUSE);
 940            while ((in_le32(&rd->status) & ACTIVE) != 0)
 941                ;
 942        }
 943#endif
 944        i = next;
 945    }
 946    if (i != mp->rx_fill) {
 947        out_le32(&rd->control, ((RUN|WAKE) << 16) | (RUN|WAKE));
 948        mp->rx_fill = i;
 949    }
 950    spin_unlock_irqrestore(&mp->lock, flags);
 951    return IRQ_HANDLED;
 952}
 953
 954MODULE_AUTHOR("Paul Mackerras");
 955MODULE_DESCRIPTION("PowerMac MACE driver.");
 956MODULE_PARM(port_aaui, "i");
 957MODULE_PARM_DESC(port_aaui, "MACE uses AAUI port (0-1)");
 958MODULE_LICENSE("GPL");
 959
 960static void __exit mace_cleanup (void)
 961{
 962    struct net_device *dev;
 963    struct mace_data *mp;
 964
 965    while ((dev = mace_devs) != 0) {
 966                mp = (struct mace_data *) mace_devs->priv;
 967                mace_devs = mp->next_mace;
 968
 969                unregister_netdev(dev);
 970                free_irq(dev->irq, dev);
 971                free_irq(mp->tx_dma_intr, dev);
 972                free_irq(mp->rx_dma_intr, dev);
 973
 974                release_OF_resource(mp->of_node, 0);
 975                release_OF_resource(mp->of_node, 1);
 976                release_OF_resource(mp->of_node, 2);
 977
 978                kfree(dev);
 979    }
 980    if (dummy_buf != NULL) {
 981                kfree(dummy_buf);
 982                dummy_buf = NULL;
 983    }
 984}
 985
 986module_init(mace_probe);
 987module_exit(mace_cleanup);
 988
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.