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