root/drivers/net/tulip.c

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

DEFINITIONS

This source file includes following definitions.
  1. dec21040_init
  2. tulip_probe
  3. tulip_probe1
  4. tulip_open
  5. tulip_init_ring
  6. tulip_start_xmit
  7. tulip_interrupt
  8. tulip_rx
  9. tulip_close
  10. tulip_get_stats
  11. set_multicast_list
  12. set_mac_address
  13. init_module
  14. cleanup_module

   1 /* tulip.c: A DEC 21040 ethernet driver for linux. */
   2 /*
   3    NOTICE: this version works with kernels 1.1.82 and later only!
   4         Written 1994,1995 by Donald Becker.
   5 
   6         This software may be used and distributed according to the terms
   7         of the GNU Public License, incorporated herein by reference.
   8 
   9         This driver is for the SMC EtherPower PCI ethernet adapter.
  10         It should work with most other DEC 21*40-based ethercards.
  11 
  12         The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
  13         Center of Excellence in Space Data and Information Sciences
  14            Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
  15 */
  16 
  17 static const char *version = "tulip.c:v0.05 1/20/95 becker@cesdis.gsfc.nasa.gov\n";
  18 
  19 #ifdef MODULE
  20 #include <linux/module.h>
  21 #include <linux/version.h>
  22 #endif
  23 
  24 #include <linux/config.h>
  25 #include <linux/kernel.h>
  26 #include <linux/sched.h>
  27 #include <linux/string.h>
  28 #include <linux/ptrace.h>
  29 #include <linux/errno.h>
  30 #include <linux/ioport.h>
  31 #include <linux/malloc.h>
  32 #include <linux/interrupt.h>
  33 #include <linux/pci.h>
  34 #include <linux/bios32.h>
  35 #include <asm/bitops.h>
  36 #include <asm/io.h>
  37 #include <asm/dma.h>
  38 
  39 #include <linux/netdevice.h>
  40 #include <linux/etherdevice.h>
  41 #include <linux/skbuff.h>
  42 
  43 /* The total size is unusually large: The 21040 aligns each of its 16
  44    longword-wide registers on a quadword boundary. */
  45 #define TULIP_TOTAL_SIZE 0x80
  46 
  47 #ifdef HAVE_DEVLIST
  48 struct netdev_entry tulip_drv =
  49 {"Tulip", tulip_pci_probe, TULIP_TOTAL_SIZE, NULL};
  50 #endif
  51 
  52 #define TULIP_DEBUG 1
  53 #ifdef TULIP_DEBUG
  54 int tulip_debug = TULIP_DEBUG;
  55 #else
  56 int tulip_debug = 1;
  57 #endif
  58 
  59 /*
  60                                 Theory of Operation
  61 
  62 I. Board Compatibility
  63 
  64 This device driver is designed for the DECchip 21040 "Tulip", Digital's
  65 single-chip ethernet controller for PCI, as used on the SMC EtherPower
  66 ethernet adapter.
  67 
  68 II. Board-specific settings
  69 
  70 PCI bus devices are configured by the system at boot time, so no jumpers
  71 need to be set on the board.  The system BIOS should be set to assign the
  72 PCI INTA signal to an otherwise unused system IRQ line.  While it's
  73 physically possible to shared PCI interrupt lines, the kernel doesn't
  74 support it. 
  75 
  76 III. Driver operation
  77 
  78 IIIa. Ring buffers
  79 The Tulip can use either ring buffers or lists of Tx and Rx descriptors.
  80 The current driver uses a statically allocated Rx ring of descriptors and
  81 buffers, and a list of the Tx buffers.
  82 
  83 IIIC. Synchronization
  84 The driver runs as two independent, single-threaded flows of control.  One
  85 is the send-packet routine, which enforces single-threaded use by the
  86 dev->tbusy flag.  The other thread is the interrupt handler, which is single
  87 threaded by the hardware and other software.
  88 
  89 The send packet thread has partial control over the Tx ring and 'dev->tbusy'
  90 flag.  It sets the tbusy flag whenever it's queuing a Tx packet. If the next
  91 queue slot is empty, it clears the tbusy flag when finished otherwise it sets
  92 the 'tp->tx_full' flag.
  93 
  94 The interrupt handler has exclusive control over the Rx ring and records stats
  95 from the Tx ring.  (The Tx-done interrupt can't be selectively turned off, so
  96 we can't avoid the interrupt overhead by having the Tx routine reap the Tx
  97 stats.)  After reaping the stats, it marks the queue entry as empty by setting
  98 the 'base' to zero.      Iff the 'tp->tx_full' flag is set, it clears both the
  99 tx_full and tbusy flags.
 100 
 101 IV. Notes
 102 
 103 Thanks to Duke Kamstra of SMC for providing an EtherPower board.
 104 
 105 The DEC databook doesn't document which Rx filter settings accept broadcast
 106 packets.  Nor does it document how to configure the part to configure the
 107 serial subsystem for normal (vs. loopback) operation or how to have it
 108 autoswitch between internal 10baseT, SIA and AUI transceivers.
 109 
 110 The databook claims that CSR13, CSR14, and CSR15 should each be the last
 111 register of the set CSR12-15 written.   Hmmm, now how is that possible?
 112 */
 113 
 114 #define DEC_VENDOR_ID   0x1011          /* Hex 'D' :-> */
 115 #define DEC_21040_ID    0x0002          /* Change for 21140. */
 116 
 117 /* Keep the ring sizes a power of two for efficiency. */
 118 #define TX_RING_SIZE    4
 119 #define RX_RING_SIZE    4
 120 #define PKT_BUF_SZ              1536                    /* Size of each temporary Rx buffer.*/
 121 
 122 /* Offsets to the Command and Status Registers, "CSRs".  All accesses
 123    must be longword instructions and quadword aligned. */
 124 enum tulip_offsets {
 125         CSR0=0,    CSR1=0x08, CSR2=0x10, CSR3=0x18, CSR4=0x20, CSR5=0x28,
 126         CSR6=0x30, CSR7=0x38, CSR8=0x40, CSR9=0x48, CSR10=0x50, CSR11=0x58,
 127         CSR12=0x60, CSR13=0x68, CSR14=0x70, CSR15=0x78 };
 128 
 129 /* The Tulip Rx and Tx buffer descriptors. */
 130 struct tulip_rx_desc {
 131         int status;
 132         int length;
 133         char *buffer1, *buffer2;                        /* We use only buffer 1.  */
 134 };
 135 
 136 struct tulip_tx_desc {
 137         int status;
 138         int length;
 139         char *buffer1, *buffer2;                        /* We use only buffer 1.  */
 140 };
 141 
 142 struct tulip_private {
 143         char devname[8];                        /* Used only for kernel debugging. */
 144         struct tulip_rx_desc rx_ring[RX_RING_SIZE];
 145         struct tulip_tx_desc tx_ring[TX_RING_SIZE];
 146         /* The saved address of a sent-in-place packet/buffer, for skfree(). */
 147         struct sk_buff* tx_skbuff[TX_RING_SIZE];
 148         long rx_buffs;                          /* Address of temporary Rx buffers. */
 149         struct enet_statistics stats;
 150         int setup_frame[48];            /* Pseudo-Tx frame to init address table. */
 151         unsigned int cur_rx, cur_tx;            /* The next free ring entry */
 152         unsigned int dirty_rx, dirty_tx;        /* The ring entries to be free()ed. */
 153         unsigned int tx_full:1;
 154         int pad0, pad1;                                         /* Used for 8-byte alignment */
 155 };
 156 
 157 static unsigned long tulip_probe1(unsigned long mem_start, int ioaddr,
 158                                                                   int irq);
 159 static int tulip_open(struct device *dev);
 160 static void tulip_init_ring(struct device *dev);
 161 static int tulip_start_xmit(struct sk_buff *skb, struct device *dev);
 162 static int tulip_rx(struct device *dev);
 163 static void tulip_interrupt(int irq, struct pt_regs *regs);
 164 static int tulip_close(struct device *dev);
 165 static struct enet_statistics *tulip_get_stats(struct device *dev);
 166 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
 167 static int set_mac_address(struct device *dev, void *addr);
 168 
 169 
 170 
 171 #ifndef MODULE
 172 /* This 21040 probe is unlike most other board probes.  We can use memory
 173    efficiently by allocating a large contiguous region and dividing it
 174    ourselves.  This is done by having the initialization occur before
 175    the 'kmalloc()' memory management system is started. */
 176 
 177 unsigned long dec21040_init(unsigned long mem_start, unsigned long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
 178 {
 179 
 180     if (pcibios_present()) {
 181             int pci_index;
 182                 for (pci_index = 0; pci_index < 8; pci_index++) {
 183                         unsigned char pci_bus, pci_device_fn, pci_irq_line;
 184                         unsigned long pci_ioaddr;
 185                 
 186                         if (pcibios_find_device (DEC_VENDOR_ID, DEC_21040_ID, pci_index,
 187                                                                          &pci_bus, &pci_device_fn) != 0)
 188                                 break;
 189                         pcibios_read_config_byte(pci_bus, pci_device_fn,
 190                                                                          PCI_INTERRUPT_LINE, &pci_irq_line);
 191                         pcibios_read_config_dword(pci_bus, pci_device_fn,
 192                                                                           PCI_BASE_ADDRESS_0, &pci_ioaddr);
 193                         /* Remove I/O space marker in bit 0. */
 194                         pci_ioaddr &= ~3;
 195                         if (tulip_debug > 2)
 196                                 printk("Found DEC PCI Tulip at I/O %#lx, IRQ %d.\n",
 197                                            pci_ioaddr, pci_irq_line);
 198                         mem_start = tulip_probe1(mem_start, pci_ioaddr, pci_irq_line);
 199                 }
 200         }
 201 
 202         return mem_start;
 203 }
 204 #endif
 205 #ifdef MODULE
 206 static int tulip_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 207 {
 208         printk("tulip: This driver does not yet install properly from module!\n");
 209         return -1;
 210 }
 211 #endif
 212 
 213 unsigned long tulip_probe1(unsigned long mem_start, int ioaddr, int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
 214 {
 215         static int did_version = 0;                     /* Already printed version info. */
 216         struct device *dev;
 217         struct tulip_private *tp;
 218         int i;
 219 
 220         if (tulip_debug > 0  &&  did_version++ == 0)
 221                 printk(version);
 222 
 223         dev = init_etherdev(0, sizeof(struct tulip_private)
 224                                                 + PKT_BUF_SZ*RX_RING_SIZE,
 225                                                 &mem_start);
 226 
 227         printk("%s: DEC 21040 Tulip at %#3x,", dev->name, ioaddr);
 228 
 229         /* Stop the chip's Tx and Rx processes. */
 230         outl(inl(ioaddr + CSR6) & ~0x2002, ioaddr + CSR6);
 231         /* Clear the missed-packet counter. */
 232         inl(ioaddr + CSR8) & 0xffff;
 233 
 234         /* The station address ROM is read byte serially.  The register must
 235            be polled, waiting for the value to be read bit serially from the
 236            EEPROM.
 237            */
 238         outl(0, ioaddr + CSR9);         /* Reset the pointer with a dummy write. */
 239         for (i = 0; i < 6; i++) {
 240                 int value, boguscnt = 100000;
 241                 do
 242                         value = inl(ioaddr + CSR9);
 243                 while (value < 0  && --boguscnt > 0);
 244                 printk(" %2.2x", dev->dev_addr[i] = value);
 245         }
 246         printk(", IRQ %d\n", irq);
 247 
 248         /* We do a request_region() only to register /proc/ioports info. */
 249         request_region(ioaddr, TULIP_TOTAL_SIZE, "DEC Tulip Ethernet");
 250 
 251         dev->base_addr = ioaddr;
 252         dev->irq = irq;
 253 
 254         /* Make certain the data structures are quadword aligned. */
 255         dev->priv = (void *)(((int)dev->priv + 7) & ~7);
 256         tp = (struct tulip_private *)dev->priv;
 257         tp->rx_buffs = (long)dev->priv + sizeof(struct tulip_private);
 258 
 259         /* The Tulip-specific entries in the device structure. */
 260         dev->open = &tulip_open;
 261         dev->hard_start_xmit = &tulip_start_xmit;
 262         dev->stop = &tulip_close;
 263         dev->get_stats = &tulip_get_stats;
 264 #ifdef HAVE_MULTICAST
 265         dev->set_multicast_list = &set_multicast_list;
 266 #endif
 267 #ifdef HAVE_SET_MAC_ADDR
 268         dev->set_mac_address = &set_mac_address;
 269 #endif
 270 
 271         return mem_start;
 272 }
 273 
 274 
 275 static int
 276 tulip_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 277 {
 278         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 279         int ioaddr = dev->base_addr;
 280 
 281         /* Reset the chip, holding bit 0 set at least 10 PCI cycles. */
 282         outl(0xfff80001, ioaddr + CSR0);
 283         SLOW_DOWN_IO;
 284         /* Deassert reset.  Set 8 longword cache alignment, 8 longword burst.
 285            Cache alignment bits 15:14        Burst length 13:8
 286         0000    No alignment  0x00000000 unlimited              0800 8 longwords
 287                 4000    8  longwords            0100 1 longword         1000 16 longwords
 288                 8000    16 longwords            0200 2 longwords        2000 32 longwords
 289                 C000    32  longwords           0400 4 longwords
 290            Wait the specified 50 PCI cycles after a reset by initializing
 291            Tx and Rx queues and the address filter list. */
 292         outl(0xfff84800, ioaddr + CSR0);
 293 
 294         if (irq2dev_map[dev->irq] != NULL
 295                 || (irq2dev_map[dev->irq] = dev) == NULL
 296                 || dev->irq == 0
 297                 || request_irq(dev->irq, &tulip_interrupt, 0, "DEC 21040 Tulip")) {
 298                 return -EAGAIN;
 299         }
 300 
 301         if (tulip_debug > 1)
 302                 printk("%s: tulip_open() irq %d.\n", dev->name, dev->irq);
 303 
 304         tulip_init_ring(dev);
 305 
 306         /* Fill the whole address filter table with our physical address. */
 307         { 
 308                 unsigned short *eaddrs = (unsigned short *)dev->dev_addr;
 309                 int *setup_frm = tp->setup_frame, i;
 310 
 311                 /* You must add the broadcast address when doing perfect filtering! */
 312                 *setup_frm++ = 0xffff;
 313                 *setup_frm++ = 0xffff;
 314                 *setup_frm++ = 0xffff;
 315                 /* Fill the rest of the accept table with our physical address. */
 316                 for (i = 1; i < 16; i++) {
 317                         *setup_frm++ = eaddrs[0];
 318                         *setup_frm++ = eaddrs[1];
 319                         *setup_frm++ = eaddrs[2];
 320                 }
 321                 /* Put the setup frame on the Tx list. */
 322                 tp->tx_ring[0].length = 0x08000000 | 192;
 323                 tp->tx_ring[0].buffer1 = (char *)tp->setup_frame;
 324                 tp->tx_ring[0].buffer2 = 0;
 325                 tp->tx_ring[0].status = 0x80000000;
 326 
 327                 tp->cur_tx++, tp->dirty_tx++;
 328         }
 329 
 330         outl((int)tp->rx_ring, ioaddr + CSR3);
 331         outl((int)tp->tx_ring, ioaddr + CSR4);
 332 
 333         /* Turn on the xcvr interface. */
 334         outl(0x00000000, ioaddr + CSR13);
 335         outl(0x00000004, ioaddr + CSR13);
 336 
 337         /* Start the chip's Tx and Rx processes. */
 338         outl(0xfffe2002, ioaddr + CSR6);
 339 
 340         /* Trigger an immediate transmit demand to process the setup frame. */
 341         outl(0, ioaddr + CSR1);
 342 
 343         dev->tbusy = 0;
 344         dev->interrupt = 0;
 345         dev->start = 1;
 346 
 347         /* Enable interrupts by setting the interrupt mask. */
 348         outl(0xFFFFFFFF, ioaddr + CSR7);
 349 
 350         if (tulip_debug > 2) {
 351                 printk("%s: Done tulip_open(), CSR0 %8.8x, CSR13 %8.8x.\n",
 352                            dev->name, inl(ioaddr + CSR0), inl(ioaddr + CSR13));
 353         }
 354 #ifdef MODULE
 355         MOD_INC_USE_COUNT;
 356 #endif
 357         return 0;
 358 }
 359 
 360 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
 361 static void
 362 tulip_init_ring(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 363 {
 364         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 365         int i;
 366 
 367         tp->tx_full = 0;
 368         tp->cur_rx = tp->cur_tx = 0;
 369         tp->dirty_rx = tp->dirty_tx = 0;
 370 
 371         for (i = 0; i < RX_RING_SIZE; i++) {
 372                 tp->rx_ring[i].status = 0x80000000;     /* Owned by Tulip chip */
 373                 tp->rx_ring[i].length = PKT_BUF_SZ;
 374                 tp->rx_ring[i].buffer1 = (char *)(tp->rx_buffs + i*PKT_BUF_SZ);
 375                 tp->rx_ring[i].buffer2 = (char *)&tp->rx_ring[i+1];
 376         }
 377         /* Mark the last entry as wrapping the ring. */ 
 378         tp->rx_ring[i-1].length = PKT_BUF_SZ | 0x02000000;
 379         tp->rx_ring[i-1].buffer2 = (char *)&tp->rx_ring[0];
 380 
 381         /* The Tx buffer descriptor is filled in as needed, but we
 382            do need to clear the ownership bit. */
 383         for (i = 0; i < TX_RING_SIZE; i++) {
 384                 tp->tx_ring[i].status = 0x00000000;
 385         }
 386 }
 387 
 388 static int
 389 tulip_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 390 {
 391         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 392         int ioaddr = dev->base_addr;
 393         int entry;
 394 
 395         /* Transmitter timeout, serious problems. */
 396         if (dev->tbusy) {
 397                 int tickssofar = jiffies - dev->trans_start;
 398                 int i;
 399                 if (tickssofar < 20)
 400                         return 1;
 401                 printk("%s: transmit timed out, status %8.8x, SIA %8.8x %8.8x %8.8x %8.8x, resetting...\n",
 402                            dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR12),
 403                            inl(ioaddr + CSR13), inl(ioaddr + CSR14), inl(ioaddr + CSR15));
 404                 printk("  Rx ring %8.8x: ", (int)tp->rx_ring);
 405                 for (i = 0; i < RX_RING_SIZE; i++)
 406                         printk(" %8.8x", (unsigned int)tp->rx_ring[i].status);
 407                 printk("\n  Tx ring %8.8x: ", (int)tp->tx_ring);
 408                 for (i = 0; i < TX_RING_SIZE; i++)
 409                         printk(" %8.8x", (unsigned int)tp->tx_ring[i].status);
 410                 printk("\n");
 411 
 412                 tp->stats.tx_errors++;
 413                 /* We should reinitialize the hardware here. */
 414                 dev->tbusy=0;
 415                 dev->trans_start = jiffies;
 416                 return 0;
 417         }
 418 
 419         if (skb == NULL || skb->len <= 0) {
 420                 printk("%s: Obsolete driver layer request made: skbuff==NULL.\n",
 421                            dev->name);
 422                 dev_tint(dev);
 423                 return 0;
 424         }
 425 
 426         /* Block a timer-based transmit from overlapping.  This could better be
 427            done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
 428            If this ever occurs the queue layer is doing something evil! */
 429         if (set_bit(0, (void*)&dev->tbusy) != 0) {
 430                 printk("%s: Transmitter access conflict.\n", dev->name);
 431                 return 1;
 432         }
 433 
 434         /* Caution: the write order is important here, set the base address
 435            with the "ownership" bits last. */
 436 
 437         /* Calculate the next Tx descriptor entry. */
 438         entry = tp->cur_tx % TX_RING_SIZE;
 439 
 440         tp->tx_full = 1;
 441         tp->tx_skbuff[entry] = skb;
 442         tp->tx_ring[entry].length = skb->len |
 443                 (entry == TX_RING_SIZE-1 ? 0xe2000000 : 0xe0000000);
 444         tp->tx_ring[entry].buffer1 = skb->data;
 445         tp->tx_ring[entry].buffer2 = 0;
 446         tp->tx_ring[entry].status = 0x80000000; /* Pass ownership to the chip. */
 447 
 448         tp->cur_tx++;
 449 
 450         /* Trigger an immediate transmit demand. */
 451         outl(0, ioaddr + CSR1);
 452 
 453         dev->trans_start = jiffies;
 454 
 455         return 0;
 456 }
 457 
 458 /* The interrupt handler does all of the Rx thread work and cleans up
 459    after the Tx thread. */
 460 static void tulip_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 461 {
 462         struct device *dev = (struct device *)(irq2dev_map[irq]);
 463         struct tulip_private *lp;
 464         int csr5, ioaddr, boguscnt=10;
 465 
 466         if (dev == NULL) {
 467                 printk ("tulip_interrupt(): irq %d for unknown device.\n", irq);
 468                 return;
 469         }
 470 
 471         ioaddr = dev->base_addr;
 472         lp = (struct tulip_private *)dev->priv;
 473         if (dev->interrupt)
 474                 printk("%s: Re-entering the interrupt handler.\n", dev->name);
 475 
 476         dev->interrupt = 1;
 477 
 478         do {
 479                 csr5 = inl(ioaddr + CSR5);
 480                 /* Acknowledge all of the current interrupt sources ASAP. */
 481                 outl(csr5 & 0x0001ffff, ioaddr + CSR5);
 482 
 483                 if (tulip_debug > 4)
 484                         printk("%s: interrupt  csr5=%#8.8x new csr5=%#8.8x.\n",
 485                                    dev->name, csr5, inl(dev->base_addr + CSR5));
 486 
 487                 if ((csr5 & 0x00018000) == 0)
 488                         break;
 489 
 490                 if (csr5 & 0x0040)                      /* Rx interrupt */
 491                         tulip_rx(dev);
 492 
 493                 if (csr5 & 0x0001) {            /* Tx-done interrupt */
 494                         int dirty_tx = lp->dirty_tx;
 495 
 496                         while (dirty_tx < lp->cur_tx) {
 497                                 int entry = dirty_tx % TX_RING_SIZE;
 498                                 int status = lp->tx_ring[entry].status;
 499 
 500                                 if (status < 0)
 501                                         break;                  /* It still hasn't been Txed */
 502 
 503                                 if (status & 0x8000) {
 504                                         /* There was an major error, log it. */
 505                                         lp->stats.tx_errors++;
 506                                         if (status & 0x4104) lp->stats.tx_aborted_errors++;
 507                                         if (status & 0x0C00) lp->stats.tx_carrier_errors++;
 508                                         if (status & 0x0200) lp->stats.tx_window_errors++;
 509                                         if (status & 0x0002) lp->stats.tx_fifo_errors++;
 510                                         if (status & 0x0080) lp->stats.tx_heartbeat_errors++;
 511 #ifdef ETHER_STATS
 512                                         if (status & 0x0100) lp->stats.collisions16++;
 513 #endif
 514                                 } else {
 515 #ifdef ETHER_STATS
 516                                         if (status & 0x0001) lp->stats.tx_deferred++;
 517 #endif
 518                                         lp->stats.collisions += (status >> 3) & 15;
 519                                         lp->stats.tx_packets++;
 520                                 }
 521 
 522                                 /* Free the original skb. */
 523                                 dev_kfree_skb(lp->tx_skbuff[entry], FREE_WRITE);
 524                                 dirty_tx++;
 525                         }
 526 
 527 #ifndef final_version
 528                         if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
 529                                 printk("out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
 530                                            dirty_tx, lp->cur_tx, lp->tx_full);
 531                                 dirty_tx += TX_RING_SIZE;
 532                         }
 533 #endif
 534 
 535                         if (lp->tx_full && dev->tbusy
 536                                 && dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
 537                                 /* The ring is no longer full, clear tbusy. */
 538                                 lp->tx_full = 0;
 539                                 dev->tbusy = 0;
 540                                 mark_bh(NET_BH);
 541                         }
 542 
 543                         lp->dirty_tx = dirty_tx;
 544                 }
 545 
 546                 /* Log errors. */
 547                 if (csr5 & 0x8000) {    /* Abnormal error summary bit. */
 548                         if (csr5 & 0x0008) lp->stats.tx_errors++; /* Tx babble. */
 549                         if (csr5 & 0x0100) {            /* Missed a Rx frame. */
 550                                 lp->stats.rx_errors++;
 551                                 lp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
 552                         }
 553                         if (csr5 & 0x0800) {
 554                                 printk("%s: Something Wicked happened! %8.8x.\n",
 555                                            dev->name, csr5);
 556                                 /* Hmmmmm, it's not clear what to do here. */
 557                         }
 558                 }
 559                 if (--boguscnt < 0) {
 560                         printk("%s: Too much work at interrupt, csr5=0x%8.8x.\n",
 561                                    dev->name, csr5);
 562                         /* Clear all interrupt sources. */
 563                         outl(0x0001ffff, ioaddr + CSR5);
 564                         break;
 565                 }
 566         } while (1);
 567 
 568         if (tulip_debug > 3)
 569                 printk("%s: exiting interrupt, csr5=%#4.4x.\n",
 570                            dev->name, inl(ioaddr + CSR5));
 571 
 572         /* Special code for testing *only*. */
 573         {
 574                 static int stopit = 10;
 575                 if (dev->start == 0  &&  --stopit < 0) {
 576                         printk("%s: Emergency stop, looping startup interrupt.\n",
 577                                    dev->name);
 578                         free_irq(irq);
 579                 }
 580         }
 581 
 582         dev->interrupt = 0;
 583         return;
 584 }
 585 
 586 static int
 587 tulip_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 588 {
 589         struct tulip_private *lp = (struct tulip_private *)dev->priv;
 590         int entry = lp->cur_rx % RX_RING_SIZE;
 591         int i;
 592                 
 593         if (tulip_debug > 4)
 594                 printk(" In tulip_rx().\n");
 595         /* If we own the next entry, it's a new packet. Send it up. */
 596         while (lp->rx_ring[entry].status >= 0) {
 597                 int status = lp->rx_ring[entry].status;
 598 
 599                 if (tulip_debug > 4)
 600                         printk("  tulip_rx() status was %8.8x.\n", status);
 601                 if ((status & 0x0300) != 0x0300) {
 602                         printk("%s: Ethernet frame spanned multiple buffers, status %8.8x!\n",
 603                                    dev->name, status);
 604                 } else if (status & 0x8000) {
 605                         /* There was a fatal error. */
 606                         lp->stats.rx_errors++; /* end of a packet.*/
 607                         if (status & 0x0890) lp->stats.rx_length_errors++;
 608                         if (status & 0x0004) lp->stats.rx_frame_errors++;
 609                         if (status & 0x0002) lp->stats.rx_crc_errors++;
 610                         if (status & 0x0001) lp->stats.rx_fifo_errors++;
 611                 } else {
 612                         /* Malloc up new buffer, compatible with net-2e. */
 613                         short pkt_len = lp->rx_ring[entry].status >> 16;
 614                         struct sk_buff *skb;
 615 
 616                         skb = dev_alloc_skb(pkt_len+2);
 617                         if (skb == NULL) {
 618                                 printk("%s: Memory squeeze, deferring packet.\n", dev->name);
 619                                 /* Check that at least two ring entries are free.
 620                                    If not, free one and mark stats->rx_dropped++. */
 621                                 for (i=0; i < RX_RING_SIZE; i++)
 622                                         if (lp->rx_ring[(entry+i) % RX_RING_SIZE].status < 0)
 623                                                 break;
 624 
 625                                 if (i > RX_RING_SIZE -2) {
 626                                         lp->stats.rx_dropped++;
 627                                         lp->rx_ring[entry].status = 0x80000000;
 628                                         lp->cur_rx++;
 629                                 }
 630                                 break;
 631                         }
 632                         skb->dev = dev;
 633                         skb_reserve(skb,2);     /* 16 byte align the data fields */
 634                         memcpy(skb_put(skb,pkt_len), lp->rx_ring[entry].buffer1, pkt_len);
 635                         skb->protocol=eth_type_trans(skb,dev);
 636                         netif_rx(skb);
 637                         lp->stats.rx_packets++;
 638                 }
 639 
 640                 lp->rx_ring[entry].status = 0x80000000;
 641                 entry = (++lp->cur_rx) % RX_RING_SIZE;
 642         }
 643 
 644         return 0;
 645 }
 646 
 647 static int
 648 tulip_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 649 {
 650         int ioaddr = dev->base_addr;
 651         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 652 
 653         dev->start = 0;
 654         dev->tbusy = 1;
 655 
 656         if (tulip_debug > 1)
 657                 printk("%s: Shutting down ethercard, status was %2.2x.\n",
 658                            dev->name, inl(ioaddr + CSR5));
 659 
 660         /* Disable interrupts by clearing the interrupt mask. */
 661         outl(0x00000000, ioaddr + CSR7);
 662         /* Stop the chip's Tx and Rx processes. */
 663         outl(inl(ioaddr + CSR6) & ~0x2002, ioaddr + CSR6);
 664 
 665         tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
 666 
 667         free_irq(dev->irq);
 668         irq2dev_map[dev->irq] = 0;
 669 
 670 #ifdef MODULE
 671         MOD_DEC_USE_COUNT;
 672 #endif
 673         return 0;
 674 }
 675 
 676 static struct enet_statistics *
 677 tulip_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 678 {
 679         struct tulip_private *tp = (struct tulip_private *)dev->priv;
 680         short ioaddr = dev->base_addr;
 681 
 682         tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
 683 
 684         return &tp->stats;
 685 }
 686 
 687 /* Set or clear the multicast filter for this adaptor.
 688    num_addrs == -1              Promiscuous mode, receive all packets
 689    num_addrs == 0               Normal mode, clear multicast list
 690    num_addrs > 0                Multicast mode, receive normal and MC packets, and do
 691                                                 best-effort filtering.
 692  */
 693 static void
 694 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
 695 {
 696         short ioaddr = dev->base_addr;
 697         int csr6 = inl(ioaddr + CSR6) & ~0x00D5;
 698 
 699         if (num_addrs > 15 || num_addrs == -2) {
 700                 /* Too many to filter perfectly -- accept all multicasts. */
 701                 outl(csr6 | 0x0080, ioaddr + CSR6);
 702         } else if (num_addrs < 0) {                     /* Set promiscuous. */
 703                 outl(csr6 | 0x00C0, ioaddr + CSR6);
 704                 /* Log any net taps. */
 705                 printk("%s: Promiscuous mode enabled.\n", dev->name);
 706         } else {
 707                 struct tulip_private *tp = (struct tulip_private *)dev->priv;
 708                 int *setup_frm = tp->setup_frame;
 709                 unsigned short *eaddrs = addrs;
 710                 int i;
 711 
 712                 /* We have <= 15 addresses that we can use the wonderful
 713                    16 address perfect filtering of the Tulip.  Note that only
 714                    the low shortword of setup_frame[] is valid. */
 715                 outl(csr6 | 0x0000, ioaddr + CSR6);
 716                 for(i = 0; i < num_addrs; i++) {
 717                         *setup_frm++ = *eaddrs++;
 718                         *setup_frm++ = *eaddrs++;
 719                         *setup_frm++ = *eaddrs++;
 720                 }
 721                 /* Fill the rest of the table with our physical address. */
 722                 eaddrs = (unsigned short *)dev->dev_addr;
 723                 do {
 724                         *setup_frm++ = eaddrs[0];
 725                         *setup_frm++ = eaddrs[1];
 726                         *setup_frm++ = eaddrs[2];
 727                 } while (++i < 16);
 728 
 729                 /* Now add this frame to the Tx list. */
 730         }
 731 }
 732 
 733 static int
 734 set_mac_address(struct device *dev, void *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 735 {
 736         int i;
 737         if (dev->start)
 738                 return -EBUSY;
 739         printk("%s: Setting MAC address to ", dev->name);
 740         for (i = 0; i < 6; i++)
 741                 printk(" %2.2x", dev->dev_addr[i] = ((unsigned char *)addr)[i]);
 742         printk(".\n");
 743         return 0;
 744 }
 745 
 746 #ifdef MODULE
 747 char kernel_version[] = UTS_RELEASE;
 748 static char devicename[9] = { 0, };
 749 static struct device dev_tulip = {
 750         devicename, /* device name is inserted by linux/drivers/net/net_init.c */
 751         0, 0, 0, 0,
 752         0, 0,
 753         0, 0, 0, NULL, tulip_probe
 754 };
 755 
 756 int io = 0;
 757 int irq = 0;
 758 
 759 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 760 {
 761         printk("tulip: Sorry, modularization is not completed\n");
 762         return -EIO;
 763 #if 0
 764         if (io == 0)
 765           printk("tulip: You should not use auto-probing with insmod!\n");
 766         dev_tulip.base_addr = io;
 767         dev_tulip.irq       = irq;
 768         if (register_netdev(&dev_tulip) != 0) {
 769                 printk("tulip: register_netdev() returned non-zero.\n");
 770                 return -EIO;
 771         }
 772         return 0;
 773 #endif
 774 }
 775 
 776 void
 777 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 778 {
 779         if (MOD_IN_USE)
 780                 printk("tulip: device busy, remove delayed\n");
 781         else
 782         {
 783                 unregister_netdev(&dev_tulip);
 784         }
 785 }
 786 #endif /* MODULE */
 787 
 788 /*
 789  * Local variables:
 790  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c tulip.c"
 791  *  c-indent-level: 4
 792  *  tab-width: 4
 793  * End:
 794  */

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