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