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