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