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

📄 sundance.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
	spin_lock_init(&np->lock);	if (dev->mem_start)		option = dev->mem_start;	/* The lower four bits are the media type. */	if (option > 0) {		if (option & 0x200)			np->full_duplex = 1;		np->default_port = option & 15;		if (np->default_port)			np->medialock = 1;	}	if (card_idx < MAX_UNITS  &&  full_duplex[card_idx] > 0)		np->full_duplex = 1;	if (np->full_duplex)		np->duplex_lock = 1;	/* The chip-specific entries in the device structure. */	dev->open = &netdev_open;	dev->hard_start_xmit = &start_tx;	dev->stop = &netdev_close;	dev->get_stats = &get_stats;	dev->set_multicast_list = &set_rx_mode;	dev->do_ioctl = &mii_ioctl;	dev->tx_timeout = &tx_timeout;	dev->watchdog_timeo = TX_TIMEOUT;	if (mtu)		dev->mtu = mtu;	if (1) {		int phy, phy_idx = 0;		np->phys[0] = 1;		/* Default setting */		for (phy = 0; phy < 32 && phy_idx < 4; phy++) {			int mii_status = mdio_read(dev, phy, 1);			if (mii_status != 0xffff  &&  mii_status != 0x0000) {				np->phys[phy_idx++] = phy;				np->advertising = mdio_read(dev, phy, 4);				printk(KERN_INFO "%s: MII PHY found at address %d, status "					   "0x%4.4x advertising %4.4x.\n",					   dev->name, phy, mii_status, np->advertising);			}		}		np->mii_cnt = phy_idx;		if (phy_idx == 0)			printk(KERN_INFO "%s: No MII transceiver found!, ASIC status %x\n",				   dev->name, readl(ioaddr + ASICCtrl));	}	/* Perhaps move the reset here? */	/* Reset the chip to erase previous misconfiguration. */	if (debug > 1)		printk("ASIC Control is %x.\n", readl(ioaddr + ASICCtrl));	writew(0x007f, ioaddr + ASICCtrl + 2);	if (debug > 1)		printk("ASIC Control is now %x.\n", readl(ioaddr + ASICCtrl));	card_idx++;	return 0;#ifndef USE_IO_OPSerr_out_iomem:	release_mem_region(pci_resource_start(pdev, 1),			   pci_id_tbl[chip_idx].io_size);#endiferr_out_netdev:	unregister_netdev (dev);	kfree (dev);	return -ENODEV;}/* Read the EEPROM and MII Management Data I/O (MDIO) interfaces. */static int eeprom_read(long ioaddr, int location){	int boguscnt = 1000;		/* Typical 190 ticks. */	writew(0x0200 | (location & 0xff), ioaddr + EECtrl);	do {		if (! (readw(ioaddr + EECtrl) & 0x8000)) {			return readw(ioaddr + EEData);		}	} while (--boguscnt > 0);	return 0;}/*  MII transceiver control section.	Read and write the MII registers using software-generated serial	MDIO protocol.  See the MII specifications or DP83840A data sheet	for details.	The maximum data clock rate is 2.5 Mhz.  The minimum timing is usually	met by back-to-back 33Mhz PCI cycles. */#define mdio_delay() readb(mdio_addr)/* Set iff a MII transceiver on any interface requires mdio preamble.   This only set with older tranceivers, so the extra   code size of a per-interface flag is not worthwhile. */static char mii_preamble_required = 0;enum mii_reg_bits {	MDIO_ShiftClk=0x0001, MDIO_Data=0x0002, MDIO_EnbOutput=0x0004,};#define MDIO_EnbIn  (0)#define MDIO_WRITE0 (MDIO_EnbOutput)#define MDIO_WRITE1 (MDIO_Data | MDIO_EnbOutput)/* Generate the preamble required for initial synchronization and   a few older transceivers. */static void mdio_sync(long mdio_addr){	int bits = 32;	/* Establish sync by sending at least 32 logic ones. */	while (--bits >= 0) {		writeb(MDIO_WRITE1, mdio_addr);		mdio_delay();		writeb(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);		mdio_delay();	}}static int mdio_read(struct net_device *dev, int phy_id, int location){	long mdio_addr = dev->base_addr + MIICtrl;	int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;	int i, retval = 0;	if (mii_preamble_required)		mdio_sync(mdio_addr);	/* Shift the read command bits out. */	for (i = 15; i >= 0; i--) {		int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;		writeb(dataval, mdio_addr);		mdio_delay();		writeb(dataval | MDIO_ShiftClk, mdio_addr);		mdio_delay();	}	/* Read the two transition, 16 data, and wire-idle bits. */	for (i = 19; i > 0; i--) {		writeb(MDIO_EnbIn, mdio_addr);		mdio_delay();		retval = (retval << 1) | ((readb(mdio_addr) & MDIO_Data) ? 1 : 0);		writeb(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);		mdio_delay();	}	return (retval>>1) & 0xffff;}static void mdio_write(struct net_device *dev, int phy_id, int location, int value){	long mdio_addr = dev->base_addr + MIICtrl;	int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;	int i;	if (mii_preamble_required)		mdio_sync(mdio_addr);	/* Shift the command bits out. */	for (i = 31; i >= 0; i--) {		int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;		writeb(dataval, mdio_addr);		mdio_delay();		writeb(dataval | MDIO_ShiftClk, mdio_addr);		mdio_delay();	}	/* Clear out extra bits. */	for (i = 2; i > 0; i--) {		writeb(MDIO_EnbIn, mdio_addr);		mdio_delay();		writeb(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);		mdio_delay();	}	return;}static int netdev_open(struct net_device *dev){	struct netdev_private *np = (struct netdev_private *)dev->priv;	long ioaddr = dev->base_addr;	int i;	/* Do we need to reset the chip??? */	i = request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev);	if (i)		return i;	if (debug > 1)		printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",			   dev->name, dev->irq);	init_ring(dev);	writel(virt_to_bus(np->rx_ring), ioaddr + RxListPtr);	/* The Tx list pointer is written as packets are queued. */	for (i = 0; i < 6; i++)		writeb(dev->dev_addr[i], ioaddr + StationAddr + i);	/* Initialize other registers. */	/* Configure the PCI bus bursts and FIFO thresholds. */	if (dev->if_port == 0)		dev->if_port = np->default_port;	np->full_duplex = np->duplex_lock;	np->mcastlock = (spinlock_t) SPIN_LOCK_UNLOCKED;	set_rx_mode(dev);	writew(0, ioaddr + DownCounter);	/* Set the chip to poll every N*320nsec. */	writeb(100, ioaddr + RxDescPoll);	writeb(127, ioaddr + TxDescPoll);	netif_start_queue(dev);	/* Enable interrupts by setting the interrupt mask. */	writew(IntrRxDone | IntrRxDMADone | IntrPCIErr | IntrDrvRqst | IntrTxDone		   | StatsMax | LinkChange, ioaddr + IntrEnable);	writew(StatsEnable | RxEnable | TxEnable, ioaddr + MACCtrl1);	if (debug > 2)		printk(KERN_DEBUG "%s: Done netdev_open(), status: Rx %x Tx %x "			   "MAC Control %x, %4.4x %4.4x.\n",			   dev->name, readl(ioaddr + RxStatus), readb(ioaddr + TxStatus),			   readl(ioaddr + MACCtrl0),			   readw(ioaddr + MACCtrl1), readw(ioaddr + MACCtrl0));	/* Set the timer to check for link beat. */	init_timer(&np->timer);	np->timer.expires = jiffies + 3*HZ;	np->timer.data = (unsigned long)dev;	np->timer.function = &netdev_timer;				/* timer handler */	add_timer(&np->timer);	return 0;}static void check_duplex(struct net_device *dev){	struct netdev_private *np = (struct netdev_private *)dev->priv;	long ioaddr = dev->base_addr;	int mii_reg5 = mdio_read(dev, np->phys[0], 5);	int negotiated = mii_reg5 & np->advertising;	int duplex;	if (np->duplex_lock  ||  mii_reg5 == 0xffff)		return;	duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;	if (np->full_duplex != duplex) {		np->full_duplex = duplex;		if (debug)			printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d "				   "negotiated capability %4.4x.\n", dev->name,				   duplex ? "full" : "half", np->phys[0], negotiated);		writew(duplex ? 0x20 : 0, ioaddr + MACCtrl0);	}}static void netdev_timer(unsigned long data){	struct net_device *dev = (struct net_device *)data;	struct netdev_private *np = (struct netdev_private *)dev->priv;	long ioaddr = dev->base_addr;	int next_tick = 10*HZ;	if (debug > 3) {		printk(KERN_DEBUG "%s: Media selection timer tick, intr status %4.4x, "			   "Tx %x Rx %x.\n",			   dev->name, readw(ioaddr + IntrEnable),			   readb(ioaddr + TxStatus), readl(ioaddr + RxStatus));	}	check_duplex(dev);	np->timer.expires = jiffies + next_tick;	add_timer(&np->timer);}static void tx_timeout(struct net_device *dev){	struct netdev_private *np = (struct netdev_private *)dev->priv;	long ioaddr = dev->base_addr;	printk(KERN_WARNING "%s: Transmit timed out, status %2.2x,"		   " resetting...\n", dev->name, readb(ioaddr + TxStatus));#ifndef __alpha__	{		int i;		printk(KERN_DEBUG "  Rx ring %8.8x: ", (int)np->rx_ring);		for (i = 0; i < RX_RING_SIZE; i++)			printk(" %8.8x", (unsigned int)np->rx_ring[i].status);		printk("\n"KERN_DEBUG"  Tx ring %8.8x: ", (int)np->tx_ring);		for (i = 0; i < TX_RING_SIZE; i++)			printk(" %4.4x", np->tx_ring[i].status);		printk("\n");	}#endif	/* Perhaps we should reinitialize the hardware here. */	dev->if_port = 0;	/* Stop and restart the chip's Tx processes . */	/* Trigger an immediate transmit demand. */	writew(IntrRxDone | IntrRxDMADone | IntrPCIErr | IntrDrvRqst | IntrTxDone		   | StatsMax | LinkChange, ioaddr + IntrEnable);	dev->trans_start = jiffies;	np->stats.tx_errors++;	return;}/* Initialize the Rx and Tx rings, along with various 'dev' bits. */static void init_ring(struct net_device *dev){	struct netdev_private *np = (struct netdev_private *)dev->priv;	int i;	np->tx_full = 0;	np->cur_rx = np->cur_tx = 0;	np->dirty_rx = np->dirty_tx = 0;	np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);	np->rx_head_desc = &np->rx_ring[0];	/* Initialize all Rx descriptors. */	for (i = 0; i < RX_RING_SIZE; i++) {		np->rx_ring[i].next_desc = virt_to_le32desc(&np->rx_ring[i+1]);		np->rx_ring[i].status = 0;		np->rx_ring[i].frag[0].length = 0;		np->rx_skbuff[i] = 0;	}	/* Wrap the ring. */	np->rx_ring[i-1].next_desc = virt_to_le32desc(&np->rx_ring[0]);	/* Fill in the Rx buffers.  Handle allocation failure gracefully. */	for (i = 0; i < RX_RING_SIZE; i++) {		struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);		np->rx_skbuff[i] = skb;		if (skb == NULL)			break;		skb->dev = dev;			/* Mark as being used by this device. */		skb_reserve(skb, 2);	/* 16 byte align the IP header. */		np->rx_ring[i].frag[0].addr = virt_to_le32desc(skb->tail);		np->rx_ring[i].frag[0].length = cpu_to_le32(np->rx_buf_sz | LastFrag);	}	np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);	for (i = 0; i < TX_RING_SIZE; i++) {		np->tx_skbuff[i] = 0;		np->tx_ring[i].status = 0;	}	return;}static int start_tx(struct sk_buff *skb, struct net_device *dev){	struct netdev_private *np = (struct netdev_private *)dev->priv;	struct netdev_desc *txdesc;	unsigned entry;	/* Note: Ordering is important here, set the field with the	   "ownership" bit last, and only then increment cur_tx. */	/* Calculate the next Tx descriptor entry. */	entry = np->cur_tx % TX_RING_SIZE;	np->tx_skbuff[entry] = skb;	txdesc = &np->tx_ring[entry];	txdesc->next_desc = 0;	/* Note: disable the interrupt generation here before releasing. */	txdesc->status =		cpu_to_le32((entry<<2) | DescIntrOnDMADone | DescIntrOnTx);	txdesc->frag[0].addr = virt_to_le32desc(skb->data);	txdesc->frag[0].length = cpu_to_le32(skb->len | LastFrag);	if (np->last_tx)		np->last_tx->next_desc = virt_to_le32desc(txdesc);	np->last_tx = txdesc;	np->cur_tx++;	/* On some architectures: explicitly flush cache lines here. */	if (np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 1) {		/* do nothing */	} else {		np->tx_full = 1;		netif_stop_queue(dev);	}	/* Side effect: The read wakes the potentially-idle transmit channel. */	if (readl(dev->base_addr + TxListPtr) == 0)		writel(virt_to_bus(&np->tx_ring[entry]), dev->base_addr + TxListPtr);	dev->trans_start = jiffies;	if (debug > 4) {		printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",			   dev->name, np->cur_tx, entry);	}	return 0;}/* The interrupt handler does all of the Rx thread work and cleans up   after the Tx thread. */static void intr_handler(int irq, void *dev_instance, struct pt_regs *rgs){	struct net_device *dev = (struct net_device *)dev_instance;	struct netdev_private *np;	long ioaddr;	int boguscnt = max_interrupt_work;	ioaddr = dev->base_addr;	np = (struct netdev_private *)dev->priv;	spin_lock(&np->lock);	do {		int intr_status = readw(ioaddr + IntrStatus);		writew(intr_status & (IntrRxDone | IntrRxDMADone | IntrPCIErr |

⌨️ 快捷键说明

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