Home | History | Annotate | Line # | Download | only in pci
if_stge.c revision 1.14
      1 /*	$NetBSD: if_stge.c,v 1.14 2002/12/23 02:58:37 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Device driver for the Sundance Tech. TC9021 10/100/1000
     41  * Ethernet controller.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: if_stge.c,v 1.14 2002/12/23 02:58:37 tsutsui Exp $");
     46 
     47 #include "bpfilter.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/callout.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/malloc.h>
     54 #include <sys/kernel.h>
     55 #include <sys/socket.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/errno.h>
     58 #include <sys/device.h>
     59 #include <sys/queue.h>
     60 
     61 #include <uvm/uvm_extern.h>		/* for PAGE_SIZE */
     62 
     63 #include <net/if.h>
     64 #include <net/if_dl.h>
     65 #include <net/if_media.h>
     66 #include <net/if_ether.h>
     67 
     68 #if NBPFILTER > 0
     69 #include <net/bpf.h>
     70 #endif
     71 
     72 #include <machine/bus.h>
     73 #include <machine/intr.h>
     74 
     75 #include <dev/mii/mii.h>
     76 #include <dev/mii/miivar.h>
     77 #include <dev/mii/mii_bitbang.h>
     78 
     79 #include <dev/pci/pcireg.h>
     80 #include <dev/pci/pcivar.h>
     81 #include <dev/pci/pcidevs.h>
     82 
     83 #include <dev/pci/if_stgereg.h>
     84 
     85 /*
     86  * Transmit descriptor list size.
     87  */
     88 #define	STGE_NTXDESC		256
     89 #define	STGE_NTXDESC_MASK	(STGE_NTXDESC - 1)
     90 #define	STGE_NEXTTX(x)		(((x) + 1) & STGE_NTXDESC_MASK)
     91 
     92 /*
     93  * Receive descriptor list size.
     94  */
     95 #define	STGE_NRXDESC		256
     96 #define	STGE_NRXDESC_MASK	(STGE_NRXDESC - 1)
     97 #define	STGE_NEXTRX(x)		(((x) + 1) & STGE_NRXDESC_MASK)
     98 
     99 /*
    100  * Only interrupt every N frames.  Must be a power-of-two.
    101  */
    102 #define	STGE_TXINTR_SPACING	16
    103 #define	STGE_TXINTR_SPACING_MASK (STGE_TXINTR_SPACING - 1)
    104 
    105 /*
    106  * Control structures are DMA'd to the TC9021 chip.  We allocate them in
    107  * a single clump that maps to a single DMA segment to make several things
    108  * easier.
    109  */
    110 struct stge_control_data {
    111 	/*
    112 	 * The transmit descriptors.
    113 	 */
    114 	struct stge_tfd scd_txdescs[STGE_NTXDESC];
    115 
    116 	/*
    117 	 * The receive descriptors.
    118 	 */
    119 	struct stge_rfd scd_rxdescs[STGE_NRXDESC];
    120 };
    121 
    122 #define	STGE_CDOFF(x)	offsetof(struct stge_control_data, x)
    123 #define	STGE_CDTXOFF(x)	STGE_CDOFF(scd_txdescs[(x)])
    124 #define	STGE_CDRXOFF(x)	STGE_CDOFF(scd_rxdescs[(x)])
    125 
    126 /*
    127  * Software state for transmit and receive jobs.
    128  */
    129 struct stge_descsoft {
    130 	struct mbuf *ds_mbuf;		/* head of our mbuf chain */
    131 	bus_dmamap_t ds_dmamap;		/* our DMA map */
    132 };
    133 
    134 /*
    135  * Software state per device.
    136  */
    137 struct stge_softc {
    138 	struct device sc_dev;		/* generic device information */
    139 	bus_space_tag_t sc_st;		/* bus space tag */
    140 	bus_space_handle_t sc_sh;	/* bus space handle */
    141 	bus_dma_tag_t sc_dmat;		/* bus DMA tag */
    142 	struct ethercom sc_ethercom;	/* ethernet common data */
    143 	void *sc_sdhook;		/* shutdown hook */
    144 	int sc_rev;			/* silicon revision */
    145 
    146 	void *sc_ih;			/* interrupt cookie */
    147 
    148 	struct mii_data sc_mii;		/* MII/media information */
    149 
    150 	struct callout sc_tick_ch;	/* tick callout */
    151 
    152 	bus_dmamap_t sc_cddmamap;	/* control data DMA map */
    153 #define	sc_cddma	sc_cddmamap->dm_segs[0].ds_addr
    154 
    155 	/*
    156 	 * Software state for transmit and receive descriptors.
    157 	 */
    158 	struct stge_descsoft sc_txsoft[STGE_NTXDESC];
    159 	struct stge_descsoft sc_rxsoft[STGE_NRXDESC];
    160 
    161 	/*
    162 	 * Control data structures.
    163 	 */
    164 	struct stge_control_data *sc_control_data;
    165 #define	sc_txdescs	sc_control_data->scd_txdescs
    166 #define	sc_rxdescs	sc_control_data->scd_rxdescs
    167 
    168 #ifdef STGE_EVENT_COUNTERS
    169 	/*
    170 	 * Event counters.
    171 	 */
    172 	struct evcnt sc_ev_txstall;	/* Tx stalled */
    173 	struct evcnt sc_ev_txdmaintr;	/* Tx DMA interrupts */
    174 	struct evcnt sc_ev_txindintr;	/* Tx Indicate interrupts */
    175 	struct evcnt sc_ev_rxintr;	/* Rx interrupts */
    176 
    177 	struct evcnt sc_ev_txseg1;	/* Tx packets w/ 1 segment */
    178 	struct evcnt sc_ev_txseg2;	/* Tx packets w/ 2 segments */
    179 	struct evcnt sc_ev_txseg3;	/* Tx packets w/ 3 segments */
    180 	struct evcnt sc_ev_txseg4;	/* Tx packets w/ 4 segments */
    181 	struct evcnt sc_ev_txseg5;	/* Tx packets w/ 5 segments */
    182 	struct evcnt sc_ev_txsegmore;	/* Tx packets w/ more than 5 segments */
    183 	struct evcnt sc_ev_txcopy;	/* Tx packets that we had to copy */
    184 
    185 	struct evcnt sc_ev_rxipsum;	/* IP checksums checked in-bound */
    186 	struct evcnt sc_ev_rxtcpsum;	/* TCP checksums checked in-bound */
    187 	struct evcnt sc_ev_rxudpsum;	/* UDP checksums checked in-bound */
    188 
    189 	struct evcnt sc_ev_txipsum;	/* IP checksums comp. out-bound */
    190 	struct evcnt sc_ev_txtcpsum;	/* TCP checksums comp. out-bound */
    191 	struct evcnt sc_ev_txudpsum;	/* UDP checksums comp. out-bound */
    192 #endif /* STGE_EVENT_COUNTERS */
    193 
    194 	int	sc_txpending;		/* number of Tx requests pending */
    195 	int	sc_txdirty;		/* first dirty Tx descriptor */
    196 	int	sc_txlast;		/* last used Tx descriptor */
    197 
    198 	int	sc_rxptr;		/* next ready Rx descriptor/descsoft */
    199 	int	sc_rxdiscard;
    200 	int	sc_rxlen;
    201 	struct mbuf *sc_rxhead;
    202 	struct mbuf *sc_rxtail;
    203 	struct mbuf **sc_rxtailp;
    204 
    205 	int	sc_txthresh;		/* Tx threshold */
    206 	int	sc_usefiber;		/* if we're fiber */
    207 	uint32_t sc_DMACtrl;		/* prototype DMACtrl register */
    208 	uint32_t sc_MACCtrl;		/* prototype MacCtrl register */
    209 	uint16_t sc_IntEnable;		/* prototype IntEnable register */
    210 	uint16_t sc_ReceiveMode;	/* prototype ReceiveMode register */
    211 	uint8_t sc_PhyCtrl;		/* prototype PhyCtrl register */
    212 };
    213 
    214 #define	STGE_RXCHAIN_RESET(sc)						\
    215 do {									\
    216 	(sc)->sc_rxtailp = &(sc)->sc_rxhead;				\
    217 	*(sc)->sc_rxtailp = NULL;					\
    218 	(sc)->sc_rxlen = 0;						\
    219 } while (/*CONSTCOND*/0)
    220 
    221 #define	STGE_RXCHAIN_LINK(sc, m)					\
    222 do {									\
    223 	*(sc)->sc_rxtailp = (sc)->sc_rxtail = (m);			\
    224 	(sc)->sc_rxtailp = &(m)->m_next;				\
    225 } while (/*CONSTCOND*/0)
    226 
    227 #ifdef STGE_EVENT_COUNTERS
    228 #define	STGE_EVCNT_INCR(ev)	(ev)->ev_count++
    229 #else
    230 #define	STGE_EVCNT_INCR(ev)	/* nothing */
    231 #endif
    232 
    233 #define	STGE_CDTXADDR(sc, x)	((sc)->sc_cddma + STGE_CDTXOFF((x)))
    234 #define	STGE_CDRXADDR(sc, x)	((sc)->sc_cddma + STGE_CDRXOFF((x)))
    235 
    236 #define	STGE_CDTXSYNC(sc, x, ops)					\
    237 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    238 	    STGE_CDTXOFF((x)), sizeof(struct stge_tfd), (ops))
    239 
    240 #define	STGE_CDRXSYNC(sc, x, ops)					\
    241 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    242 	    STGE_CDRXOFF((x)), sizeof(struct stge_rfd), (ops))
    243 
    244 #define	STGE_INIT_RXDESC(sc, x)						\
    245 do {									\
    246 	struct stge_descsoft *__ds = &(sc)->sc_rxsoft[(x)];		\
    247 	struct stge_rfd *__rfd = &(sc)->sc_rxdescs[(x)];		\
    248 									\
    249 	/*								\
    250 	 * Note: We scoot the packet forward 2 bytes in the buffer	\
    251 	 * so that the payload after the Ethernet header is aligned	\
    252 	 * to a 4-byte boundary.					\
    253 	 */								\
    254 	__rfd->rfd_frag.frag_word0 =					\
    255 	    htole64(FRAG_ADDR(__ds->ds_dmamap->dm_segs[0].ds_addr + 2) |\
    256 	    FRAG_LEN(MCLBYTES - 2));					\
    257 	__rfd->rfd_next =						\
    258 	    htole64((uint64_t)STGE_CDRXADDR((sc), STGE_NEXTRX((x))));	\
    259 	__rfd->rfd_status = 0;						\
    260 	STGE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
    261 } while (/*CONSTCOND*/0)
    262 
    263 #define STGE_TIMEOUT 1000
    264 
    265 void	stge_start(struct ifnet *);
    266 void	stge_watchdog(struct ifnet *);
    267 int	stge_ioctl(struct ifnet *, u_long, caddr_t);
    268 int	stge_init(struct ifnet *);
    269 void	stge_stop(struct ifnet *, int);
    270 
    271 void	stge_shutdown(void *);
    272 
    273 void	stge_reset(struct stge_softc *);
    274 void	stge_rxdrain(struct stge_softc *);
    275 int	stge_add_rxbuf(struct stge_softc *, int);
    276 #if 0
    277 void	stge_read_eeprom(struct stge_softc *, int, uint16_t *);
    278 #endif
    279 void	stge_tick(void *);
    280 
    281 void	stge_stats_update(struct stge_softc *);
    282 
    283 void	stge_set_filter(struct stge_softc *);
    284 
    285 int	stge_intr(void *);
    286 void	stge_txintr(struct stge_softc *);
    287 void	stge_rxintr(struct stge_softc *);
    288 
    289 int	stge_mii_readreg(struct device *, int, int);
    290 void	stge_mii_writereg(struct device *, int, int, int);
    291 void	stge_mii_statchg(struct device *);
    292 
    293 int	stge_mediachange(struct ifnet *);
    294 void	stge_mediastatus(struct ifnet *, struct ifmediareq *);
    295 
    296 int	stge_match(struct device *, struct cfdata *, void *);
    297 void	stge_attach(struct device *, struct device *, void *);
    298 
    299 int	stge_copy_small = 0;
    300 
    301 CFATTACH_DECL(stge, sizeof(struct stge_softc),
    302     stge_match, stge_attach, NULL, NULL);
    303 
    304 uint32_t stge_mii_bitbang_read(struct device *);
    305 void	stge_mii_bitbang_write(struct device *, uint32_t);
    306 
    307 const struct mii_bitbang_ops stge_mii_bitbang_ops = {
    308 	stge_mii_bitbang_read,
    309 	stge_mii_bitbang_write,
    310 	{
    311 		PC_MgmtData,		/* MII_BIT_MDO */
    312 		PC_MgmtData,		/* MII_BIT_MDI */
    313 		PC_MgmtClk,		/* MII_BIT_MDC */
    314 		PC_MgmtDir,		/* MII_BIT_DIR_HOST_PHY */
    315 		0,			/* MII_BIT_DIR_PHY_HOST */
    316 	}
    317 };
    318 
    319 /*
    320  * Devices supported by this driver.
    321  */
    322 const struct stge_product {
    323 	pci_vendor_id_t		stge_vendor;
    324 	pci_product_id_t	stge_product;
    325 	const char		*stge_name;
    326 } stge_products[] = {
    327 	{ PCI_VENDOR_SUNDANCETI,	PCI_PRODUCT_SUNDANCETI_ST2021,
    328 	  "Sundance ST-2021 Gigabit Ethernet" },
    329 
    330 	{ PCI_VENDOR_TAMARACK,		PCI_PRODUCT_TAMARACK_TC9021,
    331 	  "Tamarack TC9021 Gigabit Ethernet" },
    332 
    333 	{ PCI_VENDOR_TAMARACK,		PCI_PRODUCT_TAMARACK_TC9021_ALT,
    334 	  "Tamarack TC9021 Gigabit Ethernet" },
    335 
    336 	/*
    337 	 * The Sundance sample boards use the Sundance vendor ID,
    338 	 * but the Tamarack product ID.
    339 	 */
    340 	{ PCI_VENDOR_SUNDANCETI,	PCI_PRODUCT_TAMARACK_TC9021,
    341 	  "Sundance TC9021 Gigabit Ethernet" },
    342 
    343 	{ PCI_VENDOR_SUNDANCETI,	PCI_PRODUCT_TAMARACK_TC9021_ALT,
    344 	  "Sundance TC9021 Gigabit Ethernet" },
    345 
    346 	{ PCI_VENDOR_DLINK,		PCI_PRODUCT_DLINK_DL4000,
    347 	  "D-Link DL-4000 Gigabit Ethernet" },
    348 
    349 	{ PCI_VENDOR_ANTARES,		PCI_PRODUCT_ANTARES_TC9021,
    350 	  "Antares Gigabit Ethernet" },
    351 
    352 	{ 0,				0,
    353 	  NULL },
    354 };
    355 
    356 static const struct stge_product *
    357 stge_lookup(const struct pci_attach_args *pa)
    358 {
    359 	const struct stge_product *sp;
    360 
    361 	for (sp = stge_products; sp->stge_name != NULL; sp++) {
    362 		if (PCI_VENDOR(pa->pa_id) == sp->stge_vendor &&
    363 		    PCI_PRODUCT(pa->pa_id) == sp->stge_product)
    364 			return (sp);
    365 	}
    366 	return (NULL);
    367 }
    368 
    369 int
    370 stge_match(struct device *parent, struct cfdata *cf, void *aux)
    371 {
    372 	struct pci_attach_args *pa = aux;
    373 
    374 	if (stge_lookup(pa) != NULL)
    375 		return (1);
    376 
    377 	return (0);
    378 }
    379 
    380 void
    381 stge_attach(struct device *parent, struct device *self, void *aux)
    382 {
    383 	struct stge_softc *sc = (struct stge_softc *) self;
    384 	struct pci_attach_args *pa = aux;
    385 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    386 	pci_chipset_tag_t pc = pa->pa_pc;
    387 	pci_intr_handle_t ih;
    388 	const char *intrstr = NULL;
    389 	bus_space_tag_t iot, memt;
    390 	bus_space_handle_t ioh, memh;
    391 	bus_dma_segment_t seg;
    392 	int ioh_valid, memh_valid;
    393 	int i, rseg, error;
    394 	const struct stge_product *sp;
    395 	pcireg_t pmode;
    396 	uint8_t enaddr[ETHER_ADDR_LEN];
    397 	int pmreg;
    398 
    399 	callout_init(&sc->sc_tick_ch);
    400 
    401 	sp = stge_lookup(pa);
    402 	if (sp == NULL) {
    403 		printf("\n");
    404 		panic("ste_attach: impossible");
    405 	}
    406 
    407 	sc->sc_rev = PCI_REVISION(pa->pa_class);
    408 
    409 	printf(": %s, rev. %d\n", sp->stge_name, sc->sc_rev);
    410 
    411 	/*
    412 	 * Map the device.
    413 	 */
    414 	ioh_valid = (pci_mapreg_map(pa, STGE_PCI_IOBA,
    415 	    PCI_MAPREG_TYPE_IO, 0,
    416 	    &iot, &ioh, NULL, NULL) == 0);
    417 	memh_valid = (pci_mapreg_map(pa, STGE_PCI_MMBA,
    418 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    419 	    &memt, &memh, NULL, NULL) == 0);
    420 
    421 	if (memh_valid) {
    422 		sc->sc_st = memt;
    423 		sc->sc_sh = memh;
    424 	} else if (ioh_valid) {
    425 		sc->sc_st = iot;
    426 		sc->sc_sh = ioh;
    427 	} else {
    428 		printf("%s: unable to map device registers\n",
    429 		    sc->sc_dev.dv_xname);
    430 		return;
    431 	}
    432 
    433 	sc->sc_dmat = pa->pa_dmat;
    434 
    435 	/* Enable bus mastering. */
    436 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    437 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    438 	    PCI_COMMAND_MASTER_ENABLE);
    439 
    440 	/* Get it out of power save mode if needed. */
    441 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    442 		pmode = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR) &
    443 		    PCI_PMCSR_STATE_MASK;
    444 		if (pmode == PCI_PMCSR_STATE_D3) {
    445 			/*
    446 			 * The card has lost all configuration data in
    447 			 * this state, so punt.
    448 			 */
    449 			printf("%s: unable to wake up from power state D3\n",
    450 			    sc->sc_dev.dv_xname);
    451 			return;
    452 		}
    453 		if (pmode != 0) {
    454 			printf("%s: waking up from power state D%d\n",
    455 			    sc->sc_dev.dv_xname, pmode);
    456 			pci_conf_write(pc, pa->pa_tag, pmreg + PCI_PMCSR,
    457 			    PCI_PMCSR_STATE_D0);
    458 		}
    459 	}
    460 
    461 	/*
    462 	 * Map and establish our interrupt.
    463 	 */
    464 	if (pci_intr_map(pa, &ih)) {
    465 		printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
    466 		return;
    467 	}
    468 	intrstr = pci_intr_string(pc, ih);
    469 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, stge_intr, sc);
    470 	if (sc->sc_ih == NULL) {
    471 		printf("%s: unable to establish interrupt",
    472 		    sc->sc_dev.dv_xname);
    473 		if (intrstr != NULL)
    474 			printf(" at %s", intrstr);
    475 		printf("\n");
    476 		return;
    477 	}
    478 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    479 
    480 	/*
    481 	 * Allocate the control data structures, and create and load the
    482 	 * DMA map for it.
    483 	 */
    484 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    485 	    sizeof(struct stge_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
    486 	    0)) != 0) {
    487 		printf("%s: unable to allocate control data, error = %d\n",
    488 		    sc->sc_dev.dv_xname, error);
    489 		goto fail_0;
    490 	}
    491 
    492 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    493 	    sizeof(struct stge_control_data), (caddr_t *)&sc->sc_control_data,
    494 	    BUS_DMA_COHERENT)) != 0) {
    495 		printf("%s: unable to map control data, error = %d\n",
    496 		    sc->sc_dev.dv_xname, error);
    497 		goto fail_1;
    498 	}
    499 
    500 	if ((error = bus_dmamap_create(sc->sc_dmat,
    501 	    sizeof(struct stge_control_data), 1,
    502 	    sizeof(struct stge_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    503 		printf("%s: unable to create control data DMA map, "
    504 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    505 		goto fail_2;
    506 	}
    507 
    508 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    509 	    sc->sc_control_data, sizeof(struct stge_control_data), NULL,
    510 	    0)) != 0) {
    511 		printf("%s: unable to load control data DMA map, error = %d\n",
    512 		    sc->sc_dev.dv_xname, error);
    513 		goto fail_3;
    514 	}
    515 
    516 	/*
    517 	 * Create the transmit buffer DMA maps.  Note that rev B.3
    518 	 * and earlier seem to have a bug regarding multi-fragment
    519 	 * packets.  We need to limit the number of Tx segments on
    520 	 * such chips to 1.
    521 	 */
    522 	for (i = 0; i < STGE_NTXDESC; i++) {
    523 		if ((error = bus_dmamap_create(sc->sc_dmat,
    524 		    ETHER_MAX_LEN_JUMBO, STGE_NTXFRAGS, MCLBYTES, 0, 0,
    525 		    &sc->sc_txsoft[i].ds_dmamap)) != 0) {
    526 			printf("%s: unable to create tx DMA map %d, "
    527 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    528 			goto fail_4;
    529 		}
    530 	}
    531 
    532 	/*
    533 	 * Create the receive buffer DMA maps.
    534 	 */
    535 	for (i = 0; i < STGE_NRXDESC; i++) {
    536 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    537 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].ds_dmamap)) != 0) {
    538 			printf("%s: unable to create rx DMA map %d, "
    539 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    540 			goto fail_5;
    541 		}
    542 		sc->sc_rxsoft[i].ds_mbuf = NULL;
    543 	}
    544 
    545 	/*
    546 	 * Determine if we're copper or fiber.  It affects how we
    547 	 * reset the card.
    548 	 */
    549 	if (bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl) &
    550 	    AC_PhyMedia)
    551 		sc->sc_usefiber = 1;
    552 	else
    553 		sc->sc_usefiber = 0;
    554 
    555 	/*
    556 	 * Reset the chip to a known state.
    557 	 */
    558 	stge_reset(sc);
    559 
    560 	/*
    561 	 * Reading the station address from the EEPROM doesn't seem
    562 	 * to work, at least on my sample boards.  Instread, since
    563 	 * the reset sequence does AutoInit, read it from the station
    564 	 * address registers.
    565 	 */
    566 	enaddr[0] = bus_space_read_2(sc->sc_st, sc->sc_sh,
    567 	    STGE_StationAddress0) & 0xff;
    568 	enaddr[1] = bus_space_read_2(sc->sc_st, sc->sc_sh,
    569 	    STGE_StationAddress0) >> 8;
    570 	enaddr[2] = bus_space_read_2(sc->sc_st, sc->sc_sh,
    571 	    STGE_StationAddress1) & 0xff;
    572 	enaddr[3] = bus_space_read_2(sc->sc_st, sc->sc_sh,
    573 	    STGE_StationAddress1) >> 8;
    574 	enaddr[4] = bus_space_read_2(sc->sc_st, sc->sc_sh,
    575 	    STGE_StationAddress2) & 0xff;
    576 	enaddr[5] = bus_space_read_2(sc->sc_st, sc->sc_sh,
    577 	    STGE_StationAddress2) >> 8;
    578 
    579 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    580 	    ether_sprintf(enaddr));
    581 
    582 	/*
    583 	 * Read some important bits from the PhyCtrl register.
    584 	 */
    585 	sc->sc_PhyCtrl = bus_space_read_1(sc->sc_st, sc->sc_sh,
    586 	    STGE_PhyCtrl) & (PC_PhyDuplexPolarity | PC_PhyLnkPolarity);
    587 
    588 	/*
    589 	 * Initialize our media structures and probe the MII.
    590 	 */
    591 	sc->sc_mii.mii_ifp = ifp;
    592 	sc->sc_mii.mii_readreg = stge_mii_readreg;
    593 	sc->sc_mii.mii_writereg = stge_mii_writereg;
    594 	sc->sc_mii.mii_statchg = stge_mii_statchg;
    595 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, stge_mediachange,
    596 	    stge_mediastatus);
    597 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    598 	    MII_OFFSET_ANY, MIIF_DOPAUSE);
    599 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    600 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    601 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    602 	} else
    603 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    604 
    605 	ifp = &sc->sc_ethercom.ec_if;
    606 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    607 	ifp->if_softc = sc;
    608 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    609 	ifp->if_ioctl = stge_ioctl;
    610 	ifp->if_start = stge_start;
    611 	ifp->if_watchdog = stge_watchdog;
    612 	ifp->if_init = stge_init;
    613 	ifp->if_stop = stge_stop;
    614 	IFQ_SET_READY(&ifp->if_snd);
    615 
    616 	/*
    617 	 * The manual recommends disabling early transmit, so we
    618 	 * do.  It's disabled anyway, if using IP checksumming,
    619 	 * since the entire packet must be in the FIFO in order
    620 	 * for the chip to perform the checksum.
    621 	 */
    622 	sc->sc_txthresh = 0x0fff;
    623 
    624 	/*
    625 	 * Disable MWI if the PCI layer tells us to.
    626 	 */
    627 	sc->sc_DMACtrl = 0;
    628 	if ((pa->pa_flags & PCI_FLAGS_MWI_OKAY) == 0)
    629 		sc->sc_DMACtrl |= DMAC_MWIDisable;
    630 
    631 	/*
    632 	 * We can support 802.1Q VLAN-sized frames and jumbo
    633 	 * Ethernet frames.
    634 	 *
    635 	 * XXX Figure out how to do hw-assisted VLAN tagging in
    636 	 * XXX a reasonable way on this chip.
    637 	 */
    638 	sc->sc_ethercom.ec_capabilities |=
    639 	    ETHERCAP_VLAN_MTU /* XXX | ETHERCAP_JUMBO_MTU */;
    640 
    641 	/*
    642 	 * We can do IPv4/TCPv4/UDPv4 checksums in hardware.
    643 	 */
    644 	sc->sc_ethercom.ec_if.if_capabilities |= IFCAP_CSUM_IPv4 |
    645 	    IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
    646 
    647 	/*
    648 	 * Attach the interface.
    649 	 */
    650 	if_attach(ifp);
    651 	ether_ifattach(ifp, enaddr);
    652 
    653 #ifdef STGE_EVENT_COUNTERS
    654 	/*
    655 	 * Attach event counters.
    656 	 */
    657 	evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
    658 	    NULL, sc->sc_dev.dv_xname, "txstall");
    659 	evcnt_attach_dynamic(&sc->sc_ev_txdmaintr, EVCNT_TYPE_INTR,
    660 	    NULL, sc->sc_dev.dv_xname, "txdmaintr");
    661 	evcnt_attach_dynamic(&sc->sc_ev_txindintr, EVCNT_TYPE_INTR,
    662 	    NULL, sc->sc_dev.dv_xname, "txindintr");
    663 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
    664 	    NULL, sc->sc_dev.dv_xname, "rxintr");
    665 
    666 	evcnt_attach_dynamic(&sc->sc_ev_txseg1, EVCNT_TYPE_MISC,
    667 	    NULL, sc->sc_dev.dv_xname, "txseg1");
    668 	evcnt_attach_dynamic(&sc->sc_ev_txseg2, EVCNT_TYPE_MISC,
    669 	    NULL, sc->sc_dev.dv_xname, "txseg2");
    670 	evcnt_attach_dynamic(&sc->sc_ev_txseg3, EVCNT_TYPE_MISC,
    671 	    NULL, sc->sc_dev.dv_xname, "txseg3");
    672 	evcnt_attach_dynamic(&sc->sc_ev_txseg4, EVCNT_TYPE_MISC,
    673 	    NULL, sc->sc_dev.dv_xname, "txseg4");
    674 	evcnt_attach_dynamic(&sc->sc_ev_txseg5, EVCNT_TYPE_MISC,
    675 	    NULL, sc->sc_dev.dv_xname, "txseg5");
    676 	evcnt_attach_dynamic(&sc->sc_ev_txsegmore, EVCNT_TYPE_MISC,
    677 	    NULL, sc->sc_dev.dv_xname, "txsegmore");
    678 	evcnt_attach_dynamic(&sc->sc_ev_txcopy, EVCNT_TYPE_MISC,
    679 	    NULL, sc->sc_dev.dv_xname, "txcopy");
    680 
    681 	evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC,
    682 	    NULL, sc->sc_dev.dv_xname, "rxipsum");
    683 	evcnt_attach_dynamic(&sc->sc_ev_rxtcpsum, EVCNT_TYPE_MISC,
    684 	    NULL, sc->sc_dev.dv_xname, "rxtcpsum");
    685 	evcnt_attach_dynamic(&sc->sc_ev_rxudpsum, EVCNT_TYPE_MISC,
    686 	    NULL, sc->sc_dev.dv_xname, "rxudpsum");
    687 	evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC,
    688 	    NULL, sc->sc_dev.dv_xname, "txipsum");
    689 	evcnt_attach_dynamic(&sc->sc_ev_txtcpsum, EVCNT_TYPE_MISC,
    690 	    NULL, sc->sc_dev.dv_xname, "txtcpsum");
    691 	evcnt_attach_dynamic(&sc->sc_ev_txudpsum, EVCNT_TYPE_MISC,
    692 	    NULL, sc->sc_dev.dv_xname, "txudpsum");
    693 #endif /* STGE_EVENT_COUNTERS */
    694 
    695 	/*
    696 	 * Make sure the interface is shutdown during reboot.
    697 	 */
    698 	sc->sc_sdhook = shutdownhook_establish(stge_shutdown, sc);
    699 	if (sc->sc_sdhook == NULL)
    700 		printf("%s: WARNING: unable to establish shutdown hook\n",
    701 		    sc->sc_dev.dv_xname);
    702 	return;
    703 
    704 	/*
    705 	 * Free any resources we've allocated during the failed attach
    706 	 * attempt.  Do this in reverse order and fall through.
    707 	 */
    708  fail_5:
    709 	for (i = 0; i < STGE_NRXDESC; i++) {
    710 		if (sc->sc_rxsoft[i].ds_dmamap != NULL)
    711 			bus_dmamap_destroy(sc->sc_dmat,
    712 			    sc->sc_rxsoft[i].ds_dmamap);
    713 	}
    714  fail_4:
    715 	for (i = 0; i < STGE_NTXDESC; i++) {
    716 		if (sc->sc_txsoft[i].ds_dmamap != NULL)
    717 			bus_dmamap_destroy(sc->sc_dmat,
    718 			    sc->sc_txsoft[i].ds_dmamap);
    719 	}
    720 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    721  fail_3:
    722 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    723  fail_2:
    724 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
    725 	    sizeof(struct stge_control_data));
    726  fail_1:
    727 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    728  fail_0:
    729 	return;
    730 }
    731 
    732 /*
    733  * stge_shutdown:
    734  *
    735  *	Make sure the interface is stopped at reboot time.
    736  */
    737 void
    738 stge_shutdown(void *arg)
    739 {
    740 	struct stge_softc *sc = arg;
    741 
    742 	stge_stop(&sc->sc_ethercom.ec_if, 1);
    743 }
    744 
    745 static void
    746 stge_dma_wait(struct stge_softc *sc)
    747 {
    748 	int i;
    749 
    750 	for (i = 0; i < STGE_TIMEOUT; i++) {
    751 		delay(2);
    752 		if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_DMACtrl) &
    753 		     DMAC_TxDMAInProg) == 0)
    754 			break;
    755 	}
    756 
    757 	if (i == STGE_TIMEOUT)
    758 		printf("%s: DMA wait timed out\n", sc->sc_dev.dv_xname);
    759 }
    760 
    761 /*
    762  * stge_start:		[ifnet interface function]
    763  *
    764  *	Start packet transmission on the interface.
    765  */
    766 void
    767 stge_start(struct ifnet *ifp)
    768 {
    769 	struct stge_softc *sc = ifp->if_softc;
    770 	struct mbuf *m0;
    771 	struct stge_descsoft *ds;
    772 	struct stge_tfd *tfd;
    773 	bus_dmamap_t dmamap;
    774 	int error, firsttx, nexttx, opending, seg, totlen;
    775 	uint64_t csum_flags;
    776 
    777 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    778 		return;
    779 
    780 	/*
    781 	 * Remember the previous number of pending transmissions
    782 	 * and the first descriptor we will use.
    783 	 */
    784 	opending = sc->sc_txpending;
    785 	firsttx = STGE_NEXTTX(sc->sc_txlast);
    786 
    787 	/*
    788 	 * Loop through the send queue, setting up transmit descriptors
    789 	 * until we drain the queue, or use up all available transmit
    790 	 * descriptors.
    791 	 */
    792 	for (;;) {
    793 		/*
    794 		 * Grab a packet off the queue.
    795 		 */
    796 		IFQ_POLL(&ifp->if_snd, m0);
    797 		if (m0 == NULL)
    798 			break;
    799 
    800 		/*
    801 		 * Leave one unused descriptor at the end of the
    802 		 * list to prevent wrapping completely around.
    803 		 */
    804 		if (sc->sc_txpending == (STGE_NTXDESC - 1)) {
    805 			STGE_EVCNT_INCR(&sc->sc_ev_txstall);
    806 			break;
    807 		}
    808 
    809 		/*
    810 		 * Get the last and next available transmit descriptor.
    811 		 */
    812 		nexttx = STGE_NEXTTX(sc->sc_txlast);
    813 		tfd = &sc->sc_txdescs[nexttx];
    814 		ds = &sc->sc_txsoft[nexttx];
    815 
    816 		dmamap = ds->ds_dmamap;
    817 
    818 		/*
    819 		 * Load the DMA map.  If this fails, the packet either
    820 		 * didn't fit in the alloted number of segments, or we
    821 		 * were short on resources.  For the too-may-segments
    822 		 * case, we simply report an error and drop the packet,
    823 		 * since we can't sanely copy a jumbo packet to a single
    824 		 * buffer.
    825 		 */
    826 		error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    827 		    BUS_DMA_NOWAIT);
    828 		if (error) {
    829 			if (error == EFBIG) {
    830 				printf("%s: Tx packet consumes too many "
    831 				    "DMA segments, dropping...\n",
    832 				    sc->sc_dev.dv_xname);
    833 				IFQ_DEQUEUE(&ifp->if_snd, m0);
    834 				m_freem(m0);
    835 				continue;
    836 			}
    837 			/*
    838 			 * Short on resources, just stop for now.
    839 			 */
    840 			break;
    841 		}
    842 
    843 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    844 
    845 		/*
    846 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    847 		 */
    848 
    849 		/* Sync the DMA map. */
    850 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    851 		    BUS_DMASYNC_PREWRITE);
    852 
    853 		/* Initialize the fragment list. */
    854 		for (totlen = 0, seg = 0; seg < dmamap->dm_nsegs; seg++) {
    855 			tfd->tfd_frags[seg].frag_word0 =
    856 			    htole64(FRAG_ADDR(dmamap->dm_segs[seg].ds_addr) |
    857 			    FRAG_LEN(dmamap->dm_segs[seg].ds_len));
    858 			totlen += dmamap->dm_segs[seg].ds_len;
    859 		}
    860 
    861 #ifdef STGE_EVENT_COUNTERS
    862 		switch (dmamap->dm_nsegs) {
    863 		case 1:
    864 			STGE_EVCNT_INCR(&sc->sc_ev_txseg1);
    865 			break;
    866 		case 2:
    867 			STGE_EVCNT_INCR(&sc->sc_ev_txseg2);
    868 			break;
    869 		case 3:
    870 			STGE_EVCNT_INCR(&sc->sc_ev_txseg3);
    871 			break;
    872 		case 4:
    873 			STGE_EVCNT_INCR(&sc->sc_ev_txseg4);
    874 			break;
    875 		case 5:
    876 			STGE_EVCNT_INCR(&sc->sc_ev_txseg5);
    877 			break;
    878 		default:
    879 			STGE_EVCNT_INCR(&sc->sc_ev_txsegmore);
    880 			break;
    881 		}
    882 #endif /* STGE_EVENT_COUNTERS */
    883 
    884 		/*
    885 		 * Initialize checksumming flags in the descriptor.
    886 		 * Byte-swap constants so the compiler can optimize.
    887 		 */
    888 		csum_flags = 0;
    889 		if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
    890 			STGE_EVCNT_INCR(&sc->sc_ev_txipsum);
    891 			csum_flags |= htole64(TFD_IPChecksumEnable);
    892 		}
    893 
    894 		if (m0->m_pkthdr.csum_flags & M_CSUM_TCPv4) {
    895 			STGE_EVCNT_INCR(&sc->sc_ev_txtcpsum);
    896 			csum_flags |= htole64(TFD_TCPChecksumEnable);
    897 		}
    898 		else if (m0->m_pkthdr.csum_flags & M_CSUM_UDPv4) {
    899 			STGE_EVCNT_INCR(&sc->sc_ev_txudpsum);
    900 			csum_flags |= htole64(TFD_UDPChecksumEnable);
    901 		}
    902 
    903 		/*
    904 		 * Initialize the descriptor and give it to the chip.
    905 		 */
    906 		tfd->tfd_control = htole64(TFD_FrameId(nexttx) |
    907 		    TFD_WordAlign(/*totlen & */3) |
    908 		    TFD_FragCount(seg) | csum_flags |
    909 		    (((nexttx & STGE_TXINTR_SPACING_MASK) == 0) ?
    910 		     TFD_TxDMAIndicate : 0));
    911 
    912 		/* Sync the descriptor. */
    913 		STGE_CDTXSYNC(sc, nexttx,
    914 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    915 
    916 		/*
    917 		 * Kick the transmit DMA logic.
    918 		 */
    919 		bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_DMACtrl,
    920 		    sc->sc_DMACtrl | DMAC_TxDMAPollNow);
    921 
    922 		/*
    923 		 * Store a pointer to the packet so we can free it later.
    924 		 */
    925 		ds->ds_mbuf = m0;
    926 
    927 		/* Advance the tx pointer. */
    928 		sc->sc_txpending++;
    929 		sc->sc_txlast = nexttx;
    930 
    931 #if NBPFILTER > 0
    932 		/*
    933 		 * Pass the packet to any BPF listeners.
    934 		 */
    935 		if (ifp->if_bpf)
    936 			bpf_mtap(ifp->if_bpf, m0);
    937 #endif /* NBPFILTER > 0 */
    938 	}
    939 
    940 	if (sc->sc_txpending == (STGE_NTXDESC - 1)) {
    941 		/* No more slots left; notify upper layer. */
    942 		ifp->if_flags |= IFF_OACTIVE;
    943 	}
    944 
    945 	if (sc->sc_txpending != opending) {
    946 		/*
    947 		 * We enqueued packets.  If the transmitter was idle,
    948 		 * reset the txdirty pointer.
    949 		 */
    950 		if (opending == 0)
    951 			sc->sc_txdirty = firsttx;
    952 
    953 		/* Set a watchdog timer in case the chip flakes out. */
    954 		ifp->if_timer = 5;
    955 	}
    956 }
    957 
    958 /*
    959  * stge_watchdog:	[ifnet interface function]
    960  *
    961  *	Watchdog timer handler.
    962  */
    963 void
    964 stge_watchdog(struct ifnet *ifp)
    965 {
    966 	struct stge_softc *sc = ifp->if_softc;
    967 
    968 	/*
    969 	 * Sweep up first, since we don't interrupt every frame.
    970 	 */
    971 	stge_txintr(sc);
    972 	if (sc->sc_txpending != 0) {
    973 		printf("%s: device timeout\n", sc->sc_dev.dv_xname);
    974 		ifp->if_oerrors++;
    975 
    976 		(void) stge_init(ifp);
    977 
    978 		/* Try to get more packets going. */
    979 		stge_start(ifp);
    980 	}
    981 }
    982 
    983 /*
    984  * stge_ioctl:		[ifnet interface function]
    985  *
    986  *	Handle control requests from the operator.
    987  */
    988 int
    989 stge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    990 {
    991 	struct stge_softc *sc = ifp->if_softc;
    992 	struct ifreq *ifr = (struct ifreq *)data;
    993 	int s, error;
    994 
    995 	s = splnet();
    996 
    997 	switch (cmd) {
    998 	case SIOCSIFMEDIA:
    999 	case SIOCGIFMEDIA:
   1000 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1001 		break;
   1002 
   1003 	default:
   1004 		error = ether_ioctl(ifp, cmd, data);
   1005 		if (error == ENETRESET) {
   1006 			/*
   1007 			 * Multicast list has changed; set the hardware filter
   1008 			 * accordingly.
   1009 			 */
   1010 			stge_set_filter(sc);
   1011 			error = 0;
   1012 		}
   1013 		break;
   1014 	}
   1015 
   1016 	/* Try to get more packets going. */
   1017 	stge_start(ifp);
   1018 
   1019 	splx(s);
   1020 	return (error);
   1021 }
   1022 
   1023 /*
   1024  * stge_intr:
   1025  *
   1026  *	Interrupt service routine.
   1027  */
   1028 int
   1029 stge_intr(void *arg)
   1030 {
   1031 	struct stge_softc *sc = arg;
   1032 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1033 	uint32_t txstat;
   1034 	int wantinit;
   1035 	uint16_t isr;
   1036 
   1037 	if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_IntStatus) &
   1038 	     IS_InterruptStatus) == 0)
   1039 		return (0);
   1040 
   1041 	for (wantinit = 0; wantinit == 0;) {
   1042 		isr = bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_IntStatusAck);
   1043 		if ((isr & sc->sc_IntEnable) == 0)
   1044 			break;
   1045 
   1046 		/* Receive interrupts. */
   1047 		if (isr & (IE_RxDMAComplete|IE_RFDListEnd)) {
   1048 			STGE_EVCNT_INCR(&sc->sc_ev_rxintr);
   1049 			stge_rxintr(sc);
   1050 			if (isr & IE_RFDListEnd) {
   1051 				printf("%s: receive ring overflow\n",
   1052 				    sc->sc_dev.dv_xname);
   1053 				/*
   1054 				 * XXX Should try to recover from this
   1055 				 * XXX more gracefully.
   1056 				 */
   1057 				wantinit = 1;
   1058 			}
   1059 		}
   1060 
   1061 		/* Transmit interrupts. */
   1062 		if (isr & (IE_TxDMAComplete|IE_TxComplete)) {
   1063 #ifdef STGE_EVENT_COUNTERS
   1064 			if (isr & IE_TxDMAComplete)
   1065 				STGE_EVCNT_INCR(&sc->sc_ev_txdmaintr);
   1066 #endif
   1067 			stge_txintr(sc);
   1068 		}
   1069 
   1070 		/* Statistics overflow. */
   1071 		if (isr & IE_UpdateStats)
   1072 			stge_stats_update(sc);
   1073 
   1074 		/* Transmission errors. */
   1075 		if (isr & IE_TxComplete) {
   1076 			STGE_EVCNT_INCR(&sc->sc_ev_txindintr);
   1077 			for (;;) {
   1078 				txstat = bus_space_read_4(sc->sc_st, sc->sc_sh,
   1079 				    STGE_TxStatus);
   1080 				if ((txstat & TS_TxComplete) == 0)
   1081 					break;
   1082 				if (txstat & TS_TxUnderrun) {
   1083 					sc->sc_txthresh++;
   1084 					if (sc->sc_txthresh > 0x0fff)
   1085 						sc->sc_txthresh = 0x0fff;
   1086 					printf("%s: transmit underrun, new "
   1087 					    "threshold: %d bytes\n",
   1088 					    sc->sc_dev.dv_xname,
   1089 					    sc->sc_txthresh << 5);
   1090 				}
   1091 				if (txstat & TS_MaxCollisions)
   1092 					printf("%s: excessive collisions\n",
   1093 					    sc->sc_dev.dv_xname);
   1094 			}
   1095 			wantinit = 1;
   1096 		}
   1097 
   1098 		/* Host interface errors. */
   1099 		if (isr & IE_HostError) {
   1100 			printf("%s: Host interface error\n",
   1101 			    sc->sc_dev.dv_xname);
   1102 			wantinit = 1;
   1103 		}
   1104 	}
   1105 
   1106 	if (wantinit)
   1107 		stge_init(ifp);
   1108 
   1109 	bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_IntEnable,
   1110 	    sc->sc_IntEnable);
   1111 
   1112 	/* Try to get more packets going. */
   1113 	stge_start(ifp);
   1114 
   1115 	return (1);
   1116 }
   1117 
   1118 /*
   1119  * stge_txintr:
   1120  *
   1121  *	Helper; handle transmit interrupts.
   1122  */
   1123 void
   1124 stge_txintr(struct stge_softc *sc)
   1125 {
   1126 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1127 	struct stge_descsoft *ds;
   1128 	uint64_t control;
   1129 	int i;
   1130 
   1131 	ifp->if_flags &= ~IFF_OACTIVE;
   1132 
   1133 	/*
   1134 	 * Go through our Tx list and free mbufs for those
   1135 	 * frames which have been transmitted.
   1136 	 */
   1137 	for (i = sc->sc_txdirty; sc->sc_txpending != 0;
   1138 	     i = STGE_NEXTTX(i), sc->sc_txpending--) {
   1139 		ds = &sc->sc_txsoft[i];
   1140 
   1141 		STGE_CDTXSYNC(sc, i,
   1142 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1143 
   1144 		control = le64toh(sc->sc_txdescs[i].tfd_control);
   1145 		if ((control & TFD_TFDDone) == 0)
   1146 			break;
   1147 
   1148 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
   1149 		    0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1150 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1151 		m_freem(ds->ds_mbuf);
   1152 		ds->ds_mbuf = NULL;
   1153 	}
   1154 
   1155 	/* Update the dirty transmit buffer pointer. */
   1156 	sc->sc_txdirty = i;
   1157 
   1158 	/*
   1159 	 * If there are no more pending transmissions, cancel the watchdog
   1160 	 * timer.
   1161 	 */
   1162 	if (sc->sc_txpending == 0)
   1163 		ifp->if_timer = 0;
   1164 }
   1165 
   1166 /*
   1167  * stge_rxintr:
   1168  *
   1169  *	Helper; handle receive interrupts.
   1170  */
   1171 void
   1172 stge_rxintr(struct stge_softc *sc)
   1173 {
   1174 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1175 	struct stge_descsoft *ds;
   1176 	struct mbuf *m, *tailm;
   1177 	uint64_t status;
   1178 	int i, len;
   1179 
   1180 	for (i = sc->sc_rxptr;; i = STGE_NEXTRX(i)) {
   1181 		ds = &sc->sc_rxsoft[i];
   1182 
   1183 		STGE_CDRXSYNC(sc, i,
   1184 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1185 
   1186 		status = le64toh(sc->sc_rxdescs[i].rfd_status);
   1187 
   1188 		if ((status & RFD_RFDDone) == 0)
   1189 			break;
   1190 
   1191 		if (__predict_false(sc->sc_rxdiscard)) {
   1192 			STGE_INIT_RXDESC(sc, i);
   1193 			if (status & RFD_FrameEnd) {
   1194 				/* Reset our state. */
   1195 				sc->sc_rxdiscard = 0;
   1196 			}
   1197 			continue;
   1198 		}
   1199 
   1200 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
   1201 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1202 
   1203 		m = ds->ds_mbuf;
   1204 
   1205 		/*
   1206 		 * Add a new receive buffer to the ring.
   1207 		 */
   1208 		if (stge_add_rxbuf(sc, i) != 0) {
   1209 			/*
   1210 			 * Failed, throw away what we've done so
   1211 			 * far, and discard the rest of the packet.
   1212 			 */
   1213 			ifp->if_ierrors++;
   1214 			bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
   1215 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1216 			STGE_INIT_RXDESC(sc, i);
   1217 			if ((status & RFD_FrameEnd) == 0)
   1218 				sc->sc_rxdiscard = 1;
   1219 			if (sc->sc_rxhead != NULL)
   1220 				m_freem(sc->sc_rxhead);
   1221 			STGE_RXCHAIN_RESET(sc);
   1222 			continue;
   1223 		}
   1224 
   1225 #ifdef DIAGNOSTIC
   1226 		if (status & RFD_FrameStart) {
   1227 			KASSERT(sc->sc_rxhead == NULL);
   1228 			KASSERT(sc->sc_rxtailp == &sc->sc_rxhead);
   1229 		}
   1230 #endif
   1231 
   1232 		STGE_RXCHAIN_LINK(sc, m);
   1233 
   1234 		/*
   1235 		 * If this is not the end of the packet, keep
   1236 		 * looking.
   1237 		 */
   1238 		if ((status & RFD_FrameEnd) == 0) {
   1239 			sc->sc_rxlen += m->m_len;
   1240 			continue;
   1241 		}
   1242 
   1243 		/*
   1244 		 * Okay, we have the entire packet now...
   1245 		 */
   1246 		*sc->sc_rxtailp = NULL;
   1247 		m = sc->sc_rxhead;
   1248 		tailm = sc->sc_rxtail;
   1249 
   1250 		STGE_RXCHAIN_RESET(sc);
   1251 
   1252 		/*
   1253 		 * If the packet had an error, drop it.  Note we
   1254 		 * count the error later in the periodic stats update.
   1255 		 */
   1256 		if (status & (RFD_RxFIFOOverrun | RFD_RxRuntFrame |
   1257 			      RFD_RxAlignmentError | RFD_RxFCSError |
   1258 			      RFD_RxLengthError)) {
   1259 			m_freem(m);
   1260 			continue;
   1261 		}
   1262 
   1263 		/*
   1264 		 * No errors.
   1265 		 *
   1266 		 * Note we have configured the chip to not include
   1267 		 * the CRC at the end of the packet.
   1268 		 */
   1269 		len = RFD_RxDMAFrameLen(status);
   1270 		tailm->m_len = len - sc->sc_rxlen;
   1271 
   1272 		/*
   1273 		 * If the packet is small enough to fit in a
   1274 		 * single header mbuf, allocate one and copy
   1275 		 * the data into it.  This greatly reduces
   1276 		 * memory consumption when we receive lots
   1277 		 * of small packets.
   1278 		 */
   1279 		if (stge_copy_small != 0 && len <= (MHLEN - 2)) {
   1280 			struct mbuf *nm;
   1281 			MGETHDR(nm, M_DONTWAIT, MT_DATA);
   1282 			if (nm == NULL) {
   1283 				ifp->if_ierrors++;
   1284 				m_freem(m);
   1285 				continue;
   1286 			}
   1287 			nm->m_data += 2;
   1288 			nm->m_pkthdr.len = nm->m_len = len;
   1289 			m_copydata(m, 0, len, mtod(nm, caddr_t));
   1290 			m_freem(m);
   1291 			m = nm;
   1292 		}
   1293 
   1294 		/*
   1295 		 * Set the incoming checksum information for the packet.
   1296 		 */
   1297 		if (status & RFD_IPDetected) {
   1298 			STGE_EVCNT_INCR(&sc->sc_ev_rxipsum);
   1299 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1300 			if (status & RFD_IPError)
   1301 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1302 			if (status & RFD_TCPDetected) {
   1303 				STGE_EVCNT_INCR(&sc->sc_ev_rxtcpsum);
   1304 				m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
   1305 				if (status & RFD_TCPError)
   1306 					m->m_pkthdr.csum_flags |=
   1307 					    M_CSUM_TCP_UDP_BAD;
   1308 			} else if (status & RFD_UDPDetected) {
   1309 				STGE_EVCNT_INCR(&sc->sc_ev_rxudpsum);
   1310 				m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
   1311 				if (status & RFD_UDPError)
   1312 					m->m_pkthdr.csum_flags |=
   1313 					    M_CSUM_TCP_UDP_BAD;
   1314 			}
   1315 		}
   1316 
   1317 		m->m_pkthdr.rcvif = ifp;
   1318 		m->m_pkthdr.len = len;
   1319 
   1320 #if NBPFILTER > 0
   1321 		/*
   1322 		 * Pass this up to any BPF listeners, but only
   1323 		 * pass if up the stack if it's for us.
   1324 		 */
   1325 		if (ifp->if_bpf)
   1326 			bpf_mtap(ifp->if_bpf, m);
   1327 #endif /* NBPFILTER > 0 */
   1328 
   1329 		/* Pass it on. */
   1330 		(*ifp->if_input)(ifp, m);
   1331 	}
   1332 
   1333 	/* Update the receive pointer. */
   1334 	sc->sc_rxptr = i;
   1335 }
   1336 
   1337 /*
   1338  * stge_tick:
   1339  *
   1340  *	One second timer, used to tick the MII.
   1341  */
   1342 void
   1343 stge_tick(void *arg)
   1344 {
   1345 	struct stge_softc *sc = arg;
   1346 	int s;
   1347 
   1348 	s = splnet();
   1349 	mii_tick(&sc->sc_mii);
   1350 	stge_stats_update(sc);
   1351 	splx(s);
   1352 
   1353 	callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
   1354 }
   1355 
   1356 /*
   1357  * stge_stats_update:
   1358  *
   1359  *	Read the TC9021 statistics counters.
   1360  */
   1361 void
   1362 stge_stats_update(struct stge_softc *sc)
   1363 {
   1364 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1365 	bus_space_tag_t st = sc->sc_st;
   1366 	bus_space_handle_t sh = sc->sc_sh;
   1367 
   1368 	(void) bus_space_read_4(st, sh, STGE_OctetRcvOk);
   1369 
   1370 	ifp->if_ipackets +=
   1371 	    bus_space_read_4(st, sh, STGE_FramesRcvdOk);
   1372 
   1373 	ifp->if_ierrors +=
   1374 	    (u_int) bus_space_read_2(st, sh, STGE_FramesLostRxErrors);
   1375 
   1376 	(void) bus_space_read_4(st, sh, STGE_OctetXmtdOk);
   1377 
   1378 	ifp->if_opackets +=
   1379 	    bus_space_read_4(st, sh, STGE_FramesXmtdOk);
   1380 
   1381 	ifp->if_collisions +=
   1382 	    bus_space_read_4(st, sh, STGE_LateCollisions) +
   1383 	    bus_space_read_4(st, sh, STGE_MultiColFrames) +
   1384 	    bus_space_read_4(st, sh, STGE_SingleColFrames);
   1385 
   1386 	ifp->if_oerrors +=
   1387 	    (u_int) bus_space_read_2(st, sh, STGE_FramesAbortXSColls) +
   1388 	    (u_int) bus_space_read_2(st, sh, STGE_FramesWEXDeferal);
   1389 }
   1390 
   1391 /*
   1392  * stge_reset:
   1393  *
   1394  *	Perform a soft reset on the TC9021.
   1395  */
   1396 void
   1397 stge_reset(struct stge_softc *sc)
   1398 {
   1399 	uint32_t ac;
   1400 	int i;
   1401 
   1402 	ac = bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl);
   1403 
   1404 	/*
   1405 	 * Only assert RstOut if we're fiber.  We need GMII clocks
   1406 	 * to be present in order for the reset to complete on fiber
   1407 	 * cards.
   1408 	 */
   1409 	bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl,
   1410 	    ac | AC_GlobalReset | AC_RxReset | AC_TxReset |
   1411 	    AC_DMA | AC_FIFO | AC_Network | AC_Host | AC_AutoInit |
   1412 	    (sc->sc_usefiber ? AC_RstOut : 0));
   1413 
   1414 	delay(50000);
   1415 
   1416 	for (i = 0; i < STGE_TIMEOUT; i++) {
   1417 		delay(5000);
   1418 		if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl) &
   1419 		     AC_ResetBusy) == 0)
   1420 			break;
   1421 	}
   1422 
   1423 	if (i == STGE_TIMEOUT)
   1424 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
   1425 
   1426 	delay(1000);
   1427 }
   1428 
   1429 /*
   1430  * stge_init:		[ ifnet interface function ]
   1431  *
   1432  *	Initialize the interface.  Must be called at splnet().
   1433  */
   1434 int
   1435 stge_init(struct ifnet *ifp)
   1436 {
   1437 	struct stge_softc *sc = ifp->if_softc;
   1438 	bus_space_tag_t st = sc->sc_st;
   1439 	bus_space_handle_t sh = sc->sc_sh;
   1440 	struct stge_descsoft *ds;
   1441 	int i, error = 0;
   1442 
   1443 	/*
   1444 	 * Cancel any pending I/O.
   1445 	 */
   1446 	stge_stop(ifp, 0);
   1447 
   1448 	/*
   1449 	 * Reset the chip to a known state.
   1450 	 */
   1451 	stge_reset(sc);
   1452 
   1453 	/*
   1454 	 * Initialize the transmit descriptor ring.
   1455 	 */
   1456 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1457 	for (i = 0; i < STGE_NTXDESC; i++) {
   1458 		sc->sc_txdescs[i].tfd_next =
   1459 		    (uint64_t) STGE_CDTXADDR(sc, STGE_NEXTTX(i));
   1460 		sc->sc_txdescs[i].tfd_control = htole64(TFD_TFDDone);
   1461 	}
   1462 	sc->sc_txpending = 0;
   1463 	sc->sc_txdirty = 0;
   1464 	sc->sc_txlast = STGE_NTXDESC - 1;
   1465 
   1466 	/*
   1467 	 * Initialize the receive descriptor and receive job
   1468 	 * descriptor rings.
   1469 	 */
   1470 	for (i = 0; i < STGE_NRXDESC; i++) {
   1471 		ds = &sc->sc_rxsoft[i];
   1472 		if (ds->ds_mbuf == NULL) {
   1473 			if ((error = stge_add_rxbuf(sc, i)) != 0) {
   1474 				printf("%s: unable to allocate or map rx "
   1475 				    "buffer %d, error = %d\n",
   1476 				    sc->sc_dev.dv_xname, i, error);
   1477 				/*
   1478 				 * XXX Should attempt to run with fewer receive
   1479 				 * XXX buffers instead of just failing.
   1480 				 */
   1481 				stge_rxdrain(sc);
   1482 				goto out;
   1483 			}
   1484 		} else
   1485 			STGE_INIT_RXDESC(sc, i);
   1486 	}
   1487 	sc->sc_rxptr = 0;
   1488 	sc->sc_rxdiscard = 0;
   1489 	STGE_RXCHAIN_RESET(sc);
   1490 
   1491 	/* Set the station address. */
   1492 	bus_space_write_2(st, sh, STGE_StationAddress0,
   1493 	    LLADDR(ifp->if_sadl)[0] | (LLADDR(ifp->if_sadl)[1] << 8));
   1494 	bus_space_write_2(st, sh, STGE_StationAddress1,
   1495 	    LLADDR(ifp->if_sadl)[2] | (LLADDR(ifp->if_sadl)[3] << 8));
   1496 	bus_space_write_2(st, sh, STGE_StationAddress2,
   1497 	    LLADDR(ifp->if_sadl)[4] | (LLADDR(ifp->if_sadl)[5] << 8));
   1498 
   1499 	/*
   1500 	 * Set the statistics masks.  Disable all the RMON stats,
   1501 	 * and disable selected stats in the non-RMON stats registers.
   1502 	 */
   1503 	bus_space_write_4(st, sh, STGE_RMONStatisticsMask, 0xffffffff);
   1504 	bus_space_write_4(st, sh, STGE_StatisticsMask,
   1505 	    (1U << 1) | (1U << 2) | (1U << 3) | (1U << 4) | (1U << 5) |
   1506 	    (1U << 6) | (1U << 7) | (1U << 8) | (1U << 9) | (1U << 10) |
   1507 	    (1U << 13) | (1U << 14) | (1U << 15) | (1U << 19) | (1U << 20) |
   1508 	    (1U << 21));
   1509 
   1510 	/* Set up the receive filter. */
   1511 	stge_set_filter(sc);
   1512 
   1513 	/*
   1514 	 * Give the transmit and receive ring to the chip.
   1515 	 */
   1516 	bus_space_write_4(st, sh, STGE_TFDListPtrHi, 0); /* NOTE: 32-bit DMA */
   1517 	bus_space_write_4(st, sh, STGE_TFDListPtrLo,
   1518 	    STGE_CDTXADDR(sc, sc->sc_txdirty));
   1519 
   1520 	bus_space_write_4(st, sh, STGE_RFDListPtrHi, 0); /* NOTE: 32-bit DMA */
   1521 	bus_space_write_4(st, sh, STGE_RFDListPtrLo,
   1522 	    STGE_CDRXADDR(sc, sc->sc_rxptr));
   1523 
   1524 	/*
   1525 	 * Initialize the Tx auto-poll period.  It's OK to make this number
   1526 	 * large (255 is the max, but we use 127) -- we explicitly kick the
   1527 	 * transmit engine when there's actually a packet.
   1528 	 */
   1529 	bus_space_write_1(st, sh, STGE_TxDMAPollPeriod, 127);
   1530 
   1531 	/* ..and the Rx auto-poll period. */
   1532 	bus_space_write_1(st, sh, STGE_RxDMAPollPeriod, 64);
   1533 
   1534 	/* Initialize the Tx start threshold. */
   1535 	bus_space_write_2(st, sh, STGE_TxStartThresh, sc->sc_txthresh);
   1536 
   1537 	/*
   1538 	 * Initialize the Rx DMA interrupt control register.  We
   1539 	 * request an interrupt after every incoming packet, but
   1540 	 * defer it for 32us (64 * 512 ns).  When the number of
   1541 	 * interrupts pending reaches 8, we stop deferring the
   1542 	 * interrupt, and signal it immediately.
   1543 	 */
   1544 	bus_space_write_4(st, sh, STGE_RxDMAIntCtrl,
   1545 	    RDIC_RxFrameCount(8) | RDIC_RxDMAWaitTime(512));
   1546 
   1547 	/*
   1548 	 * Initialize the interrupt mask.
   1549 	 */
   1550 	sc->sc_IntEnable = IE_HostError | IE_TxComplete | IE_UpdateStats |
   1551 	    IE_TxDMAComplete | IE_RxDMAComplete | IE_RFDListEnd;
   1552 	bus_space_write_2(st, sh, STGE_IntStatus, 0xffff);
   1553 	bus_space_write_2(st, sh, STGE_IntEnable, sc->sc_IntEnable);
   1554 
   1555 	/*
   1556 	 * Configure the DMA engine.
   1557 	 * XXX Should auto-tune TxBurstLimit.
   1558 	 */
   1559 	bus_space_write_4(st, sh, STGE_DMACtrl, sc->sc_DMACtrl |
   1560 	    DMAC_TxBurstLimit(3));
   1561 
   1562 	/*
   1563 	 * Send a PAUSE frame when we reach 29,696 bytes in the Rx
   1564 	 * FIFO, and send an un-PAUSE frame when the FIFO is totally
   1565 	 * empty again.
   1566 	 */
   1567 	bus_space_write_2(st, sh, STGE_FlowOnTresh, 29696 / 16);
   1568 	bus_space_write_2(st, sh, STGE_FlowOffThresh, 0);
   1569 
   1570 	/*
   1571 	 * Set the maximum frame size.
   1572 	 */
   1573 	bus_space_write_2(st, sh, STGE_MaxFrameSize,
   1574 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
   1575 	    ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
   1576 	     ETHER_VLAN_ENCAP_LEN : 0));
   1577 
   1578 	/*
   1579 	 * Initialize MacCtrl -- do it before setting the media,
   1580 	 * as setting the media will actually program the register.
   1581 	 *
   1582 	 * Note: We have to poke the IFS value before poking
   1583 	 * anything else.
   1584 	 */
   1585 	sc->sc_MACCtrl = MC_IFSSelect(0);
   1586 	bus_space_write_4(st, sh, STGE_MACCtrl, sc->sc_MACCtrl);
   1587 	sc->sc_MACCtrl |= MC_StatisticsEnable | MC_TxEnable | MC_RxEnable;
   1588 
   1589 	if (sc->sc_rev >= 6) {		/* >= B.2 */
   1590 		/* Multi-frag frame bug work-around. */
   1591 		bus_space_write_2(st, sh, STGE_DebugCtrl,
   1592 		    bus_space_read_2(st, sh, STGE_DebugCtrl) | 0x0200);
   1593 
   1594 		/* Tx Poll Now bug work-around. */
   1595 		bus_space_write_2(st, sh, STGE_DebugCtrl,
   1596 		    bus_space_read_2(st, sh, STGE_DebugCtrl) | 0x0010);
   1597 	}
   1598 
   1599 	/*
   1600 	 * Set the current media.
   1601 	 */
   1602 	mii_mediachg(&sc->sc_mii);
   1603 
   1604 	/*
   1605 	 * Start the one second MII clock.
   1606 	 */
   1607 	callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
   1608 
   1609 	/*
   1610 	 * ...all done!
   1611 	 */
   1612 	ifp->if_flags |= IFF_RUNNING;
   1613 	ifp->if_flags &= ~IFF_OACTIVE;
   1614 
   1615  out:
   1616 	if (error)
   1617 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1618 	return (error);
   1619 }
   1620 
   1621 /*
   1622  * stge_drain:
   1623  *
   1624  *	Drain the receive queue.
   1625  */
   1626 void
   1627 stge_rxdrain(struct stge_softc *sc)
   1628 {
   1629 	struct stge_descsoft *ds;
   1630 	int i;
   1631 
   1632 	for (i = 0; i < STGE_NRXDESC; i++) {
   1633 		ds = &sc->sc_rxsoft[i];
   1634 		if (ds->ds_mbuf != NULL) {
   1635 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1636 			ds->ds_mbuf->m_next = NULL;
   1637 			m_freem(ds->ds_mbuf);
   1638 			ds->ds_mbuf = NULL;
   1639 		}
   1640 	}
   1641 }
   1642 
   1643 /*
   1644  * stge_stop:		[ ifnet interface function ]
   1645  *
   1646  *	Stop transmission on the interface.
   1647  */
   1648 void
   1649 stge_stop(struct ifnet *ifp, int disable)
   1650 {
   1651 	struct stge_softc *sc = ifp->if_softc;
   1652 	struct stge_descsoft *ds;
   1653 	int i;
   1654 
   1655 	/*
   1656 	 * Stop the one second clock.
   1657 	 */
   1658 	callout_stop(&sc->sc_tick_ch);
   1659 
   1660 	/* Down the MII. */
   1661 	mii_down(&sc->sc_mii);
   1662 
   1663 	/*
   1664 	 * Disable interrupts.
   1665 	 */
   1666 	bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_IntEnable, 0);
   1667 
   1668 	/*
   1669 	 * Stop receiver, transmitter, and stats update.
   1670 	 */
   1671 	bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_MACCtrl,
   1672 	    MC_StatisticsDisable | MC_TxDisable | MC_RxDisable);
   1673 
   1674 	/*
   1675 	 * Stop the transmit and receive DMA.
   1676 	 */
   1677 	stge_dma_wait(sc);
   1678 	bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_TFDListPtrHi, 0);
   1679 	bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_TFDListPtrLo, 0);
   1680 	bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_RFDListPtrHi, 0);
   1681 	bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_RFDListPtrLo, 0);
   1682 
   1683 	/*
   1684 	 * Release any queued transmit buffers.
   1685 	 */
   1686 	for (i = 0; i < STGE_NTXDESC; i++) {
   1687 		ds = &sc->sc_txsoft[i];
   1688 		if (ds->ds_mbuf != NULL) {
   1689 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1690 			m_freem(ds->ds_mbuf);
   1691 			ds->ds_mbuf = NULL;
   1692 		}
   1693 	}
   1694 
   1695 	if (disable)
   1696 		stge_rxdrain(sc);
   1697 
   1698 	/*
   1699 	 * Mark the interface down and cancel the watchdog timer.
   1700 	 */
   1701 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1702 	ifp->if_timer = 0;
   1703 }
   1704 
   1705 #if 0
   1706 static int
   1707 stge_eeprom_wait(struct stge_softc *sc)
   1708 {
   1709 	int i;
   1710 
   1711 	for (i = 0; i < STGE_TIMEOUT; i++) {
   1712 		delay(1000);
   1713 		if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_EepromCtrl) &
   1714 		     EC_EepromBusy) == 0)
   1715 			return (0);
   1716 	}
   1717 	return (1);
   1718 }
   1719 
   1720 /*
   1721  * stge_read_eeprom:
   1722  *
   1723  *	Read data from the serial EEPROM.
   1724  */
   1725 void
   1726 stge_read_eeprom(struct stge_softc *sc, int offset, uint16_t *data)
   1727 {
   1728 
   1729 	if (stge_eeprom_wait(sc))
   1730 		printf("%s: EEPROM failed to come ready\n",
   1731 		    sc->sc_dev.dv_xname);
   1732 
   1733 	bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_EepromCtrl,
   1734 	    EC_EepromAddress(offset) | EC_EepromOpcode(EC_OP_RR));
   1735 	if (stge_eeprom_wait(sc))
   1736 		printf("%s: EEPROM read timed out\n",
   1737 		    sc->sc_dev.dv_xname);
   1738 	*data = bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_EepromData);
   1739 }
   1740 #endif /* 0 */
   1741 
   1742 /*
   1743  * stge_add_rxbuf:
   1744  *
   1745  *	Add a receive buffer to the indicated descriptor.
   1746  */
   1747 int
   1748 stge_add_rxbuf(struct stge_softc *sc, int idx)
   1749 {
   1750 	struct stge_descsoft *ds = &sc->sc_rxsoft[idx];
   1751 	struct mbuf *m;
   1752 	int error;
   1753 
   1754 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1755 	if (m == NULL)
   1756 		return (ENOBUFS);
   1757 
   1758 	MCLGET(m, M_DONTWAIT);
   1759 	if ((m->m_flags & M_EXT) == 0) {
   1760 		m_freem(m);
   1761 		return (ENOBUFS);
   1762 	}
   1763 
   1764 	m->m_data = m->m_ext.ext_buf + 2;
   1765 	m->m_len = MCLBYTES - 2;
   1766 
   1767 	if (ds->ds_mbuf != NULL)
   1768 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1769 
   1770 	ds->ds_mbuf = m;
   1771 
   1772 	error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
   1773 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   1774 	if (error) {
   1775 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1776 		    sc->sc_dev.dv_xname, idx, error);
   1777 		panic("stge_add_rxbuf");	/* XXX */
   1778 	}
   1779 
   1780 	bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
   1781 	    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1782 
   1783 	STGE_INIT_RXDESC(sc, idx);
   1784 
   1785 	return (0);
   1786 }
   1787 
   1788 /*
   1789  * stge_set_filter:
   1790  *
   1791  *	Set up the receive filter.
   1792  */
   1793 void
   1794 stge_set_filter(struct stge_softc *sc)
   1795 {
   1796 	struct ethercom *ec = &sc->sc_ethercom;
   1797 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1798 	struct ether_multi *enm;
   1799 	struct ether_multistep step;
   1800 	uint32_t crc;
   1801 	uint32_t mchash[2];
   1802 
   1803 	sc->sc_ReceiveMode = RM_ReceiveUnicast;
   1804 	if (ifp->if_flags & IFF_BROADCAST)
   1805 		sc->sc_ReceiveMode |= RM_ReceiveBroadcast;
   1806 
   1807 	if (ifp->if_flags & IFF_PROMISC) {
   1808 		sc->sc_ReceiveMode |= RM_ReceiveAllFrames;
   1809 		goto allmulti;
   1810 	}
   1811 
   1812 	/*
   1813 	 * Set up the multicast address filter by passing all multicast
   1814 	 * addresses through a CRC generator, and then using the low-order
   1815 	 * 6 bits as an index into the 64 bit multicast hash table.  The
   1816 	 * high order bits select the register, while the rest of the bits
   1817 	 * select the bit within the register.
   1818 	 */
   1819 
   1820 	memset(mchash, 0, sizeof(mchash));
   1821 
   1822 	ETHER_FIRST_MULTI(step, ec, enm);
   1823 	if (enm == NULL)
   1824 		goto done;
   1825 
   1826 	while (enm != NULL) {
   1827 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1828 			/*
   1829 			 * We must listen to a range of multicast addresses.
   1830 			 * For now, just accept all multicasts, rather than
   1831 			 * trying to set only those filter bits needed to match
   1832 			 * the range.  (At this time, the only use of address
   1833 			 * ranges is for IP multicast routing, for which the
   1834 			 * range is big enough to require all bits set.)
   1835 			 */
   1836 			goto allmulti;
   1837 		}
   1838 
   1839 		crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
   1840 
   1841 		/* Just want the 6 least significant bits. */
   1842 		crc &= 0x3f;
   1843 
   1844 		/* Set the corresponding bit in the hash table. */
   1845 		mchash[crc >> 5] |= 1 << (crc & 0x1f);
   1846 
   1847 		ETHER_NEXT_MULTI(step, enm);
   1848 	}
   1849 
   1850 	sc->sc_ReceiveMode |= RM_ReceiveMulticastHash;
   1851 
   1852 	ifp->if_flags &= ~IFF_ALLMULTI;
   1853 	goto done;
   1854 
   1855  allmulti:
   1856 	ifp->if_flags |= IFF_ALLMULTI;
   1857 	sc->sc_ReceiveMode |= RM_ReceiveMulticast;
   1858 
   1859  done:
   1860 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
   1861 		/*
   1862 		 * Program the multicast hash table.
   1863 		 */
   1864 		bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_HashTable0,
   1865 		    mchash[0]);
   1866 		bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_HashTable1,
   1867 		    mchash[1]);
   1868 	}
   1869 
   1870 	bus_space_write_1(sc->sc_st, sc->sc_sh, STGE_ReceiveMode,
   1871 	    sc->sc_ReceiveMode);
   1872 }
   1873 
   1874 /*
   1875  * stge_mii_readreg:	[mii interface function]
   1876  *
   1877  *	Read a PHY register on the MII of the TC9021.
   1878  */
   1879 int
   1880 stge_mii_readreg(struct device *self, int phy, int reg)
   1881 {
   1882 
   1883 	return (mii_bitbang_readreg(self, &stge_mii_bitbang_ops, phy, reg));
   1884 }
   1885 
   1886 /*
   1887  * stge_mii_writereg:	[mii interface function]
   1888  *
   1889  *	Write a PHY register on the MII of the TC9021.
   1890  */
   1891 void
   1892 stge_mii_writereg(struct device *self, int phy, int reg, int val)
   1893 {
   1894 
   1895 	mii_bitbang_writereg(self, &stge_mii_bitbang_ops, phy, reg, val);
   1896 }
   1897 
   1898 /*
   1899  * stge_mii_statchg:	[mii interface function]
   1900  *
   1901  *	Callback from MII layer when media changes.
   1902  */
   1903 void
   1904 stge_mii_statchg(struct device *self)
   1905 {
   1906 	struct stge_softc *sc = (struct stge_softc *) self;
   1907 
   1908 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   1909 		sc->sc_MACCtrl |= MC_DuplexSelect;
   1910 	else
   1911 		sc->sc_MACCtrl &= ~MC_DuplexSelect;
   1912 
   1913 	/* XXX 802.1x flow-control? */
   1914 
   1915 	bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_MACCtrl, sc->sc_MACCtrl);
   1916 }
   1917 
   1918 /*
   1919  * sste_mii_bitbang_read: [mii bit-bang interface function]
   1920  *
   1921  *	Read the MII serial port for the MII bit-bang module.
   1922  */
   1923 uint32_t
   1924 stge_mii_bitbang_read(struct device *self)
   1925 {
   1926 	struct stge_softc *sc = (void *) self;
   1927 
   1928 	return (bus_space_read_1(sc->sc_st, sc->sc_sh, STGE_PhyCtrl));
   1929 }
   1930 
   1931 /*
   1932  * stge_mii_bitbang_write: [mii big-bang interface function]
   1933  *
   1934  *	Write the MII serial port for the MII bit-bang module.
   1935  */
   1936 void
   1937 stge_mii_bitbang_write(struct device *self, uint32_t val)
   1938 {
   1939 	struct stge_softc *sc = (void *) self;
   1940 
   1941 	bus_space_write_1(sc->sc_st, sc->sc_sh, STGE_PhyCtrl,
   1942 	    val | sc->sc_PhyCtrl);
   1943 }
   1944 
   1945 /*
   1946  * stge_mediastatus:	[ifmedia interface function]
   1947  *
   1948  *	Get the current interface media status.
   1949  */
   1950 void
   1951 stge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1952 {
   1953 	struct stge_softc *sc = ifp->if_softc;
   1954 
   1955 	mii_pollstat(&sc->sc_mii);
   1956 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1957 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1958 }
   1959 
   1960 /*
   1961  * stge_mediachange:	[ifmedia interface function]
   1962  *
   1963  *	Set hardware to newly-selected media.
   1964  */
   1965 int
   1966 stge_mediachange(struct ifnet *ifp)
   1967 {
   1968 	struct stge_softc *sc = ifp->if_softc;
   1969 
   1970 	if (ifp->if_flags & IFF_UP)
   1971 		mii_mediachg(&sc->sc_mii);
   1972 	return (0);
   1973 }
   1974