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