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