root/drivers/net/3c501.c

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

DEFINITIONS

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

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

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