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