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

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