linux-bk/drivers/net/82596.c
<<
>>
Prefs
   1/* 82596.c: A generic 82596 ethernet driver for linux. */
   2/*
   3   Based on Apricot.c
   4   Written 1994 by Mark Evans.
   5   This driver is for the Apricot 82596 bus-master interface
   6
   7   Modularised 12/94 Mark Evans
   8
   9
  10   Modified to support the 82596 ethernet chips on 680x0 VME boards.
  11   by Richard Hirst <richard@sleepie.demon.co.uk>
  12   Renamed to be 82596.c
  13
  14   980825:  Changed to receive directly in to sk_buffs which are
  15   allocated at open() time.  Eliminates copy on incoming frames
  16   (small ones are still copied).  Shared data now held in a
  17   non-cached page, so we can run on 68060 in copyback mode.
  18
  19   TBD:
  20   * look at deferring rx frames rather than discarding (as per tulip)
  21   * handle tx ring full as per tulip
  22   * performace test to tune rx_copybreak
  23
  24   Most of my modifications relate to the braindead big-endian
  25   implementation by Intel.  When the i596 is operating in
  26   'big-endian' mode, it thinks a 32 bit value of 0x12345678
  27   should be stored as 0x56781234.  This is a real pain, when
  28   you have linked lists which are shared by the 680x0 and the
  29   i596.
  30
  31   Driver skeleton
  32   Written 1993 by Donald Becker.
  33   Copyright 1993 United States Government as represented by the Director,
  34   National Security Agency. This software may only be used and distributed
  35   according to the terms of the GNU General Public License as modified by SRC,
  36   incorporated herein by reference.
  37
  38   The author may be reached as becker@scyld.com, or C/O
  39   Scyld Computing Corporation, 410 Severn Ave., Suite 210, Annapolis MD 21403
  40
  41 */
  42
  43#include <linux/config.h>
  44#include <linux/module.h>
  45#include <linux/kernel.h>
  46#include <linux/string.h>
  47#include <linux/errno.h>
  48#include <linux/ioport.h>
  49#include <linux/slab.h>
  50#include <linux/interrupt.h>
  51#include <linux/delay.h>
  52#include <linux/netdevice.h>
  53#include <linux/etherdevice.h>
  54#include <linux/skbuff.h>
  55#include <linux/init.h>
  56
  57#include <asm/bitops.h>
  58#include <asm/io.h>
  59#include <asm/dma.h>
  60#include <asm/pgtable.h>
  61#include <asm/pgalloc.h>
  62
  63static char version[] __initdata =
  64        "82596.c $Revision: 1.5 $\n";
  65
  66/* DEBUG flags
  67 */
  68
  69#define DEB_INIT        0x0001
  70#define DEB_PROBE       0x0002
  71#define DEB_SERIOUS     0x0004
  72#define DEB_ERRORS      0x0008
  73#define DEB_MULTI       0x0010
  74#define DEB_TDR         0x0020
  75#define DEB_OPEN        0x0040
  76#define DEB_RESET       0x0080
  77#define DEB_ADDCMD      0x0100
  78#define DEB_STATUS      0x0200
  79#define DEB_STARTTX     0x0400
  80#define DEB_RXADDR      0x0800
  81#define DEB_TXADDR      0x1000
  82#define DEB_RXFRAME     0x2000
  83#define DEB_INTS        0x4000
  84#define DEB_STRUCT      0x8000
  85#define DEB_ANY         0xffff
  86
  87
  88#define DEB(x,y)        if (i596_debug & (x)) y
  89
  90
  91#if defined(CONFIG_MVME16x_NET) || defined(CONFIG_MVME16x_NET_MODULE)
  92#define ENABLE_MVME16x_NET
  93#endif
  94#if defined(CONFIG_BVME6000_NET) || defined(CONFIG_BVME6000_NET_MODULE)
  95#define ENABLE_BVME6000_NET
  96#endif
  97#if defined(CONFIG_APRICOT) || defined(CONFIG_APRICOT_MODULE)
  98#define ENABLE_APRICOT
  99#endif
 100
 101#ifdef ENABLE_MVME16x_NET
 102#include <asm/mvme16xhw.h>
 103#endif
 104#ifdef ENABLE_BVME6000_NET
 105#include <asm/bvme6000hw.h>
 106#endif
 107
 108/*
 109 * Define various macros for Channel Attention, word swapping etc., dependent
 110 * on architecture.  MVME and BVME are 680x0 based, otherwise it is Intel.
 111 */
 112
 113#ifdef __mc68000__
 114#define WSWAPrfd(x)  ((struct i596_rfd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
 115#define WSWAPrbd(x)  ((struct i596_rbd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
 116#define WSWAPiscp(x) ((struct i596_iscp *)(((u32)(x)<<16) | ((((u32)(x)))>>16)))
 117#define WSWAPscb(x)  ((struct i596_scb *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
 118#define WSWAPcmd(x)  ((struct i596_cmd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
 119#define WSWAPtbd(x)  ((struct i596_tbd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
 120#define WSWAPchar(x) ((char *)            (((u32)(x)<<16) | ((((u32)(x)))>>16)))
 121#define ISCP_BUSY       0x00010000
 122#define MACH_IS_APRICOT 0
 123#else
 124#define WSWAPrfd(x)     ((struct i596_rfd *)(x))
 125#define WSWAPrbd(x)     ((struct i596_rbd *)(x))
 126#define WSWAPiscp(x)    ((struct i596_iscp *)(x))
 127#define WSWAPscb(x)     ((struct i596_scb *)(x))
 128#define WSWAPcmd(x)     ((struct i596_cmd *)(x))
 129#define WSWAPtbd(x)     ((struct i596_tbd *)(x))
 130#define WSWAPchar(x)    ((char *)(x))
 131#define ISCP_BUSY       0x0001
 132#define MACH_IS_APRICOT 1
 133#endif
 134
 135/*
 136 * The MPU_PORT command allows direct access to the 82596. With PORT access
 137 * the following commands are available (p5-18). The 32-bit port command
 138 * must be word-swapped with the most significant word written first.
 139 * This only applies to VME boards.
 140 */
 141#define PORT_RESET              0x00    /* reset 82596 */
 142#define PORT_SELFTEST           0x01    /* selftest */
 143#define PORT_ALTSCP             0x02    /* alternate SCB address */
 144#define PORT_ALTDUMP            0x03    /* Alternate DUMP address */
 145
 146static int i596_debug = (DEB_SERIOUS|DEB_PROBE);
 147
 148MODULE_AUTHOR("Richard Hirst");
 149MODULE_DESCRIPTION("i82596 driver");
 150MODULE_LICENSE("GPL");
 151
 152MODULE_PARM(i596_debug, "i");
 153MODULE_PARM_DESC(i596_debug, "i82596 debug mask");
 154
 155
 156/* Copy frames shorter than rx_copybreak, otherwise pass on up in
 157 * a full sized sk_buff.  Value of 100 stolen from tulip.c (!alpha).
 158 */
 159static int rx_copybreak = 100;
 160
 161#define PKT_BUF_SZ      1536
 162#define MAX_MC_CNT      64
 163
 164#define I596_TOTAL_SIZE 17
 165
 166#define I596_NULL ((void *)0xffffffff)
 167
 168#define CMD_EOL         0x8000  /* The last command of the list, stop. */
 169#define CMD_SUSP        0x4000  /* Suspend after doing cmd. */
 170#define CMD_INTR        0x2000  /* Interrupt after doing cmd. */
 171
 172#define CMD_FLEX        0x0008  /* Enable flexible memory model */
 173
 174enum commands {
 175        CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
 176        CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7
 177};
 178
 179#define STAT_C          0x8000  /* Set to 0 after execution */
 180#define STAT_B          0x4000  /* Command being executed */
 181#define STAT_OK         0x2000  /* Command executed ok */
 182#define STAT_A          0x1000  /* Command aborted */
 183
 184#define  CUC_START      0x0100
 185#define  CUC_RESUME     0x0200
 186#define  CUC_SUSPEND    0x0300
 187#define  CUC_ABORT      0x0400
 188#define  RX_START       0x0010
 189#define  RX_RESUME      0x0020
 190#define  RX_SUSPEND     0x0030
 191#define  RX_ABORT       0x0040
 192
 193#define TX_TIMEOUT      5
 194
 195
 196struct i596_reg {
 197        unsigned short porthi;
 198        unsigned short portlo;
 199        unsigned long ca;
 200};
 201
 202#define EOF             0x8000
 203#define SIZE_MASK       0x3fff
 204
 205struct i596_tbd {
 206        unsigned short size;
 207        unsigned short pad;
 208        struct i596_tbd *next;
 209        char *data;
 210};
 211
 212/* The command structure has two 'next' pointers; v_next is the address of
 213 * the next command as seen by the CPU, b_next is the address of the next
 214 * command as seen by the 82596.  The b_next pointer, as used by the 82596
 215 * always references the status field of the next command, rather than the
 216 * v_next field, because the 82596 is unaware of v_next.  It may seem more
 217 * logical to put v_next at the end of the structure, but we cannot do that
 218 * because the 82596 expects other fields to be there, depending on command
 219 * type.
 220 */
 221
 222struct i596_cmd {
 223        struct i596_cmd *v_next;        /* Address from CPUs viewpoint */
 224        unsigned short status;
 225        unsigned short command;
 226        struct i596_cmd *b_next;        /* Address from i596 viewpoint */
 227};
 228
 229struct tx_cmd {
 230        struct i596_cmd cmd;
 231        struct i596_tbd *tbd;
 232        unsigned short size;
 233        unsigned short pad;
 234        struct sk_buff *skb;    /* So we can free it after tx */
 235};
 236
 237struct tdr_cmd {
 238        struct i596_cmd cmd;
 239        unsigned short status;
 240        unsigned short pad;
 241};
 242
 243struct mc_cmd {
 244        struct i596_cmd cmd;
 245        short mc_cnt;
 246        char mc_addrs[MAX_MC_CNT*6];
 247};
 248
 249struct sa_cmd {
 250        struct i596_cmd cmd;
 251        char eth_addr[8];
 252};
 253
 254struct cf_cmd {
 255        struct i596_cmd cmd;
 256        char i596_config[16];
 257};
 258
 259struct i596_rfd {
 260        unsigned short stat;
 261        unsigned short cmd;
 262        struct i596_rfd *b_next;        /* Address from i596 viewpoint */
 263        struct i596_rbd *rbd;
 264        unsigned short count;
 265        unsigned short size;
 266        struct i596_rfd *v_next;        /* Address from CPUs viewpoint */
 267        struct i596_rfd *v_prev;
 268};
 269
 270struct i596_rbd {
 271    unsigned short count;
 272    unsigned short zero1;
 273    struct i596_rbd *b_next;
 274    unsigned char *b_data;              /* Address from i596 viewpoint */
 275    unsigned short size;
 276    unsigned short zero2;
 277    struct sk_buff *skb;
 278    struct i596_rbd *v_next;
 279    struct i596_rbd *b_addr;            /* This rbd addr from i596 view */
 280    unsigned char *v_data;              /* Address from CPUs viewpoint */
 281};
 282
 283#define TX_RING_SIZE 64
 284#define RX_RING_SIZE 16
 285
 286struct i596_scb {
 287        unsigned short status;
 288        unsigned short command;
 289        struct i596_cmd *cmd;
 290        struct i596_rfd *rfd;
 291        unsigned long crc_err;
 292        unsigned long align_err;
 293        unsigned long resource_err;
 294        unsigned long over_err;
 295        unsigned long rcvdt_err;
 296        unsigned long short_err;
 297        unsigned short t_on;
 298        unsigned short t_off;
 299};
 300
 301struct i596_iscp {
 302        unsigned long stat;
 303        struct i596_scb *scb;
 304};
 305
 306struct i596_scp {
 307        unsigned long sysbus;
 308        unsigned long pad;
 309        struct i596_iscp *iscp;
 310};
 311
 312struct i596_private {
 313        volatile struct i596_scp scp;
 314        volatile struct i596_iscp iscp;
 315        volatile struct i596_scb scb;
 316        struct sa_cmd sa_cmd;
 317        struct cf_cmd cf_cmd;
 318        struct tdr_cmd tdr_cmd;
 319        struct mc_cmd mc_cmd;
 320        unsigned long stat;
 321        int last_restart __attribute__((aligned(4)));
 322        struct i596_rfd *rfd_head;
 323        struct i596_rbd *rbd_head;
 324        struct i596_cmd *cmd_tail;
 325        struct i596_cmd *cmd_head;
 326        int cmd_backlog;
 327        unsigned long last_cmd;
 328        struct net_device_stats stats;
 329        struct i596_rfd rfds[RX_RING_SIZE];
 330        struct i596_rbd rbds[RX_RING_SIZE];
 331        struct tx_cmd tx_cmds[TX_RING_SIZE];
 332        struct i596_tbd tbds[TX_RING_SIZE];
 333        int next_tx_cmd;
 334        spinlock_t lock;
 335};
 336
 337static char init_setup[] =
 338{
 339        0x8E,                   /* length, prefetch on */
 340        0xC8,                   /* fifo to 8, monitor off */
 341#ifdef CONFIG_VME
 342        0xc0,                   /* don't save bad frames */
 343#else
 344        0x80,                   /* don't save bad frames */
 345#endif
 346        0x2E,                   /* No source address insertion, 8 byte preamble */
 347        0x00,                   /* priority and backoff defaults */
 348        0x60,                   /* interframe spacing */
 349        0x00,                   /* slot time LSB */
 350        0xf2,                   /* slot time and retries */
 351        0x00,                   /* promiscuous mode */
 352        0x00,                   /* collision detect */
 353        0x40,                   /* minimum frame length */
 354        0xff,
 355        0x00,
 356        0x7f /*  *multi IA */ };
 357
 358static int i596_open(struct net_device *dev);
 359static int i596_start_xmit(struct sk_buff *skb, struct net_device *dev);
 360static irqreturn_t i596_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 361static int i596_close(struct net_device *dev);
 362static struct net_device_stats *i596_get_stats(struct net_device *dev);
 363static void i596_add_cmd(struct net_device *dev, struct i596_cmd *cmd);
 364static void i596_tx_timeout (struct net_device *dev);
 365static void print_eth(unsigned char *buf, char *str);
 366static void set_multicast_list(struct net_device *dev);
 367
 368static int rx_ring_size = RX_RING_SIZE;
 369static int ticks_limit = 25;
 370static int max_cmd_backlog = TX_RING_SIZE-1;
 371
 372
 373static inline void CA(struct net_device *dev)
 374{
 375#ifdef ENABLE_MVME16x_NET
 376        if (MACH_IS_MVME16x) {
 377                ((struct i596_reg *) dev->base_addr)->ca = 1;
 378        }
 379#endif
 380#ifdef ENABLE_BVME6000_NET
 381        if (MACH_IS_BVME6000) {
 382                volatile u32 i;
 383
 384                i = *(volatile u32 *) (dev->base_addr);
 385        }
 386#endif
 387#ifdef ENABLE_APRICOT
 388        if (MACH_IS_APRICOT) {
 389                outw(0, (short) (dev->base_addr) + 4);
 390        }
 391#endif
 392}
 393
 394
 395static inline void MPU_PORT(struct net_device *dev, int c, volatile void *x)
 396{
 397#ifdef ENABLE_MVME16x_NET
 398        if (MACH_IS_MVME16x) {
 399                struct i596_reg *p = (struct i596_reg *) (dev->base_addr);
 400                p->porthi = ((c) | (u32) (x)) & 0xffff;
 401                p->portlo = ((c) | (u32) (x)) >> 16;
 402        }
 403#endif
 404#ifdef ENABLE_BVME6000_NET
 405        if (MACH_IS_BVME6000) {
 406                u32 v = (u32) (c) | (u32) (x);
 407                v = ((u32) (v) << 16) | ((u32) (v) >> 16);
 408                *(volatile u32 *) dev->base_addr = v;
 409                udelay(1);
 410                *(volatile u32 *) dev->base_addr = v;
 411        }
 412#endif
 413}
 414
 415
 416static inline int wait_istat(struct net_device *dev, struct i596_private *lp, int delcnt, char *str)
 417{
 418        while (--delcnt && lp->iscp.stat)
 419                udelay(10);
 420        if (!delcnt) {
 421                printk(KERN_ERR "%s: %s, status %4.4x, cmd %4.4x.\n",
 422                     dev->name, str, lp->scb.status, lp->scb.command);
 423                return -1;
 424        }
 425        else
 426                return 0;
 427}
 428
 429
 430static inline int wait_cmd(struct net_device *dev, struct i596_private *lp, int delcnt, char *str)
 431{
 432        while (--delcnt && lp->scb.command)
 433                udelay(10);
 434        if (!delcnt) {
 435                printk(KERN_ERR "%s: %s, status %4.4x, cmd %4.4x.\n",
 436                     dev->name, str, lp->scb.status, lp->scb.command);
 437                return -1;
 438        }
 439        else
 440                return 0;
 441}
 442
 443
 444static inline int wait_cfg(struct net_device *dev, struct i596_cmd *cmd, int delcnt, char *str)
 445{
 446        volatile struct i596_cmd *c = cmd;
 447        
 448        while (--delcnt && c->command)
 449                udelay(10);
 450        if (!delcnt) {
 451                printk(KERN_ERR "%s: %s.\n", dev->name, str);
 452                return -1;
 453        }
 454        else
 455                return 0;
 456}
 457
 458 
 459static void i596_display_data(struct net_device *dev)
 460{
 461        struct i596_private *lp = (struct i596_private *) dev->priv;
 462        struct i596_cmd *cmd;
 463        struct i596_rfd *rfd;
 464        struct i596_rbd *rbd;
 465
 466        printk(KERN_ERR "lp and scp at %p, .sysbus = %08lx, .iscp = %p\n",
 467               &lp->scp, lp->scp.sysbus, lp->scp.iscp);
 468        printk(KERN_ERR "iscp at %p, iscp.stat = %08lx, .scb = %p\n",
 469               &lp->iscp, lp->iscp.stat, lp->iscp.scb);
 470        printk(KERN_ERR "scb at %p, scb.status = %04x, .command = %04x,"
 471                " .cmd = %p, .rfd = %p\n",
 472               &lp->scb, lp->scb.status, lp->scb.command,
 473                lp->scb.cmd, lp->scb.rfd);
 474        printk(KERN_ERR "   errors: crc %lx, align %lx, resource %lx,"
 475               " over %lx, rcvdt %lx, short %lx\n",
 476                lp->scb.crc_err, lp->scb.align_err, lp->scb.resource_err,
 477                lp->scb.over_err, lp->scb.rcvdt_err, lp->scb.short_err);
 478        cmd = lp->cmd_head;
 479        while (cmd != I596_NULL) {
 480                printk(KERN_ERR "cmd at %p, .status = %04x, .command = %04x, .b_next = %p\n",
 481                  cmd, cmd->status, cmd->command, cmd->b_next);
 482                cmd = cmd->v_next;
 483        }
 484        rfd = lp->rfd_head;
 485        printk(KERN_ERR "rfd_head = %p\n", rfd);
 486        do {
 487                printk(KERN_ERR "   %p .stat %04x, .cmd %04x, b_next %p, rbd %p,"
 488                        " count %04x\n",
 489                        rfd, rfd->stat, rfd->cmd, rfd->b_next, rfd->rbd,
 490                        rfd->count);
 491                rfd = rfd->v_next;
 492        } while (rfd != lp->rfd_head);
 493        rbd = lp->rbd_head;
 494        printk(KERN_ERR "rbd_head = %p\n", rbd);
 495        do {
 496                printk(KERN_ERR "   %p .count %04x, b_next %p, b_data %p, size %04x\n",
 497                        rbd, rbd->count, rbd->b_next, rbd->b_data, rbd->size);
 498                rbd = rbd->v_next;
 499        } while (rbd != lp->rbd_head);
 500}
 501
 502
 503#if defined(ENABLE_MVME16x_NET) || defined(ENABLE_BVME6000_NET)
 504static irqreturn_t i596_error(int irq, void *dev_id, struct pt_regs *regs)
 505{
 506        struct net_device *dev = dev_id;
 507#ifdef ENABLE_MVME16x_NET
 508        if (MACH_IS_MVME16x) {
 509                volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
 510
 511                pcc2[0x28] = 1;
 512                pcc2[0x2b] = 0x1d;
 513        }
 514#endif
 515#ifdef ENABLE_BVME6000_NET
 516        if (MACH_IS_BVME6000) {
 517                volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
 518
 519                *ethirq = 1;
 520                *ethirq = 3;
 521        }
 522#endif
 523        printk(KERN_ERR "%s: Error interrupt\n", dev->name);
 524        i596_display_data(dev);
 525        return IRQ_HANDLED;
 526}
 527#endif
 528
 529static inline void init_rx_bufs(struct net_device *dev)
 530{
 531        struct i596_private *lp = (struct i596_private *)dev->priv;
 532        int i;
 533        struct i596_rfd *rfd;
 534        struct i596_rbd *rbd;
 535
 536        /* First build the Receive Buffer Descriptor List */
 537
 538        for (i = 0, rbd = lp->rbds; i < rx_ring_size; i++, rbd++) {
 539                struct sk_buff *skb = dev_alloc_skb(PKT_BUF_SZ);
 540
 541                if (skb == NULL)
 542                        panic("82596: alloc_skb() failed");
 543                skb->dev = dev;
 544                rbd->v_next = rbd+1;
 545                rbd->b_next = WSWAPrbd(virt_to_bus(rbd+1));
 546                rbd->b_addr = WSWAPrbd(virt_to_bus(rbd));
 547                rbd->skb = skb;
 548                rbd->v_data = skb->tail;
 549                rbd->b_data = WSWAPchar(virt_to_bus(skb->tail));
 550                rbd->size = PKT_BUF_SZ;
 551#ifdef __mc68000__
 552                cache_clear(virt_to_phys(skb->tail), PKT_BUF_SZ);
 553#endif
 554        }
 555        lp->rbd_head = lp->rbds;
 556        rbd = lp->rbds + rx_ring_size - 1;
 557        rbd->v_next = lp->rbds;
 558        rbd->b_next = WSWAPrbd(virt_to_bus(lp->rbds));
 559
 560        /* Now build the Receive Frame Descriptor List */
 561
 562        for (i = 0, rfd = lp->rfds; i < rx_ring_size; i++, rfd++) {
 563                rfd->rbd = I596_NULL;
 564                rfd->v_next = rfd+1;
 565                rfd->v_prev = rfd-1;
 566                rfd->b_next = WSWAPrfd(virt_to_bus(rfd+1));
 567                rfd->cmd = CMD_FLEX;
 568        }
 569        lp->rfd_head = lp->rfds;
 570        lp->scb.rfd = WSWAPrfd(virt_to_bus(lp->rfds));
 571        rfd = lp->rfds;
 572        rfd->rbd = lp->rbd_head;
 573        rfd->v_prev = lp->rfds + rx_ring_size - 1;
 574        rfd = lp->rfds + rx_ring_size - 1;
 575        rfd->v_next = lp->rfds;
 576        rfd->b_next = WSWAPrfd(virt_to_bus(lp->rfds));
 577        rfd->cmd = CMD_EOL|CMD_FLEX;
 578}
 579
 580static inline void remove_rx_bufs(struct net_device *dev)
 581{
 582        struct i596_private *lp = (struct i596_private *)dev->priv;
 583        struct i596_rbd *rbd;
 584        int i;
 585
 586        for (i = 0, rbd = lp->rbds; i < rx_ring_size; i++, rbd++) {
 587                if (rbd->skb == NULL)
 588                        break;
 589                dev_kfree_skb(rbd->skb);
 590        }
 591}
 592
 593
 594static void rebuild_rx_bufs(struct net_device *dev)
 595{
 596        struct i596_private *lp = (struct i596_private *) dev->priv;
 597        int i;
 598
 599        /* Ensure rx frame/buffer descriptors are tidy */
 600
 601        for (i = 0; i < rx_ring_size; i++) {
 602                lp->rfds[i].rbd = I596_NULL;
 603                lp->rfds[i].cmd = CMD_FLEX;
 604        }
 605        lp->rfds[rx_ring_size-1].cmd = CMD_EOL|CMD_FLEX;
 606        lp->rfd_head = lp->rfds;
 607        lp->scb.rfd = WSWAPrfd(virt_to_bus(lp->rfds));
 608        lp->rbd_head = lp->rbds;
 609        lp->rfds[0].rbd = WSWAPrbd(virt_to_bus(lp->rbds));
 610}
 611
 612
 613static int init_i596_mem(struct net_device *dev)
 614{
 615        struct i596_private *lp = (struct i596_private *) dev->priv;
 616#if !defined(ENABLE_MVME16x_NET) && !defined(ENABLE_BVME6000_NET)
 617        short ioaddr = dev->base_addr;
 618#endif
 619        unsigned long flags;
 620
 621        MPU_PORT(dev, PORT_RESET, 0);
 622
 623        udelay(100);            /* Wait 100us - seems to help */
 624
 625#if defined(ENABLE_MVME16x_NET) || defined(ENABLE_BVME6000_NET)
 626#ifdef ENABLE_MVME16x_NET
 627        if (MACH_IS_MVME16x) {
 628                volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
 629
 630                /* Disable all ints for now */
 631                pcc2[0x28] = 1;
 632                pcc2[0x2a] = 0x48;
 633                /* Following disables snooping.  Snooping is not required
 634                 * as we make appropriate use of non-cached pages for
 635                 * shared data, and cache_push/cache_clear.
 636                 */
 637                pcc2[0x2b] = 0x08;
 638        }
 639#endif
 640#ifdef ENABLE_BVME6000_NET
 641        if (MACH_IS_BVME6000) {
 642                volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
 643
 644                *ethirq = 1;
 645        }
 646#endif
 647
 648        /* change the scp address */
 649
 650        MPU_PORT(dev, PORT_ALTSCP, (void *)virt_to_bus((void *)&lp->scp));
 651
 652#elif defined(ENABLE_APRICOT)
 653
 654        {
 655                u32 scp = virt_to_bus(&lp->scp);
 656
 657                /* change the scp address */
 658                outw(0, ioaddr);
 659                outw(0, ioaddr);
 660                outb(4, ioaddr + 0xf);
 661                outw(scp | 2, ioaddr);
 662                outw(scp >> 16, ioaddr);
 663        }
 664#endif
 665
 666        lp->last_cmd = jiffies;
 667
 668#ifdef ENABLE_MVME16x_NET
 669        if (MACH_IS_MVME16x)
 670                lp->scp.sysbus = 0x00000054;
 671#endif
 672#ifdef ENABLE_BVME6000_NET
 673        if (MACH_IS_BVME6000)
 674                lp->scp.sysbus = 0x0000004c;
 675#endif
 676#ifdef ENABLE_APRICOT
 677        if (MACH_IS_APRICOT)
 678                lp->scp.sysbus = 0x00440000;
 679#endif
 680
 681        lp->scp.iscp = WSWAPiscp(virt_to_bus((void *)&lp->iscp));
 682        lp->iscp.scb = WSWAPscb(virt_to_bus((void *)&lp->scb));
 683        lp->iscp.stat = ISCP_BUSY;
 684        lp->cmd_backlog = 0;
 685
 686        lp->cmd_head = lp->scb.cmd = I596_NULL;
 687
 688#ifdef ENABLE_BVME6000_NET
 689        if (MACH_IS_BVME6000) {
 690                lp->scb.t_on  = 7 * 25;
 691                lp->scb.t_off = 1 * 25;
 692        }
 693#endif
 694
 695        DEB(DEB_INIT,printk(KERN_DEBUG "%s: starting i82596.\n", dev->name));
 696
 697#if defined(ENABLE_APRICOT)
 698        (void) inb(ioaddr + 0x10);
 699        outb(4, ioaddr + 0xf);
 700#endif
 701        CA(dev);
 702
 703        if (wait_istat(dev,lp,1000,"initialization timed out"))
 704                goto failed;
 705        DEB(DEB_INIT,printk(KERN_DEBUG "%s: i82596 initialization successful\n", dev->name));
 706
 707        /* Ensure rx frame/buffer descriptors are tidy */
 708        rebuild_rx_bufs(dev);
 709        lp->scb.command = 0;
 710
 711#ifdef ENABLE_MVME16x_NET
 712        if (MACH_IS_MVME16x) {
 713                volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
 714
 715                /* Enable ints, etc. now */
 716                pcc2[0x2a] = 0x55;      /* Edge sensitive */
 717                pcc2[0x2b] = 0x15;
 718        }
 719#endif
 720#ifdef ENABLE_BVME6000_NET
 721        if (MACH_IS_BVME6000) {
 722                volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
 723
 724                *ethirq = 3;
 725        }
 726#endif
 727
 728
 729        DEB(DEB_INIT,printk(KERN_DEBUG "%s: queuing CmdConfigure\n", dev->name));
 730        memcpy(lp->cf_cmd.i596_config, init_setup, 14);
 731        lp->cf_cmd.cmd.command = CmdConfigure;
 732        i596_add_cmd(dev, &lp->cf_cmd.cmd);
 733
 734        DEB(DEB_INIT,printk(KERN_DEBUG "%s: queuing CmdSASetup\n", dev->name));
 735        memcpy(lp->sa_cmd.eth_addr, dev->dev_addr, 6);
 736        lp->sa_cmd.cmd.command = CmdSASetup;
 737        i596_add_cmd(dev, &lp->sa_cmd.cmd);
 738
 739        DEB(DEB_INIT,printk(KERN_DEBUG "%s: queuing CmdTDR\n", dev->name));
 740        lp->tdr_cmd.cmd.command = CmdTDR;
 741        i596_add_cmd(dev, &lp->tdr_cmd.cmd);
 742
 743        spin_lock_irqsave (&lp->lock, flags);
 744
 745        if (wait_cmd(dev,lp,1000,"timed out waiting to issue RX_START")) {
 746                spin_unlock_irqrestore (&lp->lock, flags);
 747                goto failed;
 748        }
 749        DEB(DEB_INIT,printk(KERN_DEBUG "%s: Issuing RX_START\n", dev->name));
 750        lp->scb.command = RX_START;
 751        CA(dev);
 752
 753        spin_unlock_irqrestore (&lp->lock, flags);
 754
 755        if (wait_cmd(dev,lp,1000,"RX_START not processed"))
 756                goto failed;
 757        DEB(DEB_INIT,printk(KERN_DEBUG "%s: Receive unit started OK\n", dev->name));
 758        return 0;
 759
 760failed:
 761        printk(KERN_CRIT "%s: Failed to initialise 82596\n", dev->name);
 762        MPU_PORT(dev, PORT_RESET, 0);
 763        return -1;
 764}
 765
 766static inline int i596_rx(struct net_device *dev)
 767{
 768        struct i596_private *lp = (struct i596_private *)dev->priv;
 769        struct i596_rfd *rfd;
 770        struct i596_rbd *rbd;
 771        int frames = 0;
 772
 773        DEB(DEB_RXFRAME,printk(KERN_DEBUG "i596_rx(), rfd_head %p, rbd_head %p\n",
 774                        lp->rfd_head, lp->rbd_head));
 775
 776        rfd = lp->rfd_head;             /* Ref next frame to check */
 777
 778        while ((rfd->stat) & STAT_C) {  /* Loop while complete frames */
 779                if (rfd->rbd == I596_NULL)
 780                        rbd = I596_NULL;
 781                else if (rfd->rbd == lp->rbd_head->b_addr)
 782                        rbd = lp->rbd_head;
 783                else {
 784                        printk(KERN_CRIT "%s: rbd chain broken!\n", dev->name);
 785                        /* XXX Now what? */
 786                        rbd = I596_NULL;
 787                }
 788                DEB(DEB_RXFRAME, printk(KERN_DEBUG "  rfd %p, rfd.rbd %p, rfd.stat %04x\n",
 789                        rfd, rfd->rbd, rfd->stat));
 790                
 791                if (rbd != I596_NULL && ((rfd->stat) & STAT_OK)) {
 792                        /* a good frame */
 793                        int pkt_len = rbd->count & 0x3fff;
 794                        struct sk_buff *skb = rbd->skb;
 795                        int rx_in_place = 0;
 796
 797                        DEB(DEB_RXADDR,print_eth(rbd->v_data, "received"));
 798                        frames++;
 799
 800                        /* Check if the packet is long enough to just accept
 801                         * without copying to a properly sized skbuff.
 802                         */
 803
 804                        if (pkt_len > rx_copybreak) {
 805                                struct sk_buff *newskb;
 806
 807                                /* Get fresh skbuff to replace filled one. */
 808                                newskb = dev_alloc_skb(PKT_BUF_SZ);
 809                                if (newskb == NULL) {
 810                                        skb = NULL;     /* drop pkt */
 811                                        goto memory_squeeze;
 812                                }
 813                                /* Pass up the skb already on the Rx ring. */
 814                                skb_put(skb, pkt_len);
 815                                rx_in_place = 1;
 816                                rbd->skb = newskb;
 817                                newskb->dev = dev;
 818                                rbd->v_data = newskb->tail;
 819                                rbd->b_data = WSWAPchar(virt_to_bus(newskb->tail));
 820#ifdef __mc68000__
 821                                cache_clear(virt_to_phys(newskb->tail), PKT_BUF_SZ);
 822#endif
 823                        }
 824                        else
 825                                skb = dev_alloc_skb(pkt_len + 2);
 826memory_squeeze:
 827                        if (skb == NULL) {
 828                                /* XXX tulip.c can defer packets here!! */
 829                                printk(KERN_WARNING "%s: i596_rx Memory squeeze, dropping packet.\n", dev->name);
 830                                lp->stats.rx_dropped++;
 831                        }
 832                        else {
 833                                skb->dev = dev;
 834                                if (!rx_in_place) {
 835                                        /* 16 byte align the data fields */
 836                                        skb_reserve(skb, 2);
 837                                        memcpy(skb_put(skb,pkt_len), rbd->v_data, pkt_len);
 838                                }
 839                                skb->protocol=eth_type_trans(skb,dev);
 840                                skb->len = pkt_len;
 841#ifdef __mc68000__
 842                                cache_clear(virt_to_phys(rbd->skb->tail),
 843                                                pkt_len);
 844#endif
 845                                netif_rx(skb);
 846                                dev->last_rx = jiffies;
 847                                lp->stats.rx_packets++;
 848                                lp->stats.rx_bytes+=pkt_len;
 849                        }
 850                }
 851                else {
 852                        DEB(DEB_ERRORS, printk(KERN_DEBUG "%s: Error, rfd.stat = 0x%04x\n",
 853                                        dev->name, rfd->stat));
 854                        lp->stats.rx_errors++;
 855                        if ((rfd->stat) & 0x0001)
 856                                lp->stats.collisions++;
 857                        if ((rfd->stat) & 0x0080)
 858                                lp->stats.rx_length_errors++;
 859                        if ((rfd->stat) & 0x0100)
 860                                lp->stats.rx_over_errors++;
 861                        if ((rfd->stat) & 0x0200)
 862                                lp->stats.rx_fifo_errors++;
 863                        if ((rfd->stat) & 0x0400)
 864                                lp->stats.rx_frame_errors++;
 865                        if ((rfd->stat) & 0x0800)
 866                                lp->stats.rx_crc_errors++;
 867                        if ((rfd->stat) & 0x1000)
 868                                lp->stats.rx_length_errors++;
 869                }
 870
 871                /* Clear the buffer descriptor count and EOF + F flags */
 872
 873                if (rbd != I596_NULL && (rbd->count & 0x4000)) {
 874                        rbd->count = 0;
 875                        lp->rbd_head = rbd->v_next;
 876                }
 877
 878                /* Tidy the frame descriptor, marking it as end of list */
 879
 880                rfd->rbd = I596_NULL;
 881                rfd->stat = 0;
 882                rfd->cmd = CMD_EOL|CMD_FLEX;
 883                rfd->count = 0;
 884
 885                /* Remove end-of-list from old end descriptor */
 886
 887                rfd->v_prev->cmd = CMD_FLEX;
 888
 889                /* Update record of next frame descriptor to process */
 890
 891                lp->scb.rfd = rfd->b_next;
 892                lp->rfd_head = rfd->v_next;
 893                rfd = lp->rfd_head;
 894        }
 895
 896        DEB(DEB_RXFRAME,printk(KERN_DEBUG "frames %d\n", frames));
 897
 898        return 0;
 899}
 900
 901
 902static inline void i596_cleanup_cmd(struct net_device *dev, struct i596_private *lp)
 903{
 904        struct i596_cmd *ptr;
 905
 906        while (lp->cmd_head != I596_NULL) {
 907                ptr = lp->cmd_head;
 908                lp->cmd_head = ptr->v_next;
 909                lp->cmd_backlog--;
 910
 911                switch ((ptr->command) & 0x7) {
 912                case CmdTx:
 913                        {
 914                                struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
 915                                struct sk_buff *skb = tx_cmd->skb;
 916
 917                                dev_kfree_skb(skb);
 918
 919                                lp->stats.tx_errors++;
 920                                lp->stats.tx_aborted_errors++;
 921
 922                                ptr->v_next = ptr->b_next = I596_NULL;
 923                                tx_cmd->cmd.command = 0;  /* Mark as free */
 924                                break;
 925                        }
 926                default:
 927                        ptr->v_next = ptr->b_next = I596_NULL;
 928                }
 929        }
 930
 931        wait_cmd(dev,lp,100,"i596_cleanup_cmd timed out");
 932        lp->scb.cmd = I596_NULL;
 933}
 934
 935static inline void i596_reset(struct net_device *dev, struct i596_private *lp, int ioaddr)
 936{
 937        unsigned long flags;
 938
 939        DEB(DEB_RESET,printk(KERN_DEBUG "i596_reset\n"));
 940
 941        spin_lock_irqsave (&lp->lock, flags);
 942
 943        wait_cmd(dev,lp,100,"i596_reset timed out");
 944
 945        netif_stop_queue(dev);
 946
 947        lp->scb.command = CUC_ABORT | RX_ABORT;
 948        CA(dev);
 949
 950        /* wait for shutdown */
 951        wait_cmd(dev,lp,1000,"i596_reset 2 timed out");
 952        spin_unlock_irqrestore (&lp->lock, flags);
 953
 954        i596_cleanup_cmd(dev,lp);
 955        i596_rx(dev);
 956
 957        netif_start_queue(dev);
 958        init_i596_mem(dev);
 959}
 960
 961static void i596_add_cmd(struct net_device *dev, struct i596_cmd *cmd)
 962{
 963        struct i596_private *lp = (struct i596_private *) dev->priv;
 964        int ioaddr = dev->base_addr;
 965        unsigned long flags;
 966
 967        DEB(DEB_ADDCMD,printk(KERN_DEBUG "i596_add_cmd\n"));
 968
 969        cmd->status = 0;
 970        cmd->command |= (CMD_EOL | CMD_INTR);
 971        cmd->v_next = cmd->b_next = I596_NULL;
 972
 973        spin_lock_irqsave (&lp->lock, flags);
 974
 975        if (lp->cmd_head != I596_NULL) {
 976                lp->cmd_tail->v_next = cmd;
 977                lp->cmd_tail->b_next = WSWAPcmd(virt_to_bus(&cmd->status));
 978        } else {
 979                lp->cmd_head = cmd;
 980                wait_cmd(dev,lp,100,"i596_add_cmd timed out");
 981                lp->scb.cmd = WSWAPcmd(virt_to_bus(&cmd->status));
 982                lp->scb.command = CUC_START;
 983                CA(dev);
 984        }
 985        lp->cmd_tail = cmd;
 986        lp->cmd_backlog++;
 987
 988        spin_unlock_irqrestore (&lp->lock, flags);
 989
 990        if (lp->cmd_backlog > max_cmd_backlog) {
 991                unsigned long tickssofar = jiffies - lp->last_cmd;
 992
 993                if (tickssofar < ticks_limit)
 994                        return;
 995
 996                printk(KERN_NOTICE "%s: command unit timed out, status resetting.\n", dev->name);
 997
 998                i596_reset(dev, lp, ioaddr);
 999        }
1000}
1001
1002static int i596_open(struct net_device *dev)
1003{
1004        int res = 0;
1005
1006        DEB(DEB_OPEN,printk(KERN_DEBUG "%s: i596_open() irq %d.\n", dev->name, dev->irq));
1007
1008        if (request_irq(dev->irq, i596_interrupt, 0, "i82596", dev)) {
1009                printk(KERN_ERR "%s: IRQ %d not free\n", dev->name, dev->irq);
1010                return -EAGAIN;
1011        }
1012#ifdef ENABLE_MVME16x_NET
1013        if (MACH_IS_MVME16x) {
1014                if (request_irq(0x56, i596_error, 0, "i82596_error", dev))
1015                        return -EAGAIN;
1016        }
1017#endif
1018        init_rx_bufs(dev);
1019
1020        netif_start_queue(dev);
1021
1022        /* Initialize the 82596 memory */
1023        if (init_i596_mem(dev)) {
1024                res = -EAGAIN;
1025                free_irq(dev->irq, dev);
1026        }
1027
1028        return res;
1029}
1030
1031static void i596_tx_timeout (struct net_device *dev)
1032{
1033        struct i596_private *lp = (struct i596_private *) dev->priv;
1034        int ioaddr = dev->base_addr;
1035
1036        /* Transmitter timeout, serious problems. */
1037        DEB(DEB_ERRORS,printk(KERN_ERR "%s: transmit timed out, status resetting.\n",
1038                        dev->name));
1039
1040        lp->stats.tx_errors++;
1041
1042        /* Try to restart the adaptor */
1043        if (lp->last_restart == lp->stats.tx_packets) {
1044                DEB(DEB_ERRORS,printk(KERN_ERR "Resetting board.\n"));
1045                /* Shutdown and restart */
1046                i596_reset (dev, lp, ioaddr);
1047        } else {
1048                /* Issue a channel attention signal */
1049                DEB(DEB_ERRORS,printk(KERN_ERR "Kicking board.\n"));
1050                lp->scb.command = CUC_START | RX_START;
1051                CA (dev);
1052                lp->last_restart = lp->stats.tx_packets;
1053        }
1054
1055        dev->trans_start = jiffies;
1056        netif_wake_queue (dev);
1057}
1058
1059
1060static int i596_start_xmit(struct sk_buff *skb, struct net_device *dev)
1061{
1062        struct i596_private *lp = (struct i596_private *) dev->priv;
1063        struct tx_cmd *tx_cmd;
1064        struct i596_tbd *tbd;
1065        short length = skb->len;
1066        dev->trans_start = jiffies;
1067
1068        DEB(DEB_STARTTX,printk(KERN_DEBUG "%s: i596_start_xmit(%x,%x) called\n", dev->name,
1069                                skb->len, (unsigned int)skb->data));
1070
1071        if (skb->len < ETH_ZLEN) {
1072                skb = skb_padto(skb, ETH_ZLEN);
1073                if (skb == NULL)
1074                        return 0;
1075                length = ETH_ZLEN;
1076        }
1077        netif_stop_queue(dev);
1078
1079        tx_cmd = lp->tx_cmds + lp->next_tx_cmd;
1080        tbd = lp->tbds + lp->next_tx_cmd;
1081
1082        if (tx_cmd->cmd.command) {
1083                printk(KERN_NOTICE "%s: xmit ring full, dropping packet.\n",
1084                                dev->name);
1085                lp->stats.tx_dropped++;
1086
1087                dev_kfree_skb(skb);
1088        } else {
1089                if (++lp->next_tx_cmd == TX_RING_SIZE)
1090                        lp->next_tx_cmd = 0;
1091                tx_cmd->tbd = WSWAPtbd(virt_to_bus(tbd));
1092                tbd->next = I596_NULL;
1093
1094                tx_cmd->cmd.command = CMD_FLEX | CmdTx;
1095                tx_cmd->skb = skb;
1096
1097                tx_cmd->pad = 0;
1098                tx_cmd->size = 0;
1099                tbd->pad = 0;
1100                tbd->size = EOF | length;
1101
1102                tbd->data = WSWAPchar(virt_to_bus(skb->data));
1103
1104#ifdef __mc68000__
1105                cache_push(virt_to_phys(skb->data), length);
1106#endif
1107                DEB(DEB_TXADDR,print_eth(skb->data, "tx-queued"));
1108                i596_add_cmd(dev, &tx_cmd->cmd);
1109
1110                lp->stats.tx_packets++;
1111                lp->stats.tx_bytes += length;
1112        }
1113
1114        netif_start_queue(dev);
1115
1116        return 0;
1117}
1118
1119static void print_eth(unsigned char *add, char *str)
1120{
1121        int i;
1122
1123        printk(KERN_DEBUG "i596 0x%p, ", add);
1124        for (i = 0; i < 6; i++)
1125                printk(" %02X", add[i + 6]);
1126        printk(" -->");
1127        for (i = 0; i < 6; i++)
1128                printk(" %02X", add[i]);
1129        printk(" %02X%02X, %s\n", add[12], add[13], str);
1130}
1131
1132int __init i82596_probe(struct net_device *dev)
1133{
1134        int i;
1135        struct i596_private *lp;
1136        char eth_addr[8];
1137        static int probed;
1138
1139        if (probed)
1140                return -ENODEV;
1141        probed++;
1142#ifdef ENABLE_MVME16x_NET
1143        if (MACH_IS_MVME16x) {
1144                if (mvme16x_config & MVME16x_CONFIG_NO_ETHERNET) {
1145                        printk(KERN_NOTICE "Ethernet probe disabled - chip not present\n");
1146                        return -ENODEV;
1147                }
1148                memcpy(eth_addr, (void *) 0xfffc1f2c, 6);       /* YUCK! Get addr from NOVRAM */
1149                dev->base_addr = MVME_I596_BASE;
1150                dev->irq = (unsigned) MVME16x_IRQ_I596;
1151        }
1152#endif
1153#ifdef ENABLE_BVME6000_NET
1154        if (MACH_IS_BVME6000) {
1155                volatile unsigned char *rtc = (unsigned char *) BVME_RTC_BASE;
1156                unsigned char msr = rtc[3];
1157                int i;
1158
1159                rtc[3] |= 0x80;
1160                for (i = 0; i < 6; i++)
1161                        eth_addr[i] = rtc[i * 4 + 7];   /* Stored in RTC RAM at offset 1 */
1162                rtc[3] = msr;
1163                dev->base_addr = BVME_I596_BASE;
1164                dev->irq = (unsigned) BVME_IRQ_I596;
1165        }
1166#endif
1167#ifdef ENABLE_APRICOT
1168        {
1169                int checksum = 0;
1170                int ioaddr = 0x300;
1171
1172                /* this is easy the ethernet interface can only be at 0x300 */
1173                /* first check nothing is already registered here */
1174
1175                if (!request_region(ioaddr, I596_TOTAL_SIZE, dev->name)) {
1176                        printk(KERN_ERR "82596: IO address 0x%04x in use\n", ioaddr);
1177                        return -EBUSY;
1178                }
1179
1180                for (i = 0; i < 8; i++) {
1181                        eth_addr[i] = inb(ioaddr + 8 + i);
1182                        checksum += eth_addr[i];
1183                }
1184
1185                /* checksum is a multiple of 0x100, got this wrong first time
1186                   some machines have 0x100, some 0x200. The DOS driver doesn't
1187                   even bother with the checksum.
1188                   Some other boards trip the checksum.. but then appear as
1189                   ether address 0. Trap these - AC */
1190
1191                if ((checksum % 0x100) || 
1192                    (memcmp(eth_addr, "\x00\x00\x49", 3) != 0)) {
1193                        release_region(ioaddr, I596_TOTAL_SIZE);
1194                        return -ENODEV;
1195                }
1196
1197                dev->base_addr = ioaddr;
1198                dev->irq = 10;
1199        }
1200#endif
1201        dev->mem_start = (int)__get_free_pages(GFP_ATOMIC, 0);
1202        if (!dev->mem_start) {
1203#ifdef ENABLE_APRICOT
1204                release_region(dev->base_addr, I596_TOTAL_SIZE);
1205#endif
1206                return -ENOMEM;
1207        }
1208
1209        ether_setup(dev);
1210        DEB(DEB_PROBE,printk(KERN_INFO "%s: 82596 at %#3lx,", dev->name, dev->base_addr));
1211
1212        for (i = 0; i < 6; i++)
1213                DEB(DEB_PROBE,printk(" %2.2X", dev->dev_addr[i] = eth_addr[i]));
1214
1215        DEB(DEB_PROBE,printk(" IRQ %d.\n", dev->irq));
1216
1217        DEB(DEB_PROBE,printk(KERN_INFO "%s", version));
1218
1219        /* The 82596-specific entries in the device structure. */
1220        SET_MODULE_OWNER(dev);
1221        dev->open = i596_open;
1222        dev->stop = i596_close;
1223        dev->hard_start_xmit = i596_start_xmit;
1224        dev->get_stats = i596_get_stats;
1225        dev->set_multicast_list = set_multicast_list;
1226        dev->tx_timeout = i596_tx_timeout;
1227        dev->watchdog_timeo = TX_TIMEOUT;
1228
1229        dev->priv = (void *)(dev->mem_start);
1230
1231        lp = (struct i596_private *) dev->priv;
1232        DEB(DEB_INIT,printk(KERN_DEBUG "%s: lp at 0x%08lx (%d bytes), lp->scb at 0x%08lx\n",
1233                        dev->name, (unsigned long)lp,
1234                        sizeof(struct i596_private), (unsigned long)&lp->scb));
1235        memset((void *) lp, 0, sizeof(struct i596_private));
1236
1237#ifdef __mc68000__
1238        cache_push(virt_to_phys((void *)(dev->mem_start)), 4096);
1239        cache_clear(virt_to_phys((void *)(dev->mem_start)), 4096);
1240        kernel_set_cachemode((void *)(dev->mem_start), 4096, IOMAP_NOCACHE_SER);
1241#endif
1242        lp->scb.command = 0;
1243        lp->scb.cmd = I596_NULL;
1244        lp->scb.rfd = I596_NULL;
1245        lp->lock = SPIN_LOCK_UNLOCKED;
1246
1247        return 0;
1248}
1249
1250static irqreturn_t i596_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1251{
1252        struct net_device *dev = dev_id;
1253        struct i596_private *lp;
1254        short ioaddr;
1255        unsigned short status, ack_cmd = 0;
1256        int handled = 0;
1257
1258#ifdef ENABLE_BVME6000_NET
1259        if (MACH_IS_BVME6000) {
1260                if (*(char *) BVME_LOCAL_IRQ_STAT & BVME_ETHERR) {
1261                        i596_error(irq, dev_id, regs);
1262                        return IRQ_HANDLED;
1263                }
1264        }
1265#endif
1266        if (dev == NULL) {
1267                printk(KERN_ERR "i596_interrupt(): irq %d for unknown device.\n", irq);
1268                return IRQ_NONE;
1269        }
1270
1271        ioaddr = dev->base_addr;
1272        lp = (struct i596_private *) dev->priv;
1273
1274        spin_lock (&lp->lock);
1275
1276        wait_cmd(dev,lp,100,"i596 interrupt, timeout");
1277        status = lp->scb.status;
1278
1279        DEB(DEB_INTS,printk(KERN_DEBUG "%s: i596 interrupt, IRQ %d, status %4.4x.\n",
1280                        dev->name, irq, status));
1281
1282        ack_cmd = status & 0xf000;
1283
1284        if ((status & 0x8000) || (status & 0x2000)) {
1285                struct i596_cmd *ptr;
1286
1287                handled = 1;
1288                if ((status & 0x8000))
1289                        DEB(DEB_INTS,printk(KERN_DEBUG "%s: i596 interrupt completed command.\n", dev->name));
1290                if ((status & 0x2000))
1291                        DEB(DEB_INTS,printk(KERN_DEBUG "%s: i596 interrupt command unit inactive %x.\n", dev->name, status & 0x0700));
1292
1293                while ((lp->cmd_head != I596_NULL) && (lp->cmd_head->status & STAT_C)) {
1294                        ptr = lp->cmd_head;
1295
1296                        DEB(DEB_STATUS,printk(KERN_DEBUG "cmd_head->status = %04x, ->command = %04x\n",
1297                                       lp->cmd_head->status, lp->cmd_head->command));
1298                        lp->cmd_head = ptr->v_next;
1299                        lp->cmd_backlog--;
1300
1301                        switch ((ptr->command) & 0x7) {
1302                        case CmdTx:
1303                            {
1304                                struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
1305                                struct sk_buff *skb = tx_cmd->skb;
1306
1307                                if ((ptr->status) & STAT_OK) {
1308                                        DEB(DEB_TXADDR,print_eth(skb->data, "tx-done"));
1309                                } else {
1310                                        lp->stats.tx_errors++;
1311                                        if ((ptr->status) & 0x0020)
1312                                                lp->stats.collisions++;
1313                                        if (!((ptr->status) & 0x0040))
1314                                                lp->stats.tx_heartbeat_errors++;
1315                                        if ((ptr->status) & 0x0400)
1316                                                lp->stats.tx_carrier_errors++;
1317                                        if ((ptr->status) & 0x0800)
1318                                                lp->stats.collisions++;
1319                                        if ((ptr->status) & 0x1000)
1320                                                lp->stats.tx_aborted_errors++;
1321                                }
1322
1323                                dev_kfree_skb_irq(skb);
1324
1325                                tx_cmd->cmd.command = 0; /* Mark free */
1326                                break;
1327                            }
1328                        case CmdTDR:
1329                            {
1330                                unsigned short status = ((struct tdr_cmd *)ptr)->status;
1331
1332                                if (status & 0x8000) {
1333                                        DEB(DEB_TDR,printk(KERN_INFO "%s: link ok.\n", dev->name));
1334                                } else {
1335                                        if (status & 0x4000)
1336                                                printk(KERN_ERR "%s: Transceiver problem.\n", dev->name);
1337                                        if (status & 0x2000)
1338                                                printk(KERN_ERR "%s: Termination problem.\n", dev->name);
1339                                        if (status & 0x1000)
1340                                                printk(KERN_ERR "%s: Short circuit.\n", dev->name);
1341
1342                                        DEB(DEB_TDR,printk(KERN_INFO "%s: Time %d.\n", dev->name, status & 0x07ff));
1343                                }
1344                                break;
1345                            }
1346                        case CmdConfigure:
1347                        case CmdMulticastList:
1348                                /* Zap command so set_multicast_list() knows it is free */
1349                                ptr->command = 0;
1350                                break;
1351                        }
1352                        ptr->v_next = ptr->b_next = I596_NULL;
1353                        lp->last_cmd = jiffies;
1354                }
1355
1356                ptr = lp->cmd_head;
1357                while ((ptr != I596_NULL) && (ptr != lp->cmd_tail)) {
1358                        ptr->command &= 0x1fff;
1359                        ptr = ptr->v_next;
1360                }
1361
1362                if ((lp->cmd_head != I596_NULL))
1363                        ack_cmd |= CUC_START;
1364                lp->scb.cmd = WSWAPcmd(virt_to_bus(&lp->cmd_head->status));
1365        }
1366        if ((status & 0x1000) || (status & 0x4000)) {
1367                if ((status & 0x4000))
1368                        DEB(DEB_INTS,printk(KERN_DEBUG "%s: i596 interrupt received a frame.\n", dev->name));
1369                i596_rx(dev);
1370                /* Only RX_START if stopped - RGH 07-07-96 */
1371                if (status & 0x1000) {
1372                        if (netif_running(dev)) {
1373                                DEB(DEB_ERRORS,printk(KERN_ERR "%s: i596 interrupt receive unit inactive, status 0x%x\n", dev->name, status));
1374                                ack_cmd |= RX_START;
1375                                lp->stats.rx_errors++;
1376                                lp->stats.rx_fifo_errors++;
1377                                rebuild_rx_bufs(dev);
1378                        }
1379                }
1380        }
1381        wait_cmd(dev,lp,100,"i596 interrupt, timeout");
1382        lp->scb.command = ack_cmd;
1383
1384#ifdef ENABLE_MVME16x_NET
1385        if (MACH_IS_MVME16x) {
1386                /* Ack the interrupt */
1387
1388                volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
1389
1390                pcc2[0x2a] |= 0x08;
1391        }
1392#endif
1393#ifdef ENABLE_BVME6000_NET
1394        if (MACH_IS_BVME6000) {
1395                volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
1396
1397                *ethirq = 1;
1398                *ethirq = 3;
1399        }
1400#endif
1401#ifdef ENABLE_APRICOT
1402        (void) inb(ioaddr + 0x10);
1403        outb(4, ioaddr + 0xf);
1404#endif
1405        CA(dev);
1406
1407        DEB(DEB_INTS,printk(KERN_DEBUG "%s: exiting interrupt.\n", dev->name));
1408
1409        spin_unlock (&lp->lock);
1410        return IRQ_RETVAL(handled);
1411}
1412
1413static int i596_close(struct net_device *dev)
1414{
1415        struct i596_private *lp = (struct i596_private *) dev->priv;
1416        unsigned long flags;
1417
1418        netif_stop_queue(dev);
1419
1420        DEB(DEB_INIT,printk(KERN_DEBUG "%s: Shutting down ethercard, status was %4.4x.\n",
1421                       dev->name, lp->scb.status));
1422
1423        spin_lock_irqsave(&lp->lock, flags);
1424
1425        wait_cmd(dev,lp,100,"close1 timed out");
1426        lp->scb.command = CUC_ABORT | RX_ABORT;
1427        CA(dev);
1428
1429        wait_cmd(dev,lp,100,"close2 timed out");
1430
1431        spin_unlock_irqrestore(&lp->lock, flags);
1432        DEB(DEB_STRUCT,i596_display_data(dev));
1433        i596_cleanup_cmd(dev,lp);
1434
1435#ifdef ENABLE_MVME16x_NET
1436        if (MACH_IS_MVME16x) {
1437                volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
1438
1439                /* Disable all ints */
1440                pcc2[0x28] = 1;
1441                pcc2[0x2a] = 0x40;
1442                pcc2[0x2b] = 0x40;      /* Set snooping bits now! */
1443        }
1444#endif
1445#ifdef ENABLE_BVME6000_NET
1446        if (MACH_IS_BVME6000) {
1447                volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
1448
1449                *ethirq = 1;
1450        }
1451#endif
1452
1453        free_irq(dev->irq, dev);
1454        remove_rx_bufs(dev);
1455
1456        return 0;
1457}
1458
1459static struct net_device_stats *
1460 i596_get_stats(struct net_device *dev)
1461{
1462        struct i596_private *lp = (struct i596_private *) dev->priv;
1463
1464        return &lp->stats;
1465}
1466
1467/*
1468 *    Set or clear the multicast filter for this adaptor.
1469 */
1470
1471static void set_multicast_list(struct net_device *dev)
1472{
1473        struct i596_private *lp = (struct i596_private *) dev->priv;
1474        int config = 0, cnt;
1475
1476        DEB(DEB_MULTI,printk(KERN_DEBUG "%s: set multicast list, %d entries, promisc %s, allmulti %s\n",
1477                dev->name, dev->mc_count,
1478                dev->flags & IFF_PROMISC  ? "ON" : "OFF",
1479                dev->flags & IFF_ALLMULTI ? "ON" : "OFF"));
1480
1481        if (wait_cfg(dev, &lp->cf_cmd.cmd, 1000, "config change request timed out"))
1482                return;
1483
1484        if ((dev->flags & IFF_PROMISC) && !(lp->cf_cmd.i596_config[8] & 0x01)) {
1485                lp->cf_cmd.i596_config[8] |= 0x01;
1486                config = 1;
1487        }
1488        if (!(dev->flags & IFF_PROMISC) && (lp->cf_cmd.i596_config[8] & 0x01)) {
1489                lp->cf_cmd.i596_config[8] &= ~0x01;
1490                config = 1;
1491        }
1492        if ((dev->flags & IFF_ALLMULTI) && (lp->cf_cmd.i596_config[11] & 0x20)) {
1493                lp->cf_cmd.i596_config[11] &= ~0x20;
1494                config = 1;
1495        }
1496        if (!(dev->flags & IFF_ALLMULTI) && !(lp->cf_cmd.i596_config[11] & 0x20)) {
1497                lp->cf_cmd.i596_config[11] |= 0x20;
1498                config = 1;
1499        }
1500        if (config) {
1501                lp->cf_cmd.cmd.command = CmdConfigure;
1502                i596_add_cmd(dev, &lp->cf_cmd.cmd);
1503        }
1504
1505        cnt = dev->mc_count;
1506        if (cnt > MAX_MC_CNT)
1507        {
1508                cnt = MAX_MC_CNT;
1509                printk(KERN_ERR "%s: Only %d multicast addresses supported",
1510                        dev->name, cnt);
1511        }
1512        
1513        if (dev->mc_count > 0) {
1514                struct dev_mc_list *dmi;
1515                unsigned char *cp;
1516                struct mc_cmd *cmd;
1517
1518                if (wait_cfg(dev, &lp->mc_cmd.cmd, 1000, "multicast list change request timed out"))
1519                        return;
1520                cmd = &lp->mc_cmd;
1521                cmd->cmd.command = CmdMulticastList;
1522                cmd->mc_cnt = dev->mc_count * 6;
1523                cp = cmd->mc_addrs;
1524                for (dmi = dev->mc_list; cnt && dmi != NULL; dmi = dmi->next, cnt--, cp += 6) {
1525                        memcpy(cp, dmi->dmi_addr, 6);
1526                        if (i596_debug > 1)
1527                                DEB(DEB_MULTI,printk(KERN_INFO "%s: Adding address %02x:%02x:%02x:%02x:%02x:%02x\n",
1528                                                dev->name, cp[0],cp[1],cp[2],cp[3],cp[4],cp[5]));
1529                }
1530                i596_add_cmd(dev, &cmd->cmd);
1531        }
1532}
1533
1534#ifdef MODULE
1535static struct net_device dev_82596 = { .init = i82596_probe };
1536
1537#ifdef ENABLE_APRICOT
1538static int io = 0x300;
1539static int irq = 10;
1540MODULE_PARM(irq, "i");
1541MODULE_PARM_DESC(irq, "Apricot IRQ number");
1542#endif
1543
1544MODULE_PARM(debug, "i");
1545MODULE_PARM_DESC(debug, "i82596 debug mask");
1546static int debug = -1;
1547
1548int init_module(void)
1549{
1550#ifdef ENABLE_APRICOT
1551        dev_82596.base_addr = io;
1552        dev_82596.irq = irq;
1553#endif
1554        if (debug >= 0)
1555                i596_debug = debug;
1556        if (register_netdev(&dev_82596) != 0)
1557                return -EIO;
1558        return 0;
1559}
1560
1561void cleanup_module(void)
1562{
1563        unregister_netdev(&dev_82596);
1564#ifdef __mc68000__
1565        /* XXX This assumes default cache mode to be IOMAP_FULL_CACHING,
1566         * XXX which may be invalid (CONFIG_060_WRITETHROUGH)
1567         */
1568
1569        kernel_set_cachemode((void *)(dev_82596.mem_start), 4096,
1570                        IOMAP_FULL_CACHING);
1571#endif
1572        free_page ((u32)(dev_82596.mem_start));
1573        dev_82596.priv = NULL;
1574#ifdef ENABLE_APRICOT
1575        /* If we don't do this, we can't re-insmod it later. */
1576        release_region(dev_82596.base_addr, I596_TOTAL_SIZE);
1577#endif
1578}
1579
1580#endif                          /* MODULE */
1581
1582/*
1583 * Local variables:
1584 *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c 82596.c"
1585 * End:
1586 */
1587
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.