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