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

via-rhine.c

Go to the documentation of this file.
00001 /* via-rhine.c: A Linux Ethernet device driver for VIA Rhine family chips. */
00002 /*
00003         Written 1998-2000 by Donald Becker.
00004 
00005         This software may be used and distributed according to the terms of
00006         the GNU General Public License (GPL), incorporated herein by reference.
00007         Drivers based on or derived from this code fall under the GPL and must
00008         retain the authorship, copyright and license notice.  This file is not
00009         a complete program and may only be used when the entire operating
00010         system is licensed under the GPL.
00011 
00012         This driver is designed for the VIA VT86c100A Rhine-II PCI Fast Ethernet
00013         controller.  It also works with the older 3043 Rhine-I chip.
00014 
00015         The author may be reached as becker@scyld.com, or C/O
00016         Scyld Computing Corporation
00017         410 Severn Ave., Suite 210
00018         Annapolis MD 21403
00019 
00020         Support and updates available at
00021         http://www.scyld.com/network/via-rhine.html
00022 
00023 
00024         Linux kernel version history:
00025 
00026         LK1.0.0:
00027         - Urban Widmark: merges from Beckers 1.08b version and 2.4.0 (VT6102)
00028 
00029         LK1.0.1
00030         - Dennis Björklund: backport tx_timeout from 2.4.x to reset on timeout
00031                         instead of stop working...
00032 */
00033 
00034 /* These identify the driver base version and may not be removed. */
00035 static const char version1[] =
00036 "via-rhine.c:v1.08b-LK1.0.1 12/14/2000  Written by Donald Becker\n";
00037 static const char version2[] =
00038 "  http://www.scyld.com/network/via-rhine.html\n";
00039 
00040 /* The user-configurable values.
00041    These may be modified when a driver module is loaded.*/
00042 
00043 static int debug = 1;                   /* 1 normal messages, 0 quiet .. 7 verbose. */
00044 static int max_interrupt_work = 20;
00045 static int min_pci_latency = 32;
00046 
00047 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
00048    Setting to > 1518 effectively disables this feature. */
00049 static int rx_copybreak = 0;
00050 
00051 /* Used to pass the media type, etc.
00052    Both 'options[]' and 'full_duplex[]' should exist for driver
00053    interoperability.
00054    The media type is usually passed in 'options[]'.
00055 */
00056 #define MAX_UNITS 8             /* More are supported, limit only on options */
00057 static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
00058 static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
00059 
00060 /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
00061    The Rhine has a 64 element 8390-like hash table.  */
00062 static const int multicast_filter_limit = 32;
00063 
00064 /* Operational parameters that are set at compile time. */
00065 
00066 /* Keep the ring sizes a power of two for compile efficiency.
00067    The compiler will convert <unsigned>'%'<2^N> into a bit mask.
00068    Making the Tx ring too large decreases the effectiveness of channel
00069    bonding and packet priority.
00070    There are no ill effects from too-large receive rings. */
00071 #define TX_RING_SIZE    16
00072 #define TX_QUEUE_LEN    10              /* Limit ring entries actually used.  */
00073 #define RX_RING_SIZE    16
00074 
00075 /* Operational parameters that usually are not changed. */
00076 /* Time in jiffies before concluding the transmitter is hung. */
00077 #define TX_TIMEOUT  (2*HZ)
00078 
00079 #define PKT_BUF_SZ              1536                    /* Size of each temporary Rx buffer.*/
00080 
00081 
00082 #if !defined(__OPTIMIZE__)
00083 #warning  You must compile this file with the correct options!
00084 #warning  See the last lines of the source file.
00085 #error  You must compile this driver with "-O".
00086 #endif
00087 
00088 #include <linux/version.h>
00089 #include <linux/module.h>
00090 #include <linux/kernel.h>
00091 #include <linux/string.h>
00092 #include <linux/timer.h>
00093 #include <linux/errno.h>
00094 #include <linux/ioport.h>
00095 #include <linux/malloc.h>
00096 #include <linux/interrupt.h>
00097 #include <linux/pci.h>
00098 #include <linux/netdevice.h>
00099 #include <linux/etherdevice.h>
00100 #include <linux/skbuff.h>
00101 #include <linux/init.h>
00102 #include <linux/delay.h>
00103 #include <asm/processor.h>              /* Processor type for cache alignment. */
00104 #include <asm/bitops.h>
00105 #include <asm/io.h>
00106 #include <asm/irq.h>
00107 
00108 /* Condensed bus+endian portability operations. */
00109 #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
00110 #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
00111 
00112 /* This driver was written to use PCI memory space, however most versions
00113    of the Rhine only work correctly with I/O space accesses. */
00114 #if defined(VIA_USE_MEMORY)
00115 #warning Many adapters using the VIA Rhine chip are not configured to work
00116 #warning with PCI memory space accesses.
00117 #else
00118 #define USE_IO_OPS
00119 #undef readb
00120 #undef readw
00121 #undef readl
00122 #undef writeb
00123 #undef writew
00124 #undef writel
00125 #define readb inb
00126 #define readw inw
00127 #define readl inl
00128 #define writeb outb
00129 #define writew outw
00130 #define writel outl
00131 #endif
00132 
00133 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
00134 MODULE_DESCRIPTION("VIA Rhine PCI Fast Ethernet driver");
00135 MODULE_PARM(max_interrupt_work, "i");
00136 MODULE_PARM(min_pci_latency, "i");
00137 MODULE_PARM(debug, "i");
00138 MODULE_PARM(rx_copybreak, "i");
00139 MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
00140 MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
00141 
00142 #define NETSTATS_VER2
00143 
00144 /*
00145                                 Theory of Operation
00146 
00147 I. Board Compatibility
00148 
00149 This driver is designed for the VIA 86c100A Rhine-II PCI Fast Ethernet
00150 controller.
00151 
00152 II. Board-specific settings
00153 
00154 Boards with this chip are functional only in a bus-master PCI slot.
00155 
00156 Many operational settings are loaded from the EEPROM to the Config word at
00157 offset 0x78.  This driver assumes that they are correct.
00158 If this driver is compiled to use PCI memory space operations the EEPROM
00159 must be configured to enable memory ops.
00160 
00161 III. Driver operation
00162 
00163 IIIa. Ring buffers
00164 
00165 This driver uses two statically allocated fixed-size descriptor lists
00166 formed into rings by a branch from the final descriptor to the beginning of
00167 the list.  The ring sizes are set at compile time by RX/TX_RING_SIZE.
00168 
00169 IIIb/c. Transmit/Receive Structure
00170 
00171 This driver attempts to use a zero-copy receive and transmit scheme.
00172 
00173 Alas, all data buffers are required to start on a 32 bit boundary, so
00174 the driver must often copy transmit packets into bounce buffers.
00175 
00176 The driver allocates full frame size skbuffs for the Rx ring buffers at
00177 open() time and passes the skb->data field to the chip as receive data
00178 buffers.  When an incoming frame is less than RX_COPYBREAK bytes long,
00179 a fresh skbuff is allocated and the frame is copied to the new skbuff.
00180 When the incoming frame is larger, the skbuff is passed directly up the
00181 protocol stack.  Buffers consumed this way are replaced by newly allocated
00182 skbuffs in the last phase of netdev_rx().
00183 
00184 The RX_COPYBREAK value is chosen to trade-off the memory wasted by
00185 using a full-sized skbuff for small frames vs. the copying costs of larger
00186 frames.  New boards are typically used in generously configured machines
00187 and the underfilled buffers have negligible impact compared to the benefit of
00188 a single allocation size, so the default value of zero results in never
00189 copying packets.  When copying is done, the cost is usually mitigated by using
00190 a combined copy/checksum routine.  Copying also preloads the cache, which is
00191 most useful with small frames.
00192 
00193 Since the VIA chips are only able to transfer data to buffers on 32 bit
00194 boundaries, the the IP header at offset 14 in an ethernet frame isn't
00195 longword aligned for further processing.  Copying these unaligned buffers
00196 has the beneficial effect of 16-byte aligning the IP header.
00197 
00198 IIId. Synchronization
00199 
00200 The driver runs as two independent, single-threaded flows of control.  One
00201 is the send-packet routine, which enforces single-threaded use by the
00202 dev->tbusy flag.  The other thread is the interrupt handler, which is single
00203 threaded by the hardware and interrupt handling software.
00204 
00205 The send packet thread has partial control over the Tx ring and 'dev->tbusy'
00206 flag.  It sets the tbusy flag whenever it's queuing a Tx packet. If the next
00207 queue slot is empty, it clears the tbusy flag when finished otherwise it sets
00208 the 'lp->tx_full' flag.
00209 
00210 The interrupt handler has exclusive control over the Rx ring and records stats
00211 from the Tx ring.  After reaping the stats, it marks the Tx queue entry as
00212 empty by incrementing the dirty_tx mark. Iff the 'lp->tx_full' flag is set, it
00213 clears both the tx_full and tbusy flags.
00214 
00215 IV. Notes
00216 
00217 IVb. References
00218 
00219 Preliminary VT86C100A manual from http://www.via.com.tw/
00220 http://www.scyld.com/expert/100mbps.html
00221 http://www.scyld.com/expert/NWay.html
00222 
00223 IVc. Errata
00224 
00225 The VT86C100A manual is not reliable information.
00226 The 3043 chip does not handle unaligned transmit or receive buffers, resulting
00227 in significant performance degradation for bounce buffer copies on transmit
00228 and unaligned IP headers on receive.
00229 The chip does not pad to minimum transmit length.
00230 
00231 */
00232 
00233 
00234 
00235 /* This table drives the PCI probe routines.  It's mostly boilerplate in all
00236    of the drivers, and will likely be provided by some future kernel.
00237    Note the matching code -- the first table entry matchs all 56** cards but
00238    second only the 1234 card.
00239 */
00240 enum pci_flags_bit {
00241         PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
00242         PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
00243 };
00244 
00245 #if defined(VIA_USE_MEMORY)
00246 #define RHINE_IOTYPE (PCI_USES_MEM | PCI_USES_MASTER | PCI_ADDR0)
00247 #define RHINEII_IOSIZE 4096
00248 #else
00249 #define RHINE_IOTYPE (PCI_USES_IO  | PCI_USES_MASTER | PCI_ADDR1)
00250 #define RHINEII_IOSIZE 256
00251 #endif
00252 
00253 struct pci_id_info {
00254         const char *name;
00255         u16     vendor_id, device_id, device_id_mask, flags;
00256         int io_size;
00257         struct device *(*probe1)(int pci_bus, int pci_devfn, struct device *dev,
00258                                                          long ioaddr, int irq, int chip_idx, int fnd_cnt);
00259 };
00260 
00261 static struct device *via_probe1(int pci_bus, int pci_devfn,
00262                                                                  struct device *dev, long ioaddr, int irq,
00263                                                                  int chp_idx, int fnd_cnt);
00264 
00265 enum via_rhine_chips {
00266         VT86C100A = 0,
00267         VT6102,
00268         VT3043,
00269 };
00270 
00271 /* directly indexed by enum via_rhine_chips, above */
00272 static struct pci_id_info pci_tbl[] __initdata = {
00273         { "VIA VT86C100A Rhine-II", 0x1106, 0x6100, 0xffff,
00274           RHINE_IOTYPE, 128, via_probe1},
00275         { "VIA VT6102 Rhine-II", 0x1106, 0x3065, 0xffff,
00276           RHINE_IOTYPE, RHINEII_IOSIZE, via_probe1},
00277         { "VIA VT3043 Rhine", 0x1106, 0x3043, 0xffff,
00278           RHINE_IOTYPE, 128, via_probe1},
00279         {0,},                                           /* 0 terminated list. */
00280 };
00281 
00282 
00283 /* A chip capabilities table, matching the entries in pci_tbl[] above. */
00284 enum chip_capability_flags {
00285         CanHaveMII=1, HasESIPhy=2, HasDavicomPhy=4,
00286         ReqTxAlign=0x10, HasWOL=0x20,
00287 };
00288 
00289 struct chip_info {
00290         int io_size;
00291         int flags;
00292 } static cap_tbl[] __initdata = {
00293         {128, CanHaveMII | ReqTxAlign, },
00294         {128, CanHaveMII | HasWOL, },
00295         {128, CanHaveMII | ReqTxAlign, },
00296 };
00297 
00298 
00299 /* Offsets to the device registers. */
00300 enum register_offsets {
00301         StationAddr=0x00, RxConfig=0x06, TxConfig=0x07, ChipCmd=0x08,
00302         IntrStatus=0x0C, IntrEnable=0x0E,
00303         MulticastFilter0=0x10, MulticastFilter1=0x14,
00304         RxRingPtr=0x18, TxRingPtr=0x1C,
00305         MIIPhyAddr=0x6C, MIIStatus=0x6D, PCIBusConfig=0x6E,
00306         MIICmd=0x70, MIIRegAddr=0x71, MIIData=0x72,
00307         Config=0x78, ConfigA=0x7A, RxMissed=0x7C, RxCRCErrs=0x7E,
00308         StickyHW=0x83, WOLcrClr=0xA4, WOLcgClr=0xA7, PwrcsrClr=0xAC,
00309 };
00310 
00311 /* Bits in the interrupt status/mask registers. */
00312 enum intr_status_bits {
00313         IntrRxDone=0x0001, IntrRxErr=0x0004, IntrRxEmpty=0x0020,
00314         IntrTxDone=0x0002, IntrTxAbort=0x0008, IntrTxUnderrun=0x0010,
00315         IntrPCIErr=0x0040,
00316         IntrStatsMax=0x0080, IntrRxEarly=0x0100, IntrMIIChange=0x0200,
00317         IntrRxOverflow=0x0400, IntrRxDropped=0x0800, IntrRxNoBuf=0x1000,
00318         IntrTxAborted=0x2000, IntrLinkChange=0x4000,
00319         IntrRxWakeUp=0x8000,
00320         IntrNormalSummary=0x0003, IntrAbnormalSummary=0xC260,
00321 };
00322 
00323 /* The Rx and Tx buffer descriptors. */
00324 struct rx_desc {
00325         s32 rx_status;
00326         u32 desc_length;
00327         u32 addr;
00328         u32 next_desc;
00329 };
00330 struct tx_desc {
00331         s32 tx_status;
00332         u32 desc_length;
00333         u32 addr;
00334         u32 next_desc;
00335 };
00336 
00337 /* Bits in *_desc.status */
00338 enum rx_status_bits {
00339         RxOK=0x8000, RxWholePkt=0x0300, RxErr=0x008F
00340 };
00341 enum desc_status_bits {
00342         DescOwn=0x80000000, DescEndPacket=0x4000, DescIntr=0x1000,
00343 };
00344 
00345 /* Bits in ChipCmd. */
00346 enum chip_cmd_bits {
00347         CmdInit=0x0001, CmdStart=0x0002, CmdStop=0x0004, CmdRxOn=0x0008,
00348         CmdTxOn=0x0010, CmdTxDemand=0x0020, CmdRxDemand=0x0040,
00349         CmdEarlyRx=0x0100, CmdEarlyTx=0x0200, CmdFDuplex=0x0400,
00350         CmdNoTxPoll=0x0800, CmdReset=0x8000,
00351 };
00352 
00353 #define PRIV_ALIGN      15      /* Required alignment mask */
00354 struct netdev_private {
00355         /* Descriptor rings first for alignment. */
00356         struct rx_desc rx_ring[RX_RING_SIZE];
00357         struct tx_desc tx_ring[TX_RING_SIZE];
00358         /* The addresses of receive-in-place skbuffs. */
00359         struct sk_buff* rx_skbuff[RX_RING_SIZE];
00360         /* The saved address of a sent-in-place packet/buffer, for later free(). */
00361         struct sk_buff* tx_skbuff[TX_RING_SIZE];
00362         unsigned char *tx_buf[TX_RING_SIZE];    /* Tx bounce buffers */
00363         unsigned char *tx_bufs;                         /* Tx bounce buffer region. */
00364         struct device *next_module;                     /* Link for devices of this type. */
00365         void *priv_addr;                                        /* Unaligned address for kfree */
00366         struct net_device_stats stats;
00367         struct timer_list timer;        /* Media monitoring timer. */
00368         unsigned char pci_bus, pci_devfn;
00369         /* Frequently used values: keep some adjacent for cache effect. */
00370         int chip_id, drv_flags;
00371         struct rx_desc *rx_head_desc;
00372         unsigned int cur_rx, dirty_rx;          /* Producer/consumer ring indices */
00373         unsigned int cur_tx, dirty_tx;
00374         unsigned int rx_buf_sz;                         /* Based on MTU+slack. */
00375         u16 chip_cmd;                                           /* Current setting for ChipCmd */
00376         unsigned int tx_full:1;                         /* The Tx queue is full. */
00377         /* These values are keep track of the transceiver/media in use. */
00378         unsigned int full_duplex:1;                     /* Full-duplex operation requested. */
00379         unsigned int duplex_lock:1;
00380         unsigned int medialock:1;                       /* Do not sense media. */
00381         unsigned int default_port:4;            /* Last dev->if_port value. */
00382         u8 tx_thresh, rx_thresh;
00383         /* MII transceiver section. */
00384         int mii_cnt;                                            /* MII device addresses. */
00385         u16 advertising;                                        /* NWay media advertisement */
00386         unsigned char phys[2];                          /* MII device addresses. */
00387 };
00388 
00389 static int  mdio_read(struct device *dev, int phy_id, int location);
00390 static void mdio_write(struct device *dev, int phy_id, int location, int value);
00391 static int  netdev_open(struct device *dev);
00392 static void check_duplex(struct device *dev);
00393 static void netdev_timer(unsigned long data);
00394 static void tx_timeout(struct device *dev);
00395 static int  start_tx(struct sk_buff *skb, struct device *dev);
00396 static void intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
00397 static int  netdev_rx(struct device *dev);
00398 static void netdev_error(struct device *dev, int intr_status);
00399 static void set_rx_mode(struct device *dev);
00400 static struct net_device_stats *get_stats(struct device *dev);
00401 static int mii_ioctl(struct device *dev, struct ifreq *rq, int cmd);
00402 static int  netdev_close(struct device *dev);
00403 static inline void clear_tally_counters(long ioaddr);
00404 
00405 
00406 
00407 /* A list of our installed devices, for removing the driver module. */
00408 static struct device *root_net_dev = NULL;
00409 
00410 static void wait_for_reset(struct device *dev, char *name)
00411 {
00412         struct netdev_private *np = dev->priv;
00413         long ioaddr = dev->base_addr;
00414         int chip_id = np->chip_id;
00415         int i;
00416 
00417         /* 3043 may need long delay after reset (dlink) */
00418         if (chip_id == VT3043 || chip_id == VT86C100A)
00419                 udelay(100);
00420 
00421         i = 0;
00422         do {
00423                 udelay(5);
00424                 i++;
00425                 if(i > 2000) {
00426                         printk(KERN_ERR "%s: reset did not complete in 10 ms.\n", name);
00427                         break;
00428                 }
00429         } while(readw(ioaddr + ChipCmd) & CmdReset);
00430         if (debug > 1)
00431                 printk(KERN_INFO "%s: reset finished after %d microseconds.\n",
00432                            name, 5*i);
00433 }
00434 
00435 /* Ideally we would detect all network cards in slot order.  That would
00436    be best done a central PCI probe dispatch, which wouldn't work
00437    well when dynamically adding drivers.  So instead we detect just the
00438    cards we know about in slot order. */
00439 
00440 static int __init pci_etherdev_probe(struct device *dev, struct pci_id_info pci_tbl[])
00441 {
00442         int cards_found = 0;
00443         int pci_index = 0;
00444         unsigned char pci_bus, pci_device_fn;
00445 
00446         if ( ! pcibios_present())
00447                 return -ENODEV;
00448 
00449         for (;pci_index < 0xff; pci_index++) {
00450                 u16 vendor, device, pci_command, new_command;
00451                 int chip_idx, irq;
00452                 long pciaddr;
00453                 long ioaddr;
00454 
00455                 if (pcibios_find_class (PCI_CLASS_NETWORK_ETHERNET << 8, pci_index,
00456                                                                 &pci_bus, &pci_device_fn)
00457                         != PCIBIOS_SUCCESSFUL)
00458                         break;
00459                 pcibios_read_config_word(pci_bus, pci_device_fn,
00460                                                                  PCI_VENDOR_ID, &vendor);
00461                 pcibios_read_config_word(pci_bus, pci_device_fn,
00462                                                                  PCI_DEVICE_ID, &device);
00463 
00464                 for (chip_idx = 0; pci_tbl[chip_idx].vendor_id; chip_idx++)
00465                         if (vendor == pci_tbl[chip_idx].vendor_id
00466                                 && (device & pci_tbl[chip_idx].device_id_mask) ==
00467                                 pci_tbl[chip_idx].device_id)
00468                                 break;
00469                 if (pci_tbl[chip_idx].vendor_id == 0)           /* Compiled out! */
00470                         continue;
00471 
00472                 {
00473                         struct pci_dev *pdev = pci_find_slot(pci_bus, pci_device_fn);
00474 #ifdef USE_IO_OPS
00475                         pciaddr = pdev->base_address[0];
00476 #else
00477                         pciaddr = pdev->base_address[1];
00478 #endif
00479                         irq = pdev->irq;
00480                 }
00481 
00482                 if (debug > 2)
00483                         printk(KERN_INFO "Found %s at PCI address %#lx, IRQ %d.\n",
00484                                    pci_tbl[chip_idx].name, pciaddr, irq);
00485 
00486                 if (pci_tbl[chip_idx].flags & PCI_USES_IO) {
00487                         ioaddr = pciaddr & ~3;
00488                         if (check_region(ioaddr, pci_tbl[chip_idx].io_size))
00489                                 continue;
00490                 } else if ((ioaddr = (long)ioremap(pciaddr & ~0xf,
00491                                                                                  pci_tbl[chip_idx].io_size)) == 0) {
00492                         printk(KERN_INFO "Failed to map PCI address %#lx.\n",
00493                                    pciaddr);
00494                         continue;
00495                 }
00496 
00497                 pcibios_read_config_word(pci_bus, pci_device_fn,
00498                                                                  PCI_COMMAND, &pci_command);
00499                 new_command = pci_command | (pci_tbl[chip_idx].flags & 7);
00500                 if (pci_command != new_command) {
00501                         printk(KERN_INFO "  The PCI BIOS has not enabled the"
00502                                    " device at %d/%d!  Updating PCI command %4.4x->%4.4x.\n",
00503                                    pci_bus, pci_device_fn, pci_command, new_command);
00504                         pcibios_write_config_word(pci_bus, pci_device_fn,
00505                                                                           PCI_COMMAND, new_command);
00506                 }
00507 
00508                 dev = pci_tbl[chip_idx].probe1(pci_bus, pci_device_fn, dev, ioaddr,
00509                                                                            irq, chip_idx, cards_found);
00510 
00511                 if (dev  && (pci_tbl[chip_idx].flags & PCI_COMMAND_MASTER)) {
00512                         u8 pci_latency;
00513                         pcibios_read_config_byte(pci_bus, pci_device_fn,
00514                                                                          PCI_LATENCY_TIMER, &pci_latency);
00515                         if (pci_latency < min_pci_latency) {
00516                                 printk(KERN_INFO "  PCI latency timer (CFLT) is "
00517                                            "unreasonably low at %d.  Setting to %d clocks.\n",
00518                                            pci_latency, min_pci_latency);
00519                                 pcibios_write_config_byte(pci_bus, pci_device_fn,
00520                                                                                   PCI_LATENCY_TIMER, min_pci_latency);
00521                         }
00522                 }
00523                 dev = 0;
00524                 cards_found++;
00525         }
00526 
00527         return cards_found ? 0 : -ENODEV;
00528 }
00529 
00530 #ifndef MODULE
00531 int __init via_rhine_probe(struct device *dev)
00532 {
00533         static int did_version = 0;
00534         if (!did_version++)
00535                 printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
00536         return pci_etherdev_probe(dev, pci_tbl);
00537 }
00538 #endif
00539 
00540 static struct device * __init via_probe1(int pci_bus, int pci_devfn,
00541                                                                  struct device *dev, long ioaddr, int irq,
00542                                                                  int chip_id, int card_idx)
00543 {
00544         struct netdev_private *np;
00545         void *priv_mem;
00546         int i, option = card_idx < MAX_UNITS ? options[card_idx] : 0;
00547 
00548         dev = init_etherdev(dev, 0);
00549 
00550         printk(KERN_INFO "%s: %s at 0x%lx, ",
00551                    dev->name, pci_tbl[chip_id].name, ioaddr);
00552 
00553         /* We would prefer to read the EEPROM but access may be locked. */
00554         for (i = 0; i < 6; i++)
00555                 dev->dev_addr[i] = readb(ioaddr + StationAddr + i);
00556         for (i = 0; i < 5; i++)
00557                         printk("%2.2x:", dev->dev_addr[i]);
00558         printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);
00559 
00560         /* Allocate driver private memory for descriptor lists.
00561            Check for the very unlikely case of no memory. */
00562         priv_mem = kmalloc(sizeof(*np) + PRIV_ALIGN, GFP_KERNEL);
00563         if (priv_mem == NULL)
00564                 return NULL;
00565 
00566 #ifdef USE_IO_OPS
00567         request_region(ioaddr, pci_tbl[chip_id].io_size, dev->name);
00568 #endif
00569 
00570         /* Reset the chip to erase previous misconfiguration. */
00571         writew(CmdReset, ioaddr + ChipCmd);
00572 
00573         dev->base_addr = ioaddr;
00574         dev->irq = irq;
00575 
00576         /* Make certain the descriptor lists are cache-aligned. */
00577         dev->priv = np = (void *)(((long)priv_mem + PRIV_ALIGN) & ~PRIV_ALIGN);
00578         memset(np, 0, sizeof(*np));
00579         np->priv_addr = priv_mem;
00580 
00581         np->next_module = root_net_dev;
00582         root_net_dev = dev;
00583 
00584         np->pci_bus = pci_bus;
00585         np->pci_devfn = pci_devfn;
00586         np->chip_id = chip_id;
00587         np->drv_flags = cap_tbl[chip_id].flags;
00588 
00589         if (dev->mem_start)
00590                 option = dev->mem_start;
00591 
00592         /* The lower four bits are the media type. */
00593         if (option > 0) {
00594                 if (option & 0x200)
00595                         np->full_duplex = 1;
00596                 np->default_port = option & 15;
00597                 if (np->default_port)
00598                         np->medialock = 1;
00599         }
00600         if (card_idx < MAX_UNITS  &&  full_duplex[card_idx] > 0)
00601                 np->full_duplex = 1;
00602 
00603         if (np->full_duplex)
00604                 np->duplex_lock = 1;
00605 
00606         /* The chip-specific entries in the device structure. */
00607         dev->open = &netdev_open;
00608         dev->hard_start_xmit = &start_tx;
00609         dev->stop = &netdev_close;
00610         dev->get_stats = &get_stats;
00611         dev->set_multicast_list = &set_rx_mode;
00612         dev->do_ioctl = &mii_ioctl;
00613 
00614         if (np->drv_flags & CanHaveMII) {
00615                 int phy, phy_idx = 0;
00616                 np->phys[0] = 1;                /* Standard for this chip. */
00617                 for (phy = 1; phy < 32 && phy_idx < 4; phy++) {
00618                         int mii_status = mdio_read(dev, phy, 1);
00619                         if (mii_status != 0xffff  &&  mii_status != 0x0000) {
00620                                 np->phys[phy_idx++] = phy;
00621                                 np->advertising = mdio_read(dev, phy, 4);
00622                                 printk(KERN_INFO "%s: MII PHY found at address %d, status "
00623                                            "0x%4.4x advertising %4.4x Link %4.4x.\n",
00624                                            dev->name, phy, mii_status, np->advertising,
00625                                            mdio_read(dev, phy, 5));
00626                         }
00627                 }
00628                 np->mii_cnt = phy_idx;
00629         }
00630 
00631         return dev;
00632 }
00633 
00634 static void alloc_rbufs(struct device *dev)
00635 {
00636         struct netdev_private *np = (struct netdev_private *)dev->priv;
00637         int i;
00638 
00639         np->cur_rx = 0;
00640         np->dirty_rx = 0;
00641 
00642         np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
00643         np->rx_head_desc = &np->rx_ring[0];
00644 
00645         for (i = 0; i < RX_RING_SIZE; i++) {
00646                 np->rx_ring[i].rx_status = 0;
00647                 np->rx_ring[i].desc_length = cpu_to_le32(np->rx_buf_sz);
00648                 np->rx_ring[i].next_desc = virt_to_le32desc(&np->rx_ring[i+1]);
00649                 np->rx_skbuff[i] = 0;
00650         }
00651         /* Mark the last entry as wrapping the ring. */
00652         np->rx_ring[i-1].next_desc = virt_to_le32desc(&np->rx_ring[0]);
00653 
00654         /* Fill in the Rx buffers.  Handle allocation failure gracefully. */
00655         for (i = 0; i < RX_RING_SIZE; i++) {
00656                 struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
00657                 np->rx_skbuff[i] = skb;
00658                 if (skb == NULL)
00659                         break;
00660                 skb->dev = dev;                 /* Mark as being used by this device. */
00661                 np->rx_ring[i].addr = virt_to_le32desc(skb->tail);
00662                 np->rx_ring[i].rx_status = cpu_to_le32(DescOwn);
00663         }
00664         np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
00665 }
00666 
00667 static void free_rbufs(struct device* dev)
00668 {
00669         struct netdev_private *np = (struct netdev_private *)dev->priv;
00670         int i;
00671 
00672         /* Free all the skbuffs in the Rx queue. */
00673         for (i = 0; i < RX_RING_SIZE; i++) {
00674                 np->rx_ring[i].rx_status = 0;
00675                 np->rx_ring[i].addr = 0xBADF00D0; /* An invalid address. */
00676                 if (np->rx_skbuff[i]) {
00677 #if LINUX_VERSION_CODE < 0x20100
00678                         np->rx_skbuff[i]->free = 1;
00679 #endif
00680                         dev_kfree_skb(np->rx_skbuff[i]);
00681                 }
00682                 np->rx_skbuff[i] = 0;
00683         }
00684 }
00685 
00686 static void alloc_tbufs(struct device* dev)
00687 {
00688         struct netdev_private *np = (struct netdev_private *)dev->priv;
00689         int i;
00690 
00691         np->tx_full = 0;
00692         np->cur_tx = 0;
00693         np->dirty_tx = 0;
00694 
00695         for (i = 0; i < TX_RING_SIZE; i++) {
00696                 np->tx_skbuff[i] = 0;
00697                 np->tx_ring[i].tx_status = 0;
00698                 np->tx_ring[i].desc_length = cpu_to_le32(0x00e08000);
00699                 np->tx_ring[i].next_desc = virt_to_le32desc(&np->tx_ring[i+1]);
00700                 np->tx_buf[i] = kmalloc(PKT_BUF_SZ, GFP_KERNEL);
00701         }
00702         np->tx_ring[i-1].next_desc = virt_to_le32desc(&np->tx_ring[0]);
00703 }
00704 
00705 static void free_tbufs(struct device *dev)
00706 {
00707         struct netdev_private *np = (struct netdev_private *)dev->priv;
00708         int i;
00709 
00710         for (i = 0; i < TX_RING_SIZE; i++) {
00711                 if (np->tx_skbuff[i])
00712                         dev_kfree_skb(np->tx_skbuff[i]);
00713                 np->tx_skbuff[i] = 0;
00714                 if (np->tx_buf[i]) {
00715                         kfree(np->tx_buf[i]);
00716                         np->tx_buf[i] = 0;
00717                 }
00718         }
00719 }
00720 
00721 static void init_registers(struct device *dev)
00722 {
00723         struct netdev_private *np = (struct netdev_private *)dev->priv;
00724         long ioaddr = dev->base_addr;
00725         int i;
00726 
00727         for (i = 0; i < 6; i++)
00728                 writeb(dev->dev_addr[i], ioaddr + StationAddr + i);
00729 
00730         /* Initialize other registers. */
00731         writew(0x0006, ioaddr + PCIBusConfig);  /* Tune configuration??? */
00732         /* Configure the FIFO thresholds. */
00733         writeb(0x20, ioaddr + TxConfig);        /* Initial threshold 32 bytes */
00734         np->tx_thresh = 0x20;
00735         np->rx_thresh = 0x60;                           /* Written in set_rx_mode(). */
00736 
00737         if (dev->if_port == 0)
00738                 dev->if_port = np->default_port;
00739 
00740         dev->tbusy = 0;
00741         dev->interrupt = 0;
00742 
00743         writel(virt_to_bus(np->rx_ring), ioaddr + RxRingPtr);
00744         writel(virt_to_bus(np->tx_ring), ioaddr + TxRingPtr);
00745 
00746         set_rx_mode(dev);
00747 
00748         dev->start = 1;
00749 
00750         /* Enable interrupts by setting the interrupt mask. */
00751         writew(IntrRxDone | IntrRxErr | IntrRxEmpty| IntrRxOverflow| IntrRxDropped|
00752                    IntrTxDone | IntrTxAbort | IntrTxUnderrun |
00753                    IntrPCIErr | IntrStatsMax | IntrLinkChange | IntrMIIChange,
00754                    ioaddr + IntrEnable);
00755 
00756         np->chip_cmd = CmdStart|CmdTxOn|CmdRxOn|CmdNoTxPoll;
00757         if (np->duplex_lock)
00758                 np->chip_cmd |= CmdFDuplex;
00759         writew(np->chip_cmd, ioaddr + ChipCmd);
00760 
00761         check_duplex(dev);
00762         /* The LED outputs of various MII xcvrs should be configured.  */
00763         /* For NS or Mison phys, turn on bit 1 in register 0x17 */
00764         /* For ESI phys, turn on bit 7 in register 0x17. */
00765         mdio_write(dev, np->phys[0], 0x17, mdio_read(dev, np->phys[0], 0x17) |
00766                            (np->drv_flags & HasESIPhy) ? 0x0080 : 0x0001);
00767 }
00768 
00769 
00770 /* Read and write over the MII Management Data I/O (MDIO) interface. */
00771 
00772 static int mdio_read(struct device *dev, int phy_id, int regnum)
00773 {
00774         long ioaddr = dev->base_addr;
00775         int boguscnt = 1024;
00776 
00777         /* Wait for a previous command to complete. */
00778         while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)
00779                 ;
00780         writeb(0x00, ioaddr + MIICmd);
00781         writeb(phy_id, ioaddr + MIIPhyAddr);
00782         writeb(regnum, ioaddr + MIIRegAddr);
00783         writeb(0x40, ioaddr + MIICmd);                  /* Trigger read */
00784         boguscnt = 1024;
00785         while ((readb(ioaddr + MIICmd) & 0x40) && --boguscnt > 0)
00786                 ;
00787         return readw(ioaddr + MIIData);
00788 }
00789 
00790 static void mdio_write(struct device *dev, int phy_id, int regnum, int value)
00791 {
00792         struct netdev_private *np = (struct netdev_private *)dev->priv;
00793         long ioaddr = dev->base_addr;
00794         int boguscnt = 1024;
00795 
00796         if (phy_id == np->phys[0]) {
00797                 switch (regnum) {
00798                 case 0:                                 /* Is user forcing speed/duplex? */
00799                         if (value & 0x9000)     /* Autonegotiation. */
00800                                 np->duplex_lock = 0;
00801                         else
00802                                 np->full_duplex = (value & 0x0100) ? 1 : 0;
00803                         break;
00804                 case 4:
00805                         np->advertising = value;
00806                         break;
00807                 }
00808         }
00809         /* Wait for a previous command to complete. */
00810         while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)
00811                 ;
00812         writeb(0x00, ioaddr + MIICmd);
00813         writeb(phy_id, ioaddr + MIIPhyAddr);
00814         writeb(regnum, ioaddr + MIIRegAddr);
00815         writew(value, ioaddr + MIIData);
00816         writeb(0x20, ioaddr + MIICmd);                  /* Trigger write. */
00817         return;
00818 }
00819 
00820 
00821 static int netdev_open(struct device *dev)
00822 {
00823         struct netdev_private *np = (struct netdev_private *)dev->priv;
00824         long ioaddr = dev->base_addr;
00825 
00826         /* Reset the chip. */
00827         writew(CmdReset, ioaddr + ChipCmd);
00828 
00829         MOD_INC_USE_COUNT;
00830 
00831         if (request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev)) {
00832                 MOD_DEC_USE_COUNT;
00833                 return -EAGAIN;
00834         }
00835 
00836         if (debug > 1)
00837                 printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
00838                            dev->name, dev->irq);
00839 
00840         alloc_rbufs(dev);
00841         alloc_tbufs(dev);
00842 
00843         init_registers(dev);
00844 
00845         if (debug > 2)
00846                 printk(KERN_DEBUG "%s: Done netdev_open(), status %4.4x "
00847                            "MII status: %4.4x.\n",
00848                            dev->name, readw(ioaddr + ChipCmd),
00849                            mdio_read(dev, np->phys[0], 1));
00850 
00851         /* Set the timer to check for link beat. */
00852         init_timer(&np->timer);
00853         np->timer.expires = jiffies + 2;
00854         np->timer.data = (unsigned long)dev;
00855         np->timer.function = &netdev_timer;                             /* timer handler */
00856         add_timer(&np->timer);
00857 
00858         return 0;
00859 }
00860 
00861 static void check_duplex(struct device *dev)
00862 {
00863         struct netdev_private *np = (struct netdev_private *)dev->priv;
00864         long ioaddr = dev->base_addr;
00865         int mii_reg5 = mdio_read(dev, np->phys[0], 5);
00866         int negotiated = mii_reg5 & np->advertising;
00867         int duplex;
00868 
00869         if (np->duplex_lock  ||  mii_reg5 == 0xffff)
00870                 return;
00871         duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
00872         if (np->full_duplex != duplex) {
00873                 np->full_duplex = duplex;
00874                 if (debug)
00875                         printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d link"
00876                                    " partner capability of %4.4x.\n", dev->name,
00877                                    duplex ? "full" : "half", np->phys[0], mii_reg5);
00878                 if (duplex)
00879                         np->chip_cmd |= CmdFDuplex;
00880                 else
00881                         np->chip_cmd &= ~CmdFDuplex;
00882                 writew(np->chip_cmd, ioaddr + ChipCmd);
00883         }
00884 }
00885 
00886 static void netdev_timer(unsigned long data)
00887 {
00888         struct device *dev = (struct device *)data;
00889         struct netdev_private *np = (struct netdev_private *)dev->priv;
00890         long ioaddr = dev->base_addr;
00891         int next_tick = 10*HZ;
00892 
00893         if (debug > 3) {
00894                 printk(KERN_DEBUG "%s: VIA Rhine monitor tick, status %4.4x.\n",
00895                            dev->name, readw(ioaddr + IntrStatus));
00896         }
00897         if (test_bit(0, (void*)&dev->tbusy) != 0
00898                 && np->cur_tx - np->dirty_tx > 1
00899                 && jiffies - dev->trans_start > TX_TIMEOUT)
00900                 tx_timeout(dev);
00901 
00902         check_duplex(dev);
00903 
00904         np->timer.expires = jiffies + next_tick;
00905         add_timer(&np->timer);
00906 }
00907 
00908 static void tx_timeout(struct device *dev)
00909 {
00910         struct netdev_private *np = (struct netdev_private *)dev->priv;
00911         struct pci_dev *pdev = pci_find_slot(np->pci_bus, np->pci_devfn);
00912         long ioaddr = dev->base_addr;
00913 
00914         printk(KERN_WARNING "%s: Transmit timed out, status %4.4x, PHY status "
00915                    "%4.4x, resetting...\n",
00916                    dev->name, readw(ioaddr + IntrStatus),
00917                    mdio_read(dev, np->phys[0], 1));
00918 
00919         dev->if_port = 0;
00920 
00921         /* protect against concurrent rx interrupts */
00922         disable_irq(pdev->irq);
00923 
00924         /* Reset the chip. */
00925         writew(CmdReset, ioaddr + ChipCmd);
00926 
00927         /* clear all descriptors */
00928         free_tbufs(dev);
00929         free_rbufs(dev);
00930         alloc_tbufs(dev);
00931         alloc_rbufs(dev);
00932 
00933         /* Reinitialize the hardware. */
00934         wait_for_reset(dev, dev->name);
00935         init_registers(dev);
00936 
00937         enable_irq(pdev->irq);
00938 
00939         dev->trans_start = jiffies;
00940         np->stats.tx_errors++;
00941 
00942         /* wake queue */
00943         clear_bit(0, (void*)&dev->tbusy);
00944         mark_bh(NET_BH);
00945 
00946         return;
00947 }
00948 
00949 static int start_tx(struct sk_buff *skb, struct device *dev)
00950 {
00951         struct netdev_private *np = (struct netdev_private *)dev->priv;
00952         unsigned entry;
00953 
00954         /* Block a timer-based transmit from overlapping.  This could better be
00955            done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
00956         if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
00957                 /* This watchdog code is redundant with the media monitor timer. */
00958                 if (jiffies - dev->trans_start > TX_TIMEOUT)
00959                         tx_timeout(dev);
00960                 return 1;
00961         }
00962 
00963         /* Explicitly flush packet data cache lines here. */
00964 
00965         /* Caution: the write order is important here, set the descriptor word
00966            with the "ownership" bit last.  No SMP locking is needed if the
00967            cur_tx is incremented after the descriptor is consistent.  */
00968 
00969         /* Calculate the next Tx descriptor entry. */
00970         entry = np->cur_tx % TX_RING_SIZE;
00971 
00972         if (skb->len < ETH_ZLEN) {
00973                 skb = skb_padto(skb, ETH_ZLEN);
00974                 if(skb == NULL)
00975                 {
00976                         dev->tbusy = 0;
00977                         return 0;
00978                 }
00979         }
00980 
00981         np->tx_skbuff[entry] = skb;
00982 
00983         if ((np->drv_flags & ReqTxAlign)  && ((long)skb->data & 3)) {
00984                 /* Must use alignment buffer. */
00985                 if (np->tx_buf[entry] == NULL &&
00986                         (np->tx_buf[entry] = kmalloc(PKT_BUF_SZ, GFP_KERNEL)) == NULL)
00987                         return 1;
00988                 memcpy(np->tx_buf[entry], skb->data, skb->len);
00989                 np->tx_ring[entry].addr = virt_to_le32desc(np->tx_buf[entry]);
00990         } else
00991                 np->tx_ring[entry].addr = virt_to_le32desc(skb->data);
00992 
00993         np->tx_ring[entry].desc_length =
00994                 cpu_to_le32(0x00E08000 | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN));
00995         np->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);
00996 
00997         np->cur_tx++;
00998 
00999         /* Explicitly flush descriptor cache lines here. */
01000 
01001         /* Wake the potentially-idle transmit channel. */
01002         writew(CmdTxDemand | np->chip_cmd, dev->base_addr + ChipCmd);
01003 
01004         if (np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 1)
01005                 clear_bit(0, (void*)&dev->tbusy);               /* Typical path */
01006         else
01007                 np->tx_full = 1;
01008         dev->trans_start = jiffies;
01009 
01010         if (debug > 4) {
01011                 printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
01012                            dev->name, np->cur_tx, entry);
01013         }
01014         return 0;
01015 }
01016 
01017 /* The interrupt handler does all of the Rx thread work and cleans up
01018    after the Tx thread. */
01019 static void intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
01020 {
01021         struct device *dev = (struct device *)dev_instance;
01022         struct netdev_private *np = (void *)dev->priv;
01023         long ioaddr = dev->base_addr;
01024         int boguscnt = max_interrupt_work;
01025 
01026         do {
01027                 u32 intr_status = readw(ioaddr + IntrStatus);
01028 
01029                 /* Acknowledge all of the current interrupt sources ASAP. */
01030                 writew(intr_status & 0xffff, ioaddr + IntrStatus);
01031 
01032                 if (debug > 4)
01033                         printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n",
01034                                    dev->name, intr_status);
01035 
01036                 if (intr_status == 0)
01037                         break;
01038 
01039                 if (intr_status & (IntrRxDone | IntrRxErr | IntrRxDropped |
01040                                                    IntrRxWakeUp | IntrRxEmpty | IntrRxNoBuf))
01041                         netdev_rx(dev);
01042 
01043                 for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
01044                         int entry = np->dirty_tx % TX_RING_SIZE;
01045                         int txstatus = le32_to_cpu(np->tx_ring[entry].tx_status);
01046                         if (txstatus & DescOwn)
01047                                 break;
01048                         if (debug > 6)
01049                                 printk(KERN_DEBUG " Tx scavenge %d status %4.4x.\n",
01050                                            entry, txstatus);
01051                         if (txstatus & 0x8000) {
01052                                 if (debug > 1)
01053                                         printk(KERN_DEBUG "%s: Transmit error, Tx status %4.4x.\n",
01054                                                    dev->name, txstatus);
01055                                 np->stats.tx_errors++;
01056                                 if (txstatus & 0x0400) np->stats.tx_carrier_errors++;
01057                                 if (txstatus & 0x0200) np->stats.tx_window_errors++;
01058                                 if (txstatus & 0x0100) np->stats.tx_aborted_errors++;
01059                                 if (txstatus & 0x0080) np->stats.tx_heartbeat_errors++;
01060                                 if (txstatus & 0x0002) np->stats.tx_fifo_errors++;
01061 #ifdef ETHER_STATS
01062                                 if (txstatus & 0x0100) np->stats.collisions16++;
01063 #endif
01064                                 /* Transmitter restarted in 'abnormal' handler. */
01065                         } else {
01066 #ifdef ETHER_STATS
01067                                 if (txstatus & 0x0001) np->stats.tx_deferred++;
01068 #endif
01069                                 np->stats.collisions += (txstatus >> 3) & 15;
01070 #if defined(NETSTATS_VER2)
01071                                 np->stats.tx_bytes += np->tx_skbuff[entry]->len;
01072 #endif
01073                                 np->stats.tx_packets++;
01074                         }
01075                         /* Free the original skb. */
01076                         dev_kfree_skb(np->tx_skbuff[entry]);
01077                         np->tx_skbuff[entry] = 0;
01078                 }
01079                 if (np->tx_full && dev->tbusy
01080                         && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
01081                         /* The ring is no longer full, clear tbusy. */
01082                         np->tx_full = 0;
01083                         clear_bit(0, (void*)&dev->tbusy);
01084                         mark_bh(