root/drivers/net/apricot.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. init_rx_bufs
  2. remove_rx_bufs
  3. init_i596_mem
  4. i596_rx
  5. i596_cleanup_cmd
  6. i596_reset
  7. i596_add_cmd
  8. i596_open
  9. i596_start_xmit
  10. print_eth
  11. apricot_probe
  12. i596_interrupt
  13. i596_close
  14. i596_get_stats
  15. set_multicast_list
  16. init_module
  17. cleanup_module

   1 /* apricot.c: An Apricot 82596 ethernet driver for linux. */
   2 /*
   3     Apricot
   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     Driver skeleton 
  10         Written 1993 by Donald Becker.
  11         Copyright 1993 United States Government as represented by the Director,
  12         National Security Agency.  This software may only be used and distributed
  13         according to the terms of the GNU Public License as modified by SRC,
  14         incorporated herein by reference.
  15 
  16         The author may be reached as becker@super.org or
  17         C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
  18     
  19 
  20 */
  21 
  22 static const char *version = "apricot.c:v0.2 05/12/94\n";
  23 
  24 #include <linux/module.h>
  25 
  26 #include <linux/kernel.h>
  27 #include <linux/sched.h>
  28 #include <linux/string.h>
  29 #include <linux/ptrace.h>
  30 #include <linux/errno.h>
  31 #include <linux/ioport.h>
  32 #include <linux/malloc.h>
  33 #include <linux/interrupt.h>
  34 #include <linux/netdevice.h>
  35 #include <linux/etherdevice.h>
  36 #include <linux/skbuff.h>
  37 
  38 #include <asm/bitops.h>
  39 #include <asm/io.h>
  40 #include <asm/dma.h>
  41 
  42 #ifndef HAVE_PORTRESERVE
  43 #define check_region(addr, size)        0
  44 #define request_region(addr, size,name) do ; while(0)
  45 #endif
  46 
  47 #ifndef HAVE_ALLOC_SKB
  48 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
  49 #define kfree_skbmem(buff, size) kfree_s(buff,size)
  50 #endif
  51 
  52 #define APRICOT_DEBUG 1
  53 
  54 #ifdef APRICOT_DEBUG
  55 int i596_debug = APRICOT_DEBUG;
  56 #else
  57 int i596_debug = 1;
  58 #endif
  59 
  60 #define APRICOT_TOTAL_SIZE 17
  61 
  62 #define I596_NULL -1
  63 
  64 #define CMD_EOL         0x8000  /* The last command of the list, stop. */
  65 #define CMD_SUSP        0x4000  /* Suspend after doing cmd. */
  66 #define CMD_INTR        0x2000  /* Interrupt after doing cmd. */
  67 
  68 #define CMD_FLEX        0x0008  /* Enable flexible memory model */
  69 
  70 enum commands {
  71         CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
  72         CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7};
  73 
  74 #define STAT_C          0x8000  /* Set to 0 after execution */
  75 #define STAT_B          0x4000  /* Command being executed */
  76 #define STAT_OK         0x2000  /* Command executed ok */
  77 #define STAT_A          0x1000  /* Command aborted */
  78 
  79 #define  CUC_START      0x0100
  80 #define  CUC_RESUME     0x0200
  81 #define  CUC_SUSPEND    0x0300
  82 #define  CUC_ABORT      0x0400
  83 #define  RX_START       0x0010
  84 #define  RX_RESUME      0x0020
  85 #define  RX_SUSPEND     0x0030
  86 #define  RX_ABORT       0x0040
  87 
  88 struct i596_cmd {
  89     unsigned short status;
  90     unsigned short command;
  91     struct i596_cmd *next;
  92 };
  93 
  94 #define EOF             0x8000
  95 #define SIZE_MASK       0x3fff
  96 
  97 struct i596_tbd {
  98     unsigned short size;
  99     unsigned short pad;
 100     struct i596_tbd *next;
 101     char *data;
 102 };
 103 
 104 struct tx_cmd {
 105     struct i596_cmd cmd;
 106     struct i596_tbd *tbd;
 107     unsigned short size;
 108     unsigned short pad;
 109 };
 110 
 111 struct i596_rfd {
 112     unsigned short stat;
 113     unsigned short cmd;
 114     struct i596_rfd *next;
 115     long rbd; 
 116     unsigned short count;
 117     unsigned short size;
 118     char data[1532];
 119 };
 120 
 121 #define RX_RING_SIZE 8
 122 
 123 struct i596_scb {
 124     unsigned short status;
 125     unsigned short command;
 126     struct i596_cmd *cmd;
 127     struct i596_rfd *rfd;
 128     unsigned long crc_err;
 129     unsigned long align_err;
 130     unsigned long resource_err;
 131     unsigned long over_err;
 132     unsigned long rcvdt_err;
 133     unsigned long short_err;
 134     unsigned short t_on;
 135     unsigned short t_off;
 136 };
 137 
 138 struct i596_iscp {
 139     unsigned long stat;
 140     struct i596_scb *scb;
 141 };
 142 
 143 struct i596_scp {
 144     unsigned long sysbus;
 145     unsigned long pad;
 146     struct i596_iscp *iscp;
 147 };
 148 
 149 struct i596_private {
 150     struct i596_scp scp;
 151     struct i596_iscp iscp;
 152     struct i596_scb scb;
 153     struct i596_cmd set_add;
 154     char eth_addr[8];
 155     struct i596_cmd set_conf;
 156     char i596_config[16];
 157     struct i596_cmd tdr;
 158     unsigned long stat;
 159     int last_restart;
 160     struct i596_rfd *rx_tail;
 161     struct i596_cmd *cmd_tail;
 162     struct i596_cmd *cmd_head;
 163     int cmd_backlog;
 164     unsigned long last_cmd;
 165     struct enet_statistics stats;
 166 };
 167 
 168 char init_setup[] = {
 169         0x8E,   /* length, prefetch on */
 170         0xC8,   /* fifo to 8, monitor off */
 171         0x80,   /* don't save bad frames */
 172         0x2E,   /* No source address insertion, 8 byte preamble */
 173         0x00,   /* priority and backoff defaults */
 174         0x60,   /* interframe spacing */
 175         0x00,   /* slot time LSB */
 176         0xf2,   /* slot time and retries */
 177         0x00,   /* promiscuous mode */
 178         0x00,   /* collision detect */
 179         0x40,   /* minimum frame length */
 180         0xff,   
 181         0x00,
 182         0x7f    /*  *multi IA */ };
 183 
 184 static int i596_open(struct device *dev);
 185 static int i596_start_xmit(struct sk_buff *skb, struct device *dev);
 186 static void i596_interrupt(int irq, struct pt_regs *regs);
 187 static int i596_close(struct device *dev);
 188 static struct enet_statistics *i596_get_stats(struct device *dev);
 189 static void i596_add_cmd(struct device *dev, struct i596_cmd *cmd);
 190 static void print_eth(char *);
 191 #ifdef HAVE_MULTICAST
 192 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
 193 #endif
 194 
 195 
 196 static inline int
 197 init_rx_bufs(struct device *dev, int num)
     /* [previous][next][first][last][top][bottom][index][help] */
 198 {
 199     struct i596_private *lp = (struct i596_private *)dev->priv;
 200     int i;
 201     struct i596_rfd *rfd;
 202 
 203     lp->scb.rfd = (struct i596_rfd *)I596_NULL;
 204 
 205     if (i596_debug > 1) printk ("%s: init_rx_bufs %d.\n", dev->name, num);
 206 
 207     for (i = 0; i < num; i++)
 208     {
 209         if (!(rfd = (struct i596_rfd *)kmalloc(sizeof(struct i596_rfd), GFP_KERNEL)))
 210             break;
 211 
 212         rfd->stat = 0x0000;
 213         rfd->rbd = I596_NULL;
 214         rfd->count = 0;
 215         rfd->size = 1532;
 216         if (i == 0)
 217         {
 218             rfd->cmd = CMD_EOL;
 219             lp->rx_tail = rfd;
 220         }
 221         else
 222             rfd->cmd = 0x0000;
 223 
 224         rfd->next = lp->scb.rfd;
 225         lp->scb.rfd = rfd;
 226     }
 227 
 228     if (i != 0)
 229       lp->rx_tail->next = lp->scb.rfd;
 230 
 231     return (i);
 232 }
 233 
 234 static inline void
 235 remove_rx_bufs(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 236 {
 237     struct i596_private *lp = (struct i596_private *)dev->priv;
 238     struct i596_rfd *rfd = lp->scb.rfd;
 239 
 240     lp->rx_tail->next = (struct i596_rfd *)I596_NULL;
 241 
 242     do
 243     {
 244         lp->scb.rfd = rfd->next;
 245         kfree_s(rfd, sizeof(struct i596_rfd));
 246         rfd = lp->scb.rfd;
 247     }
 248     while (rfd != lp->rx_tail);
 249 }
 250 
 251 static inline void
 252 init_i596_mem(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 253 {
 254     struct i596_private *lp = (struct i596_private *)dev->priv;
 255     short ioaddr = dev->base_addr;
 256     int boguscnt = 100;
 257 
 258     /* change the scp address */
 259     outw(0, ioaddr);
 260     outw(0, ioaddr);
 261     outb(4, ioaddr+0xf);
 262     outw(((((int)&lp->scp) & 0xffff) | 2), ioaddr);
 263     outw((((int)&lp->scp)>>16) & 0xffff, ioaddr);
 264 
 265     lp->last_cmd = jiffies;
 266 
 267     lp->scp.sysbus = 0x00440000;
 268     lp->scp.iscp = &(lp->iscp);
 269     lp->iscp.scb = &(lp->scb);
 270     lp->iscp.stat = 0x0001;
 271     lp->cmd_backlog = 0;
 272 
 273     lp->cmd_head = lp->scb.cmd = (struct i596_cmd *) I596_NULL;
 274 
 275     if (i596_debug > 2) printk("%s: starting i82596.\n", dev->name);
 276 
 277     (void) inb (ioaddr+0x10);
 278     outb(4, ioaddr+0xf);
 279     outw(0, ioaddr+4);
 280 
 281     while (lp->iscp.stat)
 282         if (--boguscnt == 0)
 283         {
 284             printk("%s: i82596 initialization timed out with status %4.4x, cmd %4.4x.\n",
 285                    dev->name, lp->scb.status, lp->scb.command);
 286             break;
 287         }
 288 
 289     lp->scb.command = 0;
 290 
 291     memcpy (lp->i596_config, init_setup, 14);
 292     lp->set_conf.command = CmdConfigure;
 293     i596_add_cmd(dev, &lp->set_conf);
 294 
 295     memcpy (lp->eth_addr, dev->dev_addr, 6);
 296     lp->set_add.command = CmdSASetup;
 297     i596_add_cmd(dev, &lp->set_add);
 298 
 299     lp->tdr.command = CmdTDR;
 300     i596_add_cmd(dev, &lp->tdr);
 301 
 302     boguscnt = 200;
 303     while (lp->scb.status, lp->scb.command)
 304         if (--boguscnt == 0)
 305         {
 306             printk("%s: receive unit start timed out with status %4.4x, cmd %4.4x.\n",
 307                    dev->name, lp->scb.status, lp->scb.command);
 308             break;
 309         }
 310 
 311     lp->scb.command = RX_START;
 312     outw(0, ioaddr+4);
 313 
 314     boguscnt = 200;
 315     while (lp->scb.status, lp->scb.command)
 316         if (--boguscnt == 0)
 317         {
 318             printk("i82596 init timed out with status %4.4x, cmd %4.4x.\n",
 319                 lp->scb.status, lp->scb.command);
 320             break;
 321         }
 322 
 323     return;
 324 }
 325 
 326 static inline int
 327 i596_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 328 {
 329     struct i596_private *lp = (struct i596_private *)dev->priv;
 330     int frames = 0;
 331 
 332     if (i596_debug > 3) printk ("i596_rx()\n");
 333 
 334     while ((lp->scb.rfd->stat) & STAT_C)
 335     {
 336         if (i596_debug >2) print_eth(lp->scb.rfd->data);
 337 
 338         if ((lp->scb.rfd->stat) & STAT_OK)
 339         {
 340             /* a good frame */
 341             int pkt_len = lp->scb.rfd->count & 0x3fff;
 342             struct sk_buff *skb = dev_alloc_skb(pkt_len);
 343 
 344             frames++;
 345 
 346             if (skb == NULL)
 347             {
 348                 printk ("%s: i596_rx Memory squeeze, dropping packet.\n", dev->name);
 349                 lp->stats.rx_dropped++;
 350                 break;
 351             }
 352 
 353             skb->dev = dev;             
 354             memcpy(skb_put(skb,pkt_len), lp->scb.rfd->data, pkt_len);
 355 
 356             skb->protocol=eth_type_trans(skb,dev);
 357             netif_rx(skb);
 358             lp->stats.rx_packets++;
 359 
 360             if (i596_debug > 4) print_eth(skb->data);
 361         }
 362         else
 363         {
 364             lp->stats.rx_errors++;
 365             if ((lp->scb.rfd->stat) & 0x0001) lp->stats.collisions++;
 366             if ((lp->scb.rfd->stat) & 0x0080) lp->stats.rx_length_errors++;
 367             if ((lp->scb.rfd->stat) & 0x0100) lp->stats.rx_over_errors++;
 368             if ((lp->scb.rfd->stat) & 0x0200) lp->stats.rx_fifo_errors++;
 369             if ((lp->scb.rfd->stat) & 0x0400) lp->stats.rx_frame_errors++;
 370             if ((lp->scb.rfd->stat) & 0x0800) lp->stats.rx_crc_errors++;
 371             if ((lp->scb.rfd->stat) & 0x1000) lp->stats.rx_length_errors++;
 372         }
 373 
 374         lp->scb.rfd->stat = 0;
 375         lp->rx_tail->cmd = 0;
 376         lp->rx_tail = lp->scb.rfd;
 377         lp->scb.rfd = lp->scb.rfd->next;
 378         lp->rx_tail->count = 0;
 379         lp->rx_tail->cmd = CMD_EOL;
 380 
 381     }
 382 
 383     if (i596_debug > 3) printk ("frames %d\n", frames);
 384 
 385     return 0;
 386 }
 387 
 388 static inline void
 389 i596_cleanup_cmd(struct i596_private *lp)
     /* [previous][next][first][last][top][bottom][index][help] */
 390 {
 391     struct i596_cmd *ptr;
 392     int boguscnt = 100;
 393 
 394     if (i596_debug > 4) printk ("i596_cleanup_cmd\n");
 395 
 396     while (lp->cmd_head != (struct i596_cmd *) I596_NULL)
 397     {
 398         ptr = lp->cmd_head;
 399 
 400         lp->cmd_head = lp->cmd_head->next;
 401         lp->cmd_backlog--;
 402 
 403         switch ((ptr->command) & 0x7)
 404         {
 405             case CmdTx:
 406             {
 407                 struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
 408                 struct sk_buff *skb = ((struct sk_buff *)(tx_cmd->tbd->data)) -1;
 409 
 410                 dev_kfree_skb(skb, FREE_WRITE);
 411 
 412                 lp->stats.tx_errors++;
 413                 lp->stats.tx_aborted_errors++;
 414 
 415                 ptr->next = (struct i596_cmd * ) I596_NULL;
 416                 kfree_s((unsigned char *)tx_cmd, (sizeof (struct tx_cmd) + sizeof (struct i596_tbd)));
 417                 break;
 418             }
 419             case CmdMulticastList:
 420             {
 421                 unsigned short count = *((unsigned short *) (ptr + 1));
 422 
 423                 ptr->next = (struct i596_cmd * ) I596_NULL;
 424                 kfree_s((unsigned char *)ptr, (sizeof (struct i596_cmd) + count + 2));
 425                 break;
 426             }
 427             default:
 428                 ptr->next = (struct i596_cmd * ) I596_NULL;
 429         }
 430     }
 431 
 432     while (lp->scb.status, lp->scb.command)
 433         if (--boguscnt == 0)
 434         {
 435             printk("i596_cleanup_cmd timed out with status %4.4x, cmd %4.4x.\n",
 436                 lp->scb.status, lp->scb.command);
 437             break;
 438         }
 439 
 440     lp->scb.cmd = lp->cmd_head;
 441 }
 442 
 443 static inline void
 444 i596_reset(struct device *dev, struct i596_private *lp, int ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 445 {
 446     int boguscnt = 100;
 447 
 448     if (i596_debug > 4) printk ("i596_reset\n");
 449 
 450     while (lp->scb.status, lp->scb.command)
 451         if (--boguscnt == 0)
 452         {
 453             printk("i596_reset timed out with status %4.4x, cmd %4.4x.\n",
 454                 lp->scb.status, lp->scb.command);
 455             break;
 456         }
 457 
 458     dev->start = 0;
 459     dev->tbusy = 1;
 460 
 461     lp->scb.command = CUC_ABORT|RX_ABORT;
 462     outw(0, ioaddr+4);
 463 
 464     /* wait for shutdown */
 465     boguscnt = 400;
 466 
 467     while ((lp->scb.status, lp->scb.command) || lp->scb.command)
 468         if (--boguscnt == 0)
 469         {
 470             printk("i596_reset 2 timed out with status %4.4x, cmd %4.4x.\n",
 471                 lp->scb.status, lp->scb.command);
 472             break;
 473         }
 474 
 475     i596_cleanup_cmd(lp);
 476     i596_rx(dev);
 477 
 478     dev->start = 1;
 479     dev->tbusy = 0;
 480     dev->interrupt = 0;
 481     init_i596_mem(dev);
 482 }
 483 
 484 static void i596_add_cmd(struct device *dev, struct i596_cmd *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 485 {
 486     struct i596_private *lp = (struct i596_private *)dev->priv;
 487     int ioaddr = dev->base_addr;
 488     unsigned long flags;
 489     int boguscnt = 100;
 490 
 491     if (i596_debug > 4) printk ("i596_add_cmd\n");
 492 
 493     cmd->status = 0;
 494     cmd->command |= (CMD_EOL|CMD_INTR);
 495     cmd->next = (struct i596_cmd *) I596_NULL;
 496 
 497     save_flags(flags);
 498     cli();
 499     if (lp->cmd_head != (struct i596_cmd *) I596_NULL)
 500         lp->cmd_tail->next = cmd;
 501     else 
 502     {
 503         lp->cmd_head = cmd;
 504         while (lp->scb.status, lp->scb.command)
 505             if (--boguscnt == 0)
 506             {
 507                 printk("i596_add_cmd timed out with status %4.4x, cmd %4.4x.\n",
 508                    lp->scb.status, lp->scb.command);
 509                 break;
 510             }
 511 
 512         lp->scb.cmd = cmd;
 513         lp->scb.command = CUC_START;
 514         outw (0, ioaddr+4);
 515     }
 516     lp->cmd_tail = cmd;
 517     lp->cmd_backlog++;
 518 
 519     lp->cmd_head = lp->scb.cmd;
 520     restore_flags(flags);
 521 
 522     if (lp->cmd_backlog > 16) 
 523     {
 524         int tickssofar = jiffies - lp->last_cmd;
 525 
 526         if (tickssofar < 25) return;
 527 
 528         printk("%s: command unit timed out, status resetting.\n", dev->name);
 529 
 530         i596_reset(dev, lp, ioaddr);
 531     }
 532 }
 533 
 534 static int
 535 i596_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 536 {
 537     int i;
 538 
 539     if (i596_debug > 1)
 540         printk("%s: i596_open() irq %d.\n", dev->name, dev->irq);
 541 
 542     if (request_irq(dev->irq, &i596_interrupt, 0, "apricot"))
 543         return -EAGAIN;
 544 
 545     irq2dev_map[dev->irq] = dev;
 546 
 547     i = init_rx_bufs(dev, RX_RING_SIZE);
 548 
 549     if ((i = init_rx_bufs(dev, RX_RING_SIZE)) < RX_RING_SIZE)
 550         printk("%s: only able to allocate %d receive buffers\n", dev->name, i);
 551 
 552     if (i < 4)
 553     {
 554         free_irq(dev->irq);
 555         irq2dev_map[dev->irq] = 0;
 556         return -EAGAIN;
 557     }
 558 
 559     dev->tbusy = 0;
 560     dev->interrupt = 0;
 561     dev->start = 1;
 562     MOD_INC_USE_COUNT;
 563 
 564     /* Initialize the 82596 memory */
 565     init_i596_mem(dev);
 566 
 567     return 0;                   /* Always succeed */
 568 }
 569 
 570 static int
 571 i596_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 572 {
 573     struct i596_private *lp = (struct i596_private *)dev->priv;
 574     int ioaddr = dev->base_addr;
 575     struct tx_cmd *tx_cmd;
 576 
 577     if (i596_debug > 2) printk ("%s: Apricot start xmit\n", dev->name);
 578 
 579     /* Transmitter timeout, serious problems. */
 580     if (dev->tbusy) {
 581         int tickssofar = jiffies - dev->trans_start;
 582         if (tickssofar < 5)
 583             return 1;
 584         printk("%s: transmit timed out, status resetting.\n",
 585                dev->name);
 586         lp->stats.tx_errors++;
 587         /* Try to restart the adaptor */
 588         if (lp->last_restart == lp->stats.tx_packets) {
 589             if (i596_debug > 1) printk ("Resetting board.\n");
 590 
 591             /* Shutdown and restart */
 592             i596_reset(dev,lp, ioaddr);
 593         } else {
 594             /* Issue a channel attention signal */
 595             if (i596_debug > 1) printk ("Kicking board.\n");
 596 
 597             lp->scb.command = CUC_START|RX_START;
 598             outw(0, ioaddr+4);
 599 
 600             lp->last_restart = lp->stats.tx_packets;
 601         }
 602         dev->tbusy = 0;
 603         dev->trans_start = jiffies;
 604     }
 605 
 606     /* If some higher level thinks we've misses a tx-done interrupt
 607        we are passed NULL. n.b. dev_tint handles the cli()/sti()
 608        itself. */
 609     if (skb == NULL) {
 610         dev_tint(dev);
 611         return 0;
 612     }
 613 
 614     /* shouldn't happen */
 615     if (skb->len <= 0) return 0;
 616 
 617     if (i596_debug > 3) printk("%s: i596_start_xmit() called\n", dev->name);
 618 
 619     /* Block a timer-based transmit from overlapping.  This could better be
 620        done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
 621     if (set_bit(0, (void*)&dev->tbusy) != 0)
 622         printk("%s: Transmitter access conflict.\n", dev->name);
 623     else
 624     {
 625         short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
 626         dev->trans_start = jiffies;
 627 
 628         tx_cmd = (struct tx_cmd *) kmalloc ((sizeof (struct tx_cmd) + sizeof (struct i596_tbd)), GFP_ATOMIC);
 629         if (tx_cmd == NULL)
 630         {
 631             printk ("%s: i596_xmit Memory squeeze, dropping packet.\n", dev->name);
 632             lp->stats.tx_dropped++;
 633 
 634             dev_kfree_skb(skb, FREE_WRITE);
 635         }
 636         else
 637         {
 638             tx_cmd->tbd = (struct i596_tbd *) (tx_cmd + 1);
 639             tx_cmd->tbd->next = (struct i596_tbd *) I596_NULL;
 640 
 641             tx_cmd->cmd.command = CMD_FLEX|CmdTx;
 642 
 643             tx_cmd->pad = 0;
 644             tx_cmd->size = 0;
 645             tx_cmd->tbd->pad = 0;
 646             tx_cmd->tbd->size = EOF | length;
 647 
 648             tx_cmd->tbd->data = skb->data;
 649 
 650             if (i596_debug > 3) print_eth(skb->data);
 651 
 652             i596_add_cmd(dev, (struct i596_cmd *)tx_cmd);
 653 
 654             lp->stats.tx_packets++;
 655         }
 656     }
 657 
 658     dev->tbusy = 0;
 659 
 660     return 0;
 661 }
 662 
 663 
 664 static void print_eth(char *add)
     /* [previous][next][first][last][top][bottom][index][help] */
 665 {
 666     int i;
 667 
 668     printk ("Dest  ");
 669     for (i = 0; i < 6; i++)
 670         printk(" %2.2X", (unsigned char)add[i]);
 671     printk ("\n");
 672 
 673     printk ("Source");
 674     for (i = 0; i < 6; i++)
 675         printk(" %2.2X", (unsigned char)add[i+6]);
 676     printk ("\n");
 677     printk ("type %2.2X%2.2X\n", (unsigned char)add[12], (unsigned char)add[13]);
 678 }
 679 
 680 int apricot_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 681 {
 682     int i;
 683     struct i596_private *lp;
 684     int checksum = 0;
 685     int ioaddr = 0x300;
 686     char eth_addr[6];
 687     
 688     /* this is easy the ethernet interface can only be at 0x300 */
 689     /* first check nothing is already registered here */
 690 
 691     if (check_region(ioaddr, APRICOT_TOTAL_SIZE))
 692         return ENODEV;
 693 
 694     for (i = 0; i < 8; i++)
 695     {
 696         eth_addr[i] = inb(ioaddr+8+i);
 697         checksum += eth_addr[i];
 698      }
 699 
 700     /* checksum is a multiple of 0x100, got this wrong first time
 701        some machines have 0x100, some 0x200. The DOS driver doesn't
 702        even bother with the checksum */
 703 
 704     if (checksum % 0x100) return ENODEV;
 705 
 706     /* Some other boards trip the checksum.. but then appear as ether
 707        address 0. Trap these - AC */
 708        
 709     if(memcmp(eth_addr,"\x00\x00\x49",3)!= 0)
 710         return ENODEV;
 711 
 712     request_region(ioaddr, APRICOT_TOTAL_SIZE, "apricot");
 713 
 714     dev->base_addr = ioaddr;
 715     ether_setup(dev);
 716     printk("%s: Apricot 82596 at %#3x,", dev->name, ioaddr);
 717 
 718     for (i = 0; i < 6; i++)
 719         printk(" %2.2X", dev->dev_addr[i] = eth_addr[i]);
 720 
 721     dev->base_addr = ioaddr;
 722     dev->irq = 10;
 723     printk(" IRQ %d.\n", dev->irq);
 724 
 725     if (i596_debug > 0) printk(version);
 726 
 727     /* The APRICOT-specific entries in the device structure. */
 728     dev->open = &i596_open;
 729     dev->stop = &i596_close;
 730     dev->hard_start_xmit = &i596_start_xmit;
 731     dev->get_stats = &i596_get_stats;
 732 #ifdef HAVE_MULTICAST
 733     dev->set_multicast_list = &set_multicast_list;
 734 #endif
 735 
 736     dev->mem_start = (int)kmalloc(sizeof(struct i596_private)+ 0x0f, GFP_KERNEL);
 737     /* align for scp */
 738     dev->priv = (void *)((dev->mem_start + 0xf) & 0xfffffff0);
 739 
 740     lp = (struct i596_private *)dev->priv;
 741     memset((void *)lp, 0, sizeof(struct i596_private));
 742     lp->scb.command = 0;
 743     lp->scb.cmd = (struct i596_cmd *) I596_NULL;
 744     lp->scb.rfd = (struct i596_rfd *)I596_NULL;
 745 
 746     return 0;
 747 }
 748 
 749 static void
 750 i596_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 751 {
 752     struct device *dev = (struct device *)(irq2dev_map[irq]);
 753     struct i596_private *lp;
 754     short ioaddr;
 755     int boguscnt = 200;
 756     unsigned short status, ack_cmd = 0;
 757 
 758     if (dev == NULL) {
 759         printk ("i596_interrupt(): irq %d for unknown device.\n", irq);
 760         return;
 761     }
 762 
 763     if (i596_debug > 3) printk ("%s: i596_interrupt(): irq %d\n",dev->name, irq);
 764 
 765     if (dev->interrupt)
 766         printk("%s: Re-entering the interrupt handler.\n", dev->name);
 767 
 768     dev->interrupt = 1;
 769 
 770     ioaddr = dev->base_addr;
 771 
 772     lp = (struct i596_private *)dev->priv;
 773 
 774     while (lp->scb.status, lp->scb.command)
 775         if (--boguscnt == 0)
 776             {
 777                 printk("%s: i596 interrupt, timeout status %4.4x command %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
 778                 break;
 779             }
 780     status = lp->scb.status;
 781 
 782     if (i596_debug > 4)
 783         printk("%s: i596 interrupt, status %4.4x.\n", dev->name, status);
 784 
 785     ack_cmd = status & 0xf000;
 786 
 787     if ((status & 0x8000) || (status & 0x2000))
 788     {
 789         struct i596_cmd *ptr;
 790 
 791         if ((i596_debug > 4) && (status & 0x8000))
 792             printk("%s: i596 interrupt completed command.\n", dev->name);
 793         if ((i596_debug > 4) && (status & 0x2000))
 794             printk("%s: i596 interrupt command unit inactive %x.\n", dev->name, status & 0x0700);
 795 
 796         while ((lp->cmd_head != (struct i596_cmd *) I596_NULL) && (lp->cmd_head->status & STAT_C))
 797         {
 798             ptr = lp->cmd_head;
 799 
 800             lp->cmd_head = lp->cmd_head->next;
 801             lp->cmd_backlog--;
 802 
 803             switch ((ptr->command) & 0x7)
 804             {
 805                 case CmdTx:
 806                 {
 807                     struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
 808                     struct sk_buff *skb = ((struct sk_buff *)(tx_cmd->tbd->data)) -1;
 809 
 810                     dev_kfree_skb(skb, FREE_WRITE);
 811 
 812                     if ((ptr->status) & STAT_OK)
 813                     {
 814                         if (i596_debug >2) print_eth(skb->data);
 815                     }
 816                     else
 817                     {
 818                         lp->stats.tx_errors++;
 819                         if ((ptr->status) & 0x0020) lp->stats.collisions++;
 820                         if (!((ptr->status) & 0x0040)) lp->stats.tx_heartbeat_errors++;
 821                         if ((ptr->status) & 0x0400) lp->stats.tx_carrier_errors++;
 822                         if ((ptr->status) & 0x0800) lp->stats.collisions++;
 823                         if ((ptr->status) & 0x1000) lp->stats.tx_aborted_errors++;
 824                     }
 825 
 826 
 827                     ptr->next = (struct i596_cmd * ) I596_NULL;
 828                     kfree_s((unsigned char *)tx_cmd, (sizeof (struct tx_cmd) + sizeof (struct i596_tbd)));
 829                     break;
 830                 }
 831                 case CmdMulticastList:
 832                 {
 833                     unsigned short count = *((unsigned short *) (ptr + 1));
 834 
 835                     ptr->next = (struct i596_cmd * ) I596_NULL;
 836                     kfree_s((unsigned char *)ptr, (sizeof (struct i596_cmd) + count + 2));
 837                     break;
 838                 }
 839                 case CmdTDR:
 840                 {
 841                     unsigned long status = *((unsigned long *) (ptr + 1));
 842 
 843                     if (status & 0x8000)
 844                     {
 845                         if (i596_debug > 3)
 846                             printk("%s: link ok.\n", dev->name);
 847                     }
 848                     else
 849                     {
 850                         if (status & 0x4000)
 851                             printk("%s: Transceiver problem.\n", dev->name);
 852                         if (status & 0x2000)
 853                             printk("%s: Termination problem.\n", dev->name);
 854                         if (status & 0x1000)
 855                             printk("%s: Short circuit.\n", dev->name);
 856 
 857                         printk("%s: Time %ld.\n", dev->name, status & 0x07ff);
 858                     }
 859                 }
 860                 default:
 861                     ptr->next = (struct i596_cmd * ) I596_NULL;
 862 
 863                 lp->last_cmd = jiffies;
 864             }
 865         }
 866 
 867         ptr = lp->cmd_head;
 868         while ((ptr != (struct i596_cmd *) I596_NULL) && (ptr != lp->cmd_tail))
 869         {
 870             ptr->command &= 0x1fff;
 871             ptr = ptr->next;
 872         }
 873 
 874         if ((lp->cmd_head != (struct i596_cmd *) I596_NULL) && (dev->start)) ack_cmd |= CUC_START;
 875         lp->scb.cmd = lp->cmd_head;
 876     }
 877 
 878     if ((status & 0x1000) || (status & 0x4000))
 879     {
 880         if ((i596_debug > 4) && (status & 0x4000))
 881             printk("%s: i596 interrupt received a frame.\n", dev->name);
 882         if ((i596_debug > 4) && (status & 0x1000))
 883             printk("%s: i596 interrupt receive unit inactive %x.\n", dev->name, status & 0x0070);
 884 
 885         i596_rx(dev);
 886 
 887         if (dev->start) ack_cmd |= RX_START;
 888     }
 889 
 890     /* acknowledge the interrupt */
 891 
 892 /*
 893     if ((lp->scb.cmd != (struct i596_cmd *) I596_NULL) && (dev->start)) ack_cmd | = CUC_START;
 894 */
 895     boguscnt = 100;
 896     while (lp->scb.status, lp->scb.command)
 897         if (--boguscnt == 0)
 898             {
 899                 printk("%s: i596 interrupt, timeout status %4.4x command %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
 900                 break;
 901             }
 902     lp->scb.command = ack_cmd;
 903 
 904     (void) inb (ioaddr+0x10);
 905     outb (4, ioaddr+0xf);
 906     outw (0, ioaddr+4);
 907 
 908     if (i596_debug > 4)
 909         printk("%s: exiting interrupt.\n", dev->name);
 910 
 911     dev->interrupt = 0;
 912     return;
 913 }
 914 
 915 static int
 916 i596_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 917 {
 918     int ioaddr = dev->base_addr;
 919     struct i596_private *lp = (struct i596_private *)dev->priv;
 920     int boguscnt = 200;
 921 
 922     dev->start = 0;
 923     dev->tbusy = 1;
 924 
 925     if (i596_debug > 1)
 926         printk("%s: Shutting down ethercard, status was %4.4x.\n",
 927                dev->name, lp->scb.status);
 928 
 929     lp->scb.command = CUC_ABORT|RX_ABORT;
 930     outw(0, ioaddr+4);
 931 
 932     i596_cleanup_cmd(lp);
 933 
 934     while (lp->scb.status, lp->scb.command)
 935         if (--boguscnt == 0)
 936         {
 937             printk("%s: close timed timed out with status %4.4x, cmd %4.4x.\n",
 938                    dev->name, lp->scb.status, lp->scb.command);
 939             break;
 940         }
 941     free_irq(dev->irq);
 942     irq2dev_map[dev->irq] = 0;
 943     remove_rx_bufs(dev);
 944     MOD_DEC_USE_COUNT;
 945 
 946     return 0;
 947 }
 948 
 949 static struct enet_statistics *
 950 i596_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 951 {
 952     struct i596_private *lp = (struct i596_private *)dev->priv;
 953 
 954     return &lp->stats;
 955 }
 956 
 957 #ifdef HAVE_MULTICAST
 958 /* Set or clear the multicast filter for this adaptor.
 959    num_addrs == -1      Promiscuous mode, receive all packets
 960    num_addrs == 0       Normal mode, clear multicast list
 961    num_addrs > 0        Multicast mode, receive normal and MC packets, and do
 962                         best-effort filtering.
 963  */
 964 static void
 965 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
 966 {
 967     struct i596_private *lp = (struct i596_private *)dev->priv;
 968     struct i596_cmd *cmd;
 969 
 970     if (i596_debug > 1)
 971         printk ("%s: set multicast list %d\n", dev->name, num_addrs);
 972 
 973     if (num_addrs > 0) {
 974         cmd = (struct i596_cmd *) kmalloc(sizeof(struct i596_cmd)+2+num_addrs*6, GFP_ATOMIC);
 975         if (cmd == NULL)
 976         {
 977             printk ("%s: set_multicast Memory squeeze.\n", dev->name);
 978             return;
 979         }
 980 
 981             cmd->command = CmdMulticastList;
 982             *((unsigned short *) (cmd + 1)) = num_addrs * 6;
 983             memcpy (((char *)(cmd + 1))+2, addrs, num_addrs * 6);
 984             print_eth (((char *)(cmd + 1)) + 2);
 985                 
 986             i596_add_cmd(dev, cmd);
 987     } else
 988     {
 989         if (lp->set_conf.next != (struct i596_cmd * ) I596_NULL) return;
 990         if (num_addrs == 0)
 991             lp->i596_config[8] &= ~0x01;
 992         else
 993             lp->i596_config[8] |= 0x01;
 994 
 995         i596_add_cmd(dev, &lp->set_conf);
 996     }
 997 
 998 }
 999 #endif
1000 
1001 #ifdef HAVE_DEVLIST
1002 static unsigned int apricot_portlist[] = {0x300, 0};
1003 struct netdev_entry apricot_drv = 
1004 {"apricot", apricot_probe, APRICOT_TOTAL_SIZE, apricot_portlist};
1005 #endif
1006 
1007 #ifdef MODULE
1008 static char devicename[9] = { 0, };
1009 static struct device dev_apricot = {
1010   devicename, /* device name inserted by /linux/drivers/net/net_init.c */
1011   0, 0, 0, 0,
1012   0x300, 10,
1013   0, 0, 0, NULL, apricot_probe };
1014 
1015 static int io = 0x300;
1016 static int irq = 10;
1017 
1018 int
1019 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1020 {
1021   dev_apricot.base_addr = io;
1022   dev_apricot.irq       = irq;
1023   if (register_netdev(&dev_apricot) != 0)
1024     return -EIO;
1025   return 0;
1026 }
1027 
1028 void
1029 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1030 {
1031     unregister_netdev(&dev_apricot);
1032     kfree_s((void *)dev_apricot.mem_start, sizeof(struct i596_private) + 0xf);
1033     dev_apricot.priv = NULL;
1034 
1035     /* If we don't do this, we can't re-insmod it later. */
1036     release_region(dev_apricot.base_addr, APRICOT_TOTAL_SIZE);
1037 }
1038 #endif /* MODULE */
1039 
1040 /*
1041  * Local variables:
1042  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c apricot.c"
1043  * End:
1044  */

/* [previous][next][first][last][top][bottom][index][help] */