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