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