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

📄 smc91x.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
	unsigned char *buf;	DBG(3, "%s: %s\n", dev->name, __FUNCTION__);	if (!smc_special_trylock(&lp->lock)) {		netif_stop_queue(dev);		tasklet_schedule(&lp->tx_task);		return;	}	skb = lp->pending_tx_skb;	if (unlikely(!skb)) {		smc_special_unlock(&lp->lock);		return;	}	lp->pending_tx_skb = NULL;	packet_no = SMC_GET_AR();	if (unlikely(packet_no & AR_FAILED)) {		printk("%s: Memory allocation failed.\n", dev->name);		dev->stats.tx_errors++;		dev->stats.tx_fifo_errors++;		smc_special_unlock(&lp->lock);		goto done;	}	/* point to the beginning of the packet */	SMC_SET_PN(packet_no);	SMC_SET_PTR(PTR_AUTOINC);	buf = skb->data;	len = skb->len;	DBG(2, "%s: TX PNR 0x%x LENGTH 0x%04x (%d) BUF 0x%p\n",		dev->name, packet_no, len, len, buf);	PRINT_PKT(buf, len);	/*	 * Send the packet length (+6 for status words, length, and ctl.	 * The card will pad to 64 bytes with zeroes if packet is too small.	 */	SMC_PUT_PKT_HDR(0, len + 6);	/* send the actual data */	SMC_PUSH_DATA(buf, len & ~1);	/* Send final ctl word with the last byte if there is one */	SMC_outw(((len & 1) ? (0x2000 | buf[len-1]) : 0), ioaddr, DATA_REG);	/*	 * If THROTTLE_TX_PKTS is set, we stop the queue here. This will	 * have the effect of having at most one packet queued for TX	 * in the chip's memory at all time.	 *	 * If THROTTLE_TX_PKTS is not set then the queue is stopped only	 * when memory allocation (MC_ALLOC) does not succeed right away.	 */	if (THROTTLE_TX_PKTS)		netif_stop_queue(dev);	/* queue the packet for TX */	SMC_SET_MMU_CMD(MC_ENQUEUE);	smc_special_unlock(&lp->lock);	dev->trans_start = jiffies;	dev->stats.tx_packets++;	dev->stats.tx_bytes += len;	SMC_ENABLE_INT(IM_TX_INT | IM_TX_EMPTY_INT);done:	if (!THROTTLE_TX_PKTS)		netif_wake_queue(dev);	dev_kfree_skb(skb);}/* * Since I am not sure if I will have enough room in the chip's ram * to store the packet, I call this routine which either sends it * now, or set the card to generates an interrupt when ready * for the packet. */static int smc_hard_start_xmit(struct sk_buff *skb, struct net_device *dev){	struct smc_local *lp = netdev_priv(dev);	void __iomem *ioaddr = lp->base;	unsigned int numPages, poll_count, status;	DBG(3, "%s: %s\n", dev->name, __FUNCTION__);	BUG_ON(lp->pending_tx_skb != NULL);	/*	 * The MMU wants the number of pages to be the number of 256 bytes	 * 'pages', minus 1 (since a packet can't ever have 0 pages :))	 *	 * The 91C111 ignores the size bits, but earlier models don't.	 *	 * Pkt size for allocating is data length +6 (for additional status	 * words, length and ctl)	 *	 * If odd size then last byte is included in ctl word.	 */	numPages = ((skb->len & ~1) + (6 - 1)) >> 8;	if (unlikely(numPages > 7)) {		printk("%s: Far too big packet error.\n", dev->name);		dev->stats.tx_errors++;		dev->stats.tx_dropped++;		dev_kfree_skb(skb);		return 0;	}	smc_special_lock(&lp->lock);	/* now, try to allocate the memory */	SMC_SET_MMU_CMD(MC_ALLOC | numPages);	/*	 * Poll the chip for a short amount of time in case the	 * allocation succeeds quickly.	 */	poll_count = MEMORY_WAIT_TIME;	do {		status = SMC_GET_INT();		if (status & IM_ALLOC_INT) {			SMC_ACK_INT(IM_ALLOC_INT);  			break;		}   	} while (--poll_count);	smc_special_unlock(&lp->lock);	lp->pending_tx_skb = skb;   	if (!poll_count) {		/* oh well, wait until the chip finds memory later */		netif_stop_queue(dev);		DBG(2, "%s: TX memory allocation deferred.\n", dev->name);		SMC_ENABLE_INT(IM_ALLOC_INT);   	} else {		/*		 * Allocation succeeded: push packet to the chip's own memory		 * immediately.		 */		smc_hardware_send_pkt((unsigned long)dev);	}	return 0;}/* * This handles a TX interrupt, which is only called when: * - a TX error occurred, or * - CTL_AUTO_RELEASE is not set and TX of a packet completed. */static void smc_tx(struct net_device *dev){	struct smc_local *lp = netdev_priv(dev);	void __iomem *ioaddr = lp->base;	unsigned int saved_packet, packet_no, tx_status, pkt_len;	DBG(3, "%s: %s\n", dev->name, __FUNCTION__);	/* If the TX FIFO is empty then nothing to do */	packet_no = SMC_GET_TXFIFO();	if (unlikely(packet_no & TXFIFO_TEMPTY)) {		PRINTK("%s: smc_tx with nothing on FIFO.\n", dev->name);		return;	}	/* select packet to read from */	saved_packet = SMC_GET_PN();	SMC_SET_PN(packet_no);	/* read the first word (status word) from this packet */	SMC_SET_PTR(PTR_AUTOINC | PTR_READ);	SMC_GET_PKT_HDR(tx_status, pkt_len);	DBG(2, "%s: TX STATUS 0x%04x PNR 0x%02x\n",		dev->name, tx_status, packet_no);	if (!(tx_status & ES_TX_SUC))		dev->stats.tx_errors++;	if (tx_status & ES_LOSTCARR)		dev->stats.tx_carrier_errors++;	if (tx_status & (ES_LATCOL | ES_16COL)) {		PRINTK("%s: %s occurred on last xmit\n", dev->name,		       (tx_status & ES_LATCOL) ?			"late collision" : "too many collisions");		dev->stats.tx_window_errors++;		if (!(dev->stats.tx_window_errors & 63) && net_ratelimit()) {			printk(KERN_INFO "%s: unexpectedly large number of "			       "bad collisions. Please check duplex "			       "setting.\n", dev->name);		}	}	/* kill the packet */	SMC_WAIT_MMU_BUSY();	SMC_SET_MMU_CMD(MC_FREEPKT);	/* Don't restore Packet Number Reg until busy bit is cleared */	SMC_WAIT_MMU_BUSY();	SMC_SET_PN(saved_packet);	/* re-enable transmit */	SMC_SELECT_BANK(0);	SMC_SET_TCR(lp->tcr_cur_mode);	SMC_SELECT_BANK(2);}/*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/static void smc_mii_out(struct net_device *dev, unsigned int val, int bits){	struct smc_local *lp = netdev_priv(dev);	void __iomem *ioaddr = lp->base;	unsigned int mii_reg, mask;	mii_reg = SMC_GET_MII() & ~(MII_MCLK | MII_MDOE | MII_MDO);	mii_reg |= MII_MDOE;	for (mask = 1 << (bits - 1); mask; mask >>= 1) {		if (val & mask)			mii_reg |= MII_MDO;		else			mii_reg &= ~MII_MDO;		SMC_SET_MII(mii_reg);		udelay(MII_DELAY);		SMC_SET_MII(mii_reg | MII_MCLK);		udelay(MII_DELAY);	}}static unsigned int smc_mii_in(struct net_device *dev, int bits){	struct smc_local *lp = netdev_priv(dev);	void __iomem *ioaddr = lp->base;	unsigned int mii_reg, mask, val;	mii_reg = SMC_GET_MII() & ~(MII_MCLK | MII_MDOE | MII_MDO);	SMC_SET_MII(mii_reg);	for (mask = 1 << (bits - 1), val = 0; mask; mask >>= 1) {		if (SMC_GET_MII() & MII_MDI)			val |= mask;		SMC_SET_MII(mii_reg);		udelay(MII_DELAY);		SMC_SET_MII(mii_reg | MII_MCLK);		udelay(MII_DELAY);	}	return val;}/* * Reads a register from the MII Management serial interface */static int smc_phy_read(struct net_device *dev, int phyaddr, int phyreg){	struct smc_local *lp = netdev_priv(dev);	void __iomem *ioaddr = lp->base;	unsigned int phydata;	SMC_SELECT_BANK(3);	/* Idle - 32 ones */	smc_mii_out(dev, 0xffffffff, 32);	/* Start code (01) + read (10) + phyaddr + phyreg */	smc_mii_out(dev, 6 << 10 | phyaddr << 5 | phyreg, 14);	/* Turnaround (2bits) + phydata */	phydata = smc_mii_in(dev, 18);	/* Return to idle state */	SMC_SET_MII(SMC_GET_MII() & ~(MII_MCLK|MII_MDOE|MII_MDO));	DBG(3, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",		__FUNCTION__, phyaddr, phyreg, phydata);	SMC_SELECT_BANK(2);	return phydata;}/* * Writes a register to the MII Management serial interface */static void smc_phy_write(struct net_device *dev, int phyaddr, int phyreg,			  int phydata){	struct smc_local *lp = netdev_priv(dev);	void __iomem *ioaddr = lp->base;	SMC_SELECT_BANK(3);	/* Idle - 32 ones */	smc_mii_out(dev, 0xffffffff, 32);	/* Start code (01) + write (01) + phyaddr + phyreg + turnaround + phydata */	smc_mii_out(dev, 5 << 28 | phyaddr << 23 | phyreg << 18 | 2 << 16 | phydata, 32);	/* Return to idle state */	SMC_SET_MII(SMC_GET_MII() & ~(MII_MCLK|MII_MDOE|MII_MDO));	DBG(3, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",		__FUNCTION__, phyaddr, phyreg, phydata);	SMC_SELECT_BANK(2);}/* * Finds and reports the PHY address */static void smc_phy_detect(struct net_device *dev){	struct smc_local *lp = netdev_priv(dev);	int phyaddr;	DBG(2, "%s: %s\n", dev->name, __FUNCTION__);	lp->phy_type = 0;	/*	 * Scan all 32 PHY addresses if necessary, starting at	 * PHY#1 to PHY#31, and then PHY#0 last.	 */	for (phyaddr = 1; phyaddr < 33; ++phyaddr) {		unsigned int id1, id2;		/* Read the PHY identifiers */		id1 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID1);		id2 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID2);		DBG(3, "%s: phy_id1=0x%x, phy_id2=0x%x\n",			dev->name, id1, id2);		/* Make sure it is a valid identifier */		if (id1 != 0x0000 && id1 != 0xffff && id1 != 0x8000 &&		    id2 != 0x0000 && id2 != 0xffff && id2 != 0x8000) {			/* Save the PHY's address */			lp->mii.phy_id = phyaddr & 31;			lp->phy_type = id1 << 16 | id2;			break;		}	}}/* * Sets the PHY to a configuration as determined by the user */static int smc_phy_fixed(struct net_device *dev){	struct smc_local *lp = netdev_priv(dev);	void __iomem *ioaddr = lp->base;	int phyaddr = lp->mii.phy_id;	int bmcr, cfg1;	DBG(3, "%s: %s\n", dev->name, __FUNCTION__);	/* Enter Link Disable state */	cfg1 = smc_phy_read(dev, phyaddr, PHY_CFG1_REG);	cfg1 |= PHY_CFG1_LNKDIS;	smc_phy_write(dev, phyaddr, PHY_CFG1_REG, cfg1);	/*	 * Set our fixed capabilities	 * Disable auto-negotiation	 */	bmcr = 0;	if (lp->ctl_rfduplx)		bmcr |= BMCR_FULLDPLX;	if (lp->ctl_rspeed == 100)		bmcr |= BMCR_SPEED100;	/* Write our capabilities to the phy control register */	smc_phy_write(dev, phyaddr, MII_BMCR, bmcr);	/* Re-Configure the Receive/Phy Control register */	SMC_SELECT_BANK(0);	SMC_SET_RPC(lp->rpc_cur_mode);	SMC_SELECT_BANK(2);	return 1;}/* * smc_phy_reset - reset the phy * @dev: net device * @phy: phy address * * Issue a software reset for the specified PHY and * wait up to 100ms for the reset to complete.  We should * not access the PHY for 50ms after issuing the reset. * * The time to wait appears to be dependent on the PHY. * * Must be called with lp->lock locked. */static int smc_phy_reset(struct net_device *dev, int phy){	struct smc_local *lp = netdev_priv(dev);	unsigned int bmcr;	int timeout;	smc_phy_write(dev, phy, MII_BMCR, BMCR_RESET);	for (timeout = 2; timeout; timeout--) {		spin_unlock_irq(&lp->lock);		msleep(50);		spin_lock_irq(&lp->lock);		bmcr = smc_phy_read(dev, phy, MII_BMCR);		if (!(bmcr & BMCR_RESET))			break;	}	return bmcr & BMCR_RESET;}/* * smc_phy_powerdown - powerdown phy * @dev: net device * * Power down the specified PHY */static void smc_phy_powerdown(struct net_device *dev){	struct smc_local *lp = netdev_priv(dev);	unsigned int bmcr;	int phy = lp->mii.phy_id;	if (lp->phy_type == 0)		return;	/* We need to ensure that no calls to smc_phy_configure are	   pending.	   flush_scheduled_work() cannot be called because we are	   running with the netlink semaphore held (from	   devinet_ioctl()) and the pending work queue contains	   linkwatch_event() (scheduled by netif_carrier_off()	   above). linkwatch_event() also wants the netlink semaphore.	*/	while(lp->work_pending)		yield();	bmcr = smc_phy_read(dev, phy, MII_BMCR);	smc_phy_write(dev, phy, MII_BMCR, bmcr | BMCR_PDOWN);}/* * smc_phy_check_media - check the media status and adjust TCR * @dev: net device * @init: set true for initialisation * * Select duplex mode depending on negotiation state.  This * also updates our carrier state. */static void smc_phy_check_media(struct net_device *dev, int init){	struct smc_local *lp = netdev_priv(dev);	void __iomem *ioaddr = lp->base;	if (mii_check_media(&lp->mii, netif_msg_link(lp), init)) {		/* duplex state has changed */		if (lp->mii.full_duplex) {			lp->tcr_cur_mode |= TCR_SWFDUP;		} else {			lp->tcr_cur_mode &= ~TCR_SWFDUP;		}		SMC_SELECT_BANK(0);		SMC_SET_TCR(lp->tcr_cur_mode);	}}/* * Configures the specified PHY through the MII management interface * using Autonegotiation. * Calls smc_phy_fixed() if the user has requested a certain config. * If RPC ANEG bit is set, the media selection is dependent purely on * the selection by the MII (either in the MII BMCR reg or the result * of autonegotiation.)  If the RPC ANEG bit is cleared, the selection * is controlled by the RPC SPEED and RPC DPLX bits. */static void smc_phy_configure(struct work_struct *work){	struct smc_local *lp =		container_of(work, struct smc_local, phy_configure);	struct net_device *dev = lp->dev;	void __iomem *ioaddr = lp->base;	int phyaddr = lp->mii.phy_id;	int my_phy_caps; /* My PHY capabilities */	int my_ad_caps; /* My Advertised capabilities */	int status;	DBG(3, "%s:smc_program_phy()\n", dev->name);	spin_lock_irq(&lp->lock);	/*	 * We should not be called if phy_type is zero.	 */	if (lp->phy_type == 0)		goto smc_phy_configure_exit;	if (smc_phy_reset(dev, phyaddr)) {		printk("%s: PHY reset timed out\n", dev->name);		goto smc_phy_configure_exit;	}	/*	 * Enable PHY Interrupts (for register 18)	 * Interrupts listed here are disabled	 */	smc_phy_write(dev, phyaddr, PHY_MASK_REG,		PHY_INT_LOSSSYNC | PHY_INT_CWRD | PHY_INT_SSD |		PHY_INT_ESD | PHY_INT_RPOL | PHY_INT_JAB |		PHY_INT_SPDDET | PHY_INT_DPLXDET);	/* Configure the Receive/Phy Control register */	SMC_SELECT_BANK(0);	SMC_SET_RPC(lp->rpc_cur_mode);	/* If the user requested no auto neg, then go set his request */	if (lp->mii.force_media) {		smc_phy_fixed(dev);		goto smc_phy_configure_exit;	}	/* Copy our capabilities from MII_BMSR to MII_ADVERTISE */	my_phy_caps = smc_phy_read(dev, phyaddr, MII_BMSR);	if (!(my_phy_caps & BMSR_ANEGCAPABLE)) {		printk(KERN_INFO "Auto negotiation NOT supported\n");		smc_phy_fixed(dev);		goto smc_phy_configure_exit;	}	my_ad_caps = ADVERTISE_CSMA; /* I am CSMA capable */	if (my_phy_caps & BMSR_100BASE4)		my_ad_caps |= ADVERTISE_100BASE4;	if (my_phy_caps & BMSR_100FULL)		my_ad_caps |= ADVERTISE_100FULL;	if (my_phy_caps & BMSR_100HALF)		my_ad_caps |= ADVERTISE_100HALF;	if (my_phy_caps & BMSR_10FULL)		my_ad_caps |= ADVERTISE_10FULL;	if (my_phy_caps & BMSR_10HALF)		my_ad_caps |= ADVERTISE_10HALF;	/* Disable capabilities not selected by our user */	if (lp->ctl_rspeed != 100)		my_ad_caps &= ~(ADVERTISE_100BASE4|ADVERTISE_100FULL|ADVERTISE_100HALF);	if (!lp->ctl_rfduplx)		my_ad_caps &= ~(ADVERTISE_100FULL|ADVERTISE_10FULL);	/* Update our Auto-Neg Advertisement Register */	smc_phy_write(dev, phyaddr, MII_ADVERTISE, my_ad_caps);	lp->mii.advertising = my_ad_caps;	/*	 * Read the register back.  Without this, it appears that when	 * auto-negotiation is restarted, sometimes it isn't ready and	 * the link does not come up.	 */	status = smc_phy_read(dev, phyaddr, MII_ADVERTISE);	DBG(2, "%s: phy caps=%x\n", dev->name, my_phy_caps);

⌨️ 快捷键说明

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