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