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