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

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