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