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