if_de.c revision 58339
149560Speter/*	$NetBSD: if_de.c,v 1.86 1999/06/01 19:17:59 thorpej Exp $	*/
230556Speter
349562Speter/* $FreeBSD: head/sys/dev/de/if_de.c 58339 2000-03-20 09:18:46Z peter $ */
449562Speter
53278Swollman/*-
626797Speter * Copyright (c) 1994-1997 Matt Thomas (matt@3am-software.com)
73278Swollman * All rights reserved.
83278Swollman *
93278Swollman * Redistribution and use in source and binary forms, with or without
103278Swollman * modification, are permitted provided that the following conditions
113278Swollman * are met:
123278Swollman * 1. Redistributions of source code must retain the above copyright
133278Swollman *    notice, this list of conditions and the following disclaimer.
143278Swollman * 2. The name of the author may not be used to endorse or promote products
153278Swollman *    derived from this software withough specific prior written permission
163278Swollman *
173278Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
183278Swollman * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
193278Swollman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
203278Swollman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
213278Swollman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223278Swollman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233278Swollman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243278Swollman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253278Swollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
263278Swollman * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273278Swollman *
2830556Speter * Id: if_de.c,v 1.94 1997/07/03 16:55:07 thomas Exp
293278Swollman *
303278Swollman */
313278Swollman
323278Swollman/*
3320060Srgrimes * DEC 21040 PCI Ethernet Controller
343278Swollman *
353278Swollman * Written by Matt Thomas
363278Swollman * BPF support code stolen directly from if_ec.c
373278Swollman *
383278Swollman *   This driver supports the DEC DE435 or any other PCI
3920060Srgrimes *   board which support 21040, 21041, or 21140 (mostly).
403278Swollman */
4126797Speter#define	TULIP_HDR_DATA
423278Swollman
434772Sdg#include <sys/param.h>
444772Sdg#include <sys/systm.h>
454772Sdg#include <sys/mbuf.h>
464772Sdg#include <sys/socket.h>
4724204Sbde#include <sys/sockio.h>
484772Sdg#include <sys/malloc.h>
496132Sdg#include <sys/kernel.h>
5050133Sbillf#include <sys/eventhandler.h>
516132Sdg#include <machine/clock.h>
5258339Speter#include <machine/bus.h>
5358339Speter#include <machine/resource.h>
5458339Speter#include <sys/bus.h>
5558339Speter#include <sys/rman.h>
563278Swollman
5740944Speter#include "opt_inet.h"
5840944Speter#include "opt_ipx.h"
5940944Speter
603278Swollman#include <net/if.h>
6126797Speter#include <net/if_media.h>
6218857Swollman#include <net/if_dl.h>
6331350Sbde#ifdef TULIP_USE_SOFTINTR
6418357Sdg#include <net/netisr.h>
6531350Sbde#endif
663278Swollman
673278Swollman#include <net/bpf.h>
683278Swollman
693278Swollman#ifdef INET
703278Swollman#include <netinet/in.h>
7132350Seivind#include <netinet/if_ether.h>
723278Swollman#endif
733278Swollman
7430342Speter#ifdef IPX
7530342Speter#include <netipx/ipx.h>
7630342Speter#include <netipx/ipx_if.h>
7730342Speter#endif
7830342Speter
793278Swollman#ifdef NS
803278Swollman#include <netns/ns.h>
813278Swollman#include <netns/ns_if.h>
823278Swollman#endif
833278Swollman
843278Swollman#include <vm/vm.h>
853278Swollman
8644719Speter#include <net/if_var.h>
8716357Sdg#include <vm/pmap.h>
886132Sdg#include <pci/pcivar.h>
8957249Smjacob#include <pci/pcireg.h>
9026797Speter#include <pci/dc21040reg.h>
9149575Speter
9244829Sluigi#include "opt_bdg.h"
9344829Sluigi#ifdef BRIDGE
9444829Sluigi#include <net/bridge.h>
9544829Sluigi#endif
966132Sdg
973278Swollman/*
9811070Sdg * Intel CPUs should use I/O mapped access.
9911070Sdg */
10049575Speter#if defined(__i386__)
10111070Sdg#define	TULIP_IOMAPPED
10211070Sdg#endif
10311070Sdg
10416357Sdg#if 0
10511070Sdg/*
10616357Sdg * This turns on all sort of debugging stuff and make the
10716357Sdg * driver much larger.
10816357Sdg */
10916357Sdg#define TULIP_DEBUG
11016357Sdg#endif
11116357Sdg
11218357Sdg#if 0
11327862Speter#define	TULIP_PERFSTATS
11427862Speter#endif
11527862Speter
11627862Speter#if 0
11718357Sdg#define	TULIP_USE_SOFTINTR
11818357Sdg#endif
11918357Sdg
12026797Speter#define	TULIP_HZ	10
12126797Speter
12249572Speter#include <pci/if_devar.h>
12349572Speter
12416357Sdg/*
1257689Sdg * This module supports
12620060Srgrimes *	the DEC 21040 PCI Ethernet Controller.
12720060Srgrimes *	the DEC 21041 PCI Ethernet Controller.
12820060Srgrimes *	the DEC 21140 PCI Fast Ethernet Controller.
1293278Swollman */
13026797Speterstatic void tulip_mii_autonegotiate(tulip_softc_t * const sc, const unsigned phyaddr);
13149572Speterstatic void tulip_intr_shared(void *arg);
13249572Speterstatic void tulip_intr_normal(void *arg);
13326797Speterstatic void tulip_init(tulip_softc_t * const sc);
13426797Speterstatic void tulip_reset(tulip_softc_t * const sc);
13549575Speterstatic void tulip_ifstart_one(struct ifnet *ifp);
13649575Speterstatic void tulip_ifstart(struct ifnet *ifp);
13727862Speterstatic struct mbuf *tulip_txput(tulip_softc_t * const sc, struct mbuf *m);
13827862Speterstatic void tulip_txput_setup(tulip_softc_t * const sc);
13926797Speterstatic void tulip_rx_intr(tulip_softc_t * const sc);
14026797Speterstatic void tulip_addr_filter(tulip_softc_t * const sc);
14126797Speterstatic unsigned tulip_mii_readreg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno);
14226797Speterstatic void tulip_mii_writereg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno, unsigned data);
14326797Speterstatic int tulip_mii_map_abilities(tulip_softc_t * const sc, unsigned abilities);
14426797Speterstatic tulip_media_t tulip_mii_phy_readspecific(tulip_softc_t * const sc);
14526797Speterstatic int tulip_srom_decode(tulip_softc_t * const sc);
14626797Speterstatic int tulip_ifmedia_change(struct ifnet * const ifp);
14726797Speterstatic void tulip_ifmedia_status(struct ifnet * const ifp, struct ifmediareq *req);
14826797Speter/* static void tulip_21140_map_media(tulip_softc_t *sc); */
14926797Speter
15026797Speterstatic void
15126797Spetertulip_timeout_callback(
15226797Speter    void *arg)
15326797Speter{
15426797Speter    tulip_softc_t * const sc = arg;
15549572Speter    int s = splimp();
1563278Swollman
15727862Speter    TULIP_PERFSTART(timeout)
15827862Speter
15926797Speter    sc->tulip_flags &= ~TULIP_TIMEOUTPENDING;
16026797Speter    sc->tulip_probe_timeout -= 1000 / TULIP_HZ;
16126797Speter    (sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_TIMER);
16227862Speter
16327862Speter    TULIP_PERFEND(timeout);
16449572Speter    splx(s);
16526797Speter}
1667689Sdg
16726797Speterstatic void
16826797Spetertulip_timeout(
16926797Speter    tulip_softc_t * const sc)
17026797Speter{
17126797Speter    if (sc->tulip_flags & TULIP_TIMEOUTPENDING)
17226797Speter	return;
17326797Speter    sc->tulip_flags |= TULIP_TIMEOUTPENDING;
17426797Speter    timeout(tulip_timeout_callback, sc, (hz + TULIP_HZ / 2) / TULIP_HZ);
17526797Speter}
1767689Sdg
17726797Speter#if defined(TULIP_NEED_FASTTIMEOUT)
17826797Speterstatic void
17926797Spetertulip_fasttimeout_callback(
18026797Speter    void *arg)
18126797Speter{
18226797Speter    tulip_softc_t * const sc = arg;
18349572Speter    int s = splimp();
1847689Sdg
18526797Speter    sc->tulip_flags &= ~TULIP_FASTTIMEOUTPENDING;
18626797Speter    (sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_FASTTIMER);
18749572Speter    splx(s);
18826797Speter}
18916357Sdg
19026797Speterstatic void
19126797Spetertulip_fasttimeout(
19226797Speter    tulip_softc_t * const sc)
19326797Speter{
19426797Speter    if (sc->tulip_flags & TULIP_FASTTIMEOUTPENDING)
19526797Speter	return;
19626797Speter    sc->tulip_flags |= TULIP_FASTTIMEOUTPENDING;
19726797Speter    timeout(tulip_fasttimeout_callback, sc, 1);
19826797Speter}
1998754Sdg#endif
20026797Speter
20126797Speterstatic int
20226797Spetertulip_txprobe(
20326797Speter    tulip_softc_t * const sc)
20426797Speter{
20526797Speter    struct mbuf *m;
20616357Sdg    /*
20726797Speter     * Before we are sure this is the right media we need
20826797Speter     * to send a small packet to make sure there's carrier.
20927862Speter     * Strangely, BNC and AUI will "see" receive data if
21026797Speter     * either is connected so the transmit is the only way
21126797Speter     * to verify the connectivity.
21216357Sdg     */
21326797Speter    MGETHDR(m, M_DONTWAIT, MT_DATA);
21426797Speter    if (m == NULL)
21526797Speter	return 0;
21616357Sdg    /*
21726797Speter     * Construct a LLC TEST message which will point to ourselves.
21816357Sdg     */
21926797Speter    bcopy(sc->tulip_enaddr, mtod(m, struct ether_header *)->ether_dhost, 6);
22026797Speter    bcopy(sc->tulip_enaddr, mtod(m, struct ether_header *)->ether_shost, 6);
22126797Speter    mtod(m, struct ether_header *)->ether_type = htons(3);
22226797Speter    mtod(m, unsigned char *)[14] = 0;
22326797Speter    mtod(m, unsigned char *)[15] = 0;
22426797Speter    mtod(m, unsigned char *)[16] = 0xE3;	/* LLC Class1 TEST (no poll) */
22526797Speter    m->m_len = m->m_pkthdr.len = sizeof(struct ether_header) + 3;
22618357Sdg    /*
22726797Speter     * send it!
22818357Sdg     */
22926797Speter    sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
23027862Speter    sc->tulip_intrmask |= TULIP_STS_TXINTR;
23126797Speter    sc->tulip_flags |= TULIP_TXPROBE_ACTIVE;
23226797Speter    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
23327862Speter    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
23427862Speter    if ((m = tulip_txput(sc, m)) != NULL)
23527862Speter	m_freem(m);
23626797Speter    sc->tulip_probe.probe_txprobes++;
23726797Speter    return 1;
23826797Speter}
23926797Speter
24026797Speter#ifdef BIG_PACKET
24126797Speter#define TULIP_SIAGEN_WATCHDOG	(sc->tulip_if.if_mtu > ETHERMTU ? TULIP_WATCHDOG_RXDISABLE|TULIP_WATCHDOG_TXDISABLE : 0)
24216357Sdg#else
24326797Speter#define	TULIP_SIAGEN_WATCHDOG	0
24411070Sdg#endif
2453543Sse
24626797Speterstatic void
24726797Spetertulip_media_set(
24826797Speter    tulip_softc_t * const sc,
24926797Speter    tulip_media_t media)
25026797Speter{
25126797Speter    const tulip_media_info_t *mi = sc->tulip_mediums[media];
25218857Swollman
25326797Speter    if (mi == NULL)
25426797Speter	return;
25516357Sdg
25626797Speter    /*
25726797Speter     * If we are switching media, make sure we don't think there's
25826797Speter     * any stale RX activity
25926797Speter     */
26026797Speter    sc->tulip_flags &= ~TULIP_RXACT;
26126797Speter    if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
26226797Speter	TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
26326797Speter	TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        mi->mi_sia_tx_rx);
26426797Speter	if (sc->tulip_features & TULIP_HAVE_SIAGP) {
26526797Speter	    TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_gp_control|mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
26630556Speter	    DELAY(50);
26726797Speter	    TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_gp_data|mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
26826797Speter	} else {
26926797Speter	    TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
27026797Speter	}
27126797Speter	TULIP_CSR_WRITE(sc, csr_sia_connectivity, mi->mi_sia_connectivity);
27226797Speter    } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
27326797Speter#define	TULIP_GPR_CMDBITS	(TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER|TULIP_CMD_TXTHRSHLDCTL)
27426797Speter	/*
27526797Speter	 * If the cmdmode bits don't match the currently operating mode,
27626797Speter	 * set the cmdmode appropriately and reset the chip.
27726797Speter	 */
27826797Speter	if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
27926797Speter	    sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
28026797Speter	    sc->tulip_cmdmode |= mi->mi_cmdmode;
28126797Speter	    tulip_reset(sc);
28226797Speter	}
28326797Speter	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
28426797Speter	DELAY(10);
28526797Speter	TULIP_CSR_WRITE(sc, csr_gp, (u_int8_t) mi->mi_gpdata);
28626797Speter    } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
28726797Speter	/*
28826797Speter	 * If the cmdmode bits don't match the currently operating mode,
28926797Speter	 * set the cmdmode appropriately and reset the chip.
29026797Speter	 */
29126797Speter	if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
29226797Speter	    sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
29326797Speter	    sc->tulip_cmdmode |= mi->mi_cmdmode;
29426797Speter	    tulip_reset(sc);
29526797Speter	}
29626797Speter	TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpcontrol);
29726797Speter	TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpdata);
29826797Speter    } else if (mi->mi_type == TULIP_MEDIAINFO_MII
29926797Speter	       && sc->tulip_probe_state != TULIP_PROBE_INACTIVE) {
30026797Speter	int idx;
30126797Speter	if (sc->tulip_features & TULIP_HAVE_SIAGP) {
30226797Speter	    const u_int8_t *dp;
30326797Speter	    dp = &sc->tulip_rombuf[mi->mi_reset_offset];
30426797Speter	    for (idx = 0; idx < mi->mi_reset_length; idx++, dp += 2) {
30526797Speter		DELAY(10);
30626797Speter		TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
30726797Speter	    }
30826797Speter	    sc->tulip_phyaddr = mi->mi_phyaddr;
30926797Speter	    dp = &sc->tulip_rombuf[mi->mi_gpr_offset];
31026797Speter	    for (idx = 0; idx < mi->mi_gpr_length; idx++, dp += 2) {
31126797Speter		DELAY(10);
31226797Speter		TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
31326797Speter	    }
31426797Speter	} else {
31526797Speter	    for (idx = 0; idx < mi->mi_reset_length; idx++) {
31626797Speter		DELAY(10);
31726797Speter		TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx]);
31826797Speter	    }
31926797Speter	    sc->tulip_phyaddr = mi->mi_phyaddr;
32026797Speter	    for (idx = 0; idx < mi->mi_gpr_length; idx++) {
32126797Speter		DELAY(10);
32226797Speter		TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx]);
32326797Speter	    }
32426797Speter	}
32526797Speter	if (sc->tulip_flags & TULIP_TRYNWAY) {
32626797Speter	    tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
32726797Speter	} else if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
32826797Speter	    u_int32_t data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_CONTROL);
32926797Speter	    data &= ~(PHYCTL_SELECT_100MB|PHYCTL_FULL_DUPLEX|PHYCTL_AUTONEG_ENABLE);
33026797Speter	    sc->tulip_flags &= ~TULIP_DIDNWAY;
33126797Speter	    if (TULIP_IS_MEDIA_FD(media))
33226797Speter		data |= PHYCTL_FULL_DUPLEX;
33326797Speter	    if (TULIP_IS_MEDIA_100MB(media))
33426797Speter		data |= PHYCTL_SELECT_100MB;
33526797Speter	    tulip_mii_writereg(sc, sc->tulip_phyaddr, PHYREG_CONTROL, data);
33626797Speter	}
33726797Speter    }
33826797Speter}
33926797Speter
34026797Speterstatic void
34126797Spetertulip_linkup(
34226797Speter    tulip_softc_t * const sc,
34326797Speter    tulip_media_t media)
34426797Speter{
34526797Speter    if ((sc->tulip_flags & TULIP_LINKUP) == 0)
34626797Speter	sc->tulip_flags |= TULIP_PRINTLINKUP;
34726797Speter    sc->tulip_flags |= TULIP_LINKUP;
34826797Speter    sc->tulip_if.if_flags &= ~IFF_OACTIVE;
34926797Speter#if 0 /* XXX how does with work with ifmedia? */
35026797Speter    if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
35126797Speter	if (sc->tulip_if.if_flags & IFF_FULLDUPLEX) {
35226797Speter	    if (TULIP_CAN_MEDIA_FD(media)
35326797Speter		    && sc->tulip_mediums[TULIP_FD_MEDIA_OF(media)] != NULL)
35426797Speter		media = TULIP_FD_MEDIA_OF(media);
35526797Speter	} else {
35626797Speter	    if (TULIP_IS_MEDIA_FD(media)
35726797Speter		    && sc->tulip_mediums[TULIP_HD_MEDIA_OF(media)] != NULL)
35826797Speter		media = TULIP_HD_MEDIA_OF(media);
35926797Speter	}
36026797Speter    }
36126797Speter#endif
36226797Speter    if (sc->tulip_media != media) {
36326797Speter#ifdef TULIP_DEBUG
36426797Speter	sc->tulip_dbg.dbg_last_media = sc->tulip_media;
36526797Speter#endif
36626797Speter	sc->tulip_media = media;
36726797Speter	sc->tulip_flags |= TULIP_PRINTMEDIA;
36826797Speter	if (TULIP_IS_MEDIA_FD(sc->tulip_media)) {
36926797Speter	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
37026797Speter	} else if (sc->tulip_chipid != TULIP_21041 || (sc->tulip_flags & TULIP_DIDNWAY) == 0) {
37126797Speter	    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
37226797Speter	}
37326797Speter    }
37426797Speter    /*
37526797Speter     * We could set probe_timeout to 0 but setting to 3000 puts this
37626797Speter     * in one central place and the only matters is tulip_link is
37726797Speter     * followed by a tulip_timeout.  Therefore setting it should not
37826797Speter     * result in aberrant behavour.
37926797Speter     */
38026797Speter    sc->tulip_probe_timeout = 3000;
38126797Speter    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
38226797Speter    sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_TRYNWAY);
38326797Speter    if (sc->tulip_flags & TULIP_INRESET) {
38426797Speter	tulip_media_set(sc, sc->tulip_media);
38530556Speter    } else if (sc->tulip_probe_media != sc->tulip_media) {
38630556Speter	/*
38730556Speter	 * No reason to change media if we have the right media.
38830556Speter	 */
38926797Speter	tulip_reset(sc);
39026797Speter    }
39134317Speter    tulip_init(sc);
39226797Speter}
39326797Speter
39426797Speterstatic void
39526797Spetertulip_media_print(
39626797Speter    tulip_softc_t * const sc)
39726797Speter{
39826797Speter    if ((sc->tulip_flags & TULIP_LINKUP) == 0)
39926797Speter	return;
40026797Speter    if (sc->tulip_flags & TULIP_PRINTMEDIA) {
40149572Speter	printf("%s%d: enabling %s port\n",
40249572Speter	       sc->tulip_name, sc->tulip_unit,
40326797Speter	       tulip_mediums[sc->tulip_media]);
40426797Speter	sc->tulip_flags &= ~(TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
40526797Speter    } else if (sc->tulip_flags & TULIP_PRINTLINKUP) {
40649572Speter	printf("%s%d: link up\n", sc->tulip_name, sc->tulip_unit);
40726797Speter	sc->tulip_flags &= ~TULIP_PRINTLINKUP;
40826797Speter    }
40926797Speter}
41026797Speter
41126797Speter#if defined(TULIP_DO_GPR_SENSE)
41226797Speterstatic tulip_media_t
41326797Spetertulip_21140_gpr_media_sense(
41426797Speter    tulip_softc_t * const sc)
41526797Speter{
41626797Speter    tulip_media_t maybe_media = TULIP_MEDIA_UNKNOWN;
41726797Speter    tulip_media_t last_media = TULIP_MEDIA_UNKNOWN;
41826797Speter    tulip_media_t media;
41916357Sdg
42026797Speter    /*
42126797Speter     * If one of the media blocks contained a default media flag,
42226797Speter     * use that.
42326797Speter     */
42426797Speter    for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
42526797Speter	const tulip_media_info_t *mi;
42626797Speter	/*
42726797Speter	 * Media is not supported (or is full-duplex).
42826797Speter	 */
42926797Speter	if ((mi = sc->tulip_mediums[media]) == NULL || TULIP_IS_MEDIA_FD(media))
43026797Speter	    continue;
43126797Speter	if (mi->mi_type != TULIP_MEDIAINFO_GPR)
43226797Speter	    continue;
43316357Sdg
43426797Speter	/*
43526797Speter	 * Remember the media is this is the "default" media.
43626797Speter	 */
43726797Speter	if (mi->mi_default && maybe_media == TULIP_MEDIA_UNKNOWN)
43826797Speter	    maybe_media = media;
43916357Sdg
44026797Speter	/*
44126797Speter	 * No activity mask?  Can't see if it is active if there's no mask.
44226797Speter	 */
44326797Speter	if (mi->mi_actmask == 0)
44426797Speter	    continue;
44516357Sdg
44626797Speter	/*
44726797Speter	 * Does the activity data match?
44826797Speter	 */
44926797Speter	if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) != mi->mi_actdata)
45026797Speter	    continue;
45116357Sdg
45226797Speter#if defined(TULIP_DEBUG)
45349572Speter	printf("%s%d: gpr_media_sense: %s: 0x%02x & 0x%02x == 0x%02x\n",
45449572Speter	       sc->tulip_name, sc->tulip_unit, tulip_mediums[media],
45526797Speter	       TULIP_CSR_READ(sc, csr_gp) & 0xFF,
45626797Speter	       mi->mi_actmask, mi->mi_actdata);
45716357Sdg#endif
45826797Speter	/*
45926797Speter	 * It does!  If this is the first media we detected, then
46026797Speter	 * remember this media.  If isn't the first, then there were
46126797Speter	 * multiple matches which we equate to no match (since we don't
46226797Speter	 * which to select (if any).
46326797Speter	 */
46426797Speter	if (last_media == TULIP_MEDIA_UNKNOWN) {
46526797Speter	    last_media = media;
46626797Speter	} else if (last_media != media) {
46726797Speter	    last_media = TULIP_MEDIA_UNKNOWN;
46826797Speter	}
46926797Speter    }
47026797Speter    return (last_media != TULIP_MEDIA_UNKNOWN) ? last_media : maybe_media;
47126797Speter}
47226797Speter#endif /* TULIP_DO_GPR_SENSE */
47326797Speter
47426797Speterstatic tulip_link_status_t
47526797Spetertulip_media_link_monitor(
47626797Speter    tulip_softc_t * const sc)
47726797Speter{
47826797Speter    const tulip_media_info_t * const mi = sc->tulip_mediums[sc->tulip_media];
47926797Speter    tulip_link_status_t linkup = TULIP_LINK_DOWN;
48016357Sdg
48126797Speter    if (mi == NULL) {
48226797Speter#if defined(DIAGNOSTIC) || defined(TULIP_DEBUG)
48326797Speter	panic("tulip_media_link_monitor: %s: botch at line %d\n",
48426797Speter	      tulip_mediums[sc->tulip_media],__LINE__);
48516357Sdg#endif
48626797Speter	return TULIP_LINK_UNKNOWN;
48726797Speter    }
48816357Sdg
48916357Sdg
49026797Speter    /*
49126797Speter     * Have we seen some packets?  If so, the link must be good.
49226797Speter     */
49326797Speter    if ((sc->tulip_flags & (TULIP_RXACT|TULIP_LINKUP)) == (TULIP_RXACT|TULIP_LINKUP)) {
49426797Speter	sc->tulip_flags &= ~TULIP_RXACT;
49526797Speter	sc->tulip_probe_timeout = 3000;
49626797Speter	return TULIP_LINK_UP;
49726797Speter    }
49816357Sdg
49926797Speter    sc->tulip_flags &= ~TULIP_RXACT;
50026797Speter    if (mi->mi_type == TULIP_MEDIAINFO_MII) {
50126797Speter	u_int32_t status;
50226797Speter	/*
50326797Speter	 * Read the PHY status register.
50426797Speter	 */
50526797Speter	status = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
50626797Speter	if (status & PHYSTS_AUTONEG_DONE) {
50726797Speter	    /*
50826797Speter	     * If the PHY has completed autonegotiation, see the if the
50926797Speter	     * remote systems abilities have changed.  If so, upgrade or
51026797Speter	     * downgrade as appropriate.
51126797Speter	     */
51226797Speter	    u_int32_t abilities = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_AUTONEG_ABILITIES);
51326797Speter	    abilities = (abilities << 6) & status;
51426797Speter	    if (abilities != sc->tulip_abilities) {
51526797Speter#if defined(TULIP_DEBUG)
51649572Speter		loudprintf("%s%d(phy%d): autonegotiation changed: 0x%04x -> 0x%04x\n",
51749572Speter			   sc->tulip_name, sc->tulip_unit, sc->tulip_phyaddr,
51826797Speter			   sc->tulip_abilities, abilities);
51918357Sdg#endif
52026797Speter		if (tulip_mii_map_abilities(sc, abilities)) {
52126797Speter		    tulip_linkup(sc, sc->tulip_probe_media);
52226797Speter		    return TULIP_LINK_UP;
52326797Speter		}
52426797Speter		/*
52526797Speter		 * if we had selected media because of autonegotiation,
52626797Speter		 * we need to probe for the new media.
52726797Speter		 */
52826797Speter		sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
52926797Speter		if (sc->tulip_flags & TULIP_DIDNWAY)
53026797Speter		    return TULIP_LINK_DOWN;
53126797Speter	    }
53226797Speter	}
53326797Speter	/*
53426797Speter	 * The link is now up.  If was down, say its back up.
53526797Speter	 */
53626797Speter	if ((status & (PHYSTS_LINK_UP|PHYSTS_REMOTE_FAULT)) == PHYSTS_LINK_UP)
53726797Speter	    linkup = TULIP_LINK_UP;
53826797Speter    } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
53926797Speter	/*
54026797Speter	 * No activity sensor?  Assume all's well.
54126797Speter	 */
54226797Speter	if (mi->mi_actmask == 0)
54326797Speter	    return TULIP_LINK_UNKNOWN;
54426797Speter	/*
54526797Speter	 * Does the activity data match?
54626797Speter	 */
54726797Speter	if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) == mi->mi_actdata)
54826797Speter	    linkup = TULIP_LINK_UP;
54926797Speter    } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
55026797Speter	/*
55126797Speter	 * Assume non TP ok for now.
55226797Speter	 */
55326797Speter	if (!TULIP_IS_MEDIA_TP(sc->tulip_media))
55426797Speter	    return TULIP_LINK_UNKNOWN;
55526797Speter	if ((TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) == 0)
55626797Speter	    linkup = TULIP_LINK_UP;
55730556Speter#if defined(TULIP_DEBUG)
55830556Speter	if (sc->tulip_probe_timeout <= 0)
55949572Speter	    printf("%s%d: sia status = 0x%08x\n", sc->tulip_name,
56049572Speter		    sc->tulip_unit, TULIP_CSR_READ(sc, csr_sia_status));
56130556Speter#endif
56226797Speter    } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
56326797Speter	return TULIP_LINK_UNKNOWN;
56426797Speter    }
56526797Speter    /*
56626797Speter     * We will wait for 3 seconds until the link goes into suspect mode.
56726797Speter     */
56826797Speter    if (sc->tulip_flags & TULIP_LINKUP) {
56926797Speter	if (linkup == TULIP_LINK_UP)
57026797Speter	    sc->tulip_probe_timeout = 3000;
57126797Speter	if (sc->tulip_probe_timeout > 0)
57226797Speter	    return TULIP_LINK_UP;
57318357Sdg
57426797Speter	sc->tulip_flags &= ~TULIP_LINKUP;
57549572Speter	printf("%s%d: link down: cable problem?\n", sc->tulip_name, sc->tulip_unit);
57626797Speter    }
57726797Speter#if defined(TULIP_DEBUG)
57826797Speter    sc->tulip_dbg.dbg_link_downed++;
57916357Sdg#endif
58026797Speter    return TULIP_LINK_DOWN;
58126797Speter}
58226797Speter
58316357Sdgstatic void
58426797Spetertulip_media_poll(
58526797Speter    tulip_softc_t * const sc,
58626797Speter    tulip_mediapoll_event_t event)
58716357Sdg{
58826797Speter#if defined(TULIP_DEBUG)
58926797Speter    sc->tulip_dbg.dbg_events[event]++;
59016357Sdg#endif
59126797Speter    if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE
59226797Speter	    && event == TULIP_MEDIAPOLL_TIMER) {
59326797Speter	switch (tulip_media_link_monitor(sc)) {
59426797Speter	    case TULIP_LINK_DOWN: {
59526797Speter		/*
59626797Speter		 * Link Monitor failed.  Probe for new media.
59726797Speter		 */
59826797Speter		event = TULIP_MEDIAPOLL_LINKFAIL;
59926797Speter		break;
60026797Speter	    }
60126797Speter	    case TULIP_LINK_UP: {
60226797Speter		/*
60326797Speter		 * Check again soon.
60426797Speter		 */
60526797Speter		tulip_timeout(sc);
60626797Speter		return;
60726797Speter	    }
60826797Speter	    case TULIP_LINK_UNKNOWN: {
60926797Speter		/*
61026797Speter		 * We can't tell so don't bother.
61126797Speter		 */
61226797Speter		return;
61326797Speter	    }
61426797Speter	}
61526797Speter    }
61616357Sdg
61726797Speter    if (event == TULIP_MEDIAPOLL_LINKFAIL) {
61826797Speter	if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE) {
61926797Speter	    if (TULIP_DO_AUTOSENSE(sc)) {
62026797Speter#if defined(TULIP_DEBUG)
62126797Speter		sc->tulip_dbg.dbg_link_failures++;
6228754Sdg#endif
62326797Speter		sc->tulip_media = TULIP_MEDIA_UNKNOWN;
62436945Speter		if (sc->tulip_if.if_flags & IFF_UP)
62536945Speter		    tulip_reset(sc);	/* restart probe */
62626797Speter	    }
62726797Speter	    return;
62826797Speter	}
62926797Speter#if defined(TULIP_DEBUG)
63026797Speter	sc->tulip_dbg.dbg_link_pollintrs++;
63126797Speter#endif
63226797Speter    }
6333278Swollman
63426797Speter    if (event == TULIP_MEDIAPOLL_START) {
63526797Speter	sc->tulip_if.if_flags |= IFF_OACTIVE;
63626797Speter	if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE)
63726797Speter	    return;
63826797Speter	sc->tulip_probe_mediamask = 0;
63926797Speter	sc->tulip_probe_passes = 0;
64026797Speter#if defined(TULIP_DEBUG)
64126797Speter	sc->tulip_dbg.dbg_media_probes++;
64216357Sdg#endif
64326797Speter	/*
64426797Speter	 * If the SROM contained an explicit media to use, use it.
64526797Speter	 */
64626797Speter	sc->tulip_cmdmode &= ~(TULIP_CMD_RXRUN|TULIP_CMD_FULLDUPLEX);
64726797Speter	sc->tulip_flags |= TULIP_TRYNWAY|TULIP_PROBE1STPASS;
64826797Speter	sc->tulip_flags &= ~(TULIP_DIDNWAY|TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
64926797Speter	/*
65026797Speter	 * connidx is defaulted to a media_unknown type.
65126797Speter	 */
65226797Speter	sc->tulip_probe_media = tulip_srom_conninfo[sc->tulip_connidx].sc_media;
65326797Speter	if (sc->tulip_probe_media != TULIP_MEDIA_UNKNOWN) {
65426797Speter	    tulip_linkup(sc, sc->tulip_probe_media);
65526797Speter	    tulip_timeout(sc);
65626797Speter	    return;
65726797Speter	}
65816357Sdg
65926797Speter	if (sc->tulip_features & TULIP_HAVE_GPR) {
66026797Speter	    sc->tulip_probe_state = TULIP_PROBE_GPRTEST;
66126797Speter	    sc->tulip_probe_timeout = 2000;
66226797Speter	} else {
66326797Speter	    sc->tulip_probe_media = TULIP_MEDIA_MAX;
66426797Speter	    sc->tulip_probe_timeout = 0;
66526797Speter	    sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
66626797Speter	}
66726797Speter    }
66826797Speter
66926797Speter    /*
67026797Speter     * Ignore txprobe failures or spurious callbacks.
67126797Speter     */
67226797Speter    if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED
67326797Speter	    && sc->tulip_probe_state != TULIP_PROBE_MEDIATEST) {
67426797Speter	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
67526797Speter	return;
67626797Speter    }
67726797Speter
67826797Speter    /*
67926797Speter     * If we really transmitted a packet, then that's the media we'll use.
68026797Speter     */
68126797Speter    if (event == TULIP_MEDIAPOLL_TXPROBE_OK || event == TULIP_MEDIAPOLL_LINKPASS) {
68236945Speter	if (event == TULIP_MEDIAPOLL_LINKPASS) {
68336945Speter	    /* XXX Check media status just to be sure */
68426797Speter	    sc->tulip_probe_media = TULIP_MEDIA_10BASET;
68526797Speter#if defined(TULIP_DEBUG)
68636945Speter	} else {
68726797Speter	    sc->tulip_dbg.dbg_txprobes_ok[sc->tulip_probe_media]++;
68811070Sdg#endif
68936945Speter	}
69026797Speter	tulip_linkup(sc, sc->tulip_probe_media);
69126797Speter	tulip_timeout(sc);
69226797Speter	return;
69326797Speter    }
69411070Sdg
69526797Speter    if (sc->tulip_probe_state == TULIP_PROBE_GPRTEST) {
69626797Speter#if defined(TULIP_DO_GPR_SENSE)
69726797Speter	/*
69826797Speter	 * Check for media via the general purpose register.
69926797Speter	 *
70026797Speter	 * Try to sense the media via the GPR.  If the same value
70126797Speter	 * occurs 3 times in a row then just use that.
70226797Speter	 */
70326797Speter	if (sc->tulip_probe_timeout > 0) {
70426797Speter	    tulip_media_t new_probe_media = tulip_21140_gpr_media_sense(sc);
70526797Speter#if defined(TULIP_DEBUG)
70649572Speter	    printf("%s%d: media_poll: gpr sensing = %s\n",
70749572Speter		   sc->tulip_name, sc->tulip_unit, tulip_mediums[new_probe_media]);
70816357Sdg#endif
70926797Speter	    if (new_probe_media != TULIP_MEDIA_UNKNOWN) {
71026797Speter		if (new_probe_media == sc->tulip_probe_media) {
71126797Speter		    if (--sc->tulip_probe_count == 0)
71226797Speter			tulip_linkup(sc, sc->tulip_probe_media);
71326797Speter		} else {
71426797Speter		    sc->tulip_probe_count = 10;
71526797Speter		}
71626797Speter	    }
71726797Speter	    sc->tulip_probe_media = new_probe_media;
71826797Speter	    tulip_timeout(sc);
71926797Speter	    return;
72026797Speter	}
72126797Speter#endif /* TULIP_DO_GPR_SENSE */
72226797Speter	/*
72326797Speter	 * Brute force.  We cycle through each of the media types
72426797Speter	 * and try to transmit a packet.
72526797Speter	 */
72626797Speter	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
72726797Speter	sc->tulip_probe_media = TULIP_MEDIA_MAX;
72826797Speter	sc->tulip_probe_timeout = 0;
72926797Speter	tulip_timeout(sc);
73026797Speter	return;
73126797Speter    }
7323278Swollman
73326797Speter    if (sc->tulip_probe_state != TULIP_PROBE_MEDIATEST
73426797Speter	   && (sc->tulip_features & TULIP_HAVE_MII)) {
73526797Speter	tulip_media_t old_media = sc->tulip_probe_media;
73626797Speter	tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
73726797Speter	switch (sc->tulip_probe_state) {
73826797Speter	    case TULIP_PROBE_FAILED:
73926797Speter	    case TULIP_PROBE_MEDIATEST: {
74026797Speter		/*
74126797Speter		 * Try the next media.
74226797Speter		 */
74326797Speter		sc->tulip_probe_mediamask |= sc->tulip_mediums[sc->tulip_probe_media]->mi_mediamask;
74426797Speter		sc->tulip_probe_timeout = 0;
74526797Speter#ifdef notyet
74626797Speter		if (sc->tulip_probe_state == TULIP_PROBE_FAILED)
74726797Speter		    break;
74826797Speter		if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
74926797Speter		    break;
75026797Speter		sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 300;
75116357Sdg#endif
75226797Speter		break;
75326797Speter	    }
75426797Speter	    case TULIP_PROBE_PHYAUTONEG: {
75526797Speter		return;
75626797Speter	    }
75726797Speter	    case TULIP_PROBE_INACTIVE: {
75826797Speter		/*
75926797Speter		 * Only probe if we autonegotiated a media that hasn't failed.
76026797Speter		 */
76126797Speter		sc->tulip_probe_timeout = 0;
76226797Speter		if (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media)) {
76326797Speter		    sc->tulip_probe_media = old_media;
76426797Speter		    break;
76526797Speter		}
76626797Speter		tulip_linkup(sc, sc->tulip_probe_media);
76726797Speter		tulip_timeout(sc);
76826797Speter		return;
76926797Speter	    }
77026797Speter	    default: {
77126797Speter#if defined(DIAGNOSTIC) || defined(TULIP_DEBUG)
77226797Speter		panic("tulip_media_poll: botch at line %d\n", __LINE__);
77326797Speter#endif
77426797Speter		break;
77526797Speter	    }
77626797Speter	}
77726797Speter    }
77816357Sdg
77926797Speter    if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED) {
78026797Speter#if defined(TULIP_DEBUG)
78126797Speter	sc->tulip_dbg.dbg_txprobes_failed[sc->tulip_probe_media]++;
78216357Sdg#endif
78326797Speter	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
78426797Speter	return;
78526797Speter    }
78616357Sdg
78726797Speter    /*
78826797Speter     * switch to another media if we tried this one enough.
78926797Speter     */
79026797Speter    if (/* event == TULIP_MEDIAPOLL_TXPROBE_FAILED || */ sc->tulip_probe_timeout <= 0) {
79126797Speter#if defined(TULIP_DEBUG)
79226797Speter	if (sc->tulip_probe_media == TULIP_MEDIA_UNKNOWN) {
79349572Speter	    printf("%s%d: poll media unknown!\n",
79449572Speter		   sc->tulip_name, sc->tulip_unit);
79526797Speter	    sc->tulip_probe_media = TULIP_MEDIA_MAX;
79626797Speter	}
79716357Sdg#endif
79826797Speter	/*
79926797Speter	 * Find the next media type to check for.  Full Duplex
80026797Speter	 * types are not allowed.
80126797Speter	 */
80226797Speter	do {
80326797Speter	    sc->tulip_probe_media -= 1;
80426797Speter	    if (sc->tulip_probe_media == TULIP_MEDIA_UNKNOWN) {
80526797Speter		if (++sc->tulip_probe_passes == 3) {
80649572Speter		    printf("%s%d: autosense failed: cable problem?\n",
80749572Speter			   sc->tulip_name, sc->tulip_unit);
80826797Speter		    if ((sc->tulip_if.if_flags & IFF_UP) == 0) {
80926797Speter			sc->tulip_if.if_flags &= ~IFF_RUNNING;
81026797Speter			sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
81126797Speter			return;
81226797Speter		    }
81326797Speter		}
81426797Speter		sc->tulip_flags ^= TULIP_TRYNWAY;	/* XXX */
81526797Speter		sc->tulip_probe_mediamask = 0;
81626797Speter		sc->tulip_probe_media = TULIP_MEDIA_MAX - 1;
81726797Speter	    }
81826797Speter	} while (sc->tulip_mediums[sc->tulip_probe_media] == NULL
81926797Speter		 || (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media))
82026797Speter		 || TULIP_IS_MEDIA_FD(sc->tulip_probe_media));
82116357Sdg
82226797Speter#if defined(TULIP_DEBUG)
82349572Speter	printf("%s%d: %s: probing %s\n", sc->tulip_name, sc->tulip_unit,
82426797Speter	       event == TULIP_MEDIAPOLL_TXPROBE_FAILED ? "txprobe failed" : "timeout",
82526797Speter	       tulip_mediums[sc->tulip_probe_media]);
82616357Sdg#endif
82726797Speter	sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 1000;
82826797Speter	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
82926797Speter	sc->tulip_probe.probe_txprobes = 0;
83026797Speter	tulip_reset(sc);
83126797Speter	tulip_media_set(sc, sc->tulip_probe_media);
83226797Speter	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
83326797Speter    }
83426797Speter    tulip_timeout(sc);
83516357Sdg
83626797Speter    /*
83726797Speter     * If this is hanging off a phy, we know are doing NWAY and we have
83826797Speter     * forced the phy to a specific speed.  Wait for link up before
83926797Speter     * before sending a packet.
84026797Speter     */
84126797Speter    switch (sc->tulip_mediums[sc->tulip_probe_media]->mi_type) {
84226797Speter	case TULIP_MEDIAINFO_MII: {
84326797Speter	    if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
84426797Speter		return;
84526797Speter	    break;
84626797Speter	}
84726797Speter	case TULIP_MEDIAINFO_SIA: {
84826797Speter	    if (TULIP_IS_MEDIA_TP(sc->tulip_probe_media)) {
84926797Speter		if (TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL)
85026797Speter		    return;
85126797Speter		tulip_linkup(sc, sc->tulip_probe_media);
85226797Speter#ifdef notyet
85326797Speter		if (sc->tulip_features & TULIP_HAVE_MII)
85426797Speter		    tulip_timeout(sc);
85516357Sdg#endif
85626797Speter		return;
85726797Speter	    }
85826797Speter	    break;
85926797Speter	}
86026797Speter	case TULIP_MEDIAINFO_RESET:
86126797Speter	case TULIP_MEDIAINFO_SYM:
86230556Speter	case TULIP_MEDIAINFO_NONE:
86326797Speter	case TULIP_MEDIAINFO_GPR: {
86426797Speter	    break;
86526797Speter	}
86626797Speter    }
86726797Speter    /*
86826797Speter     * Try to send a packet.
86926797Speter     */
87026797Speter    tulip_txprobe(sc);
87126797Speter}
8727791Sdg
87326797Speterstatic void
87426797Spetertulip_media_select(
8758754Sdg    tulip_softc_t * const sc)
8767791Sdg{
87726797Speter    if (sc->tulip_features & TULIP_HAVE_GPR) {
87826797Speter	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
87926797Speter	DELAY(10);
88026797Speter	TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpdata);
88126797Speter    }
88226797Speter    /*
88326797Speter     * If this board has no media, just return
88426797Speter     */
88526797Speter    if (sc->tulip_features & TULIP_HAVE_NOMEDIA)
88626797Speter	return;
8877791Sdg
88826797Speter    if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
88926797Speter	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
89026797Speter	(*sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_START);
89126797Speter    } else {
89226797Speter	tulip_media_set(sc, sc->tulip_media);
8937791Sdg    }
8947791Sdg}
89526797Speter
8963278Swollmanstatic void
89726797Spetertulip_21040_mediainfo_init(
89826797Speter    tulip_softc_t * const sc,
89926797Speter    tulip_media_t media)
9007791Sdg{
90112341Sdg    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_THRSHLD160
90212341Sdg	|TULIP_CMD_BACKOFFCTR;
90326797Speter    sc->tulip_if.if_baudrate = 10000000;
90426797Speter
90526797Speter    if (media == TULIP_MEDIA_10BASET || media == TULIP_MEDIA_UNKNOWN) {
90626797Speter	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[0], 21040, 10BASET);
90726797Speter	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[1], 21040, 10BASET_FD);
90836945Speter	sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
9097791Sdg    }
91026797Speter
91126797Speter    if (media == TULIP_MEDIA_AUIBNC || media == TULIP_MEDIA_UNKNOWN) {
91226797Speter	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[2], 21040, AUIBNC);
91326797Speter    }
91426797Speter
91526797Speter    if (media == TULIP_MEDIA_UNKNOWN) {
91626797Speter	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[3], 21040, EXTSIA);
91726797Speter    }
9187791Sdg}
9197791Sdg
92026797Speterstatic void
92126797Spetertulip_21040_media_probe(
92226797Speter    tulip_softc_t * const sc)
92326797Speter{
92426797Speter    tulip_21040_mediainfo_init(sc, TULIP_MEDIA_UNKNOWN);
92526797Speter    return;
92626797Speter}
92726797Speter
92826797Speterstatic void
92920060Srgrimestulip_21040_10baset_only_media_probe(
93011070Sdg    tulip_softc_t * const sc)
93111070Sdg{
93226797Speter    tulip_21040_mediainfo_init(sc, TULIP_MEDIA_10BASET);
93326797Speter    tulip_media_set(sc, TULIP_MEDIA_10BASET);
93426797Speter    sc->tulip_media = TULIP_MEDIA_10BASET;
93511070Sdg}
93611070Sdg
93711070Sdgstatic void
93820060Srgrimestulip_21040_10baset_only_media_select(
93911070Sdg    tulip_softc_t * const sc)
94011070Sdg{
94116357Sdg    sc->tulip_flags |= TULIP_LINKUP;
94226797Speter    if (sc->tulip_media == TULIP_MEDIA_10BASET_FD) {
94316357Sdg	sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
94416357Sdg	sc->tulip_flags &= ~TULIP_SQETEST;
94516357Sdg    } else {
94616357Sdg	sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
94716357Sdg	sc->tulip_flags |= TULIP_SQETEST;
94816357Sdg    }
94926797Speter    tulip_media_set(sc, sc->tulip_media);
95011070Sdg}
95111070Sdg
95226797Speterstatic void
95320060Srgrimestulip_21040_auibnc_only_media_probe(
95416357Sdg    tulip_softc_t * const sc)
95516357Sdg{
95626797Speter    tulip_21040_mediainfo_init(sc, TULIP_MEDIA_AUIBNC);
95716357Sdg    sc->tulip_flags |= TULIP_SQETEST|TULIP_LINKUP;
95826797Speter    tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
95926797Speter    sc->tulip_media = TULIP_MEDIA_AUIBNC;
96016357Sdg}
96111070Sdg
96216357Sdgstatic void
96320060Srgrimestulip_21040_auibnc_only_media_select(
96416357Sdg    tulip_softc_t * const sc)
96516357Sdg{
96626797Speter    tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
96716357Sdg    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
96816357Sdg}
96916357Sdg
97020060Srgrimesstatic const tulip_boardsw_t tulip_21040_boardsw = {
97120060Srgrimes    TULIP_21040_GENERIC,
97220060Srgrimes    tulip_21040_media_probe,
97326797Speter    tulip_media_select,
97426797Speter    tulip_media_poll,
97516357Sdg};
97616357Sdg
97720060Srgrimesstatic const tulip_boardsw_t tulip_21040_10baset_only_boardsw = {
97820060Srgrimes    TULIP_21040_GENERIC,
97920060Srgrimes    tulip_21040_10baset_only_media_probe,
98020060Srgrimes    tulip_21040_10baset_only_media_select,
98116357Sdg    NULL,
98216357Sdg};
98316357Sdg
98420060Srgrimesstatic const tulip_boardsw_t tulip_21040_auibnc_only_boardsw = {
98520060Srgrimes    TULIP_21040_GENERIC,
98620060Srgrimes    tulip_21040_auibnc_only_media_probe,
98720060Srgrimes    tulip_21040_auibnc_only_media_select,
98816357Sdg    NULL,
98916357Sdg};
99026797Speter
99126797Speterstatic void
99226797Spetertulip_21041_mediainfo_init(
99326797Speter    tulip_softc_t * const sc)
99426797Speter{
99526797Speter    tulip_media_info_t * const mi = sc->tulip_mediainfo;
99616357Sdg
99726797Speter#ifdef notyet
99826797Speter    if (sc->tulip_revinfo >= 0x20) {
99926797Speter	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, 10BASET);
100026797Speter	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, 10BASET_FD);
100126797Speter	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, AUI);
100226797Speter	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, BNC);
100326797Speter	return;
100426797Speter    }
100526797Speter#endif
100626797Speter    TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041, 10BASET);
100726797Speter    TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041, 10BASET_FD);
100826797Speter    TULIP_MEDIAINFO_SIA_INIT(sc, &mi[2], 21041, AUI);
100926797Speter    TULIP_MEDIAINFO_SIA_INIT(sc, &mi[3], 21041, BNC);
101026797Speter}
101111070Sdg
101216357Sdgstatic void
101326797Spetertulip_21041_media_probe(
101416357Sdg    tulip_softc_t * const sc)
101516357Sdg{
101626797Speter    sc->tulip_if.if_baudrate = 10000000;
101726797Speter    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_ENHCAPTEFFCT
101826797Speter	|TULIP_CMD_THRSHLD160|TULIP_CMD_BACKOFFCTR;
101936945Speter    sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
102026797Speter    tulip_21041_mediainfo_init(sc);
102126797Speter}
102216357Sdg
102326797Speterstatic void
102426797Spetertulip_21041_media_poll(
102526797Speter    tulip_softc_t * const sc,
102626797Speter    const tulip_mediapoll_event_t event)
102726797Speter{
102826797Speter    u_int32_t sia_status;
102926797Speter
103026797Speter#if defined(TULIP_DEBUG)
103126797Speter    sc->tulip_dbg.dbg_events[event]++;
103226797Speter#endif
103326797Speter
103426797Speter    if (event == TULIP_MEDIAPOLL_LINKFAIL) {
103526797Speter	if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE
103626797Speter		|| !TULIP_DO_AUTOSENSE(sc))
103726797Speter	    return;
103826797Speter	sc->tulip_media = TULIP_MEDIA_UNKNOWN;
103926797Speter	tulip_reset(sc);	/* start probe */
104026797Speter	return;
104126797Speter    }
104226797Speter
104326797Speter    /*
104426797Speter     * If we've been been asked to start a poll or link change interrupt
104526797Speter     * restart the probe (and reset the tulip to a known state).
104626797Speter     */
104726797Speter    if (event == TULIP_MEDIAPOLL_START) {
104826797Speter	sc->tulip_if.if_flags |= IFF_OACTIVE;
104926797Speter	sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_RXRUN);
105026797Speter#ifdef notyet
105126797Speter	if (sc->tulip_revinfo >= 0x20) {
105226797Speter	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
105326797Speter	    sc->tulip_flags |= TULIP_DIDNWAY;
105416357Sdg	}
105526797Speter#endif
105626797Speter	TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
105726797Speter	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
105826797Speter	sc->tulip_probe_media = TULIP_MEDIA_10BASET;
105926797Speter	sc->tulip_probe_timeout = TULIP_21041_PROBE_10BASET_TIMEOUT;
106026797Speter	tulip_media_set(sc, TULIP_MEDIA_10BASET);
106126797Speter	tulip_timeout(sc);
106226797Speter	return;
106326797Speter    }
106426797Speter
106526797Speter    if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
106626797Speter	return;
106726797Speter
106826797Speter    if (event == TULIP_MEDIAPOLL_TXPROBE_OK) {
106926797Speter#if defined(TULIP_DEBUG)
107026797Speter	sc->tulip_dbg.dbg_txprobes_ok[sc->tulip_probe_media]++;
107126797Speter#endif
107226797Speter	tulip_linkup(sc, sc->tulip_probe_media);
107326797Speter	return;
107426797Speter    }
107526797Speter
107626797Speter    sia_status = TULIP_CSR_READ(sc, csr_sia_status);
107726797Speter    TULIP_CSR_WRITE(sc, csr_sia_status, sia_status);
107826797Speter    if ((sia_status & TULIP_SIASTS_LINKFAIL) == 0) {
107926797Speter	if (sc->tulip_revinfo >= 0x20) {
108026797Speter	    if (sia_status & (PHYSTS_10BASET_FD << (16 - 6)))
108126797Speter		sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
108216357Sdg	}
108326797Speter	/*
108426797Speter	 * If the link has passed LinkPass, 10baseT is the
108526797Speter	 * proper media to use.
108626797Speter	 */
108726797Speter	tulip_linkup(sc, sc->tulip_probe_media);
108826797Speter	return;
108926797Speter    }
109026797Speter
109126797Speter    /*
109226797Speter     * wait for up to 2.4 seconds for the link to reach pass state.
109326797Speter     * Only then start scanning the other media for activity.
109426797Speter     * choose media with receive activity over those without.
109526797Speter     */
109626797Speter    if (sc->tulip_probe_media == TULIP_MEDIA_10BASET) {
109726797Speter	if (event != TULIP_MEDIAPOLL_TIMER)
109826797Speter	    return;
109926797Speter	if (sc->tulip_probe_timeout > 0
110026797Speter		&& (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) == 0) {
110126797Speter	    tulip_timeout(sc);
110226797Speter	    return;
110316357Sdg	}
110426797Speter	sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
110526797Speter	sc->tulip_flags |= TULIP_WANTRXACT;
110626797Speter	if (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) {
110726797Speter	    sc->tulip_probe_media = TULIP_MEDIA_BNC;
110826797Speter	} else {
110926797Speter	    sc->tulip_probe_media = TULIP_MEDIA_AUI;
111026797Speter	}
111126797Speter	tulip_media_set(sc, sc->tulip_probe_media);
111226797Speter	tulip_timeout(sc);
111326797Speter	return;
111426797Speter    }
111516357Sdg
111626797Speter    /*
111726797Speter     * If we failed, clear the txprobe active flag.
111826797Speter     */
111926797Speter    if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED)
112026797Speter	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
112126797Speter
112226797Speter
112326797Speter    if (event == TULIP_MEDIAPOLL_TIMER) {
112426797Speter	/*
112526797Speter	 * If we've received something, then that's our link!
112626797Speter	 */
112726797Speter	if (sc->tulip_flags & TULIP_RXACT) {
112826797Speter	    tulip_linkup(sc, sc->tulip_probe_media);
112926797Speter	    return;
113016357Sdg	}
113126797Speter	/*
113226797Speter	 * if no txprobe active
113326797Speter	 */
113426797Speter	if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0
113526797Speter		&& ((sc->tulip_flags & TULIP_WANTRXACT) == 0
113626797Speter		    || (sia_status & TULIP_SIASTS_RXACTIVITY))) {
113726797Speter	    sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
113826797Speter	    tulip_txprobe(sc);
113926797Speter	    tulip_timeout(sc);
114026797Speter	    return;
114126797Speter	}
114226797Speter	/*
114326797Speter	 * Take 2 passes through before deciding to not
114426797Speter	 * wait for receive activity.  Then take another
114526797Speter	 * two passes before spitting out a warning.
114626797Speter	 */
114726797Speter	if (sc->tulip_probe_timeout <= 0) {
114826797Speter	    if (sc->tulip_flags & TULIP_WANTRXACT) {
114926797Speter		sc->tulip_flags &= ~TULIP_WANTRXACT;
115026797Speter		sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
115126797Speter	    } else {
115249572Speter		printf("%s%d: autosense failed: cable problem?\n",
115349572Speter		       sc->tulip_name, sc->tulip_unit);
115426797Speter		if ((sc->tulip_if.if_flags & IFF_UP) == 0) {
115526797Speter		    sc->tulip_if.if_flags &= ~IFF_RUNNING;
115626797Speter		    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
115726797Speter		    return;
115826797Speter		}
115916357Sdg	    }
116016357Sdg	}
116116357Sdg    }
116226797Speter
116326797Speter    /*
116426797Speter     * Since this media failed to probe, try the other one.
116526797Speter     */
116626797Speter    sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
116726797Speter    if (sc->tulip_probe_media == TULIP_MEDIA_AUI) {
116826797Speter	sc->tulip_probe_media = TULIP_MEDIA_BNC;
116926797Speter    } else {
117026797Speter	sc->tulip_probe_media = TULIP_MEDIA_AUI;
117126797Speter    }
117226797Speter    tulip_media_set(sc, sc->tulip_probe_media);
117326797Speter    sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
117426797Speter    tulip_timeout(sc);
117516357Sdg}
117626797Speter
117726797Speterstatic const tulip_boardsw_t tulip_21041_boardsw = {
117826797Speter    TULIP_21041_GENERIC,
117926797Speter    tulip_21041_media_probe,
118026797Speter    tulip_media_select,
118126797Speter    tulip_21041_media_poll
118226797Speter};
118316357Sdg
118426797Speterstatic const tulip_phy_attr_t tulip_mii_phy_attrlist[] = {
118526797Speter    { 0x20005c00, 0,		/* 08-00-17 */
118626797Speter      {
118726797Speter	{ 0x19, 0x0040, 0x0040 },	/* 10TX */
118826797Speter	{ 0x19, 0x0040, 0x0000 },	/* 100TX */
118926797Speter      },
119026797Speter#if defined(TULIP_DEBUG)
119126797Speter      "NS DP83840",
119216357Sdg#endif
119326797Speter    },
119426797Speter    { 0x0281F400, 0,		/* 00-A0-7D */
119526797Speter      {
119626797Speter	{ 0x12, 0x0010, 0x0000 },	/* 10T */
119726797Speter	{ },				/* 100TX */
119826797Speter	{ 0x12, 0x0010, 0x0010 },	/* 100T4 */
119926797Speter	{ 0x12, 0x0008, 0x0008 },	/* FULL_DUPLEX */
120026797Speter      },
120126797Speter#if defined(TULIP_DEBUG)
120226797Speter      "Seeq 80C240"
120316357Sdg#endif
120426797Speter    },
120516357Sdg#if 0
120626797Speter    { 0x0015F420, 0,	/* 00-A0-7D */
120726797Speter      {
120826797Speter	{ 0x12, 0x0010, 0x0000 },	/* 10T */
120926797Speter	{ },				/* 100TX */
121026797Speter	{ 0x12, 0x0010, 0x0010 },	/* 100T4 */
121126797Speter	{ 0x12, 0x0008, 0x0008 },	/* FULL_DUPLEX */
121226797Speter      },
121326797Speter#if defined(TULIP_DEBUG)
121426797Speter      "Broadcom BCM5000"
121516357Sdg#endif
121626797Speter    },
121726797Speter#endif
121826797Speter    { 0x0281F400, 0,		/* 00-A0-BE */
121926797Speter      {
122026797Speter	{ 0x11, 0x8000, 0x0000 },	/* 10T */
122126797Speter	{ 0x11, 0x8000, 0x8000 },	/* 100TX */
122226797Speter	{ },				/* 100T4 */
122326797Speter	{ 0x11, 0x4000, 0x4000 },	/* FULL_DUPLEX */
122426797Speter      },
122526797Speter#if defined(TULIP_DEBUG)
122626797Speter      "ICS 1890"
122726797Speter#endif
122826797Speter    },
122926797Speter    { 0 }
123026797Speter};
123126797Speter
123226797Speterstatic tulip_media_t
123326797Spetertulip_mii_phy_readspecific(
123426797Speter    tulip_softc_t * const sc)
123526797Speter{
123626797Speter    const tulip_phy_attr_t *attr;
123726797Speter    u_int16_t data;
123826797Speter    u_int32_t id;
123926797Speter    unsigned idx = 0;
124026797Speter    static const tulip_media_t table[] = {
124126797Speter	TULIP_MEDIA_UNKNOWN,
124226797Speter	TULIP_MEDIA_10BASET,
124326797Speter	TULIP_MEDIA_100BASETX,
124426797Speter	TULIP_MEDIA_100BASET4,
124526797Speter	TULIP_MEDIA_UNKNOWN,
124626797Speter	TULIP_MEDIA_10BASET_FD,
124726797Speter	TULIP_MEDIA_100BASETX_FD,
124826797Speter	TULIP_MEDIA_UNKNOWN
124926797Speter    };
125026797Speter
125126797Speter    /*
125226797Speter     * Don't read phy specific registers if link is not up.
125326797Speter     */
125426797Speter    data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
125526797Speter    if ((data & (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS)) != (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS))
125626797Speter	return TULIP_MEDIA_UNKNOWN;
125726797Speter
125826797Speter    id = (tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDLOW) << 16) |
125926797Speter	tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDHIGH);
126026797Speter    for (attr = tulip_mii_phy_attrlist;; attr++) {
126126797Speter	if (attr->attr_id == 0)
126226797Speter	    return TULIP_MEDIA_UNKNOWN;
126326797Speter	if ((id & ~0x0F) == attr->attr_id)
126426797Speter	    break;
126516357Sdg    }
126626797Speter
126726797Speter    if (attr->attr_modes[PHY_MODE_100TX].pm_regno) {
126826797Speter	const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100TX];
126926797Speter	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
127026797Speter	if ((data & pm->pm_mask) == pm->pm_value)
127126797Speter	    idx = 2;
127226797Speter    }
127326797Speter    if (idx == 0 && attr->attr_modes[PHY_MODE_100T4].pm_regno) {
127426797Speter	const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100T4];
127526797Speter	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
127626797Speter	if ((data & pm->pm_mask) == pm->pm_value)
127726797Speter	    idx = 3;
127826797Speter    }
127926797Speter    if (idx == 0 && attr->attr_modes[PHY_MODE_10T].pm_regno) {
128026797Speter	const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_10T];
128126797Speter	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
128226797Speter	if ((data & pm->pm_mask) == pm->pm_value)
128326797Speter	    idx = 1;
128426797Speter    }
128526797Speter    if (idx != 0 && attr->attr_modes[PHY_MODE_FULLDUPLEX].pm_regno) {
128626797Speter	const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_FULLDUPLEX];
128726797Speter	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
128826797Speter	idx += ((data & pm->pm_mask) == pm->pm_value ? 4 : 0);
128926797Speter    }
129026797Speter    return table[idx];
129116357Sdg}
129226797Speter
129326797Speterstatic unsigned
129426797Spetertulip_mii_get_phyaddr(
129526797Speter    tulip_softc_t * const sc,
129626797Speter    unsigned offset)
129726797Speter{
129826797Speter    unsigned phyaddr;
129916357Sdg
130026797Speter    for (phyaddr = 1; phyaddr < 32; phyaddr++) {
130126797Speter	unsigned status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
130226797Speter	if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
130326797Speter	    continue;
130426797Speter	if (offset == 0)
130526797Speter	    return phyaddr;
130626797Speter	offset--;
130726797Speter    }
130826797Speter    if (offset == 0) {
130926797Speter	unsigned status = tulip_mii_readreg(sc, 0, PHYREG_STATUS);
131026797Speter	if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
131126797Speter	    return TULIP_MII_NOPHY;
131226797Speter	return 0;
131326797Speter    }
131426797Speter    return TULIP_MII_NOPHY;
131526797Speter}
131626797Speter
131711070Sdgstatic int
131826797Spetertulip_mii_map_abilities(
131916357Sdg    tulip_softc_t * const sc,
132016357Sdg    unsigned abilities)
132116357Sdg{
132216357Sdg    sc->tulip_abilities = abilities;
132316357Sdg    if (abilities & PHYSTS_100BASETX_FD) {
132426797Speter	sc->tulip_probe_media = TULIP_MEDIA_100BASETX_FD;
132526797Speter    } else if (abilities & PHYSTS_100BASET4) {
132626797Speter	sc->tulip_probe_media = TULIP_MEDIA_100BASET4;
132716357Sdg    } else if (abilities & PHYSTS_100BASETX) {
132826797Speter	sc->tulip_probe_media = TULIP_MEDIA_100BASETX;
132916357Sdg    } else if (abilities & PHYSTS_10BASET_FD) {
133026797Speter	sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
133116357Sdg    } else if (abilities & PHYSTS_10BASET) {
133226797Speter	sc->tulip_probe_media = TULIP_MEDIA_10BASET;
133316357Sdg    } else {
133416357Sdg	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
133526797Speter	return 0;
133616357Sdg    }
133716357Sdg    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
133826797Speter    return 1;
133916357Sdg}
134016357Sdg
134116357Sdgstatic void
134226797Spetertulip_mii_autonegotiate(
134316357Sdg    tulip_softc_t * const sc,
134426797Speter    const unsigned phyaddr)
134516357Sdg{
134616357Sdg    switch (sc->tulip_probe_state) {
134726797Speter        case TULIP_PROBE_MEDIATEST:
134816357Sdg        case TULIP_PROBE_INACTIVE: {
134926797Speter	    sc->tulip_flags |= TULIP_DIDNWAY;
135026797Speter	    tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, PHYCTL_RESET);
135126797Speter	    sc->tulip_probe_timeout = 3000;
135226797Speter	    sc->tulip_intrmask |= TULIP_STS_ABNRMLINTR|TULIP_STS_NORMALINTR;
135316357Sdg	    sc->tulip_probe_state = TULIP_PROBE_PHYRESET;
135426797Speter	    /* FALL THROUGH */
135516357Sdg	}
135616357Sdg        case TULIP_PROBE_PHYRESET: {
135726797Speter	    u_int32_t status;
135826797Speter	    u_int32_t data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
135916357Sdg	    if (data & PHYCTL_RESET) {
136026797Speter		if (sc->tulip_probe_timeout > 0) {
136126797Speter		    tulip_timeout(sc);
136216357Sdg		    return;
136316357Sdg		}
136449572Speter		printf("%s%d(phy%d): error: reset of PHY never completed!\n",
136549572Speter			   sc->tulip_name, sc->tulip_unit, phyaddr);
136616357Sdg		sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
136716357Sdg		sc->tulip_probe_state = TULIP_PROBE_FAILED;
136816357Sdg		sc->tulip_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
136916357Sdg		return;
137016357Sdg	    }
137126797Speter	    status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
137226797Speter	    if ((status & PHYSTS_CAN_AUTONEG) == 0) {
137326797Speter#if defined(TULIP_DEBUG)
137449572Speter		loudprintf("%s%d(phy%d): autonegotiation disabled\n",
137549572Speter			   sc->tulip_name, sc->tulip_unit, phyaddr);
137616357Sdg#endif
137726797Speter		sc->tulip_flags &= ~TULIP_DIDNWAY;
137816357Sdg		sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
137916357Sdg		return;
138016357Sdg	    }
138126797Speter	    if (tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT) != ((status >> 6) | 0x01))
138226797Speter		tulip_mii_writereg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT, (status >> 6) | 0x01);
138326797Speter	    tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, data|PHYCTL_AUTONEG_RESTART|PHYCTL_AUTONEG_ENABLE);
138426797Speter	    data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
138526797Speter#if defined(TULIP_DEBUG)
138616357Sdg	    if ((data & PHYCTL_AUTONEG_ENABLE) == 0)
138749572Speter		loudprintf("%s%d(phy%d): oops: enable autonegotiation failed: 0x%04x\n",
138849572Speter			   sc->tulip_name, sc->tulip_unit, phyaddr, data);
138916357Sdg	    else
139049572Speter		loudprintf("%s%d(phy%d): autonegotiation restarted: 0x%04x\n",
139149572Speter			   sc->tulip_name, sc->tulip_unit, phyaddr, data);
139226797Speter	    sc->tulip_dbg.dbg_nway_starts++;
139316357Sdg#endif
139416357Sdg	    sc->tulip_probe_state = TULIP_PROBE_PHYAUTONEG;
139526797Speter	    sc->tulip_probe_timeout = 3000;
139626797Speter	    /* FALL THROUGH */
139716357Sdg	}
139816357Sdg        case TULIP_PROBE_PHYAUTONEG: {
139926797Speter	    u_int32_t status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
140026797Speter	    u_int32_t data;
140126797Speter	    if ((status & PHYSTS_AUTONEG_DONE) == 0) {
140226797Speter		if (sc->tulip_probe_timeout > 0) {
140326797Speter		    tulip_timeout(sc);
140416357Sdg		    return;
140516357Sdg		}
140626797Speter#if defined(TULIP_DEBUG)
140749572Speter		loudprintf("%s%d(phy%d): autonegotiation timeout: sts=0x%04x, ctl=0x%04x\n",
140849572Speter			   sc->tulip_name, sc->tulip_unit, phyaddr, status,
140926797Speter			   tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL));
141016357Sdg#endif
141126797Speter		sc->tulip_flags &= ~TULIP_DIDNWAY;
141216357Sdg		sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
141316357Sdg		return;
141416357Sdg	    }
141526797Speter	    data = tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ABILITIES);
141626797Speter#if defined(TULIP_DEBUG)
141749572Speter	    loudprintf("%s%d(phy%d): autonegotiation complete: 0x%04x\n",
141849572Speter		       sc->tulip_name, sc->tulip_unit, phyaddr, data);
141916357Sdg#endif
142026797Speter	    data = (data << 6) & status;
142126797Speter	    if (!tulip_mii_map_abilities(sc, data))
142226797Speter		sc->tulip_flags &= ~TULIP_DIDNWAY;
142316357Sdg	    return;
142416357Sdg	}
142526797Speter	default: {
142626797Speter#if defined(DIAGNOSTIC)
142726797Speter	    panic("tulip_media_poll: botch at line %d\n", __LINE__);
142826797Speter#endif
142926797Speter	    break;
143026797Speter	}
143116357Sdg    }
143226797Speter#if defined(TULIP_DEBUG)
143349572Speter    loudprintf("%s%d(phy%d): autonegotiation failure: state = %d\n",
143449572Speter	       sc->tulip_name, sc->tulip_unit, phyaddr, sc->tulip_probe_state);
143526797Speter	    sc->tulip_dbg.dbg_nway_failures++;
143616357Sdg#endif
143716357Sdg}
143816357Sdg
143916357Sdgstatic void
144026797Spetertulip_2114x_media_preset(
144126797Speter    tulip_softc_t * const sc)
144216357Sdg{
144326797Speter    const tulip_media_info_t *mi = NULL;
144426797Speter    tulip_media_t media = sc->tulip_media;
144516357Sdg
144626797Speter    if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
144726797Speter	media = sc->tulip_media;
144826797Speter    else
144926797Speter	media = sc->tulip_probe_media;
145026797Speter
145126797Speter    sc->tulip_cmdmode &= ~TULIP_CMD_PORTSELECT;
145226797Speter    sc->tulip_flags &= ~TULIP_SQETEST;
145330556Speter    if (media != TULIP_MEDIA_UNKNOWN && media != TULIP_MEDIA_MAX) {
145426797Speter#if defined(TULIP_DEBUG)
145526797Speter	if (media < TULIP_MEDIA_MAX && sc->tulip_mediums[media] != NULL) {
145616357Sdg#endif
145726797Speter	    mi = sc->tulip_mediums[media];
145826797Speter	    if (mi->mi_type == TULIP_MEDIAINFO_MII) {
145926797Speter		sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
146026797Speter	    } else if (mi->mi_type == TULIP_MEDIAINFO_GPR
146126797Speter		       || mi->mi_type == TULIP_MEDIAINFO_SYM) {
146226797Speter		sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
146326797Speter		sc->tulip_cmdmode |= mi->mi_cmdmode;
146426797Speter	    } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
146526797Speter		TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
146616357Sdg	    }
146726797Speter#if defined(TULIP_DEBUG)
146826797Speter	} else {
146949572Speter	    printf("%s%d: preset: bad media %d!\n",
147049572Speter		   sc->tulip_name, sc->tulip_unit, media);
147116357Sdg	}
147216357Sdg#endif
147316357Sdg    }
147426797Speter    switch (media) {
147526797Speter	case TULIP_MEDIA_BNC:
147626797Speter	case TULIP_MEDIA_AUI:
147726797Speter	case TULIP_MEDIA_10BASET: {
147826797Speter	    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
147926797Speter	    sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
148026797Speter	    sc->tulip_if.if_baudrate = 10000000;
148116357Sdg	    sc->tulip_flags |= TULIP_SQETEST;
148226797Speter	    break;
148326797Speter	}
148426797Speter	case TULIP_MEDIA_10BASET_FD: {
148526797Speter	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL;
148626797Speter	    sc->tulip_if.if_baudrate = 10000000;
148726797Speter	    break;
148826797Speter	}
148926797Speter	case TULIP_MEDIA_100BASEFX:
149026797Speter	case TULIP_MEDIA_100BASET4:
149126797Speter	case TULIP_MEDIA_100BASETX: {
149226797Speter	    sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL);
149326797Speter	    sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
149426797Speter	    sc->tulip_if.if_baudrate = 100000000;
149526797Speter	    break;
149626797Speter	}
149726797Speter	case TULIP_MEDIA_100BASEFX_FD:
149826797Speter	case TULIP_MEDIA_100BASETX_FD: {
149926797Speter	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_PORTSELECT;
150026797Speter	    sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
150126797Speter	    sc->tulip_if.if_baudrate = 100000000;
150226797Speter	    break;
150326797Speter	}
150426797Speter	default: {
150526797Speter	    break;
150626797Speter	}
150716357Sdg    }
150816357Sdg    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
150916357Sdg}
151026797Speter
151126797Speter/*
151226797Speter ********************************************************************
151326797Speter *  Start of 21140/21140A support which does not use the MII interface
151426797Speter */
151526797Speter
151616357Sdgstatic void
151726797Spetertulip_null_media_poll(
151826797Speter    tulip_softc_t * const sc,
151926797Speter    tulip_mediapoll_event_t event)
152016357Sdg{
152126797Speter#if defined(TULIP_DEBUG)
152226797Speter    sc->tulip_dbg.dbg_events[event]++;
152326797Speter#endif
152426797Speter#if defined(DIAGNOSTIC)
152549572Speter    printf("%s%d: botch(media_poll) at line %d\n",
152649572Speter	   sc->tulip_name, sc->tulip_unit, __LINE__);
152726797Speter#endif
152816357Sdg}
152916357Sdg
153026797Speter__inline__ static void
153126797Spetertulip_21140_mediainit(
153226797Speter    tulip_softc_t * const sc,
153326797Speter    tulip_media_info_t * const mip,
153426797Speter    tulip_media_t const media,
153526797Speter    unsigned gpdata,
153626797Speter    unsigned cmdmode)
153716357Sdg{
153826797Speter    sc->tulip_mediums[media] = mip;
153926797Speter    mip->mi_type = TULIP_MEDIAINFO_GPR;
154026797Speter    mip->mi_cmdmode = cmdmode;
154126797Speter    mip->mi_gpdata = gpdata;
154216357Sdg}
154316357Sdg
154426797Speterstatic void
154520060Srgrimestulip_21140_evalboard_media_probe(
15468754Sdg    tulip_softc_t * const sc)
15477791Sdg{
154826797Speter    tulip_media_info_t *mip = sc->tulip_mediainfo;
154926797Speter
155026797Speter    sc->tulip_gpinit = TULIP_GP_EB_PINS;
155126797Speter    sc->tulip_gpdata = TULIP_GP_EB_INIT;
155216357Sdg    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
155316357Sdg    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
155416357Sdg    TULIP_CSR_WRITE(sc, csr_command,
155516357Sdg	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
15568754Sdg	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
155716357Sdg    TULIP_CSR_WRITE(sc, csr_command,
155816357Sdg	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
15597791Sdg    DELAY(1000000);
156026797Speter    if ((TULIP_CSR_READ(sc, csr_gp) & TULIP_GP_EB_OK100) != 0) {
156126797Speter	sc->tulip_media = TULIP_MEDIA_10BASET;
156226797Speter    } else {
156316357Sdg	sc->tulip_media = TULIP_MEDIA_100BASETX;
15647791Sdg    }
156526797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
156626797Speter			  TULIP_GP_EB_INIT,
156726797Speter			  TULIP_CMD_TXTHRSHLDCTL);
156826797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
156926797Speter			  TULIP_GP_EB_INIT,
157026797Speter			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
157126797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
157226797Speter			  TULIP_GP_EB_INIT,
157326797Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
157426797Speter			      |TULIP_CMD_SCRAMBLER);
157526797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
157626797Speter			  TULIP_GP_EB_INIT,
157726797Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
157826797Speter			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
15797791Sdg}
15807791Sdg
158120060Srgrimesstatic const tulip_boardsw_t tulip_21140_eb_boardsw = {
158220060Srgrimes    TULIP_21140_DEC_EB,
158320060Srgrimes    tulip_21140_evalboard_media_probe,
158426797Speter    tulip_media_select,
158526797Speter    tulip_null_media_poll,
158626797Speter    tulip_2114x_media_preset,
15877791Sdg};
15887791Sdg
158926797Speterstatic void
159030556Spetertulip_21140_accton_media_probe(
159130556Speter    tulip_softc_t * const sc)
159230556Speter{
159330556Speter    tulip_media_info_t *mip = sc->tulip_mediainfo;
159430556Speter    unsigned gpdata;
159530556Speter
159630556Speter    sc->tulip_gpinit = TULIP_GP_EB_PINS;
159730556Speter    sc->tulip_gpdata = TULIP_GP_EB_INIT;
159830556Speter    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
159930556Speter    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
160030556Speter    TULIP_CSR_WRITE(sc, csr_command,
160130556Speter	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
160230556Speter	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
160330556Speter    TULIP_CSR_WRITE(sc, csr_command,
160430556Speter	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
160530556Speter    DELAY(1000000);
160630556Speter    gpdata = TULIP_CSR_READ(sc, csr_gp);
160730556Speter    if ((gpdata & TULIP_GP_EN1207_UTP_INIT) == 0) {
160830556Speter	sc->tulip_media = TULIP_MEDIA_10BASET;
160930556Speter    } else {
161030556Speter	if ((gpdata & TULIP_GP_EN1207_BNC_INIT) == 0) {
161130556Speter		sc->tulip_media = TULIP_MEDIA_BNC;
161230556Speter        } else {
161330556Speter		sc->tulip_media = TULIP_MEDIA_100BASETX;
161430556Speter        }
161530556Speter    }
161630556Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_BNC,
161730556Speter			  TULIP_GP_EN1207_BNC_INIT,
161830556Speter			  TULIP_CMD_TXTHRSHLDCTL);
161930556Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
162030556Speter			  TULIP_GP_EN1207_UTP_INIT,
162130556Speter			  TULIP_CMD_TXTHRSHLDCTL);
162230556Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
162330556Speter			  TULIP_GP_EN1207_UTP_INIT,
162430556Speter			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
162530556Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
162630556Speter			  TULIP_GP_EN1207_100_INIT,
162730556Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
162830556Speter			      |TULIP_CMD_SCRAMBLER);
162930556Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
163030556Speter			  TULIP_GP_EN1207_100_INIT,
163130556Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
163230556Speter			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
163330556Speter}
163430556Speter
163530556Speterstatic const tulip_boardsw_t tulip_21140_accton_boardsw = {
163630556Speter    TULIP_21140_EN1207,
163730556Speter    tulip_21140_accton_media_probe,
163830556Speter    tulip_media_select,
163930556Speter    tulip_null_media_poll,
164030556Speter    tulip_2114x_media_preset,
164130556Speter};
164230556Speter
164330556Speterstatic void
164420060Srgrimestulip_21140_smc9332_media_probe(
164516357Sdg    tulip_softc_t * const sc)
164616357Sdg{
164726797Speter    tulip_media_info_t *mip = sc->tulip_mediainfo;
164818357Sdg    int idx, cnt = 0;
164926797Speter
165018357Sdg    TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT|TULIP_CMD_MUSTBEONE);
165118357Sdg    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
165218357Sdg    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
165318357Sdg		   33MHz that comes to two microseconds but wait a
165418357Sdg		   bit longer anyways) */
165518357Sdg    TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT |
165618357Sdg	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
165726797Speter    sc->tulip_gpinit = TULIP_GP_SMC_9332_PINS;
165826797Speter    sc->tulip_gpdata = TULIP_GP_SMC_9332_INIT;
165926797Speter    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_PINS|TULIP_GP_PINSET);
166016357Sdg    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_INIT);
166116357Sdg    DELAY(200000);
166216357Sdg    for (idx = 1000; idx > 0; idx--) {
166320060Srgrimes	u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
166418357Sdg	if ((csr & (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) == (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) {
166518357Sdg	    if (++cnt > 100)
166618357Sdg		break;
166718357Sdg	} else if ((csr & TULIP_GP_SMC_9332_OK10) == 0) {
166818357Sdg	    break;
166918357Sdg	} else {
167018357Sdg	    cnt = 0;
167118357Sdg	}
167216357Sdg	DELAY(1000);
167316357Sdg    }
167426797Speter    sc->tulip_media = cnt > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
167526797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
167626797Speter			  TULIP_GP_SMC_9332_INIT,
167726797Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
167826797Speter			      |TULIP_CMD_SCRAMBLER);
167926797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
168026797Speter			  TULIP_GP_SMC_9332_INIT,
168126797Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
168226797Speter			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
168326797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
168426797Speter			  TULIP_GP_SMC_9332_INIT,
168526797Speter			  TULIP_CMD_TXTHRSHLDCTL);
168626797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
168726797Speter			  TULIP_GP_SMC_9332_INIT,
168826797Speter			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
168916357Sdg}
169016357Sdg
169120060Srgrimesstatic const tulip_boardsw_t tulip_21140_smc9332_boardsw = {
169220060Srgrimes    TULIP_21140_SMC_9332,
169320060Srgrimes    tulip_21140_smc9332_media_probe,
169426797Speter    tulip_media_select,
169526797Speter    tulip_null_media_poll,
169626797Speter    tulip_2114x_media_preset,
169716357Sdg};
169816357Sdg
169926797Speterstatic void
170020060Srgrimestulip_21140_cogent_em100_media_probe(
17018754Sdg    tulip_softc_t * const sc)
17028296Sdg{
170326797Speter    tulip_media_info_t *mip = sc->tulip_mediainfo;
170427862Speter    u_int32_t cmdmode = TULIP_CSR_READ(sc, csr_command);
170526797Speter
170626797Speter    sc->tulip_gpinit = TULIP_GP_EM100_PINS;
170726797Speter    sc->tulip_gpdata = TULIP_GP_EM100_INIT;
170816357Sdg    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_PINS);
170916357Sdg    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_INIT);
17108296Sdg
171127862Speter    cmdmode = TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_MUSTBEONE;
171227862Speter    cmdmode &= ~(TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_SCRAMBLER);
171327862Speter    if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
171427862Speter	TULIP_CSR_WRITE(sc, csr_command, cmdmode);
171527862Speter	sc->tulip_media = TULIP_MEDIA_100BASEFX;
171627862Speter
171727862Speter	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX,
171826797Speter			  TULIP_GP_EM100_INIT,
171927862Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION);
172027862Speter	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX_FD,
172127862Speter			  TULIP_GP_EM100_INIT,
172226797Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
172327862Speter			      |TULIP_CMD_FULLDUPLEX);
172427862Speter    } else {
172527862Speter	TULIP_CSR_WRITE(sc, csr_command, cmdmode|TULIP_CMD_SCRAMBLER);
172627862Speter	sc->tulip_media = TULIP_MEDIA_100BASETX;
172727862Speter	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
172827862Speter			  TULIP_GP_EM100_INIT,
172927862Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
173026797Speter			      |TULIP_CMD_SCRAMBLER);
173127862Speter	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
173226797Speter			  TULIP_GP_EM100_INIT,
173326797Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
173426797Speter			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
173527862Speter    }
17368296Sdg}
17378296Sdg
173820060Srgrimesstatic const tulip_boardsw_t tulip_21140_cogent_em100_boardsw = {
173920060Srgrimes    TULIP_21140_COGENT_EM100,
174020060Srgrimes    tulip_21140_cogent_em100_media_probe,
174126797Speter    tulip_media_select,
174226797Speter    tulip_null_media_poll,
174326797Speter    tulip_2114x_media_preset
17448296Sdg};
17458296Sdg
174626797Speterstatic void
174720060Srgrimestulip_21140_znyx_zx34x_media_probe(
174811070Sdg    tulip_softc_t * const sc)
174911070Sdg{
175026797Speter    tulip_media_info_t *mip = sc->tulip_mediainfo;
175126797Speter    int cnt10 = 0, cnt100 = 0, idx;
175226797Speter
175326797Speter    sc->tulip_gpinit = TULIP_GP_ZX34X_PINS;
175426797Speter    sc->tulip_gpdata = TULIP_GP_ZX34X_INIT;
175516357Sdg    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_PINS);
175616357Sdg    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_INIT);
175716357Sdg    TULIP_CSR_WRITE(sc, csr_command,
175816357Sdg	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
175911070Sdg	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
176016357Sdg    TULIP_CSR_WRITE(sc, csr_command,
176116357Sdg	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
176211070Sdg
176326797Speter    DELAY(200000);
176426797Speter    for (idx = 1000; idx > 0; idx--) {
176526797Speter	u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
176626797Speter	if ((csr & (TULIP_GP_ZX34X_LNKFAIL|TULIP_GP_ZX34X_SYMDET|TULIP_GP_ZX34X_SIGDET)) == (TULIP_GP_ZX34X_LNKFAIL|TULIP_GP_ZX34X_SYMDET|TULIP_GP_ZX34X_SIGDET)) {
176726797Speter	    if (++cnt100 > 100)
176826797Speter		break;
176926797Speter	} else if ((csr & TULIP_GP_ZX34X_LNKFAIL) == 0) {
177026797Speter	    if (++cnt10 > 100)
177126797Speter		break;
177226797Speter	} else {
177326797Speter	    cnt10 = 0;
177426797Speter	    cnt100 = 0;
177526797Speter	}
177626797Speter	DELAY(1000);
177726797Speter    }
177826797Speter    sc->tulip_media = cnt100 > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
177926797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
178026797Speter			  TULIP_GP_ZX34X_INIT,
178126797Speter			  TULIP_CMD_TXTHRSHLDCTL);
178226797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
178326797Speter			  TULIP_GP_ZX34X_INIT,
178426797Speter			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
178526797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
178626797Speter			  TULIP_GP_ZX34X_INIT,
178726797Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
178826797Speter			      |TULIP_CMD_SCRAMBLER);
178926797Speter    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
179026797Speter			  TULIP_GP_ZX34X_INIT,
179126797Speter			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
179226797Speter			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
179311070Sdg}
179411070Sdg
179526797Speterstatic const tulip_boardsw_t tulip_21140_znyx_zx34x_boardsw = {
179626797Speter    TULIP_21140_ZNYX_ZX34X,
179726797Speter    tulip_21140_znyx_zx34x_media_probe,
179826797Speter    tulip_media_select,
179926797Speter    tulip_null_media_poll,
180026797Speter    tulip_2114x_media_preset,
180126797Speter};
180226797Speter
180311070Sdgstatic void
180426797Spetertulip_2114x_media_probe(
180511070Sdg    tulip_softc_t * const sc)
180611070Sdg{
180727862Speter    sc->tulip_cmdmode |= TULIP_CMD_MUSTBEONE
180827862Speter	|TULIP_CMD_BACKOFFCTR|TULIP_CMD_THRSHLD72;
180911070Sdg}
181011070Sdg
181126797Speterstatic const tulip_boardsw_t tulip_2114x_isv_boardsw = {
181226797Speter    TULIP_21140_ISV,
181326797Speter    tulip_2114x_media_probe,
181426797Speter    tulip_media_select,
181526797Speter    tulip_media_poll,
181626797Speter    tulip_2114x_media_preset,
181711070Sdg};
181811070Sdg
181926797Speter/*
182026797Speter * ******** END of chip-specific handlers. ***********
182126797Speter */
182216357Sdg
182326797Speter/*
182426797Speter * Code the read the SROM and MII bit streams (I2C)
182526797Speter */
182650055Speter#define EMIT    do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); DELAY(1); } while (0)
182726797Speter
182826797Speterstatic void
182926797Spetertulip_srom_idle(
183026797Speter    tulip_softc_t * const sc)
183126797Speter{
183226797Speter    unsigned bit, csr;
183326797Speter
183426797Speter    csr  = SROMSEL ; EMIT;
183526797Speter    csr  = SROMSEL | SROMRD; EMIT;
183626797Speter    csr ^= SROMCS; EMIT;
183726797Speter    csr ^= SROMCLKON; EMIT;
183826797Speter
183926797Speter    /*
184026797Speter     * Write 25 cycles of 0 which will force the SROM to be idle.
184126797Speter     */
184226797Speter    for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
184326797Speter        csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
184426797Speter        csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
184526797Speter    }
184626797Speter    csr ^= SROMCLKOFF; EMIT;
184726797Speter    csr ^= SROMCS; EMIT;
184826797Speter    csr  = 0; EMIT;
184926797Speter}
185026797Speter
185126797Speter
185226797Speterstatic void
185326797Spetertulip_srom_read(
185426797Speter    tulip_softc_t * const sc)
185526797Speter{
185627862Speter    unsigned idx;
185726797Speter    const unsigned bitwidth = SROM_BITWIDTH;
185826797Speter    const unsigned cmdmask = (SROMCMD_RD << bitwidth);
185926797Speter    const unsigned msb = 1 << (bitwidth + 3 - 1);
186026797Speter    unsigned lastidx = (1 << bitwidth) - 1;
186126797Speter
186226797Speter    tulip_srom_idle(sc);
186326797Speter
186426797Speter    for (idx = 0; idx <= lastidx; idx++) {
186526797Speter        unsigned lastbit, data, bits, bit, csr;
186626797Speter	csr  = SROMSEL ;	        EMIT;
186726797Speter        csr  = SROMSEL | SROMRD;        EMIT;
186826797Speter        csr ^= SROMCSON;                EMIT;
186926797Speter        csr ^=            SROMCLKON;    EMIT;
187026797Speter
187126797Speter        lastbit = 0;
187226797Speter        for (bits = idx|cmdmask, bit = bitwidth + 3; bit > 0; bit--, bits <<= 1) {
187326797Speter            const unsigned thisbit = bits & msb;
187426797Speter            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
187526797Speter            if (thisbit != lastbit) {
187626797Speter                csr ^= SROMDOUT; EMIT;  /* clock low; invert data */
187726797Speter            } else {
187826797Speter		EMIT;
187926797Speter	    }
188026797Speter            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
188126797Speter            lastbit = thisbit;
188226797Speter        }
188326797Speter        csr ^= SROMCLKOFF; EMIT;
188426797Speter
188526797Speter        for (data = 0, bits = 0; bits < 16; bits++) {
188626797Speter            data <<= 1;
188726797Speter            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
188826797Speter            data |= TULIP_CSR_READ(sc, csr_srom_mii) & SROMDIN ? 1 : 0;
188926797Speter            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
189026797Speter        }
189126797Speter	sc->tulip_rombuf[idx*2] = data & 0xFF;
189226797Speter	sc->tulip_rombuf[idx*2+1] = data >> 8;
189326797Speter	csr  = SROMSEL | SROMRD; EMIT;
189426797Speter	csr  = 0; EMIT;
189526797Speter    }
189626797Speter    tulip_srom_idle(sc);
189726797Speter}
189826797Speter
189950055Speter#define MII_EMIT    do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); DELAY(1); } while (0)
190026797Speter
190126797Speterstatic void
190226797Spetertulip_mii_writebits(
190326797Speter    tulip_softc_t * const sc,
190426797Speter    unsigned data,
190526797Speter    unsigned bits)
190626797Speter{
190726797Speter    unsigned msb = 1 << (bits - 1);
190826797Speter    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
190926797Speter    unsigned lastbit = (csr & MII_DOUT) ? msb : 0;
191026797Speter
191126797Speter    csr |= MII_WR; MII_EMIT;  		/* clock low; assert write */
191226797Speter
191326797Speter    for (; bits > 0; bits--, data <<= 1) {
191426797Speter	const unsigned thisbit = data & msb;
191526797Speter	if (thisbit != lastbit) {
191626797Speter	    csr ^= MII_DOUT; MII_EMIT;  /* clock low; invert data */
191716357Sdg	}
191826797Speter	csr ^= MII_CLKON; MII_EMIT;     /* clock high; data valid */
191926797Speter	lastbit = thisbit;
192026797Speter	csr ^= MII_CLKOFF; MII_EMIT;    /* clock low; data not valid */
192126797Speter    }
192226797Speter}
192326797Speter
192426797Speterstatic void
192526797Spetertulip_mii_turnaround(
192626797Speter    tulip_softc_t * const sc,
192726797Speter    unsigned cmd)
192826797Speter{
192926797Speter    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
193026797Speter
193126797Speter    if (cmd == MII_WRCMD) {
193226797Speter	csr |= MII_DOUT; MII_EMIT;	/* clock low; change data */
193326797Speter	csr ^= MII_CLKON; MII_EMIT;	/* clock high; data valid */
193426797Speter	csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
193526797Speter	csr ^= MII_DOUT; MII_EMIT;	/* clock low; change data */
193616357Sdg    } else {
193726797Speter	csr |= MII_RD; MII_EMIT;	/* clock low; switch to read */
193816357Sdg    }
193926797Speter    csr ^= MII_CLKON; MII_EMIT;		/* clock high; data valid */
194026797Speter    csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
194116357Sdg}
194216357Sdg
194326797Speterstatic unsigned
194426797Spetertulip_mii_readbits(
19458754Sdg    tulip_softc_t * const sc)
19467791Sdg{
194726797Speter    unsigned data;
194826797Speter    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
194916357Sdg    int idx;
195016357Sdg
195126797Speter    for (idx = 0, data = 0; idx < 16; idx++) {
195226797Speter	data <<= 1;	/* this is NOOP on the first pass through */
195326797Speter	csr ^= MII_CLKON; MII_EMIT;	/* clock high; data valid */
195426797Speter	if (TULIP_CSR_READ(sc, csr_srom_mii) & MII_DIN)
195526797Speter	    data |= 1;
195626797Speter	csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
195716357Sdg    }
195826797Speter    csr ^= MII_RD; MII_EMIT;		/* clock low; turn off read */
195926797Speter
196026797Speter    return data;
19617791Sdg}
19627791Sdg
196326797Speterstatic unsigned
196426797Spetertulip_mii_readreg(
196526797Speter    tulip_softc_t * const sc,
196626797Speter    unsigned devaddr,
196726797Speter    unsigned regno)
196826797Speter{
196926797Speter    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
197026797Speter    unsigned data;
197126797Speter
197226797Speter    csr &= ~(MII_RD|MII_CLK); MII_EMIT;
197326797Speter    tulip_mii_writebits(sc, MII_PREAMBLE, 32);
197426797Speter    tulip_mii_writebits(sc, MII_RDCMD, 8);
197526797Speter    tulip_mii_writebits(sc, devaddr, 5);
197626797Speter    tulip_mii_writebits(sc, regno, 5);
197726797Speter    tulip_mii_turnaround(sc, MII_RDCMD);
197826797Speter
197926797Speter    data = tulip_mii_readbits(sc);
198026797Speter#if defined(TULIP_DEBUG)
198126797Speter    sc->tulip_dbg.dbg_phyregs[regno][0] = data;
198226797Speter    sc->tulip_dbg.dbg_phyregs[regno][1]++;
198326797Speter#endif
198426797Speter    return data;
198526797Speter}
198626797Speter
19877791Sdgstatic void
198826797Spetertulip_mii_writereg(
198926797Speter    tulip_softc_t * const sc,
199026797Speter    unsigned devaddr,
199126797Speter    unsigned regno,
199226797Speter    unsigned data)
19937791Sdg{
199426797Speter    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
199526797Speter    csr &= ~(MII_RD|MII_CLK); MII_EMIT;
199626797Speter    tulip_mii_writebits(sc, MII_PREAMBLE, 32);
199726797Speter    tulip_mii_writebits(sc, MII_WRCMD, 8);
199826797Speter    tulip_mii_writebits(sc, devaddr, 5);
199926797Speter    tulip_mii_writebits(sc, regno, 5);
200026797Speter    tulip_mii_turnaround(sc, MII_WRCMD);
200126797Speter    tulip_mii_writebits(sc, data, 16);
200226797Speter#if defined(TULIP_DEBUG)
200326797Speter    sc->tulip_dbg.dbg_phyregs[regno][2] = data;
200426797Speter    sc->tulip_dbg.dbg_phyregs[regno][3]++;
200516357Sdg#endif
200616357Sdg}
200726797Speter
200826797Speter#define	tulip_mchash(mca)	(tulip_crc32(mca, 6) & 0x1FF)
200926797Speter#define	tulip_srom_crcok(databuf)	( \
201027862Speter    ((tulip_crc32(databuf, 126) & 0xFFFFU) ^ 0xFFFFU) == \
201126797Speter     ((databuf)[126] | ((databuf)[127] << 8)))
201216357Sdg
201326797Speterstatic unsigned
201426797Spetertulip_crc32(
201526797Speter    const unsigned char *databuf,
201626797Speter    size_t datalen)
201726797Speter{
201836945Speter    u_int idx, crc = 0xFFFFFFFFUL;
201936945Speter    static const u_int crctab[] = {
202036945Speter	0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
202136945Speter	0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
202236945Speter	0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
202336945Speter	0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
202436945Speter    };
202526797Speter
202636945Speter    for (idx = 0; idx < datalen; idx++) {
202736945Speter	crc ^= *databuf++;
202836945Speter	crc = (crc >> 4) ^ crctab[crc & 0xf];
202936945Speter	crc = (crc >> 4) ^ crctab[crc & 0xf];
203036945Speter    }
203126797Speter    return crc;
203226797Speter}
203316357Sdg
203426797Speterstatic void
203526797Spetertulip_identify_dec_nic(
203616357Sdg    tulip_softc_t * const sc)
203716357Sdg{
203826797Speter    strcpy(sc->tulip_boardid, "DEC ");
203926797Speter#define D0	4
204049575Speter    if (sc->tulip_chipid <= TULIP_21040)
204126797Speter	return;
204226797Speter    if (bcmp(sc->tulip_rombuf + 29, "DE500", 5) == 0
204326797Speter	|| bcmp(sc->tulip_rombuf + 29, "DE450", 5) == 0) {
204426797Speter	bcopy(sc->tulip_rombuf + 29, &sc->tulip_boardid[D0], 8);
204526797Speter	sc->tulip_boardid[D0+8] = ' ';
204626797Speter    }
204726797Speter#undef D0
204816357Sdg}
204926797Speter
205016357Sdgstatic void
205126797Spetertulip_identify_znyx_nic(
205216357Sdg    tulip_softc_t * const sc)
205316357Sdg{
205426797Speter    unsigned id = 0;
205526797Speter    strcpy(sc->tulip_boardid, "ZNYX ZX3XX ");
205626797Speter    if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
205726797Speter	unsigned znyx_ptr;
205826797Speter	sc->tulip_boardid[8] = '4';
205926797Speter	znyx_ptr = sc->tulip_rombuf[124] + 256 * sc->tulip_rombuf[125];
206026797Speter	if (znyx_ptr < 26 || znyx_ptr > 116) {
206126797Speter	    sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
206216357Sdg	    return;
206326797Speter	}
206426797Speter	/* ZX344 = 0010 .. 0013FF
206526797Speter	 */
206626797Speter	if (sc->tulip_rombuf[znyx_ptr] == 0x4A
206726797Speter		&& sc->tulip_rombuf[znyx_ptr + 1] == 0x52
206826797Speter		&& sc->tulip_rombuf[znyx_ptr + 2] == 0x01) {
206926797Speter	    id = sc->tulip_rombuf[znyx_ptr + 5] + 256 * sc->tulip_rombuf[znyx_ptr + 4];
207026797Speter	    if ((id >> 8) == (TULIP_ZNYX_ID_ZX342 >> 8)) {
207126797Speter		sc->tulip_boardid[9] = '2';
207226797Speter		if (id == TULIP_ZNYX_ID_ZX342B) {
207326797Speter		    sc->tulip_boardid[10] = 'B';
207426797Speter		    sc->tulip_boardid[11] = ' ';
207526797Speter		}
207626797Speter		sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
207726797Speter	    } else if (id == TULIP_ZNYX_ID_ZX344) {
207826797Speter		sc->tulip_boardid[10] = '4';
207926797Speter		sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
208026797Speter	    } else if (id == TULIP_ZNYX_ID_ZX345) {
208126797Speter		sc->tulip_boardid[9] = (sc->tulip_rombuf[19] > 1) ? '8' : '5';
208226797Speter	    } else if (id == TULIP_ZNYX_ID_ZX346) {
208326797Speter		sc->tulip_boardid[9] = '6';
208426797Speter	    } else if (id == TULIP_ZNYX_ID_ZX351) {
208526797Speter		sc->tulip_boardid[8] = '5';
208626797Speter		sc->tulip_boardid[9] = '1';
208716357Sdg	    }
208816357Sdg	}
208926797Speter	if (id == 0) {
209026797Speter	    /*
209126797Speter	     * Assume it's a ZX342...
209226797Speter	     */
209326797Speter	    sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
209426797Speter	}
209516357Sdg	return;
209616357Sdg    }
209726797Speter    sc->tulip_boardid[8] = '1';
209826797Speter    if (sc->tulip_chipid == TULIP_21041) {
209926797Speter	sc->tulip_boardid[10] = '1';
210016357Sdg	return;
210116357Sdg    }
210226797Speter    if (sc->tulip_rombuf[32] == 0x4A && sc->tulip_rombuf[33] == 0x52) {
210326797Speter	id = sc->tulip_rombuf[37] + 256 * sc->tulip_rombuf[36];
210426797Speter	if (id == TULIP_ZNYX_ID_ZX312T) {
210526797Speter	    sc->tulip_boardid[9] = '2';
210626797Speter	    sc->tulip_boardid[10] = 'T';
210726797Speter	    sc->tulip_boardid[11] = ' ';
210826797Speter	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
210926797Speter	} else if (id == TULIP_ZNYX_ID_ZX314_INTA) {
211026797Speter	    sc->tulip_boardid[9] = '4';
211126797Speter	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
211226797Speter	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
211326797Speter	} else if (id == TULIP_ZNYX_ID_ZX314) {
211426797Speter	    sc->tulip_boardid[9] = '4';
211526797Speter	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
211626797Speter	    sc->tulip_features |= TULIP_HAVE_BASEROM;
211726797Speter	} else if (id == TULIP_ZNYX_ID_ZX315_INTA) {
211826797Speter	    sc->tulip_boardid[9] = '5';
211926797Speter	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
212026797Speter	} else if (id == TULIP_ZNYX_ID_ZX315) {
212126797Speter	    sc->tulip_boardid[9] = '5';
212226797Speter	    sc->tulip_features |= TULIP_HAVE_BASEROM;
212326797Speter	} else {
212426797Speter	    id = 0;
212526797Speter	}
212626797Speter    }
212726797Speter    if (id == 0) {
212830706Sphk	if ((sc->tulip_enaddr[3] & ~3) == 0xF0 && (sc->tulip_enaddr[5] & 2) == 0) {
212926797Speter	    sc->tulip_boardid[9] = '4';
213026797Speter	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
213126797Speter	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
213226797Speter	} else if ((sc->tulip_enaddr[3] & ~3) == 0xF4 && (sc->tulip_enaddr[5] & 1) == 0) {
213326797Speter	    sc->tulip_boardid[9] = '5';
213426797Speter	    sc->tulip_boardsw = &tulip_21040_boardsw;
213526797Speter	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
213626797Speter	} else if ((sc->tulip_enaddr[3] & ~3) == 0xEC) {
213726797Speter	    sc->tulip_boardid[9] = '2';
213826797Speter	    sc->tulip_boardsw = &tulip_21040_boardsw;
213926797Speter	}
21407791Sdg    }
21417791Sdg}
21427791Sdg
214326797Speterstatic void
214426797Spetertulip_identify_smc_nic(
214511070Sdg    tulip_softc_t * const sc)
214611070Sdg{
214726797Speter    u_int32_t id1, id2, ei;
214826797Speter    int auibnc = 0, utp = 0;
214926797Speter    char *cp;
215011070Sdg
215126797Speter    strcpy(sc->tulip_boardid, "SMC ");
215226797Speter    if (sc->tulip_chipid == TULIP_21041)
215326797Speter	return;
215426797Speter    if (sc->tulip_chipid != TULIP_21040) {
215526797Speter	if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
215626797Speter	    strcpy(&sc->tulip_boardid[4], "9332DST ");
215726797Speter	    sc->tulip_boardsw = &tulip_21140_smc9332_boardsw;
215826797Speter	} else if (sc->tulip_features & (TULIP_HAVE_BASEROM|TULIP_HAVE_SLAVEDROM)) {
215927862Speter	    strcpy(&sc->tulip_boardid[4], "9334BDT ");
216027862Speter	} else {
216126797Speter	    strcpy(&sc->tulip_boardid[4], "9332BDT ");
216226797Speter	}
216326797Speter	return;
216426797Speter    }
216526797Speter    id1 = sc->tulip_rombuf[0x60] | (sc->tulip_rombuf[0x61] << 8);
216626797Speter    id2 = sc->tulip_rombuf[0x62] | (sc->tulip_rombuf[0x63] << 8);
216726797Speter    ei  = sc->tulip_rombuf[0x66] | (sc->tulip_rombuf[0x67] << 8);
216816357Sdg
216926797Speter    strcpy(&sc->tulip_boardid[4], "8432");
217026797Speter    cp = &sc->tulip_boardid[8];
217126797Speter    if ((id1 & 1) == 0)
217226797Speter	*cp++ = 'B', auibnc = 1;
217326797Speter    if ((id1 & 0xFF) > 0x32)
217426797Speter	*cp++ = 'T', utp = 1;
217526797Speter    if ((id1 & 0x4000) == 0)
217626797Speter	*cp++ = 'A', auibnc = 1;
217726797Speter    if (id2 == 0x15) {
217826797Speter	sc->tulip_boardid[7] = '4';
217926797Speter	*cp++ = '-';
218026797Speter	*cp++ = 'C';
218126797Speter	*cp++ = 'H';
218226797Speter	*cp++ = (ei ? '2' : '1');
218326797Speter    }
218426797Speter    *cp++ = ' ';
218526797Speter    *cp = '\0';
218626797Speter    if (utp && !auibnc)
218726797Speter	sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
218826797Speter    else if (!utp && auibnc)
218926797Speter	sc->tulip_boardsw = &tulip_21040_auibnc_only_boardsw;
219026797Speter}
219126797Speter
21928754Sdgstatic void
219326797Spetertulip_identify_cogent_nic(
219411070Sdg    tulip_softc_t * const sc)
219511070Sdg{
219626797Speter    strcpy(sc->tulip_boardid, "Cogent ");
219726797Speter    if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
219827862Speter	if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100TX_ID) {
219934317Speter	    strcat(sc->tulip_boardid, "EM100TX ");
220026797Speter	    sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
220134317Speter#if defined(TULIP_COGENT_EM110TX_ID)
220234317Speter	} else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM110TX_ID) {
220334317Speter	    strcat(sc->tulip_boardid, "EM110TX ");
220434317Speter	    sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
220534317Speter#endif
220627862Speter	} else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
220727862Speter	    strcat(sc->tulip_boardid, "EM100FX ");
220827862Speter	    sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
220927862Speter	}
221026797Speter	/*
221126797Speter	 * Magic number (0x24001109U) is the SubVendor (0x2400) and
221226797Speter	 * SubDevId (0x1109) for the ANA6944TX (EM440TX).
221326797Speter	 */
221426797Speter	if (*(u_int32_t *) sc->tulip_rombuf == 0x24001109U
221526797Speter		&& (sc->tulip_features & TULIP_HAVE_BASEROM)) {
221626797Speter	    /*
221726797Speter	     * Cogent (Adaptec) is still mapping all INTs to INTA of
221826797Speter	     * first 21140.  Dumb!  Dumb!
221926797Speter	     */
222027862Speter	    strcat(sc->tulip_boardid, "EM440TX ");
222126797Speter	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR;
222211070Sdg	}
222326797Speter    } else if (sc->tulip_chipid == TULIP_21040) {
222426797Speter	sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
222511070Sdg    }
222626797Speter}
222726797Speter
222826797Speterstatic void
222930556Spetertulip_identify_accton_nic(
223030556Speter    tulip_softc_t * const sc)
223130556Speter{
223230556Speter    strcpy(sc->tulip_boardid, "ACCTON ");
223330556Speter    switch (sc->tulip_chipid) {
223430556Speter	case TULIP_21140A:
223530556Speter	    strcat(sc->tulip_boardid, "EN1207 ");
223640290Speter	    if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
223740290Speter		sc->tulip_boardsw = &tulip_21140_accton_boardsw;
223830556Speter	    break;
223930556Speter	case TULIP_21140:
224030556Speter	    strcat(sc->tulip_boardid, "EN1207TX ");
224140290Speter	    if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
224240290Speter		sc->tulip_boardsw = &tulip_21140_eb_boardsw;
224330556Speter            break;
224430556Speter        case TULIP_21040:
224530556Speter	    strcat(sc->tulip_boardid, "EN1203 ");
224630556Speter            sc->tulip_boardsw = &tulip_21040_boardsw;
224730556Speter            break;
224830556Speter        case TULIP_21041:
224930556Speter	    strcat(sc->tulip_boardid, "EN1203 ");
225030556Speter            sc->tulip_boardsw = &tulip_21041_boardsw;
225130556Speter            break;
225230556Speter	default:
225330556Speter            sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
225430556Speter            break;
225530556Speter    }
225630556Speter}
225730556Speter
225830556Speterstatic void
225926797Spetertulip_identify_asante_nic(
226026797Speter    tulip_softc_t * const sc)
226126797Speter{
226226797Speter    strcpy(sc->tulip_boardid, "Asante ");
226326797Speter    if ((sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A)
226426797Speter	    && sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
226526797Speter	tulip_media_info_t *mi = sc->tulip_mediainfo;
226626797Speter	int idx;
226726797Speter	/*
226826797Speter	 * The Asante Fast Ethernet doesn't always ship with a valid
226926797Speter	 * new format SROM.  So if isn't in the new format, we cheat
227026797Speter	 * set it up as if we had.
227126797Speter	 */
227211070Sdg
227326797Speter	sc->tulip_gpinit = TULIP_GP_ASANTE_PINS;
227426797Speter	sc->tulip_gpdata = 0;
227526797Speter
227626797Speter	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PINS|TULIP_GP_PINSET);
227726797Speter	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PHYRESET);
227826797Speter	DELAY(100);
227926797Speter	TULIP_CSR_WRITE(sc, csr_gp, 0);
228026797Speter
228126797Speter	mi->mi_type = TULIP_MEDIAINFO_MII;
228226797Speter	mi->mi_gpr_length = 0;
228326797Speter	mi->mi_gpr_offset = 0;
228426797Speter	mi->mi_reset_length = 0;
228526797Speter	mi->mi_reset_offset = 0;;
228626797Speter
228726797Speter	mi->mi_phyaddr = TULIP_MII_NOPHY;
228826797Speter	for (idx = 20; idx > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx--) {
228926797Speter	    DELAY(10000);
229026797Speter	    mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, 0);
229126797Speter	}
229226797Speter	if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
229349572Speter	    printf("%s%d: can't find phy 0\n", sc->tulip_name, sc->tulip_unit);
229411070Sdg	    return;
229511070Sdg	}
229611070Sdg
229726797Speter	sc->tulip_features |= TULIP_HAVE_MII;
229826797Speter	mi->mi_capabilities  = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
229926797Speter	mi->mi_advertisement = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
230026797Speter	mi->mi_full_duplex   = PHYSTS_10BASET_FD|PHYSTS_100BASETX_FD;
230126797Speter	mi->mi_tx_threshold  = PHYSTS_10BASET|PHYSTS_10BASET_FD;
230226797Speter	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
230326797Speter	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
230426797Speter	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
230526797Speter	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
230626797Speter	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
230726797Speter	mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
230826797Speter	    tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
230926797Speter
231026797Speter	sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
231126797Speter    }
231226797Speter}
231326797Speter
231449560Speterstatic void
231549560Spetertulip_identify_compex_nic(
231649560Speter    tulip_softc_t * const sc)
231749560Speter{
231849560Speter    strcpy(sc->tulip_boardid, "COMPEX ");
231949560Speter    if (sc->tulip_chipid == TULIP_21140A) {
232049560Speter	int root_unit;
232149560Speter	tulip_softc_t *root_sc = NULL;
232249560Speter
232349560Speter	strcat(sc->tulip_boardid, "400TX/PCI ");
232449560Speter	/*
232549560Speter	 * All 4 chips on these boards share an interrupt.  This code
232649560Speter	 * copied from tulip_read_macaddr.
232749560Speter	 */
232849560Speter	sc->tulip_features |= TULIP_HAVE_SHAREDINTR;
232949560Speter	for (root_unit = sc->tulip_unit - 1; root_unit >= 0; root_unit--) {
233049575Speter	    root_sc = tulips[root_unit];
233149560Speter	    if (root_sc == NULL
233249560Speter		|| !(root_sc->tulip_features & TULIP_HAVE_SLAVEDINTR))
233349560Speter		break;
233449560Speter	    root_sc = NULL;
233549560Speter	}
233649560Speter	if (root_sc != NULL
233749560Speter	    && root_sc->tulip_chipid == sc->tulip_chipid
233849560Speter	    && root_sc->tulip_pci_busno == sc->tulip_pci_busno) {
233949560Speter	    sc->tulip_features |= TULIP_HAVE_SLAVEDINTR;
234049560Speter	    sc->tulip_slaves = root_sc->tulip_slaves;
234149560Speter	    root_sc->tulip_slaves = sc;
234249560Speter	} else if(sc->tulip_features & TULIP_HAVE_SLAVEDINTR) {
234349560Speter	    printf("\nCannot find master device for de%d interrupts",
234449560Speter		   sc->tulip_unit);
234549560Speter	}
234649560Speter    } else {
234749560Speter	strcat(sc->tulip_boardid, "unknown ");
234849560Speter    }
234949560Speter    /*      sc->tulip_boardsw = &tulip_21140_eb_boardsw; */
235049560Speter    return;
235149560Speter}
235249560Speter
235326797Speterstatic int
235426797Spetertulip_srom_decode(
235526797Speter    tulip_softc_t * const sc)
235626797Speter{
235727862Speter    unsigned idx1, idx2, idx3;
235826797Speter
235943309Sdillon    const tulip_srom_header_t *shp = (const tulip_srom_header_t *) &sc->tulip_rombuf[0];
236043309Sdillon    const tulip_srom_adapter_info_t *saip = (const tulip_srom_adapter_info_t *) (shp + 1);
236126797Speter    tulip_srom_media_t srom_media;
236226797Speter    tulip_media_info_t *mi = sc->tulip_mediainfo;
236326797Speter    const u_int8_t *dp;
236426797Speter    u_int32_t leaf_offset, blocks, data;
236526797Speter
236626797Speter    for (idx1 = 0; idx1 < shp->sh_adapter_count; idx1++, saip++) {
236726797Speter	if (shp->sh_adapter_count == 1)
236826797Speter	    break;
236926797Speter	if (saip->sai_device == sc->tulip_pci_devno)
237026797Speter	    break;
237126797Speter    }
237226797Speter    /*
237326797Speter     * Didn't find the right media block for this card.
237426797Speter     */
237526797Speter    if (idx1 == shp->sh_adapter_count)
237626797Speter	return 0;
237726797Speter
237826797Speter    /*
237926797Speter     * Save the hardware address.
238026797Speter     */
238143391Sbde    bcopy(shp->sh_ieee802_address, sc->tulip_enaddr, 6);
238226797Speter    /*
238326797Speter     * If this is a multiple port card, add the adapter index to the last
238426797Speter     * byte of the hardware address.  (if it isn't multiport, adding 0
238526797Speter     * won't hurt.
238626797Speter     */
238726797Speter    sc->tulip_enaddr[5] += idx1;
238826797Speter
238926797Speter    leaf_offset = saip->sai_leaf_offset_lowbyte
239026797Speter	+ saip->sai_leaf_offset_highbyte * 256;
239126797Speter    dp = sc->tulip_rombuf + leaf_offset;
239226797Speter
239326797Speter    sc->tulip_conntype = (tulip_srom_connection_t) (dp[0] + dp[1] * 256); dp += 2;
239426797Speter
239526797Speter    for (idx2 = 0;; idx2++) {
239626797Speter	if (tulip_srom_conninfo[idx2].sc_type == sc->tulip_conntype
239726797Speter	        || tulip_srom_conninfo[idx2].sc_type == TULIP_SROM_CONNTYPE_NOT_USED)
239826797Speter	    break;
239926797Speter    }
240026797Speter    sc->tulip_connidx = idx2;
240126797Speter
240226797Speter    if (sc->tulip_chipid == TULIP_21041) {
240326797Speter	blocks = *dp++;
240426797Speter	for (idx2 = 0; idx2 < blocks; idx2++) {
240526797Speter	    tulip_media_t media;
240626797Speter	    data = *dp++;
240726797Speter	    srom_media = (tulip_srom_media_t) (data & 0x3F);
240826797Speter	    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
240926797Speter		if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
241026797Speter		    break;
241111070Sdg	    }
241226797Speter	    media = tulip_srom_mediums[idx3].sm_type;
241326797Speter	    if (media != TULIP_MEDIA_UNKNOWN) {
241426797Speter		if (data & TULIP_SROM_21041_EXTENDED) {
241526797Speter		    mi->mi_type = TULIP_MEDIAINFO_SIA;
241626797Speter		    sc->tulip_mediums[media] = mi;
241726797Speter		    mi->mi_sia_connectivity = dp[0] + dp[1] * 256;
241826797Speter		    mi->mi_sia_tx_rx        = dp[2] + dp[3] * 256;
241926797Speter		    mi->mi_sia_general      = dp[4] + dp[5] * 256;
242026797Speter		    mi++;
242126797Speter		} else {
242226797Speter		    switch (media) {
242326797Speter			case TULIP_MEDIA_BNC: {
242426797Speter			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC);
242526797Speter			    mi++;
242626797Speter			    break;
242726797Speter			}
242826797Speter			case TULIP_MEDIA_AUI: {
242926797Speter			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI);
243026797Speter			    mi++;
243126797Speter			    break;
243226797Speter			}
243326797Speter			case TULIP_MEDIA_10BASET: {
243426797Speter			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET);
243526797Speter			    mi++;
243626797Speter			    break;
243726797Speter			}
243826797Speter			case TULIP_MEDIA_10BASET_FD: {
243926797Speter			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD);
244026797Speter			    mi++;
244126797Speter			    break;
244226797Speter			}
244326797Speter			default: {
244426797Speter			    break;
244526797Speter			}
244626797Speter		    }
244726797Speter		}
244826797Speter	    }
244926797Speter	    if (data & TULIP_SROM_21041_EXTENDED)
245026797Speter		dp += 6;
245126797Speter	}
245226797Speter#ifdef notdef
245326797Speter	if (blocks == 0) {
245426797Speter	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC); mi++;
245526797Speter	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI); mi++;
245626797Speter	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET); mi++;
245726797Speter	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD); mi++;
245826797Speter	}
245926797Speter#endif
246026797Speter    } else {
246126797Speter	unsigned length, type;
246226797Speter	tulip_media_t gp_media = TULIP_MEDIA_UNKNOWN;
246326797Speter	if (sc->tulip_features & TULIP_HAVE_GPR)
246426797Speter	    sc->tulip_gpinit = *dp++;
246526797Speter	blocks = *dp++;
246626797Speter	for (idx2 = 0; idx2 < blocks; idx2++) {
246726797Speter	    const u_int8_t *ep;
246826797Speter	    if ((*dp & 0x80) == 0) {
246926797Speter		length = 4;
247026797Speter		type = 0;
247126797Speter	    } else {
247226797Speter		length = (*dp++ & 0x7f) - 1;
247326797Speter		type = *dp++ & 0x3f;
247426797Speter	    }
247526797Speter	    ep = dp + length;
247626797Speter	    switch (type & 0x3f) {
247726797Speter		case 0: {	/* 21140[A] GPR block */
247826797Speter		    tulip_media_t media;
247940290Speter		    srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
248026797Speter		    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
248126797Speter			if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
248226797Speter			    break;
248326797Speter		    }
248426797Speter		    media = tulip_srom_mediums[idx3].sm_type;
248526797Speter		    if (media == TULIP_MEDIA_UNKNOWN)
248611070Sdg			break;
248726797Speter		    mi->mi_type = TULIP_MEDIAINFO_GPR;
248826797Speter		    sc->tulip_mediums[media] = mi;
248926797Speter		    mi->mi_gpdata = dp[1];
249026797Speter		    if (media > gp_media && !TULIP_IS_MEDIA_FD(media)) {
249126797Speter			sc->tulip_gpdata = mi->mi_gpdata;
249226797Speter			gp_media = media;
249311070Sdg		    }
249426797Speter		    data = dp[2] + dp[3] * 256;
249526797Speter		    mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
249626797Speter		    if (data & TULIP_SROM_2114X_NOINDICATOR) {
249726797Speter			mi->mi_actmask = 0;
249826797Speter		    } else {
249926797Speter#if 0
250026797Speter			mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
250126797Speter#endif
250226797Speter			mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
250326797Speter			mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
250426797Speter		    }
250526797Speter		    mi++;
250626797Speter		    break;
250711070Sdg		}
250826797Speter		case 1: {	/* 21140[A] MII block */
250926797Speter		    const unsigned phyno = *dp++;
251026797Speter		    mi->mi_type = TULIP_MEDIAINFO_MII;
251126797Speter		    mi->mi_gpr_length = *dp++;
251226797Speter		    mi->mi_gpr_offset = dp - sc->tulip_rombuf;
251326797Speter		    dp += mi->mi_gpr_length;
251426797Speter		    mi->mi_reset_length = *dp++;
251526797Speter		    mi->mi_reset_offset = dp - sc->tulip_rombuf;
251626797Speter		    dp += mi->mi_reset_length;
251726797Speter
251826797Speter		    /*
251926797Speter		     * Before we probe for a PHY, use the GPR information
252026797Speter		     * to select it.  If we don't, it may be inaccessible.
252126797Speter		     */
252226797Speter		    TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpinit|TULIP_GP_PINSET);
252326797Speter		    for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++) {
252426797Speter			DELAY(10);
252526797Speter			TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx3]);
252626797Speter		    }
252726797Speter		    sc->tulip_phyaddr = mi->mi_phyaddr;
252826797Speter		    for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++) {
252926797Speter			DELAY(10);
253026797Speter			TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx3]);
253126797Speter		    }
253226797Speter
253326797Speter		    /*
253426797Speter		     * At least write something!
253526797Speter		     */
253626797Speter		    if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
253726797Speter			TULIP_CSR_WRITE(sc, csr_gp, 0);
253826797Speter
253926797Speter		    mi->mi_phyaddr = TULIP_MII_NOPHY;
254026797Speter		    for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
254126797Speter			DELAY(10000);
254226797Speter			mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
254326797Speter		    }
254426797Speter		    if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
254536945Speter#if defined(TULIP_DEBUG)
254649572Speter			printf("%s%d: can't find phy %d\n",
254749572Speter			       sc->tulip_name, sc->tulip_unit, phyno);
254836945Speter#endif
254926797Speter			break;
255026797Speter		    }
255126797Speter		    sc->tulip_features |= TULIP_HAVE_MII;
255226797Speter		    mi->mi_capabilities  = dp[0] + dp[1] * 256; dp += 2;
255326797Speter		    mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
255426797Speter		    mi->mi_full_duplex   = dp[0] + dp[1] * 256; dp += 2;
255526797Speter		    mi->mi_tx_threshold  = dp[0] + dp[1] * 256; dp += 2;
255626797Speter		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
255726797Speter		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
255826797Speter		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
255926797Speter		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
256026797Speter		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
256126797Speter		    mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
256226797Speter			tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
256326797Speter		    mi++;
256426797Speter		    break;
256511070Sdg		}
256626797Speter		case 2: {	/* 2114[23] SIA block */
256726797Speter		    tulip_media_t media;
256840290Speter		    srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
256926797Speter		    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
257026797Speter			if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
257126797Speter			    break;
257226797Speter		    }
257326797Speter		    media = tulip_srom_mediums[idx3].sm_type;
257426797Speter		    if (media == TULIP_MEDIA_UNKNOWN)
257526797Speter			break;
257626797Speter		    mi->mi_type = TULIP_MEDIAINFO_SIA;
257726797Speter		    sc->tulip_mediums[media] = mi;
257840290Speter		    if (dp[0] & 0x40) {
257940290Speter			mi->mi_sia_connectivity = dp[1] + dp[2] * 256;
258040290Speter			mi->mi_sia_tx_rx        = dp[3] + dp[4] * 256;
258140290Speter			mi->mi_sia_general      = dp[5] + dp[6] * 256;
258226797Speter			dp += 6;
258326797Speter		    } else {
258426797Speter			switch (media) {
258526797Speter			    case TULIP_MEDIA_BNC: {
258626797Speter				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, BNC);
258726797Speter				break;
258826797Speter			    }
258926797Speter			    case TULIP_MEDIA_AUI: {
259026797Speter				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, AUI);
259126797Speter				break;
259226797Speter			    }
259326797Speter			    case TULIP_MEDIA_10BASET: {
259426797Speter				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET);
259536945Speter				sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
259626797Speter				break;
259726797Speter			    }
259826797Speter			    case TULIP_MEDIA_10BASET_FD: {
259926797Speter				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET_FD);
260036945Speter				sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
260126797Speter				break;
260226797Speter			    }
260326797Speter			    default: {
260426797Speter				goto bad_media;
260526797Speter			    }
260616357Sdg			}
260711070Sdg		    }
260840290Speter		    mi->mi_sia_gp_control = (dp[1] + dp[2] * 256) << 16;
260940290Speter		    mi->mi_sia_gp_data    = (dp[3] + dp[4] * 256) << 16;
261026797Speter		    mi++;
261126797Speter		  bad_media:
261211070Sdg		    break;
261311070Sdg		}
261426797Speter		case 3: {	/* 2114[23] MII PHY block */
261526797Speter		    const unsigned phyno = *dp++;
261626797Speter		    const u_int8_t *dp0;
261726797Speter		    mi->mi_type = TULIP_MEDIAINFO_MII;
261826797Speter		    mi->mi_gpr_length = *dp++;
261926797Speter		    mi->mi_gpr_offset = dp - sc->tulip_rombuf;
262026797Speter		    dp += 2 * mi->mi_gpr_length;
262126797Speter		    mi->mi_reset_length = *dp++;
262226797Speter		    mi->mi_reset_offset = dp - sc->tulip_rombuf;
262326797Speter		    dp += 2 * mi->mi_reset_length;
262426797Speter
262526797Speter		    dp0 = &sc->tulip_rombuf[mi->mi_reset_offset];
262626797Speter		    for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++, dp0 += 2) {
262726797Speter			DELAY(10);
262826797Speter			TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
262926797Speter		    }
263026797Speter		    sc->tulip_phyaddr = mi->mi_phyaddr;
263126797Speter		    dp0 = &sc->tulip_rombuf[mi->mi_gpr_offset];
263226797Speter		    for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++, dp0 += 2) {
263326797Speter			DELAY(10);
263426797Speter			TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
263526797Speter		    }
263626797Speter
263726797Speter		    if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
263826797Speter			TULIP_CSR_WRITE(sc, csr_sia_general, 0);
263926797Speter
264026797Speter		    mi->mi_phyaddr = TULIP_MII_NOPHY;
264126797Speter		    for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
264226797Speter			DELAY(10000);
264326797Speter			mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
264426797Speter		    }
264526797Speter		    if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
264636945Speter#if defined(TULIP_DEBUG)
264749572Speter			printf("%s%d: can't find phy %d\n",
264849572Speter			       sc->tulip_name, sc->tulip_unit, phyno);
264936945Speter#endif
265011070Sdg			break;
265111070Sdg		    }
265226797Speter		    sc->tulip_features |= TULIP_HAVE_MII;
265326797Speter		    mi->mi_capabilities  = dp[0] + dp[1] * 256; dp += 2;
265426797Speter		    mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
265526797Speter		    mi->mi_full_duplex   = dp[0] + dp[1] * 256; dp += 2;
265626797Speter		    mi->mi_tx_threshold  = dp[0] + dp[1] * 256; dp += 2;
265726797Speter		    mi->mi_mii_interrupt = dp[0] + dp[1] * 256; dp += 2;
265826797Speter		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
265926797Speter		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
266026797Speter		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
266126797Speter		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
266226797Speter		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
266326797Speter		    mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
266426797Speter			tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
266526797Speter		    mi++;
266626797Speter		    break;
266711070Sdg		}
266826797Speter		case 4: {	/* 21143 SYM block */
266926797Speter		    tulip_media_t media;
267026797Speter		    srom_media = (tulip_srom_media_t) dp[0];
267126797Speter		    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
267226797Speter			if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
267326797Speter			    break;
267426797Speter		    }
267526797Speter		    media = tulip_srom_mediums[idx3].sm_type;
267626797Speter		    if (media == TULIP_MEDIA_UNKNOWN)
267726797Speter			break;
267826797Speter		    mi->mi_type = TULIP_MEDIAINFO_SYM;
267926797Speter		    sc->tulip_mediums[media] = mi;
268026797Speter		    mi->mi_gpcontrol = (dp[1] + dp[2] * 256) << 16;
268126797Speter		    mi->mi_gpdata    = (dp[3] + dp[4] * 256) << 16;
268226797Speter		    data = dp[5] + dp[6] * 256;
268326797Speter		    mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
268426797Speter		    if (data & TULIP_SROM_2114X_NOINDICATOR) {
268526797Speter			mi->mi_actmask = 0;
268611070Sdg		    } else {
268726797Speter			mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
268826797Speter			mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
268926797Speter			mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
269011070Sdg		    }
269136945Speter		    if (TULIP_IS_MEDIA_TP(media))
269236945Speter			sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
269326797Speter		    mi++;
269426797Speter		    break;
269511070Sdg		}
269626797Speter#if 0
269726797Speter		case 5: {	/* 21143 Reset block */
269826797Speter		    mi->mi_type = TULIP_MEDIAINFO_RESET;
269926797Speter		    mi->mi_reset_length = *dp++;
270026797Speter		    mi->mi_reset_offset = dp - sc->tulip_rombuf;
270126797Speter		    dp += 2 * mi->mi_reset_length;
270226797Speter		    mi++;
270326797Speter		    break;
270411070Sdg		}
270526797Speter#endif
270626797Speter		default: {
270726797Speter		}
270811070Sdg	    }
270926797Speter	    dp = ep;
271011070Sdg	}
271126797Speter    }
271226797Speter    return mi - sc->tulip_mediainfo;
271326797Speter}
271426797Speter
271526797Speterstatic const struct {
271626797Speter    void (*vendor_identify_nic)(tulip_softc_t * const sc);
271726797Speter    unsigned char vendor_oui[3];
271826797Speter} tulip_vendors[] = {
271926797Speter    { tulip_identify_dec_nic,		{ 0x08, 0x00, 0x2B } },
272026797Speter    { tulip_identify_dec_nic,		{ 0x00, 0x00, 0xF8 } },
272126797Speter    { tulip_identify_smc_nic,		{ 0x00, 0x00, 0xC0 } },
272226797Speter    { tulip_identify_smc_nic,		{ 0x00, 0xE0, 0x29 } },
272326797Speter    { tulip_identify_znyx_nic,		{ 0x00, 0xC0, 0x95 } },
272426797Speter    { tulip_identify_cogent_nic,	{ 0x00, 0x00, 0x92 } },
272526797Speter    { tulip_identify_asante_nic,	{ 0x00, 0x00, 0x94 } },
272641377Smsmith    { tulip_identify_cogent_nic,	{ 0x00, 0x00, 0xD1 } },
272730556Speter    { tulip_identify_accton_nic,	{ 0x00, 0x00, 0xE8 } },
272849560Speter    { tulip_identify_compex_nic,        { 0x00, 0x80, 0x48 } },
272926797Speter    { NULL }
273026797Speter};
273126797Speter
273226797Speter/*
273326797Speter * This deals with the vagaries of the address roms and the
273426797Speter * brain-deadness that various vendors commit in using them.
273526797Speter */
273626797Speterstatic int
273726797Spetertulip_read_macaddr(
273826797Speter    tulip_softc_t * const sc)
273926797Speter{
274027862Speter    unsigned cksum, rom_cksum, idx;
274126797Speter    u_int32_t csr;
274226797Speter    unsigned char tmpbuf[8];
274326797Speter    static const u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
274426797Speter
274526797Speter    sc->tulip_connidx = TULIP_SROM_LASTCONNIDX;
274626797Speter
274726797Speter    if (sc->tulip_chipid == TULIP_21040) {
274826797Speter	TULIP_CSR_WRITE(sc, csr_enetrom, 1);
274926797Speter	for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
275026797Speter	    int cnt = 0;
275126797Speter	    while (((csr = TULIP_CSR_READ(sc, csr_enetrom)) & 0x80000000L) && cnt < 10000)
275226797Speter		cnt++;
275326797Speter	    sc->tulip_rombuf[idx] = csr & 0xFF;
275426797Speter	}
275526797Speter	sc->tulip_boardsw = &tulip_21040_boardsw;
275611070Sdg    } else {
275726797Speter	if (sc->tulip_chipid == TULIP_21041) {
275826797Speter	    /*
275926797Speter	     * Thankfully all 21041's act the same.
276026797Speter	     */
276126797Speter	    sc->tulip_boardsw = &tulip_21041_boardsw;
276226797Speter	} else {
276326797Speter	    /*
276426797Speter	     * Assume all 21140 board are compatible with the
276526797Speter	     * DEC 10/100 evaluation board.  Not really valid but
276626797Speter	     * it's the best we can do until every one switches to
276726797Speter	     * the new SROM format.
276826797Speter	     */
276949560Speter
277026797Speter	    sc->tulip_boardsw = &tulip_21140_eb_boardsw;
277126797Speter	}
277226797Speter	tulip_srom_read(sc);
277326797Speter	if (tulip_srom_crcok(sc->tulip_rombuf)) {
277426797Speter	    /*
277526797Speter	     * SROM CRC is valid therefore it must be in the
277626797Speter	     * new format.
277726797Speter	     */
277831041Speter	    sc->tulip_features |= TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM;
277926797Speter	} else if (sc->tulip_rombuf[126] == 0xff && sc->tulip_rombuf[127] == 0xFF) {
278026797Speter	    /*
278126797Speter	     * No checksum is present.  See if the SROM id checks out;
278226797Speter	     * the first 18 bytes should be 0 followed by a 1 followed
278326797Speter	     * by the number of adapters (which we don't deal with yet).
278426797Speter	     */
278526797Speter	    for (idx = 0; idx < 18; idx++) {
278626797Speter		if (sc->tulip_rombuf[idx] != 0)
278726797Speter		    break;
278826797Speter	    }
278926797Speter	    if (idx == 18 && sc->tulip_rombuf[18] == 1 && sc->tulip_rombuf[19] != 0)
279026797Speter		sc->tulip_features |= TULIP_HAVE_ISVSROM;
279131041Speter	} else if (sc->tulip_chipid >= TULIP_21142) {
279231041Speter	    sc->tulip_features |= TULIP_HAVE_ISVSROM;
279331041Speter	    sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
279426797Speter	}
279526797Speter	if ((sc->tulip_features & TULIP_HAVE_ISVSROM) && tulip_srom_decode(sc)) {
279626797Speter	    if (sc->tulip_chipid != TULIP_21041)
279726797Speter		sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
279826797Speter
279926797Speter	    /*
280026797Speter	     * If the SROM specifies more than one adapter, tag this as a
280126797Speter	     * BASE rom.
280226797Speter	     */
280326797Speter	    if (sc->tulip_rombuf[19] > 1)
280426797Speter		sc->tulip_features |= TULIP_HAVE_BASEROM;
280526797Speter	    if (sc->tulip_boardsw == NULL)
280626797Speter		return -6;
280726797Speter	    goto check_oui;
280826797Speter	}
280926797Speter    }
281026797Speter
281126797Speter
281226797Speter    if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) {
281311070Sdg	/*
281426797Speter	 * Some folks don't use the standard ethernet rom format
281526797Speter	 * but instead just put the address in the first 6 bytes
281626797Speter	 * of the rom and let the rest be all 0xffs.  (Can we say
281742155Shoek	 * ZNYX?) (well sometimes they put in a checksum so we'll
281826797Speter	 * start at 8).
281911070Sdg	 */
282026797Speter	for (idx = 8; idx < 32; idx++) {
282126797Speter	    if (sc->tulip_rombuf[idx] != 0xFF)
282226797Speter		return -4;
282326797Speter	}
282426797Speter	/*
282526797Speter	 * Make sure the address is not multicast or locally assigned
282626797Speter	 * that the OUI is not 00-00-00.
282726797Speter	 */
282826797Speter	if ((sc->tulip_rombuf[0] & 3) != 0)
282926797Speter	    return -4;
283026797Speter	if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0
283126797Speter		&& sc->tulip_rombuf[2] == 0)
283226797Speter	    return -4;
283326797Speter	bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
283426797Speter	sc->tulip_features |= TULIP_HAVE_OKROM;
283526797Speter	goto check_oui;
283626797Speter    } else {
283726797Speter	/*
283826797Speter	 * A number of makers of multiport boards (ZNYX and Cogent)
283926797Speter	 * only put on one address ROM on their 21040 boards.  So
284026797Speter	 * if the ROM is all zeros (or all 0xFFs), look at the
284126797Speter	 * previous configured boards (as long as they are on the same
284226797Speter	 * PCI bus and the bus number is non-zero) until we find the
284326797Speter	 * master board with address ROM.  We then use its address ROM
284426797Speter	 * as the base for this board.  (we add our relative board
284526797Speter	 * to the last byte of its address).
284626797Speter	 */
284726797Speter	for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
284826797Speter	    if (sc->tulip_rombuf[idx] != 0 && sc->tulip_rombuf[idx] != 0xFF)
284926797Speter		break;
285026797Speter	}
285126797Speter	if (idx == sizeof(sc->tulip_rombuf)) {
285226797Speter	    int root_unit;
285326797Speter	    tulip_softc_t *root_sc = NULL;
285426797Speter	    for (root_unit = sc->tulip_unit - 1; root_unit >= 0; root_unit--) {
285549575Speter		root_sc = tulips[root_unit];
285626797Speter		if (root_sc == NULL || (root_sc->tulip_features & (TULIP_HAVE_OKROM|TULIP_HAVE_SLAVEDROM)) == TULIP_HAVE_OKROM)
285726797Speter		    break;
285826797Speter		root_sc = NULL;
285916357Sdg	    }
286026797Speter	    if (root_sc != NULL && (root_sc->tulip_features & TULIP_HAVE_BASEROM)
286126797Speter		    && root_sc->tulip_chipid == sc->tulip_chipid
286226797Speter		    && root_sc->tulip_pci_busno == sc->tulip_pci_busno) {
286326797Speter		sc->tulip_features |= TULIP_HAVE_SLAVEDROM;
286426797Speter		sc->tulip_boardsw = root_sc->tulip_boardsw;
286526797Speter		strcpy(sc->tulip_boardid, root_sc->tulip_boardid);
286626797Speter		if (sc->tulip_boardsw->bd_type == TULIP_21140_ISV) {
286726797Speter		    bcopy(root_sc->tulip_rombuf, sc->tulip_rombuf,
286826797Speter			  sizeof(sc->tulip_rombuf));
286926797Speter		    if (!tulip_srom_decode(sc))
287026797Speter			return -5;
287126797Speter		} else {
287226797Speter		    bcopy(root_sc->tulip_enaddr, sc->tulip_enaddr, 6);
287326797Speter		    sc->tulip_enaddr[5] += sc->tulip_unit - root_sc->tulip_unit;
287426797Speter		}
287526797Speter		/*
287626797Speter		 * Now for a truly disgusting kludge: all 4 21040s on
287726797Speter		 * the ZX314 share the same INTA line so the mapping
287826797Speter		 * setup by the BIOS on the PCI bridge is worthless.
287926797Speter		 * Rather than reprogramming the value in the config
288026797Speter		 * register, we will handle this internally.
288126797Speter		 */
288226797Speter		if (root_sc->tulip_features & TULIP_HAVE_SHAREDINTR) {
288326797Speter		    sc->tulip_slaves = root_sc->tulip_slaves;
288426797Speter		    root_sc->tulip_slaves = sc;
288526797Speter		    sc->tulip_features |= TULIP_HAVE_SLAVEDINTR;
288626797Speter		}
288726797Speter		return 0;
288816357Sdg	    }
288916357Sdg	}
289026797Speter    }
289126797Speter
289226797Speter    /*
289326797Speter     * This is the standard DEC address ROM test.
289426797Speter     */
289526797Speter
289626797Speter    if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0)
289726797Speter	return -3;
289826797Speter
289926797Speter    tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14];
290026797Speter    tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12];
290126797Speter    tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10];
290226797Speter    tmpbuf[6] = sc->tulip_rombuf[9];  tmpbuf[7] = sc->tulip_rombuf[8];
290326797Speter    if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0)
290426797Speter	return -2;
290526797Speter
290626797Speter    bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
290726797Speter
290826797Speter    cksum = *(u_int16_t *) &sc->tulip_enaddr[0];
290926797Speter    cksum *= 2;
291026797Speter    if (cksum > 65535) cksum -= 65535;
291126797Speter    cksum += *(u_int16_t *) &sc->tulip_enaddr[2];
291226797Speter    if (cksum > 65535) cksum -= 65535;
291326797Speter    cksum *= 2;
291426797Speter    if (cksum > 65535) cksum -= 65535;
291526797Speter    cksum += *(u_int16_t *) &sc->tulip_enaddr[4];
291626797Speter    if (cksum >= 65535) cksum -= 65535;
291726797Speter
291826797Speter    rom_cksum = *(u_int16_t *) &sc->tulip_rombuf[6];
291926797Speter
292026797Speter    if (cksum != rom_cksum)
292126797Speter	return -1;
292226797Speter
292326797Speter  check_oui:
292426797Speter    /*
292526797Speter     * Check for various boards based on OUI.  Did I say braindead?
292626797Speter     */
292726797Speter    for (idx = 0; tulip_vendors[idx].vendor_identify_nic != NULL; idx++) {
292843386Sbde	if (bcmp(sc->tulip_enaddr, tulip_vendors[idx].vendor_oui, 3) == 0) {
292926797Speter	    (*tulip_vendors[idx].vendor_identify_nic)(sc);
293026797Speter	    break;
293111070Sdg	}
293211070Sdg    }
293326797Speter
293426797Speter    sc->tulip_features |= TULIP_HAVE_OKROM;
293526797Speter    return 0;
293626797Speter}
293726797Speter
293826797Speterstatic void
293926797Spetertulip_ifmedia_add(
294026797Speter    tulip_softc_t * const sc)
294126797Speter{
294226797Speter    tulip_media_t media;
294326797Speter    int medias = 0;
294426797Speter
294526797Speter    for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
294626797Speter	if (sc->tulip_mediums[media] != NULL) {
294726797Speter	    ifmedia_add(&sc->tulip_ifmedia, tulip_media_to_ifmedia[media],
294826797Speter			0, 0);
294926797Speter	    medias++;
295026797Speter	}
295126797Speter    }
295226797Speter    if (medias == 0) {
295326797Speter	sc->tulip_features |= TULIP_HAVE_NOMEDIA;
295426797Speter	ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE, 0, 0);
295526797Speter	ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE);
295626797Speter    } else if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
295726797Speter	ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO, 0, 0);
295826797Speter	ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO);
295916357Sdg    } else {
296026797Speter	ifmedia_set(&sc->tulip_ifmedia, tulip_media_to_ifmedia[sc->tulip_media]);
296126797Speter	sc->tulip_flags |= TULIP_PRINTMEDIA;
296226797Speter	tulip_linkup(sc, sc->tulip_media);
296316357Sdg    }
296411070Sdg}
296511070Sdg
296626797Speterstatic int
296726797Spetertulip_ifmedia_change(
296826797Speter    struct ifnet * const ifp)
296926797Speter{
297049572Speter    tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
297126797Speter
297226797Speter    sc->tulip_flags |= TULIP_NEEDRESET;
297326797Speter    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
297426797Speter    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
297526797Speter    if (IFM_SUBTYPE(sc->tulip_ifmedia.ifm_media) != IFM_AUTO) {
297626797Speter	tulip_media_t media;
297726797Speter	for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
297826797Speter	    if (sc->tulip_mediums[media] != NULL
297926797Speter		&& sc->tulip_ifmedia.ifm_media == tulip_media_to_ifmedia[media]) {
298026797Speter		sc->tulip_flags |= TULIP_PRINTMEDIA;
298126797Speter		sc->tulip_flags &= ~TULIP_DIDNWAY;
298226797Speter		tulip_linkup(sc, media);
298326797Speter		return 0;
298426797Speter	    }
298526797Speter	}
298626797Speter    }
298726797Speter    sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_WANTRXACT);
298826797Speter    tulip_reset(sc);
298926797Speter    tulip_init(sc);
299026797Speter    return 0;
299126797Speter}
299211070Sdg
299326797Speter/*
299426797Speter * Media status callback
299526797Speter */
299611070Sdgstatic void
299726797Spetertulip_ifmedia_status(
299826797Speter    struct ifnet * const ifp,
299926797Speter    struct ifmediareq *req)
300026797Speter{
300149572Speter    tulip_softc_t *sc = (tulip_softc_t *)ifp->if_softc;
300226797Speter
300326797Speter    if (sc->tulip_media == TULIP_MEDIA_UNKNOWN)
300426797Speter	return;
300526797Speter
300626797Speter    req->ifm_status = IFM_AVALID;
300726797Speter    if (sc->tulip_flags & TULIP_LINKUP)
300826797Speter	req->ifm_status |= IFM_ACTIVE;
300926797Speter
301026797Speter    req->ifm_active = tulip_media_to_ifmedia[sc->tulip_media];
301126797Speter}
301226797Speter
301326797Speterstatic void
301426797Spetertulip_addr_filter(
301526797Speter    tulip_softc_t * const sc)
301626797Speter{
301726797Speter    struct ifmultiaddr *ifma;
301826797Speter    u_char *addrp;
301926797Speter    int multicnt;
302026797Speter
302126797Speter    sc->tulip_flags &= ~(TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY|TULIP_ALLMULTI);
302227862Speter    sc->tulip_flags |= TULIP_WANTSETUP|TULIP_WANTTXSTART;
302326797Speter    sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
302426797Speter    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
302526797Speter#if defined(IFF_ALLMULTI)
302644377Sluigi    if (sc->tulip_if.if_flags & IFF_ALLMULTI)
302744377Sluigi	sc->tulip_flags |= TULIP_ALLMULTI ;
302826797Speter#endif
302926797Speter
303026797Speter    multicnt = 0;
303126797Speter    for (ifma = sc->tulip_if.if_multiaddrs.lh_first; ifma != NULL;
303226797Speter	 ifma = ifma->ifma_link.le_next) {
303326797Speter
303426797Speter	    if (ifma->ifma_addr->sa_family == AF_LINK)
303526797Speter		multicnt++;
303626797Speter    }
303726797Speter
303827862Speter    sc->tulip_if.if_start = tulip_ifstart;	/* so the setup packet gets queued */
303926797Speter    if (multicnt > 14) {
304026797Speter	u_int32_t *sp = sc->tulip_setupdata;
304126797Speter	unsigned hash;
304226797Speter	/*
304326797Speter	 * Some early passes of the 21140 have broken implementations of
304426797Speter	 * hash-perfect mode.  When we get too many multicasts for perfect
304526797Speter	 * filtering with these chips, we need to switch into hash-only
304626797Speter	 * mode (this is better than all-multicast on network with lots
304726797Speter	 * of multicast traffic).
304826797Speter	 */
304926797Speter	if (sc->tulip_features & TULIP_HAVE_BROKEN_HASH)
305026797Speter	    sc->tulip_flags |= TULIP_WANTHASHONLY;
305126797Speter	else
305226797Speter	    sc->tulip_flags |= TULIP_WANTHASHPERFECT;
305326797Speter	/*
305426797Speter	 * If we have more than 14 multicasts, we have
305526797Speter	 * go into hash perfect mode (512 bit multicast
305626797Speter	 * hash and one perfect hardware).
305726797Speter	 */
305826797Speter	bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata));
305926797Speter
306026797Speter	for (ifma = sc->tulip_if.if_multiaddrs.lh_first; ifma != NULL;
306126797Speter	     ifma = ifma->ifma_link.le_next) {
306226797Speter
306326797Speter		if (ifma->ifma_addr->sa_family != AF_LINK)
306426797Speter			continue;
306526797Speter
306626797Speter		hash = tulip_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
306749560Speter#if BYTE_ORDER == BIG_ENDIAN
306849567Speter		sp[hash >> 4] |= bswap32(1 << (hash & 0xF));
306949560Speter#else
307049567Speter		sp[hash >> 4] |= 1 << (hash & 0xF);
307149560Speter#endif
307226797Speter	}
307326797Speter	/*
307426797Speter	 * No reason to use a hash if we are going to be
307526797Speter	 * receiving every multicast.
307626797Speter	 */
307726797Speter	if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
307826797Speter	    hash = tulip_mchash(etherbroadcastaddr);
307949560Speter#if BYTE_ORDER == BIG_ENDIAN
308049560Speter	    sp[hash >> 4] |= bswap32(1 << (hash & 0xF));
308149560Speter#else
308226797Speter	    sp[hash >> 4] |= 1 << (hash & 0xF);
308349560Speter#endif
308426797Speter	    if (sc->tulip_flags & TULIP_WANTHASHONLY) {
308526797Speter		hash = tulip_mchash(sc->tulip_enaddr);
308649560Speter#if BYTE_ORDER == BIG_ENDIAN
308749560Speter		sp[hash >> 4] |= bswap32(1 << (hash & 0xF));
308849560Speter#else
308926797Speter		sp[hash >> 4] |= 1 << (hash & 0xF);
309049560Speter#endif
309126797Speter	    } else {
309249560Speter#if BYTE_ORDER == BIG_ENDIAN
309349560Speter		sp[39] = ((u_int16_t *) sc->tulip_enaddr)[0] << 16;
309449560Speter		sp[40] = ((u_int16_t *) sc->tulip_enaddr)[1] << 16;
309549560Speter		sp[41] = ((u_int16_t *) sc->tulip_enaddr)[2] << 16;
309649560Speter#else
309726797Speter		sp[39] = ((u_int16_t *) sc->tulip_enaddr)[0];
309826797Speter		sp[40] = ((u_int16_t *) sc->tulip_enaddr)[1];
309926797Speter		sp[41] = ((u_int16_t *) sc->tulip_enaddr)[2];
310049560Speter#endif
310126797Speter	    }
310226797Speter	}
310326797Speter    }
310426797Speter    if ((sc->tulip_flags & (TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY)) == 0) {
310526797Speter	u_int32_t *sp = sc->tulip_setupdata;
310626797Speter	int idx = 0;
310726797Speter	if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
310826797Speter	    /*
310926797Speter	     * Else can get perfect filtering for 16 addresses.
311026797Speter	     */
311126797Speter	    for (ifma = sc->tulip_if.if_multiaddrs.lh_first; ifma != NULL;
311226797Speter		 ifma = ifma->ifma_link.le_next) {
311326797Speter		    if (ifma->ifma_addr->sa_family != AF_LINK)
311426797Speter			    continue;
311526797Speter		    addrp = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
311649567Speter#if BYTE_ORDER == BIG_ENDIAN
311749567Speter		    *sp++ = ((u_int16_t *) addrp)[0] << 16;
311849567Speter		    *sp++ = ((u_int16_t *) addrp)[1] << 16;
311949567Speter		    *sp++ = ((u_int16_t *) addrp)[2] << 16;
312049567Speter#else
312126797Speter		    *sp++ = ((u_int16_t *) addrp)[0];
312226797Speter		    *sp++ = ((u_int16_t *) addrp)[1];
312326797Speter		    *sp++ = ((u_int16_t *) addrp)[2];
312449567Speter#endif
312526797Speter		    idx++;
312626797Speter	    }
312726797Speter	    /*
312826797Speter	     * Add the broadcast address.
312926797Speter	     */
313026797Speter	    idx++;
313149560Speter#if BYTE_ORDER == BIG_ENDIAN
313249560Speter	    *sp++ = 0xFFFF << 16;
313349560Speter	    *sp++ = 0xFFFF << 16;
313449560Speter	    *sp++ = 0xFFFF << 16;
313549560Speter#else
313626797Speter	    *sp++ = 0xFFFF;
313726797Speter	    *sp++ = 0xFFFF;
313826797Speter	    *sp++ = 0xFFFF;
313949560Speter#endif
314026797Speter	}
314126797Speter	/*
314226797Speter	 * Pad the rest with our hardware address
314326797Speter	 */
314426797Speter	for (; idx < 16; idx++) {
314549560Speter#if BYTE_ORDER == BIG_ENDIAN
314649560Speter	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[0] << 16;
314749560Speter	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[1] << 16;
314849560Speter	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[2] << 16;
314949560Speter#else
315026797Speter	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[0];
315126797Speter	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[1];
315226797Speter	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[2];
315349560Speter#endif
315426797Speter	}
315526797Speter    }
315626797Speter#if defined(IFF_ALLMULTI)
315726797Speter    if (sc->tulip_flags & TULIP_ALLMULTI)
315826797Speter	sc->tulip_if.if_flags |= IFF_ALLMULTI;
315926797Speter#endif
316026797Speter}
316126797Speter
316226797Speterstatic void
31633278Swollmantulip_reset(
31648754Sdg    tulip_softc_t * const sc)
31653278Swollman{
31663278Swollman    tulip_ringinfo_t *ri;
31673278Swollman    tulip_desc_t *di;
316826797Speter    u_int32_t inreset = (sc->tulip_flags & TULIP_INRESET);
31693278Swollman
317016357Sdg    /*
317116357Sdg     * Brilliant.  Simply brilliant.  When switching modes/speeds
317220060Srgrimes     * on a 2114*, you need to set the appriopriate MII/PCS/SCL/PS
317320060Srgrimes     * bits in CSR6 and then do a software reset to get the 21140
317416357Sdg     * to properly reset its internal pathways to the right places.
317516357Sdg     *   Grrrr.
317616357Sdg     */
317744738Speter    if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0
317844738Speter	    && sc->tulip_boardsw->bd_media_preset != NULL)
317916357Sdg	(*sc->tulip_boardsw->bd_media_preset)(sc);
318016357Sdg
318116357Sdg    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
318216357Sdg    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
31833278Swollman		   33MHz that comes to two microseconds but wait a
31843278Swollman		   bit longer anyways) */
31853278Swollman
318626797Speter    if (!inreset) {
318726797Speter	sc->tulip_flags |= TULIP_INRESET;
318826797Speter	sc->tulip_flags &= ~(TULIP_NEEDRESET|TULIP_RXBUFSLOW);
318926797Speter	sc->tulip_if.if_flags &= ~IFF_OACTIVE;
319040290Speter	sc->tulip_if.if_start = tulip_ifstart;
319126797Speter    }
31927791Sdg
319334317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
319434317Speter    TULIP_CSR_WRITE(sc, csr_txlist, sc->tulip_txdescmap->dm_segs[0].ds_addr);
319534317Speter#else
319616357Sdg    TULIP_CSR_WRITE(sc, csr_txlist, TULIP_KVATOPHYS(sc, &sc->tulip_txinfo.ri_first[0]));
319734317Speter#endif
319834317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
319934317Speter    TULIP_CSR_WRITE(sc, csr_rxlist, sc->tulip_rxdescmap->dm_segs[0].ds_addr);
320034317Speter#else
320116357Sdg    TULIP_CSR_WRITE(sc, csr_rxlist, TULIP_KVATOPHYS(sc, &sc->tulip_rxinfo.ri_first[0]));
320234317Speter#endif
320316357Sdg    TULIP_CSR_WRITE(sc, csr_busmode,
320449575Speter		    (1 << (pci_max_burst_len + 8))
320526797Speter		    |TULIP_BUSMODE_CACHE_ALIGN8
320626797Speter		    |TULIP_BUSMODE_READMULTIPLE
320749560Speter		    |(BYTE_ORDER != LITTLE_ENDIAN ?
320849560Speter		      TULIP_BUSMODE_DESC_BIGENDIAN : 0));
32093278Swollman
321016357Sdg    sc->tulip_txtimer = 0;
32113278Swollman    sc->tulip_txq.ifq_maxlen = TULIP_TXDESCS;
32123278Swollman    /*
32133278Swollman     * Free all the mbufs that were on the transmit ring.
32143278Swollman     */
32153278Swollman    for (;;) {
321634317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
321734317Speter	bus_dmamap_t map;
321834317Speter#endif
32193278Swollman	struct mbuf *m;
32203278Swollman	IF_DEQUEUE(&sc->tulip_txq, m);
32213278Swollman	if (m == NULL)
32223278Swollman	    break;
322334317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
322434317Speter	map = M_GETCTX(m, bus_dmamap_t);
322534317Speter	bus_dmamap_unload(sc->tulip_dmatag, map);
322634317Speter	sc->tulip_txmaps[sc->tulip_txmaps_free++] = map;
322734317Speter#endif
32283278Swollman	m_freem(m);
32293278Swollman    }
32303278Swollman
32313278Swollman    ri = &sc->tulip_txinfo;
32323278Swollman    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
32333278Swollman    ri->ri_free = ri->ri_max;
32343278Swollman    for (di = ri->ri_first; di < ri->ri_last; di++)
32353278Swollman	di->d_status = 0;
323634317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
323734317Speter    bus_dmamap_sync(sc->tulip_dmatag, sc->tulip_txdescmap,
323834317Speter		    0, sc->tulip_txdescmap->dm_mapsize,
323934317Speter		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
324034317Speter#endif
32413278Swollman
32423278Swollman    /*
324311070Sdg     * We need to collect all the mbufs were on the
32443278Swollman     * receive ring before we reinit it either to put
32453278Swollman     * them back on or to know if we have to allocate
32463278Swollman     * more.
32473278Swollman     */
32483278Swollman    ri = &sc->tulip_rxinfo;
32493278Swollman    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
32503278Swollman    ri->ri_free = ri->ri_max;
32517689Sdg    for (di = ri->ri_first; di < ri->ri_last; di++) {
32527689Sdg	di->d_status = 0;
32537689Sdg	di->d_length1 = 0; di->d_addr1 = 0;
32543278Swollman	di->d_length2 = 0; di->d_addr2 = 0;
32553278Swollman    }
325634317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
325734317Speter    bus_dmamap_sync(sc->tulip_dmatag, sc->tulip_rxdescmap,
325834317Speter		    0, sc->tulip_rxdescmap->dm_mapsize,
325934317Speter		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
326034317Speter#endif
32617689Sdg    for (;;) {
326234317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
326334317Speter	bus_dmamap_t map;
326434317Speter#endif
32657689Sdg	struct mbuf *m;
32667689Sdg	IF_DEQUEUE(&sc->tulip_rxq, m);
32677689Sdg	if (m == NULL)
32687689Sdg	    break;
326934317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
327034317Speter	map = M_GETCTX(m, bus_dmamap_t);
327134317Speter	bus_dmamap_unload(sc->tulip_dmatag, map);
327234317Speter	sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
327334317Speter#endif
32747689Sdg	m_freem(m);
32757689Sdg    }
32763278Swollman
327726797Speter    /*
327826797Speter     * If tulip_reset is being called recurisvely, exit quickly knowing
327926797Speter     * that when the outer tulip_reset returns all the right stuff will
328026797Speter     * have happened.
328126797Speter     */
328226797Speter    if (inreset)
328326797Speter	return;
328426797Speter
328526797Speter    sc->tulip_intrmask |= TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
328626797Speter	|TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
328736945Speter	|TULIP_STS_TXUNDERFLOW|TULIP_STS_TXBABBLE
328827862Speter	|TULIP_STS_RXSTOPPED;
328926797Speter
329026797Speter    if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0)
329126797Speter	(*sc->tulip_boardsw->bd_media_select)(sc);
329226797Speter#if defined(TULIP_DEBUG)
329326797Speter    if ((sc->tulip_flags & TULIP_NEEDRESET) == TULIP_NEEDRESET)
329449572Speter	printf("%s%d: tulip_reset: additional reset needed?!?\n",
329549572Speter	       sc->tulip_name, sc->tulip_unit);
329616357Sdg#endif
329726797Speter    tulip_media_print(sc);
329826797Speter    if (sc->tulip_features & TULIP_HAVE_DUALSENSE)
329916357Sdg	TULIP_CSR_WRITE(sc, csr_sia_status, TULIP_CSR_READ(sc, csr_sia_status));
330011070Sdg
330116357Sdg    sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP|TULIP_INRESET
330216357Sdg			 |TULIP_RXACT);
33033278Swollman    tulip_addr_filter(sc);
33043278Swollman}
33053278Swollman
33068754Sdgstatic void
33073278Swollmantulip_init(
33088754Sdg    tulip_softc_t * const sc)
33093278Swollman{
33103278Swollman    if (sc->tulip_if.if_flags & IFF_UP) {
331118357Sdg	if ((sc->tulip_if.if_flags & IFF_RUNNING) == 0) {
331218357Sdg	    /* initialize the media */
331318357Sdg	    tulip_reset(sc);
331418357Sdg	}
33153278Swollman	sc->tulip_if.if_flags |= IFF_RUNNING;
33163278Swollman	if (sc->tulip_if.if_flags & IFF_PROMISC) {
331726797Speter	    sc->tulip_flags |= TULIP_PROMISC;
33183278Swollman	    sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
331927862Speter	    sc->tulip_intrmask |= TULIP_STS_TXINTR;
33203278Swollman	} else {
332126797Speter	    sc->tulip_flags &= ~TULIP_PROMISC;
33223278Swollman	    sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
332326797Speter	    if (sc->tulip_flags & TULIP_ALLMULTI) {
33243278Swollman		sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
33253278Swollman	    } else {
33263278Swollman		sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
33273278Swollman	    }
33283278Swollman	}
33293278Swollman	sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
333026797Speter	if ((sc->tulip_flags & (TULIP_TXPROBE_ACTIVE|TULIP_WANTSETUP)) == 0) {
33317689Sdg	    tulip_rx_intr(sc);
33323278Swollman	    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
33333278Swollman	    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
33343278Swollman	} else {
333526797Speter	    sc->tulip_if.if_flags |= IFF_OACTIVE;
333626797Speter	    sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
33373278Swollman	    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
33383278Swollman	}
333916357Sdg	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
334016357Sdg	TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
334127862Speter	if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
334227862Speter	    tulip_txput_setup(sc);
33433278Swollman    } else {
334418357Sdg	sc->tulip_if.if_flags &= ~IFF_RUNNING;
33458754Sdg	tulip_reset(sc);
33463278Swollman    }
33473278Swollman}
33483278Swollman
33493278Swollmanstatic void
33503278Swollmantulip_rx_intr(
33518754Sdg    tulip_softc_t * const sc)
33523278Swollman{
335327862Speter    TULIP_PERFSTART(rxintr)
33548754Sdg    tulip_ringinfo_t * const ri = &sc->tulip_rxinfo;
33558754Sdg    struct ifnet * const ifp = &sc->tulip_if;
335616357Sdg    int fillok = 1;
335726797Speter#if defined(TULIP_DEBUG)
335816357Sdg    int cnt = 0;
335916357Sdg#endif
33603278Swollman
33614322Sdg    for (;;) {
336227862Speter	TULIP_PERFSTART(rxget)
33637689Sdg	struct ether_header eh;
33647689Sdg	tulip_desc_t *eop = ri->ri_nextin;
336516357Sdg	int total_len = 0, last_offset = 0;
336616357Sdg	struct mbuf *ms = NULL, *me = NULL;
33677689Sdg	int accept = 0;
336834317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
336934317Speter	bus_dmamap_t map;
337034317Speter	int error;
337134317Speter#endif
33723278Swollman
337316357Sdg	if (fillok && sc->tulip_rxq.ifq_len < TULIP_RXQ_TARGET)
337416357Sdg	    goto queue_mbuf;
33757689Sdg
337626797Speter#if defined(TULIP_DEBUG)
337718357Sdg	if (cnt == ri->ri_max)
337818357Sdg	    break;
337916357Sdg#endif
338016357Sdg	/*
338118357Sdg	 * If the TULIP has no descriptors, there can't be any receive
338218357Sdg	 * descriptors to process.
338318357Sdg 	 */
338418357Sdg	if (eop == ri->ri_nextout)
338518357Sdg	    break;
338634317Speter
338718357Sdg	/*
338818357Sdg	 * 90% of the packets will fit in one descriptor.  So we optimize
338918357Sdg	 * for that case.
339016357Sdg	 */
339134317Speter	TULIP_RXDESC_POSTSYNC(sc, eop, sizeof(*eop));
339218357Sdg	if ((((volatile tulip_desc_t *) eop)->d_status & (TULIP_DSTS_OWNER|TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) == (TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) {
339318357Sdg	    IF_DEQUEUE(&sc->tulip_rxq, ms);
339418357Sdg	    me = ms;
339518357Sdg	} else {
339618357Sdg	    /*
339718357Sdg	     * If still owned by the TULIP, don't touch it.
339818357Sdg	     */
339918357Sdg	    if (((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER)
340018357Sdg		break;
340118357Sdg
340218357Sdg	    /*
340318357Sdg	     * It is possible (though improbable unless the BIG_PACKET support
340418357Sdg	     * is enabled or MCLBYTES < 1518) for a received packet to cross
340518357Sdg	     * more than one receive descriptor.
340618357Sdg	     */
340718357Sdg	    while ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_RxLASTDESC) == 0) {
340818357Sdg		if (++eop == ri->ri_last)
340918357Sdg		    eop = ri->ri_first;
341034317Speter		TULIP_RXDESC_POSTSYNC(sc, eop, sizeof(*eop));
341118357Sdg		if (eop == ri->ri_nextout || ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER))) {
341226797Speter#if defined(TULIP_DEBUG)
341318357Sdg		    sc->tulip_dbg.dbg_rxintrs++;
341418357Sdg		    sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
341516357Sdg#endif
341627862Speter		    TULIP_PERFEND(rxget);
341727862Speter		    TULIP_PERFEND(rxintr);
341818357Sdg		    return;
341918357Sdg		}
342018357Sdg		total_len++;
34213278Swollman	    }
342216357Sdg
342318357Sdg	    /*
342418357Sdg	     * Dequeue the first buffer for the start of the packet.  Hopefully
342518357Sdg	     * this will be the only one we need to dequeue.  However, if the
342618357Sdg	     * packet consumed multiple descriptors, then we need to dequeue
342718357Sdg	     * those buffers and chain to the starting mbuf.  All buffers but
342818357Sdg	     * the last buffer have the same length so we can set that now.
342918357Sdg	     * (we add to last_offset instead of multiplying since we normally
343018357Sdg	     * won't go into the loop and thereby saving a ourselves from
343118357Sdg	     * doing a multiplication by 0 in the normal case).
343218357Sdg	     */
343318357Sdg	    IF_DEQUEUE(&sc->tulip_rxq, ms);
343418357Sdg	    for (me = ms; total_len > 0; total_len--) {
343534317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
343634317Speter		map = M_GETCTX(me, bus_dmamap_t);
343734317Speter		TULIP_RXMAP_POSTSYNC(sc, map);
343834317Speter		bus_dmamap_unload(sc->tulip_dmatag, map);
343934317Speter		sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
344034317Speter#if defined(DIAGNOSTIC)
344134317Speter		M_SETCTX(me, NULL);
344234317Speter#endif
344334317Speter#endif /* TULIP_BUS_DMA */
344418357Sdg		me->m_len = TULIP_RX_BUFLEN;
344518357Sdg		last_offset += TULIP_RX_BUFLEN;
344618357Sdg		IF_DEQUEUE(&sc->tulip_rxq, me->m_next);
344718357Sdg		me = me->m_next;
344818357Sdg	    }
344916357Sdg	}
345016357Sdg
345116357Sdg	/*
345216357Sdg	 *  Now get the size of received packet (minus the CRC).
345316357Sdg	 */
345416357Sdg	total_len = ((eop->d_status >> 16) & 0x7FFF) - 4;
345526797Speter	if ((sc->tulip_flags & TULIP_RXIGNORE) == 0
345626797Speter		&& ((eop->d_status & TULIP_DSTS_ERRSUM) == 0
345716357Sdg#ifdef BIG_PACKET
345826797Speter		     || (total_len <= sc->tulip_if.if_mtu + sizeof(struct ether_header) &&
345926797Speter			 (eop->d_status & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxRUNT|
346026797Speter					  TULIP_DSTS_RxCOLLSEEN|TULIP_DSTS_RxBADCRC|
346126797Speter					  TULIP_DSTS_RxOVERFLOW)) == 0)
34623278Swollman#endif
346326797Speter		)) {
346416357Sdg	    me->m_len = total_len - last_offset;
346534317Speter
346634317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
346734317Speter	    map = M_GETCTX(me, bus_dmamap_t);
346834317Speter	    bus_dmamap_sync(sc->tulip_dmatag, map, 0, me->m_len,
346934317Speter			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
347034317Speter	    bus_dmamap_unload(sc->tulip_dmatag, map);
347134317Speter	    sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
347234317Speter#if defined(DIAGNOSTIC)
347334317Speter	    M_SETCTX(me, NULL);
347434317Speter#endif
347534317Speter#endif /* TULIP_BUS_DMA */
347634317Speter
347716357Sdg	    eh = *mtod(ms, struct ether_header *);
347849572Speter	    if (sc->tulip_if.if_bpf != NULL) {
347916357Sdg		if (me == ms)
348049572Speter		    bpf_tap(&sc->tulip_if, mtod(ms, caddr_t), total_len);
348116357Sdg		else
348249572Speter		    bpf_mtap(&sc->tulip_if, ms);
348340290Speter	    }
348426797Speter	    sc->tulip_flags |= TULIP_RXACT;
348544829Sluigi
348644829Sluigi#ifdef BRIDGE /* see code in if_ed.c */
348744829Sluigi            ms->m_pkthdr.rcvif = ifp; /* XXX */
348844829Sluigi            ms->m_pkthdr.len = total_len; /* XXX */
348944829Sluigi            if (do_bridge) {
349044829Sluigi                struct ifnet *bdg_ifp ;
349144829Sluigi                bdg_ifp = bridge_in(ms);
349244829Sluigi                if (bdg_ifp == BDG_DROP)
349344829Sluigi                    goto next ; /* and drop */
349444829Sluigi                if (bdg_ifp != BDG_LOCAL)
349544829Sluigi                    bdg_forward(&ms, bdg_ifp);
349644829Sluigi                if (bdg_ifp != BDG_LOCAL && bdg_ifp != BDG_BCAST &&
349744829Sluigi                        bdg_ifp != BDG_MCAST)
349844829Sluigi                    goto next ; /* and drop */
349944829Sluigi                /* all others accepted locally */
350044829Sluigi            } else
350144829Sluigi#endif
350226797Speter	    if ((sc->tulip_flags & (TULIP_PROMISC|TULIP_HASHONLY))
35038754Sdg		    && (eh.ether_dhost[0] & 1) == 0
350426797Speter		    && !TULIP_ADDREQUAL(eh.ether_dhost, sc->tulip_enaddr))
35053278Swollman		    goto next;
35067689Sdg	    accept = 1;
35073278Swollman	} else {
350816357Sdg	    ifp->if_ierrors++;
350916357Sdg	    if (eop->d_status & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxOVERFLOW|TULIP_DSTS_RxWATCHDOG)) {
351016357Sdg		sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
351116357Sdg	    } else {
351234317Speter#if defined(TULIP_VERBOSE)
351316357Sdg		const char *error = NULL;
351434317Speter#endif
351516357Sdg		if (eop->d_status & TULIP_DSTS_RxTOOLONG) {
351616357Sdg		    sc->tulip_dot3stats.dot3StatsFrameTooLongs++;
351734317Speter#if defined(TULIP_VERBOSE)
351816357Sdg		    error = "frame too long";
351934317Speter#endif
352016357Sdg		}
352116357Sdg		if (eop->d_status & TULIP_DSTS_RxBADCRC) {
352216357Sdg		    if (eop->d_status & TULIP_DSTS_RxDRBBLBIT) {
352316357Sdg			sc->tulip_dot3stats.dot3StatsAlignmentErrors++;
352434317Speter#if defined(TULIP_VERBOSE)
352516357Sdg			error = "alignment error";
352634317Speter#endif
352716357Sdg		    } else {
352816357Sdg			sc->tulip_dot3stats.dot3StatsFCSErrors++;
352934317Speter#if defined(TULIP_VERBOSE)
353016357Sdg			error = "bad crc";
353134317Speter#endif
353216357Sdg		    }
353316357Sdg		}
353434317Speter#if defined(TULIP_VERBOSE)
353516357Sdg		if (error != NULL && (sc->tulip_flags & TULIP_NOMESSAGES) == 0) {
353649572Speter		    printf("%s%d: receive: %6D: %s\n",
353749572Speter			   sc->tulip_name, sc->tulip_unit,
353849572Speter			   mtod(ms, u_char *) + 6, ":",
353916357Sdg			   error);
354016357Sdg		    sc->tulip_flags |= TULIP_NOMESSAGES;
354116357Sdg		}
354234317Speter#endif
354316357Sdg	    }
354436945Speter
354536945Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
354636945Speter	    map = M_GETCTX(me, bus_dmamap_t);
354736945Speter	    bus_dmamap_unload(sc->tulip_dmatag, map);
354836945Speter	    sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
354936945Speter#if defined(DIAGNOSTIC)
355036945Speter	    M_SETCTX(me, NULL);
355136945Speter#endif
355236945Speter#endif /* TULIP_BUS_DMA */
35533278Swollman	}
35547689Sdg      next:
355526797Speter#if defined(TULIP_DEBUG)
355616357Sdg	cnt++;
355716357Sdg#endif
35584322Sdg	ifp->if_ipackets++;
355916357Sdg	if (++eop == ri->ri_last)
356016357Sdg	    eop = ri->ri_first;
356116357Sdg	ri->ri_nextin = eop;
35627689Sdg      queue_mbuf:
35637689Sdg	/*
35647689Sdg	 * Either we are priming the TULIP with mbufs (m == NULL)
35657689Sdg	 * or we are about to accept an mbuf for the upper layers
35667689Sdg	 * so we need to allocate an mbuf to replace it.  If we
356716357Sdg	 * can't replace it, send up it anyways.  This may cause
356816357Sdg	 * us to drop packets in the future but that's better than
356916357Sdg	 * being caught in livelock.
357016357Sdg	 *
357116357Sdg	 * Note that if this packet crossed multiple descriptors
357216357Sdg	 * we don't even try to reallocate all the mbufs here.
357316357Sdg	 * Instead we rely on the test of the beginning of
357416357Sdg	 * the loop to refill for the extra consumed mbufs.
35757689Sdg	 */
357616357Sdg	if (accept || ms == NULL) {
35777689Sdg	    struct mbuf *m0;
35787689Sdg	    MGETHDR(m0, M_DONTWAIT, MT_DATA);
35797689Sdg	    if (m0 != NULL) {
35808754Sdg#if defined(TULIP_COPY_RXDATA)
358149560Speter		if (!accept || total_len >= (MHLEN - 2)) {
35828754Sdg#endif
35838754Sdg		    MCLGET(m0, M_DONTWAIT);
35848754Sdg		    if ((m0->m_flags & M_EXT) == 0) {
35858754Sdg			m_freem(m0);
35868754Sdg			m0 = NULL;
35878754Sdg		    }
35888754Sdg#if defined(TULIP_COPY_RXDATA)
35897689Sdg		}
35908754Sdg#endif
35917689Sdg	    }
359226797Speter	    if (accept
359326797Speter#if defined(TULIP_COPY_RXDATA)
359426797Speter		&& m0 != NULL
359526797Speter#endif
359626797Speter		) {
35978754Sdg#if !defined(TULIP_COPY_RXDATA)
359816357Sdg		ms->m_pkthdr.len = total_len;
359916357Sdg		ms->m_pkthdr.rcvif = ifp;
360049560Speter		m_adj(ms, sizeof(struct ether_header));
360116357Sdg		ether_input(ifp, &eh, ms);
36028754Sdg#else
360316357Sdg#ifdef BIG_PACKET
360416357Sdg#error BIG_PACKET is incompatible with TULIP_COPY_RXDATA
360516357Sdg#endif
360649560Speter		m0->m_data += 2;	/* align data after header */
360749560Speter		m_copydata(ms, 0, total_len, mtod(m0, caddr_t));
360816357Sdg		m0->m_len = m0->m_pkthdr.len = total_len;
360916357Sdg		m0->m_pkthdr.rcvif = ifp;
361049566Speter		m_adj(m0, sizeof(struct ether_header));
361116357Sdg		ether_input(ifp, &eh, m0);
361216357Sdg		m0 = ms;
361349560Speter#endif /* ! TULIP_COPY_RXDATA */
36147689Sdg	    }
361516357Sdg	    ms = m0;
36163278Swollman	}
361716357Sdg	if (ms == NULL) {
361816357Sdg	    /*
361916357Sdg	     * Couldn't allocate a new buffer.  Don't bother
362016357Sdg	     * trying to replenish the receive queue.
362116357Sdg	     */
362216357Sdg	    fillok = 0;
362316357Sdg	    sc->tulip_flags |= TULIP_RXBUFSLOW;
362426797Speter#if defined(TULIP_DEBUG)
362516357Sdg	    sc->tulip_dbg.dbg_rxlowbufs++;
362616357Sdg#endif
362727862Speter	    TULIP_PERFEND(rxget);
362816357Sdg	    continue;
362916357Sdg	}
36307689Sdg	/*
363116357Sdg	 * Now give the buffer(s) to the TULIP and save in our
36327689Sdg	 * receive queue.
36337689Sdg	 */
363416357Sdg	do {
363534317Speter	    tulip_desc_t * const nextout = ri->ri_nextout;
363634317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
363734317Speter	    if (sc->tulip_rxmaps_free > 0) {
363834317Speter		map = sc->tulip_rxmaps[--sc->tulip_rxmaps_free];
363934317Speter	    } else {
364034317Speter		m_freem(ms);
364134317Speter		sc->tulip_flags |= TULIP_RXBUFSLOW;
364234317Speter#if defined(TULIP_DEBUG)
364334317Speter		sc->tulip_dbg.dbg_rxlowbufs++;
364434317Speter#endif
364534317Speter		break;
364634317Speter	    }
364734317Speter	    M_SETCTX(ms, map);
364834317Speter	    error = bus_dmamap_load(sc->tulip_dmatag, map, mtod(ms, void *),
364934317Speter				    TULIP_RX_BUFLEN, NULL, BUS_DMA_NOWAIT);
365034317Speter	    if (error) {
365149572Speter		printf("%s%d: unable to load rx map, "
365249572Speter		       "error = %d\n", sc->tulip_name, sc->tulip_unit, error);
365334317Speter		panic("tulip_rx_intr");		/* XXX */
365434317Speter	    }
365534317Speter	    nextout->d_addr1 = map->dm_segs[0].ds_addr;
365634317Speter	    nextout->d_length1 = map->dm_segs[0].ds_len;
365734317Speter	    if (map->dm_nsegs == 2) {
365834317Speter		nextout->d_addr2 = map->dm_segs[1].ds_addr;
365934317Speter		nextout->d_length2 = map->dm_segs[1].ds_len;
366034317Speter	    } else {
366134317Speter		nextout->d_addr2 = 0;
366234317Speter		nextout->d_length2 = 0;
366334317Speter	    }
366434317Speter	    TULIP_RXDESC_POSTSYNC(sc, nextout, sizeof(*nextout));
366534317Speter#else /* TULIP_BUS_DMA */
366634317Speter	    nextout->d_addr1 = TULIP_KVATOPHYS(sc, mtod(ms, caddr_t));
366734317Speter	    nextout->d_length1 = TULIP_RX_BUFLEN;
366834317Speter#endif /* TULIP_BUS_DMA */
366934317Speter	    nextout->d_status = TULIP_DSTS_OWNER;
367034317Speter	    TULIP_RXDESC_POSTSYNC(sc, nextout, sizeof(u_int32_t));
367116357Sdg	    if (++ri->ri_nextout == ri->ri_last)
367216357Sdg		ri->ri_nextout = ri->ri_first;
367316357Sdg	    me = ms->m_next;
367416357Sdg	    ms->m_next = NULL;
367516357Sdg	    IF_ENQUEUE(&sc->tulip_rxq, ms);
367616357Sdg	} while ((ms = me) != NULL);
367716357Sdg
367818357Sdg	if (sc->tulip_rxq.ifq_len >= TULIP_RXQ_TARGET)
367916357Sdg	    sc->tulip_flags &= ~TULIP_RXBUFSLOW;
368027862Speter	TULIP_PERFEND(rxget);
36813278Swollman    }
368218357Sdg
368326797Speter#if defined(TULIP_DEBUG)
368418357Sdg    sc->tulip_dbg.dbg_rxintrs++;
368518357Sdg    sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
368618357Sdg#endif
368727862Speter    TULIP_PERFEND(rxintr);
36883278Swollman}
36893278Swollman
36903278Swollmanstatic int
36913278Swollmantulip_tx_intr(
36928754Sdg    tulip_softc_t * const sc)
36933278Swollman{
369427862Speter    TULIP_PERFSTART(txintr)
36958754Sdg    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
36963278Swollman    struct mbuf *m;
36973278Swollman    int xmits = 0;
369827862Speter    int descs = 0;
36993278Swollman
37003278Swollman    while (ri->ri_free < ri->ri_max) {
370127862Speter	u_int32_t d_flag;
370234317Speter
370334317Speter	TULIP_TXDESC_POSTSYNC(sc, ri->ri_nextin, sizeof(*ri->ri_nextin));
37043278Swollman	if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
37053278Swollman	    break;
37063278Swollman
370740290Speter	ri->ri_free++;
370840290Speter	descs++;
370927862Speter	d_flag = ri->ri_nextin->d_flag;
371027862Speter	if (d_flag & TULIP_DFLAG_TxLASTSEG) {
371127862Speter	    if (d_flag & TULIP_DFLAG_TxSETUPPKT) {
37123278Swollman		/*
37133278Swollman		 * We've just finished processing a setup packet.
371426797Speter		 * Mark that we finished it.  If there's not
37153278Swollman		 * another pending, startup the TULIP receiver.
37164772Sdg		 * Make sure we ack the RXSTOPPED so we won't get
37174772Sdg		 * an abormal interrupt indication.
37183278Swollman		 */
371934317Speter		TULIP_TXMAP_POSTSYNC(sc, sc->tulip_setupmap);
372026797Speter		sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_HASHONLY);
372126797Speter		if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxINVRSFILT)
372226797Speter		    sc->tulip_flags |= TULIP_HASHONLY;
372326797Speter		if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == 0) {
37247689Sdg		    tulip_rx_intr(sc);
37253278Swollman		    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
37263278Swollman		    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
372716357Sdg		    TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
372816357Sdg		    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
372926797Speter		    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
37303278Swollman		}
373116357Sdg	    } else {
373227862Speter		const u_int32_t d_status = ri->ri_nextin->d_status;
37333278Swollman		IF_DEQUEUE(&sc->tulip_txq, m);
373430556Speter		if (m != NULL) {
373534317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
373634317Speter		    bus_dmamap_t map = M_GETCTX(m, bus_dmamap_t);
373734317Speter		    TULIP_TXMAP_POSTSYNC(sc, map);
373834317Speter		    sc->tulip_txmaps[sc->tulip_txmaps_free++] = map;
373934317Speter#endif /* TULIP_BUS_DMA */
374049572Speter		    if (sc->tulip_if.if_bpf != NULL)
374149572Speter			bpf_mtap(&sc->tulip_if, m);
374230556Speter		    m_freem(m);
374330556Speter#if defined(TULIP_DEBUG)
374430556Speter		} else {
374549572Speter		    printf("%s%d: tx_intr: failed to dequeue mbuf?!?\n",
374649572Speter			    sc->tulip_name, sc->tulip_unit);
374730556Speter#endif
374830556Speter		}
374911070Sdg		if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
375026797Speter		    tulip_mediapoll_event_t event = TULIP_MEDIAPOLL_TXPROBE_OK;
375127862Speter		    if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxEXCCOLL)) {
375226797Speter#if defined(TULIP_DEBUG)
375327862Speter			if (d_status & TULIP_DSTS_TxNOCARR)
375426797Speter			    sc->tulip_dbg.dbg_txprobe_nocarr++;
375527862Speter			if (d_status & TULIP_DSTS_TxEXCCOLL)
375626797Speter			    sc->tulip_dbg.dbg_txprobe_exccoll++;
375726797Speter#endif
375826797Speter			event = TULIP_MEDIAPOLL_TXPROBE_FAILED;
375926797Speter		    }
376026797Speter		    (*sc->tulip_boardsw->bd_media_poll)(sc, event);
376126797Speter		    /*
376226797Speter		     * Escape from the loop before media poll has reset the TULIP!
376326797Speter		     */
376426797Speter		    break;
376511070Sdg		} else {
376626797Speter		    xmits++;
376727862Speter		    if (d_status & TULIP_DSTS_ERRSUM) {
376811070Sdg			sc->tulip_if.if_oerrors++;
376927862Speter			if (d_status & TULIP_DSTS_TxEXCCOLL)
377016357Sdg			    sc->tulip_dot3stats.dot3StatsExcessiveCollisions++;
377127862Speter			if (d_status & TULIP_DSTS_TxLATECOLL)
377216357Sdg			    sc->tulip_dot3stats.dot3StatsLateCollisions++;
377327862Speter			if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxCARRLOSS))
377416357Sdg			    sc->tulip_dot3stats.dot3StatsCarrierSenseErrors++;
377527862Speter			if (d_status & (TULIP_DSTS_TxUNDERFLOW|TULIP_DSTS_TxBABBLE))
377616357Sdg			    sc->tulip_dot3stats.dot3StatsInternalMacTransmitErrors++;
377727862Speter			if (d_status & TULIP_DSTS_TxUNDERFLOW)
377827862Speter			    sc->tulip_dot3stats.dot3StatsInternalTransmitUnderflows++;
377927862Speter			if (d_status & TULIP_DSTS_TxBABBLE)
378027862Speter			    sc->tulip_dot3stats.dot3StatsInternalTransmitBabbles++;
378116357Sdg		    } else {
378220060Srgrimes			u_int32_t collisions =
378327862Speter			    (d_status & TULIP_DSTS_TxCOLLMASK)
378416357Sdg				>> TULIP_DSTS_V_TxCOLLCNT;
378516357Sdg			sc->tulip_if.if_collisions += collisions;
378616357Sdg			if (collisions == 1)
378716357Sdg			    sc->tulip_dot3stats.dot3StatsSingleCollisionFrames++;
378816357Sdg			else if (collisions > 1)
378916357Sdg			    sc->tulip_dot3stats.dot3StatsMultipleCollisionFrames++;
379027862Speter			else if (d_status & TULIP_DSTS_TxDEFERRED)
379116357Sdg			    sc->tulip_dot3stats.dot3StatsDeferredTransmissions++;
379216357Sdg			/*
379316357Sdg			 * SQE is only valid for 10baseT/BNC/AUI when not
379416357Sdg			 * running in full-duplex.  In order to speed up the
379516357Sdg			 * test, the corresponding bit in tulip_flags needs to
379616357Sdg			 * set as well to get us to count SQE Test Errors.
379716357Sdg			 */
379827862Speter			if (d_status & TULIP_DSTS_TxNOHRTBT & sc->tulip_flags)
379916357Sdg			    sc->tulip_dot3stats.dot3StatsSQETestErrors++;
380016357Sdg		    }
380111070Sdg		}
38023278Swollman	    }
38033278Swollman	}
38043278Swollman
38053278Swollman	if (++ri->ri_nextin == ri->ri_last)
38063278Swollman	    ri->ri_nextin = ri->ri_first;
380726797Speter
380826797Speter	if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
380926797Speter	    sc->tulip_if.if_flags &= ~IFF_OACTIVE;
38103278Swollman    }
381116357Sdg    /*
381216357Sdg     * If nothing left to transmit, disable the timer.
381316357Sdg     * Else if progress, reset the timer back to 2 ticks.
381416357Sdg     */
381518357Sdg    if (ri->ri_free == ri->ri_max || (sc->tulip_flags & TULIP_TXPROBE_ACTIVE))
381616357Sdg	sc->tulip_txtimer = 0;
381716357Sdg    else if (xmits > 0)
381818357Sdg	sc->tulip_txtimer = TULIP_TXTIMER;
38193278Swollman    sc->tulip_if.if_opackets += xmits;
382027862Speter    TULIP_PERFEND(txintr);
382127862Speter    return descs;
38223278Swollman}
38233278Swollman
382418357Sdgstatic void
382518357Sdgtulip_print_abnormal_interrupt(
382618357Sdg    tulip_softc_t * const sc,
382720060Srgrimes    u_int32_t csr)
38283278Swollman{
382918357Sdg    const char * const *msgp = tulip_status_bits;
383018357Sdg    const char *sep;
383127862Speter    u_int32_t mask;
383240290Speter    const char thrsh[] = "72|128\0\0\0" "96|256\0\0\0" "128|512\0\0" "160|1024";
38333278Swollman
383418357Sdg    csr &= (1 << (sizeof(tulip_status_bits)/sizeof(tulip_status_bits[0]))) - 1;
383549572Speter    printf("%s%d: abnormal interrupt:", sc->tulip_name, sc->tulip_unit);
383627862Speter    for (sep = " ", mask = 1; mask <= csr; mask <<= 1, msgp++) {
383727862Speter	if ((csr & mask) && *msgp != NULL) {
383818357Sdg	    printf("%s%s", sep, *msgp);
383927862Speter	    if (mask == TULIP_STS_TXUNDERFLOW && (sc->tulip_flags & TULIP_NEWTXTHRESH)) {
384027862Speter		sc->tulip_flags &= ~TULIP_NEWTXTHRESH;
384127862Speter		if (sc->tulip_cmdmode & TULIP_CMD_STOREFWD) {
384227862Speter		    printf(" (switching to store-and-forward mode)");
384327862Speter		} else {
384427862Speter		    printf(" (raising TX threshold to %s)",
384527862Speter			   &thrsh[9 * ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) >> 14)]);
384627862Speter		}
384727862Speter	    }
384818357Sdg	    sep = ", ";
384918357Sdg	}
385018357Sdg    }
385118357Sdg    printf("\n");
385218357Sdg}
38533278Swollman
385418357Sdgstatic void
385518357Sdgtulip_intr_handler(
385618357Sdg    tulip_softc_t * const sc,
385718357Sdg    int *progress_p)
385818357Sdg{
385927862Speter    TULIP_PERFSTART(intr)
386020060Srgrimes    u_int32_t csr;
38618754Sdg
386218357Sdg    while ((csr = TULIP_CSR_READ(sc, csr_status)) & sc->tulip_intrmask) {
386318357Sdg	*progress_p = 1;
386418357Sdg	TULIP_CSR_WRITE(sc, csr_status, csr);
386518357Sdg
386618357Sdg	if (csr & TULIP_STS_SYSERROR) {
386718357Sdg	    sc->tulip_last_system_error = (csr & TULIP_STS_ERRORMASK) >> TULIP_STS_ERR_SHIFT;
386818357Sdg	    if (sc->tulip_flags & TULIP_NOMESSAGES) {
386918357Sdg		sc->tulip_flags |= TULIP_SYSTEMERROR;
387018357Sdg	    } else {
387149572Speter		printf("%s%d: system error: %s\n",
387249572Speter		       sc->tulip_name, sc->tulip_unit,
387318357Sdg		       tulip_system_errors[sc->tulip_last_system_error]);
38743278Swollman	    }
387518357Sdg	    sc->tulip_flags |= TULIP_NEEDRESET;
387618357Sdg	    sc->tulip_system_errors++;
387718357Sdg	    break;
38783278Swollman	}
387936945Speter	if (csr & (TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL) & sc->tulip_intrmask) {
388018357Sdg#if defined(TULIP_DEBUG)
388126797Speter	    sc->tulip_dbg.dbg_link_intrs++;
388216357Sdg#endif
388326797Speter	    if (sc->tulip_boardsw->bd_media_poll != NULL) {
388426797Speter		(*sc->tulip_boardsw->bd_media_poll)(sc, csr & TULIP_STS_LINKFAIL
388526797Speter						    ? TULIP_MEDIAPOLL_LINKFAIL
388626797Speter						    : TULIP_MEDIAPOLL_LINKPASS);
388726797Speter		csr &= ~TULIP_STS_ABNRMLINTR;
38888754Sdg	    }
388926797Speter	    tulip_media_print(sc);
389026797Speter	}
389126797Speter	if (csr & (TULIP_STS_RXINTR|TULIP_STS_RXNOBUF)) {
389226797Speter	    u_int32_t misses = TULIP_CSR_READ(sc, csr_missed_frames);
389326797Speter	    if (csr & TULIP_STS_RXNOBUF)
389426797Speter		sc->tulip_dot3stats.dot3StatsMissedFrames += misses & 0xFFFF;
389526797Speter	    /*
389626797Speter	     * Pass 2.[012] of the 21140A-A[CDE] may hang and/or corrupt data
389726797Speter	     * on receive overflows.
389826797Speter	     */
389926797Speter	   if ((misses & 0x0FFE0000) && (sc->tulip_features & TULIP_HAVE_RXBADOVRFLW)) {
390026797Speter		sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
390126797Speter		/*
390226797Speter		 * Stop the receiver process and spin until it's stopped.
390326797Speter		 * Tell rx_intr to drop the packets it dequeues.
390426797Speter		 */
390526797Speter		TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode & ~TULIP_CMD_RXRUN);
390626797Speter		while ((TULIP_CSR_READ(sc, csr_status) & TULIP_STS_RXSTOPPED) == 0)
390726797Speter		    ;
390826797Speter		TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
390926797Speter		sc->tulip_flags |= TULIP_RXIGNORE;
39103278Swollman	    }
391126797Speter	    tulip_rx_intr(sc);
391226797Speter	    if (sc->tulip_flags & TULIP_RXIGNORE) {
391326797Speter		/*
391426797Speter		 * Restart the receiver.
391526797Speter		 */
391626797Speter		sc->tulip_flags &= ~TULIP_RXIGNORE;
391726797Speter		TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
391826797Speter	    }
39193278Swollman	}
392018357Sdg	if (csr & TULIP_STS_ABNRMLINTR) {
392120060Srgrimes	    u_int32_t tmp = csr & sc->tulip_intrmask
392218357Sdg		& ~(TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR);
392327862Speter	    if (csr & TULIP_STS_TXUNDERFLOW) {
392427862Speter		if ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) != TULIP_CMD_THRSHLD160) {
392527862Speter		    sc->tulip_cmdmode += TULIP_CMD_THRSHLD96;
392627862Speter		    sc->tulip_flags |= TULIP_NEWTXTHRESH;
392727862Speter		} else if (sc->tulip_features & TULIP_HAVE_STOREFWD) {
392827862Speter		    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD;
392927862Speter		    sc->tulip_flags |= TULIP_NEWTXTHRESH;
393027862Speter		}
393127862Speter	    }
393218357Sdg	    if (sc->tulip_flags & TULIP_NOMESSAGES) {
393318357Sdg		sc->tulip_statusbits |= tmp;
393418357Sdg	    } else {
393518357Sdg		tulip_print_abnormal_interrupt(sc, tmp);
393618357Sdg		sc->tulip_flags |= TULIP_NOMESSAGES;
393718357Sdg	    }
393818357Sdg	    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
39398754Sdg	}
394027862Speter	if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_TXPROBE_ACTIVE|TULIP_DOINGSETUP|TULIP_PROMISC)) {
394118357Sdg	    tulip_tx_intr(sc);
394218357Sdg	    if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
394318357Sdg		tulip_ifstart(&sc->tulip_if);
394418357Sdg	}
39453278Swollman    }
394618357Sdg    if (sc->tulip_flags & TULIP_NEEDRESET) {
394718357Sdg	tulip_reset(sc);
394818357Sdg	tulip_init(sc);
39493278Swollman    }
395027862Speter    TULIP_PERFEND(intr);
39513278Swollman}
395218357Sdg
395318357Sdg#if defined(TULIP_USE_SOFTINTR)
395418357Sdg/*
395518357Sdg * This is a experimental idea to alleviate problems due to interrupt
395618357Sdg * livelock.  What is interrupt livelock?  It's when you spend all your
395718357Sdg * time servicing device interrupts and never drop below device ipl
395818357Sdg * to do "useful" work.
395918357Sdg *
396018357Sdg * So what we do here is see if the device needs service and if so,
396118357Sdg * disable interrupts (dismiss the interrupt), place it in a list of devices
396218357Sdg * needing service, and issue a network software interrupt.
396318357Sdg *
396418357Sdg * When our network software interrupt routine gets called, we simply
396518357Sdg * walk done the list of devices that we have created and deal with them
396618357Sdg * at splnet/splsoftnet.
396718357Sdg *
396818357Sdg */
396913597Ssestatic void
397018357Sdgtulip_hardintr_handler(
397116357Sdg    tulip_softc_t * const sc,
397218357Sdg    int *progress_p)
397316357Sdg{
397418357Sdg    if (TULIP_CSR_READ(sc, csr_status) & (TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR) == 0)
397518357Sdg	return;
397618357Sdg    *progress_p = 1;
397718357Sdg    /*
397818357Sdg     * disable interrupts
397918357Sdg     */
398018357Sdg    TULIP_CSR_WRITE(sc, csr_intr, 0);
398118357Sdg    /*
398218357Sdg     * mark it as needing a software interrupt
398318357Sdg     */
398418357Sdg    tulip_softintr_mask |= (1U << sc->tulip_unit);
398518357Sdg}
398616357Sdg
398718357Sdgstatic void
398818357Sdgtulip_softintr(
398918357Sdg    void)
399018357Sdg{
399120060Srgrimes    u_int32_t softintr_mask, mask;
399218357Sdg    int progress = 0;
399318357Sdg    int unit;
399449572Speter    int s;
399518357Sdg
399618357Sdg    /*
399718357Sdg     * Copy mask to local copy and reset global one to 0.
399818357Sdg     */
399949572Speter    s = splimp();
400018357Sdg    softintr_mask = tulip_softintr_mask;
400118357Sdg    tulip_softintr_mask = 0;
400249572Speter    splx(s);
400318357Sdg
400418357Sdg    /*
400518357Sdg     * Optimize for the single unit case.
400618357Sdg     */
400718357Sdg    if (tulip_softintr_max_unit == 0) {
400818357Sdg	if (softintr_mask & 1) {
400949575Speter	    tulip_softc_t * const sc = tulips[0];
401018357Sdg	    /*
401118357Sdg	     * Handle the "interrupt" and then reenable interrupts
401218357Sdg	     */
401326797Speter	    softintr_mask = 0;
401418357Sdg	    tulip_intr_handler(sc, &progress);
401518357Sdg	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
401616357Sdg	}
401718357Sdg	return;
401816357Sdg    }
401918357Sdg
402018357Sdg    /*
402118357Sdg     * Handle all "queued" interrupts in a round robin fashion.
402218357Sdg     * This is done so as not to favor a particular interface.
402318357Sdg     */
402418357Sdg    unit = tulip_softintr_last_unit;
402518357Sdg    mask = (1U << unit);
402618357Sdg    while (softintr_mask != 0) {
402718357Sdg	if (tulip_softintr_max_unit == unit) {
402818357Sdg	    unit  = 0; mask   = 1;
402918357Sdg	} else {
403018357Sdg	    unit += 1; mask <<= 1;
403118357Sdg	}
403218357Sdg	if (softintr_mask & mask) {
403349575Speter	    tulip_softc_t * const sc = tulips[unit];
403418357Sdg	    /*
403518357Sdg	     * Handle the "interrupt" and then reenable interrupts
403618357Sdg	     */
403726797Speter	    softintr_mask ^= mask;
403818357Sdg	    tulip_intr_handler(sc, &progress);
403918357Sdg	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
404018357Sdg	}
404118357Sdg    }
404218357Sdg
404318357Sdg    /*
404418357Sdg     * Save where we ending up.
404518357Sdg     */
404618357Sdg    tulip_softintr_last_unit = unit;
404716357Sdg}
404818357Sdg#endif	/* TULIP_USE_SOFTINTR */
404916357Sdg
405049572Speterstatic void
405118357Sdgtulip_intr_shared(
40528754Sdg    void *arg)
40533278Swollman{
405430556Speter    tulip_softc_t * sc = arg;
405511070Sdg    int progress = 0;
40563278Swollman
405730556Speter    for (; sc != NULL; sc = sc->tulip_slaves) {
405816357Sdg#if defined(TULIP_DEBUG)
405916357Sdg	sc->tulip_dbg.dbg_intrs++;
406016357Sdg#endif
406118357Sdg#if defined(TULIP_USE_SOFTINTR)
406218357Sdg	tulip_hardintr_handler(sc, &progress);
406318357Sdg#else
406418357Sdg	tulip_intr_handler(sc, &progress);
406518357Sdg#endif
406618357Sdg    }
406718357Sdg#if defined(TULIP_USE_SOFTINTR)
406818357Sdg    if (progress)
406918357Sdg	schednetisr(NETISR_DE);
407018357Sdg#endif
407118357Sdg}
40723278Swollman
407349572Speterstatic void
407418357Sdgtulip_intr_normal(
407518357Sdg    void *arg)
407618357Sdg{
407718357Sdg    tulip_softc_t * sc = (tulip_softc_t *) arg;
407818357Sdg    int progress = 0;
407918357Sdg
408016357Sdg#if defined(TULIP_DEBUG)
408118357Sdg    sc->tulip_dbg.dbg_intrs++;
408216357Sdg#endif
408318357Sdg#if defined(TULIP_USE_SOFTINTR)
408418357Sdg    tulip_hardintr_handler(sc, &progress);
408518357Sdg    if (progress)
408618357Sdg	schednetisr(NETISR_DE);
408718357Sdg#else
408818357Sdg    tulip_intr_handler(sc, &progress);
408918357Sdg#endif
40903278Swollman}
40913278Swollman
409227862Speterstatic struct mbuf *
409327862Spetertulip_mbuf_compress(
409427862Speter    struct mbuf *m)
409527862Speter{
409627862Speter    struct mbuf *m0;
409727862Speter#if MCLBYTES >= ETHERMTU + 18 && !defined(BIG_PACKET)
409827862Speter    MGETHDR(m0, M_DONTWAIT, MT_DATA);
409927862Speter    if (m0 != NULL) {
410027862Speter	if (m->m_pkthdr.len > MHLEN) {
410127862Speter	    MCLGET(m0, M_DONTWAIT);
410227862Speter	    if ((m0->m_flags & M_EXT) == 0) {
410327862Speter		m_freem(m);
410427862Speter		m_freem(m0);
410527862Speter		return NULL;
410627862Speter	    }
410727862Speter	}
410827862Speter	m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t));
410927862Speter	m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
411027862Speter    }
411127862Speter#else
411227862Speter    int mlen = MHLEN;
411327862Speter    int len = m->m_pkthdr.len;
411427862Speter    struct mbuf **mp = &m0;
411527862Speter
411627862Speter    while (len > 0) {
411727862Speter	if (mlen == MHLEN) {
411827862Speter	    MGETHDR(*mp, M_DONTWAIT, MT_DATA);
411927862Speter	} else {
412027862Speter	    MGET(*mp, M_DONTWAIT, MT_DATA);
412127862Speter	}
412227862Speter	if (*mp == NULL) {
412327862Speter	    m_freem(m0);
412427862Speter	    m0 = NULL;
412527862Speter	    break;
412627862Speter	}
412727862Speter	if (len > MLEN) {
412827862Speter	    MCLGET(*mp, M_DONTWAIT);
412927862Speter	    if (((*mp)->m_flags & M_EXT) == 0) {
413027862Speter		m_freem(m0);
413127862Speter		m0 = NULL;
413227862Speter		break;
413327862Speter	    }
413427862Speter	    (*mp)->m_len = len <= MCLBYTES ? len : MCLBYTES;
413527862Speter	} else {
413627862Speter	    (*mp)->m_len = len <= mlen ? len : mlen;
413727862Speter	}
413827862Speter	m_copydata(m, m->m_pkthdr.len - len,
413927862Speter		   (*mp)->m_len, mtod((*mp), caddr_t));
414027862Speter	len -= (*mp)->m_len;
414127862Speter	mp = &(*mp)->m_next;
414227862Speter	mlen = MLEN;
414327862Speter    }
414427862Speter#endif
414527862Speter    m_freem(m);
414627862Speter    return m0;
414727862Speter}
414827862Speter
414927862Speterstatic struct mbuf *
415027862Spetertulip_txput(
415127862Speter    tulip_softc_t * const sc,
415227862Speter    struct mbuf *m)
415327862Speter{
415427862Speter    TULIP_PERFSTART(txput)
415527862Speter    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
415627862Speter    tulip_desc_t *eop, *nextout;
415727862Speter    int segcnt, free;
415827862Speter    u_int32_t d_status;
415934317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
416034317Speter    bus_dmamap_t map;
416134317Speter    int error;
416234317Speter#else
416327862Speter    struct mbuf *m0;
416434317Speter#endif
416527862Speter
416627862Speter#if defined(TULIP_DEBUG)
416727862Speter    if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
416849572Speter	printf("%s%d: txput%s: tx not running\n",
416949572Speter	       sc->tulip_name, sc->tulip_unit,
417027862Speter	       (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) ? "(probe)" : "");
417127862Speter	sc->tulip_flags |= TULIP_WANTTXSTART;
417240290Speter	sc->tulip_dbg.dbg_txput_finishes[0]++;
417327862Speter	goto finish;
417427862Speter    }
417527862Speter#endif
417627862Speter
417727862Speter    /*
417827862Speter     * Now we try to fill in our transmit descriptors.  This is
417927862Speter     * a bit reminiscent of going on the Ark two by two
418027862Speter     * since each descriptor for the TULIP can describe
418127862Speter     * two buffers.  So we advance through packet filling
418227862Speter     * each of the two entries at a time to to fill each
418327862Speter     * descriptor.  Clear the first and last segment bits
418427862Speter     * in each descriptor (actually just clear everything
418527862Speter     * but the end-of-ring or chain bits) to make sure
418627862Speter     * we don't get messed up by previously sent packets.
418727862Speter     *
418827862Speter     * We may fail to put the entire packet on the ring if
418927862Speter     * there is either not enough ring entries free or if the
419027862Speter     * packet has more than MAX_TXSEG segments.  In the former
419127862Speter     * case we will just wait for the ring to empty.  In the
419227862Speter     * latter case we have to recopy.
419327862Speter     */
419434317Speter#if !defined(TULIP_BUS_DMA) || defined(TULIP_BUS_DMA_NOTX)
419540163Speter  again:
419634317Speter    m0 = m;
419734317Speter#endif
419827862Speter    d_status = 0;
419927862Speter    eop = nextout = ri->ri_nextout;
420027862Speter    segcnt = 0;
420127862Speter    free = ri->ri_free;
420234317Speter
420334317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
420440290Speter    /*
420540290Speter     * Reclaim some dma maps from if we are out.
420640290Speter     */
420740290Speter    if (sc->tulip_txmaps_free == 0) {
420840290Speter#if defined(TULIP_DEBUG)
420940290Speter	sc->tulip_dbg.dbg_no_txmaps++;
421040290Speter#endif
421140290Speter	free += tulip_tx_intr(sc);
421240290Speter    }
421334317Speter    if (sc->tulip_txmaps_free > 0) {
421440290Speter	map = sc->tulip_txmaps[sc->tulip_txmaps_free-1];
421534317Speter    } else {
421634317Speter	sc->tulip_flags |= TULIP_WANTTXSTART;
421740290Speter#if defined(TULIP_DEBUG)
421840290Speter	sc->tulip_dbg.dbg_txput_finishes[1]++;
421940290Speter#endif
422034317Speter	goto finish;
422134317Speter    }
422234317Speter    error = bus_dmamap_load_mbuf(sc->tulip_dmatag, map, m, BUS_DMA_NOWAIT);
422340290Speter    if (error != 0) {
422440290Speter	if (error == EFBIG) {
422540290Speter	    /*
422640290Speter	     * The packet exceeds the number of transmit buffer
422740290Speter	     * entries that we can use for one packet, so we have
422840290Speter	     * to recopy it into one mbuf and then try again.
422940290Speter	     */
423040290Speter	    m = tulip_mbuf_compress(m);
423140290Speter	    if (m == NULL) {
423240290Speter#if defined(TULIP_DEBUG)
423340290Speter		sc->tulip_dbg.dbg_txput_finishes[2]++;
423440290Speter#endif
423540290Speter		goto finish;
423640290Speter	    }
423740290Speter	    error = bus_dmamap_load_mbuf(sc->tulip_dmatag, map, m, BUS_DMA_NOWAIT);
423840290Speter	}
423940290Speter	if (error != 0) {
424049572Speter	    printf("%s%d: unable to load tx map, "
424149572Speter		   "error = %d\n", sc->tulip_name, sc->tulip_unit, error);
424240290Speter#if defined(TULIP_DEBUG)
424340290Speter	    sc->tulip_dbg.dbg_txput_finishes[3]++;
424440290Speter#endif
424534317Speter	    goto finish;
424634317Speter	}
424734317Speter    }
424834317Speter    if ((free -= (map->dm_nsegs + 1) / 2) <= 0
424934317Speter	    /*
425034317Speter	     * See if there's any unclaimed space in the transmit ring.
425134317Speter	     */
425240290Speter	    && (free += tulip_tx_intr(sc)) <= 0) {
425334317Speter	/*
425434317Speter	 * There's no more room but since nothing
425534317Speter	 * has been committed at this point, just
425634317Speter	 * show output is active, put back the
425734317Speter	 * mbuf and return.
425834317Speter	 */
425934317Speter	sc->tulip_flags |= TULIP_WANTTXSTART;
426040290Speter#if defined(TULIP_DEBUG)
426140290Speter	sc->tulip_dbg.dbg_txput_finishes[4]++;
426240290Speter#endif
426340290Speter	bus_dmamap_unload(sc->tulip_dmatag, map);
426434317Speter	goto finish;
426534317Speter    }
426634317Speter    for (; map->dm_nsegs - segcnt > 1; segcnt += 2) {
426734317Speter	eop = nextout;
426834317Speter	eop->d_flag   &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
426934317Speter	eop->d_status  = d_status;
427034317Speter	eop->d_addr1   = map->dm_segs[segcnt].ds_addr;
427134317Speter	eop->d_length1 = map->dm_segs[segcnt].ds_len;
427234317Speter	eop->d_addr2   = map->dm_segs[segcnt+1].ds_addr;
427334317Speter	eop->d_length2 = map->dm_segs[segcnt+1].ds_len;
427434317Speter	d_status = TULIP_DSTS_OWNER;
427534317Speter	if (++nextout == ri->ri_last)
427634317Speter	    nextout = ri->ri_first;
427734317Speter    }
427834317Speter    if (segcnt < map->dm_nsegs) {
427934317Speter	eop = nextout;
428034317Speter	eop->d_flag   &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
428134317Speter	eop->d_status  = d_status;
428234317Speter	eop->d_addr1   = map->dm_segs[segcnt].ds_addr;
428334317Speter	eop->d_length1 = map->dm_segs[segcnt].ds_len;
428434317Speter	eop->d_addr2   = 0;
428534317Speter	eop->d_length2 = 0;
428634317Speter	if (++nextout == ri->ri_last)
428734317Speter	    nextout = ri->ri_first;
428834317Speter    }
428934317Speter    TULIP_TXMAP_PRESYNC(sc, map);
429034317Speter    M_SETCTX(m, map);
429134317Speter    map = NULL;
429240290Speter    --sc->tulip_txmaps_free;		/* commit to using the dmamap */
429334317Speter
429434317Speter#else /* !TULIP_BUS_DMA */
429534317Speter
429627862Speter    do {
429727862Speter	int len = m0->m_len;
429827862Speter	caddr_t addr = mtod(m0, caddr_t);
429949572Speter	unsigned clsize = PAGE_SIZE - (((uintptr_t) addr) & (PAGE_SIZE-1));
430027862Speter
430127862Speter	while (len > 0) {
430227862Speter	    unsigned slen = min(len, clsize);
430327862Speter#ifdef BIG_PACKET
430427862Speter	    int partial = 0;
430527862Speter	    if (slen >= 2048)
430627862Speter		slen = 2040, partial = 1;
430727862Speter#endif
430827862Speter	    segcnt++;
430927862Speter	    if (segcnt > TULIP_MAX_TXSEG) {
431027862Speter		/*
431127862Speter		 * The packet exceeds the number of transmit buffer
431227862Speter		 * entries that we can use for one packet, so we have
431327862Speter		 * recopy it into one mbuf and then try again.
431427862Speter		 */
431527862Speter		m = tulip_mbuf_compress(m);
431627862Speter		if (m == NULL)
431727862Speter		    goto finish;
431827862Speter		goto again;
431927862Speter	    }
432027862Speter	    if (segcnt & 1) {
432127862Speter		if (--free == 0) {
432227862Speter		    /*
432327862Speter		     * See if there's any unclaimed space in the
432427862Speter		     * transmit ring.
432527862Speter		     */
432627862Speter		    if ((free += tulip_tx_intr(sc)) == 0) {
432727862Speter			/*
432827862Speter			 * There's no more room but since nothing
432927862Speter			 * has been committed at this point, just
433027862Speter			 * show output is active, put back the
433127862Speter			 * mbuf and return.
433227862Speter			 */
433327862Speter			sc->tulip_flags |= TULIP_WANTTXSTART;
433440290Speter#if defined(TULIP_DEBUG)
433540290Speter			sc->tulip_dbg.dbg_txput_finishes[1]++;
433640290Speter#endif
433727862Speter			goto finish;
433827862Speter		    }
433927862Speter		}
434027862Speter		eop = nextout;
434127862Speter		if (++nextout == ri->ri_last)
434227862Speter		    nextout = ri->ri_first;
434327862Speter		eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
434427862Speter		eop->d_status = d_status;
434527862Speter		eop->d_addr1 = TULIP_KVATOPHYS(sc, addr);
434627862Speter		eop->d_length1 = slen;
434727862Speter	    } else {
434827862Speter		/*
434927862Speter		 *  Fill in second half of descriptor
435027862Speter		 */
435127862Speter		eop->d_addr2 = TULIP_KVATOPHYS(sc, addr);
435227862Speter		eop->d_length2 = slen;
435327862Speter	    }
435427862Speter	    d_status = TULIP_DSTS_OWNER;
435527862Speter	    len -= slen;
435627862Speter	    addr += slen;
435727862Speter#ifdef BIG_PACKET
435827862Speter	    if (partial)
435927862Speter		continue;
436027862Speter#endif
436149572Speter	    clsize = PAGE_SIZE;
436227862Speter	}
436327862Speter    } while ((m0 = m0->m_next) != NULL);
436434317Speter#endif /* TULIP_BUS_DMA */
436527862Speter
436627862Speter    /*
436727862Speter     * The descriptors have been filled in.  Now get ready
436827862Speter     * to transmit.
436927862Speter     */
437027862Speter    IF_ENQUEUE(&sc->tulip_txq, m);
437127862Speter    m = NULL;
437227862Speter
437327862Speter    /*
437427862Speter     * Make sure the next descriptor after this packet is owned
437527862Speter     * by us since it may have been set up above if we ran out
437627862Speter     * of room in the ring.
437727862Speter     */
437827862Speter    nextout->d_status = 0;
437934317Speter    TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(u_int32_t));
438027862Speter
438134317Speter#if !defined(TULIP_BUS_DMA) || defined(TULIP_BUS_DMA_NOTX)
438227862Speter    /*
438327862Speter     * If we only used the first segment of the last descriptor,
438427862Speter     * make sure the second segment will not be used.
438527862Speter     */
438627862Speter    if (segcnt & 1) {
438727862Speter	eop->d_addr2 = 0;
438827862Speter	eop->d_length2 = 0;
438927862Speter    }
439034317Speter#endif /* TULIP_BUS_DMA */
439127862Speter
439227862Speter    /*
439327862Speter     * Mark the last and first segments, indicate we want a transmit
439427862Speter     * complete interrupt, and tell it to transmit!
439527862Speter     */
439627862Speter    eop->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
439727862Speter
439827862Speter    /*
439927862Speter     * Note that ri->ri_nextout is still the start of the packet
440027862Speter     * and until we set the OWNER bit, we can still back out of
440127862Speter     * everything we have done.
440227862Speter     */
440327862Speter    ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
440434317Speter#if defined(TULIP_BUS_MAP) && !defined(TULIP_BUS_DMA_NOTX)
440534317Speter    if (eop < ri->ri_nextout) {
440634317Speter	TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout,
440734317Speter			     (caddr_t) ri->ri_last - (caddr_t) ri->ri_nextout);
440834317Speter	TULIP_TXDESC_PRESYNC(sc, ri->ri_first,
440934317Speter			     (caddr_t) (eop + 1) - (caddr_t) ri->ri_first);
441034317Speter    } else {
441134317Speter	TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout,
441234317Speter			     (caddr_t) (eop + 1) - (caddr_t) ri->ri_nextout);
441334317Speter    }
441434317Speter#endif
441527862Speter    ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
441634317Speter    TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout, sizeof(u_int32_t));
441727862Speter
441827862Speter    /*
441927862Speter     * This advances the ring for us.
442027862Speter     */
442127862Speter    ri->ri_nextout = nextout;
442227862Speter    ri->ri_free = free;
442327862Speter
442427862Speter    TULIP_PERFEND(txput);
442527862Speter
442627862Speter    if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
442744738Speter	TULIP_CSR_WRITE(sc, csr_txpoll, 1);
442827862Speter	sc->tulip_if.if_flags |= IFF_OACTIVE;
442940290Speter	sc->tulip_if.if_start = tulip_ifstart;
443027862Speter	TULIP_PERFEND(txput);
443127862Speter	return NULL;
443227862Speter    }
443327862Speter
443427862Speter    /*
443527862Speter     * switch back to the single queueing ifstart.
443627862Speter     */
443727862Speter    sc->tulip_flags &= ~TULIP_WANTTXSTART;
443827862Speter    if (sc->tulip_txtimer == 0)
443927862Speter	sc->tulip_txtimer = TULIP_TXTIMER;
444040290Speter#if defined(TULIP_DEBUG)
444140290Speter    sc->tulip_dbg.dbg_txput_finishes[5]++;
444240290Speter#endif
444327862Speter
444427862Speter    /*
444527862Speter     * If we want a txstart, there must be not enough space in the
444627862Speter     * transmit ring.  So we want to enable transmit done interrupts
444727862Speter     * so we can immediately reclaim some space.  When the transmit
444827862Speter     * interrupt is posted, the interrupt handler will call tx_intr
444927862Speter     * to reclaim space and then txstart (since WANTTXSTART is set).
445027862Speter     * txstart will move the packet into the transmit ring and clear
445127862Speter     * WANTTXSTART thereby causing TXINTR to be cleared.
445227862Speter     */
445327862Speter  finish:
445440290Speter#if defined(TULIP_DEBUG)
445540290Speter    sc->tulip_dbg.dbg_txput_finishes[6]++;
445640290Speter#endif
445727862Speter    if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_DOINGSETUP)) {
445827862Speter	sc->tulip_if.if_flags |= IFF_OACTIVE;
445927862Speter	sc->tulip_if.if_start = tulip_ifstart;
446027862Speter	if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
446127862Speter	    sc->tulip_intrmask |= TULIP_STS_TXINTR;
446227862Speter	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
446327862Speter	}
446427862Speter    } else if ((sc->tulip_flags & TULIP_PROMISC) == 0) {
446527862Speter	if (sc->tulip_intrmask & TULIP_STS_TXINTR) {
446627862Speter	    sc->tulip_intrmask &= ~TULIP_STS_TXINTR;
446727862Speter	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
446827862Speter	}
446927862Speter    }
447044738Speter    TULIP_CSR_WRITE(sc, csr_txpoll, 1);
447127862Speter    TULIP_PERFEND(txput);
447227862Speter    return m;
447327862Speter}
447427862Speter
447527862Speterstatic void
447627862Spetertulip_txput_setup(
447727862Speter    tulip_softc_t * const sc)
447827862Speter{
447927862Speter    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
448027862Speter    tulip_desc_t *nextout;
448127862Speter
448227862Speter    /*
448327862Speter     * We will transmit, at most, one setup packet per call to ifstart.
448427862Speter     */
448527862Speter
448627862Speter#if defined(TULIP_DEBUG)
448727862Speter    if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
448849572Speter	printf("%s%d: txput_setup: tx not running\n",
448949572Speter	       sc->tulip_name, sc->tulip_unit);
449027862Speter	sc->tulip_flags |= TULIP_WANTTXSTART;
449127862Speter	sc->tulip_if.if_start = tulip_ifstart;
449227862Speter	return;
449327862Speter    }
449427862Speter#endif
449527862Speter    /*
449627862Speter     * Try to reclaim some free descriptors..
449727862Speter     */
449827862Speter    if (ri->ri_free < 2)
449927862Speter	tulip_tx_intr(sc);
450027862Speter    if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
450127862Speter	sc->tulip_flags |= TULIP_WANTTXSTART;
450227862Speter	sc->tulip_if.if_start = tulip_ifstart;
450327862Speter	return;
450427862Speter    }
450527862Speter    bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
450627862Speter	  sizeof(sc->tulip_setupbuf));
450727862Speter    /*
450827862Speter     * Clear WANTSETUP and set DOINGSETUP.  Set know that WANTSETUP is
450927862Speter     * set and DOINGSETUP is clear doing an XOR of the two will DTRT.
451027862Speter     */
451127862Speter    sc->tulip_flags ^= TULIP_WANTSETUP|TULIP_DOINGSETUP;
451227862Speter    ri->ri_free--;
451327862Speter    nextout = ri->ri_nextout;
451427862Speter    nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
451527862Speter    nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
451627862Speter	|TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
451727862Speter    if (sc->tulip_flags & TULIP_WANTHASHPERFECT)
451827862Speter	nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
451927862Speter    else if (sc->tulip_flags & TULIP_WANTHASHONLY)
452027862Speter	nextout->d_flag |= TULIP_DFLAG_TxHASHFILT|TULIP_DFLAG_TxINVRSFILT;
452127862Speter
452234317Speter    nextout->d_length2 = 0;
452334317Speter    nextout->d_addr2 = 0;
452434317Speter#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
452534317Speter    nextout->d_length1 = sc->tulip_setupmap->dm_segs[0].ds_len;
452634317Speter    nextout->d_addr1 = sc->tulip_setupmap->dm_segs[0].ds_addr;
452734317Speter    if (sc->tulip_setupmap->dm_nsegs == 2) {
452834317Speter	nextout->d_length2 = sc->tulip_setupmap->dm_segs[1].ds_len;
452934317Speter	nextout->d_addr2 = sc->tulip_setupmap->dm_segs[1].ds_addr;
453034317Speter    }
453134317Speter    TULIP_TXMAP_PRESYNC(sc, sc->tulip_setupmap);
453234317Speter    TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(*nextout));
453334317Speter#else
453427862Speter    nextout->d_length1 = sizeof(sc->tulip_setupbuf);
453527862Speter    nextout->d_addr1 = TULIP_KVATOPHYS(sc, sc->tulip_setupbuf);
453634317Speter#endif
453727862Speter
453827862Speter    /*
453927862Speter     * Advance the ring for the next transmit packet.
454027862Speter     */
454127862Speter    if (++ri->ri_nextout == ri->ri_last)
454227862Speter	ri->ri_nextout = ri->ri_first;
454327862Speter
454427862Speter    /*
454527862Speter     * Make sure the next descriptor is owned by us since it
454627862Speter     * may have been set up above if we ran out of room in the
454727862Speter     * ring.
454827862Speter     */
454927862Speter    ri->ri_nextout->d_status = 0;
455034317Speter    TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout, sizeof(u_int32_t));
455127862Speter    nextout->d_status = TULIP_DSTS_OWNER;
455234317Speter    /*
455334317Speter     * Flush the ownwership of the current descriptor
455434317Speter     */
455534317Speter    TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(u_int32_t));
455627862Speter    TULIP_CSR_WRITE(sc, csr_txpoll, 1);
455727862Speter    if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
455827862Speter	sc->tulip_intrmask |= TULIP_STS_TXINTR;
455927862Speter	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
456027862Speter    }
456127862Speter}
456227862Speter
456327862Speter
45643278Swollman/*
456526797Speter * This routine is entered at splnet() (splsoftnet() on NetBSD)
456626797Speter * and thereby imposes no problems when TULIP_USE_SOFTINTR is
456726797Speter * defined or not.
45687689Sdg */
45693278Swollmanstatic int
457016357Sdgtulip_ifioctl(
457127862Speter    struct ifnet * ifp,
457249575Speter    u_long cmd,
45733278Swollman    caddr_t data)
45743278Swollman{
457527862Speter    TULIP_PERFSTART(ifioctl)
457649572Speter    tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
457726797Speter    struct ifaddr *ifa = (struct ifaddr *)data;
45784437Sdg    struct ifreq *ifr = (struct ifreq *) data;
457949572Speter    int s;
458018357Sdg    int error = 0;
45813278Swollman
458218357Sdg#if defined(TULIP_USE_SOFTINTR)
458349572Speter    s = splnet();
458418357Sdg#else
458549572Speter    s = splimp();
458618357Sdg#endif
45873278Swollman    switch (cmd) {
458826797Speter	case SIOCSIFADDR: {
458926797Speter	    ifp->if_flags |= IFF_UP;
459026797Speter	    switch(ifa->ifa_addr->sa_family) {
459126797Speter#ifdef INET
459226797Speter		case AF_INET: {
459326797Speter		    tulip_init(sc);
459449572Speter		    arp_ifinit(&(sc)->tulip_ac, ifa);
459526797Speter		    break;
459626797Speter		}
459726797Speter#endif /* INET */
45983278Swollman
459930342Speter#ifdef IPX
460030342Speter		case AF_IPX: {
460130342Speter		    struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
460230342Speter		    if (ipx_nullhost(*ina)) {
460330342Speter			ina->x_host = *(union ipx_host *)(sc->tulip_enaddr);
460430342Speter		    } else {
460530342Speter			ifp->if_flags &= ~IFF_RUNNING;
460630342Speter			bcopy((caddr_t)ina->x_host.c_host,
460730342Speter			      (caddr_t)sc->tulip_enaddr,
460830342Speter			      sizeof(sc->tulip_enaddr));
460930342Speter		    }
461030342Speter		    tulip_init(sc);
461130342Speter		    break;
461230342Speter		}
461330342Speter#endif /* IPX */
461430342Speter
461526797Speter#ifdef NS
461626797Speter		/*
461726797Speter		 * This magic copied from if_is.c; I don't use XNS,
461826797Speter		 * so I have no way of telling if this actually
461926797Speter		 * works or not.
462026797Speter		 */
462126797Speter		case AF_NS: {
462226797Speter		    struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
462326797Speter		    if (ns_nullhost(*ina)) {
462426797Speter			ina->x_host = *(union ns_host *)(sc->tulip_enaddr);
462526797Speter		    } else {
462626797Speter			ifp->if_flags &= ~IFF_RUNNING;
462726797Speter			bcopy((caddr_t)ina->x_host.c_host,
462826797Speter			      (caddr_t)sc->tulip_enaddr,
462926797Speter			      sizeof(sc->tulip_enaddr));
463026797Speter		    }
463126797Speter		    tulip_init(sc);
463226797Speter		    break;
463316357Sdg		}
463426797Speter#endif /* NS */
463526797Speter
463626797Speter		default: {
463726797Speter		    tulip_init(sc);
463826797Speter		    break;
463916357Sdg		}
46403278Swollman	    }
464126797Speter	    break;
464226797Speter	}
464326797Speter	case SIOCGIFADDR: {
464426797Speter	    bcopy((caddr_t) sc->tulip_enaddr,
464526797Speter		  (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
464626797Speter		  6);
464726797Speter	    break;
464826797Speter	}
464926797Speter
465026797Speter	case SIOCSIFFLAGS: {
465144377Sluigi	    tulip_addr_filter(sc); /* reinit multicast filter */
46528754Sdg	    tulip_init(sc);
46533278Swollman	    break;
46543278Swollman	}
46553278Swollman
465626797Speter	case SIOCSIFMEDIA:
465726797Speter	case SIOCGIFMEDIA: {
465826797Speter	    error = ifmedia_ioctl(ifp, ifr, &sc->tulip_ifmedia, cmd);
465926797Speter	    break;
466026797Speter	}
466126797Speter
46623278Swollman	case SIOCADDMULTI:
466326797Speter	case SIOCDELMULTI: {
46643278Swollman	    /*
46653278Swollman	     * Update multicast listeners
46663278Swollman	     */
466721666Swollman	    tulip_addr_filter(sc);		/* reset multicast filtering */
466821666Swollman	    tulip_init(sc);
466921666Swollman	    error = 0;
467021666Swollman	    break;
467126797Speter	}
467249572Speter
46734437Sdg	case SIOCSIFMTU:
46744437Sdg	    /*
46754437Sdg	     * Set the interface MTU.
46764437Sdg	     */
467716357Sdg	    if (ifr->ifr_mtu > ETHERMTU
467816357Sdg#ifdef BIG_PACKET
467920060Srgrimes		    && sc->tulip_chipid != TULIP_21140
468020060Srgrimes		    && sc->tulip_chipid != TULIP_21140A
468120060Srgrimes		    && sc->tulip_chipid != TULIP_21041
468216357Sdg#endif
468316357Sdg		) {
46844437Sdg		error = EINVAL;
468511070Sdg		break;
46864437Sdg	    }
468711070Sdg	    ifp->if_mtu = ifr->ifr_mtu;
468816357Sdg#ifdef BIG_PACKET
468916357Sdg	    tulip_reset(sc);
469016357Sdg	    tulip_init(sc);
469116357Sdg#endif
46924437Sdg	    break;
46933278Swollman
469426797Speter#ifdef SIOCGADDRROM
469526797Speter	case SIOCGADDRROM: {
469626797Speter	    error = copyout(sc->tulip_rombuf, ifr->ifr_data, sizeof(sc->tulip_rombuf));
469726797Speter	    break;
469826797Speter	}
469926797Speter#endif
470026797Speter#ifdef SIOCGCHIPID
470126797Speter	case SIOCGCHIPID: {
470226797Speter	    ifr->ifr_metric = (int) sc->tulip_chipid;
470326797Speter	    break;
470426797Speter	}
470526797Speter#endif
47063278Swollman	default: {
47073278Swollman	    error = EINVAL;
47083278Swollman	    break;
47093278Swollman	}
47103278Swollman    }
47113278Swollman
471249572Speter    splx(s);
471327862Speter    TULIP_PERFEND(ifioctl);
47143278Swollman    return error;
47153278Swollman}
47163278Swollman
471718357Sdg/*
471827862Speter * These routines gets called at device spl (from ether_output).  This might
471926797Speter * pose a problem for TULIP_USE_SOFTINTR if ether_output is called at
472026797Speter * device spl from another driver.
472118357Sdg */
472227862Speter
472349575Speterstatic void
472418357Sdgtulip_ifstart(
472518357Sdg    struct ifnet * const ifp)
472618357Sdg{
472727862Speter    TULIP_PERFSTART(ifstart)
472849572Speter    tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
472918357Sdg
473027862Speter    if (sc->tulip_if.if_flags & IFF_RUNNING) {
473118357Sdg
473227862Speter	if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
473327862Speter	    tulip_txput_setup(sc);
473418357Sdg
473527862Speter	while (sc->tulip_if.if_snd.ifq_head != NULL) {
473627862Speter	    struct mbuf *m;
473727862Speter	    IF_DEQUEUE(&sc->tulip_if.if_snd, m);
473827862Speter	    if ((m = tulip_txput(sc, m)) != NULL) {
473927862Speter		IF_PREPEND(&sc->tulip_if.if_snd, m);
474027862Speter		break;
474126797Speter	    }
474218357Sdg	}
474340290Speter	if (sc->tulip_if.if_snd.ifq_head == NULL)
474440290Speter	    sc->tulip_if.if_start = tulip_ifstart_one;
474527862Speter    }
474618357Sdg
474727862Speter    TULIP_PERFEND(ifstart);
474827862Speter}
474918357Sdg
475049575Speterstatic void
475127862Spetertulip_ifstart_one(
475227862Speter    struct ifnet * const ifp)
475327862Speter{
475427862Speter    TULIP_PERFSTART(ifstart_one)
475549572Speter    tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
475618357Sdg
475727862Speter    if ((sc->tulip_if.if_flags & IFF_RUNNING)
475827862Speter	    && sc->tulip_if.if_snd.ifq_head != NULL) {
475927862Speter	struct mbuf *m;
476027862Speter	IF_DEQUEUE(&sc->tulip_if.if_snd, m);
476127862Speter	if ((m = tulip_txput(sc, m)) != NULL)
476227862Speter	    IF_PREPEND(&sc->tulip_if.if_snd, m);
476318357Sdg    }
476427862Speter    TULIP_PERFEND(ifstart_one);
476518357Sdg}
476618357Sdg
476718357Sdg/*
476826797Speter * Even though this routine runs at device spl, it does not break
476918357Sdg * our use of splnet (splsoftnet under NetBSD) for the majority
477018357Sdg * of this driver (if TULIP_USE_SOFTINTR defined) since
477118357Sdg * if_watcbog is called from if_watchdog which is called from
477226797Speter * splsoftclock which is below spl[soft]net.
477318357Sdg */
47743278Swollmanstatic void
477516357Sdgtulip_ifwatchdog(
477616357Sdg    struct ifnet *ifp)
477716357Sdg{
477827862Speter    TULIP_PERFSTART(ifwatchdog)
477949572Speter    tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
478016357Sdg
478116357Sdg#if defined(TULIP_DEBUG)
478220060Srgrimes    u_int32_t rxintrs = sc->tulip_dbg.dbg_rxintrs - sc->tulip_dbg.dbg_last_rxintrs;
478316357Sdg    if (rxintrs > sc->tulip_dbg.dbg_high_rxintrs_hz)
478416357Sdg	sc->tulip_dbg.dbg_high_rxintrs_hz = rxintrs;
478516357Sdg    sc->tulip_dbg.dbg_last_rxintrs = sc->tulip_dbg.dbg_rxintrs;
478616357Sdg#endif /* TULIP_DEBUG */
478716357Sdg
478816357Sdg    sc->tulip_if.if_timer = 1;
478916357Sdg    /*
479016357Sdg     * These should be rare so do a bulk test up front so we can just skip
479116357Sdg     * them if needed.
479216357Sdg     */
479326797Speter    if (sc->tulip_flags & (TULIP_SYSTEMERROR|TULIP_RXBUFSLOW|TULIP_NOMESSAGES)) {
479416357Sdg	/*
479516357Sdg	 * If the number of receive buffer is low, try to refill
479616357Sdg	 */
479716357Sdg	if (sc->tulip_flags & TULIP_RXBUFSLOW)
479816357Sdg	    tulip_rx_intr(sc);
479916357Sdg
480016357Sdg	if (sc->tulip_flags & TULIP_SYSTEMERROR) {
480149572Speter	    printf("%s%d: %d system errors: last was %s\n",
480249572Speter		   sc->tulip_name, sc->tulip_unit, sc->tulip_system_errors,
480316357Sdg		   tulip_system_errors[sc->tulip_last_system_error]);
480416357Sdg	}
480516357Sdg	if (sc->tulip_statusbits) {
480616357Sdg	    tulip_print_abnormal_interrupt(sc, sc->tulip_statusbits);
480716357Sdg	    sc->tulip_statusbits = 0;
480816357Sdg	}
480916357Sdg
481016357Sdg	sc->tulip_flags &= ~(TULIP_NOMESSAGES|TULIP_SYSTEMERROR);
481116357Sdg    }
481216357Sdg
481327862Speter    if (sc->tulip_txtimer)
481427862Speter	tulip_tx_intr(sc);
481516357Sdg    if (sc->tulip_txtimer && --sc->tulip_txtimer == 0) {
481649572Speter	printf("%s%d: transmission timeout\n", sc->tulip_name, sc->tulip_unit);
481726797Speter	if (TULIP_DO_AUTOSENSE(sc)) {
481826797Speter	    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
481926797Speter	    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
482026797Speter	    sc->tulip_flags &= ~(TULIP_WANTRXACT|TULIP_LINKUP);
482126797Speter	}
482216357Sdg	tulip_reset(sc);
482316357Sdg	tulip_init(sc);
482416357Sdg    }
482527862Speter
482627862Speter    TULIP_PERFEND(ifwatchdog);
482727862Speter    TULIP_PERFMERGE(sc, perf_intr_cycles);
482827862Speter    TULIP_PERFMERGE(sc, perf_ifstart_cycles);
482927862Speter    TULIP_PERFMERGE(sc, perf_ifioctl_cycles);
483027862Speter    TULIP_PERFMERGE(sc, perf_ifwatchdog_cycles);
483127862Speter    TULIP_PERFMERGE(sc, perf_timeout_cycles);
483227862Speter    TULIP_PERFMERGE(sc, perf_ifstart_one_cycles);
483327862Speter    TULIP_PERFMERGE(sc, perf_txput_cycles);
483427862Speter    TULIP_PERFMERGE(sc, perf_txintr_cycles);
483527862Speter    TULIP_PERFMERGE(sc, perf_rxintr_cycles);
483627862Speter    TULIP_PERFMERGE(sc, perf_rxget_cycles);
483727862Speter    TULIP_PERFMERGE(sc, perf_intr);
483827862Speter    TULIP_PERFMERGE(sc, perf_ifstart);
483927862Speter    TULIP_PERFMERGE(sc, perf_ifioctl);
484027862Speter    TULIP_PERFMERGE(sc, perf_ifwatchdog);
484127862Speter    TULIP_PERFMERGE(sc, perf_timeout);
484227862Speter    TULIP_PERFMERGE(sc, perf_ifstart_one);
484327862Speter    TULIP_PERFMERGE(sc, perf_txput);
484427862Speter    TULIP_PERFMERGE(sc, perf_txintr);
484527862Speter    TULIP_PERFMERGE(sc, perf_rxintr);
484627862Speter    TULIP_PERFMERGE(sc, perf_rxget);
484716357Sdg}
484816357Sdg
484916357Sdg/*
485016357Sdg * All printf's are real as of now!
485116357Sdg */
485216357Sdg#ifdef printf
485316357Sdg#undef printf
485416357Sdg#endif
485516357Sdg
485616357Sdgstatic void
48573278Swollmantulip_attach(
48588754Sdg    tulip_softc_t * const sc)
48593278Swollman{
48608754Sdg    struct ifnet * const ifp = &sc->tulip_if;
48613278Swollman
486249572Speter    ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
486316357Sdg    ifp->if_ioctl = tulip_ifioctl;
486416357Sdg    ifp->if_start = tulip_ifstart;
486516357Sdg    ifp->if_watchdog = tulip_ifwatchdog;
486616357Sdg    ifp->if_timer = 1;
48673278Swollman    ifp->if_output = ether_output;
486811070Sdg
486949572Speter    printf("%s%d: %s%s pass %d.%d%s\n",
487049572Speter	   sc->tulip_name, sc->tulip_unit,
487116357Sdg	   sc->tulip_boardid,
48728296Sdg	   tulip_chipdescs[sc->tulip_chipid],
48733278Swollman	   (sc->tulip_revinfo & 0xF0) >> 4,
487431041Speter	   sc->tulip_revinfo & 0x0F,
487531041Speter	   (sc->tulip_features & (TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM))
487631041Speter		 == TULIP_HAVE_ISVSROM ? " (invalid EESPROM checksum)" : "");
487749572Speter    printf("%s%d: address %6D\n",
487849572Speter	   sc->tulip_name, sc->tulip_unit, sc->tulip_enaddr, ":");
48793278Swollman
488026797Speter#if defined(__alpha__)
488126797Speter    /*
488226797Speter     * In case the SRM console told us about a bogus media,
488326797Speter     * we need to check to be safe.
488426797Speter     */
488526797Speter    if (sc->tulip_mediums[sc->tulip_media] == NULL)
488626797Speter	sc->tulip_media = TULIP_MEDIA_UNKNOWN;
488726797Speter#endif
488816357Sdg
488926797Speter    (*sc->tulip_boardsw->bd_media_probe)(sc);
489026797Speter    ifmedia_init(&sc->tulip_ifmedia, 0,
489126797Speter		 tulip_ifmedia_change,
489226797Speter		 tulip_ifmedia_status);
489326797Speter    sc->tulip_flags &= ~TULIP_DEVICEPROBE;
489426797Speter    tulip_ifmedia_add(sc);
48958296Sdg
48968754Sdg    tulip_reset(sc);
48978296Sdg
48984322Sdg    if_attach(ifp);
489944719Speter    ifp->if_snd.ifq_maxlen = ifqmaxlen;
490049572Speter    ether_ifattach(&(sc)->tulip_if);
49014322Sdg
490249572Speter    bpfattach(&sc->tulip_if, DLT_EN10MB, sizeof(struct ether_header));
49033278Swollman}
49043278Swollman
490534317Speter#if defined(TULIP_BUS_DMA)
490634317Speter#if !defined(TULIP_BUS_DMA_NOTX) || !defined(TULIP_BUS_DMA_NORX)
490734317Speterstatic int
490834317Spetertulip_busdma_allocmem(
490934317Speter    tulip_softc_t * const sc,
491034317Speter    size_t size,
491134317Speter    bus_dmamap_t *map_p,
491234317Speter    tulip_desc_t **desc_p)
491334317Speter{
491434317Speter    bus_dma_segment_t segs[1];
491534317Speter    int nsegs, error;
491649572Speter    error = bus_dmamem_alloc(sc->tulip_dmatag, size, 1, PAGE_SIZE,
491734317Speter			     segs, sizeof(segs)/sizeof(segs[0]),
491834317Speter			     &nsegs, BUS_DMA_NOWAIT);
491934317Speter    if (error == 0) {
492034317Speter	void *desc;
492134317Speter	error = bus_dmamem_map(sc->tulip_dmatag, segs, nsegs, size,
492234317Speter			       (void *) &desc, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
492334317Speter	if (error == 0) {
492434317Speter	    bus_dmamap_t map;
492534317Speter	    error = bus_dmamap_create(sc->tulip_dmatag, size, 1, size, 0,
492634317Speter				      BUS_DMA_NOWAIT, &map);
492734317Speter	    if (error == 0) {
492834317Speter		error = bus_dmamap_load(sc->tulip_dmatag, map, desc,
492934317Speter					size, NULL, BUS_DMA_NOWAIT);
493034317Speter		if (error)
493134317Speter		    bus_dmamap_destroy(sc->tulip_dmatag, map);
493234317Speter		else
493334317Speter		    *map_p = map;
493434317Speter	    }
493534317Speter	    if (error)
493634317Speter		bus_dmamem_unmap(sc->tulip_dmatag, desc, size);
493734317Speter	}
493834317Speter	if (error)
493934317Speter	    bus_dmamem_free(sc->tulip_dmatag, segs, nsegs);
494034317Speter	else
494134317Speter	    *desc_p = desc;
494234317Speter    }
494334317Speter    return error;
494434317Speter}
494534317Speter#endif
494634317Speter
494734317Speterstatic int
494834317Spetertulip_busdma_init(
494934317Speter    tulip_softc_t * const sc)
495034317Speter{
495134317Speter    int error = 0;
495234317Speter
495334317Speter#if !defined(TULIP_BUS_DMA_NOTX)
495434317Speter    /*
495534317Speter     * Allocate dmamap for setup descriptor
495634317Speter     */
495734317Speter    error = bus_dmamap_create(sc->tulip_dmatag, sizeof(sc->tulip_setupbuf), 2,
495834317Speter			      sizeof(sc->tulip_setupbuf), 0, BUS_DMA_NOWAIT,
495934317Speter			      &sc->tulip_setupmap);
496034317Speter    if (error == 0) {
496134317Speter	error = bus_dmamap_load(sc->tulip_dmatag, sc->tulip_setupmap,
496234317Speter				sc->tulip_setupbuf, sizeof(sc->tulip_setupbuf),
496334317Speter				NULL, BUS_DMA_NOWAIT);
496434317Speter	if (error)
496534317Speter	    bus_dmamap_destroy(sc->tulip_dmatag, sc->tulip_setupmap);
496634317Speter    }
496734317Speter    /*
496834317Speter     * Allocate space and dmamap for transmit ring
496934317Speter     */
497034317Speter    if (error == 0) {
497134317Speter	error = tulip_busdma_allocmem(sc, sizeof(tulip_desc_t) * TULIP_TXDESCS,
497234317Speter				      &sc->tulip_txdescmap,
497334317Speter				      &sc->tulip_txdescs);
497434317Speter    }
497534317Speter
497634317Speter    /*
497734317Speter     * Allocate dmamaps for each transmit descriptors
497834317Speter     */
497934317Speter    if (error == 0) {
498034317Speter	while (error == 0 && sc->tulip_txmaps_free < TULIP_TXDESCS) {
498134317Speter	    bus_dmamap_t map;
498234317Speter	    if ((error = TULIP_TXMAP_CREATE(sc, &map)) == 0)
498334317Speter		sc->tulip_txmaps[sc->tulip_txmaps_free++] = map;
498434317Speter	}
498534317Speter	if (error) {
498634317Speter	    while (sc->tulip_txmaps_free > 0)
498734317Speter		bus_dmamap_destroy(sc->tulip_dmatag,
498834317Speter				   sc->tulip_txmaps[--sc->tulip_txmaps_free]);
498934317Speter	}
499034317Speter    }
499134317Speter#else
499234317Speter    if (error == 0) {
499334317Speter	sc->tulip_txdescs = (tulip_desc_t *) malloc(TULIP_TXDESCS * sizeof(tulip_desc_t), M_DEVBUF, M_NOWAIT);
499434317Speter	if (sc->tulip_txdescs == NULL)
499534317Speter	    error = ENOMEM;
499634317Speter    }
499734317Speter#endif
499834317Speter#if !defined(TULIP_BUS_DMA_NORX)
499934317Speter    /*
500034317Speter     * Allocate space and dmamap for receive ring
500134317Speter     */
500234317Speter    if (error == 0) {
500334317Speter	error = tulip_busdma_allocmem(sc, sizeof(tulip_desc_t) * TULIP_RXDESCS,
500434317Speter				      &sc->tulip_rxdescmap,
500534317Speter				      &sc->tulip_rxdescs);
500634317Speter    }
500734317Speter
500834317Speter    /*
500934317Speter     * Allocate dmamaps for each receive descriptors
501034317Speter     */
501134317Speter    if (error == 0) {
501234317Speter	while (error == 0 && sc->tulip_rxmaps_free < TULIP_RXDESCS) {
501334317Speter	    bus_dmamap_t map;
501434317Speter	    if ((error = TULIP_RXMAP_CREATE(sc, &map)) == 0)
501534317Speter		sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
501634317Speter	}
501734317Speter	if (error) {
501834317Speter	    while (sc->tulip_rxmaps_free > 0)
501934317Speter		bus_dmamap_destroy(sc->tulip_dmatag,
502034317Speter				   sc->tulip_rxmaps[--sc->tulip_rxmaps_free]);
502134317Speter	}
502234317Speter    }
502334317Speter#else
502434317Speter    if (error == 0) {
502534317Speter	sc->tulip_rxdescs = (tulip_desc_t *) malloc(TULIP_RXDESCS * sizeof(tulip_desc_t), M_DEVBUF, M_NOWAIT);
502634317Speter	if (sc->tulip_rxdescs == NULL)
502734317Speter	    error = ENOMEM;
502834317Speter    }
502934317Speter#endif
503034317Speter    return error;
503134317Speter}
503234317Speter#endif /* TULIP_BUS_DMA */
503334317Speter
50343278Swollmanstatic void
50353278Swollmantulip_initcsrs(
50368754Sdg    tulip_softc_t * const sc,
503711070Sdg    tulip_csrptr_t csr_base,
50383278Swollman    size_t csr_size)
50393278Swollman{
504011070Sdg    sc->tulip_csrs.csr_busmode		= csr_base +  0 * csr_size;
504111070Sdg    sc->tulip_csrs.csr_txpoll		= csr_base +  1 * csr_size;
504211070Sdg    sc->tulip_csrs.csr_rxpoll		= csr_base +  2 * csr_size;
504311070Sdg    sc->tulip_csrs.csr_rxlist		= csr_base +  3 * csr_size;
504411070Sdg    sc->tulip_csrs.csr_txlist		= csr_base +  4 * csr_size;
504511070Sdg    sc->tulip_csrs.csr_status		= csr_base +  5 * csr_size;
504611070Sdg    sc->tulip_csrs.csr_command		= csr_base +  6 * csr_size;
504711070Sdg    sc->tulip_csrs.csr_intr		= csr_base +  7 * csr_size;
504816357Sdg    sc->tulip_csrs.csr_missed_frames	= csr_base +  8 * csr_size;
504926797Speter    sc->tulip_csrs.csr_9		= csr_base +  9 * csr_size;
505026797Speter    sc->tulip_csrs.csr_10		= csr_base + 10 * csr_size;
505126797Speter    sc->tulip_csrs.csr_11		= csr_base + 11 * csr_size;
505226797Speter    sc->tulip_csrs.csr_12		= csr_base + 12 * csr_size;
505326797Speter    sc->tulip_csrs.csr_13		= csr_base + 13 * csr_size;
505426797Speter    sc->tulip_csrs.csr_14		= csr_base + 14 * csr_size;
505526797Speter    sc->tulip_csrs.csr_15		= csr_base + 15 * csr_size;
50563278Swollman}
50573278Swollman
50583278Swollmanstatic void
50593278Swollmantulip_initring(
50608754Sdg    tulip_softc_t * const sc,
50618754Sdg    tulip_ringinfo_t * const ri,
50623278Swollman    tulip_desc_t *descs,
50633278Swollman    int ndescs)
50643278Swollman{
50653278Swollman    ri->ri_max = ndescs;
50663278Swollman    ri->ri_first = descs;
50673278Swollman    ri->ri_last = ri->ri_first + ri->ri_max;
50683278Swollman    bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
50693278Swollman    ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
50703278Swollman}
50713278Swollman
50723278Swollman/*
507349575Speter * This is the PCI configuration support.
50743278Swollman */
50753278Swollman
50763278Swollman#define	PCI_CBIO	0x10	/* Configuration Base IO Address */
50773278Swollman#define	PCI_CBMA	0x14	/* Configuration Base Memory Address */
50783278Swollman#define	PCI_CFDA	0x40	/* Configuration Driver Area */
50793278Swollman
508058339Speterstatic int
508158339Spetertulip_pci_probe(device_t dev)
50823278Swollman{
508358339Speter    const char *name = NULL;
508458339Speter
508558339Speter    if (pci_get_vendor(dev) != DEC_VENDORID)
508658339Speter	return ENXIO;
508758339Speter
508858339Speter    switch (pci_get_device(dev)) {
508958339Speter    case CHIPID_21040:
509058339Speter	name = "Digital 21040 Ethernet";
509158339Speter	break;
509258339Speter    case CHIPID_21041:
509358339Speter	name = "Digital 21041 Ethernet";
509458339Speter	break;
509558339Speter    case CHIPID_21140:
509658339Speter	if (pci_get_revid(dev) >= 0x20)
509758339Speter	    name = "Digital 21140A Fast Ethernet";
509816357Sdg	else
509958339Speter	    name = "Digital 21140 Fast Ethernet";
510058339Speter	break;
510158339Speter    case CHIPID_21142:
510258339Speter	if (pci_get_revid(dev) >= 0x20)
510358339Speter	    name = "Digital 21143 Fast Ethernet";
510426797Speter	else
510558339Speter	    name = "Digital 21142 Fast Ethernet";
510658339Speter	break;
510726797Speter    }
510858339Speter    if (name) {
510958339Speter	device_set_desc(dev, name);
511058339Speter	return -200;
511158339Speter    }
511258339Speter    return ENXIO;
51133278Swollman}
51143278Swollman
511558339Speterstatic int
511658339Spetertulip_shutdown(device_t dev)
511726797Speter{
511858339Speter    tulip_softc_t * const sc = device_get_softc(dev);
511926797Speter    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
512026797Speter    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
512126797Speter		   33MHz that comes to two microseconds but wait a
512226797Speter		   bit longer anyways) */
512358339Speter    return 0;
512426797Speter}
512526797Speter
512658339Speterstatic int
512758339Spetertulip_pci_attach(device_t dev)
51283278Swollman{
51293278Swollman    tulip_softc_t *sc;
513030556Speter#if defined(__alpha__)
513130556Speter    tulip_media_t media = TULIP_MEDIA_UNKNOWN;
513230556Speter#endif
513316357Sdg    int retval, idx;
513458339Speter    u_int32_t revinfo, cfdainfo, cfcsinfo;
513511070Sdg    unsigned csroffset = TULIP_PCI_CSROFFSET;
513611070Sdg    unsigned csrsize = TULIP_PCI_CSRSIZE;
513711070Sdg    tulip_csrptr_t csr_base;
513811070Sdg    tulip_chipid_t chipid = TULIP_CHIPID_UNKNOWN;
513958339Speter    struct resource *res;
514058339Speter    int rid, unit;
51413278Swollman
514258339Speter    unit = device_get_unit(dev);
514358339Speter
514418357Sdg    if (unit >= TULIP_MAX_DEVICES) {
514518357Sdg	printf("de%d", unit);
514618357Sdg	printf(": not configured; limit of %d reached or exceeded\n",
514718357Sdg	       TULIP_MAX_DEVICES);
514858339Speter	return ENXIO;
51497689Sdg    }
51507689Sdg
515158339Speter    revinfo  = pci_get_revid(dev);
515258339Speter    cfdainfo = pci_read_config(dev, PCI_CFDA, 4);
515358339Speter    cfcsinfo = pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4);
51548296Sdg
515557248Smsmith    /* turn busmaster on in case BIOS doesn't set it */
515657248Smsmith    if(!(cfcsinfo & PCIM_CMD_BUSMASTEREN)) {
515757248Smsmith	 cfcsinfo |= PCIM_CMD_BUSMASTEREN;
515858339Speter	 pci_write_config(dev, PCI_COMMAND_STATUS_REG, cfcsinfo, 4);
515957248Smsmith    }
516057248Smsmith
516158339Speter    if (pci_get_vendor(dev) == DEC_VENDORID) {
516258339Speter	if (pci_get_device(dev) == CHIPID_21040)
516340290Speter		chipid = TULIP_21040;
516458339Speter	else if (pci_get_device(dev) == CHIPID_21041)
516540290Speter		chipid = TULIP_21041;
516658339Speter	else if (pci_get_device(dev) == CHIPID_21140)
516740290Speter		chipid = (revinfo >= 0x20) ? TULIP_21140A : TULIP_21140;
516858339Speter	else if (pci_get_device(dev) == CHIPID_21142)
516940290Speter		chipid = (revinfo >= 0x20) ? TULIP_21143 : TULIP_21142;
517011070Sdg    }
517111070Sdg    if (chipid == TULIP_CHIPID_UNKNOWN)
517258339Speter	return ENXIO;
51738296Sdg
517449575Speter    if (chipid == TULIP_21040 && revinfo < 0x20) {
51758754Sdg	printf("de%d", unit);
517620060Srgrimes	printf(": not configured; 21040 pass 2.0 required (%d.%d found)\n",
51778754Sdg	       revinfo >> 4, revinfo & 0x0f);
517858339Speter	return ENXIO;
517920060Srgrimes    } else if (chipid == TULIP_21140 && revinfo < 0x11) {
518020060Srgrimes	printf("de%d: not configured; 21140 pass 1.1 required (%d.%d found)\n",
518116357Sdg	       unit, revinfo >> 4, revinfo & 0x0f);
518258339Speter	return ENXIO;
51837791Sdg    }
51847791Sdg
518558339Speter    sc = device_get_softc(dev);
518658339Speter    sc->tulip_pci_busno = pci_get_bus(dev);
518758339Speter    sc->tulip_pci_devno = pci_get_slot(dev);
51888296Sdg    sc->tulip_chipid = chipid;
518926797Speter    sc->tulip_flags |= TULIP_DEVICEPROBE;
519026797Speter    if (chipid == TULIP_21140 || chipid == TULIP_21140A)
519127862Speter	sc->tulip_features |= TULIP_HAVE_GPR|TULIP_HAVE_STOREFWD;
519226797Speter    if (chipid == TULIP_21140A && revinfo <= 0x22)
519326797Speter	sc->tulip_features |= TULIP_HAVE_RXBADOVRFLW;
519426797Speter    if (chipid == TULIP_21140)
519526797Speter	sc->tulip_features |= TULIP_HAVE_BROKEN_HASH;
519649575Speter    if (chipid != TULIP_21040 && chipid != TULIP_21140)
519726797Speter	sc->tulip_features |= TULIP_HAVE_POWERMGMT;
519826797Speter    if (chipid == TULIP_21041 || chipid == TULIP_21142 || chipid == TULIP_21143) {
519926797Speter	sc->tulip_features |= TULIP_HAVE_DUALSENSE;
520040290Speter	if (chipid != TULIP_21041 || revinfo >= 0x20)
520126797Speter	    sc->tulip_features |= TULIP_HAVE_SIANWAY;
520226797Speter	if (chipid != TULIP_21041)
520327862Speter	    sc->tulip_features |= TULIP_HAVE_SIAGP|TULIP_HAVE_RXBADOVRFLW|TULIP_HAVE_STOREFWD;
520440290Speter	if (chipid != TULIP_21041 && revinfo >= 0x20)
520530556Speter	    sc->tulip_features |= TULIP_HAVE_SIA100;
520626797Speter    }
520726797Speter
520826797Speter    if (sc->tulip_features & TULIP_HAVE_POWERMGMT
520926797Speter	    && (cfdainfo & (TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE))) {
521026797Speter	cfdainfo &= ~(TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE);
521158339Speter	pci_write_config(dev, PCI_CFDA, cfdainfo, 4);
521226797Speter	DELAY(11*1000);
521326797Speter    }
521446356Sdfr#if defined(__alpha__)
521526797Speter    /*
521626797Speter     * The Alpha SRM console encodes a console set media in the driver
521726797Speter     * part of the CFDA register.  Note that the Multia presents a
521826797Speter     * problem in that its BNC mode is really EXTSIA.  So in that case
521926797Speter     * force a probe.
522026797Speter     */
522126797Speter    switch ((cfdainfo >> 8) & 0xff) {
522249575Speter	case 1: media = chipid > TULIP_21040 ? TULIP_MEDIA_AUI : TULIP_MEDIA_AUIBNC; break;
522349575Speter	case 2: media = chipid > TULIP_21040 ? TULIP_MEDIA_BNC : TULIP_MEDIA_UNKNOWN; break;
522444738Speter	case 3: media = TULIP_MEDIA_10BASET; break;
522544738Speter	case 4: media = TULIP_MEDIA_10BASET_FD; break;
522644738Speter	case 5: media = TULIP_MEDIA_100BASETX; break;
522744738Speter	case 6: media = TULIP_MEDIA_100BASETX_FD; break;
522844738Speter	default: media = TULIP_MEDIA_UNKNOWN; break;
522926797Speter    }
523026797Speter#endif
523126797Speter
52323278Swollman    sc->tulip_unit = unit;
52333278Swollman    sc->tulip_name = "de";
523411070Sdg    sc->tulip_revinfo = revinfo;
523516357Sdg    sc->tulip_if.if_softc = sc;
523611070Sdg#if defined(TULIP_IOMAPPED)
523758339Speter    rid = PCI_CBIO;
523858339Speter    res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
523958339Speter			     0, ~0, 1, RF_ACTIVE);
524058339Speter    if (!res)
524158339Speter	return ENXIO;
524258339Speter    csr_base = rman_get_start(res);
524311070Sdg#else
524458339Speter    rid = PCI_CBMA;
524558339Speter    res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
524658339Speter			     0, ~0, 1, RF_ACTIVE);
524758339Speter    if (!res)
524858339Speter	return ENXIO;
524958339Speter    csr_base = (vm_offset_t) rman_get_virtual(res);
525011070Sdg#endif
52513278Swollman    tulips[unit] = sc;
525211070Sdg
525311070Sdg    tulip_initcsrs(sc, csr_base + csroffset, csrsize);
525434317Speter
525534317Speter#if defined(TULIP_BUS_DMA)
525634317Speter    if ((retval = tulip_busdma_init(sc)) != 0) {
525734317Speter	printf("error initing bus_dma: %d\n", retval);
525858339Speter	return ENXIO;
525934317Speter    }
526034317Speter#else
526134317Speter    sc->tulip_rxdescs = (tulip_desc_t *) malloc(sizeof(tulip_desc_t) * TULIP_RXDESCS, M_DEVBUF, M_NOWAIT);
526234317Speter    sc->tulip_txdescs = (tulip_desc_t *) malloc(sizeof(tulip_desc_t) * TULIP_TXDESCS, M_DEVBUF, M_NOWAIT);
526334317Speter    if (sc->tulip_rxdescs == NULL || sc->tulip_txdescs == NULL) {
526434317Speter	printf("malloc failed\n");
526534317Speter	if (sc->tulip_rxdescs)
526634317Speter	    free((caddr_t) sc->tulip_rxdescs, M_DEVBUF);
526734317Speter	if (sc->tulip_txdescs)
526834317Speter	    free((caddr_t) sc->tulip_txdescs, M_DEVBUF);
526958339Speter	return ENXIO;
527034317Speter    }
527134317Speter#endif
527234317Speter
527316357Sdg    tulip_initring(sc, &sc->tulip_rxinfo, sc->tulip_rxdescs, TULIP_RXDESCS);
527416357Sdg    tulip_initring(sc, &sc->tulip_txinfo, sc->tulip_txdescs, TULIP_TXDESCS);
527518357Sdg
527618357Sdg    /*
527718357Sdg     * Make sure there won't be any interrupts or such...
527818357Sdg     */
527918357Sdg    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
528018357Sdg    DELAY(100);	/* Wait 10 microseconds (actually 50 PCI cycles but at
528118357Sdg		   33MHz that comes to two microseconds but wait a
528218357Sdg		   bit longer anyways) */
528318357Sdg
52843278Swollman    if ((retval = tulip_read_macaddr(sc)) < 0) {
528549572Speter	printf("%s%d", sc->tulip_name, sc->tulip_unit);
52868754Sdg	printf(": can't read ENET ROM (why=%d) (", retval);
52873278Swollman	for (idx = 0; idx < 32; idx++)
52883278Swollman	    printf("%02x", sc->tulip_rombuf[idx]);
52893278Swollman	printf("\n");
529049572Speter	printf("%s%d: %s%s pass %d.%d\n",
529149572Speter	       sc->tulip_name, sc->tulip_unit,
529226797Speter	       sc->tulip_boardid, tulip_chipdescs[sc->tulip_chipid],
529316357Sdg	       (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F);
529449572Speter	printf("%s%d: address unknown\n", sc->tulip_name, sc->tulip_unit);
52953278Swollman    } else {
529649572Speter	int s;
529749572Speter	void (*intr_rtn)(void *) = tulip_intr_normal;
529818357Sdg
529926797Speter	if (sc->tulip_features & TULIP_HAVE_SHAREDINTR)
530018357Sdg	    intr_rtn = tulip_intr_shared;
530118357Sdg
530226797Speter	if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
530358339Speter	    void *ih;
530458339Speter
530558339Speter	    rid = 0;
530658339Speter	    res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
530758339Speter				     0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
530858339Speter	    if (res == 0 || bus_setup_intr(dev, res, INTR_TYPE_NET,
530958339Speter					   intr_rtn, sc, &ih)) {
531049572Speter		printf("%s%d: couldn't map interrupt\n",
531149572Speter		       sc->tulip_name, sc->tulip_unit);
531234317Speter		free((caddr_t) sc->tulip_rxdescs, M_DEVBUF);
531334317Speter		free((caddr_t) sc->tulip_txdescs, M_DEVBUF);
531458339Speter		return ENXIO;
531511132Sdg	    }
531611132Sdg	}
531718357Sdg#if defined(TULIP_USE_SOFTINTR)
531818357Sdg	if (sc->tulip_unit > tulip_softintr_max_unit)
531918357Sdg	    tulip_softintr_max_unit = sc->tulip_unit;
532018357Sdg#endif
532118357Sdg
532249572Speter	s = splimp();
532346356Sdfr#if defined(__alpha__)
532444738Speter	sc->tulip_media = media;
532544738Speter#endif
532611070Sdg	tulip_attach(sc);
532746356Sdfr#if defined(__alpha__)
532844738Speter	if (sc->tulip_media != TULIP_MEDIA_UNKNOWN)
532944738Speter		tulip_linkup(sc, media);
533030556Speter#endif
533149572Speter	splx(s);
53327689Sdg    }
533358339Speter    return 0;
53347104Sdg}
533558339Speter
533658339Speterstatic device_method_t tulip_pci_methods[] = {
533758339Speter    /* Device interface */
533858339Speter    DEVMETHOD(device_probe,	tulip_pci_probe),
533958339Speter    DEVMETHOD(device_attach,	tulip_pci_attach),
534058339Speter    DEVMETHOD(device_shutdown,	tulip_shutdown),
534158339Speter    { 0, 0 }
534258339Speter};
534358339Speterstatic driver_t tulip_pci_driver = {
534458339Speter    "de",
534558339Speter    tulip_pci_methods,
534658339Speter    sizeof(tulip_softc_t),
534758339Speter};
534858339Speterstatic devclass_t tulip_devclass;
534958339SpeterDRIVER_MODULE(if_de, pci, tulip_pci_driver, tulip_devclass, 0, 0);
5350