sb1250-mac.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 2,624 行 · 第 1/5 页

C
2,624 行
字号
		 * above will clear the timer and counter, and you		 * won't get another interrupt until a packet shows		 * up to start the timer again.  Testing		 * EOP_SEEN here takes care of this case.		 * (EOP_SEEN is part of M_MAC_INT_CHANNEL << S_MAC_RX_CH0)		 */		 				if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {			sbdma_rx_process(sc,&(sc->sbm_rxdma));		}	}	return IRQ_RETVAL(handled);}/********************************************************************** *  SBMAC_START_TX(skb,dev) *   *  Start output on the specified interface.  Basically, we  *  queue as many buffers as we can until the ring fills up, or *  we run off the end of the queue, whichever comes first. *   *  Input parameters:  *  	    *  	    *  Return value: *  	   nothing ********************************************************************* */static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev){	struct sbmac_softc *sc = netdev_priv(dev);		/* lock eth irq */	spin_lock_irq (&sc->sbm_lock);		/*	 * Put the buffer on the transmit ring.  If we 	 * don't have room, stop the queue.	 */		if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {		/* XXX save skb that we could not send */		netif_stop_queue(dev);		spin_unlock_irq(&sc->sbm_lock);		return 1;	}		dev->trans_start = jiffies;		spin_unlock_irq (&sc->sbm_lock);		return 0;}/********************************************************************** *  SBMAC_SETMULTI(sc) *   *  Reprogram the multicast table into the hardware, given *  the list of multicasts associated with the interface *  structure. *   *  Input parameters:  *  	   sc - softc *  	    *  Return value: *  	   nothing ********************************************************************* */static void sbmac_setmulti(struct sbmac_softc *sc){	uint64_t reg;	sbmac_port_t port;	int idx;	struct dev_mc_list *mclist;	struct net_device *dev = sc->sbm_dev;		/* 	 * Clear out entire multicast table.  We do this by nuking	 * the entire hash table and all the direct matches except	 * the first one, which is used for our station address 	 */		for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {		port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));		SBMAC_WRITECSR(port,0);		}		for (idx = 0; idx < MAC_HASH_COUNT; idx++) {		port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));		SBMAC_WRITECSR(port,0);		}		/*	 * Clear the filter to say we don't want any multicasts.	 */		reg = SBMAC_READCSR(sc->sbm_rxfilter);	reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);	SBMAC_WRITECSR(sc->sbm_rxfilter,reg);		if (dev->flags & IFF_ALLMULTI) {		/* 		 * Enable ALL multicasts.  Do this by inverting the 		 * multicast enable bit. 		 */		reg = SBMAC_READCSR(sc->sbm_rxfilter);		reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);		SBMAC_WRITECSR(sc->sbm_rxfilter,reg);		return;	}		/* 	 * Progam new multicast entries.  For now, only use the	 * perfect filter.  In the future we'll need to use the	 * hash filter if the perfect filter overflows	 */		/* XXX only using perfect filter for now, need to use hash	 * XXX if the table overflows */		idx = 1;		/* skip station address */	mclist = dev->mc_list;	while (mclist && (idx < MAC_ADDR_COUNT)) {		reg = sbmac_addr2reg(mclist->dmi_addr);		port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));		SBMAC_WRITECSR(port,reg);		idx++;		mclist = mclist->next;	}		/*		 * Enable the "accept multicast bits" if we programmed at least one	 * multicast. 	 */		if (idx > 1) {		reg = SBMAC_READCSR(sc->sbm_rxfilter);		reg |= M_MAC_MCAST_EN;		SBMAC_WRITECSR(sc->sbm_rxfilter,reg);	}}#if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR)/********************************************************************** *  SBMAC_PARSE_XDIGIT(str) *   *  Parse a hex digit, returning its value *   *  Input parameters:  *  	   str - character *  	    *  Return value: *  	   hex value, or -1 if invalid ********************************************************************* */static int sbmac_parse_xdigit(char str){	int digit;		if ((str >= '0') && (str <= '9'))		digit = str - '0';	else if ((str >= 'a') && (str <= 'f'))		digit = str - 'a' + 10;	else if ((str >= 'A') && (str <= 'F'))		digit = str - 'A' + 10;	else		return -1;		return digit;}/********************************************************************** *  SBMAC_PARSE_HWADDR(str,hwaddr) *   *  Convert a string in the form xx:xx:xx:xx:xx:xx into a 6-byte *  Ethernet address. *   *  Input parameters:  *  	   str - string *  	   hwaddr - pointer to hardware address *  	    *  Return value: *  	   0 if ok, else -1 ********************************************************************* */static int sbmac_parse_hwaddr(char *str, unsigned char *hwaddr){	int digit1,digit2;	int idx = 6;		while (*str && (idx > 0)) {		digit1 = sbmac_parse_xdigit(*str);		if (digit1 < 0)			return -1;		str++;		if (!*str)			return -1;				if ((*str == ':') || (*str == '-')) {			digit2 = digit1;			digit1 = 0;		}		else {			digit2 = sbmac_parse_xdigit(*str);			if (digit2 < 0)				return -1;			str++;		}				*hwaddr++ = (digit1 << 4) | digit2;		idx--;				if (*str == '-')			str++;		if (*str == ':')			str++;	}	return 0;}#endifstatic int sb1250_change_mtu(struct net_device *_dev, int new_mtu){	if (new_mtu >  ENET_PACKET_SIZE)		return -EINVAL;	_dev->mtu = new_mtu;	printk(KERN_INFO "changing the mtu to %d\n", new_mtu);	return 0;}/********************************************************************** *  SBMAC_INIT(dev) *   *  Attach routine - init hardware and hook ourselves into linux *   *  Input parameters:  *  	   dev - net_device structure *  	    *  Return value: *  	   status ********************************************************************* */static int sbmac_init(struct net_device *dev, int idx){	struct sbmac_softc *sc;	unsigned char *eaddr;	uint64_t ea_reg;	int i;	int err;		sc = netdev_priv(dev);		/* Determine controller base address */		sc->sbm_base = IOADDR(dev->base_addr);	sc->sbm_dev = dev;	sc->sbe_idx = idx;		eaddr = sc->sbm_hwaddr;		/* 	 * Read the ethernet address.  The firwmare left this programmed	 * for us in the ethernet address register for each mac.	 */		ea_reg = SBMAC_READCSR(sc->sbm_base + R_MAC_ETHERNET_ADDR);	SBMAC_WRITECSR(sc->sbm_base + R_MAC_ETHERNET_ADDR, 0);	for (i = 0; i < 6; i++) {		eaddr[i] = (uint8_t) (ea_reg & 0xFF);		ea_reg >>= 8;	}		for (i = 0; i < 6; i++) {		dev->dev_addr[i] = eaddr[i];	}			/*	 * Init packet size 	 */		sc->sbm_buffersize = ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN;	/* 	 * Initialize context (get pointers to registers and stuff), then	 * allocate the memory for the descriptor tables.	 */		sbmac_initctx(sc);		/*	 * Set up Linux device callins	 */		spin_lock_init(&(sc->sbm_lock));		dev->open               = sbmac_open;	dev->hard_start_xmit    = sbmac_start_tx;	dev->stop               = sbmac_close;	dev->get_stats          = sbmac_get_stats;	dev->set_multicast_list = sbmac_set_rx_mode;	dev->do_ioctl           = sbmac_mii_ioctl;	dev->tx_timeout         = sbmac_tx_timeout;	dev->watchdog_timeo     = TX_TIMEOUT;	dev->change_mtu         = sb1250_change_mtu;	/* This is needed for PASS2 for Rx H/W checksum feature */	sbmac_set_iphdr_offset(sc);	err = register_netdev(dev);	if (err)		goto out_uninit;	/*	 * Display Ethernet address (this is called during the config	 * process so we need to finish off the config message that	 * was being displayed)	 */	printk(KERN_INFO	       "%s: SiByte Ethernet at 0x%08lX, address: %02X:%02X:%02X:%02X:%02X:%02X\n", 	       dev->name, dev->base_addr,	       eaddr[0],eaddr[1],eaddr[2],eaddr[3],eaddr[4],eaddr[5]);		return 0;out_uninit:	sbmac_uninitctx(sc);	return err;}static int sbmac_open(struct net_device *dev){	struct sbmac_softc *sc = netdev_priv(dev);		if (debug > 1) {		printk(KERN_DEBUG "%s: sbmac_open() irq %d.\n", dev->name, dev->irq);	}		/* 	 * map/route interrupt (clear status first, in case something	 * weird is pending; we haven't initialized the mac registers	 * yet)	 */	SBMAC_READCSR(sc->sbm_isr);	if (request_irq(dev->irq, &sbmac_intr, SA_SHIRQ, dev->name, dev))		return -EBUSY;	/*	 * Configure default speed 	 */	sbmac_mii_poll(sc,noisy_mii);		/*	 * Turn on the channel	 */	sbmac_set_channel_state(sc,sbmac_state_on);		/*	 * XXX Station address is in dev->dev_addr	 */		if (dev->if_port == 0)		dev->if_port = 0; 		netif_start_queue(dev);		sbmac_set_rx_mode(dev);		/* Set the timer to check for link beat. */	init_timer(&sc->sbm_timer);	sc->sbm_timer.expires = jiffies + 2 * HZ/100;	sc->sbm_timer.data = (unsigned long)dev;	sc->sbm_timer.function = &sbmac_timer;	add_timer(&sc->sbm_timer);		return 0;}static int sbmac_mii_poll(struct sbmac_softc *s,int noisy){    int bmsr,bmcr,k1stsr,anlpar;    int chg;    char buffer[100];    char *p = buffer;    /* Read the mode status and mode control registers. */    bmsr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMSR);    bmcr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMCR);    /* get the link partner status */    anlpar = sbmac_mii_read(s,s->sbm_phys[0],MII_ANLPAR);    /* if supported, read the 1000baseT register */    if (bmsr & BMSR_1000BT_XSR) {	k1stsr = sbmac_mii_read(s,s->sbm_phys[0],MII_K1STSR);	}    else {	k1stsr = 0;	}    chg = 0;    if ((bmsr & BMSR_LINKSTAT) == 0) {	/*	 * If link status is down, clear out old info so that when	 * it comes back up it will force us to reconfigure speed	 */	s->sbm_phy_oldbmsr = 0;	s->sbm_phy_oldanlpar = 0;	s->sbm_phy_oldk1stsr = 0;	return 0;	}    if ((s->sbm_phy_oldbmsr != bmsr) ||	(s->sbm_phy_oldanlpar != anlpar) ||	(s->sbm_phy_oldk1stsr != k1stsr)) {	if (debug > 1) {	    printk(KERN_DEBUG "%s: bmsr:%x/%x anlpar:%x/%x  k1stsr:%x/%x\n",	       s->sbm_dev->name,	       s->sbm_phy_oldbmsr,bmsr,	       s->sbm_phy_oldanlpar,anlpar,	       s->sbm_phy_oldk1stsr,k1stsr);	    }	s->sbm_phy_oldbmsr = bmsr;	s->sbm_phy_oldanlpar = anlpar;	s->sbm_phy_oldk1stsr = k1stsr;	chg = 1;	}    if (chg == 0)	    return 0;    p += sprintf(p,"Link speed: ");    if (k1stsr & K1STSR_LP1KFD) {	s->sbm_speed = sbmac_speed_1000;	s->sbm_duplex = sbmac_duplex_full;	s->sbm_fc = sbmac_fc_frame;	p += sprintf(p,"1000BaseT FDX");	}    else if (k1stsr & K1STSR_LP1KHD) {	s->sbm_speed = sbmac_speed_1000;	s->sbm_duplex = sbmac_duplex_half;	s->sbm_fc = sbmac_fc_disabled;	p += sprintf(p,"1000BaseT HDX");	}    else if (anlpar & ANLPAR_TXFD) {	s->sbm_speed = sbmac_speed_100;	s->sbm_duplex = sbmac_duplex_full;	s->sbm_fc = (anlpar & ANLPAR_PAUSE) ? sbmac_fc_frame : sbmac_fc_disabled;	p += sprintf(p,"100BaseT FDX");	}    else if (anlpar & ANLPAR_TXHD) {	s->sbm_speed = sbmac_speed_100;	s->sbm_duplex = sbmac_duplex_half;	s->sbm_fc = sbmac_fc_disabled;	p += sprintf(p,"100BaseT HDX");	}    else if (anlpar & ANLPAR_10FD) {	s->sbm_speed = sbmac_speed_10;	s->sbm_duplex = sbmac_duplex_full;	s->sbm_fc = sbmac_fc_frame;	p += sprintf(p,"10BaseT FDX");	}    else if (anlpar & ANLPAR_10HD) {	s->sbm_speed = sbmac_speed_10;	s->sbm_duplex = sbmac_duplex_half;	s->sbm_fc = sbmac_fc_collision;	p += sprintf(p,"10BaseT HDX");	}    else {	p += sprintf(p,"Unknown");	}    if (noisy) {	    printk(KERN_INFO "%s: %s\n",s->sbm_dev->name,buffer);	    }    return 1;}static void sbmac_timer(unsigned long data){	struct net_device *dev = (struct net_device *)data;	struct sbmac_softc *sc = netdev_priv(dev);	int next_tick = HZ;	int mii_status;	spin_lock_irq (&sc->sbm_lock);		/* make IFF_RUNNING follow the MII status bit "Link established" */	mii_status = sbmac_mii_read(sc, sc->sbm_phys[0], MII_BMSR);		if ( (mii_status & BMSR_LINKSTAT) != (sc->sbm_phy_oldlinkstat) ) {    	        sc->sbm_phy_oldlinkstat = mii_status & BMSR_LINKSTAT;		if (mii_status & BMSR_LINKSTAT) {			netif_carrier_on(dev);		}		else {			netif_carrier_off(dev);			}	}		/*	 * Poll the PHY to see what speed we should be running at	 */	if (sbmac_mii_poll(sc,noisy_mii)) {		if (sc-

⌨️ 快捷键说明

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