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