if_de.c revision 152666
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 152666 2005-11-21 21:50:07Z 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(IF_LLADDR(sc->tulip_ifp), mtod(m, struct ether_header *)->ether_dhost, 6);
264    bcopy(IF_LLADDR(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(IF_LLADDR(ifp));
3085		sp[hash >> 4] |= htole32(1 << (hash & 0xF));
3086	    } else {
3087		sp[39] = TULIP_SP_MAC((u_int16_t *)IF_LLADDR(ifp) + 0);
3088		sp[40] = TULIP_SP_MAC((u_int16_t *)IF_LLADDR(ifp) + 1);
3089		sp[41] = TULIP_SP_MAC((u_int16_t *)IF_LLADDR(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#if BYTE_ORDER == BIG_ENDIAN
3114	    *sp++ = 0xFFFF << 16;
3115	    *sp++ = 0xFFFF << 16;
3116	    *sp++ = 0xFFFF << 16;
3117#else
3118	    *sp++ = 0xFFFF;
3119	    *sp++ = 0xFFFF;
3120	    *sp++ = 0xFFFF;
3121#endif
3122	}
3123	/*
3124	 * Pad the rest with our hardware address
3125	 */
3126	for (; idx < 16; idx++) {
3127	    *sp++ = TULIP_SP_MAC((u_int16_t *)IF_LLADDR(ifp) + 0);
3128	    *sp++ = TULIP_SP_MAC((u_int16_t *)IF_LLADDR(ifp) + 1);
3129	    *sp++ = TULIP_SP_MAC((u_int16_t *)IF_LLADDR(ifp) + 2);
3130	}
3131    }
3132    IF_ADDR_UNLOCK(ifp);
3133}
3134
3135static void
3136tulip_reset(tulip_softc_t * const sc)
3137{
3138    tulip_ringinfo_t *ri;
3139    tulip_descinfo_t *di;
3140    struct mbuf *m;
3141    u_int32_t inreset = (sc->tulip_flags & TULIP_INRESET);
3142
3143    TULIP_LOCK_ASSERT(sc);
3144
3145    CTR1(KTR_TULIP, "tulip_reset: inreset %d", inreset);
3146
3147    /*
3148     * Brilliant.  Simply brilliant.  When switching modes/speeds
3149     * on a 2114*, you need to set the appriopriate MII/PCS/SCL/PS
3150     * bits in CSR6 and then do a software reset to get the 21140
3151     * to properly reset its internal pathways to the right places.
3152     *   Grrrr.
3153     */
3154    if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0
3155	    && sc->tulip_boardsw->bd_media_preset != NULL)
3156	(*sc->tulip_boardsw->bd_media_preset)(sc);
3157
3158    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
3159    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
3160		   33MHz that comes to two microseconds but wait a
3161		   bit longer anyways) */
3162
3163    if (!inreset) {
3164	sc->tulip_flags |= TULIP_INRESET;
3165	sc->tulip_flags &= ~(TULIP_NEEDRESET|TULIP_RXBUFSLOW);
3166	sc->tulip_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3167    }
3168
3169    TULIP_CSR_WRITE(sc, csr_txlist, sc->tulip_txinfo.ri_dma_addr);
3170    TULIP_CSR_WRITE(sc, csr_rxlist, sc->tulip_rxinfo.ri_dma_addr);
3171    TULIP_CSR_WRITE(sc, csr_busmode,
3172		    (1 << (3 /*pci_max_burst_len*/ + 8))
3173		    |TULIP_BUSMODE_CACHE_ALIGN8
3174		    |TULIP_BUSMODE_READMULTIPLE
3175		    |(BYTE_ORDER != LITTLE_ENDIAN ?
3176		      TULIP_BUSMODE_DESC_BIGENDIAN : 0));
3177
3178    sc->tulip_txtimer = 0;
3179    /*
3180     * Free all the mbufs that were on the transmit ring.
3181     */
3182    CTR0(KTR_TULIP, "tulip_reset: drain transmit ring");
3183    ri = &sc->tulip_txinfo;
3184    for (di = ri->ri_first; di < ri->ri_last; di++) {
3185	m = tulip_dequeue_mbuf(ri, di, SYNC_NONE);
3186	if (m != NULL)
3187	    m_freem(m);
3188	di->di_desc->d_status = 0;
3189    }
3190
3191    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
3192    ri->ri_free = ri->ri_max;
3193    TULIP_TXDESC_PRESYNC(ri);
3194
3195    /*
3196     * We need to collect all the mbufs that were on the
3197     * receive ring before we reinit it either to put
3198     * them back on or to know if we have to allocate
3199     * more.
3200     */
3201    CTR0(KTR_TULIP, "tulip_reset: drain receive ring");
3202    ri = &sc->tulip_rxinfo;
3203    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
3204    ri->ri_free = ri->ri_max;
3205    for (di = ri->ri_first; di < ri->ri_last; di++) {
3206	di->di_desc->d_status = 0;
3207	di->di_desc->d_length1 = 0; di->di_desc->d_addr1 = 0;
3208	di->di_desc->d_length2 = 0; di->di_desc->d_addr2 = 0;
3209    }
3210    TULIP_RXDESC_PRESYNC(ri);
3211    for (di = ri->ri_first; di < ri->ri_last; di++) {
3212	m = tulip_dequeue_mbuf(ri, di, SYNC_NONE);
3213	if (m != NULL)
3214	    m_freem(m);
3215    }
3216
3217    /*
3218     * If tulip_reset is being called recursively, exit quickly knowing
3219     * that when the outer tulip_reset returns all the right stuff will
3220     * have happened.
3221     */
3222    if (inreset)
3223	return;
3224
3225    sc->tulip_intrmask |= TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
3226	|TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
3227	|TULIP_STS_TXUNDERFLOW|TULIP_STS_TXBABBLE
3228	|TULIP_STS_RXSTOPPED;
3229
3230    if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0)
3231	(*sc->tulip_boardsw->bd_media_select)(sc);
3232#if defined(TULIP_DEBUG)
3233    if ((sc->tulip_flags & TULIP_NEEDRESET) == TULIP_NEEDRESET)
3234	if_printf(sc->tulip_ifp,
3235	    "tulip_reset: additional reset needed?!?\n");
3236#endif
3237    if (bootverbose)
3238	    tulip_media_print(sc);
3239    if (sc->tulip_features & TULIP_HAVE_DUALSENSE)
3240	TULIP_CSR_WRITE(sc, csr_sia_status, TULIP_CSR_READ(sc, csr_sia_status));
3241
3242    sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP|TULIP_INRESET
3243			 |TULIP_RXACT);
3244}
3245
3246
3247static void
3248tulip_init(void *arg)
3249{
3250    tulip_softc_t *sc = (tulip_softc_t *)arg;
3251
3252    TULIP_LOCK(sc);
3253    tulip_init_locked(sc);
3254    TULIP_UNLOCK(sc);
3255}
3256
3257static void
3258tulip_init_locked(tulip_softc_t * const sc)
3259{
3260    CTR0(KTR_TULIP, "tulip_init_locked");
3261    if (sc->tulip_ifp->if_flags & IFF_UP) {
3262	if ((sc->tulip_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3263	    /* initialize the media */
3264	    CTR0(KTR_TULIP, "tulip_init_locked: up but not running, reset chip");
3265	    tulip_reset(sc);
3266	}
3267	tulip_addr_filter(sc);
3268	sc->tulip_ifp->if_drv_flags |= IFF_DRV_RUNNING;
3269	if (sc->tulip_ifp->if_flags & IFF_PROMISC) {
3270	    sc->tulip_flags |= TULIP_PROMISC;
3271	    sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
3272	    sc->tulip_intrmask |= TULIP_STS_TXINTR;
3273	} else {
3274	    sc->tulip_flags &= ~TULIP_PROMISC;
3275	    sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
3276	    if (sc->tulip_flags & TULIP_ALLMULTI) {
3277		sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
3278	    } else {
3279		sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
3280	    }
3281	}
3282	sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
3283	if ((sc->tulip_flags & (TULIP_TXPROBE_ACTIVE|TULIP_WANTSETUP)) == 0) {
3284	    tulip_rx_intr(sc);
3285	    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
3286	    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
3287	} else {
3288	    sc->tulip_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3289	    sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
3290	    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
3291	}
3292	CTR2(KTR_TULIP, "tulip_init_locked: intr mask %08x  cmdmode %08x",
3293	    sc->tulip_intrmask, sc->tulip_cmdmode);
3294	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3295	TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3296	CTR1(KTR_TULIP, "tulip_init_locked: status %08x\n",
3297	    TULIP_CSR_READ(sc, csr_status));
3298	if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
3299	    tulip_txput_setup(sc);
3300    } else {
3301	CTR0(KTR_TULIP, "tulip_init_locked: not up, reset chip");
3302	sc->tulip_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3303	tulip_reset(sc);
3304	tulip_addr_filter(sc);
3305    }
3306}
3307
3308#define DESC_STATUS(di)	(((volatile tulip_desc_t *)((di)->di_desc))->d_status)
3309#define DESC_FLAG(di)	((di)->di_desc->d_flag)
3310
3311static void
3312tulip_rx_intr(tulip_softc_t * const sc)
3313{
3314    TULIP_PERFSTART(rxintr)
3315    tulip_ringinfo_t * const ri = &sc->tulip_rxinfo;
3316    struct ifnet * const ifp = sc->tulip_ifp;
3317    int fillok = 1;
3318#if defined(TULIP_DEBUG)
3319    int cnt = 0;
3320#endif
3321
3322    TULIP_LOCK_ASSERT(sc);
3323    CTR0(KTR_TULIP, "tulip_rx_intr: start");
3324    for (;;) {
3325	TULIP_PERFSTART(rxget)
3326	tulip_descinfo_t *eop = ri->ri_nextin, *dip;
3327	int total_len = 0, last_offset = 0;
3328	struct mbuf *ms = NULL, *me = NULL;
3329	int accept = 0;
3330	int error;
3331
3332	if (fillok && (ri->ri_max - ri->ri_free) < TULIP_RXQ_TARGET)
3333	    goto queue_mbuf;
3334
3335#if defined(TULIP_DEBUG)
3336	if (cnt == ri->ri_max)
3337	    break;
3338#endif
3339	/*
3340	 * If the TULIP has no descriptors, there can't be any receive
3341	 * descriptors to process.
3342 	 */
3343	if (eop == ri->ri_nextout)
3344	    break;
3345
3346	/*
3347	 * 90% of the packets will fit in one descriptor.  So we optimize
3348	 * for that case.
3349	 */
3350	TULIP_RXDESC_POSTSYNC(ri);
3351	if ((DESC_STATUS(eop) & (TULIP_DSTS_OWNER|TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) == (TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) {
3352	    ms = tulip_dequeue_mbuf(ri, eop, SYNC_RX);
3353	    CTR2(KTR_TULIP,
3354		"tulip_rx_intr: single packet mbuf %p from descriptor %td", ms,
3355		eop - ri->ri_first);
3356	    me = ms;
3357	    ri->ri_free++;
3358	} else {
3359	    /*
3360	     * If still owned by the TULIP, don't touch it.
3361	     */
3362	    if (DESC_STATUS(eop) & TULIP_DSTS_OWNER)
3363		break;
3364
3365	    /*
3366	     * It is possible (though improbable unless MCLBYTES < 1518) for
3367	     * a received packet to cross more than one receive descriptor.
3368	     * We first loop through the descriptor ring making sure we have
3369	     * received a complete packet.  If not, we bail until the next
3370	     * interrupt.
3371	     */
3372	    dip = eop;
3373	    while ((DESC_STATUS(eop) & TULIP_DSTS_RxLASTDESC) == 0) {
3374		if (++eop == ri->ri_last)
3375		    eop = ri->ri_first;
3376		TULIP_RXDESC_POSTSYNC(ri);
3377		if (eop == ri->ri_nextout || DESC_STATUS(eop) & TULIP_DSTS_OWNER) {
3378#if defined(TULIP_DEBUG)
3379		    sc->tulip_dbg.dbg_rxintrs++;
3380		    sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
3381#endif
3382		    TULIP_PERFEND(rxget);
3383		    TULIP_PERFEND(rxintr);
3384		    return;
3385		}
3386		total_len++;
3387	    }
3388
3389	    /*
3390	     * Dequeue the first buffer for the start of the packet.  Hopefully
3391	     * this will be the only one we need to dequeue.  However, if the
3392	     * packet consumed multiple descriptors, then we need to dequeue
3393	     * those buffers and chain to the starting mbuf.  All buffers but
3394	     * the last buffer have the same length so we can set that now.
3395	     * (we add to last_offset instead of multiplying since we normally
3396	     * won't go into the loop and thereby saving ourselves from
3397	     * doing a multiplication by 0 in the normal case).
3398	     */
3399	    ms = tulip_dequeue_mbuf(ri, dip, SYNC_RX);
3400	    CTR2(KTR_TULIP,
3401		"tulip_rx_intr: start packet mbuf %p from descriptor %td", ms,
3402		dip - ri->ri_first);
3403	    ri->ri_free++;
3404	    for (me = ms; total_len > 0; total_len--) {
3405		me->m_len = TULIP_RX_BUFLEN;
3406		last_offset += TULIP_RX_BUFLEN;
3407		if (++dip == ri->ri_last)
3408		    dip = ri->ri_first;
3409		me->m_next = tulip_dequeue_mbuf(ri, dip, SYNC_RX);
3410		ri->ri_free++;
3411		me = me->m_next;
3412		CTR2(KTR_TULIP,
3413		    "tulip_rx_intr: cont packet mbuf %p from descriptor %td",
3414		    me, dip - ri->ri_first);
3415	    }
3416	    KASSERT(dip == eop, ("mismatched descinfo structs"));
3417	}
3418
3419	/*
3420	 *  Now get the size of received packet (minus the CRC).
3421	 */
3422	total_len = ((DESC_STATUS(eop) >> 16) & 0x7FFF) - ETHER_CRC_LEN;
3423	if ((sc->tulip_flags & TULIP_RXIGNORE) == 0
3424	    && ((DESC_STATUS(eop) & TULIP_DSTS_ERRSUM) == 0)) {
3425	    me->m_len = total_len - last_offset;
3426	    sc->tulip_flags |= TULIP_RXACT;
3427	    accept = 1;
3428	    CTR1(KTR_TULIP, "tulip_rx_intr: good packet; length %d",
3429		total_len);
3430	} else {
3431	    CTR1(KTR_TULIP, "tulip_rx_intr: bad packet; status %08x",
3432		DESC_STATUS(eop));
3433	    ifp->if_ierrors++;
3434	    if (DESC_STATUS(eop) & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxOVERFLOW|TULIP_DSTS_RxWATCHDOG)) {
3435		sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
3436	    } else {
3437#if defined(TULIP_VERBOSE)
3438		const char *error = NULL;
3439#endif
3440		if (DESC_STATUS(eop) & TULIP_DSTS_RxTOOLONG) {
3441		    sc->tulip_dot3stats.dot3StatsFrameTooLongs++;
3442#if defined(TULIP_VERBOSE)
3443		    error = "frame too long";
3444#endif
3445		}
3446		if (DESC_STATUS(eop) & TULIP_DSTS_RxBADCRC) {
3447		    if (DESC_STATUS(eop) & TULIP_DSTS_RxDRBBLBIT) {
3448			sc->tulip_dot3stats.dot3StatsAlignmentErrors++;
3449#if defined(TULIP_VERBOSE)
3450			error = "alignment error";
3451#endif
3452		    } else {
3453			sc->tulip_dot3stats.dot3StatsFCSErrors++;
3454#if defined(TULIP_VERBOSE)
3455			error = "bad crc";
3456#endif
3457		    }
3458		}
3459#if defined(TULIP_VERBOSE)
3460		if (error != NULL && (sc->tulip_flags & TULIP_NOMESSAGES) == 0) {
3461		    if_printf(sc->tulip_ifp, "receive: %6D: %s\n",
3462			   mtod(ms, u_char *) + 6, ":",
3463			   error);
3464		    sc->tulip_flags |= TULIP_NOMESSAGES;
3465		}
3466#endif
3467	    }
3468
3469	}
3470#if defined(TULIP_DEBUG)
3471	cnt++;
3472#endif
3473	ifp->if_ipackets++;
3474	if (++eop == ri->ri_last)
3475	    eop = ri->ri_first;
3476	ri->ri_nextin = eop;
3477      queue_mbuf:
3478	/*
3479	 * We have received a good packet that needs to be passed up the
3480	 * stack.
3481	 */
3482	if (accept) {
3483	    struct mbuf *m0;
3484
3485	    KASSERT(ms != NULL, ("no packet to accept"));
3486#if defined(TULIP_COPY_RXDATA)
3487	    /*
3488	     * Copy the data into a new mbuf that is properly aligned.  If
3489	     * we fail to allocate a new mbuf, then drop the packet.  We will
3490	     * reuse the same rx buffer ('ms') below for another packet
3491	     * regardless.
3492	     */
3493	    m0 = m_devget(mtod(ms, caddr_t), total_len, ETHER_ALIGN, ifp, NULL);
3494	    if (m0 == NULL) {
3495		ifp->if_ierrors++;
3496		goto skip_input;
3497	    }
3498#else
3499	    /*
3500	     * Update the header for the mbuf referencing this receive
3501	     * buffer and pass it up the stack.  Allocate a new mbuf cluster
3502	     * to replace the one we just passed up the stack.
3503	     *
3504	     * Note that if this packet crossed multiple descriptors
3505	     * we don't even try to reallocate all the mbufs here.
3506	     * Instead we rely on the test at the beginning of
3507	     * the loop to refill for the extra consumed mbufs.
3508	     */
3509	    ms->m_pkthdr.len = total_len;
3510	    ms->m_pkthdr.rcvif = ifp;
3511	    m0 = ms;
3512	    ms = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
3513#endif
3514	    TULIP_UNLOCK(sc);
3515	    CTR1(KTR_TULIP, "tulip_rx_intr: passing %p to upper layer", m0);
3516	    (*ifp->if_input)(ifp, m0);
3517	    TULIP_LOCK(sc);
3518	} else if (ms == NULL)
3519	    /*
3520	     * If we are priming the TULIP with mbufs, then allocate
3521	     * a new cluster for the next descriptor.
3522	     */
3523	    ms = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
3524
3525#if defined(TULIP_COPY_RXDATA)
3526    skip_input:
3527#endif
3528	if (ms == NULL) {
3529	    /*
3530	     * Couldn't allocate a new buffer.  Don't bother
3531	     * trying to replenish the receive queue.
3532	     */
3533	    fillok = 0;
3534	    sc->tulip_flags |= TULIP_RXBUFSLOW;
3535#if defined(TULIP_DEBUG)
3536	    sc->tulip_dbg.dbg_rxlowbufs++;
3537#endif
3538	    TULIP_PERFEND(rxget);
3539	    continue;
3540	}
3541	/*
3542	 * Now give the buffer(s) to the TULIP and save in our
3543	 * receive queue.
3544	 */
3545	do {
3546	    tulip_descinfo_t * const nextout = ri->ri_nextout;
3547
3548	    M_ASSERTPKTHDR(ms);
3549	    KASSERT(ms->m_data == ms->m_ext.ext_buf,
3550		("rx mbuf data doesn't point to cluster"));
3551	    ms->m_len = ms->m_pkthdr.len = MCLBYTES;
3552	    error = bus_dmamap_load_mbuf(ri->ri_data_tag, *nextout->di_map, ms,
3553		tulip_dma_map_rxbuf, nextout->di_desc, BUS_DMA_NOWAIT);
3554	    if (error) {
3555		if_printf(sc->tulip_ifp,
3556		    "unable to load rx map, error = %d\n", error);
3557		panic("tulip_rx_intr");		/* XXX */
3558	    }
3559	    nextout->di_desc->d_status = TULIP_DSTS_OWNER;
3560	    KASSERT(nextout->di_mbuf == NULL, ("clobbering earlier rx mbuf"));
3561	    nextout->di_mbuf = ms;
3562	    CTR2(KTR_TULIP, "tulip_rx_intr: enqueued mbuf %p to descriptor %td",
3563		ms, nextout - ri->ri_first);
3564	    TULIP_RXDESC_POSTSYNC(ri);
3565	    if (++ri->ri_nextout == ri->ri_last)
3566		ri->ri_nextout = ri->ri_first;
3567	    ri->ri_free--;
3568	    me = ms->m_next;
3569	    ms->m_next = NULL;
3570	} while ((ms = me) != NULL);
3571
3572	if ((ri->ri_max - ri->ri_free) >= TULIP_RXQ_TARGET)
3573	    sc->tulip_flags &= ~TULIP_RXBUFSLOW;
3574	TULIP_PERFEND(rxget);
3575    }
3576
3577#if defined(TULIP_DEBUG)
3578    sc->tulip_dbg.dbg_rxintrs++;
3579    sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
3580#endif
3581    TULIP_PERFEND(rxintr);
3582}
3583
3584static int
3585tulip_tx_intr(tulip_softc_t * const sc)
3586{
3587    TULIP_PERFSTART(txintr)
3588    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
3589    struct mbuf *m;
3590    int xmits = 0;
3591    int descs = 0;
3592
3593    CTR0(KTR_TULIP, "tulip_tx_intr: start");
3594    TULIP_LOCK_ASSERT(sc);
3595    while (ri->ri_free < ri->ri_max) {
3596	u_int32_t d_flag;
3597
3598	TULIP_TXDESC_POSTSYNC(ri);
3599	if (DESC_STATUS(ri->ri_nextin) & TULIP_DSTS_OWNER)
3600	    break;
3601
3602	ri->ri_free++;
3603	descs++;
3604	d_flag = DESC_FLAG(ri->ri_nextin);
3605	if (d_flag & TULIP_DFLAG_TxLASTSEG) {
3606	    if (d_flag & TULIP_DFLAG_TxSETUPPKT) {
3607		CTR2(KTR_TULIP,
3608		    "tulip_tx_intr: setup packet from descriptor %td: %08x",
3609		    ri->ri_nextin - ri->ri_first, DESC_STATUS(ri->ri_nextin));
3610		/*
3611		 * We've just finished processing a setup packet.
3612		 * Mark that we finished it.  If there's not
3613		 * another pending, startup the TULIP receiver.
3614		 * Make sure we ack the RXSTOPPED so we won't get
3615		 * an abormal interrupt indication.
3616		 */
3617		bus_dmamap_sync(sc->tulip_setup_tag, sc->tulip_setup_map,
3618		    BUS_DMASYNC_POSTWRITE);
3619		sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_HASHONLY);
3620		if (DESC_FLAG(ri->ri_nextin) & TULIP_DFLAG_TxINVRSFILT)
3621		    sc->tulip_flags |= TULIP_HASHONLY;
3622		if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == 0) {
3623		    tulip_rx_intr(sc);
3624		    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
3625		    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
3626		    CTR2(KTR_TULIP,
3627			"tulip_tx_intr: intr mask %08x  cmdmode %08x",
3628			sc->tulip_intrmask, sc->tulip_cmdmode);
3629		    TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
3630		    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3631		    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3632		}
3633	    } else {
3634		const u_int32_t d_status = DESC_STATUS(ri->ri_nextin);
3635
3636		m = tulip_dequeue_mbuf(ri, ri->ri_nextin, SYNC_TX);
3637		CTR2(KTR_TULIP,
3638		    "tulip_tx_intr: data packet %p from descriptor %td", m,
3639		    ri->ri_nextin - ri->ri_first);
3640		if (m != NULL) {
3641		    m_freem(m);
3642#if defined(TULIP_DEBUG)
3643		} else {
3644		    if_printf(sc->tulip_ifp,
3645		        "tx_intr: failed to dequeue mbuf?!?\n");
3646#endif
3647		}
3648		if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
3649		    tulip_mediapoll_event_t event = TULIP_MEDIAPOLL_TXPROBE_OK;
3650		    if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxEXCCOLL)) {
3651#if defined(TULIP_DEBUG)
3652			if (d_status & TULIP_DSTS_TxNOCARR)
3653			    sc->tulip_dbg.dbg_txprobe_nocarr++;
3654			if (d_status & TULIP_DSTS_TxEXCCOLL)
3655			    sc->tulip_dbg.dbg_txprobe_exccoll++;
3656#endif
3657			event = TULIP_MEDIAPOLL_TXPROBE_FAILED;
3658		    }
3659		    (*sc->tulip_boardsw->bd_media_poll)(sc, event);
3660		    /*
3661		     * Escape from the loop before media poll has reset the TULIP!
3662		     */
3663		    break;
3664		} else {
3665		    xmits++;
3666		    if (d_status & TULIP_DSTS_ERRSUM) {
3667			CTR1(KTR_TULIP, "tulip_tx_intr: output error: %08x",
3668			    d_status);
3669			sc->tulip_ifp->if_oerrors++;
3670			if (d_status & TULIP_DSTS_TxEXCCOLL)
3671			    sc->tulip_dot3stats.dot3StatsExcessiveCollisions++;
3672			if (d_status & TULIP_DSTS_TxLATECOLL)
3673			    sc->tulip_dot3stats.dot3StatsLateCollisions++;
3674			if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxCARRLOSS))
3675			    sc->tulip_dot3stats.dot3StatsCarrierSenseErrors++;
3676			if (d_status & (TULIP_DSTS_TxUNDERFLOW|TULIP_DSTS_TxBABBLE))
3677			    sc->tulip_dot3stats.dot3StatsInternalMacTransmitErrors++;
3678			if (d_status & TULIP_DSTS_TxUNDERFLOW)
3679			    sc->tulip_dot3stats.dot3StatsInternalTransmitUnderflows++;
3680			if (d_status & TULIP_DSTS_TxBABBLE)
3681			    sc->tulip_dot3stats.dot3StatsInternalTransmitBabbles++;
3682		    } else {
3683			u_int32_t collisions =
3684			    (d_status & TULIP_DSTS_TxCOLLMASK)
3685				>> TULIP_DSTS_V_TxCOLLCNT;
3686
3687			CTR2(KTR_TULIP,
3688		    "tulip_tx_intr: output ok, collisions %d, status %08x",
3689			    collisions, d_status);
3690			sc->tulip_ifp->if_collisions += collisions;
3691			if (collisions == 1)
3692			    sc->tulip_dot3stats.dot3StatsSingleCollisionFrames++;
3693			else if (collisions > 1)
3694			    sc->tulip_dot3stats.dot3StatsMultipleCollisionFrames++;
3695			else if (d_status & TULIP_DSTS_TxDEFERRED)
3696			    sc->tulip_dot3stats.dot3StatsDeferredTransmissions++;
3697			/*
3698			 * SQE is only valid for 10baseT/BNC/AUI when not
3699			 * running in full-duplex.  In order to speed up the
3700			 * test, the corresponding bit in tulip_flags needs to
3701			 * set as well to get us to count SQE Test Errors.
3702			 */
3703			if (d_status & TULIP_DSTS_TxNOHRTBT & sc->tulip_flags)
3704			    sc->tulip_dot3stats.dot3StatsSQETestErrors++;
3705		    }
3706		}
3707	    }
3708	}
3709
3710	if (++ri->ri_nextin == ri->ri_last)
3711	    ri->ri_nextin = ri->ri_first;
3712
3713	if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
3714	    sc->tulip_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3715    }
3716    /*
3717     * If nothing left to transmit, disable the timer.
3718     * Else if progress, reset the timer back to 2 ticks.
3719     */
3720    if (ri->ri_free == ri->ri_max || (sc->tulip_flags & TULIP_TXPROBE_ACTIVE))
3721	sc->tulip_txtimer = 0;
3722    else if (xmits > 0)
3723	sc->tulip_txtimer = TULIP_TXTIMER;
3724    sc->tulip_ifp->if_opackets += xmits;
3725    TULIP_PERFEND(txintr);
3726    return descs;
3727}
3728
3729static void
3730tulip_print_abnormal_interrupt(tulip_softc_t * const sc, u_int32_t csr)
3731{
3732    const char * const *msgp = tulip_status_bits;
3733    const char *sep;
3734    u_int32_t mask;
3735    const char thrsh[] = "72|128\0\0\0" "96|256\0\0\0" "128|512\0\0" "160|1024";
3736
3737    TULIP_LOCK_ASSERT(sc);
3738    csr &= (1 << (sizeof(tulip_status_bits)/sizeof(tulip_status_bits[0]))) - 1;
3739    if_printf(sc->tulip_ifp, "abnormal interrupt:");
3740    for (sep = " ", mask = 1; mask <= csr; mask <<= 1, msgp++) {
3741	if ((csr & mask) && *msgp != NULL) {
3742	    printf("%s%s", sep, *msgp);
3743	    if (mask == TULIP_STS_TXUNDERFLOW && (sc->tulip_flags & TULIP_NEWTXTHRESH)) {
3744		sc->tulip_flags &= ~TULIP_NEWTXTHRESH;
3745		if (sc->tulip_cmdmode & TULIP_CMD_STOREFWD) {
3746		    printf(" (switching to store-and-forward mode)");
3747		} else {
3748		    printf(" (raising TX threshold to %s)",
3749			   &thrsh[9 * ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) >> 14)]);
3750		}
3751	    }
3752	    sep = ", ";
3753	}
3754    }
3755    printf("\n");
3756}
3757
3758static void
3759tulip_intr_handler(tulip_softc_t * const sc)
3760{
3761    TULIP_PERFSTART(intr)
3762    u_int32_t csr;
3763
3764    CTR0(KTR_TULIP, "tulip_intr_handler invoked");
3765    TULIP_LOCK_ASSERT(sc);
3766    while ((csr = TULIP_CSR_READ(sc, csr_status)) & sc->tulip_intrmask) {
3767	TULIP_CSR_WRITE(sc, csr_status, csr);
3768
3769	if (csr & TULIP_STS_SYSERROR) {
3770	    sc->tulip_last_system_error = (csr & TULIP_STS_ERRORMASK) >> TULIP_STS_ERR_SHIFT;
3771	    if (sc->tulip_flags & TULIP_NOMESSAGES) {
3772		sc->tulip_flags |= TULIP_SYSTEMERROR;
3773	    } else {
3774		if_printf(sc->tulip_ifp, "system error: %s\n",
3775		       tulip_system_errors[sc->tulip_last_system_error]);
3776	    }
3777	    sc->tulip_flags |= TULIP_NEEDRESET;
3778	    sc->tulip_system_errors++;
3779	    break;
3780	}
3781	if (csr & (TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL) & sc->tulip_intrmask) {
3782#if defined(TULIP_DEBUG)
3783	    sc->tulip_dbg.dbg_link_intrs++;
3784#endif
3785	    if (sc->tulip_boardsw->bd_media_poll != NULL) {
3786		(*sc->tulip_boardsw->bd_media_poll)(sc, csr & TULIP_STS_LINKFAIL
3787						    ? TULIP_MEDIAPOLL_LINKFAIL
3788						    : TULIP_MEDIAPOLL_LINKPASS);
3789		csr &= ~TULIP_STS_ABNRMLINTR;
3790	    }
3791	    tulip_media_print(sc);
3792	}
3793	if (csr & (TULIP_STS_RXINTR|TULIP_STS_RXNOBUF)) {
3794	    u_int32_t misses = TULIP_CSR_READ(sc, csr_missed_frames);
3795	    if (csr & TULIP_STS_RXNOBUF)
3796		sc->tulip_dot3stats.dot3StatsMissedFrames += misses & 0xFFFF;
3797	    /*
3798	     * Pass 2.[012] of the 21140A-A[CDE] may hang and/or corrupt data
3799	     * on receive overflows.
3800	     */
3801	    if ((misses & 0x0FFE0000) && (sc->tulip_features & TULIP_HAVE_RXBADOVRFLW)) {
3802		sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
3803		/*
3804		 * Stop the receiver process and spin until it's stopped.
3805		 * Tell rx_intr to drop the packets it dequeues.
3806		 */
3807		TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode & ~TULIP_CMD_RXRUN);
3808		while ((TULIP_CSR_READ(sc, csr_status) & TULIP_STS_RXSTOPPED) == 0)
3809		    ;
3810		TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
3811		sc->tulip_flags |= TULIP_RXIGNORE;
3812	    }
3813	    tulip_rx_intr(sc);
3814	    if (sc->tulip_flags & TULIP_RXIGNORE) {
3815		/*
3816		 * Restart the receiver.
3817		 */
3818		sc->tulip_flags &= ~TULIP_RXIGNORE;
3819		TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3820	    }
3821	}
3822	if (csr & TULIP_STS_ABNRMLINTR) {
3823	    u_int32_t tmp = csr & sc->tulip_intrmask
3824		& ~(TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR);
3825	    if (csr & TULIP_STS_TXUNDERFLOW) {
3826		if ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) != TULIP_CMD_THRSHLD160) {
3827		    sc->tulip_cmdmode += TULIP_CMD_THRSHLD96;
3828		    sc->tulip_flags |= TULIP_NEWTXTHRESH;
3829		} else if (sc->tulip_features & TULIP_HAVE_STOREFWD) {
3830		    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD;
3831		    sc->tulip_flags |= TULIP_NEWTXTHRESH;
3832		}
3833	    }
3834	    if (sc->tulip_flags & TULIP_NOMESSAGES) {
3835		sc->tulip_statusbits |= tmp;
3836	    } else {
3837		tulip_print_abnormal_interrupt(sc, tmp);
3838		sc->tulip_flags |= TULIP_NOMESSAGES;
3839	    }
3840	    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3841	}
3842	if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_TXPROBE_ACTIVE|TULIP_DOINGSETUP|TULIP_PROMISC)) {
3843	    tulip_tx_intr(sc);
3844	    if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
3845		tulip_start_locked(sc);
3846	}
3847    }
3848    if (sc->tulip_flags & TULIP_NEEDRESET) {
3849	tulip_reset(sc);
3850	tulip_init_locked(sc);
3851    }
3852    TULIP_PERFEND(intr);
3853}
3854
3855static void
3856tulip_intr_shared(void *arg)
3857{
3858    tulip_softc_t * sc = arg;
3859
3860    for (; sc != NULL; sc = sc->tulip_slaves) {
3861	TULIP_LOCK(sc);
3862#if defined(TULIP_DEBUG)
3863	sc->tulip_dbg.dbg_intrs++;
3864#endif
3865	tulip_intr_handler(sc);
3866	TULIP_UNLOCK(sc);
3867    }
3868}
3869
3870static void
3871tulip_intr_normal(void *arg)
3872{
3873    tulip_softc_t * sc = (tulip_softc_t *) arg;
3874
3875    TULIP_LOCK(sc);
3876#if defined(TULIP_DEBUG)
3877    sc->tulip_dbg.dbg_intrs++;
3878#endif
3879    tulip_intr_handler(sc);
3880    TULIP_UNLOCK(sc);
3881}
3882
3883static struct mbuf *
3884tulip_txput(tulip_softc_t * const sc, struct mbuf *m)
3885{
3886    TULIP_PERFSTART(txput)
3887    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
3888    tulip_descinfo_t *eop, *nextout;
3889    int segcnt, free;
3890    u_int32_t d_status;
3891    bus_dma_segment_t segs[TULIP_MAX_TXSEG];
3892    bus_dmamap_t *map;
3893    int error, nsegs;
3894    struct mbuf *m0;
3895
3896    TULIP_LOCK_ASSERT(sc);
3897#if defined(TULIP_DEBUG)
3898    if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
3899	if_printf(sc->tulip_ifp, "txput%s: tx not running\n",
3900	       (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) ? "(probe)" : "");
3901	sc->tulip_flags |= TULIP_WANTTXSTART;
3902	sc->tulip_dbg.dbg_txput_finishes[0]++;
3903	goto finish;
3904    }
3905#endif
3906
3907    /*
3908     * Now we try to fill in our transmit descriptors.  This is
3909     * a bit reminiscent of going on the Ark two by two
3910     * since each descriptor for the TULIP can describe
3911     * two buffers.  So we advance through packet filling
3912     * each of the two entries at a time to to fill each
3913     * descriptor.  Clear the first and last segment bits
3914     * in each descriptor (actually just clear everything
3915     * but the end-of-ring or chain bits) to make sure
3916     * we don't get messed up by previously sent packets.
3917     *
3918     * We may fail to put the entire packet on the ring if
3919     * there is either not enough ring entries free or if the
3920     * packet has more than MAX_TXSEG segments.  In the former
3921     * case we will just wait for the ring to empty.  In the
3922     * latter case we have to recopy.
3923     */
3924#if defined(KTR) && KTR_TULIP
3925    segcnt = 1;
3926    m0 = m;
3927    while (m0->m_next != NULL) {
3928	    segcnt++;
3929	    m0 = m0->m_next;
3930    }
3931#endif
3932    CTR2(KTR_TULIP, "tulip_txput: sending packet %p (%d chunks)", m, segcnt);
3933    d_status = 0;
3934    eop = nextout = ri->ri_nextout;
3935    segcnt = 0;
3936    free = ri->ri_free;
3937
3938    /*
3939     * Reclaim some tx descriptors if we are out since we need at least one
3940     * free descriptor so that we have a dma_map to load the mbuf.
3941     */
3942    if (free == 0) {
3943#if defined(TULIP_DEBUG)
3944	sc->tulip_dbg.dbg_no_txmaps++;
3945#endif
3946	free += tulip_tx_intr(sc);
3947    }
3948    if (free == 0) {
3949	sc->tulip_flags |= TULIP_WANTTXSTART;
3950#if defined(TULIP_DEBUG)
3951	sc->tulip_dbg.dbg_txput_finishes[1]++;
3952#endif
3953	goto finish;
3954    }
3955    error = bus_dmamap_load_mbuf_sg(ri->ri_data_tag, *eop->di_map, m, segs,
3956	&nsegs, BUS_DMA_NOWAIT);
3957    if (error != 0) {
3958	if (error == EFBIG) {
3959	    /*
3960	     * The packet exceeds the number of transmit buffer
3961	     * entries that we can use for one packet, so we have
3962	     * to recopy it into one mbuf and then try again.  If
3963	     * we can't recopy it, try again later.
3964	     */
3965	    m0 = m_defrag(m, M_DONTWAIT);
3966	    if (m0 == NULL) {
3967		sc->tulip_flags |= TULIP_WANTTXSTART;
3968#if defined(TULIP_DEBUG)
3969		sc->tulip_dbg.dbg_txput_finishes[2]++;
3970#endif
3971		goto finish;
3972	    }
3973	    m = m0;
3974	    error = bus_dmamap_load_mbuf_sg(ri->ri_data_tag, *eop->di_map, m,
3975		segs, &nsegs, BUS_DMA_NOWAIT);
3976	}
3977	if (error != 0) {
3978	    if_printf(sc->tulip_ifp,
3979	        "unable to load tx map, error = %d\n", error);
3980#if defined(TULIP_DEBUG)
3981	    sc->tulip_dbg.dbg_txput_finishes[3]++;
3982#endif
3983	    goto finish;
3984	}
3985    }
3986    CTR1(KTR_TULIP, "tulip_txput: nsegs %d", nsegs);
3987
3988    /*
3989     * Each descriptor allows for up to 2 fragments since we don't use
3990     * the descriptor chaining mode in this driver.
3991     */
3992    if ((free -= (nsegs + 1) / 2) <= 0
3993	    /*
3994	     * See if there's any unclaimed space in the transmit ring.
3995	     */
3996	    && (free += tulip_tx_intr(sc)) <= 0) {
3997	/*
3998	 * There's no more room but since nothing
3999	 * has been committed at this point, just
4000	 * show output is active, put back the
4001	 * mbuf and return.
4002	 */
4003	sc->tulip_flags |= TULIP_WANTTXSTART;
4004#if defined(TULIP_DEBUG)
4005	sc->tulip_dbg.dbg_txput_finishes[4]++;
4006#endif
4007	bus_dmamap_unload(ri->ri_data_tag, *eop->di_map);
4008	goto finish;
4009    }
4010    for (; nsegs - segcnt > 1; segcnt += 2) {
4011	eop = nextout;
4012	eop->di_desc->d_flag   &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4013	eop->di_desc->d_status  = d_status;
4014	eop->di_desc->d_addr1   = segs[segcnt].ds_addr;
4015	eop->di_desc->d_length1 = segs[segcnt].ds_len;
4016	eop->di_desc->d_addr2   = segs[segcnt+1].ds_addr;
4017	eop->di_desc->d_length2 = segs[segcnt+1].ds_len;
4018	d_status = TULIP_DSTS_OWNER;
4019	if (++nextout == ri->ri_last)
4020	    nextout = ri->ri_first;
4021    }
4022    if (segcnt < nsegs) {
4023	eop = nextout;
4024	eop->di_desc->d_flag   &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4025	eop->di_desc->d_status  = d_status;
4026	eop->di_desc->d_addr1   = segs[segcnt].ds_addr;
4027	eop->di_desc->d_length1 = segs[segcnt].ds_len;
4028	eop->di_desc->d_addr2   = 0;
4029	eop->di_desc->d_length2 = 0;
4030	if (++nextout == ri->ri_last)
4031	    nextout = ri->ri_first;
4032    }
4033
4034    /*
4035     * tulip_tx_intr() harvests the mbuf from the last descriptor in the
4036     * frame.  We just used the dmamap in the first descriptor for the
4037     * load operation however.  Thus, to let the tulip_dequeue_mbuf() call
4038     * in tulip_tx_intr() unload the correct dmamap, we swap the dmamap
4039     * pointers in the two descriptors if this is a multiple-descriptor
4040     * packet.
4041     */
4042    if (eop != ri->ri_nextout) {
4043	    map = eop->di_map;
4044	    eop->di_map = ri->ri_nextout->di_map;
4045	    ri->ri_nextout->di_map = map;
4046    }
4047
4048    /*
4049     * bounce a copy to the bpf listener, if any.
4050     */
4051    BPF_MTAP(sc->tulip_ifp, m);
4052
4053    /*
4054     * The descriptors have been filled in.  Now get ready
4055     * to transmit.
4056     */
4057    CTR3(KTR_TULIP, "tulip_txput: enqueued mbuf %p to descriptors %td - %td",
4058	m, ri->ri_nextout - ri->ri_first, eop - ri->ri_first);
4059    KASSERT(eop->di_mbuf == NULL, ("clobbering earlier tx mbuf"));
4060    eop->di_mbuf = m;
4061    TULIP_TXMAP_PRESYNC(ri, ri->ri_nextout);
4062    m = NULL;
4063
4064    /*
4065     * Make sure the next descriptor after this packet is owned
4066     * by us since it may have been set up above if we ran out
4067     * of room in the ring.
4068     */
4069    nextout->di_desc->d_status = 0;
4070    TULIP_TXDESC_PRESYNC(ri);
4071
4072    /*
4073     * Mark the last and first segments, indicate we want a transmit
4074     * complete interrupt, and tell it to transmit!
4075     */
4076    eop->di_desc->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
4077
4078    /*
4079     * Note that ri->ri_nextout is still the start of the packet
4080     * and until we set the OWNER bit, we can still back out of
4081     * everything we have done.
4082     */
4083    ri->ri_nextout->di_desc->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
4084    TULIP_TXDESC_PRESYNC(ri);
4085    ri->ri_nextout->di_desc->d_status = TULIP_DSTS_OWNER;
4086    TULIP_TXDESC_PRESYNC(ri);
4087
4088    /*
4089     * This advances the ring for us.
4090     */
4091    ri->ri_nextout = nextout;
4092    ri->ri_free = free;
4093
4094    TULIP_PERFEND(txput);
4095
4096    if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
4097	TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4098	sc->tulip_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4099	TULIP_PERFEND(txput);
4100	return NULL;
4101    }
4102
4103    /*
4104     * switch back to the single queueing ifstart.
4105     */
4106    sc->tulip_flags &= ~TULIP_WANTTXSTART;
4107    if (sc->tulip_txtimer == 0)
4108	sc->tulip_txtimer = TULIP_TXTIMER;
4109#if defined(TULIP_DEBUG)
4110    sc->tulip_dbg.dbg_txput_finishes[5]++;
4111#endif
4112
4113    /*
4114     * If we want a txstart, there must be not enough space in the
4115     * transmit ring.  So we want to enable transmit done interrupts
4116     * so we can immediately reclaim some space.  When the transmit
4117     * interrupt is posted, the interrupt handler will call tx_intr
4118     * to reclaim space and then txstart (since WANTTXSTART is set).
4119     * txstart will move the packet into the transmit ring and clear
4120     * WANTTXSTART thereby causing TXINTR to be cleared.
4121     */
4122  finish:
4123#if defined(TULIP_DEBUG)
4124    sc->tulip_dbg.dbg_txput_finishes[6]++;
4125#endif
4126    if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_DOINGSETUP)) {
4127	sc->tulip_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4128	if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
4129	    sc->tulip_intrmask |= TULIP_STS_TXINTR;
4130	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4131	}
4132    } else if ((sc->tulip_flags & TULIP_PROMISC) == 0) {
4133	if (sc->tulip_intrmask & TULIP_STS_TXINTR) {
4134	    sc->tulip_intrmask &= ~TULIP_STS_TXINTR;
4135	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4136	}
4137    }
4138    TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4139    TULIP_PERFEND(txput);
4140    return m;
4141}
4142
4143static void
4144tulip_txput_setup(tulip_softc_t * const sc)
4145{
4146    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
4147    tulip_desc_t *nextout;
4148
4149    TULIP_LOCK_ASSERT(sc);
4150
4151    /*
4152     * We will transmit, at most, one setup packet per call to ifstart.
4153     */
4154
4155#if defined(TULIP_DEBUG)
4156    if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
4157	if_printf(sc->tulip_ifp, "txput_setup: tx not running\n");
4158	sc->tulip_flags |= TULIP_WANTTXSTART;
4159	return;
4160    }
4161#endif
4162    /*
4163     * Try to reclaim some free descriptors..
4164     */
4165    if (ri->ri_free < 2)
4166	tulip_tx_intr(sc);
4167    if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
4168	sc->tulip_flags |= TULIP_WANTTXSTART;
4169	return;
4170    }
4171    bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
4172	  sizeof(sc->tulip_setupdata));
4173    /*
4174     * Clear WANTSETUP and set DOINGSETUP.  Since we know that WANTSETUP is
4175     * set and DOINGSETUP is clear doing an XOR of the two will DTRT.
4176     */
4177    sc->tulip_flags ^= TULIP_WANTSETUP|TULIP_DOINGSETUP;
4178    ri->ri_free--;
4179    nextout = ri->ri_nextout->di_desc;
4180    nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4181    nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
4182	|TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
4183    if (sc->tulip_flags & TULIP_WANTHASHPERFECT)
4184	nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
4185    else if (sc->tulip_flags & TULIP_WANTHASHONLY)
4186	nextout->d_flag |= TULIP_DFLAG_TxHASHFILT|TULIP_DFLAG_TxINVRSFILT;
4187
4188    nextout->d_length2 = 0;
4189    nextout->d_addr2 = 0;
4190    nextout->d_length1 = sizeof(sc->tulip_setupdata);
4191    nextout->d_addr1 = sc->tulip_setup_dma_addr;
4192    bus_dmamap_sync(sc->tulip_setup_tag, sc->tulip_setup_map,
4193	BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
4194    TULIP_TXDESC_PRESYNC(ri);
4195    CTR1(KTR_TULIP, "tulip_txput_setup: using descriptor %td",
4196	ri->ri_nextout - ri->ri_first);
4197
4198    /*
4199     * Advance the ring for the next transmit packet.
4200     */
4201    if (++ri->ri_nextout == ri->ri_last)
4202	ri->ri_nextout = ri->ri_first;
4203
4204    /*
4205     * Make sure the next descriptor is owned by us since it
4206     * may have been set up above if we ran out of room in the
4207     * ring.
4208     */
4209    ri->ri_nextout->di_desc->d_status = 0;
4210    TULIP_TXDESC_PRESYNC(ri);
4211    nextout->d_status = TULIP_DSTS_OWNER;
4212    /*
4213     * Flush the ownwership of the current descriptor
4214     */
4215    TULIP_TXDESC_PRESYNC(ri);
4216    TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4217    if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
4218	sc->tulip_intrmask |= TULIP_STS_TXINTR;
4219	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4220    }
4221}
4222
4223static int
4224tulip_ifioctl(struct ifnet * ifp, u_long cmd, caddr_t data)
4225{
4226    TULIP_PERFSTART(ifioctl)
4227    tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
4228    struct ifreq *ifr = (struct ifreq *) data;
4229    int error = 0;
4230
4231    switch (cmd) {
4232	case SIOCSIFFLAGS: {
4233	    TULIP_LOCK(sc);
4234	    tulip_init_locked(sc);
4235	    TULIP_UNLOCK(sc);
4236	    break;
4237	}
4238
4239	case SIOCSIFMEDIA:
4240	case SIOCGIFMEDIA: {
4241	    error = ifmedia_ioctl(ifp, ifr, &sc->tulip_ifmedia, cmd);
4242	    break;
4243	}
4244
4245	case SIOCADDMULTI:
4246	case SIOCDELMULTI: {
4247	    /*
4248	     * Update multicast listeners
4249	     */
4250	    TULIP_LOCK(sc);
4251	    tulip_init_locked(sc);
4252	    TULIP_UNLOCK(sc);
4253	    error = 0;
4254	    break;
4255	}
4256
4257#ifdef SIOCGADDRROM
4258	case SIOCGADDRROM: {
4259	    error = copyout(sc->tulip_rombuf, ifr->ifr_data, sizeof(sc->tulip_rombuf));
4260	    break;
4261	}
4262#endif
4263#ifdef SIOCGCHIPID
4264	case SIOCGCHIPID: {
4265	    ifr->ifr_metric = (int) sc->tulip_chipid;
4266	    break;
4267	}
4268#endif
4269	default: {
4270	    error = ether_ioctl(ifp, cmd, data);
4271	    break;
4272	}
4273    }
4274
4275    TULIP_PERFEND(ifioctl);
4276    return error;
4277}
4278
4279static void
4280tulip_start(struct ifnet * const ifp)
4281{
4282    TULIP_PERFSTART(ifstart)
4283    tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
4284
4285    TULIP_LOCK(sc);
4286    tulip_start_locked(sc);
4287    TULIP_UNLOCK(sc);
4288
4289    TULIP_PERFEND(ifstart);
4290}
4291
4292static void
4293tulip_start_locked(tulip_softc_t * const sc)
4294{
4295    struct mbuf *m;
4296
4297    TULIP_LOCK_ASSERT(sc);
4298
4299    CTR0(KTR_TULIP, "tulip_start_locked invoked");
4300    if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
4301	tulip_txput_setup(sc);
4302
4303    CTR1(KTR_TULIP, "tulip_start_locked: %d tx packets pending",
4304	sc->tulip_ifp->if_snd.ifq_len);
4305    while (!IFQ_DRV_IS_EMPTY(&sc->tulip_ifp->if_snd)) {
4306	IFQ_DRV_DEQUEUE(&sc->tulip_ifp->if_snd, m);
4307	if(m == NULL)
4308	    break;
4309	if ((m = tulip_txput(sc, m)) != NULL) {
4310	    IFQ_DRV_PREPEND(&sc->tulip_ifp->if_snd, m);
4311	    break;
4312	}
4313    }
4314}
4315
4316/*
4317 * Even though this routine runs at device spl, it does not break
4318 * our use of splnet (splsoftnet under NetBSD) for the majority
4319 * of this driver since
4320 * if_watcbog is called from if_watchdog which is called from
4321 * splsoftclock which is below spl[soft]net.
4322 */
4323static void
4324tulip_ifwatchdog(struct ifnet *ifp)
4325{
4326    TULIP_PERFSTART(ifwatchdog)
4327    tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
4328#if defined(TULIP_DEBUG)
4329    u_int32_t rxintrs;
4330#endif
4331
4332    TULIP_LOCK(sc);
4333#if defined(TULIP_DEBUG)
4334    rxintrs = sc->tulip_dbg.dbg_rxintrs - sc->tulip_dbg.dbg_last_rxintrs;
4335    if (rxintrs > sc->tulip_dbg.dbg_high_rxintrs_hz)
4336	sc->tulip_dbg.dbg_high_rxintrs_hz = rxintrs;
4337    sc->tulip_dbg.dbg_last_rxintrs = sc->tulip_dbg.dbg_rxintrs;
4338#endif /* TULIP_DEBUG */
4339
4340    sc->tulip_ifp->if_timer = 1;
4341    /*
4342     * These should be rare so do a bulk test up front so we can just skip
4343     * them if needed.
4344     */
4345    if (sc->tulip_flags & (TULIP_SYSTEMERROR|TULIP_RXBUFSLOW|TULIP_NOMESSAGES)) {
4346	/*
4347	 * If the number of receive buffer is low, try to refill
4348	 */
4349	if (sc->tulip_flags & TULIP_RXBUFSLOW)
4350	    tulip_rx_intr(sc);
4351
4352	if (sc->tulip_flags & TULIP_SYSTEMERROR) {
4353	    if_printf(sc->tulip_ifp, "%d system errors: last was %s\n",
4354		   sc->tulip_system_errors,
4355		   tulip_system_errors[sc->tulip_last_system_error]);
4356	}
4357	if (sc->tulip_statusbits) {
4358	    tulip_print_abnormal_interrupt(sc, sc->tulip_statusbits);
4359	    sc->tulip_statusbits = 0;
4360	}
4361
4362	sc->tulip_flags &= ~(TULIP_NOMESSAGES|TULIP_SYSTEMERROR);
4363    }
4364
4365    if (sc->tulip_txtimer)
4366	tulip_tx_intr(sc);
4367    if (sc->tulip_txtimer && --sc->tulip_txtimer == 0) {
4368	if_printf(sc->tulip_ifp, "transmission timeout\n");
4369	if (TULIP_DO_AUTOSENSE(sc)) {
4370	    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
4371	    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
4372	    sc->tulip_flags &= ~(TULIP_WANTRXACT|TULIP_LINKUP);
4373	}
4374	tulip_reset(sc);
4375	tulip_init_locked(sc);
4376    }
4377
4378    TULIP_PERFEND(ifwatchdog);
4379    TULIP_PERFMERGE(sc, perf_intr_cycles);
4380    TULIP_PERFMERGE(sc, perf_ifstart_cycles);
4381    TULIP_PERFMERGE(sc, perf_ifioctl_cycles);
4382    TULIP_PERFMERGE(sc, perf_ifwatchdog_cycles);
4383    TULIP_PERFMERGE(sc, perf_timeout_cycles);
4384    TULIP_PERFMERGE(sc, perf_ifstart_one_cycles);
4385    TULIP_PERFMERGE(sc, perf_txput_cycles);
4386    TULIP_PERFMERGE(sc, perf_txintr_cycles);
4387    TULIP_PERFMERGE(sc, perf_rxintr_cycles);
4388    TULIP_PERFMERGE(sc, perf_rxget_cycles);
4389    TULIP_PERFMERGE(sc, perf_intr);
4390    TULIP_PERFMERGE(sc, perf_ifstart);
4391    TULIP_PERFMERGE(sc, perf_ifioctl);
4392    TULIP_PERFMERGE(sc, perf_ifwatchdog);
4393    TULIP_PERFMERGE(sc, perf_timeout);
4394    TULIP_PERFMERGE(sc, perf_ifstart_one);
4395    TULIP_PERFMERGE(sc, perf_txput);
4396    TULIP_PERFMERGE(sc, perf_txintr);
4397    TULIP_PERFMERGE(sc, perf_rxintr);
4398    TULIP_PERFMERGE(sc, perf_rxget);
4399    TULIP_UNLOCK(sc);
4400}
4401
4402static void
4403tulip_attach(tulip_softc_t * const sc)
4404{
4405    struct ifnet *ifp;
4406
4407    ifp = sc->tulip_ifp = if_alloc(IFT_ETHER);
4408
4409    /* XXX: driver name/unit should be set some other way */
4410    if_initname(ifp, "de", sc->tulip_unit);
4411    ifp->if_softc = sc;
4412    ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
4413    ifp->if_ioctl = tulip_ifioctl;
4414    ifp->if_start = tulip_start;
4415    ifp->if_watchdog = tulip_ifwatchdog;
4416    ifp->if_timer = 1;
4417    ifp->if_init = tulip_init;
4418    IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
4419    ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
4420    IFQ_SET_READY(&ifp->if_snd);
4421
4422    if_printf(ifp, "%s%s pass %d.%d%s\n",
4423	   sc->tulip_boardid,
4424	   tulip_chipdescs[sc->tulip_chipid],
4425	   (sc->tulip_revinfo & 0xF0) >> 4,
4426	   sc->tulip_revinfo & 0x0F,
4427	   (sc->tulip_features & (TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM))
4428		 == TULIP_HAVE_ISVSROM ? " (invalid EESPROM checksum)" : "");
4429
4430    TULIP_LOCK(sc);
4431#if defined(__alpha__)
4432    /*
4433     * In case the SRM console told us about a bogus media,
4434     * we need to check to be safe.
4435     */
4436    if (sc->tulip_mediums[sc->tulip_media] == NULL)
4437	sc->tulip_media = TULIP_MEDIA_UNKNOWN;
4438#endif
4439
4440    (*sc->tulip_boardsw->bd_media_probe)(sc);
4441    ifmedia_init(&sc->tulip_ifmedia, 0,
4442		 tulip_ifmedia_change,
4443		 tulip_ifmedia_status);
4444    sc->tulip_flags &= ~TULIP_DEVICEPROBE;
4445    tulip_ifmedia_add(sc);
4446
4447    tulip_reset(sc);
4448    TULIP_UNLOCK(sc);
4449
4450    ether_ifattach(sc->tulip_ifp, sc->tulip_enaddr);
4451}
4452
4453/* Release memory for a single descriptor ring. */
4454static void
4455tulip_busdma_freering(tulip_ringinfo_t *ri)
4456{
4457    int i;
4458
4459    /* Release the DMA maps and tag for data buffers. */
4460    if (ri->ri_data_maps != NULL) {
4461	for (i = 0; i < ri->ri_max; i++) {
4462	    if (ri->ri_data_maps[i] != NULL) {
4463		bus_dmamap_destroy(ri->ri_data_tag, ri->ri_data_maps[i]);
4464		ri->ri_data_maps[i] = NULL;
4465	    }
4466	}
4467	free(ri->ri_data_maps, M_DEVBUF);
4468	ri->ri_data_maps = NULL;
4469    }
4470    if (ri->ri_data_tag != NULL) {
4471	bus_dma_tag_destroy(ri->ri_data_tag);
4472	ri->ri_data_tag = NULL;
4473    }
4474
4475    /* Release the DMA memory and tag for the ring descriptors. */
4476    if (ri->ri_dma_addr != 0) {
4477	bus_dmamap_unload(ri->ri_ring_tag, ri->ri_ring_map);
4478	ri->ri_dma_addr = 0;
4479    }
4480    if (ri->ri_descs != NULL) {
4481	bus_dmamem_free(ri->ri_ring_tag, ri->ri_descs, ri->ri_ring_map);
4482	ri->ri_ring_map = NULL;
4483	ri->ri_descs = NULL;
4484    }
4485    if (ri->ri_ring_tag != NULL) {
4486	bus_dma_tag_destroy(ri->ri_ring_tag);
4487	ri->ri_ring_tag = NULL;
4488    }
4489}
4490
4491/* Allocate memory for a single descriptor ring. */
4492static int
4493tulip_busdma_allocring(device_t dev, tulip_softc_t * const sc, size_t count,
4494    bus_size_t maxsize, int nsegs, tulip_ringinfo_t *ri, const char *name)
4495{
4496    size_t size;
4497    int error, i;
4498
4499    /* First, setup a tag. */
4500    ri->ri_max = count;
4501    size = count * sizeof(tulip_desc_t);
4502    error = bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
4503	BUS_SPACE_MAXADDR, NULL, NULL, size, 1, size, 0, NULL, NULL,
4504	&ri->ri_ring_tag);
4505    if (error) {
4506	device_printf(dev, "failed to allocate %s descriptor ring dma tag\n",
4507	    name);
4508	return (error);
4509    }
4510
4511    /* Next, allocate memory for the descriptors. */
4512    error = bus_dmamem_alloc(ri->ri_ring_tag, (void **)&ri->ri_descs,
4513	BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ri->ri_ring_map);
4514    if (error) {
4515	device_printf(dev, "failed to allocate memory for %s descriptor ring\n",
4516	    name);
4517	return (error);
4518    }
4519
4520    /* Map the descriptors. */
4521    error = bus_dmamap_load(ri->ri_ring_tag, ri->ri_ring_map, ri->ri_descs,
4522	size, tulip_dma_map_addr, &ri->ri_dma_addr, BUS_DMA_NOWAIT);
4523    if (error) {
4524	device_printf(dev, "failed to get dma address for %s descriptor ring\n",
4525	    name);
4526	return (error);
4527    }
4528
4529    /* Allocate a tag for the data buffers. */
4530    error = bus_dma_tag_create(NULL, 4, 0,
4531	BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
4532	maxsize, nsegs, TULIP_DATA_PER_DESC, 0, NULL, NULL, &ri->ri_data_tag);
4533    if (error) {
4534	device_printf(dev, "failed to allocate %s buffer dma tag\n", name);
4535	return (error);
4536    }
4537
4538    /* Allocate maps for the data buffers. */
4539    ri->ri_data_maps = malloc(sizeof(bus_dmamap_t) * count, M_DEVBUF,
4540	M_WAITOK | M_ZERO);
4541    for (i = 0; i < count; i++) {
4542    	error = bus_dmamap_create(ri->ri_data_tag, 0, &ri->ri_data_maps[i]);
4543	if (error) {
4544	    device_printf(dev, "failed to create map for %s buffer %d\n",
4545		name, i);
4546	    return (error);
4547	}
4548    }
4549
4550    return (0);
4551}
4552
4553/* Release busdma maps, tags, and memory. */
4554static void
4555tulip_busdma_cleanup(tulip_softc_t * const sc)
4556{
4557
4558    /* Release resources for the setup descriptor. */
4559    if (sc->tulip_setup_dma_addr != 0) {
4560	bus_dmamap_unload(sc->tulip_setup_tag, sc->tulip_setup_map);
4561	sc->tulip_setup_dma_addr = 0;
4562    }
4563    if (sc->tulip_setupbuf != NULL) {
4564	bus_dmamem_free(sc->tulip_setup_tag, sc->tulip_setupdata,
4565	    sc->tulip_setup_map);
4566	sc->tulip_setup_map = NULL;
4567	sc->tulip_setupbuf = NULL;
4568    }
4569    if (sc->tulip_setup_tag != NULL) {
4570	bus_dma_tag_destroy(sc->tulip_setup_tag);
4571	sc->tulip_setup_tag = NULL;
4572    }
4573
4574    /* Release the transmit ring. */
4575    tulip_busdma_freering(&sc->tulip_txinfo);
4576
4577    /* Release the receive ring. */
4578    tulip_busdma_freering(&sc->tulip_rxinfo);
4579}
4580
4581static int
4582tulip_busdma_init(device_t dev, tulip_softc_t * const sc)
4583{
4584    int error;
4585
4586    /*
4587     * Allocate space and dmamap for transmit ring.
4588     */
4589    error = tulip_busdma_allocring(dev, sc, TULIP_TXDESCS, TULIP_DATA_PER_DESC,
4590	TULIP_MAX_TXSEG, &sc->tulip_txinfo, "transmit");
4591    if (error)
4592	return (error);
4593
4594    /*
4595     * Allocate space and dmamap for receive ring.  We tell bus_dma that
4596     * we can map MCLBYTES so that it will accept a full MCLBYTES cluster,
4597     * but we will only map the first TULIP_RX_BUFLEN bytes.  This is not
4598     * a waste in practice though as an ethernet frame can easily fit
4599     * in TULIP_RX_BUFLEN bytes.
4600     */
4601    error = tulip_busdma_allocring(dev, sc, TULIP_RXDESCS, MCLBYTES, 1,
4602	&sc->tulip_rxinfo, "receive");
4603    if (error)
4604	return (error);
4605
4606    /*
4607     * Allocate a DMA tag, memory, and map for setup descriptor
4608     */
4609    error = bus_dma_tag_create(NULL, 4, 0,
4610	BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
4611	sizeof(sc->tulip_setupdata), 1, sizeof(sc->tulip_setupdata), 0,
4612	NULL, NULL, &sc->tulip_setup_tag);
4613    if (error) {
4614	device_printf(dev, "failed to allocate setup descriptor dma tag\n");
4615	return (error);
4616    }
4617    error = bus_dmamem_alloc(sc->tulip_setup_tag, (void **)&sc->tulip_setupbuf,
4618	BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tulip_setup_map);
4619    if (error) {
4620	device_printf(dev, "failed to allocate memory for setup descriptor\n");
4621	return (error);
4622    }
4623    error = bus_dmamap_load(sc->tulip_setup_tag, sc->tulip_setup_map,
4624	sc->tulip_setupbuf, sizeof(sc->tulip_setupdata),
4625	tulip_dma_map_addr, &sc->tulip_setup_dma_addr, BUS_DMA_NOWAIT);
4626    if (error) {
4627	device_printf(dev, "failed to get dma address for setup descriptor\n");
4628	return (error);
4629    }
4630
4631    return error;
4632}
4633
4634static void
4635tulip_initcsrs(tulip_softc_t * const sc, tulip_csrptr_t csr_base,
4636    size_t csr_size)
4637{
4638    sc->tulip_csrs.csr_busmode		= csr_base +  0 * csr_size;
4639    sc->tulip_csrs.csr_txpoll		= csr_base +  1 * csr_size;
4640    sc->tulip_csrs.csr_rxpoll		= csr_base +  2 * csr_size;
4641    sc->tulip_csrs.csr_rxlist		= csr_base +  3 * csr_size;
4642    sc->tulip_csrs.csr_txlist		= csr_base +  4 * csr_size;
4643    sc->tulip_csrs.csr_status		= csr_base +  5 * csr_size;
4644    sc->tulip_csrs.csr_command		= csr_base +  6 * csr_size;
4645    sc->tulip_csrs.csr_intr		= csr_base +  7 * csr_size;
4646    sc->tulip_csrs.csr_missed_frames	= csr_base +  8 * csr_size;
4647    sc->tulip_csrs.csr_9		= csr_base +  9 * csr_size;
4648    sc->tulip_csrs.csr_10		= csr_base + 10 * csr_size;
4649    sc->tulip_csrs.csr_11		= csr_base + 11 * csr_size;
4650    sc->tulip_csrs.csr_12		= csr_base + 12 * csr_size;
4651    sc->tulip_csrs.csr_13		= csr_base + 13 * csr_size;
4652    sc->tulip_csrs.csr_14		= csr_base + 14 * csr_size;
4653    sc->tulip_csrs.csr_15		= csr_base + 15 * csr_size;
4654}
4655
4656static int
4657tulip_initring(
4658    device_t dev,
4659    tulip_softc_t * const sc,
4660    tulip_ringinfo_t * const ri,
4661    int ndescs)
4662{
4663    int i;
4664
4665    ri->ri_descinfo = malloc(sizeof(tulip_descinfo_t) * ndescs, M_DEVBUF,
4666	M_WAITOK | M_ZERO);
4667    for (i = 0; i < ndescs; i++) {
4668	ri->ri_descinfo[i].di_desc = &ri->ri_descs[i];
4669	ri->ri_descinfo[i].di_map = &ri->ri_data_maps[i];
4670    }
4671    ri->ri_first = ri->ri_descinfo;
4672    ri->ri_max = ndescs;
4673    ri->ri_last = ri->ri_first + ri->ri_max;
4674    bzero(ri->ri_descs, sizeof(tulip_desc_t) * ri->ri_max);
4675    ri->ri_last[-1].di_desc->d_flag = TULIP_DFLAG_ENDRING;
4676    return (0);
4677}
4678
4679/*
4680 * This is the PCI configuration support.
4681 */
4682
4683#define	PCI_CBIO	PCIR_BAR(0)	/* Configuration Base IO Address */
4684#define	PCI_CBMA	PCIR_BAR(1)	/* Configuration Base Memory Address */
4685#define	PCI_CFDA	0x40	/* Configuration Driver Area */
4686
4687static int
4688tulip_pci_probe(device_t dev)
4689{
4690    const char *name = NULL;
4691
4692    if (pci_get_vendor(dev) != DEC_VENDORID)
4693	return ENXIO;
4694
4695    /*
4696     * Some LanMedia WAN cards use the Tulip chip, but they have
4697     * their own driver, and we should not recognize them
4698     */
4699    if (pci_get_subvendor(dev) == 0x1376)
4700	return ENXIO;
4701
4702    switch (pci_get_device(dev)) {
4703    case CHIPID_21040:
4704	name = "Digital 21040 Ethernet";
4705	break;
4706    case CHIPID_21041:
4707	name = "Digital 21041 Ethernet";
4708	break;
4709    case CHIPID_21140:
4710	if (pci_get_revid(dev) >= 0x20)
4711	    name = "Digital 21140A Fast Ethernet";
4712	else
4713	    name = "Digital 21140 Fast Ethernet";
4714	break;
4715    case CHIPID_21142:
4716	if (pci_get_revid(dev) >= 0x20)
4717	    name = "Digital 21143 Fast Ethernet";
4718	else
4719	    name = "Digital 21142 Fast Ethernet";
4720	break;
4721    }
4722    if (name) {
4723	device_set_desc(dev, name);
4724	return BUS_PROBE_LOW_PRIORITY;
4725    }
4726    return ENXIO;
4727}
4728
4729static int
4730tulip_shutdown(device_t dev)
4731{
4732    tulip_softc_t * const sc = device_get_softc(dev);
4733    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
4734    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
4735		   33MHz that comes to two microseconds but wait a
4736		   bit longer anyways) */
4737    return 0;
4738}
4739
4740static int
4741tulip_pci_attach(device_t dev)
4742{
4743    tulip_softc_t *sc;
4744#if defined(__alpha__)
4745    tulip_media_t media = TULIP_MEDIA_UNKNOWN;
4746#endif
4747    int retval, idx;
4748    u_int32_t revinfo, cfdainfo;
4749    unsigned csroffset = TULIP_PCI_CSROFFSET;
4750    unsigned csrsize = TULIP_PCI_CSRSIZE;
4751    tulip_csrptr_t csr_base;
4752    tulip_chipid_t chipid = TULIP_CHIPID_UNKNOWN;
4753    struct resource *res;
4754    int rid, unit;
4755
4756    unit = device_get_unit(dev);
4757
4758    if (unit >= TULIP_MAX_DEVICES) {
4759	device_printf(dev, "not configured; limit of %d reached or exceeded\n",
4760	       TULIP_MAX_DEVICES);
4761	return ENXIO;
4762    }
4763
4764    revinfo  = pci_get_revid(dev);
4765    cfdainfo = pci_read_config(dev, PCI_CFDA, 4);
4766
4767    /* turn busmaster on in case BIOS doesn't set it */
4768    pci_enable_busmaster(dev);
4769
4770    if (pci_get_vendor(dev) == DEC_VENDORID) {
4771	if (pci_get_device(dev) == CHIPID_21040)
4772		chipid = TULIP_21040;
4773	else if (pci_get_device(dev) == CHIPID_21041)
4774		chipid = TULIP_21041;
4775	else if (pci_get_device(dev) == CHIPID_21140)
4776		chipid = (revinfo >= 0x20) ? TULIP_21140A : TULIP_21140;
4777	else if (pci_get_device(dev) == CHIPID_21142)
4778		chipid = (revinfo >= 0x20) ? TULIP_21143 : TULIP_21142;
4779    }
4780    if (chipid == TULIP_CHIPID_UNKNOWN)
4781	return ENXIO;
4782
4783    if (chipid == TULIP_21040 && revinfo < 0x20) {
4784	device_printf(dev,
4785	    "not configured; 21040 pass 2.0 required (%d.%d found)\n",
4786	    revinfo >> 4, revinfo & 0x0f);
4787	return ENXIO;
4788    } else if (chipid == TULIP_21140 && revinfo < 0x11) {
4789	device_printf(dev,
4790	    "not configured; 21140 pass 1.1 required (%d.%d found)\n",
4791	    revinfo >> 4, revinfo & 0x0f);
4792	return ENXIO;
4793    }
4794
4795    sc = device_get_softc(dev);
4796    sc->tulip_pci_busno = pci_get_bus(dev);
4797    sc->tulip_pci_devno = pci_get_slot(dev);
4798    sc->tulip_chipid = chipid;
4799    sc->tulip_flags |= TULIP_DEVICEPROBE;
4800    if (chipid == TULIP_21140 || chipid == TULIP_21140A)
4801	sc->tulip_features |= TULIP_HAVE_GPR|TULIP_HAVE_STOREFWD;
4802    if (chipid == TULIP_21140A && revinfo <= 0x22)
4803	sc->tulip_features |= TULIP_HAVE_RXBADOVRFLW;
4804    if (chipid == TULIP_21140)
4805	sc->tulip_features |= TULIP_HAVE_BROKEN_HASH;
4806    if (chipid != TULIP_21040 && chipid != TULIP_21140)
4807	sc->tulip_features |= TULIP_HAVE_POWERMGMT;
4808    if (chipid == TULIP_21041 || chipid == TULIP_21142 || chipid == TULIP_21143) {
4809	sc->tulip_features |= TULIP_HAVE_DUALSENSE;
4810	if (chipid != TULIP_21041 || revinfo >= 0x20)
4811	    sc->tulip_features |= TULIP_HAVE_SIANWAY;
4812	if (chipid != TULIP_21041)
4813	    sc->tulip_features |= TULIP_HAVE_SIAGP|TULIP_HAVE_RXBADOVRFLW|TULIP_HAVE_STOREFWD;
4814	if (chipid != TULIP_21041 && revinfo >= 0x20)
4815	    sc->tulip_features |= TULIP_HAVE_SIA100;
4816    }
4817
4818    if (sc->tulip_features & TULIP_HAVE_POWERMGMT
4819	    && (cfdainfo & (TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE))) {
4820	cfdainfo &= ~(TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE);
4821	pci_write_config(dev, PCI_CFDA, cfdainfo, 4);
4822	DELAY(11*1000);
4823    }
4824#if defined(__alpha__)
4825    /*
4826     * The Alpha SRM console encodes a console set media in the driver
4827     * part of the CFDA register.  Note that the Multia presents a
4828     * problem in that its BNC mode is really EXTSIA.  So in that case
4829     * force a probe.
4830     */
4831    switch ((cfdainfo >> 8) & 0xff) {
4832	case 1: media = chipid > TULIP_21040 ? TULIP_MEDIA_AUI : TULIP_MEDIA_AUIBNC; break;
4833	case 2: media = chipid > TULIP_21040 ? TULIP_MEDIA_BNC : TULIP_MEDIA_UNKNOWN; break;
4834	case 3: media = TULIP_MEDIA_10BASET; break;
4835	case 4: media = TULIP_MEDIA_10BASET_FD; break;
4836	case 5: media = TULIP_MEDIA_100BASETX; break;
4837	case 6: media = TULIP_MEDIA_100BASETX_FD; break;
4838	default: media = TULIP_MEDIA_UNKNOWN; break;
4839    }
4840#endif
4841
4842    sc->tulip_unit = unit;
4843    sc->tulip_revinfo = revinfo;
4844#if defined(TULIP_IOMAPPED)
4845    rid = PCI_CBIO;
4846    res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
4847#else
4848    rid = PCI_CBMA;
4849    res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
4850#endif
4851    if (!res)
4852	return ENXIO;
4853    sc->tulip_csrs_bst = rman_get_bustag(res);
4854    sc->tulip_csrs_bsh = rman_get_bushandle(res);
4855    csr_base = 0;
4856
4857    mtx_init(TULIP_MUTEX(sc), MTX_NETWORK_LOCK, device_get_nameunit(dev),
4858	MTX_DEF);
4859    callout_init_mtx(&sc->tulip_callout, TULIP_MUTEX(sc), 0);
4860    tulips[unit] = sc;
4861
4862    tulip_initcsrs(sc, csr_base + csroffset, csrsize);
4863
4864    if ((retval = tulip_busdma_init(dev, sc)) != 0) {
4865	device_printf(dev, "error initing bus_dma: %d\n", retval);
4866	tulip_busdma_cleanup(sc);
4867	mtx_destroy(TULIP_MUTEX(sc));
4868	return ENXIO;
4869    }
4870
4871    retval = tulip_initring(dev, sc, &sc->tulip_rxinfo, TULIP_RXDESCS);
4872    if (retval == 0)
4873	retval = tulip_initring(dev, sc, &sc->tulip_txinfo, TULIP_TXDESCS);
4874    if (retval) {
4875	tulip_busdma_cleanup(sc);
4876	mtx_destroy(TULIP_MUTEX(sc));
4877	return retval;
4878    }
4879
4880    /*
4881     * Make sure there won't be any interrupts or such...
4882     */
4883    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
4884    DELAY(100);	/* Wait 10 microseconds (actually 50 PCI cycles but at
4885		   33MHz that comes to two microseconds but wait a
4886		   bit longer anyways) */
4887
4888    TULIP_LOCK(sc);
4889    retval = tulip_read_macaddr(sc);
4890    TULIP_UNLOCK(sc);
4891    if (retval < 0) {
4892	device_printf(dev, "can't read ENET ROM (why=%d) (", retval);
4893	for (idx = 0; idx < 32; idx++)
4894	    printf("%02x", sc->tulip_rombuf[idx]);
4895	printf("\n");
4896	device_printf(dev, "%s%s pass %d.%d\n",
4897	       sc->tulip_boardid, tulip_chipdescs[sc->tulip_chipid],
4898	       (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F);
4899	device_printf(dev, "address unknown\n");
4900    } else {
4901	void (*intr_rtn)(void *) = tulip_intr_normal;
4902
4903	if (sc->tulip_features & TULIP_HAVE_SHAREDINTR)
4904	    intr_rtn = tulip_intr_shared;
4905
4906#if defined(__alpha__)
4907	sc->tulip_media = media;
4908#endif
4909	tulip_attach(sc);
4910
4911	/* Setup interrupt last. */
4912	if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
4913	    void *ih;
4914
4915	    rid = 0;
4916	    res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
4917					 RF_SHAREABLE | RF_ACTIVE);
4918	    if (res == 0 || bus_setup_intr(dev, res, INTR_TYPE_NET |
4919		    INTR_MPSAFE, intr_rtn, sc, &ih)) {
4920		device_printf(dev, "couldn't map interrupt\n");
4921		tulip_busdma_cleanup(sc);
4922		ether_ifdetach(sc->tulip_ifp);
4923		if_free(sc->tulip_ifp);
4924		mtx_destroy(TULIP_MUTEX(sc));
4925		return ENXIO;
4926	    }
4927	}
4928
4929#if defined(__alpha__)
4930	TULIP_LOCK(sc);
4931	if (sc->tulip_media != TULIP_MEDIA_UNKNOWN)
4932		tulip_linkup(sc, media);
4933	TULIP_UNLOCK(sc);
4934#endif
4935    }
4936    return 0;
4937}
4938
4939static device_method_t tulip_pci_methods[] = {
4940    /* Device interface */
4941    DEVMETHOD(device_probe,	tulip_pci_probe),
4942    DEVMETHOD(device_attach,	tulip_pci_attach),
4943    DEVMETHOD(device_shutdown,	tulip_shutdown),
4944    { 0, 0 }
4945};
4946
4947static driver_t tulip_pci_driver = {
4948    "de",
4949    tulip_pci_methods,
4950    sizeof(tulip_softc_t),
4951};
4952
4953static devclass_t tulip_devclass;
4954
4955DRIVER_MODULE(de, pci, tulip_pci_driver, tulip_devclass, 0, 0);
4956
4957#ifdef DDB
4958void	tulip_dumpring(int unit, int ring);
4959void	tulip_dumpdesc(int unit, int ring, int desc);
4960void	tulip_status(int unit);
4961
4962void
4963tulip_dumpring(int unit, int ring)
4964{
4965    tulip_softc_t *sc;
4966    tulip_ringinfo_t *ri;
4967    tulip_descinfo_t *di;
4968
4969    if (unit < 0 || unit >= TULIP_MAX_DEVICES) {
4970	db_printf("invalid unit %d\n", unit);
4971	return;
4972    }
4973    sc = tulips[unit];
4974    if (sc == NULL) {
4975	db_printf("unit %d not present\n", unit);
4976	return;
4977    }
4978
4979    switch (ring) {
4980    case 0:
4981	db_printf("receive ring:\n");
4982	ri = &sc->tulip_rxinfo;
4983	break;
4984    case 1:
4985	db_printf("transmit ring:\n");
4986	ri = &sc->tulip_txinfo;
4987	break;
4988    default:
4989	db_printf("invalid ring %d\n", ring);
4990	return;
4991    }
4992
4993    db_printf(" nextin: %td, nextout: %td, max: %d, free: %d\n",
4994	ri->ri_nextin - ri->ri_first, ri->ri_nextout - ri->ri_first,
4995	ri->ri_max, ri->ri_free);
4996    for (di = ri->ri_first; di != ri->ri_last; di++) {
4997	if (di->di_mbuf != NULL)
4998	    db_printf(" descriptor %td: mbuf %p\n", di - ri->ri_first,
4999		di->di_mbuf);
5000	else if (di->di_desc->d_flag & TULIP_DFLAG_TxSETUPPKT)
5001	    db_printf(" descriptor %td: setup packet\n", di - ri->ri_first);
5002    }
5003}
5004
5005void
5006tulip_dumpdesc(int unit, int ring, int desc)
5007{
5008    tulip_softc_t *sc;
5009    tulip_ringinfo_t *ri;
5010    tulip_descinfo_t *di;
5011    char *s;
5012
5013    if (unit < 0 || unit >= TULIP_MAX_DEVICES) {
5014	db_printf("invalid unit %d\n", unit);
5015	return;
5016    }
5017    sc = tulips[unit];
5018    if (sc == NULL) {
5019	db_printf("unit %d not present\n", unit);
5020	return;
5021    }
5022
5023    switch (ring) {
5024    case 0:
5025	s = "receive";
5026	ri = &sc->tulip_rxinfo;
5027	break;
5028    case 1:
5029	s = "transmit";
5030	ri = &sc->tulip_txinfo;
5031	break;
5032    default:
5033	db_printf("invalid ring %d\n", ring);
5034	return;
5035    }
5036
5037    if (desc < 0 || desc >= ri->ri_max) {
5038	db_printf("invalid descriptor %d\n", desc);
5039	return;
5040    }
5041
5042    db_printf("%s descriptor %d:\n", s, desc);
5043    di = &ri->ri_first[desc];
5044    db_printf(" mbuf: %p\n", di->di_mbuf);
5045    db_printf(" status: %08x  flag: %03x\n", di->di_desc->d_status,
5046	di->di_desc->d_flag);
5047    db_printf("  addr1: %08x  len1: %03x\n", di->di_desc->d_addr1,
5048	di->di_desc->d_length1);
5049    db_printf("  addr2: %08x  len2: %03x\n", di->di_desc->d_addr2,
5050	di->di_desc->d_length2);
5051}
5052#endif
5053