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