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