if_de.c revision 48528
1/*	$NetBSD: if_de.c,v 1.82 1999/02/28 17:08:51 explorer Exp $	*/
2/*	$Id: if_de.c,v 1.106 1999/05/10 14:12:26 peter Exp $ */
3
4/*-
5 * Copyright (c) 1994-1997 Matt Thomas (matt@3am-software.com)
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. The name of the author may not be used to endorse or promote products
14 *    derived from this software withough specific prior written permission
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Id: if_de.c,v 1.94 1997/07/03 16:55:07 thomas Exp
28 *
29 */
30
31/*
32 * DEC 21040 PCI Ethernet Controller
33 *
34 * Written by Matt Thomas
35 * BPF support code stolen directly from if_ec.c
36 *
37 *   This driver supports the DEC DE435 or any other PCI
38 *   board which support 21040, 21041, or 21140 (mostly).
39 */
40#define	TULIP_HDR_DATA
41
42#ifdef __NetBSD__
43#include "opt_inet.h"
44#include "opt_ns.h"
45#endif
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/mbuf.h>
50#include <sys/socket.h>
51#include <sys/sockio.h>
52#include <sys/malloc.h>
53#include <sys/kernel.h>
54#if defined(__FreeBSD__)
55#include <machine/clock.h>
56#elif defined(__bsdi__) || defined(__NetBSD__)
57#include <sys/device.h>
58#endif
59
60#if defined(__FreeBSD__)
61/* In case somebody is trying to run this on an older 2.2 or 3.0 */
62#ifndef __FreeBSD_version	/* defined in sys/param.h on current code */
63#if __FreeBSD__ >= 3
64#define __FreeBSD_version 300000
65#else
66#define __FreeBSD_version 200000
67#endif
68#endif
69#if __FreeBSD_version >= 300000
70#include "opt_inet.h"
71#include "opt_ipx.h"
72#endif
73#endif
74
75#if defined(__NetBSD__)
76#include "rnd.h"
77#if NRND > 0
78#include <sys/rnd.h>
79#endif
80#endif
81
82#include <net/if.h>
83#if defined(SIOCSIFMEDIA) && !defined(TULIP_NOIFMEDIA)
84#include <net/if_media.h>
85#endif
86#include <net/if_dl.h>
87#ifdef TULIP_USE_SOFTINTR
88#include <net/netisr.h>
89#endif
90
91#if defined(__bsdi__) && _BSDI_VERSION >= 199701
92#include <dev/mii/mii.h>
93#include <dev/mii/miivar.h>
94#endif
95
96#include "bpfilter.h"
97#if NBPFILTER > 0
98#include <net/bpf.h>
99#endif
100
101#ifdef INET
102#include <netinet/in.h>
103#include <netinet/if_ether.h>
104#endif
105
106#ifdef IPX
107#include <netipx/ipx.h>
108#include <netipx/ipx_if.h>
109#endif
110
111#ifdef NS
112#include <netns/ns.h>
113#include <netns/ns_if.h>
114#endif
115
116#include <vm/vm.h>
117
118#if defined(__FreeBSD__)
119#include <net/if_var.h>
120#include <vm/pmap.h>
121#include <pci/pcivar.h>
122#include <pci/dc21040reg.h>
123#define	DEVAR_INCLUDE	"pci/if_devar.h"
124#include "opt_bdg.h"
125#ifdef BRIDGE
126#include <net/bridge.h>
127#endif
128#endif /* __FreeBSD__ */
129
130#if defined(__bsdi__)
131#include <netinet/if_ether.h>
132#include <i386/pci/ic/dc21040reg.h>
133#include <i386/isa/isa.h>
134#include <i386/isa/icu.h>
135#include <i386/isa/dma.h>
136#include <i386/isa/isavar.h>
137#include <i386/pci/pci.h>
138#if _BSDI_VERSION < 199510
139#include <eisa.h>
140#else
141#define	NEISA 0
142#endif
143#if NEISA > 0 && _BSDI_VERSION >= 199401
144#include <i386/eisa/eisa.h>
145#define	TULIP_EISA
146#endif
147#define	DEVAR_INCLUDE	"i386/pci/if_devar.h"
148#endif /* __bsdi__ */
149
150#if defined(__NetBSD__)
151#include <net/if_ether.h>
152#if defined(INET)
153#include <netinet/if_inarp.h>
154#endif
155#include <machine/bus.h>
156#include <machine/intr.h>
157#include <dev/pci/pcireg.h>
158#include <dev/pci/pcivar.h>
159#include <dev/ic/dc21040reg.h>
160#define	DEVAR_INCLUDE	"dev/pci/if_devar.h"
161#endif /* __NetBSD__ */
162
163/*
164 * Intel CPUs should use I/O mapped access.
165 */
166#if defined(__i386__) || defined(TULIP_EISA)
167#define	TULIP_IOMAPPED
168#endif
169
170#if 0
171/*
172 * This turns on all sort of debugging stuff and make the
173 * driver much larger.
174 */
175#define TULIP_DEBUG
176#endif
177
178#if 0
179#define	TULIP_PERFSTATS
180#endif
181
182#if 0
183#define	TULIP_USE_SOFTINTR
184#endif
185
186#define	TULIP_HZ	10
187
188#include DEVAR_INCLUDE
189/*
190 * This module supports
191 *	the DEC 21040 PCI Ethernet Controller.
192 *	the DEC 21041 PCI Ethernet Controller.
193 *	the DEC 21140 PCI Fast Ethernet Controller.
194 */
195static void tulip_mii_autonegotiate(tulip_softc_t * const sc, const unsigned phyaddr);
196static tulip_intrfunc_t tulip_intr_shared(void *arg);
197static tulip_intrfunc_t tulip_intr_normal(void *arg);
198static void tulip_init(tulip_softc_t * const sc);
199static void tulip_reset(tulip_softc_t * const sc);
200static ifnet_ret_t tulip_ifstart_one(struct ifnet *ifp);
201static ifnet_ret_t tulip_ifstart(struct ifnet *ifp);
202static struct mbuf *tulip_txput(tulip_softc_t * const sc, struct mbuf *m);
203static void tulip_txput_setup(tulip_softc_t * const sc);
204static void tulip_rx_intr(tulip_softc_t * const sc);
205static void tulip_addr_filter(tulip_softc_t * const sc);
206static unsigned tulip_mii_readreg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno);
207static void tulip_mii_writereg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno, unsigned data);
208static int tulip_mii_map_abilities(tulip_softc_t * const sc, unsigned abilities);
209static tulip_media_t tulip_mii_phy_readspecific(tulip_softc_t * const sc);
210static int tulip_srom_decode(tulip_softc_t * const sc);
211#if defined(IFM_ETHER)
212static int tulip_ifmedia_change(struct ifnet * const ifp);
213static void tulip_ifmedia_status(struct ifnet * const ifp, struct ifmediareq *req);
214#endif
215/* static void tulip_21140_map_media(tulip_softc_t *sc); */
216
217static void
218tulip_timeout_callback(
219    void *arg)
220{
221    tulip_softc_t * const sc = arg;
222    tulip_spl_t s = TULIP_RAISESPL();
223
224    TULIP_PERFSTART(timeout)
225
226    sc->tulip_flags &= ~TULIP_TIMEOUTPENDING;
227    sc->tulip_probe_timeout -= 1000 / TULIP_HZ;
228    (sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_TIMER);
229
230    TULIP_PERFEND(timeout);
231    TULIP_RESTORESPL(s);
232}
233
234static void
235tulip_timeout(
236    tulip_softc_t * const sc)
237{
238    if (sc->tulip_flags & TULIP_TIMEOUTPENDING)
239	return;
240    sc->tulip_flags |= TULIP_TIMEOUTPENDING;
241    timeout(tulip_timeout_callback, sc, (hz + TULIP_HZ / 2) / TULIP_HZ);
242}
243
244#if defined(TULIP_NEED_FASTTIMEOUT)
245static void
246tulip_fasttimeout_callback(
247    void *arg)
248{
249    tulip_softc_t * const sc = arg;
250    tulip_spl_t s = TULIP_RAISESPL();
251
252    sc->tulip_flags &= ~TULIP_FASTTIMEOUTPENDING;
253    (sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_FASTTIMER);
254    TULIP_RESTORESPL(s);
255}
256
257static void
258tulip_fasttimeout(
259    tulip_softc_t * const sc)
260{
261    if (sc->tulip_flags & TULIP_FASTTIMEOUTPENDING)
262	return;
263    sc->tulip_flags |= TULIP_FASTTIMEOUTPENDING;
264    timeout(tulip_fasttimeout_callback, sc, 1);
265}
266#endif
267
268static int
269tulip_txprobe(
270    tulip_softc_t * const sc)
271{
272    struct mbuf *m;
273    /*
274     * Before we are sure this is the right media we need
275     * to send a small packet to make sure there's carrier.
276     * Strangely, BNC and AUI will "see" receive data if
277     * either is connected so the transmit is the only way
278     * to verify the connectivity.
279     */
280    MGETHDR(m, M_DONTWAIT, MT_DATA);
281    if (m == NULL)
282	return 0;
283    /*
284     * Construct a LLC TEST message which will point to ourselves.
285     */
286    bcopy(sc->tulip_enaddr, mtod(m, struct ether_header *)->ether_dhost, 6);
287    bcopy(sc->tulip_enaddr, mtod(m, struct ether_header *)->ether_shost, 6);
288    mtod(m, struct ether_header *)->ether_type = htons(3);
289    mtod(m, unsigned char *)[14] = 0;
290    mtod(m, unsigned char *)[15] = 0;
291    mtod(m, unsigned char *)[16] = 0xE3;	/* LLC Class1 TEST (no poll) */
292    m->m_len = m->m_pkthdr.len = sizeof(struct ether_header) + 3;
293    /*
294     * send it!
295     */
296    sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
297    sc->tulip_intrmask |= TULIP_STS_TXINTR;
298    sc->tulip_flags |= TULIP_TXPROBE_ACTIVE;
299    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
300    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
301    if ((m = tulip_txput(sc, m)) != NULL)
302	m_freem(m);
303    sc->tulip_probe.probe_txprobes++;
304    return 1;
305}
306
307#ifdef BIG_PACKET
308#define TULIP_SIAGEN_WATCHDOG	(sc->tulip_if.if_mtu > ETHERMTU ? TULIP_WATCHDOG_RXDISABLE|TULIP_WATCHDOG_TXDISABLE : 0)
309#else
310#define	TULIP_SIAGEN_WATCHDOG	0
311#endif
312
313static void
314tulip_media_set(
315    tulip_softc_t * const sc,
316    tulip_media_t media)
317{
318    const tulip_media_info_t *mi = sc->tulip_mediums[media];
319
320    if (mi == NULL)
321	return;
322
323    /*
324     * If we are switching media, make sure we don't think there's
325     * any stale RX activity
326     */
327    sc->tulip_flags &= ~TULIP_RXACT;
328    if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
329	TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
330	TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        mi->mi_sia_tx_rx);
331	if (sc->tulip_features & TULIP_HAVE_SIAGP) {
332	    TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_gp_control|mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
333	    DELAY(50);
334	    TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_gp_data|mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
335	} else {
336	    TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
337	}
338	TULIP_CSR_WRITE(sc, csr_sia_connectivity, mi->mi_sia_connectivity);
339    } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
340#define	TULIP_GPR_CMDBITS	(TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER|TULIP_CMD_TXTHRSHLDCTL)
341	/*
342	 * If the cmdmode bits don't match the currently operating mode,
343	 * set the cmdmode appropriately and reset the chip.
344	 */
345	if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
346	    sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
347	    sc->tulip_cmdmode |= mi->mi_cmdmode;
348	    tulip_reset(sc);
349	}
350	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
351	DELAY(10);
352	TULIP_CSR_WRITE(sc, csr_gp, (u_int8_t) mi->mi_gpdata);
353    } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
354	/*
355	 * If the cmdmode bits don't match the currently operating mode,
356	 * set the cmdmode appropriately and reset the chip.
357	 */
358	if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
359	    sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
360	    sc->tulip_cmdmode |= mi->mi_cmdmode;
361	    tulip_reset(sc);
362	}
363	TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpcontrol);
364	TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpdata);
365    } else if (mi->mi_type == TULIP_MEDIAINFO_MII
366	       && sc->tulip_probe_state != TULIP_PROBE_INACTIVE) {
367	int idx;
368	if (sc->tulip_features & TULIP_HAVE_SIAGP) {
369	    const u_int8_t *dp;
370	    dp = &sc->tulip_rombuf[mi->mi_reset_offset];
371	    for (idx = 0; idx < mi->mi_reset_length; idx++, dp += 2) {
372		DELAY(10);
373		TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
374	    }
375	    sc->tulip_phyaddr = mi->mi_phyaddr;
376	    dp = &sc->tulip_rombuf[mi->mi_gpr_offset];
377	    for (idx = 0; idx < mi->mi_gpr_length; idx++, dp += 2) {
378		DELAY(10);
379		TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
380	    }
381	} else {
382	    for (idx = 0; idx < mi->mi_reset_length; idx++) {
383		DELAY(10);
384		TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx]);
385	    }
386	    sc->tulip_phyaddr = mi->mi_phyaddr;
387	    for (idx = 0; idx < mi->mi_gpr_length; idx++) {
388		DELAY(10);
389		TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx]);
390	    }
391	}
392	if (sc->tulip_flags & TULIP_TRYNWAY) {
393	    tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
394	} else if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
395	    u_int32_t data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_CONTROL);
396	    data &= ~(PHYCTL_SELECT_100MB|PHYCTL_FULL_DUPLEX|PHYCTL_AUTONEG_ENABLE);
397	    sc->tulip_flags &= ~TULIP_DIDNWAY;
398	    if (TULIP_IS_MEDIA_FD(media))
399		data |= PHYCTL_FULL_DUPLEX;
400	    if (TULIP_IS_MEDIA_100MB(media))
401		data |= PHYCTL_SELECT_100MB;
402	    tulip_mii_writereg(sc, sc->tulip_phyaddr, PHYREG_CONTROL, data);
403	}
404    }
405}
406
407static void
408tulip_linkup(
409    tulip_softc_t * const sc,
410    tulip_media_t media)
411{
412    if ((sc->tulip_flags & TULIP_LINKUP) == 0)
413	sc->tulip_flags |= TULIP_PRINTLINKUP;
414    sc->tulip_flags |= TULIP_LINKUP;
415    sc->tulip_if.if_flags &= ~IFF_OACTIVE;
416#if 0 /* XXX how does with work with ifmedia? */
417    if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
418	if (sc->tulip_if.if_flags & IFF_FULLDUPLEX) {
419	    if (TULIP_CAN_MEDIA_FD(media)
420		    && sc->tulip_mediums[TULIP_FD_MEDIA_OF(media)] != NULL)
421		media = TULIP_FD_MEDIA_OF(media);
422	} else {
423	    if (TULIP_IS_MEDIA_FD(media)
424		    && sc->tulip_mediums[TULIP_HD_MEDIA_OF(media)] != NULL)
425		media = TULIP_HD_MEDIA_OF(media);
426	}
427    }
428#endif
429    if (sc->tulip_media != media) {
430#ifdef TULIP_DEBUG
431	sc->tulip_dbg.dbg_last_media = sc->tulip_media;
432#endif
433	sc->tulip_media = media;
434	sc->tulip_flags |= TULIP_PRINTMEDIA;
435	if (TULIP_IS_MEDIA_FD(sc->tulip_media)) {
436	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
437	} else if (sc->tulip_chipid != TULIP_21041 || (sc->tulip_flags & TULIP_DIDNWAY) == 0) {
438	    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
439	}
440    }
441    /*
442     * We could set probe_timeout to 0 but setting to 3000 puts this
443     * in one central place and the only matters is tulip_link is
444     * followed by a tulip_timeout.  Therefore setting it should not
445     * result in aberrant behavour.
446     */
447    sc->tulip_probe_timeout = 3000;
448    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
449    sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_TRYNWAY);
450    if (sc->tulip_flags & TULIP_INRESET) {
451	tulip_media_set(sc, sc->tulip_media);
452    } else if (sc->tulip_probe_media != sc->tulip_media) {
453	/*
454	 * No reason to change media if we have the right media.
455	 */
456	tulip_reset(sc);
457    }
458    tulip_init(sc);
459}
460
461static void
462tulip_media_print(
463    tulip_softc_t * const sc)
464{
465    if ((sc->tulip_flags & TULIP_LINKUP) == 0)
466	return;
467    if (sc->tulip_flags & TULIP_PRINTMEDIA) {
468	printf(TULIP_PRINTF_FMT ": enabling %s port\n",
469	       TULIP_PRINTF_ARGS,
470	       tulip_mediums[sc->tulip_media]);
471	sc->tulip_flags &= ~(TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
472    } else if (sc->tulip_flags & TULIP_PRINTLINKUP) {
473	printf(TULIP_PRINTF_FMT ": link up\n", TULIP_PRINTF_ARGS);
474	sc->tulip_flags &= ~TULIP_PRINTLINKUP;
475    }
476}
477
478#if defined(TULIP_DO_GPR_SENSE)
479static tulip_media_t
480tulip_21140_gpr_media_sense(
481    tulip_softc_t * const sc)
482{
483    tulip_media_t maybe_media = TULIP_MEDIA_UNKNOWN;
484    tulip_media_t last_media = TULIP_MEDIA_UNKNOWN;
485    tulip_media_t media;
486
487    /*
488     * If one of the media blocks contained a default media flag,
489     * use that.
490     */
491    for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
492	const tulip_media_info_t *mi;
493	/*
494	 * Media is not supported (or is full-duplex).
495	 */
496	if ((mi = sc->tulip_mediums[media]) == NULL || TULIP_IS_MEDIA_FD(media))
497	    continue;
498	if (mi->mi_type != TULIP_MEDIAINFO_GPR)
499	    continue;
500
501	/*
502	 * Remember the media is this is the "default" media.
503	 */
504	if (mi->mi_default && maybe_media == TULIP_MEDIA_UNKNOWN)
505	    maybe_media = media;
506
507	/*
508	 * No activity mask?  Can't see if it is active if there's no mask.
509	 */
510	if (mi->mi_actmask == 0)
511	    continue;
512
513	/*
514	 * Does the activity data match?
515	 */
516	if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) != mi->mi_actdata)
517	    continue;
518
519#if defined(TULIP_DEBUG)
520	printf(TULIP_PRINTF_FMT ": gpr_media_sense: %s: 0x%02x & 0x%02x == 0x%02x\n",
521	       TULIP_PRINTF_ARGS, tulip_mediums[media],
522	       TULIP_CSR_READ(sc, csr_gp) & 0xFF,
523	       mi->mi_actmask, mi->mi_actdata);
524#endif
525	/*
526	 * It does!  If this is the first media we detected, then
527	 * remember this media.  If isn't the first, then there were
528	 * multiple matches which we equate to no match (since we don't
529	 * which to select (if any).
530	 */
531	if (last_media == TULIP_MEDIA_UNKNOWN) {
532	    last_media = media;
533	} else if (last_media != media) {
534	    last_media = TULIP_MEDIA_UNKNOWN;
535	}
536    }
537    return (last_media != TULIP_MEDIA_UNKNOWN) ? last_media : maybe_media;
538}
539#endif /* TULIP_DO_GPR_SENSE */
540
541static tulip_link_status_t
542tulip_media_link_monitor(
543    tulip_softc_t * const sc)
544{
545    const tulip_media_info_t * const mi = sc->tulip_mediums[sc->tulip_media];
546    tulip_link_status_t linkup = TULIP_LINK_DOWN;
547
548    if (mi == NULL) {
549#if defined(DIAGNOSTIC) || defined(TULIP_DEBUG)
550	panic("tulip_media_link_monitor: %s: botch at line %d\n",
551	      tulip_mediums[sc->tulip_media],__LINE__);
552#endif
553	return TULIP_LINK_UNKNOWN;
554    }
555
556
557    /*
558     * Have we seen some packets?  If so, the link must be good.
559     */
560    if ((sc->tulip_flags & (TULIP_RXACT|TULIP_LINKUP)) == (TULIP_RXACT|TULIP_LINKUP)) {
561	sc->tulip_flags &= ~TULIP_RXACT;
562	sc->tulip_probe_timeout = 3000;
563	return TULIP_LINK_UP;
564    }
565
566    sc->tulip_flags &= ~TULIP_RXACT;
567    if (mi->mi_type == TULIP_MEDIAINFO_MII) {
568	u_int32_t status;
569	/*
570	 * Read the PHY status register.
571	 */
572	status = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
573	if (status & PHYSTS_AUTONEG_DONE) {
574	    /*
575	     * If the PHY has completed autonegotiation, see the if the
576	     * remote systems abilities have changed.  If so, upgrade or
577	     * downgrade as appropriate.
578	     */
579	    u_int32_t abilities = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_AUTONEG_ABILITIES);
580	    abilities = (abilities << 6) & status;
581	    if (abilities != sc->tulip_abilities) {
582#if defined(TULIP_DEBUG)
583		loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation changed: 0x%04x -> 0x%04x\n",
584			   TULIP_PRINTF_ARGS, sc->tulip_phyaddr,
585			   sc->tulip_abilities, abilities);
586#endif
587		if (tulip_mii_map_abilities(sc, abilities)) {
588		    tulip_linkup(sc, sc->tulip_probe_media);
589		    return TULIP_LINK_UP;
590		}
591		/*
592		 * if we had selected media because of autonegotiation,
593		 * we need to probe for the new media.
594		 */
595		sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
596		if (sc->tulip_flags & TULIP_DIDNWAY)
597		    return TULIP_LINK_DOWN;
598	    }
599	}
600	/*
601	 * The link is now up.  If was down, say its back up.
602	 */
603	if ((status & (PHYSTS_LINK_UP|PHYSTS_REMOTE_FAULT)) == PHYSTS_LINK_UP)
604	    linkup = TULIP_LINK_UP;
605    } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
606	/*
607	 * No activity sensor?  Assume all's well.
608	 */
609	if (mi->mi_actmask == 0)
610	    return TULIP_LINK_UNKNOWN;
611	/*
612	 * Does the activity data match?
613	 */
614	if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) == mi->mi_actdata)
615	    linkup = TULIP_LINK_UP;
616    } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
617	/*
618	 * Assume non TP ok for now.
619	 */
620	if (!TULIP_IS_MEDIA_TP(sc->tulip_media))
621	    return TULIP_LINK_UNKNOWN;
622	if ((TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) == 0)
623	    linkup = TULIP_LINK_UP;
624#if defined(TULIP_DEBUG)
625	if (sc->tulip_probe_timeout <= 0)
626	    printf(TULIP_PRINTF_FMT ": sia status = 0x%08x\n", TULIP_PRINTF_ARGS, TULIP_CSR_READ(sc, csr_sia_status));
627#endif
628    } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
629	return TULIP_LINK_UNKNOWN;
630    }
631    /*
632     * We will wait for 3 seconds until the link goes into suspect mode.
633     */
634    if (sc->tulip_flags & TULIP_LINKUP) {
635	if (linkup == TULIP_LINK_UP)
636	    sc->tulip_probe_timeout = 3000;
637	if (sc->tulip_probe_timeout > 0)
638	    return TULIP_LINK_UP;
639
640	sc->tulip_flags &= ~TULIP_LINKUP;
641	printf(TULIP_PRINTF_FMT ": link down: cable problem?\n", TULIP_PRINTF_ARGS);
642    }
643#if defined(TULIP_DEBUG)
644    sc->tulip_dbg.dbg_link_downed++;
645#endif
646    return TULIP_LINK_DOWN;
647}
648
649static void
650tulip_media_poll(
651    tulip_softc_t * const sc,
652    tulip_mediapoll_event_t event)
653{
654#if defined(TULIP_DEBUG)
655    sc->tulip_dbg.dbg_events[event]++;
656#endif
657    if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE
658	    && event == TULIP_MEDIAPOLL_TIMER) {
659	switch (tulip_media_link_monitor(sc)) {
660	    case TULIP_LINK_DOWN: {
661		/*
662		 * Link Monitor failed.  Probe for new media.
663		 */
664		event = TULIP_MEDIAPOLL_LINKFAIL;
665		break;
666	    }
667	    case TULIP_LINK_UP: {
668		/*
669		 * Check again soon.
670		 */
671		tulip_timeout(sc);
672		return;
673	    }
674	    case TULIP_LINK_UNKNOWN: {
675		/*
676		 * We can't tell so don't bother.
677		 */
678		return;
679	    }
680	}
681    }
682
683    if (event == TULIP_MEDIAPOLL_LINKFAIL) {
684	if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE) {
685	    if (TULIP_DO_AUTOSENSE(sc)) {
686#if defined(TULIP_DEBUG)
687		sc->tulip_dbg.dbg_link_failures++;
688#endif
689		sc->tulip_media = TULIP_MEDIA_UNKNOWN;
690		if (sc->tulip_if.if_flags & IFF_UP)
691		    tulip_reset(sc);	/* restart probe */
692	    }
693	    return;
694	}
695#if defined(TULIP_DEBUG)
696	sc->tulip_dbg.dbg_link_pollintrs++;
697#endif
698    }
699
700    if (event == TULIP_MEDIAPOLL_START) {
701	sc->tulip_if.if_flags |= IFF_OACTIVE;
702	if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE)
703	    return;
704	sc->tulip_probe_mediamask = 0;
705	sc->tulip_probe_passes = 0;
706#if defined(TULIP_DEBUG)
707	sc->tulip_dbg.dbg_media_probes++;
708#endif
709	/*
710	 * If the SROM contained an explicit media to use, use it.
711	 */
712	sc->tulip_cmdmode &= ~(TULIP_CMD_RXRUN|TULIP_CMD_FULLDUPLEX);
713	sc->tulip_flags |= TULIP_TRYNWAY|TULIP_PROBE1STPASS;
714	sc->tulip_flags &= ~(TULIP_DIDNWAY|TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
715	/*
716	 * connidx is defaulted to a media_unknown type.
717	 */
718	sc->tulip_probe_media = tulip_srom_conninfo[sc->tulip_connidx].sc_media;
719	if (sc->tulip_probe_media != TULIP_MEDIA_UNKNOWN) {
720	    tulip_linkup(sc, sc->tulip_probe_media);
721	    tulip_timeout(sc);
722	    return;
723	}
724
725	if (sc->tulip_features & TULIP_HAVE_GPR) {
726	    sc->tulip_probe_state = TULIP_PROBE_GPRTEST;
727	    sc->tulip_probe_timeout = 2000;
728	} else {
729	    sc->tulip_probe_media = TULIP_MEDIA_MAX;
730	    sc->tulip_probe_timeout = 0;
731	    sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
732	}
733    }
734
735    /*
736     * Ignore txprobe failures or spurious callbacks.
737     */
738    if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED
739	    && sc->tulip_probe_state != TULIP_PROBE_MEDIATEST) {
740	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
741	return;
742    }
743
744    /*
745     * If we really transmitted a packet, then that's the media we'll use.
746     */
747    if (event == TULIP_MEDIAPOLL_TXPROBE_OK || event == TULIP_MEDIAPOLL_LINKPASS) {
748	if (event == TULIP_MEDIAPOLL_LINKPASS) {
749	    /* XXX Check media status just to be sure */
750	    sc->tulip_probe_media = TULIP_MEDIA_10BASET;
751#if defined(TULIP_DEBUG)
752	} else {
753	    sc->tulip_dbg.dbg_txprobes_ok[sc->tulip_probe_media]++;
754#endif
755	}
756	tulip_linkup(sc, sc->tulip_probe_media);
757	tulip_timeout(sc);
758	return;
759    }
760
761    if (sc->tulip_probe_state == TULIP_PROBE_GPRTEST) {
762#if defined(TULIP_DO_GPR_SENSE)
763	/*
764	 * Check for media via the general purpose register.
765	 *
766	 * Try to sense the media via the GPR.  If the same value
767	 * occurs 3 times in a row then just use that.
768	 */
769	if (sc->tulip_probe_timeout > 0) {
770	    tulip_media_t new_probe_media = tulip_21140_gpr_media_sense(sc);
771#if defined(TULIP_DEBUG)
772	    printf(TULIP_PRINTF_FMT ": media_poll: gpr sensing = %s\n",
773		   TULIP_PRINTF_ARGS, tulip_mediums[new_probe_media]);
774#endif
775	    if (new_probe_media != TULIP_MEDIA_UNKNOWN) {
776		if (new_probe_media == sc->tulip_probe_media) {
777		    if (--sc->tulip_probe_count == 0)
778			tulip_linkup(sc, sc->tulip_probe_media);
779		} else {
780		    sc->tulip_probe_count = 10;
781		}
782	    }
783	    sc->tulip_probe_media = new_probe_media;
784	    tulip_timeout(sc);
785	    return;
786	}
787#endif /* TULIP_DO_GPR_SENSE */
788	/*
789	 * Brute force.  We cycle through each of the media types
790	 * and try to transmit a packet.
791	 */
792	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
793	sc->tulip_probe_media = TULIP_MEDIA_MAX;
794	sc->tulip_probe_timeout = 0;
795	tulip_timeout(sc);
796	return;
797    }
798
799    if (sc->tulip_probe_state != TULIP_PROBE_MEDIATEST
800	   && (sc->tulip_features & TULIP_HAVE_MII)) {
801	tulip_media_t old_media = sc->tulip_probe_media;
802	tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
803	switch (sc->tulip_probe_state) {
804	    case TULIP_PROBE_FAILED:
805	    case TULIP_PROBE_MEDIATEST: {
806		/*
807		 * Try the next media.
808		 */
809		sc->tulip_probe_mediamask |= sc->tulip_mediums[sc->tulip_probe_media]->mi_mediamask;
810		sc->tulip_probe_timeout = 0;
811#ifdef notyet
812		if (sc->tulip_probe_state == TULIP_PROBE_FAILED)
813		    break;
814		if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
815		    break;
816		sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 300;
817#endif
818		break;
819	    }
820	    case TULIP_PROBE_PHYAUTONEG: {
821		return;
822	    }
823	    case TULIP_PROBE_INACTIVE: {
824		/*
825		 * Only probe if we autonegotiated a media that hasn't failed.
826		 */
827		sc->tulip_probe_timeout = 0;
828		if (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media)) {
829		    sc->tulip_probe_media = old_media;
830		    break;
831		}
832		tulip_linkup(sc, sc->tulip_probe_media);
833		tulip_timeout(sc);
834		return;
835	    }
836	    default: {
837#if defined(DIAGNOSTIC) || defined(TULIP_DEBUG)
838		panic("tulip_media_poll: botch at line %d\n", __LINE__);
839#endif
840		break;
841	    }
842	}
843    }
844
845    if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED) {
846#if defined(TULIP_DEBUG)
847	sc->tulip_dbg.dbg_txprobes_failed[sc->tulip_probe_media]++;
848#endif
849	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
850	return;
851    }
852
853    /*
854     * switch to another media if we tried this one enough.
855     */
856    if (/* event == TULIP_MEDIAPOLL_TXPROBE_FAILED || */ sc->tulip_probe_timeout <= 0) {
857#if defined(TULIP_DEBUG)
858	if (sc->tulip_probe_media == TULIP_MEDIA_UNKNOWN) {
859	    printf(TULIP_PRINTF_FMT ": poll media unknown!\n",
860		   TULIP_PRINTF_ARGS);
861	    sc->tulip_probe_media = TULIP_MEDIA_MAX;
862	}
863#endif
864	/*
865	 * Find the next media type to check for.  Full Duplex
866	 * types are not allowed.
867	 */
868	do {
869	    sc->tulip_probe_media -= 1;
870	    if (sc->tulip_probe_media == TULIP_MEDIA_UNKNOWN) {
871		if (++sc->tulip_probe_passes == 3) {
872		    printf(TULIP_PRINTF_FMT ": autosense failed: cable problem?\n",
873			   TULIP_PRINTF_ARGS);
874		    if ((sc->tulip_if.if_flags & IFF_UP) == 0) {
875			sc->tulip_if.if_flags &= ~IFF_RUNNING;
876			sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
877			return;
878		    }
879		}
880		sc->tulip_flags ^= TULIP_TRYNWAY;	/* XXX */
881		sc->tulip_probe_mediamask = 0;
882		sc->tulip_probe_media = TULIP_MEDIA_MAX - 1;
883	    }
884	} while (sc->tulip_mediums[sc->tulip_probe_media] == NULL
885		 || (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media))
886		 || TULIP_IS_MEDIA_FD(sc->tulip_probe_media));
887
888#if defined(TULIP_DEBUG)
889	printf(TULIP_PRINTF_FMT ": %s: probing %s\n", TULIP_PRINTF_ARGS,
890	       event == TULIP_MEDIAPOLL_TXPROBE_FAILED ? "txprobe failed" : "timeout",
891	       tulip_mediums[sc->tulip_probe_media]);
892#endif
893	sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 1000;
894	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
895	sc->tulip_probe.probe_txprobes = 0;
896	tulip_reset(sc);
897	tulip_media_set(sc, sc->tulip_probe_media);
898	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
899    }
900    tulip_timeout(sc);
901
902    /*
903     * If this is hanging off a phy, we know are doing NWAY and we have
904     * forced the phy to a specific speed.  Wait for link up before
905     * before sending a packet.
906     */
907    switch (sc->tulip_mediums[sc->tulip_probe_media]->mi_type) {
908	case TULIP_MEDIAINFO_MII: {
909	    if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
910		return;
911	    break;
912	}
913	case TULIP_MEDIAINFO_SIA: {
914	    if (TULIP_IS_MEDIA_TP(sc->tulip_probe_media)) {
915		if (TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL)
916		    return;
917		tulip_linkup(sc, sc->tulip_probe_media);
918#ifdef notyet
919		if (sc->tulip_features & TULIP_HAVE_MII)
920		    tulip_timeout(sc);
921#endif
922		return;
923	    }
924	    break;
925	}
926	case TULIP_MEDIAINFO_RESET:
927	case TULIP_MEDIAINFO_SYM:
928	case TULIP_MEDIAINFO_NONE:
929	case TULIP_MEDIAINFO_GPR: {
930	    break;
931	}
932    }
933    /*
934     * Try to send a packet.
935     */
936    tulip_txprobe(sc);
937}
938
939static void
940tulip_media_select(
941    tulip_softc_t * const sc)
942{
943    if (sc->tulip_features & TULIP_HAVE_GPR) {
944	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
945	DELAY(10);
946	TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpdata);
947    }
948    /*
949     * If this board has no media, just return
950     */
951    if (sc->tulip_features & TULIP_HAVE_NOMEDIA)
952	return;
953
954    if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
955	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
956	(*sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_START);
957    } else {
958	tulip_media_set(sc, sc->tulip_media);
959    }
960}
961
962static void
963tulip_21040_mediainfo_init(
964    tulip_softc_t * const sc,
965    tulip_media_t media)
966{
967    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_THRSHLD160
968	|TULIP_CMD_BACKOFFCTR;
969    sc->tulip_if.if_baudrate = 10000000;
970
971    if (media == TULIP_MEDIA_10BASET || media == TULIP_MEDIA_UNKNOWN) {
972	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[0], 21040, 10BASET);
973	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[1], 21040, 10BASET_FD);
974	sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
975    }
976
977    if (media == TULIP_MEDIA_AUIBNC || media == TULIP_MEDIA_UNKNOWN) {
978	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[2], 21040, AUIBNC);
979    }
980
981    if (media == TULIP_MEDIA_UNKNOWN) {
982	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[3], 21040, EXTSIA);
983    }
984}
985
986static void
987tulip_21040_media_probe(
988    tulip_softc_t * const sc)
989{
990    tulip_21040_mediainfo_init(sc, TULIP_MEDIA_UNKNOWN);
991    return;
992}
993
994static void
995tulip_21040_10baset_only_media_probe(
996    tulip_softc_t * const sc)
997{
998    tulip_21040_mediainfo_init(sc, TULIP_MEDIA_10BASET);
999    tulip_media_set(sc, TULIP_MEDIA_10BASET);
1000    sc->tulip_media = TULIP_MEDIA_10BASET;
1001}
1002
1003static void
1004tulip_21040_10baset_only_media_select(
1005    tulip_softc_t * const sc)
1006{
1007    sc->tulip_flags |= TULIP_LINKUP;
1008    if (sc->tulip_media == TULIP_MEDIA_10BASET_FD) {
1009	sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
1010	sc->tulip_flags &= ~TULIP_SQETEST;
1011    } else {
1012	sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
1013	sc->tulip_flags |= TULIP_SQETEST;
1014    }
1015    tulip_media_set(sc, sc->tulip_media);
1016}
1017
1018static void
1019tulip_21040_auibnc_only_media_probe(
1020    tulip_softc_t * const sc)
1021{
1022    tulip_21040_mediainfo_init(sc, TULIP_MEDIA_AUIBNC);
1023    sc->tulip_flags |= TULIP_SQETEST|TULIP_LINKUP;
1024    tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
1025    sc->tulip_media = TULIP_MEDIA_AUIBNC;
1026}
1027
1028static void
1029tulip_21040_auibnc_only_media_select(
1030    tulip_softc_t * const sc)
1031{
1032    tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
1033    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
1034}
1035
1036static const tulip_boardsw_t tulip_21040_boardsw = {
1037    TULIP_21040_GENERIC,
1038    tulip_21040_media_probe,
1039    tulip_media_select,
1040    tulip_media_poll,
1041};
1042
1043static const tulip_boardsw_t tulip_21040_10baset_only_boardsw = {
1044    TULIP_21040_GENERIC,
1045    tulip_21040_10baset_only_media_probe,
1046    tulip_21040_10baset_only_media_select,
1047    NULL,
1048};
1049
1050static const tulip_boardsw_t tulip_21040_auibnc_only_boardsw = {
1051    TULIP_21040_GENERIC,
1052    tulip_21040_auibnc_only_media_probe,
1053    tulip_21040_auibnc_only_media_select,
1054    NULL,
1055};
1056
1057static void
1058tulip_21041_mediainfo_init(
1059    tulip_softc_t * const sc)
1060{
1061    tulip_media_info_t * const mi = sc->tulip_mediainfo;
1062
1063#ifdef notyet
1064    if (sc->tulip_revinfo >= 0x20) {
1065	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, 10BASET);
1066	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, 10BASET_FD);
1067	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, AUI);
1068	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, BNC);
1069	return;
1070    }
1071#endif
1072    TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041, 10BASET);
1073    TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041, 10BASET_FD);
1074    TULIP_MEDIAINFO_SIA_INIT(sc, &mi[2], 21041, AUI);
1075    TULIP_MEDIAINFO_SIA_INIT(sc, &mi[3], 21041, BNC);
1076}
1077
1078static void
1079tulip_21041_media_probe(
1080    tulip_softc_t * const sc)
1081{
1082    sc->tulip_if.if_baudrate = 10000000;
1083    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_ENHCAPTEFFCT
1084	|TULIP_CMD_THRSHLD160|TULIP_CMD_BACKOFFCTR;
1085    sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
1086    tulip_21041_mediainfo_init(sc);
1087}
1088
1089static void
1090tulip_21041_media_poll(
1091    tulip_softc_t * const sc,
1092    const tulip_mediapoll_event_t event)
1093{
1094    u_int32_t sia_status;
1095
1096#if defined(TULIP_DEBUG)
1097    sc->tulip_dbg.dbg_events[event]++;
1098#endif
1099
1100    if (event == TULIP_MEDIAPOLL_LINKFAIL) {
1101	if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE
1102		|| !TULIP_DO_AUTOSENSE(sc))
1103	    return;
1104	sc->tulip_media = TULIP_MEDIA_UNKNOWN;
1105	tulip_reset(sc);	/* start probe */
1106	return;
1107    }
1108
1109    /*
1110     * If we've been been asked to start a poll or link change interrupt
1111     * restart the probe (and reset the tulip to a known state).
1112     */
1113    if (event == TULIP_MEDIAPOLL_START) {
1114	sc->tulip_if.if_flags |= IFF_OACTIVE;
1115	sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_RXRUN);
1116#ifdef notyet
1117	if (sc->tulip_revinfo >= 0x20) {
1118	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
1119	    sc->tulip_flags |= TULIP_DIDNWAY;
1120	}
1121#endif
1122	TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1123	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1124	sc->tulip_probe_media = TULIP_MEDIA_10BASET;
1125	sc->tulip_probe_timeout = TULIP_21041_PROBE_10BASET_TIMEOUT;
1126	tulip_media_set(sc, TULIP_MEDIA_10BASET);
1127	tulip_timeout(sc);
1128	return;
1129    }
1130
1131    if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
1132	return;
1133
1134    if (event == TULIP_MEDIAPOLL_TXPROBE_OK) {
1135#if defined(TULIP_DEBUG)
1136	sc->tulip_dbg.dbg_txprobes_ok[sc->tulip_probe_media]++;
1137#endif
1138	tulip_linkup(sc, sc->tulip_probe_media);
1139	return;
1140    }
1141
1142    sia_status = TULIP_CSR_READ(sc, csr_sia_status);
1143    TULIP_CSR_WRITE(sc, csr_sia_status, sia_status);
1144    if ((sia_status & TULIP_SIASTS_LINKFAIL) == 0) {
1145	if (sc->tulip_revinfo >= 0x20) {
1146	    if (sia_status & (PHYSTS_10BASET_FD << (16 - 6)))
1147		sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
1148	}
1149	/*
1150	 * If the link has passed LinkPass, 10baseT is the
1151	 * proper media to use.
1152	 */
1153	tulip_linkup(sc, sc->tulip_probe_media);
1154	return;
1155    }
1156
1157    /*
1158     * wait for up to 2.4 seconds for the link to reach pass state.
1159     * Only then start scanning the other media for activity.
1160     * choose media with receive activity over those without.
1161     */
1162    if (sc->tulip_probe_media == TULIP_MEDIA_10BASET) {
1163	if (event != TULIP_MEDIAPOLL_TIMER)
1164	    return;
1165	if (sc->tulip_probe_timeout > 0
1166		&& (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) == 0) {
1167	    tulip_timeout(sc);
1168	    return;
1169	}
1170	sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1171	sc->tulip_flags |= TULIP_WANTRXACT;
1172	if (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) {
1173	    sc->tulip_probe_media = TULIP_MEDIA_BNC;
1174	} else {
1175	    sc->tulip_probe_media = TULIP_MEDIA_AUI;
1176	}
1177	tulip_media_set(sc, sc->tulip_probe_media);
1178	tulip_timeout(sc);
1179	return;
1180    }
1181
1182    /*
1183     * If we failed, clear the txprobe active flag.
1184     */
1185    if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED)
1186	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1187
1188
1189    if (event == TULIP_MEDIAPOLL_TIMER) {
1190	/*
1191	 * If we've received something, then that's our link!
1192	 */
1193	if (sc->tulip_flags & TULIP_RXACT) {
1194	    tulip_linkup(sc, sc->tulip_probe_media);
1195	    return;
1196	}
1197	/*
1198	 * if no txprobe active
1199	 */
1200	if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0
1201		&& ((sc->tulip_flags & TULIP_WANTRXACT) == 0
1202		    || (sia_status & TULIP_SIASTS_RXACTIVITY))) {
1203	    sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1204	    tulip_txprobe(sc);
1205	    tulip_timeout(sc);
1206	    return;
1207	}
1208	/*
1209	 * Take 2 passes through before deciding to not
1210	 * wait for receive activity.  Then take another
1211	 * two passes before spitting out a warning.
1212	 */
1213	if (sc->tulip_probe_timeout <= 0) {
1214	    if (sc->tulip_flags & TULIP_WANTRXACT) {
1215		sc->tulip_flags &= ~TULIP_WANTRXACT;
1216		sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1217	    } else {
1218		printf(TULIP_PRINTF_FMT ": autosense failed: cable problem?\n",
1219		       TULIP_PRINTF_ARGS);
1220		if ((sc->tulip_if.if_flags & IFF_UP) == 0) {
1221		    sc->tulip_if.if_flags &= ~IFF_RUNNING;
1222		    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1223		    return;
1224		}
1225	    }
1226	}
1227    }
1228
1229    /*
1230     * Since this media failed to probe, try the other one.
1231     */
1232    sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1233    if (sc->tulip_probe_media == TULIP_MEDIA_AUI) {
1234	sc->tulip_probe_media = TULIP_MEDIA_BNC;
1235    } else {
1236	sc->tulip_probe_media = TULIP_MEDIA_AUI;
1237    }
1238    tulip_media_set(sc, sc->tulip_probe_media);
1239    sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1240    tulip_timeout(sc);
1241}
1242
1243static const tulip_boardsw_t tulip_21041_boardsw = {
1244    TULIP_21041_GENERIC,
1245    tulip_21041_media_probe,
1246    tulip_media_select,
1247    tulip_21041_media_poll
1248};
1249
1250static const tulip_phy_attr_t tulip_mii_phy_attrlist[] = {
1251    { 0x20005c00, 0,		/* 08-00-17 */
1252      {
1253	{ 0x19, 0x0040, 0x0040 },	/* 10TX */
1254	{ 0x19, 0x0040, 0x0000 },	/* 100TX */
1255      },
1256#if defined(TULIP_DEBUG)
1257      "NS DP83840",
1258#endif
1259    },
1260    { 0x0281F400, 0,		/* 00-A0-7D */
1261      {
1262	{ 0x12, 0x0010, 0x0000 },	/* 10T */
1263	{ },				/* 100TX */
1264	{ 0x12, 0x0010, 0x0010 },	/* 100T4 */
1265	{ 0x12, 0x0008, 0x0008 },	/* FULL_DUPLEX */
1266      },
1267#if defined(TULIP_DEBUG)
1268      "Seeq 80C240"
1269#endif
1270    },
1271#if 0
1272    { 0x0015F420, 0,	/* 00-A0-7D */
1273      {
1274	{ 0x12, 0x0010, 0x0000 },	/* 10T */
1275	{ },				/* 100TX */
1276	{ 0x12, 0x0010, 0x0010 },	/* 100T4 */
1277	{ 0x12, 0x0008, 0x0008 },	/* FULL_DUPLEX */
1278      },
1279#if defined(TULIP_DEBUG)
1280      "Broadcom BCM5000"
1281#endif
1282    },
1283#endif
1284    { 0x0281F400, 0,		/* 00-A0-BE */
1285      {
1286	{ 0x11, 0x8000, 0x0000 },	/* 10T */
1287	{ 0x11, 0x8000, 0x8000 },	/* 100TX */
1288	{ },				/* 100T4 */
1289	{ 0x11, 0x4000, 0x4000 },	/* FULL_DUPLEX */
1290      },
1291#if defined(TULIP_DEBUG)
1292      "ICS 1890"
1293#endif
1294    },
1295    { 0 }
1296};
1297
1298static tulip_media_t
1299tulip_mii_phy_readspecific(
1300    tulip_softc_t * const sc)
1301{
1302    const tulip_phy_attr_t *attr;
1303    u_int16_t data;
1304    u_int32_t id;
1305    unsigned idx = 0;
1306    static const tulip_media_t table[] = {
1307	TULIP_MEDIA_UNKNOWN,
1308	TULIP_MEDIA_10BASET,
1309	TULIP_MEDIA_100BASETX,
1310	TULIP_MEDIA_100BASET4,
1311	TULIP_MEDIA_UNKNOWN,
1312	TULIP_MEDIA_10BASET_FD,
1313	TULIP_MEDIA_100BASETX_FD,
1314	TULIP_MEDIA_UNKNOWN
1315    };
1316
1317    /*
1318     * Don't read phy specific registers if link is not up.
1319     */
1320    data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
1321    if ((data & (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS)) != (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS))
1322	return TULIP_MEDIA_UNKNOWN;
1323
1324    id = (tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDLOW) << 16) |
1325	tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDHIGH);
1326    for (attr = tulip_mii_phy_attrlist;; attr++) {
1327	if (attr->attr_id == 0)
1328	    return TULIP_MEDIA_UNKNOWN;
1329	if ((id & ~0x0F) == attr->attr_id)
1330	    break;
1331    }
1332
1333    if (attr->attr_modes[PHY_MODE_100TX].pm_regno) {
1334	const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100TX];
1335	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1336	if ((data & pm->pm_mask) == pm->pm_value)
1337	    idx = 2;
1338    }
1339    if (idx == 0 && attr->attr_modes[PHY_MODE_100T4].pm_regno) {
1340	const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100T4];
1341	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1342	if ((data & pm->pm_mask) == pm->pm_value)
1343	    idx = 3;
1344    }
1345    if (idx == 0 && attr->attr_modes[PHY_MODE_10T].pm_regno) {
1346	const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_10T];
1347	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1348	if ((data & pm->pm_mask) == pm->pm_value)
1349	    idx = 1;
1350    }
1351    if (idx != 0 && attr->attr_modes[PHY_MODE_FULLDUPLEX].pm_regno) {
1352	const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_FULLDUPLEX];
1353	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1354	idx += ((data & pm->pm_mask) == pm->pm_value ? 4 : 0);
1355    }
1356    return table[idx];
1357}
1358
1359static unsigned
1360tulip_mii_get_phyaddr(
1361    tulip_softc_t * const sc,
1362    unsigned offset)
1363{
1364    unsigned phyaddr;
1365
1366    for (phyaddr = 1; phyaddr < 32; phyaddr++) {
1367	unsigned status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1368	if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
1369	    continue;
1370	if (offset == 0)
1371	    return phyaddr;
1372	offset--;
1373    }
1374    if (offset == 0) {
1375	unsigned status = tulip_mii_readreg(sc, 0, PHYREG_STATUS);
1376	if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
1377	    return TULIP_MII_NOPHY;
1378	return 0;
1379    }
1380    return TULIP_MII_NOPHY;
1381}
1382
1383static int
1384tulip_mii_map_abilities(
1385    tulip_softc_t * const sc,
1386    unsigned abilities)
1387{
1388    sc->tulip_abilities = abilities;
1389    if (abilities & PHYSTS_100BASETX_FD) {
1390	sc->tulip_probe_media = TULIP_MEDIA_100BASETX_FD;
1391    } else if (abilities & PHYSTS_100BASET4) {
1392	sc->tulip_probe_media = TULIP_MEDIA_100BASET4;
1393    } else if (abilities & PHYSTS_100BASETX) {
1394	sc->tulip_probe_media = TULIP_MEDIA_100BASETX;
1395    } else if (abilities & PHYSTS_10BASET_FD) {
1396	sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
1397    } else if (abilities & PHYSTS_10BASET) {
1398	sc->tulip_probe_media = TULIP_MEDIA_10BASET;
1399    } else {
1400	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1401	return 0;
1402    }
1403    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1404    return 1;
1405}
1406
1407static void
1408tulip_mii_autonegotiate(
1409    tulip_softc_t * const sc,
1410    const unsigned phyaddr)
1411{
1412    switch (sc->tulip_probe_state) {
1413        case TULIP_PROBE_MEDIATEST:
1414        case TULIP_PROBE_INACTIVE: {
1415	    sc->tulip_flags |= TULIP_DIDNWAY;
1416	    tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, PHYCTL_RESET);
1417	    sc->tulip_probe_timeout = 3000;
1418	    sc->tulip_intrmask |= TULIP_STS_ABNRMLINTR|TULIP_STS_NORMALINTR;
1419	    sc->tulip_probe_state = TULIP_PROBE_PHYRESET;
1420	    /* FALL THROUGH */
1421	}
1422        case TULIP_PROBE_PHYRESET: {
1423	    u_int32_t status;
1424	    u_int32_t data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
1425	    if (data & PHYCTL_RESET) {
1426		if (sc->tulip_probe_timeout > 0) {
1427		    tulip_timeout(sc);
1428		    return;
1429		}
1430		printf(TULIP_PRINTF_FMT "(phy%d): error: reset of PHY never completed!\n",
1431			   TULIP_PRINTF_ARGS, phyaddr);
1432		sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1433		sc->tulip_probe_state = TULIP_PROBE_FAILED;
1434		sc->tulip_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1435		return;
1436	    }
1437	    status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1438	    if ((status & PHYSTS_CAN_AUTONEG) == 0) {
1439#if defined(TULIP_DEBUG)
1440		loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation disabled\n",
1441			   TULIP_PRINTF_ARGS, phyaddr);
1442#endif
1443		sc->tulip_flags &= ~TULIP_DIDNWAY;
1444		sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1445		return;
1446	    }
1447	    if (tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT) != ((status >> 6) | 0x01))
1448		tulip_mii_writereg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT, (status >> 6) | 0x01);
1449	    tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, data|PHYCTL_AUTONEG_RESTART|PHYCTL_AUTONEG_ENABLE);
1450	    data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
1451#if defined(TULIP_DEBUG)
1452	    if ((data & PHYCTL_AUTONEG_ENABLE) == 0)
1453		loudprintf(TULIP_PRINTF_FMT "(phy%d): oops: enable autonegotiation failed: 0x%04x\n",
1454			   TULIP_PRINTF_ARGS, phyaddr, data);
1455	    else
1456		loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation restarted: 0x%04x\n",
1457			   TULIP_PRINTF_ARGS, phyaddr, data);
1458	    sc->tulip_dbg.dbg_nway_starts++;
1459#endif
1460	    sc->tulip_probe_state = TULIP_PROBE_PHYAUTONEG;
1461	    sc->tulip_probe_timeout = 3000;
1462	    /* FALL THROUGH */
1463	}
1464        case TULIP_PROBE_PHYAUTONEG: {
1465	    u_int32_t status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1466	    u_int32_t data;
1467	    if ((status & PHYSTS_AUTONEG_DONE) == 0) {
1468		if (sc->tulip_probe_timeout > 0) {
1469		    tulip_timeout(sc);
1470		    return;
1471		}
1472#if defined(TULIP_DEBUG)
1473		loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation timeout: sts=0x%04x, ctl=0x%04x\n",
1474			   TULIP_PRINTF_ARGS, phyaddr, status,
1475			   tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL));
1476#endif
1477		sc->tulip_flags &= ~TULIP_DIDNWAY;
1478		sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1479		return;
1480	    }
1481	    data = tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ABILITIES);
1482#if defined(TULIP_DEBUG)
1483	    loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation complete: 0x%04x\n",
1484		       TULIP_PRINTF_ARGS, phyaddr, data);
1485#endif
1486	    data = (data << 6) & status;
1487	    if (!tulip_mii_map_abilities(sc, data))
1488		sc->tulip_flags &= ~TULIP_DIDNWAY;
1489	    return;
1490	}
1491	default: {
1492#if defined(DIAGNOSTIC)
1493	    panic("tulip_media_poll: botch at line %d\n", __LINE__);
1494#endif
1495	    break;
1496	}
1497    }
1498#if defined(TULIP_DEBUG)
1499    loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation failure: state = %d\n",
1500	       TULIP_PRINTF_ARGS, phyaddr, sc->tulip_probe_state);
1501	    sc->tulip_dbg.dbg_nway_failures++;
1502#endif
1503}
1504
1505static void
1506tulip_2114x_media_preset(
1507    tulip_softc_t * const sc)
1508{
1509    const tulip_media_info_t *mi = NULL;
1510    tulip_media_t media = sc->tulip_media;
1511
1512    if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
1513	media = sc->tulip_media;
1514    else
1515	media = sc->tulip_probe_media;
1516
1517    sc->tulip_cmdmode &= ~TULIP_CMD_PORTSELECT;
1518    sc->tulip_flags &= ~TULIP_SQETEST;
1519    if (media != TULIP_MEDIA_UNKNOWN && media != TULIP_MEDIA_MAX) {
1520#if defined(TULIP_DEBUG)
1521	if (media < TULIP_MEDIA_MAX && sc->tulip_mediums[media] != NULL) {
1522#endif
1523	    mi = sc->tulip_mediums[media];
1524	    if (mi->mi_type == TULIP_MEDIAINFO_MII) {
1525		sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
1526	    } else if (mi->mi_type == TULIP_MEDIAINFO_GPR
1527		       || mi->mi_type == TULIP_MEDIAINFO_SYM) {
1528		sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
1529		sc->tulip_cmdmode |= mi->mi_cmdmode;
1530	    } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
1531		TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
1532	    }
1533#if defined(TULIP_DEBUG)
1534	} else {
1535	    printf(TULIP_PRINTF_FMT ": preset: bad media %d!\n",
1536		   TULIP_PRINTF_ARGS, media);
1537	}
1538#endif
1539    }
1540    switch (media) {
1541	case TULIP_MEDIA_BNC:
1542	case TULIP_MEDIA_AUI:
1543	case TULIP_MEDIA_10BASET: {
1544	    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
1545	    sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
1546	    sc->tulip_if.if_baudrate = 10000000;
1547	    sc->tulip_flags |= TULIP_SQETEST;
1548	    break;
1549	}
1550	case TULIP_MEDIA_10BASET_FD: {
1551	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL;
1552	    sc->tulip_if.if_baudrate = 10000000;
1553	    break;
1554	}
1555	case TULIP_MEDIA_100BASEFX:
1556	case TULIP_MEDIA_100BASET4:
1557	case TULIP_MEDIA_100BASETX: {
1558	    sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL);
1559	    sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
1560	    sc->tulip_if.if_baudrate = 100000000;
1561	    break;
1562	}
1563	case TULIP_MEDIA_100BASEFX_FD:
1564	case TULIP_MEDIA_100BASETX_FD: {
1565	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_PORTSELECT;
1566	    sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
1567	    sc->tulip_if.if_baudrate = 100000000;
1568	    break;
1569	}
1570	default: {
1571	    break;
1572	}
1573    }
1574    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1575}
1576
1577/*
1578 ********************************************************************
1579 *  Start of 21140/21140A support which does not use the MII interface
1580 */
1581
1582static void
1583tulip_null_media_poll(
1584    tulip_softc_t * const sc,
1585    tulip_mediapoll_event_t event)
1586{
1587#if defined(TULIP_DEBUG)
1588    sc->tulip_dbg.dbg_events[event]++;
1589#endif
1590#if defined(DIAGNOSTIC)
1591    printf(TULIP_PRINTF_FMT ": botch(media_poll) at line %d\n",
1592	   TULIP_PRINTF_ARGS, __LINE__);
1593#endif
1594}
1595
1596__inline__ static void
1597tulip_21140_mediainit(
1598    tulip_softc_t * const sc,
1599    tulip_media_info_t * const mip,
1600    tulip_media_t const media,
1601    unsigned gpdata,
1602    unsigned cmdmode)
1603{
1604    sc->tulip_mediums[media] = mip;
1605    mip->mi_type = TULIP_MEDIAINFO_GPR;
1606    mip->mi_cmdmode = cmdmode;
1607    mip->mi_gpdata = gpdata;
1608}
1609
1610static void
1611tulip_21140_evalboard_media_probe(
1612    tulip_softc_t * const sc)
1613{
1614    tulip_media_info_t *mip = sc->tulip_mediainfo;
1615
1616    sc->tulip_gpinit = TULIP_GP_EB_PINS;
1617    sc->tulip_gpdata = TULIP_GP_EB_INIT;
1618    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
1619    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
1620    TULIP_CSR_WRITE(sc, csr_command,
1621	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1622	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1623    TULIP_CSR_WRITE(sc, csr_command,
1624	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1625    DELAY(1000000);
1626    if ((TULIP_CSR_READ(sc, csr_gp) & TULIP_GP_EB_OK100) != 0) {
1627	sc->tulip_media = TULIP_MEDIA_10BASET;
1628    } else {
1629	sc->tulip_media = TULIP_MEDIA_100BASETX;
1630    }
1631    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1632			  TULIP_GP_EB_INIT,
1633			  TULIP_CMD_TXTHRSHLDCTL);
1634    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1635			  TULIP_GP_EB_INIT,
1636			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1637    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1638			  TULIP_GP_EB_INIT,
1639			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1640			      |TULIP_CMD_SCRAMBLER);
1641    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1642			  TULIP_GP_EB_INIT,
1643			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1644			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1645}
1646
1647static const tulip_boardsw_t tulip_21140_eb_boardsw = {
1648    TULIP_21140_DEC_EB,
1649    tulip_21140_evalboard_media_probe,
1650    tulip_media_select,
1651    tulip_null_media_poll,
1652    tulip_2114x_media_preset,
1653};
1654
1655static void
1656tulip_21140_accton_media_probe(
1657    tulip_softc_t * const sc)
1658{
1659    tulip_media_info_t *mip = sc->tulip_mediainfo;
1660    unsigned gpdata;
1661
1662    sc->tulip_gpinit = TULIP_GP_EB_PINS;
1663    sc->tulip_gpdata = TULIP_GP_EB_INIT;
1664    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
1665    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
1666    TULIP_CSR_WRITE(sc, csr_command,
1667	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1668	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1669    TULIP_CSR_WRITE(sc, csr_command,
1670	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1671    DELAY(1000000);
1672    gpdata = TULIP_CSR_READ(sc, csr_gp);
1673    if ((gpdata & TULIP_GP_EN1207_UTP_INIT) == 0) {
1674	sc->tulip_media = TULIP_MEDIA_10BASET;
1675    } else {
1676	if ((gpdata & TULIP_GP_EN1207_BNC_INIT) == 0) {
1677		sc->tulip_media = TULIP_MEDIA_BNC;
1678        } else {
1679		sc->tulip_media = TULIP_MEDIA_100BASETX;
1680        }
1681    }
1682    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_BNC,
1683			  TULIP_GP_EN1207_BNC_INIT,
1684			  TULIP_CMD_TXTHRSHLDCTL);
1685    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1686			  TULIP_GP_EN1207_UTP_INIT,
1687			  TULIP_CMD_TXTHRSHLDCTL);
1688    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1689			  TULIP_GP_EN1207_UTP_INIT,
1690			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1691    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1692			  TULIP_GP_EN1207_100_INIT,
1693			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1694			      |TULIP_CMD_SCRAMBLER);
1695    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1696			  TULIP_GP_EN1207_100_INIT,
1697			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1698			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1699}
1700
1701static const tulip_boardsw_t tulip_21140_accton_boardsw = {
1702    TULIP_21140_EN1207,
1703    tulip_21140_accton_media_probe,
1704    tulip_media_select,
1705    tulip_null_media_poll,
1706    tulip_2114x_media_preset,
1707};
1708
1709static void
1710tulip_21140_smc9332_media_probe(
1711    tulip_softc_t * const sc)
1712{
1713    tulip_media_info_t *mip = sc->tulip_mediainfo;
1714    int idx, cnt = 0;
1715
1716    TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT|TULIP_CMD_MUSTBEONE);
1717    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
1718    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
1719		   33MHz that comes to two microseconds but wait a
1720		   bit longer anyways) */
1721    TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT |
1722	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1723    sc->tulip_gpinit = TULIP_GP_SMC_9332_PINS;
1724    sc->tulip_gpdata = TULIP_GP_SMC_9332_INIT;
1725    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_PINS|TULIP_GP_PINSET);
1726    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_INIT);
1727    DELAY(200000);
1728    for (idx = 1000; idx > 0; idx--) {
1729	u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
1730	if ((csr & (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) == (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) {
1731	    if (++cnt > 100)
1732		break;
1733	} else if ((csr & TULIP_GP_SMC_9332_OK10) == 0) {
1734	    break;
1735	} else {
1736	    cnt = 0;
1737	}
1738	DELAY(1000);
1739    }
1740    sc->tulip_media = cnt > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
1741    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1742			  TULIP_GP_SMC_9332_INIT,
1743			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1744			      |TULIP_CMD_SCRAMBLER);
1745    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1746			  TULIP_GP_SMC_9332_INIT,
1747			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1748			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1749    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1750			  TULIP_GP_SMC_9332_INIT,
1751			  TULIP_CMD_TXTHRSHLDCTL);
1752    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1753			  TULIP_GP_SMC_9332_INIT,
1754			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1755}
1756
1757static const tulip_boardsw_t tulip_21140_smc9332_boardsw = {
1758    TULIP_21140_SMC_9332,
1759    tulip_21140_smc9332_media_probe,
1760    tulip_media_select,
1761    tulip_null_media_poll,
1762    tulip_2114x_media_preset,
1763};
1764
1765static void
1766tulip_21140_cogent_em100_media_probe(
1767    tulip_softc_t * const sc)
1768{
1769    tulip_media_info_t *mip = sc->tulip_mediainfo;
1770    u_int32_t cmdmode = TULIP_CSR_READ(sc, csr_command);
1771
1772    sc->tulip_gpinit = TULIP_GP_EM100_PINS;
1773    sc->tulip_gpdata = TULIP_GP_EM100_INIT;
1774    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_PINS);
1775    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_INIT);
1776
1777    cmdmode = TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_MUSTBEONE;
1778    cmdmode &= ~(TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_SCRAMBLER);
1779    if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
1780	TULIP_CSR_WRITE(sc, csr_command, cmdmode);
1781	sc->tulip_media = TULIP_MEDIA_100BASEFX;
1782
1783	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX,
1784			  TULIP_GP_EM100_INIT,
1785			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION);
1786	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX_FD,
1787			  TULIP_GP_EM100_INIT,
1788			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1789			      |TULIP_CMD_FULLDUPLEX);
1790    } else {
1791	TULIP_CSR_WRITE(sc, csr_command, cmdmode|TULIP_CMD_SCRAMBLER);
1792	sc->tulip_media = TULIP_MEDIA_100BASETX;
1793	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1794			  TULIP_GP_EM100_INIT,
1795			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1796			      |TULIP_CMD_SCRAMBLER);
1797	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1798			  TULIP_GP_EM100_INIT,
1799			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1800			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1801    }
1802}
1803
1804static const tulip_boardsw_t tulip_21140_cogent_em100_boardsw = {
1805    TULIP_21140_COGENT_EM100,
1806    tulip_21140_cogent_em100_media_probe,
1807    tulip_media_select,
1808    tulip_null_media_poll,
1809    tulip_2114x_media_preset
1810};
1811
1812static void
1813tulip_21140_znyx_zx34x_media_probe(
1814    tulip_softc_t * const sc)
1815{
1816    tulip_media_info_t *mip = sc->tulip_mediainfo;
1817    int cnt10 = 0, cnt100 = 0, idx;
1818
1819    sc->tulip_gpinit = TULIP_GP_ZX34X_PINS;
1820    sc->tulip_gpdata = TULIP_GP_ZX34X_INIT;
1821    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_PINS);
1822    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_INIT);
1823    TULIP_CSR_WRITE(sc, csr_command,
1824	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1825	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1826    TULIP_CSR_WRITE(sc, csr_command,
1827	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1828
1829    DELAY(200000);
1830    for (idx = 1000; idx > 0; idx--) {
1831	u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
1832	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)) {
1833	    if (++cnt100 > 100)
1834		break;
1835	} else if ((csr & TULIP_GP_ZX34X_LNKFAIL) == 0) {
1836	    if (++cnt10 > 100)
1837		break;
1838	} else {
1839	    cnt10 = 0;
1840	    cnt100 = 0;
1841	}
1842	DELAY(1000);
1843    }
1844    sc->tulip_media = cnt100 > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
1845    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1846			  TULIP_GP_ZX34X_INIT,
1847			  TULIP_CMD_TXTHRSHLDCTL);
1848    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1849			  TULIP_GP_ZX34X_INIT,
1850			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1851    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1852			  TULIP_GP_ZX34X_INIT,
1853			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1854			      |TULIP_CMD_SCRAMBLER);
1855    tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1856			  TULIP_GP_ZX34X_INIT,
1857			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1858			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1859}
1860
1861static const tulip_boardsw_t tulip_21140_znyx_zx34x_boardsw = {
1862    TULIP_21140_ZNYX_ZX34X,
1863    tulip_21140_znyx_zx34x_media_probe,
1864    tulip_media_select,
1865    tulip_null_media_poll,
1866    tulip_2114x_media_preset,
1867};
1868
1869static void
1870tulip_2114x_media_probe(
1871    tulip_softc_t * const sc)
1872{
1873    sc->tulip_cmdmode |= TULIP_CMD_MUSTBEONE
1874	|TULIP_CMD_BACKOFFCTR|TULIP_CMD_THRSHLD72;
1875}
1876
1877static const tulip_boardsw_t tulip_2114x_isv_boardsw = {
1878    TULIP_21140_ISV,
1879    tulip_2114x_media_probe,
1880    tulip_media_select,
1881    tulip_media_poll,
1882    tulip_2114x_media_preset,
1883};
1884
1885/*
1886 * ******** END of chip-specific handlers. ***********
1887 */
1888
1889/*
1890 * Code the read the SROM and MII bit streams (I2C)
1891 */
1892static void
1893tulip_delay_300ns(
1894    tulip_softc_t * const sc)
1895{
1896    int idx;
1897    for (idx = (300 / 33) + 1; idx > 0; idx--)
1898	(void) TULIP_CSR_READ(sc, csr_busmode);
1899}
1900
1901#define EMIT    do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); tulip_delay_300ns(sc); } while (0)
1902
1903static void
1904tulip_srom_idle(
1905    tulip_softc_t * const sc)
1906{
1907    unsigned bit, csr;
1908
1909    csr  = SROMSEL ; EMIT;
1910    csr  = SROMSEL | SROMRD; EMIT;
1911    csr ^= SROMCS; EMIT;
1912    csr ^= SROMCLKON; EMIT;
1913
1914    /*
1915     * Write 25 cycles of 0 which will force the SROM to be idle.
1916     */
1917    for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
1918        csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1919        csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1920    }
1921    csr ^= SROMCLKOFF; EMIT;
1922    csr ^= SROMCS; EMIT;
1923    csr  = 0; EMIT;
1924}
1925
1926
1927static void
1928tulip_srom_read(
1929    tulip_softc_t * const sc)
1930{
1931    unsigned idx;
1932    const unsigned bitwidth = SROM_BITWIDTH;
1933    const unsigned cmdmask = (SROMCMD_RD << bitwidth);
1934    const unsigned msb = 1 << (bitwidth + 3 - 1);
1935    unsigned lastidx = (1 << bitwidth) - 1;
1936
1937    tulip_srom_idle(sc);
1938
1939    for (idx = 0; idx <= lastidx; idx++) {
1940        unsigned lastbit, data, bits, bit, csr;
1941	csr  = SROMSEL ;	        EMIT;
1942        csr  = SROMSEL | SROMRD;        EMIT;
1943        csr ^= SROMCSON;                EMIT;
1944        csr ^=            SROMCLKON;    EMIT;
1945
1946        lastbit = 0;
1947        for (bits = idx|cmdmask, bit = bitwidth + 3; bit > 0; bit--, bits <<= 1) {
1948            const unsigned thisbit = bits & msb;
1949            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1950            if (thisbit != lastbit) {
1951                csr ^= SROMDOUT; EMIT;  /* clock low; invert data */
1952            } else {
1953		EMIT;
1954	    }
1955            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1956            lastbit = thisbit;
1957        }
1958        csr ^= SROMCLKOFF; EMIT;
1959
1960        for (data = 0, bits = 0; bits < 16; bits++) {
1961            data <<= 1;
1962            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1963            data |= TULIP_CSR_READ(sc, csr_srom_mii) & SROMDIN ? 1 : 0;
1964            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1965        }
1966	sc->tulip_rombuf[idx*2] = data & 0xFF;
1967	sc->tulip_rombuf[idx*2+1] = data >> 8;
1968	csr  = SROMSEL | SROMRD; EMIT;
1969	csr  = 0; EMIT;
1970    }
1971    tulip_srom_idle(sc);
1972}
1973
1974#define MII_EMIT    do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); tulip_delay_300ns(sc); } while (0)
1975
1976static void
1977tulip_mii_writebits(
1978    tulip_softc_t * const sc,
1979    unsigned data,
1980    unsigned bits)
1981{
1982    unsigned msb = 1 << (bits - 1);
1983    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1984    unsigned lastbit = (csr & MII_DOUT) ? msb : 0;
1985
1986    csr |= MII_WR; MII_EMIT;  		/* clock low; assert write */
1987
1988    for (; bits > 0; bits--, data <<= 1) {
1989	const unsigned thisbit = data & msb;
1990	if (thisbit != lastbit) {
1991	    csr ^= MII_DOUT; MII_EMIT;  /* clock low; invert data */
1992	}
1993	csr ^= MII_CLKON; MII_EMIT;     /* clock high; data valid */
1994	lastbit = thisbit;
1995	csr ^= MII_CLKOFF; MII_EMIT;    /* clock low; data not valid */
1996    }
1997}
1998
1999static void
2000tulip_mii_turnaround(
2001    tulip_softc_t * const sc,
2002    unsigned cmd)
2003{
2004    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2005
2006    if (cmd == MII_WRCMD) {
2007	csr |= MII_DOUT; MII_EMIT;	/* clock low; change data */
2008	csr ^= MII_CLKON; MII_EMIT;	/* clock high; data valid */
2009	csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
2010	csr ^= MII_DOUT; MII_EMIT;	/* clock low; change data */
2011    } else {
2012	csr |= MII_RD; MII_EMIT;	/* clock low; switch to read */
2013    }
2014    csr ^= MII_CLKON; MII_EMIT;		/* clock high; data valid */
2015    csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
2016}
2017
2018static unsigned
2019tulip_mii_readbits(
2020    tulip_softc_t * const sc)
2021{
2022    unsigned data;
2023    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2024    int idx;
2025
2026    for (idx = 0, data = 0; idx < 16; idx++) {
2027	data <<= 1;	/* this is NOOP on the first pass through */
2028	csr ^= MII_CLKON; MII_EMIT;	/* clock high; data valid */
2029	if (TULIP_CSR_READ(sc, csr_srom_mii) & MII_DIN)
2030	    data |= 1;
2031	csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
2032    }
2033    csr ^= MII_RD; MII_EMIT;		/* clock low; turn off read */
2034
2035    return data;
2036}
2037
2038static unsigned
2039tulip_mii_readreg(
2040    tulip_softc_t * const sc,
2041    unsigned devaddr,
2042    unsigned regno)
2043{
2044    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2045    unsigned data;
2046
2047    csr &= ~(MII_RD|MII_CLK); MII_EMIT;
2048    tulip_mii_writebits(sc, MII_PREAMBLE, 32);
2049    tulip_mii_writebits(sc, MII_RDCMD, 8);
2050    tulip_mii_writebits(sc, devaddr, 5);
2051    tulip_mii_writebits(sc, regno, 5);
2052    tulip_mii_turnaround(sc, MII_RDCMD);
2053
2054    data = tulip_mii_readbits(sc);
2055#if defined(TULIP_DEBUG)
2056    sc->tulip_dbg.dbg_phyregs[regno][0] = data;
2057    sc->tulip_dbg.dbg_phyregs[regno][1]++;
2058#endif
2059    return data;
2060}
2061
2062static void
2063tulip_mii_writereg(
2064    tulip_softc_t * const sc,
2065    unsigned devaddr,
2066    unsigned regno,
2067    unsigned data)
2068{
2069    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2070    csr &= ~(MII_RD|MII_CLK); MII_EMIT;
2071    tulip_mii_writebits(sc, MII_PREAMBLE, 32);
2072    tulip_mii_writebits(sc, MII_WRCMD, 8);
2073    tulip_mii_writebits(sc, devaddr, 5);
2074    tulip_mii_writebits(sc, regno, 5);
2075    tulip_mii_turnaround(sc, MII_WRCMD);
2076    tulip_mii_writebits(sc, data, 16);
2077#if defined(TULIP_DEBUG)
2078    sc->tulip_dbg.dbg_phyregs[regno][2] = data;
2079    sc->tulip_dbg.dbg_phyregs[regno][3]++;
2080#endif
2081}
2082
2083#define	tulip_mchash(mca)	(tulip_crc32(mca, 6) & 0x1FF)
2084#define	tulip_srom_crcok(databuf)	( \
2085    ((tulip_crc32(databuf, 126) & 0xFFFFU) ^ 0xFFFFU) == \
2086     ((databuf)[126] | ((databuf)[127] << 8)))
2087
2088static unsigned
2089tulip_crc32(
2090    const unsigned char *databuf,
2091    size_t datalen)
2092{
2093    u_int idx, crc = 0xFFFFFFFFUL;
2094    static const u_int crctab[] = {
2095	0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
2096	0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
2097	0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
2098	0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
2099    };
2100
2101    for (idx = 0; idx < datalen; idx++) {
2102	crc ^= *databuf++;
2103	crc = (crc >> 4) ^ crctab[crc & 0xf];
2104	crc = (crc >> 4) ^ crctab[crc & 0xf];
2105    }
2106    return crc;
2107}
2108
2109static void
2110tulip_identify_dec_nic(
2111    tulip_softc_t * const sc)
2112{
2113    strcpy(sc->tulip_boardid, "DEC ");
2114#define D0	4
2115    if (sc->tulip_chipid <= TULIP_DE425)
2116	return;
2117    if (bcmp(sc->tulip_rombuf + 29, "DE500", 5) == 0
2118	|| bcmp(sc->tulip_rombuf + 29, "DE450", 5) == 0) {
2119	bcopy(sc->tulip_rombuf + 29, &sc->tulip_boardid[D0], 8);
2120	sc->tulip_boardid[D0+8] = ' ';
2121    }
2122#undef D0
2123}
2124
2125static void
2126tulip_identify_znyx_nic(
2127    tulip_softc_t * const sc)
2128{
2129    unsigned id = 0;
2130    strcpy(sc->tulip_boardid, "ZNYX ZX3XX ");
2131    if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
2132	unsigned znyx_ptr;
2133	sc->tulip_boardid[8] = '4';
2134	znyx_ptr = sc->tulip_rombuf[124] + 256 * sc->tulip_rombuf[125];
2135	if (znyx_ptr < 26 || znyx_ptr > 116) {
2136	    sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2137	    return;
2138	}
2139	/* ZX344 = 0010 .. 0013FF
2140	 */
2141	if (sc->tulip_rombuf[znyx_ptr] == 0x4A
2142		&& sc->tulip_rombuf[znyx_ptr + 1] == 0x52
2143		&& sc->tulip_rombuf[znyx_ptr + 2] == 0x01) {
2144	    id = sc->tulip_rombuf[znyx_ptr + 5] + 256 * sc->tulip_rombuf[znyx_ptr + 4];
2145	    if ((id >> 8) == (TULIP_ZNYX_ID_ZX342 >> 8)) {
2146		sc->tulip_boardid[9] = '2';
2147		if (id == TULIP_ZNYX_ID_ZX342B) {
2148		    sc->tulip_boardid[10] = 'B';
2149		    sc->tulip_boardid[11] = ' ';
2150		}
2151		sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2152	    } else if (id == TULIP_ZNYX_ID_ZX344) {
2153		sc->tulip_boardid[10] = '4';
2154		sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2155	    } else if (id == TULIP_ZNYX_ID_ZX345) {
2156		sc->tulip_boardid[9] = (sc->tulip_rombuf[19] > 1) ? '8' : '5';
2157	    } else if (id == TULIP_ZNYX_ID_ZX346) {
2158		sc->tulip_boardid[9] = '6';
2159	    } else if (id == TULIP_ZNYX_ID_ZX351) {
2160		sc->tulip_boardid[8] = '5';
2161		sc->tulip_boardid[9] = '1';
2162	    }
2163	}
2164	if (id == 0) {
2165	    /*
2166	     * Assume it's a ZX342...
2167	     */
2168	    sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2169	}
2170	return;
2171    }
2172    sc->tulip_boardid[8] = '1';
2173    if (sc->tulip_chipid == TULIP_21041) {
2174	sc->tulip_boardid[10] = '1';
2175	return;
2176    }
2177    if (sc->tulip_rombuf[32] == 0x4A && sc->tulip_rombuf[33] == 0x52) {
2178	id = sc->tulip_rombuf[37] + 256 * sc->tulip_rombuf[36];
2179	if (id == TULIP_ZNYX_ID_ZX312T) {
2180	    sc->tulip_boardid[9] = '2';
2181	    sc->tulip_boardid[10] = 'T';
2182	    sc->tulip_boardid[11] = ' ';
2183	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2184	} else if (id == TULIP_ZNYX_ID_ZX314_INTA) {
2185	    sc->tulip_boardid[9] = '4';
2186	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2187	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2188	} else if (id == TULIP_ZNYX_ID_ZX314) {
2189	    sc->tulip_boardid[9] = '4';
2190	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2191	    sc->tulip_features |= TULIP_HAVE_BASEROM;
2192	} else if (id == TULIP_ZNYX_ID_ZX315_INTA) {
2193	    sc->tulip_boardid[9] = '5';
2194	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2195	} else if (id == TULIP_ZNYX_ID_ZX315) {
2196	    sc->tulip_boardid[9] = '5';
2197	    sc->tulip_features |= TULIP_HAVE_BASEROM;
2198	} else {
2199	    id = 0;
2200	}
2201    }
2202    if (id == 0) {
2203	if ((sc->tulip_enaddr[3] & ~3) == 0xF0 && (sc->tulip_enaddr[5] & 2) == 0) {
2204	    sc->tulip_boardid[9] = '4';
2205	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2206	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2207	} else if ((sc->tulip_enaddr[3] & ~3) == 0xF4 && (sc->tulip_enaddr[5] & 1) == 0) {
2208	    sc->tulip_boardid[9] = '5';
2209	    sc->tulip_boardsw = &tulip_21040_boardsw;
2210	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2211	} else if ((sc->tulip_enaddr[3] & ~3) == 0xEC) {
2212	    sc->tulip_boardid[9] = '2';
2213	    sc->tulip_boardsw = &tulip_21040_boardsw;
2214	}
2215    }
2216}
2217
2218static void
2219tulip_identify_smc_nic(
2220    tulip_softc_t * const sc)
2221{
2222    u_int32_t id1, id2, ei;
2223    int auibnc = 0, utp = 0;
2224    char *cp;
2225
2226    strcpy(sc->tulip_boardid, "SMC ");
2227    if (sc->tulip_chipid == TULIP_21041)
2228	return;
2229    if (sc->tulip_chipid != TULIP_21040) {
2230	if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
2231	    strcpy(&sc->tulip_boardid[4], "9332DST ");
2232	    sc->tulip_boardsw = &tulip_21140_smc9332_boardsw;
2233	} else if (sc->tulip_features & (TULIP_HAVE_BASEROM|TULIP_HAVE_SLAVEDROM)) {
2234	    strcpy(&sc->tulip_boardid[4], "9334BDT ");
2235	} else {
2236	    strcpy(&sc->tulip_boardid[4], "9332BDT ");
2237	}
2238	return;
2239    }
2240    id1 = sc->tulip_rombuf[0x60] | (sc->tulip_rombuf[0x61] << 8);
2241    id2 = sc->tulip_rombuf[0x62] | (sc->tulip_rombuf[0x63] << 8);
2242    ei  = sc->tulip_rombuf[0x66] | (sc->tulip_rombuf[0x67] << 8);
2243
2244    strcpy(&sc->tulip_boardid[4], "8432");
2245    cp = &sc->tulip_boardid[8];
2246    if ((id1 & 1) == 0)
2247	*cp++ = 'B', auibnc = 1;
2248    if ((id1 & 0xFF) > 0x32)
2249	*cp++ = 'T', utp = 1;
2250    if ((id1 & 0x4000) == 0)
2251	*cp++ = 'A', auibnc = 1;
2252    if (id2 == 0x15) {
2253	sc->tulip_boardid[7] = '4';
2254	*cp++ = '-';
2255	*cp++ = 'C';
2256	*cp++ = 'H';
2257	*cp++ = (ei ? '2' : '1');
2258    }
2259    *cp++ = ' ';
2260    *cp = '\0';
2261    if (utp && !auibnc)
2262	sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2263    else if (!utp && auibnc)
2264	sc->tulip_boardsw = &tulip_21040_auibnc_only_boardsw;
2265}
2266
2267static void
2268tulip_identify_cogent_nic(
2269    tulip_softc_t * const sc)
2270{
2271    strcpy(sc->tulip_boardid, "Cogent ");
2272    if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
2273	if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100TX_ID) {
2274	    strcat(sc->tulip_boardid, "EM100TX ");
2275	    sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
2276#if defined(TULIP_COGENT_EM110TX_ID)
2277	} else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM110TX_ID) {
2278	    strcat(sc->tulip_boardid, "EM110TX ");
2279	    sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
2280#endif
2281	} else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
2282	    strcat(sc->tulip_boardid, "EM100FX ");
2283	    sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
2284	}
2285	/*
2286	 * Magic number (0x24001109U) is the SubVendor (0x2400) and
2287	 * SubDevId (0x1109) for the ANA6944TX (EM440TX).
2288	 */
2289	if (*(u_int32_t *) sc->tulip_rombuf == 0x24001109U
2290		&& (sc->tulip_features & TULIP_HAVE_BASEROM)) {
2291	    /*
2292	     * Cogent (Adaptec) is still mapping all INTs to INTA of
2293	     * first 21140.  Dumb!  Dumb!
2294	     */
2295	    strcat(sc->tulip_boardid, "EM440TX ");
2296	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR;
2297	}
2298    } else if (sc->tulip_chipid == TULIP_21040) {
2299	sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2300    }
2301}
2302
2303static void
2304tulip_identify_accton_nic(
2305    tulip_softc_t * const sc)
2306{
2307    strcpy(sc->tulip_boardid, "ACCTON ");
2308    switch (sc->tulip_chipid) {
2309	case TULIP_21140A:
2310	    strcat(sc->tulip_boardid, "EN1207 ");
2311	    if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
2312		sc->tulip_boardsw = &tulip_21140_accton_boardsw;
2313	    break;
2314	case TULIP_21140:
2315	    strcat(sc->tulip_boardid, "EN1207TX ");
2316	    if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
2317		sc->tulip_boardsw = &tulip_21140_eb_boardsw;
2318            break;
2319        case TULIP_21040:
2320	    strcat(sc->tulip_boardid, "EN1203 ");
2321            sc->tulip_boardsw = &tulip_21040_boardsw;
2322            break;
2323        case TULIP_21041:
2324	    strcat(sc->tulip_boardid, "EN1203 ");
2325            sc->tulip_boardsw = &tulip_21041_boardsw;
2326            break;
2327	default:
2328            sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2329            break;
2330    }
2331}
2332
2333static void
2334tulip_identify_asante_nic(
2335    tulip_softc_t * const sc)
2336{
2337    strcpy(sc->tulip_boardid, "Asante ");
2338    if ((sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A)
2339	    && sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
2340	tulip_media_info_t *mi = sc->tulip_mediainfo;
2341	int idx;
2342	/*
2343	 * The Asante Fast Ethernet doesn't always ship with a valid
2344	 * new format SROM.  So if isn't in the new format, we cheat
2345	 * set it up as if we had.
2346	 */
2347
2348	sc->tulip_gpinit = TULIP_GP_ASANTE_PINS;
2349	sc->tulip_gpdata = 0;
2350
2351	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PINS|TULIP_GP_PINSET);
2352	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PHYRESET);
2353	DELAY(100);
2354	TULIP_CSR_WRITE(sc, csr_gp, 0);
2355
2356	mi->mi_type = TULIP_MEDIAINFO_MII;
2357	mi->mi_gpr_length = 0;
2358	mi->mi_gpr_offset = 0;
2359	mi->mi_reset_length = 0;
2360	mi->mi_reset_offset = 0;;
2361
2362	mi->mi_phyaddr = TULIP_MII_NOPHY;
2363	for (idx = 20; idx > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx--) {
2364	    DELAY(10000);
2365	    mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, 0);
2366	}
2367	if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
2368	    printf(TULIP_PRINTF_FMT ": can't find phy 0\n", TULIP_PRINTF_ARGS);
2369	    return;
2370	}
2371
2372	sc->tulip_features |= TULIP_HAVE_MII;
2373	mi->mi_capabilities  = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
2374	mi->mi_advertisement = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
2375	mi->mi_full_duplex   = PHYSTS_10BASET_FD|PHYSTS_100BASETX_FD;
2376	mi->mi_tx_threshold  = PHYSTS_10BASET|PHYSTS_10BASET_FD;
2377	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2378	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2379	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2380	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2381	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2382	mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2383	    tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2384
2385	sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2386    }
2387}
2388
2389static int
2390tulip_srom_decode(
2391    tulip_softc_t * const sc)
2392{
2393    unsigned idx1, idx2, idx3;
2394
2395    const tulip_srom_header_t *shp = (const tulip_srom_header_t *) &sc->tulip_rombuf[0];
2396    const tulip_srom_adapter_info_t *saip = (const tulip_srom_adapter_info_t *) (shp + 1);
2397    tulip_srom_media_t srom_media;
2398    tulip_media_info_t *mi = sc->tulip_mediainfo;
2399    const u_int8_t *dp;
2400    u_int32_t leaf_offset, blocks, data;
2401
2402    for (idx1 = 0; idx1 < shp->sh_adapter_count; idx1++, saip++) {
2403	if (shp->sh_adapter_count == 1)
2404	    break;
2405	if (saip->sai_device == sc->tulip_pci_devno)
2406	    break;
2407    }
2408    /*
2409     * Didn't find the right media block for this card.
2410     */
2411    if (idx1 == shp->sh_adapter_count)
2412	return 0;
2413
2414    /*
2415     * Save the hardware address.
2416     */
2417    bcopy(shp->sh_ieee802_address, sc->tulip_enaddr, 6);
2418    /*
2419     * If this is a multiple port card, add the adapter index to the last
2420     * byte of the hardware address.  (if it isn't multiport, adding 0
2421     * won't hurt.
2422     */
2423    sc->tulip_enaddr[5] += idx1;
2424
2425    leaf_offset = saip->sai_leaf_offset_lowbyte
2426	+ saip->sai_leaf_offset_highbyte * 256;
2427    dp = sc->tulip_rombuf + leaf_offset;
2428
2429    sc->tulip_conntype = (tulip_srom_connection_t) (dp[0] + dp[1] * 256); dp += 2;
2430
2431    for (idx2 = 0;; idx2++) {
2432	if (tulip_srom_conninfo[idx2].sc_type == sc->tulip_conntype
2433	        || tulip_srom_conninfo[idx2].sc_type == TULIP_SROM_CONNTYPE_NOT_USED)
2434	    break;
2435    }
2436    sc->tulip_connidx = idx2;
2437
2438    if (sc->tulip_chipid == TULIP_21041) {
2439	blocks = *dp++;
2440	for (idx2 = 0; idx2 < blocks; idx2++) {
2441	    tulip_media_t media;
2442	    data = *dp++;
2443	    srom_media = (tulip_srom_media_t) (data & 0x3F);
2444	    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2445		if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2446		    break;
2447	    }
2448	    media = tulip_srom_mediums[idx3].sm_type;
2449	    if (media != TULIP_MEDIA_UNKNOWN) {
2450		if (data & TULIP_SROM_21041_EXTENDED) {
2451		    mi->mi_type = TULIP_MEDIAINFO_SIA;
2452		    sc->tulip_mediums[media] = mi;
2453		    mi->mi_sia_connectivity = dp[0] + dp[1] * 256;
2454		    mi->mi_sia_tx_rx        = dp[2] + dp[3] * 256;
2455		    mi->mi_sia_general      = dp[4] + dp[5] * 256;
2456		    mi++;
2457		} else {
2458		    switch (media) {
2459			case TULIP_MEDIA_BNC: {
2460			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC);
2461			    mi++;
2462			    break;
2463			}
2464			case TULIP_MEDIA_AUI: {
2465			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI);
2466			    mi++;
2467			    break;
2468			}
2469			case TULIP_MEDIA_10BASET: {
2470			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET);
2471			    mi++;
2472			    break;
2473			}
2474			case TULIP_MEDIA_10BASET_FD: {
2475			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD);
2476			    mi++;
2477			    break;
2478			}
2479			default: {
2480			    break;
2481			}
2482		    }
2483		}
2484	    }
2485	    if (data & TULIP_SROM_21041_EXTENDED)
2486		dp += 6;
2487	}
2488#ifdef notdef
2489	if (blocks == 0) {
2490	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC); mi++;
2491	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI); mi++;
2492	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET); mi++;
2493	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD); mi++;
2494	}
2495#endif
2496    } else {
2497	unsigned length, type;
2498	tulip_media_t gp_media = TULIP_MEDIA_UNKNOWN;
2499	if (sc->tulip_features & TULIP_HAVE_GPR)
2500	    sc->tulip_gpinit = *dp++;
2501	blocks = *dp++;
2502	for (idx2 = 0; idx2 < blocks; idx2++) {
2503	    const u_int8_t *ep;
2504	    if ((*dp & 0x80) == 0) {
2505		length = 4;
2506		type = 0;
2507	    } else {
2508		length = (*dp++ & 0x7f) - 1;
2509		type = *dp++ & 0x3f;
2510	    }
2511	    ep = dp + length;
2512	    switch (type & 0x3f) {
2513		case 0: {	/* 21140[A] GPR block */
2514		    tulip_media_t media;
2515		    srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
2516		    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2517			if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2518			    break;
2519		    }
2520		    media = tulip_srom_mediums[idx3].sm_type;
2521		    if (media == TULIP_MEDIA_UNKNOWN)
2522			break;
2523		    mi->mi_type = TULIP_MEDIAINFO_GPR;
2524		    sc->tulip_mediums[media] = mi;
2525		    mi->mi_gpdata = dp[1];
2526		    if (media > gp_media && !TULIP_IS_MEDIA_FD(media)) {
2527			sc->tulip_gpdata = mi->mi_gpdata;
2528			gp_media = media;
2529		    }
2530		    data = dp[2] + dp[3] * 256;
2531		    mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
2532		    if (data & TULIP_SROM_2114X_NOINDICATOR) {
2533			mi->mi_actmask = 0;
2534		    } else {
2535#if 0
2536			mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
2537#endif
2538			mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
2539			mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
2540		    }
2541		    mi++;
2542		    break;
2543		}
2544		case 1: {	/* 21140[A] MII block */
2545		    const unsigned phyno = *dp++;
2546		    mi->mi_type = TULIP_MEDIAINFO_MII;
2547		    mi->mi_gpr_length = *dp++;
2548		    mi->mi_gpr_offset = dp - sc->tulip_rombuf;
2549		    dp += mi->mi_gpr_length;
2550		    mi->mi_reset_length = *dp++;
2551		    mi->mi_reset_offset = dp - sc->tulip_rombuf;
2552		    dp += mi->mi_reset_length;
2553
2554		    /*
2555		     * Before we probe for a PHY, use the GPR information
2556		     * to select it.  If we don't, it may be inaccessible.
2557		     */
2558		    TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpinit|TULIP_GP_PINSET);
2559		    for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++) {
2560			DELAY(10);
2561			TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx3]);
2562		    }
2563		    sc->tulip_phyaddr = mi->mi_phyaddr;
2564		    for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++) {
2565			DELAY(10);
2566			TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx3]);
2567		    }
2568
2569		    /*
2570		     * At least write something!
2571		     */
2572		    if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
2573			TULIP_CSR_WRITE(sc, csr_gp, 0);
2574
2575		    mi->mi_phyaddr = TULIP_MII_NOPHY;
2576		    for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
2577			DELAY(10000);
2578			mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
2579		    }
2580		    if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
2581#if defined(TULIP_DEBUG)
2582			printf(TULIP_PRINTF_FMT ": can't find phy %d\n",
2583			       TULIP_PRINTF_ARGS, phyno);
2584#endif
2585			break;
2586		    }
2587		    sc->tulip_features |= TULIP_HAVE_MII;
2588		    mi->mi_capabilities  = dp[0] + dp[1] * 256; dp += 2;
2589		    mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
2590		    mi->mi_full_duplex   = dp[0] + dp[1] * 256; dp += 2;
2591		    mi->mi_tx_threshold  = dp[0] + dp[1] * 256; dp += 2;
2592		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2593		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2594		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2595		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2596		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2597		    mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2598			tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2599		    mi++;
2600		    break;
2601		}
2602		case 2: {	/* 2114[23] SIA block */
2603		    tulip_media_t media;
2604		    srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
2605		    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2606			if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2607			    break;
2608		    }
2609		    media = tulip_srom_mediums[idx3].sm_type;
2610		    if (media == TULIP_MEDIA_UNKNOWN)
2611			break;
2612		    mi->mi_type = TULIP_MEDIAINFO_SIA;
2613		    sc->tulip_mediums[media] = mi;
2614		    if (dp[0] & 0x40) {
2615			mi->mi_sia_connectivity = dp[1] + dp[2] * 256;
2616			mi->mi_sia_tx_rx        = dp[3] + dp[4] * 256;
2617			mi->mi_sia_general      = dp[5] + dp[6] * 256;
2618			dp += 6;
2619		    } else {
2620			switch (media) {
2621			    case TULIP_MEDIA_BNC: {
2622				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, BNC);
2623				break;
2624			    }
2625			    case TULIP_MEDIA_AUI: {
2626				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, AUI);
2627				break;
2628			    }
2629			    case TULIP_MEDIA_10BASET: {
2630				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET);
2631				sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2632				break;
2633			    }
2634			    case TULIP_MEDIA_10BASET_FD: {
2635				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET_FD);
2636				sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2637				break;
2638			    }
2639			    default: {
2640				goto bad_media;
2641			    }
2642			}
2643		    }
2644		    mi->mi_sia_gp_control = (dp[1] + dp[2] * 256) << 16;
2645		    mi->mi_sia_gp_data    = (dp[3] + dp[4] * 256) << 16;
2646		    mi++;
2647		  bad_media:
2648		    break;
2649		}
2650		case 3: {	/* 2114[23] MII PHY block */
2651		    const unsigned phyno = *dp++;
2652		    const u_int8_t *dp0;
2653		    mi->mi_type = TULIP_MEDIAINFO_MII;
2654		    mi->mi_gpr_length = *dp++;
2655		    mi->mi_gpr_offset = dp - sc->tulip_rombuf;
2656		    dp += 2 * mi->mi_gpr_length;
2657		    mi->mi_reset_length = *dp++;
2658		    mi->mi_reset_offset = dp - sc->tulip_rombuf;
2659		    dp += 2 * mi->mi_reset_length;
2660
2661		    dp0 = &sc->tulip_rombuf[mi->mi_reset_offset];
2662		    for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++, dp0 += 2) {
2663			DELAY(10);
2664			TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
2665		    }
2666		    sc->tulip_phyaddr = mi->mi_phyaddr;
2667		    dp0 = &sc->tulip_rombuf[mi->mi_gpr_offset];
2668		    for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++, dp0 += 2) {
2669			DELAY(10);
2670			TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
2671		    }
2672
2673		    if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
2674			TULIP_CSR_WRITE(sc, csr_sia_general, 0);
2675
2676		    mi->mi_phyaddr = TULIP_MII_NOPHY;
2677		    for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
2678			DELAY(10000);
2679			mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
2680		    }
2681		    if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
2682#if defined(TULIP_DEBUG)
2683			printf(TULIP_PRINTF_FMT ": can't find phy %d\n",
2684			       TULIP_PRINTF_ARGS, phyno);
2685#endif
2686			break;
2687		    }
2688		    sc->tulip_features |= TULIP_HAVE_MII;
2689		    mi->mi_capabilities  = dp[0] + dp[1] * 256; dp += 2;
2690		    mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
2691		    mi->mi_full_duplex   = dp[0] + dp[1] * 256; dp += 2;
2692		    mi->mi_tx_threshold  = dp[0] + dp[1] * 256; dp += 2;
2693		    mi->mi_mii_interrupt = dp[0] + dp[1] * 256; dp += 2;
2694		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2695		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2696		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2697		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2698		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2699		    mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2700			tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2701		    mi++;
2702		    break;
2703		}
2704		case 4: {	/* 21143 SYM block */
2705		    tulip_media_t media;
2706		    srom_media = (tulip_srom_media_t) dp[0];
2707		    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2708			if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2709			    break;
2710		    }
2711		    media = tulip_srom_mediums[idx3].sm_type;
2712		    if (media == TULIP_MEDIA_UNKNOWN)
2713			break;
2714		    mi->mi_type = TULIP_MEDIAINFO_SYM;
2715		    sc->tulip_mediums[media] = mi;
2716		    mi->mi_gpcontrol = (dp[1] + dp[2] * 256) << 16;
2717		    mi->mi_gpdata    = (dp[3] + dp[4] * 256) << 16;
2718		    data = dp[5] + dp[6] * 256;
2719		    mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
2720		    if (data & TULIP_SROM_2114X_NOINDICATOR) {
2721			mi->mi_actmask = 0;
2722		    } else {
2723			mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
2724			mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
2725			mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
2726		    }
2727		    if (TULIP_IS_MEDIA_TP(media))
2728			sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2729		    mi++;
2730		    break;
2731		}
2732#if 0
2733		case 5: {	/* 21143 Reset block */
2734		    mi->mi_type = TULIP_MEDIAINFO_RESET;
2735		    mi->mi_reset_length = *dp++;
2736		    mi->mi_reset_offset = dp - sc->tulip_rombuf;
2737		    dp += 2 * mi->mi_reset_length;
2738		    mi++;
2739		    break;
2740		}
2741#endif
2742		default: {
2743		}
2744	    }
2745	    dp = ep;
2746	}
2747    }
2748    return mi - sc->tulip_mediainfo;
2749}
2750
2751static const struct {
2752    void (*vendor_identify_nic)(tulip_softc_t * const sc);
2753    unsigned char vendor_oui[3];
2754} tulip_vendors[] = {
2755    { tulip_identify_dec_nic,		{ 0x08, 0x00, 0x2B } },
2756    { tulip_identify_dec_nic,		{ 0x00, 0x00, 0xF8 } },
2757    { tulip_identify_smc_nic,		{ 0x00, 0x00, 0xC0 } },
2758    { tulip_identify_smc_nic,		{ 0x00, 0xE0, 0x29 } },
2759    { tulip_identify_znyx_nic,		{ 0x00, 0xC0, 0x95 } },
2760    { tulip_identify_cogent_nic,	{ 0x00, 0x00, 0x92 } },
2761    { tulip_identify_asante_nic,	{ 0x00, 0x00, 0x94 } },
2762    { tulip_identify_cogent_nic,	{ 0x00, 0x00, 0xD1 } },
2763    { tulip_identify_accton_nic,	{ 0x00, 0x00, 0xE8 } },
2764    { NULL }
2765};
2766
2767/*
2768 * This deals with the vagaries of the address roms and the
2769 * brain-deadness that various vendors commit in using them.
2770 */
2771static int
2772tulip_read_macaddr(
2773    tulip_softc_t * const sc)
2774{
2775    unsigned cksum, rom_cksum, idx;
2776    u_int32_t csr;
2777    unsigned char tmpbuf[8];
2778    static const u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
2779
2780    sc->tulip_connidx = TULIP_SROM_LASTCONNIDX;
2781
2782    if (sc->tulip_chipid == TULIP_21040) {
2783	TULIP_CSR_WRITE(sc, csr_enetrom, 1);
2784	for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
2785	    int cnt = 0;
2786	    while (((csr = TULIP_CSR_READ(sc, csr_enetrom)) & 0x80000000L) && cnt < 10000)
2787		cnt++;
2788	    sc->tulip_rombuf[idx] = csr & 0xFF;
2789	}
2790	sc->tulip_boardsw = &tulip_21040_boardsw;
2791#if defined(TULIP_EISA)
2792    } else if (sc->tulip_chipid == TULIP_DE425) {
2793	int cnt;
2794	for (idx = 0, cnt = 0; idx < sizeof(testpat) && cnt < 32; cnt++) {
2795	    tmpbuf[idx] = TULIP_CSR_READBYTE(sc, csr_enetrom);
2796	    if (tmpbuf[idx] == testpat[idx])
2797		++idx;
2798	    else
2799		idx = 0;
2800	}
2801	for (idx = 0; idx < 32; idx++)
2802	    sc->tulip_rombuf[idx] = TULIP_CSR_READBYTE(sc, csr_enetrom);
2803	sc->tulip_boardsw = &tulip_21040_boardsw;
2804#endif /* TULIP_EISA */
2805    } else {
2806	if (sc->tulip_chipid == TULIP_21041) {
2807	    /*
2808	     * Thankfully all 21041's act the same.
2809	     */
2810	    sc->tulip_boardsw = &tulip_21041_boardsw;
2811	} else {
2812	    /*
2813	     * Assume all 21140 board are compatible with the
2814	     * DEC 10/100 evaluation board.  Not really valid but
2815	     * it's the best we can do until every one switches to
2816	     * the new SROM format.
2817	     */
2818
2819	    sc->tulip_boardsw = &tulip_21140_eb_boardsw;
2820	}
2821	tulip_srom_read(sc);
2822	if (tulip_srom_crcok(sc->tulip_rombuf)) {
2823	    /*
2824	     * SROM CRC is valid therefore it must be in the
2825	     * new format.
2826	     */
2827	    sc->tulip_features |= TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM;
2828	} else if (sc->tulip_rombuf[126] == 0xff && sc->tulip_rombuf[127] == 0xFF) {
2829	    /*
2830	     * No checksum is present.  See if the SROM id checks out;
2831	     * the first 18 bytes should be 0 followed by a 1 followed
2832	     * by the number of adapters (which we don't deal with yet).
2833	     */
2834	    for (idx = 0; idx < 18; idx++) {
2835		if (sc->tulip_rombuf[idx] != 0)
2836		    break;
2837	    }
2838	    if (idx == 18 && sc->tulip_rombuf[18] == 1 && sc->tulip_rombuf[19] != 0)
2839		sc->tulip_features |= TULIP_HAVE_ISVSROM;
2840	} else if (sc->tulip_chipid >= TULIP_21142) {
2841	    sc->tulip_features |= TULIP_HAVE_ISVSROM;
2842	    sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2843	}
2844	if ((sc->tulip_features & TULIP_HAVE_ISVSROM) && tulip_srom_decode(sc)) {
2845	    if (sc->tulip_chipid != TULIP_21041)
2846		sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2847
2848	    /*
2849	     * If the SROM specifies more than one adapter, tag this as a
2850	     * BASE rom.
2851	     */
2852	    if (sc->tulip_rombuf[19] > 1)
2853		sc->tulip_features |= TULIP_HAVE_BASEROM;
2854	    if (sc->tulip_boardsw == NULL)
2855		return -6;
2856	    goto check_oui;
2857	}
2858    }
2859
2860
2861    if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) {
2862	/*
2863	 * Some folks don't use the standard ethernet rom format
2864	 * but instead just put the address in the first 6 bytes
2865	 * of the rom and let the rest be all 0xffs.  (Can we say
2866	 * ZNYX?) (well sometimes they put in a checksum so we'll
2867	 * start at 8).
2868	 */
2869	for (idx = 8; idx < 32; idx++) {
2870	    if (sc->tulip_rombuf[idx] != 0xFF)
2871		return -4;
2872	}
2873	/*
2874	 * Make sure the address is not multicast or locally assigned
2875	 * that the OUI is not 00-00-00.
2876	 */
2877	if ((sc->tulip_rombuf[0] & 3) != 0)
2878	    return -4;
2879	if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0
2880		&& sc->tulip_rombuf[2] == 0)
2881	    return -4;
2882	bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
2883	sc->tulip_features |= TULIP_HAVE_OKROM;
2884	goto check_oui;
2885    } else {
2886	/*
2887	 * A number of makers of multiport boards (ZNYX and Cogent)
2888	 * only put on one address ROM on their 21040 boards.  So
2889	 * if the ROM is all zeros (or all 0xFFs), look at the
2890	 * previous configured boards (as long as they are on the same
2891	 * PCI bus and the bus number is non-zero) until we find the
2892	 * master board with address ROM.  We then use its address ROM
2893	 * as the base for this board.  (we add our relative board
2894	 * to the last byte of its address).
2895	 */
2896	for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
2897	    if (sc->tulip_rombuf[idx] != 0 && sc->tulip_rombuf[idx] != 0xFF)
2898		break;
2899	}
2900	if (idx == sizeof(sc->tulip_rombuf)) {
2901	    int root_unit;
2902	    tulip_softc_t *root_sc = NULL;
2903	    for (root_unit = sc->tulip_unit - 1; root_unit >= 0; root_unit--) {
2904		root_sc = TULIP_UNIT_TO_SOFTC(root_unit);
2905		if (root_sc == NULL || (root_sc->tulip_features & (TULIP_HAVE_OKROM|TULIP_HAVE_SLAVEDROM)) == TULIP_HAVE_OKROM)
2906		    break;
2907		root_sc = NULL;
2908	    }
2909	    if (root_sc != NULL && (root_sc->tulip_features & TULIP_HAVE_BASEROM)
2910		    && root_sc->tulip_chipid == sc->tulip_chipid
2911		    && root_sc->tulip_pci_busno == sc->tulip_pci_busno) {
2912		sc->tulip_features |= TULIP_HAVE_SLAVEDROM;
2913		sc->tulip_boardsw = root_sc->tulip_boardsw;
2914		strcpy(sc->tulip_boardid, root_sc->tulip_boardid);
2915		if (sc->tulip_boardsw->bd_type == TULIP_21140_ISV) {
2916		    bcopy(root_sc->tulip_rombuf, sc->tulip_rombuf,
2917			  sizeof(sc->tulip_rombuf));
2918		    if (!tulip_srom_decode(sc))
2919			return -5;
2920		} else {
2921		    bcopy(root_sc->tulip_enaddr, sc->tulip_enaddr, 6);
2922		    sc->tulip_enaddr[5] += sc->tulip_unit - root_sc->tulip_unit;
2923		}
2924		/*
2925		 * Now for a truly disgusting kludge: all 4 21040s on
2926		 * the ZX314 share the same INTA line so the mapping
2927		 * setup by the BIOS on the PCI bridge is worthless.
2928		 * Rather than reprogramming the value in the config
2929		 * register, we will handle this internally.
2930		 */
2931		if (root_sc->tulip_features & TULIP_HAVE_SHAREDINTR) {
2932		    sc->tulip_slaves = root_sc->tulip_slaves;
2933		    root_sc->tulip_slaves = sc;
2934		    sc->tulip_features |= TULIP_HAVE_SLAVEDINTR;
2935		}
2936		return 0;
2937	    }
2938	}
2939    }
2940
2941    /*
2942     * This is the standard DEC address ROM test.
2943     */
2944
2945    if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0)
2946	return -3;
2947
2948    tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14];
2949    tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12];
2950    tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10];
2951    tmpbuf[6] = sc->tulip_rombuf[9];  tmpbuf[7] = sc->tulip_rombuf[8];
2952    if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0)
2953	return -2;
2954
2955    bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
2956
2957    cksum = *(u_int16_t *) &sc->tulip_enaddr[0];
2958    cksum *= 2;
2959    if (cksum > 65535) cksum -= 65535;
2960    cksum += *(u_int16_t *) &sc->tulip_enaddr[2];
2961    if (cksum > 65535) cksum -= 65535;
2962    cksum *= 2;
2963    if (cksum > 65535) cksum -= 65535;
2964    cksum += *(u_int16_t *) &sc->tulip_enaddr[4];
2965    if (cksum >= 65535) cksum -= 65535;
2966
2967    rom_cksum = *(u_int16_t *) &sc->tulip_rombuf[6];
2968
2969    if (cksum != rom_cksum)
2970	return -1;
2971
2972  check_oui:
2973    /*
2974     * Check for various boards based on OUI.  Did I say braindead?
2975     */
2976    for (idx = 0; tulip_vendors[idx].vendor_identify_nic != NULL; idx++) {
2977	if (bcmp(sc->tulip_enaddr, tulip_vendors[idx].vendor_oui, 3) == 0) {
2978	    (*tulip_vendors[idx].vendor_identify_nic)(sc);
2979	    break;
2980	}
2981    }
2982
2983    sc->tulip_features |= TULIP_HAVE_OKROM;
2984    return 0;
2985}
2986
2987#if defined(IFM_ETHER)
2988static void
2989tulip_ifmedia_add(
2990    tulip_softc_t * const sc)
2991{
2992    tulip_media_t media;
2993    int medias = 0;
2994
2995    for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
2996	if (sc->tulip_mediums[media] != NULL) {
2997	    ifmedia_add(&sc->tulip_ifmedia, tulip_media_to_ifmedia[media],
2998			0, 0);
2999	    medias++;
3000	}
3001    }
3002    if (medias == 0) {
3003	sc->tulip_features |= TULIP_HAVE_NOMEDIA;
3004	ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE, 0, 0);
3005	ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE);
3006    } else if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
3007	ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO, 0, 0);
3008	ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO);
3009    } else {
3010	ifmedia_set(&sc->tulip_ifmedia, tulip_media_to_ifmedia[sc->tulip_media]);
3011	sc->tulip_flags |= TULIP_PRINTMEDIA;
3012	tulip_linkup(sc, sc->tulip_media);
3013    }
3014}
3015
3016static int
3017tulip_ifmedia_change(
3018    struct ifnet * const ifp)
3019{
3020    tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
3021
3022    sc->tulip_flags |= TULIP_NEEDRESET;
3023    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
3024    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
3025    if (IFM_SUBTYPE(sc->tulip_ifmedia.ifm_media) != IFM_AUTO) {
3026	tulip_media_t media;
3027	for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
3028	    if (sc->tulip_mediums[media] != NULL
3029		&& sc->tulip_ifmedia.ifm_media == tulip_media_to_ifmedia[media]) {
3030		sc->tulip_flags |= TULIP_PRINTMEDIA;
3031		sc->tulip_flags &= ~TULIP_DIDNWAY;
3032		tulip_linkup(sc, media);
3033		return 0;
3034	    }
3035	}
3036    }
3037    sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_WANTRXACT);
3038    tulip_reset(sc);
3039    tulip_init(sc);
3040    return 0;
3041}
3042
3043/*
3044 * Media status callback
3045 */
3046static void
3047tulip_ifmedia_status(
3048    struct ifnet * const ifp,
3049    struct ifmediareq *req)
3050{
3051    tulip_softc_t *sc = TULIP_IFP_TO_SOFTC(ifp);
3052
3053#if defined(__bsdi__)
3054    if (sc->tulip_mii.mii_instance != 0) {
3055	mii_pollstat(&sc->tulip_mii);
3056	req->ifm_active = sc->tulip_mii.mii_media_active;
3057	req->ifm_status = sc->tulip_mii.mii_media_status;
3058	return;
3059    }
3060#endif
3061    if (sc->tulip_media == TULIP_MEDIA_UNKNOWN)
3062	return;
3063
3064    req->ifm_status = IFM_AVALID;
3065    if (sc->tulip_flags & TULIP_LINKUP)
3066	req->ifm_status |= IFM_ACTIVE;
3067
3068    req->ifm_active = tulip_media_to_ifmedia[sc->tulip_media];
3069}
3070#endif
3071
3072static void
3073tulip_addr_filter(
3074    tulip_softc_t * const sc)
3075{
3076#if defined(__FreeBSD__) && __FreeBSD_version >= 300000
3077    struct ifmultiaddr *ifma;
3078    u_char *addrp;
3079#else
3080    struct ether_multistep step;
3081    struct ether_multi *enm;
3082#endif
3083    int multicnt;
3084
3085    sc->tulip_flags &= ~(TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY|TULIP_ALLMULTI);
3086    sc->tulip_flags |= TULIP_WANTSETUP|TULIP_WANTTXSTART;
3087    sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
3088    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
3089#if defined(IFF_ALLMULTI)
3090    if (sc->tulip_if.if_flags & IFF_ALLMULTI)
3091	sc->tulip_flags |= TULIP_ALLMULTI ;
3092#endif
3093
3094#if defined(__FreeBSD__) && __FreeBSD_version >= 300000
3095    multicnt = 0;
3096    for (ifma = sc->tulip_if.if_multiaddrs.lh_first; ifma != NULL;
3097	 ifma = ifma->ifma_link.le_next) {
3098
3099	    if (ifma->ifma_addr->sa_family == AF_LINK)
3100		multicnt++;
3101    }
3102#else
3103    multicnt = sc->tulip_multicnt;
3104#endif
3105
3106    sc->tulip_if.if_start = tulip_ifstart;	/* so the setup packet gets queued */
3107    if (multicnt > 14) {
3108	u_int32_t *sp = sc->tulip_setupdata;
3109	unsigned hash;
3110	/*
3111	 * Some early passes of the 21140 have broken implementations of
3112	 * hash-perfect mode.  When we get too many multicasts for perfect
3113	 * filtering with these chips, we need to switch into hash-only
3114	 * mode (this is better than all-multicast on network with lots
3115	 * of multicast traffic).
3116	 */
3117	if (sc->tulip_features & TULIP_HAVE_BROKEN_HASH)
3118	    sc->tulip_flags |= TULIP_WANTHASHONLY;
3119	else
3120	    sc->tulip_flags |= TULIP_WANTHASHPERFECT;
3121	/*
3122	 * If we have more than 14 multicasts, we have
3123	 * go into hash perfect mode (512 bit multicast
3124	 * hash and one perfect hardware).
3125	 */
3126	bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata));
3127
3128#if defined(__FreeBSD__) && __FreeBSD_version >= 300000
3129	for (ifma = sc->tulip_if.if_multiaddrs.lh_first; ifma != NULL;
3130	     ifma = ifma->ifma_link.le_next) {
3131
3132		if (ifma->ifma_addr->sa_family != AF_LINK)
3133			continue;
3134
3135		hash = tulip_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
3136		sp[hash >> 4] |= 1 << (hash & 0xF);
3137	}
3138#else
3139	ETHER_FIRST_MULTI(step, TULIP_ETHERCOM(sc), enm);
3140	while (enm != NULL) {
3141		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) {
3142		    hash = tulip_mchash(enm->enm_addrlo);
3143		    sp[hash >> 4] |= 1 << (hash & 0xF);
3144		} else {
3145		    sc->tulip_flags |= TULIP_ALLMULTI;
3146		    sc->tulip_flags &= ~(TULIP_WANTHASHONLY|TULIP_WANTHASHPERFECT);
3147		    break;
3148		}
3149		ETHER_NEXT_MULTI(step, enm);
3150	}
3151#endif
3152	/*
3153	 * No reason to use a hash if we are going to be
3154	 * receiving every multicast.
3155	 */
3156	if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
3157	    hash = tulip_mchash(etherbroadcastaddr);
3158	    sp[hash >> 4] |= 1 << (hash & 0xF);
3159	    if (sc->tulip_flags & TULIP_WANTHASHONLY) {
3160		hash = tulip_mchash(sc->tulip_enaddr);
3161		sp[hash >> 4] |= 1 << (hash & 0xF);
3162	    } else {
3163		sp[39] = ((u_int16_t *) sc->tulip_enaddr)[0];
3164		sp[40] = ((u_int16_t *) sc->tulip_enaddr)[1];
3165		sp[41] = ((u_int16_t *) sc->tulip_enaddr)[2];
3166	    }
3167	}
3168    }
3169    if ((sc->tulip_flags & (TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY)) == 0) {
3170	u_int32_t *sp = sc->tulip_setupdata;
3171	int idx = 0;
3172	if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
3173	    /*
3174	     * Else can get perfect filtering for 16 addresses.
3175	     */
3176#if defined(__FreeBSD__) && __FreeBSD_version >= 300000
3177	    for (ifma = sc->tulip_if.if_multiaddrs.lh_first; ifma != NULL;
3178		 ifma = ifma->ifma_link.le_next) {
3179		    if (ifma->ifma_addr->sa_family != AF_LINK)
3180			    continue;
3181		    addrp = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
3182		    *sp++ = ((u_int16_t *) addrp)[0];
3183		    *sp++ = ((u_int16_t *) addrp)[1];
3184		    *sp++ = ((u_int16_t *) addrp)[2];
3185		    idx++;
3186	    }
3187#else
3188	    ETHER_FIRST_MULTI(step, TULIP_ETHERCOM(sc), enm);
3189	    for (; enm != NULL; idx++) {
3190		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) {
3191		    *sp++ = ((u_int16_t *) enm->enm_addrlo)[0];
3192		    *sp++ = ((u_int16_t *) enm->enm_addrlo)[1];
3193		    *sp++ = ((u_int16_t *) enm->enm_addrlo)[2];
3194		} else {
3195		    sc->tulip_flags |= TULIP_ALLMULTI;
3196		    break;
3197		}
3198		ETHER_NEXT_MULTI(step, enm);
3199	    }
3200#endif
3201	    /*
3202	     * Add the broadcast address.
3203	     */
3204	    idx++;
3205	    *sp++ = 0xFFFF;
3206	    *sp++ = 0xFFFF;
3207	    *sp++ = 0xFFFF;
3208	}
3209	/*
3210	 * Pad the rest with our hardware address
3211	 */
3212	for (; idx < 16; idx++) {
3213	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[0];
3214	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[1];
3215	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[2];
3216	}
3217    }
3218#if defined(IFF_ALLMULTI)
3219    if (sc->tulip_flags & TULIP_ALLMULTI)
3220	sc->tulip_if.if_flags |= IFF_ALLMULTI;
3221#endif
3222}
3223
3224static void
3225tulip_reset(
3226    tulip_softc_t * const sc)
3227{
3228    tulip_ringinfo_t *ri;
3229    tulip_desc_t *di;
3230    u_int32_t inreset = (sc->tulip_flags & TULIP_INRESET);
3231
3232    /*
3233     * Brilliant.  Simply brilliant.  When switching modes/speeds
3234     * on a 2114*, you need to set the appriopriate MII/PCS/SCL/PS
3235     * bits in CSR6 and then do a software reset to get the 21140
3236     * to properly reset its internal pathways to the right places.
3237     *   Grrrr.
3238     */
3239    if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0
3240	    && sc->tulip_boardsw->bd_media_preset != NULL)
3241	(*sc->tulip_boardsw->bd_media_preset)(sc);
3242
3243    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
3244    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
3245		   33MHz that comes to two microseconds but wait a
3246		   bit longer anyways) */
3247
3248    if (!inreset) {
3249	sc->tulip_flags |= TULIP_INRESET;
3250	sc->tulip_flags &= ~(TULIP_NEEDRESET|TULIP_RXBUFSLOW);
3251	sc->tulip_if.if_flags &= ~IFF_OACTIVE;
3252	sc->tulip_if.if_start = tulip_ifstart;
3253    }
3254
3255#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
3256    TULIP_CSR_WRITE(sc, csr_txlist, sc->tulip_txdescmap->dm_segs[0].ds_addr);
3257#else
3258    TULIP_CSR_WRITE(sc, csr_txlist, TULIP_KVATOPHYS(sc, &sc->tulip_txinfo.ri_first[0]));
3259#endif
3260#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3261    TULIP_CSR_WRITE(sc, csr_rxlist, sc->tulip_rxdescmap->dm_segs[0].ds_addr);
3262#else
3263    TULIP_CSR_WRITE(sc, csr_rxlist, TULIP_KVATOPHYS(sc, &sc->tulip_rxinfo.ri_first[0]));
3264#endif
3265    TULIP_CSR_WRITE(sc, csr_busmode,
3266		    (1 << (TULIP_BURSTSIZE(sc->tulip_unit) + 8))
3267		    |TULIP_BUSMODE_CACHE_ALIGN8
3268		    |TULIP_BUSMODE_READMULTIPLE
3269		    |(BYTE_ORDER != LITTLE_ENDIAN ? TULIP_BUSMODE_BIGENDIAN : 0));
3270
3271    sc->tulip_txtimer = 0;
3272    sc->tulip_txq.ifq_maxlen = TULIP_TXDESCS;
3273    /*
3274     * Free all the mbufs that were on the transmit ring.
3275     */
3276    for (;;) {
3277#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
3278	bus_dmamap_t map;
3279#endif
3280	struct mbuf *m;
3281	IF_DEQUEUE(&sc->tulip_txq, m);
3282	if (m == NULL)
3283	    break;
3284#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
3285	map = M_GETCTX(m, bus_dmamap_t);
3286	bus_dmamap_unload(sc->tulip_dmatag, map);
3287	sc->tulip_txmaps[sc->tulip_txmaps_free++] = map;
3288#endif
3289	m_freem(m);
3290    }
3291
3292    ri = &sc->tulip_txinfo;
3293    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
3294    ri->ri_free = ri->ri_max;
3295    for (di = ri->ri_first; di < ri->ri_last; di++)
3296	di->d_status = 0;
3297#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
3298    bus_dmamap_sync(sc->tulip_dmatag, sc->tulip_txdescmap,
3299		    0, sc->tulip_txdescmap->dm_mapsize,
3300		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3301#endif
3302
3303    /*
3304     * We need to collect all the mbufs were on the
3305     * receive ring before we reinit it either to put
3306     * them back on or to know if we have to allocate
3307     * more.
3308     */
3309    ri = &sc->tulip_rxinfo;
3310    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
3311    ri->ri_free = ri->ri_max;
3312    for (di = ri->ri_first; di < ri->ri_last; di++) {
3313	di->d_status = 0;
3314	di->d_length1 = 0; di->d_addr1 = 0;
3315	di->d_length2 = 0; di->d_addr2 = 0;
3316    }
3317#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3318    bus_dmamap_sync(sc->tulip_dmatag, sc->tulip_rxdescmap,
3319		    0, sc->tulip_rxdescmap->dm_mapsize,
3320		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3321#endif
3322    for (;;) {
3323#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3324	bus_dmamap_t map;
3325#endif
3326	struct mbuf *m;
3327	IF_DEQUEUE(&sc->tulip_rxq, m);
3328	if (m == NULL)
3329	    break;
3330#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3331	map = M_GETCTX(m, bus_dmamap_t);
3332	bus_dmamap_unload(sc->tulip_dmatag, map);
3333	sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
3334#endif
3335	m_freem(m);
3336    }
3337
3338    /*
3339     * If tulip_reset is being called recurisvely, exit quickly knowing
3340     * that when the outer tulip_reset returns all the right stuff will
3341     * have happened.
3342     */
3343    if (inreset)
3344	return;
3345
3346    sc->tulip_intrmask |= TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
3347	|TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
3348	|TULIP_STS_TXUNDERFLOW|TULIP_STS_TXBABBLE
3349	|TULIP_STS_RXSTOPPED;
3350
3351    if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0)
3352	(*sc->tulip_boardsw->bd_media_select)(sc);
3353#if defined(TULIP_DEBUG)
3354    if ((sc->tulip_flags & TULIP_NEEDRESET) == TULIP_NEEDRESET)
3355	printf(TULIP_PRINTF_FMT ": tulip_reset: additional reset needed?!?\n",
3356	       TULIP_PRINTF_ARGS);
3357#endif
3358    tulip_media_print(sc);
3359    if (sc->tulip_features & TULIP_HAVE_DUALSENSE)
3360	TULIP_CSR_WRITE(sc, csr_sia_status, TULIP_CSR_READ(sc, csr_sia_status));
3361
3362    sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP|TULIP_INRESET
3363			 |TULIP_RXACT);
3364    tulip_addr_filter(sc);
3365}
3366
3367static void
3368tulip_init(
3369    tulip_softc_t * const sc)
3370{
3371    if (sc->tulip_if.if_flags & IFF_UP) {
3372	if ((sc->tulip_if.if_flags & IFF_RUNNING) == 0) {
3373	    /* initialize the media */
3374	    tulip_reset(sc);
3375	}
3376	sc->tulip_if.if_flags |= IFF_RUNNING;
3377	if (sc->tulip_if.if_flags & IFF_PROMISC) {
3378	    sc->tulip_flags |= TULIP_PROMISC;
3379	    sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
3380	    sc->tulip_intrmask |= TULIP_STS_TXINTR;
3381	} else {
3382	    sc->tulip_flags &= ~TULIP_PROMISC;
3383	    sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
3384	    if (sc->tulip_flags & TULIP_ALLMULTI) {
3385		sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
3386	    } else {
3387		sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
3388	    }
3389	}
3390	sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
3391	if ((sc->tulip_flags & (TULIP_TXPROBE_ACTIVE|TULIP_WANTSETUP)) == 0) {
3392	    tulip_rx_intr(sc);
3393	    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
3394	    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
3395	} else {
3396	    sc->tulip_if.if_flags |= IFF_OACTIVE;
3397	    sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
3398	    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
3399	}
3400	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3401	TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3402	if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
3403	    tulip_txput_setup(sc);
3404    } else {
3405	sc->tulip_if.if_flags &= ~IFF_RUNNING;
3406	tulip_reset(sc);
3407    }
3408}
3409
3410static void
3411tulip_rx_intr(
3412    tulip_softc_t * const sc)
3413{
3414    TULIP_PERFSTART(rxintr)
3415    tulip_ringinfo_t * const ri = &sc->tulip_rxinfo;
3416    struct ifnet * const ifp = &sc->tulip_if;
3417    int fillok = 1;
3418#if defined(TULIP_DEBUG)
3419    int cnt = 0;
3420#endif
3421
3422    for (;;) {
3423	TULIP_PERFSTART(rxget)
3424	struct ether_header eh;
3425	tulip_desc_t *eop = ri->ri_nextin;
3426	int total_len = 0, last_offset = 0;
3427	struct mbuf *ms = NULL, *me = NULL;
3428	int accept = 0;
3429#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3430	bus_dmamap_t map;
3431	int error;
3432#endif
3433
3434	if (fillok && sc->tulip_rxq.ifq_len < TULIP_RXQ_TARGET)
3435	    goto queue_mbuf;
3436
3437#if defined(TULIP_DEBUG)
3438	if (cnt == ri->ri_max)
3439	    break;
3440#endif
3441	/*
3442	 * If the TULIP has no descriptors, there can't be any receive
3443	 * descriptors to process.
3444 	 */
3445	if (eop == ri->ri_nextout)
3446	    break;
3447
3448	/*
3449	 * 90% of the packets will fit in one descriptor.  So we optimize
3450	 * for that case.
3451	 */
3452	TULIP_RXDESC_POSTSYNC(sc, eop, sizeof(*eop));
3453	if ((((volatile tulip_desc_t *) eop)->d_status & (TULIP_DSTS_OWNER|TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) == (TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) {
3454	    IF_DEQUEUE(&sc->tulip_rxq, ms);
3455	    me = ms;
3456	} else {
3457	    /*
3458	     * If still owned by the TULIP, don't touch it.
3459	     */
3460	    if (((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER)
3461		break;
3462
3463	    /*
3464	     * It is possible (though improbable unless the BIG_PACKET support
3465	     * is enabled or MCLBYTES < 1518) for a received packet to cross
3466	     * more than one receive descriptor.
3467	     */
3468	    while ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_RxLASTDESC) == 0) {
3469		if (++eop == ri->ri_last)
3470		    eop = ri->ri_first;
3471		TULIP_RXDESC_POSTSYNC(sc, eop, sizeof(*eop));
3472		if (eop == ri->ri_nextout || ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER))) {
3473#if defined(TULIP_DEBUG)
3474		    sc->tulip_dbg.dbg_rxintrs++;
3475		    sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
3476#endif
3477		    TULIP_PERFEND(rxget);
3478		    TULIP_PERFEND(rxintr);
3479		    return;
3480		}
3481		total_len++;
3482	    }
3483
3484	    /*
3485	     * Dequeue the first buffer for the start of the packet.  Hopefully
3486	     * this will be the only one we need to dequeue.  However, if the
3487	     * packet consumed multiple descriptors, then we need to dequeue
3488	     * those buffers and chain to the starting mbuf.  All buffers but
3489	     * the last buffer have the same length so we can set that now.
3490	     * (we add to last_offset instead of multiplying since we normally
3491	     * won't go into the loop and thereby saving a ourselves from
3492	     * doing a multiplication by 0 in the normal case).
3493	     */
3494	    IF_DEQUEUE(&sc->tulip_rxq, ms);
3495	    for (me = ms; total_len > 0; total_len--) {
3496#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3497		map = M_GETCTX(me, bus_dmamap_t);
3498		TULIP_RXMAP_POSTSYNC(sc, map);
3499		bus_dmamap_unload(sc->tulip_dmatag, map);
3500		sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
3501#if defined(DIAGNOSTIC)
3502		M_SETCTX(me, NULL);
3503#endif
3504#endif /* TULIP_BUS_DMA */
3505		me->m_len = TULIP_RX_BUFLEN;
3506		last_offset += TULIP_RX_BUFLEN;
3507		IF_DEQUEUE(&sc->tulip_rxq, me->m_next);
3508		me = me->m_next;
3509	    }
3510	}
3511
3512	/*
3513	 *  Now get the size of received packet (minus the CRC).
3514	 */
3515	total_len = ((eop->d_status >> 16) & 0x7FFF) - 4;
3516	if ((sc->tulip_flags & TULIP_RXIGNORE) == 0
3517		&& ((eop->d_status & TULIP_DSTS_ERRSUM) == 0
3518#ifdef BIG_PACKET
3519		     || (total_len <= sc->tulip_if.if_mtu + sizeof(struct ether_header) &&
3520			 (eop->d_status & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxRUNT|
3521					  TULIP_DSTS_RxCOLLSEEN|TULIP_DSTS_RxBADCRC|
3522					  TULIP_DSTS_RxOVERFLOW)) == 0)
3523#endif
3524		)) {
3525	    me->m_len = total_len - last_offset;
3526
3527#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3528	    map = M_GETCTX(me, bus_dmamap_t);
3529	    bus_dmamap_sync(sc->tulip_dmatag, map, 0, me->m_len,
3530			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
3531	    bus_dmamap_unload(sc->tulip_dmatag, map);
3532	    sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
3533#if defined(DIAGNOSTIC)
3534	    M_SETCTX(me, NULL);
3535#endif
3536#endif /* TULIP_BUS_DMA */
3537
3538	    eh = *mtod(ms, struct ether_header *);
3539#if NBPFILTER > 0
3540	    if (sc->tulip_bpf != NULL) {
3541		if (me == ms)
3542		    TULIP_BPF_TAP(sc, mtod(ms, caddr_t), total_len);
3543		else
3544		    TULIP_BPF_MTAP(sc, ms);
3545	    }
3546#endif
3547	    sc->tulip_flags |= TULIP_RXACT;
3548
3549#ifdef BRIDGE /* see code in if_ed.c */
3550            ms->m_pkthdr.rcvif = ifp; /* XXX */
3551            ms->m_pkthdr.len = total_len; /* XXX */
3552            if (do_bridge) {
3553                struct ifnet *bdg_ifp ;
3554                bdg_ifp = bridge_in(ms);
3555                if (bdg_ifp == BDG_DROP)
3556                    goto next ; /* and drop */
3557                if (bdg_ifp != BDG_LOCAL)
3558                    bdg_forward(&ms, bdg_ifp);
3559                if (bdg_ifp != BDG_LOCAL && bdg_ifp != BDG_BCAST &&
3560                        bdg_ifp != BDG_MCAST)
3561                    goto next ; /* and drop */
3562                /* all others accepted locally */
3563            } else
3564#endif
3565	    if ((sc->tulip_flags & (TULIP_PROMISC|TULIP_HASHONLY))
3566		    && (eh.ether_dhost[0] & 1) == 0
3567		    && !TULIP_ADDREQUAL(eh.ether_dhost, sc->tulip_enaddr))
3568		    goto next;
3569	    accept = 1;
3570	    total_len -= sizeof(struct ether_header);
3571	} else {
3572	    ifp->if_ierrors++;
3573	    if (eop->d_status & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxOVERFLOW|TULIP_DSTS_RxWATCHDOG)) {
3574		sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
3575	    } else {
3576#if defined(TULIP_VERBOSE)
3577		const char *error = NULL;
3578#endif
3579		if (eop->d_status & TULIP_DSTS_RxTOOLONG) {
3580		    sc->tulip_dot3stats.dot3StatsFrameTooLongs++;
3581#if defined(TULIP_VERBOSE)
3582		    error = "frame too long";
3583#endif
3584		}
3585		if (eop->d_status & TULIP_DSTS_RxBADCRC) {
3586		    if (eop->d_status & TULIP_DSTS_RxDRBBLBIT) {
3587			sc->tulip_dot3stats.dot3StatsAlignmentErrors++;
3588#if defined(TULIP_VERBOSE)
3589			error = "alignment error";
3590#endif
3591		    } else {
3592			sc->tulip_dot3stats.dot3StatsFCSErrors++;
3593#if defined(TULIP_VERBOSE)
3594			error = "bad crc";
3595#endif
3596		    }
3597		}
3598#if defined(TULIP_VERBOSE)
3599		if (error != NULL && (sc->tulip_flags & TULIP_NOMESSAGES) == 0) {
3600		    printf(TULIP_PRINTF_FMT ": receive: " TULIP_EADDR_FMT ": %s\n",
3601			   TULIP_PRINTF_ARGS,
3602			   TULIP_EADDR_ARGS(mtod(ms, u_char *) + 6),
3603			   error);
3604		    sc->tulip_flags |= TULIP_NOMESSAGES;
3605		}
3606#endif
3607	    }
3608
3609#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3610	    map = M_GETCTX(me, bus_dmamap_t);
3611	    bus_dmamap_unload(sc->tulip_dmatag, map);
3612	    sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
3613#if defined(DIAGNOSTIC)
3614	    M_SETCTX(me, NULL);
3615#endif
3616#endif /* TULIP_BUS_DMA */
3617	}
3618      next:
3619#if defined(TULIP_DEBUG)
3620	cnt++;
3621#endif
3622	ifp->if_ipackets++;
3623	if (++eop == ri->ri_last)
3624	    eop = ri->ri_first;
3625	ri->ri_nextin = eop;
3626      queue_mbuf:
3627	/*
3628	 * Either we are priming the TULIP with mbufs (m == NULL)
3629	 * or we are about to accept an mbuf for the upper layers
3630	 * so we need to allocate an mbuf to replace it.  If we
3631	 * can't replace it, send up it anyways.  This may cause
3632	 * us to drop packets in the future but that's better than
3633	 * being caught in livelock.
3634	 *
3635	 * Note that if this packet crossed multiple descriptors
3636	 * we don't even try to reallocate all the mbufs here.
3637	 * Instead we rely on the test of the beginning of
3638	 * the loop to refill for the extra consumed mbufs.
3639	 */
3640	if (accept || ms == NULL) {
3641	    struct mbuf *m0;
3642	    MGETHDR(m0, M_DONTWAIT, MT_DATA);
3643	    if (m0 != NULL) {
3644#if defined(TULIP_COPY_RXDATA)
3645		if (!accept || total_len >= MHLEN) {
3646#endif
3647		    MCLGET(m0, M_DONTWAIT);
3648		    if ((m0->m_flags & M_EXT) == 0) {
3649			m_freem(m0);
3650			m0 = NULL;
3651		    }
3652#if defined(TULIP_COPY_RXDATA)
3653		}
3654#endif
3655	    }
3656	    if (accept
3657#if defined(TULIP_COPY_RXDATA)
3658		&& m0 != NULL
3659#endif
3660		) {
3661#if defined(__bsdi__)
3662		eh.ether_type = ntohs(eh.ether_type);
3663#endif
3664#if !defined(TULIP_COPY_RXDATA)
3665		ms->m_data += sizeof(struct ether_header);
3666		ms->m_len -= sizeof(struct ether_header);
3667		ms->m_pkthdr.len = total_len;
3668		ms->m_pkthdr.rcvif = ifp;
3669		ether_input(ifp, &eh, ms);
3670#else
3671#ifdef BIG_PACKET
3672#error BIG_PACKET is incompatible with TULIP_COPY_RXDATA
3673#endif
3674		if (ms == me)
3675		    bcopy(mtod(ms, caddr_t) + sizeof(struct ether_header),
3676			  mtod(m0, caddr_t), total_len);
3677		else
3678		    m_copydata(ms, 0, total_len, mtod(m0, caddr_t));
3679		m0->m_len = m0->m_pkthdr.len = total_len;
3680		m0->m_pkthdr.rcvif = ifp;
3681		ether_input(ifp, &eh, m0);
3682		m0 = ms;
3683#endif
3684	    }
3685	    ms = m0;
3686	}
3687	if (ms == NULL) {
3688	    /*
3689	     * Couldn't allocate a new buffer.  Don't bother
3690	     * trying to replenish the receive queue.
3691	     */
3692	    fillok = 0;
3693	    sc->tulip_flags |= TULIP_RXBUFSLOW;
3694#if defined(TULIP_DEBUG)
3695	    sc->tulip_dbg.dbg_rxlowbufs++;
3696#endif
3697	    TULIP_PERFEND(rxget);
3698	    continue;
3699	}
3700	/*
3701	 * Now give the buffer(s) to the TULIP and save in our
3702	 * receive queue.
3703	 */
3704	do {
3705	    tulip_desc_t * const nextout = ri->ri_nextout;
3706#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3707	    if (sc->tulip_rxmaps_free > 0) {
3708		map = sc->tulip_rxmaps[--sc->tulip_rxmaps_free];
3709	    } else {
3710		m_freem(ms);
3711		sc->tulip_flags |= TULIP_RXBUFSLOW;
3712#if defined(TULIP_DEBUG)
3713		sc->tulip_dbg.dbg_rxlowbufs++;
3714#endif
3715		break;
3716	    }
3717	    M_SETCTX(ms, map);
3718	    error = bus_dmamap_load(sc->tulip_dmatag, map, mtod(ms, void *),
3719				    TULIP_RX_BUFLEN, NULL, BUS_DMA_NOWAIT);
3720	    if (error) {
3721		printf(TULIP_PRINTF_FMT ": unable to load rx map, "
3722		       "error = %d\n", TULIP_PRINTF_ARGS, error);
3723		panic("tulip_rx_intr");		/* XXX */
3724	    }
3725	    nextout->d_addr1 = map->dm_segs[0].ds_addr;
3726	    nextout->d_length1 = map->dm_segs[0].ds_len;
3727	    if (map->dm_nsegs == 2) {
3728		nextout->d_addr2 = map->dm_segs[1].ds_addr;
3729		nextout->d_length2 = map->dm_segs[1].ds_len;
3730	    } else {
3731		nextout->d_addr2 = 0;
3732		nextout->d_length2 = 0;
3733	    }
3734	    TULIP_RXDESC_POSTSYNC(sc, nextout, sizeof(*nextout));
3735#else /* TULIP_BUS_DMA */
3736	    nextout->d_addr1 = TULIP_KVATOPHYS(sc, mtod(ms, caddr_t));
3737	    nextout->d_length1 = TULIP_RX_BUFLEN;
3738#endif /* TULIP_BUS_DMA */
3739	    nextout->d_status = TULIP_DSTS_OWNER;
3740	    TULIP_RXDESC_POSTSYNC(sc, nextout, sizeof(u_int32_t));
3741	    if (++ri->ri_nextout == ri->ri_last)
3742		ri->ri_nextout = ri->ri_first;
3743	    me = ms->m_next;
3744	    ms->m_next = NULL;
3745	    IF_ENQUEUE(&sc->tulip_rxq, ms);
3746	} while ((ms = me) != NULL);
3747
3748	if (sc->tulip_rxq.ifq_len >= TULIP_RXQ_TARGET)
3749	    sc->tulip_flags &= ~TULIP_RXBUFSLOW;
3750	TULIP_PERFEND(rxget);
3751    }
3752
3753#if defined(TULIP_DEBUG)
3754    sc->tulip_dbg.dbg_rxintrs++;
3755    sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
3756#endif
3757    TULIP_PERFEND(rxintr);
3758}
3759
3760static int
3761tulip_tx_intr(
3762    tulip_softc_t * const sc)
3763{
3764    TULIP_PERFSTART(txintr)
3765    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
3766    struct mbuf *m;
3767    int xmits = 0;
3768    int descs = 0;
3769
3770    while (ri->ri_free < ri->ri_max) {
3771	u_int32_t d_flag;
3772
3773	TULIP_TXDESC_POSTSYNC(sc, ri->ri_nextin, sizeof(*ri->ri_nextin));
3774	if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
3775	    break;
3776
3777	ri->ri_free++;
3778	descs++;
3779	d_flag = ri->ri_nextin->d_flag;
3780	if (d_flag & TULIP_DFLAG_TxLASTSEG) {
3781	    if (d_flag & TULIP_DFLAG_TxSETUPPKT) {
3782		/*
3783		 * We've just finished processing a setup packet.
3784		 * Mark that we finished it.  If there's not
3785		 * another pending, startup the TULIP receiver.
3786		 * Make sure we ack the RXSTOPPED so we won't get
3787		 * an abormal interrupt indication.
3788		 */
3789		TULIP_TXMAP_POSTSYNC(sc, sc->tulip_setupmap);
3790		sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_HASHONLY);
3791		if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxINVRSFILT)
3792		    sc->tulip_flags |= TULIP_HASHONLY;
3793		if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == 0) {
3794		    tulip_rx_intr(sc);
3795		    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
3796		    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
3797		    TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
3798		    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3799		    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3800		}
3801	    } else {
3802		const u_int32_t d_status = ri->ri_nextin->d_status;
3803		IF_DEQUEUE(&sc->tulip_txq, m);
3804		if (m != NULL) {
3805#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
3806		    bus_dmamap_t map = M_GETCTX(m, bus_dmamap_t);
3807		    TULIP_TXMAP_POSTSYNC(sc, map);
3808		    sc->tulip_txmaps[sc->tulip_txmaps_free++] = map;
3809#endif /* TULIP_BUS_DMA */
3810#if NBPFILTER > 0
3811		    if (sc->tulip_bpf != NULL)
3812			TULIP_BPF_MTAP(sc, m);
3813#endif
3814		    m_freem(m);
3815#if defined(TULIP_DEBUG)
3816		} else {
3817		    printf(TULIP_PRINTF_FMT ": tx_intr: failed to dequeue mbuf?!?\n", TULIP_PRINTF_ARGS);
3818#endif
3819		}
3820		if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
3821		    tulip_mediapoll_event_t event = TULIP_MEDIAPOLL_TXPROBE_OK;
3822		    if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxEXCCOLL)) {
3823#if defined(TULIP_DEBUG)
3824			if (d_status & TULIP_DSTS_TxNOCARR)
3825			    sc->tulip_dbg.dbg_txprobe_nocarr++;
3826			if (d_status & TULIP_DSTS_TxEXCCOLL)
3827			    sc->tulip_dbg.dbg_txprobe_exccoll++;
3828#endif
3829			event = TULIP_MEDIAPOLL_TXPROBE_FAILED;
3830		    }
3831		    (*sc->tulip_boardsw->bd_media_poll)(sc, event);
3832		    /*
3833		     * Escape from the loop before media poll has reset the TULIP!
3834		     */
3835		    break;
3836		} else {
3837		    xmits++;
3838		    if (d_status & TULIP_DSTS_ERRSUM) {
3839			sc->tulip_if.if_oerrors++;
3840			if (d_status & TULIP_DSTS_TxEXCCOLL)
3841			    sc->tulip_dot3stats.dot3StatsExcessiveCollisions++;
3842			if (d_status & TULIP_DSTS_TxLATECOLL)
3843			    sc->tulip_dot3stats.dot3StatsLateCollisions++;
3844			if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxCARRLOSS))
3845			    sc->tulip_dot3stats.dot3StatsCarrierSenseErrors++;
3846			if (d_status & (TULIP_DSTS_TxUNDERFLOW|TULIP_DSTS_TxBABBLE))
3847			    sc->tulip_dot3stats.dot3StatsInternalMacTransmitErrors++;
3848			if (d_status & TULIP_DSTS_TxUNDERFLOW)
3849			    sc->tulip_dot3stats.dot3StatsInternalTransmitUnderflows++;
3850			if (d_status & TULIP_DSTS_TxBABBLE)
3851			    sc->tulip_dot3stats.dot3StatsInternalTransmitBabbles++;
3852		    } else {
3853			u_int32_t collisions =
3854			    (d_status & TULIP_DSTS_TxCOLLMASK)
3855				>> TULIP_DSTS_V_TxCOLLCNT;
3856			sc->tulip_if.if_collisions += collisions;
3857			if (collisions == 1)
3858			    sc->tulip_dot3stats.dot3StatsSingleCollisionFrames++;
3859			else if (collisions > 1)
3860			    sc->tulip_dot3stats.dot3StatsMultipleCollisionFrames++;
3861			else if (d_status & TULIP_DSTS_TxDEFERRED)
3862			    sc->tulip_dot3stats.dot3StatsDeferredTransmissions++;
3863			/*
3864			 * SQE is only valid for 10baseT/BNC/AUI when not
3865			 * running in full-duplex.  In order to speed up the
3866			 * test, the corresponding bit in tulip_flags needs to
3867			 * set as well to get us to count SQE Test Errors.
3868			 */
3869			if (d_status & TULIP_DSTS_TxNOHRTBT & sc->tulip_flags)
3870			    sc->tulip_dot3stats.dot3StatsSQETestErrors++;
3871		    }
3872		}
3873	    }
3874	}
3875
3876	if (++ri->ri_nextin == ri->ri_last)
3877	    ri->ri_nextin = ri->ri_first;
3878
3879	if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
3880	    sc->tulip_if.if_flags &= ~IFF_OACTIVE;
3881    }
3882    /*
3883     * If nothing left to transmit, disable the timer.
3884     * Else if progress, reset the timer back to 2 ticks.
3885     */
3886    if (ri->ri_free == ri->ri_max || (sc->tulip_flags & TULIP_TXPROBE_ACTIVE))
3887	sc->tulip_txtimer = 0;
3888    else if (xmits > 0)
3889	sc->tulip_txtimer = TULIP_TXTIMER;
3890    sc->tulip_if.if_opackets += xmits;
3891    TULIP_PERFEND(txintr);
3892    return descs;
3893}
3894
3895static void
3896tulip_print_abnormal_interrupt(
3897    tulip_softc_t * const sc,
3898    u_int32_t csr)
3899{
3900    const char * const *msgp = tulip_status_bits;
3901    const char *sep;
3902    u_int32_t mask;
3903    const char thrsh[] = "72|128\0\0\0" "96|256\0\0\0" "128|512\0\0" "160|1024";
3904
3905    csr &= (1 << (sizeof(tulip_status_bits)/sizeof(tulip_status_bits[0]))) - 1;
3906    printf(TULIP_PRINTF_FMT ": abnormal interrupt:", TULIP_PRINTF_ARGS);
3907    for (sep = " ", mask = 1; mask <= csr; mask <<= 1, msgp++) {
3908	if ((csr & mask) && *msgp != NULL) {
3909	    printf("%s%s", sep, *msgp);
3910	    if (mask == TULIP_STS_TXUNDERFLOW && (sc->tulip_flags & TULIP_NEWTXTHRESH)) {
3911		sc->tulip_flags &= ~TULIP_NEWTXTHRESH;
3912		if (sc->tulip_cmdmode & TULIP_CMD_STOREFWD) {
3913		    printf(" (switching to store-and-forward mode)");
3914		} else {
3915		    printf(" (raising TX threshold to %s)",
3916			   &thrsh[9 * ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) >> 14)]);
3917		}
3918	    }
3919	    sep = ", ";
3920	}
3921    }
3922    printf("\n");
3923}
3924
3925static void
3926tulip_intr_handler(
3927    tulip_softc_t * const sc,
3928    int *progress_p)
3929{
3930    TULIP_PERFSTART(intr)
3931    u_int32_t csr;
3932#if defined(__NetBSD__) && !defined(TULIP_USE_SOFTINTR)
3933    int only_once;
3934
3935    only_once = 1;
3936#endif
3937
3938    while ((csr = TULIP_CSR_READ(sc, csr_status)) & sc->tulip_intrmask) {
3939#if defined(__NetBSD__) && !defined(TULIP_USE_SOFTINTR)
3940        if (only_once == 1) {
3941#if NRND > 0
3942	    rnd_add_uint32(&sc->tulip_rndsource, csr);
3943#endif
3944	    only_once = 0;
3945	}
3946#endif
3947
3948	*progress_p = 1;
3949	TULIP_CSR_WRITE(sc, csr_status, csr);
3950
3951	if (csr & TULIP_STS_SYSERROR) {
3952	    sc->tulip_last_system_error = (csr & TULIP_STS_ERRORMASK) >> TULIP_STS_ERR_SHIFT;
3953	    if (sc->tulip_flags & TULIP_NOMESSAGES) {
3954		sc->tulip_flags |= TULIP_SYSTEMERROR;
3955	    } else {
3956		printf(TULIP_PRINTF_FMT ": system error: %s\n",
3957		       TULIP_PRINTF_ARGS,
3958		       tulip_system_errors[sc->tulip_last_system_error]);
3959	    }
3960	    sc->tulip_flags |= TULIP_NEEDRESET;
3961	    sc->tulip_system_errors++;
3962	    break;
3963	}
3964	if (csr & (TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL) & sc->tulip_intrmask) {
3965#if defined(TULIP_DEBUG)
3966	    sc->tulip_dbg.dbg_link_intrs++;
3967#endif
3968	    if (sc->tulip_boardsw->bd_media_poll != NULL) {
3969		(*sc->tulip_boardsw->bd_media_poll)(sc, csr & TULIP_STS_LINKFAIL
3970						    ? TULIP_MEDIAPOLL_LINKFAIL
3971						    : TULIP_MEDIAPOLL_LINKPASS);
3972		csr &= ~TULIP_STS_ABNRMLINTR;
3973	    }
3974	    tulip_media_print(sc);
3975	}
3976	if (csr & (TULIP_STS_RXINTR|TULIP_STS_RXNOBUF)) {
3977	    u_int32_t misses = TULIP_CSR_READ(sc, csr_missed_frames);
3978	    if (csr & TULIP_STS_RXNOBUF)
3979		sc->tulip_dot3stats.dot3StatsMissedFrames += misses & 0xFFFF;
3980	    /*
3981	     * Pass 2.[012] of the 21140A-A[CDE] may hang and/or corrupt data
3982	     * on receive overflows.
3983	     */
3984	   if ((misses & 0x0FFE0000) && (sc->tulip_features & TULIP_HAVE_RXBADOVRFLW)) {
3985		sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
3986		/*
3987		 * Stop the receiver process and spin until it's stopped.
3988		 * Tell rx_intr to drop the packets it dequeues.
3989		 */
3990		TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode & ~TULIP_CMD_RXRUN);
3991		while ((TULIP_CSR_READ(sc, csr_status) & TULIP_STS_RXSTOPPED) == 0)
3992		    ;
3993		TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
3994		sc->tulip_flags |= TULIP_RXIGNORE;
3995	    }
3996	    tulip_rx_intr(sc);
3997	    if (sc->tulip_flags & TULIP_RXIGNORE) {
3998		/*
3999		 * Restart the receiver.
4000		 */
4001		sc->tulip_flags &= ~TULIP_RXIGNORE;
4002		TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
4003	    }
4004	}
4005	if (csr & TULIP_STS_ABNRMLINTR) {
4006	    u_int32_t tmp = csr & sc->tulip_intrmask
4007		& ~(TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR);
4008	    if (csr & TULIP_STS_TXUNDERFLOW) {
4009		if ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) != TULIP_CMD_THRSHLD160) {
4010		    sc->tulip_cmdmode += TULIP_CMD_THRSHLD96;
4011		    sc->tulip_flags |= TULIP_NEWTXTHRESH;
4012		} else if (sc->tulip_features & TULIP_HAVE_STOREFWD) {
4013		    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD;
4014		    sc->tulip_flags |= TULIP_NEWTXTHRESH;
4015		}
4016	    }
4017	    if (sc->tulip_flags & TULIP_NOMESSAGES) {
4018		sc->tulip_statusbits |= tmp;
4019	    } else {
4020		tulip_print_abnormal_interrupt(sc, tmp);
4021		sc->tulip_flags |= TULIP_NOMESSAGES;
4022	    }
4023	    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
4024	}
4025	if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_TXPROBE_ACTIVE|TULIP_DOINGSETUP|TULIP_PROMISC)) {
4026	    tulip_tx_intr(sc);
4027	    if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
4028		tulip_ifstart(&sc->tulip_if);
4029	}
4030    }
4031    if (sc->tulip_flags & TULIP_NEEDRESET) {
4032	tulip_reset(sc);
4033	tulip_init(sc);
4034    }
4035    TULIP_PERFEND(intr);
4036}
4037
4038#if defined(TULIP_USE_SOFTINTR)
4039/*
4040 * This is a experimental idea to alleviate problems due to interrupt
4041 * livelock.  What is interrupt livelock?  It's when you spend all your
4042 * time servicing device interrupts and never drop below device ipl
4043 * to do "useful" work.
4044 *
4045 * So what we do here is see if the device needs service and if so,
4046 * disable interrupts (dismiss the interrupt), place it in a list of devices
4047 * needing service, and issue a network software interrupt.
4048 *
4049 * When our network software interrupt routine gets called, we simply
4050 * walk done the list of devices that we have created and deal with them
4051 * at splnet/splsoftnet.
4052 *
4053 */
4054static void
4055tulip_hardintr_handler(
4056    tulip_softc_t * const sc,
4057    int *progress_p)
4058{
4059    if (TULIP_CSR_READ(sc, csr_status) & (TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR) == 0)
4060	return;
4061    *progress_p = 1;
4062    /*
4063     * disable interrupts
4064     */
4065    TULIP_CSR_WRITE(sc, csr_intr, 0);
4066    /*
4067     * mark it as needing a software interrupt
4068     */
4069    tulip_softintr_mask |= (1U << sc->tulip_unit);
4070
4071#if defined(__NetBSD__) && NRND > 0
4072    /*
4073     * This isn't all that random (the value we feed in) but it is
4074     * better than a constant probably.  It isn't used in entropy
4075     * calculation anyway, just to add something to the pool.
4076     */
4077    rnd_add_uint32(&sc->tulip_rndsource, sc->tulip_flags);
4078#endif
4079}
4080
4081static void
4082tulip_softintr(
4083    void)
4084{
4085    u_int32_t softintr_mask, mask;
4086    int progress = 0;
4087    int unit;
4088    tulip_spl_t s;
4089
4090    /*
4091     * Copy mask to local copy and reset global one to 0.
4092     */
4093    s = TULIP_RAISESPL();
4094    softintr_mask = tulip_softintr_mask;
4095    tulip_softintr_mask = 0;
4096    TULIP_RESTORESPL(s);
4097
4098    /*
4099     * Optimize for the single unit case.
4100     */
4101    if (tulip_softintr_max_unit == 0) {
4102	if (softintr_mask & 1) {
4103	    tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(0);
4104	    /*
4105	     * Handle the "interrupt" and then reenable interrupts
4106	     */
4107	    softintr_mask = 0;
4108	    tulip_intr_handler(sc, &progress);
4109	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4110	}
4111	return;
4112    }
4113
4114    /*
4115     * Handle all "queued" interrupts in a round robin fashion.
4116     * This is done so as not to favor a particular interface.
4117     */
4118    unit = tulip_softintr_last_unit;
4119    mask = (1U << unit);
4120    while (softintr_mask != 0) {
4121	if (tulip_softintr_max_unit == unit) {
4122	    unit  = 0; mask   = 1;
4123	} else {
4124	    unit += 1; mask <<= 1;
4125	}
4126	if (softintr_mask & mask) {
4127	    tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(unit);
4128	    /*
4129	     * Handle the "interrupt" and then reenable interrupts
4130	     */
4131	    softintr_mask ^= mask;
4132	    tulip_intr_handler(sc, &progress);
4133	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4134	}
4135    }
4136
4137    /*
4138     * Save where we ending up.
4139     */
4140    tulip_softintr_last_unit = unit;
4141}
4142#endif	/* TULIP_USE_SOFTINTR */
4143
4144static tulip_intrfunc_t
4145tulip_intr_shared(
4146    void *arg)
4147{
4148    tulip_softc_t * sc = arg;
4149    int progress = 0;
4150
4151    for (; sc != NULL; sc = sc->tulip_slaves) {
4152#if defined(TULIP_DEBUG)
4153	sc->tulip_dbg.dbg_intrs++;
4154#endif
4155#if defined(TULIP_USE_SOFTINTR)
4156	tulip_hardintr_handler(sc, &progress);
4157#else
4158	tulip_intr_handler(sc, &progress);
4159#endif
4160    }
4161#if defined(TULIP_USE_SOFTINTR)
4162    if (progress)
4163	schednetisr(NETISR_DE);
4164#endif
4165#if !defined(TULIP_VOID_INTRFUNC)
4166    return progress;
4167#endif
4168}
4169
4170static tulip_intrfunc_t
4171tulip_intr_normal(
4172    void *arg)
4173{
4174    tulip_softc_t * sc = (tulip_softc_t *) arg;
4175    int progress = 0;
4176
4177#if defined(TULIP_DEBUG)
4178    sc->tulip_dbg.dbg_intrs++;
4179#endif
4180#if defined(TULIP_USE_SOFTINTR)
4181    tulip_hardintr_handler(sc, &progress);
4182    if (progress)
4183	schednetisr(NETISR_DE);
4184#else
4185    tulip_intr_handler(sc, &progress);
4186#endif
4187#if !defined(TULIP_VOID_INTRFUNC)
4188    return progress;
4189#endif
4190}
4191
4192static struct mbuf *
4193tulip_mbuf_compress(
4194    struct mbuf *m)
4195{
4196    struct mbuf *m0;
4197#if MCLBYTES >= ETHERMTU + 18 && !defined(BIG_PACKET)
4198    MGETHDR(m0, M_DONTWAIT, MT_DATA);
4199    if (m0 != NULL) {
4200	if (m->m_pkthdr.len > MHLEN) {
4201	    MCLGET(m0, M_DONTWAIT);
4202	    if ((m0->m_flags & M_EXT) == 0) {
4203		m_freem(m);
4204		m_freem(m0);
4205		return NULL;
4206	    }
4207	}
4208	m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t));
4209	m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
4210    }
4211#else
4212    int mlen = MHLEN;
4213    int len = m->m_pkthdr.len;
4214    struct mbuf **mp = &m0;
4215
4216    while (len > 0) {
4217	if (mlen == MHLEN) {
4218	    MGETHDR(*mp, M_DONTWAIT, MT_DATA);
4219	} else {
4220	    MGET(*mp, M_DONTWAIT, MT_DATA);
4221	}
4222	if (*mp == NULL) {
4223	    m_freem(m0);
4224	    m0 = NULL;
4225	    break;
4226	}
4227	if (len > MLEN) {
4228	    MCLGET(*mp, M_DONTWAIT);
4229	    if (((*mp)->m_flags & M_EXT) == 0) {
4230		m_freem(m0);
4231		m0 = NULL;
4232		break;
4233	    }
4234	    (*mp)->m_len = len <= MCLBYTES ? len : MCLBYTES;
4235	} else {
4236	    (*mp)->m_len = len <= mlen ? len : mlen;
4237	}
4238	m_copydata(m, m->m_pkthdr.len - len,
4239		   (*mp)->m_len, mtod((*mp), caddr_t));
4240	len -= (*mp)->m_len;
4241	mp = &(*mp)->m_next;
4242	mlen = MLEN;
4243    }
4244#endif
4245    m_freem(m);
4246    return m0;
4247}
4248
4249static struct mbuf *
4250tulip_txput(
4251    tulip_softc_t * const sc,
4252    struct mbuf *m)
4253{
4254    TULIP_PERFSTART(txput)
4255    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
4256    tulip_desc_t *eop, *nextout;
4257    int segcnt, free;
4258    u_int32_t d_status;
4259#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
4260    bus_dmamap_t map;
4261    int error;
4262#else
4263    struct mbuf *m0;
4264#endif
4265
4266#if defined(TULIP_DEBUG)
4267    if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
4268	printf(TULIP_PRINTF_FMT ": txput%s: tx not running\n",
4269	       TULIP_PRINTF_ARGS,
4270	       (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) ? "(probe)" : "");
4271	sc->tulip_flags |= TULIP_WANTTXSTART;
4272	sc->tulip_dbg.dbg_txput_finishes[0]++;
4273	goto finish;
4274    }
4275#endif
4276
4277    /*
4278     * Now we try to fill in our transmit descriptors.  This is
4279     * a bit reminiscent of going on the Ark two by two
4280     * since each descriptor for the TULIP can describe
4281     * two buffers.  So we advance through packet filling
4282     * each of the two entries at a time to to fill each
4283     * descriptor.  Clear the first and last segment bits
4284     * in each descriptor (actually just clear everything
4285     * but the end-of-ring or chain bits) to make sure
4286     * we don't get messed up by previously sent packets.
4287     *
4288     * We may fail to put the entire packet on the ring if
4289     * there is either not enough ring entries free or if the
4290     * packet has more than MAX_TXSEG segments.  In the former
4291     * case we will just wait for the ring to empty.  In the
4292     * latter case we have to recopy.
4293     */
4294#if !defined(TULIP_BUS_DMA) || defined(TULIP_BUS_DMA_NOTX)
4295  again:
4296    m0 = m;
4297#endif
4298    d_status = 0;
4299    eop = nextout = ri->ri_nextout;
4300    segcnt = 0;
4301    free = ri->ri_free;
4302
4303#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
4304    /*
4305     * Reclaim some dma maps from if we are out.
4306     */
4307    if (sc->tulip_txmaps_free == 0) {
4308#if defined(TULIP_DEBUG)
4309	sc->tulip_dbg.dbg_no_txmaps++;
4310#endif
4311	free += tulip_tx_intr(sc);
4312    }
4313    if (sc->tulip_txmaps_free > 0) {
4314	map = sc->tulip_txmaps[sc->tulip_txmaps_free-1];
4315    } else {
4316	sc->tulip_flags |= TULIP_WANTTXSTART;
4317#if defined(TULIP_DEBUG)
4318	sc->tulip_dbg.dbg_txput_finishes[1]++;
4319#endif
4320	goto finish;
4321    }
4322    error = bus_dmamap_load_mbuf(sc->tulip_dmatag, map, m, BUS_DMA_NOWAIT);
4323    if (error != 0) {
4324	if (error == EFBIG) {
4325	    /*
4326	     * The packet exceeds the number of transmit buffer
4327	     * entries that we can use for one packet, so we have
4328	     * to recopy it into one mbuf and then try again.
4329	     */
4330	    m = tulip_mbuf_compress(m);
4331	    if (m == NULL) {
4332#if defined(TULIP_DEBUG)
4333		sc->tulip_dbg.dbg_txput_finishes[2]++;
4334#endif
4335		goto finish;
4336	    }
4337	    error = bus_dmamap_load_mbuf(sc->tulip_dmatag, map, m, BUS_DMA_NOWAIT);
4338	}
4339	if (error != 0) {
4340	    printf(TULIP_PRINTF_FMT ": unable to load tx map, "
4341		   "error = %d\n", TULIP_PRINTF_ARGS, error);
4342#if defined(TULIP_DEBUG)
4343	    sc->tulip_dbg.dbg_txput_finishes[3]++;
4344#endif
4345	    goto finish;
4346	}
4347    }
4348    if ((free -= (map->dm_nsegs + 1) / 2) <= 0
4349	    /*
4350	     * See if there's any unclaimed space in the transmit ring.
4351	     */
4352	    && (free += tulip_tx_intr(sc)) <= 0) {
4353	/*
4354	 * There's no more room but since nothing
4355	 * has been committed at this point, just
4356	 * show output is active, put back the
4357	 * mbuf and return.
4358	 */
4359	sc->tulip_flags |= TULIP_WANTTXSTART;
4360#if defined(TULIP_DEBUG)
4361	sc->tulip_dbg.dbg_txput_finishes[4]++;
4362#endif
4363	bus_dmamap_unload(sc->tulip_dmatag, map);
4364	goto finish;
4365    }
4366    for (; map->dm_nsegs - segcnt > 1; segcnt += 2) {
4367	eop = nextout;
4368	eop->d_flag   &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4369	eop->d_status  = d_status;
4370	eop->d_addr1   = map->dm_segs[segcnt].ds_addr;
4371	eop->d_length1 = map->dm_segs[segcnt].ds_len;
4372	eop->d_addr2   = map->dm_segs[segcnt+1].ds_addr;
4373	eop->d_length2 = map->dm_segs[segcnt+1].ds_len;
4374	d_status = TULIP_DSTS_OWNER;
4375	if (++nextout == ri->ri_last)
4376	    nextout = ri->ri_first;
4377    }
4378    if (segcnt < map->dm_nsegs) {
4379	eop = nextout;
4380	eop->d_flag   &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4381	eop->d_status  = d_status;
4382	eop->d_addr1   = map->dm_segs[segcnt].ds_addr;
4383	eop->d_length1 = map->dm_segs[segcnt].ds_len;
4384	eop->d_addr2   = 0;
4385	eop->d_length2 = 0;
4386	if (++nextout == ri->ri_last)
4387	    nextout = ri->ri_first;
4388    }
4389    TULIP_TXMAP_PRESYNC(sc, map);
4390    M_SETCTX(m, map);
4391    map = NULL;
4392    --sc->tulip_txmaps_free;		/* commit to using the dmamap */
4393
4394#else /* !TULIP_BUS_DMA */
4395
4396    do {
4397	int len = m0->m_len;
4398	caddr_t addr = mtod(m0, caddr_t);
4399	unsigned clsize = CLBYTES - (((uintptr_t) addr) & (CLBYTES-1));
4400
4401	while (len > 0) {
4402	    unsigned slen = min(len, clsize);
4403#ifdef BIG_PACKET
4404	    int partial = 0;
4405	    if (slen >= 2048)
4406		slen = 2040, partial = 1;
4407#endif
4408	    segcnt++;
4409	    if (segcnt > TULIP_MAX_TXSEG) {
4410		/*
4411		 * The packet exceeds the number of transmit buffer
4412		 * entries that we can use for one packet, so we have
4413		 * recopy it into one mbuf and then try again.
4414		 */
4415		m = tulip_mbuf_compress(m);
4416		if (m == NULL)
4417		    goto finish;
4418		goto again;
4419	    }
4420	    if (segcnt & 1) {
4421		if (--free == 0) {
4422		    /*
4423		     * See if there's any unclaimed space in the
4424		     * transmit ring.
4425		     */
4426		    if ((free += tulip_tx_intr(sc)) == 0) {
4427			/*
4428			 * There's no more room but since nothing
4429			 * has been committed at this point, just
4430			 * show output is active, put back the
4431			 * mbuf and return.
4432			 */
4433			sc->tulip_flags |= TULIP_WANTTXSTART;
4434#if defined(TULIP_DEBUG)
4435			sc->tulip_dbg.dbg_txput_finishes[1]++;
4436#endif
4437			goto finish;
4438		    }
4439		}
4440		eop = nextout;
4441		if (++nextout == ri->ri_last)
4442		    nextout = ri->ri_first;
4443		eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4444		eop->d_status = d_status;
4445		eop->d_addr1 = TULIP_KVATOPHYS(sc, addr);
4446		eop->d_length1 = slen;
4447	    } else {
4448		/*
4449		 *  Fill in second half of descriptor
4450		 */
4451		eop->d_addr2 = TULIP_KVATOPHYS(sc, addr);
4452		eop->d_length2 = slen;
4453	    }
4454	    d_status = TULIP_DSTS_OWNER;
4455	    len -= slen;
4456	    addr += slen;
4457#ifdef BIG_PACKET
4458	    if (partial)
4459		continue;
4460#endif
4461	    clsize = CLBYTES;
4462	}
4463    } while ((m0 = m0->m_next) != NULL);
4464#endif /* TULIP_BUS_DMA */
4465
4466    /*
4467     * The descriptors have been filled in.  Now get ready
4468     * to transmit.
4469     */
4470    IF_ENQUEUE(&sc->tulip_txq, m);
4471    m = NULL;
4472
4473    /*
4474     * Make sure the next descriptor after this packet is owned
4475     * by us since it may have been set up above if we ran out
4476     * of room in the ring.
4477     */
4478    nextout->d_status = 0;
4479    TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(u_int32_t));
4480
4481#if !defined(TULIP_BUS_DMA) || defined(TULIP_BUS_DMA_NOTX)
4482    /*
4483     * If we only used the first segment of the last descriptor,
4484     * make sure the second segment will not be used.
4485     */
4486    if (segcnt & 1) {
4487	eop->d_addr2 = 0;
4488	eop->d_length2 = 0;
4489    }
4490#endif /* TULIP_BUS_DMA */
4491
4492    /*
4493     * Mark the last and first segments, indicate we want a transmit
4494     * complete interrupt, and tell it to transmit!
4495     */
4496    eop->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
4497
4498    /*
4499     * Note that ri->ri_nextout is still the start of the packet
4500     * and until we set the OWNER bit, we can still back out of
4501     * everything we have done.
4502     */
4503    ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
4504#if defined(TULIP_BUS_MAP) && !defined(TULIP_BUS_DMA_NOTX)
4505    if (eop < ri->ri_nextout) {
4506	TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout,
4507			     (caddr_t) ri->ri_last - (caddr_t) ri->ri_nextout);
4508	TULIP_TXDESC_PRESYNC(sc, ri->ri_first,
4509			     (caddr_t) (eop + 1) - (caddr_t) ri->ri_first);
4510    } else {
4511	TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout,
4512			     (caddr_t) (eop + 1) - (caddr_t) ri->ri_nextout);
4513    }
4514#endif
4515    ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
4516    TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout, sizeof(u_int32_t));
4517
4518    /*
4519     * This advances the ring for us.
4520     */
4521    ri->ri_nextout = nextout;
4522    ri->ri_free = free;
4523
4524    TULIP_PERFEND(txput);
4525
4526    if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
4527	TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4528	sc->tulip_if.if_flags |= IFF_OACTIVE;
4529	sc->tulip_if.if_start = tulip_ifstart;
4530	TULIP_PERFEND(txput);
4531	return NULL;
4532    }
4533
4534    /*
4535     * switch back to the single queueing ifstart.
4536     */
4537    sc->tulip_flags &= ~TULIP_WANTTXSTART;
4538    if (sc->tulip_txtimer == 0)
4539	sc->tulip_txtimer = TULIP_TXTIMER;
4540#if defined(TULIP_DEBUG)
4541    sc->tulip_dbg.dbg_txput_finishes[5]++;
4542#endif
4543
4544    /*
4545     * If we want a txstart, there must be not enough space in the
4546     * transmit ring.  So we want to enable transmit done interrupts
4547     * so we can immediately reclaim some space.  When the transmit
4548     * interrupt is posted, the interrupt handler will call tx_intr
4549     * to reclaim space and then txstart (since WANTTXSTART is set).
4550     * txstart will move the packet into the transmit ring and clear
4551     * WANTTXSTART thereby causing TXINTR to be cleared.
4552     */
4553  finish:
4554#if defined(TULIP_DEBUG)
4555    sc->tulip_dbg.dbg_txput_finishes[6]++;
4556#endif
4557    if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_DOINGSETUP)) {
4558	sc->tulip_if.if_flags |= IFF_OACTIVE;
4559	sc->tulip_if.if_start = tulip_ifstart;
4560	if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
4561	    sc->tulip_intrmask |= TULIP_STS_TXINTR;
4562	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4563	}
4564    } else if ((sc->tulip_flags & TULIP_PROMISC) == 0) {
4565	if (sc->tulip_intrmask & TULIP_STS_TXINTR) {
4566	    sc->tulip_intrmask &= ~TULIP_STS_TXINTR;
4567	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4568	}
4569    }
4570    TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4571    TULIP_PERFEND(txput);
4572    return m;
4573}
4574
4575static void
4576tulip_txput_setup(
4577    tulip_softc_t * const sc)
4578{
4579    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
4580    tulip_desc_t *nextout;
4581
4582    /*
4583     * We will transmit, at most, one setup packet per call to ifstart.
4584     */
4585
4586#if defined(TULIP_DEBUG)
4587    if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
4588	printf(TULIP_PRINTF_FMT ": txput_setup: tx not running\n",
4589	       TULIP_PRINTF_ARGS);
4590	sc->tulip_flags |= TULIP_WANTTXSTART;
4591	sc->tulip_if.if_start = tulip_ifstart;
4592	return;
4593    }
4594#endif
4595    /*
4596     * Try to reclaim some free descriptors..
4597     */
4598    if (ri->ri_free < 2)
4599	tulip_tx_intr(sc);
4600    if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
4601	sc->tulip_flags |= TULIP_WANTTXSTART;
4602	sc->tulip_if.if_start = tulip_ifstart;
4603	return;
4604    }
4605    bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
4606	  sizeof(sc->tulip_setupbuf));
4607    /*
4608     * Clear WANTSETUP and set DOINGSETUP.  Set know that WANTSETUP is
4609     * set and DOINGSETUP is clear doing an XOR of the two will DTRT.
4610     */
4611    sc->tulip_flags ^= TULIP_WANTSETUP|TULIP_DOINGSETUP;
4612    ri->ri_free--;
4613    nextout = ri->ri_nextout;
4614    nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4615    nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
4616	|TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
4617    if (sc->tulip_flags & TULIP_WANTHASHPERFECT)
4618	nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
4619    else if (sc->tulip_flags & TULIP_WANTHASHONLY)
4620	nextout->d_flag |= TULIP_DFLAG_TxHASHFILT|TULIP_DFLAG_TxINVRSFILT;
4621
4622    nextout->d_length2 = 0;
4623    nextout->d_addr2 = 0;
4624#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
4625    nextout->d_length1 = sc->tulip_setupmap->dm_segs[0].ds_len;
4626    nextout->d_addr1 = sc->tulip_setupmap->dm_segs[0].ds_addr;
4627    if (sc->tulip_setupmap->dm_nsegs == 2) {
4628	nextout->d_length2 = sc->tulip_setupmap->dm_segs[1].ds_len;
4629	nextout->d_addr2 = sc->tulip_setupmap->dm_segs[1].ds_addr;
4630    }
4631    TULIP_TXMAP_PRESYNC(sc, sc->tulip_setupmap);
4632    TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(*nextout));
4633#else
4634    nextout->d_length1 = sizeof(sc->tulip_setupbuf);
4635    nextout->d_addr1 = TULIP_KVATOPHYS(sc, sc->tulip_setupbuf);
4636#endif
4637
4638    /*
4639     * Advance the ring for the next transmit packet.
4640     */
4641    if (++ri->ri_nextout == ri->ri_last)
4642	ri->ri_nextout = ri->ri_first;
4643
4644    /*
4645     * Make sure the next descriptor is owned by us since it
4646     * may have been set up above if we ran out of room in the
4647     * ring.
4648     */
4649    ri->ri_nextout->d_status = 0;
4650    TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout, sizeof(u_int32_t));
4651    nextout->d_status = TULIP_DSTS_OWNER;
4652    /*
4653     * Flush the ownwership of the current descriptor
4654     */
4655    TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(u_int32_t));
4656    TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4657    if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
4658	sc->tulip_intrmask |= TULIP_STS_TXINTR;
4659	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4660    }
4661}
4662
4663
4664/*
4665 * This routine is entered at splnet() (splsoftnet() on NetBSD)
4666 * and thereby imposes no problems when TULIP_USE_SOFTINTR is
4667 * defined or not.
4668 */
4669static int
4670tulip_ifioctl(
4671    struct ifnet * ifp,
4672    ioctl_cmd_t cmd,
4673    caddr_t data)
4674{
4675    TULIP_PERFSTART(ifioctl)
4676    tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
4677    struct ifaddr *ifa = (struct ifaddr *)data;
4678    struct ifreq *ifr = (struct ifreq *) data;
4679    tulip_spl_t s;
4680    int error = 0;
4681
4682#if defined(TULIP_USE_SOFTINTR)
4683    s = TULIP_RAISESOFTSPL();
4684#else
4685    s = TULIP_RAISESPL();
4686#endif
4687    switch (cmd) {
4688	case SIOCSIFADDR: {
4689	    ifp->if_flags |= IFF_UP;
4690	    switch(ifa->ifa_addr->sa_family) {
4691#ifdef INET
4692		case AF_INET: {
4693		    tulip_init(sc);
4694		    TULIP_ARP_IFINIT(sc, ifa);
4695		    break;
4696		}
4697#endif /* INET */
4698
4699#ifdef IPX
4700		case AF_IPX: {
4701		    struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
4702		    if (ipx_nullhost(*ina)) {
4703			ina->x_host = *(union ipx_host *)(sc->tulip_enaddr);
4704		    } else {
4705			ifp->if_flags &= ~IFF_RUNNING;
4706			bcopy((caddr_t)ina->x_host.c_host,
4707			      (caddr_t)sc->tulip_enaddr,
4708			      sizeof(sc->tulip_enaddr));
4709		    }
4710		    tulip_init(sc);
4711		    break;
4712		}
4713#endif /* IPX */
4714
4715#ifdef NS
4716		/*
4717		 * This magic copied from if_is.c; I don't use XNS,
4718		 * so I have no way of telling if this actually
4719		 * works or not.
4720		 */
4721		case AF_NS: {
4722		    struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
4723		    if (ns_nullhost(*ina)) {
4724			ina->x_host = *(union ns_host *)(sc->tulip_enaddr);
4725		    } else {
4726			ifp->if_flags &= ~IFF_RUNNING;
4727			bcopy((caddr_t)ina->x_host.c_host,
4728			      (caddr_t)sc->tulip_enaddr,
4729			      sizeof(sc->tulip_enaddr));
4730		    }
4731		    tulip_init(sc);
4732		    break;
4733		}
4734#endif /* NS */
4735
4736		default: {
4737		    tulip_init(sc);
4738		    break;
4739		}
4740	    }
4741	    break;
4742	}
4743	case SIOCGIFADDR: {
4744	    bcopy((caddr_t) sc->tulip_enaddr,
4745		  (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
4746		  6);
4747	    break;
4748	}
4749
4750	case SIOCSIFFLAGS: {
4751#if !defined(IFM_ETHER)
4752	    int flags = 0;
4753	    if (ifp->if_flags & IFF_LINK0) flags |= 1;
4754	    if (ifp->if_flags & IFF_LINK1) flags |= 2;
4755	    if (ifp->if_flags & IFF_LINK2) flags |= 4;
4756	    if (flags == 7) {
4757		ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1|IFF_LINK2);
4758		sc->tulip_media = TULIP_MEDIA_UNKNOWN;
4759		sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
4760		sc->tulip_flags &= ~(TULIP_WANTRXACT|TULIP_LINKUP|TULIP_NOAUTOSENSE);
4761		tulip_reset(sc);
4762	    } else if (flags) {
4763		tulip_media_t media;
4764		for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
4765		    if (sc->tulip_mediums[media] != NULL && --flags == 0) {
4766			sc->tulip_flags |= TULIP_NOAUTOSENSE;
4767			if (sc->tulip_media != media || (sc->tulip_flags & TULIP_DIDNWAY)) {
4768			    sc->tulip_flags &= ~TULIP_DIDNWAY;
4769			    tulip_linkup(sc, media);
4770			}
4771			break;
4772		    }
4773		}
4774		if (flags)
4775		    printf(TULIP_PRINTF_FMT ": ignored invalid media request\n", TULIP_PRINTF_ARGS);
4776	    }
4777#endif
4778	    tulip_addr_filter(sc); /* reinit multicast filter */
4779	    tulip_init(sc);
4780	    break;
4781	}
4782
4783#if defined(SIOCSIFMEDIA)
4784	case SIOCSIFMEDIA:
4785	case SIOCGIFMEDIA: {
4786	    error = ifmedia_ioctl(ifp, ifr, &sc->tulip_ifmedia, cmd);
4787	    break;
4788	}
4789#endif
4790
4791	case SIOCADDMULTI:
4792	case SIOCDELMULTI: {
4793	    /*
4794	     * Update multicast listeners
4795	     */
4796#if defined(__FreeBSD__) && __FreeBSD_version >= 300000
4797	    tulip_addr_filter(sc);		/* reset multicast filtering */
4798	    tulip_init(sc);
4799	    error = 0;
4800#else
4801	    if (cmd == SIOCADDMULTI)
4802		error = ether_addmulti(ifr, TULIP_ETHERCOM(sc));
4803	    else
4804		error = ether_delmulti(ifr, TULIP_ETHERCOM(sc));
4805
4806	    if (error == ENETRESET) {
4807		tulip_addr_filter(sc);		/* reset multicast filtering */
4808		tulip_init(sc);
4809		error = 0;
4810	    }
4811#endif
4812	    break;
4813	}
4814#if defined(SIOCSIFMTU)
4815#if !defined(ifr_mtu)
4816#define ifr_mtu ifr_metric
4817#endif
4818	case SIOCSIFMTU:
4819	    /*
4820	     * Set the interface MTU.
4821	     */
4822	    if (ifr->ifr_mtu > ETHERMTU
4823#ifdef BIG_PACKET
4824		    && sc->tulip_chipid != TULIP_21140
4825		    && sc->tulip_chipid != TULIP_21140A
4826		    && sc->tulip_chipid != TULIP_21041
4827#endif
4828		) {
4829		error = EINVAL;
4830		break;
4831	    }
4832	    ifp->if_mtu = ifr->ifr_mtu;
4833#ifdef BIG_PACKET
4834	    tulip_reset(sc);
4835	    tulip_init(sc);
4836#endif
4837	    break;
4838#endif /* SIOCSIFMTU */
4839
4840#ifdef SIOCGADDRROM
4841	case SIOCGADDRROM: {
4842	    error = copyout(sc->tulip_rombuf, ifr->ifr_data, sizeof(sc->tulip_rombuf));
4843	    break;
4844	}
4845#endif
4846#ifdef SIOCGCHIPID
4847	case SIOCGCHIPID: {
4848	    ifr->ifr_metric = (int) sc->tulip_chipid;
4849	    break;
4850	}
4851#endif
4852	default: {
4853	    error = EINVAL;
4854	    break;
4855	}
4856    }
4857
4858    TULIP_RESTORESPL(s);
4859    TULIP_PERFEND(ifioctl);
4860    return error;
4861}
4862
4863/*
4864 * These routines gets called at device spl (from ether_output).  This might
4865 * pose a problem for TULIP_USE_SOFTINTR if ether_output is called at
4866 * device spl from another driver.
4867 */
4868
4869static ifnet_ret_t
4870tulip_ifstart(
4871    struct ifnet * const ifp)
4872{
4873    TULIP_PERFSTART(ifstart)
4874    tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
4875
4876    if (sc->tulip_if.if_flags & IFF_RUNNING) {
4877
4878	if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
4879	    tulip_txput_setup(sc);
4880
4881	while (sc->tulip_if.if_snd.ifq_head != NULL) {
4882	    struct mbuf *m;
4883	    IF_DEQUEUE(&sc->tulip_if.if_snd, m);
4884	    if ((m = tulip_txput(sc, m)) != NULL) {
4885		IF_PREPEND(&sc->tulip_if.if_snd, m);
4886		break;
4887	    }
4888	}
4889	if (sc->tulip_if.if_snd.ifq_head == NULL)
4890	    sc->tulip_if.if_start = tulip_ifstart_one;
4891    }
4892
4893    TULIP_PERFEND(ifstart);
4894}
4895
4896static ifnet_ret_t
4897tulip_ifstart_one(
4898    struct ifnet * const ifp)
4899{
4900    TULIP_PERFSTART(ifstart_one)
4901    tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
4902
4903    if ((sc->tulip_if.if_flags & IFF_RUNNING)
4904	    && sc->tulip_if.if_snd.ifq_head != NULL) {
4905	struct mbuf *m;
4906	IF_DEQUEUE(&sc->tulip_if.if_snd, m);
4907	if ((m = tulip_txput(sc, m)) != NULL)
4908	    IF_PREPEND(&sc->tulip_if.if_snd, m);
4909    }
4910    TULIP_PERFEND(ifstart_one);
4911}
4912
4913/*
4914 * Even though this routine runs at device spl, it does not break
4915 * our use of splnet (splsoftnet under NetBSD) for the majority
4916 * of this driver (if TULIP_USE_SOFTINTR defined) since
4917 * if_watcbog is called from if_watchdog which is called from
4918 * splsoftclock which is below spl[soft]net.
4919 */
4920static void
4921tulip_ifwatchdog(
4922    struct ifnet *ifp)
4923{
4924    TULIP_PERFSTART(ifwatchdog)
4925    tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
4926
4927#if defined(TULIP_DEBUG)
4928    u_int32_t rxintrs = sc->tulip_dbg.dbg_rxintrs - sc->tulip_dbg.dbg_last_rxintrs;
4929    if (rxintrs > sc->tulip_dbg.dbg_high_rxintrs_hz)
4930	sc->tulip_dbg.dbg_high_rxintrs_hz = rxintrs;
4931    sc->tulip_dbg.dbg_last_rxintrs = sc->tulip_dbg.dbg_rxintrs;
4932#endif /* TULIP_DEBUG */
4933
4934    sc->tulip_if.if_timer = 1;
4935    /*
4936     * These should be rare so do a bulk test up front so we can just skip
4937     * them if needed.
4938     */
4939    if (sc->tulip_flags & (TULIP_SYSTEMERROR|TULIP_RXBUFSLOW|TULIP_NOMESSAGES)) {
4940	/*
4941	 * If the number of receive buffer is low, try to refill
4942	 */
4943	if (sc->tulip_flags & TULIP_RXBUFSLOW)
4944	    tulip_rx_intr(sc);
4945
4946	if (sc->tulip_flags & TULIP_SYSTEMERROR) {
4947	    printf(TULIP_PRINTF_FMT ": %d system errors: last was %s\n",
4948		   TULIP_PRINTF_ARGS, sc->tulip_system_errors,
4949		   tulip_system_errors[sc->tulip_last_system_error]);
4950	}
4951	if (sc->tulip_statusbits) {
4952	    tulip_print_abnormal_interrupt(sc, sc->tulip_statusbits);
4953	    sc->tulip_statusbits = 0;
4954	}
4955
4956	sc->tulip_flags &= ~(TULIP_NOMESSAGES|TULIP_SYSTEMERROR);
4957    }
4958
4959    if (sc->tulip_txtimer)
4960	tulip_tx_intr(sc);
4961    if (sc->tulip_txtimer && --sc->tulip_txtimer == 0) {
4962	printf(TULIP_PRINTF_FMT ": transmission timeout\n", TULIP_PRINTF_ARGS);
4963	if (TULIP_DO_AUTOSENSE(sc)) {
4964	    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
4965	    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
4966	    sc->tulip_flags &= ~(TULIP_WANTRXACT|TULIP_LINKUP);
4967	}
4968	tulip_reset(sc);
4969	tulip_init(sc);
4970    }
4971
4972    TULIP_PERFEND(ifwatchdog);
4973    TULIP_PERFMERGE(sc, perf_intr_cycles);
4974    TULIP_PERFMERGE(sc, perf_ifstart_cycles);
4975    TULIP_PERFMERGE(sc, perf_ifioctl_cycles);
4976    TULIP_PERFMERGE(sc, perf_ifwatchdog_cycles);
4977    TULIP_PERFMERGE(sc, perf_timeout_cycles);
4978    TULIP_PERFMERGE(sc, perf_ifstart_one_cycles);
4979    TULIP_PERFMERGE(sc, perf_txput_cycles);
4980    TULIP_PERFMERGE(sc, perf_txintr_cycles);
4981    TULIP_PERFMERGE(sc, perf_rxintr_cycles);
4982    TULIP_PERFMERGE(sc, perf_rxget_cycles);
4983    TULIP_PERFMERGE(sc, perf_intr);
4984    TULIP_PERFMERGE(sc, perf_ifstart);
4985    TULIP_PERFMERGE(sc, perf_ifioctl);
4986    TULIP_PERFMERGE(sc, perf_ifwatchdog);
4987    TULIP_PERFMERGE(sc, perf_timeout);
4988    TULIP_PERFMERGE(sc, perf_ifstart_one);
4989    TULIP_PERFMERGE(sc, perf_txput);
4990    TULIP_PERFMERGE(sc, perf_txintr);
4991    TULIP_PERFMERGE(sc, perf_rxintr);
4992    TULIP_PERFMERGE(sc, perf_rxget);
4993}
4994
4995#if defined(__bsdi__) || (defined(__FreeBSD__) && BSD < 199506)
4996static ifnet_ret_t
4997tulip_ifwatchdog_wrapper(
4998    int unit)
4999{
5000    tulip_ifwatchdog(&TULIP_UNIT_TO_SOFTC(unit)->tulip_if);
5001}
5002#define	tulip_ifwatchdog	tulip_ifwatchdog_wrapper
5003#endif
5004
5005/*
5006 * All printf's are real as of now!
5007 */
5008#ifdef printf
5009#undef printf
5010#endif
5011#if !defined(IFF_NOTRAILERS)
5012#define IFF_NOTRAILERS		0
5013#endif
5014
5015static void
5016tulip_attach(
5017    tulip_softc_t * const sc)
5018{
5019    struct ifnet * const ifp = &sc->tulip_if;
5020
5021    ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
5022    ifp->if_ioctl = tulip_ifioctl;
5023    ifp->if_start = tulip_ifstart;
5024    ifp->if_watchdog = tulip_ifwatchdog;
5025    ifp->if_timer = 1;
5026#if !defined(__bsdi__) || _BSDI_VERSION < 199401
5027    ifp->if_output = ether_output;
5028#endif
5029#if defined(__bsdi__) && _BSDI_VERSION < 199401
5030    ifp->if_mtu = ETHERMTU;
5031#endif
5032
5033#if defined(__bsdi__) && _BSDI_VERSION >= 199510
5034    aprint_naive(": DEC Ethernet");
5035    aprint_normal(": %s%s", sc->tulip_boardid,
5036        tulip_chipdescs[sc->tulip_chipid]);
5037    aprint_verbose(" pass %d.%d", (sc->tulip_revinfo & 0xF0) >> 4,
5038        sc->tulip_revinfo & 0x0F);
5039    printf("\n");
5040    sc->tulip_pf = aprint_normal;
5041    aprint_normal(TULIP_PRINTF_FMT ": address " TULIP_EADDR_FMT "\n",
5042		  TULIP_PRINTF_ARGS,
5043		  TULIP_EADDR_ARGS(sc->tulip_enaddr));
5044#else
5045    printf(
5046#if defined(__bsdi__)
5047	   "\n"
5048#endif
5049	   TULIP_PRINTF_FMT ": %s%s pass %d.%d%s\n",
5050	   TULIP_PRINTF_ARGS,
5051	   sc->tulip_boardid,
5052	   tulip_chipdescs[sc->tulip_chipid],
5053	   (sc->tulip_revinfo & 0xF0) >> 4,
5054	   sc->tulip_revinfo & 0x0F,
5055	   (sc->tulip_features & (TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM))
5056		 == TULIP_HAVE_ISVSROM ? " (invalid EESPROM checksum)" : "");
5057    printf(TULIP_PRINTF_FMT ": address " TULIP_EADDR_FMT "\n",
5058	   TULIP_PRINTF_ARGS,
5059	   TULIP_EADDR_ARGS(sc->tulip_enaddr));
5060#endif
5061
5062#if defined(__alpha__)
5063    /*
5064     * In case the SRM console told us about a bogus media,
5065     * we need to check to be safe.
5066     */
5067    if (sc->tulip_mediums[sc->tulip_media] == NULL)
5068	sc->tulip_media = TULIP_MEDIA_UNKNOWN;
5069#endif
5070
5071    (*sc->tulip_boardsw->bd_media_probe)(sc);
5072#if defined(IFM_ETHER)
5073    ifmedia_init(&sc->tulip_ifmedia, 0,
5074		 tulip_ifmedia_change,
5075		 tulip_ifmedia_status);
5076#else
5077    {
5078	tulip_media_t media;
5079	int cnt;
5080	printf(TULIP_PRINTF_FMT ": media:", TULIP_PRINTF_ARGS);
5081	for (media = TULIP_MEDIA_UNKNOWN, cnt = 1; cnt < 7 && media < TULIP_MEDIA_MAX; media++) {
5082	    if (sc->tulip_mediums[media] != NULL) {
5083		printf(" %d=\"%s\"", cnt, tulip_mediums[media]);
5084		cnt++;
5085	    }
5086	}
5087	if (cnt == 1) {
5088	    sc->tulip_features |= TULIP_HAVE_NOMEDIA;
5089	    printf(" none\n");
5090	} else {
5091	    printf("\n");
5092	}
5093    }
5094#endif
5095    sc->tulip_flags &= ~TULIP_DEVICEPROBE;
5096#if defined(IFM_ETHER)
5097    tulip_ifmedia_add(sc);
5098#endif
5099
5100    tulip_reset(sc);
5101
5102#if defined(__bsdi__) && _BSDI_VERSION >= 199510
5103    sc->tulip_pf = printf;
5104    TULIP_ETHER_IFATTACH(sc);
5105#else
5106    if_attach(ifp);
5107#if defined(__FreeBSD__) && __FreeBSD_version >= 300000
5108    ifp->if_snd.ifq_maxlen = ifqmaxlen;
5109#endif
5110#if defined(__NetBSD__) || (defined(__FreeBSD__) && BSD >= 199506)
5111    TULIP_ETHER_IFATTACH(sc);
5112#endif
5113#endif /* __bsdi__ */
5114
5115#if NBPFILTER > 0
5116    TULIP_BPF_ATTACH(sc);
5117#endif
5118
5119#if defined(__NetBSD__) && NRND > 0
5120    rnd_attach_source(&sc->tulip_rndsource, sc->tulip_dev.dv_xname,
5121		      RND_TYPE_NET, 0);
5122#endif
5123}
5124
5125#if defined(TULIP_BUS_DMA)
5126#if !defined(TULIP_BUS_DMA_NOTX) || !defined(TULIP_BUS_DMA_NORX)
5127static int
5128tulip_busdma_allocmem(
5129    tulip_softc_t * const sc,
5130    size_t size,
5131    bus_dmamap_t *map_p,
5132    tulip_desc_t **desc_p)
5133{
5134    bus_dma_segment_t segs[1];
5135    int nsegs, error;
5136    error = bus_dmamem_alloc(sc->tulip_dmatag, size, 1, CLBYTES,
5137			     segs, sizeof(segs)/sizeof(segs[0]),
5138			     &nsegs, BUS_DMA_NOWAIT);
5139    if (error == 0) {
5140	void *desc;
5141	error = bus_dmamem_map(sc->tulip_dmatag, segs, nsegs, size,
5142			       (void *) &desc, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
5143	if (error == 0) {
5144	    bus_dmamap_t map;
5145	    error = bus_dmamap_create(sc->tulip_dmatag, size, 1, size, 0,
5146				      BUS_DMA_NOWAIT, &map);
5147	    if (error == 0) {
5148		error = bus_dmamap_load(sc->tulip_dmatag, map, desc,
5149					size, NULL, BUS_DMA_NOWAIT);
5150		if (error)
5151		    bus_dmamap_destroy(sc->tulip_dmatag, map);
5152		else
5153		    *map_p = map;
5154	    }
5155	    if (error)
5156		bus_dmamem_unmap(sc->tulip_dmatag, desc, size);
5157	}
5158	if (error)
5159	    bus_dmamem_free(sc->tulip_dmatag, segs, nsegs);
5160	else
5161	    *desc_p = desc;
5162    }
5163    return error;
5164}
5165#endif
5166
5167static int
5168tulip_busdma_init(
5169    tulip_softc_t * const sc)
5170{
5171    int error = 0;
5172
5173#if !defined(TULIP_BUS_DMA_NOTX)
5174    /*
5175     * Allocate dmamap for setup descriptor
5176     */
5177    error = bus_dmamap_create(sc->tulip_dmatag, sizeof(sc->tulip_setupbuf), 2,
5178			      sizeof(sc->tulip_setupbuf), 0, BUS_DMA_NOWAIT,
5179			      &sc->tulip_setupmap);
5180    if (error == 0) {
5181	error = bus_dmamap_load(sc->tulip_dmatag, sc->tulip_setupmap,
5182				sc->tulip_setupbuf, sizeof(sc->tulip_setupbuf),
5183				NULL, BUS_DMA_NOWAIT);
5184	if (error)
5185	    bus_dmamap_destroy(sc->tulip_dmatag, sc->tulip_setupmap);
5186    }
5187    /*
5188     * Allocate space and dmamap for transmit ring
5189     */
5190    if (error == 0) {
5191	error = tulip_busdma_allocmem(sc, sizeof(tulip_desc_t) * TULIP_TXDESCS,
5192				      &sc->tulip_txdescmap,
5193				      &sc->tulip_txdescs);
5194    }
5195
5196    /*
5197     * Allocate dmamaps for each transmit descriptors
5198     */
5199    if (error == 0) {
5200	while (error == 0 && sc->tulip_txmaps_free < TULIP_TXDESCS) {
5201	    bus_dmamap_t map;
5202	    if ((error = TULIP_TXMAP_CREATE(sc, &map)) == 0)
5203		sc->tulip_txmaps[sc->tulip_txmaps_free++] = map;
5204	}
5205	if (error) {
5206	    while (sc->tulip_txmaps_free > 0)
5207		bus_dmamap_destroy(sc->tulip_dmatag,
5208				   sc->tulip_txmaps[--sc->tulip_txmaps_free]);
5209	}
5210    }
5211#else
5212    if (error == 0) {
5213	sc->tulip_txdescs = (tulip_desc_t *) malloc(TULIP_TXDESCS * sizeof(tulip_desc_t), M_DEVBUF, M_NOWAIT);
5214	if (sc->tulip_txdescs == NULL)
5215	    error = ENOMEM;
5216    }
5217#endif
5218#if !defined(TULIP_BUS_DMA_NORX)
5219    /*
5220     * Allocate space and dmamap for receive ring
5221     */
5222    if (error == 0) {
5223	error = tulip_busdma_allocmem(sc, sizeof(tulip_desc_t) * TULIP_RXDESCS,
5224				      &sc->tulip_rxdescmap,
5225				      &sc->tulip_rxdescs);
5226    }
5227
5228    /*
5229     * Allocate dmamaps for each receive descriptors
5230     */
5231    if (error == 0) {
5232	while (error == 0 && sc->tulip_rxmaps_free < TULIP_RXDESCS) {
5233	    bus_dmamap_t map;
5234	    if ((error = TULIP_RXMAP_CREATE(sc, &map)) == 0)
5235		sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
5236	}
5237	if (error) {
5238	    while (sc->tulip_rxmaps_free > 0)
5239		bus_dmamap_destroy(sc->tulip_dmatag,
5240				   sc->tulip_rxmaps[--sc->tulip_rxmaps_free]);
5241	}
5242    }
5243#else
5244    if (error == 0) {
5245	sc->tulip_rxdescs = (tulip_desc_t *) malloc(TULIP_RXDESCS * sizeof(tulip_desc_t), M_DEVBUF, M_NOWAIT);
5246	if (sc->tulip_rxdescs == NULL)
5247	    error = ENOMEM;
5248    }
5249#endif
5250    return error;
5251}
5252#endif /* TULIP_BUS_DMA */
5253
5254static void
5255tulip_initcsrs(
5256    tulip_softc_t * const sc,
5257    tulip_csrptr_t csr_base,
5258    size_t csr_size)
5259{
5260    sc->tulip_csrs.csr_busmode		= csr_base +  0 * csr_size;
5261    sc->tulip_csrs.csr_txpoll		= csr_base +  1 * csr_size;
5262    sc->tulip_csrs.csr_rxpoll		= csr_base +  2 * csr_size;
5263    sc->tulip_csrs.csr_rxlist		= csr_base +  3 * csr_size;
5264    sc->tulip_csrs.csr_txlist		= csr_base +  4 * csr_size;
5265    sc->tulip_csrs.csr_status		= csr_base +  5 * csr_size;
5266    sc->tulip_csrs.csr_command		= csr_base +  6 * csr_size;
5267    sc->tulip_csrs.csr_intr		= csr_base +  7 * csr_size;
5268    sc->tulip_csrs.csr_missed_frames	= csr_base +  8 * csr_size;
5269    sc->tulip_csrs.csr_9		= csr_base +  9 * csr_size;
5270    sc->tulip_csrs.csr_10		= csr_base + 10 * csr_size;
5271    sc->tulip_csrs.csr_11		= csr_base + 11 * csr_size;
5272    sc->tulip_csrs.csr_12		= csr_base + 12 * csr_size;
5273    sc->tulip_csrs.csr_13		= csr_base + 13 * csr_size;
5274    sc->tulip_csrs.csr_14		= csr_base + 14 * csr_size;
5275    sc->tulip_csrs.csr_15		= csr_base + 15 * csr_size;
5276#if defined(TULIP_EISA)
5277    sc->tulip_csrs.csr_enetrom		= csr_base + DE425_ENETROM_OFFSET;
5278#endif
5279}
5280
5281static void
5282tulip_initring(
5283    tulip_softc_t * const sc,
5284    tulip_ringinfo_t * const ri,
5285    tulip_desc_t *descs,
5286    int ndescs)
5287{
5288    ri->ri_max = ndescs;
5289    ri->ri_first = descs;
5290    ri->ri_last = ri->ri_first + ri->ri_max;
5291    bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
5292    ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
5293}
5294
5295/*
5296 * This is the PCI configuration support.  Since the 21040 is available
5297 * on both EISA and PCI boards, one must be careful in how defines the
5298 * 21040 in the config file.
5299 */
5300
5301#define	PCI_CFID	0x00	/* Configuration ID */
5302#define	PCI_CFCS	0x04	/* Configurtion Command/Status */
5303#define	PCI_CFRV	0x08	/* Configuration Revision */
5304#define	PCI_CFLT	0x0c	/* Configuration Latency Timer */
5305#define	PCI_CBIO	0x10	/* Configuration Base IO Address */
5306#define	PCI_CBMA	0x14	/* Configuration Base Memory Address */
5307#define	PCI_CFIT	0x3c	/* Configuration Interrupt */
5308#define	PCI_CFDA	0x40	/* Configuration Driver Area */
5309
5310#if defined(TULIP_EISA)
5311static const int tulip_eisa_irqs[4] = { IRQ5, IRQ9, IRQ10, IRQ11 };
5312#endif
5313
5314#if defined(__FreeBSD__)
5315
5316#define	TULIP_PCI_ATTACH_ARGS	pcici_t config_id, int unit
5317#define	TULIP_SHUTDOWN_ARGS	int howto, void * arg
5318
5319#if defined(TULIP_DEVCONF)
5320static void tulip_shutdown(TULIP_SHUTDOWN_ARGS);
5321
5322static int
5323tulip_pci_shutdown(
5324    struct kern_devconf * const kdc,
5325    int force)
5326{
5327    if (kdc->kdc_unit < TULIP_MAX_DEVICES) {
5328	tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(kdc->kdc_unit);
5329	if (sc != NULL)
5330	    tulip_shutdown(0, sc);
5331    }
5332    (void) dev_detach(kdc);
5333    return 0;
5334}
5335#endif
5336
5337static const char*
5338tulip_pci_probe(
5339    pcici_t config_id,
5340    pcidi_t device_id)
5341{
5342    if (PCI_VENDORID(device_id) != DEC_VENDORID)
5343	return NULL;
5344    if (PCI_CHIPID(device_id) == CHIPID_21040)
5345	return "Digital 21040 Ethernet";
5346    if (PCI_CHIPID(device_id) == CHIPID_21041)
5347	return "Digital 21041 Ethernet";
5348    if (PCI_CHIPID(device_id) == CHIPID_21140) {
5349	u_int32_t revinfo = pci_conf_read(config_id, PCI_CFRV) & 0xFF;
5350	if (revinfo >= 0x20)
5351	    return "Digital 21140A Fast Ethernet";
5352	else
5353	    return "Digital 21140 Fast Ethernet";
5354    }
5355    if (PCI_CHIPID(device_id) == CHIPID_21142) {
5356	u_int32_t revinfo = pci_conf_read(config_id, PCI_CFRV) & 0xFF;
5357	if (revinfo >= 0x20)
5358	    return "Digital 21143 Fast Ethernet";
5359	else
5360	    return "Digital 21142 Fast Ethernet";
5361    }
5362    return NULL;
5363}
5364
5365static void  tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
5366static u_long tulip_pci_count;
5367
5368static struct pci_device dedevice = {
5369    "de",
5370    tulip_pci_probe,
5371    tulip_pci_attach,
5372   &tulip_pci_count,
5373#if defined(TULIP_DEVCONF)
5374    tulip_pci_shutdown,
5375#endif
5376};
5377
5378COMPAT_PCI_DRIVER(de, dedevice);
5379#endif /* __FreeBSD__ */
5380
5381#if defined(__bsdi__)
5382#define	TULIP_PCI_ATTACH_ARGS	struct device * const parent, struct device * const self, void * const aux
5383#define	TULIP_SHUTDOWN_ARGS	void *arg
5384
5385static int
5386tulip_pci_match(
5387    pci_devaddr_t *pa)
5388{
5389    int irq;
5390    unsigned id;
5391
5392    id = pci_inl(pa, PCI_VENDOR_ID);
5393    if (PCI_VENDORID(id) != DEC_VENDORID)
5394	return 0;
5395    id = PCI_CHIPID(id);
5396    if (id != CHIPID_21040 && id != CHIPID_21041
5397	    && id != CHIPID_21140 && id != CHIPID_21142)
5398	return 0;
5399    irq = pci_inl(pa, PCI_I_LINE) & 0xFF;
5400    if (irq == 0 || irq >= 16) {
5401	printf("de?: invalid IRQ %d; skipping\n", irq);
5402	return 0;
5403    }
5404    return 1;
5405}
5406
5407static int
5408tulip_probe(
5409    struct device *parent,
5410    struct cfdata *cf,
5411    void *aux)
5412{
5413    struct isa_attach_args * const ia = (struct isa_attach_args *) aux;
5414    unsigned irq, slot;
5415    pci_devaddr_t *pa;
5416
5417#if _BSDI_VERSION >= 199401
5418    switch (ia->ia_bustype) {
5419    case BUS_PCI:
5420#endif
5421	pa = pci_scan(tulip_pci_match);
5422	if (pa == NULL)
5423	    return 0;
5424
5425	irq = (1 << (pci_inl(pa, PCI_I_LINE) & 0xFF));
5426
5427	/* Get the base address; assume the BIOS set it up correctly */
5428#if defined(TULIP_IOMAPPED)
5429	ia->ia_maddr = NULL;
5430	ia->ia_msize = 0;
5431	ia->ia_iobase = pci_inl(pa, PCI_CBIO) & ~7;
5432	pci_outl(pa, PCI_CBIO, 0xFFFFFFFF);
5433	ia->ia_iosize = ((~pci_inl(pa, PCI_CBIO)) | 7) + 1;
5434	pci_outl(pa, PCI_CBIO, (int) ia->ia_iobase);
5435
5436	/* Disable memory space access */
5437	pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~2);
5438#else
5439	ia->ia_maddr = (caddr_t) (pci_inl(pa, PCI_CBMA) & ~7);
5440	pci_outl(pa, PCI_CBMA, 0xFFFFFFFF);
5441	ia->ia_msize = ((~pci_inl(pa, PCI_CBMA)) | 7) + 1;
5442	pci_outl(pa, PCI_CBMA, (int) ia->ia_maddr);
5443	ia->ia_iobase = 0;
5444	ia->ia_iosize = 0;
5445
5446	/* Disable I/O space access */
5447	pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~1);
5448#endif /* TULIP_IOMAPPED */
5449
5450	ia->ia_aux = (void *) pa;
5451#if _BSDI_VERSION >= 199401
5452	break;
5453
5454#if defined(TULIP_EISA)
5455    case BUS_EISA: {
5456	unsigned tmp;
5457
5458	if ((slot = eisa_match(cf, ia)) == 0)
5459	    return 0;
5460	ia->ia_iobase = slot << 12;
5461	ia->ia_iosize = EISA_NPORT;
5462	eisa_slotalloc(slot);
5463	tmp = inb(ia->ia_iobase + DE425_CFG0);
5464	irq = tulip_eisa_irqs[(tmp >> 1) & 0x03];
5465	/*
5466	 * Until BSD/OS likes level interrupts, force
5467	 * the DE425 into edge-triggered mode.
5468	 */
5469	if ((tmp & 1) == 0)
5470	    outb(ia->ia_iobase + DE425_CFG0, tmp | 1);
5471	/*
5472	 * CBIO needs to map to the EISA slot
5473	 * enable I/O access and Master
5474	 */
5475	outl(ia->ia_iobase + DE425_CBIO, ia->ia_iobase);
5476	outl(ia->ia_iobase + DE425_CFCS, 5 | inl(ia->ia_iobase + DE425_CFCS));
5477	ia->ia_aux = NULL;
5478	break;
5479    }
5480#endif /* TULIP_EISA */
5481    default:
5482	return 0;
5483    }
5484#endif
5485
5486    /* PCI bus masters don't use host DMA channels */
5487    ia->ia_drq = DRQNONE;
5488
5489    if (ia->ia_irq != IRQUNK && irq != ia->ia_irq) {
5490	printf("de%d: error: desired IRQ of %d does not match device's "
5491	    "actual IRQ of %d,\n",
5492	       cf->cf_unit,
5493	       ffs(ia->ia_irq) - 1, ffs(irq) - 1);
5494	return 0;
5495    }
5496    if (ia->ia_irq == IRQUNK)
5497	ia->ia_irq = irq;
5498#ifdef IRQSHARE
5499    ia->ia_irq |= IRQSHARE;
5500#endif
5501    return 1;
5502}
5503
5504static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
5505
5506#if defined(TULIP_EISA)
5507static char *tulip_eisa_ids[] = {
5508    "DEC4250",
5509    NULL
5510};
5511#endif
5512
5513struct cfdriver decd = {
5514    0, "de", tulip_probe, tulip_pci_attach,
5515#if _BSDI_VERSION >= 199401
5516    DV_IFNET,
5517#endif
5518    sizeof(tulip_softc_t),
5519#if defined(TULIP_EISA)
5520    tulip_eisa_ids
5521#endif
5522};
5523
5524#endif /* __bsdi__ */
5525
5526#if defined(__NetBSD__)
5527#define	TULIP_PCI_ATTACH_ARGS	struct device * const parent, struct device * const self, void * const aux
5528#define	TULIP_SHUTDOWN_ARGS	void *arg
5529static int
5530tulip_pci_probe(
5531    struct device *parent,
5532    struct cfdata *match,
5533    void *aux)
5534{
5535    struct pci_attach_args *pa = (struct pci_attach_args *) aux;
5536
5537    if (PCI_VENDORID(pa->pa_id) != DEC_VENDORID)
5538	return 0;
5539    if (PCI_CHIPID(pa->pa_id) == CHIPID_21040
5540	    || PCI_CHIPID(pa->pa_id) == CHIPID_21041
5541	    || PCI_CHIPID(pa->pa_id) == CHIPID_21140
5542	    || PCI_CHIPID(pa->pa_id) == CHIPID_21142)
5543	return 1;
5544
5545    return 0;
5546}
5547
5548static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
5549
5550struct cfattach de_ca = {
5551    sizeof(tulip_softc_t), tulip_pci_probe, tulip_pci_attach
5552};
5553
5554#endif /* __NetBSD__ */
5555
5556static void
5557tulip_shutdown(
5558    TULIP_SHUTDOWN_ARGS)
5559{
5560    tulip_softc_t * const sc = arg;
5561    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
5562    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
5563		   33MHz that comes to two microseconds but wait a
5564		   bit longer anyways) */
5565}
5566
5567static void
5568tulip_pci_attach(
5569    TULIP_PCI_ATTACH_ARGS)
5570{
5571#if defined(__FreeBSD__)
5572    tulip_softc_t *sc;
5573#define	PCI_CONF_WRITE(r, v)	pci_conf_write(config_id, (r), (v))
5574#define	PCI_CONF_READ(r)	pci_conf_read(config_id, (r))
5575#if __FreeBSD_version >= 300000
5576#define	PCI_GETBUSDEVINFO(sc)	((void)((sc)->tulip_pci_busno = (config_id->bus), /* XXX */ \
5577					(sc)->tulip_pci_devno = (config_id->slot))) /* XXX */
5578#else
5579#define	PCI_GETBUSDEVINFO(sc)	((void)((sc)->tulip_pci_busno = ((config_id.cfg1 >> 16) & 0xFF), /* XXX */ \
5580					(sc)->tulip_pci_devno = ((config_id.cfg1 >> 11) & 0x1F))) /* XXX */
5581#endif
5582#endif
5583#if defined(__bsdi__)
5584    tulip_softc_t * const sc = (tulip_softc_t *) self;
5585    struct isa_attach_args * const ia = (struct isa_attach_args *) aux;
5586    pci_devaddr_t *pa = (pci_devaddr_t *) ia->ia_aux;
5587    const int unit = sc->tulip_dev.dv_unit;
5588#define	PCI_CONF_WRITE(r, v)	pci_outl(pa, (r), (v))
5589#define	PCI_CONF_READ(r)	pci_inl(pa, (r))
5590#define	PCI_GETBUSDEVINFO(sc)	((void)((sc)->tulip_pci_busno = pa->d_bus, \
5591					(sc)->tulip_pci_devno = pa->d_agent))
5592#endif
5593#if defined(__NetBSD__)
5594    tulip_softc_t * const sc = (tulip_softc_t *) self;
5595    struct pci_attach_args * const pa = (struct pci_attach_args *) aux;
5596    const int unit = sc->tulip_dev.dv_unit;
5597#define	PCI_CONF_WRITE(r, v)	pci_conf_write(pa->pa_pc, pa->pa_tag, (r), (v))
5598#define	PCI_CONF_READ(r)	pci_conf_read(pa->pa_pc, pa->pa_tag, (r))
5599#define	PCI_GETBUSDEVINFO(sc)	do { \
5600	(sc)->tulip_pci_busno = parent; \
5601	(sc)->tulip_pci_devno = pa->pa_device; \
5602    } while (0)
5603#endif /* __NetBSD__ */
5604#if defined(__alpha__)
5605    tulip_media_t media = TULIP_MEDIA_UNKNOWN;
5606#endif
5607    int retval, idx;
5608    u_int32_t revinfo, cfdainfo, id;
5609#if !defined(TULIP_IOMAPPED) && defined(__FreeBSD__)
5610    vm_offset_t pa_csrs;
5611#endif
5612    unsigned csroffset = TULIP_PCI_CSROFFSET;
5613    unsigned csrsize = TULIP_PCI_CSRSIZE;
5614    tulip_csrptr_t csr_base;
5615    tulip_chipid_t chipid = TULIP_CHIPID_UNKNOWN;
5616
5617    if (unit >= TULIP_MAX_DEVICES) {
5618#ifdef __FreeBSD__
5619	printf("de%d", unit);
5620#endif
5621	printf(": not configured; limit of %d reached or exceeded\n",
5622	       TULIP_MAX_DEVICES);
5623	return;
5624    }
5625
5626#if defined(__bsdi__)
5627    if (pa != NULL) {
5628	revinfo = pci_inl(pa, PCI_CFRV) & 0xFF;
5629	id = pci_inl(pa, PCI_CFID);
5630	cfdainfo = pci_inl(pa, PCI_CFDA);
5631#if defined(TULIP_EISA)
5632    } else {
5633	revinfo = inl(ia->ia_iobase + DE425_CFRV) & 0xFF;
5634	csroffset = TULIP_EISA_CSROFFSET;
5635	csrsize = TULIP_EISA_CSRSIZE;
5636	chipid = TULIP_DE425;
5637	cfdainfo = 0;
5638#endif /* TULIP_EISA */
5639    }
5640#else /* __bsdi__ */
5641    revinfo  = PCI_CONF_READ(PCI_CFRV) & 0xFF;
5642    id       = PCI_CONF_READ(PCI_CFID);
5643    cfdainfo = PCI_CONF_READ(PCI_CFDA);
5644#endif /* __bsdi__ */
5645
5646    if (PCI_VENDORID(id) == DEC_VENDORID) {
5647	if (PCI_CHIPID(id) == CHIPID_21040)
5648		chipid = TULIP_21040;
5649	else if (PCI_CHIPID(id) == CHIPID_21041)
5650		chipid = TULIP_21041;
5651	else if (PCI_CHIPID(id) == CHIPID_21140)
5652		chipid = (revinfo >= 0x20) ? TULIP_21140A : TULIP_21140;
5653	else if (PCI_CHIPID(id) == CHIPID_21142)
5654		chipid = (revinfo >= 0x20) ? TULIP_21143 : TULIP_21142;
5655    }
5656    if (chipid == TULIP_CHIPID_UNKNOWN)
5657	return;
5658
5659    if ((chipid == TULIP_21040 || chipid == TULIP_DE425) && revinfo < 0x20) {
5660#ifdef __FreeBSD__
5661	printf("de%d", unit);
5662#endif
5663	printf(": not configured; 21040 pass 2.0 required (%d.%d found)\n",
5664	       revinfo >> 4, revinfo & 0x0f);
5665	return;
5666    } else if (chipid == TULIP_21140 && revinfo < 0x11) {
5667#ifndef __FreeBSD__
5668	printf("\n");
5669#endif
5670	printf("de%d: not configured; 21140 pass 1.1 required (%d.%d found)\n",
5671	       unit, revinfo >> 4, revinfo & 0x0f);
5672	return;
5673    }
5674
5675#if defined(__FreeBSD__)
5676    sc = (tulip_softc_t *) malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
5677    if (sc == NULL)
5678	return;
5679    bzero(sc, sizeof(*sc));				/* Zero out the softc*/
5680#endif
5681
5682    PCI_GETBUSDEVINFO(sc);
5683    sc->tulip_chipid = chipid;
5684    sc->tulip_flags |= TULIP_DEVICEPROBE;
5685    if (chipid == TULIP_21140 || chipid == TULIP_21140A)
5686	sc->tulip_features |= TULIP_HAVE_GPR|TULIP_HAVE_STOREFWD;
5687    if (chipid == TULIP_21140A && revinfo <= 0x22)
5688	sc->tulip_features |= TULIP_HAVE_RXBADOVRFLW;
5689    if (chipid == TULIP_21140)
5690	sc->tulip_features |= TULIP_HAVE_BROKEN_HASH;
5691    if (chipid != TULIP_21040 && chipid != TULIP_DE425 && chipid != TULIP_21140)
5692	sc->tulip_features |= TULIP_HAVE_POWERMGMT;
5693    if (chipid == TULIP_21041 || chipid == TULIP_21142 || chipid == TULIP_21143) {
5694	sc->tulip_features |= TULIP_HAVE_DUALSENSE;
5695	if (chipid != TULIP_21041 || revinfo >= 0x20)
5696	    sc->tulip_features |= TULIP_HAVE_SIANWAY;
5697	if (chipid != TULIP_21041)
5698	    sc->tulip_features |= TULIP_HAVE_SIAGP|TULIP_HAVE_RXBADOVRFLW|TULIP_HAVE_STOREFWD;
5699	if (chipid != TULIP_21041 && revinfo >= 0x20)
5700	    sc->tulip_features |= TULIP_HAVE_SIA100;
5701    }
5702
5703    if (sc->tulip_features & TULIP_HAVE_POWERMGMT
5704	    && (cfdainfo & (TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE))) {
5705	cfdainfo &= ~(TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE);
5706	PCI_CONF_WRITE(PCI_CFDA, cfdainfo);
5707	DELAY(11*1000);
5708    }
5709#if defined(__alpha__)
5710    /*
5711     * The Alpha SRM console encodes a console set media in the driver
5712     * part of the CFDA register.  Note that the Multia presents a
5713     * problem in that its BNC mode is really EXTSIA.  So in that case
5714     * force a probe.
5715     */
5716    switch ((cfdainfo >> 8) & 0xff) {
5717	case 1: media = chipid > TULIP_DE425 ? TULIP_MEDIA_AUI : TULIP_MEDIA_AUIBNC; break;
5718	case 2: media = chipid > TULIP_DE425 ? TULIP_MEDIA_BNC : TULIP_MEDIA_UNKNOWN; break;
5719	case 3: media = TULIP_MEDIA_10BASET; break;
5720	case 4: media = TULIP_MEDIA_10BASET_FD; break;
5721	case 5: media = TULIP_MEDIA_100BASETX; break;
5722	case 6: media = TULIP_MEDIA_100BASETX_FD; break;
5723	default: media = TULIP_MEDIA_UNKNOWN; break;
5724    }
5725#endif
5726
5727#if defined(__NetBSD__)
5728    bcopy(self->dv_xname, sc->tulip_if.if_xname, IFNAMSIZ);
5729    sc->tulip_if.if_softc = sc;
5730    sc->tulip_pc = pa->pa_pc;
5731#if defined(TULIP_BUS_DMA)
5732    sc->tulip_dmatag = pa->pa_dmat;
5733#endif
5734#else
5735    sc->tulip_unit = unit;
5736    sc->tulip_name = "de";
5737#endif
5738    sc->tulip_revinfo = revinfo;
5739#if defined(__FreeBSD__)
5740#if BSD >= 199506
5741    sc->tulip_if.if_softc = sc;
5742#endif
5743#if defined(TULIP_IOMAPPED)
5744    retval = pci_map_port(config_id, PCI_CBIO, &csr_base);
5745#else
5746    retval = pci_map_mem(config_id, PCI_CBMA, (vm_offset_t *) &csr_base, &pa_csrs);
5747#endif
5748    if (!retval) {
5749	free((caddr_t) sc, M_DEVBUF);
5750	return;
5751    }
5752    tulips[unit] = sc;
5753#endif /* __FreeBSD__ */
5754
5755#if defined(__bsdi__)
5756    sc->tulip_pf = printf;
5757#if defined(TULIP_IOMAPPED)
5758    csr_base = ia->ia_iobase;
5759#else
5760    csr_base = (vm_offset_t) mapphys((vm_offset_t) ia->ia_maddr, ia->ia_msize);
5761#endif
5762#endif /* __bsdi__ */
5763
5764#if defined(__NetBSD__)
5765    csr_base = 0;
5766    {
5767	bus_space_tag_t iot, memt;
5768	bus_space_handle_t ioh, memh;
5769	int ioh_valid, memh_valid;
5770
5771	ioh_valid = (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
5772				    &iot, &ioh, NULL, NULL) == 0);
5773	memh_valid = (pci_mapreg_map(pa, PCI_CBMA,
5774				     PCI_MAPREG_TYPE_MEM |
5775				     PCI_MAPREG_MEM_TYPE_32BIT,
5776				     0, &memt, &memh, NULL, NULL) == 0);
5777	if (memh_valid) {
5778	    sc->tulip_bustag = memt;
5779	    sc->tulip_bushandle = memh;
5780	} else if (ioh_valid) {
5781	    sc->tulip_bustag = iot;
5782	    sc->tulip_bushandle = ioh;
5783	} else {
5784	    printf(": unable to map device registers\n");
5785	    return;
5786	}
5787    }
5788#endif /* __NetBSD__ */
5789
5790    tulip_initcsrs(sc, csr_base + csroffset, csrsize);
5791
5792#if defined(TULIP_BUS_DMA)
5793    if ((retval = tulip_busdma_init(sc)) != 0) {
5794	printf("error initing bus_dma: %d\n", retval);
5795	return;
5796    }
5797#else
5798#if defined(__FreeBSD__)
5799    sc->tulip_rxdescs = (tulip_desc_t *) malloc(sizeof(tulip_desc_t) * TULIP_RXDESCS, M_DEVBUF, M_NOWAIT);
5800    sc->tulip_txdescs = (tulip_desc_t *) malloc(sizeof(tulip_desc_t) * TULIP_TXDESCS, M_DEVBUF, M_NOWAIT);
5801    if (sc->tulip_rxdescs == NULL || sc->tulip_txdescs == NULL) {
5802	printf("malloc failed\n");
5803	if (sc->tulip_rxdescs)
5804	    free((caddr_t) sc->tulip_rxdescs, M_DEVBUF);
5805	if (sc->tulip_txdescs)
5806	    free((caddr_t) sc->tulip_txdescs, M_DEVBUF);
5807	free((caddr_t) sc, M_DEVBUF);
5808	return;
5809    }
5810#else
5811    sc->tulip_txdescs = (tulip_desc_t *) malloc((TULIP_TXDESCS+TULIP_RXDESCS)*sizeof(tulip_desc_t), M_DEVBUF, M_WAITOK);
5812    sc->tulip_rxdescs = sc->tulip_txdescs + TULIP_TXDESCS;
5813#endif
5814#endif
5815
5816    tulip_initring(sc, &sc->tulip_rxinfo, sc->tulip_rxdescs, TULIP_RXDESCS);
5817    tulip_initring(sc, &sc->tulip_txinfo, sc->tulip_txdescs, TULIP_TXDESCS);
5818
5819    /*
5820     * Make sure there won't be any interrupts or such...
5821     */
5822    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
5823    DELAY(100);	/* Wait 10 microseconds (actually 50 PCI cycles but at
5824		   33MHz that comes to two microseconds but wait a
5825		   bit longer anyways) */
5826
5827    if ((retval = tulip_read_macaddr(sc)) < 0) {
5828#if defined(__FreeBSD__)
5829	printf(TULIP_PRINTF_FMT, TULIP_PRINTF_ARGS);
5830#endif
5831	printf(": can't read ENET ROM (why=%d) (", retval);
5832	for (idx = 0; idx < 32; idx++)
5833	    printf("%02x", sc->tulip_rombuf[idx]);
5834	printf("\n");
5835	printf(TULIP_PRINTF_FMT ": %s%s pass %d.%d\n",
5836	       TULIP_PRINTF_ARGS,
5837	       sc->tulip_boardid, tulip_chipdescs[sc->tulip_chipid],
5838	       (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F);
5839	printf(TULIP_PRINTF_FMT ": address unknown\n", TULIP_PRINTF_ARGS);
5840    } else {
5841	tulip_spl_t s;
5842	tulip_intrfunc_t (*intr_rtn)(void *) = tulip_intr_normal;
5843
5844	if (sc->tulip_features & TULIP_HAVE_SHAREDINTR)
5845	    intr_rtn = tulip_intr_shared;
5846
5847#if defined(__NetBSD__)
5848	if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
5849	    pci_intr_handle_t intrhandle;
5850	    const char *intrstr;
5851
5852	    printf("\n");
5853
5854	    if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
5855			     pa->pa_intrline, &intrhandle)) {
5856		printf("%s: couldn't map interrupt\n", sc->tulip_dev.dv_xname);
5857		return;
5858	    }
5859	    intrstr = pci_intr_string(pa->pa_pc, intrhandle);
5860	    sc->tulip_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
5861					      intr_rtn, sc);
5862	    if (sc->tulip_ih == NULL) {
5863		printf("%s: couldn't establish interrupt",
5864		       sc->tulip_dev.dv_xname);
5865		if (intrstr != NULL)
5866		    printf(" at %s", intrstr);
5867		printf("\n");
5868		return;
5869	    }
5870	    printf("%s: interrupting at %s\n", sc->tulip_dev.dv_xname, intrstr);
5871	}
5872	sc->tulip_ats = shutdownhook_establish(tulip_shutdown, sc);
5873	if (sc->tulip_ats == NULL)
5874	    printf("%s: warning: couldn't establish shutdown hook\n",
5875		   sc->tulip_xname);
5876#endif
5877#if defined(__FreeBSD__)
5878	if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
5879	    if (!pci_map_int (config_id, intr_rtn, (void*) sc, &net_imask)) {
5880		printf(TULIP_PRINTF_FMT ": couldn't map interrupt\n",
5881		       TULIP_PRINTF_ARGS);
5882		free((caddr_t) sc->tulip_rxdescs, M_DEVBUF);
5883		free((caddr_t) sc->tulip_txdescs, M_DEVBUF);
5884		free((caddr_t) sc, M_DEVBUF);
5885		return;
5886	    }
5887	}
5888#if !defined(TULIP_DEVCONF)
5889	at_shutdown(tulip_shutdown, sc, SHUTDOWN_POST_SYNC);
5890#endif
5891#endif
5892#if defined(__bsdi__)
5893	if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
5894	    isa_establish(&sc->tulip_id, &sc->tulip_dev);
5895
5896	    sc->tulip_ih.ih_fun = intr_rtn;
5897	    sc->tulip_ih.ih_arg = (void *) sc;
5898	    intr_establish(ia->ia_irq, &sc->tulip_ih, DV_NET);
5899	}
5900
5901	sc->tulip_ats.func = tulip_shutdown;
5902	sc->tulip_ats.arg = (void *) sc;
5903	atshutdown(&sc->tulip_ats, ATSH_ADD);
5904#endif
5905#if defined(TULIP_USE_SOFTINTR)
5906	if (sc->tulip_unit > tulip_softintr_max_unit)
5907	    tulip_softintr_max_unit = sc->tulip_unit;
5908#endif
5909
5910	s = TULIP_RAISESPL();
5911#if defined(__alpha__)
5912	sc->tulip_media = media;
5913#endif
5914	tulip_attach(sc);
5915#if defined(__alpha__)
5916	if (sc->tulip_media != TULIP_MEDIA_UNKNOWN)
5917		tulip_linkup(sc, media);
5918#endif
5919	TULIP_RESTORESPL(s);
5920    }
5921}
5922