if_xl.c

来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 2,435 行 · 第 1/5 页

C
2,435
字号
/* * Copyright (c) 1997, 1998 *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *	This product includes software developed by Bill Paul. * 4. Neither the name of the author nor the names of any co-contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. * *	$Id: if_xl.c,v 1.22.2.12 1999/05/05 17:10:48 wpaul Exp $ *//* * 3Com 3c90x Etherlink XL PCI NIC driver * * Supports the 3Com "boomerang", "cyclone" and "hurricane" PCI * bus-master chips (3c90x cards and embedded controllers) including * the following: * * 3Com 3c900-TPO	10Mbps/RJ-45 * 3Com 3c900-COMBO	10Mbps/RJ-45,AUI,BNC * 3Com 3c905-TX	10/100Mbps/RJ-45 * 3Com 3c905-T4	10/100Mbps/RJ-45 * 3Com 3c900B-TPO	10Mbps/RJ-45 * 3Com 3c900B-COMBO	10Mbps/RJ-45,AUI,BNC * 3Com 3c900B-TPC	10Mbps/RJ-45,BNC * 3Com 3c900B-FL	10Mbps/Fiber-optic * 3Com 3c905B-COMBO	10/100Mbps/RJ-45,AUI,BNC * 3Com 3c905B-TX	10/100Mbps/RJ-45 * 3Com 3c905B-FL/FX	10/100Mbps/Fiber-optic * 3Com 3c980-TX	10/100Mbps server adapter * 3Com 3cSOHO100-TX	10/100Mbps/RJ-45 * Dell Optiplex GX1 on-board 3c918 10/100Mbps/RJ-45 * Dell Precision on-board 3c905B 10/100Mbps/RJ-45 * Dell Latitude laptop docking station embedded 3c905-TX * * Written by Bill Paul <wpaul@ctr.columbia.edu> * Electrical Engineering Department * Columbia University, New York City *//* * The 3c90x series chips use a bus-master DMA interface for transfering * packets to and from the controller chip. Some of the "vortex" cards * (3c59x) also supported a bus master mode, however for those chips * you could only DMA packets to/from a contiguous memory buffer. For * transmission this would mean copying the contents of the queued mbuf * chain into a an mbuf cluster and then DMAing the cluster. This extra * copy would sort of defeat the purpose of the bus master support for * any packet that doesn't fit into a single mbuf. * * By contrast, the 3c90x cards support a fragment-based bus master * mode where mbuf chains can be encapsulated using TX descriptors. * This is similar to other PCI chips such as the Texas Instruments * ThunderLAN and the Intel 82557/82558. * * The "vortex" driver (if_vx.c) happens to work for the "boomerang" * bus master chips because they maintain the old PIO interface for * backwards compatibility, but starting with the 3c905B and the * "cyclone" chips, the compatibility interface has been dropped. * Since using bus master DMA is a big win, we use this driver to * support the PCI "boomerang" chips even though they work with the * "vortex" driver in order to obtain better performance. * * This driver is in the /sys/pci directory because it only supports * PCI-based NICs. */#include "bpfilter.h"#include <sys/param.h>#include <sys/systm.h>#include <sys/sockio.h>#include <sys/mbuf.h>#include <sys/malloc.h>#include <sys/kernel.h>#include <sys/socket.h>#include <net/if.h>#include <net/if_arp.h>#include <net/ethernet.h>#include <net/if_dl.h>#include <net/if_media.h>#if NBPFILTER > 0#include <net/bpf.h>#endif#include "opt_bdg.h"#ifdef BRIDGE#include <net/bridge.h>#endif#include <vm/vm.h>              /* for vtophys */#include <vm/pmap.h>            /* for vtophys */#include <machine/clock.h>      /* for DELAY */#include <machine/bus_memio.h>#include <machine/bus_pio.h>#include <machine/bus.h>#include <pci/pcireg.h>#include <pci/pcivar.h>/* * The following #define causes the code to use PIO to access the * chip's registers instead of memory mapped mode. The reason PIO mode * is on by default is that the Etherlink XL manual seems to indicate * that only the newer revision chips (3c905B) support both PIO and * memory mapped access. Since we want to be compatible with the older * bus master chips, we use PIO here. If you comment this out, the * driver will use memory mapped I/O, which may be faster but which * might not work on some devices. */#define XL_USEIOSPACE/* * This #define controls the behavior of autonegotiation during the * bootstrap phase. It's possible to have the driver initiate an * autonegotiation session and then set a timeout which will cause the * autoneg results to be polled later, usually once the kernel has * finished booting. This is clever and all, but it can have bad side * effects in some cases, particularly where NFS is involved. For * example, if we're booting diskless with an NFS rootfs, the network * interface has to be up and running before we hit the mountroot() * code, otherwise mounting the rootfs will fail and we'll probably * panic. * * Consequently, the 'backgrounded' autoneg behavior is turned off * by default and we actually sit and wait 5 seconds for autonegotiation * to complete before proceeding with the other device probes. If you * choose to use the other behavior, you can uncomment this #define and * recompile. *//* #define XL_BACKGROUND_AUTONEG */#include <pci/if_xlreg.h>#if !defined(lint)static const char rcsid[] =	"$Id: if_xl.c,v 1.22.2.12 1999/05/05 17:10:48 wpaul Exp $";#endif/* * Various supported device vendors/types and their names. */static struct xl_type xl_devs[] = {	{ TC_VENDORID, TC_DEVICEID_BOOMERANG_10BT,		"3Com 3c900-TPO Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_BOOMERANG_10BT_COMBO,		"3Com 3c900-COMBO Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_BOOMERANG_10_100BT,		"3Com 3c905-TX Fast Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_BOOMERANG_100BT4,		"3Com 3c905-T4 Fast Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT,		"3Com 3c900B-TPO Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT_COMBO,		"3Com 3c900B-COMBO Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT_TPC,		"3Com 3c900B-TPC Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_CYCLONE_10FL,		"3Com 3c900B-FL Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_HURRICANE_10_100BT,		"3Com 3c905B-TX Fast Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_CYCLONE_10_100BT4,		"3Com 3c905B-T4 Fast Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_CYCLONE_10_100FX,		"3Com 3c905B-FX/SC Fast Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_CYCLONE_10_100_COMBO,		"3Com 3c905B-COMBO Fast Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_HURRICANE_10_100BT_SERV,		"3Com 3c980 Fast Etherlink XL" },	{ TC_VENDORID, TC_DEVICEID_HURRICANE_SOHO100TX,		"3Com 3cSOHO100-TX OfficeConnect" },	{ 0, 0, NULL }};/* * Various supported PHY vendors/types and their names. Note that * this driver will work with pretty much any MII-compliant PHY, * so failure to positively identify the chip is not a fatal error. */static struct xl_type xl_phys[] = {	{ TI_PHY_VENDORID, TI_PHY_10BT, "<TI ThunderLAN 10BT (internal)>" },	{ TI_PHY_VENDORID, TI_PHY_100VGPMI, "<TI TNETE211 100VG Any-LAN>" },	{ NS_PHY_VENDORID, NS_PHY_83840A, "<National Semiconductor DP83840A>"},	{ LEVEL1_PHY_VENDORID, LEVEL1_PHY_LXT970, "<Level 1 LXT970>" }, 	{ INTEL_PHY_VENDORID, INTEL_PHY_82555, "<Intel 82555>" },	{ SEEQ_PHY_VENDORID, SEEQ_PHY_80220, "<SEEQ 80220>" },	{ 0, 0, "<MII-compliant physical interface>" }};static unsigned long xl_count = 0;static const char *xl_probe	__P((pcici_t, pcidi_t));static void xl_attach		__P((pcici_t, int));static int xl_newbuf		__P((struct xl_softc *,						struct xl_chain_onefrag *));static void xl_stats_update	__P((void *));static int xl_encap		__P((struct xl_softc *, struct xl_chain *,						struct mbuf * ));static void xl_rxeof		__P((struct xl_softc *));static void xl_txeof		__P((struct xl_softc *));static void xl_txeoc		__P((struct xl_softc *));static void xl_intr		__P((void *));static void xl_start		__P((struct ifnet *));static int xl_ioctl		__P((struct ifnet *, u_long, caddr_t));static void xl_init		__P((void *));static void xl_stop		__P((struct xl_softc *));static void xl_watchdog		__P((struct ifnet *));static void xl_shutdown		__P((int, void *));static int xl_ifmedia_upd	__P((struct ifnet *));static void xl_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));static int xl_eeprom_wait	__P((struct xl_softc *));static int xl_read_eeprom	__P((struct xl_softc *, caddr_t, int,							int, int));static void xl_mii_sync		__P((struct xl_softc *));static void xl_mii_send		__P((struct xl_softc *, u_int32_t, int));static int xl_mii_readreg	__P((struct xl_softc *, struct xl_mii_frame *));static int xl_mii_writereg	__P((struct xl_softc *, struct xl_mii_frame *));static u_int16_t xl_phy_readreg	__P((struct xl_softc *, int));static void xl_phy_writereg	__P((struct xl_softc *, int, int));static void xl_autoneg_xmit	__P((struct xl_softc *));static void xl_autoneg_mii	__P((struct xl_softc *, int, int));static void xl_setmode_mii	__P((struct xl_softc *, int));static void xl_getmode_mii	__P((struct xl_softc *));static void xl_setmode		__P((struct xl_softc *, int));static u_int8_t xl_calchash	__P((caddr_t));static void xl_setmulti		__P((struct xl_softc *));static void xl_setmulti_hash	__P((struct xl_softc *));static void xl_reset		__P((struct xl_softc *));static int xl_list_rx_init	__P((struct xl_softc *));static int xl_list_tx_init	__P((struct xl_softc *));static void xl_wait		__P((struct xl_softc *));static void xl_mediacheck	__P((struct xl_softc *));#ifdef notdefstatic void xl_testpacket	__P((struct xl_softc *));#endif/* * Murphy's law says that it's possible the chip can wedge and * the 'command in progress' bit may never clear. Hence, we wait * only a finite amount of time to avoid getting caught in an * infinite loop. Normally this delay routine would be a macro, * but it isn't called during normal operation so we can afford * to make it a function. */static void xl_wait(sc)	struct xl_softc		*sc;{	register int		i;	for (i = 0; i < XL_TIMEOUT; i++) {		if (!(CSR_READ_2(sc, XL_STATUS) & XL_STAT_CMDBUSY))			break;	}	if (i == XL_TIMEOUT)		printf("xl%d: command never completed!\n", sc->xl_unit);	return;}/* * MII access routines are provided for adapters with external * PHYs (3c905-TX, 3c905-T4, 3c905B-T4) and those with built-in * autoneg logic that's faked up to look like a PHY (3c905B-TX). * Note: if you don't perform the MDIO operations just right, * it's possible to end up with code that works correctly with * some chips/CPUs/processor speeds/bus speeds/etc but not * with others. */#define MII_SET(x)					\	CSR_WRITE_2(sc, XL_W4_PHY_MGMT,			\		CSR_READ_2(sc, XL_W4_PHY_MGMT) | x)#define MII_CLR(x)					\	CSR_WRITE_2(sc, XL_W4_PHY_MGMT,			\		CSR_READ_2(sc, XL_W4_PHY_MGMT) & ~x)/* * Sync the PHYs by setting data bit and strobing the clock 32 times. */static void xl_mii_sync(sc)	struct xl_softc		*sc;{	register int		i;	XL_SEL_WIN(4);	MII_SET(XL_MII_DIR|XL_MII_DATA);	for (i = 0; i < 32; i++) {		MII_SET(XL_MII_CLK);		DELAY(1);		MII_CLR(XL_MII_CLK);		DELAY(1);	}	return;}/* * Clock a series of bits through the MII. */static void xl_mii_send(sc, bits, cnt)	struct xl_softc		*sc;	u_int32_t		bits;	int			cnt;{	int			i;	XL_SEL_WIN(4);	MII_CLR(XL_MII_CLK);	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {                if (bits & i) {			MII_SET(XL_MII_DATA);                } else {			MII_CLR(XL_MII_DATA);                }		DELAY(1);		MII_CLR(XL_MII_CLK);		DELAY(1);		MII_SET(XL_MII_CLK);	}}/* * Read an PHY register through the MII. */static int xl_mii_readreg(sc, frame)	struct xl_softc		*sc;	struct xl_mii_frame	*frame;	{	int			i, ack, s;	s = splimp();	/*	 * Set up frame for RX.	 */	frame->mii_stdelim = XL_MII_STARTDELIM;	frame->mii_opcode = XL_MII_READOP;	frame->mii_turnaround = 0;	frame->mii_data = 0;		/*	 * Select register window 4.	 */	XL_SEL_WIN(4);	CSR_WRITE_2(sc, XL_W4_PHY_MGMT, 0);	/* 	 * Turn on data xmit.	 */	MII_SET(XL_MII_DIR);	xl_mii_sync(sc);	/*	 * Send command/address info.	 */	xl_mii_send(sc, frame->mii_stdelim, 2);	xl_mii_send(sc, frame->mii_opcode, 2);	xl_mii_send(sc, frame->mii_phyaddr, 5);	xl_mii_send(sc, frame->mii_regaddr, 5);	/* Idle bit */	MII_CLR((XL_MII_CLK|XL_MII_DATA));	DELAY(1);	MII_SET(XL_MII_CLK);	DELAY(1);	/* Turn off xmit. */	MII_CLR(XL_MII_DIR);	/* Check for ack */	MII_CLR(XL_MII_CLK);	DELAY(1);	MII_SET(XL_MII_CLK);	DELAY(1);	ack = CSR_READ_2(sc, XL_W4_PHY_MGMT) & XL_MII_DATA;	/*	 * Now try reading data bits. If the ack failed, we still	 * need to clock through 16 cycles to keep the PHY(s) in sync.	 */	if (ack) {		for(i = 0; i < 16; i++) {			MII_CLR(XL_MII_CLK);			DELAY(1);			MII_SET(XL_MII_CLK);			DELAY(1);		}		goto fail;	}	for (i = 0x8000; i; i >>= 1) {		MII_CLR(XL_MII_CLK);		DELAY(1);		if (!ack) {			if (CSR_READ_2(sc, XL_W4_PHY_MGMT) & XL_MII_DATA)				frame->mii_data |= i;			DELAY(1);		}		MII_SET(XL_MII_CLK);		DELAY(1);	}fail:	MII_CLR(XL_MII_CLK);	DELAY(1);	MII_SET(XL_MII_CLK);	DELAY(1);	splx(s);	if (ack)		return(1);	return(0);}/* * Write to a PHY register through the MII. */static int xl_mii_writereg(sc, frame)	struct xl_softc		*sc;	struct xl_mii_frame	*frame;	{	int			s;	s = splimp();	/*	 * Set up frame for TX.	 */	frame->mii_stdelim = XL_MII_STARTDELIM;	frame->mii_opcode = XL_MII_WRITEOP;	frame->mii_turnaround = XL_MII_TURNAROUND;		/*	 * Select the window 4.	 */	XL_SEL_WIN(4);	/* 	 * Turn on data output.	 */	MII_SET(XL_MII_DIR);	xl_mii_sync(sc);	xl_mii_send(sc, frame->mii_stdelim, 2);	xl_mii_send(sc, frame->mii_opcode, 2);	xl_mii_send(sc, frame->mii_phyaddr, 5);	xl_mii_send(sc, frame->mii_regaddr, 5);	xl_mii_send(sc, frame->mii_turnaround, 2);

⌨️ 快捷键说明

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