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