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