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