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