epic100.c

来自「linux 内核源代码」· C语言 代码 · 共 1,609 行 · 第 1/3 页

C
1,609
字号
/* epic100.c: A SMC 83c170 EPIC/100 Fast Ethernet driver for Linux. *//*	Written/copyright 1997-2001 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	[this link no longer provides anything useful -jgarzik]	---------------------------------------------------------------------*/#define DRV_NAME        "epic100"#define DRV_VERSION     "2.1"#define DRV_RELDATE     "Sept 11, 2006"/* 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. *//* 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;/* 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	256#define TX_QUEUE_LEN	240		/* Limit ring entries actually used.  */#define RX_RING_SIZE	256#define TX_TOTAL_SIZE	TX_RING_SIZE*sizeof(struct epic_tx_desc)#define RX_TOTAL_SIZE	RX_RING_SIZE*sizeof(struct epic_rx_desc)/* 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  */#include <linux/module.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/errno.h>#include <linux/ioport.h>#include <linux/slab.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 <linux/ethtool.h>#include <linux/mii.h>#include <linux/crc32.h>#include <linux/bitops.h>#include <asm/io.h>#include <asm/uaccess.h>/* These identify the driver base version and may not be removed. */static char version[] __devinitdata =DRV_NAME ".c:v1.11 1/7/2001 Written by Donald Becker <becker@scyld.com>\n";static char version2[] __devinitdata ="  (unofficial 2.4.x kernel port, version " DRV_VERSION ", " DRV_RELDATE ")\n";MODULE_AUTHOR("Donald Becker <becker@scyld.com>");MODULE_DESCRIPTION("SMC 83c170 EPIC series Ethernet driver");MODULE_LICENSE("GPL");module_param(debug, int, 0);module_param(rx_copybreak, int, 0);module_param_array(options, int, NULL, 0);module_param_array(full_duplex, int, NULL, 0);MODULE_PARM_DESC(debug, "EPIC/100 debug level (0-5)");MODULE_PARM_DESC(options, "EPIC/100: Bits 0-3: media type, bit 4: full duplex");MODULE_PARM_DESC(rx_copybreak, "EPIC/100 copy breakpoint for copy-only-tiny-frames");MODULE_PARM_DESC(full_duplex, "EPIC/100 full duplex setting(s) (1)");/*				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 chip_capability_flags { MII_PWRDWN=1, TYPE2_INTR=2, NO_MII=4 };#define EPIC_TOTAL_SIZE 0x100#define USE_IO_OPS 1typedef enum {	SMSC_83C170_0,	SMSC_83C170,	SMSC_83C175,} chip_t;struct epic_chip_info {	const char *name;        int drv_flags;                          /* Driver use, intended as capability flags. */};/* indexed by chip_t */static const struct epic_chip_info pci_id_tbl[] = {	{ "SMSC EPIC/100 83c170",	TYPE2_INTR | NO_MII | MII_PWRDWN },	{ "SMSC EPIC/100 83c170",	TYPE2_INTR },	{ "SMSC EPIC/C 83c175",		TYPE2_INTR | MII_PWRDWN },};static struct pci_device_id epic_pci_tbl[] = {	{ 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,};#define EpicRemoved	0xffffffff	/* Chip failed or removed (CardBus) */#define EpicNapiEvent	(TxEmpty | TxDone | \			 RxDone | RxStarted | RxEarlyWarn | RxOverflow | RxFull)#define EpicNormalEvent	(0x0000ffff & ~EpicNapiEvent)static const 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,};#define PRIV_ALIGN	15 	/* Required alignment mask */struct epic_private {	struct epic_rx_desc *rx_ring;	struct epic_tx_desc *tx_ring;	/* 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];	dma_addr_t tx_ring_dma;	dma_addr_t rx_ring_dma;	/* Ring pointers. */	spinlock_t lock;				/* Group with Tx control cache line. */	spinlock_t napi_lock;	struct napi_struct napi;	unsigned int reschedule_in_poll;	unsigned int cur_tx, dirty_tx;	unsigned int cur_rx, dirty_rx;	u32 irq_mask;	unsigned int rx_buf_sz;				/* Based on MTU+slack. */	struct pci_dev *pci_dev;			/* PCI bus location. */	int chip_id, 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;	struct mii_if_info mii;	unsigned int tx_full:1;				/* The Tx queue is full. */	unsigned int default_port:4;		/* Last dev->if_port value. */};static int epic_open(struct net_device *dev);static int read_eeprom(long ioaddr, int location);static int mdio_read(struct net_device *dev, int phy_id, int location);static void mdio_write(struct net_device *dev, int phy_id, int loc, int val);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, int budget);static int epic_poll(struct napi_struct *napi, int budget);static irqreturn_t epic_interrupt(int irq, void *dev_instance);static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);static const struct ethtool_ops netdev_ethtool_ops;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;	long ioaddr;	int chip_idx = (int) ent->driver_data;	int irq;	struct net_device *dev;	struct epic_private *ep;	int i, ret, option = 0, duplex = 0;	void *ring_space;	dma_addr_t ring_dma;	DECLARE_MAC_BUF(mac);/* when built into the kernel, we only print version if device is found */#ifndef MODULE	static int printed_version;	if (!printed_version++)		printk (KERN_INFO "%s" KERN_INFO "%s",			version, version2);#endif	card_idx++;	ret = pci_enable_device(pdev);	if (ret)		goto out;	irq = pdev->irq;	if (pci_resource_len(pdev, 0) < EPIC_TOTAL_SIZE) {		dev_err(&pdev->dev, "no PCI region space\n");		ret = -ENODEV;		goto err_out_disable;	}	pci_set_master(pdev);	ret = pci_request_regions(pdev, DRV_NAME);	if (ret < 0)		goto err_out_disable;	ret = -ENOMEM;	dev = alloc_etherdev(sizeof (*ep));	if (!dev) {		dev_err(&pdev->dev, "no memory for eth device\n");		goto err_out_free_res;	}	SET_NETDEV_DEV(dev, &pdev->dev);#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) {		dev_err(&pdev->dev, "ioremap failed\n");		goto err_out_free_netdev;	}#endif	pci_set_drvdata(pdev, dev);	ep = dev->priv;	ep->mii.dev = dev;	ep->mii.mdio_read = mdio_read;	ep->mii.mdio_write = mdio_write;	ep->mii.phy_id_mask = 0x1f;	ep->mii.reg_num_mask = 0x1f;	ring_space = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);	if (!ring_space)		goto err_out_iounmap;	ep->tx_ring = (struct epic_tx_desc *)ring_space;	ep->tx_ring_dma = ring_dma;	ring_space = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);	if (!ring_space)		goto err_out_unmap_tx;	ep->rx_ring = (struct epic_rx_desc *)ring_space;	ep->rx_ring_dma = ring_dma;	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];	}	dev->base_addr = ioaddr;	dev->irq = irq;	spin_lock_init(&ep->lock);	spin_lock_init(&ep->napi_lock);	ep->reschedule_in_poll = 0;	/* 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. */	/* This magic is documented in SMSC app note 7.15 */	for (i = 16; i > 0; i--)		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);	/* Note: the '175 does not have a serial EEPROM. */	for (i = 0; i < 3; i++)		((u16 *)dev->dev_addr)[i] = le16_to_cpu(inw(ioaddr + LAN0 + i*4));	if (debug > 2) {		dev_printk(KERN_DEBUG, &pdev->dev, "EEPROM contents:\n");		for (i = 0; i < 64; i++)			printk(" %4.4x%s", read_eeprom(ioaddr, i),				   i % 16 == 15 ? "\n" : "");	}	ep->pci_dev = pdev;	ep->chip_id = chip_idx;	ep->chip_flags = pci_id_tbl[chip_idx].drv_flags;	ep->irq_mask =		(ep->chip_flags & TYPE2_INTR ?  PCIBusErr175 : PCIBusErr170)		 | CntFull | TxUnderrun | EpicNapiEvent;	/* 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(dev, phy, MII_BMSR);			if (mii_status != 0xffff  &&  mii_status != 0x0000) {				ep->phys[phy_idx++] = phy;				dev_info(&pdev->dev,					"MII transceiver #%d control "					"%4.4x status %4.4x.\n",					phy, mdio_read(dev, phy, 0), mii_status);			}		}		ep->mii_phy_cnt = phy_idx;		if (phy_idx != 0) {			phy = ep->phys[0];			ep->mii.advertising = mdio_read(dev, phy, MII_ADVERTISE);			dev_info(&pdev->dev,				"Autonegotiation advertising %4.4x link "				   "partner %4.4x.\n",				   ep->mii.advertising, mdio_read(dev, phy, 5));		} else if ( ! (ep->chip_flags & NO_MII)) {			dev_warn(&pdev->dev,				"***WARNING***: No MII transceiver found!\n");			/* Use the known PHY address of the EPII. */			ep->phys[0] = 3;		}		ep->mii.phy_id = ep->phys[0];	}	/* Turn off the MII xcvr (175 only!), leave the chip in low-power mode. */	if (ep->chip_flags & MII_PWRDWN)		outl(inl(ioaddr + NVCTL) & ~0x483C, ioaddr + NVCTL);	outl(0x0008, ioaddr + GENCTL);	/* The lower four bits are the media type. */	if (duplex) {		ep->mii.force_media = ep->mii.full_duplex = 1;		dev_info(&pdev->dev, "Forced full duplex requested.\n");	}	dev->if_port = ep->default_port = option;	/* The Epic-specific entries in the device structure. */	dev->open = &epic_open;	dev->hard_start_xmit = &epic_start_xmit;	dev->stop = &epic_close;	dev->get_stats = &epic_get_stats;	dev->set_multicast_list = &set_rx_mode;	dev->do_ioctl = &netdev_ioctl;	dev->ethtool_ops = &netdev_ethtool_ops;	dev->watchdog_timeo = TX_TIMEOUT;	dev->tx_timeout = &epic_tx_timeout;	netif_napi_add(dev, &ep->napi, epic_poll, 64);	ret = register_netdev(dev);	if (ret < 0)		goto err_out_unmap_rx;	printk(KERN_INFO "%s: %s at %#lx, IRQ %d, %s\n",	       dev->name, pci_id_tbl[chip_idx].name, ioaddr, dev->irq,	       print_mac(mac, dev->dev_addr));out:	return ret;err_out_unmap_rx:	pci_free_consistent(pdev, RX_TOTAL_SIZE, ep->rx_ring, ep->rx_ring_dma);err_out_unmap_tx:	pci_free_consistent(pdev, TX_TOTAL_SIZE, ep->tx_ring, ep->tx_ring_dma);err_out_iounmap:#ifndef USE_IO_OPS	iounmap(ioaddr);err_out_free_netdev:#endif	free_netdev(dev);err_out_free_res:	pci_release_regions(pdev);err_out_disable:	pci_disable_device(pdev);	goto out;}/* Serial EEPROM section. *//*  EEPROM_Ctrl bits. */#define EE_SHIFT_CLK	0x04	/* EEPROM shift clock. */#define EE_CS			0x02	/* EEPROM chip select. */#define EE_DATA_WRITE	0x08	/* EEPROM chip data in. */#define EE_WRITE_0		0x01#define EE_WRITE_1		0x09#define EE_DATA_READ	0x10	/* EEPROM chip data out. */#define EE_ENB			(0x0001 | EE_CS)/* Delay between EEPROM clock transitions.   This serves to flush the operation to the PCI bus. */#define eeprom_delay()	inl(ee_addr)

⌨️ 快捷键说明

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