⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 epic100.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* epic100.c: A SMC 83c170 EPIC/100 Fast Ethernet driver for Linux. *//*	Written/copyright 1997-2000 by Donald Becker.	This software may be used and distributed according to the terms of	the GNU General Public License (GPL), incorporated herein by reference.	Drivers based on or derived from this code fall under the GPL and must	retain the authorship, copyright and license notice.  This file is not	a complete program and may only be used when the entire operating	system is licensed under the GPL.	This driver is for the SMC83c170/175 "EPIC" series, as used on the	SMC EtherPower II 9432 PCI adapter, and several CardBus cards.	The author may be reached as becker@scyld.com, or C/O	Scyld Computing Corporation	410 Severn Ave., Suite 210	Annapolis MD 21403	Information and updates available at	http://www.scyld.com/network/epic100.html	---------------------------------------------------------------------		Linux kernel-specific changes:		LK1.1.2 (jgarzik):	* Merge becker version 1.09 (4/08/2000)	LK1.1.3:	* Major bugfix to 1.09 driver (Francis Romieu)		LK1.1.4 (jgarzik):	* Merge becker test version 1.09 (5/29/2000)	LK1.1.5:	* Fix locking (jgarzik)	* Limit 83c175 probe to ethernet-class PCI devices (rgooch)*//* These identify the driver base version and may not be removed. */static const char version[] ="epic100.c:v1.09 5/29/2000 Written by Donald Becker <becker@scyld.com>\n";static const char version2[] ="  http://www.scyld.com/network/epic100.html\n";static const char version3[] =" (unofficial 2.4.x kernel port, version 1.1.5, September 7, 2000)\n";/* The user-configurable values.   These may be modified when a driver module is loaded.*/static int debug = 1;			/* 1 normal messages, 0 quiet .. 7 verbose. *//* Maximum events (Rx packets, etc.) to handle at each interrupt. */static int max_interrupt_work = 32;/* Used to pass the full-duplex flag, etc. */#define MAX_UNITS 8		/* More are supported, limit only on options */static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};/* Set the copy breakpoint for the copy-only-tiny-frames scheme.   Setting to > 1518 effectively disables this feature. */static int rx_copybreak = 0;/* Operational parameters that are set at compile time. *//* Keep the ring sizes a power of two for operational efficiency.   The compiler will convert <unsigned>'%'<2^N> into a bit mask.   Making the Tx ring too large decreases the effectiveness of channel   bonding and packet priority.   There are no ill effects from too-large receive rings. */#define TX_RING_SIZE	16#define TX_QUEUE_LEN	10		/* Limit ring entries actually used.  */#define RX_RING_SIZE	32/* Operational parameters that usually are not changed. *//* Time in jiffies before concluding the transmitter is hung. */#define TX_TIMEOUT  (2*HZ)#define PKT_BUF_SZ		1536			/* Size of each temporary Rx buffer.*//* Bytes transferred to chip before transmission starts. *//* Initial threshold, increased on underflow, rounded down to 4 byte units. */#define TX_FIFO_THRESH 256#define RX_FIFO_THRESH 1		/* 0-3, 0==32, 64,96, or 3==128 bytes  */#if !defined(__OPTIMIZE__)#warning  You must compile this file with the correct options!#warning  See the last lines of the source file.#error You must compile this driver with "-O".#endif#include <linux/version.h>#include <linux/module.h>#if LINUX_VERSION_CODE < 0x20300  &&  defined(MODVERSIONS)#include <linux/modversions.h>#endif#include <linux/kernel.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/errno.h>#include <linux/ioport.h>#include <linux/malloc.h>#include <linux/interrupt.h>#include <linux/pci.h>#include <linux/delay.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <linux/init.h>#include <linux/spinlock.h>#include <asm/bitops.h>#include <asm/io.h>MODULE_AUTHOR("Donald Becker <becker@scyld.com>");MODULE_DESCRIPTION("SMC 83c170 EPIC series Ethernet driver");MODULE_PARM(debug, "i");MODULE_PARM(max_interrupt_work, "i");MODULE_PARM(rx_copybreak, "i");MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");/*				Theory of OperationI. Board CompatibilityThis device driver is designed for the SMC "EPIC/100", the SMCsingle-chip Ethernet controllers for PCI.  This chip is used onthe SMC EtherPower II boards.II. Board-specific settingsPCI bus devices are configured by the system at boot time, so no jumpersneed to be set on the board.  The system BIOS will assign thePCI INTA signal to a (preferably otherwise unused) system IRQ line.Note: Kernel versions earlier than 1.3.73 do not support shared PCIinterrupt lines.III. Driver operationIIIa. Ring buffersIVb. Referenceshttp://www.smsc.com/main/datasheets/83c171.pdfhttp://www.smsc.com/main/datasheets/83c175.pdfhttp://scyld.com/expert/NWay.htmlhttp://www.national.com/pf/DP/DP83840A.htmlIVc. Errata*/enum pci_id_flags_bits {        /* Set PCI command register bits before calling probe1(). */        PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,        /* Read and map the single following PCI BAR. */        PCI_ADDR0=0<<4, PCI_ADDR1=1<<4, PCI_ADDR2=2<<4, PCI_ADDR3=3<<4,        PCI_ADDR_64BITS=0x100, PCI_NO_ACPI_WAKE=0x200, PCI_NO_MIN_LATENCY=0x400,};enum chip_capability_flags { MII_PWRDWN=1, TYPE2_INTR=2, NO_MII=4 };#define EPIC_TOTAL_SIZE 0x100#ifdef USE_IO_OPS#define EPIC_IOTYPE PCI_USES_MASTER|PCI_USES_IO|PCI_ADDR0#else#define EPIC_IOTYPE PCI_USES_MASTER|PCI_USES_MEM|PCI_ADDR1#endif#define virt_to_le32desc(addr)  cpu_to_le32(virt_to_bus(addr))typedef enum {	SMSC_83C170_0,	SMSC_83C170,	SMSC_83C175,} chip_t;struct epic_chip_info {	const char *name;	enum pci_id_flags_bits pci_flags;        int io_size;                            /* Needed for I/O region check or ioremap(). */        int drv_flags;                          /* Driver use, intended as capability flags. */};/* indexed by chip_t */static struct epic_chip_info epic_chip_info[] __devinitdata = {	{ "SMSC EPIC/100 83c170",	 EPIC_IOTYPE, EPIC_TOTAL_SIZE, TYPE2_INTR | NO_MII | MII_PWRDWN },	{ "SMSC EPIC/100 83c170",	 EPIC_IOTYPE, EPIC_TOTAL_SIZE, TYPE2_INTR },	{ "SMSC EPIC/C 83c175",	 EPIC_IOTYPE, EPIC_TOTAL_SIZE, TYPE2_INTR | MII_PWRDWN },};static struct pci_device_id epic_pci_tbl[] __devinitdata = {	{ 0x10B8, 0x0005, 0x1092, 0x0AB4, 0, 0, SMSC_83C170_0 },	{ 0x10B8, 0x0005, PCI_ANY_ID, PCI_ANY_ID, 0, 0, SMSC_83C170 },	{ 0x10B8, 0x0006, PCI_ANY_ID, PCI_ANY_ID,	  PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, SMSC_83C175 },	{ 0,}};MODULE_DEVICE_TABLE (pci, epic_pci_tbl);	#ifndef USE_IO_OPS#undef inb#undef inw#undef inl#undef outb#undef outw#undef outl#define inb readb#define inw readw#define inl readl#define outb writeb#define outw writew#define outl writel#endif/* Offsets to registers, using the (ugh) SMC names. */enum epic_registers {  COMMAND=0, INTSTAT=4, INTMASK=8, GENCTL=0x0C, NVCTL=0x10, EECTL=0x14,  PCIBurstCnt=0x18,  TEST1=0x1C, CRCCNT=0x20, ALICNT=0x24, MPCNT=0x28,	/* Rx error counters. */  MIICtrl=0x30, MIIData=0x34, MIICfg=0x38,  LAN0=64,						/* MAC address. */  MC0=80,						/* Multicast filter table. */  RxCtrl=96, TxCtrl=112, TxSTAT=0x74,  PRxCDAR=0x84, RxSTAT=0xA4, EarlyRx=0xB0, PTxCDAR=0xC4, TxThresh=0xDC,};/* Interrupt register bits, using my own meaningful names. */enum IntrStatus {	TxIdle=0x40000, RxIdle=0x20000, IntrSummary=0x010000,	PCIBusErr170=0x7000, PCIBusErr175=0x1000, PhyEvent175=0x8000,	RxStarted=0x0800, RxEarlyWarn=0x0400, CntFull=0x0200, TxUnderrun=0x0100,	TxEmpty=0x0080, TxDone=0x0020, RxError=0x0010,	RxOverflow=0x0008, RxFull=0x0004, RxHeader=0x0002, RxDone=0x0001,};enum CommandBits {	StopRx=1, StartRx=2, TxQueued=4, RxQueued=8,	StopTxDMA=0x20, StopRxDMA=0x40, RestartTx=0x80,};static u16 media2miictl[16] = {	0, 0x0C00, 0x0C00, 0x2000,  0x0100, 0x2100, 0, 0,	0, 0, 0, 0,  0, 0, 0, 0 };/* The EPIC100 Rx and Tx buffer descriptors. */struct epic_tx_desc {	u32 txstatus;	u32 bufaddr;	u32 buflength;	u32 next;};struct epic_rx_desc {	u32 rxstatus;	u32 bufaddr;	u32 buflength;	u32 next;};enum desc_status_bits {	DescOwn=0x8000,};struct epic_private {	/* Tx and Rx rings first so that they remain paragraph aligned. */	struct epic_rx_desc rx_ring[RX_RING_SIZE];	struct epic_tx_desc tx_ring[TX_RING_SIZE];	/* The saved address of a sent-in-place packet/buffer, for skfree(). */	struct sk_buff* tx_skbuff[TX_RING_SIZE];	/* The addresses of receive-in-place skbuffs. */	struct sk_buff* rx_skbuff[RX_RING_SIZE];	/* Ring pointers. */	spinlock_t lock;				/* Group with Tx control cache line. */	unsigned int cur_tx, dirty_tx;	struct descriptor  *last_tx_desc;	unsigned int cur_rx, dirty_rx;	unsigned int rx_buf_sz;				/* Based on MTU+slack. */	struct descriptor  *last_rx_desc;	long last_rx_time;					/* Last Rx, in jiffies. */	struct pci_dev *pci_dev;			/* PCI bus location. */	int chip_flags;	struct net_device_stats stats;	struct timer_list timer;			/* Media selection timer. */	int tx_threshold;	unsigned char mc_filter[8];	signed char phys[4];				/* MII device addresses. */	u16 advertising;					/* NWay media advertisement */	int mii_phy_cnt;	unsigned int tx_full:1;				/* The Tx queue is full. */	unsigned int full_duplex:1;			/* Current duplex setting. */	unsigned int force_fd:1;			/* Full-duplex operation requested. */	unsigned int default_port:4;		/* Last dev->if_port value. */	unsigned int media2:4;				/* Secondary monitored media port. */	unsigned int medialock:1;			/* Don't sense media type. */	unsigned int mediasense:1;			/* Media sensing in progress. */};static int epic_open(struct net_device *dev);static int read_eeprom(long ioaddr, int location);static int mdio_read(long ioaddr, int phy_id, int location);static void mdio_write(long ioaddr, int phy_id, int location, int value);static void epic_restart(struct net_device *dev);static void epic_timer(unsigned long data);static void epic_tx_timeout(struct net_device *dev);static void epic_init_ring(struct net_device *dev);static int epic_start_xmit(struct sk_buff *skb, struct net_device *dev);static int epic_rx(struct net_device *dev);static void epic_interrupt(int irq, void *dev_instance, struct pt_regs *regs);static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);static int epic_close(struct net_device *dev);static struct net_device_stats *epic_get_stats(struct net_device *dev);static void set_rx_mode(struct net_device *dev);static int __devinit epic_init_one (struct pci_dev *pdev,				    const struct pci_device_id *ent){	static int card_idx = -1;	static int printed_version = 0;	struct net_device *dev;	struct epic_private *ep;	int i, option = 0, duplex = 0;	struct epic_chip_info *ci = &epic_chip_info[ent->driver_data];	long ioaddr;	int chip_idx = (int) ent->driver_data;	card_idx++;		if (!printed_version++)		printk (KERN_INFO "%s" KERN_INFO "%s" KERN_INFO "%s",			version, version2, version3);		if ((pci_resource_len(pdev, 0) < ci->io_size) ||	    (pci_resource_len(pdev, 1) < ci->io_size)) {		printk (KERN_ERR "card %d: no PCI region space\n", card_idx);		return -ENODEV;	}		i = pci_enable_device(pdev);	if (i)		return i;			pci_set_master(pdev);	dev = init_etherdev(NULL, sizeof (*ep));	if (!dev) {		printk (KERN_ERR "card %d: no memory for eth device\n", card_idx);		return -ENOMEM;	}	/* request 100% of both regions 0 and 1, just to make	 * sure noone else steals our regions while we are talking	 * to them */	if (!request_region (pci_resource_start (pdev, 0),			     pci_resource_len (pdev, 0), dev->name)) {		printk (KERN_ERR "epic100 %d: I/O region busy\n", card_idx);		goto err_out_free_netdev;	}	if (!request_mem_region (pci_resource_start (pdev, 1),				 pci_resource_len (pdev, 1), dev->name)) {		printk (KERN_ERR "epic100 %d: I/O region busy\n", card_idx);		goto err_out_free_pio;	}#ifdef USE_IO_OPS	ioaddr = pci_resource_start (pdev, 0);#else	ioaddr = pci_resource_start (pdev, 1);	ioaddr = (long) ioremap (ioaddr, pci_resource_len (pdev, 1));	if (!ioaddr) {		printk (KERN_ERR "epic100 %d: ioremap failed\n", card_idx);		goto err_out_free_mmio;	}#endif	if (dev->mem_start) {		option = dev->mem_start;		duplex = (dev->mem_start & 16) ? 1 : 0;	} else if (card_idx >= 0  &&  card_idx < MAX_UNITS) {		if (options[card_idx] >= 0)			option = options[card_idx];		if (full_duplex[card_idx] >= 0)			duplex = full_duplex[card_idx];	}	pdev->driver_data = dev;	dev->base_addr = ioaddr;	dev->irq = pdev->irq;	ep = dev->priv;	ep->pci_dev = pdev;	ep->chip_flags = ci->drv_flags;	spin_lock_init (&ep->lock);	printk(KERN_INFO "%s: %s at %#lx, IRQ %d, ",		   dev->name, ci->name, ioaddr, dev->irq);	/* Bring the chip out of low-power mode. */	outl(0x4200, ioaddr + GENCTL);	/* Magic?!  If we don't set this bit the MII interface won't work. */	outl(0x0008, ioaddr + TEST1);	/* Turn on the MII transceiver. */	outl(0x12, ioaddr + MIICfg);	if (chip_idx == 1)		outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);	outl(0x0200, ioaddr + GENCTL);	/* This could also be read from the EEPROM. */	for (i = 0; i < 3; i++)		((u16 *)dev->dev_addr)[i] = le16_to_cpu(inw(ioaddr + LAN0 + i*4));	for (i = 0; i < 5; i++)		printk("%2.2x:", dev->dev_addr[i]);	printk("%2.2x.\n", dev->dev_addr[i]);	if (debug > 2) {		printk(KERN_DEBUG "%s: EEPROM contents\n", dev->name);		for (i = 0; i < 64; i++)			printk(" %4.4x%s", read_eeprom(ioaddr, i),				   i % 16 == 15 ? "\n" : "");	}	/* Find the connected MII xcvrs.	   Doing this in open() would allow detecting external xcvrs later, but	   takes much time and no cards have external MII. */	{		int phy, phy_idx = 0;		for (phy = 1; phy < 32 && phy_idx < sizeof(ep->phys); phy++) {			int mii_status = mdio_read(ioaddr, phy, 1);			if (mii_status != 0xffff  &&  mii_status != 0x0000) {				ep->phys[phy_idx++] = phy;				printk(KERN_INFO "%s: MII transceiver #%d control "					   "%4.4x status %4.4x.\n",					   dev->name, phy, mdio_read(ioaddr, phy, 0), mii_status);			}		}		ep->mii_phy_cnt = phy_idx;		if (phy_idx != 0) {			phy = ep->phys[0];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -