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