Home | History | Annotate | Line # | Download | only in pci
if_ste.c revision 1.52
      1 /*	$NetBSD: if_ste.c,v 1.52 2018/12/09 11:14:02 jdolecek 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Device driver for the Sundance Tech. ST-201 10/100
     34  * Ethernet controller.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: if_ste.c,v 1.52 2018/12/09 11:14:02 jdolecek Exp $");
     39 
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/callout.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/malloc.h>
     46 #include <sys/kernel.h>
     47 #include <sys/socket.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/errno.h>
     50 #include <sys/device.h>
     51 #include <sys/queue.h>
     52 
     53 #include <net/if.h>
     54 #include <net/if_dl.h>
     55 #include <net/if_media.h>
     56 #include <net/if_ether.h>
     57 
     58 #include <net/bpf.h>
     59 
     60 #include <sys/bus.h>
     61 #include <sys/intr.h>
     62 
     63 #include <dev/mii/mii.h>
     64 #include <dev/mii/miivar.h>
     65 #include <dev/mii/mii_bitbang.h>
     66 
     67 #include <dev/pci/pcireg.h>
     68 #include <dev/pci/pcivar.h>
     69 #include <dev/pci/pcidevs.h>
     70 
     71 #include <dev/pci/if_stereg.h>
     72 
     73 /*
     74  * Transmit descriptor list size.
     75  */
     76 #define	STE_NTXDESC		256
     77 #define	STE_NTXDESC_MASK	(STE_NTXDESC - 1)
     78 #define	STE_NEXTTX(x)		(((x) + 1) & STE_NTXDESC_MASK)
     79 
     80 /*
     81  * Receive descriptor list size.
     82  */
     83 #define	STE_NRXDESC		128
     84 #define	STE_NRXDESC_MASK	(STE_NRXDESC - 1)
     85 #define	STE_NEXTRX(x)		(((x) + 1) & STE_NRXDESC_MASK)
     86 
     87 /*
     88  * Control structures are DMA'd to the ST-201 chip.  We allocate them in
     89  * a single clump that maps to a single DMA segment to make several things
     90  * easier.
     91  */
     92 struct ste_control_data {
     93 	/*
     94 	 * The transmit descriptors.
     95 	 */
     96 	struct ste_tfd scd_txdescs[STE_NTXDESC];
     97 
     98 	/*
     99 	 * The receive descriptors.
    100 	 */
    101 	struct ste_rfd scd_rxdescs[STE_NRXDESC];
    102 };
    103 
    104 #define	STE_CDOFF(x)	offsetof(struct ste_control_data, x)
    105 #define	STE_CDTXOFF(x)	STE_CDOFF(scd_txdescs[(x)])
    106 #define	STE_CDRXOFF(x)	STE_CDOFF(scd_rxdescs[(x)])
    107 
    108 /*
    109  * Software state for transmit and receive jobs.
    110  */
    111 struct ste_descsoft {
    112 	struct mbuf *ds_mbuf;		/* head of our mbuf chain */
    113 	bus_dmamap_t ds_dmamap;		/* our DMA map */
    114 };
    115 
    116 /*
    117  * Software state per device.
    118  */
    119 struct ste_softc {
    120 	device_t sc_dev;		/* generic device information */
    121 	bus_space_tag_t sc_st;		/* bus space tag */
    122 	bus_space_handle_t sc_sh;	/* bus space handle */
    123 	bus_dma_tag_t sc_dmat;		/* bus DMA tag */
    124 	struct ethercom sc_ethercom;	/* ethernet common data */
    125 
    126 	void *sc_ih;			/* interrupt cookie */
    127 
    128 	struct mii_data sc_mii;		/* MII/media information */
    129 
    130 	callout_t sc_tick_ch;		/* tick callout */
    131 
    132 	bus_dmamap_t sc_cddmamap;	/* control data DMA map */
    133 #define	sc_cddma	sc_cddmamap->dm_segs[0].ds_addr
    134 
    135 	/*
    136 	 * Software state for transmit and receive descriptors.
    137 	 */
    138 	struct ste_descsoft sc_txsoft[STE_NTXDESC];
    139 	struct ste_descsoft sc_rxsoft[STE_NRXDESC];
    140 
    141 	/*
    142 	 * Control data structures.
    143 	 */
    144 	struct ste_control_data *sc_control_data;
    145 #define	sc_txdescs	sc_control_data->scd_txdescs
    146 #define	sc_rxdescs	sc_control_data->scd_rxdescs
    147 
    148 	int	sc_txpending;		/* number of Tx requests pending */
    149 	int	sc_txdirty;		/* first dirty Tx descriptor */
    150 	int	sc_txlast;		/* last used Tx descriptor */
    151 
    152 	int	sc_rxptr;		/* next ready Rx descriptor/descsoft */
    153 
    154 	int	sc_txthresh;		/* Tx threshold */
    155 	uint32_t sc_DMACtrl;		/* prototype DMACtrl register */
    156 	uint16_t sc_IntEnable;		/* prototype IntEnable register */
    157 	uint16_t sc_MacCtrl0;		/* prototype MacCtrl0 register */
    158 	uint8_t	sc_ReceiveMode;		/* prototype ReceiveMode register */
    159 };
    160 
    161 #define	STE_CDTXADDR(sc, x)	((sc)->sc_cddma + STE_CDTXOFF((x)))
    162 #define	STE_CDRXADDR(sc, x)	((sc)->sc_cddma + STE_CDRXOFF((x)))
    163 
    164 #define	STE_CDTXSYNC(sc, x, ops)					\
    165 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    166 	    STE_CDTXOFF((x)), sizeof(struct ste_tfd), (ops))
    167 
    168 #define	STE_CDRXSYNC(sc, x, ops)					\
    169 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    170 	    STE_CDRXOFF((x)), sizeof(struct ste_rfd), (ops))
    171 
    172 #define	STE_INIT_RXDESC(sc, x)						\
    173 do {									\
    174 	struct ste_descsoft *__ds = &(sc)->sc_rxsoft[(x)];		\
    175 	struct ste_rfd *__rfd = &(sc)->sc_rxdescs[(x)];			\
    176 	struct mbuf *__m = __ds->ds_mbuf;				\
    177 									\
    178 	/*								\
    179 	 * Note: We scoot the packet forward 2 bytes in the buffer	\
    180 	 * so that the payload after the Ethernet header is aligned	\
    181 	 * to a 4-byte boundary.					\
    182 	 */								\
    183 	__m->m_data = __m->m_ext.ext_buf + 2;				\
    184 	__rfd->rfd_frag.frag_addr =					\
    185 	    htole32(__ds->ds_dmamap->dm_segs[0].ds_addr + 2);		\
    186 	__rfd->rfd_frag.frag_len = htole32((MCLBYTES - 2) | FRAG_LAST);	\
    187 	__rfd->rfd_next = htole32(STE_CDRXADDR((sc), STE_NEXTRX((x))));	\
    188 	__rfd->rfd_status = 0;						\
    189 	STE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
    190 } while (/*CONSTCOND*/0)
    191 
    192 #define STE_TIMEOUT 1000
    193 
    194 static void	ste_start(struct ifnet *);
    195 static void	ste_watchdog(struct ifnet *);
    196 static int	ste_ioctl(struct ifnet *, u_long, void *);
    197 static int	ste_init(struct ifnet *);
    198 static void	ste_stop(struct ifnet *, int);
    199 
    200 static bool	ste_shutdown(device_t, int);
    201 
    202 static void	ste_reset(struct ste_softc *, u_int32_t);
    203 static void	ste_setthresh(struct ste_softc *);
    204 static void	ste_txrestart(struct ste_softc *, u_int8_t);
    205 static void	ste_rxdrain(struct ste_softc *);
    206 static int	ste_add_rxbuf(struct ste_softc *, int);
    207 static void	ste_read_eeprom(struct ste_softc *, int, uint16_t *);
    208 static void	ste_tick(void *);
    209 
    210 static void	ste_stats_update(struct ste_softc *);
    211 
    212 static void	ste_set_filter(struct ste_softc *);
    213 
    214 static int	ste_intr(void *);
    215 static void	ste_txintr(struct ste_softc *);
    216 static void	ste_rxintr(struct ste_softc *);
    217 
    218 static int	ste_mii_readreg(device_t, int, int);
    219 static void	ste_mii_writereg(device_t, int, int, int);
    220 static void	ste_mii_statchg(struct ifnet *);
    221 
    222 static int	ste_match(device_t, cfdata_t, void *);
    223 static void	ste_attach(device_t, device_t, void *);
    224 
    225 int	ste_copy_small = 0;
    226 
    227 CFATTACH_DECL_NEW(ste, sizeof(struct ste_softc),
    228     ste_match, ste_attach, NULL, NULL);
    229 
    230 static uint32_t ste_mii_bitbang_read(device_t);
    231 static void	ste_mii_bitbang_write(device_t, uint32_t);
    232 
    233 static const struct mii_bitbang_ops ste_mii_bitbang_ops = {
    234 	ste_mii_bitbang_read,
    235 	ste_mii_bitbang_write,
    236 	{
    237 		PC_MgmtData,		/* MII_BIT_MDO */
    238 		PC_MgmtData,		/* MII_BIT_MDI */
    239 		PC_MgmtClk,		/* MII_BIT_MDC */
    240 		PC_MgmtDir,		/* MII_BIT_DIR_HOST_PHY */
    241 		0,			/* MII_BIT_DIR_PHY_HOST */
    242 	}
    243 };
    244 
    245 /*
    246  * Devices supported by this driver.
    247  */
    248 static const struct ste_product {
    249 	pci_vendor_id_t		ste_vendor;
    250 	pci_product_id_t	ste_product;
    251 	const char		*ste_name;
    252 } ste_products[] = {
    253 	{ PCI_VENDOR_SUNDANCETI, 	PCI_PRODUCT_SUNDANCETI_IP100A,
    254 	  "IC Plus Corp. IP00A 10/100 Fast Ethernet Adapter" },
    255 
    256 	{ PCI_VENDOR_SUNDANCETI,	PCI_PRODUCT_SUNDANCETI_ST201,
    257 	  "Sundance ST-201 10/100 Ethernet" },
    258 
    259 	{ PCI_VENDOR_DLINK,		PCI_PRODUCT_DLINK_DL1002,
    260 	  "D-Link DL-1002 10/100 Ethernet" },
    261 
    262 	{ 0,				0,
    263 	  NULL },
    264 };
    265 
    266 static const struct ste_product *
    267 ste_lookup(const struct pci_attach_args *pa)
    268 {
    269 	const struct ste_product *sp;
    270 
    271 	for (sp = ste_products; sp->ste_name != NULL; sp++) {
    272 		if (PCI_VENDOR(pa->pa_id) == sp->ste_vendor &&
    273 		    PCI_PRODUCT(pa->pa_id) == sp->ste_product)
    274 			return (sp);
    275 	}
    276 	return (NULL);
    277 }
    278 
    279 static int
    280 ste_match(device_t parent, cfdata_t cf, void *aux)
    281 {
    282 	struct pci_attach_args *pa = aux;
    283 
    284 	if (ste_lookup(pa) != NULL)
    285 		return (1);
    286 
    287 	return (0);
    288 }
    289 
    290 static void
    291 ste_attach(device_t parent, device_t self, void *aux)
    292 {
    293 	struct ste_softc *sc = device_private(self);
    294 	struct pci_attach_args *pa = aux;
    295 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    296 	pci_chipset_tag_t pc = pa->pa_pc;
    297 	pci_intr_handle_t ih;
    298 	const char *intrstr = NULL;
    299 	bus_space_tag_t iot, memt;
    300 	bus_space_handle_t ioh, memh;
    301 	bus_dma_segment_t seg;
    302 	int ioh_valid, memh_valid;
    303 	int i, rseg, error;
    304 	const struct ste_product *sp;
    305 	uint8_t enaddr[ETHER_ADDR_LEN];
    306 	uint16_t myea[ETHER_ADDR_LEN / 2];
    307 	char intrbuf[PCI_INTRSTR_LEN];
    308 
    309 	sc->sc_dev = self;
    310 
    311 	callout_init(&sc->sc_tick_ch, 0);
    312 
    313 	sp = ste_lookup(pa);
    314 	if (sp == NULL) {
    315 		printf("\n");
    316 		panic("ste_attach: impossible");
    317 	}
    318 
    319 	printf(": %s\n", sp->ste_name);
    320 
    321 	/*
    322 	 * Map the device.
    323 	 */
    324 	ioh_valid = (pci_mapreg_map(pa, STE_PCI_IOBA,
    325 	    PCI_MAPREG_TYPE_IO, 0,
    326 	    &iot, &ioh, NULL, NULL) == 0);
    327 	memh_valid = (pci_mapreg_map(pa, STE_PCI_MMBA,
    328 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    329 	    &memt, &memh, NULL, NULL) == 0);
    330 
    331 	if (memh_valid) {
    332 		sc->sc_st = memt;
    333 		sc->sc_sh = memh;
    334 	} else if (ioh_valid) {
    335 		sc->sc_st = iot;
    336 		sc->sc_sh = ioh;
    337 	} else {
    338 		aprint_error_dev(self, "unable to map device registers\n");
    339 		return;
    340 	}
    341 
    342 	sc->sc_dmat = pa->pa_dmat;
    343 
    344 	/* Enable bus mastering. */
    345 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    346 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    347 	    PCI_COMMAND_MASTER_ENABLE);
    348 
    349 	/* power up chip */
    350 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self,
    351 	    NULL)) && error != EOPNOTSUPP) {
    352 		aprint_error_dev(sc->sc_dev, "cannot activate %d\n", error);
    353 		return;
    354 	}
    355 
    356 	/*
    357 	 * Map and establish our interrupt.
    358 	 */
    359 	if (pci_intr_map(pa, &ih)) {
    360 		aprint_error_dev(sc->sc_dev, "unable to map interrupt\n");
    361 		return;
    362 	}
    363 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    364 	sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_NET, ste_intr, sc,
    365 	    device_xname(self));
    366 	if (sc->sc_ih == NULL) {
    367 		aprint_error_dev(sc->sc_dev, "unable to establish interrupt");
    368 		if (intrstr != NULL)
    369 			aprint_error(" at %s", intrstr);
    370 		aprint_error("\n");
    371 		return;
    372 	}
    373 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
    374 
    375 	/*
    376 	 * Allocate the control data structures, and create and load the
    377 	 * DMA map for it.
    378 	 */
    379 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    380 	    sizeof(struct ste_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
    381 	    0)) != 0) {
    382 		aprint_error_dev(sc->sc_dev,
    383 		    "unable to allocate control data, error = %d\n", error);
    384 		goto fail_0;
    385 	}
    386 
    387 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    388 	    sizeof(struct ste_control_data), (void **)&sc->sc_control_data,
    389 	    BUS_DMA_COHERENT)) != 0) {
    390 		aprint_error_dev(sc->sc_dev,
    391 		    "unable to map control data, error = %d\n", error);
    392 		goto fail_1;
    393 	}
    394 
    395 	if ((error = bus_dmamap_create(sc->sc_dmat,
    396 	    sizeof(struct ste_control_data), 1,
    397 	    sizeof(struct ste_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    398 		aprint_error_dev(sc->sc_dev,
    399 		    "unable to create control data DMA map, error = %d\n",
    400 		    error);
    401 		goto fail_2;
    402 	}
    403 
    404 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    405 	    sc->sc_control_data, sizeof(struct ste_control_data), NULL,
    406 	    0)) != 0) {
    407 		aprint_error_dev(sc->sc_dev,
    408 		    "unable to load control data DMA map, error = %d\n",
    409 		    error);
    410 		goto fail_3;
    411 	}
    412 
    413 	/*
    414 	 * Create the transmit buffer DMA maps.
    415 	 */
    416 	for (i = 0; i < STE_NTXDESC; i++) {
    417 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    418 		    STE_NTXFRAGS, MCLBYTES, 0, 0,
    419 		    &sc->sc_txsoft[i].ds_dmamap)) != 0) {
    420 			aprint_error_dev(sc->sc_dev,
    421 			    "unable to create tx DMA map %d, error = %d\n", i,
    422 			    error);
    423 			goto fail_4;
    424 		}
    425 	}
    426 
    427 	/*
    428 	 * Create the receive buffer DMA maps.
    429 	 */
    430 	for (i = 0; i < STE_NRXDESC; i++) {
    431 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    432 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].ds_dmamap)) != 0) {
    433 			aprint_error_dev(sc->sc_dev,
    434 			    "unable to create rx DMA map %d, error = %d\n", i,
    435 			    error);
    436 			goto fail_5;
    437 		}
    438 		sc->sc_rxsoft[i].ds_mbuf = NULL;
    439 	}
    440 
    441 	/*
    442 	 * Reset the chip to a known state.
    443 	 */
    444 	ste_reset(sc, AC_GlobalReset | AC_RxReset | AC_TxReset | AC_DMA |
    445 	    AC_FIFO | AC_Network | AC_Host | AC_AutoInit | AC_RstOut);
    446 
    447 	/*
    448 	 * Read the Ethernet address from the EEPROM.
    449 	 */
    450 	for (i = 0; i < 3; i++) {
    451 		ste_read_eeprom(sc, STE_EEPROM_StationAddress0 + i, &myea[i]);
    452 		myea[i] = le16toh(myea[i]);
    453 	}
    454 	memcpy(enaddr, myea, sizeof(enaddr));
    455 
    456 	printf("%s: Ethernet address %s\n", device_xname(sc->sc_dev),
    457 	    ether_sprintf(enaddr));
    458 
    459 	/*
    460 	 * Initialize our media structures and probe the MII.
    461 	 */
    462 	sc->sc_mii.mii_ifp = ifp;
    463 	sc->sc_mii.mii_readreg = ste_mii_readreg;
    464 	sc->sc_mii.mii_writereg = ste_mii_writereg;
    465 	sc->sc_mii.mii_statchg = ste_mii_statchg;
    466 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    467 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, ether_mediachange,
    468 	    ether_mediastatus);
    469 	mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    470 	    MII_OFFSET_ANY, 0);
    471 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    472 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0,NULL);
    473 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    474 	} else
    475 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    476 
    477 	ifp = &sc->sc_ethercom.ec_if;
    478 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    479 	ifp->if_softc = sc;
    480 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    481 	ifp->if_ioctl = ste_ioctl;
    482 	ifp->if_start = ste_start;
    483 	ifp->if_watchdog = ste_watchdog;
    484 	ifp->if_init = ste_init;
    485 	ifp->if_stop = ste_stop;
    486 	IFQ_SET_READY(&ifp->if_snd);
    487 
    488 	/*
    489 	 * Default the transmit threshold to 128 bytes.
    490 	 */
    491 	sc->sc_txthresh = 128;
    492 
    493 	/*
    494 	 * Disable MWI if the PCI layer tells us to.
    495 	 */
    496 	sc->sc_DMACtrl = 0;
    497 	if ((pa->pa_flags & PCI_FLAGS_MWI_OKAY) == 0)
    498 		sc->sc_DMACtrl |= DC_MWIDisable;
    499 
    500 	/*
    501 	 * We can support 802.1Q VLAN-sized frames.
    502 	 */
    503 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    504 
    505 	/*
    506 	 * Attach the interface.
    507 	 */
    508 	if_attach(ifp);
    509 	if_deferred_start_init(ifp, NULL);
    510 	ether_ifattach(ifp, enaddr);
    511 
    512 	/*
    513 	 * Make sure the interface is shutdown during reboot.
    514 	 */
    515 	if (pmf_device_register1(self, NULL, NULL, ste_shutdown))
    516 		pmf_class_network_register(self, ifp);
    517 	else
    518 		aprint_error_dev(self, "couldn't establish power handler\n");
    519 
    520 	return;
    521 
    522 	/*
    523 	 * Free any resources we've allocated during the failed attach
    524 	 * attempt.  Do this in reverse order and fall through.
    525 	 */
    526  fail_5:
    527 	for (i = 0; i < STE_NRXDESC; i++) {
    528 		if (sc->sc_rxsoft[i].ds_dmamap != NULL)
    529 			bus_dmamap_destroy(sc->sc_dmat,
    530 			    sc->sc_rxsoft[i].ds_dmamap);
    531 	}
    532  fail_4:
    533 	for (i = 0; i < STE_NTXDESC; i++) {
    534 		if (sc->sc_txsoft[i].ds_dmamap != NULL)
    535 			bus_dmamap_destroy(sc->sc_dmat,
    536 			    sc->sc_txsoft[i].ds_dmamap);
    537 	}
    538 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    539  fail_3:
    540 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    541  fail_2:
    542 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    543 	    sizeof(struct ste_control_data));
    544  fail_1:
    545 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    546  fail_0:
    547 	return;
    548 }
    549 
    550 /*
    551  * ste_shutdown:
    552  *
    553  *	Make sure the interface is stopped at reboot time.
    554  */
    555 static bool
    556 ste_shutdown(device_t self, int howto)
    557 {
    558 	struct ste_softc *sc;
    559 
    560 	sc = device_private(self);
    561 	ste_stop(&sc->sc_ethercom.ec_if, 1);
    562 
    563 	return true;
    564 }
    565 
    566 static void
    567 ste_dmahalt_wait(struct ste_softc *sc)
    568 {
    569 	int i;
    570 
    571 	for (i = 0; i < STE_TIMEOUT; i++) {
    572 		delay(2);
    573 		if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STE_DMACtrl) &
    574 		     DC_DMAHaltBusy) == 0)
    575 			break;
    576 	}
    577 
    578 	if (i == STE_TIMEOUT)
    579 		printf("%s: DMA halt timed out\n", device_xname(sc->sc_dev));
    580 }
    581 
    582 /*
    583  * ste_start:		[ifnet interface function]
    584  *
    585  *	Start packet transmission on the interface.
    586  */
    587 static void
    588 ste_start(struct ifnet *ifp)
    589 {
    590 	struct ste_softc *sc = ifp->if_softc;
    591 	struct mbuf *m0, *m;
    592 	struct ste_descsoft *ds;
    593 	struct ste_tfd *tfd;
    594 	bus_dmamap_t dmamap;
    595 	int error, olasttx, nexttx, opending, seg, totlen;
    596 
    597 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    598 		return;
    599 
    600 	/*
    601 	 * Remember the previous number of pending transmissions
    602 	 * and the current last descriptor in the list.
    603 	 */
    604 	opending = sc->sc_txpending;
    605 	olasttx = sc->sc_txlast;
    606 
    607 	/*
    608 	 * Loop through the send queue, setting up transmit descriptors
    609 	 * until we drain the queue, or use up all available transmit
    610 	 * descriptors.
    611 	 */
    612 	while (sc->sc_txpending < STE_NTXDESC) {
    613 		/*
    614 		 * Grab a packet off the queue.
    615 		 */
    616 		IFQ_POLL(&ifp->if_snd, m0);
    617 		if (m0 == NULL)
    618 			break;
    619 		m = NULL;
    620 
    621 		/*
    622 		 * Get the last and next available transmit descriptor.
    623 		 */
    624 		nexttx = STE_NEXTTX(sc->sc_txlast);
    625 		tfd = &sc->sc_txdescs[nexttx];
    626 		ds = &sc->sc_txsoft[nexttx];
    627 
    628 		dmamap = ds->ds_dmamap;
    629 
    630 		/*
    631 		 * Load the DMA map.  If this fails, the packet either
    632 		 * didn't fit in the alloted number of segments, or we
    633 		 * were short on resources.  In this case, we'll copy
    634 		 * and try again.
    635 		 */
    636 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    637 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
    638 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    639 			if (m == NULL) {
    640 				printf("%s: unable to allocate Tx mbuf\n",
    641 				    device_xname(sc->sc_dev));
    642 				break;
    643 			}
    644 			if (m0->m_pkthdr.len > MHLEN) {
    645 				MCLGET(m, M_DONTWAIT);
    646 				if ((m->m_flags & M_EXT) == 0) {
    647 					printf("%s: unable to allocate Tx "
    648 					    "cluster\n",
    649 					    device_xname(sc->sc_dev));
    650 					m_freem(m);
    651 					break;
    652 				}
    653 			}
    654 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
    655 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    656 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    657 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    658 			if (error) {
    659 				printf("%s: unable to load Tx buffer, "
    660 				    "error = %d\n", device_xname(sc->sc_dev),
    661 				    error);
    662 				break;
    663 			}
    664 		}
    665 
    666 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    667 		if (m != NULL) {
    668 			m_freem(m0);
    669 			m0 = m;
    670 		}
    671 
    672 		/*
    673 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    674 		 */
    675 
    676 		/* Sync the DMA map. */
    677 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    678 		    BUS_DMASYNC_PREWRITE);
    679 
    680 		/* Initialize the fragment list. */
    681 		for (totlen = 0, seg = 0; seg < dmamap->dm_nsegs; seg++) {
    682 			tfd->tfd_frags[seg].frag_addr =
    683 			    htole32(dmamap->dm_segs[seg].ds_addr);
    684 			tfd->tfd_frags[seg].frag_len =
    685 			    htole32(dmamap->dm_segs[seg].ds_len);
    686 			totlen += dmamap->dm_segs[seg].ds_len;
    687 		}
    688 		tfd->tfd_frags[seg - 1].frag_len |= htole32(FRAG_LAST);
    689 
    690 		/* Initialize the descriptor. */
    691 		tfd->tfd_next = htole32(STE_CDTXADDR(sc, nexttx));
    692 		tfd->tfd_control = htole32(TFD_FrameId(nexttx) | (totlen & 3));
    693 
    694 		/* Sync the descriptor. */
    695 		STE_CDTXSYNC(sc, nexttx,
    696 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    697 
    698 		/*
    699 		 * Store a pointer to the packet so we can free it later,
    700 		 * and remember what txdirty will be once the packet is
    701 		 * done.
    702 		 */
    703 		ds->ds_mbuf = m0;
    704 
    705 		/* Advance the tx pointer. */
    706 		sc->sc_txpending++;
    707 		sc->sc_txlast = nexttx;
    708 
    709 		/*
    710 		 * Pass the packet to any BPF listeners.
    711 		 */
    712 		bpf_mtap(ifp, m0, BPF_D_OUT);
    713 	}
    714 
    715 	if (sc->sc_txpending == STE_NTXDESC) {
    716 		/* No more slots left; notify upper layer. */
    717 		ifp->if_flags |= IFF_OACTIVE;
    718 	}
    719 
    720 	if (sc->sc_txpending != opending) {
    721 		/*
    722 		 * We enqueued packets.  If the transmitter was idle,
    723 		 * reset the txdirty pointer.
    724 		 */
    725 		if (opending == 0)
    726 			sc->sc_txdirty = STE_NEXTTX(olasttx);
    727 
    728 		/*
    729 		 * Cause a descriptor interrupt to happen on the
    730 		 * last packet we enqueued, and also cause the
    731 		 * DMA engine to wait after is has finished processing
    732 		 * it.
    733 		 */
    734 		sc->sc_txdescs[sc->sc_txlast].tfd_next = 0;
    735 		sc->sc_txdescs[sc->sc_txlast].tfd_control |=
    736 		    htole32(TFD_TxDMAIndicate);
    737 		STE_CDTXSYNC(sc, sc->sc_txlast,
    738 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    739 
    740 		/*
    741 		 * Link up the new chain of descriptors to the
    742 		 * last.
    743 		 */
    744 		sc->sc_txdescs[olasttx].tfd_next =
    745 		    htole32(STE_CDTXADDR(sc, STE_NEXTTX(olasttx)));
    746 		STE_CDTXSYNC(sc, olasttx,
    747 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    748 
    749 		/*
    750 		 * Kick the transmit DMA logic.  Note that since we're
    751 		 * using auto-polling, reading the Tx desc pointer will
    752 		 * give it the nudge it needs to get going.
    753 		 */
    754 		if (bus_space_read_4(sc->sc_st, sc->sc_sh,
    755 		    STE_TxDMAListPtr) == 0) {
    756 			bus_space_write_4(sc->sc_st, sc->sc_sh,
    757 			    STE_DMACtrl, DC_TxDMAHalt);
    758 			ste_dmahalt_wait(sc);
    759 			bus_space_write_4(sc->sc_st, sc->sc_sh,
    760 			    STE_TxDMAListPtr,
    761 			    STE_CDTXADDR(sc, STE_NEXTTX(olasttx)));
    762 			bus_space_write_4(sc->sc_st, sc->sc_sh,
    763 			    STE_DMACtrl, DC_TxDMAResume);
    764 		}
    765 
    766 		/* Set a watchdog timer in case the chip flakes out. */
    767 		ifp->if_timer = 5;
    768 	}
    769 }
    770 
    771 /*
    772  * ste_watchdog:	[ifnet interface function]
    773  *
    774  *	Watchdog timer handler.
    775  */
    776 static void
    777 ste_watchdog(struct ifnet *ifp)
    778 {
    779 	struct ste_softc *sc = ifp->if_softc;
    780 
    781 	printf("%s: device timeout\n", device_xname(sc->sc_dev));
    782 	ifp->if_oerrors++;
    783 
    784 	ste_txintr(sc);
    785 	ste_rxintr(sc);
    786 	(void) ste_init(ifp);
    787 
    788 	/* Try to get more packets going. */
    789 	ste_start(ifp);
    790 }
    791 
    792 /*
    793  * ste_ioctl:		[ifnet interface function]
    794  *
    795  *	Handle control requests from the operator.
    796  */
    797 static int
    798 ste_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    799 {
    800 	struct ste_softc *sc = ifp->if_softc;
    801 	int s, error;
    802 
    803 	s = splnet();
    804 
    805 	error = ether_ioctl(ifp, cmd, data);
    806 	if (error == ENETRESET) {
    807 		/*
    808 		 * Multicast list has changed; set the hardware filter
    809 		 * accordingly.
    810 		 */
    811 		if (ifp->if_flags & IFF_RUNNING)
    812 			ste_set_filter(sc);
    813 		error = 0;
    814 	}
    815 
    816 	/* Try to get more packets going. */
    817 	ste_start(ifp);
    818 
    819 	splx(s);
    820 	return (error);
    821 }
    822 
    823 /*
    824  * ste_intr:
    825  *
    826  *	Interrupt service routine.
    827  */
    828 static int
    829 ste_intr(void *arg)
    830 {
    831 	struct ste_softc *sc = arg;
    832 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    833 	uint16_t isr;
    834 	uint8_t txstat;
    835 	int wantinit;
    836 
    837 	if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STE_IntStatus) &
    838 	     IS_InterruptStatus) == 0)
    839 		return (0);
    840 
    841 	for (wantinit = 0; wantinit == 0;) {
    842 		isr = bus_space_read_2(sc->sc_st, sc->sc_sh, STE_IntStatusAck);
    843 		if ((isr & sc->sc_IntEnable) == 0)
    844 			break;
    845 
    846 		/* Receive interrupts. */
    847 		if (isr & IE_RxDMAComplete)
    848 			ste_rxintr(sc);
    849 
    850 		/* Transmit interrupts. */
    851 		if (isr & (IE_TxDMAComplete|IE_TxComplete))
    852 			ste_txintr(sc);
    853 
    854 		/* Statistics overflow. */
    855 		if (isr & IE_UpdateStats)
    856 			ste_stats_update(sc);
    857 
    858 		/* Transmission errors. */
    859 		if (isr & IE_TxComplete) {
    860 			for (;;) {
    861 				txstat = bus_space_read_1(sc->sc_st, sc->sc_sh,
    862 				    STE_TxStatus);
    863 				if ((txstat & TS_TxComplete) == 0)
    864 					break;
    865 				if (txstat & TS_TxUnderrun) {
    866 					sc->sc_txthresh += 32;
    867 					if (sc->sc_txthresh > 0x1ffc)
    868 						sc->sc_txthresh = 0x1ffc;
    869 					printf("%s: transmit underrun, new "
    870 					    "threshold: %d bytes\n",
    871 					    device_xname(sc->sc_dev),
    872 					    sc->sc_txthresh);
    873 					ste_reset(sc, AC_TxReset | AC_DMA |
    874 					    AC_FIFO | AC_Network);
    875 					ste_setthresh(sc);
    876 					bus_space_write_1(sc->sc_st, sc->sc_sh,
    877 					    STE_TxDMAPollPeriod, 127);
    878 					ste_txrestart(sc,
    879 					    bus_space_read_1(sc->sc_st,
    880 						sc->sc_sh, STE_TxFrameId));
    881 				}
    882 				if (txstat & TS_TxReleaseError) {
    883 					printf("%s: Tx FIFO release error\n",
    884 					    device_xname(sc->sc_dev));
    885 					wantinit = 1;
    886 				}
    887 				if (txstat & TS_MaxCollisions) {
    888 					printf("%s: excessive collisions\n",
    889 					    device_xname(sc->sc_dev));
    890 					wantinit = 1;
    891 				}
    892 				if (txstat & TS_TxStatusOverflow) {
    893 					printf("%s: status overflow\n",
    894 					    device_xname(sc->sc_dev));
    895 					wantinit = 1;
    896 				}
    897 				bus_space_write_2(sc->sc_st, sc->sc_sh,
    898 				    STE_TxStatus, 0);
    899 			}
    900 		}
    901 
    902 		/* Host interface errors. */
    903 		if (isr & IE_HostError) {
    904 			printf("%s: Host interface error\n",
    905 			    device_xname(sc->sc_dev));
    906 			wantinit = 1;
    907 		}
    908 	}
    909 
    910 	if (wantinit)
    911 		ste_init(ifp);
    912 
    913 	bus_space_write_2(sc->sc_st, sc->sc_sh, STE_IntEnable,
    914 	    sc->sc_IntEnable);
    915 
    916 	/* Try to get more packets going. */
    917 	if_schedule_deferred_start(ifp);
    918 
    919 	return (1);
    920 }
    921 
    922 /*
    923  * ste_txintr:
    924  *
    925  *	Helper; handle transmit interrupts.
    926  */
    927 static void
    928 ste_txintr(struct ste_softc *sc)
    929 {
    930 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    931 	struct ste_descsoft *ds;
    932 	uint32_t control;
    933 	int i;
    934 
    935 	ifp->if_flags &= ~IFF_OACTIVE;
    936 
    937 	/*
    938 	 * Go through our Tx list and free mbufs for those
    939 	 * frames which have been transmitted.
    940 	 */
    941 	for (i = sc->sc_txdirty; sc->sc_txpending != 0;
    942 	     i = STE_NEXTTX(i), sc->sc_txpending--) {
    943 		ds = &sc->sc_txsoft[i];
    944 
    945 		STE_CDTXSYNC(sc, i,
    946 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    947 
    948 		control = le32toh(sc->sc_txdescs[i].tfd_control);
    949 		if ((control & TFD_TxDMAComplete) == 0)
    950 			break;
    951 
    952 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
    953 		    0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    954 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
    955 		m_freem(ds->ds_mbuf);
    956 		ds->ds_mbuf = NULL;
    957 	}
    958 
    959 	/* Update the dirty transmit buffer pointer. */
    960 	sc->sc_txdirty = i;
    961 
    962 	/*
    963 	 * If there are no more pending transmissions, cancel the watchdog
    964 	 * timer.
    965 	 */
    966 	if (sc->sc_txpending == 0)
    967 		ifp->if_timer = 0;
    968 }
    969 
    970 /*
    971  * ste_rxintr:
    972  *
    973  *	Helper; handle receive interrupts.
    974  */
    975 static void
    976 ste_rxintr(struct ste_softc *sc)
    977 {
    978 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    979 	struct ste_descsoft *ds;
    980 	struct mbuf *m;
    981 	uint32_t status;
    982 	int i, len;
    983 
    984 	for (i = sc->sc_rxptr;; i = STE_NEXTRX(i)) {
    985 		ds = &sc->sc_rxsoft[i];
    986 
    987 		STE_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    988 
    989 		status = le32toh(sc->sc_rxdescs[i].rfd_status);
    990 
    991 		if ((status & RFD_RxDMAComplete) == 0)
    992 			break;
    993 
    994 		/*
    995 		 * If the packet had an error, simply recycle the
    996 		 * buffer.  Note, we count the error later in the
    997 		 * periodic stats update.
    998 		 */
    999 		if (status & RFD_RxFrameError) {
   1000 			STE_INIT_RXDESC(sc, i);
   1001 			continue;
   1002 		}
   1003 
   1004 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
   1005 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1006 
   1007 		/*
   1008 		 * No errors; receive the packet.  Note, we have
   1009 		 * configured the chip to not include the CRC at
   1010 		 * the end of the packet.
   1011 		 */
   1012 		len = RFD_RxDMAFrameLen(status);
   1013 
   1014 		/*
   1015 		 * If the packet is small enough to fit in a
   1016 		 * single header mbuf, allocate one and copy
   1017 		 * the data into it.  This greatly reduces
   1018 		 * memory consumption when we receive lots
   1019 		 * of small packets.
   1020 		 *
   1021 		 * Otherwise, we add a new buffer to the receive
   1022 		 * chain.  If this fails, we drop the packet and
   1023 		 * recycle the old buffer.
   1024 		 */
   1025 		if (ste_copy_small != 0 && len <= (MHLEN - 2)) {
   1026 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1027 			if (m == NULL)
   1028 				goto dropit;
   1029 			m->m_data += 2;
   1030 			memcpy(mtod(m, void *),
   1031 			    mtod(ds->ds_mbuf, void *), len);
   1032 			STE_INIT_RXDESC(sc, i);
   1033 			bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
   1034 			    ds->ds_dmamap->dm_mapsize,
   1035 			    BUS_DMASYNC_PREREAD);
   1036 		} else {
   1037 			m = ds->ds_mbuf;
   1038 			if (ste_add_rxbuf(sc, i) != 0) {
   1039  dropit:
   1040 				ifp->if_ierrors++;
   1041 				STE_INIT_RXDESC(sc, i);
   1042 				bus_dmamap_sync(sc->sc_dmat,
   1043 				    ds->ds_dmamap, 0,
   1044 				    ds->ds_dmamap->dm_mapsize,
   1045 				    BUS_DMASYNC_PREREAD);
   1046 				continue;
   1047 			}
   1048 		}
   1049 
   1050 		m_set_rcvif(m, ifp);
   1051 		m->m_pkthdr.len = m->m_len = len;
   1052 
   1053 		/* Pass it on. */
   1054 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1055 	}
   1056 
   1057 	/* Update the receive pointer. */
   1058 	sc->sc_rxptr = i;
   1059 }
   1060 
   1061 /*
   1062  * ste_tick:
   1063  *
   1064  *	One second timer, used to tick the MII.
   1065  */
   1066 static void
   1067 ste_tick(void *arg)
   1068 {
   1069 	struct ste_softc *sc = arg;
   1070 	int s;
   1071 
   1072 	s = splnet();
   1073 	mii_tick(&sc->sc_mii);
   1074 	ste_stats_update(sc);
   1075 	splx(s);
   1076 
   1077 	callout_reset(&sc->sc_tick_ch, hz, ste_tick, sc);
   1078 }
   1079 
   1080 /*
   1081  * ste_stats_update:
   1082  *
   1083  *	Read the ST-201 statistics counters.
   1084  */
   1085 static void
   1086 ste_stats_update(struct ste_softc *sc)
   1087 {
   1088 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1089 	bus_space_tag_t st = sc->sc_st;
   1090 	bus_space_handle_t sh = sc->sc_sh;
   1091 
   1092 	(void) bus_space_read_2(st, sh, STE_OctetsReceivedOk0);
   1093 	(void) bus_space_read_2(st, sh, STE_OctetsReceivedOk1);
   1094 
   1095 	(void) bus_space_read_2(st, sh, STE_OctetsTransmittedOk0);
   1096 	(void) bus_space_read_2(st, sh, STE_OctetsTransmittedOk1);
   1097 
   1098 	ifp->if_opackets +=
   1099 	    (u_int) bus_space_read_2(st, sh, STE_FramesTransmittedOK);
   1100 	ifp->if_ipackets +=
   1101 	    (u_int) bus_space_read_2(st, sh, STE_FramesReceivedOK);
   1102 
   1103 	ifp->if_collisions +=
   1104 	    (u_int) bus_space_read_1(st, sh, STE_LateCollisions) +
   1105 	    (u_int) bus_space_read_1(st, sh, STE_MultipleColFrames) +
   1106 	    (u_int) bus_space_read_1(st, sh, STE_SingleColFrames);
   1107 
   1108 	(void) bus_space_read_1(st, sh, STE_FramesWDeferredXmt);
   1109 
   1110 	ifp->if_ierrors +=
   1111 	    (u_int) bus_space_read_1(st, sh, STE_FramesLostRxErrors);
   1112 
   1113 	ifp->if_oerrors +=
   1114 	    (u_int) bus_space_read_1(st, sh, STE_FramesWExDeferral) +
   1115 	    (u_int) bus_space_read_1(st, sh, STE_FramesXbortXSColls) +
   1116 	    bus_space_read_1(st, sh, STE_CarrierSenseErrors);
   1117 
   1118 	(void) bus_space_read_1(st, sh, STE_BcstFramesXmtdOk);
   1119 	(void) bus_space_read_1(st, sh, STE_BcstFramesRcvdOk);
   1120 	(void) bus_space_read_1(st, sh, STE_McstFramesXmtdOk);
   1121 	(void) bus_space_read_1(st, sh, STE_McstFramesRcvdOk);
   1122 }
   1123 
   1124 /*
   1125  * ste_reset:
   1126  *
   1127  *	Perform a soft reset on the ST-201.
   1128  */
   1129 static void
   1130 ste_reset(struct ste_softc *sc, u_int32_t rstbits)
   1131 {
   1132 	uint32_t ac;
   1133 	int i;
   1134 
   1135 	ac = bus_space_read_4(sc->sc_st, sc->sc_sh, STE_AsicCtrl);
   1136 
   1137 	bus_space_write_4(sc->sc_st, sc->sc_sh, STE_AsicCtrl, ac | rstbits);
   1138 
   1139 	delay(50000);
   1140 
   1141 	for (i = 0; i < STE_TIMEOUT; i++) {
   1142 		delay(1000);
   1143 		if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STE_AsicCtrl) &
   1144 		     AC_ResetBusy) == 0)
   1145 			break;
   1146 	}
   1147 
   1148 	if (i == STE_TIMEOUT)
   1149 		printf("%s: reset failed to complete\n",
   1150 		    device_xname(sc->sc_dev));
   1151 
   1152 	delay(1000);
   1153 }
   1154 
   1155 /*
   1156  * ste_setthresh:
   1157  *
   1158  * 	set the various transmit threshold registers
   1159  */
   1160 static void
   1161 ste_setthresh(struct ste_softc *sc)
   1162 {
   1163 	/* set the TX threhold */
   1164 	bus_space_write_2(sc->sc_st, sc->sc_sh,
   1165 	    STE_TxStartThresh, sc->sc_txthresh);
   1166 	/* Urgent threshold: set to sc_txthresh / 2 */
   1167 	bus_space_write_2(sc->sc_st, sc->sc_sh, STE_TxDMAUrgentThresh,
   1168 	    sc->sc_txthresh >> 6);
   1169 	/* Burst threshold: use default value (256 bytes) */
   1170 }
   1171 
   1172 /*
   1173  * restart TX at the given frame ID in the transmitter ring
   1174  */
   1175 static void
   1176 ste_txrestart(struct ste_softc *sc, u_int8_t id)
   1177 {
   1178 	u_int32_t control;
   1179 
   1180 	STE_CDTXSYNC(sc, id, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1181 	control = le32toh(sc->sc_txdescs[id].tfd_control);
   1182 	control &= ~TFD_TxDMAComplete;
   1183 	sc->sc_txdescs[id].tfd_control = htole32(control);
   1184 	STE_CDTXSYNC(sc, id, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1185 
   1186 	bus_space_write_4(sc->sc_st, sc->sc_sh, STE_TxDMAListPtr, 0);
   1187 	bus_space_write_2(sc->sc_st, sc->sc_sh, STE_MacCtrl1, MC1_TxEnable);
   1188 	bus_space_write_4(sc->sc_st, sc->sc_sh, STE_DMACtrl, DC_TxDMAHalt);
   1189 	ste_dmahalt_wait(sc);
   1190 	bus_space_write_4(sc->sc_st, sc->sc_sh, STE_TxDMAListPtr,
   1191 	    STE_CDTXADDR(sc, id));
   1192 	bus_space_write_4(sc->sc_st, sc->sc_sh, STE_DMACtrl, DC_TxDMAResume);
   1193 }
   1194 
   1195 /*
   1196  * ste_init:		[ ifnet interface function ]
   1197  *
   1198  *	Initialize the interface.  Must be called at splnet().
   1199  */
   1200 static int
   1201 ste_init(struct ifnet *ifp)
   1202 {
   1203 	struct ste_softc *sc = ifp->if_softc;
   1204 	bus_space_tag_t st = sc->sc_st;
   1205 	bus_space_handle_t sh = sc->sc_sh;
   1206 	struct ste_descsoft *ds;
   1207 	int i, error = 0;
   1208 
   1209 	/*
   1210 	 * Cancel any pending I/O.
   1211 	 */
   1212 	ste_stop(ifp, 0);
   1213 
   1214 	/*
   1215 	 * Reset the chip to a known state.
   1216 	 */
   1217 	ste_reset(sc, AC_GlobalReset | AC_RxReset | AC_TxReset | AC_DMA |
   1218 	    AC_FIFO | AC_Network | AC_Host | AC_AutoInit | AC_RstOut);
   1219 
   1220 	/*
   1221 	 * Initialize the transmit descriptor ring.
   1222 	 */
   1223 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1224 	sc->sc_txpending = 0;
   1225 	sc->sc_txdirty = 0;
   1226 	sc->sc_txlast = STE_NTXDESC - 1;
   1227 
   1228 	/*
   1229 	 * Initialize the receive descriptor and receive job
   1230 	 * descriptor rings.
   1231 	 */
   1232 	for (i = 0; i < STE_NRXDESC; i++) {
   1233 		ds = &sc->sc_rxsoft[i];
   1234 		if (ds->ds_mbuf == NULL) {
   1235 			if ((error = ste_add_rxbuf(sc, i)) != 0) {
   1236 				printf("%s: unable to allocate or map rx "
   1237 				    "buffer %d, error = %d\n",
   1238 				    device_xname(sc->sc_dev), i, error);
   1239 				/*
   1240 				 * XXX Should attempt to run with fewer receive
   1241 				 * XXX buffers instead of just failing.
   1242 				 */
   1243 				ste_rxdrain(sc);
   1244 				goto out;
   1245 			}
   1246 		} else
   1247 			STE_INIT_RXDESC(sc, i);
   1248 	}
   1249 	sc->sc_rxptr = 0;
   1250 
   1251 	/* Set the station address. */
   1252 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1253 		bus_space_write_1(st, sh, STE_StationAddress0 + 1,
   1254 		    CLLADDR(ifp->if_sadl)[i]);
   1255 
   1256 	/* Set up the receive filter. */
   1257 	ste_set_filter(sc);
   1258 
   1259 	/*
   1260 	 * Give the receive ring to the chip.
   1261 	 */
   1262 	bus_space_write_4(st, sh, STE_RxDMAListPtr,
   1263 	    STE_CDRXADDR(sc, sc->sc_rxptr));
   1264 
   1265 	/*
   1266 	 * We defer giving the transmit ring to the chip until we
   1267 	 * transmit the first packet.
   1268 	 */
   1269 
   1270 	/*
   1271 	 * Initialize the Tx auto-poll period.  It's OK to make this number
   1272 	 * large (127 is the max) -- we explicitly kick the transmit engine
   1273 	 * when there's actually a packet.  We are using auto-polling only
   1274 	 * to make the interface to the transmit engine not suck.
   1275 	 */
   1276 	bus_space_write_1(sc->sc_st, sc->sc_sh, STE_TxDMAPollPeriod, 127);
   1277 
   1278 	/* ..and the Rx auto-poll period. */
   1279 	bus_space_write_1(st, sh, STE_RxDMAPollPeriod, 64);
   1280 
   1281 	/* Initialize the Tx start threshold. */
   1282 	ste_setthresh(sc);
   1283 
   1284 	/* Set the FIFO release threshold to 512 bytes. */
   1285 	bus_space_write_1(st, sh, STE_TxReleaseThresh, 512 >> 4);
   1286 
   1287 	/* Set maximum packet size for VLAN. */
   1288 	if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
   1289 		bus_space_write_2(st, sh, STE_MaxFrameSize, ETHER_MAX_LEN + 4);
   1290 	else
   1291 		bus_space_write_2(st, sh, STE_MaxFrameSize, ETHER_MAX_LEN);
   1292 
   1293 	/*
   1294 	 * Initialize the interrupt mask.
   1295 	 */
   1296 	sc->sc_IntEnable = IE_HostError | IE_TxComplete | IE_UpdateStats |
   1297 	    IE_TxDMAComplete | IE_RxDMAComplete;
   1298 
   1299 	bus_space_write_2(st, sh, STE_IntStatus, 0xffff);
   1300 	bus_space_write_2(st, sh, STE_IntEnable, sc->sc_IntEnable);
   1301 
   1302 	/*
   1303 	 * Start the receive DMA engine.
   1304 	 */
   1305 	bus_space_write_4(st, sh, STE_DMACtrl, sc->sc_DMACtrl | DC_RxDMAResume);
   1306 
   1307 	/*
   1308 	 * Initialize MacCtrl0 -- do it before setting the media,
   1309 	 * as setting the media will actually program the register.
   1310 	 */
   1311 	sc->sc_MacCtrl0 = MC0_IFSSelect(0);
   1312 	if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
   1313 		sc->sc_MacCtrl0 |= MC0_RcvLargeFrames;
   1314 
   1315 	/*
   1316 	 * Set the current media.
   1317 	 */
   1318 	if ((error = ether_mediachange(ifp)) != 0)
   1319 		goto out;
   1320 
   1321 	/*
   1322 	 * Start the MAC.
   1323 	 */
   1324 	bus_space_write_2(st, sh, STE_MacCtrl1,
   1325 	    MC1_StatisticsEnable | MC1_TxEnable | MC1_RxEnable);
   1326 
   1327 	/*
   1328 	 * Start the one second MII clock.
   1329 	 */
   1330 	callout_reset(&sc->sc_tick_ch, hz, ste_tick, sc);
   1331 
   1332 	/*
   1333 	 * ...all done!
   1334 	 */
   1335 	ifp->if_flags |= IFF_RUNNING;
   1336 	ifp->if_flags &= ~IFF_OACTIVE;
   1337 
   1338  out:
   1339 	if (error)
   1340 		printf("%s: interface not running\n", device_xname(sc->sc_dev));
   1341 	return (error);
   1342 }
   1343 
   1344 /*
   1345  * ste_drain:
   1346  *
   1347  *	Drain the receive queue.
   1348  */
   1349 static void
   1350 ste_rxdrain(struct ste_softc *sc)
   1351 {
   1352 	struct ste_descsoft *ds;
   1353 	int i;
   1354 
   1355 	for (i = 0; i < STE_NRXDESC; i++) {
   1356 		ds = &sc->sc_rxsoft[i];
   1357 		if (ds->ds_mbuf != NULL) {
   1358 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1359 			m_freem(ds->ds_mbuf);
   1360 			ds->ds_mbuf = NULL;
   1361 		}
   1362 	}
   1363 }
   1364 
   1365 /*
   1366  * ste_stop:		[ ifnet interface function ]
   1367  *
   1368  *	Stop transmission on the interface.
   1369  */
   1370 static void
   1371 ste_stop(struct ifnet *ifp, int disable)
   1372 {
   1373 	struct ste_softc *sc = ifp->if_softc;
   1374 	struct ste_descsoft *ds;
   1375 	int i;
   1376 
   1377 	/*
   1378 	 * Stop the one second clock.
   1379 	 */
   1380 	callout_stop(&sc->sc_tick_ch);
   1381 
   1382 	/* Down the MII. */
   1383 	mii_down(&sc->sc_mii);
   1384 
   1385 	/*
   1386 	 * Disable interrupts.
   1387 	 */
   1388 	bus_space_write_2(sc->sc_st, sc->sc_sh, STE_IntEnable, 0);
   1389 
   1390 	/*
   1391 	 * Stop receiver, transmitter, and stats update.
   1392 	 */
   1393 	bus_space_write_2(sc->sc_st, sc->sc_sh, STE_MacCtrl1,
   1394 	    MC1_StatisticsDisable | MC1_TxDisable | MC1_RxDisable);
   1395 
   1396 	/*
   1397 	 * Stop the transmit and receive DMA.
   1398 	 */
   1399 	bus_space_write_4(sc->sc_st, sc->sc_sh, STE_DMACtrl,
   1400 	    DC_RxDMAHalt | DC_TxDMAHalt);
   1401 	ste_dmahalt_wait(sc);
   1402 
   1403 	/*
   1404 	 * Release any queued transmit buffers.
   1405 	 */
   1406 	for (i = 0; i < STE_NTXDESC; i++) {
   1407 		ds = &sc->sc_txsoft[i];
   1408 		if (ds->ds_mbuf != NULL) {
   1409 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1410 			m_freem(ds->ds_mbuf);
   1411 			ds->ds_mbuf = NULL;
   1412 		}
   1413 	}
   1414 
   1415 	/*
   1416 	 * Mark the interface down and cancel the watchdog timer.
   1417 	 */
   1418 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1419 	ifp->if_timer = 0;
   1420 
   1421 	if (disable)
   1422 		ste_rxdrain(sc);
   1423 }
   1424 
   1425 static int
   1426 ste_eeprom_wait(struct ste_softc *sc)
   1427 {
   1428 	int i;
   1429 
   1430 	for (i = 0; i < STE_TIMEOUT; i++) {
   1431 		delay(1000);
   1432 		if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STE_EepromCtrl) &
   1433 		     EC_EepromBusy) == 0)
   1434 			return (0);
   1435 	}
   1436 	return (1);
   1437 }
   1438 
   1439 /*
   1440  * ste_read_eeprom:
   1441  *
   1442  *	Read data from the serial EEPROM.
   1443  */
   1444 static void
   1445 ste_read_eeprom(struct ste_softc *sc, int offset, uint16_t *data)
   1446 {
   1447 
   1448 	if (ste_eeprom_wait(sc))
   1449 		printf("%s: EEPROM failed to come ready\n",
   1450 		    device_xname(sc->sc_dev));
   1451 
   1452 	bus_space_write_2(sc->sc_st, sc->sc_sh, STE_EepromCtrl,
   1453 	    EC_EepromAddress(offset) | EC_EepromOpcode(EC_OP_R));
   1454 	if (ste_eeprom_wait(sc))
   1455 		printf("%s: EEPROM read timed out\n",
   1456 		    device_xname(sc->sc_dev));
   1457 	*data = bus_space_read_2(sc->sc_st, sc->sc_sh, STE_EepromData);
   1458 }
   1459 
   1460 /*
   1461  * ste_add_rxbuf:
   1462  *
   1463  *	Add a receive buffer to the indicated descriptor.
   1464  */
   1465 static int
   1466 ste_add_rxbuf(struct ste_softc *sc, int idx)
   1467 {
   1468 	struct ste_descsoft *ds = &sc->sc_rxsoft[idx];
   1469 	struct mbuf *m;
   1470 	int error;
   1471 
   1472 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1473 	if (m == NULL)
   1474 		return (ENOBUFS);
   1475 
   1476 	MCLGET(m, M_DONTWAIT);
   1477 	if ((m->m_flags & M_EXT) == 0) {
   1478 		m_freem(m);
   1479 		return (ENOBUFS);
   1480 	}
   1481 
   1482 	if (ds->ds_mbuf != NULL)
   1483 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1484 
   1485 	ds->ds_mbuf = m;
   1486 
   1487 	error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
   1488 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1489 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1490 	if (error) {
   1491 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1492 		    device_xname(sc->sc_dev), idx, error);
   1493 		panic("ste_add_rxbuf");		/* XXX */
   1494 	}
   1495 
   1496 	bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
   1497 	    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1498 
   1499 	STE_INIT_RXDESC(sc, idx);
   1500 
   1501 	return (0);
   1502 }
   1503 
   1504 /*
   1505  * ste_set_filter:
   1506  *
   1507  *	Set up the receive filter.
   1508  */
   1509 static void
   1510 ste_set_filter(struct ste_softc *sc)
   1511 {
   1512 	struct ethercom *ec = &sc->sc_ethercom;
   1513 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1514 	struct ether_multi *enm;
   1515 	struct ether_multistep step;
   1516 	uint32_t crc;
   1517 	uint16_t mchash[4];
   1518 
   1519 	sc->sc_ReceiveMode = RM_ReceiveUnicast;
   1520 	if (ifp->if_flags & IFF_BROADCAST)
   1521 		sc->sc_ReceiveMode |= RM_ReceiveBroadcast;
   1522 
   1523 	if (ifp->if_flags & IFF_PROMISC) {
   1524 		sc->sc_ReceiveMode |= RM_ReceiveAllFrames;
   1525 		goto allmulti;
   1526 	}
   1527 
   1528 	/*
   1529 	 * Set up the multicast address filter by passing all multicast
   1530 	 * addresses through a CRC generator, and then using the low-order
   1531 	 * 6 bits as an index into the 64 bit multicast hash table.  The
   1532 	 * high order bits select the register, while the rest of the bits
   1533 	 * select the bit within the register.
   1534 	 */
   1535 
   1536 	memset(mchash, 0, sizeof(mchash));
   1537 
   1538 	ETHER_FIRST_MULTI(step, ec, enm);
   1539 	if (enm == NULL)
   1540 		goto done;
   1541 
   1542 	while (enm != NULL) {
   1543 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1544 			/*
   1545 			 * We must listen to a range of multicast addresses.
   1546 			 * For now, just accept all multicasts, rather than
   1547 			 * trying to set only those filter bits needed to match
   1548 			 * the range.  (At this time, the only use of address
   1549 			 * ranges is for IP multicast routing, for which the
   1550 			 * range is big enough to require all bits set.)
   1551 			 */
   1552 			goto allmulti;
   1553 		}
   1554 
   1555 		crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
   1556 
   1557 		/* Just want the 6 least significant bits. */
   1558 		crc &= 0x3f;
   1559 
   1560 		/* Set the corresponding bit in the hash table. */
   1561 		mchash[crc >> 4] |= 1 << (crc & 0xf);
   1562 
   1563 		ETHER_NEXT_MULTI(step, enm);
   1564 	}
   1565 
   1566 	sc->sc_ReceiveMode |= RM_ReceiveMulticastHash;
   1567 
   1568 	ifp->if_flags &= ~IFF_ALLMULTI;
   1569 	goto done;
   1570 
   1571  allmulti:
   1572 	ifp->if_flags |= IFF_ALLMULTI;
   1573 	sc->sc_ReceiveMode |= RM_ReceiveMulticast;
   1574 
   1575  done:
   1576 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
   1577 		/*
   1578 		 * Program the multicast hash table.
   1579 		 */
   1580 		bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable0,
   1581 		    mchash[0]);
   1582 		bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable1,
   1583 		    mchash[1]);
   1584 		bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable2,
   1585 		    mchash[2]);
   1586 		bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable3,
   1587 		    mchash[3]);
   1588 	}
   1589 
   1590 	bus_space_write_1(sc->sc_st, sc->sc_sh, STE_ReceiveMode,
   1591 	    sc->sc_ReceiveMode);
   1592 }
   1593 
   1594 /*
   1595  * ste_mii_readreg:	[mii interface function]
   1596  *
   1597  *	Read a PHY register on the MII of the ST-201.
   1598  */
   1599 static int
   1600 ste_mii_readreg(device_t self, int phy, int reg)
   1601 {
   1602 
   1603 	return (mii_bitbang_readreg(self, &ste_mii_bitbang_ops, phy, reg));
   1604 }
   1605 
   1606 /*
   1607  * ste_mii_writereg:	[mii interface function]
   1608  *
   1609  *	Write a PHY register on the MII of the ST-201.
   1610  */
   1611 static void
   1612 ste_mii_writereg(device_t self, int phy, int reg, int val)
   1613 {
   1614 
   1615 	mii_bitbang_writereg(self, &ste_mii_bitbang_ops, phy, reg, val);
   1616 }
   1617 
   1618 /*
   1619  * ste_mii_statchg:	[mii interface function]
   1620  *
   1621  *	Callback from MII layer when media changes.
   1622  */
   1623 static void
   1624 ste_mii_statchg(struct ifnet *ifp)
   1625 {
   1626 	struct ste_softc *sc = ifp->if_softc;
   1627 
   1628 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   1629 		sc->sc_MacCtrl0 |= MC0_FullDuplexEnable;
   1630 	else
   1631 		sc->sc_MacCtrl0 &= ~MC0_FullDuplexEnable;
   1632 
   1633 	/* XXX 802.1x flow-control? */
   1634 
   1635 	bus_space_write_2(sc->sc_st, sc->sc_sh, STE_MacCtrl0, sc->sc_MacCtrl0);
   1636 }
   1637 
   1638 /*
   1639  * ste_mii_bitbang_read: [mii bit-bang interface function]
   1640  *
   1641  *	Read the MII serial port for the MII bit-bang module.
   1642  */
   1643 static uint32_t
   1644 ste_mii_bitbang_read(device_t self)
   1645 {
   1646 	struct ste_softc *sc = device_private(self);
   1647 
   1648 	return (bus_space_read_1(sc->sc_st, sc->sc_sh, STE_PhyCtrl));
   1649 }
   1650 
   1651 /*
   1652  * ste_mii_bitbang_write: [mii big-bang interface function]
   1653  *
   1654  *	Write the MII serial port for the MII bit-bang module.
   1655  */
   1656 static void
   1657 ste_mii_bitbang_write(device_t self, uint32_t val)
   1658 {
   1659 	struct ste_softc *sc = device_private(self);
   1660 
   1661 	bus_space_write_1(sc->sc_st, sc->sc_sh, STE_PhyCtrl, val);
   1662 }
   1663