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