|
|
1.1 ! root 1: /* natsemi.c: A Linux PCI Ethernet driver for the NatSemi DP83810 series. */ ! 2: /* ! 3: Written/copyright 1999-2003 by Donald Becker. ! 4: ! 5: This software may be used and distributed according to the terms of ! 6: the GNU General Public License (GPL), incorporated herein by reference. ! 7: Drivers based on or derived from this code fall under the GPL and must ! 8: retain the authorship, copyright and license notice. This file is not ! 9: a complete program and may only be used when the entire operating ! 10: system is licensed under the GPL. License for under other terms may be ! 11: available. Contact the original author for details. ! 12: ! 13: The original author may be reached as [email protected], or at ! 14: Scyld Computing Corporation ! 15: 914 Bay Ridge Road, Suite 220 ! 16: Annapolis MD 21403 ! 17: ! 18: Support information and updates available at ! 19: http://www.scyld.com/network/natsemi.html ! 20: The information and support mailing lists are based at ! 21: http://www.scyld.com/mailman/listinfo/ ! 22: */ ! 23: ! 24: /* These identify the driver base version and may not be removed. */ ! 25: static const char version1[] = ! 26: "natsemi.c:v1.17a 8/09/2003 Written by Donald Becker <[email protected]>\n"; ! 27: static const char version2[] = ! 28: " http://www.scyld.com/network/natsemi.html\n"; ! 29: /* Updated to recommendations in pci-skeleton v2.11. */ ! 30: ! 31: /* Automatically extracted configuration info: ! 32: probe-func: natsemi_probe ! 33: config-in: tristate 'National Semiconductor DP8381x series PCI Ethernet support' CONFIG_NATSEMI ! 34: ! 35: c-help-name: National Semiconductor DP8381x series PCI Ethernet support ! 36: c-help-symbol: CONFIG_NATSEMI ! 37: c-help: This driver is for the National Semiconductor DP83810 series, ! 38: c-help: including the 83815 chip. ! 39: c-help: Usage information and updates are available from ! 40: c-help: http://www.scyld.com/network/natsemi.html ! 41: */ ! 42: ! 43: /* The user-configurable values. ! 44: These may be modified when a driver module is loaded.*/ ! 45: ! 46: /* Message enable level: 0..31 = no..all messages. See NETIF_MSG docs. */ ! 47: static int debug = 2; ! 48: ! 49: /* Maximum events (Rx packets, etc.) to handle at each interrupt. */ ! 50: static int max_interrupt_work = 20; ! 51: ! 52: /* Maximum number of multicast addresses to filter (vs. rx-all-multicast). ! 53: This chip uses a 512 element hash table based on the Ethernet CRC. ! 54: Some chip versions are reported to have unreliable multicast filter ! 55: circuitry. To work around an observed problem set this value to '0', ! 56: which will immediately switch to Rx-all-multicast. ! 57: */ ! 58: static int multicast_filter_limit = 100; ! 59: ! 60: /* Set the copy breakpoint for the copy-only-tiny-frames scheme. ! 61: Setting to > 1518 effectively disables this feature. ! 62: This chip can only receive into aligned buffers, so architectures such ! 63: as the Alpha AXP might benefit from a copy-align. ! 64: */ ! 65: static int rx_copybreak = 0; ! 66: ! 67: /* Used to pass the media type, etc. ! 68: Both 'options[]' and 'full_duplex[]' should exist for driver ! 69: interoperability, however setting full_duplex[] is deprecated. ! 70: The media type is usually passed in 'options[]'. ! 71: The default is autonegotation for speed and duplex. ! 72: This should rarely be overridden. ! 73: Use option values 0x10/0x20 for 10Mbps, 0x100,0x200 for 100Mbps. ! 74: Use option values 0x10 and 0x100 for forcing half duplex fixed speed. ! 75: Use option values 0x20 and 0x200 for forcing full duplex operation. ! 76: */ ! 77: #define MAX_UNITS 8 /* More are supported, limit only on options */ ! 78: static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1}; ! 79: static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1}; ! 80: ! 81: /* Operational parameters that are set at compile time. */ ! 82: ! 83: /* Keep the ring sizes a power of two for compile efficiency. ! 84: Understand the implications before changing these settings! ! 85: The compiler will convert <unsigned>'%'<2^N> into a bit mask. ! 86: Making the Tx ring too large decreases the effectiveness of channel ! 87: bonding and packet priority. ! 88: Too-large receive rings waste memory and confound network buffer limits. */ ! 89: #define TX_RING_SIZE 16 ! 90: #define TX_QUEUE_LEN 10 /* Limit ring entries actually used, min 4. */ ! 91: #define RX_RING_SIZE 32 ! 92: ! 93: /* Operational parameters that usually are not changed. */ ! 94: /* Time in jiffies before concluding the transmitter is hung. ! 95: Re-autonegotiation may take up to 3 seconds. ! 96: */ ! 97: #define TX_TIMEOUT (6*HZ) ! 98: ! 99: /* Allocation size of Rx buffers with normal sized Ethernet frames. ! 100: Do not change this value without good reason. This is not a limit, ! 101: but a way to keep a consistent allocation size among drivers. ! 102: */ ! 103: #define PKT_BUF_SZ 1536 ! 104: ! 105: #ifndef __KERNEL__ ! 106: #define __KERNEL__ ! 107: #endif ! 108: #if !defined(__OPTIMIZE__) ! 109: #warning You must compile this file with the correct options! ! 110: #warning See the last lines of the source file. ! 111: #error You must compile this driver with "-O". ! 112: #endif ! 113: ! 114: /* Include files, designed to support most kernel versions 2.0.0 and later. */ ! 115: #include <linux/config.h> ! 116: #if defined(CONFIG_SMP) && ! defined(__SMP__) ! 117: #define __SMP__ ! 118: #endif ! 119: #if defined(MODULE) && defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS) ! 120: #define MODVERSIONS ! 121: #endif ! 122: ! 123: #include <linux/version.h> ! 124: #if defined(MODVERSIONS) ! 125: #include <linux/modversions.h> ! 126: #endif ! 127: #include <linux/module.h> ! 128: ! 129: #include <linux/kernel.h> ! 130: #include <linux/string.h> ! 131: #include <linux/timer.h> ! 132: #include <linux/errno.h> ! 133: #include <linux/ioport.h> ! 134: #if LINUX_VERSION_CODE >= 0x20400 ! 135: #include <linux/slab.h> ! 136: #else ! 137: #include <linux/malloc.h> ! 138: #endif ! 139: #include <linux/interrupt.h> ! 140: #include <linux/pci.h> ! 141: #include <linux/netdevice.h> ! 142: #include <linux/etherdevice.h> ! 143: #include <linux/skbuff.h> ! 144: #include <asm/processor.h> /* Processor type for cache alignment. */ ! 145: #include <asm/bitops.h> ! 146: #include <asm/io.h> ! 147: ! 148: #ifdef INLINE_PCISCAN ! 149: #include "k_compat.h" ! 150: #else ! 151: #include "pci-scan.h" ! 152: #include "kern_compat.h" ! 153: #endif ! 154: ! 155: /* Condensed operations for readability. */ ! 156: #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr)) ! 157: #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr)) ! 158: ! 159: #if (LINUX_VERSION_CODE >= 0x20100) && defined(MODULE) ! 160: char kernel_version[] = UTS_RELEASE; ! 161: #endif ! 162: ! 163: MODULE_AUTHOR("Donald Becker <[email protected]>"); ! 164: MODULE_DESCRIPTION("National Semiconductor DP83810 series PCI Ethernet driver"); ! 165: MODULE_LICENSE("GPL"); ! 166: MODULE_PARM(debug, "i"); ! 167: MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i"); ! 168: MODULE_PARM(rx_copybreak, "i"); ! 169: MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i"); ! 170: MODULE_PARM(multicast_filter_limit, "i"); ! 171: MODULE_PARM(max_interrupt_work, "i"); ! 172: MODULE_PARM_DESC(debug, "Driver message level (0-31)"); ! 173: MODULE_PARM_DESC(options, "Force transceiver type or fixed speed+duplex"); ! 174: MODULE_PARM_DESC(max_interrupt_work, ! 175: "Driver maximum events handled per interrupt"); ! 176: MODULE_PARM_DESC(full_duplex, ! 177: "Non-zero to force full duplex, non-negotiated link " ! 178: "(deprecated)."); ! 179: MODULE_PARM_DESC(rx_copybreak, ! 180: "Breakpoint in bytes for copy-only-tiny-frames"); ! 181: MODULE_PARM_DESC(multicast_filter_limit, ! 182: "Multicast addresses before switching to Rx-all-multicast"); ! 183: ! 184: /* ! 185: Theory of Operation ! 186: ! 187: I. Board Compatibility ! 188: ! 189: This driver is designed for National Semiconductor DP83815 PCI Ethernet NIC. ! 190: It also works with other chips in in the DP83810 series. ! 191: The most common board is the Netgear FA311 using the 83815. ! 192: ! 193: II. Board-specific settings ! 194: ! 195: This driver requires the PCI interrupt line to be valid. ! 196: It honors the EEPROM-set values. ! 197: ! 198: III. Driver operation ! 199: ! 200: IIIa. Ring buffers ! 201: ! 202: This driver uses two statically allocated fixed-size descriptor lists ! 203: formed into rings by a branch from the final descriptor to the beginning of ! 204: the list. The ring sizes are set at compile time by RX/TX_RING_SIZE. ! 205: The NatSemi design uses a 'next descriptor' pointer that the driver forms ! 206: into a list, thus rings can be arbitrarily sized. Before changing the ! 207: ring sizes you should understand the flow and cache effects of the ! 208: full/available/empty hysteresis. ! 209: ! 210: IIIb/c. Transmit/Receive Structure ! 211: ! 212: This driver uses a zero-copy receive and transmit scheme. ! 213: The driver allocates full frame size skbuffs for the Rx ring buffers at ! 214: open() time and passes the skb->data field to the chip as receive data ! 215: buffers. When an incoming frame is less than RX_COPYBREAK bytes long, ! 216: a fresh skbuff is allocated and the frame is copied to the new skbuff. ! 217: When the incoming frame is larger, the skbuff is passed directly up the ! 218: protocol stack. Buffers consumed this way are replaced by newly allocated ! 219: skbuffs in a later phase of receives. ! 220: ! 221: The RX_COPYBREAK value is chosen to trade-off the memory wasted by ! 222: using a full-sized skbuff for small frames vs. the copying costs of larger ! 223: frames. New boards are typically used in generously configured machines ! 224: and the underfilled buffers have negligible impact compared to the benefit of ! 225: a single allocation size, so the default value of zero results in never ! 226: copying packets. When copying is done, the cost is usually mitigated by using ! 227: a combined copy/checksum routine. Copying also preloads the cache, which is ! 228: most useful with small frames. ! 229: ! 230: A subtle aspect of the operation is that unaligned buffers are not permitted ! 231: by the hardware. Thus the IP header at offset 14 in an ethernet frame isn't ! 232: longword aligned for further processing. On copies frames are put into the ! 233: skbuff at an offset of "+2", 16-byte aligning the IP header. ! 234: ! 235: IIId. Synchronization ! 236: ! 237: The driver runs as two independent, single-threaded flows of control. One ! 238: is the send-packet routine, which enforces single-threaded use by the ! 239: dev->tbusy flag. The other thread is the interrupt handler, which is single ! 240: threaded by the hardware and interrupt handling software. ! 241: ! 242: The send packet thread has partial control over the Tx ring and 'dev->tbusy' ! 243: flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the next ! 244: queue slot is empty, it clears the tbusy flag when finished otherwise it sets ! 245: the 'lp->tx_full' flag. ! 246: ! 247: The interrupt handler has exclusive control over the Rx ring and records stats ! 248: from the Tx ring. After reaping the stats, it marks the Tx queue entry as ! 249: empty by incrementing the dirty_tx mark. Iff the 'lp->tx_full' flag is set, it ! 250: clears both the tx_full and tbusy flags. ! 251: ! 252: IV. Notes ! 253: ! 254: The older dp83810 chips are so uncommon that support is not relevant. ! 255: No NatSemi datasheet was publically available at the initial release date, ! 256: but the dp83815 has now been published. ! 257: ! 258: IVb. References ! 259: ! 260: http://www.scyld.com/expert/100mbps.html ! 261: http://www.scyld.com/expert/NWay.html ! 262: ! 263: ! 264: IVc. Errata ! 265: ! 266: Qustionable multicast filter implementation. ! 267: The EEPROM format is obviously the result of a chip bug. ! 268: */ ! 269: ! 270: ! 271: ! 272: static void *natsemi_probe1(struct pci_dev *pdev, void *init_dev, ! 273: long ioaddr, int irq, int chip_idx, int find_cnt); ! 274: static int power_event(void *dev_instance, int event); ! 275: #ifdef USE_IO_OPS ! 276: #define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_IO | PCI_ADDR0) ! 277: #else ! 278: #define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_MEM | PCI_ADDR1) ! 279: #endif ! 280: ! 281: static struct pci_id_info pci_id_tbl[] = { ! 282: {"Netgear FA311 (NatSemi DP83815)", ! 283: { 0x0020100B, 0xffffffff, 0xf3111385, 0xffffffff, }, ! 284: PCI_IOTYPE, 256, 0}, ! 285: {"NatSemi DP83815", { 0x0020100B, 0xffffffff }, ! 286: PCI_IOTYPE, 256, 0}, ! 287: {0,}, /* 0 terminated list. */ ! 288: }; ! 289: ! 290: struct drv_id_info natsemi_drv_id = { ! 291: "natsemi", PCI_HOTSWAP, PCI_CLASS_NETWORK_ETHERNET<<8, pci_id_tbl, ! 292: natsemi_probe1, power_event }; ! 293: ! 294: /* Offsets to the device registers. ! 295: Unlike software-only systems, device drivers interact with complex hardware. ! 296: It's not useful to define symbolic names for every register bit in the ! 297: device. Please do not change these names without good reason. ! 298: */ ! 299: enum register_offsets { ! 300: ChipCmd=0x00, ChipConfig=0x04, EECtrl=0x08, PCIBusCfg=0x0C, ! 301: IntrStatus=0x10, IntrMask=0x14, IntrEnable=0x18, ! 302: TxRingPtr=0x20, TxConfig=0x24, ! 303: RxRingPtr=0x30, RxConfig=0x34, ClkRunCtrl=0x3C, ! 304: WOLCmd=0x40, PauseCmd=0x44, RxFilterAddr=0x48, RxFilterData=0x4C, ! 305: BootRomAddr=0x50, BootRomData=0x54, ChipRevReg=0x58, ! 306: StatsCtrl=0x5C, StatsData=0x60, ! 307: RxPktErrs=0x60, RxMissed=0x68, RxCRCErrs=0x64, ! 308: NS_Xcvr_Mgmt = 0x80, NS_MII_BMCR=0x80, NS_MII_BMSR=0x84, ! 309: NS_MII_Advert=0x90, NS_MIILinkPartner=0x94, ! 310: }; ! 311: ! 312: /* Bits in ChipCmd. */ ! 313: enum ChipCmdBits { ! 314: ChipReset=0x100, SoftIntr=0x80, RxReset=0x20, TxReset=0x10, ! 315: RxOff=0x08, RxOn=0x04, TxOff=0x02, TxOn=0x01, ! 316: }; ! 317: ! 318: /* Bits in ChipConfig. */ ! 319: enum ChipConfigBits { ! 320: CfgLinkGood=0x80000000, CfgFDX=0x20000000, ! 321: }; ! 322: ! 323: /* Bits in the interrupt status/mask registers. */ ! 324: enum intr_status_bits { ! 325: IntrRxDone=0x0001, IntrRxIntr=0x0002, IntrRxErr=0x0004, IntrRxEarly=0x0008, ! 326: IntrRxIdle=0x0010, IntrRxOverrun=0x0020, ! 327: IntrTxDone=0x0040, IntrTxIntr=0x0080, IntrTxErr=0x0100, ! 328: IntrTxIdle=0x0200, IntrTxUnderrun=0x0400, ! 329: StatsMax=0x0800, IntrDrv=0x1000, WOLPkt=0x2000, LinkChange=0x4000, ! 330: RxStatusOverrun=0x10000, ! 331: RxResetDone=0x1000000, TxResetDone=0x2000000, ! 332: IntrPCIErr=0x00f00000, ! 333: IntrNormalSummary=0x0251, IntrAbnormalSummary=0xED20, ! 334: }; ! 335: ! 336: /* Bits in the RxMode register. */ ! 337: enum rx_mode_bits { ! 338: AcceptErr=0x20, AcceptRunt=0x10, ! 339: AcceptBroadcast=0xC0000000, ! 340: AcceptMulticast=0x00200000, AcceptAllMulticast=0x20000000, ! 341: AcceptAllPhys=0x10000000, AcceptMyPhys=0x08000000, ! 342: }; ! 343: ! 344: /* The Rx and Tx buffer descriptors. */ ! 345: /* Note that using only 32 bit fields simplifies conversion to big-endian ! 346: architectures. */ ! 347: struct netdev_desc { ! 348: u32 next_desc; ! 349: s32 cmd_status; ! 350: u32 buf_addr; ! 351: u32 software_use; ! 352: }; ! 353: ! 354: /* Bits in network_desc.status */ ! 355: enum desc_status_bits { ! 356: DescOwn=0x80000000, DescMore=0x40000000, DescIntr=0x20000000, ! 357: DescNoCRC=0x10000000, ! 358: DescPktOK=0x08000000, RxTooLong=0x00400000, ! 359: }; ! 360: ! 361: #define PRIV_ALIGN 15 /* Required alignment mask */ ! 362: struct netdev_private { ! 363: /* Descriptor rings first for alignment. */ ! 364: struct netdev_desc rx_ring[RX_RING_SIZE]; ! 365: struct netdev_desc tx_ring[TX_RING_SIZE]; ! 366: struct net_device *next_module; /* Link for devices of this type. */ ! 367: void *priv_addr; /* Unaligned address for kfree */ ! 368: const char *product_name; ! 369: /* The addresses of receive-in-place skbuffs. */ ! 370: struct sk_buff* rx_skbuff[RX_RING_SIZE]; ! 371: /* The saved address of a sent-in-place packet/buffer, for later free(). */ ! 372: struct sk_buff* tx_skbuff[TX_RING_SIZE]; ! 373: struct net_device_stats stats; ! 374: struct timer_list timer; /* Media monitoring timer. */ ! 375: /* Frequently used values: keep some adjacent for cache effect. */ ! 376: int msg_level; ! 377: int chip_id, drv_flags; ! 378: struct pci_dev *pci_dev; ! 379: long in_interrupt; /* Word-long for SMP locks. */ ! 380: int max_interrupt_work; ! 381: int intr_enable; ! 382: unsigned int restore_intr_enable:1; /* Set if temporarily masked. */ ! 383: unsigned int rx_q_empty:1; /* Set out-of-skbuffs. */ ! 384: ! 385: struct netdev_desc *rx_head_desc; ! 386: unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */ ! 387: unsigned int rx_buf_sz; /* Based on MTU+slack. */ ! 388: int rx_copybreak; ! 389: ! 390: unsigned int cur_tx, dirty_tx; ! 391: unsigned int tx_full:1; /* The Tx queue is full. */ ! 392: /* These values keep track of the transceiver/media in use. */ ! 393: unsigned int full_duplex:1; /* Full-duplex operation requested. */ ! 394: unsigned int duplex_lock:1; ! 395: unsigned int medialock:1; /* Do not sense media. */ ! 396: unsigned int default_port; /* Last dev->if_port value. */ ! 397: /* Rx filter. */ ! 398: u32 cur_rx_mode; ! 399: u16 rx_filter[32]; ! 400: int multicast_filter_limit; ! 401: /* FIFO and PCI burst thresholds. */ ! 402: int tx_config, rx_config; ! 403: /* MII transceiver section. */ ! 404: u16 advertising; /* NWay media advertisement */ ! 405: }; ! 406: ! 407: static int eeprom_read(long ioaddr, int location); ! 408: static int mdio_read(struct net_device *dev, int phy_id, int location); ! 409: static void mdio_write(struct net_device *dev, int phy_id, int location, ! 410: int value); ! 411: static int netdev_open(struct net_device *dev); ! 412: static void check_duplex(struct net_device *dev); ! 413: static void netdev_timer(unsigned long data); ! 414: static void tx_timeout(struct net_device *dev); ! 415: static int rx_ring_fill(struct net_device *dev); ! 416: static void init_ring(struct net_device *dev); ! 417: static int start_tx(struct sk_buff *skb, struct net_device *dev); ! 418: static void intr_handler(int irq, void *dev_instance, struct pt_regs *regs); ! 419: static void netdev_error(struct net_device *dev, int intr_status); ! 420: static int netdev_rx(struct net_device *dev); ! 421: static void netdev_error(struct net_device *dev, int intr_status); ! 422: static void set_rx_mode(struct net_device *dev); ! 423: static struct net_device_stats *get_stats(struct net_device *dev); ! 424: static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd); ! 425: static int netdev_close(struct net_device *dev); ! 426: ! 427: ! 428: ! 429: /* A list of our installed devices, for removing the driver module. */ ! 430: static struct net_device *root_net_dev = NULL; ! 431: ! 432: #ifndef MODULE ! 433: int natsemi_probe(struct net_device *dev) ! 434: { ! 435: if (pci_drv_register(&natsemi_drv_id, dev) < 0) ! 436: return -ENODEV; ! 437: printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2); ! 438: return 0; ! 439: } ! 440: #endif ! 441: ! 442: static void *natsemi_probe1(struct pci_dev *pdev, void *init_dev, ! 443: long ioaddr, int irq, int chip_idx, int card_idx) ! 444: { ! 445: struct net_device *dev; ! 446: struct netdev_private *np; ! 447: void *priv_mem; ! 448: int i, option = card_idx < MAX_UNITS ? options[card_idx] : 0; ! 449: int prev_eedata; ! 450: ! 451: dev = init_etherdev(init_dev, 0); ! 452: if (!dev) ! 453: return NULL; ! 454: ! 455: /* Perhaps NETIF_MSG_PROBE */ ! 456: printk(KERN_INFO "%s: %s at 0x%lx, ", ! 457: dev->name, pci_id_tbl[chip_idx].name, ioaddr); ! 458: ! 459: /* Work around the dropped serial bit. */ ! 460: prev_eedata = eeprom_read(ioaddr, 6); ! 461: for (i = 0; i < 3; i++) { ! 462: int eedata = eeprom_read(ioaddr, i + 7); ! 463: dev->dev_addr[i*2] = (eedata << 1) + (prev_eedata >> 15); ! 464: dev->dev_addr[i*2+1] = eedata >> 7; ! 465: prev_eedata = eedata; ! 466: } ! 467: for (i = 0; i < 5; i++) ! 468: printk("%2.2x:", dev->dev_addr[i]); ! 469: printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq); ! 470: ! 471: /* Reset the chip to erase previous misconfiguration. */ ! 472: writel(ChipReset, ioaddr + ChipCmd); ! 473: ! 474: /* Make certain elements e.g. descriptor lists are aligned. */ ! 475: priv_mem = kmalloc(sizeof(*np) + PRIV_ALIGN, GFP_KERNEL); ! 476: /* Check for the very unlikely case of no memory. */ ! 477: if (priv_mem == NULL) ! 478: return NULL; ! 479: ! 480: dev->base_addr = ioaddr; ! 481: dev->irq = irq; ! 482: ! 483: dev->priv = np = (void *)(((long)priv_mem + PRIV_ALIGN) & ~PRIV_ALIGN); ! 484: memset(np, 0, sizeof(*np)); ! 485: np->priv_addr = priv_mem; ! 486: ! 487: np->next_module = root_net_dev; ! 488: root_net_dev = dev; ! 489: ! 490: np->pci_dev = pdev; ! 491: np->chip_id = chip_idx; ! 492: np->drv_flags = pci_id_tbl[chip_idx].drv_flags; ! 493: np->msg_level = (1 << debug) - 1; ! 494: np->rx_copybreak = rx_copybreak; ! 495: np->max_interrupt_work = max_interrupt_work; ! 496: np->multicast_filter_limit = multicast_filter_limit; ! 497: ! 498: if (dev->mem_start) ! 499: option = dev->mem_start; ! 500: ! 501: /* 0x10/0x20/0x100/0x200 set forced speed&duplex modes. */ ! 502: if (option > 0) { ! 503: if (option & 0x220) ! 504: np->full_duplex = 1; ! 505: np->default_port = option & 0x3ff; ! 506: if (np->default_port & 0x330) { ! 507: np->medialock = 1; ! 508: if (np->msg_level & NETIF_MSG_PROBE) ! 509: printk(KERN_INFO " Forcing %dMbs %s-duplex operation.\n", ! 510: (option & 0x300 ? 100 : 10), ! 511: (np->full_duplex ? "full" : "half")); ! 512: writew(((option & 0x300) ? 0x2000 : 0) | /* 100mbps? */ ! 513: (np->full_duplex ? 0x0100 : 0), /* Full duplex? */ ! 514: ioaddr + NS_MII_BMCR); ! 515: } ! 516: } ! 517: if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0) ! 518: np->full_duplex = 1; ! 519: ! 520: if (np->full_duplex) { ! 521: if (np->msg_level & NETIF_MSG_PROBE) ! 522: printk(KERN_INFO "%s: Set to forced full duplex, autonegotiation" ! 523: " disabled.\n", dev->name); ! 524: np->duplex_lock = 1; ! 525: } ! 526: ! 527: /* The chip-specific entries in the device structure. */ ! 528: dev->open = &netdev_open; ! 529: dev->hard_start_xmit = &start_tx; ! 530: dev->stop = &netdev_close; ! 531: dev->get_stats = &get_stats; ! 532: dev->set_multicast_list = &set_rx_mode; ! 533: dev->do_ioctl = &mii_ioctl; ! 534: ! 535: /* Override the PME enable from the EEPROM. */ ! 536: writel(0x8000, ioaddr + ClkRunCtrl); ! 537: ! 538: if ((readl(ioaddr + ChipConfig) & 0xe000) != 0xe000) { ! 539: u32 chip_config = readl(ioaddr + ChipConfig); ! 540: if (np->msg_level & NETIF_MSG_PROBE) ! 541: printk(KERN_INFO "%s: Transceiver default autonegotiation %s " ! 542: "10%s %s duplex.\n", ! 543: dev->name, chip_config & 0x2000 ? "enabled, advertise" ! 544: : "disabled, force", chip_config & 0x4000 ? "0" : "", ! 545: chip_config & 0x8000 ? "full" : "half"); ! 546: } ! 547: if (np->msg_level & NETIF_MSG_PROBE) ! 548: printk(KERN_INFO "%s: Transceiver status 0x%4.4x partner %4.4x.\n", ! 549: dev->name, (int)readl(ioaddr + NS_MII_BMSR), ! 550: (int)readl(ioaddr + NS_MIILinkPartner)); ! 551: ! 552: return dev; ! 553: } ! 554: ! 555: ! 556: /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces. ! 557: The EEPROM code is for the common 93c06/46 EEPROMs with 6 bit addresses. ! 558: Update to the code in other drivers for 8/10 bit addresses. ! 559: */ ! 560: ! 561: /* Delay between EEPROM clock transitions. ! 562: This "delay" forces out buffered PCI writes, which is sufficient to meet ! 563: the timing requirements of most EEPROMs. ! 564: */ ! 565: #define eeprom_delay(ee_addr) readl(ee_addr) ! 566: ! 567: enum EEPROM_Ctrl_Bits { ! 568: EE_ShiftClk=0x04, EE_DataIn=0x01, EE_ChipSelect=0x08, EE_DataOut=0x02, ! 569: }; ! 570: #define EE_Write0 (EE_ChipSelect) ! 571: #define EE_Write1 (EE_ChipSelect | EE_DataIn) ! 572: ! 573: /* The EEPROM commands include the preamble. */ ! 574: enum EEPROM_Cmds { ! 575: EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6), ! 576: }; ! 577: ! 578: static int eeprom_read(long addr, int location) ! 579: { ! 580: int i; ! 581: int retval = 0; ! 582: long ee_addr = addr + EECtrl; ! 583: int read_cmd = location | EE_ReadCmd; ! 584: writel(EE_Write0, ee_addr); ! 585: ! 586: /* Shift the read command bits out. */ ! 587: for (i = 10; i >= 0; i--) { ! 588: short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0; ! 589: writel(dataval, ee_addr); ! 590: eeprom_delay(ee_addr); ! 591: writel(dataval | EE_ShiftClk, ee_addr); ! 592: eeprom_delay(ee_addr); ! 593: } ! 594: writel(EE_ChipSelect, ee_addr); ! 595: eeprom_delay(ee_addr); ! 596: ! 597: for (i = 0; i < 16; i++) { ! 598: writel(EE_ChipSelect | EE_ShiftClk, ee_addr); ! 599: eeprom_delay(ee_addr); ! 600: retval |= (readl(ee_addr) & EE_DataOut) ? 1 << i : 0; ! 601: writel(EE_ChipSelect, ee_addr); ! 602: eeprom_delay(ee_addr); ! 603: } ! 604: ! 605: /* Terminate the EEPROM access. */ ! 606: writel(EE_Write0, ee_addr); ! 607: writel(0, ee_addr); ! 608: return retval; ! 609: } ! 610: ! 611: /* MII transceiver control section. ! 612: The 83815 series has an internal, directly accessable transceiver. ! 613: We present the management registers as if they were MII connected. */ ! 614: ! 615: static int mdio_read(struct net_device *dev, int phy_id, int location) ! 616: { ! 617: if (phy_id == 1 && location < 32) ! 618: return readw(dev->base_addr + NS_Xcvr_Mgmt + (location<<2)); ! 619: else ! 620: return 0xffff; ! 621: } ! 622: ! 623: static void mdio_write(struct net_device *dev, int phy_id, int location, ! 624: int value) ! 625: { ! 626: if (phy_id == 1 && location < 32) ! 627: writew(value, dev->base_addr + NS_Xcvr_Mgmt + (location<<2)); ! 628: } ! 629: ! 630: ! 631: static int netdev_open(struct net_device *dev) ! 632: { ! 633: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 634: long ioaddr = dev->base_addr; ! 635: int i; ! 636: ! 637: /* We do not need to reset the '815 chip. */ ! 638: ! 639: MOD_INC_USE_COUNT; ! 640: ! 641: if (request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev)) { ! 642: MOD_DEC_USE_COUNT; ! 643: return -EAGAIN; ! 644: } ! 645: ! 646: if (np->msg_level & NETIF_MSG_IFUP) ! 647: printk(KERN_DEBUG "%s: netdev_open() irq %d.\n", ! 648: dev->name, dev->irq); ! 649: ! 650: init_ring(dev); ! 651: ! 652: writel(virt_to_bus(np->rx_ring), ioaddr + RxRingPtr); ! 653: writel(virt_to_bus(np->tx_ring), ioaddr + TxRingPtr); ! 654: ! 655: for (i = 0; i < 6; i += 2) { ! 656: writel(i, ioaddr + RxFilterAddr); ! 657: writel(dev->dev_addr[i] + (dev->dev_addr[i+1] << 8), ! 658: ioaddr + RxFilterData); ! 659: } ! 660: ! 661: /* Initialize other registers. */ ! 662: /* See the datasheet for this correction. */ ! 663: if (readl(ioaddr + ChipRevReg) == 0x0203) { ! 664: writew(0x0001, ioaddr + 0xCC); ! 665: writew(0x18C9, ioaddr + 0xE4); ! 666: writew(0x0000, ioaddr + 0xFC); ! 667: writew(0x5040, ioaddr + 0xF4); ! 668: writew(0x008C, ioaddr + 0xF8); ! 669: } ! 670: ! 671: /* Configure the PCI bus bursts and FIFO thresholds. */ ! 672: /* Configure for standard, in-spec Ethernet. */ ! 673: ! 674: if (readl(ioaddr + ChipConfig) & CfgFDX) { /* Full duplex */ ! 675: np->tx_config = 0xD0801002; ! 676: np->rx_config = 0x10000020; ! 677: } else { ! 678: np->tx_config = 0x10801002; ! 679: np->rx_config = 0x0020; ! 680: } ! 681: if (dev->mtu > 1500) ! 682: np->rx_config |= 0x08000000; ! 683: writel(np->tx_config, ioaddr + TxConfig); ! 684: writel(np->rx_config, ioaddr + RxConfig); ! 685: ! 686: if (dev->if_port == 0) ! 687: dev->if_port = np->default_port; ! 688: ! 689: np->in_interrupt = 0; ! 690: ! 691: check_duplex(dev); ! 692: set_rx_mode(dev); ! 693: netif_start_tx_queue(dev); ! 694: ! 695: /* Enable interrupts by setting the interrupt mask. */ ! 696: np->intr_enable = IntrNormalSummary | IntrAbnormalSummary | 0x1f; ! 697: writel(np->intr_enable, ioaddr + IntrMask); ! 698: writel(1, ioaddr + IntrEnable); ! 699: ! 700: writel(RxOn | TxOn, ioaddr + ChipCmd); ! 701: writel(4, ioaddr + StatsCtrl); /* Clear Stats */ ! 702: ! 703: if (np->msg_level & NETIF_MSG_IFUP) ! 704: printk(KERN_DEBUG "%s: Done netdev_open(), status: %x.\n", ! 705: dev->name, (int)readl(ioaddr + ChipCmd)); ! 706: ! 707: /* Set the timer to check for link beat. */ ! 708: init_timer(&np->timer); ! 709: np->timer.expires = jiffies + 3*HZ; ! 710: np->timer.data = (unsigned long)dev; ! 711: np->timer.function = &netdev_timer; /* timer handler */ ! 712: add_timer(&np->timer); ! 713: ! 714: return 0; ! 715: } ! 716: ! 717: static void check_duplex(struct net_device *dev) ! 718: { ! 719: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 720: long ioaddr = dev->base_addr; ! 721: int duplex; ! 722: ! 723: if (np->duplex_lock) ! 724: return; ! 725: duplex = readl(ioaddr + ChipConfig) & 0x20000000 ? 1 : 0; ! 726: if (np->full_duplex != duplex) { ! 727: np->full_duplex = duplex; ! 728: if (np->msg_level & NETIF_MSG_LINK) ! 729: printk(KERN_INFO "%s: Setting %s-duplex based on negotiated link" ! 730: " capability.\n", dev->name, ! 731: duplex ? "full" : "half"); ! 732: if (duplex) { ! 733: np->rx_config |= 0x10000000; ! 734: np->tx_config |= 0xC0000000; ! 735: } else { ! 736: np->rx_config &= ~0x10000000; ! 737: np->tx_config &= ~0xC0000000; ! 738: } ! 739: writel(np->tx_config, ioaddr + TxConfig); ! 740: writel(np->rx_config, ioaddr + RxConfig); ! 741: } ! 742: } ! 743: ! 744: static void netdev_timer(unsigned long data) ! 745: { ! 746: struct net_device *dev = (struct net_device *)data; ! 747: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 748: long ioaddr = dev->base_addr; ! 749: int next_tick = 10*HZ; ! 750: ! 751: if (np->msg_level & NETIF_MSG_TIMER) ! 752: printk(KERN_DEBUG "%s: Driver monitor timer tick, status %8.8x.\n", ! 753: dev->name, (int)readl(ioaddr + IntrStatus)); ! 754: if (np->rx_q_empty) { ! 755: /* Trigger an interrupt to refill. */ ! 756: writel(SoftIntr, ioaddr + ChipCmd); ! 757: } ! 758: /* This will either have a small false-trigger window or will not catch ! 759: tbusy incorrectly set when the queue is empty. */ ! 760: if (netif_queue_paused(dev) && ! 761: np->cur_tx - np->dirty_tx > 1 && ! 762: (jiffies - dev->trans_start) > TX_TIMEOUT) { ! 763: tx_timeout(dev); ! 764: } ! 765: check_duplex(dev); ! 766: np->timer.expires = jiffies + next_tick; ! 767: add_timer(&np->timer); ! 768: } ! 769: ! 770: static void tx_timeout(struct net_device *dev) ! 771: { ! 772: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 773: long ioaddr = dev->base_addr; ! 774: ! 775: printk(KERN_WARNING "%s: Transmit timed out, status %8.8x," ! 776: " resetting...\n", dev->name, (int)readl(ioaddr + TxRingPtr)); ! 777: ! 778: if (np->msg_level & NETIF_MSG_TX_ERR) { ! 779: int i; ! 780: printk(KERN_DEBUG " Rx ring %p: ", np->rx_ring); ! 781: for (i = 0; i < RX_RING_SIZE; i++) ! 782: printk(" %8.8x", (unsigned int)np->rx_ring[i].cmd_status); ! 783: printk("\n"KERN_DEBUG" Tx ring %p: ", np->tx_ring); ! 784: for (i = 0; i < TX_RING_SIZE; i++) ! 785: printk(" %4.4x", np->tx_ring[i].cmd_status); ! 786: printk("\n"); ! 787: } ! 788: ! 789: /* Reinitialize the hardware here. */ ! 790: /* Stop and restart the chip's Tx processes . */ ! 791: ! 792: /* Trigger an immediate transmit demand. */ ! 793: ! 794: dev->trans_start = jiffies; ! 795: np->stats.tx_errors++; ! 796: return; ! 797: } ! 798: ! 799: /* Refill the Rx ring buffers, returning non-zero if not full. */ ! 800: static int rx_ring_fill(struct net_device *dev) ! 801: { ! 802: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 803: unsigned int entry; ! 804: ! 805: for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) { ! 806: entry = np->dirty_rx % RX_RING_SIZE; ! 807: if (np->rx_skbuff[entry] == NULL) { ! 808: struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz); ! 809: np->rx_skbuff[entry] = skb; ! 810: if (skb == NULL) ! 811: return 1; /* Better luck next time. */ ! 812: skb->dev = dev; /* Mark as being used by this device. */ ! 813: np->rx_ring[entry].buf_addr = virt_to_le32desc(skb->tail); ! 814: } ! 815: np->rx_ring[entry].cmd_status = cpu_to_le32(DescIntr | np->rx_buf_sz); ! 816: } ! 817: return 0; ! 818: } ! 819: ! 820: /* Initialize the Rx and Tx rings, along with various 'dev' bits. */ ! 821: static void init_ring(struct net_device *dev) ! 822: { ! 823: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 824: int i; ! 825: ! 826: np->tx_full = 0; ! 827: np->cur_rx = np->cur_tx = 0; ! 828: np->dirty_rx = np->dirty_tx = 0; ! 829: ! 830: /* MAX(PKT_BUF_SZ, dev->mtu + 8); */ ! 831: /* I know you _want_ to change this without understanding it. Don't. */ ! 832: np->rx_buf_sz = (dev->mtu <= 1532 ? PKT_BUF_SZ : dev->mtu + 8); ! 833: np->rx_head_desc = &np->rx_ring[0]; ! 834: ! 835: /* Initialize all Rx descriptors. */ ! 836: for (i = 0; i < RX_RING_SIZE; i++) { ! 837: np->rx_ring[i].next_desc = virt_to_le32desc(&np->rx_ring[i+1]); ! 838: np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn); ! 839: np->rx_skbuff[i] = 0; ! 840: } ! 841: /* Mark the last entry as wrapping the ring. */ ! 842: np->rx_ring[i-1].next_desc = virt_to_le32desc(&np->rx_ring[0]); ! 843: ! 844: for (i = 0; i < TX_RING_SIZE; i++) { ! 845: np->tx_skbuff[i] = 0; ! 846: np->tx_ring[i].next_desc = virt_to_le32desc(&np->tx_ring[i+1]); ! 847: np->tx_ring[i].cmd_status = 0; ! 848: } ! 849: np->tx_ring[i-1].next_desc = virt_to_le32desc(&np->tx_ring[0]); ! 850: ! 851: /* Fill in the Rx buffers. ! 852: Allocation failure just leaves a "negative" np->dirty_rx. */ ! 853: np->dirty_rx = (unsigned int)(0 - RX_RING_SIZE); ! 854: rx_ring_fill(dev); ! 855: ! 856: return; ! 857: } ! 858: ! 859: static int start_tx(struct sk_buff *skb, struct net_device *dev) ! 860: { ! 861: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 862: unsigned int entry; ! 863: ! 864: /* Block a timer-based transmit from overlapping. This happens when ! 865: packets are presumed lost, and we use this check the Tx status. */ ! 866: if (netif_pause_tx_queue(dev) != 0) { ! 867: /* This watchdog code is redundant with the media monitor timer. */ ! 868: if (jiffies - dev->trans_start > TX_TIMEOUT) ! 869: tx_timeout(dev); ! 870: return 1; ! 871: } ! 872: ! 873: /* Note: Ordering is important here, set the field with the ! 874: "ownership" bit last, and only then increment cur_tx. ! 875: No spinlock is needed for either Tx or Rx. ! 876: */ ! 877: ! 878: /* Calculate the next Tx descriptor entry. */ ! 879: entry = np->cur_tx % TX_RING_SIZE; ! 880: ! 881: np->tx_skbuff[entry] = skb; ! 882: ! 883: np->tx_ring[entry].buf_addr = virt_to_le32desc(skb->data); ! 884: np->tx_ring[entry].cmd_status = cpu_to_le32(DescOwn|DescIntr | skb->len); ! 885: np->cur_tx++; ! 886: ! 887: /* For some architectures explicitly flushing np->tx_ring,sizeof(tx_ring) ! 888: and skb->data,skb->len improves performance. */ ! 889: ! 890: if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1) { ! 891: np->tx_full = 1; ! 892: /* Check for a just-cleared queue. */ ! 893: if (np->cur_tx - (volatile unsigned int)np->dirty_tx ! 894: < TX_QUEUE_LEN - 4) { ! 895: np->tx_full = 0; ! 896: netif_unpause_tx_queue(dev); ! 897: } else ! 898: netif_stop_tx_queue(dev); ! 899: } else ! 900: netif_unpause_tx_queue(dev); /* Typical path */ ! 901: /* Wake the potentially-idle transmit channel. */ ! 902: writel(TxOn, dev->base_addr + ChipCmd); ! 903: ! 904: dev->trans_start = jiffies; ! 905: ! 906: if (np->msg_level & NETIF_MSG_TX_QUEUED) { ! 907: printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n", ! 908: dev->name, np->cur_tx, entry); ! 909: } ! 910: return 0; ! 911: } ! 912: ! 913: /* The interrupt handler does all of the Rx thread work and cleans up ! 914: after the Tx thread. */ ! 915: static void intr_handler(int irq, void *dev_instance, struct pt_regs *rgs) ! 916: { ! 917: struct net_device *dev = (struct net_device *)dev_instance; ! 918: struct netdev_private *np; ! 919: long ioaddr; ! 920: int boguscnt; ! 921: ! 922: #ifndef final_version /* Can never occur. */ ! 923: if (dev == NULL) { ! 924: printk (KERN_ERR "Netdev interrupt handler(): IRQ %d for unknown " ! 925: "device.\n", irq); ! 926: return; ! 927: } ! 928: #endif ! 929: ! 930: ioaddr = dev->base_addr; ! 931: np = (struct netdev_private *)dev->priv; ! 932: boguscnt = np->max_interrupt_work; ! 933: ! 934: do { ! 935: u32 intr_status = readl(ioaddr + IntrStatus); ! 936: ! 937: if (intr_status == 0 || intr_status == 0xffffffff) ! 938: break; ! 939: ! 940: /* Acknowledge all of the current interrupt sources ASAP. ! 941: Nominally the read above accomplishes this, but... */ ! 942: writel(intr_status & 0x001ffff, ioaddr + IntrStatus); ! 943: ! 944: if (np->msg_level & NETIF_MSG_INTR) ! 945: printk(KERN_DEBUG "%s: Interrupt, status %8.8x.\n", ! 946: dev->name, intr_status); ! 947: ! 948: if (intr_status & (IntrRxDone | IntrRxIntr)) { ! 949: netdev_rx(dev); ! 950: np->rx_q_empty = rx_ring_fill(dev); ! 951: } ! 952: ! 953: if (intr_status & (IntrRxIdle | IntrDrv)) { ! 954: unsigned int old_dirty_rx = np->dirty_rx; ! 955: if (rx_ring_fill(dev) == 0) ! 956: np->rx_q_empty = 0; ! 957: /* Restart Rx engine iff we did add a buffer. */ ! 958: if (np->dirty_rx != old_dirty_rx) ! 959: writel(RxOn, dev->base_addr + ChipCmd); ! 960: } ! 961: ! 962: for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) { ! 963: int entry = np->dirty_tx % TX_RING_SIZE; ! 964: int tx_status = le32_to_cpu(np->tx_ring[entry].cmd_status); ! 965: if (tx_status & DescOwn) ! 966: break; ! 967: if (np->msg_level & NETIF_MSG_TX_DONE) ! 968: printk(KERN_DEBUG "%s: Transmit done, Tx status %8.8x.\n", ! 969: dev->name, tx_status); ! 970: if (tx_status & 0x08000000) { ! 971: np->stats.tx_packets++; ! 972: #if LINUX_VERSION_CODE > 0x20127 ! 973: np->stats.tx_bytes += np->tx_skbuff[entry]->len; ! 974: #endif ! 975: } else { /* Various Tx errors */ ! 976: if (np->msg_level & NETIF_MSG_TX_ERR) ! 977: printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n", ! 978: dev->name, tx_status); ! 979: if (tx_status & 0x04010000) np->stats.tx_aborted_errors++; ! 980: if (tx_status & 0x02000000) np->stats.tx_fifo_errors++; ! 981: if (tx_status & 0x01000000) np->stats.tx_carrier_errors++; ! 982: if (tx_status & 0x00200000) np->stats.tx_window_errors++; ! 983: np->stats.tx_errors++; ! 984: } ! 985: /* Free the original skb. */ ! 986: dev_free_skb_irq(np->tx_skbuff[entry]); ! 987: np->tx_skbuff[entry] = 0; ! 988: } ! 989: /* Note the 4 slot hysteresis to mark the queue non-full. */ ! 990: if (np->tx_full ! 991: && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) { ! 992: /* The ring is no longer full, allow new TX entries. */ ! 993: np->tx_full = 0; ! 994: netif_resume_tx_queue(dev); ! 995: } ! 996: ! 997: /* Abnormal error summary/uncommon events handlers. */ ! 998: if (intr_status & IntrAbnormalSummary) ! 999: netdev_error(dev, intr_status); ! 1000: ! 1001: if (--boguscnt < 0) { ! 1002: printk(KERN_WARNING "%s: Too much work at interrupt, " ! 1003: "status=0x%4.4x.\n", ! 1004: dev->name, intr_status); ! 1005: np->restore_intr_enable = 1; ! 1006: break; ! 1007: } ! 1008: } while (1); ! 1009: ! 1010: if (np->msg_level & NETIF_MSG_INTR) ! 1011: printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n", ! 1012: dev->name, (int)readl(ioaddr + IntrStatus)); ! 1013: ! 1014: return; ! 1015: } ! 1016: ! 1017: /* This routine is logically part of the interrupt handler, but separated ! 1018: for clarity and better register allocation. */ ! 1019: static int netdev_rx(struct net_device *dev) ! 1020: { ! 1021: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 1022: int entry = np->cur_rx % RX_RING_SIZE; ! 1023: int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx; ! 1024: s32 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status); ! 1025: ! 1026: /* If the driver owns the next entry it's a new packet. Send it up. */ ! 1027: while (desc_status < 0) { /* e.g. & DescOwn */ ! 1028: if (np->msg_level & NETIF_MSG_RX_STATUS) ! 1029: printk(KERN_DEBUG " In netdev_rx() entry %d status was %8.8x.\n", ! 1030: entry, desc_status); ! 1031: if (--boguscnt < 0) ! 1032: break; ! 1033: if ((desc_status & (DescMore|DescPktOK|RxTooLong)) != DescPktOK) { ! 1034: if (desc_status & DescMore) { ! 1035: printk(KERN_WARNING "%s: Oversized(?) Ethernet frame spanned " ! 1036: "multiple buffers, entry %#x status %x.\n", ! 1037: dev->name, np->cur_rx, desc_status); ! 1038: np->stats.rx_length_errors++; ! 1039: } else { ! 1040: /* There was a error. */ ! 1041: if (np->msg_level & NETIF_MSG_RX_ERR) ! 1042: printk(KERN_DEBUG " netdev_rx() Rx error was %8.8x.\n", ! 1043: desc_status); ! 1044: np->stats.rx_errors++; ! 1045: if (desc_status & 0x06000000) np->stats.rx_over_errors++; ! 1046: if (desc_status & 0x00600000) np->stats.rx_length_errors++; ! 1047: if (desc_status & 0x00140000) np->stats.rx_frame_errors++; ! 1048: if (desc_status & 0x00080000) np->stats.rx_crc_errors++; ! 1049: } ! 1050: } else { ! 1051: struct sk_buff *skb; ! 1052: int pkt_len = (desc_status & 0x0fff) - 4; /* Omit CRC size. */ ! 1053: /* Check if the packet is long enough to accept without copying ! 1054: to a minimally-sized skbuff. */ ! 1055: if (pkt_len < np->rx_copybreak ! 1056: && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) { ! 1057: skb->dev = dev; ! 1058: skb_reserve(skb, 2); /* 16 byte align the IP header */ ! 1059: #if defined(HAS_IP_COPYSUM) || (LINUX_VERSION_CODE >= 0x20100) ! 1060: eth_copy_and_sum(skb, np->rx_skbuff[entry]->tail, pkt_len, 0); ! 1061: skb_put(skb, pkt_len); ! 1062: #else ! 1063: memcpy(skb_put(skb, pkt_len), np->rx_skbuff[entry]->tail, ! 1064: pkt_len); ! 1065: #endif ! 1066: } else { ! 1067: skb_put(skb = np->rx_skbuff[entry], pkt_len); ! 1068: np->rx_skbuff[entry] = NULL; ! 1069: } ! 1070: skb->protocol = eth_type_trans(skb, dev); ! 1071: /* W/ hardware checksum: skb->ip_summed = CHECKSUM_UNNECESSARY; */ ! 1072: netif_rx(skb); ! 1073: dev->last_rx = jiffies; ! 1074: np->stats.rx_packets++; ! 1075: #if LINUX_VERSION_CODE > 0x20127 ! 1076: np->stats.rx_bytes += pkt_len; ! 1077: #endif ! 1078: } ! 1079: entry = (++np->cur_rx) % RX_RING_SIZE; ! 1080: np->rx_head_desc = &np->rx_ring[entry]; ! 1081: desc_status = le32_to_cpu(np->rx_head_desc->cmd_status); ! 1082: } ! 1083: ! 1084: /* Refill is now done in the main interrupt loop. */ ! 1085: return 0; ! 1086: } ! 1087: ! 1088: static void netdev_error(struct net_device *dev, int intr_status) ! 1089: { ! 1090: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 1091: long ioaddr = dev->base_addr; ! 1092: ! 1093: if (intr_status & LinkChange) { ! 1094: int chip_config = readl(ioaddr + ChipConfig); ! 1095: if (np->msg_level & NETIF_MSG_LINK) ! 1096: printk(KERN_NOTICE "%s: Link changed: Autonegotiation advertising" ! 1097: " %4.4x partner %4.4x.\n", dev->name, ! 1098: (int)readw(ioaddr + NS_MII_Advert), ! 1099: (int)readw(ioaddr + NS_MIILinkPartner)); ! 1100: if (chip_config & CfgLinkGood) ! 1101: netif_link_up(dev); ! 1102: else ! 1103: netif_link_down(dev); ! 1104: check_duplex(dev); ! 1105: } ! 1106: if (intr_status & StatsMax) { ! 1107: get_stats(dev); ! 1108: } ! 1109: if (intr_status & IntrTxUnderrun) { ! 1110: /* Increase the Tx threshold, 32 byte units. */ ! 1111: if ((np->tx_config & 0x3f) < 62) ! 1112: np->tx_config += 2; /* +64 bytes */ ! 1113: writel(np->tx_config, ioaddr + TxConfig); ! 1114: } ! 1115: if (intr_status & WOLPkt) { ! 1116: int wol_status = readl(ioaddr + WOLCmd); ! 1117: printk(KERN_NOTICE "%s: Link wake-up event %8.8x", ! 1118: dev->name, wol_status); ! 1119: } ! 1120: if (intr_status & (RxStatusOverrun | IntrRxOverrun)) { ! 1121: if (np->msg_level & NETIF_MSG_DRV) ! 1122: printk(KERN_ERR "%s: Rx overflow! ns815 %8.8x.\n", ! 1123: dev->name, intr_status); ! 1124: np->stats.rx_fifo_errors++; ! 1125: } ! 1126: if (intr_status & ~(LinkChange|StatsMax|RxResetDone|TxResetDone| ! 1127: RxStatusOverrun|0xA7ff)) { ! 1128: if (np->msg_level & NETIF_MSG_DRV) ! 1129: printk(KERN_ERR "%s: Something Wicked happened! natsemi %8.8x.\n", ! 1130: dev->name, intr_status); ! 1131: } ! 1132: /* Hmmmmm, it's not clear how to recover from PCI faults. */ ! 1133: if (intr_status & IntrPCIErr) { ! 1134: np->stats.tx_fifo_errors++; ! 1135: np->stats.rx_fifo_errors++; ! 1136: } ! 1137: } ! 1138: ! 1139: static struct net_device_stats *get_stats(struct net_device *dev) ! 1140: { ! 1141: long ioaddr = dev->base_addr; ! 1142: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 1143: int crc_errs = readl(ioaddr + RxCRCErrs); ! 1144: ! 1145: if (crc_errs != 0xffffffff) { ! 1146: /* We need not lock this segment of code for SMP. ! 1147: There is no atomic-add vulnerability for most CPUs, ! 1148: and statistics are non-critical. */ ! 1149: /* The chip only need report frame silently dropped. */ ! 1150: np->stats.rx_crc_errors += crc_errs; ! 1151: np->stats.rx_missed_errors += readl(ioaddr + RxMissed); ! 1152: } ! 1153: ! 1154: return &np->stats; ! 1155: } ! 1156: ! 1157: /* The big-endian AUTODIN II ethernet CRC calculations. ! 1158: See ns820.c for how to fill the table on new chips. ! 1159: */ ! 1160: static unsigned const ethernet_polynomial = 0x04c11db7U; ! 1161: static inline u32 ether_crc(int length, unsigned char *data) ! 1162: { ! 1163: int crc = -1; ! 1164: ! 1165: while(--length >= 0) { ! 1166: unsigned char current_octet = *data++; ! 1167: int bit; ! 1168: for (bit = 0; bit < 8; bit++, current_octet >>= 1) ! 1169: crc = (crc << 1) ^ ! 1170: ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0); ! 1171: } ! 1172: return crc; ! 1173: } ! 1174: ! 1175: static void set_rx_mode(struct net_device *dev) ! 1176: { ! 1177: long ioaddr = dev->base_addr; ! 1178: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 1179: u8 mc_filter[64]; /* Multicast hash filter */ ! 1180: u32 rx_mode; ! 1181: ! 1182: if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */ ! 1183: /* Unconditionally log net taps. */ ! 1184: printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name); ! 1185: rx_mode = AcceptBroadcast | AcceptAllMulticast | AcceptAllPhys ! 1186: | AcceptMyPhys; ! 1187: } else if ((dev->mc_count > np->multicast_filter_limit) ! 1188: || (dev->flags & IFF_ALLMULTI)) { ! 1189: rx_mode = AcceptBroadcast | AcceptAllMulticast | AcceptMyPhys; ! 1190: } else { ! 1191: struct dev_mc_list *mclist; ! 1192: int i; ! 1193: memset(mc_filter, 0, sizeof(mc_filter)); ! 1194: for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; ! 1195: i++, mclist = mclist->next) { ! 1196: int filterbit = ether_crc(ETH_ALEN, mclist->dmi_addr); ! 1197: set_bit(filterbit & 0x1ff, mc_filter); ! 1198: if (np->msg_level & NETIF_MSG_RXFILTER) ! 1199: printk(KERN_INFO "%s: Added filter for %2.2x:%2.2x:%2.2x:" ! 1200: "%2.2x:%2.2x:%2.2x crc %8.8x bit %d.\n", dev->name, ! 1201: mclist->dmi_addr[0], mclist->dmi_addr[1], ! 1202: mclist->dmi_addr[2], mclist->dmi_addr[3], ! 1203: mclist->dmi_addr[4], mclist->dmi_addr[5], ! 1204: filterbit, filterbit & 0x1ff); ! 1205: } ! 1206: rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys; ! 1207: for (i = 0; i < 64; i += 2) { ! 1208: u16 filterword = (mc_filter[i+1]<<8) + mc_filter[i]; ! 1209: if (filterword != np->rx_filter[i>>2]) { ! 1210: writel(0x200 + i, ioaddr + RxFilterAddr); ! 1211: writel(filterword, ioaddr + RxFilterData); ! 1212: np->rx_filter[i>>2] = filterword; ! 1213: } ! 1214: } ! 1215: } ! 1216: writel(rx_mode, ioaddr + RxFilterAddr); ! 1217: np->cur_rx_mode = rx_mode; ! 1218: } ! 1219: ! 1220: static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) ! 1221: { ! 1222: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 1223: u16 *data = (u16 *)&rq->ifr_data; ! 1224: u32 *data32 = (void *)&rq->ifr_data; ! 1225: ! 1226: switch(cmd) { ! 1227: case 0x8947: case 0x89F0: ! 1228: /* SIOCGMIIPHY: Get the address of the PHY in use. */ ! 1229: data[0] = 1; ! 1230: /* Fall Through */ ! 1231: case 0x8948: case 0x89F1: ! 1232: /* SIOCGMIIREG: Read the specified MII register. */ ! 1233: data[3] = mdio_read(dev, data[0] & 0x1f, data[1] & 0x1f); ! 1234: return 0; ! 1235: case 0x8949: case 0x89F2: ! 1236: /* SIOCSMIIREG: Write the specified MII register */ ! 1237: if (!capable(CAP_NET_ADMIN)) ! 1238: return -EPERM; ! 1239: if (data[0] == 1) { ! 1240: u16 miireg = data[1] & 0x1f; ! 1241: u16 value = data[2]; ! 1242: mdio_write(dev, 1, miireg, value); ! 1243: switch (miireg) { ! 1244: case 0: ! 1245: /* Check for autonegotiation on or reset. */ ! 1246: np->duplex_lock = (value & 0x9000) ? 0 : 1; ! 1247: if (np->duplex_lock) ! 1248: np->full_duplex = (value & 0x0100) ? 1 : 0; ! 1249: break; ! 1250: case 4: np->advertising = value; break; ! 1251: } ! 1252: } ! 1253: return 0; ! 1254: case SIOCGPARAMS: ! 1255: data32[0] = np->msg_level; ! 1256: data32[1] = np->multicast_filter_limit; ! 1257: data32[2] = np->max_interrupt_work; ! 1258: data32[3] = np->rx_copybreak; ! 1259: return 0; ! 1260: case SIOCSPARAMS: ! 1261: if (!capable(CAP_NET_ADMIN)) ! 1262: return -EPERM; ! 1263: np->msg_level = data32[0]; ! 1264: np->multicast_filter_limit = data32[1]; ! 1265: np->max_interrupt_work = data32[2]; ! 1266: np->rx_copybreak = data32[3]; ! 1267: return 0; ! 1268: default: ! 1269: return -EOPNOTSUPP; ! 1270: } ! 1271: } ! 1272: ! 1273: static int netdev_close(struct net_device *dev) ! 1274: { ! 1275: long ioaddr = dev->base_addr; ! 1276: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 1277: int i; ! 1278: ! 1279: netif_stop_tx_queue(dev); ! 1280: ! 1281: if (np->msg_level & NETIF_MSG_IFDOWN) { ! 1282: printk(KERN_DEBUG "%s: Shutting down ethercard, status was %4.4x " ! 1283: "Int %2.2x.\n", ! 1284: dev->name, (int)readl(ioaddr + ChipCmd), ! 1285: (int)readl(ioaddr + IntrStatus)); ! 1286: printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n", ! 1287: dev->name, np->cur_tx, np->dirty_tx, np->cur_rx, np->dirty_rx); ! 1288: } ! 1289: ! 1290: /* We don't want the timer to re-start anything. */ ! 1291: del_timer(&np->timer); ! 1292: ! 1293: /* Disable interrupts using the mask. */ ! 1294: writel(0, ioaddr + IntrMask); ! 1295: writel(0, ioaddr + IntrEnable); ! 1296: writel(2, ioaddr + StatsCtrl); /* Freeze Stats */ ! 1297: ! 1298: /* Stop the chip's Tx and Rx processes. */ ! 1299: writel(RxOff | TxOff, ioaddr + ChipCmd); ! 1300: ! 1301: get_stats(dev); ! 1302: ! 1303: #ifdef __i386__ ! 1304: if (np->msg_level & NETIF_MSG_IFDOWN) { ! 1305: printk("\n"KERN_DEBUG" Tx ring at %8.8x:\n", ! 1306: (int)virt_to_bus(np->tx_ring)); ! 1307: for (i = 0; i < TX_RING_SIZE; i++) ! 1308: printk(" #%d desc. %8.8x %8.8x.\n", ! 1309: i, np->tx_ring[i].cmd_status, (u32)np->tx_ring[i].buf_addr); ! 1310: printk("\n"KERN_DEBUG " Rx ring %8.8x:\n", ! 1311: (int)virt_to_bus(np->rx_ring)); ! 1312: for (i = 0; i < RX_RING_SIZE; i++) { ! 1313: printk(KERN_DEBUG " #%d desc. %8.8x %8.8x\n", ! 1314: i, np->rx_ring[i].cmd_status, (u32)np->rx_ring[i].buf_addr); ! 1315: } ! 1316: } ! 1317: #endif /* __i386__ debugging only */ ! 1318: ! 1319: free_irq(dev->irq, dev); ! 1320: ! 1321: /* Free all the skbuffs in the Rx queue. */ ! 1322: for (i = 0; i < RX_RING_SIZE; i++) { ! 1323: np->rx_ring[i].cmd_status = 0; ! 1324: np->rx_ring[i].buf_addr = 0xBADF00D0; /* An invalid address. */ ! 1325: if (np->rx_skbuff[i]) { ! 1326: #if LINUX_VERSION_CODE < 0x20100 ! 1327: np->rx_skbuff[i]->free = 1; ! 1328: #endif ! 1329: dev_free_skb(np->rx_skbuff[i]); ! 1330: } ! 1331: np->rx_skbuff[i] = 0; ! 1332: } ! 1333: for (i = 0; i < TX_RING_SIZE; i++) { ! 1334: if (np->tx_skbuff[i]) ! 1335: dev_free_skb(np->tx_skbuff[i]); ! 1336: np->tx_skbuff[i] = 0; ! 1337: } ! 1338: ! 1339: #if 0 ! 1340: writel(0x0200, ioaddr + ChipConfig); /* Power down Xcvr. */ ! 1341: #endif ! 1342: ! 1343: MOD_DEC_USE_COUNT; ! 1344: ! 1345: return 0; ! 1346: } ! 1347: ! 1348: static int power_event(void *dev_instance, int event) ! 1349: { ! 1350: struct net_device *dev = dev_instance; ! 1351: struct netdev_private *np = (struct netdev_private *)dev->priv; ! 1352: long ioaddr = dev->base_addr; ! 1353: ! 1354: if (np->msg_level & NETIF_MSG_LINK) ! 1355: printk(KERN_DEBUG "%s: Handling power event %d.\n", dev->name, event); ! 1356: switch(event) { ! 1357: case DRV_ATTACH: ! 1358: MOD_INC_USE_COUNT; ! 1359: break; ! 1360: case DRV_SUSPEND: ! 1361: /* Disable interrupts, freeze stats, stop Tx and Rx. */ ! 1362: writel(0, ioaddr + IntrEnable); ! 1363: writel(2, ioaddr + StatsCtrl); ! 1364: writel(RxOff | TxOff, ioaddr + ChipCmd); ! 1365: break; ! 1366: case DRV_RESUME: ! 1367: /* This is incomplete: the open() actions should be repeated. */ ! 1368: set_rx_mode(dev); ! 1369: writel(np->intr_enable, ioaddr + IntrEnable); ! 1370: writel(1, ioaddr + IntrEnable); ! 1371: writel(RxOn | TxOn, ioaddr + ChipCmd); ! 1372: break; ! 1373: case DRV_DETACH: { ! 1374: struct net_device **devp, **next; ! 1375: if (dev->flags & IFF_UP) { ! 1376: /* Some, but not all, kernel versions close automatically. */ ! 1377: dev_close(dev); ! 1378: dev->flags &= ~(IFF_UP|IFF_RUNNING); ! 1379: } ! 1380: unregister_netdev(dev); ! 1381: release_region(dev->base_addr, pci_id_tbl[np->chip_id].io_size); ! 1382: for (devp = &root_net_dev; *devp; devp = next) { ! 1383: next = &((struct netdev_private *)(*devp)->priv)->next_module; ! 1384: if (*devp == dev) { ! 1385: *devp = *next; ! 1386: break; ! 1387: } ! 1388: } ! 1389: if (np->priv_addr) ! 1390: kfree(np->priv_addr); ! 1391: kfree(dev); ! 1392: MOD_DEC_USE_COUNT; ! 1393: break; ! 1394: } ! 1395: } ! 1396: ! 1397: return 0; ! 1398: } ! 1399: ! 1400: ! 1401: #ifdef MODULE ! 1402: int init_module(void) ! 1403: { ! 1404: if (debug >= NETIF_MSG_DRV) /* Emit version even if no cards detected. */ ! 1405: printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2); ! 1406: #ifdef CARDBUS ! 1407: register_driver(ðerdev_ops); ! 1408: return 0; ! 1409: #else ! 1410: return pci_drv_register(&natsemi_drv_id, NULL); ! 1411: #endif ! 1412: } ! 1413: ! 1414: void cleanup_module(void) ! 1415: { ! 1416: struct net_device *next_dev; ! 1417: ! 1418: #ifdef CARDBUS ! 1419: unregister_driver(ðerdev_ops); ! 1420: #else ! 1421: pci_drv_unregister(&natsemi_drv_id); ! 1422: #endif ! 1423: ! 1424: /* No need to check MOD_IN_USE, as sys_delete_module() checks. */ ! 1425: while (root_net_dev) { ! 1426: struct netdev_private *np = (void *)(root_net_dev->priv); ! 1427: unregister_netdev(root_net_dev); ! 1428: iounmap((char *)root_net_dev->base_addr); ! 1429: next_dev = np->next_module; ! 1430: if (np->priv_addr) ! 1431: kfree(np->priv_addr); ! 1432: kfree(root_net_dev); ! 1433: root_net_dev = next_dev; ! 1434: } ! 1435: } ! 1436: ! 1437: #endif /* MODULE */ ! 1438: ! 1439: /* ! 1440: * Local variables: ! 1441: * compile-command: "make KERNVER=`uname -r` natsemi.o" ! 1442: * compile-cmd: "gcc -DMODULE -Wall -Wstrict-prototypes -O6 -c natsemi.c" ! 1443: * simple-compile-command: "gcc -DMODULE -O6 -c natsemi.c" ! 1444: * c-indent-level: 4 ! 1445: * c-basic-offset: 4 ! 1446: * tab-width: 4 ! 1447: * End: ! 1448: */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.