eepro100.c

来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 1,918 行 · 第 1/5 页

C
1,918 行
字号
		eeprom_delay(100);	}	/* Terminate the EEPROM access. */	outw(EE_ENB & ~EE_CS, ee_addr);	return retval;}static int mdio_read(long ioaddr, int phy_id, int location){	int val, boguscnt = 64*10;		/* <64 usec. to complete, typ 27 ticks */	outl(0x08000000 | (location<<16) | (phy_id<<21), ioaddr + SCBCtrlMDI);	do {		val = inl(ioaddr + SCBCtrlMDI);		if (--boguscnt < 0) {			printk(KERN_ERR " mdio_read() timed out with val = %8.8x.\n", val);		}	} while (! (val & 0x10000000));	return val & 0xffff;}static int mdio_write(long ioaddr, int phy_id, int location, int value){	int val, boguscnt = 64*10;		/* <64 usec. to complete, typ 27 ticks */	outl(0x04000000 | (location<<16) | (phy_id<<21) | value,		 ioaddr + SCBCtrlMDI);	do {		val = inl(ioaddr + SCBCtrlMDI);		if (--boguscnt < 0) {			printk(KERN_ERR" mdio_write() timed out with val = %8.8x.\n", val);		}	} while (! (val & 0x10000000));	return val & 0xffff;}static intspeedo_open(struct device *dev){	struct speedo_private *sp = (struct speedo_private *)dev->priv;	long ioaddr = dev->base_addr;#ifdef notdef	/* We could reset the chip, but should not need to. */	/* In fact we MUST NOT, unless we also re-do the init */	outl(0, ioaddr + SCBPort);	udelay(10);#endif	/* This had better be initialized before we initialize the interrupt! */	sp->lock = (spinlock_t) SPIN_LOCK_UNLOCKED;	if (speedo_debug > 1)		printk(KERN_DEBUG "%s: speedo_open() irq %d.\n", dev->name, dev->irq);#ifdef oh_no_you_dont_unless_you_honour_the_options_passed_in_to_us	/* Retrigger negotiation to reset previous errors. */	if ((sp->phy[0] & 0x8000) == 0) {		int phy_addr = sp->phy[0] & 0x1f ;		/* Use 0x3300 for restarting NWay, other values to force xcvr:		   0x0000 10-HD		   0x0100 10-FD		   0x2000 100-HD		   0x2100 100-FD		*/#ifdef honor_default_port		mdio_write(ioaddr, phy_addr, 0, mii_ctrl[dev->default_port & 7]);#else		mdio_write(ioaddr, phy_addr, 0, 0x3300);#endif	}#endif	/* Load the statistics block address. */	wait_for_cmd_done(ioaddr + SCBCmd);	outl(virt_to_bus(&sp->lstats), ioaddr + SCBPointer);	outw(INT_MASK | CU_STATSADDR, ioaddr + SCBCmd);	sp->lstats.done_marker = 0;	speedo_init_rx_ring(dev);	wait_for_cmd_done(ioaddr + SCBCmd);	outl(0, ioaddr + SCBPointer);	outw(INT_MASK | RX_ADDR_LOAD, ioaddr + SCBCmd);	/* Todo: verify that we must wait for previous command completion. */	wait_for_cmd_done(ioaddr + SCBCmd);	outl(virt_to_bus(sp->rx_ringp[0]), ioaddr + SCBPointer);	outw(INT_MASK | RX_START, ioaddr + SCBCmd);	/* Fill the first command with our physical address. */	{		u16 *eaddrs = (u16 *)dev->dev_addr;		u16 *setup_frm = (u16 *)&(sp->tx_ring[0].tx_desc_addr);		/* Avoid a bug(?!) here by marking the command already completed. */		sp->tx_ring[0].status = ((CmdSuspend | CmdIASetup) << 16) | 0xa000;		sp->tx_ring[0].link = virt_to_bus(&(sp->tx_ring[1]));		*setup_frm++ = eaddrs[0];		*setup_frm++ = eaddrs[1];		*setup_frm++ = eaddrs[2];	}	sp->last_cmd = (struct descriptor *)&sp->tx_ring[0];	sp->cur_tx = 1;	sp->dirty_tx = 0;	sp->tx_full = 0;	wait_for_cmd_done(ioaddr + SCBCmd);	outl(0, ioaddr + SCBPointer);	outw(INT_MASK | CU_CMD_BASE, ioaddr + SCBCmd);	dev->if_port = sp->default_port;	dev->tbusy = 0;	dev->interrupt = 0;	dev->start = 1;	/*	 * Request the IRQ last, after we have set up all data structures.	 * It would be bad to get an interrupt before we're ready.	 *	 * Register ourself first before turn on the interrupt. Someone	 * else may share the same interrupt. H.J.	 */	if (request_irq(dev->irq, &speedo_interrupt, SA_SHIRQ,					"Intel EtherExpress Pro 10/100 Ethernet", dev)) {		return -EAGAIN;	}	/* Start the chip's Tx process and unmask interrupts. */	/* Todo: verify that we must wait for previous command completion. */	wait_for_cmd_done(ioaddr + SCBCmd);	outl(virt_to_bus(&sp->tx_ring[0]), ioaddr + SCBPointer);	outw(CU_START, ioaddr + SCBCmd);	/* Setup the chip and configure the multicast list. */	sp->mc_setup_frm = NULL;	sp->mc_setup_frm_len = 0;	sp->mc_setup_busy = 0;	sp->rx_mode = -1;			/* Invalid -> always reset the mode. */	set_rx_mode(dev);	if (speedo_debug > 2) {		printk(KERN_DEBUG "%s: Done speedo_open(), status %8.8x.\n",			   dev->name, inw(ioaddr + SCBStatus));	}	wait_for_cmd_done(ioaddr + SCBCmd);	outw(CU_DUMPSTATS, ioaddr + SCBCmd);	/* No need to wait for the command unit to accept here. */	if ((sp->phy[0] & 0x8000) == 0)		mdio_read(ioaddr, sp->phy[0] & 0x1f, 0);	MOD_INC_USE_COUNT;	/* Set the timer.  The timer serves a dual purpose:	   1) to monitor the media interface (e.g. link beat) and perhaps switch	   to an alternate media type	   2) to monitor Rx activity, and restart the Rx process if the receiver	   hangs. */	init_timer(&sp->timer);	sp->timer.expires = RUN_AT((24*HZ)/10); 			/* 2.4 sec. */	sp->timer.data = (unsigned long)dev;	sp->timer.function = &speedo_timer;					/* timer handler */	add_timer(&sp->timer);	return 0;}/* Media monitoring and control. */static void speedo_timer(unsigned long data){	struct device *dev = (struct device *)data;	struct speedo_private *sp = (struct speedo_private *)dev->priv;	if (speedo_debug > 3) {		long ioaddr = dev->base_addr;		printk(KERN_DEBUG "%s: Media control tick, status %4.4x.\n",			   dev->name, inw(ioaddr + SCBStatus));	}	if (sp->rx_mode < 0  ||		(sp->rx_bug  && jiffies - sp->last_rx_time > 2*HZ)) {		/* We haven't received a packet in a Long Time.  We might have been		   bitten by the receiver hang bug.  This can be cleared by sending		   a set multicast list command. */		set_rx_mode(dev);	}	/* We must continue to monitor the media. */	sp->timer.expires = RUN_AT(2*HZ); 			/* 2.0 sec. */	add_timer(&sp->timer);}/* Initialize the Rx and Tx rings, along with various 'dev' bits. */static voidspeedo_init_rx_ring(struct device *dev){	struct speedo_private *sp = (struct speedo_private *)dev->priv;	struct RxFD *rxf, *last_rxf = NULL;	int i;	sp->cur_rx = 0;	for (i = 0; i < RX_RING_SIZE; i++) {		struct sk_buff *skb;		skb = dev_alloc_skb(PKT_BUF_SZ + sizeof(struct RxFD));		sp->rx_skbuff[i] = skb;		if (skb == NULL)			break;			/* OK.  Just initially short of Rx bufs. */		skb->dev = dev;			/* Mark as being used by this device. */		rxf = (struct RxFD *)skb->tail;		sp->rx_ringp[i] = rxf;		skb_reserve(skb, sizeof(struct RxFD));		if (last_rxf)			last_rxf->link = virt_to_bus(rxf);		last_rxf = rxf;		rxf->status = 0x00000001; 			/* '1' is flag value only. */		rxf->link = 0;						/* None yet. */		/* This field unused by i82557, we use it as a consistency check. */#ifdef final_version		rxf->rx_buf_addr = 0xffffffff;#else		rxf->rx_buf_addr = virt_to_bus(skb->tail);#endif		rxf->count = 0;		rxf->size = PKT_BUF_SZ;	}	sp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);	/* Mark the last entry as end-of-list. */	last_rxf->status = 0xC0000002; 			/* '2' is flag value only. */	sp->last_rxf = last_rxf;}static void speedo_tx_timeout(struct device *dev){	struct speedo_private *sp = (struct speedo_private *)dev->priv;	long ioaddr = dev->base_addr;	printk(KERN_WARNING "%s: Transmit timed out: status %4.4x "		   " %4.4x at %d/%d command %8.8x.\n",		   dev->name, inw(ioaddr + SCBStatus), inw(ioaddr + SCBCmd),		   sp->dirty_tx, sp->cur_tx,		   sp->tx_ring[sp->dirty_tx % TX_RING_SIZE].status);	if ((inw(ioaddr + SCBStatus) & 0x00C0) != 0x0080) {		printk(KERN_WARNING "%s: Trying to restart the transmitter...\n",			   dev->name);		outl(virt_to_bus(&sp->tx_ring[sp->dirty_tx % TX_RING_SIZE]),			 ioaddr + SCBPointer);		outw(CU_START, ioaddr + SCBCmd);	} else {		outw(DRVR_INT, ioaddr + SCBCmd);	}#ifdef oh_no_you_dont_unless_you_honour_the_options_passed_in_to_us	/* Reset the MII transceiver, suggested by Fred Young @ scalable.com. */	if ((sp->phy[0] & 0x8000) == 0) {		int phy_addr = sp->phy[0] & 0x1f;		mdio_write(ioaddr, phy_addr, 0, 0x0400);		mdio_write(ioaddr, phy_addr, 1, 0x0000);		mdio_write(ioaddr, phy_addr, 4, 0x0000);		mdio_write(ioaddr, phy_addr, 0, 0x8000);#ifdef honor_default_port		mdio_write(ioaddr, phy_addr, 0, mii_ctrl[dev->default_port & 7]);#endif	}#endif	sp->stats.tx_errors++;	dev->trans_start = jiffies;	return;}static intspeedo_start_xmit(struct sk_buff *skb, struct device *dev){	struct speedo_private *sp = (struct speedo_private *)dev->priv;	long ioaddr = dev->base_addr;	int entry;	unsigned long flags;	/* Block a timer-based transmit from overlapping.  This could better be	   done with atomic_swap(1, dev->tbusy), but set_bit() works as well.	   If this ever occurs the queue layer is doing something evil! */	if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {		int tickssofar = jiffies - dev->trans_start;#if defined(OSKIT) && defined(DEVICE_POLLING)		/* Should never happen when polling! */		if (sp->polling & POLLING_TX) {			printk(KERN_DEBUG "%s: device busy in polling mode!\n", dev->name);			return 1;		}#endif		if (tickssofar < TX_TIMEOUT - 2)			return 1;		if (tickssofar < TX_TIMEOUT) {			/* Reap sent packets from the full Tx queue. */			OUTSCBCMD(DRVR_INT, ioaddr);			return 1;		}		speedo_tx_timeout(dev);		return 1;	}	/* Prevent interrupts from changing the Tx ring from underneath us. */	spin_lock_irqsave(&sp->lock, flags);#if defined(OSKIT) && defined(DEVICE_POLLING)	if ((sp->polling & POLLING_TX) && sp->tx_full) {		if (speedo_tx_reap(dev) == 0) {			spin_unlock_irqrestore(&sp->lock, flags);			clear_bit(0, (void*)&dev->tbusy);			return 1;		}		sp->tx_full = 0;	}#endif	/* Caution: the write order is important here, set the base address	   with the "ownership" bits last. */	/* Calculate the Tx descriptor entry. */	entry = sp->cur_tx++ % TX_RING_SIZE;	sp->tx_skbuff[entry] = skb;	/* Todo: be a little more clever about setting the interrupt bit. */	sp->tx_ring[entry].status =		(CmdSuspend | CmdTx | CmdTxFlex) << 16;	sp->tx_ring[entry].link =		virt_to_bus(&sp->tx_ring[sp->cur_tx % TX_RING_SIZE]);	sp->tx_ring[entry].tx_desc_addr =		virt_to_bus(&sp->tx_ring[entry].tx_buf_addr0);	/* The data region is always in one buffer descriptor, Tx FIFO	   threshold of 256. */	sp->tx_ring[entry].count = 0x01208000;	sp->tx_ring[entry].tx_buf_addr0 = virt_to_bus(skb->data);	sp->tx_ring[entry].tx_buf_size0 = skb->len;	/* Todo: perhaps leave the interrupt bit set if the Tx queue is more	   than half full.  Argument against: we should be receiving packets	   and scavenging the queue.  Argument for: if so, it shouldn't	   matter. */	sp->last_cmd->command &= ~(CmdSuspend | CmdIntr);	sp->last_cmd = (struct descriptor *)&sp->tx_ring[entry];	/* Trigger the command unit resume. */	wait_for_cmd_done(ioaddr + SCBCmd);	OUTSCBCMD(CU_RESUME, ioaddr);	spin_unlock_irqrestore(&sp->lock, flags);#if defined(OSKIT) && defined(DEVICE_POLLING)	/* when polling, always clear tbusy */	if (sp->polling & POLLING_TX) {		if (sp->cur_tx - sp->dirty_tx > TX_RING_SIZE - 3)			sp->tx_full = 1;		clear_bit(0, (void*)&dev->tbusy);	} else#endif	/* Leave room for set_rx_mode() to fill two entries. */	if (sp->cur_tx - sp->dirty_tx > TX_RING_SIZE - 3)		sp->tx_full = 1;	else		clear_bit(0, (void*)&dev->tbusy);	dev->trans_start = jiffies;	return 0;}/* The interrupt handler does all of the Rx thread work and cleans up   after the Tx thread. */static void speedo_interrupt(int irq, void *dev_instance, struct pt_regs *regs){	struct device *dev = (struct device *)dev_instance;	struct speedo_private *sp;	long ioaddr, boguscnt = max_interrupt_work;	unsigned short status;#ifndef final_version	if (dev == NULL) {		printk(KERN_ERR "speedo_interrupt(): irq %d for unknown device.\n", irq);		return;	}#endif	ioaddr = dev->base_addr;	sp = (struct speedo_private *)dev->priv;

⌨️ 快捷键说明

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