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