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