Home | History | Annotate | Line # | Download | only in pci
if_vge.c revision 1.30
      1 /* $NetBSD: if_vge.c,v 1.30 2006/11/26 15:40:14 tsutsui Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2004
      5  *	Bill Paul <wpaul (at) windriver.com>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  * FreeBSD: src/sys/dev/vge/if_vge.c,v 1.5 2005/02/07 19:39:29 glebius Exp
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: if_vge.c,v 1.30 2006/11/26 15:40:14 tsutsui Exp $");
     39 
     40 /*
     41  * VIA Networking Technologies VT612x PCI gigabit ethernet NIC driver.
     42  *
     43  * Written by Bill Paul <wpaul (at) windriver.com>
     44  * Senior Networking Software Engineer
     45  * Wind River Systems
     46  */
     47 
     48 /*
     49  * The VIA Networking VT6122 is a 32bit, 33/66 MHz PCI device that
     50  * combines a tri-speed ethernet MAC and PHY, with the following
     51  * features:
     52  *
     53  *	o Jumbo frame support up to 16K
     54  *	o Transmit and receive flow control
     55  *	o IPv4 checksum offload
     56  *	o VLAN tag insertion and stripping
     57  *	o TCP large send
     58  *	o 64-bit multicast hash table filter
     59  *	o 64 entry CAM filter
     60  *	o 16K RX FIFO and 48K TX FIFO memory
     61  *	o Interrupt moderation
     62  *
     63  * The VT6122 supports up to four transmit DMA queues. The descriptors
     64  * in the transmit ring can address up to 7 data fragments; frames which
     65  * span more than 7 data buffers must be coalesced, but in general the
     66  * BSD TCP/IP stack rarely generates frames more than 2 or 3 fragments
     67  * long. The receive descriptors address only a single buffer.
     68  *
     69  * There are two peculiar design issues with the VT6122. One is that
     70  * receive data buffers must be aligned on a 32-bit boundary. This is
     71  * not a problem where the VT6122 is used as a LOM device in x86-based
     72  * systems, but on architectures that generate unaligned access traps, we
     73  * have to do some copying.
     74  *
     75  * The other issue has to do with the way 64-bit addresses are handled.
     76  * The DMA descriptors only allow you to specify 48 bits of addressing
     77  * information. The remaining 16 bits are specified using one of the
     78  * I/O registers. If you only have a 32-bit system, then this isn't
     79  * an issue, but if you have a 64-bit system and more than 4GB of
     80  * memory, you must have to make sure your network data buffers reside
     81  * in the same 48-bit 'segment.'
     82  *
     83  * Special thanks to Ryan Fu at VIA Networking for providing documentation
     84  * and sample NICs for testing.
     85  */
     86 
     87 #include "bpfilter.h"
     88 
     89 #include <sys/param.h>
     90 #include <sys/endian.h>
     91 #include <sys/systm.h>
     92 #include <sys/device.h>
     93 #include <sys/sockio.h>
     94 #include <sys/mbuf.h>
     95 #include <sys/malloc.h>
     96 #include <sys/kernel.h>
     97 #include <sys/socket.h>
     98 
     99 #include <net/if.h>
    100 #include <net/if_arp.h>
    101 #include <net/if_ether.h>
    102 #include <net/if_dl.h>
    103 #include <net/if_media.h>
    104 
    105 #include <net/bpf.h>
    106 
    107 #include <machine/bus.h>
    108 
    109 #include <dev/mii/mii.h>
    110 #include <dev/mii/miivar.h>
    111 
    112 #include <dev/pci/pcireg.h>
    113 #include <dev/pci/pcivar.h>
    114 #include <dev/pci/pcidevs.h>
    115 
    116 #include <dev/pci/if_vgereg.h>
    117 
    118 #define VGE_JUMBO_MTU		9000
    119 
    120 #define VGE_IFQ_MAXLEN		64
    121 
    122 #define VGE_RING_ALIGN		256
    123 
    124 #define VGE_NTXDESC		256
    125 #define VGE_NTXDESC_MASK	(VGE_NTXDESC - 1)
    126 #define VGE_NEXT_TXDESC(x)	((x + 1) & VGE_NTXDESC_MASK)
    127 #define VGE_PREV_TXDESC(x)	((x - 1) & VGE_NTXDESC_MASK)
    128 
    129 #define VGE_NRXDESC		256	/* Must be a multiple of 4!! */
    130 #define VGE_NRXDESC_MASK	(VGE_NRXDESC - 1)
    131 #define VGE_NEXT_RXDESC(x)	((x + 1) & VGE_NRXDESC_MASK)
    132 #define VGE_PREV_RXDESC(x)	((x - 1) & VGE_NRXDESC_MASK)
    133 
    134 #define VGE_ADDR_LO(y)		((uint64_t)(y) & 0xFFFFFFFF)
    135 #define VGE_ADDR_HI(y)		((uint64_t)(y) >> 32)
    136 #define VGE_BUFLEN(y)		((y) & 0x7FFF)
    137 #define ETHER_PAD_LEN		(ETHER_MIN_LEN - ETHER_CRC_LEN)
    138 
    139 #define VGE_POWER_MANAGEMENT	0	/* disabled for now */
    140 
    141 /*
    142  * Mbuf adjust factor to force 32-bit alignment of IP header.
    143  * Drivers should pad ETHER_ALIGN bytes when setting up a
    144  * RX mbuf so the upper layers get the IP header properly aligned
    145  * past the 14-byte Ethernet header.
    146  *
    147  * See also comment in vge_encap().
    148  */
    149 #define ETHER_ALIGN		2
    150 
    151 #ifdef __NO_STRICT_ALIGNMENT
    152 #define VGE_RX_BUFSIZE		MCLBYTES
    153 #else
    154 #define VGE_RX_PAD		sizeof(uint32_t)
    155 #define VGE_RX_BUFSIZE		(MCLBYTES - VGE_RX_PAD)
    156 #endif
    157 
    158 /*
    159  * Control structures are DMA'd to the vge chip. We allocate them in
    160  * a single clump that maps to a single DMA segment to make several things
    161  * easier.
    162  */
    163 struct vge_control_data {
    164 	/* TX descriptors */
    165 	struct vge_txdesc	vcd_txdescs[VGE_NTXDESC];
    166 	/* RX descriptors */
    167 	struct vge_rxdesc	vcd_rxdescs[VGE_NRXDESC];
    168 	/* dummy data for TX padding */
    169 	uint8_t			vcd_pad[ETHER_PAD_LEN];
    170 };
    171 
    172 #define VGE_CDOFF(x)	offsetof(struct vge_control_data, x)
    173 #define VGE_CDTXOFF(x)	VGE_CDOFF(vcd_txdescs[(x)])
    174 #define VGE_CDRXOFF(x)	VGE_CDOFF(vcd_rxdescs[(x)])
    175 #define VGE_CDPADOFF()	VGE_CDOFF(vcd_pad[0])
    176 
    177 /*
    178  * Software state for TX jobs.
    179  */
    180 struct vge_txsoft {
    181 	struct mbuf	*txs_mbuf;		/* head of our mbuf chain */
    182 	bus_dmamap_t	txs_dmamap;		/* our DMA map */
    183 };
    184 
    185 /*
    186  * Software state for RX jobs.
    187  */
    188 struct vge_rxsoft {
    189 	struct mbuf	*rxs_mbuf;		/* head of our mbuf chain */
    190 	bus_dmamap_t	rxs_dmamap;		/* our DMA map */
    191 };
    192 
    193 
    194 struct vge_softc {
    195 	struct device		sc_dev;
    196 
    197 	bus_space_tag_t		sc_bst;		/* bus space tag */
    198 	bus_space_handle_t	sc_bsh;		/* bus space handle */
    199 	bus_dma_tag_t		sc_dmat;
    200 
    201 	struct ethercom		sc_ethercom;	/* interface info */
    202 	uint8_t			sc_eaddr[ETHER_ADDR_LEN];
    203 
    204 	void			*sc_intrhand;
    205 	struct mii_data		sc_mii;
    206 	uint8_t			sc_type;
    207 	int			sc_if_flags;
    208 	int			sc_link;
    209 	int			sc_camidx;
    210 	struct callout		sc_timeout;
    211 
    212 	bus_dmamap_t		sc_cddmamap;
    213 #define sc_cddma		sc_cddmamap->dm_segs[0].ds_addr
    214 
    215 	struct vge_txsoft	sc_txsoft[VGE_NTXDESC];
    216 	struct vge_rxsoft	sc_rxsoft[VGE_NRXDESC];
    217 	struct vge_control_data	*sc_control_data;
    218 #define sc_txdescs		sc_control_data->vcd_txdescs
    219 #define sc_rxdescs		sc_control_data->vcd_rxdescs
    220 
    221 	int			sc_tx_prodidx;
    222 	int			sc_tx_considx;
    223 	int			sc_tx_free;
    224 
    225 	struct mbuf		*sc_rx_mhead;
    226 	struct mbuf		*sc_rx_mtail;
    227 	int			sc_rx_prodidx;
    228 	int			sc_rx_consumed;
    229 
    230 	int			sc_suspended;	/* 0 = normal  1 = suspended */
    231 	uint32_t		sc_saved_maps[5];	/* pci data */
    232 	uint32_t		sc_saved_biosaddr;
    233 	uint8_t			sc_saved_intline;
    234 	uint8_t			sc_saved_cachelnsz;
    235 	uint8_t			sc_saved_lattimer;
    236 };
    237 
    238 #define VGE_CDTXADDR(sc, x)	((sc)->sc_cddma + VGE_CDTXOFF(x))
    239 #define VGE_CDRXADDR(sc, x)	((sc)->sc_cddma + VGE_CDRXOFF(x))
    240 #define VGE_CDPADADDR(sc)	((sc)->sc_cddma + VGE_CDPADOFF())
    241 
    242 #define VGE_TXDESCSYNC(sc, idx, ops)					\
    243 	bus_dmamap_sync((sc)->sc_dmat,(sc)->sc_cddmamap,		\
    244 	    VGE_CDTXOFF(idx),						\
    245 	    offsetof(struct vge_txdesc, td_frag[0]),			\
    246 	    (ops))
    247 #define VGE_TXFRAGSYNC(sc, idx, nsegs, ops)				\
    248 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    249 	    VGE_CDTXOFF(idx) +						\
    250 	    offsetof(struct vge_txdesc, td_frag[0]),			\
    251 	    sizeof(struct vge_txfrag) * (nsegs),			\
    252 	    (ops))
    253 #define VGE_RXDESCSYNC(sc, idx, ops)					\
    254 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    255 	    VGE_CDRXOFF(idx),						\
    256 	    sizeof(struct vge_rxdesc),					\
    257 	    (ops))
    258 
    259 /*
    260  * register space access macros
    261  */
    262 #define CSR_WRITE_4(sc, reg, val)	\
    263 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    264 #define CSR_WRITE_2(sc, reg, val)	\
    265 	bus_space_write_2((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    266 #define CSR_WRITE_1(sc, reg, val)	\
    267 	bus_space_write_1((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    268 
    269 #define CSR_READ_4(sc, reg)		\
    270 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    271 #define CSR_READ_2(sc, reg)		\
    272 	bus_space_read_2((sc)->sc_bst, (sc)->sc_bsh, (reg))
    273 #define CSR_READ_1(sc, reg)		\
    274 	bus_space_read_1((sc)->sc_bst, (sc)->sc_bsh, (reg))
    275 
    276 #define CSR_SETBIT_1(sc, reg, x)	\
    277 	CSR_WRITE_1((sc), (reg), CSR_READ_1((sc), (reg)) | (x))
    278 #define CSR_SETBIT_2(sc, reg, x)	\
    279 	CSR_WRITE_2((sc), (reg), CSR_READ_2((sc), (reg)) | (x))
    280 #define CSR_SETBIT_4(sc, reg, x)	\
    281 	CSR_WRITE_4((sc), (reg), CSR_READ_4((sc), (reg)) | (x))
    282 
    283 #define CSR_CLRBIT_1(sc, reg, x)	\
    284 	CSR_WRITE_1((sc), (reg), CSR_READ_1((sc), (reg)) & ~(x))
    285 #define CSR_CLRBIT_2(sc, reg, x)	\
    286 	CSR_WRITE_2((sc), (reg), CSR_READ_2((sc), (reg)) & ~(x))
    287 #define CSR_CLRBIT_4(sc, reg, x)	\
    288 	CSR_WRITE_4((sc), (reg), CSR_READ_4((sc), (reg)) & ~(x))
    289 
    290 #define VGE_TIMEOUT		10000
    291 
    292 #define VGE_PCI_LOIO             0x10
    293 #define VGE_PCI_LOMEM            0x14
    294 
    295 static inline void vge_set_txaddr(struct vge_txfrag *, bus_addr_t);
    296 static inline void vge_set_rxaddr(struct vge_rxdesc *, bus_addr_t);
    297 
    298 static int vge_probe(struct device *, struct cfdata *, void *);
    299 static void vge_attach(struct device *, struct device *, void *);
    300 
    301 static int vge_encap(struct vge_softc *, struct mbuf *, int);
    302 
    303 static int vge_allocmem(struct vge_softc *);
    304 static int vge_newbuf(struct vge_softc *, int, struct mbuf *);
    305 #ifndef __NO_STRICT_ALIGNMENT
    306 static inline void vge_fixup_rx(struct mbuf *);
    307 #endif
    308 static void vge_rxeof(struct vge_softc *);
    309 static void vge_txeof(struct vge_softc *);
    310 static int vge_intr(void *);
    311 static void vge_tick(void *);
    312 static void vge_start(struct ifnet *);
    313 static int vge_ioctl(struct ifnet *, u_long, caddr_t);
    314 static int vge_init(struct ifnet *);
    315 static void vge_stop(struct vge_softc *);
    316 static void vge_watchdog(struct ifnet *);
    317 #if VGE_POWER_MANAGEMENT
    318 static int vge_suspend(struct device *);
    319 static int vge_resume(struct device *);
    320 #endif
    321 static void vge_shutdown(void *);
    322 static int vge_ifmedia_upd(struct ifnet *);
    323 static void vge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    324 
    325 static uint16_t vge_read_eeprom(struct vge_softc *, int);
    326 
    327 static void vge_miipoll_start(struct vge_softc *);
    328 static void vge_miipoll_stop(struct vge_softc *);
    329 static int vge_miibus_readreg(struct device *, int, int);
    330 static void vge_miibus_writereg(struct device *, int, int, int);
    331 static void vge_miibus_statchg(struct device *);
    332 
    333 static void vge_cam_clear(struct vge_softc *);
    334 static int vge_cam_set(struct vge_softc *, uint8_t *);
    335 static void vge_setmulti(struct vge_softc *);
    336 static void vge_reset(struct vge_softc *);
    337 
    338 CFATTACH_DECL(vge, sizeof(struct vge_softc),
    339     vge_probe, vge_attach, NULL, NULL);
    340 
    341 static inline void
    342 vge_set_txaddr(struct vge_txfrag *f, bus_addr_t daddr)
    343 {
    344 
    345 	f->tf_addrlo = htole32((uint32_t)daddr);
    346 	if (sizeof(bus_addr_t) == sizeof(uint64_t))
    347 		f->tf_addrhi = htole16(((uint64_t)daddr >> 32) & 0xFFFF);
    348 	else
    349 		f->tf_addrhi = 0;
    350 }
    351 
    352 static inline void
    353 vge_set_rxaddr(struct vge_rxdesc *rxd, bus_addr_t daddr)
    354 {
    355 
    356 	rxd->rd_addrlo = htole32((uint32_t)daddr);
    357 	if (sizeof(bus_addr_t) == sizeof(uint64_t))
    358 		rxd->rd_addrhi = htole16(((uint64_t)daddr >> 32) & 0xFFFF);
    359 	else
    360 		rxd->rd_addrhi = 0;
    361 }
    362 
    363 /*
    364  * Defragment mbuf chain contents to be as linear as possible.
    365  * Returns new mbuf chain on success, NULL on failure. Old mbuf
    366  * chain is always freed.
    367  * XXX temporary until there would be generic function doing this.
    368  */
    369 #define m_defrag	vge_m_defrag
    370 struct mbuf * vge_m_defrag(struct mbuf *, int);
    371 
    372 struct mbuf *
    373 vge_m_defrag(struct mbuf *mold, int flags)
    374 {
    375 	struct mbuf *m0, *mn, *n;
    376 	size_t sz = mold->m_pkthdr.len;
    377 
    378 #ifdef DIAGNOSTIC
    379 	if ((mold->m_flags & M_PKTHDR) == 0)
    380 		panic("m_defrag: not a mbuf chain header");
    381 #endif
    382 
    383 	MGETHDR(m0, flags, MT_DATA);
    384 	if (m0 == NULL)
    385 		return NULL;
    386 	m0->m_pkthdr.len = mold->m_pkthdr.len;
    387 	mn = m0;
    388 
    389 	do {
    390 		if (sz > MHLEN) {
    391 			MCLGET(mn, M_DONTWAIT);
    392 			if ((mn->m_flags & M_EXT) == 0) {
    393 				m_freem(m0);
    394 				return NULL;
    395 			}
    396 		}
    397 
    398 		mn->m_len = MIN(sz, MCLBYTES);
    399 
    400 		m_copydata(mold, mold->m_pkthdr.len - sz, mn->m_len,
    401 		     mtod(mn, caddr_t));
    402 
    403 		sz -= mn->m_len;
    404 
    405 		if (sz > 0) {
    406 			/* need more mbufs */
    407 			MGET(n, M_NOWAIT, MT_DATA);
    408 			if (n == NULL) {
    409 				m_freem(m0);
    410 				return NULL;
    411 			}
    412 
    413 			mn->m_next = n;
    414 			mn = n;
    415 		}
    416 	} while (sz > 0);
    417 
    418 	return m0;
    419 }
    420 
    421 /*
    422  * Read a word of data stored in the EEPROM at address 'addr.'
    423  */
    424 static uint16_t
    425 vge_read_eeprom(struct vge_softc *sc, int addr)
    426 {
    427 	int i;
    428 	uint16_t word = 0;
    429 
    430 	/*
    431 	 * Enter EEPROM embedded programming mode. In order to
    432 	 * access the EEPROM at all, we first have to set the
    433 	 * EELOAD bit in the CHIPCFG2 register.
    434 	 */
    435 	CSR_SETBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
    436 	CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
    437 
    438 	/* Select the address of the word we want to read */
    439 	CSR_WRITE_1(sc, VGE_EEADDR, addr);
    440 
    441 	/* Issue read command */
    442 	CSR_SETBIT_1(sc, VGE_EECMD, VGE_EECMD_ERD);
    443 
    444 	/* Wait for the done bit to be set. */
    445 	for (i = 0; i < VGE_TIMEOUT; i++) {
    446 		if (CSR_READ_1(sc, VGE_EECMD) & VGE_EECMD_EDONE)
    447 			break;
    448 	}
    449 
    450 	if (i == VGE_TIMEOUT) {
    451 		aprint_error("%s: EEPROM read timed out\n",
    452 		    sc->sc_dev.dv_xname);
    453 		return 0;
    454 	}
    455 
    456 	/* Read the result */
    457 	word = CSR_READ_2(sc, VGE_EERDDAT);
    458 
    459 	/* Turn off EEPROM access mode. */
    460 	CSR_CLRBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
    461 	CSR_CLRBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
    462 
    463 	return word;
    464 }
    465 
    466 static void
    467 vge_miipoll_stop(struct vge_softc *sc)
    468 {
    469 	int i;
    470 
    471 	CSR_WRITE_1(sc, VGE_MIICMD, 0);
    472 
    473 	for (i = 0; i < VGE_TIMEOUT; i++) {
    474 		DELAY(1);
    475 		if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
    476 			break;
    477 	}
    478 
    479 	if (i == VGE_TIMEOUT) {
    480 		aprint_error("%s: failed to idle MII autopoll\n",
    481 		    sc->sc_dev.dv_xname);
    482 	}
    483 }
    484 
    485 static void
    486 vge_miipoll_start(struct vge_softc *sc)
    487 {
    488 	int i;
    489 
    490 	/* First, make sure we're idle. */
    491 
    492 	CSR_WRITE_1(sc, VGE_MIICMD, 0);
    493 	CSR_WRITE_1(sc, VGE_MIIADDR, VGE_MIIADDR_SWMPL);
    494 
    495 	for (i = 0; i < VGE_TIMEOUT; i++) {
    496 		DELAY(1);
    497 		if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
    498 			break;
    499 	}
    500 
    501 	if (i == VGE_TIMEOUT) {
    502 		aprint_error("%s: failed to idle MII autopoll\n",
    503 		    sc->sc_dev.dv_xname);
    504 		return;
    505 	}
    506 
    507 	/* Now enable auto poll mode. */
    508 
    509 	CSR_WRITE_1(sc, VGE_MIICMD, VGE_MIICMD_MAUTO);
    510 
    511 	/* And make sure it started. */
    512 
    513 	for (i = 0; i < VGE_TIMEOUT; i++) {
    514 		DELAY(1);
    515 		if ((CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL) == 0)
    516 			break;
    517 	}
    518 
    519 	if (i == VGE_TIMEOUT) {
    520 		aprint_error("%s: failed to start MII autopoll\n",
    521 		    sc->sc_dev.dv_xname);
    522 	}
    523 }
    524 
    525 static int
    526 vge_miibus_readreg(struct device *dev, int phy, int reg)
    527 {
    528 	struct vge_softc *sc;
    529 	int i, s;
    530 	uint16_t rval;
    531 
    532 	sc = (void *)dev;
    533 	rval = 0;
    534 	if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F))
    535 		return 0;
    536 
    537 	s = splnet();
    538 	vge_miipoll_stop(sc);
    539 
    540 	/* Specify the register we want to read. */
    541 	CSR_WRITE_1(sc, VGE_MIIADDR, reg);
    542 
    543 	/* Issue read command. */
    544 	CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_RCMD);
    545 
    546 	/* Wait for the read command bit to self-clear. */
    547 	for (i = 0; i < VGE_TIMEOUT; i++) {
    548 		DELAY(1);
    549 		if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_RCMD) == 0)
    550 			break;
    551 	}
    552 
    553 	if (i == VGE_TIMEOUT)
    554 		aprint_error("%s: MII read timed out\n", sc->sc_dev.dv_xname);
    555 	else
    556 		rval = CSR_READ_2(sc, VGE_MIIDATA);
    557 
    558 	vge_miipoll_start(sc);
    559 	splx(s);
    560 
    561 	return rval;
    562 }
    563 
    564 static void
    565 vge_miibus_writereg(struct device *dev, int phy, int reg, int data)
    566 {
    567 	struct vge_softc *sc;
    568 	int i, s;
    569 
    570 	sc = (void *)dev;
    571 	if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F))
    572 		return;
    573 
    574 	s = splnet();
    575 	vge_miipoll_stop(sc);
    576 
    577 	/* Specify the register we want to write. */
    578 	CSR_WRITE_1(sc, VGE_MIIADDR, reg);
    579 
    580 	/* Specify the data we want to write. */
    581 	CSR_WRITE_2(sc, VGE_MIIDATA, data);
    582 
    583 	/* Issue write command. */
    584 	CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_WCMD);
    585 
    586 	/* Wait for the write command bit to self-clear. */
    587 	for (i = 0; i < VGE_TIMEOUT; i++) {
    588 		DELAY(1);
    589 		if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_WCMD) == 0)
    590 			break;
    591 	}
    592 
    593 	if (i == VGE_TIMEOUT) {
    594 		aprint_error("%s: MII write timed out\n", sc->sc_dev.dv_xname);
    595 	}
    596 
    597 	vge_miipoll_start(sc);
    598 	splx(s);
    599 }
    600 
    601 static void
    602 vge_cam_clear(struct vge_softc *sc)
    603 {
    604 	int i;
    605 
    606 	/*
    607 	 * Turn off all the mask bits. This tells the chip
    608 	 * that none of the entries in the CAM filter are valid.
    609 	 * desired entries will be enabled as we fill the filter in.
    610 	 */
    611 
    612 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
    613 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
    614 	CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE);
    615 	for (i = 0; i < 8; i++)
    616 		CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
    617 
    618 	/* Clear the VLAN filter too. */
    619 
    620 	CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|VGE_CAMADDR_AVSEL|0);
    621 	for (i = 0; i < 8; i++)
    622 		CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
    623 
    624 	CSR_WRITE_1(sc, VGE_CAMADDR, 0);
    625 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
    626 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
    627 
    628 	sc->sc_camidx = 0;
    629 }
    630 
    631 static int
    632 vge_cam_set(struct vge_softc *sc, uint8_t *addr)
    633 {
    634 	int i, error;
    635 
    636 	error = 0;
    637 
    638 	if (sc->sc_camidx == VGE_CAM_MAXADDRS)
    639 		return ENOSPC;
    640 
    641 	/* Select the CAM data page. */
    642 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
    643 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMDATA);
    644 
    645 	/* Set the filter entry we want to update and enable writing. */
    646 	CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE | sc->sc_camidx);
    647 
    648 	/* Write the address to the CAM registers */
    649 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    650 		CSR_WRITE_1(sc, VGE_CAM0 + i, addr[i]);
    651 
    652 	/* Issue a write command. */
    653 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_WRITE);
    654 
    655 	/* Wake for it to clear. */
    656 	for (i = 0; i < VGE_TIMEOUT; i++) {
    657 		DELAY(1);
    658 		if ((CSR_READ_1(sc, VGE_CAMCTL) & VGE_CAMCTL_WRITE) == 0)
    659 			break;
    660 	}
    661 
    662 	if (i == VGE_TIMEOUT) {
    663 		aprint_error("%s: setting CAM filter failed\n",
    664 		    sc->sc_dev.dv_xname);
    665 		error = EIO;
    666 		goto fail;
    667 	}
    668 
    669 	/* Select the CAM mask page. */
    670 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
    671 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
    672 
    673 	/* Set the mask bit that enables this filter. */
    674 	CSR_SETBIT_1(sc, VGE_CAM0 + (sc->sc_camidx / 8),
    675 	    1 << (sc->sc_camidx & 7));
    676 
    677 	sc->sc_camidx++;
    678 
    679  fail:
    680 	/* Turn off access to CAM. */
    681 	CSR_WRITE_1(sc, VGE_CAMADDR, 0);
    682 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
    683 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
    684 
    685 	return error;
    686 }
    687 
    688 /*
    689  * Program the multicast filter. We use the 64-entry CAM filter
    690  * for perfect filtering. If there's more than 64 multicast addresses,
    691  * we use the hash filter instead.
    692  */
    693 static void
    694 vge_setmulti(struct vge_softc *sc)
    695 {
    696 	struct ifnet *ifp;
    697 	int error;
    698 	uint32_t h, hashes[2] = { 0, 0 };
    699 	struct ether_multi *enm;
    700 	struct ether_multistep step;
    701 
    702 	error = 0;
    703 	ifp = &sc->sc_ethercom.ec_if;
    704 
    705 	/* First, zot all the multicast entries. */
    706 	vge_cam_clear(sc);
    707 	CSR_WRITE_4(sc, VGE_MAR0, 0);
    708 	CSR_WRITE_4(sc, VGE_MAR1, 0);
    709 	ifp->if_flags &= ~IFF_ALLMULTI;
    710 
    711 	/*
    712 	 * If the user wants allmulti or promisc mode, enable reception
    713 	 * of all multicast frames.
    714 	 */
    715 	if (ifp->if_flags & IFF_PROMISC) {
    716  allmulti:
    717 		CSR_WRITE_4(sc, VGE_MAR0, 0xFFFFFFFF);
    718 		CSR_WRITE_4(sc, VGE_MAR1, 0xFFFFFFFF);
    719 		ifp->if_flags |= IFF_ALLMULTI;
    720 		return;
    721 	}
    722 
    723 	/* Now program new ones */
    724 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
    725 	while (enm != NULL) {
    726 		/*
    727 		 * If multicast range, fall back to ALLMULTI.
    728 		 */
    729 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    730 				ETHER_ADDR_LEN) != 0)
    731 			goto allmulti;
    732 
    733 		error = vge_cam_set(sc, enm->enm_addrlo);
    734 		if (error)
    735 			break;
    736 
    737 		ETHER_NEXT_MULTI(step, enm);
    738 	}
    739 
    740 	/* If there were too many addresses, use the hash filter. */
    741 	if (error) {
    742 		vge_cam_clear(sc);
    743 
    744 		ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
    745 		while (enm != NULL) {
    746 			/*
    747 			 * If multicast range, fall back to ALLMULTI.
    748 			 */
    749 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    750 					ETHER_ADDR_LEN) != 0)
    751 				goto allmulti;
    752 
    753 			h = ether_crc32_be(enm->enm_addrlo,
    754 			    ETHER_ADDR_LEN) >> 26;
    755 			hashes[h >> 5] |= 1 << (h & 0x1f);
    756 
    757 			ETHER_NEXT_MULTI(step, enm);
    758 		}
    759 
    760 		CSR_WRITE_4(sc, VGE_MAR0, hashes[0]);
    761 		CSR_WRITE_4(sc, VGE_MAR1, hashes[1]);
    762 	}
    763 }
    764 
    765 static void
    766 vge_reset(struct vge_softc *sc)
    767 {
    768 	int i;
    769 
    770 	CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_SOFTRESET);
    771 
    772 	for (i = 0; i < VGE_TIMEOUT; i++) {
    773 		DELAY(5);
    774 		if ((CSR_READ_1(sc, VGE_CRS1) & VGE_CR1_SOFTRESET) == 0)
    775 			break;
    776 	}
    777 
    778 	if (i == VGE_TIMEOUT) {
    779 		aprint_error("%s: soft reset timed out", sc->sc_dev.dv_xname);
    780 		CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_STOP_FORCE);
    781 		DELAY(2000);
    782 	}
    783 
    784 	DELAY(5000);
    785 
    786 	CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_RELOAD);
    787 
    788 	for (i = 0; i < VGE_TIMEOUT; i++) {
    789 		DELAY(5);
    790 		if ((CSR_READ_1(sc, VGE_EECSR) & VGE_EECSR_RELOAD) == 0)
    791 			break;
    792 	}
    793 
    794 	if (i == VGE_TIMEOUT) {
    795 		aprint_error("%s: EEPROM reload timed out\n",
    796 		    sc->sc_dev.dv_xname);
    797 		return;
    798 	}
    799 
    800 	/*
    801 	 * On some machine, the first read data from EEPROM could be
    802 	 * messed up, so read one dummy data here to avoid the mess.
    803 	 */
    804 	(void)vge_read_eeprom(sc, 0);
    805 
    806 	CSR_CLRBIT_1(sc, VGE_CHIPCFG0, VGE_CHIPCFG0_PACPI);
    807 }
    808 
    809 /*
    810  * Probe for a VIA gigabit chip. Check the PCI vendor and device
    811  * IDs against our list and return a device name if we find a match.
    812  */
    813 static int
    814 vge_probe(struct device *parent, struct cfdata *match,
    815     void *aux)
    816 {
    817 	struct pci_attach_args *pa = aux;
    818 
    819 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_VIATECH
    820 	    && PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_VIATECH_VT612X)
    821 		return 1;
    822 
    823 	return 0;
    824 }
    825 
    826 static int
    827 vge_allocmem(struct vge_softc *sc)
    828 {
    829 	int error;
    830 	int nseg;
    831 	int i;
    832 	bus_dma_segment_t seg;
    833 
    834 	/*
    835 	 * Allocate memory for control data.
    836 	 */
    837 
    838 	error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct vge_control_data),
    839 	     VGE_RING_ALIGN, 0, &seg, 1, &nseg, BUS_DMA_NOWAIT);
    840 	if (error) {
    841 		aprint_error("%s: could not allocate control data dma memory\n",
    842 		    sc->sc_dev.dv_xname);
    843 		return ENOMEM;
    844 	}
    845 
    846 	/* Map the memory to kernel VA space */
    847 
    848 	error = bus_dmamem_map(sc->sc_dmat, &seg, nseg,
    849 	    sizeof(struct vge_control_data), (caddr_t *)&sc->sc_control_data,
    850 	    BUS_DMA_NOWAIT);
    851 	if (error) {
    852 		aprint_error("%s: could not map control data dma memory\n",
    853 		    sc->sc_dev.dv_xname);
    854 		return ENOMEM;
    855 	}
    856 	memset(sc->sc_control_data, 0, sizeof(struct vge_control_data));
    857 
    858 	/*
    859 	 * Create map for control data.
    860 	 */
    861 	error = bus_dmamap_create(sc->sc_dmat,
    862 	    sizeof(struct vge_control_data), 1,
    863 	    sizeof(struct vge_control_data), 0, BUS_DMA_NOWAIT,
    864 	    &sc->sc_cddmamap);
    865 	if (error) {
    866 		aprint_error("%s: could not create control data dmamap\n",
    867 		    sc->sc_dev.dv_xname);
    868 		return ENOMEM;
    869 	}
    870 
    871 	/* Load the map for the control data. */
    872 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    873 	    sc->sc_control_data, sizeof(struct vge_control_data), NULL,
    874 	    BUS_DMA_NOWAIT);
    875 	if (error) {
    876 		aprint_error("%s: could not load control data dma memory\n",
    877 		    sc->sc_dev.dv_xname);
    878 		return ENOMEM;
    879 	}
    880 
    881 	/* Create DMA maps for TX buffers */
    882 
    883 	for (i = 0; i < VGE_NTXDESC; i++) {
    884 		error = bus_dmamap_create(sc->sc_dmat, VGE_TX_MAXLEN,
    885 		    VGE_TX_FRAGS, VGE_TX_MAXLEN, 0, BUS_DMA_NOWAIT,
    886 		    &sc->sc_txsoft[i].txs_dmamap);
    887 		if (error) {
    888 			aprint_error("%s: can't create DMA map for TX descs\n",
    889 			    sc->sc_dev.dv_xname);
    890 			return ENOMEM;
    891 		}
    892 	}
    893 
    894 	/* Create DMA maps for RX buffers */
    895 
    896 	for (i = 0; i < VGE_NRXDESC; i++) {
    897 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    898 		    1, MCLBYTES, 0, BUS_DMA_NOWAIT,
    899 		    &sc->sc_rxsoft[i].rxs_dmamap);
    900 		if (error) {
    901 			aprint_error("%s: can't create DMA map for RX descs\n",
    902 			    sc->sc_dev.dv_xname);
    903 			return ENOMEM;
    904 		}
    905 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    906 	}
    907 
    908 	return 0;
    909 }
    910 
    911 /*
    912  * Attach the interface. Allocate softc structures, do ifmedia
    913  * setup and ethernet/BPF attach.
    914  */
    915 static void
    916 vge_attach(struct device *parent, struct device *self, void *aux)
    917 {
    918 	uint8_t	*eaddr;
    919 	struct vge_softc *sc = (void *)self;
    920 	struct ifnet *ifp;
    921 	struct pci_attach_args *pa = aux;
    922 	pci_chipset_tag_t pc = pa->pa_pc;
    923 	const char *intrstr;
    924 	pci_intr_handle_t ih;
    925 	uint16_t val;
    926 
    927 	aprint_normal(": VIA VT612X Gigabit Ethernet (rev. %#x)\n",
    928 		PCI_REVISION(pa->pa_class));
    929 
    930 	/* Make sure bus-mastering is enabled */
    931         pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    932 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    933 	    PCI_COMMAND_MASTER_ENABLE);
    934 
    935 	/*
    936 	 * Map control/status registers.
    937 	 */
    938 	if (pci_mapreg_map(pa, VGE_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    939 	    &sc->sc_bst, &sc->sc_bsh, NULL, NULL) != 0) {
    940 		aprint_error("%s: couldn't map memory\n", sc->sc_dev.dv_xname);
    941 		return;
    942 	}
    943 
    944         /*
    945          * Map and establish our interrupt.
    946          */
    947 	if (pci_intr_map(pa, &ih)) {
    948 		aprint_error("%s: unable to map interrupt\n",
    949 		    sc->sc_dev.dv_xname);
    950 		return;
    951 	}
    952 	intrstr = pci_intr_string(pc, ih);
    953 	sc->sc_intrhand = pci_intr_establish(pc, ih, IPL_NET, vge_intr, sc);
    954 	if (sc->sc_intrhand == NULL) {
    955 		aprint_error("%s: unable to establish interrupt",
    956 		    sc->sc_dev.dv_xname);
    957 		if (intrstr != NULL)
    958 			aprint_error(" at %s", intrstr);
    959 		aprint_error("\n");
    960 		return;
    961 	}
    962 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    963 
    964 	/* Reset the adapter. */
    965 	vge_reset(sc);
    966 
    967 	/*
    968 	 * Get station address from the EEPROM.
    969 	 */
    970 	eaddr = sc->sc_eaddr;
    971 	val = vge_read_eeprom(sc, VGE_EE_EADDR + 0);
    972 	eaddr[0] = val & 0xff;
    973 	eaddr[1] = val >> 8;
    974 	val = vge_read_eeprom(sc, VGE_EE_EADDR + 1);
    975 	eaddr[2] = val & 0xff;
    976 	eaddr[3] = val >> 8;
    977 	val = vge_read_eeprom(sc, VGE_EE_EADDR + 2);
    978 	eaddr[4] = val & 0xff;
    979 	eaddr[5] = val >> 8;
    980 
    981 	aprint_normal("%s: Ethernet address: %s\n", sc->sc_dev.dv_xname,
    982 	    ether_sprintf(eaddr));
    983 
    984 	/*
    985 	 * Use the 32bit tag. Hardware supports 48bit physical addresses,
    986 	 * but we don't use that for now.
    987 	 */
    988 	sc->sc_dmat = pa->pa_dmat;
    989 
    990 	if (vge_allocmem(sc))
    991 		return;
    992 
    993 	ifp = &sc->sc_ethercom.ec_if;
    994 	ifp->if_softc = sc;
    995 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    996 	ifp->if_mtu = ETHERMTU;
    997 	ifp->if_baudrate = IF_Gbps(1);
    998 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    999 	ifp->if_ioctl = vge_ioctl;
   1000 	ifp->if_start = vge_start;
   1001 
   1002 	/*
   1003 	 * We can support 802.1Q VLAN-sized frames and jumbo
   1004 	 * Ethernet frames.
   1005 	 */
   1006 	sc->sc_ethercom.ec_capabilities |=
   1007 	    ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU |
   1008 	    ETHERCAP_VLAN_HWTAGGING;
   1009 
   1010 	/*
   1011 	 * We can do IPv4/TCPv4/UDPv4 checksums in hardware.
   1012 	 */
   1013 	ifp->if_capabilities |=
   1014 	    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
   1015 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
   1016 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
   1017 
   1018 #ifdef DEVICE_POLLING
   1019 #ifdef IFCAP_POLLING
   1020 	ifp->if_capabilities |= IFCAP_POLLING;
   1021 #endif
   1022 #endif
   1023 	ifp->if_watchdog = vge_watchdog;
   1024 	ifp->if_init = vge_init;
   1025 	IFQ_SET_MAXLEN(&ifp->if_snd, max(VGE_IFQ_MAXLEN, IFQ_MAXLEN));
   1026 
   1027 	/*
   1028 	 * Initialize our media structures and probe the MII.
   1029 	 */
   1030 	sc->sc_mii.mii_ifp = ifp;
   1031 	sc->sc_mii.mii_readreg = vge_miibus_readreg;
   1032 	sc->sc_mii.mii_writereg = vge_miibus_writereg;
   1033 	sc->sc_mii.mii_statchg = vge_miibus_statchg;
   1034 	ifmedia_init(&sc->sc_mii.mii_media, 0, vge_ifmedia_upd,
   1035 	    vge_ifmedia_sts);
   1036 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   1037 	    MII_OFFSET_ANY, MIIF_DOPAUSE);
   1038 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   1039 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   1040 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   1041 	} else
   1042 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   1043 
   1044 	/*
   1045 	 * Attach the interface.
   1046 	 */
   1047 	if_attach(ifp);
   1048 	ether_ifattach(ifp, eaddr);
   1049 
   1050 	callout_init(&sc->sc_timeout);
   1051 	callout_setfunc(&sc->sc_timeout, vge_tick, sc);
   1052 
   1053 	/*
   1054 	 * Make sure the interface is shutdown during reboot.
   1055 	 */
   1056 	if (shutdownhook_establish(vge_shutdown, sc) == NULL) {
   1057 		aprint_error("%s: WARNING: unable to establish shutdown hook\n",
   1058 		    sc->sc_dev.dv_xname);
   1059 	}
   1060 }
   1061 
   1062 static int
   1063 vge_newbuf(struct vge_softc *sc, int idx, struct mbuf *m)
   1064 {
   1065 	struct mbuf *m_new;
   1066 	struct vge_rxdesc *rxd;
   1067 	struct vge_rxsoft *rxs;
   1068 	bus_dmamap_t map;
   1069 	int i;
   1070 #ifdef DIAGNOSTIC
   1071 	uint32_t rd_sts;
   1072 #endif
   1073 
   1074 	m_new = NULL;
   1075 	if (m == NULL) {
   1076 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1077 		if (m_new == NULL)
   1078 			return ENOBUFS;
   1079 
   1080 		MCLGET(m_new, M_DONTWAIT);
   1081 		if ((m_new->m_flags & M_EXT) == 0) {
   1082 			m_freem(m_new);
   1083 			return ENOBUFS;
   1084 		}
   1085 
   1086 		m = m_new;
   1087 	} else
   1088 		m->m_data = m->m_ext.ext_buf;
   1089 
   1090 
   1091 	/*
   1092 	 * This is part of an evil trick to deal with non-x86 platforms.
   1093 	 * The VIA chip requires RX buffers to be aligned on 32-bit
   1094 	 * boundaries, but that will hose non-x86 machines. To get around
   1095 	 * this, we leave some empty space at the start of each buffer
   1096 	 * and for non-x86 hosts, we copy the buffer back two bytes
   1097 	 * to achieve word alignment. This is slightly more efficient
   1098 	 * than allocating a new buffer, copying the contents, and
   1099 	 * discarding the old buffer.
   1100 	 */
   1101 	m->m_len = m->m_pkthdr.len = VGE_RX_BUFSIZE;
   1102 #ifndef __NO_STRICT_ALIGNMENT
   1103 	m->m_data += VGE_RX_PAD;
   1104 #endif
   1105 	rxs = &sc->sc_rxsoft[idx];
   1106 	map = rxs->rxs_dmamap;
   1107 
   1108 	if (bus_dmamap_load_mbuf(sc->sc_dmat, map, m, BUS_DMA_NOWAIT) != 0)
   1109 		goto out;
   1110 
   1111 	rxd = &sc->sc_rxdescs[idx];
   1112 
   1113 #ifdef DIAGNOSTIC
   1114 	/* If this descriptor is still owned by the chip, bail. */
   1115 	VGE_RXDESCSYNC(sc, idx, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1116 	rd_sts = le32toh(rxd->rd_sts);
   1117 	VGE_RXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
   1118 	if (rd_sts & VGE_RDSTS_OWN) {
   1119 		panic("%s: tried to map busy RX descriptor",
   1120 		    sc->sc_dev.dv_xname);
   1121 	}
   1122 #endif
   1123 
   1124 	rxs->rxs_mbuf = m;
   1125 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   1126 	    BUS_DMASYNC_PREREAD);
   1127 
   1128 	rxd->rd_buflen =
   1129 	    htole16(VGE_BUFLEN(map->dm_segs[0].ds_len) | VGE_RXDESC_I);
   1130 	vge_set_rxaddr(rxd, map->dm_segs[0].ds_addr);
   1131 	rxd->rd_sts = 0;
   1132 	rxd->rd_ctl = 0;
   1133 	VGE_RXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1134 
   1135 	/*
   1136 	 * Note: the manual fails to document the fact that for
   1137 	 * proper opration, the driver needs to replentish the RX
   1138 	 * DMA ring 4 descriptors at a time (rather than one at a
   1139 	 * time, like most chips). We can allocate the new buffers
   1140 	 * but we should not set the OWN bits until we're ready
   1141 	 * to hand back 4 of them in one shot.
   1142 	 */
   1143 
   1144 #define VGE_RXCHUNK 4
   1145 	sc->sc_rx_consumed++;
   1146 	if (sc->sc_rx_consumed == VGE_RXCHUNK) {
   1147 		for (i = idx; i != idx - VGE_RXCHUNK; i--) {
   1148 			KASSERT(i >= 0);
   1149 			sc->sc_rxdescs[i].rd_sts |= htole32(VGE_RDSTS_OWN);
   1150 			VGE_RXDESCSYNC(sc, i,
   1151 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1152 		}
   1153 		sc->sc_rx_consumed = 0;
   1154 	}
   1155 
   1156 	return 0;
   1157  out:
   1158 	if (m_new != NULL)
   1159 		m_freem(m_new);
   1160 	return ENOMEM;
   1161 }
   1162 
   1163 #ifndef __NO_STRICT_ALIGNMENT
   1164 static inline void
   1165 vge_fixup_rx(struct mbuf *m)
   1166 {
   1167 	int i;
   1168 	uint16_t *src, *dst;
   1169 
   1170 	src = mtod(m, uint16_t *);
   1171 	dst = src - 1;
   1172 
   1173 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
   1174 		*dst++ = *src++;
   1175 
   1176 	m->m_data -= ETHER_ALIGN;
   1177 }
   1178 #endif
   1179 
   1180 /*
   1181  * RX handler. We support the reception of jumbo frames that have
   1182  * been fragmented across multiple 2K mbuf cluster buffers.
   1183  */
   1184 static void
   1185 vge_rxeof(struct vge_softc *sc)
   1186 {
   1187 	struct mbuf *m;
   1188 	struct ifnet *ifp;
   1189 	int idx, total_len, lim;
   1190 	struct vge_rxdesc *cur_rxd;
   1191 	struct vge_rxsoft *rxs;
   1192 	uint32_t rxstat, rxctl;
   1193 
   1194 	ifp = &sc->sc_ethercom.ec_if;
   1195 	lim = 0;
   1196 
   1197 	/* Invalidate the descriptor memory */
   1198 
   1199 	for (idx = sc->sc_rx_prodidx;; idx = VGE_NEXT_RXDESC(idx)) {
   1200 		cur_rxd = &sc->sc_rxdescs[idx];
   1201 
   1202 		VGE_RXDESCSYNC(sc, idx,
   1203 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1204 		rxstat = le32toh(cur_rxd->rd_sts);
   1205 		if ((rxstat & VGE_RDSTS_OWN) != 0) {
   1206 			VGE_RXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
   1207 			break;
   1208 		}
   1209 
   1210 		rxctl = le32toh(cur_rxd->rd_ctl);
   1211 		rxs = &sc->sc_rxsoft[idx];
   1212 		m = rxs->rxs_mbuf;
   1213 		total_len = (rxstat & VGE_RDSTS_BUFSIZ) >> 16;
   1214 
   1215 		/* Invalidate the RX mbuf and unload its map */
   1216 
   1217 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap,
   1218 		    0, rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1219 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1220 
   1221 		/*
   1222 		 * If the 'start of frame' bit is set, this indicates
   1223 		 * either the first fragment in a multi-fragment receive,
   1224 		 * or an intermediate fragment. Either way, we want to
   1225 		 * accumulate the buffers.
   1226 		 */
   1227 		if (rxstat & VGE_RXPKT_SOF) {
   1228 			m->m_len = VGE_RX_BUFSIZE;
   1229 			if (sc->sc_rx_mhead == NULL)
   1230 				sc->sc_rx_mhead = sc->sc_rx_mtail = m;
   1231 			else {
   1232 				m->m_flags &= ~M_PKTHDR;
   1233 				sc->sc_rx_mtail->m_next = m;
   1234 				sc->sc_rx_mtail = m;
   1235 			}
   1236 			vge_newbuf(sc, idx, NULL);
   1237 			continue;
   1238 		}
   1239 
   1240 		/*
   1241 		 * Bad/error frames will have the RXOK bit cleared.
   1242 		 * However, there's one error case we want to allow:
   1243 		 * if a VLAN tagged frame arrives and the chip can't
   1244 		 * match it against the CAM filter, it considers this
   1245 		 * a 'VLAN CAM filter miss' and clears the 'RXOK' bit.
   1246 		 * We don't want to drop the frame though: our VLAN
   1247 		 * filtering is done in software.
   1248 		 */
   1249 		if (!(rxstat & VGE_RDSTS_RXOK) && !(rxstat & VGE_RDSTS_VIDM)
   1250 		    && !(rxstat & VGE_RDSTS_CSUMERR)) {
   1251 			ifp->if_ierrors++;
   1252 			/*
   1253 			 * If this is part of a multi-fragment packet,
   1254 			 * discard all the pieces.
   1255 			 */
   1256 			if (sc->sc_rx_mhead != NULL) {
   1257 				m_freem(sc->sc_rx_mhead);
   1258 				sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
   1259 			}
   1260 			vge_newbuf(sc, idx, m);
   1261 			continue;
   1262 		}
   1263 
   1264 		/*
   1265 		 * If allocating a replacement mbuf fails,
   1266 		 * reload the current one.
   1267 		 */
   1268 
   1269 		if (vge_newbuf(sc, idx, NULL)) {
   1270 			ifp->if_ierrors++;
   1271 			if (sc->sc_rx_mhead != NULL) {
   1272 				m_freem(sc->sc_rx_mhead);
   1273 				sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
   1274 			}
   1275 			vge_newbuf(sc, idx, m);
   1276 			continue;
   1277 		}
   1278 
   1279 		if (sc->sc_rx_mhead != NULL) {
   1280 			m->m_len = total_len % VGE_RX_BUFSIZE;
   1281 			/*
   1282 			 * Special case: if there's 4 bytes or less
   1283 			 * in this buffer, the mbuf can be discarded:
   1284 			 * the last 4 bytes is the CRC, which we don't
   1285 			 * care about anyway.
   1286 			 */
   1287 			if (m->m_len <= ETHER_CRC_LEN) {
   1288 				sc->sc_rx_mtail->m_len -=
   1289 				    (ETHER_CRC_LEN - m->m_len);
   1290 				m_freem(m);
   1291 			} else {
   1292 				m->m_len -= ETHER_CRC_LEN;
   1293 				m->m_flags &= ~M_PKTHDR;
   1294 				sc->sc_rx_mtail->m_next = m;
   1295 			}
   1296 			m = sc->sc_rx_mhead;
   1297 			sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
   1298 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
   1299 		} else
   1300 			m->m_pkthdr.len = m->m_len = total_len - ETHER_CRC_LEN;
   1301 
   1302 #ifndef __NO_STRICT_ALIGNMENT
   1303 		vge_fixup_rx(m);
   1304 #endif
   1305 		ifp->if_ipackets++;
   1306 		m->m_pkthdr.rcvif = ifp;
   1307 
   1308 		/* Do RX checksumming if enabled */
   1309 		if (ifp->if_csum_flags_rx & M_CSUM_IPv4) {
   1310 
   1311 			/* Check IP header checksum */
   1312 			if (rxctl & VGE_RDCTL_IPPKT)
   1313 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1314 			if ((rxctl & VGE_RDCTL_IPCSUMOK) == 0)
   1315 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1316 		}
   1317 
   1318 		if (ifp->if_csum_flags_rx & M_CSUM_TCPv4) {
   1319 			/* Check UDP checksum */
   1320 			if (rxctl & VGE_RDCTL_TCPPKT)
   1321 				m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
   1322 
   1323 			if ((rxctl & VGE_RDCTL_PROTOCSUMOK) == 0)
   1324 				m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1325 		}
   1326 
   1327 		if (ifp->if_csum_flags_rx & M_CSUM_UDPv4) {
   1328 			/* Check UDP checksum */
   1329 			if (rxctl & VGE_RDCTL_UDPPKT)
   1330 				m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
   1331 
   1332 			if ((rxctl & VGE_RDCTL_PROTOCSUMOK) == 0)
   1333 				m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1334 		}
   1335 
   1336 		if (rxstat & VGE_RDSTS_VTAG) {
   1337 			/*
   1338 			 * We use bswap16() here because:
   1339 			 * On LE machines, tag is stored in BE as stream data.
   1340 			 * On BE machines, tag is stored in BE as stream data
   1341 			 *  but it was already swapped by le32toh() above.
   1342 			 */
   1343 			VLAN_INPUT_TAG(ifp, m,
   1344 			    bswap16(rxctl & VGE_RDCTL_VLANID), continue);
   1345 		}
   1346 
   1347 #if NBPFILTER > 0
   1348 		/*
   1349 		 * Handle BPF listeners.
   1350 		 */
   1351 		if (ifp->if_bpf)
   1352 			bpf_mtap(ifp->if_bpf, m);
   1353 #endif
   1354 
   1355 		(*ifp->if_input)(ifp, m);
   1356 
   1357 		lim++;
   1358 		if (lim == VGE_NRXDESC)
   1359 			break;
   1360 
   1361 	}
   1362 
   1363 	sc->sc_rx_prodidx = idx;
   1364 	CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, lim);
   1365 }
   1366 
   1367 static void
   1368 vge_txeof(struct vge_softc *sc)
   1369 {
   1370 	struct ifnet *ifp;
   1371 	struct vge_txsoft *txs;
   1372 	uint32_t txstat;
   1373 	int idx;
   1374 
   1375 	ifp = &sc->sc_ethercom.ec_if;
   1376 
   1377 	for (idx = sc->sc_tx_considx;
   1378 	    sc->sc_tx_free < VGE_NTXDESC;
   1379 	    idx = VGE_NEXT_TXDESC(idx), sc->sc_tx_free++) {
   1380 		VGE_TXDESCSYNC(sc, idx,
   1381 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1382 		txstat = le32toh(sc->sc_txdescs[idx].td_sts);
   1383 		VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
   1384 		if (txstat & VGE_TDSTS_OWN) {
   1385 			break;
   1386 		}
   1387 
   1388 		txs = &sc->sc_txsoft[idx];
   1389 		m_freem(txs->txs_mbuf);
   1390 		txs->txs_mbuf = NULL;
   1391 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap, 0,
   1392 		    txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1393 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1394 		if (txstat & (VGE_TDSTS_EXCESSCOLL|VGE_TDSTS_COLL))
   1395 			ifp->if_collisions++;
   1396 		if (txstat & VGE_TDSTS_TXERR)
   1397 			ifp->if_oerrors++;
   1398 		else
   1399 			ifp->if_opackets++;
   1400 	}
   1401 
   1402 	sc->sc_tx_considx = idx;
   1403 
   1404 	if (sc->sc_tx_free > 0) {
   1405 		ifp->if_flags &= ~IFF_OACTIVE;
   1406 	}
   1407 
   1408 	/*
   1409 	 * If not all descriptors have been released reaped yet,
   1410 	 * reload the timer so that we will eventually get another
   1411 	 * interrupt that will cause us to re-enter this routine.
   1412 	 * This is done in case the transmitter has gone idle.
   1413 	 */
   1414 	if (sc->sc_tx_free < VGE_NTXDESC)
   1415 		CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
   1416 	else
   1417 		ifp->if_timer = 0;
   1418 }
   1419 
   1420 static void
   1421 vge_tick(void *xsc)
   1422 {
   1423 	struct vge_softc *sc;
   1424 	struct ifnet *ifp;
   1425 	struct mii_data *mii;
   1426 	int s;
   1427 
   1428 	sc = xsc;
   1429 	ifp = &sc->sc_ethercom.ec_if;
   1430 	mii = &sc->sc_mii;
   1431 
   1432 	s = splnet();
   1433 
   1434 	callout_schedule(&sc->sc_timeout, hz);
   1435 
   1436 	mii_tick(mii);
   1437 	if (sc->sc_link) {
   1438 		if (!(mii->mii_media_status & IFM_ACTIVE))
   1439 			sc->sc_link = 0;
   1440 	} else {
   1441 		if (mii->mii_media_status & IFM_ACTIVE &&
   1442 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1443 			sc->sc_link = 1;
   1444 			if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1445 				vge_start(ifp);
   1446 		}
   1447 	}
   1448 
   1449 	splx(s);
   1450 }
   1451 
   1452 static int
   1453 vge_intr(void *arg)
   1454 {
   1455 	struct vge_softc *sc;
   1456 	struct ifnet *ifp;
   1457 	uint32_t status;
   1458 	int claim;
   1459 
   1460 	sc = arg;
   1461 	claim = 0;
   1462 	if (sc->sc_suspended) {
   1463 		return claim;
   1464 	}
   1465 
   1466 	ifp = &sc->sc_ethercom.ec_if;
   1467 
   1468 	if (!(ifp->if_flags & IFF_UP)) {
   1469 		return claim;
   1470 	}
   1471 
   1472 	/* Disable interrupts */
   1473 	CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
   1474 
   1475 	for (;;) {
   1476 
   1477 		status = CSR_READ_4(sc, VGE_ISR);
   1478 		/* If the card has gone away the read returns 0xffff. */
   1479 		if (status == 0xFFFFFFFF)
   1480 			break;
   1481 
   1482 		if (status) {
   1483 			claim = 1;
   1484 			CSR_WRITE_4(sc, VGE_ISR, status);
   1485 		}
   1486 
   1487 		if ((status & VGE_INTRS) == 0)
   1488 			break;
   1489 
   1490 		if (status & (VGE_ISR_RXOK|VGE_ISR_RXOK_HIPRIO))
   1491 			vge_rxeof(sc);
   1492 
   1493 		if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
   1494 			vge_rxeof(sc);
   1495 			CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
   1496 			CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
   1497 		}
   1498 
   1499 		if (status & (VGE_ISR_TXOK0|VGE_ISR_TIMER0))
   1500 			vge_txeof(sc);
   1501 
   1502 		if (status & (VGE_ISR_TXDMA_STALL|VGE_ISR_RXDMA_STALL))
   1503 			vge_init(ifp);
   1504 
   1505 		if (status & VGE_ISR_LINKSTS)
   1506 			vge_tick(sc);
   1507 	}
   1508 
   1509 	/* Re-enable interrupts */
   1510 	CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
   1511 
   1512 	if (claim && !IFQ_IS_EMPTY(&ifp->if_snd))
   1513 		vge_start(ifp);
   1514 
   1515 	return claim;
   1516 }
   1517 
   1518 static int
   1519 vge_encap(struct vge_softc *sc, struct mbuf *m_head, int idx)
   1520 {
   1521 	struct vge_txsoft *txs;
   1522 	struct vge_txdesc *txd;
   1523 	struct vge_txfrag *f;
   1524 	struct mbuf *m_new;
   1525 	bus_dmamap_t map;
   1526 	int m_csumflags, seg, error, flags;
   1527 	struct m_tag *mtag;
   1528 	size_t sz;
   1529 	uint32_t td_sts, td_ctl;
   1530 
   1531 	KASSERT(sc->sc_tx_free > 0);
   1532 
   1533 	txd = &sc->sc_txdescs[idx];
   1534 
   1535 #ifdef DIAGNOSTIC
   1536 	/* If this descriptor is still owned by the chip, bail. */
   1537 	VGE_TXDESCSYNC(sc, idx,
   1538 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1539 	td_sts = le32toh(txd->td_sts);
   1540 	VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
   1541 	if (td_sts & VGE_TDSTS_OWN) {
   1542 		return ENOBUFS;
   1543 	}
   1544 #endif
   1545 
   1546 	/*
   1547 	 * Preserve m_pkthdr.csum_flags here since m_head might be
   1548 	 * updated by m_defrag()
   1549 	 */
   1550 	m_csumflags = m_head->m_pkthdr.csum_flags;
   1551 
   1552 	txs = &sc->sc_txsoft[idx];
   1553 	map = txs->txs_dmamap;
   1554 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m_head, BUS_DMA_NOWAIT);
   1555 
   1556 	/* If too many segments to map, coalesce */
   1557 	if (error == EFBIG ||
   1558 	    (m_head->m_pkthdr.len < ETHER_PAD_LEN &&
   1559 	     map->dm_nsegs == VGE_TX_FRAGS)) {
   1560 		m_new = m_defrag(m_head, M_DONTWAIT);
   1561 		if (m_new == NULL)
   1562 			return EFBIG;
   1563 
   1564 		error = bus_dmamap_load_mbuf(sc->sc_dmat, map,
   1565 		    m_new, BUS_DMA_NOWAIT);
   1566 		if (error) {
   1567 			m_freem(m_new);
   1568 			return error;
   1569 		}
   1570 
   1571 		m_head = m_new;
   1572 	} else if (error)
   1573 		return error;
   1574 
   1575 	txs->txs_mbuf = m_head;
   1576 
   1577 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   1578 	    BUS_DMASYNC_PREWRITE);
   1579 
   1580 	for (seg = 0, f = &txd->td_frag[0]; seg < map->dm_nsegs; seg++, f++) {
   1581 		f->tf_buflen = htole16(VGE_BUFLEN(map->dm_segs[seg].ds_len));
   1582 		vge_set_txaddr(f, map->dm_segs[seg].ds_addr);
   1583 	}
   1584 
   1585 	/* Argh. This chip does not autopad short frames */
   1586 	sz = m_head->m_pkthdr.len;
   1587 	if (sz < ETHER_PAD_LEN) {
   1588 		f->tf_buflen = htole16(VGE_BUFLEN(ETHER_PAD_LEN - sz));
   1589 		vge_set_txaddr(f, VGE_CDPADADDR(sc));
   1590 		sz = ETHER_PAD_LEN;
   1591 		seg++;
   1592 	}
   1593 	VGE_TXFRAGSYNC(sc, idx, seg, BUS_DMASYNC_PREWRITE);
   1594 
   1595 	/*
   1596 	 * When telling the chip how many segments there are, we
   1597 	 * must use nsegs + 1 instead of just nsegs. Darned if I
   1598 	 * know why.
   1599 	 */
   1600 	seg++;
   1601 
   1602 	flags = 0;
   1603 	if (m_csumflags & M_CSUM_IPv4)
   1604 		flags |= VGE_TDCTL_IPCSUM;
   1605 	if (m_csumflags & M_CSUM_TCPv4)
   1606 		flags |= VGE_TDCTL_TCPCSUM;
   1607 	if (m_csumflags & M_CSUM_UDPv4)
   1608 		flags |= VGE_TDCTL_UDPCSUM;
   1609 	td_sts = sz << 16;
   1610 	td_ctl = flags | (seg << 28) | VGE_TD_LS_NORM;
   1611 
   1612 	if (sz > ETHERMTU + ETHER_HDR_LEN)
   1613 		td_ctl |= VGE_TDCTL_JUMBO;
   1614 
   1615 	/*
   1616 	 * Set up hardware VLAN tagging.
   1617 	 */
   1618 	mtag = VLAN_OUTPUT_TAG(&sc->sc_ethercom, m_head);
   1619 	if (mtag != NULL) {
   1620 		/*
   1621 		 * No need htons() here since vge(4) chip assumes
   1622 		 * that tags are written in little endian and
   1623 		 * we already use htole32() here.
   1624 		 */
   1625 		td_ctl |= VLAN_TAG_VALUE(mtag) | VGE_TDCTL_VTAG;
   1626 	}
   1627 	txd->td_ctl = htole32(td_ctl);
   1628 	txd->td_sts = htole32(td_sts);
   1629 	VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1630 
   1631 	txd->td_sts = htole32(VGE_TDSTS_OWN | td_sts);
   1632 	VGE_TXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1633 
   1634 	sc->sc_tx_free--;
   1635 
   1636 	return 0;
   1637 }
   1638 
   1639 /*
   1640  * Main transmit routine.
   1641  */
   1642 
   1643 static void
   1644 vge_start(struct ifnet *ifp)
   1645 {
   1646 	struct vge_softc *sc;
   1647 	struct vge_txsoft *txs;
   1648 	struct mbuf *m_head;
   1649 	int idx, pidx, ofree, error;
   1650 
   1651 	sc = ifp->if_softc;
   1652 
   1653 	if (!sc->sc_link ||
   1654 	    (ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) {
   1655 		return;
   1656 	}
   1657 
   1658 	m_head = NULL;
   1659 	idx = sc->sc_tx_prodidx;
   1660 	pidx = VGE_PREV_TXDESC(idx);
   1661 	ofree = sc->sc_tx_free;
   1662 
   1663 	/*
   1664 	 * Loop through the send queue, setting up transmit descriptors
   1665 	 * until we drain the queue, or use up all available transmit
   1666 	 * descriptors.
   1667 	 */
   1668 	for (;;) {
   1669 		/* Grab a packet off the queue. */
   1670 		IFQ_POLL(&ifp->if_snd, m_head);
   1671 		if (m_head == NULL)
   1672 			break;
   1673 
   1674 		if (sc->sc_tx_free == 0) {
   1675 			/*
   1676 			 * All slots used, stop for now.
   1677 			 */
   1678 			ifp->if_flags |= IFF_OACTIVE;
   1679 			break;
   1680 		}
   1681 
   1682 		txs = &sc->sc_txsoft[idx];
   1683 		KASSERT(txs->txs_mbuf == NULL);
   1684 
   1685 		if ((error = vge_encap(sc, m_head, idx))) {
   1686 			if (error == EFBIG) {
   1687 				aprint_error("%s: Tx packet consumes too many "
   1688 				    "DMA segments, dropping...\n",
   1689 				    sc->sc_dev.dv_xname);
   1690 				IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1691 				m_freem(m_head);
   1692 				continue;
   1693 			}
   1694 
   1695 			/*
   1696 			 * Short on resources, just stop for now.
   1697 			 */
   1698 			if (error == ENOBUFS)
   1699 				ifp->if_flags |= IFF_OACTIVE;
   1700 			break;
   1701 		}
   1702 
   1703 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1704 
   1705 		/*
   1706 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
   1707 		 */
   1708 
   1709 		sc->sc_txdescs[pidx].td_frag[0].tf_buflen |=
   1710 		    htole16(VGE_TXDESC_Q);
   1711 		VGE_TXFRAGSYNC(sc, pidx, 1,
   1712 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1713 
   1714 		if (txs->txs_mbuf != m_head) {
   1715 			m_freem(m_head);
   1716 			m_head = txs->txs_mbuf;
   1717 		}
   1718 
   1719 		pidx = idx;
   1720 		idx = VGE_NEXT_TXDESC(idx);
   1721 
   1722 		/*
   1723 		 * If there's a BPF listener, bounce a copy of this frame
   1724 		 * to him.
   1725 		 */
   1726 #if NBPFILTER > 0
   1727 		if (ifp->if_bpf)
   1728 			bpf_mtap(ifp->if_bpf, m_head);
   1729 #endif
   1730 	}
   1731 
   1732 	if (sc->sc_tx_free < ofree) {
   1733 		/* TX packet queued */
   1734 
   1735 		sc->sc_tx_prodidx = idx;
   1736 
   1737 		/* Issue a transmit command. */
   1738 		CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_WAK0);
   1739 
   1740 		/*
   1741 		 * Use the countdown timer for interrupt moderation.
   1742 		 * 'TX done' interrupts are disabled. Instead, we reset the
   1743 		 * countdown timer, which will begin counting until it hits
   1744 		 * the value in the SSTIMER register, and then trigger an
   1745 		 * interrupt. Each time we set the TIMER0_ENABLE bit, the
   1746 		 * the timer count is reloaded. Only when the transmitter
   1747 		 * is idle will the timer hit 0 and an interrupt fire.
   1748 		 */
   1749 		CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE);
   1750 
   1751 		/*
   1752 		 * Set a timeout in case the chip goes out to lunch.
   1753 		 */
   1754 		ifp->if_timer = 5;
   1755 	}
   1756 }
   1757 
   1758 static int
   1759 vge_init(struct ifnet *ifp)
   1760 {
   1761 	struct vge_softc *sc;
   1762 	struct vge_rxsoft *rxs;
   1763 	int i;
   1764 
   1765 	sc = ifp->if_softc;
   1766 
   1767 	/*
   1768 	 * Cancel pending I/O and free all RX/TX buffers.
   1769 	 */
   1770 	vge_stop(sc);
   1771 	vge_reset(sc);
   1772 
   1773 	/* Initialize the RX descriptors and mbufs. */
   1774 	memset(sc->sc_rxdescs, 0, sizeof(sc->sc_rxdescs));
   1775 	for (i = 0; i < VGE_NRXDESC; i++) {
   1776 		rxs = &sc->sc_rxsoft[i];
   1777 		if (rxs->rxs_mbuf) {
   1778 			m_freem(rxs->rxs_mbuf);
   1779 			rxs->rxs_mbuf = NULL;
   1780 		}
   1781 		if (rxs->rxs_dmamap)
   1782 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1783 		if (vge_newbuf(sc, i, NULL) == ENOBUFS) {
   1784 			aprint_error("%s: unable to allocate or map "
   1785 			    "rx buffer\n", sc->sc_dev.dv_xname);
   1786 			return 1; /* XXX */
   1787 		}
   1788 	}
   1789 	sc->sc_rx_prodidx = 0;
   1790 	sc->sc_rx_consumed = 0;
   1791 	sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
   1792 
   1793 	/* Initialize the  TX descriptors and mbufs. */
   1794 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1795 	bus_dmamap_sync(sc->sc_dmat, sc->sc_cddmamap,
   1796 	    VGE_CDTXOFF(0), sizeof(sc->sc_txdescs),
   1797 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1798 	for (i = 0; i < VGE_NTXDESC; i++)
   1799 		sc->sc_txsoft[i].txs_mbuf = NULL;
   1800 
   1801 	sc->sc_tx_prodidx = 0;
   1802 	sc->sc_tx_considx = 0;
   1803 	sc->sc_tx_free = VGE_NTXDESC;
   1804 
   1805 	/* Set our station address */
   1806 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1807 		CSR_WRITE_1(sc, VGE_PAR0 + i, sc->sc_eaddr[i]);
   1808 
   1809 	/*
   1810 	 * Set receive FIFO threshold. Also allow transmission and
   1811 	 * reception of VLAN tagged frames.
   1812 	 */
   1813 	CSR_CLRBIT_1(sc, VGE_RXCFG, VGE_RXCFG_FIFO_THR|VGE_RXCFG_VTAGOPT);
   1814 	CSR_SETBIT_1(sc, VGE_RXCFG, VGE_RXFIFOTHR_128BYTES|VGE_VTAG_OPT2);
   1815 
   1816 	/* Set DMA burst length */
   1817 	CSR_CLRBIT_1(sc, VGE_DMACFG0, VGE_DMACFG0_BURSTLEN);
   1818 	CSR_SETBIT_1(sc, VGE_DMACFG0, VGE_DMABURST_128);
   1819 
   1820 	CSR_SETBIT_1(sc, VGE_TXCFG, VGE_TXCFG_ARB_PRIO|VGE_TXCFG_NONBLK);
   1821 
   1822 	/* Set collision backoff algorithm */
   1823 	CSR_CLRBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_CRANDOM|
   1824 	    VGE_CHIPCFG1_CAP|VGE_CHIPCFG1_MBA|VGE_CHIPCFG1_BAKOPT);
   1825 	CSR_SETBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_OFSET);
   1826 
   1827 	/* Disable LPSEL field in priority resolution */
   1828 	CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_LPSEL_DIS);
   1829 
   1830 	/*
   1831 	 * Load the addresses of the DMA queues into the chip.
   1832 	 * Note that we only use one transmit queue.
   1833 	 */
   1834 
   1835 	CSR_WRITE_4(sc, VGE_TXDESC_ADDR_LO0, VGE_ADDR_LO(VGE_CDTXADDR(sc, 0)));
   1836 	CSR_WRITE_2(sc, VGE_TXDESCNUM, VGE_NTXDESC - 1);
   1837 
   1838 	CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, VGE_ADDR_LO(VGE_CDRXADDR(sc, 0)));
   1839 	CSR_WRITE_2(sc, VGE_RXDESCNUM, VGE_NRXDESC - 1);
   1840 	CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, VGE_NRXDESC);
   1841 
   1842 	/* Enable and wake up the RX descriptor queue */
   1843 	CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
   1844 	CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
   1845 
   1846 	/* Enable the TX descriptor queue */
   1847 	CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_RUN0);
   1848 
   1849 	/* Set up the receive filter -- allow large frames for VLANs. */
   1850 	CSR_WRITE_1(sc, VGE_RXCTL, VGE_RXCTL_RX_UCAST|VGE_RXCTL_RX_GIANT);
   1851 
   1852 	/* If we want promiscuous mode, set the allframes bit. */
   1853 	if (ifp->if_flags & IFF_PROMISC) {
   1854 		CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_PROMISC);
   1855 	}
   1856 
   1857 	/* Set capture broadcast bit to capture broadcast frames. */
   1858 	if (ifp->if_flags & IFF_BROADCAST) {
   1859 		CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_BCAST);
   1860 	}
   1861 
   1862 	/* Set multicast bit to capture multicast frames. */
   1863 	if (ifp->if_flags & IFF_MULTICAST) {
   1864 		CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_MCAST);
   1865 	}
   1866 
   1867 	/* Init the cam filter. */
   1868 	vge_cam_clear(sc);
   1869 
   1870 	/* Init the multicast filter. */
   1871 	vge_setmulti(sc);
   1872 
   1873 	/* Enable flow control */
   1874 
   1875 	CSR_WRITE_1(sc, VGE_CRS2, 0x8B);
   1876 
   1877 	/* Enable jumbo frame reception (if desired) */
   1878 
   1879 	/* Start the MAC. */
   1880 	CSR_WRITE_1(sc, VGE_CRC0, VGE_CR0_STOP);
   1881 	CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_NOPOLL);
   1882 	CSR_WRITE_1(sc, VGE_CRS0,
   1883 	    VGE_CR0_TX_ENABLE|VGE_CR0_RX_ENABLE|VGE_CR0_START);
   1884 
   1885 	/*
   1886 	 * Configure one-shot timer for microsecond
   1887 	 * resulution and load it for 500 usecs.
   1888 	 */
   1889 	CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_TIMER0_RES);
   1890 	CSR_WRITE_2(sc, VGE_SSTIMER, 400);
   1891 
   1892 	/*
   1893 	 * Configure interrupt moderation for receive. Enable
   1894 	 * the holdoff counter and load it, and set the RX
   1895 	 * suppression count to the number of descriptors we
   1896 	 * want to allow before triggering an interrupt.
   1897 	 * The holdoff timer is in units of 20 usecs.
   1898 	 */
   1899 
   1900 #ifdef notyet
   1901 	CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_TXINTSUP_DISABLE);
   1902 	/* Select the interrupt holdoff timer page. */
   1903 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
   1904 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_INTHLDOFF);
   1905 	CSR_WRITE_1(sc, VGE_INTHOLDOFF, 10); /* ~200 usecs */
   1906 
   1907 	/* Enable use of the holdoff timer. */
   1908 	CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_HOLDOFF);
   1909 	CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_SC_RELOAD);
   1910 
   1911 	/* Select the RX suppression threshold page. */
   1912 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
   1913 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_RXSUPPTHR);
   1914 	CSR_WRITE_1(sc, VGE_RXSUPPTHR, 64); /* interrupt after 64 packets */
   1915 
   1916 	/* Restore the page select bits. */
   1917 	CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
   1918 	CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
   1919 #endif
   1920 
   1921 #ifdef DEVICE_POLLING
   1922 	/*
   1923 	 * Disable interrupts if we are polling.
   1924 	 */
   1925 	if (ifp->if_flags & IFF_POLLING) {
   1926 		CSR_WRITE_4(sc, VGE_IMR, 0);
   1927 		CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
   1928 	} else	/* otherwise ... */
   1929 #endif /* DEVICE_POLLING */
   1930 	{
   1931 	/*
   1932 	 * Enable interrupts.
   1933 	 */
   1934 		CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
   1935 		CSR_WRITE_4(sc, VGE_ISR, 0);
   1936 		CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
   1937 	}
   1938 
   1939 	mii_mediachg(&sc->sc_mii);
   1940 
   1941 	ifp->if_flags |= IFF_RUNNING;
   1942 	ifp->if_flags &= ~IFF_OACTIVE;
   1943 
   1944 	sc->sc_if_flags = 0;
   1945 	sc->sc_link = 0;
   1946 
   1947 	callout_schedule(&sc->sc_timeout, hz);
   1948 
   1949 	return 0;
   1950 }
   1951 
   1952 /*
   1953  * Set media options.
   1954  */
   1955 static int
   1956 vge_ifmedia_upd(struct ifnet *ifp)
   1957 {
   1958 	struct vge_softc *sc;
   1959 
   1960 	sc = ifp->if_softc;
   1961 	mii_mediachg(&sc->sc_mii);
   1962 
   1963 	return 0;
   1964 }
   1965 
   1966 /*
   1967  * Report current media status.
   1968  */
   1969 static void
   1970 vge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
   1971 {
   1972 	struct vge_softc *sc;
   1973 	struct mii_data *mii;
   1974 
   1975 	sc = ifp->if_softc;
   1976 	mii = &sc->sc_mii;
   1977 
   1978 	mii_pollstat(mii);
   1979 	ifmr->ifm_active = mii->mii_media_active;
   1980 	ifmr->ifm_status = mii->mii_media_status;
   1981 }
   1982 
   1983 static void
   1984 vge_miibus_statchg(struct device *self)
   1985 {
   1986 	struct vge_softc *sc;
   1987 	struct mii_data *mii;
   1988 	struct ifmedia_entry *ife;
   1989 
   1990 	sc = (void *)self;
   1991 	mii = &sc->sc_mii;
   1992 	ife = mii->mii_media.ifm_cur;
   1993 	/*
   1994 	 * If the user manually selects a media mode, we need to turn
   1995 	 * on the forced MAC mode bit in the DIAGCTL register. If the
   1996 	 * user happens to choose a full duplex mode, we also need to
   1997 	 * set the 'force full duplex' bit. This applies only to
   1998 	 * 10Mbps and 100Mbps speeds. In autoselect mode, forced MAC
   1999 	 * mode is disabled, and in 1000baseT mode, full duplex is
   2000 	 * always implied, so we turn on the forced mode bit but leave
   2001 	 * the FDX bit cleared.
   2002 	 */
   2003 
   2004 	switch (IFM_SUBTYPE(ife->ifm_media)) {
   2005 	case IFM_AUTO:
   2006 		CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
   2007 		CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
   2008 		break;
   2009 	case IFM_1000_T:
   2010 		CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
   2011 		CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
   2012 		break;
   2013 	case IFM_100_TX:
   2014 	case IFM_10_T:
   2015 		CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
   2016 		if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
   2017 			CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
   2018 		} else {
   2019 			CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
   2020 		}
   2021 		break;
   2022 	default:
   2023 		aprint_error("%s: unknown media type: %x\n",
   2024 		    sc->sc_dev.dv_xname,
   2025 		    IFM_SUBTYPE(ife->ifm_media));
   2026 		break;
   2027 	}
   2028 }
   2029 
   2030 static int
   2031 vge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
   2032 {
   2033 	struct vge_softc *sc;
   2034 	struct ifreq *ifr;
   2035 	struct mii_data *mii;
   2036 	int s, error;
   2037 
   2038 	sc = ifp->if_softc;
   2039 	ifr = (struct ifreq *)data;
   2040 	error = 0;
   2041 
   2042 	s = splnet();
   2043 
   2044 	switch (command) {
   2045 	case SIOCSIFMTU:
   2046 		if (ifr->ifr_mtu > VGE_JUMBO_MTU)
   2047 			error = EINVAL;
   2048 		ifp->if_mtu = ifr->ifr_mtu;
   2049 		break;
   2050 	case SIOCSIFFLAGS:
   2051 		if (ifp->if_flags & IFF_UP) {
   2052 			if (ifp->if_flags & IFF_RUNNING &&
   2053 			    ifp->if_flags & IFF_PROMISC &&
   2054 			    !(sc->sc_if_flags & IFF_PROMISC)) {
   2055 				CSR_SETBIT_1(sc, VGE_RXCTL,
   2056 				    VGE_RXCTL_RX_PROMISC);
   2057 				vge_setmulti(sc);
   2058 			} else if (ifp->if_flags & IFF_RUNNING &&
   2059 			    !(ifp->if_flags & IFF_PROMISC) &&
   2060 			    sc->sc_if_flags & IFF_PROMISC) {
   2061 				CSR_CLRBIT_1(sc, VGE_RXCTL,
   2062 				    VGE_RXCTL_RX_PROMISC);
   2063 				vge_setmulti(sc);
   2064                         } else
   2065 				vge_init(ifp);
   2066 		} else {
   2067 			if (ifp->if_flags & IFF_RUNNING)
   2068 				vge_stop(sc);
   2069 		}
   2070 		sc->sc_if_flags = ifp->if_flags;
   2071 		break;
   2072 	case SIOCADDMULTI:
   2073 	case SIOCDELMULTI:
   2074 		error = (command == SIOCADDMULTI) ?
   2075 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   2076 		    ether_delmulti(ifr, &sc->sc_ethercom);
   2077 
   2078 		if (error == ENETRESET) {
   2079 			/*
   2080 			 * Multicast list has changed; set the hardware filter
   2081 			 * accordingly.
   2082 			 */
   2083 			if (ifp->if_flags & IFF_RUNNING)
   2084 				vge_setmulti(sc);
   2085 			error = 0;
   2086 		}
   2087 		break;
   2088 	case SIOCGIFMEDIA:
   2089 	case SIOCSIFMEDIA:
   2090 		mii = &sc->sc_mii;
   2091 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
   2092 		break;
   2093 	default:
   2094 		error = ether_ioctl(ifp, command, data);
   2095 		break;
   2096 	}
   2097 
   2098 	splx(s);
   2099 	return error;
   2100 }
   2101 
   2102 static void
   2103 vge_watchdog(struct ifnet *ifp)
   2104 {
   2105 	struct vge_softc *sc;
   2106 	int s;
   2107 
   2108 	sc = ifp->if_softc;
   2109 	s = splnet();
   2110 	aprint_error("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
   2111 	ifp->if_oerrors++;
   2112 
   2113 	vge_txeof(sc);
   2114 	vge_rxeof(sc);
   2115 
   2116 	vge_init(ifp);
   2117 
   2118 	splx(s);
   2119 }
   2120 
   2121 /*
   2122  * Stop the adapter and free any mbufs allocated to the
   2123  * RX and TX lists.
   2124  */
   2125 static void
   2126 vge_stop(struct vge_softc *sc)
   2127 {
   2128 	struct ifnet *ifp;
   2129 	struct vge_txsoft *txs;
   2130 	struct vge_rxsoft *rxs;
   2131 	int i, s;
   2132 
   2133 	ifp = &sc->sc_ethercom.ec_if;
   2134 
   2135 	s = splnet();
   2136 	ifp->if_timer = 0;
   2137 
   2138 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2139 #ifdef DEVICE_POLLING
   2140 	ether_poll_deregister(ifp);
   2141 #endif /* DEVICE_POLLING */
   2142 
   2143 	CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
   2144 	CSR_WRITE_1(sc, VGE_CRS0, VGE_CR0_STOP);
   2145 	CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
   2146 	CSR_WRITE_2(sc, VGE_TXQCSRC, 0xFFFF);
   2147 	CSR_WRITE_1(sc, VGE_RXQCSRC, 0xFF);
   2148 	CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 0);
   2149 
   2150 	if (sc->sc_rx_mhead != NULL) {
   2151 		m_freem(sc->sc_rx_mhead);
   2152 		sc->sc_rx_mhead = sc->sc_rx_mtail = NULL;
   2153 	}
   2154 
   2155 	/* Free the TX list buffers. */
   2156 
   2157 	for (i = 0; i < VGE_NTXDESC; i++) {
   2158 		txs = &sc->sc_txsoft[i];
   2159 		if (txs->txs_mbuf != NULL) {
   2160 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   2161 			m_freem(txs->txs_mbuf);
   2162 			txs->txs_mbuf = NULL;
   2163 		}
   2164 	}
   2165 
   2166 	/* Free the RX list buffers. */
   2167 
   2168 	for (i = 0; i < VGE_NRXDESC; i++) {
   2169 		rxs = &sc->sc_rxsoft[i];
   2170 		if (rxs->rxs_mbuf != NULL) {
   2171 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   2172 			m_freem(rxs->rxs_mbuf);
   2173 			rxs->rxs_mbuf = NULL;
   2174 		}
   2175 	}
   2176 
   2177 	splx(s);
   2178 }
   2179 
   2180 #if VGE_POWER_MANAGEMENT
   2181 /*
   2182  * Device suspend routine.  Stop the interface and save some PCI
   2183  * settings in case the BIOS doesn't restore them properly on
   2184  * resume.
   2185  */
   2186 static int
   2187 vge_suspend(struct device *dev)
   2188 {
   2189 	struct vge_softc *sc;
   2190 	int i;
   2191 
   2192 	sc = device_get_softc(dev);
   2193 
   2194 	vge_stop(sc);
   2195 
   2196         for (i = 0; i < 5; i++)
   2197 		sc->sc_saved_maps[i] =
   2198 		    pci_read_config(dev, PCIR_MAPS + i * 4, 4);
   2199 	sc->sc_saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
   2200 	sc->sc_saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
   2201 	sc->sc_saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
   2202 	sc->sc_saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
   2203 
   2204 	sc->suspended = 1;
   2205 
   2206 	return 0;
   2207 }
   2208 
   2209 /*
   2210  * Device resume routine.  Restore some PCI settings in case the BIOS
   2211  * doesn't, re-enable busmastering, and restart the interface if
   2212  * appropriate.
   2213  */
   2214 static int
   2215 vge_resume(struct device *dev)
   2216 {
   2217 	struct vge_softc *sc;
   2218 	struct ifnet *ifp;
   2219 	int i;
   2220 
   2221 	sc = (void *)dev;
   2222 	ifp = &sc->sc_ethercom.ec_if;
   2223 
   2224         /* better way to do this? */
   2225 	for (i = 0; i < 5; i++)
   2226 		pci_write_config(dev, PCIR_MAPS + i * 4,
   2227 		    sc->sc_saved_maps[i], 4);
   2228 	pci_write_config(dev, PCIR_BIOS, sc->sc_saved_biosaddr, 4);
   2229 	pci_write_config(dev, PCIR_INTLINE, sc->sc_saved_intline, 1);
   2230 	pci_write_config(dev, PCIR_CACHELNSZ, sc->sc_saved_cachelnsz, 1);
   2231 	pci_write_config(dev, PCIR_LATTIMER, sc->sc_saved_lattimer, 1);
   2232 
   2233 	/* reenable busmastering */
   2234 	pci_enable_busmaster(dev);
   2235 	pci_enable_io(dev, SYS_RES_MEMORY);
   2236 
   2237 	/* reinitialize interface if necessary */
   2238 	if (ifp->if_flags & IFF_UP)
   2239 		vge_init(sc);
   2240 
   2241 	sc->suspended = 0;
   2242 
   2243 	return 0;
   2244 }
   2245 #endif
   2246 
   2247 /*
   2248  * Stop all chip I/O so that the kernel's probe routines don't
   2249  * get confused by errant DMAs when rebooting.
   2250  */
   2251 static void
   2252 vge_shutdown(void *arg)
   2253 {
   2254 	struct vge_softc *sc;
   2255 
   2256 	sc = arg;
   2257 	vge_stop(sc);
   2258 }
   2259