root/drivers/net/3c509.c

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

DEFINITIONS

This source file includes following definitions.
  1. el3_probe
  2. read_eeprom
  3. id_read_eeprom
  4. el3_open
  5. el3_start_xmit
  6. el3_interrupt
  7. el3_get_stats
  8. update_stats
  9. el3_rx
  10. set_multicast_list
  11. el3_close
  12. init_module
  13. cleanup_module

   1 /* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */
   2 /*
   3         Written 1993-1995 by Donald Becker.
   4 
   5         Copyright 1994,1995 by Donald Becker.
   6         Copyright 1993 United States Government as represented by the
   7         Director, National Security Agency.      This software may be used and
   8         distributed according to the terms of the GNU Public License,
   9         incorporated herein by reference.
  10 
  11         This driver is for the 3Com EtherLinkIII series.
  12 
  13         The author may be reached as becker@cesdis.gsfc.nasa.gov or
  14         C/O Center of Excellence in Space Data and Information Sciences
  15                 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
  16 
  17         Known limitations:
  18         Because of the way 3c509 ISA detection works it's difficult to predict
  19         a priori which of several ISA-mode cards will be detected first.
  20 
  21         This driver does not use predictive interrupt mode, resulting in higher
  22         packet latency but lower overhead.  If interrupts are disabled for an
  23         unusually long time it could also result in missed packets, but in
  24         practice this rarely happens.
  25 
  26 
  27         FIXES:
  28                 Alan Cox:       Removed the 'Unexpected interrupt' bug.
  29                 Michael Meskes: Upgraded to Donald Becker's version 1.07.
  30 */
  31 
  32 static char *version = "3c509.c:1.07 6/15/95 becker@cesdis.gsfc.nasa.gov\n";
  33 
  34 #include <linux/module.h>
  35 
  36 #include <linux/config.h>
  37 #include <linux/kernel.h>
  38 #include <linux/sched.h>
  39 #include <linux/string.h>
  40 #include <linux/interrupt.h>
  41 #include <linux/ptrace.h>
  42 #include <linux/errno.h>
  43 #include <linux/in.h>
  44 #include <linux/malloc.h>
  45 #include <linux/ioport.h>
  46 #include <linux/netdevice.h>
  47 #include <linux/etherdevice.h>
  48 #include <linux/skbuff.h>
  49 #include <linux/config.h>       /* for CONFIG_MCA */
  50 
  51 #include <asm/bitops.h>
  52 #include <asm/io.h>
  53 
  54 
  55 #ifdef EL3_DEBUG
  56 int el3_debug = EL3_DEBUG;
  57 #else
  58 int el3_debug = 2;
  59 #endif
  60 
  61 /* To minimize the size of the driver source I only define operating
  62    constants if they are used several times.  You'll need the manual
  63    if you want to understand driver details. */
  64 /* Offsets from base I/O address. */
  65 #define EL3_DATA 0x00
  66 #define EL3_CMD 0x0e
  67 #define EL3_STATUS 0x0e
  68 #define  EEPROM_READ 0x80
  69 
  70 #define EL3_IO_EXTENT   16
  71 
  72 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
  73 
  74 
  75 /* The top five bits written to EL3_CMD are a command, the lower
  76    11 bits are the parameter, if applicable. */
  77 enum c509cmd {
  78         TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
  79         RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
  80         TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
  81         FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
  82         SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
  83         SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
  84         StatsDisable = 22<<11, StopCoax = 23<<11,};
  85 
  86 enum c509status {
  87         IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
  88         TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
  89         IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, };
  90 
  91 /* The SetRxFilter command accepts the following classes: */
  92 enum RxFilter {
  93         RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
  94 
  95 /* Register window 1 offsets, the window used in normal operation. */
  96 #define TX_FIFO         0x00
  97 #define RX_FIFO         0x00
  98 #define RX_STATUS       0x08
  99 #define TX_STATUS       0x0B
 100 #define TX_FREE         0x0C            /* Remaining free bytes in Tx buffer. */
 101 
 102 #define WN0_IRQ         0x08            /* Window 0: Set IRQ line in bits 12-15. */
 103 #define WN4_MEDIA       0x0A            /* Window 4: Various transcvr/media bits. */
 104 #define  MEDIA_TP       0x00C0          /* Enable link beat and jabber for 10baseT. */
 105 
 106 /*
 107  * Must be a power of two (we use a binary and in the
 108  * circular queue)
 109  */
 110 #define SKB_QUEUE_SIZE  64
 111 
 112 struct el3_private {
 113         struct enet_statistics stats;
 114         /* skb send-queue */
 115         int head, size;
 116         struct sk_buff *queue[SKB_QUEUE_SIZE];
 117 };
 118 static int id_port = 0x100;
 119 
 120 static ushort id_read_eeprom(int index);
 121 static ushort read_eeprom(short ioaddr, int index);
 122 static int el3_open(struct device *dev);
 123 static int el3_start_xmit(struct sk_buff *skb, struct device *dev);
 124 static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 125 static void update_stats(int addr, struct device *dev);
 126 static struct enet_statistics *el3_get_stats(struct device *dev);
 127 static int el3_rx(struct device *dev);
 128 static int el3_close(struct device *dev);
 129 #ifdef HAVE_MULTICAST
 130 static void set_multicast_list(struct device *dev);
 131 #endif
 132 
 133 
 134 
 135 int el3_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 136 {
 137         short lrs_state = 0xff, i;
 138         ushort ioaddr, irq, if_port;
 139         short *phys_addr = (short *)dev->dev_addr;
 140         static int current_tag = 0;
 141 
 142         /* First check all slots of the EISA bus.  The next slot address to
 143            probe is kept in 'eisa_addr' to support multiple probe() calls. */
 144         if (EISA_bus) {
 145                 static int eisa_addr = 0x1000;
 146                 while (eisa_addr < 0x9000) {
 147                         ioaddr = eisa_addr;
 148                         eisa_addr += 0x1000;
 149 
 150                         /* Check the standard EISA ID register for an encoded '3Com'. */
 151                         if (inw(ioaddr + 0xC80) != 0x6d50)
 152                                 continue;
 153 
 154                         /* Change the register set to the configuration window 0. */
 155                         outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD);
 156 
 157                         irq = inw(ioaddr + WN0_IRQ) >> 12;
 158                         if_port = inw(ioaddr + 6)>>14;
 159                         for (i = 0; i < 3; i++)
 160                                 phys_addr[i] = htons(read_eeprom(ioaddr, i));
 161 
 162                         /* Restore the "Product ID" to the EEPROM read register. */
 163                         read_eeprom(ioaddr, 3);
 164 
 165                         /* Was the EISA code an add-on hack?  Nahhhhh... */
 166                         goto found;
 167                 }
 168         }
 169 
 170 #ifdef CONFIG_MCA
 171         if (MCA_bus) {
 172                 mca_adaptor_select_mode(1);
 173                 for (i = 0; i < 8; i++)
 174                         if ((mca_adaptor_id(i) | 1) == 0x627c) {
 175                                 ioaddr = mca_pos_base_addr(i);
 176                                 irq = inw(ioaddr + WN0_IRQ) >> 12;
 177                                 if_port = inw(ioaddr + 6)>>14;
 178                                 for (i = 0; i < 3; i++)
 179                                         phys_addr[i] = htons(read_eeprom(ioaddr, i));
 180 
 181                                 mca_adaptor_select_mode(0);
 182                                 goto found;
 183                         }
 184                 mca_adaptor_select_mode(0);
 185 
 186         }
 187 #endif
 188 
 189         /* Reset the ISA PnP mechanism on 3c509b. */
 190         outb(0x02, 0x279);           /* Select PnP config control register. */
 191         outb(0x02, 0xA79);           /* Return to WaitForKey state. */
 192         /* Select an open I/O location at 0x1*0 to do contention select. */
 193         for (id_port = 0x100; id_port < 0x200; id_port += 0x10) {
 194                 outb(0x00, id_port);
 195                 outb(0xff, id_port);
 196                 if (inb(id_port) & 0x01)
 197                         break;
 198         }
 199         if (id_port >= 0x200) {             /* GCC optimizes this test out. */
 200                 /* Rare -- do we really need a warning? */
 201                 printk(" WARNING: No I/O port available for 3c509 activation.\n");
 202                 return -ENODEV;
 203         }
 204         /* Next check for all ISA bus boards by sending the ID sequence to the
 205            ID_PORT.  We find cards past the first by setting the 'current_tag'
 206            on cards as they are found.  Cards with their tag set will not
 207            respond to subsequent ID sequences. */
 208 
 209         if (check_region(id_port,1)) {
 210           static int once = 1;
 211           if (once) printk("3c509: Somebody has reserved 0x%x, can't do ID_PORT lookup, nor card auto-probing\n",id_port);
 212           once = 0;
 213           return -ENODEV;
 214         }
 215 
 216         outb(0x00, id_port);
 217         outb(0x00, id_port);
 218         for(i = 0; i < 255; i++) {
 219                 outb(lrs_state, id_port);
 220                 lrs_state <<= 1;
 221                 lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
 222         }
 223 
 224         current_tag = dev->name[3]-'0';
 225         /* For the first probe, clear all board's tag registers. */
 226         if (current_tag == 0)
 227                 outb(0xd0, id_port);
 228         else                            /* Otherwise kill off already-found boards. */
 229                 outb(0xd8, id_port);
 230 
 231         if (id_read_eeprom(7) != 0x6d50) {
 232                 return -ENODEV;
 233         }
 234 
 235         /* Read in EEPROM data, which does contention-select.
 236            Only the lowest address board will stay "on-line".
 237            3Com got the byte order backwards. */
 238         for (i = 0; i < 3; i++) {
 239                 phys_addr[i] = htons(id_read_eeprom(i));
 240         }
 241 
 242         {
 243                 unsigned short iobase = id_read_eeprom(8);
 244                 if_port = iobase >> 14;
 245                 ioaddr = 0x200 + ((iobase & 0x1f) << 4);
 246         }
 247         if (dev->irq > 1  &&  dev->irq < 16)
 248                 irq = dev->irq;
 249         else
 250                 irq = id_read_eeprom(9) >> 12;
 251 
 252         if (dev->base_addr != 0
 253                 &&      dev->base_addr != (unsigned short)ioaddr) {
 254                 return -ENODEV;
 255         }
 256 
 257         /* Set the adaptor tag so that the next card can be found. */
 258         outb(0xd0 + ++current_tag, id_port);
 259 
 260         /* Activate the adaptor at the EEPROM location. */
 261         outb((ioaddr >> 4) | 0xe0, id_port);
 262 
 263         EL3WINDOW(0);
 264         if (inw(ioaddr) != 0x6d50)
 265                 return -ENODEV;
 266 
 267         /* Free the interrupt so that some other card can use it. */
 268         outw(0x0f00, ioaddr + WN0_IRQ);
 269 
 270         if( check_region(ioaddr, EL3_IO_EXTENT) )
 271                 return -ENODEV;
 272 
 273  found:
 274         dev->base_addr = ioaddr;
 275         dev->irq = irq;
 276         dev->if_port = if_port;
 277         request_region(dev->base_addr, EL3_IO_EXTENT, "3c509");
 278 
 279         {
 280                 const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
 281                 printk("%s: 3c509 at %#3.3lx tag %d, %s port, address ",
 282                            dev->name, dev->base_addr, current_tag, if_names[dev->if_port]);
 283         }
 284 
 285         /* Read in the station address. */
 286         for (i = 0; i < 6; i++)
 287                 printk(" %2.2x", dev->dev_addr[i]);
 288         printk(", IRQ %d.\n", dev->irq);
 289 
 290         /* Make up a EL3-specific-data structure. */
 291         dev->priv = kmalloc(sizeof(struct el3_private), GFP_KERNEL);
 292         if (dev->priv == NULL)
 293                 return -ENOMEM;
 294         memset(dev->priv, 0, sizeof(struct el3_private));
 295 
 296         if (el3_debug > 0)
 297                 printk(version);
 298 
 299         /* The EL3-specific entries in the device structure. */
 300         dev->open = &el3_open;
 301         dev->hard_start_xmit = &el3_start_xmit;
 302         dev->stop = &el3_close;
 303         dev->get_stats = &el3_get_stats;
 304 #ifdef HAVE_MULTICAST
 305                 dev->set_multicast_list = &set_multicast_list;
 306 #endif
 307 
 308         /* Fill in the generic fields of the device structure. */
 309         ether_setup(dev);
 310         return 0;
 311 }
 312 
 313 /* Read a word from the EEPROM using the regular EEPROM access register.
 314    Assume that we are in register window zero.
 315  */
 316 static ushort read_eeprom(short ioaddr, int index)
     /* [previous][next][first][last][top][bottom][index][help] */
 317 {
 318         int timer;
 319 
 320         outw(EEPROM_READ + index, ioaddr + 10);
 321         /* Pause for at least 162 us. for the read to take place. */
 322         for (timer = 0; timer < 162*4 + 400; timer++)
 323                 SLOW_DOWN_IO;
 324         return inw(ioaddr + 12);
 325 }
 326 
 327 /* Read a word from the EEPROM when in the ISA ID probe state. */
 328 static ushort id_read_eeprom(int index)
     /* [previous][next][first][last][top][bottom][index][help] */
 329 {
 330         int timer, bit, word = 0;
 331 
 332         /* Issue read command, and pause for at least 162 us. for it to complete.
 333            Assume extra-fast 16Mhz bus. */
 334         outb(EEPROM_READ + index, id_port);
 335 
 336         /* This should really be done by looking at one of the timer channels. */
 337         for (timer = 0; timer < 162*4 + 400; timer++)
 338                 SLOW_DOWN_IO;
 339 
 340         for (bit = 15; bit >= 0; bit--)
 341                 word = (word << 1) + (inb(id_port) & 0x01);
 342 
 343         if (el3_debug > 3)
 344                 printk("  3c509 EEPROM word %d %#4.4x.\n", index, word);
 345 
 346         return word;
 347 }
 348 
 349 
 350 
 351 static int
 352 el3_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 353 {
 354         int ioaddr = dev->base_addr;
 355         int i;
 356 
 357         outw(TxReset, ioaddr + EL3_CMD);
 358         outw(RxReset, ioaddr + EL3_CMD);
 359         outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
 360 
 361         if (request_irq(dev->irq, &el3_interrupt, 0, "3c509", dev)) {
 362                 irq2dev_map[dev->irq] = NULL;
 363                 return -EAGAIN;
 364         }
 365 
 366         EL3WINDOW(0);
 367         if (el3_debug > 3)
 368                 printk("%s: Opening, IRQ %d      status@%x %4.4x.\n", dev->name,
 369                            dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));
 370 
 371         /* Activate board: this is probably unnecessary. */
 372         outw(0x0001, ioaddr + 4);
 373 
 374         /* Set the IRQ line. */
 375         outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ);
 376 
 377         /* Set the station address in window 2 each time opened. */
 378         EL3WINDOW(2);
 379 
 380         for (i = 0; i < 6; i++)
 381                 outb(dev->dev_addr[i], ioaddr + i);
 382 
 383         if (dev->if_port == 3)
 384                 /* Start the thinnet transceiver. We should really wait 50ms...*/
 385                 outw(StartCoax, ioaddr + EL3_CMD);
 386         else if (dev->if_port == 0) {
 387                 /* 10baseT interface, enabled link beat and jabber check. */
 388                 EL3WINDOW(4);
 389                 outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
 390         }
 391 
 392         /* Switch to the stats window, and clear all stats by reading. */
 393         outw(StatsDisable, ioaddr + EL3_CMD);
 394         EL3WINDOW(6);
 395         for (i = 0; i < 9; i++)
 396                 inb(ioaddr + i);
 397         inw(ioaddr + 10);
 398         inw(ioaddr + 12);
 399 
 400         /* Switch to register set 1 for normal use. */
 401         EL3WINDOW(1);
 402 
 403         /* Accept b-case and phys addr only. */
 404         outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
 405         outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
 406 
 407         dev->interrupt = 0;
 408         dev->tbusy = 0;
 409         dev->start = 1;
 410 
 411         outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
 412         outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
 413         /* Allow status bits to be seen. */
 414         outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
 415         /* Ack all pending events, and set active indicator mask. */
 416         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
 417                  ioaddr + EL3_CMD);
 418         outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull,
 419                  ioaddr + EL3_CMD);
 420 
 421         if (el3_debug > 3)
 422                 printk("%s: Opened 3c509  IRQ %d  status %4.4x.\n",
 423                            dev->name, dev->irq, inw(ioaddr + EL3_STATUS));
 424 
 425         MOD_INC_USE_COUNT;
 426         return 0;                                       /* Always succeed */
 427 }
 428 
 429 static int
 430 el3_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 431 {
 432         struct el3_private *lp = (struct el3_private *)dev->priv;
 433         int ioaddr = dev->base_addr;
 434 
 435         /* Transmitter timeout, serious problems. */
 436         if (dev->tbusy) {
 437                 int tickssofar = jiffies - dev->trans_start;
 438                 if (tickssofar < 40)
 439                         return 1;
 440                 printk("%s: transmit timed out, Tx_status %2.2x status %4.4x "
 441                            "Tx FIFO room %d.\n",
 442                            dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
 443                            inw(ioaddr + TX_FREE));
 444                 lp->stats.tx_errors++;
 445                 dev->trans_start = jiffies;
 446                 /* Issue TX_RESET and TX_START commands. */
 447                 outw(TxReset, ioaddr + EL3_CMD);
 448                 outw(TxEnable, ioaddr + EL3_CMD);
 449                 dev->tbusy = 0;
 450         }
 451 
 452         if (skb == NULL) {
 453                 dev_tint(dev);
 454                 return 0;
 455         }
 456 
 457         if (skb->len <= 0)
 458                 return 0;
 459 
 460         if (el3_debug > 4) {
 461                 printk("%s: el3_start_xmit(length = %ld) called, status %4.4x.\n",
 462                            dev->name, skb->len, inw(ioaddr + EL3_STATUS));
 463         }
 464 #if 0
 465 #ifndef final_version
 466         {       /* Error-checking code, delete someday. */
 467                 ushort status = inw(ioaddr + EL3_STATUS);
 468                 if (status & 0x0001             /* IRQ line active, missed one. */
 469                         && inw(ioaddr + EL3_STATUS) & 1) {                      /* Make sure. */
 470                         printk("%s: Missed interrupt, status then %04x now %04x"
 471                                    "  Tx %2.2x Rx %4.4x.\n", dev->name, status,
 472                                    inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS),
 473                                    inw(ioaddr + RX_STATUS));
 474                         /* Fake interrupt trigger by masking, acknowledge interrupts. */
 475                         outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
 476                         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
 477                                  ioaddr + EL3_CMD);
 478                         outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
 479                 }
 480         }
 481 #endif
 482 #endif
 483         /* Avoid timer-based retransmission conflicts. */
 484         if (set_bit(0, (void*)&dev->tbusy) != 0)
 485                 printk("%s: Transmitter access conflict.\n", dev->name);
 486         else {
 487                 /* Put out the doubleword header... */
 488                 outw(skb->len, ioaddr + TX_FIFO);
 489                 outw(0x00, ioaddr + TX_FIFO);
 490                 /* ... and the packet rounded to a doubleword. */
 491                 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
 492 
 493                 dev->trans_start = jiffies;
 494                 if (inw(ioaddr + TX_FREE) > 1536) {
 495                         dev->tbusy = 0;
 496                 } else
 497                         /* Interrupt us when the FIFO has room for max-sized packet. */
 498                         outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
 499         }
 500 
 501         dev_kfree_skb (skb, FREE_WRITE);
 502 
 503         /* Clear the Tx status stack. */
 504         {
 505                 short tx_status;
 506                 int i = 4;
 507 
 508                 while (--i > 0  &&      (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
 509                         if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
 510                         if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
 511                         if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
 512                         outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
 513                 }
 514         }
 515         return 0;
 516 }
 517 
 518 /* The EL3 interrupt handler. */
 519 static void
 520 el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 521 {
 522         struct device *dev = (struct device *)dev_id;
 523         int ioaddr, status;
 524         int i = 0;
 525 
 526         if (dev == NULL) {
 527                 printk ("el3_interrupt(): irq %d for unknown device.\n", irq);
 528                 return;
 529         }
 530 
 531         if (dev->interrupt)
 532                 printk("%s: Re-entering the interrupt handler.\n", dev->name);
 533         dev->interrupt = 1;
 534 
 535         ioaddr = dev->base_addr;
 536         status = inw(ioaddr + EL3_STATUS);
 537 
 538         if (el3_debug > 4)
 539                 printk("%s: interrupt, status %4.4x.\n", dev->name, status);
 540 
 541         while ((status = inw(ioaddr + EL3_STATUS)) &
 542                    (IntLatch | RxComplete | StatsFull)) {
 543 
 544                 if (status & RxComplete)
 545                         el3_rx(dev);
 546 
 547                 if (status & TxAvailable) {
 548                         if (el3_debug > 5)
 549                                 printk("        TX room bit was handled.\n");
 550                         /* There's room in the FIFO for a full-sized packet. */
 551                         outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
 552                         dev->tbusy = 0;
 553                         mark_bh(NET_BH);
 554                 }
 555                 if (status & (AdapterFailure | RxEarly | StatsFull)) {
 556                         /* Handle all uncommon interrupts. */
 557                         if (status & StatsFull)                         /* Empty statistics. */
 558                                 update_stats(ioaddr, dev);
 559                         if (status & RxEarly) {                         /* Rx early is unused. */
 560                                 el3_rx(dev);
 561                                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
 562                         }
 563                         if (status & AdapterFailure) {
 564                                 /* Adapter failure requires Rx reset and reinit. */
 565                                 outw(RxReset, ioaddr + EL3_CMD);
 566                                 /* Set the Rx filter to the current state. */
 567                                 outw(SetRxFilter | RxStation | RxBroadcast
 568                                          | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
 569                                          | (dev->flags & IFF_PROMISC ? RxProm : 0),
 570                                          ioaddr + EL3_CMD);
 571                                 outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
 572                                 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
 573                         }
 574                 }
 575 
 576                 if (++i > 10) {
 577                         printk("%s: Infinite loop in interrupt, status %4.4x.\n",
 578                                    dev->name, status);
 579                         /* Clear all interrupts. */
 580                         outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
 581                         break;
 582                 }
 583                 /* Acknowledge the IRQ. */
 584                 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */
 585         }
 586 
 587         if (el3_debug > 4) {
 588                 printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
 589                            inw(ioaddr + EL3_STATUS));
 590         }
 591 
 592         dev->interrupt = 0;
 593         return;
 594 }
 595 
 596 
 597 static struct enet_statistics *
 598 el3_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 599 {
 600         struct el3_private *lp = (struct el3_private *)dev->priv;
 601         unsigned long flags;
 602 
 603         save_flags(flags);
 604         cli();
 605         update_stats(dev->base_addr, dev);
 606         restore_flags(flags);
 607         return &lp->stats;
 608 }
 609 
 610 /*  Update statistics.  We change to register window 6, so this should be run
 611         single-threaded if the device is active. This is expected to be a rare
 612         operation, and it's simpler for the rest of the driver to assume that
 613         window 1 is always valid rather than use a special window-state variable.
 614         */
 615 static void update_stats(int ioaddr, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 616 {
 617         struct el3_private *lp = (struct el3_private *)dev->priv;
 618 
 619         if (el3_debug > 5)
 620                 printk("   Updating the statistics.\n");
 621         /* Turn off statistics updates while reading. */
 622         outw(StatsDisable, ioaddr + EL3_CMD);
 623         /* Switch to the stats window, and read everything. */
 624         EL3WINDOW(6);
 625         lp->stats.tx_carrier_errors     += inb(ioaddr + 0);
 626         lp->stats.tx_heartbeat_errors   += inb(ioaddr + 1);
 627         /* Multiple collisions. */              inb(ioaddr + 2);
 628         lp->stats.collisions                    += inb(ioaddr + 3);
 629         lp->stats.tx_window_errors              += inb(ioaddr + 4);
 630         lp->stats.rx_fifo_errors                += inb(ioaddr + 5);
 631         lp->stats.tx_packets                    += inb(ioaddr + 6);
 632         /* Rx packets   */                              inb(ioaddr + 7);
 633         /* Tx deferrals */                              inb(ioaddr + 8);
 634         inw(ioaddr + 10);       /* Total Rx and Tx octets. */
 635         inw(ioaddr + 12);
 636 
 637         /* Back to window 1, and turn statistics back on. */
 638         EL3WINDOW(1);
 639         outw(StatsEnable, ioaddr + EL3_CMD);
 640         return;
 641 }
 642 
 643 static int
 644 el3_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 645 {
 646         struct el3_private *lp = (struct el3_private *)dev->priv;
 647         int ioaddr = dev->base_addr;
 648         short rx_status;
 649 
 650         if (el3_debug > 5)
 651                 printk("   In rx_packet(), status %4.4x, rx_status %4.4x.\n",
 652                            inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
 653         while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
 654                 if (rx_status & 0x4000) { /* Error, update stats. */
 655                         short error = rx_status & 0x3800;
 656                         lp->stats.rx_errors++;
 657                         switch (error) {
 658                         case 0x0000:            lp->stats.rx_over_errors++; break;
 659                         case 0x0800:            lp->stats.rx_length_errors++; break;
 660                         case 0x1000:            lp->stats.rx_frame_errors++; break;
 661                         case 0x1800:            lp->stats.rx_length_errors++; break;
 662                         case 0x2000:            lp->stats.rx_frame_errors++; break;
 663                         case 0x2800:            lp->stats.rx_crc_errors++; break;
 664                         }
 665                 } else {
 666                         short pkt_len = rx_status & 0x7ff;
 667                         struct sk_buff *skb;
 668 
 669                         skb = dev_alloc_skb(pkt_len+5);
 670                         if (el3_debug > 4)
 671                                 printk("Receiving packet size %d status %4.4x.\n",
 672                                            pkt_len, rx_status);
 673                         if (skb != NULL) {
 674                                 skb->dev = dev;
 675                                 skb_reserve(skb,2);     /* Align IP on 16 byte */
 676 
 677                                 /* 'skb->data' points to the start of sk_buff data area. */
 678                                 insl(ioaddr+RX_FIFO, skb_put(skb,pkt_len),
 679                                                         (pkt_len + 3) >> 2);
 680 
 681                                 skb->protocol=eth_type_trans(skb,dev);
 682                                 netif_rx(skb);
 683                                 outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
 684                                 lp->stats.rx_packets++;
 685                                 continue;
 686                         } else if (el3_debug)
 687                                 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
 688                                            dev->name, pkt_len);
 689                 }
 690                 lp->stats.rx_dropped++;
 691                 outw(RxDiscard, ioaddr + EL3_CMD);
 692                 while (inw(ioaddr + EL3_STATUS) & 0x1000)
 693                         printk("        Waiting for 3c509 to discard packet, status %x.\n",
 694                                    inw(ioaddr + EL3_STATUS) );
 695         }
 696 
 697         return 0;
 698 }
 699 
 700 #ifdef HAVE_MULTICAST
 701 /*
 702  *     Set or clear the multicast filter for this adaptor.
 703  */
 704 static void
 705 set_multicast_list(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 706 {
 707         short ioaddr = dev->base_addr;
 708         if (el3_debug > 1) {
 709                 static int old = 0;
 710                 if (old != dev->mc_count) {
 711                         old = dev->mc_count;
 712                         printk("%s: Setting Rx mode to %d addresses.\n", dev->name, dev->mc_count);
 713                 }
 714         }
 715         if (dev->flags&IFF_PROMISC) {
 716                 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
 717                          ioaddr + EL3_CMD);
 718         }
 719         else if (dev->mc_count || (dev->flags&IFF_ALLMULTI)) {
 720                 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD);
 721         }
 722         else
 723                 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
 724 }
 725 #endif
 726 
 727 static int
 728 el3_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 729 {
 730         int ioaddr = dev->base_addr;
 731 
 732         if (el3_debug > 2)
 733                 printk("%s: Shutting down ethercard.\n", dev->name);
 734 
 735         dev->tbusy = 1;
 736         dev->start = 0;
 737 
 738         /* Turn off statistics ASAP.  We update lp->stats below. */
 739         outw(StatsDisable, ioaddr + EL3_CMD);
 740 
 741         /* Disable the receiver and transmitter. */
 742         outw(RxDisable, ioaddr + EL3_CMD);
 743         outw(TxDisable, ioaddr + EL3_CMD);
 744 
 745         if (dev->if_port == 3)
 746                 /* Turn off thinnet power.  Green! */
 747                 outw(StopCoax, ioaddr + EL3_CMD);
 748         else if (dev->if_port == 0) {
 749                 /* Disable link beat and jabber, if_port may change ere next open(). */
 750                 EL3WINDOW(4);
 751                 outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
 752         }
 753 
 754         free_irq(dev->irq, dev);
 755         /* Switching back to window 0 disables the IRQ. */
 756         EL3WINDOW(0);
 757         /* But we explicitly zero the IRQ line select anyway. */
 758         outw(0x0f00, ioaddr + WN0_IRQ);
 759 
 760         update_stats(ioaddr, dev);
 761         MOD_DEC_USE_COUNT;
 762         return 0;
 763 }
 764 
 765 #ifdef MODULE
 766 static char devicename[9] = { 0, };
 767 static struct device dev_3c509 = {
 768         devicename, /* device name is inserted by linux/drivers/net/net_init.c */
 769         0, 0, 0, 0,
 770         0, 0,
 771         0, 0, 0, NULL, el3_probe };
 772 
 773 static int io  = 0;
 774 static int irq = 0;
 775 
 776 int
 777 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 778 {
 779         dev_3c509.base_addr = io;
 780         dev_3c509.irq       = irq;
 781         if (!EISA_bus && !io) {
 782                 printk("3c509: WARNING! Module load-time probing works reliably only for EISA bus!!\n");
 783         }
 784         if (register_netdev(&dev_3c509) != 0)
 785                 return -EIO;
 786         return 0;
 787 }
 788 
 789 void
 790 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 791 {
 792         if (MOD_IN_USE)
 793                 printk("3c509: device busy, remove delayed\n");
 794         else
 795         {
 796                 unregister_netdev(&dev_3c509);
 797                 kfree_s(dev_3c509.priv,sizeof(struct el3_private));
 798                 dev_3c509.priv=NULL;
 799                 /* If we don't do this, we can't re-insmod it later. */
 800                 release_region(dev_3c509.base_addr, EL3_IO_EXTENT);
 801         }
 802 }
 803 #endif /* MODULE */
 804 
 805 /*
 806  * Local variables:
 807  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c 3c509.c"
 808  *  version-control: t
 809  *  kept-new-versions: 5
 810  *  tab-width: 4
 811  * End:
 812  */

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