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