xref: /dragonfly/sys/dev/netif/de/if_de.c (revision 066b6da2)
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 
5 /*-
6  * Copyright (c) 1994-1997 Matt Thomas (matt@3am-software.com)
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Id: if_de.c,v 1.94 1997/07/03 16:55:07 thomas Exp
29  *
30  */
31 
32 /*
33  * DEC 21040 PCI Ethernet Controller
34  *
35  * Written by Matt Thomas
36  * BPF support code stolen directly from if_ec.c
37  *
38  *   This driver supports the DEC DE435 or any other PCI
39  *   board which support 21040, 21041, or 21140 (mostly).
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/eventhandler.h>
50 #include <sys/bus.h>
51 #include <sys/rman.h>
52 #include <sys/thread2.h>
53 #include <sys/interrupt.h>
54 
55 #include "opt_inet.h"
56 
57 #include <net/if.h>
58 #include <net/if_media.h>
59 #include <net/if_dl.h>
60 #include <net/ifq_var.h>
61 #include <net/bpf.h>
62 
63 #ifdef INET
64 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
66 #endif
67 
68 #include <vm/vm.h>
69 
70 #include <net/if_var.h>
71 #include <vm/pmap.h>
72 #include <bus/pci/pcivar.h>
73 #include <bus/pci/pcireg.h>
74 #include <dev/netif/de/dc21040reg.h>
75 
76 /*
77  * Intel CPUs should use I/O mapped access.
78  */
79 #if defined(__i386__)
80 #define	TULIP_IOMAPPED
81 #endif
82 
83 #define	TULIP_HZ	10
84 
85 #include "if_devar.h"
86 
87 static tulip_softc_t *tulips[TULIP_MAX_DEVICES];
88 
89 /*
90  * This module supports
91  *	the DEC 21040 PCI Ethernet Controller.
92  *	the DEC 21041 PCI Ethernet Controller.
93  *	the DEC 21140 PCI Fast Ethernet Controller.
94  */
95 static void	tulip_mii_autonegotiate(tulip_softc_t *, u_int);
96 static void	tulip_intr_shared(void *);
97 static void	tulip_intr_normal(void *);
98 static void	tulip_init(tulip_softc_t *);
99 static void	tulip_reset(tulip_softc_t *);
100 static void	tulip_ifstart(struct ifnet *, struct ifaltq_subque *);
101 static struct mbuf *tulip_txput(tulip_softc_t *, struct mbuf *);
102 static void	tulip_txput_setup(tulip_softc_t *);
103 static void	tulip_rx_intr(tulip_softc_t *);
104 static void	tulip_addr_filter(tulip_softc_t *);
105 static u_int	tulip_mii_readreg(tulip_softc_t *, u_int, u_int);
106 static void	tulip_mii_writereg(tulip_softc_t *, u_int, u_int, u_int);
107 static int	tulip_mii_map_abilities(tulip_softc_t * const sc, unsigned abilities);
108 static tulip_media_t tulip_mii_phy_readspecific(tulip_softc_t *);
109 static int	tulip_srom_decode(tulip_softc_t *);
110 static int	tulip_ifmedia_change(struct ifnet *);
111 static void	tulip_ifmedia_status(struct ifnet *, struct ifmediareq *);
112 /* static void tulip_21140_map_media(tulip_softc_t *sc); */
113 
114 static void
115 tulip_timeout_callback(void *arg)
116 {
117     tulip_softc_t *sc = arg;
118 
119     lwkt_serialize_enter(sc->tulip_if.if_serializer);
120     sc->tulip_flags &= ~TULIP_TIMEOUTPENDING;
121     sc->tulip_probe_timeout -= 1000 / TULIP_HZ;
122     (sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_TIMER);
123     lwkt_serialize_exit(sc->tulip_if.if_serializer);
124 }
125 
126 static void
127 tulip_timeout(tulip_softc_t *sc)
128 {
129     if (sc->tulip_flags & TULIP_TIMEOUTPENDING)
130 	return;
131     sc->tulip_flags |= TULIP_TIMEOUTPENDING;
132     callout_reset(&sc->tulip_timer, (hz + TULIP_HZ / 2) / TULIP_HZ,
133     	    tulip_timeout_callback, sc);
134 }
135 
136 static int
137 tulip_txprobe(tulip_softc_t *sc)
138 {
139     struct mbuf *m;
140 
141     /*
142      * Before we are sure this is the right media we need
143      * to send a small packet to make sure there's carrier.
144      * Strangely, BNC and AUI will "see" receive data if
145      * either is connected so the transmit is the only way
146      * to verify the connectivity.
147      */
148     MGETHDR(m, MB_DONTWAIT, MT_DATA);
149     if (m == NULL)
150 	return 0;
151     /*
152      * Construct a LLC TEST message which will point to ourselves.
153      */
154     bcopy(sc->tulip_enaddr, mtod(m, struct ether_header *)->ether_dhost, 6);
155     bcopy(sc->tulip_enaddr, mtod(m, struct ether_header *)->ether_shost, 6);
156     mtod(m, struct ether_header *)->ether_type = htons(3);
157     mtod(m, unsigned char *)[14] = 0;
158     mtod(m, unsigned char *)[15] = 0;
159     mtod(m, unsigned char *)[16] = 0xE3;	/* LLC Class1 TEST (no poll) */
160     m->m_len = m->m_pkthdr.len = sizeof(struct ether_header) + 3;
161     /*
162      * send it!
163      */
164     sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
165     sc->tulip_intrmask |= TULIP_STS_TXINTR;
166     sc->tulip_flags |= TULIP_TXPROBE_ACTIVE;
167     TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
168     TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
169     if ((m = tulip_txput(sc, m)) != NULL)
170 	m_freem(m);
171     sc->tulip_probe.probe_txprobes++;
172     return 1;
173 }
174 
175 #ifdef BIG_PACKET
176 #define TULIP_SIAGEN_WATCHDOG	(sc->tulip_if.if_mtu > ETHERMTU ? TULIP_WATCHDOG_RXDISABLE|TULIP_WATCHDOG_TXDISABLE : 0)
177 #else
178 #define	TULIP_SIAGEN_WATCHDOG	0
179 #endif
180 
181 static void
182 tulip_media_set(tulip_softc_t *sc, tulip_media_t media)
183 {
184     const tulip_media_info_t *mi = sc->tulip_mediums[media];
185 
186     if (mi == NULL)
187 	return;
188 
189     /*
190      * If we are switching media, make sure we don't think there's
191      * any stale RX activity
192      */
193     sc->tulip_flags &= ~TULIP_RXACT;
194     if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
195 	TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
196 	TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        mi->mi_sia_tx_rx);
197 	if (sc->tulip_features & TULIP_HAVE_SIAGP) {
198 	    TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_gp_control|mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
199 	    DELAY(50);
200 	    TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_gp_data|mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
201 	} else {
202 	    TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
203 	}
204 	TULIP_CSR_WRITE(sc, csr_sia_connectivity, mi->mi_sia_connectivity);
205     } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
206 #define	TULIP_GPR_CMDBITS	(TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER|TULIP_CMD_TXTHRSHLDCTL)
207 	/*
208 	 * If the cmdmode bits don't match the currently operating mode,
209 	 * set the cmdmode appropriately and reset the chip.
210 	 */
211 	if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
212 	    sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
213 	    sc->tulip_cmdmode |= mi->mi_cmdmode;
214 	    tulip_reset(sc);
215 	}
216 	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
217 	DELAY(10);
218 	TULIP_CSR_WRITE(sc, csr_gp, (u_int8_t) mi->mi_gpdata);
219     } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
220 	/*
221 	 * If the cmdmode bits don't match the currently operating mode,
222 	 * set the cmdmode appropriately and reset the chip.
223 	 */
224 	if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
225 	    sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
226 	    sc->tulip_cmdmode |= mi->mi_cmdmode;
227 	    tulip_reset(sc);
228 	}
229 	TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpcontrol);
230 	TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpdata);
231     } else if (mi->mi_type == TULIP_MEDIAINFO_MII
232 	       && sc->tulip_probe_state != TULIP_PROBE_INACTIVE) {
233 	int idx;
234 	if (sc->tulip_features & TULIP_HAVE_SIAGP) {
235 	    const u_int8_t *dp;
236 	    dp = &sc->tulip_rombuf[mi->mi_reset_offset];
237 	    for (idx = 0; idx < mi->mi_reset_length; idx++, dp += 2) {
238 		DELAY(10);
239 		TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
240 	    }
241 	    sc->tulip_phyaddr = mi->mi_phyaddr;
242 	    dp = &sc->tulip_rombuf[mi->mi_gpr_offset];
243 	    for (idx = 0; idx < mi->mi_gpr_length; idx++, dp += 2) {
244 		DELAY(10);
245 		TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
246 	    }
247 	} else {
248 	    for (idx = 0; idx < mi->mi_reset_length; idx++) {
249 		DELAY(10);
250 		TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx]);
251 	    }
252 	    sc->tulip_phyaddr = mi->mi_phyaddr;
253 	    for (idx = 0; idx < mi->mi_gpr_length; idx++) {
254 		DELAY(10);
255 		TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx]);
256 	    }
257 	}
258 	if (sc->tulip_flags & TULIP_TRYNWAY) {
259 	    tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
260 	} else if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
261 	    u_int32_t data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_CONTROL);
262 	    data &= ~(PHYCTL_SELECT_100MB|PHYCTL_FULL_DUPLEX|PHYCTL_AUTONEG_ENABLE);
263 	    sc->tulip_flags &= ~TULIP_DIDNWAY;
264 	    if (TULIP_IS_MEDIA_FD(media))
265 		data |= PHYCTL_FULL_DUPLEX;
266 	    if (TULIP_IS_MEDIA_100MB(media))
267 		data |= PHYCTL_SELECT_100MB;
268 	    tulip_mii_writereg(sc, sc->tulip_phyaddr, PHYREG_CONTROL, data);
269 	}
270     }
271 }
272 
273 static void
274 tulip_linkup(tulip_softc_t *sc, tulip_media_t media)
275 {
276     if ((sc->tulip_flags & TULIP_LINKUP) == 0)
277 	sc->tulip_flags |= TULIP_PRINTLINKUP;
278     sc->tulip_flags |= TULIP_LINKUP;
279     ifq_clr_oactive(&sc->tulip_if.if_snd);
280 #if 0 /* XXX how does with work with ifmedia? */
281     if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
282 	if (sc->tulip_if.if_flags & IFF_FULLDUPLEX) {
283 	    if (TULIP_CAN_MEDIA_FD(media)
284 		    && sc->tulip_mediums[TULIP_FD_MEDIA_OF(media)] != NULL)
285 		media = TULIP_FD_MEDIA_OF(media);
286 	} else {
287 	    if (TULIP_IS_MEDIA_FD(media)
288 		    && sc->tulip_mediums[TULIP_HD_MEDIA_OF(media)] != NULL)
289 		media = TULIP_HD_MEDIA_OF(media);
290 	}
291     }
292 #endif
293     if (sc->tulip_media != media) {
294 	sc->tulip_media = media;
295 	sc->tulip_flags |= TULIP_PRINTMEDIA;
296 	if (TULIP_IS_MEDIA_FD(sc->tulip_media)) {
297 	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
298 	} else if (sc->tulip_chipid != TULIP_21041 || (sc->tulip_flags & TULIP_DIDNWAY) == 0) {
299 	    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
300 	}
301     }
302     /*
303      * We could set probe_timeout to 0 but setting to 3000 puts this
304      * in one central place and the only matters is tulip_link is
305      * followed by a tulip_timeout.  Therefore setting it should not
306      * result in aberrant behavour.
307      */
308     sc->tulip_probe_timeout = 3000;
309     sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
310     sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_TRYNWAY);
311     if (sc->tulip_flags & TULIP_INRESET) {
312 	tulip_media_set(sc, sc->tulip_media);
313     } else if (sc->tulip_probe_media != sc->tulip_media) {
314 	/*
315 	 * No reason to change media if we have the right media.
316 	 */
317 	tulip_reset(sc);
318     }
319     tulip_init(sc);
320 }
321 
322 static void
323 tulip_media_print(tulip_softc_t *sc)
324 {
325     if ((sc->tulip_flags & TULIP_LINKUP) == 0)
326 	return;
327     if (sc->tulip_flags & TULIP_PRINTMEDIA) {
328 	if_printf(&sc->tulip_if, "enabling %s port\n",
329 		  tulip_mediums[sc->tulip_media]);
330 	sc->tulip_flags &= ~(TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
331     } else if (sc->tulip_flags & TULIP_PRINTLINKUP) {
332 	if_printf(&sc->tulip_if, "link up\n");
333 	sc->tulip_flags &= ~TULIP_PRINTLINKUP;
334     }
335 }
336 
337 #if defined(TULIP_DO_GPR_SENSE)
338 static tulip_media_t
339 tulip_21140_gpr_media_sense(tulip_softc_t *sc)
340 {
341     tulip_media_t maybe_media = TULIP_MEDIA_UNKNOWN;
342     tulip_media_t last_media = TULIP_MEDIA_UNKNOWN;
343     tulip_media_t media;
344 
345     /*
346      * If one of the media blocks contained a default media flag,
347      * use that.
348      */
349     for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
350 	const tulip_media_info_t *mi;
351 	/*
352 	 * Media is not supported (or is full-duplex).
353 	 */
354 	if ((mi = sc->tulip_mediums[media]) == NULL || TULIP_IS_MEDIA_FD(media))
355 	    continue;
356 	if (mi->mi_type != TULIP_MEDIAINFO_GPR)
357 	    continue;
358 
359 	/*
360 	 * Remember the media is this is the "default" media.
361 	 */
362 	if (mi->mi_default && maybe_media == TULIP_MEDIA_UNKNOWN)
363 	    maybe_media = media;
364 
365 	/*
366 	 * No activity mask?  Can't see if it is active if there's no mask.
367 	 */
368 	if (mi->mi_actmask == 0)
369 	    continue;
370 
371 	/*
372 	 * Does the activity data match?
373 	 */
374 	if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) != mi->mi_actdata)
375 	    continue;
376 
377 	/*
378 	 * It does!  If this is the first media we detected, then
379 	 * remember this media.  If isn't the first, then there were
380 	 * multiple matches which we equate to no match (since we don't
381 	 * which to select (if any).
382 	 */
383 	if (last_media == TULIP_MEDIA_UNKNOWN) {
384 	    last_media = media;
385 	} else if (last_media != media) {
386 	    last_media = TULIP_MEDIA_UNKNOWN;
387 	}
388     }
389     return (last_media != TULIP_MEDIA_UNKNOWN) ? last_media : maybe_media;
390 }
391 #endif /* TULIP_DO_GPR_SENSE */
392 
393 static tulip_link_status_t
394 tulip_media_link_monitor(tulip_softc_t *sc)
395 {
396     const tulip_media_info_t *mi = sc->tulip_mediums[sc->tulip_media];
397     tulip_link_status_t linkup = TULIP_LINK_DOWN;
398 
399     if (mi == NULL) {
400 #if defined(DIAGNOSTIC)
401 	panic("tulip_media_link_monitor: %s: botch at line %d",
402 	      tulip_mediums[sc->tulip_media],__LINE__);
403 #endif
404 	return TULIP_LINK_UNKNOWN;
405     }
406 
407 
408     /*
409      * Have we seen some packets?  If so, the link must be good.
410      */
411     if ((sc->tulip_flags & (TULIP_RXACT|TULIP_LINKUP)) == (TULIP_RXACT|TULIP_LINKUP)) {
412 	sc->tulip_flags &= ~TULIP_RXACT;
413 	sc->tulip_probe_timeout = 3000;
414 	return TULIP_LINK_UP;
415     }
416 
417     sc->tulip_flags &= ~TULIP_RXACT;
418     if (mi->mi_type == TULIP_MEDIAINFO_MII) {
419 	u_int32_t status;
420 	/*
421 	 * Read the PHY status register.
422 	 */
423 	status = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
424 	if (status & PHYSTS_AUTONEG_DONE) {
425 	    /*
426 	     * If the PHY has completed autonegotiation, see the if the
427 	     * remote systems abilities have changed.  If so, upgrade or
428 	     * downgrade as appropriate.
429 	     */
430 	    u_int32_t abilities = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_AUTONEG_ABILITIES);
431 	    abilities = (abilities << 6) & status;
432 	    if (abilities != sc->tulip_abilities) {
433 		if (tulip_mii_map_abilities(sc, abilities)) {
434 		    tulip_linkup(sc, sc->tulip_probe_media);
435 		    return TULIP_LINK_UP;
436 		}
437 		/*
438 		 * if we had selected media because of autonegotiation,
439 		 * we need to probe for the new media.
440 		 */
441 		sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
442 		if (sc->tulip_flags & TULIP_DIDNWAY)
443 		    return TULIP_LINK_DOWN;
444 	    }
445 	}
446 	/*
447 	 * The link is now up.  If was down, say its back up.
448 	 */
449 	if ((status & (PHYSTS_LINK_UP|PHYSTS_REMOTE_FAULT)) == PHYSTS_LINK_UP)
450 	    linkup = TULIP_LINK_UP;
451     } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
452 	/*
453 	 * No activity sensor?  Assume all's well.
454 	 */
455 	if (mi->mi_actmask == 0)
456 	    return TULIP_LINK_UNKNOWN;
457 	/*
458 	 * Does the activity data match?
459 	 */
460 	if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) == mi->mi_actdata)
461 	    linkup = TULIP_LINK_UP;
462     } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
463 	/*
464 	 * Assume non TP ok for now.
465 	 */
466 	if (!TULIP_IS_MEDIA_TP(sc->tulip_media))
467 	    return TULIP_LINK_UNKNOWN;
468 	if ((TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) == 0)
469 	    linkup = TULIP_LINK_UP;
470     } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
471 	return TULIP_LINK_UNKNOWN;
472     }
473     /*
474      * We will wait for 3 seconds until the link goes into suspect mode.
475      */
476     if (sc->tulip_flags & TULIP_LINKUP) {
477 	if (linkup == TULIP_LINK_UP)
478 	    sc->tulip_probe_timeout = 3000;
479 	if (sc->tulip_probe_timeout > 0)
480 	    return TULIP_LINK_UP;
481 
482 	sc->tulip_flags &= ~TULIP_LINKUP;
483 	if_printf(&sc->tulip_if, "link down: cable problem?\n");
484     }
485     return TULIP_LINK_DOWN;
486 }
487 
488 static void
489 tulip_media_poll(tulip_softc_t *sc, tulip_mediapoll_event_t event)
490 {
491     if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE
492 	    && event == TULIP_MEDIAPOLL_TIMER) {
493 	switch (tulip_media_link_monitor(sc)) {
494 	    case TULIP_LINK_DOWN: {
495 		/*
496 		 * Link Monitor failed.  Probe for new media.
497 		 */
498 		event = TULIP_MEDIAPOLL_LINKFAIL;
499 		break;
500 	    }
501 	    case TULIP_LINK_UP: {
502 		/*
503 		 * Check again soon.
504 		 */
505 		tulip_timeout(sc);
506 		return;
507 	    }
508 	    case TULIP_LINK_UNKNOWN: {
509 		/*
510 		 * We can't tell so don't bother.
511 		 */
512 		return;
513 	    }
514 	}
515     }
516 
517     if (event == TULIP_MEDIAPOLL_LINKFAIL) {
518 	if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE) {
519 	    if (TULIP_DO_AUTOSENSE(sc)) {
520 		sc->tulip_media = TULIP_MEDIA_UNKNOWN;
521 		if (sc->tulip_if.if_flags & IFF_UP)
522 		    tulip_reset(sc);	/* restart probe */
523 	    }
524 	    return;
525 	}
526     }
527 
528     if (event == TULIP_MEDIAPOLL_START) {
529     	ifq_set_oactive(&sc->tulip_if.if_snd);
530 	if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE)
531 	    return;
532 	sc->tulip_probe_mediamask = 0;
533 	sc->tulip_probe_passes = 0;
534 	/*
535 	 * If the SROM contained an explicit media to use, use it.
536 	 */
537 	sc->tulip_cmdmode &= ~(TULIP_CMD_RXRUN|TULIP_CMD_FULLDUPLEX);
538 	sc->tulip_flags |= TULIP_TRYNWAY|TULIP_PROBE1STPASS;
539 	sc->tulip_flags &= ~(TULIP_DIDNWAY|TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
540 	/*
541 	 * connidx is defaulted to a media_unknown type.
542 	 */
543 	sc->tulip_probe_media = tulip_srom_conninfo[sc->tulip_connidx].sc_media;
544 	if (sc->tulip_probe_media != TULIP_MEDIA_UNKNOWN) {
545 	    tulip_linkup(sc, sc->tulip_probe_media);
546 	    tulip_timeout(sc);
547 	    return;
548 	}
549 
550 	if (sc->tulip_features & TULIP_HAVE_GPR) {
551 	    sc->tulip_probe_state = TULIP_PROBE_GPRTEST;
552 	    sc->tulip_probe_timeout = 2000;
553 	} else {
554 	    sc->tulip_probe_media = TULIP_MEDIA_MAX;
555 	    sc->tulip_probe_timeout = 0;
556 	    sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
557 	}
558     }
559 
560     /*
561      * Ignore txprobe failures or spurious callbacks.
562      */
563     if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED
564 	    && sc->tulip_probe_state != TULIP_PROBE_MEDIATEST) {
565 	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
566 	return;
567     }
568 
569     /*
570      * If we really transmitted a packet, then that's the media we'll use.
571      */
572     if (event == TULIP_MEDIAPOLL_TXPROBE_OK || event == TULIP_MEDIAPOLL_LINKPASS) {
573 	if (event == TULIP_MEDIAPOLL_LINKPASS) {
574 	    /* XXX Check media status just to be sure */
575 	    sc->tulip_probe_media = TULIP_MEDIA_10BASET;
576 	}
577 	tulip_linkup(sc, sc->tulip_probe_media);
578 	tulip_timeout(sc);
579 	return;
580     }
581 
582     if (sc->tulip_probe_state == TULIP_PROBE_GPRTEST) {
583 #if defined(TULIP_DO_GPR_SENSE)
584 	/*
585 	 * Check for media via the general purpose register.
586 	 *
587 	 * Try to sense the media via the GPR.  If the same value
588 	 * occurs 3 times in a row then just use that.
589 	 */
590 	if (sc->tulip_probe_timeout > 0) {
591 	    tulip_media_t new_probe_media = tulip_21140_gpr_media_sense(sc);
592 	    if (new_probe_media != TULIP_MEDIA_UNKNOWN) {
593 		if (new_probe_media == sc->tulip_probe_media) {
594 		    if (--sc->tulip_probe_count == 0)
595 			tulip_linkup(sc, sc->tulip_probe_media);
596 		} else {
597 		    sc->tulip_probe_count = 10;
598 		}
599 	    }
600 	    sc->tulip_probe_media = new_probe_media;
601 	    tulip_timeout(sc);
602 	    return;
603 	}
604 #endif /* TULIP_DO_GPR_SENSE */
605 	/*
606 	 * Brute force.  We cycle through each of the media types
607 	 * and try to transmit a packet.
608 	 */
609 	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
610 	sc->tulip_probe_media = TULIP_MEDIA_MAX;
611 	sc->tulip_probe_timeout = 0;
612 	tulip_timeout(sc);
613 	return;
614     }
615 
616     if (sc->tulip_probe_state != TULIP_PROBE_MEDIATEST
617 	   && (sc->tulip_features & TULIP_HAVE_MII)) {
618 	tulip_media_t old_media = sc->tulip_probe_media;
619 	tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
620 	switch (sc->tulip_probe_state) {
621 	    case TULIP_PROBE_FAILED:
622 	    case TULIP_PROBE_MEDIATEST: {
623 		/*
624 		 * Try the next media.
625 		 */
626 		sc->tulip_probe_mediamask |= sc->tulip_mediums[sc->tulip_probe_media]->mi_mediamask;
627 		sc->tulip_probe_timeout = 0;
628 #ifdef notyet
629 		if (sc->tulip_probe_state == TULIP_PROBE_FAILED)
630 		    break;
631 		if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
632 		    break;
633 		sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 300;
634 #endif
635 		break;
636 	    }
637 	    case TULIP_PROBE_PHYAUTONEG: {
638 		return;
639 	    }
640 	    case TULIP_PROBE_INACTIVE: {
641 		/*
642 		 * Only probe if we autonegotiated a media that hasn't failed.
643 		 */
644 		sc->tulip_probe_timeout = 0;
645 		if (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media)) {
646 		    sc->tulip_probe_media = old_media;
647 		    break;
648 		}
649 		tulip_linkup(sc, sc->tulip_probe_media);
650 		tulip_timeout(sc);
651 		return;
652 	    }
653 	    default: {
654 #if defined(DIAGNOSTIC)
655 		panic("tulip_media_poll: botch at line %d", __LINE__);
656 #endif
657 		break;
658 	    }
659 	}
660     }
661 
662     if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED) {
663 	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
664 	return;
665     }
666 
667     /*
668      * switch to another media if we tried this one enough.
669      */
670     if (/* event == TULIP_MEDIAPOLL_TXPROBE_FAILED || */ sc->tulip_probe_timeout <= 0) {
671 	/*
672 	 * Find the next media type to check for.  Full Duplex
673 	 * types are not allowed.
674 	 */
675 	do {
676 	    sc->tulip_probe_media -= 1;
677 	    if (sc->tulip_probe_media == TULIP_MEDIA_UNKNOWN) {
678 		if (++sc->tulip_probe_passes == 3) {
679 		    if_printf(&sc->tulip_if, "autosense failed: cable problem?\n");
680 		    if ((sc->tulip_if.if_flags & IFF_UP) == 0) {
681 			sc->tulip_if.if_flags &= ~IFF_RUNNING;
682 			sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
683 			return;
684 		    }
685 		}
686 		sc->tulip_flags ^= TULIP_TRYNWAY;	/* XXX */
687 		sc->tulip_probe_mediamask = 0;
688 		sc->tulip_probe_media = TULIP_MEDIA_MAX - 1;
689 	    }
690 	} while (sc->tulip_mediums[sc->tulip_probe_media] == NULL
691 		 || (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media))
692 		 || TULIP_IS_MEDIA_FD(sc->tulip_probe_media));
693 
694 	sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 1000;
695 	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
696 	sc->tulip_probe.probe_txprobes = 0;
697 	tulip_reset(sc);
698 	tulip_media_set(sc, sc->tulip_probe_media);
699 	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
700     }
701     tulip_timeout(sc);
702 
703     /*
704      * If this is hanging off a phy, we know are doing NWAY and we have
705      * forced the phy to a specific speed.  Wait for link up before
706      * before sending a packet.
707      */
708     switch (sc->tulip_mediums[sc->tulip_probe_media]->mi_type) {
709 	case TULIP_MEDIAINFO_MII: {
710 	    if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
711 		return;
712 	    break;
713 	}
714 	case TULIP_MEDIAINFO_SIA: {
715 	    if (TULIP_IS_MEDIA_TP(sc->tulip_probe_media)) {
716 		if (TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL)
717 		    return;
718 		tulip_linkup(sc, sc->tulip_probe_media);
719 #ifdef notyet
720 		if (sc->tulip_features & TULIP_HAVE_MII)
721 		    tulip_timeout(sc);
722 #endif
723 		return;
724 	    }
725 	    break;
726 	}
727 	case TULIP_MEDIAINFO_RESET:
728 	case TULIP_MEDIAINFO_SYM:
729 	case TULIP_MEDIAINFO_NONE:
730 	case TULIP_MEDIAINFO_GPR: {
731 	    break;
732 	}
733     }
734     /*
735      * Try to send a packet.
736      */
737     tulip_txprobe(sc);
738 }
739 
740 static void
741 tulip_media_select(tulip_softc_t *sc)
742 {
743     if (sc->tulip_features & TULIP_HAVE_GPR) {
744 	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
745 	DELAY(10);
746 	TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpdata);
747     }
748     /*
749      * If this board has no media, just return
750      */
751     if (sc->tulip_features & TULIP_HAVE_NOMEDIA)
752 	return;
753 
754     if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
755 	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
756 	(*sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_START);
757     } else {
758 	tulip_media_set(sc, sc->tulip_media);
759     }
760 }
761 
762 static void
763 tulip_21040_mediainfo_init(tulip_softc_t *sc, tulip_media_t media)
764 {
765     sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_THRSHLD160
766 	|TULIP_CMD_BACKOFFCTR;
767     sc->tulip_if.if_baudrate = 10000000;
768 
769     if (media == TULIP_MEDIA_10BASET || media == TULIP_MEDIA_UNKNOWN) {
770 	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[0], 21040, 10BASET);
771 	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[1], 21040, 10BASET_FD);
772 	sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
773     }
774 
775     if (media == TULIP_MEDIA_AUIBNC || media == TULIP_MEDIA_UNKNOWN) {
776 	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[2], 21040, AUIBNC);
777     }
778 
779     if (media == TULIP_MEDIA_UNKNOWN) {
780 	TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[3], 21040, EXTSIA);
781     }
782 }
783 
784 static void
785 tulip_21040_media_probe(tulip_softc_t *sc)
786 {
787     tulip_21040_mediainfo_init(sc, TULIP_MEDIA_UNKNOWN);
788 }
789 
790 static void
791 tulip_21040_10baset_only_media_probe(tulip_softc_t *sc)
792 {
793     tulip_21040_mediainfo_init(sc, TULIP_MEDIA_10BASET);
794     tulip_media_set(sc, TULIP_MEDIA_10BASET);
795     sc->tulip_media = TULIP_MEDIA_10BASET;
796 }
797 
798 static void
799 tulip_21040_10baset_only_media_select(tulip_softc_t *sc)
800 {
801     sc->tulip_flags |= TULIP_LINKUP;
802     if (sc->tulip_media == TULIP_MEDIA_10BASET_FD) {
803 	sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
804 	sc->tulip_flags &= ~TULIP_SQETEST;
805     } else {
806 	sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
807 	sc->tulip_flags |= TULIP_SQETEST;
808     }
809     tulip_media_set(sc, sc->tulip_media);
810 }
811 
812 static void
813 tulip_21040_auibnc_only_media_probe(tulip_softc_t *sc)
814 {
815     tulip_21040_mediainfo_init(sc, TULIP_MEDIA_AUIBNC);
816     sc->tulip_flags |= TULIP_SQETEST|TULIP_LINKUP;
817     tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
818     sc->tulip_media = TULIP_MEDIA_AUIBNC;
819 }
820 
821 static void
822 tulip_21040_auibnc_only_media_select(tulip_softc_t *sc)
823 {
824     tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
825     sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
826 }
827 
828 static const tulip_boardsw_t tulip_21040_boardsw = {
829     TULIP_21040_GENERIC,
830     tulip_21040_media_probe,
831     tulip_media_select,
832     tulip_media_poll,
833 };
834 
835 static const tulip_boardsw_t tulip_21040_10baset_only_boardsw = {
836     TULIP_21040_GENERIC,
837     tulip_21040_10baset_only_media_probe,
838     tulip_21040_10baset_only_media_select,
839     NULL,
840 };
841 
842 static const tulip_boardsw_t tulip_21040_auibnc_only_boardsw = {
843     TULIP_21040_GENERIC,
844     tulip_21040_auibnc_only_media_probe,
845     tulip_21040_auibnc_only_media_select,
846     NULL,
847 };
848 
849 static void
850 tulip_21041_mediainfo_init(tulip_softc_t *sc)
851 {
852     tulip_media_info_t *mi = sc->tulip_mediainfo;
853 
854 #ifdef notyet
855     if (sc->tulip_revinfo >= 0x20) {
856 	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, 10BASET);
857 	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, 10BASET_FD);
858 	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, AUI);
859 	TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, BNC);
860 	return;
861     }
862 #endif
863     TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041, 10BASET);
864     TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041, 10BASET_FD);
865     TULIP_MEDIAINFO_SIA_INIT(sc, &mi[2], 21041, AUI);
866     TULIP_MEDIAINFO_SIA_INIT(sc, &mi[3], 21041, BNC);
867 }
868 
869 static void
870 tulip_21041_media_probe(tulip_softc_t *sc)
871 {
872     sc->tulip_if.if_baudrate = 10000000;
873     sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_ENHCAPTEFFCT
874 	|TULIP_CMD_THRSHLD160|TULIP_CMD_BACKOFFCTR;
875     sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
876     tulip_21041_mediainfo_init(sc);
877 }
878 
879 static void
880 tulip_21041_media_poll(tulip_softc_t *sc, tulip_mediapoll_event_t event)
881 {
882     uint32_t sia_status;
883 
884     if (event == TULIP_MEDIAPOLL_LINKFAIL) {
885 	if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE
886 		|| !TULIP_DO_AUTOSENSE(sc))
887 	    return;
888 	sc->tulip_media = TULIP_MEDIA_UNKNOWN;
889 	tulip_reset(sc);	/* start probe */
890 	return;
891     }
892 
893     /*
894      * If we've been been asked to start a poll or link change interrupt
895      * restart the probe (and reset the tulip to a known state).
896      */
897     if (event == TULIP_MEDIAPOLL_START) {
898 	ifq_set_oactive(&sc->tulip_if.if_snd);
899 	sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_RXRUN);
900 #ifdef notyet
901 	if (sc->tulip_revinfo >= 0x20) {
902 	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
903 	    sc->tulip_flags |= TULIP_DIDNWAY;
904 	}
905 #endif
906 	TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
907 	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
908 	sc->tulip_probe_media = TULIP_MEDIA_10BASET;
909 	sc->tulip_probe_timeout = TULIP_21041_PROBE_10BASET_TIMEOUT;
910 	tulip_media_set(sc, TULIP_MEDIA_10BASET);
911 	tulip_timeout(sc);
912 	return;
913     }
914 
915     if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
916 	return;
917 
918     if (event == TULIP_MEDIAPOLL_TXPROBE_OK) {
919 	tulip_linkup(sc, sc->tulip_probe_media);
920 	return;
921     }
922 
923     sia_status = TULIP_CSR_READ(sc, csr_sia_status);
924     TULIP_CSR_WRITE(sc, csr_sia_status, sia_status);
925     if ((sia_status & TULIP_SIASTS_LINKFAIL) == 0) {
926 	if (sc->tulip_revinfo >= 0x20) {
927 	    if (sia_status & (PHYSTS_10BASET_FD << (16 - 6)))
928 		sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
929 	}
930 	/*
931 	 * If the link has passed LinkPass, 10baseT is the
932 	 * proper media to use.
933 	 */
934 	tulip_linkup(sc, sc->tulip_probe_media);
935 	return;
936     }
937 
938     /*
939      * wait for up to 2.4 seconds for the link to reach pass state.
940      * Only then start scanning the other media for activity.
941      * choose media with receive activity over those without.
942      */
943     if (sc->tulip_probe_media == TULIP_MEDIA_10BASET) {
944 	if (event != TULIP_MEDIAPOLL_TIMER)
945 	    return;
946 	if (sc->tulip_probe_timeout > 0
947 		&& (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) == 0) {
948 	    tulip_timeout(sc);
949 	    return;
950 	}
951 	sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
952 	sc->tulip_flags |= TULIP_WANTRXACT;
953 	if (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) {
954 	    sc->tulip_probe_media = TULIP_MEDIA_BNC;
955 	} else {
956 	    sc->tulip_probe_media = TULIP_MEDIA_AUI;
957 	}
958 	tulip_media_set(sc, sc->tulip_probe_media);
959 	tulip_timeout(sc);
960 	return;
961     }
962 
963     /*
964      * If we failed, clear the txprobe active flag.
965      */
966     if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED)
967 	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
968 
969 
970     if (event == TULIP_MEDIAPOLL_TIMER) {
971 	/*
972 	 * If we've received something, then that's our link!
973 	 */
974 	if (sc->tulip_flags & TULIP_RXACT) {
975 	    tulip_linkup(sc, sc->tulip_probe_media);
976 	    return;
977 	}
978 	/*
979 	 * if no txprobe active
980 	 */
981 	if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0
982 		&& ((sc->tulip_flags & TULIP_WANTRXACT) == 0
983 		    || (sia_status & TULIP_SIASTS_RXACTIVITY))) {
984 	    sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
985 	    tulip_txprobe(sc);
986 	    tulip_timeout(sc);
987 	    return;
988 	}
989 	/*
990 	 * Take 2 passes through before deciding to not
991 	 * wait for receive activity.  Then take another
992 	 * two passes before spitting out a warning.
993 	 */
994 	if (sc->tulip_probe_timeout <= 0) {
995 	    if (sc->tulip_flags & TULIP_WANTRXACT) {
996 		sc->tulip_flags &= ~TULIP_WANTRXACT;
997 		sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
998 	    } else {
999 		if_printf(&sc->tulip_if, "autosense failed: cable problem?\n");
1000 		if ((sc->tulip_if.if_flags & IFF_UP) == 0) {
1001 		    sc->tulip_if.if_flags &= ~IFF_RUNNING;
1002 		    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1003 		    return;
1004 		}
1005 	    }
1006 	}
1007     }
1008 
1009     /*
1010      * Since this media failed to probe, try the other one.
1011      */
1012     sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1013     if (sc->tulip_probe_media == TULIP_MEDIA_AUI) {
1014 	sc->tulip_probe_media = TULIP_MEDIA_BNC;
1015     } else {
1016 	sc->tulip_probe_media = TULIP_MEDIA_AUI;
1017     }
1018     tulip_media_set(sc, sc->tulip_probe_media);
1019     sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1020     tulip_timeout(sc);
1021 }
1022 
1023 static const tulip_boardsw_t tulip_21041_boardsw = {
1024     TULIP_21041_GENERIC,
1025     tulip_21041_media_probe,
1026     tulip_media_select,
1027     tulip_21041_media_poll
1028 };
1029 
1030 static const tulip_phy_attr_t tulip_mii_phy_attrlist[] = {
1031     { 0x20005c00, 0,		/* 08-00-17 */
1032       {
1033 	{ 0x19, 0x0040, 0x0040 },	/* 10TX */
1034 	{ 0x19, 0x0040, 0x0000 },	/* 100TX */
1035       },
1036     },
1037     { 0x0281F400, 0,		/* 00-A0-7D */
1038       {
1039 	{ 0x12, 0x0010, 0x0000 },	/* 10T */
1040 	{ },				/* 100TX */
1041 	{ 0x12, 0x0010, 0x0010 },	/* 100T4 */
1042 	{ 0x12, 0x0008, 0x0008 },	/* FULL_DUPLEX */
1043       },
1044     },
1045 #if 0
1046     { 0x0015F420, 0,	/* 00-A0-7D */
1047       {
1048 	{ 0x12, 0x0010, 0x0000 },	/* 10T */
1049 	{ },				/* 100TX */
1050 	{ 0x12, 0x0010, 0x0010 },	/* 100T4 */
1051 	{ 0x12, 0x0008, 0x0008 },	/* FULL_DUPLEX */
1052       },
1053     },
1054 #endif
1055     { 0x0281F400, 0,		/* 00-A0-BE */
1056       {
1057 	{ 0x11, 0x8000, 0x0000 },	/* 10T */
1058 	{ 0x11, 0x8000, 0x8000 },	/* 100TX */
1059 	{ },				/* 100T4 */
1060 	{ 0x11, 0x4000, 0x4000 },	/* FULL_DUPLEX */
1061       },
1062     },
1063     { 0 }
1064 };
1065 
1066 static tulip_media_t
1067 tulip_mii_phy_readspecific(tulip_softc_t *sc)
1068 {
1069     const tulip_phy_attr_t *attr;
1070     u_int16_t data;
1071     u_int32_t id;
1072     unsigned idx = 0;
1073     static const tulip_media_t table[] = {
1074 	TULIP_MEDIA_UNKNOWN,
1075 	TULIP_MEDIA_10BASET,
1076 	TULIP_MEDIA_100BASETX,
1077 	TULIP_MEDIA_100BASET4,
1078 	TULIP_MEDIA_UNKNOWN,
1079 	TULIP_MEDIA_10BASET_FD,
1080 	TULIP_MEDIA_100BASETX_FD,
1081 	TULIP_MEDIA_UNKNOWN
1082     };
1083 
1084     /*
1085      * Don't read phy specific registers if link is not up.
1086      */
1087     data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
1088     if ((data & (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS)) != (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS))
1089 	return TULIP_MEDIA_UNKNOWN;
1090 
1091     id = (tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDLOW) << 16) |
1092 	tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDHIGH);
1093     for (attr = tulip_mii_phy_attrlist;; attr++) {
1094 	if (attr->attr_id == 0)
1095 	    return TULIP_MEDIA_UNKNOWN;
1096 	if ((id & ~0x0F) == attr->attr_id)
1097 	    break;
1098     }
1099 
1100     if (attr->attr_modes[PHY_MODE_100TX].pm_regno) {
1101 	const tulip_phy_modedata_t *pm = &attr->attr_modes[PHY_MODE_100TX];
1102 	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1103 	if ((data & pm->pm_mask) == pm->pm_value)
1104 	    idx = 2;
1105     }
1106     if (idx == 0 && attr->attr_modes[PHY_MODE_100T4].pm_regno) {
1107 	const tulip_phy_modedata_t *pm = &attr->attr_modes[PHY_MODE_100T4];
1108 	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1109 	if ((data & pm->pm_mask) == pm->pm_value)
1110 	    idx = 3;
1111     }
1112     if (idx == 0 && attr->attr_modes[PHY_MODE_10T].pm_regno) {
1113 	const tulip_phy_modedata_t *pm = &attr->attr_modes[PHY_MODE_10T];
1114 	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1115 	if ((data & pm->pm_mask) == pm->pm_value)
1116 	    idx = 1;
1117     }
1118     if (idx != 0 && attr->attr_modes[PHY_MODE_FULLDUPLEX].pm_regno) {
1119 	const tulip_phy_modedata_t *pm = &attr->attr_modes[PHY_MODE_FULLDUPLEX];
1120 	data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1121 	idx += ((data & pm->pm_mask) == pm->pm_value ? 4 : 0);
1122     }
1123     return table[idx];
1124 }
1125 
1126 static u_int
1127 tulip_mii_get_phyaddr(tulip_softc_t *sc, u_int offset)
1128 {
1129     u_int phyaddr;
1130 
1131     for (phyaddr = 1; phyaddr < 32; phyaddr++) {
1132 	u_int status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1133 	if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
1134 	    continue;
1135 	if (offset == 0)
1136 	    return phyaddr;
1137 	offset--;
1138     }
1139     if (offset == 0) {
1140 	u_int status = tulip_mii_readreg(sc, 0, PHYREG_STATUS);
1141 	if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
1142 	    return TULIP_MII_NOPHY;
1143 	return 0;
1144     }
1145     return TULIP_MII_NOPHY;
1146 }
1147 
1148 static int
1149 tulip_mii_map_abilities(tulip_softc_t *sc, u_int abilities)
1150 {
1151     sc->tulip_abilities = abilities;
1152     if (abilities & PHYSTS_100BASETX_FD) {
1153 	sc->tulip_probe_media = TULIP_MEDIA_100BASETX_FD;
1154     } else if (abilities & PHYSTS_100BASET4) {
1155 	sc->tulip_probe_media = TULIP_MEDIA_100BASET4;
1156     } else if (abilities & PHYSTS_100BASETX) {
1157 	sc->tulip_probe_media = TULIP_MEDIA_100BASETX;
1158     } else if (abilities & PHYSTS_10BASET_FD) {
1159 	sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
1160     } else if (abilities & PHYSTS_10BASET) {
1161 	sc->tulip_probe_media = TULIP_MEDIA_10BASET;
1162     } else {
1163 	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1164 	return 0;
1165     }
1166     sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1167     return 1;
1168 }
1169 
1170 static void
1171 tulip_mii_autonegotiate(tulip_softc_t *sc, u_int phyaddr)
1172 {
1173     switch (sc->tulip_probe_state) {
1174         case TULIP_PROBE_MEDIATEST:
1175         case TULIP_PROBE_INACTIVE: {
1176 	    sc->tulip_flags |= TULIP_DIDNWAY;
1177 	    tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, PHYCTL_RESET);
1178 	    sc->tulip_probe_timeout = 3000;
1179 	    sc->tulip_intrmask |= TULIP_STS_ABNRMLINTR|TULIP_STS_NORMALINTR;
1180 	    sc->tulip_probe_state = TULIP_PROBE_PHYRESET;
1181 	    /* FALL THROUGH */
1182 	}
1183         case TULIP_PROBE_PHYRESET: {
1184 	    uint32_t status;
1185 	    uint32_t data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
1186 	    if (data & PHYCTL_RESET) {
1187 		if (sc->tulip_probe_timeout > 0) {
1188 		    tulip_timeout(sc);
1189 		    return;
1190 		}
1191 		if_printf(&sc->tulip_if,
1192 		    "(phy%d): error: reset of PHY never completed!\n", phyaddr);
1193 		sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1194 		sc->tulip_probe_state = TULIP_PROBE_FAILED;
1195 		sc->tulip_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1196 		return;
1197 	    }
1198 	    status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1199 	    if ((status & PHYSTS_CAN_AUTONEG) == 0) {
1200 		sc->tulip_flags &= ~TULIP_DIDNWAY;
1201 		sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1202 		return;
1203 	    }
1204 	    if (tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT) != ((status >> 6) | 0x01))
1205 		tulip_mii_writereg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT, (status >> 6) | 0x01);
1206 	    tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, data|PHYCTL_AUTONEG_RESTART|PHYCTL_AUTONEG_ENABLE);
1207 	    data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
1208 	    sc->tulip_probe_state = TULIP_PROBE_PHYAUTONEG;
1209 	    sc->tulip_probe_timeout = 3000;
1210 	    /* FALL THROUGH */
1211 	}
1212         case TULIP_PROBE_PHYAUTONEG: {
1213 	    u_int32_t status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1214 	    u_int32_t data;
1215 	    if ((status & PHYSTS_AUTONEG_DONE) == 0) {
1216 		if (sc->tulip_probe_timeout > 0) {
1217 		    tulip_timeout(sc);
1218 		    return;
1219 		}
1220 		sc->tulip_flags &= ~TULIP_DIDNWAY;
1221 		sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1222 		return;
1223 	    }
1224 	    data = tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ABILITIES);
1225 	    data = (data << 6) & status;
1226 	    if (!tulip_mii_map_abilities(sc, data))
1227 		sc->tulip_flags &= ~TULIP_DIDNWAY;
1228 	    return;
1229 	}
1230 	default: {
1231 #if defined(DIAGNOSTIC)
1232 	    panic("tulip_media_poll: botch at line %d", __LINE__);
1233 #endif
1234 	    break;
1235 	}
1236     }
1237 }
1238 
1239 static void
1240 tulip_2114x_media_preset(tulip_softc_t *sc)
1241 {
1242     const tulip_media_info_t *mi = NULL;
1243     tulip_media_t media;
1244 
1245     if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
1246 	media = sc->tulip_media;
1247     else
1248 	media = sc->tulip_probe_media;
1249 
1250     sc->tulip_cmdmode &= ~TULIP_CMD_PORTSELECT;
1251     sc->tulip_flags &= ~TULIP_SQETEST;
1252     if (media != TULIP_MEDIA_UNKNOWN && media != TULIP_MEDIA_MAX) {
1253 	    mi = sc->tulip_mediums[media];
1254 	    if (mi->mi_type == TULIP_MEDIAINFO_MII) {
1255 		sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
1256 	    } else if (mi->mi_type == TULIP_MEDIAINFO_GPR
1257 		       || mi->mi_type == TULIP_MEDIAINFO_SYM) {
1258 		sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
1259 		sc->tulip_cmdmode |= mi->mi_cmdmode;
1260 	    } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
1261 		TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
1262 	    }
1263     }
1264     switch (media) {
1265 	case TULIP_MEDIA_BNC:
1266 	case TULIP_MEDIA_AUI:
1267 	case TULIP_MEDIA_10BASET: {
1268 	    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
1269 	    sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
1270 	    sc->tulip_if.if_baudrate = 10000000;
1271 	    sc->tulip_flags |= TULIP_SQETEST;
1272 	    break;
1273 	}
1274 	case TULIP_MEDIA_10BASET_FD: {
1275 	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL;
1276 	    sc->tulip_if.if_baudrate = 10000000;
1277 	    break;
1278 	}
1279 	case TULIP_MEDIA_100BASEFX:
1280 	case TULIP_MEDIA_100BASET4:
1281 	case TULIP_MEDIA_100BASETX: {
1282 	    sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL);
1283 	    sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
1284 	    sc->tulip_if.if_baudrate = 100000000;
1285 	    break;
1286 	}
1287 	case TULIP_MEDIA_100BASEFX_FD:
1288 	case TULIP_MEDIA_100BASETX_FD: {
1289 	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_PORTSELECT;
1290 	    sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
1291 	    sc->tulip_if.if_baudrate = 100000000;
1292 	    break;
1293 	}
1294 	default: {
1295 	    break;
1296 	}
1297     }
1298     TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1299 }
1300 
1301 /*
1302  ********************************************************************
1303  *  Start of 21140/21140A support which does not use the MII interface
1304  */
1305 
1306 static void
1307 tulip_null_media_poll(tulip_softc_t *sc, tulip_mediapoll_event_t event)
1308 {
1309 #if defined(DIAGNOSTIC)
1310     if_printf(&sc->tulip_if, "botch(media_poll) at line %d\n", __LINE__);
1311 #endif
1312 }
1313 
1314 static void
1315 tulip_21140_mediainit(tulip_softc_t *sc, tulip_media_info_t *mip,
1316 		      tulip_media_t media, u_int gpdata, u_int cmdmode)
1317 {
1318     sc->tulip_mediums[media] = mip;
1319     mip->mi_type = TULIP_MEDIAINFO_GPR;
1320     mip->mi_cmdmode = cmdmode;
1321     mip->mi_gpdata = gpdata;
1322 }
1323 
1324 static void
1325 tulip_21140_evalboard_media_probe(tulip_softc_t *sc)
1326 {
1327     tulip_media_info_t *mip = sc->tulip_mediainfo;
1328 
1329     sc->tulip_gpinit = TULIP_GP_EB_PINS;
1330     sc->tulip_gpdata = TULIP_GP_EB_INIT;
1331     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
1332     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
1333     TULIP_CSR_WRITE(sc, csr_command,
1334 	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1335 	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1336     TULIP_CSR_WRITE(sc, csr_command,
1337 	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1338     DELAY(1000000);
1339     if ((TULIP_CSR_READ(sc, csr_gp) & TULIP_GP_EB_OK100) != 0) {
1340 	sc->tulip_media = TULIP_MEDIA_10BASET;
1341     } else {
1342 	sc->tulip_media = TULIP_MEDIA_100BASETX;
1343     }
1344     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1345 			  TULIP_GP_EB_INIT,
1346 			  TULIP_CMD_TXTHRSHLDCTL);
1347     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1348 			  TULIP_GP_EB_INIT,
1349 			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1350     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1351 			  TULIP_GP_EB_INIT,
1352 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1353 			      |TULIP_CMD_SCRAMBLER);
1354     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1355 			  TULIP_GP_EB_INIT,
1356 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1357 			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1358 }
1359 
1360 static const tulip_boardsw_t tulip_21140_eb_boardsw = {
1361     TULIP_21140_DEC_EB,
1362     tulip_21140_evalboard_media_probe,
1363     tulip_media_select,
1364     tulip_null_media_poll,
1365     tulip_2114x_media_preset,
1366 };
1367 
1368 static void
1369 tulip_21140_accton_media_probe(tulip_softc_t *sc)
1370 {
1371     tulip_media_info_t *mip = sc->tulip_mediainfo;
1372     u_int gpdata;
1373 
1374     sc->tulip_gpinit = TULIP_GP_EB_PINS;
1375     sc->tulip_gpdata = TULIP_GP_EB_INIT;
1376     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
1377     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
1378     TULIP_CSR_WRITE(sc, csr_command,
1379 	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1380 	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1381     TULIP_CSR_WRITE(sc, csr_command,
1382 	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1383     DELAY(1000000);
1384     gpdata = TULIP_CSR_READ(sc, csr_gp);
1385     if ((gpdata & TULIP_GP_EN1207_UTP_INIT) == 0) {
1386 	sc->tulip_media = TULIP_MEDIA_10BASET;
1387     } else {
1388 	if ((gpdata & TULIP_GP_EN1207_BNC_INIT) == 0) {
1389 		sc->tulip_media = TULIP_MEDIA_BNC;
1390         } else {
1391 		sc->tulip_media = TULIP_MEDIA_100BASETX;
1392         }
1393     }
1394     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_BNC,
1395 			  TULIP_GP_EN1207_BNC_INIT,
1396 			  TULIP_CMD_TXTHRSHLDCTL);
1397     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1398 			  TULIP_GP_EN1207_UTP_INIT,
1399 			  TULIP_CMD_TXTHRSHLDCTL);
1400     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1401 			  TULIP_GP_EN1207_UTP_INIT,
1402 			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1403     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1404 			  TULIP_GP_EN1207_100_INIT,
1405 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1406 			      |TULIP_CMD_SCRAMBLER);
1407     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1408 			  TULIP_GP_EN1207_100_INIT,
1409 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1410 			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1411 }
1412 
1413 static const tulip_boardsw_t tulip_21140_accton_boardsw = {
1414     TULIP_21140_EN1207,
1415     tulip_21140_accton_media_probe,
1416     tulip_media_select,
1417     tulip_null_media_poll,
1418     tulip_2114x_media_preset,
1419 };
1420 
1421 static void
1422 tulip_21140_smc9332_media_probe(tulip_softc_t *sc)
1423 {
1424     tulip_media_info_t *mip = sc->tulip_mediainfo;
1425     int idx, cnt = 0;
1426 
1427     TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT|TULIP_CMD_MUSTBEONE);
1428     TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
1429     DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
1430 		   33MHz that comes to two microseconds but wait a
1431 		   bit longer anyways) */
1432     TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT |
1433 	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1434     sc->tulip_gpinit = TULIP_GP_SMC_9332_PINS;
1435     sc->tulip_gpdata = TULIP_GP_SMC_9332_INIT;
1436     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_PINS|TULIP_GP_PINSET);
1437     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_INIT);
1438     DELAY(200000);
1439     for (idx = 1000; idx > 0; idx--) {
1440 	u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
1441 	if ((csr & (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) == (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) {
1442 	    if (++cnt > 100)
1443 		break;
1444 	} else if ((csr & TULIP_GP_SMC_9332_OK10) == 0) {
1445 	    break;
1446 	} else {
1447 	    cnt = 0;
1448 	}
1449 	DELAY(1000);
1450     }
1451     sc->tulip_media = cnt > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
1452     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1453 			  TULIP_GP_SMC_9332_INIT,
1454 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1455 			      |TULIP_CMD_SCRAMBLER);
1456     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1457 			  TULIP_GP_SMC_9332_INIT,
1458 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1459 			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1460     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1461 			  TULIP_GP_SMC_9332_INIT,
1462 			  TULIP_CMD_TXTHRSHLDCTL);
1463     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1464 			  TULIP_GP_SMC_9332_INIT,
1465 			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1466 }
1467 
1468 static const tulip_boardsw_t tulip_21140_smc9332_boardsw = {
1469     TULIP_21140_SMC_9332,
1470     tulip_21140_smc9332_media_probe,
1471     tulip_media_select,
1472     tulip_null_media_poll,
1473     tulip_2114x_media_preset,
1474 };
1475 
1476 static void
1477 tulip_21140_cogent_em100_media_probe(tulip_softc_t *sc)
1478 {
1479     tulip_media_info_t *mip = sc->tulip_mediainfo;
1480     uint32_t cmdmode;
1481 
1482     sc->tulip_gpinit = TULIP_GP_EM100_PINS;
1483     sc->tulip_gpdata = TULIP_GP_EM100_INIT;
1484     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_PINS);
1485     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_INIT);
1486 
1487     cmdmode = TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_MUSTBEONE;
1488     cmdmode &= ~(TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_SCRAMBLER);
1489     if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
1490 	TULIP_CSR_WRITE(sc, csr_command, cmdmode);
1491 	sc->tulip_media = TULIP_MEDIA_100BASEFX;
1492 
1493 	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX,
1494 			  TULIP_GP_EM100_INIT,
1495 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION);
1496 	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX_FD,
1497 			  TULIP_GP_EM100_INIT,
1498 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1499 			      |TULIP_CMD_FULLDUPLEX);
1500     } else {
1501 	TULIP_CSR_WRITE(sc, csr_command, cmdmode|TULIP_CMD_SCRAMBLER);
1502 	sc->tulip_media = TULIP_MEDIA_100BASETX;
1503 	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1504 			  TULIP_GP_EM100_INIT,
1505 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1506 			      |TULIP_CMD_SCRAMBLER);
1507 	tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1508 			  TULIP_GP_EM100_INIT,
1509 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1510 			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1511     }
1512 }
1513 
1514 static const tulip_boardsw_t tulip_21140_cogent_em100_boardsw = {
1515     TULIP_21140_COGENT_EM100,
1516     tulip_21140_cogent_em100_media_probe,
1517     tulip_media_select,
1518     tulip_null_media_poll,
1519     tulip_2114x_media_preset
1520 };
1521 
1522 static void
1523 tulip_21140_znyx_zx34x_media_probe(tulip_softc_t *sc)
1524 {
1525     tulip_media_info_t *mip = sc->tulip_mediainfo;
1526     int cnt10 = 0, cnt100 = 0, idx;
1527 
1528     sc->tulip_gpinit = TULIP_GP_ZX34X_PINS;
1529     sc->tulip_gpdata = TULIP_GP_ZX34X_INIT;
1530     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_PINS);
1531     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_INIT);
1532     TULIP_CSR_WRITE(sc, csr_command,
1533 	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1534 	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1535     TULIP_CSR_WRITE(sc, csr_command,
1536 	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1537 
1538     DELAY(200000);
1539     for (idx = 1000; idx > 0; idx--) {
1540 	uint32_t csr = TULIP_CSR_READ(sc, csr_gp);
1541 	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)) {
1542 	    if (++cnt100 > 100)
1543 		break;
1544 	} else if ((csr & TULIP_GP_ZX34X_LNKFAIL) == 0) {
1545 	    if (++cnt10 > 100)
1546 		break;
1547 	} else {
1548 	    cnt10 = 0;
1549 	    cnt100 = 0;
1550 	}
1551 	DELAY(1000);
1552     }
1553     sc->tulip_media = cnt100 > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
1554     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1555 			  TULIP_GP_ZX34X_INIT,
1556 			  TULIP_CMD_TXTHRSHLDCTL);
1557     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1558 			  TULIP_GP_ZX34X_INIT,
1559 			  TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1560     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1561 			  TULIP_GP_ZX34X_INIT,
1562 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1563 			      |TULIP_CMD_SCRAMBLER);
1564     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1565 			  TULIP_GP_ZX34X_INIT,
1566 			  TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1567 			      |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1568 }
1569 
1570 static const tulip_boardsw_t tulip_21140_znyx_zx34x_boardsw = {
1571     TULIP_21140_ZNYX_ZX34X,
1572     tulip_21140_znyx_zx34x_media_probe,
1573     tulip_media_select,
1574     tulip_null_media_poll,
1575     tulip_2114x_media_preset,
1576 };
1577 
1578 static void
1579 tulip_2114x_media_probe(tulip_softc_t *sc)
1580 {
1581     sc->tulip_cmdmode |= TULIP_CMD_MUSTBEONE | TULIP_CMD_BACKOFFCTR |
1582 	TULIP_CMD_THRSHLD72;
1583 }
1584 
1585 static const tulip_boardsw_t tulip_2114x_isv_boardsw = {
1586     TULIP_21140_ISV,
1587     tulip_2114x_media_probe,
1588     tulip_media_select,
1589     tulip_media_poll,
1590     tulip_2114x_media_preset,
1591 };
1592 
1593 /*
1594  * ******** END of chip-specific handlers. ***********
1595  */
1596 
1597 /*
1598  * Code the read the SROM and MII bit streams (I2C)
1599  */
1600 #define	EMIT    do {					\
1601 	TULIP_CSR_WRITE(sc, csr_srom_mii, csr);	\
1602 	DELAY(1);					\
1603 } while (0)
1604 
1605 static void
1606 tulip_srom_idle(tulip_softc_t *sc)
1607 {
1608     u_int bit, csr;
1609 
1610     csr  = SROMSEL ; EMIT;
1611     csr  = SROMSEL | SROMRD; EMIT;
1612     csr ^= SROMCS; EMIT;
1613     csr ^= SROMCLKON; EMIT;
1614 
1615     /*
1616      * Write 25 cycles of 0 which will force the SROM to be idle.
1617      */
1618     for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
1619         csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1620         csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1621     }
1622     csr ^= SROMCLKOFF; EMIT;
1623     csr ^= SROMCS; EMIT;
1624     csr  = 0; EMIT;
1625 }
1626 
1627 
1628 static void
1629 tulip_srom_read(tulip_softc_t *sc)
1630 {
1631     const u_int bitwidth = SROM_BITWIDTH;
1632     const u_int cmdmask = (SROMCMD_RD << bitwidth);
1633     const u_int msb = 1 << (bitwidth + 3 - 1);
1634     u_int idx, lastidx = (1 << bitwidth) - 1;
1635 
1636     tulip_srom_idle(sc);
1637 
1638     for (idx = 0; idx <= lastidx; idx++) {
1639         u_int lastbit, data, bits, bit, csr;
1640 	csr  = SROMSEL ;	        EMIT;
1641         csr  = SROMSEL | SROMRD;        EMIT;
1642         csr ^= SROMCSON;                EMIT;
1643         csr ^=            SROMCLKON;    EMIT;
1644 
1645         lastbit = 0;
1646         for (bits = idx|cmdmask, bit = bitwidth + 3; bit > 0; bit--, bits <<= 1) {
1647             u_int thisbit = bits & msb;
1648             csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1649             if (thisbit != lastbit) {
1650                 csr ^= SROMDOUT; EMIT;  /* clock low; invert data */
1651             } else {
1652 		EMIT;
1653 	    }
1654             csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1655             lastbit = thisbit;
1656         }
1657         csr ^= SROMCLKOFF; EMIT;
1658 
1659         for (data = 0, bits = 0; bits < 16; bits++) {
1660             data <<= 1;
1661             csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1662             data |= TULIP_CSR_READ(sc, csr_srom_mii) & SROMDIN ? 1 : 0;
1663             csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1664         }
1665 	sc->tulip_rombuf[idx*2] = data & 0xFF;
1666 	sc->tulip_rombuf[idx*2+1] = data >> 8;
1667 	csr  = SROMSEL | SROMRD; EMIT;
1668 	csr  = 0; EMIT;
1669     }
1670     tulip_srom_idle(sc);
1671 }
1672 
1673 #define	MII_EMIT    do { 				\
1674 	TULIP_CSR_WRITE(sc, csr_srom_mii, csr);		\
1675 	DELAY(1);					\
1676 } while (0)
1677 
1678 static void
1679 tulip_mii_writebits(tulip_softc_t *sc, u_int data, u_int bits)
1680 {
1681     u_int csr, msb, lastbit;
1682 
1683     msb = 1 << (bits - 1);
1684     csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1685     lastbit = (csr & MII_DOUT) ? msb : 0;
1686     csr |= MII_WR; MII_EMIT;  		/* clock low; assert write */
1687 
1688     for (; bits > 0; bits--, data <<= 1) {
1689 	const unsigned thisbit = data & msb;
1690 	if (thisbit != lastbit) {
1691 	    csr ^= MII_DOUT; MII_EMIT;  /* clock low; invert data */
1692 	}
1693 	csr ^= MII_CLKON; MII_EMIT;     /* clock high; data valid */
1694 	lastbit = thisbit;
1695 	csr ^= MII_CLKOFF; MII_EMIT;    /* clock low; data not valid */
1696     }
1697 }
1698 
1699 static void
1700 tulip_mii_turnaround(tulip_softc_t *sc, u_int cmd)
1701 {
1702     u_int csr;
1703 
1704     csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1705 
1706     if (cmd == MII_WRCMD) {
1707 	csr |= MII_DOUT; MII_EMIT;	/* clock low; change data */
1708 	csr ^= MII_CLKON; MII_EMIT;	/* clock high; data valid */
1709 	csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
1710 	csr ^= MII_DOUT; MII_EMIT;	/* clock low; change data */
1711     } else {
1712 	csr |= MII_RD; MII_EMIT;	/* clock low; switch to read */
1713     }
1714     csr ^= MII_CLKON; MII_EMIT;		/* clock high; data valid */
1715     csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
1716 }
1717 
1718 static u_int
1719 tulip_mii_readbits(tulip_softc_t *sc)
1720 {
1721     u_int csr, data, idx;
1722 
1723     csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1724 
1725     for (idx = 0, data = 0; idx < 16; idx++) {
1726 	data <<= 1;	/* this is NOOP on the first pass through */
1727 	csr ^= MII_CLKON; MII_EMIT;	/* clock high; data valid */
1728 	if (TULIP_CSR_READ(sc, csr_srom_mii) & MII_DIN)
1729 	    data |= 1;
1730 	csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
1731     }
1732     csr ^= MII_RD; MII_EMIT;		/* clock low; turn off read */
1733 
1734     return data;
1735 }
1736 
1737 static u_int
1738 tulip_mii_readreg(tulip_softc_t *sc, u_int devaddr, u_int regno)
1739 {
1740     u_int csr;
1741     u_int data;
1742 
1743     csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1744     csr &= ~(MII_RD|MII_CLK); MII_EMIT;
1745     tulip_mii_writebits(sc, MII_PREAMBLE, 32);
1746     tulip_mii_writebits(sc, MII_RDCMD, 8);
1747     tulip_mii_writebits(sc, devaddr, 5);
1748     tulip_mii_writebits(sc, regno, 5);
1749     tulip_mii_turnaround(sc, MII_RDCMD);
1750 
1751     data = tulip_mii_readbits(sc);
1752     return data;
1753 }
1754 
1755 static void
1756 tulip_mii_writereg(tulip_softc_t *sc, u_int devaddr, u_int regno, u_int data)
1757 {
1758     u_int csr;
1759 
1760     csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1761     csr &= ~(MII_RD|MII_CLK); MII_EMIT;
1762     tulip_mii_writebits(sc, MII_PREAMBLE, 32);
1763     tulip_mii_writebits(sc, MII_WRCMD, 8);
1764     tulip_mii_writebits(sc, devaddr, 5);
1765     tulip_mii_writebits(sc, regno, 5);
1766     tulip_mii_turnaround(sc, MII_WRCMD);
1767     tulip_mii_writebits(sc, data, 16);
1768 }
1769 
1770 #define	tulip_mchash(mca)	(ether_crc32_le(mca, 6) & 0x1FF)
1771 #define	tulip_srom_crcok(databuf)	( \
1772     ((ether_crc32_le(databuf, 126) & 0xFFFFU) ^ 0xFFFFU) == \
1773      ((databuf)[126] | ((databuf)[127] << 8)))
1774 
1775 static void
1776 tulip_identify_dec_nic(tulip_softc_t *sc)
1777 {
1778     strcpy(sc->tulip_boardid, "DEC ");
1779 #define D0	4
1780     if (sc->tulip_chipid <= TULIP_21040)
1781 	return;
1782     if (bcmp(sc->tulip_rombuf + 29, "DE500", 5) == 0
1783 	|| bcmp(sc->tulip_rombuf + 29, "DE450", 5) == 0) {
1784 	bcopy(sc->tulip_rombuf + 29, &sc->tulip_boardid[D0], 8);
1785 	sc->tulip_boardid[D0+8] = ' ';
1786     }
1787 #undef D0
1788 }
1789 
1790 static void
1791 tulip_identify_znyx_nic(tulip_softc_t * const sc)
1792 {
1793     u_int id = 0;
1794     strcpy(sc->tulip_boardid, "ZNYX ZX3XX ");
1795     if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
1796 	u_int znyx_ptr;
1797 	sc->tulip_boardid[8] = '4';
1798 	znyx_ptr = sc->tulip_rombuf[124] + 256 * sc->tulip_rombuf[125];
1799 	if (znyx_ptr < 26 || znyx_ptr > 116) {
1800 	    sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
1801 	    return;
1802 	}
1803 	/* ZX344 = 0010 .. 0013FF
1804 	 */
1805 	if (sc->tulip_rombuf[znyx_ptr] == 0x4A
1806 		&& sc->tulip_rombuf[znyx_ptr + 1] == 0x52
1807 		&& sc->tulip_rombuf[znyx_ptr + 2] == 0x01) {
1808 	    id = sc->tulip_rombuf[znyx_ptr + 5] + 256 * sc->tulip_rombuf[znyx_ptr + 4];
1809 	    if ((id >> 8) == (TULIP_ZNYX_ID_ZX342 >> 8)) {
1810 		sc->tulip_boardid[9] = '2';
1811 		if (id == TULIP_ZNYX_ID_ZX342B) {
1812 		    sc->tulip_boardid[10] = 'B';
1813 		    sc->tulip_boardid[11] = ' ';
1814 		}
1815 		sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
1816 	    } else if (id == TULIP_ZNYX_ID_ZX344) {
1817 		sc->tulip_boardid[10] = '4';
1818 		sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
1819 	    } else if (id == TULIP_ZNYX_ID_ZX345) {
1820 		sc->tulip_boardid[9] = (sc->tulip_rombuf[19] > 1) ? '8' : '5';
1821 	    } else if (id == TULIP_ZNYX_ID_ZX346) {
1822 		sc->tulip_boardid[9] = '6';
1823 	    } else if (id == TULIP_ZNYX_ID_ZX351) {
1824 		sc->tulip_boardid[8] = '5';
1825 		sc->tulip_boardid[9] = '1';
1826 	    }
1827 	}
1828 	if (id == 0) {
1829 	    /*
1830 	     * Assume it's a ZX342...
1831 	     */
1832 	    sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
1833 	}
1834 	return;
1835     }
1836     sc->tulip_boardid[8] = '1';
1837     if (sc->tulip_chipid == TULIP_21041) {
1838 	sc->tulip_boardid[10] = '1';
1839 	return;
1840     }
1841     if (sc->tulip_rombuf[32] == 0x4A && sc->tulip_rombuf[33] == 0x52) {
1842 	id = sc->tulip_rombuf[37] + 256 * sc->tulip_rombuf[36];
1843 	if (id == TULIP_ZNYX_ID_ZX312T) {
1844 	    sc->tulip_boardid[9] = '2';
1845 	    sc->tulip_boardid[10] = 'T';
1846 	    sc->tulip_boardid[11] = ' ';
1847 	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
1848 	} else if (id == TULIP_ZNYX_ID_ZX314_INTA) {
1849 	    sc->tulip_boardid[9] = '4';
1850 	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
1851 	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
1852 	} else if (id == TULIP_ZNYX_ID_ZX314) {
1853 	    sc->tulip_boardid[9] = '4';
1854 	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
1855 	    sc->tulip_features |= TULIP_HAVE_BASEROM;
1856 	} else if (id == TULIP_ZNYX_ID_ZX315_INTA) {
1857 	    sc->tulip_boardid[9] = '5';
1858 	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
1859 	} else if (id == TULIP_ZNYX_ID_ZX315) {
1860 	    sc->tulip_boardid[9] = '5';
1861 	    sc->tulip_features |= TULIP_HAVE_BASEROM;
1862 	} else {
1863 	    id = 0;
1864 	}
1865     }
1866     if (id == 0) {
1867 	if ((sc->tulip_enaddr[3] & ~3) == 0xF0 && (sc->tulip_enaddr[5] & 2) == 0) {
1868 	    sc->tulip_boardid[9] = '4';
1869 	    sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
1870 	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
1871 	} else if ((sc->tulip_enaddr[3] & ~3) == 0xF4 && (sc->tulip_enaddr[5] & 1) == 0) {
1872 	    sc->tulip_boardid[9] = '5';
1873 	    sc->tulip_boardsw = &tulip_21040_boardsw;
1874 	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
1875 	} else if ((sc->tulip_enaddr[3] & ~3) == 0xEC) {
1876 	    sc->tulip_boardid[9] = '2';
1877 	    sc->tulip_boardsw = &tulip_21040_boardsw;
1878 	}
1879     }
1880 }
1881 
1882 static void
1883 tulip_identify_smc_nic(tulip_softc_t * const sc)
1884 {
1885     uint32_t id1, id2, ei;
1886     int auibnc = 0, utp = 0;
1887     char *cp;
1888 
1889     strcpy(sc->tulip_boardid, "SMC ");
1890     if (sc->tulip_chipid == TULIP_21041)
1891 	return;
1892     if (sc->tulip_chipid != TULIP_21040) {
1893 	if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
1894 	    strcpy(&sc->tulip_boardid[4], "9332DST ");
1895 	    sc->tulip_boardsw = &tulip_21140_smc9332_boardsw;
1896 	} else if (sc->tulip_features & (TULIP_HAVE_BASEROM|TULIP_HAVE_SLAVEDROM)) {
1897 	    strcpy(&sc->tulip_boardid[4], "9334BDT ");
1898 	} else {
1899 	    strcpy(&sc->tulip_boardid[4], "9332BDT ");
1900 	}
1901 	return;
1902     }
1903     id1 = sc->tulip_rombuf[0x60] | (sc->tulip_rombuf[0x61] << 8);
1904     id2 = sc->tulip_rombuf[0x62] | (sc->tulip_rombuf[0x63] << 8);
1905     ei  = sc->tulip_rombuf[0x66] | (sc->tulip_rombuf[0x67] << 8);
1906 
1907     strcpy(&sc->tulip_boardid[4], "8432");
1908     cp = &sc->tulip_boardid[8];
1909     if ((id1 & 1) == 0)
1910 	*cp++ = 'B', auibnc = 1;
1911     if ((id1 & 0xFF) > 0x32)
1912 	*cp++ = 'T', utp = 1;
1913     if ((id1 & 0x4000) == 0)
1914 	*cp++ = 'A', auibnc = 1;
1915     if (id2 == 0x15) {
1916 	sc->tulip_boardid[7] = '4';
1917 	*cp++ = '-';
1918 	*cp++ = 'C';
1919 	*cp++ = 'H';
1920 	*cp++ = (ei ? '2' : '1');
1921     }
1922     *cp++ = ' ';
1923     *cp = '\0';
1924     if (utp && !auibnc)
1925 	sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
1926     else if (!utp && auibnc)
1927 	sc->tulip_boardsw = &tulip_21040_auibnc_only_boardsw;
1928 }
1929 
1930 static void
1931 tulip_identify_cogent_nic(tulip_softc_t * const sc)
1932 {
1933     strcpy(sc->tulip_boardid, "Cogent ");
1934     if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
1935 	if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100TX_ID) {
1936 	    strcat(sc->tulip_boardid, "EM100TX ");
1937 	    sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
1938 #if defined(TULIP_COGENT_EM110TX_ID)
1939 	} else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM110TX_ID) {
1940 	    strcat(sc->tulip_boardid, "EM110TX ");
1941 	    sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
1942 #endif
1943 	} else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
1944 	    strcat(sc->tulip_boardid, "EM100FX ");
1945 	    sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
1946 	}
1947 	/*
1948 	 * Magic number (0x24001109U) is the SubVendor (0x2400) and
1949 	 * SubDevId (0x1109) for the ANA6944TX (EM440TX).
1950 	 */
1951 	if (*(uint32_t *) sc->tulip_rombuf == 0x24001109U
1952 		&& (sc->tulip_features & TULIP_HAVE_BASEROM)) {
1953 	    /*
1954 	     * Cogent (Adaptec) is still mapping all INTs to INTA of
1955 	     * first 21140.  Dumb!  Dumb!
1956 	     */
1957 	    strcat(sc->tulip_boardid, "EM440TX ");
1958 	    sc->tulip_features |= TULIP_HAVE_SHAREDINTR;
1959 	}
1960     } else if (sc->tulip_chipid == TULIP_21040) {
1961 	sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
1962     }
1963 }
1964 
1965 static void
1966 tulip_identify_accton_nic(tulip_softc_t * const sc)
1967 {
1968     strcpy(sc->tulip_boardid, "ACCTON ");
1969     switch (sc->tulip_chipid) {
1970 	case TULIP_21140A:
1971 	    strcat(sc->tulip_boardid, "EN1207 ");
1972 	    if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
1973 		sc->tulip_boardsw = &tulip_21140_accton_boardsw;
1974 	    break;
1975 	case TULIP_21140:
1976 	    strcat(sc->tulip_boardid, "EN1207TX ");
1977 	    if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
1978 		sc->tulip_boardsw = &tulip_21140_eb_boardsw;
1979             break;
1980         case TULIP_21040:
1981 	    strcat(sc->tulip_boardid, "EN1203 ");
1982             sc->tulip_boardsw = &tulip_21040_boardsw;
1983             break;
1984         case TULIP_21041:
1985 	    strcat(sc->tulip_boardid, "EN1203 ");
1986             sc->tulip_boardsw = &tulip_21041_boardsw;
1987             break;
1988 	default:
1989             sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
1990             break;
1991     }
1992 }
1993 
1994 static void
1995 tulip_identify_asante_nic(tulip_softc_t * const sc)
1996 {
1997     strcpy(sc->tulip_boardid, "Asante ");
1998     if ((sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A)
1999 	    && sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
2000 	tulip_media_info_t *mi = sc->tulip_mediainfo;
2001 	int idx;
2002 	/*
2003 	 * The Asante Fast Ethernet doesn't always ship with a valid
2004 	 * new format SROM.  So if isn't in the new format, we cheat
2005 	 * set it up as if we had.
2006 	 */
2007 
2008 	sc->tulip_gpinit = TULIP_GP_ASANTE_PINS;
2009 	sc->tulip_gpdata = 0;
2010 
2011 	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PINS|TULIP_GP_PINSET);
2012 	TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PHYRESET);
2013 	DELAY(100);
2014 	TULIP_CSR_WRITE(sc, csr_gp, 0);
2015 
2016 	mi->mi_type = TULIP_MEDIAINFO_MII;
2017 	mi->mi_gpr_length = 0;
2018 	mi->mi_gpr_offset = 0;
2019 	mi->mi_reset_length = 0;
2020 	mi->mi_reset_offset = 0;
2021 
2022 	mi->mi_phyaddr = TULIP_MII_NOPHY;
2023 	for (idx = 20; idx > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx--) {
2024 	    DELAY(10000);
2025 	    mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, 0);
2026 	}
2027 	if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
2028 	    if_printf(&sc->tulip_if, "can't find phy 0\n");
2029 	    return;
2030 	}
2031 
2032 	sc->tulip_features |= TULIP_HAVE_MII;
2033 	mi->mi_capabilities  = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
2034 	mi->mi_advertisement = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
2035 	mi->mi_full_duplex   = PHYSTS_10BASET_FD|PHYSTS_100BASETX_FD;
2036 	mi->mi_tx_threshold  = PHYSTS_10BASET|PHYSTS_10BASET_FD;
2037 	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2038 	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2039 	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2040 	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2041 	TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2042 	mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2043 	    tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2044 
2045 	sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2046     }
2047 }
2048 
2049 static void
2050 tulip_identify_compex_nic(tulip_softc_t *sc)
2051 {
2052     strcpy(sc->tulip_boardid, "COMPEX ");
2053     if (sc->tulip_chipid == TULIP_21140A) {
2054 	int root_unit;
2055 	tulip_softc_t *root_sc = NULL;
2056 
2057 	strcat(sc->tulip_boardid, "400TX/PCI ");
2058 	/*
2059 	 * All 4 chips on these boards share an interrupt.  This code
2060 	 * copied from tulip_read_macaddr.
2061 	 */
2062 	sc->tulip_features |= TULIP_HAVE_SHAREDINTR;
2063 	for (root_unit = device_get_unit(sc->tulip_dev) - 1; root_unit >= 0; root_unit--) {
2064 	    root_sc = tulips[root_unit];
2065 	    if (root_sc == NULL
2066 		|| !(root_sc->tulip_features & TULIP_HAVE_SLAVEDINTR))
2067 		break;
2068 	    root_sc = NULL;
2069 	}
2070 	if (root_sc != NULL
2071 	    && root_sc->tulip_chipid == sc->tulip_chipid
2072 	    && root_sc->tulip_pci_busno == sc->tulip_pci_busno) {
2073 	    sc->tulip_features |= TULIP_HAVE_SLAVEDINTR;
2074 	    sc->tulip_slaves = root_sc->tulip_slaves;
2075 	    root_sc->tulip_slaves = sc;
2076 	} else if(sc->tulip_features & TULIP_HAVE_SLAVEDINTR) {
2077 	    if_printf(&sc->tulip_if, "can't find master device for interrupts");
2078 	}
2079     } else {
2080 	strcat(sc->tulip_boardid, "unknown ");
2081     }
2082     /*      sc->tulip_boardsw = &tulip_21140_eb_boardsw; */
2083     return;
2084 }
2085 
2086 static int
2087 tulip_srom_decode(tulip_softc_t *sc)
2088 {
2089     u_int idx1, idx2, idx3;
2090 
2091     const tulip_srom_header_t *shp = (const tulip_srom_header_t *) &sc->tulip_rombuf[0];
2092     const tulip_srom_adapter_info_t *saip = (const tulip_srom_adapter_info_t *) (shp + 1);
2093     tulip_srom_media_t srom_media;
2094     tulip_media_info_t *mi = sc->tulip_mediainfo;
2095     const uint8_t *dp;
2096     uint32_t leaf_offset, blocks, data;
2097 
2098     for (idx1 = 0; idx1 < shp->sh_adapter_count; idx1++, saip++) {
2099 	if (shp->sh_adapter_count == 1)
2100 	    break;
2101 	if (saip->sai_device == sc->tulip_pci_devno)
2102 	    break;
2103     }
2104     /*
2105      * Didn't find the right media block for this card.
2106      */
2107     if (idx1 == shp->sh_adapter_count)
2108 	return 0;
2109 
2110     /*
2111      * Save the hardware address.
2112      */
2113     bcopy(shp->sh_ieee802_address, sc->tulip_enaddr, 6);
2114     /*
2115      * If this is a multiple port card, add the adapter index to the last
2116      * byte of the hardware address.  (if it isn't multiport, adding 0
2117      * won't hurt.
2118      */
2119     sc->tulip_enaddr[5] += idx1;
2120 
2121     leaf_offset = saip->sai_leaf_offset_lowbyte
2122 	+ saip->sai_leaf_offset_highbyte * 256;
2123     dp = sc->tulip_rombuf + leaf_offset;
2124 
2125     sc->tulip_conntype = (tulip_srom_connection_t) (dp[0] + dp[1] * 256); dp += 2;
2126 
2127     for (idx2 = 0;; idx2++) {
2128 	if (tulip_srom_conninfo[idx2].sc_type == sc->tulip_conntype
2129 	        || tulip_srom_conninfo[idx2].sc_type == TULIP_SROM_CONNTYPE_NOT_USED)
2130 	    break;
2131     }
2132     sc->tulip_connidx = idx2;
2133 
2134     if (sc->tulip_chipid == TULIP_21041) {
2135 	blocks = *dp++;
2136 	for (idx2 = 0; idx2 < blocks; idx2++) {
2137 	    tulip_media_t media;
2138 	    data = *dp++;
2139 	    srom_media = (tulip_srom_media_t) (data & 0x3F);
2140 	    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2141 		if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2142 		    break;
2143 	    }
2144 	    media = tulip_srom_mediums[idx3].sm_type;
2145 	    if (media != TULIP_MEDIA_UNKNOWN) {
2146 		if (data & TULIP_SROM_21041_EXTENDED) {
2147 		    mi->mi_type = TULIP_MEDIAINFO_SIA;
2148 		    sc->tulip_mediums[media] = mi;
2149 		    mi->mi_sia_connectivity = dp[0] + dp[1] * 256;
2150 		    mi->mi_sia_tx_rx        = dp[2] + dp[3] * 256;
2151 		    mi->mi_sia_general      = dp[4] + dp[5] * 256;
2152 		    mi++;
2153 		} else {
2154 		    switch (media) {
2155 			case TULIP_MEDIA_BNC: {
2156 			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC);
2157 			    mi++;
2158 			    break;
2159 			}
2160 			case TULIP_MEDIA_AUI: {
2161 			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI);
2162 			    mi++;
2163 			    break;
2164 			}
2165 			case TULIP_MEDIA_10BASET: {
2166 			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET);
2167 			    mi++;
2168 			    break;
2169 			}
2170 			case TULIP_MEDIA_10BASET_FD: {
2171 			    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD);
2172 			    mi++;
2173 			    break;
2174 			}
2175 			default: {
2176 			    break;
2177 			}
2178 		    }
2179 		}
2180 	    }
2181 	    if (data & TULIP_SROM_21041_EXTENDED)
2182 		dp += 6;
2183 	}
2184 #ifdef notdef
2185 	if (blocks == 0) {
2186 	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC); mi++;
2187 	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI); mi++;
2188 	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET); mi++;
2189 	    TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD); mi++;
2190 	}
2191 #endif
2192     } else {
2193 	unsigned length, type;
2194 	tulip_media_t gp_media = TULIP_MEDIA_UNKNOWN;
2195 	if (sc->tulip_features & TULIP_HAVE_GPR)
2196 	    sc->tulip_gpinit = *dp++;
2197 	blocks = *dp++;
2198 	for (idx2 = 0; idx2 < blocks; idx2++) {
2199 	    const uint8_t *ep;
2200 	    if ((*dp & 0x80) == 0) {
2201 		length = 4;
2202 		type = 0;
2203 	    } else {
2204 		length = (*dp++ & 0x7f) - 1;
2205 		type = *dp++ & 0x3f;
2206 	    }
2207 	    ep = dp + length;
2208 	    switch (type & 0x3f) {
2209 		case 0: {	/* 21140[A] GPR block */
2210 		    tulip_media_t media;
2211 		    srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
2212 		    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2213 			if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2214 			    break;
2215 		    }
2216 		    media = tulip_srom_mediums[idx3].sm_type;
2217 		    if (media == TULIP_MEDIA_UNKNOWN)
2218 			break;
2219 		    mi->mi_type = TULIP_MEDIAINFO_GPR;
2220 		    sc->tulip_mediums[media] = mi;
2221 		    mi->mi_gpdata = dp[1];
2222 		    if (media > gp_media && !TULIP_IS_MEDIA_FD(media)) {
2223 			sc->tulip_gpdata = mi->mi_gpdata;
2224 			gp_media = media;
2225 		    }
2226 		    data = dp[2] + dp[3] * 256;
2227 		    mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
2228 		    if (data & TULIP_SROM_2114X_NOINDICATOR) {
2229 			mi->mi_actmask = 0;
2230 		    } else {
2231 #if 0
2232 			mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
2233 #endif
2234 			mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
2235 			mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
2236 		    }
2237 		    mi++;
2238 		    break;
2239 		}
2240 		case 1: {	/* 21140[A] MII block */
2241 		    const u_int phyno = *dp++;
2242 		    mi->mi_type = TULIP_MEDIAINFO_MII;
2243 		    mi->mi_gpr_length = *dp++;
2244 		    mi->mi_gpr_offset = dp - sc->tulip_rombuf;
2245 		    dp += mi->mi_gpr_length;
2246 		    mi->mi_reset_length = *dp++;
2247 		    mi->mi_reset_offset = dp - sc->tulip_rombuf;
2248 		    dp += mi->mi_reset_length;
2249 
2250 		    /*
2251 		     * Before we probe for a PHY, use the GPR information
2252 		     * to select it.  If we don't, it may be inaccessible.
2253 		     */
2254 		    TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpinit|TULIP_GP_PINSET);
2255 		    for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++) {
2256 			DELAY(10);
2257 			TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx3]);
2258 		    }
2259 		    sc->tulip_phyaddr = mi->mi_phyaddr;
2260 		    for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++) {
2261 			DELAY(10);
2262 			TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx3]);
2263 		    }
2264 
2265 		    /*
2266 		     * At least write something!
2267 		     */
2268 		    if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
2269 			TULIP_CSR_WRITE(sc, csr_gp, 0);
2270 
2271 		    mi->mi_phyaddr = TULIP_MII_NOPHY;
2272 		    for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
2273 			DELAY(10000);
2274 			mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
2275 		    }
2276 		    if (mi->mi_phyaddr == TULIP_MII_NOPHY)
2277 			break;
2278 		    sc->tulip_features |= TULIP_HAVE_MII;
2279 		    mi->mi_capabilities  = dp[0] + dp[1] * 256; dp += 2;
2280 		    mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
2281 		    mi->mi_full_duplex   = dp[0] + dp[1] * 256; dp += 2;
2282 		    mi->mi_tx_threshold  = dp[0] + dp[1] * 256; dp += 2;
2283 		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2284 		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2285 		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2286 		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2287 		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2288 		    mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2289 			tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2290 		    mi++;
2291 		    break;
2292 		}
2293 		case 2: {	/* 2114[23] SIA block */
2294 		    tulip_media_t media;
2295 		    srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
2296 		    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2297 			if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2298 			    break;
2299 		    }
2300 		    media = tulip_srom_mediums[idx3].sm_type;
2301 		    if (media == TULIP_MEDIA_UNKNOWN)
2302 			break;
2303 		    mi->mi_type = TULIP_MEDIAINFO_SIA;
2304 		    sc->tulip_mediums[media] = mi;
2305 		    if (dp[0] & 0x40) {
2306 			mi->mi_sia_connectivity = dp[1] + dp[2] * 256;
2307 			mi->mi_sia_tx_rx        = dp[3] + dp[4] * 256;
2308 			mi->mi_sia_general      = dp[5] + dp[6] * 256;
2309 			dp += 6;
2310 		    } else {
2311 			switch (media) {
2312 			    case TULIP_MEDIA_BNC: {
2313 				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, BNC);
2314 				break;
2315 			    }
2316 			    case TULIP_MEDIA_AUI: {
2317 				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, AUI);
2318 				break;
2319 			    }
2320 			    case TULIP_MEDIA_10BASET: {
2321 				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET);
2322 				sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2323 				break;
2324 			    }
2325 			    case TULIP_MEDIA_10BASET_FD: {
2326 				TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET_FD);
2327 				sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2328 				break;
2329 			    }
2330 			    default: {
2331 				goto bad_media;
2332 			    }
2333 			}
2334 		    }
2335 		    mi->mi_sia_gp_control = (dp[1] + dp[2] * 256) << 16;
2336 		    mi->mi_sia_gp_data    = (dp[3] + dp[4] * 256) << 16;
2337 		    mi++;
2338 		  bad_media:
2339 		    break;
2340 		}
2341 		case 3: {	/* 2114[23] MII PHY block */
2342 		    const u_int phyno = *dp++;
2343 		    const uint8_t *dp0;
2344 		    mi->mi_type = TULIP_MEDIAINFO_MII;
2345 		    mi->mi_gpr_length = *dp++;
2346 		    mi->mi_gpr_offset = dp - sc->tulip_rombuf;
2347 		    dp += 2 * mi->mi_gpr_length;
2348 		    mi->mi_reset_length = *dp++;
2349 		    mi->mi_reset_offset = dp - sc->tulip_rombuf;
2350 		    dp += 2 * mi->mi_reset_length;
2351 
2352 		    dp0 = &sc->tulip_rombuf[mi->mi_reset_offset];
2353 		    for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++, dp0 += 2) {
2354 			DELAY(10);
2355 			TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
2356 		    }
2357 		    sc->tulip_phyaddr = mi->mi_phyaddr;
2358 		    dp0 = &sc->tulip_rombuf[mi->mi_gpr_offset];
2359 		    for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++, dp0 += 2) {
2360 			DELAY(10);
2361 			TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
2362 		    }
2363 
2364 		    if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
2365 			TULIP_CSR_WRITE(sc, csr_sia_general, 0);
2366 
2367 		    mi->mi_phyaddr = TULIP_MII_NOPHY;
2368 		    for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
2369 			DELAY(10000);
2370 			mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
2371 		    }
2372 		    if (mi->mi_phyaddr == TULIP_MII_NOPHY)
2373 			break;
2374 		    sc->tulip_features |= TULIP_HAVE_MII;
2375 		    mi->mi_capabilities  = dp[0] + dp[1] * 256; dp += 2;
2376 		    mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
2377 		    mi->mi_full_duplex   = dp[0] + dp[1] * 256; dp += 2;
2378 		    mi->mi_tx_threshold  = dp[0] + dp[1] * 256; dp += 2;
2379 		    mi->mi_mii_interrupt = dp[0] + dp[1] * 256; dp += 2;
2380 		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2381 		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2382 		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2383 		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2384 		    TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2385 		    mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2386 			tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2387 		    mi++;
2388 		    break;
2389 		}
2390 		case 4: {	/* 21143 SYM block */
2391 		    tulip_media_t media;
2392 		    srom_media = (tulip_srom_media_t) dp[0];
2393 		    for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2394 			if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2395 			    break;
2396 		    }
2397 		    media = tulip_srom_mediums[idx3].sm_type;
2398 		    if (media == TULIP_MEDIA_UNKNOWN)
2399 			break;
2400 		    mi->mi_type = TULIP_MEDIAINFO_SYM;
2401 		    sc->tulip_mediums[media] = mi;
2402 		    mi->mi_gpcontrol = (dp[1] + dp[2] * 256) << 16;
2403 		    mi->mi_gpdata    = (dp[3] + dp[4] * 256) << 16;
2404 		    data = dp[5] + dp[6] * 256;
2405 		    mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
2406 		    if (data & TULIP_SROM_2114X_NOINDICATOR) {
2407 			mi->mi_actmask = 0;
2408 		    } else {
2409 			mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
2410 			mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
2411 			mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
2412 		    }
2413 		    if (TULIP_IS_MEDIA_TP(media))
2414 			sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2415 		    mi++;
2416 		    break;
2417 		}
2418 #if 0
2419 		case 5: {	/* 21143 Reset block */
2420 		    mi->mi_type = TULIP_MEDIAINFO_RESET;
2421 		    mi->mi_reset_length = *dp++;
2422 		    mi->mi_reset_offset = dp - sc->tulip_rombuf;
2423 		    dp += 2 * mi->mi_reset_length;
2424 		    mi++;
2425 		    break;
2426 		}
2427 #endif
2428 		default: {
2429 		}
2430 	    }
2431 	    dp = ep;
2432 	}
2433     }
2434     return mi - sc->tulip_mediainfo;
2435 }
2436 
2437 static const struct {
2438     void (*vendor_identify_nic)(tulip_softc_t * const sc);
2439     unsigned char vendor_oui[3];
2440 } tulip_vendors[] = {
2441     { tulip_identify_dec_nic,		{ 0x08, 0x00, 0x2B } },
2442     { tulip_identify_dec_nic,		{ 0x00, 0x00, 0xF8 } },
2443     { tulip_identify_smc_nic,		{ 0x00, 0x00, 0xC0 } },
2444     { tulip_identify_smc_nic,		{ 0x00, 0xE0, 0x29 } },
2445     { tulip_identify_znyx_nic,		{ 0x00, 0xC0, 0x95 } },
2446     { tulip_identify_cogent_nic,	{ 0x00, 0x00, 0x92 } },
2447     { tulip_identify_asante_nic,	{ 0x00, 0x00, 0x94 } },
2448     { tulip_identify_cogent_nic,	{ 0x00, 0x00, 0xD1 } },
2449     { tulip_identify_accton_nic,	{ 0x00, 0x00, 0xE8 } },
2450     { tulip_identify_compex_nic,        { 0x00, 0x80, 0x48 } },
2451     { NULL }
2452 };
2453 
2454 /*
2455  * This deals with the vagaries of the address roms and the
2456  * brain-deadness that various vendors commit in using them.
2457  */
2458 static int
2459 tulip_read_macaddr(tulip_softc_t *sc)
2460 {
2461     u_int cksum, rom_cksum, idx;
2462     uint32_t csr;
2463     unsigned char tmpbuf[8];
2464     static const u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
2465 
2466     sc->tulip_connidx = TULIP_SROM_LASTCONNIDX;
2467 
2468     if (sc->tulip_chipid == TULIP_21040) {
2469 	TULIP_CSR_WRITE(sc, csr_enetrom, 1);
2470 	for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
2471 	    int cnt = 0;
2472 	    while (((csr = TULIP_CSR_READ(sc, csr_enetrom)) & 0x80000000L) && cnt < 10000)
2473 		cnt++;
2474 	    sc->tulip_rombuf[idx] = csr & 0xFF;
2475 	}
2476 	sc->tulip_boardsw = &tulip_21040_boardsw;
2477     } else {
2478 	if (sc->tulip_chipid == TULIP_21041) {
2479 	    /*
2480 	     * Thankfully all 21041's act the same.
2481 	     */
2482 	    sc->tulip_boardsw = &tulip_21041_boardsw;
2483 	} else {
2484 	    /*
2485 	     * Assume all 21140 board are compatible with the
2486 	     * DEC 10/100 evaluation board.  Not really valid but
2487 	     * it's the best we can do until every one switches to
2488 	     * the new SROM format.
2489 	     */
2490 
2491 	    sc->tulip_boardsw = &tulip_21140_eb_boardsw;
2492 	}
2493 	tulip_srom_read(sc);
2494 	if (tulip_srom_crcok(sc->tulip_rombuf)) {
2495 	    /*
2496 	     * SROM CRC is valid therefore it must be in the
2497 	     * new format.
2498 	     */
2499 	    sc->tulip_features |= TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM;
2500 	} else if (sc->tulip_rombuf[126] == 0xff && sc->tulip_rombuf[127] == 0xFF) {
2501 	    /*
2502 	     * No checksum is present.  See if the SROM id checks out;
2503 	     * the first 18 bytes should be 0 followed by a 1 followed
2504 	     * by the number of adapters (which we don't deal with yet).
2505 	     */
2506 	    for (idx = 0; idx < 18; idx++) {
2507 		if (sc->tulip_rombuf[idx] != 0)
2508 		    break;
2509 	    }
2510 	    if (idx == 18 && sc->tulip_rombuf[18] == 1 && sc->tulip_rombuf[19] != 0)
2511 		sc->tulip_features |= TULIP_HAVE_ISVSROM;
2512 	} else if (sc->tulip_chipid >= TULIP_21142) {
2513 	    sc->tulip_features |= TULIP_HAVE_ISVSROM;
2514 	    sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2515 	}
2516 	if ((sc->tulip_features & TULIP_HAVE_ISVSROM) && tulip_srom_decode(sc)) {
2517 	    if (sc->tulip_chipid != TULIP_21041)
2518 		sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2519 
2520 	    /*
2521 	     * If the SROM specifies more than one adapter, tag this as a
2522 	     * BASE rom.
2523 	     */
2524 	    if (sc->tulip_rombuf[19] > 1)
2525 		sc->tulip_features |= TULIP_HAVE_BASEROM;
2526 	    if (sc->tulip_boardsw == NULL)
2527 		return -6;
2528 	    goto check_oui;
2529 	}
2530     }
2531 
2532 
2533     if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) {
2534 	/*
2535 	 * Some folks don't use the standard ethernet rom format
2536 	 * but instead just put the address in the first 6 bytes
2537 	 * of the rom and let the rest be all 0xffs.  (Can we say
2538 	 * ZNYX?) (well sometimes they put in a checksum so we'll
2539 	 * start at 8).
2540 	 */
2541 	for (idx = 8; idx < 32; idx++) {
2542 	    if (sc->tulip_rombuf[idx] != 0xFF)
2543 		return -4;
2544 	}
2545 	/*
2546 	 * Make sure the address is not multicast or locally assigned
2547 	 * that the OUI is not 00-00-00.
2548 	 */
2549 	if ((sc->tulip_rombuf[0] & 3) != 0)
2550 	    return -4;
2551 	if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0
2552 		&& sc->tulip_rombuf[2] == 0)
2553 	    return -4;
2554 	bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
2555 	sc->tulip_features |= TULIP_HAVE_OKROM;
2556 	goto check_oui;
2557     } else {
2558 	/*
2559 	 * A number of makers of multiport boards (ZNYX and Cogent)
2560 	 * only put on one address ROM on their 21040 boards.  So
2561 	 * if the ROM is all zeros (or all 0xFFs), look at the
2562 	 * previous configured boards (as long as they are on the same
2563 	 * PCI bus and the bus number is non-zero) until we find the
2564 	 * master board with address ROM.  We then use its address ROM
2565 	 * as the base for this board.  (we add our relative board
2566 	 * to the last byte of its address).
2567 	 */
2568 	for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
2569 	    if (sc->tulip_rombuf[idx] != 0 && sc->tulip_rombuf[idx] != 0xFF)
2570 		break;
2571 	}
2572 	if (idx == sizeof(sc->tulip_rombuf)) {
2573 	    int root_unit;
2574 	    tulip_softc_t *root_sc = NULL;
2575 	    for (root_unit = device_get_unit(sc->tulip_dev) - 1; root_unit >= 0; root_unit--) {
2576 		root_sc = tulips[root_unit];
2577 		if (root_sc == NULL || (root_sc->tulip_features & (TULIP_HAVE_OKROM|TULIP_HAVE_SLAVEDROM)) == TULIP_HAVE_OKROM)
2578 		    break;
2579 		root_sc = NULL;
2580 	    }
2581 	    if (root_sc != NULL && (root_sc->tulip_features & TULIP_HAVE_BASEROM)
2582 		    && root_sc->tulip_chipid == sc->tulip_chipid
2583 		    && root_sc->tulip_pci_busno == sc->tulip_pci_busno) {
2584 		sc->tulip_features |= TULIP_HAVE_SLAVEDROM;
2585 		sc->tulip_boardsw = root_sc->tulip_boardsw;
2586 		strcpy(sc->tulip_boardid, root_sc->tulip_boardid);
2587 		if (sc->tulip_boardsw->bd_type == TULIP_21140_ISV) {
2588 		    bcopy(root_sc->tulip_rombuf, sc->tulip_rombuf,
2589 			  sizeof(sc->tulip_rombuf));
2590 		    if (!tulip_srom_decode(sc))
2591 			return -5;
2592 		} else {
2593 		    bcopy(root_sc->tulip_enaddr, sc->tulip_enaddr, 6);
2594 		    sc->tulip_enaddr[5] += device_get_unit(sc->tulip_dev) - device_get_unit(root_sc->tulip_dev);
2595 		}
2596 		/*
2597 		 * Now for a truly disgusting kludge: all 4 21040s on
2598 		 * the ZX314 share the same INTA line so the mapping
2599 		 * setup by the BIOS on the PCI bridge is worthless.
2600 		 * Rather than reprogramming the value in the config
2601 		 * register, we will handle this internally.
2602 		 */
2603 		if (root_sc->tulip_features & TULIP_HAVE_SHAREDINTR) {
2604 		    sc->tulip_slaves = root_sc->tulip_slaves;
2605 		    root_sc->tulip_slaves = sc;
2606 		    sc->tulip_features |= TULIP_HAVE_SLAVEDINTR;
2607 		}
2608 		return 0;
2609 	    }
2610 	}
2611     }
2612 
2613     /*
2614      * This is the standard DEC address ROM test.
2615      */
2616 
2617     if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0)
2618 	return -3;
2619 
2620     tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14];
2621     tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12];
2622     tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10];
2623     tmpbuf[6] = sc->tulip_rombuf[9];  tmpbuf[7] = sc->tulip_rombuf[8];
2624     if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0)
2625 	return -2;
2626 
2627     bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
2628 
2629     cksum = *(u_int16_t *) &sc->tulip_enaddr[0];
2630     cksum *= 2;
2631     if (cksum > 65535) cksum -= 65535;
2632     cksum += *(u_int16_t *) &sc->tulip_enaddr[2];
2633     if (cksum > 65535) cksum -= 65535;
2634     cksum *= 2;
2635     if (cksum > 65535) cksum -= 65535;
2636     cksum += *(u_int16_t *) &sc->tulip_enaddr[4];
2637     if (cksum >= 65535) cksum -= 65535;
2638 
2639     rom_cksum = *(u_int16_t *) &sc->tulip_rombuf[6];
2640 
2641     if (cksum != rom_cksum)
2642 	return -1;
2643 
2644   check_oui:
2645     /*
2646      * Check for various boards based on OUI.  Did I say braindead?
2647      */
2648     for (idx = 0; tulip_vendors[idx].vendor_identify_nic != NULL; idx++) {
2649 	if (bcmp(sc->tulip_enaddr, tulip_vendors[idx].vendor_oui, 3) == 0) {
2650 	    (*tulip_vendors[idx].vendor_identify_nic)(sc);
2651 	    break;
2652 	}
2653     }
2654 
2655     sc->tulip_features |= TULIP_HAVE_OKROM;
2656     return 0;
2657 }
2658 
2659 static void
2660 tulip_ifmedia_add(tulip_softc_t * const sc)
2661 {
2662     tulip_media_t media;
2663     int medias = 0;
2664 
2665     for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
2666 	if (sc->tulip_mediums[media] != NULL) {
2667 	    ifmedia_add(&sc->tulip_ifmedia, tulip_media_to_ifmedia[media],
2668 			0, 0);
2669 	    medias++;
2670 	}
2671     }
2672     if (medias == 0) {
2673 	sc->tulip_features |= TULIP_HAVE_NOMEDIA;
2674 	ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE, 0, 0);
2675 	ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE);
2676     } else if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
2677 	ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO, 0, 0);
2678 	ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO);
2679     } else {
2680 	ifmedia_set(&sc->tulip_ifmedia, tulip_media_to_ifmedia[sc->tulip_media]);
2681 	sc->tulip_flags |= TULIP_PRINTMEDIA;
2682 	tulip_linkup(sc, sc->tulip_media);
2683     }
2684 }
2685 
2686 static int
2687 tulip_ifmedia_change(struct ifnet *ifp)
2688 {
2689     tulip_softc_t *sc = (tulip_softc_t *)ifp->if_softc;
2690 
2691     sc->tulip_flags |= TULIP_NEEDRESET;
2692     sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
2693     sc->tulip_media = TULIP_MEDIA_UNKNOWN;
2694     if (IFM_SUBTYPE(sc->tulip_ifmedia.ifm_media) != IFM_AUTO) {
2695 	tulip_media_t media;
2696 	for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
2697 	    if (sc->tulip_mediums[media] != NULL
2698 		&& sc->tulip_ifmedia.ifm_media == tulip_media_to_ifmedia[media]) {
2699 		sc->tulip_flags |= TULIP_PRINTMEDIA;
2700 		sc->tulip_flags &= ~TULIP_DIDNWAY;
2701 		tulip_linkup(sc, media);
2702 		return 0;
2703 	    }
2704 	}
2705     }
2706     sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_WANTRXACT);
2707     tulip_reset(sc);
2708     tulip_init(sc);
2709     return 0;
2710 }
2711 
2712 /*
2713  * Media status callback
2714  */
2715 static void
2716 tulip_ifmedia_status(struct ifnet *ifp, struct ifmediareq *req)
2717 {
2718     tulip_softc_t *sc = (tulip_softc_t *)ifp->if_softc;
2719 
2720     if (sc->tulip_media == TULIP_MEDIA_UNKNOWN)
2721 	return;
2722 
2723     req->ifm_status = IFM_AVALID;
2724     if (sc->tulip_flags & TULIP_LINKUP)
2725 	req->ifm_status |= IFM_ACTIVE;
2726 
2727     req->ifm_active = tulip_media_to_ifmedia[sc->tulip_media];
2728 }
2729 
2730 static void
2731 tulip_addr_filter(tulip_softc_t *sc)
2732 {
2733     struct ifmultiaddr *ifma;
2734     u_char *addrp;
2735     int multicnt;
2736 
2737     sc->tulip_flags &= ~(TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY|TULIP_ALLMULTI);
2738     sc->tulip_flags |= TULIP_WANTSETUP|TULIP_WANTTXSTART;
2739     sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
2740     sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
2741 #if defined(IFF_ALLMULTI)
2742     if (sc->tulip_if.if_flags & IFF_ALLMULTI)
2743 	sc->tulip_flags |= TULIP_ALLMULTI ;
2744 #endif
2745 
2746     multicnt = 0;
2747     TAILQ_FOREACH(ifma, &sc->tulip_if.if_multiaddrs, ifma_link) {
2748 	    if (ifma->ifma_addr->sa_family == AF_LINK)
2749 		multicnt++;
2750     }
2751 
2752     sc->tulip_if.if_start = tulip_ifstart;	/* so the setup packet gets queued */
2753     if (multicnt > 14) {
2754 	uint32_t *sp = sc->tulip_setupdata;
2755 	u_int hash;
2756 	/*
2757 	 * Some early passes of the 21140 have broken implementations of
2758 	 * hash-perfect mode.  When we get too many multicasts for perfect
2759 	 * filtering with these chips, we need to switch into hash-only
2760 	 * mode (this is better than all-multicast on network with lots
2761 	 * of multicast traffic).
2762 	 */
2763 	if (sc->tulip_features & TULIP_HAVE_BROKEN_HASH)
2764 	    sc->tulip_flags |= TULIP_WANTHASHONLY;
2765 	else
2766 	    sc->tulip_flags |= TULIP_WANTHASHPERFECT;
2767 	/*
2768 	 * If we have more than 14 multicasts, we have
2769 	 * go into hash perfect mode (512 bit multicast
2770 	 * hash and one perfect hardware).
2771 	 */
2772 	bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata));
2773 
2774 	TAILQ_FOREACH(ifma, &sc->tulip_if.if_multiaddrs, ifma_link) {
2775 		if (ifma->ifma_addr->sa_family != AF_LINK)
2776 			continue;
2777 
2778 		hash = tulip_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
2779 #if BYTE_ORDER == BIG_ENDIAN
2780 		sp[hash >> 4] |= bswap32(1 << (hash & 0xF));
2781 #else
2782 		sp[hash >> 4] |= 1 << (hash & 0xF);
2783 #endif
2784 	}
2785 	/*
2786 	 * No reason to use a hash if we are going to be
2787 	 * receiving every multicast.
2788 	 */
2789 	if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
2790 	    hash = tulip_mchash(sc->tulip_if.if_broadcastaddr);
2791 #if BYTE_ORDER == BIG_ENDIAN
2792 	    sp[hash >> 4] |= bswap32(1 << (hash & 0xF));
2793 #else
2794 	    sp[hash >> 4] |= 1 << (hash & 0xF);
2795 #endif
2796 	    if (sc->tulip_flags & TULIP_WANTHASHONLY) {
2797 		hash = tulip_mchash(sc->tulip_enaddr);
2798 #if BYTE_ORDER == BIG_ENDIAN
2799 		sp[hash >> 4] |= bswap32(1 << (hash & 0xF));
2800 #else
2801 		sp[hash >> 4] |= 1 << (hash & 0xF);
2802 #endif
2803 	    } else {
2804 #if BYTE_ORDER == BIG_ENDIAN
2805 		sp[39] = ((u_int16_t *) sc->tulip_enaddr)[0] << 16;
2806 		sp[40] = ((u_int16_t *) sc->tulip_enaddr)[1] << 16;
2807 		sp[41] = ((u_int16_t *) sc->tulip_enaddr)[2] << 16;
2808 #else
2809 		sp[39] = ((u_int16_t *) sc->tulip_enaddr)[0];
2810 		sp[40] = ((u_int16_t *) sc->tulip_enaddr)[1];
2811 		sp[41] = ((u_int16_t *) sc->tulip_enaddr)[2];
2812 #endif
2813 	    }
2814 	}
2815     }
2816     if ((sc->tulip_flags & (TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY)) == 0) {
2817 	uint32_t *sp = sc->tulip_setupdata;
2818 	int idx = 0;
2819 	if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
2820 	    /*
2821 	     * Else can get perfect filtering for 16 addresses.
2822 	     */
2823 	    TAILQ_FOREACH(ifma, &sc->tulip_if.if_multiaddrs, ifma_link) {
2824 		    if (ifma->ifma_addr->sa_family != AF_LINK)
2825 			    continue;
2826 		    addrp = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
2827 #if BYTE_ORDER == BIG_ENDIAN
2828 		    *sp++ = ((u_int16_t *) addrp)[0] << 16;
2829 		    *sp++ = ((u_int16_t *) addrp)[1] << 16;
2830 		    *sp++ = ((u_int16_t *) addrp)[2] << 16;
2831 #else
2832 		    *sp++ = ((u_int16_t *) addrp)[0];
2833 		    *sp++ = ((u_int16_t *) addrp)[1];
2834 		    *sp++ = ((u_int16_t *) addrp)[2];
2835 #endif
2836 		    idx++;
2837 	    }
2838 	    /*
2839 	     * Add the broadcast address.
2840 	     */
2841 	    idx++;
2842 #if BYTE_ORDER == BIG_ENDIAN
2843 	    *sp++ = 0xFFFF << 16;
2844 	    *sp++ = 0xFFFF << 16;
2845 	    *sp++ = 0xFFFF << 16;
2846 #else
2847 	    *sp++ = 0xFFFF;
2848 	    *sp++ = 0xFFFF;
2849 	    *sp++ = 0xFFFF;
2850 #endif
2851 	}
2852 	/*
2853 	 * Pad the rest with our hardware address
2854 	 */
2855 	for (; idx < 16; idx++) {
2856 #if BYTE_ORDER == BIG_ENDIAN
2857 	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[0] << 16;
2858 	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[1] << 16;
2859 	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[2] << 16;
2860 #else
2861 	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[0];
2862 	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[1];
2863 	    *sp++ = ((u_int16_t *) sc->tulip_enaddr)[2];
2864 #endif
2865 	}
2866     }
2867 #if defined(IFF_ALLMULTI)
2868     if (sc->tulip_flags & TULIP_ALLMULTI)
2869 	sc->tulip_if.if_flags |= IFF_ALLMULTI;
2870 #endif
2871 }
2872 
2873 static void
2874 tulip_reset(tulip_softc_t *sc)
2875 {
2876     tulip_ringinfo_t *ri;
2877     tulip_desc_t *di;
2878     uint32_t inreset = (sc->tulip_flags & TULIP_INRESET);
2879 
2880     /*
2881      * Brilliant.  Simply brilliant.  When switching modes/speeds
2882      * on a 2114*, you need to set the appriopriate MII/PCS/SCL/PS
2883      * bits in CSR6 and then do a software reset to get the 21140
2884      * to properly reset its internal pathways to the right places.
2885      *   Grrrr.
2886      */
2887     if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0
2888 	    && sc->tulip_boardsw->bd_media_preset != NULL)
2889 	(*sc->tulip_boardsw->bd_media_preset)(sc);
2890 
2891     TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
2892     DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
2893 		   33MHz that comes to two microseconds but wait a
2894 		   bit longer anyways) */
2895 
2896     if (!inreset) {
2897 	sc->tulip_flags |= TULIP_INRESET;
2898 	sc->tulip_flags &= ~(TULIP_NEEDRESET|TULIP_RXBUFSLOW);
2899 	ifq_clr_oactive(&sc->tulip_if.if_snd);
2900 	sc->tulip_if.if_start = tulip_ifstart;
2901     }
2902 
2903     TULIP_CSR_WRITE(sc, csr_txlist, TULIP_KVATOPHYS(sc, &sc->tulip_txinfo.ri_first[0]));
2904     TULIP_CSR_WRITE(sc, csr_rxlist, TULIP_KVATOPHYS(sc, &sc->tulip_rxinfo.ri_first[0]));
2905     TULIP_CSR_WRITE(sc, csr_busmode,
2906 		    (1 << (3 /*pci_max_burst_len*/ + 8))
2907 		    |TULIP_BUSMODE_CACHE_ALIGN8
2908 		    |TULIP_BUSMODE_READMULTIPLE
2909 		    |(BYTE_ORDER != LITTLE_ENDIAN ?
2910 		      TULIP_BUSMODE_DESC_BIGENDIAN : 0));
2911 
2912     sc->tulip_txtimer = 0;
2913     sc->tulip_txq.ifq_maxlen = TULIP_TXDESCS;
2914     /*
2915      * Free all the mbufs that were on the transmit ring.
2916      */
2917     IF_DRAIN(&sc->tulip_txq);
2918 
2919     ri = &sc->tulip_txinfo;
2920     ri->ri_nextin = ri->ri_nextout = ri->ri_first;
2921     ri->ri_free = ri->ri_max;
2922     for (di = ri->ri_first; di < ri->ri_last; di++)
2923 	di->d_status = 0;
2924 
2925     /*
2926      * We need to collect all the mbufs were on the
2927      * receive ring before we reinit it either to put
2928      * them back on or to know if we have to allocate
2929      * more.
2930      */
2931     ri = &sc->tulip_rxinfo;
2932     ri->ri_nextin = ri->ri_nextout = ri->ri_first;
2933     ri->ri_free = ri->ri_max;
2934     for (di = ri->ri_first; di < ri->ri_last; di++) {
2935 	di->d_status = 0;
2936 	di->d_length1 = 0; di->d_addr1 = 0;
2937 	di->d_length2 = 0; di->d_addr2 = 0;
2938     }
2939     IF_DRAIN(&sc->tulip_rxq);
2940 
2941     /*
2942      * If tulip_reset is being called recurisvely, exit quickly knowing
2943      * that when the outer tulip_reset returns all the right stuff will
2944      * have happened.
2945      */
2946     if (inreset)
2947 	return;
2948 
2949     sc->tulip_intrmask |= TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
2950 	|TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
2951 	|TULIP_STS_TXUNDERFLOW|TULIP_STS_TXBABBLE
2952 	|TULIP_STS_RXSTOPPED;
2953 
2954     if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0)
2955 	(*sc->tulip_boardsw->bd_media_select)(sc);
2956     tulip_media_print(sc);
2957     if (sc->tulip_features & TULIP_HAVE_DUALSENSE)
2958 	TULIP_CSR_WRITE(sc, csr_sia_status, TULIP_CSR_READ(sc, csr_sia_status));
2959 
2960     sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP|TULIP_INRESET
2961 			 |TULIP_RXACT);
2962     tulip_addr_filter(sc);
2963 }
2964 
2965 static void
2966 tulip_init(tulip_softc_t * const sc)
2967 {
2968     if (sc->tulip_if.if_flags & IFF_UP) {
2969 	if ((sc->tulip_if.if_flags & IFF_RUNNING) == 0) {
2970 	    /* initialize the media */
2971 	    tulip_reset(sc);
2972 	}
2973 	sc->tulip_if.if_flags |= IFF_RUNNING;
2974 	if (sc->tulip_if.if_flags & IFF_PROMISC) {
2975 	    sc->tulip_flags |= TULIP_PROMISC;
2976 	    sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
2977 	    sc->tulip_intrmask |= TULIP_STS_TXINTR;
2978 	} else {
2979 	    sc->tulip_flags &= ~TULIP_PROMISC;
2980 	    sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
2981 	    if (sc->tulip_flags & TULIP_ALLMULTI) {
2982 		sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
2983 	    } else {
2984 		sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
2985 	    }
2986 	}
2987 	sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
2988 	if ((sc->tulip_flags & (TULIP_TXPROBE_ACTIVE|TULIP_WANTSETUP)) == 0) {
2989 	    tulip_rx_intr(sc);
2990 	    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
2991 	    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
2992 	} else {
2993 	    ifq_set_oactive(&sc->tulip_if.if_snd);
2994 	    sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
2995 	    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
2996 	}
2997 	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
2998 	TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
2999 	if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
3000 	    tulip_txput_setup(sc);
3001     } else {
3002 	sc->tulip_if.if_flags &= ~IFF_RUNNING;
3003 	tulip_reset(sc);
3004     }
3005 }
3006 
3007 static void
3008 tulip_rx_intr(tulip_softc_t *sc)
3009 {
3010     tulip_ringinfo_t *ri = &sc->tulip_rxinfo;
3011     struct ifnet *ifp = &sc->tulip_if;
3012     int fillok = 1;
3013 
3014     for (;;) {
3015 	tulip_desc_t *eop = ri->ri_nextin;
3016 	int total_len = 0, last_offset = 0;
3017 	struct mbuf *ms = NULL, *me = NULL;
3018 	int accept = 0;
3019 
3020 	if (fillok && sc->tulip_rxq.ifq_len < TULIP_RXQ_TARGET)
3021 	    goto queue_mbuf;
3022 
3023 	/*
3024 	 * If the TULIP has no descriptors, there can't be any receive
3025 	 * descriptors to process.
3026  	 */
3027 	if (eop == ri->ri_nextout)
3028 	    break;
3029 
3030 	/*
3031 	 * 90% of the packets will fit in one descriptor.  So we optimize
3032 	 * for that case.
3033 	 */
3034 	TULIP_RXDESC_POSTSYNC(sc, eop, sizeof(*eop));
3035 	if ((((volatile tulip_desc_t *) eop)->d_status & (TULIP_DSTS_OWNER|TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) == (TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) {
3036 	    IF_DEQUEUE(&sc->tulip_rxq, ms);
3037 	    me = ms;
3038 	} else {
3039 	    /*
3040 	     * If still owned by the TULIP, don't touch it.
3041 	     */
3042 	    if (((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER)
3043 		break;
3044 
3045 	    /*
3046 	     * It is possible (though improbable unless the BIG_PACKET support
3047 	     * is enabled or MCLBYTES < 1518) for a received packet to cross
3048 	     * more than one receive descriptor.
3049 	     */
3050 	    while ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_RxLASTDESC) == 0) {
3051 		if (++eop == ri->ri_last)
3052 		    eop = ri->ri_first;
3053 		TULIP_RXDESC_POSTSYNC(sc, eop, sizeof(*eop));
3054 		if (eop == ri->ri_nextout || ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER))) {
3055 		    return;
3056 		}
3057 		total_len++;
3058 	    }
3059 
3060 	    /*
3061 	     * Dequeue the first buffer for the start of the packet.  Hopefully
3062 	     * this will be the only one we need to dequeue.  However, if the
3063 	     * packet consumed multiple descriptors, then we need to dequeue
3064 	     * those buffers and chain to the starting mbuf.  All buffers but
3065 	     * the last buffer have the same length so we can set that now.
3066 	     * (we add to last_offset instead of multiplying since we normally
3067 	     * won't go into the loop and thereby saving a ourselves from
3068 	     * doing a multiplication by 0 in the normal case).
3069 	     */
3070 	    IF_DEQUEUE(&sc->tulip_rxq, ms);
3071 	    for (me = ms; total_len > 0; total_len--) {
3072 		me->m_len = TULIP_RX_BUFLEN;
3073 		last_offset += TULIP_RX_BUFLEN;
3074 		IF_DEQUEUE(&sc->tulip_rxq, me->m_next);
3075 		me = me->m_next;
3076 	    }
3077 	}
3078 
3079 	/*
3080 	 *  Now get the size of received packet (minus the CRC).
3081 	 */
3082 	total_len = ((eop->d_status >> 16) & 0x7FFF) - 4;
3083 	if ((sc->tulip_flags & TULIP_RXIGNORE) == 0
3084 		&& ((eop->d_status & TULIP_DSTS_ERRSUM) == 0
3085 #ifdef BIG_PACKET
3086 		     || (total_len <= sc->tulip_if.if_mtu + sizeof(struct ether_header) &&
3087 			 (eop->d_status & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxRUNT|
3088 					  TULIP_DSTS_RxCOLLSEEN|TULIP_DSTS_RxBADCRC|
3089 					  TULIP_DSTS_RxOVERFLOW)) == 0)
3090 #endif
3091 		)) {
3092 	    me->m_len = total_len - last_offset;
3093 
3094 	    sc->tulip_flags |= TULIP_RXACT;
3095 	    accept = 1;
3096 	} else {
3097 	    IFNET_STAT_INC(ifp, ierrors, 1);
3098 	    if (eop->d_status & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxOVERFLOW|TULIP_DSTS_RxWATCHDOG)) {
3099 		sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
3100 	    } else {
3101 		if (eop->d_status & TULIP_DSTS_RxTOOLONG) {
3102 		    sc->tulip_dot3stats.dot3StatsFrameTooLongs++;
3103 		}
3104 		if (eop->d_status & TULIP_DSTS_RxBADCRC) {
3105 		    if (eop->d_status & TULIP_DSTS_RxDRBBLBIT) {
3106 			sc->tulip_dot3stats.dot3StatsAlignmentErrors++;
3107 		    } else {
3108 			sc->tulip_dot3stats.dot3StatsFCSErrors++;
3109 		    }
3110 		}
3111 	    }
3112 	}
3113 	IFNET_STAT_INC(ifp, ipackets, 1);
3114 	if (++eop == ri->ri_last)
3115 	    eop = ri->ri_first;
3116 	ri->ri_nextin = eop;
3117       queue_mbuf:
3118 	/*
3119 	 * Either we are priming the TULIP with mbufs (m == NULL)
3120 	 * or we are about to accept an mbuf for the upper layers
3121 	 * so we need to allocate an mbuf to replace it.  If we
3122 	 * can't replace it, send up it anyways.  This may cause
3123 	 * us to drop packets in the future but that's better than
3124 	 * being caught in livelock.
3125 	 *
3126 	 * Note that if this packet crossed multiple descriptors
3127 	 * we don't even try to reallocate all the mbufs here.
3128 	 * Instead we rely on the test of the beginning of
3129 	 * the loop to refill for the extra consumed mbufs.
3130 	 */
3131 	if (accept || ms == NULL) {
3132 	    struct mbuf *m0;
3133 
3134 #if defined(TULIP_COPY_RXDATA)
3135 	    if (!accept || total_len >= (MHLEN - 2))
3136 		m0 = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
3137 	    else
3138 		m0 = m_gethdr(MB_DONTWAIT, MT_DATA);
3139 #else
3140 	    m0 = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
3141 #endif
3142 
3143 	    if (accept
3144 #if defined(TULIP_COPY_RXDATA)
3145 		&& m0 != NULL
3146 #endif
3147 		) {
3148 #if !defined(TULIP_COPY_RXDATA)
3149 		ms->m_pkthdr.len = total_len;
3150 		ifp->if_input(ifp, ms, NULL, -1);
3151 #else
3152 #ifdef BIG_PACKET
3153 #error BIG_PACKET is incompatible with TULIP_COPY_RXDATA
3154 #endif
3155 		m0->m_data += 2;	/* align data after header */
3156 		m_copydata(ms, 0, total_len, mtod(m0, caddr_t));
3157 		m0->m_len = m0->m_pkthdr.len = total_len;
3158 		m0->m_pkthdr.rcvif = ifp;
3159 		ifp->if_input(ifp, m0, NULL, -1);
3160 		m0 = ms;
3161 #endif /* ! TULIP_COPY_RXDATA */
3162 	    }
3163 	    ms = m0;
3164 	}
3165 	if (ms == NULL) {
3166 	    /*
3167 	     * Couldn't allocate a new buffer.  Don't bother
3168 	     * trying to replenish the receive queue.
3169 	     */
3170 	    fillok = 0;
3171 	    sc->tulip_flags |= TULIP_RXBUFSLOW;
3172 	    continue;
3173 	}
3174 	/*
3175 	 * Now give the buffer(s) to the TULIP and save in our
3176 	 * receive queue.
3177 	 */
3178 	do {
3179 	    tulip_desc_t * const nextout = ri->ri_nextout;
3180 	    nextout->d_addr1 = TULIP_KVATOPHYS(sc, mtod(ms, caddr_t));
3181 	    nextout->d_length1 = TULIP_RX_BUFLEN;
3182 	    nextout->d_status = TULIP_DSTS_OWNER;
3183 	    TULIP_RXDESC_POSTSYNC(sc, nextout, sizeof(u_int32_t));
3184 	    if (++ri->ri_nextout == ri->ri_last)
3185 		ri->ri_nextout = ri->ri_first;
3186 	    me = ms->m_next;
3187 	    ms->m_next = NULL;
3188 	    IF_ENQUEUE(&sc->tulip_rxq, ms);
3189 	} while ((ms = me) != NULL);
3190 
3191 	if (sc->tulip_rxq.ifq_len >= TULIP_RXQ_TARGET)
3192 	    sc->tulip_flags &= ~TULIP_RXBUFSLOW;
3193     }
3194 }
3195 
3196 static int
3197 tulip_tx_intr(tulip_softc_t *sc)
3198 {
3199     tulip_ringinfo_t *ri = &sc->tulip_txinfo;
3200     struct mbuf *m;
3201     int xmits = 0;
3202     int descs = 0;
3203 
3204     while (ri->ri_free < ri->ri_max) {
3205 	uint32_t d_flag;
3206 
3207 	TULIP_TXDESC_POSTSYNC(sc, ri->ri_nextin, sizeof(*ri->ri_nextin));
3208 	if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
3209 	    break;
3210 
3211 	ri->ri_free++;
3212 	descs++;
3213 	d_flag = ri->ri_nextin->d_flag;
3214 	if (d_flag & TULIP_DFLAG_TxLASTSEG) {
3215 	    if (d_flag & TULIP_DFLAG_TxSETUPPKT) {
3216 		/*
3217 		 * We've just finished processing a setup packet.
3218 		 * Mark that we finished it.  If there's not
3219 		 * another pending, startup the TULIP receiver.
3220 		 * Make sure we ack the RXSTOPPED so we won't get
3221 		 * an abormal interrupt indication.
3222 		 */
3223 		TULIP_TXMAP_POSTSYNC(sc, sc->tulip_setupmap);
3224 		sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_HASHONLY);
3225 		if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxINVRSFILT)
3226 		    sc->tulip_flags |= TULIP_HASHONLY;
3227 		if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == 0) {
3228 		    tulip_rx_intr(sc);
3229 		    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
3230 		    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
3231 		    TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
3232 		    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3233 		    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3234 		}
3235 	    } else {
3236 		const uint32_t d_status = ri->ri_nextin->d_status;
3237 		IF_DEQUEUE(&sc->tulip_txq, m);
3238 		if (m != NULL) {
3239 		    m_freem(m);
3240 		}
3241 		if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
3242 		    tulip_mediapoll_event_t event = TULIP_MEDIAPOLL_TXPROBE_OK;
3243 		    if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxEXCCOLL)) {
3244 			event = TULIP_MEDIAPOLL_TXPROBE_FAILED;
3245 		    }
3246 		    (*sc->tulip_boardsw->bd_media_poll)(sc, event);
3247 		    /*
3248 		     * Escape from the loop before media poll has reset the TULIP!
3249 		     */
3250 		    break;
3251 		} else {
3252 		    xmits++;
3253 		    if (d_status & TULIP_DSTS_ERRSUM) {
3254 			IFNET_STAT_INC(&sc->tulip_if, oerrors, 1);
3255 			if (d_status & TULIP_DSTS_TxEXCCOLL)
3256 			    sc->tulip_dot3stats.dot3StatsExcessiveCollisions++;
3257 			if (d_status & TULIP_DSTS_TxLATECOLL)
3258 			    sc->tulip_dot3stats.dot3StatsLateCollisions++;
3259 			if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxCARRLOSS))
3260 			    sc->tulip_dot3stats.dot3StatsCarrierSenseErrors++;
3261 			if (d_status & (TULIP_DSTS_TxUNDERFLOW|TULIP_DSTS_TxBABBLE))
3262 			    sc->tulip_dot3stats.dot3StatsInternalMacTransmitErrors++;
3263 			if (d_status & TULIP_DSTS_TxUNDERFLOW)
3264 			    sc->tulip_dot3stats.dot3StatsInternalTransmitUnderflows++;
3265 			if (d_status & TULIP_DSTS_TxBABBLE)
3266 			    sc->tulip_dot3stats.dot3StatsInternalTransmitBabbles++;
3267 		    } else {
3268 			u_int32_t collisions =
3269 			    (d_status & TULIP_DSTS_TxCOLLMASK)
3270 				>> TULIP_DSTS_V_TxCOLLCNT;
3271 			IFNET_STAT_INC(&sc->tulip_if, collisions, collisions);
3272 			if (collisions == 1)
3273 			    sc->tulip_dot3stats.dot3StatsSingleCollisionFrames++;
3274 			else if (collisions > 1)
3275 			    sc->tulip_dot3stats.dot3StatsMultipleCollisionFrames++;
3276 			else if (d_status & TULIP_DSTS_TxDEFERRED)
3277 			    sc->tulip_dot3stats.dot3StatsDeferredTransmissions++;
3278 			/*
3279 			 * SQE is only valid for 10baseT/BNC/AUI when not
3280 			 * running in full-duplex.  In order to speed up the
3281 			 * test, the corresponding bit in tulip_flags needs to
3282 			 * set as well to get us to count SQE Test Errors.
3283 			 */
3284 			if (d_status & TULIP_DSTS_TxNOHRTBT & sc->tulip_flags)
3285 			    sc->tulip_dot3stats.dot3StatsSQETestErrors++;
3286 		    }
3287 		}
3288 	    }
3289 	}
3290 
3291 	if (++ri->ri_nextin == ri->ri_last)
3292 	    ri->ri_nextin = ri->ri_first;
3293 
3294 	if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
3295 	    ifq_clr_oactive(&sc->tulip_if.if_snd);
3296     }
3297     /*
3298      * If nothing left to transmit, disable the timer.
3299      * Else if progress, reset the timer back to 2 ticks.
3300      *
3301      * XXX 2 bit field, TXTIMER is 4?
3302      */
3303     if (ri->ri_free == ri->ri_max || (sc->tulip_flags & TULIP_TXPROBE_ACTIVE))
3304 	sc->tulip_txtimer = 0;
3305     else if (xmits > 0)
3306 	sc->tulip_txtimer = (TULIP_TXTIMER & 3);
3307     IFNET_STAT_INC(&sc->tulip_if, opackets, xmits);
3308     return descs;
3309 }
3310 
3311 static void
3312 tulip_print_abnormal_interrupt(tulip_softc_t *sc, uint32_t csr)
3313 {
3314     const char * const *msgp = tulip_status_bits;
3315     const char *sep;
3316     uint32_t mask;
3317     const char thrsh[] = "72|128\0\0\0" "96|256\0\0\0" "128|512\0\0" "160|1024";
3318 
3319     csr &= (1 << (NELEM(tulip_status_bits))) - 1;
3320     if_printf(&sc->tulip_if, "abnormal interrupt:");
3321     for (sep = " ", mask = 1; mask <= csr; mask <<= 1, msgp++) {
3322 	if ((csr & mask) && *msgp != NULL) {
3323 	    kprintf("%s%s", sep, *msgp);
3324 	    if (mask == TULIP_STS_TXUNDERFLOW && (sc->tulip_flags & TULIP_NEWTXTHRESH)) {
3325 		sc->tulip_flags &= ~TULIP_NEWTXTHRESH;
3326 		if (sc->tulip_cmdmode & TULIP_CMD_STOREFWD) {
3327 		    kprintf(" (switching to store-and-forward mode)");
3328 		} else {
3329 		    kprintf(" (raising TX threshold to %s)",
3330 			   &thrsh[9 * ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) >> 14)]);
3331 		}
3332 	    }
3333 	    sep = ", ";
3334 	}
3335     }
3336     kprintf("\n");
3337 }
3338 
3339 static void
3340 tulip_intr_handler(tulip_softc_t *sc)
3341 {
3342     uint32_t csr;
3343 
3344     while ((csr = TULIP_CSR_READ(sc, csr_status)) & sc->tulip_intrmask) {
3345 	TULIP_CSR_WRITE(sc, csr_status, csr);
3346 
3347 	if (csr & TULIP_STS_SYSERROR) {
3348 	    sc->tulip_last_system_error = (csr & TULIP_STS_ERRORMASK) >> TULIP_STS_ERR_SHIFT;
3349 	    if (sc->tulip_flags & TULIP_NOMESSAGES) {
3350 		sc->tulip_flags |= TULIP_SYSTEMERROR;
3351 	    } else {
3352 		if_printf(&sc->tulip_if, "system error: %s\n",
3353 		       tulip_system_errors[sc->tulip_last_system_error]);
3354 	    }
3355 	    sc->tulip_flags |= TULIP_NEEDRESET;
3356 	    sc->tulip_system_errors++;
3357 	    break;
3358 	}
3359 	if (csr & (TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL) & sc->tulip_intrmask) {
3360 	    if (sc->tulip_boardsw->bd_media_poll != NULL) {
3361 		(*sc->tulip_boardsw->bd_media_poll)(sc, csr & TULIP_STS_LINKFAIL
3362 						    ? TULIP_MEDIAPOLL_LINKFAIL
3363 						    : TULIP_MEDIAPOLL_LINKPASS);
3364 		csr &= ~TULIP_STS_ABNRMLINTR;
3365 	    }
3366 	    tulip_media_print(sc);
3367 	}
3368 	if (csr & (TULIP_STS_RXINTR|TULIP_STS_RXNOBUF)) {
3369 	    u_int32_t misses = TULIP_CSR_READ(sc, csr_missed_frames);
3370 	    if (csr & TULIP_STS_RXNOBUF)
3371 		sc->tulip_dot3stats.dot3StatsMissedFrames += misses & 0xFFFF;
3372 	    /*
3373 	     * Pass 2.[012] of the 21140A-A[CDE] may hang and/or corrupt data
3374 	     * on receive overflows.
3375 	     */
3376 	   if ((misses & 0x0FFE0000) && (sc->tulip_features & TULIP_HAVE_RXBADOVRFLW)) {
3377 		sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
3378 		/*
3379 		 * Stop the receiver process and spin until it's stopped.
3380 		 * Tell rx_intr to drop the packets it dequeues.
3381 		 */
3382 		TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode & ~TULIP_CMD_RXRUN);
3383 		while ((TULIP_CSR_READ(sc, csr_status) & TULIP_STS_RXSTOPPED) == 0)
3384 		    ;
3385 		TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
3386 		sc->tulip_flags |= TULIP_RXIGNORE;
3387 	    }
3388 	    tulip_rx_intr(sc);
3389 	    if (sc->tulip_flags & TULIP_RXIGNORE) {
3390 		/*
3391 		 * Restart the receiver.
3392 		 */
3393 		sc->tulip_flags &= ~TULIP_RXIGNORE;
3394 		TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3395 	    }
3396 	}
3397 	if (csr & TULIP_STS_ABNRMLINTR) {
3398 	    u_int32_t tmp = csr & sc->tulip_intrmask
3399 		& ~(TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR);
3400 	    if (csr & TULIP_STS_TXUNDERFLOW) {
3401 		if ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) != TULIP_CMD_THRSHLD160) {
3402 		    sc->tulip_cmdmode += TULIP_CMD_THRSHLD96;
3403 		    sc->tulip_flags |= TULIP_NEWTXTHRESH;
3404 		} else if (sc->tulip_features & TULIP_HAVE_STOREFWD) {
3405 		    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD;
3406 		    sc->tulip_flags |= TULIP_NEWTXTHRESH;
3407 		}
3408 	    }
3409 	    if (sc->tulip_flags & TULIP_NOMESSAGES) {
3410 		sc->tulip_statusbits |= tmp;
3411 	    } else {
3412 		tulip_print_abnormal_interrupt(sc, tmp);
3413 		sc->tulip_flags |= TULIP_NOMESSAGES;
3414 	    }
3415 	    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3416 	}
3417 	if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_TXPROBE_ACTIVE|TULIP_DOINGSETUP|TULIP_PROMISC)) {
3418 	    tulip_tx_intr(sc);
3419 	    if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
3420 		if_devstart(&sc->tulip_if);
3421 	}
3422     }
3423     if (sc->tulip_flags & TULIP_NEEDRESET) {
3424 	tulip_reset(sc);
3425 	tulip_init(sc);
3426     }
3427 }
3428 
3429 static void
3430 tulip_intr_shared(void *arg)
3431 {
3432     tulip_softc_t *sc;
3433 
3434     for (sc = arg; sc != NULL; sc = sc->tulip_slaves)
3435 	tulip_intr_handler(sc);
3436 }
3437 
3438 static void
3439 tulip_intr_normal(void *arg)
3440 {
3441     tulip_softc_t *sc = (tulip_softc_t *)arg;
3442 
3443     tulip_intr_handler(sc);
3444 }
3445 
3446 static struct mbuf *
3447 tulip_txput(tulip_softc_t *sc, struct mbuf *m)
3448 {
3449     tulip_ringinfo_t *ri = &sc->tulip_txinfo;
3450     tulip_desc_t *eop, *nextout;
3451     int segcnt, free;
3452     uint32_t d_status;
3453     struct mbuf *m0;
3454 
3455     /*
3456      * Now we try to fill in our transmit descriptors.  This is
3457      * a bit reminiscent of going on the Ark two by two
3458      * since each descriptor for the TULIP can describe
3459      * two buffers.  So we advance through packet filling
3460      * each of the two entries at a time to to fill each
3461      * descriptor.  Clear the first and last segment bits
3462      * in each descriptor (actually just clear everything
3463      * but the end-of-ring or chain bits) to make sure
3464      * we don't get messed up by previously sent packets.
3465      *
3466      * We may fail to put the entire packet on the ring if
3467      * there is either not enough ring entries free or if the
3468      * packet has more than MAX_TXSEG segments.  In the former
3469      * case we will just wait for the ring to empty.  In the
3470      * latter case we have to recopy.
3471      */
3472   again:
3473     m0 = m;
3474     d_status = 0;
3475     eop = nextout = ri->ri_nextout;
3476     segcnt = 0;
3477     free = ri->ri_free;
3478 
3479     do {
3480 	int len = m0->m_len;
3481 	caddr_t addr = mtod(m0, caddr_t);
3482 	unsigned clsize = PAGE_SIZE - (((uintptr_t) addr) & (PAGE_SIZE-1));
3483 
3484 	while (len > 0) {
3485 	    unsigned slen = min(len, clsize);
3486 #ifdef BIG_PACKET
3487 	    int partial = 0;
3488 	    if (slen >= 2048)
3489 		slen = 2040, partial = 1;
3490 #endif
3491 	    segcnt++;
3492 	    if (segcnt > TULIP_MAX_TXSEG) {
3493 		/*
3494 		 * The packet exceeds the number of transmit buffer
3495 		 * entries that we can use for one packet, so we have
3496 		 * recopy it into one mbuf and then try again.
3497 		 */
3498 		m0 = m_defrag(m, MB_DONTWAIT);
3499 		if (m0 == NULL)
3500 		    goto finish;
3501 		m = m0;
3502 		goto again;
3503 	    }
3504 	    if (segcnt & 1) {
3505 		if (--free == 0) {
3506 		    /*
3507 		     * See if there's any unclaimed space in the
3508 		     * transmit ring.
3509 		     */
3510 		    if ((free += tulip_tx_intr(sc)) == 0) {
3511 			/*
3512 			 * There's no more room but since nothing
3513 			 * has been committed at this point, just
3514 			 * show output is active, put back the
3515 			 * mbuf and return.
3516 			 */
3517 			sc->tulip_flags |= TULIP_WANTTXSTART;
3518 			goto finish;
3519 		    }
3520 		}
3521 		eop = nextout;
3522 		if (++nextout == ri->ri_last)
3523 		    nextout = ri->ri_first;
3524 		eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
3525 		eop->d_status = d_status;
3526 		eop->d_addr1 = TULIP_KVATOPHYS(sc, addr);
3527 		eop->d_length1 = slen;
3528 	    } else {
3529 		/*
3530 		 *  Fill in second half of descriptor
3531 		 */
3532 		eop->d_addr2 = TULIP_KVATOPHYS(sc, addr);
3533 		eop->d_length2 = slen;
3534 	    }
3535 	    d_status = TULIP_DSTS_OWNER;
3536 	    len -= slen;
3537 	    addr += slen;
3538 #ifdef BIG_PACKET
3539 	    if (partial)
3540 		continue;
3541 #endif
3542 	    clsize = PAGE_SIZE;
3543 	}
3544     } while ((m0 = m0->m_next) != NULL);
3545 
3546     BPF_MTAP(&sc->tulip_if, m);
3547 
3548     /*
3549      * The descriptors have been filled in.  Now get ready
3550      * to transmit.
3551      */
3552     IF_ENQUEUE(&sc->tulip_txq, m);
3553     m = NULL;
3554 
3555     /*
3556      * Make sure the next descriptor after this packet is owned
3557      * by us since it may have been set up above if we ran out
3558      * of room in the ring.
3559      */
3560     nextout->d_status = 0;
3561     TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(u_int32_t));
3562 
3563     /*
3564      * If we only used the first segment of the last descriptor,
3565      * make sure the second segment will not be used.
3566      */
3567     if (segcnt & 1) {
3568 	eop->d_addr2 = 0;
3569 	eop->d_length2 = 0;
3570     }
3571 
3572     /*
3573      * Mark the last and first segments, indicate we want a transmit
3574      * complete interrupt, and tell it to transmit!
3575      */
3576     eop->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
3577 
3578     /*
3579      * Note that ri->ri_nextout is still the start of the packet
3580      * and until we set the OWNER bit, we can still back out of
3581      * everything we have done.
3582      */
3583     ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
3584     ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
3585     TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout, sizeof(u_int32_t));
3586 
3587     /*
3588      * This advances the ring for us.
3589      */
3590     ri->ri_nextout = nextout;
3591     ri->ri_free = free;
3592 
3593     if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
3594 	TULIP_CSR_WRITE(sc, csr_txpoll, 1);
3595 	ifq_set_oactive(&sc->tulip_if.if_snd);
3596 	sc->tulip_if.if_start = tulip_ifstart;
3597 	return NULL;
3598     }
3599 
3600     /*
3601      * switch back to the single queueing ifstart.
3602      *
3603      * XXX 2 bit field, TXTIMER is 4?
3604      */
3605     sc->tulip_flags &= ~TULIP_WANTTXSTART;
3606     if (sc->tulip_txtimer == 0)
3607 	sc->tulip_txtimer = TULIP_TXTIMER & 3;
3608 
3609     /*
3610      * If we want a txstart, there must be not enough space in the
3611      * transmit ring.  So we want to enable transmit done interrupts
3612      * so we can immediately reclaim some space.  When the transmit
3613      * interrupt is posted, the interrupt handler will call tx_intr
3614      * to reclaim space and then txstart (since WANTTXSTART is set).
3615      * txstart will move the packet into the transmit ring and clear
3616      * WANTTXSTART thereby causing TXINTR to be cleared.
3617      */
3618   finish:
3619     if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_DOINGSETUP)) {
3620 	ifq_set_oactive(&sc->tulip_if.if_snd);
3621 	sc->tulip_if.if_start = tulip_ifstart;
3622 	if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
3623 	    sc->tulip_intrmask |= TULIP_STS_TXINTR;
3624 	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3625 	}
3626     } else if ((sc->tulip_flags & TULIP_PROMISC) == 0) {
3627 	if (sc->tulip_intrmask & TULIP_STS_TXINTR) {
3628 	    sc->tulip_intrmask &= ~TULIP_STS_TXINTR;
3629 	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3630 	}
3631     }
3632     TULIP_CSR_WRITE(sc, csr_txpoll, 1);
3633     return m;
3634 }
3635 
3636 static void
3637 tulip_txput_setup(tulip_softc_t *sc)
3638 {
3639     tulip_ringinfo_t *ri = &sc->tulip_txinfo;
3640     tulip_desc_t *nextout;
3641 
3642     /*
3643      * We will transmit, at most, one setup packet per call to ifstart.
3644      */
3645 
3646     /*
3647      * Try to reclaim some free descriptors..
3648      */
3649     if (ri->ri_free < 2)
3650 	tulip_tx_intr(sc);
3651     if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
3652 	sc->tulip_flags |= TULIP_WANTTXSTART;
3653 	sc->tulip_if.if_start = tulip_ifstart;
3654 	return;
3655     }
3656     bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
3657 	  sizeof(sc->tulip_setupbuf));
3658     /*
3659      * Clear WANTSETUP and set DOINGSETUP.  Set know that WANTSETUP is
3660      * set and DOINGSETUP is clear doing an XOR of the two will DTRT.
3661      */
3662     sc->tulip_flags ^= TULIP_WANTSETUP|TULIP_DOINGSETUP;
3663     ri->ri_free--;
3664     nextout = ri->ri_nextout;
3665     nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
3666     nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
3667 	|TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
3668     if (sc->tulip_flags & TULIP_WANTHASHPERFECT)
3669 	nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
3670     else if (sc->tulip_flags & TULIP_WANTHASHONLY)
3671 	nextout->d_flag |= TULIP_DFLAG_TxHASHFILT|TULIP_DFLAG_TxINVRSFILT;
3672 
3673     nextout->d_length2 = 0;
3674     nextout->d_addr2 = 0;
3675     nextout->d_length1 = sizeof(sc->tulip_setupbuf);
3676     nextout->d_addr1 = TULIP_KVATOPHYS(sc, sc->tulip_setupbuf);
3677 
3678     /*
3679      * Advance the ring for the next transmit packet.
3680      */
3681     if (++ri->ri_nextout == ri->ri_last)
3682 	ri->ri_nextout = ri->ri_first;
3683 
3684     /*
3685      * Make sure the next descriptor is owned by us since it
3686      * may have been set up above if we ran out of room in the
3687      * ring.
3688      */
3689     ri->ri_nextout->d_status = 0;
3690     TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout, sizeof(u_int32_t));
3691     nextout->d_status = TULIP_DSTS_OWNER;
3692     /*
3693      * Flush the ownwership of the current descriptor
3694      */
3695     TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(u_int32_t));
3696     TULIP_CSR_WRITE(sc, csr_txpoll, 1);
3697     if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
3698 	sc->tulip_intrmask |= TULIP_STS_TXINTR;
3699 	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3700     }
3701 }
3702 
3703 static int
3704 tulip_ifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred * cr)
3705 {
3706     tulip_softc_t *sc = (tulip_softc_t *)ifp->if_softc;
3707     struct ifreq *ifr = (struct ifreq *)data;
3708     int error = 0;
3709 
3710     switch (cmd) {
3711 	case SIOCGIFADDR: {
3712 	    bcopy((caddr_t) sc->tulip_enaddr,
3713 		  (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
3714 		  6);
3715 	    break;
3716 	}
3717 
3718 	case SIOCSIFFLAGS: {
3719 	    tulip_addr_filter(sc); /* reinit multicast filter */
3720 	    tulip_init(sc);
3721 	    break;
3722 	}
3723 
3724 	case SIOCSIFMEDIA:
3725 	case SIOCGIFMEDIA: {
3726 	    error = ifmedia_ioctl(ifp, ifr, &sc->tulip_ifmedia, cmd);
3727 	    break;
3728 	}
3729 
3730 	case SIOCADDMULTI:
3731 	case SIOCDELMULTI: {
3732 	    /*
3733 	     * Update multicast listeners
3734 	     */
3735 	    tulip_addr_filter(sc);		/* reset multicast filtering */
3736 	    tulip_init(sc);
3737 	    error = 0;
3738 	    break;
3739 	}
3740 
3741 	case SIOCSIFMTU:
3742 	    /*
3743 	     * Set the interface MTU.
3744 	     */
3745 	    if (ifr->ifr_mtu > ETHERMTU
3746 #ifdef BIG_PACKET
3747 		    && sc->tulip_chipid != TULIP_21140
3748 		    && sc->tulip_chipid != TULIP_21140A
3749 		    && sc->tulip_chipid != TULIP_21041
3750 #endif
3751 		) {
3752 		error = EINVAL;
3753 		break;
3754 	    }
3755 	    ifp->if_mtu = ifr->ifr_mtu;
3756 #ifdef BIG_PACKET
3757 	    tulip_reset(sc);
3758 	    tulip_init(sc);
3759 #endif
3760 	    break;
3761 
3762 #ifdef SIOCGADDRROM
3763 	case SIOCGADDRROM: {
3764 	    error = copyout(sc->tulip_rombuf, ifr->ifr_data, sizeof(sc->tulip_rombuf));
3765 	    break;
3766 	}
3767 #endif
3768 #ifdef SIOCGCHIPID
3769 	case SIOCGCHIPID: {
3770 	    ifr->ifr_metric = (int) sc->tulip_chipid;
3771 	    break;
3772 	}
3773 #endif
3774 	default: {
3775 	    error = ether_ioctl(ifp, cmd, data);
3776 	    break;
3777 	}
3778     }
3779     return error;
3780 }
3781 
3782 static void
3783 tulip_ifstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
3784 {
3785     tulip_softc_t *sc = (tulip_softc_t *)ifp->if_softc;
3786 
3787     ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
3788     if (sc->tulip_if.if_flags & IFF_RUNNING) {
3789 
3790 	if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
3791 	    tulip_txput_setup(sc);
3792 
3793 	while (!ifq_is_empty(&sc->tulip_if.if_snd)) {
3794 	    struct mbuf *m;
3795 
3796 	    m = ifq_dequeue(&sc->tulip_if.if_snd);
3797 	    if (m == NULL)
3798 	    	break;
3799 
3800 	    if ((m = tulip_txput(sc, m)) != NULL) {
3801 		ifq_prepend(&sc->tulip_if.if_snd, m);
3802 		break;
3803 	    }
3804 	}
3805     }
3806 }
3807 
3808 static void
3809 tulip_ifwatchdog(struct ifnet *ifp)
3810 {
3811     tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
3812 
3813     sc->tulip_if.if_timer = 1;
3814     /*
3815      * These should be rare so do a bulk test up front so we can just skip
3816      * them if needed.
3817      */
3818     if (sc->tulip_flags & (TULIP_SYSTEMERROR|TULIP_RXBUFSLOW|TULIP_NOMESSAGES)) {
3819 	/*
3820 	 * If the number of receive buffer is low, try to refill
3821 	 */
3822 	if (sc->tulip_flags & TULIP_RXBUFSLOW)
3823 	    tulip_rx_intr(sc);
3824 
3825 	if (sc->tulip_flags & TULIP_SYSTEMERROR) {
3826 	    if_printf(ifp, "%d system errors: last was %s\n",
3827 		sc->tulip_system_errors,
3828 		tulip_system_errors[sc->tulip_last_system_error]);
3829 	}
3830 	if (sc->tulip_statusbits) {
3831 	    tulip_print_abnormal_interrupt(sc, sc->tulip_statusbits);
3832 	    sc->tulip_statusbits = 0;
3833 	}
3834 
3835 	sc->tulip_flags &= ~(TULIP_NOMESSAGES|TULIP_SYSTEMERROR);
3836     }
3837 
3838     if (sc->tulip_txtimer)
3839 	tulip_tx_intr(sc);
3840     if (sc->tulip_txtimer && --sc->tulip_txtimer == 0) {
3841 	if_printf(ifp, "transmission timeout\n");
3842 	if (TULIP_DO_AUTOSENSE(sc)) {
3843 	    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
3844 	    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
3845 	    sc->tulip_flags &= ~(TULIP_WANTRXACT|TULIP_LINKUP);
3846 	}
3847 	tulip_reset(sc);
3848 	tulip_init(sc);
3849     }
3850 }
3851 
3852 static void
3853 tulip_attach(tulip_softc_t *sc)
3854 {
3855     struct ifnet *ifp = &sc->tulip_if;
3856 
3857     callout_init(&sc->tulip_timer);
3858     callout_init(&sc->tulip_fast_timer);
3859 
3860     ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
3861     ifp->if_ioctl = tulip_ifioctl;
3862     ifp->if_start = tulip_ifstart;
3863     ifp->if_watchdog = tulip_ifwatchdog;
3864     ifp->if_timer = 1;
3865 
3866     if_printf(ifp, "%s%s pass %d.%d%s\n",
3867 	   sc->tulip_boardid,
3868 	   tulip_chipdescs[sc->tulip_chipid],
3869 	   (sc->tulip_revinfo & 0xF0) >> 4,
3870 	   sc->tulip_revinfo & 0x0F,
3871 	   (sc->tulip_features & (TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM))
3872 		 == TULIP_HAVE_ISVSROM ? " (invalid EESPROM checksum)" : "");
3873 
3874     (*sc->tulip_boardsw->bd_media_probe)(sc);
3875     ifmedia_init(&sc->tulip_ifmedia, 0,
3876 		 tulip_ifmedia_change,
3877 		 tulip_ifmedia_status);
3878     sc->tulip_flags &= ~TULIP_DEVICEPROBE;
3879     tulip_ifmedia_add(sc);
3880 
3881     ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
3882     ether_ifattach(&(sc)->tulip_if, sc->tulip_enaddr, NULL);
3883 
3884     tulip_reset(sc);
3885 }
3886 
3887 static void
3888 tulip_detach(tulip_softc_t *sc)
3889 {
3890     ifmedia_removeall(&sc->tulip_ifmedia);
3891     ether_ifdetach(&sc->tulip_if);
3892 }
3893 
3894 static void
3895 tulip_initcsrs(tulip_softc_t *sc, tulip_csrptr_t csr_base, size_t csr_size)
3896 {
3897     sc->tulip_csrs.csr_busmode		= csr_base +  0 * csr_size;
3898     sc->tulip_csrs.csr_txpoll		= csr_base +  1 * csr_size;
3899     sc->tulip_csrs.csr_rxpoll		= csr_base +  2 * csr_size;
3900     sc->tulip_csrs.csr_rxlist		= csr_base +  3 * csr_size;
3901     sc->tulip_csrs.csr_txlist		= csr_base +  4 * csr_size;
3902     sc->tulip_csrs.csr_status		= csr_base +  5 * csr_size;
3903     sc->tulip_csrs.csr_command		= csr_base +  6 * csr_size;
3904     sc->tulip_csrs.csr_intr		= csr_base +  7 * csr_size;
3905     sc->tulip_csrs.csr_missed_frames	= csr_base +  8 * csr_size;
3906     sc->tulip_csrs.csr_9		= csr_base +  9 * csr_size;
3907     sc->tulip_csrs.csr_10		= csr_base + 10 * csr_size;
3908     sc->tulip_csrs.csr_11		= csr_base + 11 * csr_size;
3909     sc->tulip_csrs.csr_12		= csr_base + 12 * csr_size;
3910     sc->tulip_csrs.csr_13		= csr_base + 13 * csr_size;
3911     sc->tulip_csrs.csr_14		= csr_base + 14 * csr_size;
3912     sc->tulip_csrs.csr_15		= csr_base + 15 * csr_size;
3913 }
3914 
3915 static void
3916 tulip_initring(tulip_softc_t *sc, tulip_ringinfo_t *ri, tulip_desc_t *descs,
3917 	       int ndescs)
3918 {
3919     ri->ri_max = ndescs;
3920     ri->ri_first = descs;
3921     ri->ri_last = ri->ri_first + ri->ri_max;
3922     bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
3923     ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
3924 }
3925 
3926 /*
3927  * This is the PCI configuration support.
3928  */
3929 
3930 #define	PCI_CBIO	0x10	/* Configuration Base IO Address */
3931 #define	PCI_CBMA	0x14	/* Configuration Base Memory Address */
3932 #define	PCI_CFDA	0x40	/* Configuration Driver Area */
3933 
3934 static int
3935 tulip_pci_probe(device_t dev)
3936 {
3937     const char *name = NULL;
3938 
3939     if (pci_get_vendor(dev) != DEC_VENDORID)
3940 	return ENXIO;
3941 
3942     /*
3943      * Some LanMedia WAN cards use the Tulip chip, but they have
3944      * their own driver, and we should not recognize them
3945      */
3946     if (pci_get_subvendor(dev) == 0x1376)
3947 	return ENXIO;
3948 
3949     switch (pci_get_device(dev)) {
3950     case CHIPID_21040:
3951 	name = "Digital 21040 Ethernet";
3952 	break;
3953     case CHIPID_21041:
3954 	name = "Digital 21041 Ethernet";
3955 	break;
3956     case CHIPID_21140:
3957 	if (pci_get_revid(dev) >= 0x20)
3958 	    name = "Digital 21140A Fast Ethernet";
3959 	else
3960 	    name = "Digital 21140 Fast Ethernet";
3961 	break;
3962     case CHIPID_21142:
3963 	if (pci_get_revid(dev) >= 0x20)
3964 	    name = "Digital 21143 Fast Ethernet";
3965 	else
3966 	    name = "Digital 21142 Fast Ethernet";
3967 	break;
3968     }
3969     if (name) {
3970 	device_set_desc(dev, name);
3971 	return -200;
3972     }
3973     return ENXIO;
3974 }
3975 
3976 static int
3977 tulip_shutdown(device_t dev)
3978 {
3979     tulip_softc_t *sc = device_get_softc(dev);
3980 
3981     lwkt_serialize_enter(sc->tulip_if.if_serializer);
3982     TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
3983     DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
3984 		   33MHz that comes to two microseconds but wait a
3985 		   bit longer anyways) */
3986     lwkt_serialize_exit(sc->tulip_if.if_serializer);
3987     return 0;
3988 }
3989 
3990 static int
3991 tulip_pci_attach(device_t dev)
3992 {
3993     tulip_softc_t *sc;
3994     int retval, idx;
3995     uint32_t revinfo, cfdainfo;
3996     u_int csroffset = TULIP_PCI_CSROFFSET;
3997     u_int csrsize = TULIP_PCI_CSRSIZE;
3998     tulip_csrptr_t csr_base;
3999     tulip_chipid_t chipid = TULIP_CHIPID_UNKNOWN;
4000     struct resource *res;
4001     int rid;
4002 
4003     if (device_get_unit(dev) >= TULIP_MAX_DEVICES) {
4004 	device_printf(dev, "not configured; limit of %d reached or exceeded\n",
4005 	       TULIP_MAX_DEVICES);
4006 	return ENXIO;
4007     }
4008 
4009     revinfo  = pci_get_revid(dev);
4010     cfdainfo = pci_read_config(dev, PCI_CFDA, 4);
4011 
4012     /* turn busmaster on in case BIOS doesn't set it */
4013     pci_enable_busmaster(dev);
4014 
4015     if (pci_get_vendor(dev) == DEC_VENDORID) {
4016 	if (pci_get_device(dev) == CHIPID_21040)
4017 		chipid = TULIP_21040;
4018 	else if (pci_get_device(dev) == CHIPID_21041)
4019 		chipid = TULIP_21041;
4020 	else if (pci_get_device(dev) == CHIPID_21140)
4021 		chipid = (revinfo >= 0x20) ? TULIP_21140A : TULIP_21140;
4022 	else if (pci_get_device(dev) == CHIPID_21142)
4023 		chipid = (revinfo >= 0x20) ? TULIP_21143 : TULIP_21142;
4024     }
4025     if (chipid == TULIP_CHIPID_UNKNOWN)
4026 	return ENXIO;
4027 
4028     if (chipid == TULIP_21040 && revinfo < 0x20) {
4029 	device_printf(dev, "not configured; 21040 pass 2.0 required (%d.%d found)\n",
4030 	       revinfo >> 4, revinfo & 0x0f);
4031 	return ENXIO;
4032     } else if (chipid == TULIP_21140 && revinfo < 0x11) {
4033 	device_printf(dev, "not configured; 21140 pass 1.1 required (%d.%d found)\n",
4034 	       revinfo >> 4, revinfo & 0x0f);
4035 	return ENXIO;
4036     }
4037 
4038     sc = device_get_softc(dev);
4039     sc->tulip_dev = dev;
4040     sc->tulip_pci_busno = pci_get_bus(dev);
4041     sc->tulip_pci_devno = pci_get_slot(dev);
4042     sc->tulip_chipid = chipid;
4043     sc->tulip_flags |= TULIP_DEVICEPROBE;
4044     if (chipid == TULIP_21140 || chipid == TULIP_21140A)
4045 	sc->tulip_features |= TULIP_HAVE_GPR|TULIP_HAVE_STOREFWD;
4046     if (chipid == TULIP_21140A && revinfo <= 0x22)
4047 	sc->tulip_features |= TULIP_HAVE_RXBADOVRFLW;
4048     if (chipid == TULIP_21140)
4049 	sc->tulip_features |= TULIP_HAVE_BROKEN_HASH;
4050     if (chipid != TULIP_21040 && chipid != TULIP_21140)
4051 	sc->tulip_features |= TULIP_HAVE_POWERMGMT;
4052     if (chipid == TULIP_21041 || chipid == TULIP_21142 || chipid == TULIP_21143) {
4053 	sc->tulip_features |= TULIP_HAVE_DUALSENSE;
4054 	if (chipid != TULIP_21041 || revinfo >= 0x20)
4055 	    sc->tulip_features |= TULIP_HAVE_SIANWAY;
4056 	if (chipid != TULIP_21041)
4057 	    sc->tulip_features |= TULIP_HAVE_SIAGP|TULIP_HAVE_RXBADOVRFLW|TULIP_HAVE_STOREFWD;
4058 	if (chipid != TULIP_21041 && revinfo >= 0x20)
4059 	    sc->tulip_features |= TULIP_HAVE_SIA100;
4060     }
4061 
4062     if (sc->tulip_features & TULIP_HAVE_POWERMGMT
4063 	    && (cfdainfo & (TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE))) {
4064 	cfdainfo &= ~(TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE);
4065 	pci_write_config(dev, PCI_CFDA, cfdainfo, 4);
4066 	DELAY(11*1000);
4067     }
4068     if_initname(&sc->tulip_if, device_get_name(dev), device_get_unit(dev));
4069     sc->tulip_revinfo = revinfo;
4070     sc->tulip_if.if_softc = sc;
4071 #if defined(TULIP_IOMAPPED)
4072     rid = PCI_CBIO;
4073     res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
4074 #else
4075     rid = PCI_CBMA;
4076     res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
4077 #endif
4078     if (!res)
4079 	return ENXIO;
4080     sc->tulip_csrs_bst = rman_get_bustag(res);
4081     sc->tulip_csrs_bsh = rman_get_bushandle(res);
4082     csr_base = 0;
4083 
4084     tulips[device_get_unit(dev)] = sc;
4085 
4086     tulip_initcsrs(sc, csr_base + csroffset, csrsize);
4087 
4088     sc->tulip_rxdescs = kmalloc(sizeof(tulip_desc_t) * TULIP_RXDESCS,
4089 				M_DEVBUF, M_INTWAIT);
4090     sc->tulip_txdescs = kmalloc(sizeof(tulip_desc_t) * TULIP_TXDESCS,
4091 				M_DEVBUF, M_INTWAIT);
4092 
4093     tulip_initring(sc, &sc->tulip_rxinfo, sc->tulip_rxdescs, TULIP_RXDESCS);
4094     tulip_initring(sc, &sc->tulip_txinfo, sc->tulip_txdescs, TULIP_TXDESCS);
4095 
4096     /*
4097      * Make sure there won't be any interrupts or such...
4098      */
4099     TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
4100     DELAY(100);	/* Wait 10 microseconds (actually 50 PCI cycles but at
4101 		   33MHz that comes to two microseconds but wait a
4102 		   bit longer anyways) */
4103 
4104     if ((retval = tulip_read_macaddr(sc)) < 0) {
4105 	device_printf(dev, "can't read ENET ROM (why=%d) (", retval);
4106 	for (idx = 0; idx < 32; idx++)
4107 	    kprintf("%02x", sc->tulip_rombuf[idx]);
4108 	kprintf("\n");
4109 	device_printf(dev, "%s%s pass %d.%d\n",
4110 	       sc->tulip_boardid, tulip_chipdescs[sc->tulip_chipid],
4111 	       (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F);
4112 	device_printf(dev, "address unknown\n");
4113     } else {
4114 	void (*intr_rtn)(void *) = tulip_intr_normal;
4115 
4116 	if (sc->tulip_features & TULIP_HAVE_SHAREDINTR)
4117 	    intr_rtn = tulip_intr_shared;
4118 
4119 	tulip_attach(sc);
4120 	if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
4121 	    void *ih;
4122 
4123 	    rid = 0;
4124 	    res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
4125 				         RF_SHAREABLE | RF_ACTIVE);
4126 	    if (res == NULL || bus_setup_intr(dev, res, INTR_MPSAFE,
4127 					   intr_rtn, sc, &ih,
4128 					   sc->tulip_if.if_serializer)) {
4129 		device_printf(dev, "couldn't map interrupt\n");
4130 		tulip_detach(sc);
4131 		kfree((caddr_t) sc->tulip_rxdescs, M_DEVBUF);
4132 		kfree((caddr_t) sc->tulip_txdescs, M_DEVBUF);
4133 		return ENXIO;
4134 	    }
4135 
4136 	    ifq_set_cpuid(&sc->tulip_if.if_snd, rman_get_cpuid(res));
4137 	}
4138     }
4139     return 0;
4140 }
4141 
4142 static device_method_t tulip_pci_methods[] = {
4143     /* Device interface */
4144     DEVMETHOD(device_probe,	tulip_pci_probe),
4145     DEVMETHOD(device_attach,	tulip_pci_attach),
4146     DEVMETHOD(device_shutdown,	tulip_shutdown),
4147     DEVMETHOD_END
4148 };
4149 static driver_t tulip_pci_driver = {
4150     "de",
4151     tulip_pci_methods,
4152     sizeof(tulip_softc_t),
4153 };
4154 static devclass_t tulip_devclass;
4155 
4156 DECLARE_DUMMY_MODULE(if_de);
4157 DRIVER_MODULE(if_de, pci, tulip_pci_driver, tulip_devclass, NULL, NULL);
4158 
4159