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