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