Home | History | Annotate | Line # | Download | only in pci
if_vr.c revision 1.19
      1 /*	$NetBSD: if_vr.c,v 1.19 1999/03/24 01:07:59 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1997, 1998
     42  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by Bill Paul.
     55  * 4. Neither the name of the author nor the names of any co-contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     69  * THE POSSIBILITY OF SUCH DAMAGE.
     70  *
     71  *	$FreeBSD: if_vr.c,v 1.7 1999/01/10 18:51:49 wpaul Exp $
     72  */
     73 
     74 /*
     75  * VIA Rhine fast ethernet PCI NIC driver
     76  *
     77  * Supports various network adapters based on the VIA Rhine
     78  * and Rhine II PCI controllers, including the D-Link DFE530TX.
     79  * Datasheets are available at http://www.via.com.tw.
     80  *
     81  * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
     82  * Electrical Engineering Department
     83  * Columbia University, New York City
     84  */
     85 
     86 /*
     87  * The VIA Rhine controllers are similar in some respects to the
     88  * the DEC tulip chips, except less complicated. The controller
     89  * uses an MII bus and an external physical layer interface. The
     90  * receiver has a one entry perfect filter and a 64-bit hash table
     91  * multicast filter. Transmit and receive descriptors are similar
     92  * to the tulip.
     93  *
     94  * The Rhine has a serious flaw in its transmit DMA mechanism:
     95  * transmit buffers must be longword aligned. Unfortunately,
     96  * the kernel doesn't guarantee that mbufs will be filled in starting
     97  * at longword boundaries, so we have to do a buffer copy before
     98  * transmission.
     99  *
    100  * Apparently, the receive DMA mechanism also has the same flaw.  This
    101  * means that on systems with struct alignment requirements, incoming
    102  * frames must be copied to a new buffer which shifts the data forward
    103  * 2 bytes so that the payload is aligned on a 4-byte boundary.
    104  */
    105 
    106 #include "opt_inet.h"
    107 
    108 #include <sys/param.h>
    109 #include <sys/systm.h>
    110 #include <sys/sockio.h>
    111 #include <sys/mbuf.h>
    112 #include <sys/malloc.h>
    113 #include <sys/kernel.h>
    114 #include <sys/socket.h>
    115 #include <sys/device.h>
    116 
    117 #include <vm/vm.h>		/* for PAGE_SIZE */
    118 
    119 #include <net/if.h>
    120 #include <net/if_arp.h>
    121 #include <net/if_dl.h>
    122 #include <net/if_media.h>
    123 #include <net/if_ether.h>
    124 
    125 #if defined(INET)
    126 #include <netinet/in.h>
    127 #include <netinet/if_inarp.h>
    128 #endif
    129 
    130 #include "bpfilter.h"
    131 #if NBPFILTER > 0
    132 #include <net/bpf.h>
    133 #endif
    134 
    135 #include <machine/bus.h>
    136 #include <machine/intr.h>
    137 
    138 #include <dev/mii/mii.h>
    139 #include <dev/mii/miivar.h>
    140 
    141 #include <dev/pci/pcireg.h>
    142 #include <dev/pci/pcivar.h>
    143 #include <dev/pci/pcidevs.h>
    144 
    145 #include <dev/pci/if_vrreg.h>
    146 
    147 #define	VR_USEIOSPACE
    148 
    149 /*
    150  * Various supported device vendors/types and their names.
    151  */
    152 static struct vr_type {
    153 	pci_vendor_id_t		vr_vid;
    154 	pci_product_id_t	vr_did;
    155 	const char		*vr_name;
    156 } vr_devs[] = {
    157 	{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT3043,
    158 		"VIA VT3043 (Rhine) 10/100 Ethernet" },
    159 	{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT86C100A,
    160 		"VIA VT86C100A (Rhine-II) 10/100 Ethernet" },
    161 	{ 0, 0, NULL }
    162 };
    163 
    164 /*
    165  * Transmit descriptor list size.
    166  */
    167 #define	VR_NTXDESC		64
    168 #define	VR_NTXDESC_MASK		(VR_NTXDESC - 1)
    169 #define	VR_NEXTTX(x)		(((x) + 1) & VR_NTXDESC_MASK)
    170 
    171 /*
    172  * Receive descriptor list size.
    173  */
    174 #define	VR_NRXDESC		64
    175 #define	VR_NRXDESC_MASK		(VR_NRXDESC - 1)
    176 #define	VR_NEXTRX(x)		(((x) + 1) & VR_NRXDESC_MASK)
    177 
    178 /*
    179  * Control data structres that are DMA'd to the Rhine chip.  We allocate
    180  * them in a single clump that maps to a single DMA segment to make several
    181  * things easier.
    182  *
    183  * Note that since we always copy outgoing packets to aligned transmit
    184  * buffers, we can reduce the transmit descriptors to one per packet.
    185  */
    186 struct vr_control_data {
    187 	struct vr_desc		vr_txdescs[VR_NTXDESC];
    188 	struct vr_desc		vr_rxdescs[VR_NRXDESC];
    189 };
    190 
    191 #define	VR_CDOFF(x)		offsetof(struct vr_control_data, x)
    192 #define	VR_CDTXOFF(x)		VR_CDOFF(vr_txdescs[(x)])
    193 #define	VR_CDRXOFF(x)		VR_CDOFF(vr_rxdescs[(x)])
    194 
    195 /*
    196  * Software state of transmit and receive descriptors.
    197  */
    198 struct vr_descsoft {
    199 	struct mbuf		*ds_mbuf;	/* head of mbuf chain */
    200 	bus_dmamap_t		ds_dmamap;	/* our DMA map */
    201 };
    202 
    203 struct vr_softc {
    204 	struct device		vr_dev;		/* generic device glue */
    205 	void			*vr_ih;		/* interrupt cookie */
    206 	void			*vr_ats;	/* shutdown hook */
    207 	bus_space_tag_t		vr_bst;		/* bus space tag */
    208 	bus_space_handle_t	vr_bsh;		/* bus space handle */
    209 	bus_dma_tag_t		vr_dmat;	/* bus DMA tag */
    210 	pci_chipset_tag_t	vr_pc;		/* PCI chipset info */
    211 	struct ethercom		vr_ec;		/* Ethernet common info */
    212 	u_int8_t 		vr_enaddr[ETHER_ADDR_LEN];
    213 	struct mii_data		vr_mii;		/* MII/media info */
    214 
    215 	bus_dmamap_t		vr_cddmamap;	/* control data DMA map */
    216 #define	vr_cddma	vr_cddmamap->dm_segs[0].ds_addr
    217 
    218 	/*
    219 	 * Software state for transmit and receive descriptors.
    220 	 */
    221 	struct vr_descsoft	vr_txsoft[VR_NTXDESC];
    222 	struct vr_descsoft	vr_rxsoft[VR_NRXDESC];
    223 
    224 	/*
    225 	 * Control data structures.
    226 	 */
    227 	struct vr_control_data	*vr_control_data;
    228 
    229 	int	vr_txpending;		/* number of TX requests pending */
    230 	int	vr_txdirty;		/* first dirty TX descriptor */
    231 	int	vr_txlast;		/* last used TX descriptor */
    232 
    233 	int	vr_rxptr;		/* next ready RX descriptor */
    234 };
    235 
    236 #define	VR_CDTXADDR(sc, x)	((sc)->vr_cddma + VR_CDTXOFF((x)))
    237 #define	VR_CDRXADDR(sc, x)	((sc)->vr_cddma + VR_CDRXOFF((x)))
    238 
    239 #define	VR_CDTX(sc, x)		(&(sc)->vr_control_data->vr_txdescs[(x)])
    240 #define	VR_CDRX(sc, x)		(&(sc)->vr_control_data->vr_rxdescs[(x)])
    241 
    242 #define	VR_DSTX(sc, x)		(&(sc)->vr_txsoft[(x)])
    243 #define	VR_DSRX(sc, x)		(&(sc)->vr_rxsoft[(x)])
    244 
    245 #define	VR_CDTXSYNC(sc, x, ops)						\
    246 	bus_dmamap_sync((sc)->vr_dmat, (sc)->vr_cddmamap,		\
    247 	    VR_CDTXOFF((x)), sizeof(struct vr_desc), (ops))
    248 
    249 #define	VR_CDRXSYNC(sc, x, ops)						\
    250 	bus_dmamap_sync((sc)->vr_dmat, (sc)->vr_cddmamap,		\
    251 	    VR_CDRXOFF((x)), sizeof(struct vr_desc), (ops))
    252 
    253 /*
    254  * Note we rely on MCLBYTES being a power of two below.
    255  */
    256 #define	VR_INIT_RXDESC(sc, i)						\
    257 do {									\
    258 	struct vr_desc *__d = VR_CDRX((sc), (i));			\
    259 	struct vr_descsoft *__ds = VR_DSRX((sc), (i));			\
    260 									\
    261 	__d->vr_next = VR_CDRXADDR((sc), VR_NEXTRX((i)));		\
    262 	__d->vr_status = VR_RXSTAT_FIRSTFRAG | VR_RXSTAT_LASTFRAG |	\
    263 	    VR_RXSTAT_OWN;						\
    264 	__d->vr_data = __ds->ds_dmamap->dm_segs[0].ds_addr;		\
    265 	__d->vr_ctl = VR_RXCTL_CHAIN | VR_RXCTL_RX_INTR |		\
    266 	    ((MCLBYTES - 1) & VR_RXCTL_BUFLEN);				\
    267 	VR_CDRXSYNC((sc), (i), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
    268 } while (0)
    269 
    270 /*
    271  * register space access macros
    272  */
    273 #define	CSR_WRITE_4(sc, reg, val)					\
    274 	bus_space_write_4(sc->vr_bst, sc->vr_bsh, reg, val)
    275 #define	CSR_WRITE_2(sc, reg, val)					\
    276 	bus_space_write_2(sc->vr_bst, sc->vr_bsh, reg, val)
    277 #define	CSR_WRITE_1(sc, reg, val)					\
    278 	bus_space_write_1(sc->vr_bst, sc->vr_bsh, reg, val)
    279 
    280 #define	CSR_READ_4(sc, reg)						\
    281 	bus_space_read_4(sc->vr_bst, sc->vr_bsh, reg)
    282 #define	CSR_READ_2(sc, reg)						\
    283 	bus_space_read_2(sc->vr_bst, sc->vr_bsh, reg)
    284 #define	CSR_READ_1(sc, reg)						\
    285 	bus_space_read_1(sc->vr_bst, sc->vr_bsh, reg)
    286 
    287 #define	VR_TIMEOUT		1000
    288 
    289 static int vr_add_rxbuf		__P((struct vr_softc *, int));
    290 
    291 static void vr_rxeof		__P((struct vr_softc *));
    292 static void vr_rxeoc		__P((struct vr_softc *));
    293 static void vr_txeof		__P((struct vr_softc *));
    294 static int vr_intr		__P((void *));
    295 static void vr_start		__P((struct ifnet *));
    296 static int vr_ioctl		__P((struct ifnet *, u_long, caddr_t));
    297 static void vr_init		__P((void *));
    298 static void vr_stop		__P((struct vr_softc *));
    299 static void vr_watchdog		__P((struct ifnet *));
    300 static void vr_tick		__P((void *));
    301 
    302 static int vr_ifmedia_upd	__P((struct ifnet *));
    303 static void vr_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
    304 
    305 static void vr_mii_sync		__P((struct vr_softc *));
    306 static void vr_mii_send		__P((struct vr_softc *, u_int32_t, int));
    307 static int vr_mii_readreg	__P((struct device *, int, int));
    308 static void vr_mii_writereg	__P((struct device *, int, int, int));
    309 static void vr_mii_statchg	__P((struct device *));
    310 
    311 static u_int8_t vr_calchash	__P((u_int8_t *));
    312 static void vr_setmulti		__P((struct vr_softc *));
    313 static void vr_reset		__P((struct vr_softc *));
    314 
    315 #define	VR_SETBIT(sc, reg, x)				\
    316 	CSR_WRITE_1(sc, reg,				\
    317 		CSR_READ_1(sc, reg) | x)
    318 
    319 #define	VR_CLRBIT(sc, reg, x)				\
    320 	CSR_WRITE_1(sc, reg,				\
    321 		CSR_READ_1(sc, reg) & ~x)
    322 
    323 #define	VR_SETBIT16(sc, reg, x)				\
    324 	CSR_WRITE_2(sc, reg,				\
    325 		CSR_READ_2(sc, reg) | x)
    326 
    327 #define	VR_CLRBIT16(sc, reg, x)				\
    328 	CSR_WRITE_2(sc, reg,				\
    329 		CSR_READ_2(sc, reg) & ~x)
    330 
    331 #define	VR_SETBIT32(sc, reg, x)				\
    332 	CSR_WRITE_4(sc, reg,				\
    333 		CSR_READ_4(sc, reg) | x)
    334 
    335 #define	VR_CLRBIT32(sc, reg, x)				\
    336 	CSR_WRITE_4(sc, reg,				\
    337 		CSR_READ_4(sc, reg) & ~x)
    338 
    339 #define	SIO_SET(x)					\
    340 	CSR_WRITE_1(sc, VR_MIICMD,			\
    341 		CSR_READ_1(sc, VR_MIICMD) | x)
    342 
    343 #define	SIO_CLR(x)					\
    344 	CSR_WRITE_1(sc, VR_MIICMD,			\
    345 		CSR_READ_1(sc, VR_MIICMD) & ~x)
    346 
    347 /*
    348  * Sync the PHYs by setting data bit and strobing the clock 32 times.
    349  */
    350 static void
    351 vr_mii_sync(sc)
    352 	struct vr_softc *sc;
    353 {
    354 	int i;
    355 
    356 	SIO_SET(VR_MIICMD_DIR|VR_MIICMD_DATAOUT);
    357 
    358 	for (i = 0; i < 32; i++) {
    359 		SIO_SET(VR_MIICMD_CLK);
    360 		DELAY(1);
    361 		SIO_CLR(VR_MIICMD_CLK);
    362 		DELAY(1);
    363 	}
    364 }
    365 
    366 /*
    367  * Clock a series of bits through the MII.
    368  */
    369 static void
    370 vr_mii_send(sc, bits, cnt)
    371 	struct vr_softc *sc;
    372 	u_int32_t bits;
    373 	int cnt;
    374 {
    375 	int i;
    376 
    377 	SIO_CLR(VR_MIICMD_CLK);
    378 
    379 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
    380 		if (bits & i) {
    381 			SIO_SET(VR_MIICMD_DATAOUT);
    382 		} else {
    383 			SIO_CLR(VR_MIICMD_DATAOUT);
    384 		}
    385 		DELAY(1);
    386 		SIO_CLR(VR_MIICMD_CLK);
    387 		DELAY(1);
    388 		SIO_SET(VR_MIICMD_CLK);
    389 	}
    390 }
    391 
    392 /*
    393  * Read an PHY register through the MII.
    394  */
    395 static int
    396 vr_mii_readreg(self, phy, reg)
    397 	struct device *self;
    398 	int phy, reg;
    399 {
    400 	struct vr_softc *sc = (struct vr_softc *)self;
    401 	int i, ack, val = 0;
    402 
    403 	CSR_WRITE_1(sc, VR_MIICMD, 0);
    404 	VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
    405 
    406 	/*
    407 	 * Turn on data xmit.
    408 	 */
    409 	SIO_SET(VR_MIICMD_DIR);
    410 
    411 	vr_mii_sync(sc);
    412 
    413 	/*
    414 	 * Send command/address info.
    415 	 */
    416 	vr_mii_send(sc, MII_COMMAND_START, 2);
    417 	vr_mii_send(sc, MII_COMMAND_READ, 2);
    418 	vr_mii_send(sc, phy, 5);
    419 	vr_mii_send(sc, reg, 5);
    420 
    421 	/* Idle bit */
    422 	SIO_CLR((VR_MIICMD_CLK|VR_MIICMD_DATAOUT));
    423 	DELAY(1);
    424 	SIO_SET(VR_MIICMD_CLK);
    425 	DELAY(1);
    426 
    427 	/* Turn off xmit. */
    428 	SIO_CLR(VR_MIICMD_DIR);
    429 
    430 	/* Check for ack */
    431 	SIO_CLR(VR_MIICMD_CLK);
    432 	DELAY(1);
    433 	SIO_SET(VR_MIICMD_CLK);
    434 	DELAY(1);
    435 	ack = CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAIN;
    436 
    437 	/*
    438 	 * Now try reading data bits. If the ack failed, we still
    439 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
    440 	 */
    441 	if (ack) {
    442 		for (i = 0; i < 16; i++) {
    443 			SIO_CLR(VR_MIICMD_CLK);
    444 			DELAY(1);
    445 			SIO_SET(VR_MIICMD_CLK);
    446 			DELAY(1);
    447 		}
    448 		goto fail;
    449 	}
    450 
    451 	for (i = 0x8000; i; i >>= 1) {
    452 		SIO_CLR(VR_MIICMD_CLK);
    453 		DELAY(1);
    454 		if (!ack) {
    455 			if (CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAIN)
    456 				val |= i;
    457 			DELAY(1);
    458 		}
    459 		SIO_SET(VR_MIICMD_CLK);
    460 		DELAY(1);
    461 	}
    462 
    463  fail:
    464 
    465 	SIO_CLR(VR_MIICMD_CLK);
    466 	DELAY(1);
    467 	SIO_SET(VR_MIICMD_CLK);
    468 	DELAY(1);
    469 
    470 	return (val);
    471 }
    472 
    473 /*
    474  * Write to a PHY register through the MII.
    475  */
    476 static void
    477 vr_mii_writereg(self, phy, reg, val)
    478 	struct device *self;
    479 	int phy, reg, val;
    480 {
    481 	struct vr_softc *sc = (struct vr_softc *)self;
    482 
    483 	CSR_WRITE_1(sc, VR_MIICMD, 0);
    484 	VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
    485 
    486 	/*
    487 	 * Turn on data output.
    488 	 */
    489 	SIO_SET(VR_MIICMD_DIR);
    490 
    491 	vr_mii_sync(sc);
    492 
    493 	vr_mii_send(sc, MII_COMMAND_START, 2);
    494 	vr_mii_send(sc, MII_COMMAND_WRITE, 2);
    495 	vr_mii_send(sc, phy, 5);
    496 	vr_mii_send(sc, reg, 5);
    497 	vr_mii_send(sc, MII_COMMAND_ACK, 2);
    498 	vr_mii_send(sc, val, 16);
    499 
    500 	/* Idle bit. */
    501 	SIO_SET(VR_MIICMD_CLK);
    502 	DELAY(1);
    503 	SIO_CLR(VR_MIICMD_CLK);
    504 	DELAY(1);
    505 
    506 	/*
    507 	 * Turn off xmit.
    508 	 */
    509 	SIO_CLR(VR_MIICMD_DIR);
    510 }
    511 
    512 static void
    513 vr_mii_statchg(self)
    514 	struct device *self;
    515 {
    516 	struct vr_softc *sc = (struct vr_softc *)self;
    517 
    518 	/*
    519 	 * In order to fiddle with the 'full-duplex' bit in the netconfig
    520 	 * register, we first have to put the transmit and/or receive logic
    521 	 * in the idle state.
    522 	 */
    523 	VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_TX_ON|VR_CMD_RX_ON));
    524 
    525 	if (sc->vr_mii.mii_media_active & IFM_FDX)
    526 		VR_SETBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
    527 	else
    528 		VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
    529 
    530 	if (sc->vr_ec.ec_if.if_flags & IFF_RUNNING)
    531 		VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_RX_ON);
    532 
    533 	/* XXX Update ifp->if_baudrate */
    534 }
    535 
    536 /*
    537  * Calculate CRC of a multicast group address, return the lower 6 bits.
    538  */
    539 static u_int8_t
    540 vr_calchash(addr)
    541 	u_int8_t *addr;
    542 {
    543 	u_int32_t crc, carry;
    544 	int i, j;
    545 	u_int8_t c;
    546 
    547 	/* Compute CRC for the address value. */
    548 	crc = 0xFFFFFFFF; /* initial value */
    549 
    550 	for (i = 0; i < 6; i++) {
    551 		c = *(addr + i);
    552 		for (j = 0; j < 8; j++) {
    553 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
    554 			crc <<= 1;
    555 			c >>= 1;
    556 			if (carry)
    557 				crc = (crc ^ 0x04c11db6) | carry;
    558 		}
    559 	}
    560 
    561 	/* return the filter bit position */
    562 	return ((crc >> 26) & 0x0000003F);
    563 }
    564 
    565 /*
    566  * Program the 64-bit multicast hash filter.
    567  */
    568 static void
    569 vr_setmulti(sc)
    570 	struct vr_softc *sc;
    571 {
    572 	struct ifnet *ifp;
    573 	int h = 0;
    574 	u_int32_t hashes[2] = { 0, 0 };
    575 	struct ether_multistep step;
    576 	struct ether_multi *enm;
    577 	int mcnt = 0;
    578 	u_int8_t rxfilt;
    579 
    580 	ifp = &sc->vr_ec.ec_if;
    581 
    582 	rxfilt = CSR_READ_1(sc, VR_RXCFG);
    583 
    584 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    585 		rxfilt |= VR_RXCFG_RX_MULTI;
    586 		CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
    587 		CSR_WRITE_4(sc, VR_MAR0, 0xFFFFFFFF);
    588 		CSR_WRITE_4(sc, VR_MAR1, 0xFFFFFFFF);
    589 		return;
    590 	}
    591 
    592 	/* first, zot all the existing hash bits */
    593 	CSR_WRITE_4(sc, VR_MAR0, 0);
    594 	CSR_WRITE_4(sc, VR_MAR1, 0);
    595 
    596 	/* now program new ones */
    597 	ETHER_FIRST_MULTI(step, &sc->vr_ec, enm);
    598 	while (enm != NULL) {
    599 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0)
    600 			continue;
    601 
    602 		h = vr_calchash(enm->enm_addrlo);
    603 
    604 		if (h < 32)
    605 			hashes[0] |= (1 << h);
    606 		else
    607 			hashes[1] |= (1 << (h - 32));
    608 		ETHER_NEXT_MULTI(step, enm);
    609 		mcnt++;
    610 	}
    611 
    612 	if (mcnt)
    613 		rxfilt |= VR_RXCFG_RX_MULTI;
    614 	else
    615 		rxfilt &= ~VR_RXCFG_RX_MULTI;
    616 
    617 	CSR_WRITE_4(sc, VR_MAR0, hashes[0]);
    618 	CSR_WRITE_4(sc, VR_MAR1, hashes[1]);
    619 	CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
    620 }
    621 
    622 static void
    623 vr_reset(sc)
    624 	struct vr_softc *sc;
    625 {
    626 	int i;
    627 
    628 	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RESET);
    629 
    630 	for (i = 0; i < VR_TIMEOUT; i++) {
    631 		DELAY(10);
    632 		if (!(CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RESET))
    633 			break;
    634 	}
    635 	if (i == VR_TIMEOUT)
    636 		printf("%s: reset never completed!\n",
    637 			sc->vr_dev.dv_xname);
    638 
    639 	/* Wait a little while for the chip to get its brains in order. */
    640 	DELAY(1000);
    641 }
    642 
    643 /*
    644  * Initialize an RX descriptor and attach an MBUF cluster.
    645  * Note: the length fields are only 11 bits wide, which means the
    646  * largest size we can specify is 2047. This is important because
    647  * MCLBYTES is 2048, so we have to subtract one otherwise we'll
    648  * overflow the field and make a mess.
    649  */
    650 static int
    651 vr_add_rxbuf(sc, i)
    652 	struct vr_softc *sc;
    653 	int i;
    654 {
    655 	struct vr_descsoft *ds = VR_DSRX(sc, i);
    656 	struct mbuf *m_new;
    657 	int error;
    658 
    659 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    660 	if (m_new == NULL)
    661 		return (ENOBUFS);
    662 
    663 	MCLGET(m_new, M_DONTWAIT);
    664 	if ((m_new->m_flags & M_EXT) == 0) {
    665 		m_freem(m_new);
    666 		return (ENOBUFS);
    667 	}
    668 
    669 	if (ds->ds_mbuf != NULL)
    670 		bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
    671 
    672 	ds->ds_mbuf = m_new;
    673 
    674 	error = bus_dmamap_load(sc->vr_dmat, ds->ds_dmamap,
    675 	    m_new->m_ext.ext_buf, m_new->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
    676 	if (error) {
    677 		printf("%s: unable to load rx DMA map %d, error = %d\n",
    678 		    sc->vr_dev.dv_xname, i, error);
    679 		panic("vr_add_rxbuf");		/* XXX */
    680 	}
    681 
    682 	bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
    683 	    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    684 
    685 	VR_INIT_RXDESC(sc, i);
    686 
    687 	return (0);
    688 }
    689 
    690 /*
    691  * A frame has been uploaded: pass the resulting mbuf chain up to
    692  * the higher level protocols.
    693  */
    694 static void
    695 vr_rxeof(sc)
    696 	struct vr_softc *sc;
    697 {
    698 	struct ether_header *eh;
    699 	struct mbuf *m;
    700 	struct ifnet *ifp;
    701 	struct vr_desc *d;
    702 	struct vr_descsoft *ds;
    703 	int i, total_len;
    704 	u_int32_t rxstat;
    705 
    706 	ifp = &sc->vr_ec.ec_if;
    707 
    708 	for (i = sc->vr_rxptr;; i = VR_NEXTRX(i)) {
    709 		d = VR_CDRX(sc, i);
    710 		ds = VR_DSRX(sc, i);
    711 
    712 		VR_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    713 
    714 		rxstat = d->vr_status;
    715 
    716 		if (rxstat & VR_RXSTAT_OWN) {
    717 			/*
    718 			 * We have processed all of the receive buffers.
    719 			 */
    720 			break;
    721 		}
    722 
    723 		/*
    724 		 * If an error occurs, update stats, clear the
    725 		 * status word and leave the mbuf cluster in place:
    726 		 * it should simply get re-used next time this descriptor
    727 		 * comes up in the ring.
    728 		 */
    729 		if (rxstat & VR_RXSTAT_RXERR) {
    730 			const char *errstr;
    731 
    732 			ifp->if_ierrors++;
    733 			switch (rxstat & 0x000000FF) {
    734 			case VR_RXSTAT_CRCERR:
    735 				errstr = "crc error";
    736 				break;
    737 			case VR_RXSTAT_FRAMEALIGNERR:
    738 				errstr = "frame alignment error";
    739 				break;
    740 			case VR_RXSTAT_FIFOOFLOW:
    741 				errstr = "FIFO overflow";
    742 				break;
    743 			case VR_RXSTAT_GIANT:
    744 				errstr = "received giant packet";
    745 				break;
    746 			case VR_RXSTAT_RUNT:
    747 				errstr = "received runt packet";
    748 				break;
    749 			case VR_RXSTAT_BUSERR:
    750 				errstr = "system bus error";
    751 				break;
    752 			case VR_RXSTAT_BUFFERR:
    753 				errstr = "rx buffer error";
    754 				break;
    755 			default:
    756 				errstr = "unknown rx error";
    757 				break;
    758 			}
    759 			printf("%s: receive error: %s\n", sc->vr_dev.dv_xname,
    760 			    errstr);
    761 
    762 			VR_INIT_RXDESC(sc, i);
    763 
    764 			continue;
    765 		}
    766 
    767 		bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
    768 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
    769 
    770 		/* No errors; receive the packet. */
    771 		total_len = VR_RXBYTES(d->vr_status);
    772 
    773 		/*
    774 		 * XXX The VIA Rhine chip includes the CRC with every
    775 		 * received frame, and there's no way to turn this
    776 		 * behavior off (at least, I can't find anything in
    777 		 * the manual that explains how to do it) so we have
    778 		 * to trim off the CRC manually.
    779 		 */
    780 		total_len -= ETHER_CRC_LEN;
    781 
    782 #ifdef __NO_STRICT_ALIGNMENT
    783 		/*
    784 		 * Try to conjure up a new mbuf cluster. If that
    785 		 * fails, it means we have an out of memory condition and
    786 		 * should leave the buffer in place and continue. This will
    787 		 * result in a lost packet, but there's little else we
    788 		 * can do in this situation.
    789 		 */
    790 		m = ds->ds_mbuf;
    791 		if (vr_add_rxbuf(sc, i) == ENOBUFS) {
    792 			ifp->if_ierrors++;
    793 			VR_INIT_RXDESC(sc, i);
    794 			bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
    795 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    796 			continue;
    797 		}
    798 #else
    799 		/*
    800 		 * The Rhine's packet buffers must be 4-byte aligned.
    801 		 * But this means that the data after the Ethernet header
    802 		 * is misaligned.  We must allocate a new buffer and
    803 		 * copy the data, shifted forward 2 bytes.
    804 		 */
    805 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    806 		if (m == NULL) {
    807  dropit:
    808 			ifp->if_ierrors++;
    809 			VR_INIT_RXDESC(sc, i);
    810 			bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
    811 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    812 			continue;
    813 		}
    814 		if (total_len > (MHLEN - 2)) {
    815 			MCLGET(m, M_DONTWAIT);
    816 			if ((m->m_flags & M_EXT) == 0)
    817 				goto dropit;
    818 		}
    819 		m->m_data += 2;
    820 
    821 		/*
    822 		 * Note that we use clusters for incoming frames, so the
    823 		 * buffer is virtually contiguous.
    824 		 */
    825 		memcpy(mtod(m, caddr_t), mtod(ds->ds_mbuf, caddr_t),
    826 		    total_len);
    827 
    828 		/* Allow the recieve descriptor to continue using its mbuf. */
    829 		VR_INIT_RXDESC(sc, i);
    830 		bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
    831 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    832 #endif /* __NO_STRICT_ALIGNMENT */
    833 
    834 		ifp->if_ipackets++;
    835 		eh = mtod(m, struct ether_header *);
    836 		m->m_pkthdr.rcvif = ifp;
    837 		m->m_pkthdr.len = m->m_len = total_len;
    838 #if NBPFILTER > 0
    839 		/*
    840 		 * Handle BPF listeners. Let the BPF user see the packet, but
    841 		 * don't pass it up to the ether_input() layer unless it's
    842 		 * a broadcast packet, multicast packet, matches our ethernet
    843 		 * address or the interface is in promiscuous mode.
    844 		 */
    845 		if (ifp->if_bpf) {
    846 			bpf_mtap(ifp->if_bpf, m);
    847 			if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    848 			    (rxstat & (VR_RXSTAT_RX_PHYS | VR_RXSTAT_RX_BROAD |
    849 				       VR_RXSTAT_RX_MULTI)) == 0) {
    850 				m_freem(m);
    851 				continue;
    852 			}
    853 		}
    854 #endif
    855 		/* Remove header from mbuf and pass it on. */
    856 		m_adj(m, sizeof(struct ether_header));
    857 		ether_input(ifp, eh, m);
    858 	}
    859 
    860 	/* Update the receive pointer. */
    861 	sc->vr_rxptr = i;
    862 }
    863 
    864 void
    865 vr_rxeoc(sc)
    866 	struct vr_softc *sc;
    867 {
    868 
    869 	vr_rxeof(sc);
    870 	VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
    871 	CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
    872 	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
    873 	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_GO);
    874 }
    875 
    876 /*
    877  * A frame was downloaded to the chip. It's safe for us to clean up
    878  * the list buffers.
    879  */
    880 static void
    881 vr_txeof(sc)
    882 	struct vr_softc *sc;
    883 {
    884 	struct ifnet *ifp = &sc->vr_ec.ec_if;
    885 	struct vr_desc *d;
    886 	struct vr_descsoft *ds;
    887 	u_int32_t txstat;
    888 	int i;
    889 
    890 	ifp->if_flags &= ~IFF_OACTIVE;
    891 
    892 	/*
    893 	 * Go through our tx list and free mbufs for those
    894 	 * frames that have been transmitted.
    895 	 */
    896 	for (i = sc->vr_txdirty; sc->vr_txpending != 0;
    897 	     i = VR_NEXTTX(i), sc->vr_txpending--) {
    898 		d = VR_CDTX(sc, i);
    899 		ds = VR_DSTX(sc, i);
    900 
    901 		VR_CDTXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    902 
    903 		txstat = d->vr_status;
    904 		if (txstat & VR_TXSTAT_OWN)
    905 			break;
    906 
    907 		bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap,
    908 		    0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    909 		bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
    910 		m_freem(ds->ds_mbuf);
    911 		ds->ds_mbuf = NULL;
    912 
    913 		if (txstat & VR_TXSTAT_ERRSUM) {
    914 			ifp->if_oerrors++;
    915 			if (txstat & VR_TXSTAT_DEFER)
    916 				ifp->if_collisions++;
    917 			if (txstat & VR_TXSTAT_LATECOLL)
    918 				ifp->if_collisions++;
    919 		}
    920 
    921 		ifp->if_collisions += (txstat & VR_TXSTAT_COLLCNT) >> 3;
    922 		ifp->if_opackets++;
    923 	}
    924 
    925 	/* Update the dirty transmit buffer pointer. */
    926 	sc->vr_txdirty = i;
    927 
    928 	/*
    929 	 * Cancel the watchdog timer if there are no pending
    930 	 * transmissions.
    931 	 */
    932 	if (sc->vr_txpending == 0)
    933 		ifp->if_timer = 0;
    934 }
    935 
    936 static int
    937 vr_intr(arg)
    938 	void *arg;
    939 {
    940 	struct vr_softc *sc;
    941 	struct ifnet *ifp;
    942 	u_int16_t status;
    943 	int handled = 0, dotx = 0;
    944 
    945 	sc = arg;
    946 	ifp = &sc->vr_ec.ec_if;
    947 
    948 	/* Suppress unwanted interrupts. */
    949 	if ((ifp->if_flags & IFF_UP) == 0) {
    950 		vr_stop(sc);
    951 		return (0);
    952 	}
    953 
    954 	/* Disable interrupts. */
    955 	CSR_WRITE_2(sc, VR_IMR, 0x0000);
    956 
    957 	for (;;) {
    958 		status = CSR_READ_2(sc, VR_ISR);
    959 		if (status)
    960 			CSR_WRITE_2(sc, VR_ISR, status);
    961 
    962 		if ((status & VR_INTRS) == 0)
    963 			break;
    964 
    965 		handled = 1;
    966 
    967 		if (status & VR_ISR_RX_OK)
    968 			vr_rxeof(sc);
    969 
    970 		if (status &
    971 		    (VR_ISR_RX_ERR | VR_ISR_RX_NOBUF | VR_ISR_RX_OFLOW |
    972 		     VR_ISR_RX_DROPPED))
    973 			vr_rxeoc(sc);
    974 
    975 		if (status & VR_ISR_TX_OK) {
    976 			dotx = 1;
    977 			vr_txeof(sc);
    978 		}
    979 
    980 		if (status & (VR_ISR_TX_UNDERRUN | VR_ISR_TX_ABRT)) {
    981 			if (status & VR_ISR_TX_UNDERRUN)
    982 				printf("%s: transmit underrun\n",
    983 				    sc->vr_dev.dv_xname);
    984 			if (status & VR_ISR_TX_ABRT)
    985 				printf("%s: transmit aborted\n",
    986 				    sc->vr_dev.dv_xname);
    987 			ifp->if_oerrors++;
    988 			dotx = 1;
    989 			vr_txeof(sc);
    990 			if (sc->vr_txpending) {
    991 				VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON);
    992 				VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO);
    993 			}
    994 		}
    995 
    996 		if (status & VR_ISR_BUSERR) {
    997 			printf("%s: PCI bus error\n", sc->vr_dev.dv_xname);
    998 			/* vr_init() calls vr_start() */
    999 			dotx = 0;
   1000 			vr_init(sc);
   1001 		}
   1002 	}
   1003 
   1004 	/* Re-enable interrupts. */
   1005 	CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
   1006 
   1007 	if (dotx)
   1008 		vr_start(ifp);
   1009 
   1010 	return (handled);
   1011 }
   1012 
   1013 /*
   1014  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
   1015  * to the mbuf data regions directly in the transmit lists. We also save a
   1016  * copy of the pointers since the transmit list fragment pointers are
   1017  * physical addresses.
   1018  */
   1019 static void
   1020 vr_start(ifp)
   1021 	struct ifnet *ifp;
   1022 {
   1023 	struct vr_softc *sc = ifp->if_softc;
   1024 	struct mbuf *m0, *m;
   1025 	struct vr_desc *d;
   1026 	struct vr_descsoft *ds;
   1027 	int error, firsttx, nexttx, opending;
   1028 
   1029 	/*
   1030 	 * Remember the previous txpending and the first transmit
   1031 	 * descriptor we use.
   1032 	 */
   1033 	opending = sc->vr_txpending;
   1034 	firsttx = VR_NEXTTX(sc->vr_txlast);
   1035 
   1036 	/*
   1037 	 * Loop through the send queue, setting up transmit descriptors
   1038 	 * until we drain the queue, or use up all available transmit
   1039 	 * descriptors.
   1040 	 */
   1041 	while (sc->vr_txpending < VR_NTXDESC) {
   1042 		/*
   1043 		 * Grab a packet off the queue.
   1044 		 */
   1045 		IF_DEQUEUE(&ifp->if_snd, m0);
   1046 		if (m0 == NULL)
   1047 			break;
   1048 
   1049 		/*
   1050 		 * Get the next available transmit descriptor.
   1051 		 */
   1052 		nexttx = VR_NEXTTX(sc->vr_txlast);
   1053 		d = VR_CDTX(sc, nexttx);
   1054 		ds = VR_DSTX(sc, nexttx);
   1055 
   1056 		/*
   1057 		 * Load the DMA map.  If this fails, the packet didn't
   1058 		 * fit in one DMA segment, and we need to copy.  Note,
   1059 		 * the packet must also be aligned.
   1060 		 */
   1061 		if ((mtod(m0, bus_addr_t) & 3) != 0 ||
   1062 		    bus_dmamap_load_mbuf(sc->vr_dmat, ds->ds_dmamap, m0,
   1063 		     BUS_DMA_NOWAIT) != 0) {
   1064 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1065 			if (m == NULL) {
   1066 				printf("%s: unable to allocate Tx mbuf\n",
   1067 				    sc->vr_dev.dv_xname);
   1068 				IF_PREPEND(&ifp->if_snd, m0);
   1069 				break;
   1070 			}
   1071 			if (m0->m_pkthdr.len > MHLEN) {
   1072 				MCLGET(m, M_DONTWAIT);
   1073 				if ((m->m_flags & M_EXT) == 0) {
   1074 					printf("%s: unable to allocate Tx "
   1075 					    "cluster\n", sc->vr_dev.dv_xname);
   1076 					m_freem(m);
   1077 					IF_PREPEND(&ifp->if_snd, m0);
   1078 					break;
   1079 				}
   1080 			}
   1081 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
   1082 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
   1083 			m_freem(m0);
   1084 			m0 = m;
   1085 			error = bus_dmamap_load_mbuf(sc->vr_dmat,
   1086 			    ds->ds_dmamap, m0, BUS_DMA_NOWAIT);
   1087 			if (error) {
   1088 				printf("%s: unable to load Tx buffer, "
   1089 				    "error = %d\n", sc->vr_dev.dv_xname, error);
   1090 				IF_PREPEND(&ifp->if_snd, m0);
   1091 				break;
   1092 			}
   1093 		}
   1094 
   1095 		/* Sync the DMA map. */
   1096 		bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
   1097 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1098 
   1099 		/*
   1100 		 * Store a pointer to the packet so we can free it later.
   1101 		 */
   1102 		ds->ds_mbuf = m0;
   1103 
   1104 #if NBPFILTER > 0
   1105 		/*
   1106 		 * If there's a BPF listener, bounce a copy of this frame
   1107 		 * to him.
   1108 		 */
   1109 		if (ifp->if_bpf)
   1110 			bpf_mtap(ifp->if_bpf, m0);
   1111 #endif
   1112 
   1113 		/*
   1114 		 * Fill in the transmit descriptor.  The Rhine
   1115 		 * doesn't auto-pad, so we have to do this ourselves.
   1116 		 */
   1117 		d->vr_data = ds->ds_dmamap->dm_segs[0].ds_addr;
   1118 		d->vr_ctl = m0->m_pkthdr.len < VR_MIN_FRAMELEN ?
   1119 		    VR_MIN_FRAMELEN : m0->m_pkthdr.len;
   1120 		d->vr_ctl |=
   1121 		    VR_TXCTL_TLINK|VR_TXCTL_FIRSTFRAG|VR_TXCTL_LASTFRAG;
   1122 
   1123 		/*
   1124 		 * If this is the first descriptor we're enqueuing,
   1125 		 * don't give it to the Rhine yet.  That could cause
   1126 		 * a race condition.  We'll do it below.
   1127 		 */
   1128 		if (nexttx == firsttx)
   1129 			d->vr_status = 0;
   1130 		else
   1131 			d->vr_status = VR_TXSTAT_OWN;
   1132 
   1133 		VR_CDTXSYNC(sc, nexttx,
   1134 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1135 
   1136 		/* Advance the tx pointer. */
   1137 		sc->vr_txpending++;
   1138 		sc->vr_txlast = nexttx;
   1139 	}
   1140 
   1141 	if (sc->vr_txpending == VR_NTXDESC) {
   1142 		/* No more slots left; notify upper layer. */
   1143 		ifp->if_flags |= IFF_OACTIVE;
   1144 	}
   1145 
   1146 	if (sc->vr_txpending != opending) {
   1147 		/*
   1148 		 * We enqueued packets.  If the transmitter was idle,
   1149 		 * reset the txdirty pointer.
   1150 		 */
   1151 		if (opending == 0)
   1152 			sc->vr_txdirty = firsttx;
   1153 
   1154 		/*
   1155 		 * Cause a transmit interrupt to happen on the
   1156 		 * last packet we enqueued.
   1157 		 */
   1158 		VR_CDTX(sc, sc->vr_txlast)->vr_ctl |= VR_TXCTL_FINT;
   1159 		VR_CDTXSYNC(sc, sc->vr_txlast,
   1160 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1161 
   1162 		/*
   1163 		 * The entire packet chain is set up.  Give the
   1164 		 * first descriptor to the Rhine now.
   1165 		 */
   1166 		VR_CDTX(sc, firsttx)->vr_status = VR_TXSTAT_OWN;
   1167 		VR_CDTXSYNC(sc, firsttx,
   1168 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1169 
   1170 		/* Start the transmitter. */
   1171 		VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_TX_GO);
   1172 
   1173 		/* Set the watchdog timer in case the chip flakes out. */
   1174 		ifp->if_timer = 5;
   1175 	}
   1176 }
   1177 
   1178 /*
   1179  * Initialize the interface.  Must be called at splnet.
   1180  */
   1181 static void
   1182 vr_init(xsc)
   1183 	void *xsc;
   1184 {
   1185 	struct vr_softc *sc = xsc;
   1186 	struct ifnet *ifp = &sc->vr_ec.ec_if;
   1187 	struct vr_desc *d;
   1188 	int i;
   1189 
   1190 	/* Cancel pending I/O. */
   1191 	vr_stop(sc);
   1192 
   1193 	/* Reset the Rhine to a known state. */
   1194 	vr_reset(sc);
   1195 
   1196 	VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_THRESH);
   1197 	VR_SETBIT(sc, VR_RXCFG, VR_RXTHRESH_STORENFWD);
   1198 
   1199 	VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TX_THRESH);
   1200 	VR_SETBIT(sc, VR_TXCFG, VR_TXTHRESH_STORENFWD);
   1201 
   1202 	/*
   1203 	 * Initialize the transmit desciptor ring.  txlast is initialized
   1204 	 * to the end of the list so that it will wrap around to the first
   1205 	 * descriptor when the first packet is transmitted.
   1206 	 */
   1207 	for (i = 0; i < VR_NTXDESC; i++) {
   1208 		d = VR_CDTX(sc, i);
   1209 		memset(d, 0, sizeof(struct vr_desc));
   1210 		d->vr_next = VR_CDTXADDR(sc, VR_NEXTTX(i));
   1211 		VR_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1212 	}
   1213 	sc->vr_txpending = 0;
   1214 	sc->vr_txdirty = 0;
   1215 	sc->vr_txlast = VR_NTXDESC - 1;
   1216 
   1217 	/*
   1218 	 * Initialize the receive descriptor ring.  The buffers are
   1219 	 * already allocated.
   1220 	 */
   1221 	for (i = 0; i < VR_NRXDESC; i++)
   1222 		VR_INIT_RXDESC(sc, i);
   1223 	sc->vr_rxptr = 0;
   1224 
   1225 	/* If we want promiscuous mode, set the allframes bit. */
   1226 	if (ifp->if_flags & IFF_PROMISC)
   1227 		VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
   1228 	else
   1229 		VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
   1230 
   1231 	/* Set capture broadcast bit to capture broadcast frames. */
   1232 	if (ifp->if_flags & IFF_BROADCAST)
   1233 		VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
   1234 	else
   1235 		VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
   1236 
   1237 	/* Program the multicast filter, if necessary. */
   1238 	vr_setmulti(sc);
   1239 
   1240 	/* Give the transmit and recieve rings to the Rhine. */
   1241 	CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
   1242 	CSR_WRITE_4(sc, VR_TXADDR, VR_CDTXADDR(sc, VR_NEXTTX(sc->vr_txlast)));
   1243 
   1244 	/* Set current media. */
   1245 	mii_mediachg(&sc->vr_mii);
   1246 
   1247 	/* Enable receiver and transmitter. */
   1248 	CSR_WRITE_2(sc, VR_COMMAND, VR_CMD_TX_NOPOLL|VR_CMD_START|
   1249 				    VR_CMD_TX_ON|VR_CMD_RX_ON|
   1250 				    VR_CMD_RX_GO);
   1251 
   1252 	/* Enable interrupts. */
   1253 	CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
   1254 	CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
   1255 
   1256 	ifp->if_flags |= IFF_RUNNING;
   1257 	ifp->if_flags &= ~IFF_OACTIVE;
   1258 
   1259 	/* Start one second timer. */
   1260 	timeout(vr_tick, sc, hz);
   1261 
   1262 	/* Attempt to start output on the interface. */
   1263 	vr_start(ifp);
   1264 }
   1265 
   1266 /*
   1267  * Set media options.
   1268  */
   1269 static int
   1270 vr_ifmedia_upd(ifp)
   1271 	struct ifnet *ifp;
   1272 {
   1273 	struct vr_softc *sc = ifp->if_softc;
   1274 
   1275 	if (ifp->if_flags & IFF_UP)
   1276 		mii_mediachg(&sc->vr_mii);
   1277 	return (0);
   1278 }
   1279 
   1280 /*
   1281  * Report current media status.
   1282  */
   1283 static void
   1284 vr_ifmedia_sts(ifp, ifmr)
   1285 	struct ifnet *ifp;
   1286 	struct ifmediareq *ifmr;
   1287 {
   1288 	struct vr_softc *sc = ifp->if_softc;
   1289 
   1290 	mii_pollstat(&sc->vr_mii);
   1291 	ifmr->ifm_status = sc->vr_mii.mii_media_status;
   1292 	ifmr->ifm_active = sc->vr_mii.mii_media_active;
   1293 }
   1294 
   1295 static int
   1296 vr_ioctl(ifp, command, data)
   1297 	struct ifnet *ifp;
   1298 	u_long command;
   1299 	caddr_t data;
   1300 {
   1301 	struct vr_softc *sc = ifp->if_softc;
   1302 	struct ifreq *ifr = (struct ifreq *)data;
   1303 	struct ifaddr *ifa = (struct ifaddr *)data;
   1304 	int s, error = 0;
   1305 
   1306 	s = splnet();
   1307 
   1308 	switch (command) {
   1309 	case SIOCSIFADDR:
   1310 		ifp->if_flags |= IFF_UP;
   1311 
   1312 		switch (ifa->ifa_addr->sa_family) {
   1313 #ifdef INET
   1314 		case AF_INET:
   1315 			vr_init(sc);
   1316 			arp_ifinit(ifp, ifa);
   1317 			break;
   1318 #endif /* INET */
   1319 		default:
   1320 			vr_init(sc);
   1321 			break;
   1322 		}
   1323 		break;
   1324 
   1325 	case SIOCGIFADDR:
   1326 		bcopy((caddr_t) sc->vr_enaddr,
   1327 			(caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
   1328 			ETHER_ADDR_LEN);
   1329 		break;
   1330 
   1331 	case SIOCSIFMTU:
   1332 		if (ifr->ifr_mtu > ETHERMTU)
   1333 			error = EINVAL;
   1334 		else
   1335 			ifp->if_mtu = ifr->ifr_mtu;
   1336 		break;
   1337 
   1338 	case SIOCSIFFLAGS:
   1339 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1340 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1341 			/*
   1342 			 * If interface is marked down and it is running, then
   1343 			 * stop it.
   1344 			 */
   1345 			vr_stop(sc);
   1346 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1347 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1348 			/*
   1349 			 * If interface is marked up and it is stopped, then
   1350 			 * start it.
   1351 			 */
   1352 			vr_init(sc);
   1353 		} else if ((ifp->if_flags & IFF_UP) != 0) {
   1354 			/*
   1355 			 * Reset the interface to pick up changes in any other
   1356 			 * flags that affect the hardware state.
   1357 			 */
   1358 			vr_init(sc);
   1359 		}
   1360 		break;
   1361 
   1362 	case SIOCADDMULTI:
   1363 	case SIOCDELMULTI:
   1364 		if (command == SIOCADDMULTI)
   1365 			error = ether_addmulti(ifr, &sc->vr_ec);
   1366 		else
   1367 			error = ether_delmulti(ifr, &sc->vr_ec);
   1368 
   1369 		if (error == ENETRESET) {
   1370 			/*
   1371 			 * Multicast list has changed; set the hardware filter
   1372 			 * accordingly.
   1373 			 */
   1374 			vr_setmulti(sc);
   1375 			error = 0;
   1376 		}
   1377 		break;
   1378 
   1379 	case SIOCGIFMEDIA:
   1380 	case SIOCSIFMEDIA:
   1381 		error = ifmedia_ioctl(ifp, ifr, &sc->vr_mii.mii_media, command);
   1382 		break;
   1383 
   1384 	default:
   1385 		error = EINVAL;
   1386 		break;
   1387 	}
   1388 
   1389 	splx(s);
   1390 	return (error);
   1391 }
   1392 
   1393 static void
   1394 vr_watchdog(ifp)
   1395 	struct ifnet *ifp;
   1396 {
   1397 	struct vr_softc *sc = ifp->if_softc;
   1398 
   1399 	printf("%s: device timeout\n", sc->vr_dev.dv_xname);
   1400 	ifp->if_oerrors++;
   1401 
   1402 	vr_init(sc);
   1403 }
   1404 
   1405 /*
   1406  * One second timer, used to tick MII.
   1407  */
   1408 static void
   1409 vr_tick(arg)
   1410 	void *arg;
   1411 {
   1412 	struct vr_softc *sc = arg;
   1413 	int s;
   1414 
   1415 	s = splnet();
   1416 	mii_tick(&sc->vr_mii);
   1417 	splx(s);
   1418 
   1419 	timeout(vr_tick, sc, hz);
   1420 }
   1421 
   1422 /*
   1423  * Stop the adapter and free any mbufs allocated to the
   1424  * transmit lists.
   1425  */
   1426 static void
   1427 vr_stop(sc)
   1428 	struct vr_softc *sc;
   1429 {
   1430 	struct vr_descsoft *ds;
   1431 	struct ifnet *ifp;
   1432 	int i;
   1433 
   1434 	/* Cancel one second timer. */
   1435 	untimeout(vr_tick, sc);
   1436 
   1437 	ifp = &sc->vr_ec.ec_if;
   1438 	ifp->if_timer = 0;
   1439 
   1440 	VR_SETBIT16(sc, VR_COMMAND, VR_CMD_STOP);
   1441 	VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_RX_ON|VR_CMD_TX_ON));
   1442 	CSR_WRITE_2(sc, VR_IMR, 0x0000);
   1443 	CSR_WRITE_4(sc, VR_TXADDR, 0x00000000);
   1444 	CSR_WRITE_4(sc, VR_RXADDR, 0x00000000);
   1445 
   1446 	/*
   1447 	 * Release any queued transmit buffers.
   1448 	 */
   1449 	for (i = 0; i < VR_NTXDESC; i++) {
   1450 		ds = VR_DSTX(sc, i);
   1451 		if (ds->ds_mbuf != NULL) {
   1452 			bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
   1453 			m_freem(ds->ds_mbuf);
   1454 			ds->ds_mbuf = NULL;
   1455 		}
   1456 	}
   1457 
   1458 	/*
   1459 	 * Mark the interface down and cancel the watchdog timer.
   1460 	 */
   1461 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1462 	ifp->if_timer = 0;
   1463 }
   1464 
   1465 static struct vr_type *vr_lookup __P((struct pci_attach_args *));
   1466 static int vr_probe __P((struct device *, struct cfdata *, void *));
   1467 static void vr_attach __P((struct device *, struct device *, void *));
   1468 static void vr_shutdown __P((void *));
   1469 
   1470 struct cfattach vr_ca = {
   1471 	sizeof (struct vr_softc), vr_probe, vr_attach
   1472 };
   1473 
   1474 static struct vr_type *
   1475 vr_lookup(pa)
   1476 	struct pci_attach_args *pa;
   1477 {
   1478 	struct vr_type *vrt;
   1479 
   1480 	for (vrt = vr_devs; vrt->vr_name != NULL; vrt++) {
   1481 		if (PCI_VENDOR(pa->pa_id) == vrt->vr_vid &&
   1482 		    PCI_PRODUCT(pa->pa_id) == vrt->vr_did)
   1483 			return (vrt);
   1484 	}
   1485 	return (NULL);
   1486 }
   1487 
   1488 static int
   1489 vr_probe(parent, match, aux)
   1490 	struct device *parent;
   1491 	struct cfdata *match;
   1492 	void *aux;
   1493 {
   1494 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
   1495 
   1496 	if (vr_lookup(pa) != NULL)
   1497 		return (1);
   1498 
   1499 	return (0);
   1500 }
   1501 
   1502 /*
   1503  * Stop all chip I/O so that the kernel's probe routines don't
   1504  * get confused by errant DMAs when rebooting.
   1505  */
   1506 static void
   1507 vr_shutdown(arg)
   1508 	void *arg;
   1509 {
   1510 	struct vr_softc *sc = (struct vr_softc *)arg;
   1511 
   1512 	vr_stop(sc);
   1513 }
   1514 
   1515 /*
   1516  * Attach the interface. Allocate softc structures, do ifmedia
   1517  * setup and ethernet/BPF attach.
   1518  */
   1519 static void
   1520 vr_attach(parent, self, aux)
   1521 	struct device *parent;
   1522 	struct device *self;
   1523 	void *aux;
   1524 {
   1525 	struct vr_softc *sc = (struct vr_softc *) self;
   1526 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
   1527 	bus_dma_segment_t seg;
   1528 	struct vr_type *vrt;
   1529 	u_int32_t command;
   1530 	struct ifnet *ifp;
   1531 	u_char eaddr[ETHER_ADDR_LEN];
   1532 	int i, rseg, error;
   1533 
   1534 #define	PCI_CONF_WRITE(r, v)	pci_conf_write(pa->pa_pc, pa->pa_tag, (r), (v))
   1535 #define	PCI_CONF_READ(r)	pci_conf_read(pa->pa_pc, pa->pa_tag, (r))
   1536 
   1537 	vrt = vr_lookup(pa);
   1538 	if (vrt == NULL) {
   1539 		printf("\n");
   1540 		panic("vr_attach: impossible");
   1541 	}
   1542 
   1543 	printf(": %s Ethernet\n", vrt->vr_name);
   1544 
   1545 	/*
   1546 	 * Handle power management nonsense.
   1547 	 */
   1548 
   1549 	command = PCI_CONF_READ(VR_PCI_CAPID) & 0x000000FF;
   1550 	if (command == 0x01) {
   1551 		command = PCI_CONF_READ(VR_PCI_PWRMGMTCTRL);
   1552 		if (command & VR_PSTATE_MASK) {
   1553 			u_int32_t iobase, membase, irq;
   1554 
   1555 			/* Save important PCI config data. */
   1556 			iobase = PCI_CONF_READ(VR_PCI_LOIO);
   1557 			membase = PCI_CONF_READ(VR_PCI_LOMEM);
   1558 			irq = PCI_CONF_READ(VR_PCI_INTLINE);
   1559 
   1560 			/* Reset the power state. */
   1561 			printf("%s: chip is in D%d power mode "
   1562 				"-- setting to D0\n",
   1563 				sc->vr_dev.dv_xname, command & VR_PSTATE_MASK);
   1564 			command &= 0xFFFFFFFC;
   1565 			PCI_CONF_WRITE(VR_PCI_PWRMGMTCTRL, command);
   1566 
   1567 			/* Restore PCI config data. */
   1568 			PCI_CONF_WRITE(VR_PCI_LOIO, iobase);
   1569 			PCI_CONF_WRITE(VR_PCI_LOMEM, membase);
   1570 			PCI_CONF_WRITE(VR_PCI_INTLINE, irq);
   1571 		}
   1572 	}
   1573 
   1574 	/* Make sure bus mastering is enabled. */
   1575 	command = PCI_CONF_READ(PCI_COMMAND_STATUS_REG);
   1576 	command |= PCI_COMMAND_MASTER_ENABLE;
   1577 	PCI_CONF_WRITE(PCI_COMMAND_STATUS_REG, command);
   1578 
   1579 	/*
   1580 	 * Map control/status registers.
   1581 	 */
   1582 	{
   1583 		bus_space_tag_t iot, memt;
   1584 		bus_space_handle_t ioh, memh;
   1585 		int ioh_valid, memh_valid;
   1586 		pci_intr_handle_t intrhandle;
   1587 		const char *intrstr;
   1588 
   1589 		ioh_valid = (pci_mapreg_map(pa, VR_PCI_LOIO,
   1590 			PCI_MAPREG_TYPE_IO, 0,
   1591 			&iot, &ioh, NULL, NULL) == 0);
   1592 		memh_valid = (pci_mapreg_map(pa, VR_PCI_LOMEM,
   1593 			PCI_MAPREG_TYPE_MEM |
   1594 			PCI_MAPREG_MEM_TYPE_32BIT,
   1595 			0, &memt, &memh, NULL, NULL) == 0);
   1596 #if defined(VR_USEIOSPACE)
   1597 		if (ioh_valid) {
   1598 			sc->vr_bst = iot;
   1599 			sc->vr_bsh = ioh;
   1600 		} else if (memh_valid) {
   1601 			sc->vr_bst = memt;
   1602 			sc->vr_bsh = memh;
   1603 		}
   1604 #else
   1605 		if (memh_valid) {
   1606 			sc->vr_bst = memt;
   1607 			sc->vr_bsh = memh;
   1608 		} else if (ioh_valid) {
   1609 			sc->vr_bst = iot;
   1610 			sc->vr_bsh = ioh;
   1611 		}
   1612 #endif
   1613 		else {
   1614 			printf(": unable to map device registers\n");
   1615 			return;
   1616 		}
   1617 
   1618 		/* Allocate interrupt */
   1619 		if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
   1620 				pa->pa_intrline, &intrhandle)) {
   1621 			printf("%s: couldn't map interrupt\n",
   1622 				sc->vr_dev.dv_xname);
   1623 			return;
   1624 		}
   1625 		intrstr = pci_intr_string(pa->pa_pc, intrhandle);
   1626 		sc->vr_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
   1627 						vr_intr, sc);
   1628 		if (sc->vr_ih == NULL) {
   1629 			printf("%s: couldn't establish interrupt",
   1630 				sc->vr_dev.dv_xname);
   1631 			if (intrstr != NULL)
   1632 				printf(" at %s", intrstr);
   1633 			printf("\n");
   1634 		}
   1635 		printf("%s: interrupting at %s\n",
   1636 			sc->vr_dev.dv_xname, intrstr);
   1637 	}
   1638 
   1639 	/* Reset the adapter. */
   1640 	vr_reset(sc);
   1641 
   1642 	/*
   1643 	 * Get station address. The way the Rhine chips work,
   1644 	 * you're not allowed to directly access the EEPROM once
   1645 	 * they've been programmed a special way. Consequently,
   1646 	 * we need to read the node address from the PAR0 and PAR1
   1647 	 * registers.
   1648 	 */
   1649 	VR_SETBIT(sc, VR_EECSR, VR_EECSR_LOAD);
   1650 	DELAY(200);
   1651 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1652 		eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i);
   1653 
   1654 	/*
   1655 	 * A Rhine chip was detected. Inform the world.
   1656 	 */
   1657 	printf("%s: Ethernet address: %s\n",
   1658 		sc->vr_dev.dv_xname, ether_sprintf(eaddr));
   1659 
   1660 	bcopy(eaddr, sc->vr_enaddr, ETHER_ADDR_LEN);
   1661 
   1662 	sc->vr_dmat = pa->pa_dmat;
   1663 
   1664 	/*
   1665 	 * Allocate the control data structures, and create and load
   1666 	 * the DMA map for it.
   1667 	 */
   1668 	if ((error = bus_dmamem_alloc(sc->vr_dmat,
   1669 	    sizeof(struct vr_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
   1670 	    0)) != 0) {
   1671 		printf("%s: unable to allocate control data, error = %d\n",
   1672 		    sc->vr_dev.dv_xname, error);
   1673 		goto fail_0;
   1674 	}
   1675 
   1676 	if ((error = bus_dmamem_map(sc->vr_dmat, &seg, rseg,
   1677 	    sizeof(struct vr_control_data), (caddr_t *)&sc->vr_control_data,
   1678 	    BUS_DMA_COHERENT)) != 0) {
   1679 		printf("%s: unable to map control data, error = %d\n",
   1680 		    sc->vr_dev.dv_xname, error);
   1681 		goto fail_1;
   1682 	}
   1683 
   1684 	if ((error = bus_dmamap_create(sc->vr_dmat,
   1685 	    sizeof(struct vr_control_data), 1,
   1686 	    sizeof(struct vr_control_data), 0, 0,
   1687 	    &sc->vr_cddmamap)) != 0) {
   1688 		printf("%s: unable to create control data DMA map, "
   1689 		    "error = %d\n", sc->vr_dev.dv_xname, error);
   1690 		goto fail_2;
   1691 	}
   1692 
   1693 	if ((error = bus_dmamap_load(sc->vr_dmat, sc->vr_cddmamap,
   1694 	    sc->vr_control_data, sizeof(struct vr_control_data), NULL,
   1695 	    0)) != 0) {
   1696 		printf("%s: unable to load control data DMA map, error = %d\n",
   1697 		    sc->vr_dev.dv_xname, error);
   1698 		goto fail_3;
   1699 	}
   1700 
   1701 	/*
   1702 	 * Create the transmit buffer DMA maps.
   1703 	 */
   1704 	for (i = 0; i < VR_NTXDESC; i++) {
   1705 		if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES,
   1706 		    1, MCLBYTES, 0, 0,
   1707 		    &VR_DSTX(sc, i)->ds_dmamap)) != 0) {
   1708 			printf("%s: unable to create tx DMA map %d, "
   1709 			    "error = %d\n", sc->vr_dev.dv_xname, i, error);
   1710 			goto fail_4;
   1711 		}
   1712 	}
   1713 
   1714 	/*
   1715 	 * Create the receive buffer DMA maps.
   1716 	 */
   1717 	for (i = 0; i < VR_NRXDESC; i++) {
   1718 		if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES, 1,
   1719 		    MCLBYTES, 0, 0,
   1720 		    &VR_DSRX(sc, i)->ds_dmamap)) != 0) {
   1721 			printf("%s: unable to create rx DMA map %d, "
   1722 			    "error = %d\n", sc->vr_dev.dv_xname, i, error);
   1723 			goto fail_5;
   1724 		}
   1725 	}
   1726 
   1727 	/*
   1728 	 * Pre-allocate the receive buffers.
   1729 	 */
   1730 	for (i = 0; i < VR_NRXDESC; i++) {
   1731 		if ((error = vr_add_rxbuf(sc, i)) != 0) {
   1732 			printf("%s: unable to allocate or map rx buffer %d, "
   1733 			    "error = %d\n", sc->vr_dev.dv_xname, i, error);
   1734 			goto fail_6;
   1735 		}
   1736 	}
   1737 
   1738 	ifp = &sc->vr_ec.ec_if;
   1739 	ifp->if_softc = sc;
   1740 	ifp->if_mtu = ETHERMTU;
   1741 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1742 	ifp->if_ioctl = vr_ioctl;
   1743 	ifp->if_output = ether_output;
   1744 	ifp->if_start = vr_start;
   1745 	ifp->if_watchdog = vr_watchdog;
   1746 	ifp->if_baudrate = 10000000;
   1747 	bcopy(sc->vr_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
   1748 
   1749 	/*
   1750 	 * Initialize MII/media info.
   1751 	 */
   1752 	sc->vr_mii.mii_ifp = ifp;
   1753 	sc->vr_mii.mii_readreg = vr_mii_readreg;
   1754 	sc->vr_mii.mii_writereg = vr_mii_writereg;
   1755 	sc->vr_mii.mii_statchg = vr_mii_statchg;
   1756 	ifmedia_init(&sc->vr_mii.mii_media, 0, vr_ifmedia_upd, vr_ifmedia_sts);
   1757 	mii_phy_probe(&sc->vr_dev, &sc->vr_mii, 0xffffffff);
   1758 	if (LIST_FIRST(&sc->vr_mii.mii_phys) == NULL) {
   1759 		ifmedia_add(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   1760 		ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE);
   1761 	} else
   1762 		ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_AUTO);
   1763 
   1764 	/*
   1765 	 * Call MI attach routines.
   1766 	 */
   1767 	if_attach(ifp);
   1768 	ether_ifattach(ifp, sc->vr_enaddr);
   1769 
   1770 #if NBPFILTER > 0
   1771 	bpfattach(&sc->vr_ec.ec_if.if_bpf,
   1772 		ifp, DLT_EN10MB, sizeof (struct ether_header));
   1773 #endif
   1774 
   1775 	sc->vr_ats = shutdownhook_establish(vr_shutdown, sc);
   1776 	if (sc->vr_ats == NULL)
   1777 		printf("%s: warning: couldn't establish shutdown hook\n",
   1778 			sc->vr_dev.dv_xname);
   1779 	return;
   1780 
   1781  fail_6:
   1782 	for (i = 0; i < VR_NRXDESC; i++) {
   1783 		if (sc->vr_rxsoft[i].ds_mbuf != NULL) {
   1784 			bus_dmamap_unload(sc->vr_dmat,
   1785 			    sc->vr_rxsoft[i].ds_dmamap);
   1786 			(void) m_freem(sc->vr_rxsoft[i].ds_mbuf);
   1787 		}
   1788 	}
   1789  fail_5:
   1790 	for (i = 0; i < VR_NRXDESC; i++) {
   1791 		if (sc->vr_rxsoft[i].ds_dmamap != NULL)
   1792 			bus_dmamap_destroy(sc->vr_dmat,
   1793 			    sc->vr_rxsoft[i].ds_dmamap);
   1794 	}
   1795  fail_4:
   1796 	for (i = 0; i < VR_NTXDESC; i++) {
   1797 		if (sc->vr_txsoft[i].ds_dmamap != NULL)
   1798 			bus_dmamap_destroy(sc->vr_dmat,
   1799 			    sc->vr_txsoft[i].ds_dmamap);
   1800 	}
   1801 	bus_dmamap_unload(sc->vr_dmat, sc->vr_cddmamap);
   1802  fail_3:
   1803 	bus_dmamap_destroy(sc->vr_dmat, sc->vr_cddmamap);
   1804  fail_2:
   1805 	bus_dmamem_unmap(sc->vr_dmat, (caddr_t)sc->vr_control_data,
   1806 	    sizeof(struct vr_control_data));
   1807  fail_1:
   1808 	bus_dmamem_free(sc->vr_dmat, &seg, rseg);
   1809  fail_0:
   1810 	return;
   1811 }
   1812