if_vr.c
来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 1,963 行 · 第 1/4 页
C
1,963 行
/* * 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_vr.c,v 1.7.2.2 1999/02/23 15:40:55 wpaul Exp $ *//* * VIA Rhine fast ethernet PCI NIC driver * * Supports various network adapters based on the VIA Rhine * and Rhine II PCI controllers, including the D-Link DFE530TX. * Datasheets are available at http://www.via.com.tw. * * Written by Bill Paul <wpaul@ctr.columbia.edu> * Electrical Engineering Department * Columbia University, New York City *//* * The VIA Rhine controllers are similar in some respects to the * the DEC tulip chips, except less complicated. The controller * uses an MII bus and an external physical layer interface. The * receiver has a one entry perfect filter and a 64-bit hash table * multicast filter. Transmit and receive descriptors are similar * to the tulip. * * The Rhine has a serious flaw in its transmit DMA mechanism: * transmit buffers must be longword aligned. Unfortunately, * FreeBSD doesn't guarantee that mbufs will be filled in starting * at longword boundaries, so we have to do a buffer copy before * transmission. */#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 <vm/vm.h> /* for vtophys */#include <vm/pmap.h> /* for vtophys */#include <machine/clock.h> /* for DELAY */#include <machine/bus_pio.h>#include <machine/bus_memio.h>#include <machine/bus.h>#include <pci/pcireg.h>#include <pci/pcivar.h>#define VR_USEIOSPACE/* #define VR_BACKGROUND_AUTONEG */#include <pci/if_vrreg.h>#ifndef lintstatic const char rcsid[] = "$Id: if_vr.c,v 1.7.2.2 1999/02/23 15:40:55 wpaul Exp $";#endif/* * Various supported device vendors/types and their names. */static struct vr_type vr_devs[] = { { VIA_VENDORID, VIA_DEVICEID_RHINE, "VIA VT3043 Rhine I 10/100BaseTX" }, { VIA_VENDORID, VIA_DEVICEID_RHINE_II, "VIA VT86C100A Rhine II 10/100BaseTX" }, { DELTA_VENDORID, DELTA_DEVICEID_RHINE_II, "Delta Electronics Rhine II 10/100BaseTX" }, { ADDTRON_VENDORID, ADDTRON_DEVICEID_RHINE_II, "Addtron Technology Rhine II 10/100BaseTX" }, { 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 vr_type vr_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 vr_count = 0;static const char *vr_probe __P((pcici_t, pcidi_t));static void vr_attach __P((pcici_t, int));static int vr_newbuf __P((struct vr_softc *, struct vr_chain_onefrag *));static int vr_encap __P((struct vr_softc *, struct vr_chain *, struct mbuf * ));static void vr_rxeof __P((struct vr_softc *));static void vr_rxeoc __P((struct vr_softc *));static void vr_txeof __P((struct vr_softc *));static void vr_txeoc __P((struct vr_softc *));static void vr_intr __P((void *));static void vr_start __P((struct ifnet *));static int vr_ioctl __P((struct ifnet *, u_long, caddr_t));static void vr_init __P((void *));static void vr_stop __P((struct vr_softc *));static void vr_watchdog __P((struct ifnet *));static void vr_shutdown __P((int, void *));static int vr_ifmedia_upd __P((struct ifnet *));static void vr_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));static void vr_mii_sync __P((struct vr_softc *));static void vr_mii_send __P((struct vr_softc *, u_int32_t, int));static int vr_mii_readreg __P((struct vr_softc *, struct vr_mii_frame *));static int vr_mii_writereg __P((struct vr_softc *, struct vr_mii_frame *));static u_int16_t vr_phy_readreg __P((struct vr_softc *, int));static void vr_phy_writereg __P((struct vr_softc *, u_int16_t, u_int16_t));static void vr_autoneg_xmit __P((struct vr_softc *));static void vr_autoneg_mii __P((struct vr_softc *, int, int));static void vr_setmode_mii __P((struct vr_softc *, int));static void vr_getmode_mii __P((struct vr_softc *));static void vr_setcfg __P((struct vr_softc *, u_int16_t));static u_int8_t vr_calchash __P((u_int8_t *));static void vr_setmulti __P((struct vr_softc *));static void vr_reset __P((struct vr_softc *));static int vr_list_rx_init __P((struct vr_softc *));static int vr_list_tx_init __P((struct vr_softc *));#define VR_SETBIT(sc, reg, x) \ CSR_WRITE_1(sc, reg, \ CSR_READ_1(sc, reg) | x)#define VR_CLRBIT(sc, reg, x) \ CSR_WRITE_1(sc, reg, \ CSR_READ_1(sc, reg) & ~x)#define VR_SETBIT16(sc, reg, x) \ CSR_WRITE_2(sc, reg, \ CSR_READ_2(sc, reg) | x)#define VR_CLRBIT16(sc, reg, x) \ CSR_WRITE_2(sc, reg, \ CSR_READ_2(sc, reg) & ~x)#define VR_SETBIT32(sc, reg, x) \ CSR_WRITE_4(sc, reg, \ CSR_READ_4(sc, reg) | x)#define VR_CLRBIT32(sc, reg, x) \ CSR_WRITE_4(sc, reg, \ CSR_READ_4(sc, reg) & ~x)#define SIO_SET(x) \ CSR_WRITE_1(sc, VR_MIICMD, \ CSR_READ_1(sc, VR_MIICMD) | x)#define SIO_CLR(x) \ CSR_WRITE_1(sc, VR_MIICMD, \ CSR_READ_1(sc, VR_MIICMD) & ~x)/* * Sync the PHYs by setting data bit and strobing the clock 32 times. */static void vr_mii_sync(sc) struct vr_softc *sc;{ register int i; SIO_SET(VR_MIICMD_DIR|VR_MIICMD_DATAIN); for (i = 0; i < 32; i++) { SIO_SET(VR_MIICMD_CLK); DELAY(1); SIO_CLR(VR_MIICMD_CLK); DELAY(1); } return;}/* * Clock a series of bits through the MII. */static void vr_mii_send(sc, bits, cnt) struct vr_softc *sc; u_int32_t bits; int cnt;{ int i; SIO_CLR(VR_MIICMD_CLK); for (i = (0x1 << (cnt - 1)); i; i >>= 1) { if (bits & i) { SIO_SET(VR_MIICMD_DATAIN); } else { SIO_CLR(VR_MIICMD_DATAIN); } DELAY(1); SIO_CLR(VR_MIICMD_CLK); DELAY(1); SIO_SET(VR_MIICMD_CLK); }}/* * Read an PHY register through the MII. */static int vr_mii_readreg(sc, frame) struct vr_softc *sc; struct vr_mii_frame *frame; { int i, ack, s; s = splimp(); /* * Set up frame for RX. */ frame->mii_stdelim = VR_MII_STARTDELIM; frame->mii_opcode = VR_MII_READOP; frame->mii_turnaround = 0; frame->mii_data = 0; CSR_WRITE_1(sc, VR_MIICMD, 0); VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM); /* * Turn on data xmit. */ SIO_SET(VR_MIICMD_DIR); vr_mii_sync(sc); /* * Send command/address info. */ vr_mii_send(sc, frame->mii_stdelim, 2); vr_mii_send(sc, frame->mii_opcode, 2); vr_mii_send(sc, frame->mii_phyaddr, 5); vr_mii_send(sc, frame->mii_regaddr, 5); /* Idle bit */ SIO_CLR((VR_MIICMD_CLK|VR_MIICMD_DATAIN)); DELAY(1); SIO_SET(VR_MIICMD_CLK); DELAY(1); /* Turn off xmit. */ SIO_CLR(VR_MIICMD_DIR); /* Check for ack */ SIO_CLR(VR_MIICMD_CLK); DELAY(1); SIO_SET(VR_MIICMD_CLK); DELAY(1); ack = CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAOUT; /* * 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++) { SIO_CLR(VR_MIICMD_CLK); DELAY(1); SIO_SET(VR_MIICMD_CLK); DELAY(1); } goto fail; } for (i = 0x8000; i; i >>= 1) { SIO_CLR(VR_MIICMD_CLK); DELAY(1); if (!ack) { if (CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAOUT) frame->mii_data |= i; DELAY(1); } SIO_SET(VR_MIICMD_CLK); DELAY(1); }fail: SIO_CLR(VR_MIICMD_CLK); DELAY(1); SIO_SET(VR_MIICMD_CLK); DELAY(1); splx(s); if (ack) return(1); return(0);}/* * Write to a PHY register through the MII. */static int vr_mii_writereg(sc, frame) struct vr_softc *sc; struct vr_mii_frame *frame; { int s; s = splimp(); CSR_WRITE_1(sc, VR_MIICMD, 0); VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM); /* * Set up frame for TX. */ frame->mii_stdelim = VR_MII_STARTDELIM; frame->mii_opcode = VR_MII_WRITEOP; frame->mii_turnaround = VR_MII_TURNAROUND; /* * Turn on data output. */ SIO_SET(VR_MIICMD_DIR); vr_mii_sync(sc); vr_mii_send(sc, frame->mii_stdelim, 2); vr_mii_send(sc, frame->mii_opcode, 2); vr_mii_send(sc, frame->mii_phyaddr, 5); vr_mii_send(sc, frame->mii_regaddr, 5); vr_mii_send(sc, frame->mii_turnaround, 2); vr_mii_send(sc, frame->mii_data, 16); /* Idle bit. */ SIO_SET(VR_MIICMD_CLK); DELAY(1); SIO_CLR(VR_MIICMD_CLK); DELAY(1); /* * Turn off xmit. */ SIO_CLR(VR_MIICMD_DIR); splx(s); return(0);}static u_int16_t vr_phy_readreg(sc, reg) struct vr_softc *sc; int reg;{ struct vr_mii_frame frame; bzero((char *)&frame, sizeof(frame)); frame.mii_phyaddr = sc->vr_phy_addr; frame.mii_regaddr = reg; vr_mii_readreg(sc, &frame); return(frame.mii_data);}static void vr_phy_writereg(sc, reg, data) struct vr_softc *sc; u_int16_t reg; u_int16_t data;{ struct vr_mii_frame frame; bzero((char *)&frame, sizeof(frame)); frame.mii_phyaddr = sc->vr_phy_addr; frame.mii_regaddr = reg; frame.mii_data = data; vr_mii_writereg(sc, &frame); return;}/* * Calculate CRC of a multicast group address, return the lower 6 bits. */static u_int8_t vr_calchash(addr) u_int8_t *addr;{ u_int32_t crc, carry; int i, j; u_int8_t c; /* Compute CRC for the address value. */ crc = 0xFFFFFFFF; /* initial value */ for (i = 0; i < 6; i++) { c = *(addr + i); for (j = 0; j < 8; j++) { carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01); crc <<= 1; c >>= 1; if (carry) crc = (crc ^ 0x04c11db6) | carry; } } /* return the filter bit position */ return((crc >> 26) & 0x0000003F);}/* * Program the 64-bit multicast hash filter. */static void vr_setmulti(sc) struct vr_softc *sc;{ struct ifnet *ifp; int h = 0; u_int32_t hashes[2] = { 0, 0 }; struct ifmultiaddr *ifma; u_int8_t rxfilt; int mcnt = 0; ifp = &sc->arpcom.ac_if; rxfilt = CSR_READ_1(sc, VR_RXCFG); if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { rxfilt |= VR_RXCFG_RX_MULTI; CSR_WRITE_1(sc, VR_RXCFG, rxfilt); CSR_WRITE_4(sc, VR_MAR0, 0xFFFFFFFF); CSR_WRITE_4(sc, VR_MAR1, 0xFFFFFFFF); return; } /* first, zot all the existing hash bits */ CSR_WRITE_4(sc, VR_MAR0, 0); CSR_WRITE_4(sc, VR_MAR1, 0); /* now program new ones */ for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL; ifma = ifma->ifma_link.le_next) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?