root/drivers/net/3c501.c

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

DEFINITIONS

This source file includes following definitions.
  1. el1_probe
  2. el_open
  3. el_start_xmit
  4. el_interrupt
  5. el_receive
  6. el_reset
  7. el1_close
  8. el1_get_stats
  9. set_multicast_list

   1 /* 3c501.c: A 3Com 3c501 ethernet driver for linux. */
   2 /*
   3     Copyright (C) 1992,1993  Donald Becker
   4 
   5     Copyright 1993 United States Government as represented by the
   6     Director, National Security Agency.  This software may be used and
   7     distributed according to the terms of the GNU Public License,
   8     incorporated herein by reference.
   9 
  10     This is a device driver for the 3Com Etherlink 3c501.
  11     Do not purchase this card, even as a joke.  It's performance is horrible,
  12     and it breaks in many ways.  
  13 
  14     The Author may be reached as becker@super.org or
  15     C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
  16     I'll only accept bug fixes, not reports, for the 3c501 driver.
  17 */
  18 
  19 static char *version =
  20     "3c501.c: 3/3/94 Donald Becker (becker@super.org).\n";
  21 
  22 /*
  23   Braindamage remaining:
  24   The 3c501 board.
  25   */
  26 
  27 #include <linux/config.h>
  28 #include <linux/kernel.h>
  29 #include <linux/sched.h>
  30 #include <linux/ptrace.h>
  31 #include <linux/fcntl.h>
  32 #include <linux/ioport.h>
  33 #include <linux/interrupt.h>
  34 #include <linux/malloc.h>
  35 #include <linux/ioport.h>
  36 #include <asm/bitops.h>
  37 #include <asm/io.h>
  38 #include <errno.h>
  39 
  40 #include <linux/netdevice.h>
  41 #include <linux/etherdevice.h>
  42 #include <linux/skbuff.h>
  43 
  44 #ifndef HAVE_AUTOIRQ
  45 /* From auto_irq.c, should be in a *.h file. */
  46 extern void autoirq_setup(int waittime);
  47 extern int autoirq_report(int waittime);
  48 extern struct device *irq2dev_map[16];
  49 #endif
  50 
  51 
  52 /* Index to functions. */
  53 int el1_probe(struct device *dev);
  54 static int  el_open(struct device *dev);
  55 static int  el_start_xmit(struct sk_buff *skb, struct device *dev);
  56 static void el_interrupt(int reg_ptr);
  57 static void el_receive(struct device *dev);
  58 static void el_reset(struct device *dev);
  59 static int  el1_close(struct device *dev);
  60 static struct enet_statistics *el1_get_stats(struct device *dev);
  61 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
  62 
  63 #define EL_NAME "EtherLink 3c501"
  64 
  65 #ifndef EL_DEBUG
  66 #define EL_DEBUG  2     /* use 0 for production, 1 for devel., >2 for debug */
  67 #endif                  /* Anything above 5 is wordy death! */
  68 static int el_debug = EL_DEBUG;
  69 static int el_base;
  70 static struct device *eldev;    /* Only for consistency checking.  */
  71  
  72 /* We could easily have this struct kmalloc()ed per-board, but
  73    who would want more than one 3c501?. */
  74 static struct {
  75     struct enet_statistics stats;
  76     int tx_pkt_start;           /* The length of the current Tx packet. */
  77     int collisions;             /* Tx collisions this packet */
  78 } el_status;                    /* This should be stored per-board */
  79 
  80 
  81 #define RX_STATUS (el_base + 0x06)
  82 #define RX_CMD    RX_STATUS
  83 #define TX_STATUS (el_base + 0x07)
  84 #define TX_CMD    TX_STATUS
  85 #define GP_LOW    (el_base + 0x08)
  86 #define GP_HIGH   (el_base + 0x09)
  87 #define RX_BUF_CLR (el_base + 0x0A)
  88 #define RX_LOW    (el_base + 0x0A)
  89 #define RX_HIGH   (el_base + 0x0B)
  90 #define SAPROM    (el_base + 0x0C)
  91 #define AX_STATUS (el_base + 0x0E)
  92 #define AX_CMD    AX_STATUS
  93 #define DATAPORT  (el_base + 0x0F)
  94 #define TX_RDY 0x08             /* In TX_STATUS */
  95 
  96 #define EL1_DATAPTR     0x08
  97 #define EL1_RXPTR       0x0A
  98 #define EL1_SAPROM      0x0C
  99 #define EL1_DATAPORT    0x0f
 100 
 101 /* Writes to the ax command register. */
 102 #define AX_OFF  0x00                    /* Irq off, buffer access on */
 103 #define AX_SYS  0x40                    /* Load the buffer */
 104 #define AX_XMIT 0x44                    /* Transmit a packet */
 105 #define AX_RX   0x48                    /* Receive a packet */
 106 #define AX_LOOP 0x0C                    /* Loopback mode */
 107 #define AX_RESET 0x80
 108 
 109 /* Normal receive mode written to RX_STATUS.  We must intr on short packets
 110    to avoid bogus rx lockups. */
 111 #define RX_NORM 0xA8            /* 0x68 == all addrs, 0xA8 only to me. */
 112 #define RX_PROM 0x68            /* Senior Prom, uhmm promiscuous mode. */
 113 #define RX_MULT 0xE8            /* Accept multicast packets. */
 114 #define TX_NORM 0x0A    /* Interrupt on everything that might hang the chip */
 115 
 116 /* TX_STATUS register. */
 117 #define TX_COLLISION 0x02
 118 #define TX_16COLLISIONS 0x04
 119 #define TX_READY 0x08
 120 
 121 #define RX_RUNT 0x08
 122 #define RX_MISSED 0x01          /* Missed a packet due to 3c501 braindamage. */
 123 #define RX_GOOD 0x30            /* Good packet 0x20, or simple overflow 0x10. */
 124 
 125 
 126 int
 127 el1_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 128 {
 129     int i;
 130     int ioaddr;
 131     unsigned char station_addr[6];
 132     int autoirq = 0;
 133 
 134     eldev = dev;                /* Store for debugging. */
 135     el_base = dev->base_addr;
 136 
 137     if (el_base < 0x40)         /* Invalid?  Probe for it. */
 138         el_base = 0x280;
 139 
 140     ioaddr = el_base;
 141 
 142     /* Read the station address PROM data from the special port.  */
 143     for (i = 0; i < 6; i++) {
 144         outw(i, ioaddr + EL1_DATAPTR);
 145         station_addr[i] = inb(ioaddr + EL1_SAPROM);
 146     }
 147     /* Check the first three octets of the S.A. for 3Com's code. */ 
 148     if (station_addr[0] != 0x02  ||  station_addr[1] != 0x60
 149         || station_addr[2] != 0x8c) {
 150         return ENODEV;
 151     }
 152 
 153 #ifdef HAVE_PORTRESERVE
 154     /* Grab the region so we can find the another board if autoIRQ fails. */
 155     snarf_region(ioaddr, 16);
 156 #endif
 157 
 158     /* We auto-IRQ by shutting off the interrupt line and letting it float
 159        high. */
 160     if (dev->irq < 2) {
 161 
 162         autoirq_setup(2);
 163 
 164         inb(RX_STATUS);         /* Clear pending interrupts. */
 165         inb(TX_STATUS);
 166         outb(AX_LOOP + 1, AX_CMD);
 167 
 168         outb(0x00, AX_CMD);
 169 
 170         autoirq = autoirq_report(1);
 171 
 172         if (autoirq == 0) {
 173             printk("%s: 3c501 probe failed to detect IRQ line.\n", dev->name);
 174             return EAGAIN;
 175         }
 176         dev->irq = autoirq;
 177     }
 178 
 179     outb(AX_RESET+AX_LOOP, AX_CMD);                     /* Loopback mode. */
 180 
 181     dev->base_addr = el_base;
 182     memcpy(dev->dev_addr, station_addr, ETH_ALEN);
 183     if (dev->mem_start & 0xf)
 184         el_debug = dev->mem_start & 0x7;
 185 
 186     printk("%s: 3c501 EtherLink at %#x, using %sIRQ %d, melting ethernet.\n",
 187            dev->name, dev->base_addr, autoirq ? "auto":"assigned ", dev->irq);
 188 
 189     if (el_debug)
 190         printk("%s", version);
 191 
 192     /* The EL1-specific entries in the device structure. */
 193     dev->open = &el_open;
 194     dev->hard_start_xmit = &el_start_xmit;
 195     dev->stop = &el1_close;
 196     dev->get_stats = &el1_get_stats;
 197     dev->set_multicast_list = &set_multicast_list;
 198     /* Setup the generic properties */
 199     ether_setup(dev);
 200 
 201     return 0;
 202 }
 203 
 204 /* Open/initialize the board. */
 205 static int
 206 el_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 207 {
 208 
 209   if (el_debug > 2)
 210       printk("%s: Doing el_open()...", dev->name);
 211 
 212   if (request_irq(dev->irq, &el_interrupt)) {
 213       if (el_debug > 2)
 214           printk("interrupt busy, exiting el_open().\n");
 215       return -EAGAIN;
 216   }
 217   irq2dev_map[dev->irq] = dev;
 218 
 219   el_reset(dev);
 220 
 221   dev->start = 1;
 222 
 223   outb(AX_RX, AX_CMD);  /* Aux control, irq and receive enabled */
 224   if (el_debug > 2)
 225      printk("finished el_open().\n");
 226   return (0);
 227 }
 228 
 229 static int
 230 el_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 231 {
 232 
 233     if (dev->tbusy) {
 234         if (jiffies - dev->trans_start < 20) {
 235             if (el_debug > 2)
 236                 printk(" transmitter busy, deferred.\n");
 237             return 1;
 238         }
 239         if (el_debug)
 240             printk ("%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n",
 241                     dev->name, inb(TX_STATUS), inb(AX_STATUS), inb(RX_STATUS));
 242         el_status.stats.tx_errors++;
 243 #ifdef oldway
 244         el_reset(dev);
 245 #else
 246         outb(TX_NORM, TX_CMD);
 247         outb(RX_NORM, RX_CMD);
 248         outb(AX_OFF, AX_CMD);   /* Just trigger a false interrupt. */
 249 #endif
 250         outb(AX_RX, AX_CMD);    /* Aux control, irq and receive enabled */
 251         dev->tbusy = 0;
 252         dev->trans_start = jiffies;
 253     }
 254 
 255     if (skb == NULL) {
 256         dev_tint(dev);
 257         return 0;
 258     }
 259 
 260     if (skb->len <= 0)
 261         return 0;
 262 
 263     /* Avoid timer-based retransmission conflicts. */
 264     if (set_bit(0, (void*)&dev->tbusy) != 0)
 265         printk("%s: Transmitter access conflict.\n", dev->name);
 266     else {
 267         int gp_start = 0x800 - (ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN);
 268         unsigned char *buf = skb->data;
 269 
 270         el_status.tx_pkt_start = gp_start;
 271         el_status.collisions = 0;
 272 
 273         outb(AX_SYS, AX_CMD);
 274         inb(RX_STATUS);
 275         inb(TX_STATUS);
 276         outb(0x00, RX_BUF_CLR); /* Set rx packet area to 0. */
 277         outw(gp_start, GP_LOW);
 278         outsb(DATAPORT,buf,skb->len);
 279         outw(gp_start, GP_LOW);
 280         outb(AX_XMIT, AX_CMD);          /* Trigger xmit.  */
 281         dev->trans_start = jiffies;
 282     }
 283 
 284     if (el_debug > 2)
 285         printk(" queued xmit.\n");
 286     if (skb->free)
 287         kfree_skb (skb, FREE_WRITE);
 288     return 0;
 289 }
 290 
 291 
 292 /* The typical workload of the driver:
 293    Handle the ether interface interrupts. */
 294 static void
 295 el_interrupt(int reg_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 296 {
 297     int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
 298     /*struct device *dev = (struct device *)(irq2dev_map[irq]);*/
 299     struct device *dev = eldev;
 300     int axsr;                   /* Aux. status reg. */
 301     short ioaddr;
 302 
 303     if (eldev->irq != irq) {
 304         printk (EL_NAME ": irq %d for unknown device\n", irq);
 305         return;
 306     }
 307 
 308     ioaddr = dev->base_addr;
 309 
 310     axsr = inb(AX_STATUS);
 311 
 312     if (el_debug > 3)
 313       printk("%s: el_interrupt() aux=%#02x", dev->name, axsr);
 314     if (dev->interrupt)
 315         printk("%s: Reentering the interrupt driver!\n", dev->name);
 316     dev->interrupt = 1;
 317 
 318     if (dev->tbusy) {
 319         int txsr = inb(TX_STATUS);
 320 
 321         if (el_debug > 6)
 322             printk(" txsr=%02x gp=%04x rp=%04x", txsr, inw(GP_LOW),
 323                    inw(RX_LOW));
 324 
 325         if ((axsr & 0x80) && (txsr & TX_READY) == 0) {
 326             printk("%s: Unusual interrupt during Tx, txsr=%02x axsr=%02x"
 327                    " gp=%03x rp=%03x.\n", dev->name, txsr, axsr,
 328                    inw(ioaddr + EL1_DATAPTR), inw(ioaddr + EL1_RXPTR));
 329             dev->tbusy = 0;
 330             mark_bh(INET_BH);
 331         } else if (txsr & TX_16COLLISIONS) {
 332             if (el_debug)
 333                 printk("%s: Transmit failed 16 times, ethernet jammed?\n",
 334                        dev->name);
 335             outb(AX_SYS, AX_CMD);
 336             el_status.stats.tx_aborted_errors++;
 337         } else if (txsr & TX_COLLISION) {       /* Retrigger xmit. */
 338             if (el_debug > 6)
 339                 printk(" retransmitting after a collision.\n");
 340             outb(AX_SYS, AX_CMD);
 341             outw(el_status.tx_pkt_start, GP_LOW);
 342             outb(AX_XMIT, AX_CMD);
 343             el_status.stats.collisions++;
 344             dev->interrupt = 0;
 345             return;
 346         } else {
 347             el_status.stats.tx_packets++;
 348             if (el_debug > 6)
 349                 printk(" Tx succeeded %s\n",
 350                        (txsr & TX_RDY) ? "." : "but tx is busy!");
 351             dev->tbusy = 0;
 352             mark_bh(INET_BH);
 353         }
 354     } else {
 355         int rxsr = inb(RX_STATUS);
 356         if (el_debug > 5)
 357             printk(" rxsr=%02x txsr=%02x rp=%04x", rxsr, inb(TX_STATUS),
 358                    inw(RX_LOW));
 359 
 360         /* Just reading rx_status fixes most errors. */
 361         if (rxsr & RX_MISSED)
 362             el_status.stats.rx_missed_errors++;
 363         if (rxsr & RX_RUNT) {   /* Handled to avoid board lock-up. */
 364             el_status.stats.rx_length_errors++;
 365             if (el_debug > 5) printk(" runt.\n");
 366         } else if (rxsr & RX_GOOD) {
 367             el_receive(eldev);
 368         } else {                        /* Nothing?  Something is broken! */
 369             if (el_debug > 2)
 370                 printk("%s: No packet seen, rxsr=%02x **resetting 3c501***\n",
 371                        dev->name, rxsr);
 372             el_reset(eldev);
 373         }
 374         if (el_debug > 3)
 375             printk(".\n");
 376     }
 377 
 378     outb(AX_RX, AX_CMD);
 379     outb(0x00, RX_BUF_CLR);
 380     inb(RX_STATUS);             /* Be certain that interrupts are cleared. */
 381     inb(TX_STATUS);
 382     dev->interrupt = 0;
 383     return;
 384 }
 385 
 386 
 387 /* We have a good packet. Well, not really "good", just mostly not broken.
 388    We must check everything to see if it is good. */
 389 static void
 390 el_receive(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 391 {
 392     int pkt_len;
 393     struct sk_buff *skb;
 394 
 395     pkt_len = inw(RX_LOW);
 396 
 397     if (el_debug > 4)
 398       printk(" el_receive %d.\n", pkt_len);
 399 
 400     if ((pkt_len < 60)  ||  (pkt_len > 1536)) {
 401         if (el_debug)
 402           printk("%s: bogus packet, length=%d\n", dev->name, pkt_len);
 403         el_status.stats.rx_over_errors++;
 404         return;
 405     }
 406     outb(AX_SYS, AX_CMD);
 407 
 408     skb = alloc_skb(pkt_len, GFP_ATOMIC);
 409     outw(0x00, GP_LOW);
 410     if (skb == NULL) {
 411         printk("%s: Memory squeeze, dropping packet.\n", dev->name);
 412         el_status.stats.rx_dropped++;
 413         return;
 414     } else {
 415         skb->len = pkt_len;
 416         skb->dev = dev;
 417 
 418         insb(DATAPORT, skb->data, pkt_len);
 419 
 420         netif_rx(skb);
 421         el_status.stats.rx_packets++;
 422     }
 423     return;
 424 }
 425 
 426 static void 
 427 el_reset(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 428 {
 429     if (el_debug> 2)
 430         printk("3c501 reset...");
 431     outb(AX_RESET, AX_CMD);     /* Reset the chip */
 432     outb(AX_LOOP, AX_CMD);      /* Aux control, irq and loopback enabled */
 433     {
 434         int i;
 435         for (i = 0; i < 6; i++) /* Set the station address. */
 436             outb(dev->dev_addr[i], el_base + i);
 437     }
 438     
 439     outb(0, RX_BUF_CLR);                /* Set rx packet area to 0. */
 440     cli();                      /* Avoid glitch on writes to CMD regs */
 441     outb(TX_NORM, TX_CMD);              /* tx irq on done, collision */
 442     outb(RX_NORM, RX_CMD);      /* Set Rx commands. */
 443     inb(RX_STATUS);             /* Clear status. */
 444     inb(TX_STATUS);
 445     dev->interrupt = 0;
 446     dev->tbusy = 0;
 447     sti();
 448 }
 449 
 450 static int
 451 el1_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 452 {
 453     int ioaddr = dev->base_addr;
 454 
 455     if (el_debug > 2)
 456         printk("%s: Shutting down ethercard at %#x.\n", dev->name, ioaddr);
 457 
 458     dev->tbusy = 1;
 459     dev->start = 0;
 460 
 461     /* Free and disable the IRQ. */
 462     free_irq(dev->irq);
 463     outb(AX_RESET, AX_CMD);     /* Reset the chip */
 464     irq2dev_map[dev->irq] = 0;
 465 
 466     return 0;
 467 }
 468 
 469 static struct enet_statistics *
 470 el1_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 471 {
 472     return &el_status.stats;
 473 }
 474 
 475 /* Set or clear the multicast filter for this adaptor.
 476    num_addrs == -1      Promiscuous mode, receive all packets
 477    num_addrs == 0       Normal mode, clear multicast list
 478    num_addrs > 0        Multicast mode, receive normal and MC packets, and do
 479                         best-effort filtering.
 480  */
 481 static void
 482 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
 483 {
 484     if (num_addrs > 0) {
 485         outb(RX_MULT, RX_CMD);
 486         inb(RX_STATUS);         /* Clear status. */
 487     } else if (num_addrs < 0) {
 488         outb(RX_PROM, RX_CMD);
 489         inb(RX_STATUS);
 490     } else {
 491         outb(RX_NORM, RX_CMD);
 492         inb(RX_STATUS);
 493     }
 494 }
 495 
 496 /*
 497  * Local variables:
 498  *  compile-command: "gcc -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer  -m486 -c -o 3c501.o 3c501.c"
 499  *  kept-new-versions: 5
 500  * End:
 501  */

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