Home | History | Annotate | Line # | Download | only in ic
tulip.c revision 1.6
      1  1.6  thorpej /*	$NetBSD: tulip.c,v 1.6 1999/09/09 21:48:18 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 1998, 1999 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 of the Numerical Aerospace Simulation Facility,
      9  1.1  thorpej  * NASA Ames Research Center.
     10  1.1  thorpej  *
     11  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.1  thorpej  * modification, are permitted provided that the following conditions
     13  1.1  thorpej  * are met:
     14  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.1  thorpej  *    must display the following acknowledgement:
     21  1.1  thorpej  *	This product includes software developed by the NetBSD
     22  1.1  thorpej  *	Foundation, Inc. and its contributors.
     23  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     25  1.1  thorpej  *    from this software without specific prior written permission.
     26  1.1  thorpej  *
     27  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  thorpej  */
     39  1.1  thorpej 
     40  1.1  thorpej /*
     41  1.1  thorpej  * Device driver for the Digital Semiconductor ``Tulip'' (21x4x)
     42  1.1  thorpej  * Ethernet controller family, and a variety of clone chips.
     43  1.1  thorpej  */
     44  1.1  thorpej 
     45  1.1  thorpej #include "opt_inet.h"
     46  1.1  thorpej #include "opt_ns.h"
     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/mbuf.h>
     52  1.1  thorpej #include <sys/malloc.h>
     53  1.1  thorpej #include <sys/kernel.h>
     54  1.1  thorpej #include <sys/socket.h>
     55  1.1  thorpej #include <sys/ioctl.h>
     56  1.1  thorpej #include <sys/errno.h>
     57  1.1  thorpej #include <sys/device.h>
     58  1.1  thorpej 
     59  1.1  thorpej #include <vm/vm.h>		/* for PAGE_SIZE */
     60  1.1  thorpej 
     61  1.1  thorpej #include <net/if.h>
     62  1.1  thorpej #include <net/if_dl.h>
     63  1.1  thorpej #include <net/if_media.h>
     64  1.1  thorpej #include <net/if_ether.h>
     65  1.1  thorpej 
     66  1.1  thorpej #if NBPFILTER > 0
     67  1.1  thorpej #include <net/bpf.h>
     68  1.1  thorpej #endif
     69  1.1  thorpej 
     70  1.1  thorpej #ifdef INET
     71  1.1  thorpej #include <netinet/in.h>
     72  1.1  thorpej #include <netinet/if_inarp.h>
     73  1.1  thorpej #endif
     74  1.1  thorpej 
     75  1.1  thorpej #ifdef NS
     76  1.1  thorpej #include <netns/ns.h>
     77  1.1  thorpej #include <netns/ns_if.h>
     78  1.1  thorpej #endif
     79  1.1  thorpej 
     80  1.1  thorpej #include <machine/bus.h>
     81  1.1  thorpej #include <machine/intr.h>
     82  1.1  thorpej 
     83  1.1  thorpej #include <dev/mii/mii.h>
     84  1.1  thorpej #include <dev/mii/miivar.h>
     85  1.1  thorpej 
     86  1.1  thorpej #include <dev/ic/tulipreg.h>
     87  1.1  thorpej #include <dev/ic/tulipvar.h>
     88  1.1  thorpej 
     89  1.1  thorpej /*
     90  1.1  thorpej  * The following tables compute the transmit threshold mode.  We start
     91  1.1  thorpej  * at index 0.  When ever we get a transmit underrun, we increment our
     92  1.1  thorpej  * index, falling back if we encounter the NULL terminator.
     93  1.1  thorpej  *
     94  1.1  thorpej  * Note: Store and forward mode is only available on the 100mbps chips
     95  1.1  thorpej  * (21140 and higher).
     96  1.1  thorpej  */
     97  1.1  thorpej const struct tulip_txthresh_tab tlp_10_txthresh_tab[] = {
     98  1.1  thorpej 	{ OPMODE_TR_72,		"72 bytes" },
     99  1.1  thorpej 	{ OPMODE_TR_96,		"96 bytes" },
    100  1.1  thorpej 	{ OPMODE_TR_128,	"128 bytes" },
    101  1.1  thorpej 	{ OPMODE_TR_160,	"160 bytes" },
    102  1.1  thorpej 	{ 0,			NULL },
    103  1.1  thorpej };
    104  1.1  thorpej 
    105  1.1  thorpej const struct tulip_txthresh_tab tlp_10_100_txthresh_tab[] = {
    106  1.1  thorpej 	{ OPMODE_TR_72,		"72/128 bytes" },
    107  1.1  thorpej 	{ OPMODE_TR_96,		"96/256 bytes" },
    108  1.1  thorpej 	{ OPMODE_TR_128,	"128/512 bytes" },
    109  1.1  thorpej 	{ OPMODE_TR_160,	"160/1024 bytes" },
    110  1.1  thorpej 	{ OPMODE_SF,		"store and forward mode" },
    111  1.1  thorpej 	{ 0,			NULL },
    112  1.1  thorpej };
    113  1.1  thorpej 
    114  1.3  thorpej #define	TXTH_72		0
    115  1.3  thorpej #define	TXTH_96		1
    116  1.3  thorpej #define	TXTH_128	2
    117  1.3  thorpej #define	TXTH_160	3
    118  1.3  thorpej #define	TXTH_SF		4
    119  1.3  thorpej 
    120  1.5  thorpej /*
    121  1.5  thorpej  * The Winbond 89C840F does transmit threshold control totally
    122  1.5  thorpej  * differently.  It simply has a 7-bit field which indicates
    123  1.5  thorpej  * the threshold:
    124  1.5  thorpej  *
    125  1.5  thorpej  *	txth = ((OPMODE & OPMODE_WINB_TTH) >> OPMODE_WINB_TTH_SHIFT) * 16;
    126  1.5  thorpej  *
    127  1.5  thorpej  * However, we just do Store-and-Forward mode on these chips, since
    128  1.5  thorpej  * the DMA engines seem to be flaky.
    129  1.5  thorpej  */
    130  1.5  thorpej const struct tulip_txthresh_tab tlp_winb_txthresh_tab[] = {
    131  1.5  thorpej 	{ 0,			"store and forward mode" },
    132  1.5  thorpej 	{ 0,			NULL },
    133  1.5  thorpej };
    134  1.5  thorpej 
    135  1.5  thorpej #define	TXTH_WINB_SF	0
    136  1.5  thorpej 
    137  1.1  thorpej void	tlp_start __P((struct ifnet *));
    138  1.1  thorpej void	tlp_watchdog __P((struct ifnet *));
    139  1.1  thorpej int	tlp_ioctl __P((struct ifnet *, u_long, caddr_t));
    140  1.1  thorpej 
    141  1.1  thorpej void	tlp_shutdown __P((void *));
    142  1.1  thorpej 
    143  1.1  thorpej void	tlp_reset __P((struct tulip_softc *));
    144  1.1  thorpej int	tlp_init __P((struct tulip_softc *));
    145  1.1  thorpej void	tlp_rxdrain __P((struct tulip_softc *));
    146  1.1  thorpej void	tlp_stop __P((struct tulip_softc *, int));
    147  1.1  thorpej int	tlp_add_rxbuf __P((struct tulip_softc *, int));
    148  1.1  thorpej void	tlp_idle __P((struct tulip_softc *, u_int32_t));
    149  1.1  thorpej void	tlp_srom_idle __P((struct tulip_softc *));
    150  1.1  thorpej 
    151  1.1  thorpej void	tlp_filter_setup __P((struct tulip_softc *));
    152  1.1  thorpej void	tlp_winb_filter_setup __P((struct tulip_softc *));
    153  1.1  thorpej 
    154  1.1  thorpej void	tlp_rxintr __P((struct tulip_softc *));
    155  1.1  thorpej void	tlp_txintr __P((struct tulip_softc *));
    156  1.1  thorpej 
    157  1.1  thorpej void	tlp_mii_tick __P((void *));
    158  1.1  thorpej void	tlp_mii_statchg __P((struct device *));
    159  1.5  thorpej void	tlp_winb_mii_statchg __P((struct device *));
    160  1.1  thorpej 
    161  1.1  thorpej void	tlp_mii_getmedia __P((struct tulip_softc *, struct ifmediareq *));
    162  1.1  thorpej int	tlp_mii_setmedia __P((struct tulip_softc *));
    163  1.1  thorpej 
    164  1.1  thorpej void	tlp_sio_mii_sync __P((struct tulip_softc *));
    165  1.1  thorpej void	tlp_sio_mii_sendbits __P((struct tulip_softc *, u_int32_t, int));
    166  1.1  thorpej int	tlp_sio_mii_readreg __P((struct device *, int, int));
    167  1.1  thorpej void	tlp_sio_mii_writereg __P((struct device *, int, int, int));
    168  1.1  thorpej 
    169  1.1  thorpej int	tlp_pnic_mii_readreg __P((struct device *, int, int));
    170  1.1  thorpej void	tlp_pnic_mii_writereg __P((struct device *, int, int, int));
    171  1.1  thorpej 
    172  1.1  thorpej u_int32_t tlp_crc32 __P((const u_int8_t *, size_t));
    173  1.1  thorpej #define	tlp_mchash(addr)	(tlp_crc32((addr), ETHER_ADDR_LEN) &	\
    174  1.1  thorpej 				 (TULIP_MCHASHSIZE - 1))
    175  1.1  thorpej 
    176  1.1  thorpej #ifdef TLP_DEBUG
    177  1.5  thorpej #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
    178  1.5  thorpej 				printf x
    179  1.1  thorpej #else
    180  1.5  thorpej #define	DPRINTF(sc, x)	/* nothing */
    181  1.1  thorpej #endif
    182  1.1  thorpej 
    183  1.1  thorpej /*
    184  1.1  thorpej  * tlp_attach:
    185  1.1  thorpej  *
    186  1.1  thorpej  *	Attach a Tulip interface to the system.
    187  1.1  thorpej  */
    188  1.1  thorpej void
    189  1.1  thorpej tlp_attach(sc, name, enaddr)
    190  1.1  thorpej 	struct tulip_softc *sc;
    191  1.1  thorpej 	const char *name;
    192  1.1  thorpej 	const u_int8_t *enaddr;
    193  1.1  thorpej {
    194  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    195  1.1  thorpej 	int i, rseg, error;
    196  1.1  thorpej 	bus_dma_segment_t seg;
    197  1.1  thorpej 
    198  1.1  thorpej 	/*
    199  1.1  thorpej 	 * NOTE: WE EXPECT THE FRONT-END TO INITIALIZE sc_regshift!
    200  1.1  thorpej 	 */
    201  1.1  thorpej 
    202  1.1  thorpej 	/*
    203  1.1  thorpej 	 * Setup the transmit threshold table.
    204  1.1  thorpej 	 */
    205  1.1  thorpej 	switch (sc->sc_chip) {
    206  1.1  thorpej 	case TULIP_CHIP_DE425:
    207  1.1  thorpej 	case TULIP_CHIP_21040:
    208  1.1  thorpej 	case TULIP_CHIP_21041:
    209  1.1  thorpej 		sc->sc_txth = tlp_10_txthresh_tab;
    210  1.1  thorpej 		break;
    211  1.1  thorpej 
    212  1.1  thorpej 	default:
    213  1.1  thorpej 		sc->sc_txth = tlp_10_100_txthresh_tab;
    214  1.1  thorpej 		break;
    215  1.1  thorpej 	}
    216  1.1  thorpej 
    217  1.1  thorpej 	/*
    218  1.1  thorpej 	 * Setup the filter setup function.
    219  1.1  thorpej 	 */
    220  1.1  thorpej 	switch (sc->sc_chip) {
    221  1.1  thorpej 	case TULIP_CHIP_WB89C840F:
    222  1.1  thorpej 		sc->sc_filter_setup = tlp_winb_filter_setup;
    223  1.1  thorpej 		break;
    224  1.1  thorpej 
    225  1.1  thorpej 	default:
    226  1.1  thorpej 		sc->sc_filter_setup = tlp_filter_setup;
    227  1.1  thorpej 		break;
    228  1.1  thorpej 	}
    229  1.1  thorpej 
    230  1.2  thorpej 	/*
    231  1.5  thorpej 	 * Set up the media status change function.
    232  1.5  thorpej 	 */
    233  1.5  thorpej 	switch (sc->sc_chip) {
    234  1.5  thorpej 	case TULIP_CHIP_WB89C840F:
    235  1.5  thorpej 		sc->sc_statchg = tlp_winb_mii_statchg;
    236  1.5  thorpej 		break;
    237  1.5  thorpej 
    238  1.5  thorpej 	default:
    239  1.5  thorpej 		sc->sc_statchg = tlp_mii_statchg;
    240  1.5  thorpej 		break;
    241  1.5  thorpej 	}
    242  1.5  thorpej 
    243  1.5  thorpej 	/*
    244  1.2  thorpej 	 * Set up various chip-specific quirks.
    245  1.2  thorpej 	 */
    246  1.2  thorpej 	switch (sc->sc_chip) {
    247  1.3  thorpej 	case TULIP_CHIP_82C168:
    248  1.3  thorpej 	case TULIP_CHIP_82C169:
    249  1.3  thorpej 		/*
    250  1.3  thorpej 		 * These chips seem to have busted DMA engines; just put them
    251  1.3  thorpej 		 * in Store-and-Forward mode from the get-go.
    252  1.3  thorpej 		 */
    253  1.3  thorpej 		sc->sc_txthresh = TXTH_SF;
    254  1.3  thorpej 		break;
    255  1.3  thorpej 
    256  1.2  thorpej 	case TULIP_CHIP_WB89C840F:
    257  1.2  thorpej 		sc->sc_flags |= TULIPF_IC_FS;
    258  1.2  thorpej 		break;
    259  1.2  thorpej 
    260  1.2  thorpej 	default:
    261  1.2  thorpej 		/* Nothing. */
    262  1.2  thorpej 	}
    263  1.2  thorpej 
    264  1.1  thorpej 	SIMPLEQ_INIT(&sc->sc_txfreeq);
    265  1.1  thorpej 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
    266  1.1  thorpej 
    267  1.1  thorpej 	/*
    268  1.1  thorpej 	 * Allocate the control data structures, and create and load the
    269  1.1  thorpej 	 * DMA map for it.
    270  1.1  thorpej 	 */
    271  1.1  thorpej 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    272  1.1  thorpej 	    sizeof(struct tulip_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
    273  1.1  thorpej 	    0)) != 0) {
    274  1.1  thorpej 		printf("%s: unable to allocate control data, error = %d\n",
    275  1.1  thorpej 		    sc->sc_dev.dv_xname, error);
    276  1.1  thorpej 		goto fail_0;
    277  1.1  thorpej 	}
    278  1.1  thorpej 
    279  1.1  thorpej 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    280  1.1  thorpej 	    sizeof(struct tulip_control_data), (caddr_t *)&sc->sc_control_data,
    281  1.1  thorpej 	    BUS_DMA_COHERENT)) != 0) {
    282  1.1  thorpej 		printf("%s: unable to map control data, error = %d\n",
    283  1.1  thorpej 		    sc->sc_dev.dv_xname, error);
    284  1.1  thorpej 		goto fail_1;
    285  1.1  thorpej 	}
    286  1.1  thorpej 
    287  1.1  thorpej 	if ((error = bus_dmamap_create(sc->sc_dmat,
    288  1.1  thorpej 	    sizeof(struct tulip_control_data), 1,
    289  1.1  thorpej 	    sizeof(struct tulip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    290  1.1  thorpej 		printf("%s: unable to create control data DMA map, "
    291  1.1  thorpej 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    292  1.1  thorpej 		goto fail_2;
    293  1.1  thorpej 	}
    294  1.1  thorpej 
    295  1.1  thorpej 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    296  1.1  thorpej 	    sc->sc_control_data, sizeof(struct tulip_control_data), NULL,
    297  1.1  thorpej 	    0)) != 0) {
    298  1.1  thorpej 		printf("%s: unable to load control data DMA map, error = %d\n",
    299  1.1  thorpej 		    sc->sc_dev.dv_xname, error);
    300  1.1  thorpej 		goto fail_3;
    301  1.1  thorpej 	}
    302  1.1  thorpej 
    303  1.1  thorpej 	/*
    304  1.1  thorpej 	 * Create the transmit buffer DMA maps.
    305  1.1  thorpej 	 */
    306  1.1  thorpej 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
    307  1.1  thorpej 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    308  1.1  thorpej 		    TULIP_NTXSEGS, MCLBYTES, 0, 0,
    309  1.1  thorpej 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    310  1.1  thorpej 			printf("%s: unable to create tx DMA map %d, "
    311  1.1  thorpej 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    312  1.1  thorpej 			goto fail_4;
    313  1.1  thorpej 		}
    314  1.1  thorpej 	}
    315  1.1  thorpej 
    316  1.1  thorpej 	/*
    317  1.1  thorpej 	 * Create the recieve buffer DMA maps.
    318  1.1  thorpej 	 */
    319  1.1  thorpej 	for (i = 0; i < TULIP_NRXDESC; i++) {
    320  1.1  thorpej 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    321  1.1  thorpej 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    322  1.1  thorpej 			printf("%s: unable to create rx DMA map %d, "
    323  1.1  thorpej 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    324  1.1  thorpej 			goto fail_5;
    325  1.1  thorpej 		}
    326  1.1  thorpej 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    327  1.1  thorpej 	}
    328  1.1  thorpej 
    329  1.1  thorpej 	/*
    330  1.1  thorpej 	 * Reset the chip to a known state.
    331  1.1  thorpej 	 */
    332  1.1  thorpej 	tlp_reset(sc);
    333  1.1  thorpej 
    334  1.1  thorpej 	/* Announce ourselves. */
    335  1.1  thorpej 	printf("%s: %s%sEthernet address %s\n", sc->sc_dev.dv_xname,
    336  1.1  thorpej 	    name != NULL ? name : "", name != NULL ? ", " : "",
    337  1.1  thorpej 	    ether_sprintf(enaddr));
    338  1.1  thorpej 
    339  1.1  thorpej 	/*
    340  1.1  thorpej 	 * Initialize our media structures.  This may probe the MII, if
    341  1.1  thorpej 	 * present.
    342  1.1  thorpej 	 */
    343  1.1  thorpej 	(*sc->sc_mediasw->tmsw_init)(sc);
    344  1.1  thorpej 
    345  1.1  thorpej 	ifp = &sc->sc_ethercom.ec_if;
    346  1.1  thorpej 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    347  1.1  thorpej 	ifp->if_softc = sc;
    348  1.1  thorpej 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    349  1.1  thorpej 	ifp->if_ioctl = tlp_ioctl;
    350  1.1  thorpej 	ifp->if_start = tlp_start;
    351  1.1  thorpej 	ifp->if_watchdog = tlp_watchdog;
    352  1.1  thorpej 
    353  1.1  thorpej 	/*
    354  1.1  thorpej 	 * Attach the interface.
    355  1.1  thorpej 	 */
    356  1.1  thorpej 	if_attach(ifp);
    357  1.1  thorpej 	ether_ifattach(ifp, enaddr);
    358  1.1  thorpej #if NBPFILTER > 0
    359  1.1  thorpej 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
    360  1.1  thorpej 	    sizeof(struct ether_header));
    361  1.1  thorpej #endif
    362  1.1  thorpej 
    363  1.1  thorpej 	/*
    364  1.1  thorpej 	 * Make sure the interface is shutdown during reboot.
    365  1.1  thorpej 	 */
    366  1.1  thorpej 	sc->sc_sdhook = shutdownhook_establish(tlp_shutdown, sc);
    367  1.1  thorpej 	if (sc->sc_sdhook == NULL)
    368  1.1  thorpej 		printf("%s: WARNING: unable to establish shutdown hook\n",
    369  1.1  thorpej 		    sc->sc_dev.dv_xname);
    370  1.1  thorpej 	return;
    371  1.1  thorpej 
    372  1.1  thorpej 	/*
    373  1.1  thorpej 	 * Free any resources we've allocated during the failed attach
    374  1.1  thorpej 	 * attempt.  Do this in reverse order and fall through.
    375  1.1  thorpej 	 */
    376  1.1  thorpej  fail_5:
    377  1.1  thorpej 	for (i = 0; i < TULIP_NRXDESC; i++) {
    378  1.1  thorpej 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    379  1.1  thorpej 			bus_dmamap_destroy(sc->sc_dmat,
    380  1.1  thorpej 			    sc->sc_rxsoft[i].rxs_dmamap);
    381  1.1  thorpej 	}
    382  1.1  thorpej  fail_4:
    383  1.1  thorpej 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
    384  1.1  thorpej 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    385  1.1  thorpej 			bus_dmamap_destroy(sc->sc_dmat,
    386  1.1  thorpej 			    sc->sc_txsoft[i].txs_dmamap);
    387  1.1  thorpej 	}
    388  1.1  thorpej 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    389  1.1  thorpej  fail_3:
    390  1.1  thorpej 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    391  1.1  thorpej  fail_2:
    392  1.1  thorpej 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
    393  1.1  thorpej 	    sizeof(struct tulip_control_data));
    394  1.1  thorpej  fail_1:
    395  1.1  thorpej 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    396  1.1  thorpej  fail_0:
    397  1.1  thorpej 	return;
    398  1.1  thorpej }
    399  1.1  thorpej 
    400  1.1  thorpej /*
    401  1.1  thorpej  * tlp_shutdown:
    402  1.1  thorpej  *
    403  1.1  thorpej  *	Make sure the interface is stopped at reboot time.
    404  1.1  thorpej  */
    405  1.1  thorpej void
    406  1.1  thorpej tlp_shutdown(arg)
    407  1.1  thorpej 	void *arg;
    408  1.1  thorpej {
    409  1.1  thorpej 	struct tulip_softc *sc = arg;
    410  1.1  thorpej 
    411  1.1  thorpej 	tlp_stop(sc, 1);
    412  1.1  thorpej }
    413  1.1  thorpej 
    414  1.1  thorpej /*
    415  1.1  thorpej  * tlp_start:		[ifnet interface function]
    416  1.1  thorpej  *
    417  1.1  thorpej  *	Start packet transmission on the interface.
    418  1.1  thorpej  */
    419  1.1  thorpej void
    420  1.1  thorpej tlp_start(ifp)
    421  1.1  thorpej 	struct ifnet *ifp;
    422  1.1  thorpej {
    423  1.1  thorpej 	struct tulip_softc *sc = ifp->if_softc;
    424  1.1  thorpej 	struct mbuf *m0, *m;
    425  1.2  thorpej 	struct tulip_txsoft *txs, *last_txs;
    426  1.1  thorpej 	bus_dmamap_t dmamap;
    427  1.1  thorpej 	int error, firsttx, nexttx, lasttx, ofree, seg;
    428  1.1  thorpej 
    429  1.5  thorpej 	DPRINTF(sc, ("%s: tlp_start: sc_flags 0x%08x, if_flags 0x%08x\n",
    430  1.1  thorpej 	    sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
    431  1.1  thorpej 
    432  1.1  thorpej 	/*
    433  1.1  thorpej 	 * If we want a filter setup, it means no more descriptors were
    434  1.1  thorpej 	 * available for the setup routine.  Let it get a chance to wedge
    435  1.1  thorpej 	 * itself into the ring.
    436  1.1  thorpej 	 */
    437  1.1  thorpej 	if (sc->sc_flags & TULIPF_WANT_SETUP)
    438  1.1  thorpej 		ifp->if_flags |= IFF_OACTIVE;
    439  1.1  thorpej 
    440  1.1  thorpej 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    441  1.1  thorpej 		return;
    442  1.1  thorpej 
    443  1.1  thorpej 	/*
    444  1.1  thorpej 	 * Remember the previous number of free descriptors and
    445  1.1  thorpej 	 * the first descriptor we'll use.
    446  1.1  thorpej 	 */
    447  1.1  thorpej 	ofree = sc->sc_txfree;
    448  1.1  thorpej 	firsttx = sc->sc_txnext;
    449  1.1  thorpej 
    450  1.5  thorpej 	DPRINTF(sc, ("%s: tlp_start: txfree %d, txnext %d\n",
    451  1.1  thorpej 	    sc->sc_dev.dv_xname, ofree, firsttx));
    452  1.1  thorpej 
    453  1.1  thorpej 	/*
    454  1.1  thorpej 	 * Loop through the send queue, setting up transmit descriptors
    455  1.1  thorpej 	 * until we drain the queue, or use up all available transmit
    456  1.1  thorpej 	 * descriptors.
    457  1.1  thorpej 	 */
    458  1.1  thorpej 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
    459  1.1  thorpej 	       sc->sc_txfree != 0) {
    460  1.1  thorpej 		/*
    461  1.1  thorpej 		 * Grab a packet off the queue.
    462  1.1  thorpej 		 */
    463  1.1  thorpej 		IF_DEQUEUE(&ifp->if_snd, m0);
    464  1.1  thorpej 		if (m0 == NULL)
    465  1.1  thorpej 			break;
    466  1.1  thorpej 
    467  1.1  thorpej 		dmamap = txs->txs_dmamap;
    468  1.1  thorpej 
    469  1.1  thorpej 		/*
    470  1.1  thorpej 		 * Load the DMA map.  If this fails, the packet either
    471  1.1  thorpej 		 * didn't fit in the alloted number of segments, or we were
    472  1.1  thorpej 		 * short on resources.  In this case, we'll copy and try
    473  1.1  thorpej 		 * again.
    474  1.1  thorpej 		 */
    475  1.1  thorpej 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    476  1.1  thorpej 		    BUS_DMA_NOWAIT) != 0) {
    477  1.1  thorpej 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    478  1.1  thorpej 			if (m == NULL) {
    479  1.1  thorpej 				printf("%s: unable to allocate Tx mbuf\n",
    480  1.1  thorpej 				    sc->sc_dev.dv_xname);
    481  1.1  thorpej 				IF_PREPEND(&ifp->if_snd, m0);
    482  1.1  thorpej 				break;
    483  1.1  thorpej 			}
    484  1.1  thorpej 			if (m0->m_pkthdr.len > MHLEN) {
    485  1.1  thorpej 				MCLGET(m, M_DONTWAIT);
    486  1.1  thorpej 				if ((m->m_flags & M_EXT) == 0) {
    487  1.1  thorpej 					printf("%s: unable to allocate Tx "
    488  1.1  thorpej 					    "cluster\n", sc->sc_dev.dv_xname);
    489  1.1  thorpej 					m_freem(m);
    490  1.1  thorpej 					IF_PREPEND(&ifp->if_snd, m0);
    491  1.1  thorpej 					break;
    492  1.1  thorpej 				}
    493  1.1  thorpej 			}
    494  1.1  thorpej 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
    495  1.1  thorpej 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    496  1.1  thorpej 			m_freem(m0);
    497  1.1  thorpej 			m0 = m;
    498  1.1  thorpej 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    499  1.1  thorpej 			    m0, BUS_DMA_NOWAIT);
    500  1.1  thorpej 			if (error) {
    501  1.1  thorpej 				printf("%s: unable to load Tx buffer, "
    502  1.1  thorpej 				    "error = %d\n", sc->sc_dev.dv_xname, error);
    503  1.1  thorpej 				IF_PREPEND(&ifp->if_snd, m0);
    504  1.1  thorpej 				break;
    505  1.1  thorpej 			}
    506  1.1  thorpej 		}
    507  1.1  thorpej 
    508  1.1  thorpej 		/*
    509  1.1  thorpej 		 * Ensure we have enough descriptors free to describe
    510  1.1  thorpej 		 * the packet.
    511  1.1  thorpej 		 */
    512  1.1  thorpej 		if (dmamap->dm_nsegs > sc->sc_txfree) {
    513  1.1  thorpej 			/*
    514  1.1  thorpej 			 * Not enough free descriptors to transmit this
    515  1.1  thorpej 			 * packet.  We haven't committed to anything yet,
    516  1.1  thorpej 			 * so just unload the DMA map, put the packet
    517  1.1  thorpej 			 * back on the queue, and punt.  Notify the upper
    518  1.1  thorpej 			 * layer that there are no more slots left.
    519  1.1  thorpej 			 *
    520  1.1  thorpej 			 * XXX We could allocate an mbuf and copy, but
    521  1.1  thorpej 			 * XXX it is worth it?
    522  1.1  thorpej 			 */
    523  1.1  thorpej 			ifp->if_flags |= IFF_OACTIVE;
    524  1.1  thorpej 			bus_dmamap_unload(sc->sc_dmat, dmamap);
    525  1.1  thorpej 			IF_PREPEND(&ifp->if_snd, m0);
    526  1.1  thorpej 			break;
    527  1.1  thorpej 		}
    528  1.1  thorpej 
    529  1.1  thorpej 		/*
    530  1.1  thorpej 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    531  1.1  thorpej 		 */
    532  1.1  thorpej 
    533  1.1  thorpej 		/* Sync the DMA map. */
    534  1.1  thorpej 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    535  1.1  thorpej 		    BUS_DMASYNC_PREWRITE);
    536  1.1  thorpej 
    537  1.1  thorpej 		/*
    538  1.1  thorpej 		 * Initialize the transmit descriptors.
    539  1.1  thorpej 		 */
    540  1.1  thorpej 		for (nexttx = sc->sc_txnext, seg = 0;
    541  1.1  thorpej 		     seg < dmamap->dm_nsegs;
    542  1.1  thorpej 		     seg++, nexttx = TULIP_NEXTTX(nexttx)) {
    543  1.1  thorpej 			/*
    544  1.1  thorpej 			 * If this is the first descriptor we're
    545  1.1  thorpej 			 * enqueueing, don't set the OWN bit just
    546  1.1  thorpej 			 * yet.  That could cause a race condition.
    547  1.1  thorpej 			 * We'll do it below.
    548  1.1  thorpej 			 */
    549  1.1  thorpej 			sc->sc_txdescs[nexttx].td_status =
    550  1.1  thorpej 			    (nexttx == firsttx) ? 0 : TDSTAT_OWN;
    551  1.1  thorpej 			sc->sc_txdescs[nexttx].td_bufaddr1 =
    552  1.1  thorpej 			    dmamap->dm_segs[seg].ds_addr;
    553  1.1  thorpej 			sc->sc_txdescs[nexttx].td_ctl =
    554  1.1  thorpej 			    (dmamap->dm_segs[seg].ds_len << TDCTL_SIZE1_SHIFT) |
    555  1.1  thorpej 			    TDCTL_CH;
    556  1.1  thorpej 			lasttx = nexttx;
    557  1.1  thorpej 		}
    558  1.1  thorpej 
    559  1.1  thorpej 		/* Set `first segment' and `last segment' appropriately. */
    560  1.1  thorpej 		sc->sc_txdescs[sc->sc_txnext].td_ctl |= TDCTL_Tx_FS;
    561  1.1  thorpej 		sc->sc_txdescs[lasttx].td_ctl |= TDCTL_Tx_LS;
    562  1.1  thorpej 
    563  1.1  thorpej #ifdef TLP_DEBUG
    564  1.5  thorpej 		if (ifp->if_flags & IFF_DEBUG) {
    565  1.5  thorpej 			printf("     txsoft %p trainsmit chain:\n", txs);
    566  1.5  thorpej 			for (seg = sc->sc_txnext;; seg = TULIP_NEXTTX(seg)) {
    567  1.5  thorpej 				printf("     descriptor %d:\n", seg);
    568  1.5  thorpej 				printf("       td_status:   0x%08x\n",
    569  1.5  thorpej 				    sc->sc_txdescs[seg].td_status);
    570  1.5  thorpej 				printf("       td_ctl:      0x%08x\n",
    571  1.5  thorpej 				    sc->sc_txdescs[seg].td_ctl);
    572  1.5  thorpej 				printf("       td_bufaddr1: 0x%08x\n",
    573  1.5  thorpej 				    sc->sc_txdescs[seg].td_bufaddr1);
    574  1.5  thorpej 				printf("       td_bufaddr2: 0x%08x\n",
    575  1.5  thorpej 				    sc->sc_txdescs[seg].td_bufaddr2);
    576  1.5  thorpej 				if (seg == lasttx)
    577  1.5  thorpej 					break;
    578  1.5  thorpej 			}
    579  1.1  thorpej 		}
    580  1.1  thorpej #endif
    581  1.1  thorpej 
    582  1.1  thorpej 		/* Sync the descriptors we're using. */
    583  1.1  thorpej 		TULIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
    584  1.1  thorpej 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    585  1.1  thorpej 
    586  1.1  thorpej 		/*
    587  1.1  thorpej 		 * Store a pointer to the packet so we can free it later,
    588  1.1  thorpej 		 * and remember what txdirty will be once the packet is
    589  1.1  thorpej 		 * done.
    590  1.1  thorpej 		 */
    591  1.1  thorpej 		txs->txs_mbuf = m0;
    592  1.1  thorpej 		txs->txs_firstdesc = sc->sc_txnext;
    593  1.1  thorpej 		txs->txs_lastdesc = lasttx;
    594  1.1  thorpej 
    595  1.1  thorpej 		/* Advance the tx pointer. */
    596  1.1  thorpej 		sc->sc_txfree -= dmamap->dm_nsegs;
    597  1.1  thorpej 		sc->sc_txnext = nexttx;
    598  1.1  thorpej 
    599  1.1  thorpej 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
    600  1.1  thorpej 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
    601  1.1  thorpej 
    602  1.2  thorpej 		last_txs = txs;
    603  1.2  thorpej 
    604  1.1  thorpej #if NBPFILTER > 0
    605  1.1  thorpej 		/*
    606  1.1  thorpej 		 * Pass the packet to any BPF listeners.
    607  1.1  thorpej 		 */
    608  1.1  thorpej 		if (ifp->if_bpf)
    609  1.1  thorpej 			bpf_mtap(ifp->if_bpf, m0);
    610  1.1  thorpej #endif /* NBPFILTER > 0 */
    611  1.1  thorpej 	}
    612  1.1  thorpej 
    613  1.1  thorpej 	if (txs == NULL || sc->sc_txfree == 0) {
    614  1.1  thorpej 		/* No more slots left; notify upper layer. */
    615  1.1  thorpej 		ifp->if_flags |= IFF_OACTIVE;
    616  1.1  thorpej 	}
    617  1.1  thorpej 
    618  1.1  thorpej 	if (sc->sc_txfree != ofree) {
    619  1.5  thorpej 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
    620  1.1  thorpej 		    sc->sc_dev.dv_xname, lasttx, firsttx));
    621  1.1  thorpej 		/*
    622  1.1  thorpej 		 * Cause a transmit interrupt to happen on the
    623  1.1  thorpej 		 * last packet we enqueued.
    624  1.1  thorpej 		 */
    625  1.1  thorpej 		sc->sc_txdescs[lasttx].td_ctl |= TDCTL_Tx_IC;
    626  1.1  thorpej 		TULIP_CDTXSYNC(sc, lasttx, 1,
    627  1.1  thorpej 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    628  1.2  thorpej 
    629  1.2  thorpej 		/*
    630  1.2  thorpej 		 * Some clone chips want IC on the *first* segment in
    631  1.2  thorpej 		 * the packet.  Appease them.
    632  1.2  thorpej 		 */
    633  1.2  thorpej 		if ((sc->sc_flags & TULIPF_IC_FS) != 0 &&
    634  1.2  thorpej 		    last_txs->txs_firstdesc != lasttx) {
    635  1.2  thorpej 			sc->sc_txdescs[last_txs->txs_firstdesc].td_ctl |=
    636  1.2  thorpej 			    TDCTL_Tx_IC;
    637  1.2  thorpej 			TULIP_CDTXSYNC(sc, last_txs->txs_firstdesc, 1,
    638  1.2  thorpej 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    639  1.2  thorpej 		}
    640  1.1  thorpej 
    641  1.1  thorpej 		/*
    642  1.1  thorpej 		 * The entire packet chain is set up.  Give the
    643  1.1  thorpej 		 * first descriptor to the chip now.
    644  1.1  thorpej 		 */
    645  1.1  thorpej 		sc->sc_txdescs[firsttx].td_status |= TDSTAT_OWN;
    646  1.1  thorpej 		TULIP_CDTXSYNC(sc, firsttx, 1,
    647  1.1  thorpej 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    648  1.1  thorpej 
    649  1.1  thorpej 		/* Wake up the transmitter. */
    650  1.1  thorpej 		/* XXX USE AUTOPOLLING? */
    651  1.1  thorpej 		TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
    652  1.1  thorpej 
    653  1.1  thorpej 		/* Set a watchdog timer in case the chip flakes out. */
    654  1.1  thorpej 		ifp->if_timer = 5;
    655  1.1  thorpej 	}
    656  1.1  thorpej }
    657  1.1  thorpej 
    658  1.1  thorpej /*
    659  1.1  thorpej  * tlp_watchdog:	[ifnet interface function]
    660  1.1  thorpej  *
    661  1.1  thorpej  *	Watchdog timer handler.
    662  1.1  thorpej  */
    663  1.1  thorpej void
    664  1.1  thorpej tlp_watchdog(ifp)
    665  1.1  thorpej 	struct ifnet *ifp;
    666  1.1  thorpej {
    667  1.1  thorpej 	struct tulip_softc *sc = ifp->if_softc;
    668  1.4  thorpej 	int doing_setup, doing_transmit;
    669  1.4  thorpej 
    670  1.4  thorpej 	doing_setup = (sc->sc_flags & TULIPF_DOING_SETUP);
    671  1.4  thorpej 	doing_transmit = (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL);
    672  1.4  thorpej 
    673  1.4  thorpej 	if (doing_setup && doing_transmit) {
    674  1.4  thorpej 		printf("%s: filter setup and transmit timeout\n",
    675  1.4  thorpej 		    sc->sc_dev.dv_xname);
    676  1.4  thorpej 		ifp->if_oerrors++;
    677  1.4  thorpej 	} else if (doing_transmit) {
    678  1.4  thorpej 		printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
    679  1.4  thorpej 		ifp->if_oerrors++;
    680  1.4  thorpej 	} else if (doing_setup)
    681  1.4  thorpej 		printf("%s: filter setup timeout\n", sc->sc_dev.dv_xname);
    682  1.4  thorpej 	else
    683  1.4  thorpej 		printf("%s: spurious watchdog timeout\n", sc->sc_dev.dv_xname);
    684  1.1  thorpej 
    685  1.1  thorpej 	(void) tlp_init(sc);
    686  1.1  thorpej 
    687  1.1  thorpej 	/* Try to get more packets going. */
    688  1.1  thorpej 	tlp_start(ifp);
    689  1.1  thorpej }
    690  1.1  thorpej 
    691  1.1  thorpej /*
    692  1.1  thorpej  * tlp_ioctl:		[ifnet interface function]
    693  1.1  thorpej  *
    694  1.1  thorpej  *	Handle control requests from the operator.
    695  1.1  thorpej  */
    696  1.1  thorpej int
    697  1.1  thorpej tlp_ioctl(ifp, cmd, data)
    698  1.1  thorpej 	struct ifnet *ifp;
    699  1.1  thorpej 	u_long cmd;
    700  1.1  thorpej 	caddr_t data;
    701  1.1  thorpej {
    702  1.1  thorpej 	struct tulip_softc *sc = ifp->if_softc;
    703  1.1  thorpej 	struct ifreq *ifr = (struct ifreq *)data;
    704  1.1  thorpej 	struct ifaddr *ifa = (struct ifaddr *)data;
    705  1.1  thorpej 	int s, error = 0;
    706  1.1  thorpej 
    707  1.1  thorpej 	s = splnet();
    708  1.1  thorpej 
    709  1.1  thorpej 	switch (cmd) {
    710  1.1  thorpej 	case SIOCSIFADDR:
    711  1.1  thorpej 		ifp->if_flags |= IFF_UP;
    712  1.1  thorpej 
    713  1.1  thorpej 		switch (ifa->ifa_addr->sa_family) {
    714  1.1  thorpej #ifdef INET
    715  1.1  thorpej 		case AF_INET:
    716  1.1  thorpej 			if ((error = tlp_init(sc)) != 0)
    717  1.1  thorpej 				break;
    718  1.1  thorpej 			arp_ifinit(ifp, ifa);
    719  1.1  thorpej 			break;
    720  1.1  thorpej #endif /* INET */
    721  1.1  thorpej #ifdef NS
    722  1.1  thorpej 		case AF_NS:
    723  1.1  thorpej 		    {
    724  1.1  thorpej 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    725  1.1  thorpej 
    726  1.1  thorpej 			if (ns_nullhost(*ina))
    727  1.1  thorpej 				ina->x_host = *(union ns_host *)
    728  1.1  thorpej 				    LLADDR(ifp->if_sadl);
    729  1.1  thorpej 			else
    730  1.1  thorpej 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
    731  1.1  thorpej 				    ifp->if_addrlen);
    732  1.1  thorpej 			/* Set new address. */
    733  1.1  thorpej 			error = tlp_init(sc);
    734  1.1  thorpej 			break;
    735  1.1  thorpej 		    }
    736  1.1  thorpej #endif /* NS */
    737  1.1  thorpej 		default:
    738  1.1  thorpej 			error = tlp_init(sc);
    739  1.1  thorpej 			break;
    740  1.1  thorpej 		}
    741  1.1  thorpej 		break;
    742  1.1  thorpej 
    743  1.1  thorpej 	case SIOCSIFMTU:
    744  1.1  thorpej 		if (ifr->ifr_mtu > ETHERMTU)
    745  1.1  thorpej 			error = EINVAL;
    746  1.1  thorpej 		else
    747  1.1  thorpej 			ifp->if_mtu = ifr->ifr_mtu;
    748  1.1  thorpej 		break;
    749  1.1  thorpej 
    750  1.1  thorpej 	case SIOCSIFFLAGS:
    751  1.1  thorpej 		if ((ifp->if_flags & IFF_UP) == 0 &&
    752  1.1  thorpej 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    753  1.1  thorpej 			/*
    754  1.1  thorpej 			 * If interface is marked down and it is running, then
    755  1.1  thorpej 			 * stop it.
    756  1.1  thorpej 			 */
    757  1.1  thorpej 			tlp_stop(sc, 1);
    758  1.1  thorpej 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    759  1.1  thorpej 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    760  1.1  thorpej 			/*
    761  1.1  thorpej 			 * If interfase it marked up and it is stopped, then
    762  1.1  thorpej 			 * start it.
    763  1.1  thorpej 			 */
    764  1.1  thorpej 			error = tlp_init(sc);
    765  1.1  thorpej 		} else if ((ifp->if_flags & IFF_UP) != 0) {
    766  1.1  thorpej 			/*
    767  1.1  thorpej 			 * Reset the interface to pick up changes in any other
    768  1.1  thorpej 			 * flags that affect the hardware state.
    769  1.1  thorpej 			 */
    770  1.1  thorpej 			error = tlp_init(sc);
    771  1.1  thorpej 		}
    772  1.1  thorpej 		break;
    773  1.1  thorpej 
    774  1.1  thorpej 	case SIOCADDMULTI:
    775  1.1  thorpej 	case SIOCDELMULTI:
    776  1.1  thorpej 		error = (cmd == SIOCADDMULTI) ?
    777  1.1  thorpej 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    778  1.1  thorpej 		    ether_delmulti(ifr, &sc->sc_ethercom);
    779  1.1  thorpej 
    780  1.1  thorpej 		if (error == ENETRESET) {
    781  1.1  thorpej 			/*
    782  1.1  thorpej 			 * Multicast list has changed.  Set the filter
    783  1.1  thorpej 			 * accordingly.
    784  1.1  thorpej 			 */
    785  1.1  thorpej 			(*sc->sc_filter_setup)(sc);
    786  1.1  thorpej 			error = 0;
    787  1.1  thorpej 		}
    788  1.1  thorpej 		break;
    789  1.1  thorpej 
    790  1.1  thorpej 	case SIOCSIFMEDIA:
    791  1.1  thorpej 	case SIOCGIFMEDIA:
    792  1.1  thorpej 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    793  1.1  thorpej 		break;
    794  1.1  thorpej 
    795  1.1  thorpej 	default:
    796  1.1  thorpej 		error = EINVAL;
    797  1.1  thorpej 		break;
    798  1.1  thorpej 	}
    799  1.1  thorpej 
    800  1.1  thorpej 	/* Try to get more packets going. */
    801  1.1  thorpej 	tlp_start(ifp);
    802  1.1  thorpej 
    803  1.1  thorpej 	splx(s);
    804  1.1  thorpej 	return (error);
    805  1.1  thorpej }
    806  1.1  thorpej 
    807  1.1  thorpej /*
    808  1.1  thorpej  * tlp_intr:
    809  1.1  thorpej  *
    810  1.1  thorpej  *	Interrupt service routine.
    811  1.1  thorpej  */
    812  1.1  thorpej int
    813  1.1  thorpej tlp_intr(arg)
    814  1.1  thorpej 	void *arg;
    815  1.1  thorpej {
    816  1.1  thorpej 	struct tulip_softc *sc = arg;
    817  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    818  1.5  thorpej 	u_int32_t status, rxstatus, txstatus;
    819  1.1  thorpej 	int handled = 0, txthresh;
    820  1.1  thorpej 
    821  1.5  thorpej 	DPRINTF(sc, ("%s: tlp_intr\n", sc->sc_dev.dv_xname));
    822  1.1  thorpej 
    823  1.1  thorpej 	for (;;) {
    824  1.1  thorpej 		status = TULIP_READ(sc, CSR_STATUS);
    825  1.1  thorpej 		if (status)
    826  1.1  thorpej 			TULIP_WRITE(sc, CSR_STATUS, status);
    827  1.1  thorpej 
    828  1.1  thorpej 		if ((status & sc->sc_inten) == 0)
    829  1.1  thorpej 			break;
    830  1.1  thorpej 
    831  1.1  thorpej 		handled = 1;
    832  1.1  thorpej 
    833  1.5  thorpej 		rxstatus = status & sc->sc_rxint_mask;
    834  1.5  thorpej 		txstatus = status & sc->sc_txint_mask;
    835  1.5  thorpej 
    836  1.5  thorpej 		if (rxstatus) {
    837  1.1  thorpej 			/* Grab new any new packets. */
    838  1.1  thorpej 			tlp_rxintr(sc);
    839  1.1  thorpej 
    840  1.5  thorpej 			if (rxstatus & STATUS_RWT)
    841  1.1  thorpej 				printf("%s: receive watchdog timeout\n",
    842  1.1  thorpej 				    sc->sc_dev.dv_xname);
    843  1.1  thorpej 
    844  1.5  thorpej 			if (rxstatus & STATUS_RU) {
    845  1.1  thorpej 				printf("%s: receive ring overrun\n",
    846  1.1  thorpej 				    sc->sc_dev.dv_xname);
    847  1.1  thorpej 				/* Get the receive process going again. */
    848  1.1  thorpej 				tlp_idle(sc, OPMODE_SR);
    849  1.1  thorpej 				TULIP_WRITE(sc, CSR_RXLIST,
    850  1.1  thorpej 				    TULIP_CDRXADDR(sc, sc->sc_rxptr));
    851  1.1  thorpej 				TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
    852  1.1  thorpej 				TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
    853  1.1  thorpej 				break;
    854  1.1  thorpej 			}
    855  1.1  thorpej 		}
    856  1.1  thorpej 
    857  1.5  thorpej 		if (txstatus) {
    858  1.1  thorpej 			/* Sweep up transmit descriptors. */
    859  1.1  thorpej 			tlp_txintr(sc);
    860  1.1  thorpej 
    861  1.5  thorpej 			if (txstatus & STATUS_TJT)
    862  1.1  thorpej 				printf("%s: transmit jabber timeout\n",
    863  1.1  thorpej 				    sc->sc_dev.dv_xname);
    864  1.1  thorpej 
    865  1.5  thorpej 			if (txstatus & STATUS_UNF) {
    866  1.1  thorpej 				/*
    867  1.1  thorpej 				 * Increase our transmit threshold if
    868  1.1  thorpej 				 * another is available.
    869  1.1  thorpej 				 */
    870  1.1  thorpej 				txthresh = sc->sc_txthresh + 1;
    871  1.1  thorpej 				if (sc->sc_txth[txthresh].txth_name != NULL) {
    872  1.1  thorpej 					/* Idle the transmit process. */
    873  1.1  thorpej 					tlp_idle(sc, OPMODE_ST);
    874  1.1  thorpej 
    875  1.1  thorpej 					sc->sc_txthresh = txthresh;
    876  1.1  thorpej 					sc->sc_opmode &= ~(OPMODE_TR|OPMODE_SF);
    877  1.1  thorpej 					sc->sc_opmode |=
    878  1.1  thorpej 					    sc->sc_txth[txthresh].txth_opmode;
    879  1.1  thorpej 					printf("%s: transmit underrun; new "
    880  1.1  thorpej 					    "threshold: %s\n",
    881  1.1  thorpej 					    sc->sc_dev.dv_xname,
    882  1.1  thorpej 					    sc->sc_txth[txthresh].txth_name);
    883  1.1  thorpej 
    884  1.1  thorpej 					/*
    885  1.1  thorpej 					 * Set the new threshold and restart
    886  1.1  thorpej 					 * the transmit process.
    887  1.1  thorpej 					 */
    888  1.1  thorpej 					TULIP_WRITE(sc, CSR_OPMODE,
    889  1.1  thorpej 					    sc->sc_opmode);
    890  1.1  thorpej 				}
    891  1.1  thorpej 					/*
    892  1.1  thorpej 					 * XXX Log every Nth underrun from
    893  1.1  thorpej 					 * XXX now on?
    894  1.1  thorpej 					 */
    895  1.1  thorpej 			}
    896  1.1  thorpej 		}
    897  1.1  thorpej 
    898  1.1  thorpej 		if (status & (STATUS_TPS|STATUS_RPS)) {
    899  1.1  thorpej 			if (status & STATUS_TPS)
    900  1.1  thorpej 				printf("%s: transmit process stopped\n",
    901  1.1  thorpej 				    sc->sc_dev.dv_xname);
    902  1.1  thorpej 			if (status & STATUS_RPS)
    903  1.1  thorpej 				printf("%s: receive process stopped\n",
    904  1.1  thorpej 				    sc->sc_dev.dv_xname);
    905  1.1  thorpej 			(void) tlp_init(sc);
    906  1.1  thorpej 			break;
    907  1.1  thorpej 		}
    908  1.1  thorpej 
    909  1.1  thorpej 		if (status & STATUS_SE) {
    910  1.1  thorpej 			const char *str;
    911  1.1  thorpej 			switch (status & STATUS_EB) {
    912  1.1  thorpej 			case STATUS_EB_PARITY:
    913  1.1  thorpej 				str = "parity error";
    914  1.1  thorpej 				break;
    915  1.1  thorpej 
    916  1.1  thorpej 			case STATUS_EB_MABT:
    917  1.1  thorpej 				str = "master abort";
    918  1.1  thorpej 				break;
    919  1.1  thorpej 
    920  1.1  thorpej 			case STATUS_EB_TABT:
    921  1.1  thorpej 				str = "target abort";
    922  1.1  thorpej 				break;
    923  1.1  thorpej 
    924  1.1  thorpej 			default:
    925  1.1  thorpej 				str = "unknown error";
    926  1.1  thorpej 				break;
    927  1.1  thorpej 			}
    928  1.1  thorpej 			printf("%s: fatal system error: %s\n",
    929  1.1  thorpej 			    sc->sc_dev.dv_xname, str);
    930  1.1  thorpej 			(void) tlp_init(sc);
    931  1.1  thorpej 			break;
    932  1.1  thorpej 		}
    933  1.1  thorpej 
    934  1.1  thorpej 		/*
    935  1.1  thorpej 		 * Not handled:
    936  1.1  thorpej 		 *
    937  1.1  thorpej 		 *	Transmit buffer unavailable -- normal
    938  1.1  thorpej 		 *	condition, nothing to do, really.
    939  1.1  thorpej 		 *
    940  1.1  thorpej 		 *	General purpose timer experied -- we don't
    941  1.1  thorpej 		 *	use the general purpose timer.
    942  1.1  thorpej 		 *
    943  1.1  thorpej 		 *	Early receive interrupt -- not available on
    944  1.1  thorpej 		 *	all chips, we just use RI.  We also only
    945  1.1  thorpej 		 *	use single-segment receive DMA, so this
    946  1.1  thorpej 		 *	is mostly useless.
    947  1.1  thorpej 		 */
    948  1.1  thorpej 	}
    949  1.1  thorpej 
    950  1.1  thorpej 	/* Try to get more packets going. */
    951  1.1  thorpej 	tlp_start(ifp);
    952  1.1  thorpej 
    953  1.1  thorpej 	return (handled);
    954  1.1  thorpej }
    955  1.1  thorpej 
    956  1.1  thorpej /*
    957  1.1  thorpej  * tlp_rxintr:
    958  1.1  thorpej  *
    959  1.1  thorpej  *	Helper; handle receive interrupts.
    960  1.1  thorpej  */
    961  1.1  thorpej void
    962  1.1  thorpej tlp_rxintr(sc)
    963  1.1  thorpej 	struct tulip_softc *sc;
    964  1.1  thorpej {
    965  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    966  1.1  thorpej 	struct ether_header *eh;
    967  1.1  thorpej 	struct tulip_rxsoft *rxs;
    968  1.1  thorpej 	struct mbuf *m;
    969  1.1  thorpej 	u_int32_t rxstat;
    970  1.1  thorpej 	int i, len;
    971  1.1  thorpej 
    972  1.1  thorpej 	for (i = sc->sc_rxptr;; i = TULIP_NEXTRX(i)) {
    973  1.1  thorpej 		rxs = &sc->sc_rxsoft[i];
    974  1.1  thorpej 
    975  1.1  thorpej 		TULIP_CDRXSYNC(sc, i,
    976  1.1  thorpej 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    977  1.1  thorpej 
    978  1.1  thorpej 		rxstat = sc->sc_rxdescs[i].td_status;
    979  1.1  thorpej 
    980  1.1  thorpej 		if (rxstat & TDSTAT_OWN) {
    981  1.1  thorpej 			/*
    982  1.1  thorpej 			 * We have processed all of the receive buffers.
    983  1.1  thorpej 			 */
    984  1.1  thorpej 			break;
    985  1.1  thorpej 		}
    986  1.1  thorpej 
    987  1.1  thorpej 		/*
    988  1.1  thorpej 		 * Make sure the packet fit in one buffer.  This should
    989  1.1  thorpej 		 * always be the case.  But the Lite-On PNIC, rev 33
    990  1.1  thorpej 		 * has an awful receive engine bug, which may require
    991  1.1  thorpej 		 * a very icky work-around.
    992  1.1  thorpej 		 */
    993  1.1  thorpej 		if ((rxstat & (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) !=
    994  1.1  thorpej 		    (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) {
    995  1.1  thorpej 			printf("%s: incoming packet spilled, resetting\n",
    996  1.1  thorpej 			    sc->sc_dev.dv_xname);
    997  1.1  thorpej 			(void) tlp_init(sc);
    998  1.1  thorpej 			return;
    999  1.1  thorpej 		}
   1000  1.1  thorpej 
   1001  1.1  thorpej 		/*
   1002  1.1  thorpej 		 * If any collisions were seen on the wire, count one.
   1003  1.1  thorpej 		 */
   1004  1.1  thorpej 		if (rxstat & TDSTAT_Rx_CS)
   1005  1.1  thorpej 			ifp->if_collisions++;
   1006  1.1  thorpej 
   1007  1.1  thorpej 		/*
   1008  1.1  thorpej 		 * If an error occured, update stats, clear the status
   1009  1.1  thorpej 		 * word, and leave the packet buffer in place.  It will
   1010  1.1  thorpej 		 * simply be reused the next time the ring comes around.
   1011  1.1  thorpej 		 */
   1012  1.1  thorpej 		if (rxstat & TDSTAT_ES) {
   1013  1.1  thorpej #define	PRINTERR(bit, str)						\
   1014  1.1  thorpej 			if (rxstat & (bit))				\
   1015  1.1  thorpej 				printf("%s: receive error: %s\n",	\
   1016  1.1  thorpej 				    sc->sc_dev.dv_xname, str)
   1017  1.1  thorpej 			ifp->if_ierrors++;
   1018  1.1  thorpej 			PRINTERR(TDSTAT_Rx_DE, "descriptor error");
   1019  1.1  thorpej 			PRINTERR(TDSTAT_Rx_RF, "runt frame");
   1020  1.1  thorpej 			PRINTERR(TDSTAT_Rx_TL, "frame too long");
   1021  1.1  thorpej 			PRINTERR(TDSTAT_Rx_RE, "MII error");
   1022  1.1  thorpej 			PRINTERR(TDSTAT_Rx_DB, "dribbling bit");
   1023  1.1  thorpej 			PRINTERR(TDSTAT_Rx_CE, "CRC error");
   1024  1.1  thorpej #undef PRINTERR
   1025  1.1  thorpej 			TULIP_INIT_RXDESC(sc, i);
   1026  1.1  thorpej 			continue;
   1027  1.1  thorpej 		}
   1028  1.1  thorpej 
   1029  1.1  thorpej 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1030  1.1  thorpej 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1031  1.1  thorpej 
   1032  1.1  thorpej 		/*
   1033  1.1  thorpej 		 * No errors; receive the packet.  Note the Tulip
   1034  1.1  thorpej 		 * includes the CRC with every packet; trim it.
   1035  1.1  thorpej 		 */
   1036  1.1  thorpej 		len = TDSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;
   1037  1.1  thorpej 
   1038  1.1  thorpej #ifdef __NO_STRICT_ALIGNMENT
   1039  1.1  thorpej 		/*
   1040  1.1  thorpej 		 * Allocate a new mbuf cluster.  If that fails, we are
   1041  1.1  thorpej 		 * out of memory, and must drop the packet and recycle
   1042  1.1  thorpej 		 * the buffer that's already attached to this descriptor.
   1043  1.1  thorpej 		 */
   1044  1.1  thorpej 		m = rxs->rxs_mbuf;
   1045  1.1  thorpej 		if (tlp_add_rxbuf(sc, i) != 0) {
   1046  1.1  thorpej 			ifp->if_ierrors++;
   1047  1.1  thorpej 			TULIP_INIT_RXDESC(sc, i);
   1048  1.1  thorpej 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1049  1.1  thorpej 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1050  1.1  thorpej 			continue;
   1051  1.1  thorpej 		}
   1052  1.1  thorpej #else
   1053  1.1  thorpej 		/*
   1054  1.1  thorpej 		 * The Tulip's receive buffers must be 4-byte aligned.
   1055  1.1  thorpej 		 * But this means that the data after the Ethernet header
   1056  1.1  thorpej 		 * is misaligned.  We must allocate a new buffer and
   1057  1.1  thorpej 		 * copy the data, shifted forward 2 bytes.
   1058  1.1  thorpej 		 */
   1059  1.1  thorpej 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1060  1.1  thorpej 		if (m == NULL) {
   1061  1.1  thorpej  dropit:
   1062  1.1  thorpej 			ifp->if_ierrors++;
   1063  1.1  thorpej 			TULIP_INIT_RXDESC(sc, i);
   1064  1.1  thorpej 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1065  1.1  thorpej 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1066  1.1  thorpej 			continue;
   1067  1.1  thorpej 		}
   1068  1.1  thorpej 		if (len > (MHLEN - 2)) {
   1069  1.1  thorpej 			MCLGET(m, M_DONTWAIT);
   1070  1.1  thorpej 			if ((m->m_flags & M_EXT) == 0) {
   1071  1.1  thorpej 				m_freem(m);
   1072  1.1  thorpej 				goto dropit;
   1073  1.1  thorpej 			}
   1074  1.1  thorpej 		}
   1075  1.1  thorpej 		m->m_data += 2;
   1076  1.1  thorpej 
   1077  1.1  thorpej 		/*
   1078  1.1  thorpej 		 * Note that we use clusters for incoming frames, so the
   1079  1.1  thorpej 		 * buffer is virtually contiguous.
   1080  1.1  thorpej 		 */
   1081  1.1  thorpej 		memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
   1082  1.1  thorpej 
   1083  1.1  thorpej 		/* Allow the receive descriptor to continue using its mbuf. */
   1084  1.1  thorpej 		TULIP_INIT_RXDESC(sc, i);
   1085  1.1  thorpej 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1086  1.1  thorpej 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1087  1.1  thorpej #endif /* __NO_STRICT_ALIGNMENT */
   1088  1.1  thorpej 
   1089  1.1  thorpej 		ifp->if_ipackets++;
   1090  1.1  thorpej 		eh = mtod(m, struct ether_header *);
   1091  1.1  thorpej 		m->m_pkthdr.rcvif = ifp;
   1092  1.1  thorpej 		m->m_pkthdr.len = m->m_len = len;
   1093  1.1  thorpej 
   1094  1.1  thorpej #if NBPFILTER > 0
   1095  1.1  thorpej 		/*
   1096  1.1  thorpej 		 * Pass this up to any BPF listeners, but only
   1097  1.1  thorpej 		 * pass it up the stack if its for us.
   1098  1.1  thorpej 		 */
   1099  1.1  thorpej 		if (ifp->if_bpf)
   1100  1.1  thorpej 			bpf_mtap(ifp->if_bpf, m);
   1101  1.1  thorpej #endif /* NPBFILTER > 0 */
   1102  1.1  thorpej 
   1103  1.1  thorpej 		/*
   1104  1.1  thorpej 		 * This test is outside the NBPFILTER block because
   1105  1.1  thorpej 		 * on the 21140 we have to use Hash-Only mode due to
   1106  1.1  thorpej 		 * a bug in the filter logic.
   1107  1.1  thorpej 		 */
   1108  1.1  thorpej 		if ((ifp->if_flags & IFF_PROMISC) != 0 ||
   1109  1.1  thorpej 		    sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   1110  1.1  thorpej 			if (memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
   1111  1.1  thorpej 				 ETHER_ADDR_LEN) != 0 &&
   1112  1.1  thorpej 			    ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
   1113  1.1  thorpej 				m_freem(m);
   1114  1.1  thorpej 				continue;
   1115  1.1  thorpej 			}
   1116  1.1  thorpej 		}
   1117  1.1  thorpej 
   1118  1.1  thorpej 		/* Pass it on. */
   1119  1.1  thorpej 		(*ifp->if_input)(ifp, m);
   1120  1.1  thorpej 	}
   1121  1.1  thorpej 
   1122  1.1  thorpej 	/* Update the recieve pointer. */
   1123  1.1  thorpej 	sc->sc_rxptr = i;
   1124  1.1  thorpej }
   1125  1.1  thorpej 
   1126  1.1  thorpej /*
   1127  1.1  thorpej  * tlp_txintr:
   1128  1.1  thorpej  *
   1129  1.1  thorpej  *	Helper; handle transmit interrupts.
   1130  1.1  thorpej  */
   1131  1.1  thorpej void
   1132  1.1  thorpej tlp_txintr(sc)
   1133  1.1  thorpej 	struct tulip_softc *sc;
   1134  1.1  thorpej {
   1135  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1136  1.1  thorpej 	struct tulip_txsoft *txs;
   1137  1.1  thorpej 	u_int32_t txstat;
   1138  1.1  thorpej 
   1139  1.5  thorpej 	DPRINTF(sc, ("%s: tlp_txintr: sc_flags 0x%08x\n",
   1140  1.1  thorpej 	    sc->sc_dev.dv_xname, sc->sc_flags));
   1141  1.1  thorpej 
   1142  1.1  thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
   1143  1.1  thorpej 
   1144  1.1  thorpej 	/*
   1145  1.4  thorpej 	 * If we were doing a filter setup, check to see if it completed.
   1146  1.4  thorpej 	 */
   1147  1.4  thorpej 	if (sc->sc_flags & TULIPF_DOING_SETUP) {
   1148  1.4  thorpej 		TULIP_CDSDSYNC(sc, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1149  1.4  thorpej 		if ((sc->sc_setup_desc.td_status & TDSTAT_OWN) == 0)
   1150  1.4  thorpej 			sc->sc_flags &= ~TULIPF_DOING_SETUP;
   1151  1.4  thorpej 	}
   1152  1.4  thorpej 
   1153  1.4  thorpej 	/*
   1154  1.1  thorpej 	 * Go through our Tx list and free mbufs for those
   1155  1.1  thorpej 	 * frames that have been transmitted.
   1156  1.1  thorpej 	 */
   1157  1.1  thorpej 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1158  1.1  thorpej 		TULIP_CDTXSYNC(sc, txs->txs_firstdesc,
   1159  1.1  thorpej 		    txs->txs_dmamap->dm_nsegs,
   1160  1.1  thorpej 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1161  1.1  thorpej 
   1162  1.1  thorpej #ifdef TLP_DEBUG
   1163  1.5  thorpej 		if (ifp->if_flags & IFF_DEBUG) {
   1164  1.5  thorpej 			int i;
   1165  1.5  thorpej 			printf("    txsoft %p trainsmit chain:\n", txs);
   1166  1.5  thorpej 			for (i = txs->txs_firstdesc;; i = TULIP_NEXTTX(i)) {
   1167  1.5  thorpej 				printf("     descriptor %d:\n", i);
   1168  1.5  thorpej 				printf("       td_status:   0x%08x\n",
   1169  1.5  thorpej 				    sc->sc_txdescs[i].td_status);
   1170  1.5  thorpej 				printf("       td_ctl:      0x%08x\n",
   1171  1.5  thorpej 				    sc->sc_txdescs[i].td_ctl);
   1172  1.5  thorpej 				printf("       td_bufaddr1: 0x%08x\n",
   1173  1.5  thorpej 				    sc->sc_txdescs[i].td_bufaddr1);
   1174  1.5  thorpej 				printf("       td_bufaddr2: 0x%08x\n",
   1175  1.5  thorpej 				    sc->sc_txdescs[i].td_bufaddr2);
   1176  1.5  thorpej 				if (i == txs->txs_lastdesc)
   1177  1.5  thorpej 					break;
   1178  1.5  thorpej 			}
   1179  1.5  thorpej 		}
   1180  1.1  thorpej #endif
   1181  1.1  thorpej 
   1182  1.1  thorpej 		txstat = sc->sc_txdescs[txs->txs_firstdesc].td_status;
   1183  1.1  thorpej 		if (txstat & TDSTAT_OWN)
   1184  1.1  thorpej 			break;
   1185  1.1  thorpej 
   1186  1.1  thorpej 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1187  1.1  thorpej 
   1188  1.1  thorpej 		sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
   1189  1.1  thorpej 
   1190  1.1  thorpej 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1191  1.1  thorpej 		    0, txs->txs_dmamap->dm_mapsize,
   1192  1.1  thorpej 		    BUS_DMASYNC_POSTWRITE);
   1193  1.1  thorpej 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1194  1.1  thorpej 		m_freem(txs->txs_mbuf);
   1195  1.1  thorpej 		txs->txs_mbuf = NULL;
   1196  1.1  thorpej 
   1197  1.1  thorpej 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1198  1.1  thorpej 
   1199  1.1  thorpej 		/*
   1200  1.1  thorpej 		 * Check for errors and collisions.
   1201  1.1  thorpej 		 */
   1202  1.1  thorpej 		if (txstat & TDSTAT_ES) {
   1203  1.1  thorpej 			ifp->if_oerrors++;
   1204  1.1  thorpej 			if (txstat & TDSTAT_Tx_EC)
   1205  1.1  thorpej 				ifp->if_collisions += 16;
   1206  1.1  thorpej 			if (txstat & TDSTAT_Tx_LC)
   1207  1.1  thorpej 				ifp->if_collisions++;
   1208  1.1  thorpej 		} else {
   1209  1.1  thorpej 			/* Packet was transmitted successfully. */
   1210  1.1  thorpej 			ifp->if_opackets++;
   1211  1.1  thorpej 			ifp->if_collisions += TDSTAT_Tx_COLLISIONS(txstat);
   1212  1.1  thorpej 		}
   1213  1.1  thorpej 	}
   1214  1.1  thorpej 
   1215  1.1  thorpej 	/*
   1216  1.1  thorpej 	 * If there are no more pending transmissions, cancel the watchdog
   1217  1.1  thorpej 	 * timer.
   1218  1.1  thorpej 	 */
   1219  1.4  thorpej 	if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0)
   1220  1.1  thorpej 		ifp->if_timer = 0;
   1221  1.1  thorpej 
   1222  1.1  thorpej 	/*
   1223  1.1  thorpej 	 * If we have a receive filter setup pending, do it now.
   1224  1.1  thorpej 	 */
   1225  1.1  thorpej 	if (sc->sc_flags & TULIPF_WANT_SETUP)
   1226  1.1  thorpej 		(*sc->sc_filter_setup)(sc);
   1227  1.1  thorpej }
   1228  1.1  thorpej 
   1229  1.1  thorpej /*
   1230  1.1  thorpej  * tlp_reset:
   1231  1.1  thorpej  *
   1232  1.1  thorpej  *	Perform a soft reset on the Tulip.
   1233  1.1  thorpej  */
   1234  1.1  thorpej void
   1235  1.1  thorpej tlp_reset(sc)
   1236  1.1  thorpej 	struct tulip_softc *sc;
   1237  1.1  thorpej {
   1238  1.1  thorpej 	int i;
   1239  1.1  thorpej 
   1240  1.1  thorpej 	TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
   1241  1.1  thorpej 
   1242  1.1  thorpej 	for (i = 0; i < 1000; i++) {
   1243  1.1  thorpej 		if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
   1244  1.1  thorpej 			break;
   1245  1.1  thorpej 		delay(10);
   1246  1.1  thorpej 	}
   1247  1.1  thorpej 
   1248  1.1  thorpej 	if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
   1249  1.1  thorpej 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
   1250  1.1  thorpej 
   1251  1.1  thorpej 	delay(1000);
   1252  1.1  thorpej }
   1253  1.1  thorpej 
   1254  1.1  thorpej /*
   1255  1.1  thorpej  * tlp_init:
   1256  1.1  thorpej  *
   1257  1.1  thorpej  *	Initialize the interface.  Must be called at splnet().
   1258  1.1  thorpej  */
   1259  1.1  thorpej int
   1260  1.1  thorpej tlp_init(sc)
   1261  1.1  thorpej 	struct tulip_softc *sc;
   1262  1.1  thorpej {
   1263  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1264  1.1  thorpej 	struct tulip_txsoft *txs;
   1265  1.1  thorpej 	struct tulip_rxsoft *rxs;
   1266  1.1  thorpej 	int i, error = 0;
   1267  1.1  thorpej 
   1268  1.1  thorpej 	/*
   1269  1.1  thorpej 	 * Cancel any pending I/O.
   1270  1.1  thorpej 	 */
   1271  1.1  thorpej 	tlp_stop(sc, 0);
   1272  1.1  thorpej 
   1273  1.1  thorpej 	/*
   1274  1.1  thorpej 	 * Reset the Tulip to a known state.
   1275  1.1  thorpej 	 */
   1276  1.1  thorpej 	tlp_reset(sc);
   1277  1.1  thorpej 
   1278  1.1  thorpej 	/*
   1279  1.1  thorpej 	 * Initialize the BUSMODE register.
   1280  1.1  thorpej 	 *
   1281  1.1  thorpej 	 * XXX What about read-multiple/read-line/write-line on
   1282  1.1  thorpej 	 * XXX the 21140 and up?
   1283  1.1  thorpej 	 */
   1284  1.1  thorpej 	sc->sc_busmode = BUSMODE_BAR | BUSMODE_PBL_DEFAULT;
   1285  1.1  thorpej 	switch (sc->sc_cacheline) {
   1286  1.1  thorpej 	default:
   1287  1.1  thorpej 		/*
   1288  1.1  thorpej 		 * Note: We must *always* set these bits; a cache
   1289  1.1  thorpej 		 * alignment of 0 is RESERVED.
   1290  1.1  thorpej 		 */
   1291  1.1  thorpej 	case 8:
   1292  1.1  thorpej 		sc->sc_busmode |= BUSMODE_CAL_8LW;
   1293  1.1  thorpej 		break;
   1294  1.1  thorpej 	case 16:
   1295  1.1  thorpej 		sc->sc_busmode |= BUSMODE_CAL_16LW;
   1296  1.1  thorpej 		break;
   1297  1.1  thorpej 	case 32:
   1298  1.1  thorpej 		sc->sc_busmode |= BUSMODE_CAL_32LW;
   1299  1.1  thorpej 		break;
   1300  1.1  thorpej 	}
   1301  1.1  thorpej 	switch (sc->sc_chip) {
   1302  1.1  thorpej 	case TULIP_CHIP_82C168:
   1303  1.1  thorpej 	case TULIP_CHIP_82C169:
   1304  1.1  thorpej 		sc->sc_busmode |= BUSMODE_PNIC_MBO;
   1305  1.1  thorpej 		break;
   1306  1.1  thorpej 	default:
   1307  1.1  thorpej 		/* Nothing. */
   1308  1.1  thorpej 		break;
   1309  1.1  thorpej 	}
   1310  1.1  thorpej #if BYTE_ORDER == BIG_ENDIAN
   1311  1.1  thorpej 	/*
   1312  1.1  thorpej 	 * XXX There are reports that this doesn't work properly
   1313  1.1  thorpej 	 * in the old Tulip driver, but BUSMODE_DBO does.  However,
   1314  1.1  thorpej 	 * BUSMODE_DBO is not available on the 21040, and requires
   1315  1.1  thorpej 	 * us to byte-swap the setup packet.  What to do?
   1316  1.1  thorpej 	 */
   1317  1.1  thorpej 	sc->sc_busmode |= BUSMODE_BLE;
   1318  1.1  thorpej #endif
   1319  1.1  thorpej 	TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);
   1320  1.1  thorpej 
   1321  1.1  thorpej 	/*
   1322  1.1  thorpej 	 * Initialize the OPMODE register.  We don't write it until
   1323  1.1  thorpej 	 * we're ready to begin the transmit and receive processes.
   1324  1.1  thorpej 	 *
   1325  1.1  thorpej 	 * Media-related OPMODE bits are set in the media callbacks
   1326  1.1  thorpej 	 * for each specific chip/board.
   1327  1.1  thorpej 	 */
   1328  1.1  thorpej 	sc->sc_opmode = OPMODE_SR | OPMODE_ST |
   1329  1.1  thorpej 	    sc->sc_txth[sc->sc_txthresh].txth_opmode;
   1330  1.1  thorpej 	switch (sc->sc_chip) {
   1331  1.1  thorpej 	case TULIP_CHIP_21140:
   1332  1.1  thorpej 	case TULIP_CHIP_21140A:
   1333  1.1  thorpej 	case TULIP_CHIP_21142:
   1334  1.1  thorpej 	case TULIP_CHIP_21143:
   1335  1.1  thorpej 		sc->sc_opmode |= OPMODE_MBO;
   1336  1.1  thorpej 		break;
   1337  1.1  thorpej 
   1338  1.1  thorpej 	default:
   1339  1.1  thorpej 		/* Nothing. */
   1340  1.1  thorpej 	}
   1341  1.1  thorpej 
   1342  1.1  thorpej 	if (sc->sc_flags & TULIPF_HAS_MII) {
   1343  1.1  thorpej 		switch (sc->sc_chip) {
   1344  1.1  thorpej 		case TULIP_CHIP_82C168:
   1345  1.1  thorpej 		case TULIP_CHIP_82C169:
   1346  1.5  thorpej 			/* Enable the MII port. */
   1347  1.5  thorpej 			sc->sc_opmode |= OPMODE_PS;
   1348  1.5  thorpej 
   1349  1.6  thorpej 			TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
   1350  1.1  thorpej 			break;
   1351  1.1  thorpej 
   1352  1.5  thorpej 		case TULIP_CHIP_WB89C840F:
   1353  1.5  thorpej 			/* Nothing. */
   1354  1.5  thorpej 			break;
   1355  1.5  thorpej 
   1356  1.1  thorpej 		default:
   1357  1.5  thorpej 			/* Enable the MII port. */
   1358  1.5  thorpej 			sc->sc_opmode |= OPMODE_PS;
   1359  1.5  thorpej 			break;
   1360  1.1  thorpej 		}
   1361  1.1  thorpej 	}
   1362  1.1  thorpej 
   1363  1.1  thorpej 	/*
   1364  1.1  thorpej 	 * Magical mystery initialization on the Macronix chips.
   1365  1.1  thorpej 	 * The MX98713 uses its own magic value, the rest share
   1366  1.1  thorpej 	 * a common one.
   1367  1.1  thorpej 	 */
   1368  1.1  thorpej 	switch (sc->sc_chip) {
   1369  1.1  thorpej 	case TULIP_CHIP_MX98713:
   1370  1.1  thorpej 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
   1371  1.1  thorpej 		break;
   1372  1.1  thorpej 
   1373  1.1  thorpej 	case TULIP_CHIP_MX98713A:
   1374  1.1  thorpej 	case TULIP_CHIP_MX98715:
   1375  1.1  thorpej 	case TULIP_CHIP_MX98725:
   1376  1.1  thorpej 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
   1377  1.1  thorpej 		break;
   1378  1.1  thorpej 
   1379  1.1  thorpej 	default:
   1380  1.1  thorpej 		/* Nothing. */
   1381  1.1  thorpej 	}
   1382  1.1  thorpej 
   1383  1.1  thorpej 	/*
   1384  1.1  thorpej 	 * Initialize the transmit descriptor ring.
   1385  1.1  thorpej 	 */
   1386  1.1  thorpej 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1387  1.1  thorpej 	for (i = 0; i < TULIP_NTXDESC; i++) {
   1388  1.1  thorpej 		sc->sc_txdescs[i].td_ctl = TDCTL_CH;
   1389  1.1  thorpej 		sc->sc_txdescs[i].td_bufaddr2 =
   1390  1.1  thorpej 		    TULIP_CDTXADDR(sc, TULIP_NEXTTX(i));
   1391  1.1  thorpej 	}
   1392  1.1  thorpej 	TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
   1393  1.1  thorpej 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1394  1.1  thorpej 	sc->sc_txfree = TULIP_NTXDESC;
   1395  1.1  thorpej 	sc->sc_txnext = 0;
   1396  1.1  thorpej 
   1397  1.1  thorpej 	/*
   1398  1.1  thorpej 	 * Initialize the transmit job descriptors.
   1399  1.1  thorpej 	 */
   1400  1.1  thorpej 	SIMPLEQ_INIT(&sc->sc_txfreeq);
   1401  1.1  thorpej 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
   1402  1.1  thorpej 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
   1403  1.1  thorpej 		txs = &sc->sc_txsoft[i];
   1404  1.1  thorpej 		txs->txs_mbuf = NULL;
   1405  1.1  thorpej 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1406  1.1  thorpej 	}
   1407  1.1  thorpej 
   1408  1.1  thorpej 	/*
   1409  1.1  thorpej 	 * Initialize the receive descriptor and receive job
   1410  1.1  thorpej 	 * descriptor rings.
   1411  1.1  thorpej 	 */
   1412  1.1  thorpej 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1413  1.1  thorpej 		rxs = &sc->sc_rxsoft[i];
   1414  1.1  thorpej 		if (rxs->rxs_mbuf == NULL) {
   1415  1.1  thorpej 			if ((error = tlp_add_rxbuf(sc, i)) != 0) {
   1416  1.1  thorpej 				printf("%s: unable to allocate or map rx "
   1417  1.1  thorpej 				    "buffer %d, error = %d\n",
   1418  1.1  thorpej 				    sc->sc_dev.dv_xname, i, error);
   1419  1.1  thorpej 				/*
   1420  1.1  thorpej 				 * XXX Should attempt to run with fewer receive
   1421  1.1  thorpej 				 * XXX buffers instead of just failing.
   1422  1.1  thorpej 				 */
   1423  1.1  thorpej 				tlp_rxdrain(sc);
   1424  1.1  thorpej 				goto out;
   1425  1.1  thorpej 			}
   1426  1.1  thorpej 		}
   1427  1.1  thorpej 	}
   1428  1.1  thorpej 	sc->sc_rxptr = 0;
   1429  1.1  thorpej 
   1430  1.1  thorpej 	/*
   1431  1.1  thorpej 	 * Initialize the interrupt mask and enable interrupts.
   1432  1.1  thorpej 	 */
   1433  1.1  thorpej 	/* normal interrupts */
   1434  1.1  thorpej 	sc->sc_inten =  STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
   1435  1.5  thorpej 
   1436  1.1  thorpej 	/* abnormal interrupts */
   1437  1.1  thorpej 	sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
   1438  1.1  thorpej 	    STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;
   1439  1.1  thorpej 
   1440  1.5  thorpej 	sc->sc_rxint_mask = STATUS_RI|STATUS_RU|STATUS_RWT;
   1441  1.5  thorpej 	sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
   1442  1.5  thorpej 
   1443  1.5  thorpej 	switch (sc->sc_chip) {
   1444  1.5  thorpej 	case TULIP_CHIP_WB89C840F:
   1445  1.5  thorpej 		/*
   1446  1.5  thorpej 		 * Clear bits that we don't want that happen to
   1447  1.5  thorpej 		 * overlap or don't exist.
   1448  1.5  thorpej 		 */
   1449  1.5  thorpej 		sc->sc_inten &= ~(STATUS_WINB_REI|STATUS_RWT);
   1450  1.5  thorpej 		break;
   1451  1.5  thorpej 
   1452  1.5  thorpej 	default:
   1453  1.5  thorpej 		/* Nothing. */
   1454  1.5  thorpej 	}
   1455  1.5  thorpej 
   1456  1.5  thorpej 	sc->sc_rxint_mask &= sc->sc_inten;
   1457  1.5  thorpej 	sc->sc_txint_mask &= sc->sc_inten;
   1458  1.5  thorpej 
   1459  1.1  thorpej 	TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
   1460  1.1  thorpej 	TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);
   1461  1.1  thorpej 
   1462  1.1  thorpej 	/*
   1463  1.1  thorpej 	 * Give the transmit and receive rings to the Tulip.
   1464  1.1  thorpej 	 */
   1465  1.1  thorpej 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
   1466  1.1  thorpej 	TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));
   1467  1.1  thorpej 
   1468  1.1  thorpej 	/*
   1469  1.1  thorpej 	 * On chips that do this differently, set the station address.
   1470  1.1  thorpej 	 */
   1471  1.1  thorpej 	switch (sc->sc_chip) {
   1472  1.1  thorpej 	case TULIP_CHIP_WB89C840F:
   1473  1.1  thorpej 		/* XXX Do this with stream writes? */
   1474  1.1  thorpej 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1475  1.1  thorpej 			bus_space_write_1(sc->sc_st, sc->sc_sh,
   1476  1.5  thorpej 			    (CSR_WINB_CPA0 >> sc->sc_regshift) + i,
   1477  1.1  thorpej 			    LLADDR(ifp->if_sadl)[i]);
   1478  1.1  thorpej 		}
   1479  1.1  thorpej 		break;
   1480  1.1  thorpej 
   1481  1.1  thorpej 	default:
   1482  1.1  thorpej 		/* Nothing. */
   1483  1.1  thorpej 	}
   1484  1.1  thorpej 
   1485  1.1  thorpej 	/*
   1486  1.1  thorpej 	 * Set the receive filter.  This will start the transmit and
   1487  1.1  thorpej 	 * receive processes.
   1488  1.1  thorpej 	 */
   1489  1.1  thorpej 	(*sc->sc_filter_setup)(sc);
   1490  1.1  thorpej 
   1491  1.1  thorpej 	/*
   1492  1.1  thorpej 	 * Start the receive process.
   1493  1.1  thorpej 	 */
   1494  1.1  thorpej 	TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
   1495  1.1  thorpej 
   1496  1.6  thorpej 	if (sc->sc_tick != NULL) {
   1497  1.1  thorpej 		/* Start the one second clock. */
   1498  1.6  thorpej 		timeout(sc->sc_tick, sc, hz);
   1499  1.1  thorpej 	}
   1500  1.1  thorpej 
   1501  1.1  thorpej 	/*
   1502  1.1  thorpej 	 * Note that the interface is now running.
   1503  1.1  thorpej 	 */
   1504  1.1  thorpej 	ifp->if_flags |= IFF_RUNNING;
   1505  1.1  thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
   1506  1.1  thorpej 
   1507  1.1  thorpej 	/*
   1508  1.1  thorpej 	 * Set the media.  We must do this after the transmit process is
   1509  1.1  thorpej 	 * running, since we may actually have to transmit packets on
   1510  1.1  thorpej 	 * our board to test link integrity.
   1511  1.1  thorpej 	 */
   1512  1.1  thorpej 	(void) (*sc->sc_mediasw->tmsw_set)(sc);
   1513  1.1  thorpej 
   1514  1.1  thorpej  out:
   1515  1.1  thorpej 	if (error)
   1516  1.1  thorpej 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1517  1.1  thorpej 	return (error);
   1518  1.1  thorpej }
   1519  1.1  thorpej 
   1520  1.1  thorpej /*
   1521  1.1  thorpej  * tlp_rxdrain:
   1522  1.1  thorpej  *
   1523  1.1  thorpej  *	Drain the receive queue.
   1524  1.1  thorpej  */
   1525  1.1  thorpej void
   1526  1.1  thorpej tlp_rxdrain(sc)
   1527  1.1  thorpej 	struct tulip_softc *sc;
   1528  1.1  thorpej {
   1529  1.1  thorpej 	struct tulip_rxsoft *rxs;
   1530  1.1  thorpej 	int i;
   1531  1.1  thorpej 
   1532  1.1  thorpej 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1533  1.1  thorpej 		rxs = &sc->sc_rxsoft[i];
   1534  1.1  thorpej 		if (rxs->rxs_mbuf != NULL) {
   1535  1.1  thorpej 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1536  1.1  thorpej 			m_freem(rxs->rxs_mbuf);
   1537  1.1  thorpej 			rxs->rxs_mbuf = NULL;
   1538  1.1  thorpej 		}
   1539  1.1  thorpej 	}
   1540  1.1  thorpej }
   1541  1.1  thorpej 
   1542  1.1  thorpej /*
   1543  1.1  thorpej  * tlp_stop:
   1544  1.1  thorpej  *
   1545  1.1  thorpej  *	Stop transmission on the interface.
   1546  1.1  thorpej  */
   1547  1.1  thorpej void
   1548  1.1  thorpej tlp_stop(sc, drain)
   1549  1.1  thorpej 	struct tulip_softc *sc;
   1550  1.1  thorpej 	int drain;
   1551  1.1  thorpej {
   1552  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1553  1.1  thorpej 	struct tulip_txsoft *txs;
   1554  1.1  thorpej 
   1555  1.6  thorpej 	if (sc->sc_tick != NULL) {
   1556  1.1  thorpej 		/* Stop the one second clock. */
   1557  1.6  thorpej 		untimeout(sc->sc_tick, sc);
   1558  1.1  thorpej 	}
   1559  1.1  thorpej 
   1560  1.1  thorpej 	/* Disable interrupts. */
   1561  1.1  thorpej 	TULIP_WRITE(sc, CSR_INTEN, 0);
   1562  1.1  thorpej 
   1563  1.1  thorpej 	/* Stop the transmit and receive processes. */
   1564  1.1  thorpej 	TULIP_WRITE(sc, CSR_OPMODE, 0);
   1565  1.1  thorpej 	TULIP_WRITE(sc, CSR_RXLIST, 0);
   1566  1.1  thorpej 	TULIP_WRITE(sc, CSR_TXLIST, 0);
   1567  1.1  thorpej 
   1568  1.1  thorpej 	/*
   1569  1.1  thorpej 	 * Release any queued transmit buffers.
   1570  1.1  thorpej 	 */
   1571  1.1  thorpej 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1572  1.1  thorpej 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1573  1.1  thorpej 		if (txs->txs_mbuf != NULL) {
   1574  1.1  thorpej 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1575  1.1  thorpej 			m_freem(txs->txs_mbuf);
   1576  1.1  thorpej 			txs->txs_mbuf = NULL;
   1577  1.1  thorpej 		}
   1578  1.1  thorpej 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1579  1.1  thorpej 	}
   1580  1.1  thorpej 
   1581  1.1  thorpej 	if (drain) {
   1582  1.1  thorpej 		/*
   1583  1.1  thorpej 		 * Release the receive buffers.
   1584  1.1  thorpej 		 */
   1585  1.1  thorpej 		tlp_rxdrain(sc);
   1586  1.1  thorpej 	}
   1587  1.1  thorpej 
   1588  1.4  thorpej 	sc->sc_flags &= ~(TULIPF_WANT_SETUP|TULIPF_DOING_SETUP);
   1589  1.1  thorpej 
   1590  1.1  thorpej 	/*
   1591  1.1  thorpej 	 * Mark the interface down and cancel the watchdog timer.
   1592  1.1  thorpej 	 */
   1593  1.1  thorpej 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1594  1.1  thorpej 	ifp->if_timer = 0;
   1595  1.1  thorpej }
   1596  1.1  thorpej 
   1597  1.1  thorpej #define	SROM_EMIT(sc, x)						\
   1598  1.1  thorpej do {									\
   1599  1.1  thorpej 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
   1600  1.1  thorpej 	delay(1);							\
   1601  1.1  thorpej } while (0)
   1602  1.1  thorpej 
   1603  1.1  thorpej /*
   1604  1.1  thorpej  * tlp_srom_idle:
   1605  1.1  thorpej  *
   1606  1.1  thorpej  *	Put the SROM in idle state.
   1607  1.1  thorpej  */
   1608  1.1  thorpej void
   1609  1.1  thorpej tlp_srom_idle(sc)
   1610  1.1  thorpej 	struct tulip_softc *sc;
   1611  1.1  thorpej {
   1612  1.1  thorpej 	u_int32_t miirom;
   1613  1.1  thorpej 	int i;
   1614  1.1  thorpej 
   1615  1.1  thorpej 	miirom = MIIROM_SR;
   1616  1.1  thorpej 	SROM_EMIT(sc, miirom);
   1617  1.1  thorpej 
   1618  1.1  thorpej 	miirom |= MIIROM_RD;
   1619  1.1  thorpej 	SROM_EMIT(sc, miirom);
   1620  1.1  thorpej 
   1621  1.1  thorpej 	miirom |= MIIROM_SROMCS;
   1622  1.1  thorpej 	SROM_EMIT(sc, miirom);
   1623  1.1  thorpej 
   1624  1.1  thorpej 	SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1625  1.1  thorpej 
   1626  1.1  thorpej 	/* Strobe the clock 25 times. */
   1627  1.1  thorpej 	for (i = 0; i < 25; i++) {
   1628  1.1  thorpej 		SROM_EMIT(sc, miirom);
   1629  1.1  thorpej 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1630  1.1  thorpej 	}
   1631  1.1  thorpej 
   1632  1.1  thorpej 	SROM_EMIT(sc, miirom);
   1633  1.1  thorpej 
   1634  1.1  thorpej 	miirom &= ~MIIROM_SROMCS;
   1635  1.1  thorpej 	SROM_EMIT(sc, miirom);
   1636  1.1  thorpej 
   1637  1.1  thorpej 	SROM_EMIT(sc, 0);
   1638  1.1  thorpej }
   1639  1.1  thorpej 
   1640  1.1  thorpej /*
   1641  1.1  thorpej  * tlp_read_srom:
   1642  1.1  thorpej  *
   1643  1.1  thorpej  *	Read the Tulip SROM.
   1644  1.1  thorpej  */
   1645  1.1  thorpej void
   1646  1.1  thorpej tlp_read_srom(sc, word, wordcnt, data)
   1647  1.1  thorpej 	struct tulip_softc *sc;
   1648  1.1  thorpej 	int word, wordcnt;
   1649  1.1  thorpej 	u_int16_t *data;
   1650  1.1  thorpej {
   1651  1.1  thorpej 	u_int32_t miirom;
   1652  1.1  thorpej 	int i, x;
   1653  1.1  thorpej 
   1654  1.1  thorpej 	tlp_srom_idle(sc);
   1655  1.1  thorpej 
   1656  1.1  thorpej 	/* Select the SROM. */
   1657  1.1  thorpej 	miirom = MIIROM_SR;
   1658  1.1  thorpej 	SROM_EMIT(sc, miirom);
   1659  1.1  thorpej 
   1660  1.1  thorpej 	miirom |= MIIROM_RD;
   1661  1.1  thorpej 	SROM_EMIT(sc, miirom);
   1662  1.1  thorpej 
   1663  1.1  thorpej 	for (i = 0; i < wordcnt; i++) {
   1664  1.1  thorpej 		/* Send CHIP SELECT for one clock tick. */
   1665  1.1  thorpej 		miirom |= MIIROM_SROMCS;
   1666  1.1  thorpej 		SROM_EMIT(sc, miirom);
   1667  1.1  thorpej 
   1668  1.1  thorpej 		/* Shift in the READ opcode. */
   1669  1.1  thorpej 		for (x = 3; x > 0; x--) {
   1670  1.1  thorpej 			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
   1671  1.1  thorpej 				miirom |= MIIROM_SROMDI;
   1672  1.1  thorpej 			else
   1673  1.1  thorpej 				miirom &= ~MIIROM_SROMDI;
   1674  1.1  thorpej 			SROM_EMIT(sc, miirom);
   1675  1.1  thorpej 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1676  1.1  thorpej 			SROM_EMIT(sc, miirom);
   1677  1.1  thorpej 		}
   1678  1.1  thorpej 
   1679  1.1  thorpej 		/* Shift in address. */
   1680  1.1  thorpej 		for (x = 6; x > 0; x--) {
   1681  1.1  thorpej 			if ((word + i) & (1 << (x - 1)))
   1682  1.1  thorpej 				miirom |= MIIROM_SROMDI;
   1683  1.1  thorpej 			else
   1684  1.1  thorpej 				miirom &= ~MIIROM_SROMDI;
   1685  1.1  thorpej 			SROM_EMIT(sc, miirom);
   1686  1.1  thorpej 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1687  1.1  thorpej 			SROM_EMIT(sc, miirom);
   1688  1.1  thorpej 		}
   1689  1.1  thorpej 
   1690  1.1  thorpej 		/* Shift out data. */
   1691  1.1  thorpej 		miirom &= ~MIIROM_SROMDI;
   1692  1.1  thorpej 		data[i] = 0;
   1693  1.1  thorpej 		for (x = 16; x > 0; x--) {
   1694  1.1  thorpej 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1695  1.1  thorpej 			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
   1696  1.1  thorpej 				data[i] |= (1 << (x - 1));
   1697  1.1  thorpej 			SROM_EMIT(sc, miirom);
   1698  1.1  thorpej 		}
   1699  1.1  thorpej 
   1700  1.1  thorpej 		/* Clear CHIP SELECT. */
   1701  1.1  thorpej 		miirom &= ~MIIROM_SROMCS;
   1702  1.1  thorpej 		SROM_EMIT(sc, miirom);
   1703  1.1  thorpej 	}
   1704  1.1  thorpej 
   1705  1.1  thorpej 	/* Deselect the SROM. */
   1706  1.1  thorpej 	SROM_EMIT(sc, 0);
   1707  1.1  thorpej 
   1708  1.1  thorpej 	/* ...and idle it. */
   1709  1.1  thorpej 	tlp_srom_idle(sc);
   1710  1.1  thorpej }
   1711  1.1  thorpej 
   1712  1.1  thorpej #undef SROM_EMIT
   1713  1.1  thorpej 
   1714  1.1  thorpej /*
   1715  1.1  thorpej  * tlp_add_rxbuf:
   1716  1.1  thorpej  *
   1717  1.1  thorpej  *	Add a receive buffer to the indicated descriptor.
   1718  1.1  thorpej  */
   1719  1.1  thorpej int
   1720  1.1  thorpej tlp_add_rxbuf(sc, idx)
   1721  1.1  thorpej 	struct tulip_softc *sc;
   1722  1.1  thorpej 	int idx;
   1723  1.1  thorpej {
   1724  1.1  thorpej 	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
   1725  1.1  thorpej 	struct mbuf *m;
   1726  1.1  thorpej 	int error;
   1727  1.1  thorpej 
   1728  1.1  thorpej 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1729  1.1  thorpej 	if (m == NULL)
   1730  1.1  thorpej 		return (ENOBUFS);
   1731  1.1  thorpej 
   1732  1.1  thorpej 	MCLGET(m, M_DONTWAIT);
   1733  1.1  thorpej 	if ((m->m_flags & M_EXT) == 0) {
   1734  1.1  thorpej 		m_freem(m);
   1735  1.1  thorpej 		return (ENOBUFS);
   1736  1.1  thorpej 	}
   1737  1.1  thorpej 
   1738  1.1  thorpej 	if (rxs->rxs_mbuf != NULL)
   1739  1.1  thorpej 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1740  1.1  thorpej 
   1741  1.1  thorpej 	rxs->rxs_mbuf = m;
   1742  1.1  thorpej 
   1743  1.1  thorpej 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   1744  1.1  thorpej 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   1745  1.1  thorpej 	if (error) {
   1746  1.1  thorpej 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1747  1.1  thorpej 		    sc->sc_dev.dv_xname, idx, error);
   1748  1.1  thorpej 		panic("tlp_add_rxbuf");	/* XXX */
   1749  1.1  thorpej 	}
   1750  1.1  thorpej 
   1751  1.1  thorpej 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1752  1.1  thorpej 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1753  1.1  thorpej 
   1754  1.1  thorpej 	TULIP_INIT_RXDESC(sc, idx);
   1755  1.1  thorpej 
   1756  1.1  thorpej 	return (0);
   1757  1.1  thorpej }
   1758  1.1  thorpej 
   1759  1.1  thorpej /*
   1760  1.1  thorpej  * tlp_crc32:
   1761  1.1  thorpej  *
   1762  1.1  thorpej  *	Compute the 32-bit CRC of the provided buffer.
   1763  1.1  thorpej  */
   1764  1.1  thorpej u_int32_t
   1765  1.1  thorpej tlp_crc32(buf, len)
   1766  1.1  thorpej 	const u_int8_t *buf;
   1767  1.1  thorpej 	size_t len;
   1768  1.1  thorpej {
   1769  1.1  thorpej 	static const u_int32_t crctab[] = {
   1770  1.1  thorpej 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1771  1.1  thorpej 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1772  1.1  thorpej 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1773  1.1  thorpej 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1774  1.1  thorpej 	};
   1775  1.1  thorpej 	u_int32_t crc;
   1776  1.1  thorpej 	int i;
   1777  1.1  thorpej 
   1778  1.1  thorpej 	crc = 0xffffffff;
   1779  1.1  thorpej 	for (i = 0; i < len; i++) {
   1780  1.1  thorpej 		crc ^= buf[i];
   1781  1.1  thorpej 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1782  1.1  thorpej 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1783  1.1  thorpej 	}
   1784  1.1  thorpej 	return (crc);
   1785  1.1  thorpej }
   1786  1.1  thorpej 
   1787  1.1  thorpej /*
   1788  1.1  thorpej  * tlp_srom_crcok:
   1789  1.1  thorpej  *
   1790  1.1  thorpej  *	Check the CRC of the Tulip SROM.
   1791  1.1  thorpej  */
   1792  1.1  thorpej int
   1793  1.1  thorpej tlp_srom_crcok(romdata)
   1794  1.1  thorpej 	u_int8_t *romdata;
   1795  1.1  thorpej {
   1796  1.1  thorpej 	u_int32_t crc;
   1797  1.1  thorpej 
   1798  1.1  thorpej 	crc = tlp_crc32(romdata, 126);
   1799  1.1  thorpej 	if ((crc ^ 0xffff) == (romdata[126] | (romdata[127] << 8)))
   1800  1.1  thorpej 		return (1);
   1801  1.1  thorpej 	return (0);
   1802  1.1  thorpej }
   1803  1.1  thorpej 
   1804  1.1  thorpej /*
   1805  1.1  thorpej  * tlp_filter_setup:
   1806  1.1  thorpej  *
   1807  1.1  thorpej  *	Set the Tulip's receive filter.
   1808  1.1  thorpej  */
   1809  1.1  thorpej void
   1810  1.1  thorpej tlp_filter_setup(sc)
   1811  1.1  thorpej 	struct tulip_softc *sc;
   1812  1.1  thorpej {
   1813  1.1  thorpej 	struct ethercom *ec = &sc->sc_ethercom;
   1814  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1815  1.1  thorpej 	struct ether_multi *enm;
   1816  1.1  thorpej 	struct ether_multistep step;
   1817  1.1  thorpej 	__volatile u_int32_t *sp;
   1818  1.1  thorpej 	u_int8_t enaddr[ETHER_ADDR_LEN];
   1819  1.1  thorpej 	u_int32_t hash;
   1820  1.1  thorpej 	int cnt;
   1821  1.1  thorpej 
   1822  1.5  thorpej 	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
   1823  1.1  thorpej 	    sc->sc_dev.dv_xname, sc->sc_flags));
   1824  1.1  thorpej 
   1825  1.1  thorpej 	memcpy(enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1826  1.1  thorpej 
   1827  1.1  thorpej 	/*
   1828  1.1  thorpej 	 * If there are transmissions pending, wait until they have
   1829  1.1  thorpej 	 * completed.
   1830  1.1  thorpej 	 */
   1831  1.4  thorpej 	if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL ||
   1832  1.4  thorpej 	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
   1833  1.1  thorpej 		sc->sc_flags |= TULIPF_WANT_SETUP;
   1834  1.5  thorpej 		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
   1835  1.1  thorpej 		    sc->sc_dev.dv_xname));
   1836  1.1  thorpej 		return;
   1837  1.1  thorpej 	}
   1838  1.1  thorpej 	sc->sc_flags &= ~TULIPF_WANT_SETUP;
   1839  1.1  thorpej 
   1840  1.1  thorpej 	/*
   1841  1.1  thorpej 	 * If we're running, idle the transmit and receive engines.  If
   1842  1.1  thorpej 	 * we're NOT running, we're being called from tlp_init(), and our
   1843  1.1  thorpej 	 * writing OPMODE will start the transmit and receive processes
   1844  1.1  thorpej 	 * in motion.
   1845  1.1  thorpej 	 */
   1846  1.1  thorpej 	if (ifp->if_flags & IFF_RUNNING)
   1847  1.1  thorpej 		tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   1848  1.1  thorpej 
   1849  1.1  thorpej 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   1850  1.1  thorpej 
   1851  1.1  thorpej 	if (ifp->if_flags & IFF_PROMISC) {
   1852  1.1  thorpej 		sc->sc_opmode |= OPMODE_PR;
   1853  1.1  thorpej 		goto allmulti;
   1854  1.1  thorpej 	}
   1855  1.1  thorpej 
   1856  1.1  thorpej 	/*
   1857  1.1  thorpej 	 * Try Perfect filtering first.
   1858  1.1  thorpej 	 */
   1859  1.1  thorpej 
   1860  1.1  thorpej 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   1861  1.1  thorpej 	sp = TULIP_CDSP(sc);
   1862  1.1  thorpej 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   1863  1.1  thorpej 	cnt = 0;
   1864  1.1  thorpej 	ETHER_FIRST_MULTI(step, ec, enm);
   1865  1.1  thorpej 	while (enm != NULL) {
   1866  1.1  thorpej 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1867  1.1  thorpej 			/*
   1868  1.1  thorpej 			 * We must listen to a range of multicast addresses.
   1869  1.1  thorpej 			 * For now, just accept all multicasts, rather than
   1870  1.1  thorpej 			 * trying to set only those filter bits needed to match
   1871  1.1  thorpej 			 * the range.  (At this time, the only use of address
   1872  1.1  thorpej 			 * ranges is for IP multicast routing, for which the
   1873  1.1  thorpej 			 * range is big enough to require all bits set.)
   1874  1.1  thorpej 			 */
   1875  1.1  thorpej 			goto allmulti;
   1876  1.1  thorpej 		}
   1877  1.1  thorpej 		if (cnt == (TULIP_MAXADDRS - 2)) {
   1878  1.1  thorpej 			/*
   1879  1.1  thorpej 			 * We already have our multicast limit (still need
   1880  1.1  thorpej 			 * our station address and broadcast).  Go to
   1881  1.1  thorpej 			 * Hash-Perfect mode.
   1882  1.1  thorpej 			 */
   1883  1.1  thorpej 			goto hashperfect;
   1884  1.1  thorpej 		}
   1885  1.1  thorpej 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[0];
   1886  1.1  thorpej 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[1];
   1887  1.1  thorpej 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[2];
   1888  1.1  thorpej 		ETHER_NEXT_MULTI(step, enm);
   1889  1.1  thorpej 	}
   1890  1.1  thorpej 
   1891  1.1  thorpej 	if (ifp->if_flags & IFF_BROADCAST) {
   1892  1.1  thorpej 		/* ...and the broadcast address. */
   1893  1.1  thorpej 		cnt++;
   1894  1.1  thorpej 		*sp++ = 0xffff;
   1895  1.1  thorpej 		*sp++ = 0xffff;
   1896  1.1  thorpej 		*sp++ = 0xffff;
   1897  1.1  thorpej 	}
   1898  1.1  thorpej 
   1899  1.1  thorpej 	/* Pad the rest with our station address. */
   1900  1.1  thorpej 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   1901  1.1  thorpej 		*sp++ = ((u_int16_t *) enaddr)[0];
   1902  1.1  thorpej 		*sp++ = ((u_int16_t *) enaddr)[1];
   1903  1.1  thorpej 		*sp++ = ((u_int16_t *) enaddr)[2];
   1904  1.1  thorpej 	}
   1905  1.1  thorpej 	ifp->if_flags &= ~IFF_ALLMULTI;
   1906  1.1  thorpej 	goto setit;
   1907  1.1  thorpej 
   1908  1.1  thorpej  hashperfect:
   1909  1.1  thorpej 	/*
   1910  1.1  thorpej 	 * Try Hash-Perfect mode.
   1911  1.1  thorpej 	 */
   1912  1.1  thorpej 
   1913  1.1  thorpej 	/*
   1914  1.1  thorpej 	 * Some 21140 chips have broken Hash-Perfect modes.  On these
   1915  1.1  thorpej 	 * chips, we simply use Hash-Only mode, and put our station
   1916  1.1  thorpej 	 * address into the filter.
   1917  1.1  thorpej 	 */
   1918  1.1  thorpej 	if (sc->sc_chip == TULIP_CHIP_21140)
   1919  1.1  thorpej 		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
   1920  1.1  thorpej 	else
   1921  1.1  thorpej 		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
   1922  1.1  thorpej 	sp = TULIP_CDSP(sc);
   1923  1.1  thorpej 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   1924  1.1  thorpej 	ETHER_FIRST_MULTI(step, ec, enm);
   1925  1.1  thorpej 	while (enm != NULL) {
   1926  1.1  thorpej 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1927  1.1  thorpej 			/*
   1928  1.1  thorpej 			 * We must listen to a range of multicast addresses.
   1929  1.1  thorpej 			 * For now, just accept all multicasts, rather than
   1930  1.1  thorpej 			 * trying to set only those filter bits needed to match
   1931  1.1  thorpej 			 * the range.  (At this time, the only use of address
   1932  1.1  thorpej 			 * ranges is for IP multicast routing, for which the
   1933  1.1  thorpej 			 * range is big enough to require all bits set.)
   1934  1.1  thorpej 			 */
   1935  1.1  thorpej 			goto allmulti;
   1936  1.1  thorpej 		}
   1937  1.1  thorpej 		hash = tlp_mchash(enm->enm_addrlo);
   1938  1.1  thorpej 		sp[hash >> 4] |= 1 << (hash & 0xf);
   1939  1.1  thorpej 		ETHER_NEXT_MULTI(step, enm);
   1940  1.1  thorpej 	}
   1941  1.1  thorpej 
   1942  1.1  thorpej 	if (ifp->if_flags & IFF_BROADCAST) {
   1943  1.1  thorpej 		/* ...and the broadcast address. */
   1944  1.1  thorpej 		hash = tlp_mchash(etherbroadcastaddr);
   1945  1.1  thorpej 		sp[hash >> 4] |= 1 << (hash & 0xf);
   1946  1.1  thorpej 	}
   1947  1.1  thorpej 
   1948  1.1  thorpej 	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   1949  1.1  thorpej 		/* ...and our station address. */
   1950  1.1  thorpej 		hash = tlp_mchash(enaddr);
   1951  1.1  thorpej 		sp[hash >> 4] |= 1 << (hash & 0xf);
   1952  1.1  thorpej 	} else {
   1953  1.1  thorpej 		/*
   1954  1.1  thorpej 		 * Hash-Perfect mode; put our station address after
   1955  1.1  thorpej 		 * the hash table.
   1956  1.1  thorpej 		 */
   1957  1.1  thorpej 		sp[39] = ((u_int16_t *) enaddr)[0];
   1958  1.1  thorpej 		sp[40] = ((u_int16_t *) enaddr)[1];
   1959  1.1  thorpej 		sp[41] = ((u_int16_t *) enaddr)[2];
   1960  1.1  thorpej 	}
   1961  1.1  thorpej 	ifp->if_flags &= ~IFF_ALLMULTI;
   1962  1.1  thorpej 	goto setit;
   1963  1.1  thorpej 
   1964  1.1  thorpej  allmulti:
   1965  1.1  thorpej 	/*
   1966  1.1  thorpej 	 * Use Perfect filter mode.  First address is the broadcast address,
   1967  1.1  thorpej 	 * and pad the rest with our station address.  We'll set Pass-all-
   1968  1.1  thorpej 	 * multicast in OPMODE below.
   1969  1.1  thorpej 	 */
   1970  1.1  thorpej 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   1971  1.1  thorpej 	sp = TULIP_CDSP(sc);
   1972  1.1  thorpej 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   1973  1.1  thorpej 	cnt = 0;
   1974  1.1  thorpej 	if (ifp->if_flags & IFF_BROADCAST) {
   1975  1.1  thorpej 		cnt++;
   1976  1.1  thorpej 		*sp++ = 0xffff;
   1977  1.1  thorpej 		*sp++ = 0xffff;
   1978  1.1  thorpej 		*sp++ = 0xffff;
   1979  1.1  thorpej 	}
   1980  1.1  thorpej 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   1981  1.1  thorpej 		*sp++ = ((u_int16_t *) enaddr)[0];
   1982  1.1  thorpej 		*sp++ = ((u_int16_t *) enaddr)[1];
   1983  1.1  thorpej 		*sp++ = ((u_int16_t *) enaddr)[2];
   1984  1.1  thorpej 	}
   1985  1.1  thorpej 	ifp->if_flags |= IFF_ALLMULTI;
   1986  1.1  thorpej 
   1987  1.1  thorpej  setit:
   1988  1.1  thorpej 	if (ifp->if_flags & IFF_ALLMULTI)
   1989  1.1  thorpej 		sc->sc_opmode |= OPMODE_PM;
   1990  1.1  thorpej 
   1991  1.1  thorpej 	/* Sync the setup packet buffer. */
   1992  1.1  thorpej 	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
   1993  1.1  thorpej 
   1994  1.1  thorpej 	/*
   1995  1.1  thorpej 	 * Fill in the setup packet descriptor.
   1996  1.1  thorpej 	 */
   1997  1.1  thorpej 	sc->sc_setup_desc.td_bufaddr1 = TULIP_CDSPADDR(sc);
   1998  1.1  thorpej 	sc->sc_setup_desc.td_bufaddr2 = TULIP_CDTXADDR(sc, sc->sc_txnext);
   1999  1.1  thorpej 	sc->sc_setup_desc.td_ctl =
   2000  1.1  thorpej 	    (TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
   2001  1.1  thorpej 	    sc->sc_filtmode | TDCTL_Tx_SET | TDCTL_Tx_FS | TDCTL_Tx_LS |
   2002  1.4  thorpej 	    TDCTL_Tx_IC | TDCTL_CH;
   2003  1.1  thorpej 	sc->sc_setup_desc.td_status = TDSTAT_OWN;
   2004  1.1  thorpej 	TULIP_CDSDSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2005  1.1  thorpej 
   2006  1.1  thorpej 	/*
   2007  1.1  thorpej 	 * Write the address of the setup descriptor.  This also has
   2008  1.1  thorpej 	 * the side effect of giving the transmit ring to the chip,
   2009  1.1  thorpej 	 * since the setup descriptor points to the next available
   2010  1.1  thorpej 	 * descriptor in the ring.
   2011  1.1  thorpej 	 */
   2012  1.1  thorpej 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDSDADDR(sc));
   2013  1.1  thorpej 
   2014  1.1  thorpej 	/*
   2015  1.1  thorpej 	 * Set the OPMODE register.  This will also resume the
   2016  1.1  thorpej 	 * transmit transmit process we idled above.
   2017  1.1  thorpej 	 */
   2018  1.1  thorpej 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2019  1.1  thorpej 
   2020  1.4  thorpej 	sc->sc_flags |= TULIPF_DOING_SETUP;
   2021  1.4  thorpej 
   2022  1.1  thorpej 	/*
   2023  1.1  thorpej 	 * Kick the transmitter; this will cause the Tulip to
   2024  1.1  thorpej 	 * read the setup descriptor.
   2025  1.1  thorpej 	 */
   2026  1.1  thorpej 	/* XXX USE AUTOPOLLING? */
   2027  1.1  thorpej 	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
   2028  1.1  thorpej 
   2029  1.4  thorpej 	/* Set up a watchdog timer in case the chip flakes out. */
   2030  1.4  thorpej 	ifp->if_timer = 5;
   2031  1.4  thorpej 
   2032  1.5  thorpej 	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
   2033  1.1  thorpej }
   2034  1.1  thorpej 
   2035  1.1  thorpej /*
   2036  1.1  thorpej  * tlp_winb_filter_setup:
   2037  1.1  thorpej  *
   2038  1.1  thorpej  *	Set the Winbond 89C840F's receive filter.
   2039  1.1  thorpej  */
   2040  1.1  thorpej void
   2041  1.1  thorpej tlp_winb_filter_setup(sc)
   2042  1.1  thorpej 	struct tulip_softc *sc;
   2043  1.1  thorpej {
   2044  1.1  thorpej 	struct ethercom *ec = &sc->sc_ethercom;
   2045  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2046  1.1  thorpej 	struct ether_multi *enm;
   2047  1.1  thorpej 	struct ether_multistep step;
   2048  1.1  thorpej 	u_int32_t hash, mchash[2];
   2049  1.1  thorpej 
   2050  1.5  thorpej 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
   2051  1.1  thorpej 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2052  1.1  thorpej 
   2053  1.5  thorpej 	sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
   2054  1.5  thorpej 
   2055  1.5  thorpej 	if (ifp->if_flags & IFF_MULTICAST)
   2056  1.5  thorpej 		sc->sc_opmode |= OPMODE_WINB_AMP;
   2057  1.5  thorpej 
   2058  1.5  thorpej 	if (ifp->if_flags & IFF_BROADCAST)
   2059  1.5  thorpej 		sc->sc_opmode |= OPMODE_WINB_ABP;
   2060  1.1  thorpej 
   2061  1.1  thorpej 	if (ifp->if_flags & IFF_PROMISC) {
   2062  1.5  thorpej 		sc->sc_opmode |= OPMODE_WINB_APP;
   2063  1.1  thorpej 		goto allmulti;
   2064  1.1  thorpej 	}
   2065  1.1  thorpej 
   2066  1.1  thorpej 	mchash[0] = mchash[1] = 0;
   2067  1.1  thorpej 
   2068  1.1  thorpej 	ETHER_FIRST_MULTI(step, ec, enm);
   2069  1.1  thorpej 	while (enm != NULL) {
   2070  1.1  thorpej 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2071  1.1  thorpej 			/*
   2072  1.1  thorpej 			 * We must listen to a range of multicast addresses.
   2073  1.1  thorpej 			 * For now, just accept all multicasts, rather than
   2074  1.1  thorpej 			 * trying to set only those filter bits needed to match
   2075  1.1  thorpej 			 * the range.  (At this time, the only use of address
   2076  1.1  thorpej 			 * ranges is for IP multicast routing, for which the
   2077  1.1  thorpej 			 * range is big enough to require all bits set.)
   2078  1.1  thorpej 			 */
   2079  1.1  thorpej 			goto allmulti;
   2080  1.1  thorpej 		}
   2081  1.1  thorpej 
   2082  1.1  thorpej 		/*
   2083  1.1  thorpej 		 * According to the FreeBSD `wb' driver, yes, you
   2084  1.1  thorpej 		 * really do invert the hash.
   2085  1.1  thorpej 		 */
   2086  1.1  thorpej 		hash = (~(tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
   2087  1.1  thorpej 		    & 0x3f;
   2088  1.1  thorpej 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2089  1.1  thorpej 		ETHER_NEXT_MULTI(step, enm);
   2090  1.1  thorpej 	}
   2091  1.1  thorpej 	ifp->if_flags &= ~IFF_ALLMULTI;
   2092  1.1  thorpej 	goto setit;
   2093  1.1  thorpej 
   2094  1.1  thorpej  allmulti:
   2095  1.1  thorpej 	ifp->if_flags |= IFF_ALLMULTI;
   2096  1.1  thorpej 	mchash[0] = mchash[1] = 0xffffffff;
   2097  1.1  thorpej 
   2098  1.1  thorpej  setit:
   2099  1.5  thorpej 	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
   2100  1.5  thorpej 	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
   2101  1.1  thorpej 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2102  1.5  thorpej 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
   2103  1.1  thorpej 	    sc->sc_dev.dv_xname));
   2104  1.1  thorpej }
   2105  1.1  thorpej 
   2106  1.1  thorpej /*
   2107  1.1  thorpej  * tlp_idle:
   2108  1.1  thorpej  *
   2109  1.1  thorpej  *	Cause the transmit and/or receive processes to go idle.
   2110  1.1  thorpej  */
   2111  1.1  thorpej void
   2112  1.1  thorpej tlp_idle(sc, bits)
   2113  1.1  thorpej 	struct tulip_softc *sc;
   2114  1.1  thorpej 	u_int32_t bits;
   2115  1.1  thorpej {
   2116  1.1  thorpej 	static const char *tx_state_names[] = {
   2117  1.1  thorpej 		"STOPPED",
   2118  1.1  thorpej 		"RUNNING - FETCH",
   2119  1.1  thorpej 		"RUNNING - WAIT",
   2120  1.1  thorpej 		"RUNNING - READING",
   2121  1.1  thorpej 		"-- RESERVED --",
   2122  1.1  thorpej 		"RUNNING - SETUP",
   2123  1.1  thorpej 		"SUSPENDED",
   2124  1.1  thorpej 		"RUNNING - CLOSE",
   2125  1.1  thorpej 	};
   2126  1.1  thorpej 	static const char *rx_state_names[] = {
   2127  1.1  thorpej 		"STOPPED",
   2128  1.1  thorpej 		"RUNNING - FETCH",
   2129  1.1  thorpej 		"RUNNING - CHECK",
   2130  1.1  thorpej 		"RUNNING - WAIT",
   2131  1.1  thorpej 		"SUSPENDED",
   2132  1.1  thorpej 		"RUNNING - CLOSE",
   2133  1.1  thorpej 		"RUNNING - FLUSH",
   2134  1.1  thorpej 		"RUNNING - QUEUE",
   2135  1.1  thorpej 	};
   2136  1.1  thorpej 	u_int32_t csr, ackmask = 0;
   2137  1.1  thorpej 	int i;
   2138  1.1  thorpej 
   2139  1.1  thorpej 	if (bits & OPMODE_ST)
   2140  1.1  thorpej 		ackmask |= STATUS_TPS;
   2141  1.1  thorpej 
   2142  1.1  thorpej 	if (bits & OPMODE_SR)
   2143  1.1  thorpej 		ackmask |= STATUS_RPS;
   2144  1.1  thorpej 
   2145  1.1  thorpej 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
   2146  1.1  thorpej 
   2147  1.1  thorpej 	for (i = 0; i < 1000; i++) {
   2148  1.1  thorpej 		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
   2149  1.1  thorpej 			break;
   2150  1.1  thorpej 		delay(10);
   2151  1.1  thorpej 	}
   2152  1.1  thorpej 
   2153  1.1  thorpej 	csr = TULIP_READ(sc, CSR_STATUS);
   2154  1.1  thorpej 	if ((csr & ackmask) != ackmask) {
   2155  1.1  thorpej 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
   2156  1.1  thorpej 		    (csr & STATUS_TS) != STATUS_TS_STOPPED)
   2157  1.1  thorpej 			printf("%s: transmit process failed to idle: "
   2158  1.1  thorpej 			    "state %s\n", sc->sc_dev.dv_xname,
   2159  1.1  thorpej 			    tx_state_names[(csr & STATUS_TS) >> 20]);
   2160  1.1  thorpej 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
   2161  1.1  thorpej 		    (csr & STATUS_RS) != STATUS_RS_STOPPED)
   2162  1.1  thorpej 			printf("%s: receive process failed to idle: "
   2163  1.1  thorpej 			    "state %s\n", sc->sc_dev.dv_xname,
   2164  1.1  thorpej 			    rx_state_names[(csr & STATUS_RS) >> 17]);
   2165  1.1  thorpej 	}
   2166  1.1  thorpej 	TULIP_WRITE(sc, CSR_STATUS, ackmask);
   2167  1.1  thorpej }
   2168  1.1  thorpej 
   2169  1.1  thorpej /*****************************************************************************
   2170  1.1  thorpej  * Generic media support functions.
   2171  1.1  thorpej  *****************************************************************************/
   2172  1.1  thorpej 
   2173  1.1  thorpej /*
   2174  1.1  thorpej  * tlp_mediastatus:	[ifmedia interface function]
   2175  1.1  thorpej  *
   2176  1.1  thorpej  *	Query the current media.
   2177  1.1  thorpej  */
   2178  1.1  thorpej void
   2179  1.1  thorpej tlp_mediastatus(ifp, ifmr)
   2180  1.1  thorpej 	struct ifnet *ifp;
   2181  1.1  thorpej 	struct ifmediareq *ifmr;
   2182  1.1  thorpej {
   2183  1.1  thorpej 	struct tulip_softc *sc = ifp->if_softc;
   2184  1.1  thorpej 
   2185  1.1  thorpej 	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
   2186  1.1  thorpej }
   2187  1.1  thorpej 
   2188  1.1  thorpej /*
   2189  1.1  thorpej  * tlp_mediachange:	[ifmedia interface function]
   2190  1.1  thorpej  *
   2191  1.1  thorpej  *	Update the current media.
   2192  1.1  thorpej  */
   2193  1.1  thorpej int
   2194  1.1  thorpej tlp_mediachange(ifp)
   2195  1.1  thorpej 	struct ifnet *ifp;
   2196  1.1  thorpej {
   2197  1.1  thorpej 	struct tulip_softc *sc = ifp->if_softc;
   2198  1.1  thorpej 
   2199  1.1  thorpej 	return ((*sc->sc_mediasw->tmsw_set)(sc));
   2200  1.1  thorpej }
   2201  1.1  thorpej 
   2202  1.1  thorpej /*****************************************************************************
   2203  1.1  thorpej  * Support functions for MII-attached media.
   2204  1.1  thorpej  *****************************************************************************/
   2205  1.1  thorpej 
   2206  1.1  thorpej /*
   2207  1.1  thorpej  * tlp_mii_tick:
   2208  1.1  thorpej  *
   2209  1.1  thorpej  *	One second timer, used to tick the MII.
   2210  1.1  thorpej  */
   2211  1.1  thorpej void
   2212  1.1  thorpej tlp_mii_tick(arg)
   2213  1.1  thorpej 	void *arg;
   2214  1.1  thorpej {
   2215  1.1  thorpej 	struct tulip_softc *sc = arg;
   2216  1.1  thorpej 	int s;
   2217  1.1  thorpej 
   2218  1.1  thorpej 	s = splnet();
   2219  1.1  thorpej 	mii_tick(&sc->sc_mii);
   2220  1.1  thorpej 	splx(s);
   2221  1.1  thorpej 
   2222  1.6  thorpej 	timeout(sc->sc_tick, sc, hz);
   2223  1.1  thorpej }
   2224  1.1  thorpej 
   2225  1.1  thorpej /*
   2226  1.1  thorpej  * tlp_mii_statchg:	[mii interface function]
   2227  1.1  thorpej  *
   2228  1.1  thorpej  *	Callback from PHY when media changes.
   2229  1.1  thorpej  */
   2230  1.1  thorpej void
   2231  1.1  thorpej tlp_mii_statchg(self)
   2232  1.1  thorpej 	struct device *self;
   2233  1.1  thorpej {
   2234  1.1  thorpej 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2235  1.1  thorpej 
   2236  1.1  thorpej 	/* Idle the transmit and receive processes. */
   2237  1.1  thorpej 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2238  1.1  thorpej 
   2239  1.1  thorpej 	/*
   2240  1.1  thorpej 	 * XXX What about Heartbeat Disable?  Is it magically frobbed
   2241  1.1  thorpej 	 * XXX by the PHY?  I hope so...
   2242  1.1  thorpej 	 */
   2243  1.1  thorpej 
   2244  1.1  thorpej 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
   2245  1.1  thorpej 		sc->sc_opmode |= OPMODE_TTM;
   2246  1.1  thorpej 	else
   2247  1.1  thorpej 		sc->sc_opmode &= ~OPMODE_TTM;
   2248  1.1  thorpej 
   2249  1.1  thorpej 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2250  1.1  thorpej 		sc->sc_opmode |= OPMODE_FD;
   2251  1.1  thorpej 	else
   2252  1.1  thorpej 		sc->sc_opmode &= ~OPMODE_FD;
   2253  1.1  thorpej 
   2254  1.1  thorpej 	/*
   2255  1.1  thorpej 	 * Write new OPMODE bits.  This also restarts the transmit
   2256  1.1  thorpej 	 * and receive processes.
   2257  1.1  thorpej 	 */
   2258  1.1  thorpej 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2259  1.1  thorpej 
   2260  1.1  thorpej 	/* XXX Update ifp->if_baudrate */
   2261  1.1  thorpej }
   2262  1.1  thorpej 
   2263  1.1  thorpej /*
   2264  1.5  thorpej  * tlp_winb_mii_statchg: [mii interface function]
   2265  1.5  thorpej  *
   2266  1.5  thorpej  *	Callback from PHY when media changes.  This version is
   2267  1.5  thorpej  *	for the Winbond 89C840F, which has different OPMODE bits.
   2268  1.5  thorpej  */
   2269  1.5  thorpej void
   2270  1.5  thorpej tlp_winb_mii_statchg(self)
   2271  1.5  thorpej 	struct device *self;
   2272  1.5  thorpej {
   2273  1.5  thorpej 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2274  1.5  thorpej 
   2275  1.5  thorpej 	/* Idle the transmit and receive processes. */
   2276  1.5  thorpej 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2277  1.5  thorpej 
   2278  1.5  thorpej 	/*
   2279  1.5  thorpej 	 * XXX What about Heartbeat Disable?  Is it magically frobbed
   2280  1.5  thorpej 	 * XXX by the PHY?  I hope so...
   2281  1.5  thorpej 	 */
   2282  1.5  thorpej 
   2283  1.5  thorpej 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
   2284  1.5  thorpej 		sc->sc_opmode |= OPMODE_WINB_FES;
   2285  1.5  thorpej 	else
   2286  1.5  thorpej 		sc->sc_opmode &= ~OPMODE_WINB_FES;
   2287  1.5  thorpej 
   2288  1.5  thorpej 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2289  1.5  thorpej 		sc->sc_opmode |= OPMODE_FD;
   2290  1.5  thorpej 	else
   2291  1.5  thorpej 		sc->sc_opmode &= ~OPMODE_FD;
   2292  1.5  thorpej 
   2293  1.5  thorpej 	/*
   2294  1.5  thorpej 	 * Write new OPMODE bits.  This also restarts the transmit
   2295  1.5  thorpej 	 * and receive processes.
   2296  1.5  thorpej 	 */
   2297  1.5  thorpej 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2298  1.5  thorpej 
   2299  1.5  thorpej 	/* XXX Update ifp->if_baudrate */
   2300  1.5  thorpej }
   2301  1.5  thorpej 
   2302  1.5  thorpej /*
   2303  1.1  thorpej  * tlp_mii_getmedia:
   2304  1.1  thorpej  *
   2305  1.1  thorpej  *	Callback from ifmedia to request current media status.
   2306  1.1  thorpej  */
   2307  1.1  thorpej void
   2308  1.1  thorpej tlp_mii_getmedia(sc, ifmr)
   2309  1.1  thorpej 	struct tulip_softc *sc;
   2310  1.1  thorpej 	struct ifmediareq *ifmr;
   2311  1.1  thorpej {
   2312  1.1  thorpej 
   2313  1.1  thorpej 	mii_pollstat(&sc->sc_mii);
   2314  1.1  thorpej 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   2315  1.1  thorpej 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   2316  1.1  thorpej }
   2317  1.1  thorpej 
   2318  1.1  thorpej /*
   2319  1.1  thorpej  * tlp_mii_setmedia:
   2320  1.1  thorpej  *
   2321  1.1  thorpej  *	Callback from ifmedia to request new media setting.
   2322  1.1  thorpej  */
   2323  1.1  thorpej int
   2324  1.1  thorpej tlp_mii_setmedia(sc)
   2325  1.1  thorpej 	struct tulip_softc *sc;
   2326  1.1  thorpej {
   2327  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2328  1.1  thorpej 
   2329  1.1  thorpej 	if (ifp->if_flags & IFF_UP)
   2330  1.1  thorpej 		mii_mediachg(&sc->sc_mii);
   2331  1.1  thorpej 	return (0);
   2332  1.1  thorpej }
   2333  1.1  thorpej 
   2334  1.1  thorpej #define	MII_EMIT(sc, x)							\
   2335  1.1  thorpej do {									\
   2336  1.1  thorpej 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
   2337  1.1  thorpej 	delay(1);							\
   2338  1.1  thorpej } while (0)
   2339  1.1  thorpej 
   2340  1.1  thorpej /*
   2341  1.1  thorpej  * tlp_sio_mii_sync:
   2342  1.1  thorpej  *
   2343  1.1  thorpej  *	Synchronize the SIO-attached MII.
   2344  1.1  thorpej  */
   2345  1.1  thorpej void
   2346  1.1  thorpej tlp_sio_mii_sync(sc)
   2347  1.1  thorpej 	struct tulip_softc *sc;
   2348  1.1  thorpej {
   2349  1.1  thorpej 	u_int32_t miirom;
   2350  1.1  thorpej 	int i;
   2351  1.1  thorpej 
   2352  1.1  thorpej 	miirom = MIIROM_MIIDIR|MIIROM_MDO;
   2353  1.1  thorpej 
   2354  1.1  thorpej 	MII_EMIT(sc, miirom);
   2355  1.1  thorpej 	for (i = 0; i < 32; i++) {
   2356  1.1  thorpej 		MII_EMIT(sc, miirom | MIIROM_MDC);
   2357  1.1  thorpej 		MII_EMIT(sc, miirom);
   2358  1.1  thorpej 	}
   2359  1.1  thorpej }
   2360  1.1  thorpej 
   2361  1.1  thorpej /*
   2362  1.1  thorpej  * tlp_sio_mii_sendbits:
   2363  1.1  thorpej  *
   2364  1.1  thorpej  *	Send a series of bits out the SIO to the MII.
   2365  1.1  thorpej  */
   2366  1.1  thorpej void
   2367  1.1  thorpej tlp_sio_mii_sendbits(sc, data, nbits)
   2368  1.1  thorpej 	struct tulip_softc *sc;
   2369  1.1  thorpej 	u_int32_t data;
   2370  1.1  thorpej 	int nbits;
   2371  1.1  thorpej {
   2372  1.1  thorpej 	u_int32_t miirom, i;
   2373  1.1  thorpej 
   2374  1.1  thorpej 	miirom = MIIROM_MIIDIR;
   2375  1.1  thorpej 	MII_EMIT(sc, miirom);
   2376  1.1  thorpej 
   2377  1.1  thorpej 	for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
   2378  1.1  thorpej 		if (data & i)
   2379  1.1  thorpej 			miirom |= MIIROM_MDO;
   2380  1.1  thorpej 		else
   2381  1.1  thorpej 			miirom &= ~MIIROM_MDO;
   2382  1.1  thorpej 		MII_EMIT(sc, miirom);
   2383  1.1  thorpej 		MII_EMIT(sc, miirom|MIIROM_MDC);
   2384  1.1  thorpej 		MII_EMIT(sc, miirom);
   2385  1.1  thorpej 	}
   2386  1.1  thorpej }
   2387  1.1  thorpej 
   2388  1.1  thorpej /*
   2389  1.1  thorpej  * tlp_sio_mii_readreg:
   2390  1.1  thorpej  *
   2391  1.1  thorpej  *	Read a PHY register via SIO-attached MII.
   2392  1.1  thorpej  */
   2393  1.1  thorpej int
   2394  1.1  thorpej tlp_sio_mii_readreg(self, phy, reg)
   2395  1.1  thorpej 	struct device *self;
   2396  1.1  thorpej 	int phy, reg;
   2397  1.1  thorpej {
   2398  1.1  thorpej 	struct tulip_softc *sc = (void *) self;
   2399  1.1  thorpej 	int val = 0, err = 0, i;
   2400  1.1  thorpej 
   2401  1.1  thorpej 	tlp_sio_mii_sync(sc);
   2402  1.1  thorpej 
   2403  1.1  thorpej 	tlp_sio_mii_sendbits(sc, MII_COMMAND_START, 2);
   2404  1.1  thorpej 	tlp_sio_mii_sendbits(sc, MII_COMMAND_READ, 2);
   2405  1.1  thorpej 	tlp_sio_mii_sendbits(sc, phy, 5);
   2406  1.1  thorpej 	tlp_sio_mii_sendbits(sc, reg, 5);
   2407  1.1  thorpej 
   2408  1.1  thorpej 	MII_EMIT(sc, MIIROM_MIIDIR);
   2409  1.1  thorpej 	MII_EMIT(sc, MIIROM_MIIDIR|MIIROM_MDC);
   2410  1.1  thorpej 
   2411  1.1  thorpej 	MII_EMIT(sc, 0);
   2412  1.1  thorpej 	MII_EMIT(sc, MIIROM_MDC);
   2413  1.1  thorpej 
   2414  1.1  thorpej 	err = TULIP_ISSET(sc, CSR_MIIROM, MIIROM_MDI);
   2415  1.1  thorpej 
   2416  1.1  thorpej 	MII_EMIT(sc, 0);
   2417  1.1  thorpej 	MII_EMIT(sc, MIIROM_MDC);
   2418  1.1  thorpej 
   2419  1.1  thorpej 	for (i = 0; i < 16; i++) {
   2420  1.1  thorpej 		val <<= 1;
   2421  1.1  thorpej 		MII_EMIT(sc, 0);
   2422  1.1  thorpej 		if (err == 0 && TULIP_ISSET(sc, CSR_MIIROM, MIIROM_MDI))
   2423  1.1  thorpej 			val |= 1;
   2424  1.1  thorpej 		MII_EMIT(sc, MIIROM_MDC);
   2425  1.1  thorpej 	}
   2426  1.1  thorpej 
   2427  1.1  thorpej 	MII_EMIT(sc, 0);
   2428  1.1  thorpej 
   2429  1.1  thorpej 	return (err ? 0 : val);
   2430  1.1  thorpej }
   2431  1.1  thorpej 
   2432  1.1  thorpej /*
   2433  1.1  thorpej  * tlp_sio_mii_writereg:
   2434  1.1  thorpej  *
   2435  1.1  thorpej  *	Write a PHY register via SIO-attached MII.
   2436  1.1  thorpej  */
   2437  1.1  thorpej void
   2438  1.1  thorpej tlp_sio_mii_writereg(self, phy, reg, val)
   2439  1.1  thorpej 	struct device *self;
   2440  1.1  thorpej 	int phy, reg, val;
   2441  1.1  thorpej {
   2442  1.1  thorpej 	struct tulip_softc *sc = (void *) self;
   2443  1.1  thorpej 
   2444  1.1  thorpej 	tlp_sio_mii_sync(sc);
   2445  1.1  thorpej 
   2446  1.1  thorpej 	tlp_sio_mii_sendbits(sc, MII_COMMAND_START, 2);
   2447  1.1  thorpej 	tlp_sio_mii_sendbits(sc, MII_COMMAND_WRITE, 2);
   2448  1.1  thorpej 	tlp_sio_mii_sendbits(sc, phy, 5);
   2449  1.1  thorpej 	tlp_sio_mii_sendbits(sc, reg, 5);
   2450  1.1  thorpej 	tlp_sio_mii_sendbits(sc, MII_COMMAND_ACK, 2);
   2451  1.1  thorpej 	tlp_sio_mii_sendbits(sc, val, 16);
   2452  1.1  thorpej 
   2453  1.1  thorpej 	MII_EMIT(sc, 0);
   2454  1.1  thorpej }
   2455  1.1  thorpej 
   2456  1.1  thorpej #undef MII_EMIT
   2457  1.1  thorpej 
   2458  1.1  thorpej /*
   2459  1.1  thorpej  * tlp_pnic_mii_readreg:
   2460  1.1  thorpej  *
   2461  1.1  thorpej  *	Read a PHY register on the Lite-On PNIC.
   2462  1.1  thorpej  */
   2463  1.1  thorpej int
   2464  1.1  thorpej tlp_pnic_mii_readreg(self, phy, reg)
   2465  1.1  thorpej 	struct device *self;
   2466  1.1  thorpej 	int phy, reg;
   2467  1.1  thorpej {
   2468  1.1  thorpej 	struct tulip_softc *sc = (void *) self;
   2469  1.1  thorpej 	u_int32_t val;
   2470  1.1  thorpej 	int i;
   2471  1.1  thorpej 
   2472  1.1  thorpej 	TULIP_WRITE(sc, CSR_PNIC_MII,
   2473  1.6  thorpej 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   2474  1.1  thorpej 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
   2475  1.1  thorpej 	    (reg << PNIC_MII_REGSHIFT));
   2476  1.1  thorpej 
   2477  1.1  thorpej 	for (i = 0; i < 1000; i++) {
   2478  1.1  thorpej 		delay(10);
   2479  1.1  thorpej 		val = TULIP_READ(sc, CSR_PNIC_MII);
   2480  1.1  thorpej 		if ((val & PNIC_MII_BUSY) == 0) {
   2481  1.1  thorpej 			if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
   2482  1.1  thorpej 				return (0);
   2483  1.1  thorpej 			else
   2484  1.1  thorpej 				return (val & PNIC_MII_DATA);
   2485  1.1  thorpej 		}
   2486  1.1  thorpej 	}
   2487  1.1  thorpej 	printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
   2488  1.1  thorpej 	return (0);
   2489  1.1  thorpej }
   2490  1.1  thorpej 
   2491  1.1  thorpej /*
   2492  1.1  thorpej  * tlp_pnic_mii_writereg:
   2493  1.1  thorpej  *
   2494  1.1  thorpej  *	Write a PHY register on the Lite-On PNIC.
   2495  1.1  thorpej  */
   2496  1.1  thorpej void
   2497  1.1  thorpej tlp_pnic_mii_writereg(self, phy, reg, val)
   2498  1.1  thorpej 	struct device *self;
   2499  1.1  thorpej 	int phy, reg, val;
   2500  1.1  thorpej {
   2501  1.1  thorpej 	struct tulip_softc *sc = (void *) self;
   2502  1.1  thorpej 	int i;
   2503  1.1  thorpej 
   2504  1.1  thorpej 	TULIP_WRITE(sc, CSR_PNIC_MII,
   2505  1.6  thorpej 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   2506  1.1  thorpej 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
   2507  1.1  thorpej 	    (reg << PNIC_MII_REGSHIFT) | val);
   2508  1.1  thorpej 
   2509  1.1  thorpej 	for (i = 0; i < 1000; i++) {
   2510  1.1  thorpej 		delay(10);
   2511  1.1  thorpej 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
   2512  1.1  thorpej 			return;
   2513  1.1  thorpej 	}
   2514  1.1  thorpej 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
   2515  1.1  thorpej }
   2516  1.1  thorpej 
   2517  1.1  thorpej /*****************************************************************************
   2518  1.1  thorpej  * Chip/board-specific media switches.  The ones here are ones that
   2519  1.1  thorpej  * are potentially common to multiple front-ends.
   2520  1.1  thorpej  *****************************************************************************/
   2521  1.1  thorpej 
   2522  1.1  thorpej /*
   2523  1.1  thorpej  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
   2524  1.1  thorpej  */
   2525  1.1  thorpej void	tlp_sio_mii_tmsw_init __P((struct tulip_softc *));
   2526  1.1  thorpej 
   2527  1.1  thorpej const struct tulip_mediasw tlp_sio_mii_mediasw = {
   2528  1.1  thorpej 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   2529  1.1  thorpej };
   2530  1.1  thorpej 
   2531  1.1  thorpej void
   2532  1.1  thorpej tlp_sio_mii_tmsw_init(sc)
   2533  1.1  thorpej 	struct tulip_softc *sc;
   2534  1.1  thorpej {
   2535  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2536  1.1  thorpej 
   2537  1.1  thorpej 	sc->sc_mii.mii_ifp = ifp;
   2538  1.1  thorpej 	sc->sc_mii.mii_readreg = tlp_sio_mii_readreg;
   2539  1.1  thorpej 	sc->sc_mii.mii_writereg = tlp_sio_mii_writereg;
   2540  1.5  thorpej 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   2541  1.1  thorpej 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   2542  1.1  thorpej 	    tlp_mediastatus);
   2543  1.1  thorpej 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
   2544  1.1  thorpej 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   2545  1.1  thorpej 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   2546  1.1  thorpej 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   2547  1.1  thorpej 	} else {
   2548  1.1  thorpej 		sc->sc_flags |= TULIPF_HAS_MII;
   2549  1.6  thorpej 		sc->sc_tick = tlp_mii_tick;
   2550  1.1  thorpej 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   2551  1.1  thorpej 	}
   2552  1.1  thorpej }
   2553  1.1  thorpej 
   2554  1.1  thorpej /*
   2555  1.1  thorpej  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
   2556  1.1  thorpej  */
   2557  1.1  thorpej void	tlp_pnic_tmsw_init __P((struct tulip_softc *));
   2558  1.1  thorpej void	tlp_pnic_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   2559  1.1  thorpej int	tlp_pnic_tmsw_set __P((struct tulip_softc *));
   2560  1.1  thorpej 
   2561  1.1  thorpej const struct tulip_mediasw tlp_pnic_mediasw = {
   2562  1.1  thorpej 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
   2563  1.1  thorpej };
   2564  1.1  thorpej 
   2565  1.6  thorpej void	tlp_pnic_nway_tick __P((void *));
   2566  1.6  thorpej int	tlp_pnic_nway_service __P((struct tulip_softc *, int));
   2567  1.6  thorpej void	tlp_pnic_nway_reset __P((struct tulip_softc *));
   2568  1.6  thorpej int	tlp_pnic_nway_auto __P((struct tulip_softc *, int));
   2569  1.6  thorpej void	tlp_pnic_nway_auto_timeout __P((void *));
   2570  1.6  thorpej void	tlp_pnic_nway_status __P((struct tulip_softc *));
   2571  1.6  thorpej void	tlp_pnic_nway_acomp __P((struct tulip_softc *));
   2572  1.6  thorpej 
   2573  1.1  thorpej void
   2574  1.1  thorpej tlp_pnic_tmsw_init(sc)
   2575  1.1  thorpej 	struct tulip_softc *sc;
   2576  1.1  thorpej {
   2577  1.1  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2578  1.6  thorpej 	const char *sep = "";
   2579  1.6  thorpej 
   2580  1.6  thorpej #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
   2581  1.6  thorpej #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   2582  1.1  thorpej 
   2583  1.1  thorpej 	sc->sc_mii.mii_ifp = ifp;
   2584  1.1  thorpej 	sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
   2585  1.1  thorpej 	sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
   2586  1.5  thorpej 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   2587  1.1  thorpej 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   2588  1.1  thorpej 	    tlp_mediastatus);
   2589  1.1  thorpej 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
   2590  1.1  thorpej 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   2591  1.6  thorpej 		/* XXX What about AUI/BNC support? */
   2592  1.6  thorpej 		printf("%s: ", sc->sc_dev.dv_xname);
   2593  1.6  thorpej 
   2594  1.6  thorpej 		tlp_pnic_nway_reset(sc);
   2595  1.6  thorpej 
   2596  1.6  thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
   2597  1.6  thorpej 		    PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
   2598  1.6  thorpej 		PRINT("10baseT");
   2599  1.6  thorpej 
   2600  1.6  thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
   2601  1.6  thorpej 		    PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
   2602  1.6  thorpej 		PRINT("10baseT-FDX");
   2603  1.6  thorpej 
   2604  1.6  thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
   2605  1.6  thorpej 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
   2606  1.6  thorpej 		PRINT("100baseTX");
   2607  1.6  thorpej 
   2608  1.6  thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
   2609  1.6  thorpej 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
   2610  1.6  thorpej 		    PNIC_NWAY_CAP100TXFDX);
   2611  1.6  thorpej 		PRINT("100baseTX-FDX");
   2612  1.6  thorpej 
   2613  1.6  thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
   2614  1.6  thorpej 		    PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
   2615  1.6  thorpej 		    PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
   2616  1.6  thorpej 		    PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
   2617  1.6  thorpej 		PRINT("auto");
   2618  1.6  thorpej 
   2619  1.6  thorpej 		sc->sc_tick = tlp_pnic_nway_tick;
   2620  1.6  thorpej 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   2621  1.1  thorpej 	} else {
   2622  1.1  thorpej 		sc->sc_flags |= TULIPF_HAS_MII;
   2623  1.6  thorpej 		sc->sc_tick = tlp_mii_tick;
   2624  1.1  thorpej 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   2625  1.1  thorpej 	}
   2626  1.6  thorpej 
   2627  1.6  thorpej #undef ADD
   2628  1.6  thorpej #undef PRINT
   2629  1.1  thorpej }
   2630  1.1  thorpej 
   2631  1.1  thorpej void
   2632  1.1  thorpej tlp_pnic_tmsw_get(sc, ifmr)
   2633  1.1  thorpej 	struct tulip_softc *sc;
   2634  1.1  thorpej 	struct ifmediareq *ifmr;
   2635  1.1  thorpej {
   2636  1.6  thorpej 	struct mii_data *mii = &sc->sc_mii;
   2637  1.1  thorpej 
   2638  1.1  thorpej 	if (sc->sc_flags & TULIPF_HAS_MII)
   2639  1.1  thorpej 		tlp_mii_getmedia(sc, ifmr);
   2640  1.1  thorpej 	else {
   2641  1.6  thorpej 		mii->mii_media_status = 0;
   2642  1.6  thorpej 		mii->mii_media_active = IFM_NONE;
   2643  1.6  thorpej 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
   2644  1.6  thorpej 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
   2645  1.6  thorpej 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
   2646  1.1  thorpej 	}
   2647  1.1  thorpej }
   2648  1.1  thorpej 
   2649  1.1  thorpej int
   2650  1.1  thorpej tlp_pnic_tmsw_set(sc)
   2651  1.1  thorpej 	struct tulip_softc *sc;
   2652  1.1  thorpej {
   2653  1.6  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2654  1.6  thorpej 	struct mii_data *mii = &sc->sc_mii;
   2655  1.1  thorpej 
   2656  1.1  thorpej 	if (sc->sc_flags & TULIPF_HAS_MII)
   2657  1.1  thorpej 		return (tlp_mii_setmedia(sc));
   2658  1.1  thorpej 
   2659  1.6  thorpej 	if (ifp->if_flags & IFF_UP) {
   2660  1.6  thorpej 		mii->mii_media_status = 0;
   2661  1.6  thorpej 		mii->mii_media_active = IFM_NONE;
   2662  1.6  thorpej 		return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
   2663  1.6  thorpej 	}
   2664  1.6  thorpej 
   2665  1.6  thorpej 	return (0);
   2666  1.6  thorpej }
   2667  1.6  thorpej 
   2668  1.6  thorpej void
   2669  1.6  thorpej tlp_pnic_nway_tick(arg)
   2670  1.6  thorpej 	void *arg;
   2671  1.6  thorpej {
   2672  1.6  thorpej 	struct tulip_softc *sc = arg;
   2673  1.6  thorpej 	int s;
   2674  1.6  thorpej 
   2675  1.6  thorpej 	s = splnet();
   2676  1.6  thorpej 	tlp_pnic_nway_service(sc, MII_TICK);
   2677  1.6  thorpej 	splx(s);
   2678  1.6  thorpej 
   2679  1.6  thorpej 	timeout(tlp_pnic_nway_tick, sc, hz);
   2680  1.6  thorpej }
   2681  1.6  thorpej 
   2682  1.6  thorpej /*
   2683  1.6  thorpej  * Support for the Lite-On PNIC internal NWay block.  This is constructed
   2684  1.6  thorpej  * somewhat like a PHY driver for simplicity.
   2685  1.6  thorpej  */
   2686  1.6  thorpej 
   2687  1.6  thorpej int
   2688  1.6  thorpej tlp_pnic_nway_service(sc, cmd)
   2689  1.6  thorpej 	struct tulip_softc *sc;
   2690  1.6  thorpej 	int cmd;
   2691  1.6  thorpej {
   2692  1.6  thorpej 	struct mii_data *mii = &sc->sc_mii;
   2693  1.6  thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   2694  1.6  thorpej 
   2695  1.6  thorpej 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   2696  1.6  thorpej 		return (0);
   2697  1.6  thorpej 
   2698  1.6  thorpej 	switch (cmd) {
   2699  1.6  thorpej 	case MII_POLLSTAT:
   2700  1.6  thorpej 		/* Nothing special to do here. */
   2701  1.6  thorpej 		break;
   2702  1.6  thorpej 
   2703  1.6  thorpej 	case MII_MEDIACHG:
   2704  1.6  thorpej 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   2705  1.6  thorpej 		case IFM_AUTO:
   2706  1.6  thorpej 			(void) tlp_pnic_nway_auto(sc, 1);
   2707  1.6  thorpej 			break;
   2708  1.6  thorpej 		case IFM_100_T4:
   2709  1.6  thorpej 			/*
   2710  1.6  thorpej 			 * XXX Not supported as a manual setting right now.
   2711  1.6  thorpej 			 */
   2712  1.6  thorpej 			return (EINVAL);
   2713  1.6  thorpej 		default:
   2714  1.6  thorpej 			/*
   2715  1.6  thorpej 			 * NWAY register data is stored in the ifmedia entry.
   2716  1.6  thorpej 			 */
   2717  1.6  thorpej 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   2718  1.6  thorpej 		}
   2719  1.6  thorpej 		break;
   2720  1.6  thorpej 
   2721  1.6  thorpej 	case MII_TICK:
   2722  1.6  thorpej 		/*
   2723  1.6  thorpej 		 * Only used for autonegotiation.
   2724  1.6  thorpej 		 */
   2725  1.6  thorpej 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   2726  1.6  thorpej 			return (0);
   2727  1.6  thorpej 
   2728  1.6  thorpej 		/*
   2729  1.6  thorpej 		 * Check to see if we have link.  If we do, we don't
   2730  1.6  thorpej 		 * need to restart the autonegotiation process.
   2731  1.6  thorpej 		 */
   2732  1.6  thorpej 		if (sc->sc_flags & TULIPF_LINK_UP)
   2733  1.6  thorpej 			return (0);
   2734  1.6  thorpej 
   2735  1.6  thorpej 		/*
   2736  1.6  thorpej 		 * Only retry autonegotiation every 5 seconds.
   2737  1.6  thorpej 		 */
   2738  1.6  thorpej 		if (++sc->sc_nway_ticks != 5)
   2739  1.6  thorpej 			return (0);
   2740  1.6  thorpej 
   2741  1.6  thorpej 		sc->sc_nway_ticks = 0;
   2742  1.6  thorpej 		tlp_pnic_nway_reset(sc);
   2743  1.6  thorpej 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
   2744  1.6  thorpej 			return (0);
   2745  1.6  thorpej 		break;
   2746  1.6  thorpej 	}
   2747  1.6  thorpej 
   2748  1.6  thorpej 	/* Update the media status. */
   2749  1.6  thorpej 	tlp_pnic_nway_status(sc);
   2750  1.6  thorpej 
   2751  1.6  thorpej 	/* Callback if something changed. */
   2752  1.6  thorpej 	if (sc->sc_nway_active != mii->mii_media_active ||
   2753  1.6  thorpej 	    cmd == MII_MEDIACHG) {
   2754  1.6  thorpej 		(*sc->sc_statchg)(&sc->sc_dev);
   2755  1.6  thorpej 		sc->sc_nway_active = mii->mii_media_active;
   2756  1.6  thorpej 	}
   2757  1.6  thorpej 	return (0);
   2758  1.6  thorpej }
   2759  1.6  thorpej 
   2760  1.6  thorpej void
   2761  1.6  thorpej tlp_pnic_nway_reset(sc)
   2762  1.6  thorpej 	struct tulip_softc *sc;
   2763  1.6  thorpej {
   2764  1.6  thorpej 
   2765  1.6  thorpej 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
   2766  1.6  thorpej 	delay(100);
   2767  1.6  thorpej 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
   2768  1.6  thorpej }
   2769  1.6  thorpej 
   2770  1.6  thorpej int
   2771  1.6  thorpej tlp_pnic_nway_auto(sc, waitfor)
   2772  1.6  thorpej 	struct tulip_softc *sc;
   2773  1.6  thorpej 	int waitfor;
   2774  1.6  thorpej {
   2775  1.6  thorpej 	struct mii_data *mii = &sc->sc_mii;
   2776  1.6  thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   2777  1.6  thorpej 	u_int32_t reg;
   2778  1.6  thorpej 	int i;
   2779  1.6  thorpej 
   2780  1.6  thorpej 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
   2781  1.6  thorpej 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   2782  1.6  thorpej 
   2783  1.6  thorpej 	if (waitfor) {
   2784  1.6  thorpej 		/* Wait 500ms for it to complete. */
   2785  1.6  thorpej 		for (i = 0; i < 500; i++) {
   2786  1.6  thorpej 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   2787  1.6  thorpej 			if (reg & PNIC_NWAY_LPAR_MASK) {
   2788  1.6  thorpej 				tlp_pnic_nway_acomp(sc);
   2789  1.6  thorpej 				return (0);
   2790  1.6  thorpej 			}
   2791  1.6  thorpej 			delay(1000);
   2792  1.6  thorpej 		}
   2793  1.6  thorpej #if 0
   2794  1.6  thorpej 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   2795  1.6  thorpej 			printf("%s: autonegotiation failed to complete\n",
   2796  1.6  thorpej 			    sc->sc_dev.dv_xname);
   2797  1.6  thorpej #endif
   2798  1.6  thorpej 
   2799  1.6  thorpej 		/*
   2800  1.6  thorpej 		 * Don't need to worry about clearing DOINGAUTO.
   2801  1.6  thorpej 		 * If that's set, a timeout is pending, and it will
   2802  1.6  thorpej 		 * clear the flag.
   2803  1.6  thorpej 		 */
   2804  1.6  thorpej 		return (EIO);
   2805  1.6  thorpej 	}
   2806  1.6  thorpej 
   2807  1.6  thorpej 	/*
   2808  1.6  thorpej 	 * Just let it finish asynchronously.  This is for the benefit of
   2809  1.6  thorpej 	 * the tick handler driving autonegotiation.  Don't want 500ms
   2810  1.6  thorpej 	 * delays all the time while the system is running!
   2811  1.6  thorpej 	 */
   2812  1.6  thorpej 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
   2813  1.6  thorpej 		sc->sc_flags |= TULIPF_DOINGAUTO;
   2814  1.6  thorpej 		timeout(tlp_pnic_nway_auto_timeout, sc, hz >> 1);
   2815  1.6  thorpej 	}
   2816  1.6  thorpej 	return (EJUSTRETURN);
   2817  1.6  thorpej }
   2818  1.6  thorpej 
   2819  1.6  thorpej void
   2820  1.6  thorpej tlp_pnic_nway_auto_timeout(arg)
   2821  1.6  thorpej 	void *arg;
   2822  1.6  thorpej {
   2823  1.6  thorpej 	struct tulip_softc *sc = arg;
   2824  1.6  thorpej 	u_int32_t reg;
   2825  1.6  thorpej 	int s;
   2826  1.6  thorpej 
   2827  1.6  thorpej 	s = splnet();
   2828  1.6  thorpej 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
   2829  1.6  thorpej 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   2830  1.6  thorpej #if 0
   2831  1.6  thorpej 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   2832  1.6  thorpej 		printf("%s: autonegotiation failed to complete\n",
   2833  1.6  thorpej 		    sc->sc_dev.dv_xname);
   2834  1.6  thorpej #endif
   2835  1.6  thorpej 
   2836  1.6  thorpej 	tlp_pnic_nway_acomp(sc);
   2837  1.6  thorpej 
   2838  1.6  thorpej 	/* Update the media status. */
   2839  1.6  thorpej 	(void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
   2840  1.6  thorpej 	splx(s);
   2841  1.6  thorpej }
   2842  1.6  thorpej 
   2843  1.6  thorpej void
   2844  1.6  thorpej tlp_pnic_nway_status(sc)
   2845  1.6  thorpej 	struct tulip_softc *sc;
   2846  1.6  thorpej {
   2847  1.6  thorpej 	struct mii_data *mii = &sc->sc_mii;
   2848  1.6  thorpej 	u_int32_t reg;
   2849  1.6  thorpej 
   2850  1.6  thorpej 	mii->mii_media_status = IFM_AVALID;
   2851  1.6  thorpej 	mii->mii_media_active = IFM_ETHER;
   2852  1.6  thorpej 
   2853  1.6  thorpej 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   2854  1.6  thorpej 
   2855  1.6  thorpej 	if (sc->sc_flags & TULIPF_LINK_UP)
   2856  1.6  thorpej 		mii->mii_media_status |= IFM_ACTIVE;
   2857  1.6  thorpej 
   2858  1.6  thorpej 	if (reg & PNIC_NWAY_NW) {
   2859  1.6  thorpej 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
   2860  1.6  thorpej 			/* Erg, still trying, I guess... */
   2861  1.6  thorpej 			mii->mii_media_active |= IFM_NONE;
   2862  1.6  thorpej 			return;
   2863  1.6  thorpej 		}
   2864  1.6  thorpej 
   2865  1.6  thorpej #if 0
   2866  1.6  thorpej 		if (reg & PNIC_NWAY_LPAR100T4)
   2867  1.6  thorpej 			mii->mii_media_active |= IFM_100_T4;
   2868  1.6  thorpej 		else
   2869  1.6  thorpej #endif
   2870  1.6  thorpej 		if (reg & PNIC_NWAY_LPAR100TXFDX)
   2871  1.6  thorpej 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
   2872  1.6  thorpej 		else if (reg & PNIC_NWAY_LPAR100TX)
   2873  1.6  thorpej 			mii->mii_media_active |= IFM_100_TX;
   2874  1.6  thorpej 		else if (reg & PNIC_NWAY_LPAR10TFDX)
   2875  1.6  thorpej 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
   2876  1.6  thorpej 		else if (reg & PNIC_NWAY_LPAR10T)
   2877  1.6  thorpej 			mii->mii_media_active |= IFM_10_T;
   2878  1.6  thorpej 		else
   2879  1.6  thorpej 			mii->mii_media_active |= IFM_NONE;
   2880  1.6  thorpej 	} else {
   2881  1.6  thorpej 		if (reg & PNIC_NWAY_100)
   2882  1.6  thorpej 			mii->mii_media_active |= IFM_100_TX;
   2883  1.6  thorpej 		else
   2884  1.6  thorpej 			mii->mii_media_active |= IFM_10_T;
   2885  1.6  thorpej 		if (reg & PNIC_NWAY_FD)
   2886  1.6  thorpej 			mii->mii_media_active |= IFM_FDX;
   2887  1.6  thorpej 	}
   2888  1.6  thorpej }
   2889  1.6  thorpej 
   2890  1.6  thorpej void
   2891  1.6  thorpej tlp_pnic_nway_acomp(sc)
   2892  1.6  thorpej 	struct tulip_softc *sc;
   2893  1.6  thorpej {
   2894  1.6  thorpej 	u_int32_t reg;
   2895  1.6  thorpej 
   2896  1.6  thorpej 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   2897  1.6  thorpej 	reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
   2898  1.6  thorpej 
   2899  1.6  thorpej 	if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
   2900  1.6  thorpej 		reg |= PNIC_NWAY_100;
   2901  1.6  thorpej 	if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
   2902  1.6  thorpej 		reg |= PNIC_NWAY_FD;
   2903  1.6  thorpej 
   2904  1.6  thorpej 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
   2905  1.1  thorpej }
   2906