📄 3c501.c
字号:
/* 3c501.c: A 3Com 3c501 Ethernet driver for Linux. *//* Written 1992,1993,1994 Donald Becker Copyright 1993 United States Government as represented by the Director, National Security Agency. This software may be used and distributed according to the terms of the GNU Public License, incorporated herein by reference. This is a device driver for the 3Com Etherlink 3c501. Do not purchase this card, even as a joke. It's performance is horrible, and it breaks in many ways. The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O Center of Excellence in Space Data and Information Sciences Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771 Fixed (again!) the missing interrupt locking on TX/RX shifting. Alan Cox <Alan.Cox@linux.org> Removed calls to init_etherdev since they are no longer needed, and cleaned up modularization just a bit. The driver still allows only the default address for cards when loaded as a module, but that's really less braindead than anyone using a 3c501 board. :) 19950208 (invid@msen.com) Added traps for interrupts hitting the window as we clear and TX load the board. Now getting 150K/second FTP with a 3c501 card. Still playing with a TX-TX optimisation to see if we can touch 180-200K/second as seems theoretically maximum. 19950402 Alan Cox <Alan.Cox@linux.org> Cleaned up for 2.3.x because we broke SMP now. 20000208 Alan Cox <alan@redhat.com> *//** * DOC: 3c501 Card Notes * * Some notes on this thing if you have to hack it. [Alan] * * Some documentation is available from 3Com. Due to the boards age * standard responses when you ask for this will range from 'be serious' * to 'give it to a museum'. The documentation is incomplete and mostly * of historical interest anyway. * * The basic system is a single buffer which can be used to receive or * transmit a packet. A third command mode exists when you are setting * things up. * * If it's transmitting it's not receiving and vice versa. In fact the * time to get the board back into useful state after an operation is * quite large. * * The driver works by keeping the board in receive mode waiting for a * packet to arrive. When one arrives it is copied out of the buffer * and delivered to the kernel. The card is reloaded and off we go. * * When transmitting lp->txing is set and the card is reset (from * receive mode) [possibly losing a packet just received] to command * mode. A packet is loaded and transmit mode triggered. The interrupt * handler runs different code for transmit interrupts and can handle * returning to receive mode or retransmissions (yes you have to help * out with those too). * * DOC: Problems * * There are a wide variety of undocumented error returns from the card * and you basically have to kick the board and pray if they turn up. Most * only occur under extreme load or if you do something the board doesn't * like (eg touching a register at the wrong time). * * The driver is less efficient than it could be. It switches through * receive mode even if more transmits are queued. If this worries you buy * a real Ethernet card. * * The combination of slow receive restart and no real multicast * filter makes the board unusable with a kernel compiled for IP * multicasting in a real multicast environment. That's down to the board, * but even with no multicast programs running a multicast IP kernel is * in group 224.0.0.1 and you will therefore be listening to all multicasts. * One nv conference running over that Ethernet and you can give up. * */static const char *version = "3c501.c: 2000/02/08 Alan Cox (alan@redhat.com).\n";/* * Braindamage remaining: * The 3c501 board. */#include <linux/module.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/ptrace.h>#include <linux/fcntl.h>#include <linux/ioport.h>#include <linux/interrupt.h>#include <linux/malloc.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/config.h> /* for CONFIG_IP_MULTICAST */#include <linux/spinlock.h>#include <asm/bitops.h>#include <asm/io.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <linux/init.h>/* A zero-terminated list of I/O addresses to be probed. The 3c501 can be at many locations, but here are the popular ones. */static unsigned int netcard_portlist[] __initdata = { 0x280, 0x300, 0};/* * Index to functions. */int el1_probe(struct net_device *dev);static int el1_probe1(struct net_device *dev, int ioaddr);static int el_open(struct net_device *dev);static void el_timeout(struct net_device *dev);static int el_start_xmit(struct sk_buff *skb, struct net_device *dev);static void el_interrupt(int irq, void *dev_id, struct pt_regs *regs);static void el_receive(struct net_device *dev);static void el_reset(struct net_device *dev);static int el1_close(struct net_device *dev);static struct net_device_stats *el1_get_stats(struct net_device *dev);static void set_multicast_list(struct net_device *dev);#define EL1_IO_EXTENT 16#ifndef EL_DEBUG#define EL_DEBUG 0 /* use 0 for production, 1 for devel., >2 for debug */#endif /* Anything above 5 is wordy death! */static int el_debug = EL_DEBUG;/* * Board-specific info in dev->priv. */struct net_local{ struct net_device_stats stats; int tx_pkt_start; /* The length of the current Tx packet. */ int collisions; /* Tx collisions this packet */ int loading; /* Spot buffer load collisions */ int txing; /* True if card is in TX mode */ spinlock_t lock; /* Serializing lock */};#define RX_STATUS (ioaddr + 0x06)#define RX_CMD RX_STATUS#define TX_STATUS (ioaddr + 0x07)#define TX_CMD TX_STATUS#define GP_LOW (ioaddr + 0x08)#define GP_HIGH (ioaddr + 0x09)#define RX_BUF_CLR (ioaddr + 0x0A)#define RX_LOW (ioaddr + 0x0A)#define RX_HIGH (ioaddr + 0x0B)#define SAPROM (ioaddr + 0x0C)#define AX_STATUS (ioaddr + 0x0E)#define AX_CMD AX_STATUS#define DATAPORT (ioaddr + 0x0F)#define TX_RDY 0x08 /* In TX_STATUS */#define EL1_DATAPTR 0x08#define EL1_RXPTR 0x0A#define EL1_SAPROM 0x0C#define EL1_DATAPORT 0x0f/* * Writes to the ax command register. */#define AX_OFF 0x00 /* Irq off, buffer access on */#define AX_SYS 0x40 /* Load the buffer */#define AX_XMIT 0x44 /* Transmit a packet */#define AX_RX 0x48 /* Receive a packet */#define AX_LOOP 0x0C /* Loopback mode */#define AX_RESET 0x80/* * Normal receive mode written to RX_STATUS. We must intr on short packets * to avoid bogus rx lockups. */#define RX_NORM 0xA8 /* 0x68 == all addrs, 0xA8 only to me. */#define RX_PROM 0x68 /* Senior Prom, uhmm promiscuous mode. */#define RX_MULT 0xE8 /* Accept multicast packets. */#define TX_NORM 0x0A /* Interrupt on everything that might hang the chip *//* * TX_STATUS register. */#define TX_COLLISION 0x02#define TX_16COLLISIONS 0x04#define TX_READY 0x08#define RX_RUNT 0x08#define RX_MISSED 0x01 /* Missed a packet due to 3c501 braindamage. */#define RX_GOOD 0x30 /* Good packet 0x20, or simple overflow 0x10. *//* * The boilerplate probe code. *//** * el1_probe: * @dev: The device structure passed in to probe. * * This can be called from two places. The network layer will probe using * a device structure passed in with the probe information completed. For a * modular driver we use #init_module to fill in our own structure and probe * for it. * * Returns 0 on success. ENXIO if asked not to probe and ENODEV if asked to * probe and failing to find anything. */ int __init el1_probe(struct net_device *dev){ int i; int base_addr = dev->base_addr; SET_MODULE_OWNER(dev); if (base_addr > 0x1ff) /* Check a single specified location. */ return el1_probe1(dev, base_addr); else if (base_addr != 0) /* Don't probe at all. */ return -ENXIO; for (i = 0; netcard_portlist[i]; i++) if (el1_probe1(dev, netcard_portlist[i]) == 0) return 0; return -ENODEV;}/** * el1_probe: * @dev: The device structure to use * @ioaddr: An I/O address to probe at. * * The actual probe. This is iterated over by #el1_probe in order to * check all the applicable device locations. * * Returns 0 for a success, in which case the device is activated, * EAGAIN if the IRQ is in use by another driver, and ENODEV if the * board cannot be found. */static int __init el1_probe1(struct net_device *dev, int ioaddr){ struct net_local *lp; const char *mname; /* Vendor name */ unsigned char station_addr[6]; int autoirq = 0; int i; /* * Reserve I/O resource for exclusive use by this driver */ if (!request_region(ioaddr, EL1_IO_EXTENT, dev->name)) return -ENODEV; /* * Read the station address PROM data from the special port. */ for (i = 0; i < 6; i++) { outw(i, ioaddr + EL1_DATAPTR); station_addr[i] = inb(ioaddr + EL1_SAPROM); } /* * Check the first three octets of the S.A. for 3Com's prefix, or * for the Sager NP943 prefix. */ if (station_addr[0] == 0x02 && station_addr[1] == 0x60 && station_addr[2] == 0x8c) { mname = "3c501"; } else if (station_addr[0] == 0x00 && station_addr[1] == 0x80 && station_addr[2] == 0xC8) { mname = "NP943"; } else { release_region(ioaddr, EL1_IO_EXTENT); return -ENODEV; } /* * We auto-IRQ by shutting off the interrupt line and letting it float * high. */ if (dev->irq < 2) { autoirq_setup(2); inb(RX_STATUS); /* Clear pending interrupts. */ inb(TX_STATUS); outb(AX_LOOP + 1, AX_CMD); outb(0x00, AX_CMD); autoirq = autoirq_report(1); if (autoirq == 0) { printk("%s probe at %#x failed to detect IRQ line.\n", mname, ioaddr); release_region(ioaddr, EL1_IO_EXTENT); return -EAGAIN; } } outb(AX_RESET+AX_LOOP, AX_CMD); /* Loopback mode. */ dev->base_addr = ioaddr; memcpy(dev->dev_addr, station_addr, ETH_ALEN); if (dev->mem_start & 0xf) el_debug = dev->mem_start & 0x7; if (autoirq) dev->irq = autoirq; printk(KERN_INFO "%s: %s EtherLink at %#lx, using %sIRQ %d.\n", dev->name, mname, dev->base_addr, autoirq ? "auto":"assigned ", dev->irq);#ifdef CONFIG_IP_MULTICAST printk(KERN_WARNING "WARNING: Use of the 3c501 in a multicast kernel is NOT recommended.\n");#endif if (el_debug) printk("%s", version); /* * Initialize the device structure. */ dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL); if (dev->priv == NULL) { release_region(ioaddr, EL1_IO_EXTENT); return -ENOMEM; } memset(dev->priv, 0, sizeof(struct net_local)); lp=dev->priv; spin_lock_init(&lp->lock); /* * The EL1-specific entries in the device structure. */ dev->open = &el_open; dev->hard_start_xmit = &el_start_xmit; dev->tx_timeout = &el_timeout; dev->watchdog_timeo = HZ; dev->stop = &el1_close; dev->get_stats = &el1_get_stats; dev->set_multicast_list = &set_multicast_list; /* * Setup the generic properties */ ether_setup(dev); return 0;}/** * el1_open: * @dev: device that is being opened * * When an ifconfig is issued which changes the device flags to include * IFF_UP this function is called. It is only called when the change * occurs, not when the interface remains up. #el1_close will be called * when it goes down. * * Returns 0 for a successful open, or -EAGAIN if someone has run off * with our interrupt line. */static int el_open(struct net_device *dev){ int retval; int ioaddr = dev->base_addr; struct net_local *lp = (struct net_local *)dev->priv; unsigned long flags; if (el_debug > 2) printk("%s: Doing el_open()...", dev->name); if ((retval = request_irq(dev->irq, &el_interrupt, 0, dev->name, dev))) return retval; spin_lock_irqsave(&lp->lock, flags); el_reset(dev); spin_unlock_irqrestore(&lp->lock, flags); lp->txing = 0; /* Board in RX mode */ outb(AX_RX, AX_CMD); /* Aux control, irq and receive enabled */ netif_start_queue(dev); return 0;}/** * el_timeout: * @dev: The 3c501 card that has timed out * * Attempt to restart the board. This is basically a mixture of extreme * violence and prayer * */ static void el_timeout(struct net_device *dev){ struct net_local *lp = (struct net_local *)dev->priv; int ioaddr = dev->base_addr; if (el_debug) printk (KERN_DEBUG "%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n", dev->name, inb(TX_STATUS), inb(AX_STATUS), inb(RX_STATUS)); lp->stats.tx_errors++; outb(TX_NORM, TX_CMD); outb(RX_NORM, RX_CMD); outb(AX_OFF, AX_CMD); /* Just trigger a false interrupt. */ outb(AX_RX, AX_CMD); /* Aux control, irq and receive enabled */ lp->txing = 0; /* Ripped back in to RX */ netif_wake_queue(dev);} /** * el_start_xmit: * @skb: The packet that is queued to be sent * @dev: The 3c501 card we want to throw it down * * Attempt to send a packet to a 3c501 card. There are some interesting * catches here because the 3c501 is an extremely old and therefore * stupid piece of technology. * * If we are handling an interrupt on the other CPU we cannot load a packet * as we may still be attempting to retrieve the last RX packet buffer. * * When a transmit times out we dump the card into control mode and just * start again. It happens enough that it isnt worth logging. * * We avoid holding the spin locks when doing the packet load to the board. * The device is very slow, and its DMA mode is even slower. If we held the * lock while loading 1500 bytes onto the controller we would drop a lot of * serial port characters. This requires we do extra locking, but we have * no real choice. */static int el_start_xmit(struct sk_buff *skb, struct net_device *dev){ struct net_local *lp = (struct net_local *)dev->priv; int ioaddr = dev->base_addr; unsigned long flags; /* * Avoid incoming interrupts between us flipping txing and flipping * mode as the driver assumes txing is a faithful indicator of card * state */ spin_lock_irqsave(&lp->lock, flags); /* * Avoid timer-based retransmission conflicts. */ netif_stop_queue(dev); do
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -