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