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