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