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