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