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