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

📄 sundance.c

📁 Linux下各种网卡的驱动程序
💻 C
📖 第 1 页 / 共 4 页
字号:
	/* Station address must be written as 16 bit words with the Kendin chip. */	for (i = 0; i < 6; i += 2)		writew((dev->dev_addr[i + 1] << 8) + dev->dev_addr[i],			   ioaddr + StationAddr + i);	np->link_status = readb(ioaddr + MIICtrl) & 0xE0;	writew((np->full_duplex || (np->link_status & 0x20)) ? 0x120 : 0,		   ioaddr + MACCtrl0);	writew(dev->mtu + 14, ioaddr + MaxFrameSize);	if (dev->mtu > 2047)		writel(readl(ioaddr + ASICCtrl) | 0x0C, ioaddr + ASICCtrl);	set_rx_mode(dev);	writew(0, ioaddr + DownCounter);	/* Set the chip to poll every N*320nsec. */	writeb(100, ioaddr + RxDescPoll);	writeb(127, ioaddr + TxDescPoll);#if 0	if (np->drv_flags & KendinPktDropBug)		writeb(0x01, ioaddr + DebugCtrl1);#endif	/* Enable interrupts by setting the interrupt mask. */	writew(IntrRxDMADone | IntrPCIErr | IntrDrvRqst | IntrTxDone		   | StatsMax | LinkChange, ioaddr + IntrEnable);	writew(StatsEnable | RxEnable | TxEnable, ioaddr + MACCtrl1);}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 (np->msg_level & NETIF_MSG_LINK)			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 (np->msg_level & NETIF_MSG_TIMER) {		printk(KERN_DEBUG "%s: Media selection timer tick, intr status %4.4x, "			   "Tx %x Rx %x.\n",			   dev->name, (int)readw(ioaddr + IntrEnable),			   (int)readw(ioaddr + TxStatus), (int)readl(ioaddr + RxStatus));	}	/* Note: This does not catch a 0 or 1 element stuck queue. */	if (netif_queue_paused(dev)  &&		np->cur_tx - np->dirty_tx > 1  &&		(jiffies - dev->trans_start) > TX_TIMEOUT) {		tx_timeout(dev);	}	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 %4.4x,"		   " resetting...\n", dev->name, (int)readw(ioaddr + TxStatus));#ifdef __i386__	if (np->msg_level & NETIF_MSG_TX_ERR) {		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(" %8.8x", 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(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 + 20;	if (np->rx_buf_sz < PKT_BUF_SZ)		np->rx_buf_sz = PKT_BUF_SZ;	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;	/* Block a timer-based transmit from overlapping. */	if (netif_pause_tx_queue(dev) != 0) {		/* This watchdog code is redundant with the media monitor timer. */		if (jiffies - dev->trans_start > TX_TIMEOUT)			tx_timeout(dev);		return 1;	}	/* 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 | 1);	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) {		np->tx_full = 1;		/* Check for a just-cleared queue. */		if (np->cur_tx - (volatile unsigned int)np->dirty_tx			< TX_QUEUE_LEN - 2) {			np->tx_full = 0;			netif_unpause_tx_queue(dev);		} else			netif_stop_tx_queue(dev);	} else		netif_unpause_tx_queue(dev);		/* Typical path */	/* 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 (np->msg_level & NETIF_MSG_TX_QUEUED) {		printk(KERN_DEBUG "%s: Transmit frame #%d len %d queued in slot %d.\n",			   dev->name, np->cur_tx, skb->len, 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;	ioaddr = dev->base_addr;	np = (struct netdev_private *)dev->priv;	boguscnt = np->max_interrupt_work;	do {		int intr_status = readw(ioaddr + IntrStatus);		if ((intr_status & ~IntrRxDone) == 0 || intr_status == 0xffff)			break;		writew(intr_status & (IntrRxDMADone | IntrPCIErr |							  IntrDrvRqst |IntrTxDone|IntrTxDMADone |							  StatsMax | LinkChange),							  ioaddr + IntrStatus);		if (np->msg_level & NETIF_MSG_INTR)			printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n",				   dev->name, intr_status);		if (intr_status & IntrRxDMADone)			netdev_rx(dev);		if (intr_status & IntrTxDone) {			int txboguscnt = 32;			int tx_status = readw(ioaddr + TxStatus);			while (tx_status & 0x80) {				if (np->msg_level & NETIF_MSG_TX_DONE)					printk("%s: Transmit status is %4.4x.\n",						   dev->name, tx_status);				if (tx_status & 0x1e) {					if (np->msg_level & NETIF_MSG_TX_ERR)						printk("%s: Transmit error status %4.4x.\n",							   dev->name, tx_status);					np->stats.tx_errors++;					if (tx_status & 0x10)  np->stats.tx_fifo_errors++;#ifdef ETHER_STATS					if (tx_status & 0x08)  np->stats.collisions16++;#else					if (tx_status & 0x08)  np->stats.collisions++;#endif					if (tx_status & 0x04)  np->stats.tx_fifo_errors++;					if (tx_status & 0x02)  np->stats.tx_window_errors++;					/* This reset has not been verified!. */					if (tx_status & 0x10) {			/* Reset the Tx. */						writel(0x001c0000 | readl(ioaddr + ASICCtrl),							   ioaddr + ASICCtrl);#if 0					/* Do we need to reset the Tx pointer here? */						writel(virt_to_bus(&np->tx_ring[np->dirty_tx]),							   dev->base_addr + TxListPtr);#endif					}					if (tx_status & 0x1e) 		/* Restart the Tx. */						writew(TxEnable, ioaddr + MACCtrl1);				}				/* Yup, this is a documentation bug.  It cost me *hours*. */				writew(0, ioaddr + TxStatus);				if (--txboguscnt < 0)					break;				tx_status = readw(ioaddr + TxStatus);			}		}		for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {			int entry = np->dirty_tx % TX_RING_SIZE;			if ( ! (np->tx_ring[entry].status & cpu_to_le32(DescTxDMADone)))				break;			/* Free the original skb. */			dev_free_skb_irq(np->tx_skbuff[entry]);			np->tx_skbuff[entry] = 0;		}		if (np->tx_full  &&  np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {			/* The ring is no longer full, allow new TX entries. */			np->tx_full = 0;			netif_resume_tx_queue(dev);		}		/* Abnormal error summary/uncommon events handlers. */		if (intr_status & (IntrDrvRqst | IntrPCIErr | LinkChange | StatsMax))			netdev_error(dev, intr_status);		if (--boguscnt < 0) {			int intr_clear = readw(ioaddr + IntrClear);			get_stats(dev);			printk(KERN_WARNING "%s: Too much work at interrupt, "				   "status=0x%4.4x / 0x%4.4x .. 0x%4.4x.\n",				   dev->name, intr_status, intr_clear,				   (int)readw(ioaddr + IntrClear));			/* Re-enable us in 3.2msec. */			writew(1000, ioaddr + DownCounter);			writew(IntrDrvRqst, ioaddr + IntrEnable);			break;		}	} while (1);	if (np->msg_level & NETIF_MSG_INTR)		printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",			   dev->name, (int)readw(ioaddr + IntrStatus));	return;}/* This routine is logically part of the interrupt handler, but separated   for clarity and better register allocation. */static int netdev_rx(struct net_device *dev){	struct netdev_private *np = (struct netdev_private *)dev->priv;	int entry = np->cur_rx % RX_RING_SIZE;	int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;	if (np->msg_level & NETIF_MSG_RX_STATUS) {		printk(KERN_DEBUG " In netdev_rx(), entry %d status %4.4x.\n",			   entry, np->rx_ring[entry].status);	}	/* If EOP is set on the next entry, it's a new packet. Send it up. */	while (np->rx_head_desc->status & cpu_to_le32(DescOwn)) {		struct netdev_desc *desc = np->rx_head_desc;		u32 frame_status = le32_to_cpu(desc->status);		int pkt_len = frame_status & 0x1fff;		/* Chip omits the CRC. */		if (np->msg_level & NETIF_MSG_RX_STATUS)			printk(KERN_DEBUG "  netdev_rx() status was %8.8x.\n",				   frame_status);		if (--boguscnt < 0)			break;		if (frame_status & 0x001f4000) {			/* There was a error. */			if (np->msg_level & NETIF_MSG_RX_ERR)				printk(KERN_DEBUG "  netdev_rx() Rx error was %8.8x.\n",					   frame_status);			np->stats.rx_errors++;			if (frame_status & 0x00100000) np->stats.rx_length_errors++;			if (frame_status & 0x00010000) np->stats.rx_fifo_errors++;			if (frame_status & 0x00060000) np->stats.rx_frame_errors++;			if (frame_status & 0x00080000) np->stats.rx_crc_errors++;			if (frame_status & 0x00100000) {				printk(KERN_WARNING "%s: Oversized Ethernet frame,"					   " status %8.8x.\n",					   dev->name, frame_status);			}		} else {			struct sk_buff *skb;#ifndef final_version			if (np->msg_level & NETIF_MSG_RX_STATUS)				printk(KERN_DEBUG "  netdev_rx() normal Rx pkt length %d"					   ", bogus_cnt %d.\n",					   pkt_len, boguscnt);#endif			/* Check if the packet is long enough to accept without copying			   to a minimally-sized skbuff. */			if (pkt_len < np->rx_copybreak				&& (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {				skb->dev = dev;				skb_reserve(skb, 2);	/* 16 byte align the IP header */				eth_copy_and_sum(skb, np->rx_skbuff[entry]->tail, pkt_len, 0);				skb_put(skb, pkt_len);			} else {				skb_put(skb = np->rx_skbuff[entry], pkt_len);				np->rx_skbuff[entry] = NULL;			}			skb->protocol = eth_type_trans(skb, dev);			/* Note: checksum -> skb->ip_summed = CHECKSUM_UNNECESSARY; */			netif_rx(skb);			dev->last_rx = jiffies;		}		entry = (++np->cur_rx) % RX_RING_SIZE;		np->rx_head_desc = &np->rx_ring[entry];	}

⌨️ 快捷键说明

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