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