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