Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

epic100.c

Go to the documentation of this file.
00001 /* epic100.c: A SMC 83c170 EPIC/100 Fast Ethernet driver for Linux. */
00002 /*
00003         Written/copyright 1997-1999 by Donald Becker.
00004 
00005         This software may be used and distributed according to the terms
00006         of the GNU Public License, incorporated herein by reference.
00007         All other rights reserved.
00008 
00009         This driver is for the SMC83c170/175 "EPIC" series, as used on the
00010         SMC EtherPower II 9432 PCI adapter, and several CardBus cards.
00011 
00012         The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
00013         USRA Center of Excellence in Space Data and Information Sciences
00014            Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
00015 
00016         Information and updates available at
00017         http://cesdis.gsfc.nasa.gov/linux/drivers/epic100.html
00018 */
00019 
00020 static const char *version =
00021 "epic100.c:v1.07h 8/18/99 Donald Becker http://cesdis.gsfc.nasa.gov/linux/drivers/epic100.html\n";
00022 
00023 /* User-tunable values. */
00024 
00025 static int debug = 1;
00026 #define epic_debug debug
00027 
00028 /* Used to pass the full-duplex flag, etc. */
00029 #define MAX_UNITS 8
00030 static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
00031 static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
00032 
00033 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
00034    Setting to > 1518 effectively disables this feature. */
00035 static int rx_copybreak = 200;
00036 
00037 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
00038 static int max_interrupt_work = 32;
00039 
00040 /* Operational parameters that usually are not changed. */
00041 /* Keep the ring sizes a power of two for efficiency.
00042    Making the Tx ring too large decreases the effectiveness of channel
00043    bonding and packet priority.
00044    There are no ill effects from too-large receive rings. */
00045 #define TX_RING_SIZE    16
00046 #define RX_RING_SIZE    32
00047 
00048 /* Time in jiffies before concluding the transmitter is hung. */
00049 #define TX_TIMEOUT  (2*HZ)
00050 
00051 #define PKT_BUF_SZ              1536                    /* Size of each temporary Rx buffer.*/
00052 
00053 /* Bytes transferred to chip before transmission starts. */
00054 /* Initial threshold, increased on underflow, rounded down to 4 byte units. */
00055 #define TX_FIFO_THRESH 256
00056 #define RX_FIFO_THRESH 1                /* 0-3, 0==32, 64,96, or 3==128 bytes  */
00057 
00058 #include <linux/config.h>
00059 #include <linux/version.h>              /* Evil, but neccessary */
00060 #include <linux/module.h>
00061 
00062 #include <linux/kernel.h>
00063 #include <linux/string.h>
00064 #include <linux/timer.h>
00065 #include <linux/errno.h>
00066 #include <linux/ioport.h>
00067 #include <linux/malloc.h>
00068 #include <linux/interrupt.h>
00069 #include <linux/pci.h>
00070 #include <linux/delay.h>
00071 
00072 #include <asm/processor.h>              /* Processor type for cache alignment. */
00073 #include <asm/bitops.h>
00074 #include <asm/io.h>
00075 #include <asm/spinlock.h>
00076 
00077 #include <linux/netdevice.h>
00078 #include <linux/etherdevice.h>
00079 #include <linux/skbuff.h>
00080 
00081 /* Kernel compatibility defines, common to David Hind's PCMCIA package.
00082    This is only in the support-all-kernels source code. */
00083 
00084 #if ! defined (LINUX_VERSION_CODE) || LINUX_VERSION_CODE < 0x20000
00085 #warning This driver version is only for kernel versions 2.0.0 and later.
00086 #endif
00087 
00088 #define RUN_AT(x) (jiffies + (x))
00089 
00090 MODULE_AUTHOR("Donald Becker <becker@cesdis.gsfc.nasa.gov>");
00091 MODULE_DESCRIPTION("SMC 83c170 EPIC series Ethernet driver");
00092 MODULE_PARM(debug, "i");
00093 MODULE_PARM(options, "1-" __MODULE_STRING(8) "i");
00094 MODULE_PARM(full_duplex, "1-" __MODULE_STRING(8) "i");
00095 MODULE_PARM(rx_copybreak, "i");
00096 MODULE_PARM(max_interrupt_work, "i");
00097 
00098 static char kernel_version[] = UTS_RELEASE;
00099 
00100 /* The I/O extent. */
00101 #define EPIC_TOTAL_SIZE 0x100
00102 
00103 /*
00104                                 Theory of Operation
00105 
00106 I. Board Compatibility
00107 
00108 This device driver is designed for the SMC "EPIC/100", the SMC
00109 single-chip Ethernet controllers for PCI.  This chip is used on
00110 the SMC EtherPower II boards.
00111 
00112 II. Board-specific settings
00113 
00114 PCI bus devices are configured by the system at boot time, so no jumpers
00115 need to be set on the board.  The system BIOS will assign the
00116 PCI INTA signal to a (preferably otherwise unused) system IRQ line.
00117 Note: Kernel versions earlier than 1.3.73 do not support shared PCI
00118 interrupt lines.
00119 
00120 III. Driver operation
00121 
00122 IIIa. Ring buffers
00123 
00124 IVb. References
00125 
00126 http://www.smsc.com/main/datasheets/83c171.pdf
00127 http://www.smsc.com/main/datasheets/83c175.pdf
00128 http://cesdis.gsfc.nasa.gov/linux/misc/NWay.html
00129 http://www.national.com/pf/DP/DP83840A.html
00130 
00131 IVc. Errata
00132 
00133 */
00134 
00135 /* The rest of these values should never change. */
00136 
00137 static struct device *epic_probe1(int pci_bus, int pci_devfn,
00138 
00139                                                                   struct device *dev, long ioaddr, int irq,
00140                                                                   int chip_id, int card_idx);
00141                                                                   
00142 /* For 2.2 resource management we are kind of stuck with this */
00143 #define USE_IO
00144 
00145                                                                   
00146 #ifdef USE_IO
00147 #define EPIC_USE_IO
00148 #define EPIC_IOTYPE PCI_USES_MASTER|PCI_USES_IO|PCI_ADDR0
00149 #else
00150 #define EPIC_IOTYPE PCI_USES_MASTER|PCI_USES_MEM|PCI_ADDR1
00151 #endif
00152 
00153 enum pci_flags_bit {
00154         PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
00155         PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
00156 };
00157 
00158 struct chip_info {
00159         const char *name;
00160         u16     vendor_id, device_id, device_id_mask, pci_flags;
00161         int io_size, min_latency;
00162         struct device *(*probe1)(int pci_bus, int pci_devfn, struct device *dev,
00163                                                          long ioaddr, int irq, int chip_idx, int fnd_cnt);
00164 } static chip_tbl[] = {
00165         {"SMSC EPIC/100 83c170", 0x10B8, 0x0005, 0x7fff,
00166          EPIC_IOTYPE, EPIC_TOTAL_SIZE, 32, epic_probe1},
00167         {"SMSC EPIC/C 83c175", 0x10B8, 0x0006, 0x7fff,
00168          EPIC_IOTYPE, EPIC_TOTAL_SIZE, 32, epic_probe1},
00169         {0,},
00170 };
00171 
00172 /* This matches the table above. */
00173 static enum { MII_PWRDWN=1, TYPE2_INTR=2 } chip_features[] ={
00174         TYPE2_INTR, TYPE2_INTR | MII_PWRDWN, };
00175 
00176 #ifndef USE_IO
00177 #undef inb
00178 #undef inw
00179 #undef inl
00180 #undef outb
00181 #undef outw
00182 #undef outl
00183 #define inb readb
00184 #define inw readw
00185 #define inl readl
00186 #define outb writeb
00187 #define outw writew
00188 #define outl writel
00189 #endif
00190 
00191 /* Offsets to registers, using the (ugh) SMC names. */
00192 enum epic_registers {
00193   COMMAND=0, INTSTAT=4, INTMASK=8, GENCTL=0x0C, NVCTL=0x10, EECTL=0x14,
00194   PCIBurstCnt=0x18,
00195   TEST1=0x1C, CRCCNT=0x20, ALICNT=0x24, MPCNT=0x28,     /* Rx error counters. */
00196   MIICtrl=0x30, MIIData=0x34, MIICfg=0x38,
00197   LAN0=64,                                              /* MAC address. */
00198   MC0=80,                                               /* Multicast filter table. */
00199   RxCtrl=96, TxCtrl=112, TxSTAT=0x74,
00200   PRxCDAR=0x84, RxSTAT=0xA4, EarlyRx=0xB0, PTxCDAR=0xC4, TxThresh=0xDC,
00201 };
00202 
00203 /* Interrupt register bits, using my own meaningful names. */
00204 enum IntrStatus {
00205         TxIdle=0x40000, RxIdle=0x20000, IntrSummary=0x010000,
00206         PCIBusErr170=0x7000, PCIBusErr175=0x1000, PhyEvent175=0x8000,
00207         RxStarted=0x0800, RxEarlyWarn=0x0400, CntFull=0x0200, TxUnderrun=0x0100,
00208         TxEmpty=0x0080, TxDone=0x0020, RxError=0x0010,
00209         RxOverflow=0x0008, RxFull=0x0004, RxHeader=0x0002, RxDone=0x0001,
00210 };
00211 enum CommandBits {
00212         StopRx=1, StartRx=2, TxQueued=4, RxQueued=8,
00213         StopTxDMA=0x20, StopRxDMA=0x40, RestartTx=0x80,
00214 };
00215 
00216 static u16 media2miictl[16] = {
00217         0, 0x0C00, 0x0C00, 0x2000,  0x0100, 0x2100, 0, 0,
00218         0, 0, 0, 0,  0, 0, 0, 0 };
00219 
00220 /* The EPIC100 Rx and Tx buffer descriptors. */
00221 
00222 struct epic_tx_desc {
00223         s16 status;
00224         u16 txlength;
00225         u32 bufaddr;
00226         u16 buflength;
00227         u16 control;
00228         u32 next;
00229 };
00230 
00231 struct epic_rx_desc {
00232         s16 status;
00233         u16 rxlength;
00234         u32 bufaddr;
00235         u32 buflength;
00236         u32 next;
00237 };
00238 
00239 struct epic_private {
00240         char devname[8];                        /* Used only for kernel debugging. */
00241         const char *product_name;
00242         struct device *next_module;
00243 
00244         /* Tx and Rx rings here so that they remain paragraph aligned. */
00245         struct epic_rx_desc rx_ring[RX_RING_SIZE];
00246         struct epic_tx_desc tx_ring[TX_RING_SIZE];
00247         /* The saved address of a sent-in-place packet/buffer, for skfree(). */
00248         struct sk_buff* tx_skbuff[TX_RING_SIZE];
00249         /* The addresses of receive-in-place skbuffs. */
00250         struct sk_buff* rx_skbuff[RX_RING_SIZE];
00251 
00252         /* Ring pointers. */
00253         spinlock_t lock;                                /* Group with Tx control cache line. */
00254         unsigned int cur_tx, dirty_tx;
00255         struct descriptor  *last_tx_desc;
00256         unsigned int cur_rx, dirty_rx;
00257         struct descriptor  *last_rx_desc;
00258         long last_rx_time;                              /* Last Rx, in jiffies. */
00259 
00260         u8 pci_bus, pci_dev_fn;                         /* PCI bus location. */
00261         u16 chip_flags;
00262 
00263         struct net_device_stats stats;
00264         struct timer_list timer;                        /* Media selection timer. */
00265         int tx_threshold;
00266         unsigned char mc_filter[8];
00267         signed char phys[4];                            /* MII device addresses. */
00268         int mii_phy_cnt;
00269         unsigned int tx_full:1;                         /* The Tx queue is full. */
00270         unsigned int full_duplex:1;                     /* Current duplex setting. */
00271         unsigned int force_fd:1;                        /* Full-duplex operation requested. */
00272         unsigned int default_port:4;            /* Last dev->if_port value. */
00273         unsigned int media2:4;                          /* Secondary monitored media port. */
00274         unsigned int medialock:1;                       /* Don't sense media type. */
00275         unsigned int mediasense:1;                      /* Media sensing in progress. */
00276         int pad0, pad1;                                         /* Used for 8-byte alignment */
00277 };
00278 
00279 static int epic_open(struct device *dev);
00280 static int read_eeprom(long ioaddr, int location);
00281 static int mdio_read(long ioaddr, int phy_id, int location);
00282 static void mdio_write(long ioaddr, int phy_id, int location, int value);
00283 static void epic_restart(struct device *dev);
00284 static void epic_timer(unsigned long data);
00285 static void epic_tx_timeout(struct device *dev);
00286 static void epic_init_ring(struct device *dev);
00287 static int epic_start_xmit(struct sk_buff *skb, struct device *dev);
00288 static int epic_rx(struct device *dev);
00289 static void epic_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
00290 static int mii_ioctl(struct device *dev, struct ifreq *rq, int cmd);
00291 static int epic_close(struct device *dev);
00292 static struct net_device_stats *epic_get_stats(struct device *dev);
00293 static void set_rx_mode(struct device *dev);
00294 
00295 
00296 /* A list of all installed EPIC devices, for removing the driver module. */
00297 static struct device *root_epic_dev = NULL;
00298 
00299 #if !defined(CARDBUS)  && !defined(HAS_PCI_NETIF)
00300 int epic100_probe(struct device *dev)
00301 {
00302         int cards_found = 0;
00303         int chip_idx, irq, pci_flags;
00304         u16 pci_command, new_command;
00305         unsigned char pci_bus, pci_device_fn;
00306         long ioaddr;
00307 
00308         struct pci_dev *pcidev = NULL;
00309         while ((pcidev = pci_find_class(PCI_CLASS_NETWORK_ETHERNET << 8, pcidev))
00310                         != NULL) {
00311                 long pciaddr;
00312                 int vendor = pcidev->vendor;
00313                 int device = pcidev->device;
00314 
00315                 for (chip_idx = 0; chip_tbl[chip_idx].vendor_id; chip_idx++)
00316                         if (vendor == chip_tbl[chip_idx].vendor_id
00317                                 && (device & chip_tbl[chip_idx].device_id_mask) ==
00318                                 chip_tbl[chip_idx].device_id)
00319                                 break;
00320                 if (chip_tbl[chip_idx].vendor_id == 0)          /* Compiled out! */
00321                         continue;
00322 
00323                 pci_flags = chip_tbl[chip_idx].pci_flags;
00324                 pciaddr = pcidev->base_address[pci_flags & PCI_ADDR0 ? 0 : 1];
00325                 pci_bus = pcidev->bus->number;
00326                 pci_device_fn = pcidev->devfn;
00327                 irq = pcidev->irq;
00328 
00329                 if ((pciaddr & PCI_BASE_ADDRESS_SPACE_IO)) {
00330                         ioaddr = pciaddr & PCI_BASE_ADDRESS_IO_MASK;
00331                         if (check_region(ioaddr, chip_tbl[chip_idx].io_size))
00332                                 continue;
00333                 } else if ((ioaddr = (long)ioremap(pciaddr & PCI_BASE_ADDRESS_MEM_MASK,
00334                                                                                    chip_tbl[chip_idx].io_size)) == 0) {
00335                         printk(KERN_INFO "Failed to map PCI address %#x.\n",
00336                                    (int)pciaddr);
00337                         continue;
00338                 }
00339 
00340                 /* EPIC-specific code: Soft-reset the chip ere setting as master. */
00341                 outl(0x0001, ioaddr + GENCTL);
00342 
00343                 /* Activate the card: fix for brain-damaged Win98 BIOSes. */
00344                 pcibios_read_config_word(pci_bus, pci_device_fn,
00345                                                                  PCI_COMMAND, &pci_command);
00346                 new_command = pci_command | (pci_flags & 7);
00347                 if (pci_command != new_command) {
00348                         printk(KERN_INFO "  The PCI BIOS has not enabled Ethernet"
00349                                    " device %4.4x-%4.4x."
00350                                    "  Updating PCI command %4.4x->%4.4x.\n",
00351                                    vendor, device, pci_command, new_command);
00352                         pcibios_write_config_word(pci_bus, pci_device_fn,
00353                                                                           PCI_COMMAND, new_command);
00354                 }
00355 
00356                 dev = chip_tbl[chip_idx].probe1(pci_bus, pci_device_fn, dev, ioaddr,
00357                                                                                 irq, chip_idx, cards_found);
00358                 /* Check the latency timer. */
00359                 if (dev) {
00360                         u8 pci_latency;
00361                         pcibios_read_config_byte(pci_bus, pci_device_fn,
00362                                                                          PCI_LATENCY_TIMER, &pci_latency);
00363                         if (pci_latency < chip_tbl[chip_idx].min_latency) {
00364                                 printk(KERN_INFO "  PCI latency timer (CFLT) value of %d is "
00365                                            "unreasonably low, setting to %d.\n", pci_latency,
00366                                            chip_tbl[chip_idx].min_latency);
00367                                 pcibios_write_config_byte(pci_bus, pci_device_fn,
00368                                                                                   PCI_LATENCY_TIMER,
00369                                                                                   chip_tbl[chip_idx].min_latency);
00370                         }
00371                         dev = 0;
00372                         cards_found++;
00373                 }
00374         }
00375 
00376         return cards_found ? 0 : -ENODEV;
00377 }
00378 #endif  /* not CARDBUS */
00379 
00380 static struct device *epic_probe1(int pci_bus, int pci_devfn,
00381                                                                   struct device *dev, long ioaddr, int irq,
00382                                                                   int chip_idx, int card_idx)
00383 {
00384         struct epic_private *ep;
00385         int i, option = 0, duplex = 0;
00386 
00387         if (dev && dev->mem_start) {
00388                 option = dev->mem_start;
00389                 duplex = (dev->mem_start & 16) ? 1 : 0;
00390         } else if (card_idx >= 0  &&  card_idx < MAX_UNITS) {
00391                 if (options[card_idx] >= 0)
00392                         option = options[card_idx];
00393                 if (full_duplex[card_idx] >= 0)
00394                         duplex = full_duplex[card_idx];
00395         }
00396 
00397         dev = init_etherdev(dev, 0);
00398 
00399         dev->base_addr = ioaddr;
00400         dev->irq = irq;
00401         printk(KERN_INFO "%s: %s at %#lx, IRQ %d, ",
00402                    dev->name, chip_tbl[chip_idx].name, ioaddr, dev->irq);
00403 
00404 #if defined(HAS_PCI_NETIF)
00405         acpi_set_pwr_state(pci_bus, pci_devfn, ACPI_D0);
00406 #endif
00407         /* Bring the chip out of low-power mode. */
00408         outl(0x4200, ioaddr + GENCTL);
00409         /* Magic?!  If we don't set this bit the MII interface won't work. */
00410         outl(0x0008, ioaddr + TEST1);
00411 
00412         /* Turn on the MII transceiver. */
00413         outl(0x12, ioaddr + MIICfg);
00414         if (chip_idx == 1)
00415                 outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
00416         outl(0x0200, ioaddr + GENCTL);
00417 
00418         /* This could also be read from the EEPROM. */
00419         for (i = 0; i < 3; i++)
00420                 ((u16 *)dev->dev_addr)[i] = le16_to_cpu(inw(ioaddr + LAN0 + i*4));
00421 
00422         for (i = 0; i < 5; i++)
00423                 printk("%2.2x:", dev->dev_addr[i]);
00424         printk("%2.2x.\n", dev->dev_addr[i]);
00425 
00426         if (epic_debug > 2) {
00427                 printk(KERN_DEBUG "%s: EEPROM contents\n", dev->name);
00428                 for (i = 0; i < 64; i++)
00429                         printk(" %4.4x%s", read_eeprom(ioaddr, i),
00430                                    i % 16 == 15 ? "\n" : "");
00431         }
00432 
00433         /* We do a request_region() to register /proc/ioports info. */
00434         request_region(ioaddr, EPIC_TOTAL_SIZE, dev->name);
00435 
00436         /* The data structures must be quadword aligned. */
00437         ep = kmalloc(sizeof(*ep), GFP_KERNEL | GFP_DMA);
00438         memset(ep, 0, sizeof(*ep));
00439         dev->priv = ep;
00440 
00441         ep->next_module = root_epic_dev;
00442         root_epic_dev = dev;
00443 
00444         ep->pci_bus = pci_bus;
00445         ep->pci_dev_fn = pci_devfn;
00446         ep->chip_flags = chip_features[chip_idx];
00447 
00448         /* Find the connected MII xcvrs.
00449            Doing this in open() would allow detecting external xcvrs later, but
00450            takes too much time. */
00451         {
00452                 int phy, phy_idx;
00453                 for (phy = 1, phy_idx = 0; phy < 32 && phy_idx < sizeof(ep->phys);
00454                          phy++) {
00455                         int mii_status = mdio_read(ioaddr, phy, 1);
00456                         if (mii_status != 0xffff  && mii_status != 0x0000) {
00457                                 ep->phys[phy_idx++] = phy;
00458                                 printk(KERN_INFO "%s: MII transceiver #%d control "
00459                                            "%4.4x status %4.4x.\n"
00460                                            KERN_INFO "%s:  Autonegotiation advertising %4.4x "
00461                                            "link partner %4.4x.\n",
00462                                            dev->name, phy, mdio_read(ioaddr, phy, 0), mii_status,
00463                                            dev->name, mdio_read(ioaddr, phy, 4),
00464                                            mdio_read(ioaddr, phy, 5));
00465                         }
00466                 }
00467                 ep->mii_phy_cnt = phy_idx;
00468                 if (phy_idx == 0) {
00469                         printk(KERN_WARNING "%s: ***WARNING***: No MII transceiver found!\n",
00470                                    dev->name);
00471                         /* Use the known PHY address of the EPII. */
00472                         ep->phys[0] = 3;
00473                 }
00474         }
00475 
00476         /* Turn off the MII xcvr (175 only!), leave the chip in low-power mode. */
00477         if (ep->chip_flags & MII_PWRDWN)
00478                 outl(inl(ioaddr + NVCTL) & ~0x483C, ioaddr + NVCTL);
00479         outl(0x0008, ioaddr + GENCTL);
00480 
00481         /* The lower four bits are the media type. */
00482         ep->force_fd = duplex;
00483         dev->if_port = ep->default_port = option;
00484         if (ep->default_port)
00485                 ep->medialock = 1;
00486 
00487         /* The Epic-specific entries in the device structure. */
00488         dev->open = &epic_open;
00489         dev->hard_start_xmit = &epic_start_xmit;
00490         dev->stop = &epic_close;
00491         dev->get_stats = &epic_get_stats;
00492         dev->set_multicast_list = &set_rx_mode;
00493         dev->do_ioctl = &mii_ioctl;
00494 
00495         return dev;
00496 }
00497 
00498 /* Serial EEPROM section. */
00499 
00500 /*  EEPROM_Ctrl bits. */
00501 #define EE_SHIFT_CLK    0x04    /* EEPROM shift clock. */
00502 #define EE_CS                   0x02    /* EEPROM chip select. */
00503 #define EE_DATA_WRITE   0x08    /* EEPROM chip data in. */
00504 #define EE_WRITE_0              0x01
00505 #define EE_WRITE_1              0x09
00506 #define EE_DATA_READ    0x10    /* EEPROM chip data out. */
00507 #define EE_ENB                  (0x0001 | EE_CS)
00508 
00509 /* Delay between EEPROM clock transitions.
00510    No extra delay is needed with 33Mhz PCI, but 66Mhz is untested.
00511  */
00512 
00513 #define eeprom_delay()  inl(ee_addr)
00514 
00515 /* The EEPROM commands include the alway-set leading bit. */
00516 #define EE_WRITE_CMD    (5 << 6)
00517 #define EE_READ64_CMD   (6 << 6)
00518 #define EE_READ256_CMD  (6 << 8)
00519 #define EE_ERASE_CMD    (7 << 6)
00520 
00521 static int read_eeprom(long ioaddr, int location)
00522 {
00523         int i;
00524         int retval = 0;
00525         long ee_addr = ioaddr + EECTL;
00526         int read_cmd = location |
00527                 (inl(ee_addr) & 0x40 ? EE_READ64_CMD : EE_READ256_CMD);
00528 
00529         outl(EE_ENB & ~EE_CS, ee_addr);
00530         outl(EE_ENB, ee_addr);
00531 
00532         /* Shift the read command bits out. */
00533         for (i = 12; i >= 0; i--) {
00534                 short dataval = (read_cmd & (1 << i)) ? EE_WRITE_1 : EE_WRITE_0;
00535                 outl(EE_ENB | dataval, ee_addr);
00536                 eeprom_delay();
00537                 outl(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
00538                 eeprom_delay();
00539         }
00540         outl(EE_ENB, ee_addr);
00541 
00542         for (i = 16; i > 0; i--) {
00543                 outl(EE_ENB | EE_SHIFT_CLK, ee_addr);
00544                 eeprom_delay();
00545                 retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
00546                 outl(EE_ENB, ee_addr);
00547                 eeprom_delay();
00548         }
00549 
00550         /* Terminate the EEPROM access. */
00551         outl(EE_ENB & ~EE_CS, ee_addr);
00552         return retval;
00553 }
00554 
00555 #define MII_READOP              1
00556 #define MII_WRITEOP             2
00557 static int mdio_read(long ioaddr, int phy_id, int location)
00558 {
00559         int i;
00560 
00561         outl((phy_id << 9) | (location << 4) | MII_READOP, ioaddr + MIICtrl);
00562         /* Typical operation takes < 50 ticks. */
00563         for (i = 4000; i > 0; i--)
00564                 if ((inl(ioaddr + MIICtrl) & MII_READOP) == 0)
00565                         return inw(ioaddr + MIIData);
00566         return 0xffff;
00567 }
00568 
00569 static void mdio_write(long ioaddr, int phy_id, int location, int value)
00570 {
00571         int i;
00572 
00573         outw(value, ioaddr + MIIData);
00574         outl((phy_id << 9) | (location << 4) | MII_WRITEOP, ioaddr + MIICtrl);
00575         for (i = 10000; i > 0; i--) {
00576                 if ((inl(ioaddr + MIICtrl) & MII_WRITEOP) == 0)
00577                         break;
00578         }
00579         return;
00580 }
00581 
00582 
00583 static int
00584 epic_open(struct device *dev)
00585 {
00586         struct epic_private *ep = (struct epic_private *)dev->priv;
00587         long ioaddr = dev->base_addr;
00588         int i;
00589 
00590         ep->full_duplex = ep->force_fd;
00591 
00592         /* Soft reset the chip. */
00593         outl(0x4001, ioaddr + GENCTL);
00594 
00595         if (request_irq(dev->irq, &epic_interrupt, SA_SHIRQ, dev->name, dev))
00596                 return -EAGAIN;
00597 
00598         MOD_INC_USE_COUNT;
00599 
00600         epic_init_ring(dev);
00601 
00602         outl(0x4000, ioaddr + GENCTL);
00603         /* This next magic! line by Ken Yamaguchi.. ?? */
00604         outl(0x0008, ioaddr + TEST1);
00605 
00606         /* Pull the chip out of low-power mode, enable interrupts, and set for
00607            PCI read multiple.  The MIIcfg setting and strange write order are
00608            required by the details of which bits are reset and the transceiver
00609            wiring on the Ositech CardBus card.
00610         */
00611         outl(0x12, ioaddr + MIICfg);
00612         if (ep->chip_flags & MII_PWRDWN)
00613                 outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
00614 
00615 #if defined(__powerpc__) || defined(__sparc__)          /* Big endian */
00616         outl(0x4432 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
00617         inl(ioaddr + GENCTL);
00618         outl(0x0432 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
00619 #else
00620         outl(0x4412 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
00621         inl(ioaddr + GENCTL);
00622         outl(0x0412 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
00623 #endif
00624 
00625         for (i = 0; i < 3; i++)
00626                 outl(cpu_to_le16(((u16*)dev->dev_addr)[i]), ioaddr + LAN0 + i*4);
00627 
00628         
00629         ep->tx_threshold = TX_FIFO_THRESH;
00630         outl(ep->tx_threshold, ioaddr + TxThresh);
00631 
00632         if (media2miictl[dev->if_port & 15]) {
00633                 if (ep->mii_phy_cnt)
00634                         mdio_write(ioaddr, ep->phys[0], 0, media2miictl[dev->if_port&15]);
00635                 if (dev->if_port == 1) {
00636                         if (epic_debug > 1)
00637                                 printk(KERN_INFO "%s: Using the 10base2 transceiver, MII "
00638                                            "status %4.4x.\n",
00639                                            dev->name, mdio_read(ioaddr, ep->phys[0], 1));
00640                         outl(0x13, ioaddr + MIICfg);
00641                 }
00642         } else {
00643                 int mii_reg5 = mdio_read(ioaddr, ep->phys[0], 5);
00644                 if (mii_reg5 != 0xffff) {
00645                         if ((mii_reg5 & 0x0100) || (mii_reg5 & 0x01C0) == 0x0040)
00646                                 ep->full_duplex = 1;
00647                         else if (! (mii_reg5 & 0x4000))
00648                                 mdio_write(ioaddr, ep->phys[0], 0, 0x1200);
00649                         if (epic_debug > 1)
00650                                 printk(KERN_INFO "%s: Setting %s-duplex based on MII xcvr %d"
00651                                            " register read of %4.4x.\n", dev->name,
00652                                            ep->full_duplex ? "full" : "half",
00653                                            ep->phys[0], mii_reg5);
00654                 }
00655         }
00656 
00657         outl(ep->full_duplex ? 0x7F : 0x79, ioaddr + TxCtrl);
00658         outl(virt_to_bus(ep->rx_ring), ioaddr + PRxCDAR);
00659         outl(virt_to_bus(ep->tx_ring), ioaddr + PTxCDAR);
00660 
00661         /* Start the chip's Rx process. */
00662         set_rx_mode(dev);
00663         outl(StartRx | RxQueued, ioaddr + COMMAND);
00664 
00665         dev->tbusy = 0;
00666         dev->interrupt = 0;
00667         dev->start = 1;
00668 
00669         /* Enable interrupts by setting the interrupt mask. */
00670         outl((ep->chip_flags & TYPE2_INTR ? PCIBusErr175 : PCIBusErr170)
00671                  | CntFull | TxUnderrun | TxDone | TxEmpty
00672                  | RxError | RxOverflow | RxFull | RxHeader | RxDone,
00673                  ioaddr + INTMASK);
00674 
00675         if (epic_debug > 1)
00676                 printk(KERN_DEBUG "%s: epic_open() ioaddr %lx IRQ %d status %4.4x "
00677                            "%s-duplex.\n",
00678                            dev->name, ioaddr, dev->irq, inl(ioaddr + GENCTL),
00679                            ep->full_duplex ? "full" : "half");
00680 
00681         /* Set the timer to switch to check for link beat and perhaps switch
00682            to an alternate media type. */
00683         init_timer(&ep->timer);
00684         ep->timer.expires = RUN_AT((24*HZ)/10);                 /* 2.4 sec. */
00685         ep->timer.data = (unsigned long)dev;
00686         ep->timer.function = &epic_timer;                               /* timer handler */
00687         add_timer(&ep->timer);
00688 
00689         return 0;
00690 }
00691 
00692 /* Reset the chip to recover from a PCI transaction error.
00693    This may occur at interrupt time. */
00694 static void epic_pause(struct device *dev)
00695 {
00696         long ioaddr = dev->base_addr;
00697         struct epic_private *ep = (struct epic_private *)dev->priv;
00698 
00699         /* Disable interrupts by clearing the interrupt mask. */
00700         outl(0x00000000, ioaddr + INTMASK);
00701         /* Stop the chip's Tx and Rx DMA processes. */
00702         outw(StopRx | StopTxDMA | StopRxDMA, ioaddr + COMMAND);
00703 
00704         /* Update the error counts. */
00705         if (inw(ioaddr + COMMAND) != 0xffff) {
00706                 ep->stats.rx_missed_errors += inb(ioaddr + MPCNT);
00707                 ep->stats.rx_frame_errors += inb(ioaddr + ALICNT);
00708                 ep->stats.rx_crc_errors += inb(ioaddr + CRCCNT);
00709         }
00710 
00711         /* Remove the packets on the Rx queue. */
00712         epic_rx(dev);
00713 }
00714 
00715 static void epic_restart(struct device *dev)
00716 {
00717         long ioaddr = dev->base_addr;
00718         struct epic_private *ep = (struct epic_private *)dev->priv;
00719         int i;
00720 
00721         printk(KERN_DEBUG "%s: Restarting the EPIC chip, Rx %d/%d Tx %d/%d.\n",
00722                    dev->name, ep->cur_rx, ep->dirty_rx, ep->dirty_tx, ep->cur_tx);
00723         /* Soft reset the chip. */
00724         outl(0x0001, ioaddr + GENCTL);
00725 
00726         udelay(1);
00727         /* Duplicate code from epic_open(). */
00728         outl(0x0008, ioaddr + TEST1);
00729 
00730 #if defined(__powerpc__) || defined(__sparc__)          /* Big endian */
00731         outl(0x0432 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
00732 #else
00733         outl(0x0412 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
00734 #endif
00735         outl(dev->if_port == 1 ? 0x13 : 0x12, ioaddr + MIICfg);
00736         if (ep->chip_flags & MII_PWRDWN)
00737                 outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
00738 
00739         for (i = 0; i < 3; i++)
00740                 outl(cpu_to_le16(((u16*)dev->dev_addr)[i]), ioaddr + LAN0 + i*4);
00741 
00742         ep->tx_threshold = TX_FIFO_THRESH;
00743         outl(ep->tx_threshold, ioaddr + TxThresh);
00744         outl(ep->full_duplex ? 0x7F : 0x79, ioaddr + TxCtrl);
00745         outl(virt_to_bus(&ep->rx_ring[ep->cur_rx%RX_RING_SIZE]), ioaddr + PRxCDAR);
00746         outl(virt_to_bus(&ep->tx_ring[ep->dirty_tx%TX_RING_SIZE]),
00747                  ioaddr + PTxCDAR);
00748 
00749         /* Start the chip's Rx process. */
00750         set_rx_mode(dev);
00751         outl(StartRx | RxQueued, ioaddr + COMMAND);
00752 
00753         /* Enable interrupts by setting the interrupt mask. */
00754         outl((ep->chip_flags & TYPE2_INTR ? PCIBusErr175 : PCIBusErr170)
00755                  | CntFull | TxUnderrun | TxDone | TxEmpty
00756                  | RxError | RxOverflow | RxFull | RxHeader | RxDone,
00757                  ioaddr + INTMASK);
00758         printk(KERN_DEBUG "%s: epic_restart() done, cmd status %4.4x, ctl %4.4x"
00759                    " interrupt %4.4x.\n",
00760                            dev->name, inl(ioaddr + COMMAND), inl(ioaddr + GENCTL),
00761                    inl(ioaddr + INTSTAT));
00762         return;
00763 }
00764 
00765 static void epic_timer(unsigned long data)
00766 {
00767         struct device *dev = (struct device *)data;
00768         struct epic_private *ep = (struct epic_private *)dev->priv;
00769         long ioaddr = dev->base_addr;
00770         int next_tick = 60*HZ;
00771         int mii_reg5 = ep->mii_phy_cnt ? mdio_read(ioaddr, ep->phys[0], 5) : 0;
00772 
00773         if (epic_debug > 3) {
00774                 printk(KERN_DEBUG "%s: Media monitor tick, Tx status %8.8x.\n",
00775                            dev->name, inl(ioaddr + TxSTAT));
00776                 printk(KERN_DEBUG "%s: Other registers are IntMask %4.4x "
00777                            "IntStatus %4.4x RxStatus %4.4x.\n",
00778                            dev->name, inl(ioaddr + INTMASK), inl(ioaddr + INTSTAT),
00779                            inl(ioaddr + RxSTAT));
00780         }
00781 
00782         if (ep->cur_tx - ep->dirty_tx > TX_RING_SIZE/2  &&
00783                 jiffies - dev->trans_start > TX_TIMEOUT) {
00784                 printk(KERN_WARNING "%s: Tx hung, %d vs. %d.\n",
00785                            dev->name, ep->cur_tx, ep->dirty_tx);
00786                 epic_tx_timeout(dev);
00787         }
00788 
00789         if (! ep->force_fd  &&  mii_reg5 != 0xffff) {
00790                 int duplex = (mii_reg5&0x0100) || (mii_reg5 & 0x01C0) == 0x0040;
00791                 if (ep->full_duplex != duplex) {
00792                         ep->full_duplex = duplex;
00793                         printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d link"
00794                                    " partner capability of %4.4x.\n", dev->name,
00795                                    ep->full_duplex ? "full" : "half", ep->phys[0], mii_reg5);
00796                         outl(ep->full_duplex ? 0x7F : 0x79, ioaddr + TxCtrl);
00797                 }
00798         }
00799 
00800         ep->timer.expires = RUN_AT(next_tick);
00801         add_timer(&ep->timer);
00802 }
00803 
00804 static void epic_tx_timeout(struct device *dev)
00805 {
00806         struct epic_private *ep = (struct epic_private *)dev->priv;
00807         long ioaddr = dev->base_addr;
00808 
00809         if (epic_debug > 0) {
00810                 printk(KERN_WARNING "%s: Transmit timeout using MII device, "
00811                            "Tx status %4.4x.\n",
00812                            dev->name, inw(ioaddr + TxSTAT));
00813                 if (epic_debug > 1) {
00814                         printk(KERN_DEBUG "%s: Tx indices: dirty_tx %d, cur_tx %d.\n",
00815                                    dev->name, ep->dirty_tx, ep->cur_tx);
00816                 }
00817         }
00818         if (inw(ioaddr + TxSTAT) & 0x10) {              /* Tx FIFO underflow. */
00819                 ep->stats.tx_fifo_errors++;
00820                 outl(RestartTx, ioaddr + COMMAND);
00821         } else {
00822                 epic_restart(dev);
00823                 outl(TxQueued, dev->base_addr + COMMAND);
00824         }
00825 
00826         dev->trans_start = jiffies;
00827         ep->stats.tx_errors++;
00828         return;
00829 }
00830 
00831 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
00832 static void
00833 epic_init_ring(struct device *dev)
00834 {
00835         struct epic_private *ep = (struct epic_private *)dev->priv;
00836         int i;
00837 
00838         ep->tx_full = 0;
00839         ep->lock = (spinlock_t) SPIN_LOCK_UNLOCKED;
00840         ep->dirty_tx = ep->cur_tx = 0;
00841         ep->cur_rx = ep->dirty_rx = 0;
00842         ep->last_rx_time = jiffies;
00843 
00844         for (i = 0; i < RX_RING_SIZE; i++) {
00845                 ep->rx_ring[i].status = 0x8000;         /* Owned by Epic chip */
00846                 ep->rx_ring[i].buflength = PKT_BUF_SZ;
00847                 {
00848                         /* Note the receive buffer must be longword aligned.
00849                            dev_alloc_skb() provides 16 byte alignment.  But do *not*
00850                            use skb_reserve() to align the IP header! */
00851                         struct sk_buff *skb;
00852                         skb = dev_alloc_skb(PKT_BUF_SZ);
00853                         ep->rx_skbuff[i] = skb;
00854                         if (skb == NULL)
00855                                 break;                  /* Bad news!  */
00856                         skb->dev = dev;                 /* Mark as being used by this device. */
00857                         skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
00858                         ep->rx_ring[i].bufaddr = virt_to_bus(skb->tail);
00859                 }
00860                 ep->rx_ring[i].next = virt_to_bus(&ep->rx_ring[i+1]);
00861         }
00862         /* Mark the last entry as wrapping the ring. */
00863         ep->rx_ring[i-1].next = virt_to_bus(&ep->rx_ring[0]);
00864 
00865         /* The Tx buffer descriptor is filled in as needed, but we
00866            do need to clear the ownership bit. */
00867         for (i = 0; i < TX_RING_SIZE; i++) {
00868                 ep->tx_skbuff[i] = 0;
00869                 ep->tx_ring[i].status = 0x0000;
00870                 ep->tx_ring[i].next = virt_to_bus(&ep->tx_ring[i+1]);
00871         }
00872         ep->tx_ring[i-1].next = virt_to_bus(&ep->tx_ring[0]);
00873 }
00874 
00875 static int
00876 epic_start_xmit(struct sk_buff *skb, struct device *dev)
00877 {
00878         struct epic_private *ep = (struct epic_private *)dev->priv;
00879         int entry, free_count;
00880         u32 ctrl_word;
00881         long flags;
00882         
00883         if(skb->len < ETH_ZLEN)
00884         {
00885                 skb = skb_padto(skb, ETH_ZLEN);
00886                 if(skb == NULL)
00887                         return 0;
00888         }
00889 
00890         /* Block a timer-based transmit from overlapping.  This could better be
00891            done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
00892         if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
00893                 if (jiffies - dev->trans_start > TX_TIMEOUT)
00894                         epic_tx_timeout(dev);
00895                 return 1;
00896         }
00897 
00898         /* Caution: the write order is important here, set the base address
00899            with the "ownership" bits last. */
00900 
00901         /* Calculate the next Tx descriptor entry. */
00902         spin_lock_irqsave(&ep->lock, flags);
00903         free_count = ep->cur_tx - ep->dirty_tx;
00904         entry = ep->cur_tx++ % TX_RING_SIZE;
00905 
00906         ep->tx_skbuff[entry] = skb;
00907         ep->tx_ring[entry].txlength = (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN);
00908         ep->tx_ring[entry].bufaddr = virt_to_bus(skb->data);
00909         ep->tx_ring[entry].buflength = skb->len;
00910 
00911         if (free_count < TX_RING_SIZE/2) {/* Typical path */
00912                 ctrl_word = 0x10; /* No interrupt */
00913         } else if (free_count == TX_RING_SIZE/2) {
00914                 ctrl_word = 0x14; /* Tx-done intr. */
00915         } else if (free_count < TX_RING_SIZE - 1) {
00916                 ctrl_word = 0x10; /* No Tx-done intr. */
00917         } else {
00918                 /* Leave room for an additional entry. */
00919                 ctrl_word = 0x14; /* Tx-done intr. */
00920                 ep->tx_full = 1;
00921         }
00922 
00923         ep->tx_ring[entry].control = ctrl_word;
00924         ep->tx_ring[entry].status = 0x8000;     /* Pass ownership to the chip. */
00925 
00926         if ( ! ep->tx_full)
00927                 clear_bit(0, (void*)&dev->tbusy);
00928         spin_unlock_irqrestore(&ep->lock, flags);
00929         /* Trigger an immediate transmit demand. */
00930         outl(TxQueued, dev->base_addr + COMMAND);
00931 
00932         dev->trans_start = jiffies;
00933         if (epic_debug > 4)
00934                 printk(KERN_DEBUG "%s: Queued Tx packet size %d to slot %d, "
00935                            "flag %2.2x Tx status %8.8x.\n",
00936                            dev->name, (int)skb->len, entry, ctrl_word,
00937                            inl(dev->base_addr + TxSTAT));
00938 
00939         return 0;
00940 }
00941 
00942 /* The interrupt handler does all of the Rx thread work and cleans up
00943    after the Tx thread. */
00944 static void epic_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
00945 {
00946         struct device *dev = (struct device *)dev_instance;
00947         struct epic_private *ep = (struct epic_private *)dev->priv;
00948         long ioaddr = dev->base_addr;
00949         int status, boguscnt = max_interrupt_work;
00950 
00951 #if defined(__i386__)
00952         /* A lock to prevent simultaneous entry bug on Intel SMP machines. */
00953         if (test_and_set_bit(0, (void*)&dev->interrupt)) {
00954                 printk(KERN_ERR "%s: SMP simultaneous entry of an interrupt handler.\n",
00955                            dev->name);
00956                 dev->interrupt = 0;     /* Avoid halting machine. */
00957                 return;
00958         }
00959 #else
00960         if (dev->interrupt) {
00961                 printk(KERN_ERR "%s: Re-entering the interrupt handler.\n", dev->name);
00962                 return;
00963         }
00964         dev->interrupt = 1;
00965 #endif
00966 
00967         do {
00968                 status = inl(ioaddr + INTSTAT);
00969                 /* Acknowledge all of the current interrupt sources ASAP. */
00970                 outl(status & 0x00007fff, ioaddr + INTSTAT);
00971 
00972                 if (epic_debug > 4)
00973                         printk(KERN_DEBUG "%s: interrupt  interrupt=%#8.8x new "
00974                                    "intstat=%#8.8x.\n",
00975                                    dev->name, status, inl(ioaddr + INTSTAT));
00976 
00977                 if ((status & IntrSummary) == 0)
00978                         break;
00979 
00980                 if (status & (RxDone | RxStarted | RxEarlyWarn | RxOverflow))
00981                         epic_rx(dev);
00982 
00983                 if (status & (TxEmpty | TxDone)) {
00984                         unsigned int dirty_tx, cur_tx;
00985 
00986                         /* Note: if this lock becomes a problem we can narrow the locked
00987                            region at the cost of occasionally grabbing the lock more
00988                            times. */
00989                         spin_lock(&ep->lock);
00990                         cur_tx = ep->cur_tx;
00991                         dirty_tx = ep->dirty_tx;
00992                         for (; cur_tx - dirty_tx > 0; dirty_tx++) {
00993                                 int entry = dirty_tx % TX_RING_SIZE;
00994                                 int txstatus = ep->tx_ring[entry].status;
00995 
00996                                 if (txstatus < 0)
00997                                         break;                  /* It still hasn't been Txed */
00998 
00999                                 if ( ! (txstatus & 0x0001)) {
01000                                         /* There was an major error, log it. */
01001 #ifndef final_version
01002                                         if (epic_debug > 1)
01003                                                 printk("%s: Transmit error, Tx status %8.8x.\n",
01004                                                            dev->name, txstatus);
01005 #endif
01006                                         ep->stats.tx_errors++;
01007                                         if (txstatus & 0x1050) ep->stats.tx_aborted_errors++;
01008                                         if (txstatus & 0x0008) ep->stats.tx_carrier_errors++;
01009                                         if (txstatus & 0x0040) ep->stats.tx_window_errors++;
01010                                         if (txstatus & 0x0010) ep->stats.tx_fifo_errors++;
01011 #ifdef ETHER_STATS
01012                                         if (txstatus & 0x1000) ep->stats.collisions16++;
01013 #endif
01014                                 } else {
01015 #ifdef ETHER_STATS
01016                                         if ((txstatus & 0x0002) != 0) ep->stats.tx_deferred++;
01017 #endif
01018                                         ep->stats.collisions += (txstatus >> 8) & 15;
01019                                         ep->stats.tx_packets++;
01020                                         ep->stats.tx_bytes += ep->tx_ring[entry].txlength;
01021