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