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