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

📄 via-rhine.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
				    RX_RING_SIZE * sizeof(struct rx_desc) +				    TX_RING_SIZE * sizeof(struct tx_desc),				    &ring_dma);	if (!ring) {		printk(KERN_ERR "Could not allocate DMA memory.\n");		goto err_out;	}	ioaddr = pci_resource_start (pdev, pci_flags & PCI_ADDR0 ? 0 : 1);	if (pci_enable_device (pdev))		goto err_out_free_dma;		if (pci_flags & PCI_USES_MASTER)		pci_set_master (pdev);	dev = init_etherdev(NULL, sizeof(*np));	if (dev == NULL) {		printk (KERN_ERR "init_ethernet failed for card #%d\n",			card_idx);		goto err_out_free_dma;	}	SET_MODULE_OWNER(dev);		/* request all PIO and MMIO regions just to make sure	 * noone else attempts to use any portion of our I/O space */	if (!request_region (pci_resource_start (pdev, 0),			     pci_resource_len (pdev, 0), dev->name)) {		printk (KERN_ERR "request_region failed for device %s, region 0x%X @ 0x%lX\n",			dev->name, io_size,			pci_resource_start (pdev, 0));		goto err_out_free_netdev;	}	if (!request_mem_region (pci_resource_start (pdev, 1),				 pci_resource_len (pdev, 1), dev->name)) {		printk (KERN_ERR "request_mem_region failed for device %s, region 0x%X @ 0x%lX\n",			dev->name, io_size,			pci_resource_start (pdev, 1));		goto err_out_free_pio;	}#ifndef USE_IO	ioaddr = (long) ioremap (ioaddr, io_size);	if (!ioaddr) {		printk (KERN_ERR "ioremap failed for device %s, region 0x%X @ 0x%X\n",			dev->name, io_size,			pci_resource_start (pdev, 1));		goto err_out_free_mmio;	}#endif	printk(KERN_INFO "%s: %s at 0x%lx, ",		   dev->name, via_rhine_chip_info[chip_id].name, ioaddr);	/* Ideally we would read the EEPROM but access may be locked. */	for (i = 0; i < 6; i++)		dev->dev_addr[i] = readb(ioaddr + StationAddr + i);	for (i = 0; i < 5; i++)			printk("%2.2x:", dev->dev_addr[i]);	printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);	/* Reset the chip to erase previous misconfiguration. */	writew(CmdReset, ioaddr + ChipCmd);	dev->base_addr = ioaddr;	dev->irq = irq;	np = dev->priv;	spin_lock_init (&np->lock);	np->chip_id = chip_id;	np->drv_flags = via_rhine_chip_info[chip_id].drv_flags;	np->pdev = pdev;	np->rx_ring = ring;	np->tx_ring = ring + RX_RING_SIZE * sizeof(struct rx_desc);	np->rx_ring_dma = ring_dma;	np->tx_ring_dma = ring_dma + RX_RING_SIZE * sizeof(struct rx_desc);	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 (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 = via_rhine_open;	dev->hard_start_xmit = via_rhine_start_tx;	dev->stop = via_rhine_close;	dev->get_stats = via_rhine_get_stats;	dev->set_multicast_list = via_rhine_set_rx_mode;	dev->do_ioctl = mii_ioctl;	dev->tx_timeout = via_rhine_tx_timeout;	dev->watchdog_timeo = TX_TIMEOUT;		pdev->driver_data = dev;	if (np->drv_flags & CanHaveMII) {		int phy, phy_idx = 0;		np->phys[0] = 1;		/* Standard for this chip. */		for (phy = 1; 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 Link %4.4x.\n",					   dev->name, phy, mii_status, np->advertising,					   mdio_read(dev, phy, 5));				/* set IFF_RUNNING */				if (mii_status & MIILink)					netif_carrier_on(dev);				else					netif_carrier_off(dev);			}		}	}	return 0;#ifndef USE_IO/* note this is ifdef'd because the ioremap is ifdef'd... * so additional exit conditions above this must move * release_mem_region outside of the ifdef */err_out_free_mmio:	release_mem_region(pci_resource_start (pdev, 1),			   pci_resource_len (pdev, 1));#endiferr_out_free_pio:	release_region(pci_resource_start (pdev, 0),		       pci_resource_len (pdev, 0));err_out_free_netdev:	unregister_netdev (dev);	kfree (dev);err_out_free_dma:	pci_free_consistent(pdev, 			    RX_RING_SIZE * sizeof(struct rx_desc) +			    TX_RING_SIZE * sizeof(struct tx_desc),			    ring, ring_dma);err_out:	return -ENODEV;}/* Read and write over the MII Management Data I/O (MDIO) interface. */static int mdio_read(struct net_device *dev, int phy_id, int regnum){	long ioaddr = dev->base_addr;	int boguscnt = 1024;	/* Wait for a previous command to complete. */	while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)		;	writeb(0x00, ioaddr + MIICmd);	writeb(phy_id, ioaddr + MIIPhyAddr);	writeb(regnum, ioaddr + MIIRegAddr);	writeb(0x40, ioaddr + MIICmd);			/* Trigger read */	boguscnt = 1024;	while ((readb(ioaddr + MIICmd) & 0x40) && --boguscnt > 0)		;	return readw(ioaddr + MIIData);}static void mdio_write(struct net_device *dev, int phy_id, int regnum, int value){	struct netdev_private *np = (struct netdev_private *)dev->priv;	long ioaddr = dev->base_addr;	int boguscnt = 1024;	if (phy_id == np->phys[0]) {		switch (regnum) {		case 0:							/* Is user forcing speed/duplex? */			if (value & 0x9000)			/* Autonegotiation. */				np->duplex_lock = 0;			else				np->full_duplex = (value & 0x0100) ? 1 : 0;			break;		case 4:			np->advertising = value;			break;		}	}	/* Wait for a previous command to complete. */	while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)		;	writeb(0x00, ioaddr + MIICmd);	writeb(phy_id, ioaddr + MIIPhyAddr);	writeb(regnum, ioaddr + MIIRegAddr);	writew(value, ioaddr + MIIData);	writeb(0x20, ioaddr + MIICmd);			/* Trigger write. */}static int via_rhine_open(struct net_device *dev){	struct netdev_private *np = (struct netdev_private *)dev->priv;	long ioaddr = dev->base_addr;	int i;	/* Reset the chip. */	writew(CmdReset, ioaddr + ChipCmd);	i = request_irq(dev->irq, &via_rhine_interrupt, SA_SHIRQ, dev->name, dev);	if (i)		return i;	if (debug > 1)		printk(KERN_DEBUG "%s: via_rhine_open() irq %d.\n",			   dev->name, dev->irq);	np->tx_bufs = pci_alloc_consistent(np->pdev, PKT_BUF_SZ * TX_RING_SIZE,									   &np->tx_bufs_dma);	if (np->tx_bufs == NULL) {		free_irq(dev->irq, dev);		return -ENOMEM;	}	via_rhine_init_ring(dev);	writel(np->rx_ring_dma, ioaddr + RxRingPtr);	writel(np->tx_ring_dma, ioaddr + TxRingPtr);	for (i = 0; i < 6; i++)		writeb(dev->dev_addr[i], ioaddr + StationAddr + i);	/* Initialize other registers. */	writew(0x0006, ioaddr + PCIBusConfig);	/* Tune configuration??? */	/* Configure the FIFO thresholds. */	writeb(0x20, ioaddr + TxConfig);	/* Initial threshold 32 bytes */	np->tx_thresh = 0x20;	np->rx_thresh = 0x60;			/* Written in via_rhine_set_rx_mode(). */	if (dev->if_port == 0)		dev->if_port = np->default_port;	netif_start_queue(dev);	via_rhine_set_rx_mode(dev);	/* Enable interrupts by setting the interrupt mask. */	writew(IntrRxDone | IntrRxErr | IntrRxEmpty| IntrRxOverflow| IntrRxDropped|		   IntrTxDone | IntrTxAbort | IntrTxUnderrun |		   IntrPCIErr | IntrStatsMax | IntrLinkChange | IntrMIIChange,		   ioaddr + IntrEnable);	np->chip_cmd = CmdStart|CmdTxOn|CmdRxOn|CmdNoTxPoll;	if (np->duplex_lock)		np->chip_cmd |= CmdFDuplex;	writew(np->chip_cmd, ioaddr + ChipCmd);	via_rhine_check_duplex(dev);	/* The LED outputs of various MII xcvrs should be configured.  */	/* For NS or Mison phys, turn on bit 1 in register 0x17 */	/* For ESI phys, turn on bit 7 in register 0x17. */	mdio_write(dev, np->phys[0], 0x17, mdio_read(dev, np->phys[0], 0x17) |			   (np->drv_flags & HasESIPhy) ? 0x0080 : 0x0001);	if (debug > 2)		printk(KERN_DEBUG "%s: Done via_rhine_open(), status %4.4x "			   "MII status: %4.4x.\n",			   dev->name, readw(ioaddr + ChipCmd),			   mdio_read(dev, np->phys[0], 1));	/* Set the timer to check for link beat. */	init_timer(&np->timer);	np->timer.expires = jiffies + 2;	np->timer.data = (unsigned long)dev;	np->timer.function = &via_rhine_timer;				/* timer handler */	add_timer(&np->timer);	return 0;}static void via_rhine_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 link"				   " partner capability of %4.4x.\n", dev->name,				   duplex ? "full" : "half", np->phys[0], mii_reg5);		if (duplex)			np->chip_cmd |= CmdFDuplex;		else			np->chip_cmd &= ~CmdFDuplex;		writew(np->chip_cmd, ioaddr + ChipCmd);	}}static void via_rhine_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;	int mii_status;	if (debug > 3) {		printk(KERN_DEBUG "%s: VIA Rhine monitor tick, status %4.4x.\n",			   dev->name, readw(ioaddr + IntrStatus));	}	spin_lock_irq (&np->lock);	via_rhine_check_duplex(dev);	/* make IFF_RUNNING follow the MII status bit "Link established" */	mii_status = mdio_read(dev, np->phys[0], 1);	if ( (mii_status & MIILink) != (np->mii_status & MIILink) ) {		if (mii_status & MIILink)			netif_carrier_on(dev);		else			netif_carrier_off(dev);	}	np->mii_status = mii_status;	spin_unlock_irq (&np->lock);	np->timer.expires = jiffies + next_tick;	add_timer(&np->timer);}static void via_rhine_tx_timeout (struct net_device *dev){	struct netdev_private *np = (struct netdev_private *) dev->priv;	long ioaddr = dev->base_addr;	/* Lock to protect mdio_read and access to stats. A friendly       advice to the implementor of the XXXs in this function is to be       sure not to spin too long (whatever that means :) */	spin_lock_irq (&np->lock);	printk (KERN_WARNING "%s: Transmit timed out, status %4.4x, PHY status "		"%4.4x, resetting...\n",		dev->name, readw (ioaddr + IntrStatus),		mdio_read (dev, np->phys[0], 1));	/* XXX Perhaps we should reinitialize the hardware here. */	dev->if_port = 0;	/* Stop and restart the chip's Tx processes . */	/* XXX to do */	/* Trigger an immediate transmit demand. */	/* XXX to do */	dev->trans_start = jiffies;	np->stats.tx_errors++;	spin_unlock_irq (&np->lock);}/* Initialize the Rx and Tx rings, along with various 'dev' bits. */static void via_rhine_init_ring(struct net_device *dev){	struct netdev_private *np = (struct netdev_private *)dev->priv;	int i;	dma_addr_t next = np->rx_ring_dma;	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];	for (i = 0; i < RX_RING_SIZE; i++) {		np->rx_ring[i].rx_status = 0;		np->rx_ring[i].desc_length = cpu_to_le32(np->rx_buf_sz);		next += sizeof(struct rx_desc);		np->rx_ring[i].next_desc = cpu_to_le32(next);		np->rx_skbuff[i] = 0;	}	/* Mark the last entry as wrapping the ring. */	np->rx_ring[i-1].next_desc = cpu_to_le32(np->rx_ring_dma);	/* 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. */		np->rx_skbuff_dma[i] =			pci_map_single(np->pdev, skb->tail, np->rx_buf_sz,						   PCI_DMA_FROMDEVICE);		np->rx_ring[i].addr = cpu_to_le32(np->rx_skbuff_dma[i]);		np->rx_ring[i].rx_status = cpu_to_le32(DescOwn);	}	np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);	next = np->tx_ring_dma;	for (i = 0; i < TX_RING_SIZE; i++) {		np->tx_skbuff[i] = 0;		np->tx_ring[i].tx_status = 0;		np->tx_ring[i].desc_length = cpu_to_le32(0x00e08000);		next += sizeof(struct tx_desc);		np->tx_ring[i].next_desc = cpu_to_le32(next);		np->tx_buf[i] = &np->tx_bufs[i * PKT_BUF_SZ];	}	np->tx_ring[i-1].next_desc = cpu_to_le32(np->tx_ring_dma);	return;}static int via_rhine_start_tx(struct sk_buff *skb, struct net_device *dev){	struct netdev_private *np = (struct netdev_private *)dev->priv;	unsigned entry;	/* Caution: the write order is important here, set the field	   with the "ownership" bits last. */	/* lock eth irq */	spin_lock_irq (&np->lock);	/* Calculate the next Tx descriptor entry. */	entry = np->cur_tx % TX_RING_SIZE;	np->tx_skbuff[entry] = skb;	if ((np->drv_flags & ReqTxAlign) && ((long)skb->data & 3)) {		/* Must use alignment buffer. */		memcpy(np->tx_buf[entry], skb->data, skb->len);		np->tx_skbuff_dma[entry] = 0;		np->tx_ring[entry].addr = cpu_to_le32(np->tx_bufs_dma +										  (np->tx_buf[entry] - np->tx_bufs));	} else {		np->tx_skbuff_dma[entry] =			pci_map_single(np->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);		np->tx_ring[entry].addr = cpu_to_le32(np->tx_skbuff_dma[entry]);	}	np->tx_ring[entry].desc_length = 		cpu_to_le32(0x00E08000 | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN));	np->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);	np->cur_tx++;	/* Non-x86 Todo: explicitly flush cache lines here. */	/* Wake the potentially-idle transmit channel. */	writew(CmdTxDemand | np->chip_cmd, dev->base_addr + ChipCmd);	if (np->cur_tx == np->dirty_tx + TX_QUEUE_LEN)		netif_stop_queue(dev);	dev->trans_start = jiffies;	spin_unlock_irq (&np->lock);	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 via_rhine_interrupt(int irq, void *dev_instance, struct pt_regs *rgs){	struct net_device *dev = (struct net_device *)dev_instance;	long ioaddr;

⌨️ 快捷键说明

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