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