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