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