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