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