if_vr.c revision 71271
141502Swpaul/* 241502Swpaul * Copyright (c) 1997, 1998 341502Swpaul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 441502Swpaul * 541502Swpaul * Redistribution and use in source and binary forms, with or without 641502Swpaul * modification, are permitted provided that the following conditions 741502Swpaul * are met: 841502Swpaul * 1. Redistributions of source code must retain the above copyright 941502Swpaul * notice, this list of conditions and the following disclaimer. 1041502Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1141502Swpaul * notice, this list of conditions and the following disclaimer in the 1241502Swpaul * documentation and/or other materials provided with the distribution. 1341502Swpaul * 3. All advertising materials mentioning features or use of this software 1441502Swpaul * must display the following acknowledgement: 1541502Swpaul * This product includes software developed by Bill Paul. 1641502Swpaul * 4. Neither the name of the author nor the names of any co-contributors 1741502Swpaul * may be used to endorse or promote products derived from this software 1841502Swpaul * without specific prior written permission. 1941502Swpaul * 2041502Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2141502Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2241502Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2341502Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 2441502Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2541502Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2641502Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2741502Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2841502Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2941502Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3041502Swpaul * THE POSSIBILITY OF SUCH DAMAGE. 3141502Swpaul * 3250477Speter * $FreeBSD: head/sys/dev/vr/if_vr.c 71271 2001-01-19 23:55:07Z wpaul $ 3341502Swpaul */ 3441502Swpaul 3541502Swpaul/* 3641502Swpaul * VIA Rhine fast ethernet PCI NIC driver 3741502Swpaul * 3841502Swpaul * Supports various network adapters based on the VIA Rhine 3941502Swpaul * and Rhine II PCI controllers, including the D-Link DFE530TX. 4041502Swpaul * Datasheets are available at http://www.via.com.tw. 4141502Swpaul * 4241502Swpaul * Written by Bill Paul <wpaul@ctr.columbia.edu> 4341502Swpaul * Electrical Engineering Department 4441502Swpaul * Columbia University, New York City 4541502Swpaul */ 4641502Swpaul 4741502Swpaul/* 4841502Swpaul * The VIA Rhine controllers are similar in some respects to the 4941502Swpaul * the DEC tulip chips, except less complicated. The controller 5041502Swpaul * uses an MII bus and an external physical layer interface. The 5141502Swpaul * receiver has a one entry perfect filter and a 64-bit hash table 5241502Swpaul * multicast filter. Transmit and receive descriptors are similar 5341502Swpaul * to the tulip. 5441502Swpaul * 5541502Swpaul * The Rhine has a serious flaw in its transmit DMA mechanism: 5641502Swpaul * transmit buffers must be longword aligned. Unfortunately, 5741502Swpaul * FreeBSD doesn't guarantee that mbufs will be filled in starting 5841502Swpaul * at longword boundaries, so we have to do a buffer copy before 5941502Swpaul * transmission. 6041502Swpaul */ 6141502Swpaul 6241502Swpaul#include <sys/param.h> 6341502Swpaul#include <sys/systm.h> 6441502Swpaul#include <sys/sockio.h> 6541502Swpaul#include <sys/mbuf.h> 6641502Swpaul#include <sys/malloc.h> 6741502Swpaul#include <sys/kernel.h> 6841502Swpaul#include <sys/socket.h> 6941502Swpaul 7041502Swpaul#include <net/if.h> 7141502Swpaul#include <net/if_arp.h> 7241502Swpaul#include <net/ethernet.h> 7341502Swpaul#include <net/if_dl.h> 7441502Swpaul#include <net/if_media.h> 7541502Swpaul 7641502Swpaul#include <net/bpf.h> 7741502Swpaul 7841502Swpaul#include <vm/vm.h> /* for vtophys */ 7941502Swpaul#include <vm/pmap.h> /* for vtophys */ 8041502Swpaul#include <machine/bus_pio.h> 8141502Swpaul#include <machine/bus_memio.h> 8241502Swpaul#include <machine/bus.h> 8349610Swpaul#include <machine/resource.h> 8449610Swpaul#include <sys/bus.h> 8549610Swpaul#include <sys/rman.h> 8641502Swpaul 8751432Swpaul#include <dev/mii/mii.h> 8851432Swpaul#include <dev/mii/miivar.h> 8951432Swpaul 9041502Swpaul#include <pci/pcireg.h> 9141502Swpaul#include <pci/pcivar.h> 9241502Swpaul 9341502Swpaul#define VR_USEIOSPACE 9441502Swpaul 9541502Swpaul#include <pci/if_vrreg.h> 9641502Swpaul 9759758SpeterMODULE_DEPEND(vr, miibus, 1, 1, 1); 9859758Speter 9951432Swpaul/* "controller miibus0" required. See GENERIC if you get errors here. */ 10051432Swpaul#include "miibus_if.h" 10151432Swpaul 10241502Swpaul#ifndef lint 10341591Sarchiestatic const char rcsid[] = 10450477Speter "$FreeBSD: head/sys/dev/vr/if_vr.c 71271 2001-01-19 23:55:07Z wpaul $"; 10541502Swpaul#endif 10641502Swpaul 10741502Swpaul/* 10841502Swpaul * Various supported device vendors/types and their names. 10941502Swpaul */ 11041502Swpaulstatic struct vr_type vr_devs[] = { 11141502Swpaul { VIA_VENDORID, VIA_DEVICEID_RHINE, 11241502Swpaul "VIA VT3043 Rhine I 10/100BaseTX" }, 11341502Swpaul { VIA_VENDORID, VIA_DEVICEID_RHINE_II, 11441502Swpaul "VIA VT86C100A Rhine II 10/100BaseTX" }, 11562653Swpaul { VIA_VENDORID, VIA_DEVICEID_RHINE_II_2, 11662653Swpaul "VIA VT6102 Rhine II 10/100BaseTX" }, 11744238Swpaul { DELTA_VENDORID, DELTA_DEVICEID_RHINE_II, 11844238Swpaul "Delta Electronics Rhine II 10/100BaseTX" }, 11944238Swpaul { ADDTRON_VENDORID, ADDTRON_DEVICEID_RHINE_II, 12044238Swpaul "Addtron Technology Rhine II 10/100BaseTX" }, 12141502Swpaul { 0, 0, NULL } 12241502Swpaul}; 12341502Swpaul 12449610Swpaulstatic int vr_probe __P((device_t)); 12549610Swpaulstatic int vr_attach __P((device_t)); 12649610Swpaulstatic int vr_detach __P((device_t)); 12741502Swpaul 12841502Swpaulstatic int vr_newbuf __P((struct vr_softc *, 12949610Swpaul struct vr_chain_onefrag *, 13049610Swpaul struct mbuf *)); 13141502Swpaulstatic int vr_encap __P((struct vr_softc *, struct vr_chain *, 13241502Swpaul struct mbuf * )); 13341502Swpaul 13441502Swpaulstatic void vr_rxeof __P((struct vr_softc *)); 13541502Swpaulstatic void vr_rxeoc __P((struct vr_softc *)); 13641502Swpaulstatic void vr_txeof __P((struct vr_softc *)); 13741502Swpaulstatic void vr_txeoc __P((struct vr_softc *)); 13851432Swpaulstatic void vr_tick __P((void *)); 13941502Swpaulstatic void vr_intr __P((void *)); 14041502Swpaulstatic void vr_start __P((struct ifnet *)); 14141502Swpaulstatic int vr_ioctl __P((struct ifnet *, u_long, caddr_t)); 14241502Swpaulstatic void vr_init __P((void *)); 14341502Swpaulstatic void vr_stop __P((struct vr_softc *)); 14441502Swpaulstatic void vr_watchdog __P((struct ifnet *)); 14549610Swpaulstatic void vr_shutdown __P((device_t)); 14641502Swpaulstatic int vr_ifmedia_upd __P((struct ifnet *)); 14741502Swpaulstatic void vr_ifmedia_sts __P((struct ifnet *, struct ifmediareq *)); 14841502Swpaul 14941502Swpaulstatic void vr_mii_sync __P((struct vr_softc *)); 15041502Swpaulstatic void vr_mii_send __P((struct vr_softc *, u_int32_t, int)); 15141502Swpaulstatic int vr_mii_readreg __P((struct vr_softc *, struct vr_mii_frame *)); 15241502Swpaulstatic int vr_mii_writereg __P((struct vr_softc *, struct vr_mii_frame *)); 15351432Swpaulstatic int vr_miibus_readreg __P((device_t, int, int)); 15451432Swpaulstatic int vr_miibus_writereg __P((device_t, int, int, int)); 15551432Swpaulstatic void vr_miibus_statchg __P((device_t)); 15641502Swpaul 15751432Swpaulstatic void vr_setcfg __P((struct vr_softc *, int)); 15841502Swpaulstatic u_int8_t vr_calchash __P((u_int8_t *)); 15941502Swpaulstatic void vr_setmulti __P((struct vr_softc *)); 16041502Swpaulstatic void vr_reset __P((struct vr_softc *)); 16141502Swpaulstatic int vr_list_rx_init __P((struct vr_softc *)); 16241502Swpaulstatic int vr_list_tx_init __P((struct vr_softc *)); 16341502Swpaul 16449610Swpaul#ifdef VR_USEIOSPACE 16549610Swpaul#define VR_RES SYS_RES_IOPORT 16649610Swpaul#define VR_RID VR_PCI_LOIO 16749610Swpaul#else 16849610Swpaul#define VR_RES SYS_RES_MEMORY 16949610Swpaul#define VR_RID VR_PCI_LOMEM 17049610Swpaul#endif 17149610Swpaul 17249610Swpaulstatic device_method_t vr_methods[] = { 17349610Swpaul /* Device interface */ 17449610Swpaul DEVMETHOD(device_probe, vr_probe), 17549610Swpaul DEVMETHOD(device_attach, vr_attach), 17649610Swpaul DEVMETHOD(device_detach, vr_detach), 17749610Swpaul DEVMETHOD(device_shutdown, vr_shutdown), 17851432Swpaul 17951432Swpaul /* bus interface */ 18051432Swpaul DEVMETHOD(bus_print_child, bus_generic_print_child), 18151432Swpaul DEVMETHOD(bus_driver_added, bus_generic_driver_added), 18251432Swpaul 18351432Swpaul /* MII interface */ 18451432Swpaul DEVMETHOD(miibus_readreg, vr_miibus_readreg), 18551432Swpaul DEVMETHOD(miibus_writereg, vr_miibus_writereg), 18651432Swpaul DEVMETHOD(miibus_statchg, vr_miibus_statchg), 18751432Swpaul 18849610Swpaul { 0, 0 } 18949610Swpaul}; 19049610Swpaul 19149610Swpaulstatic driver_t vr_driver = { 19251455Swpaul "vr", 19349610Swpaul vr_methods, 19449610Swpaul sizeof(struct vr_softc) 19549610Swpaul}; 19649610Swpaul 19749610Swpaulstatic devclass_t vr_devclass; 19849610Swpaul 19951533SwpaulDRIVER_MODULE(if_vr, pci, vr_driver, vr_devclass, 0, 0); 20051473SwpaulDRIVER_MODULE(miibus, vr, miibus_driver, miibus_devclass, 0, 0); 20149610Swpaul 20241502Swpaul#define VR_SETBIT(sc, reg, x) \ 20341502Swpaul CSR_WRITE_1(sc, reg, \ 20441502Swpaul CSR_READ_1(sc, reg) | x) 20541502Swpaul 20641502Swpaul#define VR_CLRBIT(sc, reg, x) \ 20741502Swpaul CSR_WRITE_1(sc, reg, \ 20841502Swpaul CSR_READ_1(sc, reg) & ~x) 20941502Swpaul 21041502Swpaul#define VR_SETBIT16(sc, reg, x) \ 21141502Swpaul CSR_WRITE_2(sc, reg, \ 21241502Swpaul CSR_READ_2(sc, reg) | x) 21341502Swpaul 21441502Swpaul#define VR_CLRBIT16(sc, reg, x) \ 21541502Swpaul CSR_WRITE_2(sc, reg, \ 21641502Swpaul CSR_READ_2(sc, reg) & ~x) 21741502Swpaul 21841502Swpaul#define VR_SETBIT32(sc, reg, x) \ 21941502Swpaul CSR_WRITE_4(sc, reg, \ 22041502Swpaul CSR_READ_4(sc, reg) | x) 22141502Swpaul 22241502Swpaul#define VR_CLRBIT32(sc, reg, x) \ 22341502Swpaul CSR_WRITE_4(sc, reg, \ 22441502Swpaul CSR_READ_4(sc, reg) & ~x) 22541502Swpaul 22641502Swpaul#define SIO_SET(x) \ 22741502Swpaul CSR_WRITE_1(sc, VR_MIICMD, \ 22841502Swpaul CSR_READ_1(sc, VR_MIICMD) | x) 22941502Swpaul 23041502Swpaul#define SIO_CLR(x) \ 23141502Swpaul CSR_WRITE_1(sc, VR_MIICMD, \ 23241502Swpaul CSR_READ_1(sc, VR_MIICMD) & ~x) 23341502Swpaul 23441502Swpaul/* 23541502Swpaul * Sync the PHYs by setting data bit and strobing the clock 32 times. 23641502Swpaul */ 23741502Swpaulstatic void vr_mii_sync(sc) 23841502Swpaul struct vr_softc *sc; 23941502Swpaul{ 24041502Swpaul register int i; 24141502Swpaul 24241502Swpaul SIO_SET(VR_MIICMD_DIR|VR_MIICMD_DATAIN); 24341502Swpaul 24441502Swpaul for (i = 0; i < 32; i++) { 24541502Swpaul SIO_SET(VR_MIICMD_CLK); 24641502Swpaul DELAY(1); 24741502Swpaul SIO_CLR(VR_MIICMD_CLK); 24841502Swpaul DELAY(1); 24941502Swpaul } 25041502Swpaul 25141502Swpaul return; 25241502Swpaul} 25341502Swpaul 25441502Swpaul/* 25541502Swpaul * Clock a series of bits through the MII. 25641502Swpaul */ 25741502Swpaulstatic void vr_mii_send(sc, bits, cnt) 25841502Swpaul struct vr_softc *sc; 25941502Swpaul u_int32_t bits; 26041502Swpaul int cnt; 26141502Swpaul{ 26241502Swpaul int i; 26341502Swpaul 26441502Swpaul SIO_CLR(VR_MIICMD_CLK); 26541502Swpaul 26641502Swpaul for (i = (0x1 << (cnt - 1)); i; i >>= 1) { 26741502Swpaul if (bits & i) { 26841502Swpaul SIO_SET(VR_MIICMD_DATAIN); 26941502Swpaul } else { 27041502Swpaul SIO_CLR(VR_MIICMD_DATAIN); 27141502Swpaul } 27241502Swpaul DELAY(1); 27341502Swpaul SIO_CLR(VR_MIICMD_CLK); 27441502Swpaul DELAY(1); 27541502Swpaul SIO_SET(VR_MIICMD_CLK); 27641502Swpaul } 27741502Swpaul} 27841502Swpaul 27941502Swpaul/* 28041502Swpaul * Read an PHY register through the MII. 28141502Swpaul */ 28241502Swpaulstatic int vr_mii_readreg(sc, frame) 28341502Swpaul struct vr_softc *sc; 28441502Swpaul struct vr_mii_frame *frame; 28541502Swpaul 28641502Swpaul{ 28767087Swpaul int i, ack; 28841502Swpaul 28967087Swpaul VR_LOCK(sc); 29041502Swpaul 29141502Swpaul /* 29241502Swpaul * Set up frame for RX. 29341502Swpaul */ 29441502Swpaul frame->mii_stdelim = VR_MII_STARTDELIM; 29541502Swpaul frame->mii_opcode = VR_MII_READOP; 29641502Swpaul frame->mii_turnaround = 0; 29741502Swpaul frame->mii_data = 0; 29841502Swpaul 29941502Swpaul CSR_WRITE_1(sc, VR_MIICMD, 0); 30041502Swpaul VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM); 30141502Swpaul 30241502Swpaul /* 30341502Swpaul * Turn on data xmit. 30441502Swpaul */ 30541502Swpaul SIO_SET(VR_MIICMD_DIR); 30641502Swpaul 30741502Swpaul vr_mii_sync(sc); 30841502Swpaul 30941502Swpaul /* 31041502Swpaul * Send command/address info. 31141502Swpaul */ 31241502Swpaul vr_mii_send(sc, frame->mii_stdelim, 2); 31341502Swpaul vr_mii_send(sc, frame->mii_opcode, 2); 31441502Swpaul vr_mii_send(sc, frame->mii_phyaddr, 5); 31541502Swpaul vr_mii_send(sc, frame->mii_regaddr, 5); 31641502Swpaul 31741502Swpaul /* Idle bit */ 31841502Swpaul SIO_CLR((VR_MIICMD_CLK|VR_MIICMD_DATAIN)); 31941502Swpaul DELAY(1); 32041502Swpaul SIO_SET(VR_MIICMD_CLK); 32141502Swpaul DELAY(1); 32241502Swpaul 32341502Swpaul /* Turn off xmit. */ 32441502Swpaul SIO_CLR(VR_MIICMD_DIR); 32541502Swpaul 32641502Swpaul /* Check for ack */ 32741502Swpaul SIO_CLR(VR_MIICMD_CLK); 32841502Swpaul DELAY(1); 32941502Swpaul SIO_SET(VR_MIICMD_CLK); 33041502Swpaul DELAY(1); 33141502Swpaul ack = CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAOUT; 33241502Swpaul 33341502Swpaul /* 33441502Swpaul * Now try reading data bits. If the ack failed, we still 33541502Swpaul * need to clock through 16 cycles to keep the PHY(s) in sync. 33641502Swpaul */ 33741502Swpaul if (ack) { 33841502Swpaul for(i = 0; i < 16; i++) { 33941502Swpaul SIO_CLR(VR_MIICMD_CLK); 34041502Swpaul DELAY(1); 34141502Swpaul SIO_SET(VR_MIICMD_CLK); 34241502Swpaul DELAY(1); 34341502Swpaul } 34441502Swpaul goto fail; 34541502Swpaul } 34641502Swpaul 34741502Swpaul for (i = 0x8000; i; i >>= 1) { 34841502Swpaul SIO_CLR(VR_MIICMD_CLK); 34941502Swpaul DELAY(1); 35041502Swpaul if (!ack) { 35141502Swpaul if (CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAOUT) 35241502Swpaul frame->mii_data |= i; 35341502Swpaul DELAY(1); 35441502Swpaul } 35541502Swpaul SIO_SET(VR_MIICMD_CLK); 35641502Swpaul DELAY(1); 35741502Swpaul } 35841502Swpaul 35941502Swpaulfail: 36041502Swpaul 36141502Swpaul SIO_CLR(VR_MIICMD_CLK); 36241502Swpaul DELAY(1); 36341502Swpaul SIO_SET(VR_MIICMD_CLK); 36441502Swpaul DELAY(1); 36541502Swpaul 36667087Swpaul VR_UNLOCK(sc); 36741502Swpaul 36841502Swpaul if (ack) 36941502Swpaul return(1); 37041502Swpaul return(0); 37141502Swpaul} 37241502Swpaul 37341502Swpaul/* 37441502Swpaul * Write to a PHY register through the MII. 37541502Swpaul */ 37641502Swpaulstatic int vr_mii_writereg(sc, frame) 37741502Swpaul struct vr_softc *sc; 37841502Swpaul struct vr_mii_frame *frame; 37941502Swpaul 38041502Swpaul{ 38167087Swpaul VR_LOCK(sc); 38241502Swpaul 38341502Swpaul CSR_WRITE_1(sc, VR_MIICMD, 0); 38441502Swpaul VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM); 38541502Swpaul 38641502Swpaul /* 38741502Swpaul * Set up frame for TX. 38841502Swpaul */ 38941502Swpaul 39041502Swpaul frame->mii_stdelim = VR_MII_STARTDELIM; 39141502Swpaul frame->mii_opcode = VR_MII_WRITEOP; 39241502Swpaul frame->mii_turnaround = VR_MII_TURNAROUND; 39341502Swpaul 39441502Swpaul /* 39541502Swpaul * Turn on data output. 39641502Swpaul */ 39741502Swpaul SIO_SET(VR_MIICMD_DIR); 39841502Swpaul 39941502Swpaul vr_mii_sync(sc); 40041502Swpaul 40141502Swpaul vr_mii_send(sc, frame->mii_stdelim, 2); 40241502Swpaul vr_mii_send(sc, frame->mii_opcode, 2); 40341502Swpaul vr_mii_send(sc, frame->mii_phyaddr, 5); 40441502Swpaul vr_mii_send(sc, frame->mii_regaddr, 5); 40541502Swpaul vr_mii_send(sc, frame->mii_turnaround, 2); 40641502Swpaul vr_mii_send(sc, frame->mii_data, 16); 40741502Swpaul 40841502Swpaul /* Idle bit. */ 40941502Swpaul SIO_SET(VR_MIICMD_CLK); 41041502Swpaul DELAY(1); 41141502Swpaul SIO_CLR(VR_MIICMD_CLK); 41241502Swpaul DELAY(1); 41341502Swpaul 41441502Swpaul /* 41541502Swpaul * Turn off xmit. 41641502Swpaul */ 41741502Swpaul SIO_CLR(VR_MIICMD_DIR); 41841502Swpaul 41967087Swpaul VR_UNLOCK(sc); 42041502Swpaul 42141502Swpaul return(0); 42241502Swpaul} 42341502Swpaul 42451432Swpaulstatic int vr_miibus_readreg(dev, phy, reg) 42551432Swpaul device_t dev; 42651432Swpaul int phy, reg; 42751432Swpaul{ 42841502Swpaul struct vr_softc *sc; 42941502Swpaul struct vr_mii_frame frame; 43041502Swpaul 43151432Swpaul sc = device_get_softc(dev); 43241502Swpaul bzero((char *)&frame, sizeof(frame)); 43341502Swpaul 43451432Swpaul frame.mii_phyaddr = phy; 43541502Swpaul frame.mii_regaddr = reg; 43641502Swpaul vr_mii_readreg(sc, &frame); 43741502Swpaul 43841502Swpaul return(frame.mii_data); 43941502Swpaul} 44041502Swpaul 44151432Swpaulstatic int vr_miibus_writereg(dev, phy, reg, data) 44251432Swpaul device_t dev; 44351432Swpaul u_int16_t phy, reg, data; 44451432Swpaul{ 44541502Swpaul struct vr_softc *sc; 44641502Swpaul struct vr_mii_frame frame; 44741502Swpaul 44851432Swpaul sc = device_get_softc(dev); 44941502Swpaul bzero((char *)&frame, sizeof(frame)); 45041502Swpaul 45151432Swpaul frame.mii_phyaddr = phy; 45241502Swpaul frame.mii_regaddr = reg; 45341502Swpaul frame.mii_data = data; 45441502Swpaul 45541502Swpaul vr_mii_writereg(sc, &frame); 45641502Swpaul 45751432Swpaul return(0); 45851432Swpaul} 45951432Swpaul 46051432Swpaulstatic void vr_miibus_statchg(dev) 46151432Swpaul device_t dev; 46251432Swpaul{ 46351432Swpaul struct vr_softc *sc; 46451432Swpaul struct mii_data *mii; 46551432Swpaul 46651432Swpaul sc = device_get_softc(dev); 46767087Swpaul VR_LOCK(sc); 46851432Swpaul mii = device_get_softc(sc->vr_miibus); 46951432Swpaul vr_setcfg(sc, mii->mii_media_active); 47067087Swpaul VR_UNLOCK(sc); 47151432Swpaul 47241502Swpaul return; 47341502Swpaul} 47441502Swpaul 47541502Swpaul/* 47641502Swpaul * Calculate CRC of a multicast group address, return the lower 6 bits. 47741502Swpaul */ 47841502Swpaulstatic u_int8_t vr_calchash(addr) 47941502Swpaul u_int8_t *addr; 48041502Swpaul{ 48141502Swpaul u_int32_t crc, carry; 48241502Swpaul int i, j; 48341502Swpaul u_int8_t c; 48441502Swpaul 48541502Swpaul /* Compute CRC for the address value. */ 48641502Swpaul crc = 0xFFFFFFFF; /* initial value */ 48741502Swpaul 48841502Swpaul for (i = 0; i < 6; i++) { 48941502Swpaul c = *(addr + i); 49041502Swpaul for (j = 0; j < 8; j++) { 49141502Swpaul carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01); 49241502Swpaul crc <<= 1; 49341502Swpaul c >>= 1; 49441502Swpaul if (carry) 49541502Swpaul crc = (crc ^ 0x04c11db6) | carry; 49641502Swpaul } 49741502Swpaul } 49841502Swpaul 49941502Swpaul /* return the filter bit position */ 50041502Swpaul return((crc >> 26) & 0x0000003F); 50141502Swpaul} 50241502Swpaul 50341502Swpaul/* 50441502Swpaul * Program the 64-bit multicast hash filter. 50541502Swpaul */ 50641502Swpaulstatic void vr_setmulti(sc) 50741502Swpaul struct vr_softc *sc; 50841502Swpaul{ 50941502Swpaul struct ifnet *ifp; 51041502Swpaul int h = 0; 51141502Swpaul u_int32_t hashes[2] = { 0, 0 }; 51241502Swpaul struct ifmultiaddr *ifma; 51341502Swpaul u_int8_t rxfilt; 51441502Swpaul int mcnt = 0; 51541502Swpaul 51641502Swpaul ifp = &sc->arpcom.ac_if; 51741502Swpaul 51841502Swpaul rxfilt = CSR_READ_1(sc, VR_RXCFG); 51941502Swpaul 52041502Swpaul if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 52141502Swpaul rxfilt |= VR_RXCFG_RX_MULTI; 52241502Swpaul CSR_WRITE_1(sc, VR_RXCFG, rxfilt); 52341502Swpaul CSR_WRITE_4(sc, VR_MAR0, 0xFFFFFFFF); 52441502Swpaul CSR_WRITE_4(sc, VR_MAR1, 0xFFFFFFFF); 52541502Swpaul return; 52641502Swpaul } 52741502Swpaul 52841502Swpaul /* first, zot all the existing hash bits */ 52941502Swpaul CSR_WRITE_4(sc, VR_MAR0, 0); 53041502Swpaul CSR_WRITE_4(sc, VR_MAR1, 0); 53141502Swpaul 53241502Swpaul /* now program new ones */ 53341502Swpaul for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL; 53441502Swpaul ifma = ifma->ifma_link.le_next) { 53541502Swpaul if (ifma->ifma_addr->sa_family != AF_LINK) 53641502Swpaul continue; 53741502Swpaul h = vr_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 53841502Swpaul if (h < 32) 53941502Swpaul hashes[0] |= (1 << h); 54041502Swpaul else 54141502Swpaul hashes[1] |= (1 << (h - 32)); 54241502Swpaul mcnt++; 54341502Swpaul } 54441502Swpaul 54541502Swpaul if (mcnt) 54641502Swpaul rxfilt |= VR_RXCFG_RX_MULTI; 54741502Swpaul else 54841502Swpaul rxfilt &= ~VR_RXCFG_RX_MULTI; 54941502Swpaul 55041502Swpaul CSR_WRITE_4(sc, VR_MAR0, hashes[0]); 55141502Swpaul CSR_WRITE_4(sc, VR_MAR1, hashes[1]); 55241502Swpaul CSR_WRITE_1(sc, VR_RXCFG, rxfilt); 55341502Swpaul 55441502Swpaul return; 55541502Swpaul} 55641502Swpaul 55741502Swpaul/* 55841502Swpaul * In order to fiddle with the 55941502Swpaul * 'full-duplex' and '100Mbps' bits in the netconfig register, we 56041502Swpaul * first have to put the transmit and/or receive logic in the idle state. 56141502Swpaul */ 56251432Swpaulstatic void vr_setcfg(sc, media) 56341502Swpaul struct vr_softc *sc; 56451432Swpaul int media; 56541502Swpaul{ 56641502Swpaul int restart = 0; 56741502Swpaul 56841502Swpaul if (CSR_READ_2(sc, VR_COMMAND) & (VR_CMD_TX_ON|VR_CMD_RX_ON)) { 56941502Swpaul restart = 1; 57041502Swpaul VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_TX_ON|VR_CMD_RX_ON)); 57141502Swpaul } 57241502Swpaul 57351432Swpaul if ((media & IFM_GMASK) == IFM_FDX) 57441502Swpaul VR_SETBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX); 57541502Swpaul else 57641502Swpaul VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX); 57741502Swpaul 57841502Swpaul if (restart) 57941502Swpaul VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_RX_ON); 58041502Swpaul 58141502Swpaul return; 58241502Swpaul} 58341502Swpaul 58441502Swpaulstatic void vr_reset(sc) 58541502Swpaul struct vr_softc *sc; 58641502Swpaul{ 58741502Swpaul register int i; 58841502Swpaul 58941502Swpaul VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RESET); 59041502Swpaul 59141502Swpaul for (i = 0; i < VR_TIMEOUT; i++) { 59241502Swpaul DELAY(10); 59341502Swpaul if (!(CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RESET)) 59441502Swpaul break; 59541502Swpaul } 59641502Swpaul if (i == VR_TIMEOUT) 59741502Swpaul printf("vr%d: reset never completed!\n", sc->vr_unit); 59841502Swpaul 59941502Swpaul /* Wait a little while for the chip to get its brains in order. */ 60041502Swpaul DELAY(1000); 60141502Swpaul 60241502Swpaul return; 60341502Swpaul} 60441502Swpaul 60541502Swpaul/* 60641502Swpaul * Probe for a VIA Rhine chip. Check the PCI vendor and device 60741502Swpaul * IDs against our list and return a device name if we find a match. 60841502Swpaul */ 60949610Swpaulstatic int vr_probe(dev) 61049610Swpaul device_t dev; 61141502Swpaul{ 61241502Swpaul struct vr_type *t; 61341502Swpaul 61441502Swpaul t = vr_devs; 61541502Swpaul 61641502Swpaul while(t->vr_name != NULL) { 61749610Swpaul if ((pci_get_vendor(dev) == t->vr_vid) && 61849610Swpaul (pci_get_device(dev) == t->vr_did)) { 61949610Swpaul device_set_desc(dev, t->vr_name); 62049610Swpaul return(0); 62141502Swpaul } 62241502Swpaul t++; 62341502Swpaul } 62441502Swpaul 62549610Swpaul return(ENXIO); 62641502Swpaul} 62741502Swpaul 62841502Swpaul/* 62941502Swpaul * Attach the interface. Allocate softc structures, do ifmedia 63041502Swpaul * setup and ethernet/BPF attach. 63141502Swpaul */ 63249610Swpaulstatic int vr_attach(dev) 63349610Swpaul device_t dev; 63441502Swpaul{ 63567087Swpaul int i; 63641502Swpaul u_char eaddr[ETHER_ADDR_LEN]; 63741502Swpaul u_int32_t command; 63841502Swpaul struct vr_softc *sc; 63941502Swpaul struct ifnet *ifp; 64049610Swpaul int unit, error = 0, rid; 64141502Swpaul 64249610Swpaul sc = device_get_softc(dev); 64349610Swpaul unit = device_get_unit(dev); 64449610Swpaul bzero(sc, sizeof(struct vr_softc *)); 64541502Swpaul 64671228Sbmilekic mtx_init(&sc->vr_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE); 64769583Swpaul VR_LOCK(sc); 64869583Swpaul 64941502Swpaul /* 65041502Swpaul * Handle power management nonsense. 65141502Swpaul */ 65241502Swpaul 65349610Swpaul command = pci_read_config(dev, VR_PCI_CAPID, 4) & 0x000000FF; 65441502Swpaul if (command == 0x01) { 65541502Swpaul 65649610Swpaul command = pci_read_config(dev, VR_PCI_PWRMGMTCTRL, 4); 65741502Swpaul if (command & VR_PSTATE_MASK) { 65841502Swpaul u_int32_t iobase, membase, irq; 65941502Swpaul 66041502Swpaul /* Save important PCI config data. */ 66149610Swpaul iobase = pci_read_config(dev, VR_PCI_LOIO, 4); 66249610Swpaul membase = pci_read_config(dev, VR_PCI_LOMEM, 4); 66349610Swpaul irq = pci_read_config(dev, VR_PCI_INTLINE, 4); 66441502Swpaul 66541502Swpaul /* Reset the power state. */ 66641502Swpaul printf("vr%d: chip is in D%d power mode " 66741502Swpaul "-- setting to D0\n", unit, command & VR_PSTATE_MASK); 66841502Swpaul command &= 0xFFFFFFFC; 66949610Swpaul pci_write_config(dev, VR_PCI_PWRMGMTCTRL, command, 4); 67041502Swpaul 67141502Swpaul /* Restore PCI config data. */ 67249610Swpaul pci_write_config(dev, VR_PCI_LOIO, iobase, 4); 67349610Swpaul pci_write_config(dev, VR_PCI_LOMEM, membase, 4); 67449610Swpaul pci_write_config(dev, VR_PCI_INTLINE, irq, 4); 67541502Swpaul } 67641502Swpaul } 67741502Swpaul 67841502Swpaul /* 67941502Swpaul * Map control/status registers. 68041502Swpaul */ 68161041Speter command = pci_read_config(dev, PCIR_COMMAND, 4); 68241502Swpaul command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN); 68361041Speter pci_write_config(dev, PCIR_COMMAND, command, 4); 68461041Speter command = pci_read_config(dev, PCIR_COMMAND, 4); 68541502Swpaul 68641502Swpaul#ifdef VR_USEIOSPACE 68741502Swpaul if (!(command & PCIM_CMD_PORTEN)) { 68841502Swpaul printf("vr%d: failed to enable I/O ports!\n", unit); 68941502Swpaul free(sc, M_DEVBUF); 69041502Swpaul goto fail; 69141502Swpaul } 69241502Swpaul#else 69341502Swpaul if (!(command & PCIM_CMD_MEMEN)) { 69441502Swpaul printf("vr%d: failed to enable memory mapping!\n", unit); 69541502Swpaul goto fail; 69641502Swpaul } 69749610Swpaul#endif 69841502Swpaul 69949610Swpaul rid = VR_RID; 70049610Swpaul sc->vr_res = bus_alloc_resource(dev, VR_RES, &rid, 70149610Swpaul 0, ~0, 1, RF_ACTIVE); 70249610Swpaul 70349610Swpaul if (sc->vr_res == NULL) { 70449610Swpaul printf("vr%d: couldn't map ports/memory\n", unit); 70549610Swpaul error = ENXIO; 70641502Swpaul goto fail; 70741502Swpaul } 70841502Swpaul 70949610Swpaul sc->vr_btag = rman_get_bustag(sc->vr_res); 71049610Swpaul sc->vr_bhandle = rman_get_bushandle(sc->vr_res); 71141502Swpaul 71241502Swpaul /* Allocate interrupt */ 71349610Swpaul rid = 0; 71449610Swpaul sc->vr_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 71549610Swpaul RF_SHAREABLE | RF_ACTIVE); 71649610Swpaul 71749610Swpaul if (sc->vr_irq == NULL) { 71841502Swpaul printf("vr%d: couldn't map interrupt\n", unit); 71949610Swpaul bus_release_resource(dev, VR_RES, VR_RID, sc->vr_res); 72049610Swpaul error = ENXIO; 72141502Swpaul goto fail; 72241502Swpaul } 72341502Swpaul 72449610Swpaul error = bus_setup_intr(dev, sc->vr_irq, INTR_TYPE_NET, 72549610Swpaul vr_intr, sc, &sc->vr_intrhand); 72649610Swpaul 72749610Swpaul if (error) { 72849610Swpaul bus_release_resource(dev, SYS_RES_IRQ, 0, sc->vr_irq); 72949610Swpaul bus_release_resource(dev, VR_RES, VR_RID, sc->vr_res); 73049610Swpaul printf("vr%d: couldn't set up irq\n", unit); 73149610Swpaul goto fail; 73249610Swpaul } 73349610Swpaul 73441502Swpaul /* Reset the adapter. */ 73541502Swpaul vr_reset(sc); 73641502Swpaul 73741502Swpaul /* 73841502Swpaul * Get station address. The way the Rhine chips work, 73941502Swpaul * you're not allowed to directly access the EEPROM once 74041502Swpaul * they've been programmed a special way. Consequently, 74141502Swpaul * we need to read the node address from the PAR0 and PAR1 74241502Swpaul * registers. 74341502Swpaul */ 74441502Swpaul VR_SETBIT(sc, VR_EECSR, VR_EECSR_LOAD); 74541502Swpaul DELAY(200); 74641502Swpaul for (i = 0; i < ETHER_ADDR_LEN; i++) 74741502Swpaul eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i); 74841502Swpaul 74941502Swpaul /* 75041502Swpaul * A Rhine chip was detected. Inform the world. 75141502Swpaul */ 75241502Swpaul printf("vr%d: Ethernet address: %6D\n", unit, eaddr, ":"); 75341502Swpaul 75441502Swpaul sc->vr_unit = unit; 75541502Swpaul bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 75641502Swpaul 75751432Swpaul sc->vr_ldata = contigmalloc(sizeof(struct vr_list_data), M_DEVBUF, 75851657Swpaul M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 75951432Swpaul 76051432Swpaul if (sc->vr_ldata == NULL) { 76141502Swpaul printf("vr%d: no memory for list buffers!\n", unit); 76249610Swpaul bus_teardown_intr(dev, sc->vr_irq, sc->vr_intrhand); 76349610Swpaul bus_release_resource(dev, SYS_RES_IRQ, 0, sc->vr_irq); 76449610Swpaul bus_release_resource(dev, VR_RES, VR_RID, sc->vr_res); 76549610Swpaul error = ENXIO; 76649610Swpaul goto fail; 76741502Swpaul } 76841502Swpaul 76941502Swpaul bzero(sc->vr_ldata, sizeof(struct vr_list_data)); 77041502Swpaul 77141502Swpaul ifp = &sc->arpcom.ac_if; 77241502Swpaul ifp->if_softc = sc; 77341502Swpaul ifp->if_unit = unit; 77441502Swpaul ifp->if_name = "vr"; 77541502Swpaul ifp->if_mtu = ETHERMTU; 77641502Swpaul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 77741502Swpaul ifp->if_ioctl = vr_ioctl; 77841502Swpaul ifp->if_output = ether_output; 77941502Swpaul ifp->if_start = vr_start; 78041502Swpaul ifp->if_watchdog = vr_watchdog; 78141502Swpaul ifp->if_init = vr_init; 78241502Swpaul ifp->if_baudrate = 10000000; 78343515Swpaul ifp->if_snd.ifq_maxlen = VR_TX_LIST_CNT - 1; 78441502Swpaul 78551432Swpaul /* 78651432Swpaul * Do MII setup. 78751432Swpaul */ 78851432Swpaul if (mii_phy_probe(dev, &sc->vr_miibus, 78951432Swpaul vr_ifmedia_upd, vr_ifmedia_sts)) { 79041502Swpaul printf("vr%d: MII without any phy!\n", sc->vr_unit); 79149610Swpaul bus_teardown_intr(dev, sc->vr_irq, sc->vr_intrhand); 79249610Swpaul bus_release_resource(dev, SYS_RES_IRQ, 0, sc->vr_irq); 79349610Swpaul bus_release_resource(dev, VR_RES, VR_RID, sc->vr_res); 79451432Swpaul contigfree(sc->vr_ldata, 79551432Swpaul sizeof(struct vr_list_data), M_DEVBUF); 79649610Swpaul error = ENXIO; 79741502Swpaul goto fail; 79841502Swpaul } 79941502Swpaul 80051432Swpaul callout_handle_init(&sc->vr_stat_ch); 80141502Swpaul 80241502Swpaul /* 80363090Sarchie * Call MI attach routine. 80441502Swpaul */ 80563090Sarchie ether_ifattach(ifp, ETHER_BPF_SUPPORTED); 80667087Swpaul VR_UNLOCK(sc); 80767087Swpaul return(0); 80841502Swpaul 80941502Swpaulfail: 81067087Swpaul VR_UNLOCK(sc); 81167087Swpaul mtx_destroy(&sc->vr_mtx); 81267087Swpaul 81349610Swpaul return(error); 81441502Swpaul} 81541502Swpaul 81649610Swpaulstatic int vr_detach(dev) 81749610Swpaul device_t dev; 81849610Swpaul{ 81949610Swpaul struct vr_softc *sc; 82049610Swpaul struct ifnet *ifp; 82149610Swpaul 82249610Swpaul sc = device_get_softc(dev); 82367087Swpaul VR_LOCK(sc); 82449610Swpaul ifp = &sc->arpcom.ac_if; 82549610Swpaul 82649610Swpaul vr_stop(sc); 82763090Sarchie ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); 82849610Swpaul 82951432Swpaul bus_generic_detach(dev); 83051432Swpaul device_delete_child(dev, sc->vr_miibus); 83151432Swpaul 83249610Swpaul bus_teardown_intr(dev, sc->vr_irq, sc->vr_intrhand); 83349610Swpaul bus_release_resource(dev, SYS_RES_IRQ, 0, sc->vr_irq); 83449610Swpaul bus_release_resource(dev, VR_RES, VR_RID, sc->vr_res); 83549610Swpaul 83651432Swpaul contigfree(sc->vr_ldata, sizeof(struct vr_list_data), M_DEVBUF); 83749610Swpaul 83867087Swpaul VR_UNLOCK(sc); 83967087Swpaul mtx_destroy(&sc->vr_mtx); 84049610Swpaul 84149610Swpaul return(0); 84249610Swpaul} 84349610Swpaul 84441502Swpaul/* 84541502Swpaul * Initialize the transmit descriptors. 84641502Swpaul */ 84741502Swpaulstatic int vr_list_tx_init(sc) 84841502Swpaul struct vr_softc *sc; 84941502Swpaul{ 85041502Swpaul struct vr_chain_data *cd; 85141502Swpaul struct vr_list_data *ld; 85241502Swpaul int i; 85341502Swpaul 85441502Swpaul cd = &sc->vr_cdata; 85541502Swpaul ld = sc->vr_ldata; 85641502Swpaul for (i = 0; i < VR_TX_LIST_CNT; i++) { 85741502Swpaul cd->vr_tx_chain[i].vr_ptr = &ld->vr_tx_list[i]; 85841502Swpaul if (i == (VR_TX_LIST_CNT - 1)) 85941502Swpaul cd->vr_tx_chain[i].vr_nextdesc = 86041502Swpaul &cd->vr_tx_chain[0]; 86141502Swpaul else 86241502Swpaul cd->vr_tx_chain[i].vr_nextdesc = 86341502Swpaul &cd->vr_tx_chain[i + 1]; 86441502Swpaul } 86541502Swpaul 86641502Swpaul cd->vr_tx_free = &cd->vr_tx_chain[0]; 86741502Swpaul cd->vr_tx_tail = cd->vr_tx_head = NULL; 86841502Swpaul 86941502Swpaul return(0); 87041502Swpaul} 87141502Swpaul 87241502Swpaul 87341502Swpaul/* 87441502Swpaul * Initialize the RX descriptors and allocate mbufs for them. Note that 87541502Swpaul * we arrange the descriptors in a closed ring, so that the last descriptor 87641502Swpaul * points back to the first. 87741502Swpaul */ 87841502Swpaulstatic int vr_list_rx_init(sc) 87941502Swpaul struct vr_softc *sc; 88041502Swpaul{ 88141502Swpaul struct vr_chain_data *cd; 88241502Swpaul struct vr_list_data *ld; 88341502Swpaul int i; 88441502Swpaul 88541502Swpaul cd = &sc->vr_cdata; 88641502Swpaul ld = sc->vr_ldata; 88741502Swpaul 88841502Swpaul for (i = 0; i < VR_RX_LIST_CNT; i++) { 88941502Swpaul cd->vr_rx_chain[i].vr_ptr = 89041502Swpaul (struct vr_desc *)&ld->vr_rx_list[i]; 89149610Swpaul if (vr_newbuf(sc, &cd->vr_rx_chain[i], NULL) == ENOBUFS) 89241502Swpaul return(ENOBUFS); 89341502Swpaul if (i == (VR_RX_LIST_CNT - 1)) { 89441502Swpaul cd->vr_rx_chain[i].vr_nextdesc = 89541502Swpaul &cd->vr_rx_chain[0]; 89641502Swpaul ld->vr_rx_list[i].vr_next = 89741502Swpaul vtophys(&ld->vr_rx_list[0]); 89841502Swpaul } else { 89941502Swpaul cd->vr_rx_chain[i].vr_nextdesc = 90041502Swpaul &cd->vr_rx_chain[i + 1]; 90141502Swpaul ld->vr_rx_list[i].vr_next = 90241502Swpaul vtophys(&ld->vr_rx_list[i + 1]); 90341502Swpaul } 90441502Swpaul } 90541502Swpaul 90641502Swpaul cd->vr_rx_head = &cd->vr_rx_chain[0]; 90741502Swpaul 90841502Swpaul return(0); 90941502Swpaul} 91041502Swpaul 91141502Swpaul/* 91241502Swpaul * Initialize an RX descriptor and attach an MBUF cluster. 91341502Swpaul * Note: the length fields are only 11 bits wide, which means the 91441502Swpaul * largest size we can specify is 2047. This is important because 91541502Swpaul * MCLBYTES is 2048, so we have to subtract one otherwise we'll 91641502Swpaul * overflow the field and make a mess. 91741502Swpaul */ 91849610Swpaulstatic int vr_newbuf(sc, c, m) 91941502Swpaul struct vr_softc *sc; 92041502Swpaul struct vr_chain_onefrag *c; 92149610Swpaul struct mbuf *m; 92241502Swpaul{ 92341502Swpaul struct mbuf *m_new = NULL; 92441502Swpaul 92549610Swpaul if (m == NULL) { 92649610Swpaul MGETHDR(m_new, M_DONTWAIT, MT_DATA); 92749610Swpaul if (m_new == NULL) { 92849610Swpaul printf("vr%d: no memory for rx list " 92949610Swpaul "-- packet dropped!\n", sc->vr_unit); 93049610Swpaul return(ENOBUFS); 93149610Swpaul } 93241502Swpaul 93349610Swpaul MCLGET(m_new, M_DONTWAIT); 93449610Swpaul if (!(m_new->m_flags & M_EXT)) { 93549610Swpaul printf("vr%d: no memory for rx list " 93649610Swpaul "-- packet dropped!\n", sc->vr_unit); 93749610Swpaul m_freem(m_new); 93849610Swpaul return(ENOBUFS); 93949610Swpaul } 94049610Swpaul m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 94149610Swpaul } else { 94249610Swpaul m_new = m; 94349610Swpaul m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 94449610Swpaul m_new->m_data = m_new->m_ext.ext_buf; 94541502Swpaul } 94641502Swpaul 94749610Swpaul m_adj(m_new, sizeof(u_int64_t)); 94849610Swpaul 94941502Swpaul c->vr_mbuf = m_new; 95041502Swpaul c->vr_ptr->vr_status = VR_RXSTAT; 95141502Swpaul c->vr_ptr->vr_data = vtophys(mtod(m_new, caddr_t)); 95242491Swpaul c->vr_ptr->vr_ctl = VR_RXCTL | VR_RXLEN; 95341502Swpaul 95441502Swpaul return(0); 95541502Swpaul} 95641502Swpaul 95741502Swpaul/* 95841502Swpaul * A frame has been uploaded: pass the resulting mbuf chain up to 95941502Swpaul * the higher level protocols. 96041502Swpaul */ 96141502Swpaulstatic void vr_rxeof(sc) 96241502Swpaul struct vr_softc *sc; 96341502Swpaul{ 96441502Swpaul struct ether_header *eh; 96541502Swpaul struct mbuf *m; 96641502Swpaul struct ifnet *ifp; 96741502Swpaul struct vr_chain_onefrag *cur_rx; 96841502Swpaul int total_len = 0; 96941502Swpaul u_int32_t rxstat; 97041502Swpaul 97141502Swpaul ifp = &sc->arpcom.ac_if; 97241502Swpaul 97341502Swpaul while(!((rxstat = sc->vr_cdata.vr_rx_head->vr_ptr->vr_status) & 97441502Swpaul VR_RXSTAT_OWN)) { 97549610Swpaul struct mbuf *m0 = NULL; 97649610Swpaul 97741502Swpaul cur_rx = sc->vr_cdata.vr_rx_head; 97841502Swpaul sc->vr_cdata.vr_rx_head = cur_rx->vr_nextdesc; 97949610Swpaul m = cur_rx->vr_mbuf; 98041502Swpaul 98141502Swpaul /* 98241502Swpaul * If an error occurs, update stats, clear the 98341502Swpaul * status word and leave the mbuf cluster in place: 98441502Swpaul * it should simply get re-used next time this descriptor 98541502Swpaul * comes up in the ring. 98641502Swpaul */ 98741502Swpaul if (rxstat & VR_RXSTAT_RXERR) { 98841502Swpaul ifp->if_ierrors++; 98941502Swpaul printf("vr%d: rx error: ", sc->vr_unit); 99041502Swpaul switch(rxstat & 0x000000FF) { 99141502Swpaul case VR_RXSTAT_CRCERR: 99241502Swpaul printf("crc error\n"); 99341502Swpaul break; 99441502Swpaul case VR_RXSTAT_FRAMEALIGNERR: 99541502Swpaul printf("frame alignment error\n"); 99641502Swpaul break; 99741502Swpaul case VR_RXSTAT_FIFOOFLOW: 99841502Swpaul printf("FIFO overflow\n"); 99941502Swpaul break; 100041502Swpaul case VR_RXSTAT_GIANT: 100141502Swpaul printf("received giant packet\n"); 100241502Swpaul break; 100341502Swpaul case VR_RXSTAT_RUNT: 100441502Swpaul printf("received runt packet\n"); 100541502Swpaul break; 100641502Swpaul case VR_RXSTAT_BUSERR: 100741502Swpaul printf("system bus error\n"); 100841502Swpaul break; 100941502Swpaul case VR_RXSTAT_BUFFERR: 101041502Swpaul printf("rx buffer error\n"); 101141502Swpaul break; 101241502Swpaul default: 101341502Swpaul printf("unknown rx error\n"); 101441502Swpaul break; 101541502Swpaul } 101649610Swpaul vr_newbuf(sc, cur_rx, m); 101741502Swpaul continue; 101841502Swpaul } 101941502Swpaul 102041502Swpaul /* No errors; receive the packet. */ 102141502Swpaul total_len = VR_RXBYTES(cur_rx->vr_ptr->vr_status); 102241502Swpaul 102341502Swpaul /* 102442048Swpaul * XXX The VIA Rhine chip includes the CRC with every 102542048Swpaul * received frame, and there's no way to turn this 102642048Swpaul * behavior off (at least, I can't find anything in 102742048Swpaul * the manual that explains how to do it) so we have 102842048Swpaul * to trim off the CRC manually. 102942048Swpaul */ 103042048Swpaul total_len -= ETHER_CRC_LEN; 103142048Swpaul 103249610Swpaul m0 = m_devget(mtod(m, char *) - ETHER_ALIGN, 103349610Swpaul total_len + ETHER_ALIGN, 0, ifp, NULL); 103449610Swpaul vr_newbuf(sc, cur_rx, m); 103549610Swpaul if (m0 == NULL) { 103641502Swpaul ifp->if_ierrors++; 103741502Swpaul continue; 103841502Swpaul } 103949610Swpaul m_adj(m0, ETHER_ALIGN); 104049610Swpaul m = m0; 104141502Swpaul 104241502Swpaul ifp->if_ipackets++; 104341502Swpaul eh = mtod(m, struct ether_header *); 104449610Swpaul 104541502Swpaul /* Remove header from mbuf and pass it on. */ 104641502Swpaul m_adj(m, sizeof(struct ether_header)); 104741502Swpaul ether_input(ifp, eh, m); 104841502Swpaul } 104941502Swpaul 105041502Swpaul return; 105141502Swpaul} 105241502Swpaul 105341502Swpaulvoid vr_rxeoc(sc) 105441502Swpaul struct vr_softc *sc; 105541502Swpaul{ 105641502Swpaul 105741502Swpaul vr_rxeof(sc); 105841502Swpaul VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_RX_ON); 105941502Swpaul CSR_WRITE_4(sc, VR_RXADDR, vtophys(sc->vr_cdata.vr_rx_head->vr_ptr)); 106041502Swpaul VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_ON); 106141502Swpaul VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_GO); 106241502Swpaul 106341502Swpaul return; 106441502Swpaul} 106541502Swpaul 106641502Swpaul/* 106741502Swpaul * A frame was downloaded to the chip. It's safe for us to clean up 106841502Swpaul * the list buffers. 106941502Swpaul */ 107041502Swpaul 107141502Swpaulstatic void vr_txeof(sc) 107241502Swpaul struct vr_softc *sc; 107341502Swpaul{ 107441502Swpaul struct vr_chain *cur_tx; 107541502Swpaul struct ifnet *ifp; 107641502Swpaul 107741502Swpaul ifp = &sc->arpcom.ac_if; 107841502Swpaul 107941502Swpaul /* Clear the timeout timer. */ 108041502Swpaul ifp->if_timer = 0; 108141502Swpaul 108241502Swpaul /* Sanity check. */ 108341502Swpaul if (sc->vr_cdata.vr_tx_head == NULL) 108441502Swpaul return; 108541502Swpaul 108641502Swpaul /* 108741502Swpaul * Go through our tx list and free mbufs for those 108841502Swpaul * frames that have been transmitted. 108941502Swpaul */ 109041502Swpaul while(sc->vr_cdata.vr_tx_head->vr_mbuf != NULL) { 109141502Swpaul u_int32_t txstat; 109241502Swpaul 109341502Swpaul cur_tx = sc->vr_cdata.vr_tx_head; 109441502Swpaul txstat = cur_tx->vr_ptr->vr_status; 109541502Swpaul 109642491Swpaul if (txstat & VR_TXSTAT_OWN) 109741502Swpaul break; 109841502Swpaul 109941502Swpaul if (txstat & VR_TXSTAT_ERRSUM) { 110041502Swpaul ifp->if_oerrors++; 110141502Swpaul if (txstat & VR_TXSTAT_DEFER) 110241502Swpaul ifp->if_collisions++; 110341502Swpaul if (txstat & VR_TXSTAT_LATECOLL) 110441502Swpaul ifp->if_collisions++; 110541502Swpaul } 110641502Swpaul 110741502Swpaul ifp->if_collisions +=(txstat & VR_TXSTAT_COLLCNT) >> 3; 110841502Swpaul 110941502Swpaul ifp->if_opackets++; 111051432Swpaul if (cur_tx->vr_mbuf != NULL) { 111151432Swpaul m_freem(cur_tx->vr_mbuf); 111251432Swpaul cur_tx->vr_mbuf = NULL; 111351432Swpaul } 111441502Swpaul 111541502Swpaul if (sc->vr_cdata.vr_tx_head == sc->vr_cdata.vr_tx_tail) { 111641502Swpaul sc->vr_cdata.vr_tx_head = NULL; 111741502Swpaul sc->vr_cdata.vr_tx_tail = NULL; 111841502Swpaul break; 111941502Swpaul } 112041502Swpaul 112141502Swpaul sc->vr_cdata.vr_tx_head = cur_tx->vr_nextdesc; 112241502Swpaul } 112341502Swpaul 112441502Swpaul return; 112541502Swpaul} 112641502Swpaul 112741502Swpaul/* 112841502Swpaul * TX 'end of channel' interrupt handler. 112941502Swpaul */ 113041502Swpaulstatic void vr_txeoc(sc) 113141502Swpaul struct vr_softc *sc; 113241502Swpaul{ 113341502Swpaul struct ifnet *ifp; 113441502Swpaul 113541502Swpaul ifp = &sc->arpcom.ac_if; 113641502Swpaul 113741502Swpaul ifp->if_timer = 0; 113841502Swpaul 113941502Swpaul if (sc->vr_cdata.vr_tx_head == NULL) { 114041502Swpaul ifp->if_flags &= ~IFF_OACTIVE; 114141502Swpaul sc->vr_cdata.vr_tx_tail = NULL; 114241502Swpaul } 114341502Swpaul 114441502Swpaul return; 114541502Swpaul} 114641502Swpaul 114751432Swpaulstatic void vr_tick(xsc) 114851432Swpaul void *xsc; 114951432Swpaul{ 115051432Swpaul struct vr_softc *sc; 115151432Swpaul struct mii_data *mii; 115251432Swpaul 115351432Swpaul sc = xsc; 115467087Swpaul VR_LOCK(sc); 115551432Swpaul mii = device_get_softc(sc->vr_miibus); 115651432Swpaul mii_tick(mii); 115751432Swpaul 115851432Swpaul sc->vr_stat_ch = timeout(vr_tick, sc, hz); 115951432Swpaul 116067087Swpaul VR_UNLOCK(sc); 116151432Swpaul 116251432Swpaul return; 116351432Swpaul} 116451432Swpaul 116541502Swpaulstatic void vr_intr(arg) 116641502Swpaul void *arg; 116741502Swpaul{ 116841502Swpaul struct vr_softc *sc; 116941502Swpaul struct ifnet *ifp; 117041502Swpaul u_int16_t status; 117141502Swpaul 117241502Swpaul sc = arg; 117367087Swpaul VR_LOCK(sc); 117441502Swpaul ifp = &sc->arpcom.ac_if; 117541502Swpaul 117641502Swpaul /* Supress unwanted interrupts. */ 117741502Swpaul if (!(ifp->if_flags & IFF_UP)) { 117841502Swpaul vr_stop(sc); 117967087Swpaul VR_UNLOCK(sc); 118041502Swpaul return; 118141502Swpaul } 118241502Swpaul 118341502Swpaul /* Disable interrupts. */ 118441502Swpaul CSR_WRITE_2(sc, VR_IMR, 0x0000); 118541502Swpaul 118641502Swpaul for (;;) { 118741502Swpaul 118841502Swpaul status = CSR_READ_2(sc, VR_ISR); 118941502Swpaul if (status) 119041502Swpaul CSR_WRITE_2(sc, VR_ISR, status); 119141502Swpaul 119241502Swpaul if ((status & VR_INTRS) == 0) 119341502Swpaul break; 119441502Swpaul 119541502Swpaul if (status & VR_ISR_RX_OK) 119641502Swpaul vr_rxeof(sc); 119741502Swpaul 119841502Swpaul if ((status & VR_ISR_RX_ERR) || (status & VR_ISR_RX_NOBUF) || 119941502Swpaul (status & VR_ISR_RX_NOBUF) || (status & VR_ISR_RX_OFLOW) || 120041502Swpaul (status & VR_ISR_RX_DROPPED)) { 120141502Swpaul vr_rxeof(sc); 120241502Swpaul vr_rxeoc(sc); 120341502Swpaul } 120441502Swpaul 120541502Swpaul if (status & VR_ISR_TX_OK) { 120641502Swpaul vr_txeof(sc); 120741502Swpaul vr_txeoc(sc); 120841502Swpaul } 120941502Swpaul 121041502Swpaul if ((status & VR_ISR_TX_UNDERRUN)||(status & VR_ISR_TX_ABRT)){ 121141502Swpaul ifp->if_oerrors++; 121241502Swpaul vr_txeof(sc); 121341502Swpaul if (sc->vr_cdata.vr_tx_head != NULL) { 121441502Swpaul VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON); 121541502Swpaul VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO); 121641502Swpaul } 121741502Swpaul } 121841502Swpaul 121941502Swpaul if (status & VR_ISR_BUSERR) { 122041502Swpaul vr_reset(sc); 122141502Swpaul vr_init(sc); 122241502Swpaul } 122341502Swpaul } 122441502Swpaul 122541502Swpaul /* Re-enable interrupts. */ 122641502Swpaul CSR_WRITE_2(sc, VR_IMR, VR_INTRS); 122741502Swpaul 122841502Swpaul if (ifp->if_snd.ifq_head != NULL) { 122941502Swpaul vr_start(ifp); 123041502Swpaul } 123141502Swpaul 123267087Swpaul VR_UNLOCK(sc); 123367087Swpaul 123441502Swpaul return; 123541502Swpaul} 123641502Swpaul 123741502Swpaul/* 123841502Swpaul * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 123941502Swpaul * pointers to the fragment pointers. 124041502Swpaul */ 124141502Swpaulstatic int vr_encap(sc, c, m_head) 124241502Swpaul struct vr_softc *sc; 124341502Swpaul struct vr_chain *c; 124441502Swpaul struct mbuf *m_head; 124541502Swpaul{ 124641502Swpaul int frag = 0; 124741502Swpaul struct vr_desc *f = NULL; 124841502Swpaul int total_len; 124941502Swpaul struct mbuf *m; 125041502Swpaul 125141502Swpaul m = m_head; 125241502Swpaul total_len = 0; 125341502Swpaul 125441502Swpaul /* 125541502Swpaul * The VIA Rhine wants packet buffers to be longword 125641502Swpaul * aligned, but very often our mbufs aren't. Rather than 125741502Swpaul * waste time trying to decide when to copy and when not 125841502Swpaul * to copy, just do it all the time. 125941502Swpaul */ 126041502Swpaul if (m != NULL) { 126141502Swpaul struct mbuf *m_new = NULL; 126241502Swpaul 126341502Swpaul MGETHDR(m_new, M_DONTWAIT, MT_DATA); 126441502Swpaul if (m_new == NULL) { 126571271Swpaul printf("vr%d: no memory for tx list\n", sc->vr_unit); 126641502Swpaul return(1); 126741502Swpaul } 126841502Swpaul if (m_head->m_pkthdr.len > MHLEN) { 126941502Swpaul MCLGET(m_new, M_DONTWAIT); 127041502Swpaul if (!(m_new->m_flags & M_EXT)) { 127141502Swpaul m_freem(m_new); 127271271Swpaul printf("vr%d: no memory for tx list\n", 127341502Swpaul sc->vr_unit); 127441502Swpaul return(1); 127541502Swpaul } 127641502Swpaul } 127741502Swpaul m_copydata(m_head, 0, m_head->m_pkthdr.len, 127841502Swpaul mtod(m_new, caddr_t)); 127941502Swpaul m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len; 128041502Swpaul m_freem(m_head); 128141502Swpaul m_head = m_new; 128241502Swpaul /* 128341502Swpaul * The Rhine chip doesn't auto-pad, so we have to make 128441502Swpaul * sure to pad short frames out to the minimum frame length 128541502Swpaul * ourselves. 128641502Swpaul */ 128741502Swpaul if (m_head->m_len < VR_MIN_FRAMELEN) { 128841502Swpaul m_new->m_pkthdr.len += VR_MIN_FRAMELEN - m_new->m_len; 128941502Swpaul m_new->m_len = m_new->m_pkthdr.len; 129041502Swpaul } 129141502Swpaul f = c->vr_ptr; 129241502Swpaul f->vr_data = vtophys(mtod(m_new, caddr_t)); 129341502Swpaul f->vr_ctl = total_len = m_new->m_len; 129441502Swpaul f->vr_ctl |= VR_TXCTL_TLINK|VR_TXCTL_FIRSTFRAG; 129541502Swpaul f->vr_status = 0; 129641502Swpaul frag = 1; 129741502Swpaul } 129841502Swpaul 129941502Swpaul c->vr_mbuf = m_head; 130042491Swpaul c->vr_ptr->vr_ctl |= VR_TXCTL_LASTFRAG|VR_TXCTL_FINT; 130141502Swpaul c->vr_ptr->vr_next = vtophys(c->vr_nextdesc->vr_ptr); 130241502Swpaul 130341502Swpaul return(0); 130441502Swpaul} 130541502Swpaul 130641502Swpaul/* 130741502Swpaul * Main transmit routine. To avoid having to do mbuf copies, we put pointers 130841502Swpaul * to the mbuf data regions directly in the transmit lists. We also save a 130941502Swpaul * copy of the pointers since the transmit list fragment pointers are 131041502Swpaul * physical addresses. 131141502Swpaul */ 131241502Swpaul 131341502Swpaulstatic void vr_start(ifp) 131441502Swpaul struct ifnet *ifp; 131541502Swpaul{ 131641502Swpaul struct vr_softc *sc; 131741502Swpaul struct mbuf *m_head = NULL; 131841502Swpaul struct vr_chain *cur_tx = NULL, *start_tx; 131941502Swpaul 132041502Swpaul sc = ifp->if_softc; 132141502Swpaul 132267087Swpaul VR_LOCK(sc); 132367087Swpaul if (ifp->if_flags & IFF_OACTIVE) { 132467087Swpaul VR_UNLOCK(sc); 132541502Swpaul return; 132667087Swpaul } 132741502Swpaul 132841502Swpaul /* 132941502Swpaul * Check for an available queue slot. If there are none, 133041502Swpaul * punt. 133141502Swpaul */ 133241502Swpaul if (sc->vr_cdata.vr_tx_free->vr_mbuf != NULL) { 133341502Swpaul ifp->if_flags |= IFF_OACTIVE; 133441502Swpaul return; 133541502Swpaul } 133641502Swpaul 133741502Swpaul start_tx = sc->vr_cdata.vr_tx_free; 133841502Swpaul 133941502Swpaul while(sc->vr_cdata.vr_tx_free->vr_mbuf == NULL) { 134041502Swpaul IF_DEQUEUE(&ifp->if_snd, m_head); 134141502Swpaul if (m_head == NULL) 134241502Swpaul break; 134341502Swpaul 134441502Swpaul /* Pick a descriptor off the free list. */ 134541502Swpaul cur_tx = sc->vr_cdata.vr_tx_free; 134641502Swpaul sc->vr_cdata.vr_tx_free = cur_tx->vr_nextdesc; 134741502Swpaul 134841502Swpaul /* Pack the data into the descriptor. */ 134971271Swpaul if (vr_encap(sc, cur_tx, m_head)) { 135071271Swpaul IF_PREPEND(&ifp->if_snd, m_head); 135171271Swpaul cur_tx = NULL; 135271271Swpaul break; 135371271Swpaul } 135441502Swpaul 135541502Swpaul if (cur_tx != start_tx) 135641502Swpaul VR_TXOWN(cur_tx) = VR_TXSTAT_OWN; 135741502Swpaul 135841502Swpaul /* 135941502Swpaul * If there's a BPF listener, bounce a copy of this frame 136041502Swpaul * to him. 136141502Swpaul */ 136241502Swpaul if (ifp->if_bpf) 136341502Swpaul bpf_mtap(ifp, cur_tx->vr_mbuf); 136451583Swpaul 136542491Swpaul VR_TXOWN(cur_tx) = VR_TXSTAT_OWN; 136651432Swpaul VR_SETBIT16(sc, VR_COMMAND, /*VR_CMD_TX_ON|*/VR_CMD_TX_GO); 136741502Swpaul } 136841502Swpaul 136941502Swpaul /* 137041526Swpaul * If there are no frames queued, bail. 137141526Swpaul */ 137267087Swpaul if (cur_tx == NULL) { 137367087Swpaul VR_UNLOCK(sc); 137441526Swpaul return; 137567087Swpaul } 137641526Swpaul 137741502Swpaul sc->vr_cdata.vr_tx_tail = cur_tx; 137841502Swpaul 137942491Swpaul if (sc->vr_cdata.vr_tx_head == NULL) 138041502Swpaul sc->vr_cdata.vr_tx_head = start_tx; 138141502Swpaul 138241502Swpaul /* 138341502Swpaul * Set a timeout in case the chip goes out to lunch. 138441502Swpaul */ 138541502Swpaul ifp->if_timer = 5; 138667087Swpaul VR_UNLOCK(sc); 138741502Swpaul 138841502Swpaul return; 138941502Swpaul} 139041502Swpaul 139141502Swpaulstatic void vr_init(xsc) 139241502Swpaul void *xsc; 139341502Swpaul{ 139441502Swpaul struct vr_softc *sc = xsc; 139541502Swpaul struct ifnet *ifp = &sc->arpcom.ac_if; 139651432Swpaul struct mii_data *mii; 139741502Swpaul 139867087Swpaul VR_LOCK(sc); 139941502Swpaul 140051432Swpaul mii = device_get_softc(sc->vr_miibus); 140141502Swpaul 140241502Swpaul /* 140341502Swpaul * Cancel pending I/O and free all RX/TX buffers. 140441502Swpaul */ 140541502Swpaul vr_stop(sc); 140641502Swpaul vr_reset(sc); 140741502Swpaul 140841502Swpaul VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_THRESH); 140941502Swpaul VR_SETBIT(sc, VR_RXCFG, VR_RXTHRESH_STORENFWD); 141041502Swpaul 141141502Swpaul VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TX_THRESH); 141241502Swpaul VR_SETBIT(sc, VR_TXCFG, VR_TXTHRESH_STORENFWD); 141341502Swpaul 141441502Swpaul /* Init circular RX list. */ 141541502Swpaul if (vr_list_rx_init(sc) == ENOBUFS) { 141641502Swpaul printf("vr%d: initialization failed: no " 141741502Swpaul "memory for rx buffers\n", sc->vr_unit); 141841502Swpaul vr_stop(sc); 141967087Swpaul VR_UNLOCK(sc); 142041502Swpaul return; 142141502Swpaul } 142241502Swpaul 142341502Swpaul /* 142441502Swpaul * Init tx descriptors. 142541502Swpaul */ 142641502Swpaul vr_list_tx_init(sc); 142741502Swpaul 142841502Swpaul /* If we want promiscuous mode, set the allframes bit. */ 142941502Swpaul if (ifp->if_flags & IFF_PROMISC) 143041502Swpaul VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC); 143141502Swpaul else 143241502Swpaul VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC); 143341502Swpaul 143441502Swpaul /* Set capture broadcast bit to capture broadcast frames. */ 143541502Swpaul if (ifp->if_flags & IFF_BROADCAST) 143641502Swpaul VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD); 143741502Swpaul else 143841502Swpaul VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD); 143941502Swpaul 144041502Swpaul /* 144141502Swpaul * Program the multicast filter, if necessary. 144241502Swpaul */ 144341502Swpaul vr_setmulti(sc); 144441502Swpaul 144541502Swpaul /* 144641502Swpaul * Load the address of the RX list. 144741502Swpaul */ 144841502Swpaul CSR_WRITE_4(sc, VR_RXADDR, vtophys(sc->vr_cdata.vr_rx_head->vr_ptr)); 144941502Swpaul 145041502Swpaul /* Enable receiver and transmitter. */ 145141502Swpaul CSR_WRITE_2(sc, VR_COMMAND, VR_CMD_TX_NOPOLL|VR_CMD_START| 145241502Swpaul VR_CMD_TX_ON|VR_CMD_RX_ON| 145341502Swpaul VR_CMD_RX_GO); 145441502Swpaul 145541502Swpaul CSR_WRITE_4(sc, VR_TXADDR, vtophys(&sc->vr_ldata->vr_tx_list[0])); 145641502Swpaul 145741502Swpaul /* 145841502Swpaul * Enable interrupts. 145941502Swpaul */ 146041502Swpaul CSR_WRITE_2(sc, VR_ISR, 0xFFFF); 146141502Swpaul CSR_WRITE_2(sc, VR_IMR, VR_INTRS); 146241502Swpaul 146351432Swpaul mii_mediachg(mii); 146441502Swpaul 146541502Swpaul ifp->if_flags |= IFF_RUNNING; 146641502Swpaul ifp->if_flags &= ~IFF_OACTIVE; 146741502Swpaul 146851432Swpaul sc->vr_stat_ch = timeout(vr_tick, sc, hz); 146951432Swpaul 147067087Swpaul VR_UNLOCK(sc); 147167087Swpaul 147241502Swpaul return; 147341502Swpaul} 147441502Swpaul 147541502Swpaul/* 147641502Swpaul * Set media options. 147741502Swpaul */ 147841502Swpaulstatic int vr_ifmedia_upd(ifp) 147941502Swpaul struct ifnet *ifp; 148041502Swpaul{ 148141502Swpaul struct vr_softc *sc; 148241502Swpaul 148341502Swpaul sc = ifp->if_softc; 148441502Swpaul 148551432Swpaul if (ifp->if_flags & IFF_UP) 148651432Swpaul vr_init(sc); 148741502Swpaul 148841502Swpaul return(0); 148941502Swpaul} 149041502Swpaul 149141502Swpaul/* 149241502Swpaul * Report current media status. 149341502Swpaul */ 149441502Swpaulstatic void vr_ifmedia_sts(ifp, ifmr) 149541502Swpaul struct ifnet *ifp; 149641502Swpaul struct ifmediareq *ifmr; 149741502Swpaul{ 149841502Swpaul struct vr_softc *sc; 149951432Swpaul struct mii_data *mii; 150041502Swpaul 150141502Swpaul sc = ifp->if_softc; 150251432Swpaul mii = device_get_softc(sc->vr_miibus); 150351432Swpaul mii_pollstat(mii); 150451432Swpaul ifmr->ifm_active = mii->mii_media_active; 150551432Swpaul ifmr->ifm_status = mii->mii_media_status; 150641502Swpaul 150741502Swpaul return; 150841502Swpaul} 150941502Swpaul 151041502Swpaulstatic int vr_ioctl(ifp, command, data) 151141502Swpaul struct ifnet *ifp; 151241502Swpaul u_long command; 151341502Swpaul caddr_t data; 151441502Swpaul{ 151541502Swpaul struct vr_softc *sc = ifp->if_softc; 151641502Swpaul struct ifreq *ifr = (struct ifreq *) data; 151751432Swpaul struct mii_data *mii; 151867087Swpaul int error = 0; 151941502Swpaul 152067087Swpaul VR_LOCK(sc); 152141502Swpaul 152241502Swpaul switch(command) { 152341502Swpaul case SIOCSIFADDR: 152441502Swpaul case SIOCGIFADDR: 152541502Swpaul case SIOCSIFMTU: 152641502Swpaul error = ether_ioctl(ifp, command, data); 152741502Swpaul break; 152841502Swpaul case SIOCSIFFLAGS: 152941502Swpaul if (ifp->if_flags & IFF_UP) { 153041502Swpaul vr_init(sc); 153141502Swpaul } else { 153241502Swpaul if (ifp->if_flags & IFF_RUNNING) 153341502Swpaul vr_stop(sc); 153441502Swpaul } 153541502Swpaul error = 0; 153641502Swpaul break; 153741502Swpaul case SIOCADDMULTI: 153841502Swpaul case SIOCDELMULTI: 153941502Swpaul vr_setmulti(sc); 154041502Swpaul error = 0; 154141502Swpaul break; 154241502Swpaul case SIOCGIFMEDIA: 154341502Swpaul case SIOCSIFMEDIA: 154451432Swpaul mii = device_get_softc(sc->vr_miibus); 154551432Swpaul error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 154641502Swpaul break; 154741502Swpaul default: 154841502Swpaul error = EINVAL; 154941502Swpaul break; 155041502Swpaul } 155141502Swpaul 155267087Swpaul VR_UNLOCK(sc); 155341502Swpaul 155441502Swpaul return(error); 155541502Swpaul} 155641502Swpaul 155741502Swpaulstatic void vr_watchdog(ifp) 155841502Swpaul struct ifnet *ifp; 155941502Swpaul{ 156041502Swpaul struct vr_softc *sc; 156141502Swpaul 156241502Swpaul sc = ifp->if_softc; 156341502Swpaul 156467087Swpaul VR_LOCK(sc); 156541502Swpaul ifp->if_oerrors++; 156641502Swpaul printf("vr%d: watchdog timeout\n", sc->vr_unit); 156741502Swpaul 156841502Swpaul vr_stop(sc); 156941502Swpaul vr_reset(sc); 157041502Swpaul vr_init(sc); 157141502Swpaul 157241502Swpaul if (ifp->if_snd.ifq_head != NULL) 157341502Swpaul vr_start(ifp); 157441502Swpaul 157567087Swpaul VR_UNLOCK(sc); 157667087Swpaul 157741502Swpaul return; 157841502Swpaul} 157941502Swpaul 158041502Swpaul/* 158141502Swpaul * Stop the adapter and free any mbufs allocated to the 158241502Swpaul * RX and TX lists. 158341502Swpaul */ 158441502Swpaulstatic void vr_stop(sc) 158541502Swpaul struct vr_softc *sc; 158641502Swpaul{ 158741502Swpaul register int i; 158841502Swpaul struct ifnet *ifp; 158941502Swpaul 159067087Swpaul VR_LOCK(sc); 159167087Swpaul 159241502Swpaul ifp = &sc->arpcom.ac_if; 159341502Swpaul ifp->if_timer = 0; 159441502Swpaul 159551432Swpaul untimeout(vr_tick, sc, sc->vr_stat_ch); 159651432Swpaul 159741502Swpaul VR_SETBIT16(sc, VR_COMMAND, VR_CMD_STOP); 159841502Swpaul VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_RX_ON|VR_CMD_TX_ON)); 159941502Swpaul CSR_WRITE_2(sc, VR_IMR, 0x0000); 160041502Swpaul CSR_WRITE_4(sc, VR_TXADDR, 0x00000000); 160141502Swpaul CSR_WRITE_4(sc, VR_RXADDR, 0x00000000); 160241502Swpaul 160341502Swpaul /* 160441502Swpaul * Free data in the RX lists. 160541502Swpaul */ 160641502Swpaul for (i = 0; i < VR_RX_LIST_CNT; i++) { 160741502Swpaul if (sc->vr_cdata.vr_rx_chain[i].vr_mbuf != NULL) { 160841502Swpaul m_freem(sc->vr_cdata.vr_rx_chain[i].vr_mbuf); 160941502Swpaul sc->vr_cdata.vr_rx_chain[i].vr_mbuf = NULL; 161041502Swpaul } 161141502Swpaul } 161241502Swpaul bzero((char *)&sc->vr_ldata->vr_rx_list, 161341502Swpaul sizeof(sc->vr_ldata->vr_rx_list)); 161441502Swpaul 161541502Swpaul /* 161641502Swpaul * Free the TX list buffers. 161741502Swpaul */ 161841502Swpaul for (i = 0; i < VR_TX_LIST_CNT; i++) { 161941502Swpaul if (sc->vr_cdata.vr_tx_chain[i].vr_mbuf != NULL) { 162041502Swpaul m_freem(sc->vr_cdata.vr_tx_chain[i].vr_mbuf); 162141502Swpaul sc->vr_cdata.vr_tx_chain[i].vr_mbuf = NULL; 162241502Swpaul } 162341502Swpaul } 162441502Swpaul 162541502Swpaul bzero((char *)&sc->vr_ldata->vr_tx_list, 162641502Swpaul sizeof(sc->vr_ldata->vr_tx_list)); 162741502Swpaul 162841502Swpaul ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 162967087Swpaul VR_UNLOCK(sc); 163041502Swpaul 163141502Swpaul return; 163241502Swpaul} 163341502Swpaul 163441502Swpaul/* 163541502Swpaul * Stop all chip I/O so that the kernel's probe routines don't 163641502Swpaul * get confused by errant DMAs when rebooting. 163741502Swpaul */ 163849610Swpaulstatic void vr_shutdown(dev) 163949610Swpaul device_t dev; 164041502Swpaul{ 164149610Swpaul struct vr_softc *sc; 164241502Swpaul 164349610Swpaul sc = device_get_softc(dev); 164449610Swpaul 164541502Swpaul vr_stop(sc); 164641502Swpaul 164741502Swpaul return; 164841502Swpaul} 1649