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