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         vortex_probe1(dev);
 417 #endif /* MODULE */
 418         return 0;
 419 }
 420 
 421 static int vortex_probe1(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 422 {
 423         int ioaddr = dev->base_addr;
 424         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 425         int i;
 426 
 427         printk("%s: 3Com %s at %#3x,", dev->name,
 428                    vp->product_name, ioaddr);
 429 
 430         /* Read the station address from the EEPROM. */
 431         EL3WINDOW(0);
 432         for (i = 0; i < 3; i++) {
 433                 short *phys_addr = (short *)dev->dev_addr;
 434                 int timer;
 435                 outw(EEPROM_Read + PhysAddr01 + i, ioaddr + Wn0EepromCmd);
 436                 /* Pause for at least 162 us. for the read to take place. */
 437                 for (timer = 0; timer < 162*4 + 400; timer++) {
 438                         SLOW_DOWN_IO;
 439                         if ((inw(ioaddr + Wn0EepromCmd) & 0x8000) == 0)
 440                                 break;
 441                 }
 442                 phys_addr[i] = htons(inw(ioaddr + 12));
 443         }
 444         for (i = 0; i < 6; i++)
 445                 printk("%c%2.2x", i ? ':' : ' ', dev->dev_addr[i]);
 446         printk(", IRQ %d\n", dev->irq);
 447         /* Tell them about an invalid IRQ. */
 448         if (vortex_debug && (dev->irq <= 0 || dev->irq > 15))
 449                 printk(" *** Warning: this IRQ is unlikely to work!\n");
 450 
 451         {
 452                 char *ram_split[] = {"5:3", "3:1", "1:1", "invalid"};
 453                 union wn3_config config;
 454                 EL3WINDOW(3);
 455                 config.i = inl(ioaddr + Wn3_Config);
 456                 if (vortex_debug > 1)
 457                         printk("  Internal config register is %4.4x, transceivers %#x.\n",
 458                                    config.i, inw(ioaddr + Wn3_Options));
 459                 printk("  %dK %s-wide RAM %s Rx:Tx split, %s%s interface.\n",
 460                            8 << config.u.ram_size,
 461                            config.u.ram_width ? "word" : "byte",
 462                            ram_split[config.u.ram_split],
 463                            config.u.autoselect ? "autoselect/" : "",
 464                            if_names[config.u.xcvr]);
 465                 dev->if_port = config.u.xcvr;
 466                 vp->autoselect = config.u.autoselect;
 467         }
 468 
 469         /* We do a request_region() only to register /proc/ioports info. */
 470         request_region(ioaddr, VORTEX_TOTAL_SIZE, vp->product_name);
 471 
 472         /* The 3c59x-specific entries in the device structure. */
 473         dev->open = &vortex_open;
 474         dev->hard_start_xmit = &vortex_start_xmit;
 475         dev->stop = &vortex_close;
 476         dev->get_stats = &vortex_get_stats;
 477         dev->set_multicast_list = &set_multicast_list;
 478 #if defined (HAVE_SET_MAC_ADDR) && 0
 479         dev->set_mac_address = &set_mac_address;
 480 #endif
 481 
 482         return 0;
 483 }
 484 
 485 
 486 static int
 487 vortex_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 488 {
 489         int ioaddr = dev->base_addr;
 490         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 491         union wn3_config config;
 492         int i;
 493 
 494         /* Before initializing select the active media port. */
 495         EL3WINDOW(3);
 496         if (vp->full_duplex)
 497                 outb(0x20, ioaddr + Wn3_MAC_Ctrl); /* Set the full-duplex bit. */
 498         config.i = inl(ioaddr + Wn3_Config);
 499 
 500         if (vp->media_override != 7) {
 501                 if (vortex_debug > 1)
 502                         printk("%s: Media override to transceiver %d (%s).\n",
 503                                    dev->name, vp->media_override, if_names[vp->media_override]);
 504                 config.u.xcvr = vp->media_override;
 505                 dev->if_port = vp->media_override;
 506                 outl(config.i, ioaddr + Wn3_Config);
 507         }
 508 
 509         if (vortex_debug > 1) {
 510                 printk("%s: vortex_open() InternalConfig %8.8x.\n",
 511                         dev->name, config.i);
 512         }
 513 
 514         outw(TxReset, ioaddr + EL3_CMD);
 515         for (i = 20; i >= 0 ; i--)
 516                 if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 517                         break;
 518 
 519         outw(RxReset, ioaddr + EL3_CMD);
 520         /* Wait a few ticks for the RxReset command to complete. */
 521         for (i = 20; i >= 0 ; i--)
 522                 if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 523                         break;
 524 
 525         outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
 526 
 527         if (irq2dev_map[dev->irq] != NULL
 528                 || (irq2dev_map[dev->irq] = dev) == NULL
 529                 || dev->irq == 0
 530                 || request_irq(dev->irq, &vortex_interrupt, 0, vp->product_name)) {
 531                 return -EAGAIN;
 532         }
 533 
 534         if (vortex_debug > 1) {
 535                 EL3WINDOW(4);
 536                 printk("%s: vortex_open() irq %d media status %4.4x.\n",
 537                            dev->name, dev->irq, inw(ioaddr + Wn4_Media));
 538         }
 539 
 540         /* Set the station address and mask in window 2 each time opened. */
 541         EL3WINDOW(2);
 542         for (i = 0; i < 6; i++)
 543                 outb(dev->dev_addr[i], ioaddr + i);
 544         for (; i < 12; i+=2)
 545                 outw(0, ioaddr + i);
 546 
 547         if (dev->if_port == 3)
 548                 /* Start the thinnet transceiver. We should really wait 50ms...*/
 549                 outw(StartCoax, ioaddr + EL3_CMD);
 550         else if (dev->if_port == 0) {
 551                 /* 10baseT interface, enabled link beat and jabber check. */
 552                 EL3WINDOW(4);
 553                 outw(inw(ioaddr + Wn4_Media) | Media_TP, ioaddr + Wn4_Media);
 554         }
 555 
 556         /* Switch to the stats window, and clear all stats by reading. */
 557         outw(StatsDisable, ioaddr + EL3_CMD);
 558         EL3WINDOW(6);
 559         for (i = 0; i < 10; i++)        
 560                 inb(ioaddr + i);
 561         inw(ioaddr + 10);
 562         inw(ioaddr + 12);
 563         /* New: On the Vortex we must also clear the BadSSD counter. */
 564         EL3WINDOW(3);
 565         inb(ioaddr + 12);
 566 
 567         /* Switch to register set 7 for normal use. */
 568         EL3WINDOW(7);
 569 
 570         /* Accept b-case and phys addr only. */
 571         outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
 572         outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
 573 
 574         dev->tbusy = 0;
 575         dev->interrupt = 0;
 576         dev->start = 1;
 577 
 578         outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
 579         outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
 580         /* Allow status bits to be seen. */
 581         outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
 582         /* Ack all pending events, and set active indicator mask. */
 583         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
 584                  ioaddr + EL3_CMD);
 585         outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
 586                  | DMADone, ioaddr + EL3_CMD);
 587 
 588 #ifdef MODULE
 589         MOD_INC_USE_COUNT;
 590 #endif
 591 
 592         if (vp->autoselect) {
 593                 init_timer(&vp->timer);
 594                 vp->timer.expires = (14*HZ)/10;                         /* 1.4 sec. */
 595                 vp->timer.data = (unsigned long)dev;
 596                 vp->timer.function = &vortex_timer;    /* timer handler */
 597                 add_timer(&vp->timer);
 598         }
 599         return 0;
 600 }
 601 
 602 static void vortex_timer(unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
 603 {
 604         struct device *dev = (struct device *)data;
 605         if (vortex_debug > 2)
 606                 printk("%s: Media selection timer tick happened.\n", dev->name);
 607         /* ToDo: active media selection here! */
 608 }
 609 
 610 static int
 611 vortex_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 612 {
 613         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 614         int ioaddr = dev->base_addr;
 615 
 616         /* Transmitter timeout, serious problems. */
 617         if (dev->tbusy) {
 618                 int tickssofar = jiffies - dev->trans_start;
 619                 if (tickssofar < 40)
 620                         return 1;
 621                 printk("%s: transmit timed out, tx_status %2.2x status %4.4x.\n",
 622                            dev->name, inb(ioaddr + TxStatus), inw(ioaddr + EL3_STATUS));
 623                 vp->stats.tx_errors++;
 624                 /* Issue TX_RESET and TX_START commands. */
 625                 outw(TxReset, ioaddr + EL3_CMD);
 626                 {
 627                         int i;
 628                         for (i = 20; i >= 0 ; i--)
 629                                 if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 630                                         break;
 631                 }
 632                 outw(TxEnable, ioaddr + EL3_CMD);
 633                 dev->trans_start = jiffies;
 634                 dev->tbusy = 0;
 635                 return 0;
 636         }
 637 
 638         if (skb == NULL || skb->len <= 0) {
 639                 printk("%s: Obsolete driver layer request made: skbuff==NULL.\n",
 640                            dev->name);
 641                 dev_tint(dev);
 642                 return 0;
 643         }
 644 
 645         /* Block a timer-based transmit from overlapping.  This could better be
 646            done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
 647            If this ever occurs the queue layer is doing something evil! */
 648         if (set_bit(0, (void*)&dev->tbusy) != 0) {
 649                 printk("%s: Transmitter access conflict.\n", dev->name);
 650                 return 1;
 651         }
 652 
 653         /* Put out the doubleword header... */
 654         outl(skb->len, ioaddr + TX_FIFO);
 655 #ifdef VORTEX_BUS_MASTER
 656         if (vp->bus_master) {
 657                 /* Set the bus-master controller to transfer the packet. */
 658                 outl((int)(skb->data), ioaddr + Wn7_MasterAddr);
 659                 outw((skb->len + 3) & ~3, ioaddr + Wn7_MasterLen);
 660                 vp->tx_skb = skb;
 661                 outw(StartDMADown, ioaddr + EL3_CMD);
 662         } else {
 663                 /* ... and the packet rounded to a doubleword. */
 664                 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
 665                 dev_kfree_skb (skb, FREE_WRITE);
 666                 if (inw(ioaddr + TxFree) > 1536) {
 667                         dev->tbusy = 0;
 668                 } else
 669                         /* Interrupt us when the FIFO has room for max-sized packet. */
 670                         outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
 671         }
 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 #endif  /* bus master */
 682 
 683         dev->trans_start = jiffies;
 684 
 685         /* Clear the Tx status stack. */
 686         {
 687                 short tx_status;
 688                 int i = 4;
 689 
 690                 while (--i > 0  &&      (tx_status = inb(ioaddr + TxStatus)) > 0) {
 691                         if (tx_status & 0x3C) {         /* A Tx-disabling error occured.  */
 692                                 if (vortex_debug > 2)
 693                                   printk("%s: Tx error, status %2.2x.\n",
 694                                                  dev->name, tx_status);
 695                                 if (tx_status & 0x04) vp->stats.tx_fifo_errors++;
 696                                 if (tx_status & 0x38) vp->stats.tx_aborted_errors++;
 697                                 if (tx_status & 0x30) {
 698                                         int j;
 699                                         outw(TxReset, ioaddr + EL3_CMD);
 700                                         for (j = 20; j >= 0 ; j--)
 701                                                 if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 702                                                         break;
 703                                 }
 704                                 outw(TxEnable, ioaddr + EL3_CMD);
 705                         }
 706                         outb(0x00, ioaddr + TxStatus); /* Pop the status stack. */
 707                 }
 708         }
 709         return 0;
 710 }
 711 
 712 /* The interrupt handler does all of the Rx thread work and cleans up
 713    after the Tx thread. */
 714 static void vortex_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 715 {
 716         struct device *dev = (struct device *)(irq2dev_map[irq]);
 717         struct vortex_private *lp;
 718         int ioaddr, status;
 719         int latency;
 720         int i = 0;
 721 
 722         if (dev == NULL) {
 723                 printk ("vortex_interrupt(): irq %d for unknown device.\n", irq);
 724                 return;
 725         }
 726 
 727         if (dev->interrupt)
 728                 printk("%s: Re-entering the interrupt handler.\n", dev->name);
 729         dev->interrupt = 1;
 730 
 731         ioaddr = dev->base_addr;
 732         latency = inb(ioaddr + Timer);
 733         lp = (struct vortex_private *)dev->priv;
 734 
 735         status = inw(ioaddr + EL3_STATUS);
 736 
 737         if (vortex_debug > 4)
 738                 printk("%s: interrupt, status %4.4x, timer %d.\n", dev->name,
 739                            status, latency);
 740         if ((status & 0xE000) != 0xE000) {
 741                 static int donedidthis=0;
 742                 /* Some interrupt controllers store a bogus interrupt from boot-time.
 743                    Ignore a single early interrupt, but don't hang the machine for
 744                    other interrupt problems. */
 745                 if (donedidthis++ > 1) {
 746                         printk("%s: Bogus interrupt, bailing. Status %4.4x, start=%d.\n",
 747                                    dev->name, status, dev->start);
 748                         free_irq(dev->irq);
 749                 }
 750         }
 751 
 752         do {
 753                 if (vortex_debug > 5)
 754                                 printk("%s: In interrupt loop, status %4.4x.\n",
 755                                            dev->name, status);
 756                 if (status & RxComplete)
 757                         vortex_rx(dev);
 758 
 759                 if (status & TxAvailable) {
 760                         if (vortex_debug > 5)
 761                                 printk("        TX room bit was handled.\n");
 762                         /* There's room in the FIFO for a full-sized packet. */
 763                         outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
 764                         dev->tbusy = 0;
 765                         mark_bh(NET_BH);
 766                 }
 767 #ifdef VORTEX_BUS_MASTER
 768                 if (status & DMADone) {
 769                         outw(0x1000, ioaddr + Wn7_MasterStatus); /* Ack the event. */
 770                         dev->tbusy = 0;
 771                         mark_bh(NET_BH);
 772                 }
 773 #endif
 774                 if (status & (AdapterFailure | RxEarly | StatsFull)) {
 775                         /* Handle all uncommon interrupts at once. */
 776                         if (status & RxEarly) {                         /* Rx early is unused. */
 777                                 vortex_rx(dev);
 778                                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
 779                         }
 780                         if (status & StatsFull) {       /* Empty statistics. */
 781                                 static int DoneDidThat = 0;
 782                                 if (vortex_debug > 4)
 783                                         printk("%s: Updating stats.\n", dev->name);
 784                                 update_stats(ioaddr, dev);
 785                                 /* DEBUG HACK: Disable statistics as an interrupt source. */
 786                                 /* This occurs when we have the wrong media type! */
 787                                 if (DoneDidThat == 0  &&
 788                                         inw(ioaddr + EL3_STATUS) & StatsFull) {
 789                                         int win, reg;
 790                                         printk("%s: Updating stats failed, disabling stats as an"
 791                                                    " interrupt source.\n", dev->name);
 792                                         for (win = 0; win < 8; win++) {
 793                                                 EL3WINDOW(win);
 794                                                 printk("\n Vortex window %d:", win);
 795                                                 for (reg = 0; reg < 16; reg++)
 796                                                         printk(" %2.2x", inb(ioaddr+reg));
 797                                         }
 798                                         EL3WINDOW(7);
 799                                         outw(SetIntrEnb | 0x18, ioaddr + EL3_CMD);
 800                                         DoneDidThat++;
 801                                 }
 802                         }
 803                         if (status & AdapterFailure) {
 804                                 /* Adapter failure requires Rx reset and reinit. */
 805                                 outw(RxReset, ioaddr + EL3_CMD);
 806                                 /* Set the Rx filter to the current state. */
 807                                 outw(SetRxFilter | RxStation | RxBroadcast
 808                                          | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
 809                                          | (dev->flags & IFF_PROMISC ? RxProm : 0),
 810                                          ioaddr + EL3_CMD);
 811                                 outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
 812                                 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
 813                         }
 814                 }
 815 
 816                 if (++i > 10) {
 817                         printk("%s: Infinite loop in interrupt, status %4.4x.  "
 818                                    "Disabling functions (%4.4x).\n",
 819                                    dev->name, status, SetStatusEnb | ((~status) & 0xFE));
 820                         /* Disable all pending interrupts. */
 821                         outw(SetStatusEnb | ((~status) & 0xFE), ioaddr + EL3_CMD);
 822                         outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
 823                         break;
 824                 }
 825                 /* Acknowledge the IRQ. */
 826                 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
 827 
 828         } while ((status = inw(ioaddr + EL3_STATUS)) & (IntLatch | RxComplete));
 829 
 830         if (vortex_debug > 4)
 831                 printk("%s: exiting interrupt, status %4.4x.\n", dev->name, status);
 832 
 833         dev->interrupt = 0;
 834         return;
 835 }
 836 
 837 static int
 838 vortex_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 839 {
 840         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 841         int ioaddr = dev->base_addr;
 842         int i;
 843         short rx_status;
 844 
 845         if (vortex_debug > 5)
 846                 printk("   In rx_packet(), status %4.4x, rx_status %4.4x.\n",
 847                            inw(ioaddr+EL3_STATUS), inw(ioaddr+RxStatus));
 848         while ((rx_status = inw(ioaddr + RxStatus)) > 0) {
 849                 if (rx_status & 0x4000) { /* Error, update stats. */
 850                         unsigned char rx_error = inb(ioaddr + RxErrors);
 851                         if (vortex_debug > 4)
 852                                 printk(" Rx error: status %2.2x.\n", rx_error);
 853                         vp->stats.rx_errors++;
 854                         if (rx_error & 0x01)  vp->stats.rx_over_errors++;
 855                         if (rx_error & 0x02)  vp->stats.rx_length_errors++;
 856                         if (rx_error & 0x04)  vp->stats.rx_frame_errors++;
 857                         if (rx_error & 0x08)  vp->stats.rx_crc_errors++;
 858                         if (rx_error & 0x10)  vp->stats.rx_length_errors++;
 859                 } else {
 860                         /* The packet length: up to 4.5K!. */
 861                         short pkt_len = rx_status & 0x1fff;
 862                         struct sk_buff *skb;
 863 
 864                         skb = dev_alloc_skb(pkt_len + 5);
 865                         if (vortex_debug > 4)
 866                                 printk("Receiving packet size %d status %4.4x.\n",
 867                                            pkt_len, rx_status);
 868                         if (skb != NULL) {
 869                                 skb->dev = dev;
 870                                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
 871                                 /* 'skb_put()' points to the start of sk_buff data area. */
 872                                 insl(ioaddr + RX_FIFO, skb_put(skb, pkt_len),
 873                                          (pkt_len + 3) >> 2);
 874                                 skb->protocol = eth_type_trans(skb, dev);
 875                                 netif_rx(skb);
 876                                 outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
 877                                 /* Wait a limited time to go to next packet. */
 878                                 for (i = 200; i >= 0; i--)
 879                                         if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 880                                                 break;
 881                                 vp->stats.rx_packets++;
 882                                 continue;
 883                         } else if (vortex_debug)
 884                                 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
 885                                            dev->name, pkt_len);
 886                 }
 887                 vp->stats.rx_dropped++;
 888                 outw(RxDiscard, ioaddr + EL3_CMD);
 889                 /* Wait a limited time to skip this packet. */
 890                 for (i = 200; i >= 0; i--)
 891                         if ( ! inw(ioaddr + EL3_STATUS) & CmdInProgress)
 892                                 break;
 893         }
 894 
 895         return 0;
 896 }
 897 
 898 static int
 899 vortex_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 900 {
 901         int ioaddr = dev->base_addr;
 902 
 903         dev->start = 0;
 904         dev->tbusy = 1;
 905 
 906         if (vortex_debug > 1)
 907                 printk("%s: vortex_close() status %4.4x, Tx status %2.2x.\n",
 908                            dev->name, inw(ioaddr + EL3_STATUS), inb(ioaddr + TxStatus));
 909 
 910         /* Turn off statistics ASAP.  We update lp->stats below. */
 911         outw(StatsDisable, ioaddr + EL3_CMD);
 912 
 913         /* Disable the receiver and transmitter. */
 914         outw(RxDisable, ioaddr + EL3_CMD);
 915         outw(TxDisable, ioaddr + EL3_CMD);
 916 
 917         if (dev->if_port == 3)
 918                 /* Turn off thinnet power.  Green! */
 919                 outw(StopCoax, ioaddr + EL3_CMD);
 920         else if (dev->if_port == 0) {
 921                 /* Disable link beat and jabber, if_port may change ere next open(). */
 922                 EL3WINDOW(4);
 923                 outw(inw(ioaddr + Wn4_Media) & ~Media_TP, ioaddr + Wn4_Media);
 924         }
 925 
 926         free_irq(dev->irq);
 927         /* Mmmm, we should diable all interrupt sources here. */
 928         irq2dev_map[dev->irq] = 0;
 929 
 930         update_stats(ioaddr, dev);
 931 #ifdef MODULE
 932         MOD_DEC_USE_COUNT;
 933 #endif
 934 
 935         return 0;
 936 }
 937 
 938 static struct enet_statistics *
 939 vortex_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 940 {
 941         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 942         unsigned long flags;
 943 
 944         save_flags(flags);
 945         cli();
 946         update_stats(dev->base_addr, dev);
 947         restore_flags(flags);
 948         return &vp->stats;
 949 }
 950 
 951 /*  Update statistics.
 952         Unlike with the EL3 we need not worry about interrupts changing
 953         the window setting from underneath us, but we must still guard
 954         against a race condition with a StatsUpdate interrupt updating the
 955         table.  This is done by checking that the ASM (!) code generated uses
 956         atomic updates with '+='.
 957         */
 958 static void update_stats(int ioaddr, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 959 {
 960         struct vortex_private *vp = (struct vortex_private *)dev->priv;
 961 
 962         /* Unlike the 3c5x9 we need not turn off stats updates while reading. */
 963         /* Switch to the stats window, and read everything. */
 964         EL3WINDOW(6);
 965         vp->stats.tx_carrier_errors             += inb(ioaddr + 0);
 966         vp->stats.tx_heartbeat_errors   += inb(ioaddr + 1);
 967         /* Multiple collisions. */              inb(ioaddr + 2);
 968         vp->stats.collisions                    += inb(ioaddr + 3);
 969         vp->stats.tx_window_errors              += inb(ioaddr + 4);
 970         vp->stats.rx_fifo_errors                += inb(ioaddr + 5);
 971         vp->stats.tx_packets                    += inb(ioaddr + 6);
 972         vp->stats.tx_packets                    += (inb(ioaddr + 9)&15) << 8;
 973         /* Rx packets   */                              inb(ioaddr + 7);   /* Must read to clear */
 974         /* Tx deferrals */                              inb(ioaddr + 8);
 975         /* Don't bother with register 9, an extention of registers 6&7.
 976            If we do use the 6&7 values the atomic update assumption above
 977            is invalid. */
 978         inw(ioaddr + 10);       /* Total Rx and Tx octets. */
 979         inw(ioaddr + 12);
 980         /* New: On the Vortex we must also clear the BadSSD counter. */
 981         EL3WINDOW(3);
 982         inb(ioaddr + 12);
 983 
 984         /* We change back to window 7 (not 1) with the Vortex. */
 985         EL3WINDOW(7);
 986         return;
 987 }
 988 
 989 /* There are two version of set_multicast_list() to support both v1.2 and
 990    v1.4 kernels. */
 991 static void
 992 set_multicast_list(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 993 {
 994         short ioaddr = dev->base_addr;
 995 
 996         if ((dev->mc_list)  ||  (dev->flags & IFF_ALLMULTI)) {
 997                 outw(SetRxFilter|RxStation|RxMulticast|RxBroadcast, ioaddr + EL3_CMD);
 998                 if (vortex_debug > 3) {
 999                         printk("%s: Setting Rx multicast mode, %d addresses.\n",
1000                                    dev->name, dev->mc_count);
1001                 }
1002         } else if (dev->flags & IFF_PROMISC) {
1003                 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
1004                          ioaddr + EL3_CMD);
1005         } else
1006                 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1007 }
1008 
1009 
1010 #ifdef MODULE
1011 void
1012 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1013 {
1014         struct device *next_dev;
1015 
1016         /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1017         while (root_vortex_dev) {
1018                 next_dev = ((struct vortex_private *)root_vortex_dev->priv)->next_module;
1019                 unregister_netdev(root_vortex_dev);
1020                 release_region(root_vortex_dev->base_addr, VORTEX_TOTAL_SIZE);
1021                 kfree(root_vortex_dev);
1022                 root_vortex_dev = next_dev;
1023         }
1024 }
1025 #endif /* MODULE */
1026 
1027 /*
1028  * Local variables:
1029  *  compile-command: "gcc -DMODULE -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c 3c59x.c -o 3c59x.o"
1030  *  c-indent-level: 4
1031  *  tab-width: 4
1032  * End:
1033  */

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