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