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