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