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