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