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