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