root/drivers/net/3c59x.c

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

DEFINITIONS

This source file includes following definitions.
  1. init_module
  2. tc59x_probe
  3. vortex_scan
  4. vortex_found_device
  5. vortex_probe1
  6. vortex_open
  7. vortex_timer
  8. vortex_start_xmit
  9. vortex_interrupt
  10. vortex_rx
  11. vortex_close
  12. vortex_get_stats
  13. update_stats
  14. set_multicast_list
  15. cleanup_module

   1 /* 3c59x.c: A 3Com 3c590/3c595 "Vortex" ethernet driver for linux. */
   2 /*
   3         Written 1995 by Donald Becker.
   4 
   5         This software may be used and distributed according to the terms
   6         of the GNU Public License, incorporated herein by reference.
   7 
   8         This driver is for the 3Com "Vortex" series ethercards.  Members of
   9         the series include the 3c590 PCI EtherLink III and 3c595-Tx PCI Fast
  10         EtherLink.  It also works with the 10Mbs-only 3c590 PCI EtherLink III.
  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 char *version = "3c59x.c:v0.11 1/21/96 becker@cesdis.gsfc.nasa.gov\n";
  18 
  19 /* "Knobs" that turn on special features. */
  20 /* Use bus master transfers instead of programmed-I/O for the Tx process.
  21    This is disabled by default! */
  22 #define VORTEX_BUS_MASTER
  23 
  24 /* Put out somewhat more debugging messages. (0 - no msg, 1 minimal msgs). */
  25 #define VORTEX_DEBUG 1
  26 
  27 #include <linux/module.h>
  28 
  29 #include <linux/kernel.h>
  30 #include <linux/sched.h>
  31 #include <linux/string.h>
  32 #include <linux/ptrace.h>
  33 #include <linux/errno.h>
  34 #include <linux/in.h>
  35 #include <linux/ioport.h>
  36 #include <linux/malloc.h>
  37 #include <linux/interrupt.h>
  38 #include <linux/pci.h>
  39 #include <linux/bios32.h>
  40 #include <linux/timer.h>
  41 #include <asm/bitops.h>
  42 #include <asm/io.h>
  43 
  44 #include <linux/netdevice.h>
  45 #include <linux/etherdevice.h>
  46 #include <linux/skbuff.h>
  47 
  48 /* The total size is twice that of the original EtherLinkIII series: the
  49    runtime register window, window 1, is now always mapped in. */
  50 #define VORTEX_TOTAL_SIZE 0x20
  51 
  52 #ifdef HAVE_DEVLIST
  53 struct netdev_entry tc59x_drv =
  54 {"Vortex", vortex_pci_probe, VORTEX_TOTAL_SIZE, NULL};
  55 #endif
  56 
  57 #ifdef VORTEX_DEBUG
  58 int vortex_debug = VORTEX_DEBUG;
  59 #else
  60 int vortex_debug = 1;
  61 #endif
  62 
  63 static int product_ids[] = {0x5900, 0x5950, 0x5951, 0x5952, 0, 0};
  64 static const char *product_names[] = {
  65         "3c590 Vortex 10Mbps",
  66         "3c595 Vortex 100baseTX",
  67         "3c595 Vortex 100baseT4",
  68         "3c595 Vortex 100base-MII",
  69         "EISA Vortex 3c597",
  70 };
  71 #define DEMON_INDEX 5                   /* Caution!  Must be consistent with above! */
  72 
  73 /*
  74                                 Theory of Operation
  75 
  76 I. Board Compatibility
  77 
  78 This device driver is designed for the 3Com FastEtherLink, 3Com's PCI to
  79 10/100baseT adapter.  It also works with the 3c590, a similar product
  80 with only a 10Mbs interface.
  81 
  82 II. Board-specific settings
  83 
  84 PCI bus devices are configured by the system at boot time, so no jumpers
  85 need to be set on the board.  The system BIOS should be set to assign the
  86 PCI INTA signal to an otherwise unused system IRQ line.  While it's
  87 physically possible to shared PCI interrupt lines, the 1.2.0 kernel doesn't
  88 support it.
  89 
  90 III. Driver operation
  91 
  92 The 3c59x series use an interface that's very similar to the previous 3c5x9
  93 series.  The primary interface is two programmed-I/O FIFOs, with an
  94 alternate single-contiguous-region bus-master transfer (see next).
  95 
  96 One extension that is advertised in a very large font is that the adapters
  97 are capable of being bus masters.  Unfortunately this capability is only for
  98 a single contiguous region making it less useful than the list of transfer
  99 regions available with the DEC Tulip or AMD PCnet.  Given the significant
 100 performance impact of taking an extra interrupt for each transfer, using
 101 DMA transfers is a win only with large blocks.
 102 
 103 IIIC. Synchronization
 104 The driver runs as two independent, single-threaded flows of control.  One
 105 is the send-packet routine, which enforces single-threaded use by the
 106 dev->tbusy flag.  The other thread is the interrupt handler, which is single
 107 threaded by the hardware and other software.
 108 
 109 IV. Notes
 110 
 111 Thanks to Cameron Spitzer and Terry Murphy of 3Com for providing both
 112 3c590 and 3c595 boards.
 113 The name "Vortex" is the internal 3Com project name for the PCI ASIC, and
 114 the not-yet-released (3/95) EISA version is called "Demon".  According to
 115 Terry these names come from rides at the local amusement park.
 116 
 117 The new chips support both ethernet (1.5K) and FDDI (4.5K) packet sizes!
 118 This driver only supports ethernet packets because of the skbuff allocation
 119 limit of 4K.
 120 */
 121 
 122 #define TCOM_VENDOR_ID  0x10B7          /* 3Com's manufacturer's ID. */
 123 
 124 /* Operational defintions.
 125    These are not used by other compilation units and thus are not
 126    exported in a ".h" file.
 127 
 128    First the windows.  There are eight register windows, with the command
 129    and status registers available in each.
 130    */
 131 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
 132 #define EL3_CMD 0x0e
 133 #define EL3_STATUS 0x0e
 134 
 135 /* The top five bits written to EL3_CMD are a command, the lower
 136    11 bits are the parameter, if applicable.
 137    Note that 11 parameters bits was fine for ethernet, but the new chip
 138    can handle FDDI lenght frames (~4500 octets) and now parameters count
 139    32-bit 'Dwords' rather than octets. */
 140 
 141 enum vortex_cmd {
 142         TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
 143         RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
 144         TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
 145         FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
 146         SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
 147         SetTxThreshold = 18<<11, SetTxStart = 19<<11,
 148         StartDMAUp = 20<<11, StartDMADown = (20<<11)+1, StatsEnable = 21<<11,
 149         StatsDisable = 22<<11, StopCoax = 23<<11,};
 150 
 151 /* The SetRxFilter command accepts the following classes: */
 152 enum RxFilter {
 153         RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
 154 
 155 /* Bits in the general status register. */
 156 enum vortex_status {
 157         IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
 158         TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
 159         IntReq = 0x0040, StatsFull = 0x0080, DMADone = 1<<8,
 160         DMAInProgress = 1<<11,                  /* DMA controller is still busy.*/
 161         CmdInProgress = 1<<12,                  /* EL3_CMD is still busy.*/
 162 };
 163 
 164 /* Register window 1 offsets, the window used in normal operation.
 165    On the Vortex this window is always mapped at offsets 0x10-0x1f. */
 166 enum Window1 {
 167         TX_FIFO = 0x10,  RX_FIFO = 0x10,  RxErrors = 0x14,
 168         RxStatus = 0x18,  Timer=0x1A, TxStatus = 0x1B,
 169         TxFree = 0x1C, /* Remaining free bytes in Tx buffer. */
 170 };
 171 enum Window0 {
 172         Wn0EepromCmd = 10,              /* Window 0: EEPROM command register. */
 173 };
 174 enum Win0_EEPROM_bits {
 175         EEPROM_Read = 0x80, EEPROM_WRITE = 0x40, EEPROM_ERASE = 0xC0,
 176         EEPROM_EWENB = 0x30,            /* Enable erasing/writing for 10 msec. */
 177         EEPROM_EWDIS = 0x00,            /* Disable EWENB before 10 msec timeout. */
 178 };
 179 /* EEPROM locations. */
 180 enum eeprom_offset {
 181         PhysAddr01=0, PhysAddr23=1, PhysAddr45=2, ModelID=3,
 182         EtherLink3ID=7, IFXcvrIO=8, IRQLine=9,
 183         NodeAddr01=10, NodeAddr23=11, NodeAddr45=12,
 184         DriverTune=13, Checksum=15};
 185 
 186 enum Window3 {                  /* Window 3: MAC/config bits. */
 187         Wn3_Config=0, Wn3_MAC_Ctrl=6, Wn3_Options=8,
 188 };
 189 union wn3_config {
 190         int i;
 191         struct w3_config_fields {
 192                 unsigned int ram_size:3, ram_width:1, ram_speed:2, rom_size:2;
 193                 int pad8:8;
 194                 unsigned int ram_split:2, pad18:2, xcvr:3, pad21:1, autoselect:1;
 195                 int pad24:8;
 196         } u;
 197 };
 198 
 199 enum Window4 {
 200         Wn4_Media = 0x0A,               /* Window 4: Various transcvr/media bits. */
 201 };
 202 enum Win4_Media_bits {
 203         Media_TP = 0x00C0,              /* Enable link beat and jabber for 10baseT. */
 204 };
 205 enum Window7 {                                  /* Window 7: Bus Master control. */
 206         Wn7_MasterAddr = 0, Wn7_MasterLen = 6, Wn7_MasterStatus = 12,
 207 };
 208 
 209 struct vortex_private {
 210         char devname[8];                        /* "ethN" string, also for kernel debug. */
 211         const char *product_name;
 212         struct device *next_module;
 213         struct enet_statistics stats;
 214 #ifdef VORTEX_BUS_MASTER
 215         struct sk_buff *tx_skb;         /* Packet being eaten by bus master ctrl.  */
 216 #endif
 217         struct timer_list timer;        /* Media selection timer. */
 218         int options;                            /* User-settable driver options (none yet). */
 219         unsigned int media_override:3, full_duplex:1, bus_master:1, autoselect:1;
 220 };
 221 
 222 static char *if_names[] = {
 223         "10baseT", "10Mbs AUI", "undefined", "10base2",
 224         "100baseTX", "100baseFX", "MII", "undefined"};
 225 
 226 static int vortex_scan(struct device *dev);
 227 static int vortex_found_device(struct device *dev, int ioaddr, int irq,
 228                                                            int product_index, int options);
 229 static int vortex_probe1(struct device *dev);
 230 static int vortex_open(struct device *dev);
 231 static void vortex_timer(unsigned long arg);
 232 static int vortex_start_xmit(struct sk_buff *skb, struct device *dev);
 233 static int vortex_rx(struct device *dev);
 234 static void vortex_interrupt(int irq, struct pt_regs *regs);
 235 static int vortex_close(struct device *dev);
 236 static void update_stats(int addr, struct device *dev);
 237 static struct enet_statistics *vortex_get_stats(struct device *dev);
 238 static void set_multicast_list(struct device *dev);
 239 
 240 
 241 /* Unlike the other PCI cards the 59x cards don't need a large contiguous
 242    memory region, so making the driver a loadable module is feasible.
 243 
 244    Unfortuneately maximizing the shared code between the integrated and
 245    module version of the driver results in a complicated set of initialization
 246    procedures.
 247    init_module() -- modules /  tc59x_init()  -- built-in
 248                 The wrappers for vortex_scan()
 249    vortex_scan()                 The common routine that scans for PCI and EISA cards
 250    vortex_found_device() Allocate a device structure when we find a card.
 251                                         Different versions exist for modules and built-in.
 252    vortex_probe1()              Fill in the device structure -- this is seperated
 253                                         so that the modules code can put it in dev->init.
 254 */
 255 /* This driver uses 'options' to pass the media type, full-duplex flag, etc. */
 256 /* Note: this is the only limit on the number of cards supported!! */
 257 int options[8] = { -1, -1, -1, -1, -1, -1, -1, -1,};
 258 
 259 #ifdef MODULE
 260 static int debug = -1;
 261 /* A list of all installed Vortex devices, for removing the driver module. */
 262 static struct device *root_vortex_dev = NULL;
 263 
 264 int
 265 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 266 {
 267         int cards_found;
 268 
 269         if (debug >= 0)
 270                 vortex_debug = debug;
 271         if (vortex_debug)
 272                 printk(version);
 273 
 274         root_vortex_dev = NULL;
 275         cards_found = vortex_scan(0);
 276         return cards_found < 0 ? cards_found : 0;
 277 }
 278 
 279 #else
 280 unsigned long tc59x_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 281 {
 282         int cards_found = 0;
 283 
 284         cards_found = vortex_scan(dev);
 285 
 286         if (vortex_debug > 0  &&  cards_found)
 287                 printk(version);
 288 
 289         return cards_found ? 0 : -ENODEV;
 290 }
 291 #endif  /* not MODULE */
 292 
 293 static int vortex_scan(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 294 {
 295         int cards_found = 0;
 296 
 297         if (pcibios_present()) {
 298                 int pci_index;
 299                 for (pci_index = 0; pci_index < 8; pci_index++) {
 300                         unsigned char pci_bus, pci_device_fn, pci_irq_line, pci_latency;
 301                         unsigned int pci_ioaddr;
 302                         unsigned short pci_command;
 303                         int index;
 304 
 305                         for (index = 0; product_ids[index]; index++) {
 306                                 if ( ! pcibios_find_device(TCOM_VENDOR_ID, product_ids[index],
 307                                                                                    pci_index, &pci_bus,
 308                                                                                    &pci_device_fn))
 309                                         break;
 310                         }
 311                         if ( ! product_ids[index])
 312                                 break;
 313 
 314                         pcibios_read_config_byte(pci_bus, pci_device_fn,
 315                                                                          PCI_INTERRUPT_LINE, &pci_irq_line);
 316                         pcibios_read_config_dword(pci_bus, pci_device_fn,
 317                                                                           PCI_BASE_ADDRESS_0, &pci_ioaddr);
 318                         /* Remove I/O space marker in bit 0. */
 319                         pci_ioaddr &= ~3;
 320 
 321 #ifdef VORTEX_BUS_MASTER
 322                         /* Get and check the bus-master and latency values.
 323                            Some PCI BIOSes fail to set the master-enable bit, and
 324                            the latency timer must be set to the maximum value to avoid
 325                            data corruption that occurs when the timer expires during
 326                            a transfer.  Yes, it's a bug. */
 327                         pcibios_read_config_word(pci_bus, pci_device_fn,
 328                                                                          PCI_COMMAND, &pci_command);
 329                         if ( ! (pci_command & PCI_COMMAND_MASTER)) {
 330                                 printk("  PCI Master Bit has not been set! Setting...\n");
 331                                 pci_command |= PCI_COMMAND_MASTER;
 332                                 pcibios_write_config_word(pci_bus, pci_device_fn,
 333                                                                                   PCI_COMMAND, pci_command);
 334                         }
 335                         pcibios_read_config_byte(pci_bus, pci_device_fn,
 336                                                                                  PCI_LATENCY_TIMER, &pci_latency);
 337                         if (pci_latency != 255) {
 338                                 printk("  Overriding PCI latency timer (CFLT) setting of %d, new value is 255.\n", pci_latency);
 339                                 pcibios_write_config_byte(pci_bus, pci_device_fn,
 340                                                                                   PCI_LATENCY_TIMER, 255);
 341                         }
 342 #endif  /* VORTEX_BUS_MASTER */
 343                         vortex_found_device(dev, pci_ioaddr, pci_irq_line,
 344                                                            index, options[cards_found]);
 345                         dev = 0;
 346                         cards_found++;
 347                 }
 348         }
 349 
 350         /* Now check all slots of the EISA bus. */
 351         if (EISA_bus) {
 352                 static int ioaddr = 0x1000;
 353                 for (ioaddr = 0x1000; ioaddr < 0x9000; ioaddr += 0x1000) {
 354                         /* Check the standard EISA ID register for an encoded '3Com'. */
 355                         if (inw(ioaddr + 0xC80) != 0x6d50)
 356                                 continue;
 357                         /* Check for a product that we support. */
 358                         if ((inw(ioaddr + 0xC82) & 0xFFF0) != 0x5970
 359                                 && (inw(ioaddr + 0xC82) & 0xFFF0) != 0x5920)
 360                                 continue;
 361                         vortex_found_device(dev, ioaddr, inw(ioaddr + 0xC88) >> 12,
 362                                                            DEMON_INDEX, options[cards_found]);
 363                         dev = 0;
 364                         cards_found++;
 365                 }
 366         }
 367 
 368         return cards_found;
 369 }
 370 
 371 static int vortex_found_device(struct device *dev, int ioaddr, int irq,
     /* [previous][next][first][last][top][bottom][index][help] */
 372                                                            int product_index, int options)
 373 {
 374         struct vortex_private *vp;
 375 
 376 #ifdef MODULE
 377         /* Allocate and fill new device structure. */
 378         int dev_size = sizeof(struct device) +
 379                 sizeof(struct vortex_private);
 380         
 381         dev = (struct device *) kmalloc(dev_size, GFP_KERNEL);
 382         memset(dev, 0, dev_size);
 383         dev->priv = ((void *)dev) + sizeof(struct device);
 384         vp = (struct vortex_private *)dev->priv;
 385         dev->name = vp->devname; /* An empty string. */
 386         dev->base_addr = ioaddr;
 387         dev->irq = irq;
 388         dev->init = vortex_probe1;
 389         vp->product_name = product_names[product_index];
 390         vp->options = options;
 391         if (options >= 0) {
 392                 vp->media_override = options & 7;
 393                 vp->full_duplex = (options & 8) ? 1 : 0;
 394                 vp->bus_master = (options & 16) ? 1 : 0;
 395         } else {
 396                 vp->media_override = 7;
 397                 vp->full_duplex = 0;
 398                 vp->bus_master = 0;
 399         }
 400         ether_setup(dev);
 401         vp->next_module = root_vortex_dev;
 402         root_vortex_dev = dev;
 403         if (register_netdev(dev) != 0)
 404                 return -EIO;
 405 #else  /* not a MODULE */
 406         if (dev) {
 407                 dev->priv = kmalloc(sizeof (struct vortex_private), GFP_KERNEL);
 408                 memset(dev->priv, 0, sizeof (struct vortex_private));
 409         }
 410         dev = init_etherdev(dev, sizeof(struct vortex_private));
 411         dev->base_addr = ioaddr;
 412         dev->irq = irq;
 413         vp  = (struct vortex_private *)dev->priv;
 414         vp->product_name = product_names[product_index];
 415         vp->options = options;
 416         if (options >= 0) {
 417                 vp->media_override = options & 7;
 418                 vp->full_duplex = (options & 8) ? 1 : 0;
 419                 vp->bus_master = (options & 16) ? 1 : 0;
 420         } else {
 421                 vp->media_override = 7;
 422                 vp->full_duplex = 0;
 423                 vp->bus_master = 0;
 424         }
 425 
 426         vortex_probe1(dev);
 427 #endif /* MODULE */
 428         return 0;
 429 }
 430 
 431 static int vortex_probe1(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 432 {
 433         int ioaddr = dev->base_addr;
 434         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 435         int i;
 436 
 437         printk("%s: 3Com %s at %#3x,", dev->name,
 438                    vp->product_name, ioaddr);
 439 
 440         /* Read the station address from the EEPROM. */
 441         EL3WINDOW(0);
 442         for (i = 0; i < 3; i++) {
 443                 short *phys_addr = (short *)dev->dev_addr;
 444                 int timer;
 445                 outw(EEPROM_Read + PhysAddr01 + i, ioaddr + Wn0EepromCmd);
 446                 /* Pause for at least 162 us. for the read to take place. */
 447                 for (timer = 0; timer < 162*4 + 400; timer++) {
 448                         SLOW_DOWN_IO;
 449                         if ((inw(ioaddr + Wn0EepromCmd) & 0x8000) == 0)
 450                                 break;
 451                 }
 452                 phys_addr[i] = htons(inw(ioaddr + 12));
 453         }
 454         for (i = 0; i < 6; i++)
 455                 printk("%c%2.2x", i ? ':' : ' ', dev->dev_addr[i]);
 456         printk(", IRQ %d\n", dev->irq);
 457         /* Tell them about an invalid IRQ. */
 458         if (vortex_debug && (dev->irq <= 0 || dev->irq > 15))
 459                 printk(" *** Warning: this IRQ is unlikely to work!\n");
 460 
 461         {
 462                 char *ram_split[] = {"5:3", "3:1", "1:1", "invalid"};
 463                 union wn3_config config;
 464                 EL3WINDOW(3);
 465                 config.i = inl(ioaddr + Wn3_Config);
 466                 if (vortex_debug > 1)
 467                         printk("  Internal config register is %4.4x, transceivers %#x.\n",
 468                                    config.i, inw(ioaddr + Wn3_Options));
 469                 printk("  %dK %s-wide RAM %s Rx:Tx split, %s%s interface.\n",
 470                            8 << config.u.ram_size,
 471                            config.u.ram_width ? "word" : "byte",
 472                            ram_split[config.u.ram_split],
 473                            config.u.autoselect ? "autoselect/" : "",
 474                            if_names[config.u.xcvr]);
 475                 dev->if_port = config.u.xcvr;
 476                 vp->autoselect = config.u.autoselect;
 477         }
 478 
 479         /* We do a request_region() only to register /proc/ioports info. */
 480         request_region(ioaddr, VORTEX_TOTAL_SIZE, vp->product_name);
 481 
 482         /* The 3c59x-specific entries in the device structure. */
 483         dev->open = &vortex_open;
 484         dev->hard_start_xmit = &vortex_start_xmit;
 485         dev->stop = &vortex_close;
 486         dev->get_stats = &vortex_get_stats;
 487         dev->set_multicast_list = &set_multicast_list;
 488 #if defined (HAVE_SET_MAC_ADDR) && 0
 489         dev->set_mac_address = &set_mac_address;
 490 #endif
 491 
 492         return 0;
 493 }
 494 
 495 
 496 static int
 497 vortex_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 498 {
 499         int ioaddr = dev->base_addr;
 500         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 501         union wn3_config config;
 502         int i;
 503 
 504         /* Before initializing select the active media port. */
 505         EL3WINDOW(3);
 506         if (vp->full_duplex)
 507                 outb(0x20, ioaddr + Wn3_MAC_Ctrl); /* Set the full-duplex bit. */
 508         config.i = inl(ioaddr + Wn3_Config);
 509 
 510         if (vp->media_override != 7) {
 511                 if (vortex_debug > 1)
 512                         printk("%s: Media override to transceiver %d (%s).\n",
 513                                    dev->name, vp->media_override, if_names[vp->media_override]);
 514                 config.u.xcvr = vp->media_override;
 515                 dev->if_port = vp->media_override;
 516                 outl(config.i, ioaddr + Wn3_Config);
 517         }
 518 
 519         if (vortex_debug > 1) {
 520                 printk("%s: vortex_open() InternalConfig %8.8x.\n",
 521                         dev->name, config.i);
 522         }
 523 
 524         outw(TxReset, ioaddr + EL3_CMD);
 525         for (i = 20; i >= 0 ; i--)
 526                 if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 527                         break;
 528 
 529         outw(RxReset, ioaddr + EL3_CMD);
 530         /* Wait a few ticks for the RxReset command to complete. */
 531         for (i = 20; i >= 0 ; i--)
 532                 if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 533                         break;
 534 
 535         outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
 536 
 537         if (irq2dev_map[dev->irq] != NULL
 538                 || (irq2dev_map[dev->irq] = dev) == NULL
 539                 || dev->irq == 0
 540                 || request_irq(dev->irq, &vortex_interrupt, 0, vp->product_name)) {
 541                 return -EAGAIN;
 542         }
 543 
 544         if (vortex_debug > 1) {
 545                 EL3WINDOW(4);
 546                 printk("%s: vortex_open() irq %d media status %4.4x.\n",
 547                            dev->name, dev->irq, inw(ioaddr + Wn4_Media));
 548         }
 549 
 550         /* Set the station address and mask in window 2 each time opened. */
 551         EL3WINDOW(2);
 552         for (i = 0; i < 6; i++)
 553                 outb(dev->dev_addr[i], ioaddr + i);
 554         for (; i < 12; i+=2)
 555                 outw(0, ioaddr + i);
 556 
 557         if (dev->if_port == 3)
 558                 /* Start the thinnet transceiver. We should really wait 50ms...*/
 559                 outw(StartCoax, ioaddr + EL3_CMD);
 560         else if (dev->if_port == 0) {
 561                 /* 10baseT interface, enabled link beat and jabber check. */
 562                 EL3WINDOW(4);
 563                 outw(inw(ioaddr + Wn4_Media) | Media_TP, ioaddr + Wn4_Media);
 564         }
 565 
 566         /* Switch to the stats window, and clear all stats by reading. */
 567         outw(StatsDisable, ioaddr + EL3_CMD);
 568         EL3WINDOW(6);
 569         for (i = 0; i < 10; i++)        
 570                 inb(ioaddr + i);
 571         inw(ioaddr + 10);
 572         inw(ioaddr + 12);
 573         /* New: On the Vortex we must also clear the BadSSD counter. */
 574         EL3WINDOW(3);
 575         inb(ioaddr + 12);
 576 
 577         /* Switch to register set 7 for normal use. */
 578         EL3WINDOW(7);
 579 
 580         /* Accept b-case and phys addr only. */
 581         outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
 582         outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
 583 
 584         dev->tbusy = 0;
 585         dev->interrupt = 0;
 586         dev->start = 1;
 587 
 588         outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
 589         outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
 590         /* Allow status bits to be seen. */
 591         outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
 592         /* Ack all pending events, and set active indicator mask. */
 593         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
 594                  ioaddr + EL3_CMD);
 595         outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
 596                  | DMADone, ioaddr + EL3_CMD);
 597 
 598 #ifdef MODULE
 599         MOD_INC_USE_COUNT;
 600 #endif
 601 
 602         if (vp->autoselect) {
 603                 init_timer(&vp->timer);
 604                 vp->timer.expires = (14*HZ)/10;                         /* 1.4 sec. */
 605                 vp->timer.data = (unsigned long)dev;
 606                 vp->timer.function = &vortex_timer;    /* timer handler */
 607                 add_timer(&vp->timer);
 608         }
 609         return 0;
 610 }
 611 
 612 static void vortex_timer(unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
 613 {
 614         struct device *dev = (struct device *)data;
 615         if (vortex_debug > 2)
 616                 printk("%s: Media selection timer tick happened.\n", dev->name);
 617         /* ToDo: active media selection here! */
 618 }
 619 
 620 static int
 621 vortex_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 622 {
 623         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 624         int ioaddr = dev->base_addr;
 625 
 626         /* Transmitter timeout, serious problems. */
 627         if (dev->tbusy) {
 628                 int tickssofar = jiffies - dev->trans_start;
 629                 if (tickssofar < 40)
 630                         return 1;
 631                 printk("%s: transmit timed out, tx_status %2.2x status %4.4x.\n",
 632                            dev->name, inb(ioaddr + TxStatus), inw(ioaddr + EL3_STATUS));
 633                 vp->stats.tx_errors++;
 634                 /* Issue TX_RESET and TX_START commands. */
 635                 outw(TxReset, ioaddr + EL3_CMD);
 636                 {
 637                         int i;
 638                         for (i = 20; i >= 0 ; i--)
 639                                 if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 640                                         break;
 641                 }
 642                 outw(TxEnable, ioaddr + EL3_CMD);
 643                 dev->trans_start = jiffies;
 644                 dev->tbusy = 0;
 645                 return 0;
 646         }
 647 
 648         if (skb == NULL || skb->len <= 0) {
 649                 printk("%s: Obsolete driver layer request made: skbuff==NULL.\n",
 650                            dev->name);
 651                 dev_tint(dev);
 652                 return 0;
 653         }
 654 
 655         /* Block a timer-based transmit from overlapping.  This could better be
 656            done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
 657            If this ever occurs the queue layer is doing something evil! */
 658         if (set_bit(0, (void*)&dev->tbusy) != 0) {
 659                 printk("%s: Transmitter access conflict.\n", dev->name);
 660                 return 1;
 661         }
 662 
 663         /* Put out the doubleword header... */
 664         outl(skb->len, ioaddr + TX_FIFO);
 665 #ifdef VORTEX_BUS_MASTER
 666         if (vp->bus_master) {
 667                 /* Set the bus-master controller to transfer the packet. */
 668                 outl((int)(skb->data), ioaddr + Wn7_MasterAddr);
 669                 outw((skb->len + 3) & ~3, ioaddr + Wn7_MasterLen);
 670                 vp->tx_skb = skb;
 671                 outw(StartDMADown, ioaddr + EL3_CMD);
 672         } else {
 673                 /* ... and the packet rounded to a doubleword. */
 674                 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
 675                 dev_kfree_skb (skb, FREE_WRITE);
 676                 if (inw(ioaddr + TxFree) > 1536) {
 677                         dev->tbusy = 0;
 678                 } else
 679                         /* Interrupt us when the FIFO has room for max-sized packet. */
 680                         outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
 681         }
 682 #else
 683         /* ... and the packet rounded to a doubleword. */
 684         outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
 685         dev_kfree_skb (skb, FREE_WRITE);
 686         if (inw(ioaddr + TxFree) > 1536) {
 687                 dev->tbusy = 0;
 688         } else
 689                 /* Interrupt us when the FIFO has room for max-sized packet. */
 690                 outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
 691 #endif  /* bus master */
 692 
 693         dev->trans_start = jiffies;
 694 
 695         /* Clear the Tx status stack. */
 696         {
 697                 short tx_status;
 698                 int i = 4;
 699 
 700                 while (--i > 0  &&      (tx_status = inb(ioaddr + TxStatus)) > 0) {
 701                         if (tx_status & 0x3C) {         /* A Tx-disabling error occured.  */
 702                                 if (vortex_debug > 2)
 703                                   printk("%s: Tx error, status %2.2x.\n",
 704                                                  dev->name, tx_status);
 705                                 if (tx_status & 0x04) vp->stats.tx_fifo_errors++;
 706                                 if (tx_status & 0x38) vp->stats.tx_aborted_errors++;
 707                                 if (tx_status & 0x30) {
 708                                         int j;
 709                                         outw(TxReset, ioaddr + EL3_CMD);
 710                                         for (j = 20; j >= 0 ; j--)
 711                                                 if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 712                                                         break;
 713                                 }
 714                                 outw(TxEnable, ioaddr + EL3_CMD);
 715                         }
 716                         outb(0x00, ioaddr + TxStatus); /* Pop the status stack. */
 717                 }
 718         }
 719         return 0;
 720 }
 721 
 722 /* The interrupt handler does all of the Rx thread work and cleans up
 723    after the Tx thread. */
 724 static void vortex_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 725 {
 726         struct device *dev = (struct device *)(irq2dev_map[irq]);
 727         struct vortex_private *lp;
 728         int ioaddr, status;
 729         int latency;
 730         int i = 0;
 731 
 732         if (dev == NULL) {
 733                 printk ("vortex_interrupt(): irq %d for unknown device.\n", irq);
 734                 return;
 735         }
 736 
 737         if (dev->interrupt)
 738                 printk("%s: Re-entering the interrupt handler.\n", dev->name);
 739         dev->interrupt = 1;
 740 
 741         ioaddr = dev->base_addr;
 742         latency = inb(ioaddr + Timer);
 743         lp = (struct vortex_private *)dev->priv;
 744 
 745         status = inw(ioaddr + EL3_STATUS);
 746 
 747         if (vortex_debug > 4)
 748                 printk("%s: interrupt, status %4.4x, timer %d.\n", dev->name,
 749                            status, latency);
 750         if ((status & 0xE000) != 0xE000) {
 751                 static int donedidthis=0;
 752                 /* Some interrupt controllers store a bogus interrupt from boot-time.
 753                    Ignore a single early interrupt, but don't hang the machine for
 754                    other interrupt problems. */
 755                 if (donedidthis++ > 1) {
 756                         printk("%s: Bogus interrupt, bailing. Status %4.4x, start=%d.\n",
 757                                    dev->name, status, dev->start);
 758                         free_irq(dev->irq);
 759                 }
 760         }
 761 
 762         do {
 763                 if (vortex_debug > 5)
 764                                 printk("%s: In interrupt loop, status %4.4x.\n",
 765                                            dev->name, status);
 766                 if (status & RxComplete)
 767                         vortex_rx(dev);
 768 
 769                 if (status & TxAvailable) {
 770                         if (vortex_debug > 5)
 771                                 printk("        TX room bit was handled.\n");
 772                         /* There's room in the FIFO for a full-sized packet. */
 773                         outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
 774                         dev->tbusy = 0;
 775                         mark_bh(NET_BH);
 776                 }
 777 #ifdef VORTEX_BUS_MASTER
 778                 if (status & DMADone) {
 779                         outw(0x1000, ioaddr + Wn7_MasterStatus); /* Ack the event. */
 780                         dev->tbusy = 0;
 781                         mark_bh(NET_BH);
 782                 }
 783 #endif
 784                 if (status & (AdapterFailure | RxEarly | StatsFull)) {
 785                         /* Handle all uncommon interrupts at once. */
 786                         if (status & RxEarly) {                         /* Rx early is unused. */
 787                                 vortex_rx(dev);
 788                                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
 789                         }
 790                         if (status & StatsFull) {       /* Empty statistics. */
 791                                 static int DoneDidThat = 0;
 792                                 if (vortex_debug > 4)
 793                                         printk("%s: Updating stats.\n", dev->name);
 794                                 update_stats(ioaddr, dev);
 795                                 /* DEBUG HACK: Disable statistics as an interrupt source. */
 796                                 /* This occurs when we have the wrong media type! */
 797                                 if (DoneDidThat == 0  &&
 798                                         inw(ioaddr + EL3_STATUS) & StatsFull) {
 799                                         int win, reg;
 800                                         printk("%s: Updating stats failed, disabling stats as an"
 801                                                    " interrupt source.\n", dev->name);
 802                                         for (win = 0; win < 8; win++) {
 803                                                 EL3WINDOW(win);
 804                                                 printk("\n Vortex window %d:", win);
 805                                                 for (reg = 0; reg < 16; reg++)
 806                                                         printk(" %2.2x", inb(ioaddr+reg));
 807                                         }
 808                                         EL3WINDOW(7);
 809                                         outw(SetIntrEnb | 0x18, ioaddr + EL3_CMD);
 810                                         DoneDidThat++;
 811                                 }
 812                         }
 813                         if (status & AdapterFailure) {
 814                                 /* Adapter failure requires Rx reset and reinit. */
 815                                 outw(RxReset, ioaddr + EL3_CMD);
 816                                 /* Set the Rx filter to the current state. */
 817                                 outw(SetRxFilter | RxStation | RxBroadcast
 818                                          | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
 819                                          | (dev->flags & IFF_PROMISC ? RxProm : 0),
 820                                          ioaddr + EL3_CMD);
 821                                 outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
 822                                 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
 823                         }
 824                 }
 825 
 826                 if (++i > 10) {
 827                         printk("%s: Infinite loop in interrupt, status %4.4x.  "
 828                                    "Disabling functions (%4.4x).\n",
 829                                    dev->name, status, SetStatusEnb | ((~status) & 0xFE));
 830                         /* Disable all pending interrupts. */
 831                         outw(SetStatusEnb | ((~status) & 0xFE), ioaddr + EL3_CMD);
 832                         outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
 833                         break;
 834                 }
 835                 /* Acknowledge the IRQ. */
 836                 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
 837 
 838         } while ((status = inw(ioaddr + EL3_STATUS)) & (IntLatch | RxComplete));
 839 
 840         if (vortex_debug > 4)
 841                 printk("%s: exiting interrupt, status %4.4x.\n", dev->name, status);
 842 
 843         dev->interrupt = 0;
 844         return;
 845 }
 846 
 847 static int
 848 vortex_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 849 {
 850         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 851         int ioaddr = dev->base_addr;
 852         int i;
 853         short rx_status;
 854 
 855         if (vortex_debug > 5)
 856                 printk("   In rx_packet(), status %4.4x, rx_status %4.4x.\n",
 857                            inw(ioaddr+EL3_STATUS), inw(ioaddr+RxStatus));
 858         while ((rx_status = inw(ioaddr + RxStatus)) > 0) {
 859                 if (rx_status & 0x4000) { /* Error, update stats. */
 860                         unsigned char rx_error = inb(ioaddr + RxErrors);
 861                         if (vortex_debug > 4)
 862                                 printk(" Rx error: status %2.2x.\n", rx_error);
 863                         vp->stats.rx_errors++;
 864                         if (rx_error & 0x01)  vp->stats.rx_over_errors++;
 865                         if (rx_error & 0x02)  vp->stats.rx_length_errors++;
 866                         if (rx_error & 0x04)  vp->stats.rx_frame_errors++;
 867                         if (rx_error & 0x08)  vp->stats.rx_crc_errors++;
 868                         if (rx_error & 0x10)  vp->stats.rx_length_errors++;
 869                 } else {
 870                         /* The packet length: up to 4.5K!. */
 871                         short pkt_len = rx_status & 0x1fff;
 872                         struct sk_buff *skb;
 873 
 874                         skb = dev_alloc_skb(pkt_len + 5);
 875                         if (vortex_debug > 4)
 876                                 printk("Receiving packet size %d status %4.4x.\n",
 877                                            pkt_len, rx_status);
 878                         if (skb != NULL) {
 879                                 skb->dev = dev;
 880                                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
 881                                 /* 'skb_put()' points to the start of sk_buff data area. */
 882                                 insl(ioaddr + RX_FIFO, skb_put(skb, pkt_len),
 883                                          (pkt_len + 3) >> 2);
 884                                 skb->protocol = eth_type_trans(skb, dev);
 885                                 netif_rx(skb);
 886                                 outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
 887                                 /* Wait a limited time to go to next packet. */
 888                                 for (i = 200; i >= 0; i--)
 889                                         if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 890                                                 break;
 891                                 vp->stats.rx_packets++;
 892                                 continue;
 893                         } else if (vortex_debug)
 894                                 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
 895                                            dev->name, pkt_len);
 896                 }
 897                 vp->stats.rx_dropped++;
 898                 outw(RxDiscard, ioaddr + EL3_CMD);
 899                 /* Wait a limited time to skip this packet. */
 900                 for (i = 200; i >= 0; i--)
 901                         if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 902                                 break;
 903         }
 904 
 905         return 0;
 906 }
 907 
 908 static int
 909 vortex_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 910 {
 911         int ioaddr = dev->base_addr;
 912 
 913         dev->start = 0;
 914         dev->tbusy = 1;
 915 
 916         if (vortex_debug > 1)
 917                 printk("%s: vortex_close() status %4.4x, Tx status %2.2x.\n",
 918                            dev->name, inw(ioaddr + EL3_STATUS), inb(ioaddr + TxStatus));
 919 
 920         /* Turn off statistics ASAP.  We update lp->stats below. */
 921         outw(StatsDisable, ioaddr + EL3_CMD);
 922 
 923         /* Disable the receiver and transmitter. */
 924         outw(RxDisable, ioaddr + EL3_CMD);
 925         outw(TxDisable, ioaddr + EL3_CMD);
 926 
 927         if (dev->if_port == 3)
 928                 /* Turn off thinnet power.  Green! */
 929                 outw(StopCoax, ioaddr + EL3_CMD);
 930         else if (dev->if_port == 0) {
 931                 /* Disable link beat and jabber, if_port may change ere next open(). */
 932                 EL3WINDOW(4);
 933                 outw(inw(ioaddr + Wn4_Media) & ~Media_TP, ioaddr + Wn4_Media);
 934         }
 935 
 936         free_irq(dev->irq);
 937         /* Mmmm, we should diable all interrupt sources here. */
 938         irq2dev_map[dev->irq] = 0;
 939 
 940         update_stats(ioaddr, dev);
 941 #ifdef MODULE
 942         MOD_DEC_USE_COUNT;
 943 #endif
 944 
 945         return 0;
 946 }
 947 
 948 static struct enet_statistics *
 949 vortex_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 950 {
 951         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 952         unsigned long flags;
 953 
 954         save_flags(flags);
 955         cli();
 956         update_stats(dev->base_addr, dev);
 957         restore_flags(flags);
 958         return &vp->stats;
 959 }
 960 
 961 /*  Update statistics.
 962         Unlike with the EL3 we need not worry about interrupts changing
 963         the window setting from underneath us, but we must still guard
 964         against a race condition with a StatsUpdate interrupt updating the
 965         table.  This is done by checking that the ASM (!) code generated uses
 966         atomic updates with '+='.
 967         */
 968 static void update_stats(int ioaddr, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 969 {
 970         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 971 
 972         /* Unlike the 3c5x9 we need not turn off stats updates while reading. */
 973         /* Switch to the stats window, and read everything. */
 974         EL3WINDOW(6);
 975         vp->stats.tx_carrier_errors             += inb(ioaddr + 0);
 976         vp->stats.tx_heartbeat_errors   += inb(ioaddr + 1);
 977         /* Multiple collisions. */              inb(ioaddr + 2);
 978         vp->stats.collisions                    += inb(ioaddr + 3);
 979         vp->stats.tx_window_errors              += inb(ioaddr + 4);
 980         vp->stats.rx_fifo_errors                += inb(ioaddr + 5);
 981         vp->stats.tx_packets                    += inb(ioaddr + 6);
 982         vp->stats.tx_packets                    += (inb(ioaddr + 9)&15) << 8;
 983         /* Rx packets   */                              inb(ioaddr + 7);   /* Must read to clear */
 984         /* Tx deferrals */                              inb(ioaddr + 8);
 985         /* Don't bother with register 9, an extention of registers 6&7.
 986            If we do use the 6&7 values the atomic update assumption above
 987            is invalid. */
 988         inw(ioaddr + 10);       /* Total Rx and Tx octets. */
 989         inw(ioaddr + 12);
 990         /* New: On the Vortex we must also clear the BadSSD counter. */
 991         EL3WINDOW(3);
 992         inb(ioaddr + 12);
 993 
 994         /* We change back to window 7 (not 1) with the Vortex. */
 995         EL3WINDOW(7);
 996         return;
 997 }
 998 
 999 /* There are two version of set_multicast_list() to support both v1.2 and
1000    v1.4 kernels. */
1001 static void
1002 set_multicast_list(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1003 {
1004         short ioaddr = dev->base_addr;
1005 
1006         if ((dev->mc_list)  ||  (dev->flags & IFF_ALLMULTI)) {
1007                 outw(SetRxFilter|RxStation|RxMulticast|RxBroadcast, ioaddr + EL3_CMD);
1008                 if (vortex_debug > 3) {
1009                         printk("%s: Setting Rx multicast mode, %d addresses.\n",
1010                                    dev->name, dev->mc_count);
1011                 }
1012         } else if (dev->flags & IFF_PROMISC) {
1013                 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
1014                          ioaddr + EL3_CMD);
1015         } else
1016                 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1017 }
1018 
1019 
1020 #ifdef MODULE
1021 void
1022 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1023 {
1024         struct device *next_dev;
1025 
1026         /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1027         while (root_vortex_dev) {
1028                 next_dev = ((struct vortex_private *)root_vortex_dev->priv)->next_module;
1029                 unregister_netdev(root_vortex_dev);
1030                 release_region(root_vortex_dev->base_addr, VORTEX_TOTAL_SIZE);
1031                 kfree(root_vortex_dev);
1032                 root_vortex_dev = next_dev;
1033         }
1034 }
1035 #endif /* MODULE */
1036 
1037 /*
1038  * Local variables:
1039  *  compile-command: "gcc -DMODULE -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c 3c59x.c -o 3c59x.o"
1040  *  c-indent-level: 4
1041  *  tab-width: 4
1042  * End:
1043  */

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