xref: /netbsd-src/sys/dev/ic/tulip.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: tulip.c,v 1.68 2000/05/26 16:38:13 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Device driver for the Digital Semiconductor ``Tulip'' (21x4x)
42  * Ethernet controller family, and a variety of clone chips.
43  */
44 
45 #include "opt_inet.h"
46 #include "opt_ns.h"
47 #include "bpfilter.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/callout.h>
52 #include <sys/mbuf.h>
53 #include <sys/malloc.h>
54 #include <sys/kernel.h>
55 #include <sys/socket.h>
56 #include <sys/ioctl.h>
57 #include <sys/errno.h>
58 #include <sys/device.h>
59 
60 #include <machine/endian.h>
61 
62 #include <vm/vm.h>		/* for PAGE_SIZE */
63 
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67 #include <net/if_ether.h>
68 
69 #if NBPFILTER > 0
70 #include <net/bpf.h>
71 #endif
72 
73 #ifdef INET
74 #include <netinet/in.h>
75 #include <netinet/if_inarp.h>
76 #endif
77 
78 #ifdef NS
79 #include <netns/ns.h>
80 #include <netns/ns_if.h>
81 #endif
82 
83 #include <machine/bus.h>
84 #include <machine/intr.h>
85 
86 #include <dev/mii/mii.h>
87 #include <dev/mii/miivar.h>
88 #include <dev/mii/mii_bitbang.h>
89 
90 #include <dev/ic/tulipreg.h>
91 #include <dev/ic/tulipvar.h>
92 
93 const char *tlp_chip_names[] = TULIP_CHIP_NAMES;
94 
95 const struct tulip_txthresh_tab tlp_10_txthresh_tab[] =
96     TLP_TXTHRESH_TAB_10;
97 
98 const struct tulip_txthresh_tab tlp_10_100_txthresh_tab[] =
99     TLP_TXTHRESH_TAB_10_100;
100 
101 const struct tulip_txthresh_tab tlp_winb_txthresh_tab[] =
102     TLP_TXTHRESH_TAB_WINB;
103 
104 const struct tulip_txthresh_tab tlp_dm9102_txthresh_tab[] =
105     TLP_TXTHRESH_TAB_DM9102;
106 
107 void	tlp_start __P((struct ifnet *));
108 void	tlp_watchdog __P((struct ifnet *));
109 int	tlp_ioctl __P((struct ifnet *, u_long, caddr_t));
110 
111 void	tlp_shutdown __P((void *));
112 
113 void	tlp_reset __P((struct tulip_softc *));
114 int	tlp_init __P((struct tulip_softc *));
115 void	tlp_rxdrain __P((struct tulip_softc *));
116 void	tlp_stop __P((struct tulip_softc *, int));
117 int	tlp_add_rxbuf __P((struct tulip_softc *, int));
118 void	tlp_idle __P((struct tulip_softc *, u_int32_t));
119 void	tlp_srom_idle __P((struct tulip_softc *));
120 int	tlp_srom_size __P((struct tulip_softc *));
121 
122 int	tlp_enable __P((struct tulip_softc *));
123 void	tlp_disable __P((struct tulip_softc *));
124 void	tlp_power __P((int, void *));
125 
126 void	tlp_filter_setup __P((struct tulip_softc *));
127 void	tlp_winb_filter_setup __P((struct tulip_softc *));
128 void	tlp_al981_filter_setup __P((struct tulip_softc *));
129 
130 void	tlp_rxintr __P((struct tulip_softc *));
131 void	tlp_txintr __P((struct tulip_softc *));
132 
133 void	tlp_mii_tick __P((void *));
134 void	tlp_mii_statchg __P((struct device *));
135 void	tlp_winb_mii_statchg __P((struct device *));
136 void	tlp_dm9102_mii_statchg __P((struct device *));
137 
138 void	tlp_mii_getmedia __P((struct tulip_softc *, struct ifmediareq *));
139 int	tlp_mii_setmedia __P((struct tulip_softc *));
140 
141 int	tlp_bitbang_mii_readreg __P((struct device *, int, int));
142 void	tlp_bitbang_mii_writereg __P((struct device *, int, int, int));
143 
144 int	tlp_pnic_mii_readreg __P((struct device *, int, int));
145 void	tlp_pnic_mii_writereg __P((struct device *, int, int, int));
146 
147 int	tlp_al981_mii_readreg __P((struct device *, int, int));
148 void	tlp_al981_mii_writereg __P((struct device *, int, int, int));
149 
150 void	tlp_2114x_preinit __P((struct tulip_softc *));
151 void	tlp_2114x_mii_preinit __P((struct tulip_softc *));
152 void	tlp_pnic_preinit __P((struct tulip_softc *));
153 void	tlp_dm9102_preinit __P((struct tulip_softc *));
154 
155 void	tlp_21140_reset __P((struct tulip_softc *));
156 void	tlp_21142_reset __P((struct tulip_softc *));
157 void	tlp_pmac_reset __P((struct tulip_softc *));
158 void	tlp_dm9102_reset __P((struct tulip_softc *));
159 
160 #define	tlp_mchash(addr, sz)						\
161 	(ether_crc32_le((addr), ETHER_ADDR_LEN) & ((sz) - 1))
162 
163 /*
164  * MII bit-bang glue.
165  */
166 u_int32_t tlp_sio_mii_bitbang_read __P((struct device *));
167 void	tlp_sio_mii_bitbang_write __P((struct device *, u_int32_t));
168 
169 const struct mii_bitbang_ops tlp_sio_mii_bitbang_ops = {
170 	tlp_sio_mii_bitbang_read,
171 	tlp_sio_mii_bitbang_write,
172 	{
173 		MIIROM_MDO,		/* MII_BIT_MDO */
174 		MIIROM_MDI,		/* MII_BIT_MDI */
175 		MIIROM_MDC,		/* MII_BIT_MDC */
176 		0,			/* MII_BIT_DIR_HOST_PHY */
177 		MIIROM_MIIDIR,		/* MII_BIT_DIR_PHY_HOST */
178 	}
179 };
180 
181 #ifdef TLP_DEBUG
182 #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
183 				printf x
184 #else
185 #define	DPRINTF(sc, x)	/* nothing */
186 #endif
187 
188 #ifdef TLP_STATS
189 void	tlp_print_stats __P((struct tulip_softc *));
190 #endif
191 
192 /*
193  * Can be used to debug the SROM-related things, including contents.
194  * Initialized so that it's patchable.
195  */
196 int	tlp_srom_debug = 0;
197 
198 /*
199  * tlp_attach:
200  *
201  *	Attach a Tulip interface to the system.
202  */
203 void
204 tlp_attach(sc, enaddr)
205 	struct tulip_softc *sc;
206 	const u_int8_t *enaddr;
207 {
208 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
209 	int i, error;
210 
211 	callout_init(&sc->sc_nway_callout);
212 	callout_init(&sc->sc_tick_callout);
213 
214 	/*
215 	 * NOTE: WE EXPECT THE FRONT-END TO INITIALIZE sc_regshift!
216 	 */
217 
218 	/*
219 	 * Setup the transmit threshold table.
220 	 */
221 	switch (sc->sc_chip) {
222 	case TULIP_CHIP_DE425:
223 	case TULIP_CHIP_21040:
224 	case TULIP_CHIP_21041:
225 		sc->sc_txth = tlp_10_txthresh_tab;
226 		break;
227 
228 	case TULIP_CHIP_DM9102:
229 	case TULIP_CHIP_DM9102A:
230 		sc->sc_txth = tlp_dm9102_txthresh_tab;
231 		break;
232 
233 	default:
234 		sc->sc_txth = tlp_10_100_txthresh_tab;
235 		break;
236 	}
237 
238 	/*
239 	 * Setup the filter setup function.
240 	 */
241 	switch (sc->sc_chip) {
242 	case TULIP_CHIP_WB89C840F:
243 		sc->sc_filter_setup = tlp_winb_filter_setup;
244 		break;
245 
246 	case TULIP_CHIP_AL981:
247 		sc->sc_filter_setup = tlp_al981_filter_setup;
248 		break;
249 
250 	default:
251 		sc->sc_filter_setup = tlp_filter_setup;
252 		break;
253 	}
254 
255 	/*
256 	 * Set up the media status change function.
257 	 */
258 	switch (sc->sc_chip) {
259 	case TULIP_CHIP_WB89C840F:
260 		sc->sc_statchg = tlp_winb_mii_statchg;
261 		break;
262 
263 	case TULIP_CHIP_DM9102:
264 	case TULIP_CHIP_DM9102A:
265 		sc->sc_statchg = tlp_dm9102_mii_statchg;
266 		break;
267 
268 	default:
269 		/*
270 		 * We may override this if we have special media
271 		 * handling requirements (e.g. flipping GPIO pins).
272 		 *
273 		 * The pure-MII statchg function covers the basics.
274 		 */
275 		sc->sc_statchg = tlp_mii_statchg;
276 		break;
277 	}
278 
279 	/*
280 	 * Default to no FS|LS in setup packet descriptors.  They're
281 	 * supposed to be zero according to the 21040 and 21143
282 	 * manuals, and some chips fall over badly if they're
283 	 * included.  Yet, other chips seem to require them.  Sigh.
284 	 */
285 	switch (sc->sc_chip) {
286 	case TULIP_CHIP_X3201_3:
287 		sc->sc_setup_fsls = TDCTL_Tx_FS|TDCTL_Tx_LS;
288 		break;
289 
290 	default:
291 		sc->sc_setup_fsls = 0;
292 	}
293 
294 	/*
295 	 * Set up various chip-specific quirks.
296 	 *
297 	 * Note that wherever we can, we use the "ring" option for
298 	 * transmit and receive descriptors.  This is because some
299 	 * clone chips apparently have problems when using chaining,
300 	 * although some *only* support chaining.
301 	 *
302 	 * What we do is always program the "next" pointer, and then
303 	 * conditionally set the TDCTL_CH and TDCTL_ER bits in the
304 	 * appropriate places.
305 	 */
306 	switch (sc->sc_chip) {
307 	case TULIP_CHIP_21140:
308 	case TULIP_CHIP_21140A:
309 	case TULIP_CHIP_21142:
310 	case TULIP_CHIP_21143:
311 	case TULIP_CHIP_82C115:		/* 21143-like */
312 	case TULIP_CHIP_MX98713:	/* 21140-like */
313 	case TULIP_CHIP_MX98713A:	/* 21143-like */
314 	case TULIP_CHIP_MX98715:	/* 21143-like */
315 	case TULIP_CHIP_MX98715A:	/* 21143-like */
316 	case TULIP_CHIP_MX98725:	/* 21143-like */
317 		/*
318 		 * Run these chips in ring mode.
319 		 */
320 		sc->sc_tdctl_ch = 0;
321 		sc->sc_tdctl_er = TDCTL_ER;
322 		sc->sc_preinit = tlp_2114x_preinit;
323 		break;
324 
325 	case TULIP_CHIP_82C168:
326 	case TULIP_CHIP_82C169:
327 		/*
328 		 * Run these chips in ring mode.
329 		 */
330 		sc->sc_tdctl_ch = 0;
331 		sc->sc_tdctl_er = TDCTL_ER;
332 		sc->sc_preinit = tlp_pnic_preinit;
333 
334 		/*
335 		 * These chips seem to have busted DMA engines; just put them
336 		 * in Store-and-Forward mode from the get-go.
337 		 */
338 		sc->sc_txthresh = TXTH_SF;
339 		break;
340 
341 	case TULIP_CHIP_WB89C840F:
342 		/*
343 		 * Run this chip in chained mode.
344 		 */
345 		sc->sc_tdctl_ch = TDCTL_CH;
346 		sc->sc_tdctl_er = 0;
347 		sc->sc_flags |= TULIPF_IC_FS;
348 		break;
349 
350 	case TULIP_CHIP_DM9102:
351 	case TULIP_CHIP_DM9102A:
352 		/*
353 		 * Run these chips in chained mode.
354 		 */
355 		sc->sc_tdctl_ch = TDCTL_CH;
356 		sc->sc_tdctl_er = 0;
357 		sc->sc_preinit = tlp_dm9102_preinit;
358 
359 		/*
360 		 * These chips have a broken bus interface, so we
361 		 * can't use any optimized bus commands.  For this
362 		 * reason, we tend to underrun pretty quickly, so
363 		 * just to Store-and-Forward mode from the get-go.
364 		 */
365 		sc->sc_txthresh = TXTH_DM9102_SF;
366 		break;
367 
368 	default:
369 		/*
370 		 * Default to running in ring mode.
371 		 */
372 		sc->sc_tdctl_ch = 0;
373 		sc->sc_tdctl_er = TDCTL_ER;
374 	}
375 
376 	/*
377 	 * Set up the MII bit-bang operations.
378 	 */
379 	switch (sc->sc_chip) {
380 	case TULIP_CHIP_WB89C840F:	/* XXX direction bit different? */
381 		sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops;
382 		break;
383 
384 	default:
385 		sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops;
386 	}
387 
388 	SIMPLEQ_INIT(&sc->sc_txfreeq);
389 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
390 
391 	/*
392 	 * Allocate the control data structures, and create and load the
393 	 * DMA map for it.
394 	 */
395 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
396 	    sizeof(struct tulip_control_data), PAGE_SIZE, 0, &sc->sc_cdseg,
397 	    1, &sc->sc_cdnseg, 0)) != 0) {
398 		printf("%s: unable to allocate control data, error = %d\n",
399 		    sc->sc_dev.dv_xname, error);
400 		goto fail_0;
401 	}
402 
403 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg,
404 	    sizeof(struct tulip_control_data), (caddr_t *)&sc->sc_control_data,
405 	    BUS_DMA_COHERENT)) != 0) {
406 		printf("%s: unable to map control data, error = %d\n",
407 		    sc->sc_dev.dv_xname, error);
408 		goto fail_1;
409 	}
410 
411 	if ((error = bus_dmamap_create(sc->sc_dmat,
412 	    sizeof(struct tulip_control_data), 1,
413 	    sizeof(struct tulip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
414 		printf("%s: unable to create control data DMA map, "
415 		    "error = %d\n", sc->sc_dev.dv_xname, error);
416 		goto fail_2;
417 	}
418 
419 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
420 	    sc->sc_control_data, sizeof(struct tulip_control_data), NULL,
421 	    0)) != 0) {
422 		printf("%s: unable to load control data DMA map, error = %d\n",
423 		    sc->sc_dev.dv_xname, error);
424 		goto fail_3;
425 	}
426 
427 	/*
428 	 * Create the transmit buffer DMA maps.
429 	 *
430 	 * Note that on the Xircom clone, transmit buffers must be
431 	 * 4-byte aligned.  We're almost guaranteed to have to copy
432 	 * the packet in that case, so we just limit ourselves to
433 	 * one segment.
434 	 *
435 	 * On the DM9102, the transmit logic can only handle one
436 	 * DMA segment.
437 	 */
438 	switch (sc->sc_chip) {
439 	case TULIP_CHIP_X3201_3:
440 	case TULIP_CHIP_DM9102:
441 	case TULIP_CHIP_DM9102A:
442 		sc->sc_ntxsegs = 1;
443 		break;
444 
445 	default:
446 		sc->sc_ntxsegs = TULIP_NTXSEGS;
447 	}
448 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
449 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
450 		    sc->sc_ntxsegs, MCLBYTES, 0, 0,
451 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
452 			printf("%s: unable to create tx DMA map %d, "
453 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
454 			goto fail_4;
455 		}
456 	}
457 
458 	/*
459 	 * Create the recieve buffer DMA maps.
460 	 */
461 	for (i = 0; i < TULIP_NRXDESC; i++) {
462 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
463 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
464 			printf("%s: unable to create rx DMA map %d, "
465 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
466 			goto fail_5;
467 		}
468 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
469 	}
470 
471 	/*
472 	 * From this point forward, the attachment cannot fail.  A failure
473 	 * before this point releases all resources that may have been
474 	 * allocated.
475 	 */
476 	sc->sc_flags |= TULIPF_ATTACHED;
477 
478 	/*
479 	 * Reset the chip to a known state.
480 	 */
481 	tlp_reset(sc);
482 
483 	/* Announce ourselves. */
484 	printf("%s: %s%sEthernet address %s\n", sc->sc_dev.dv_xname,
485 	    sc->sc_name[0] != '\0' ? sc->sc_name : "",
486 	    sc->sc_name[0] != '\0' ? ", " : "",
487 	    ether_sprintf(enaddr));
488 
489 	/*
490 	 * Initialize our media structures.  This may probe the MII, if
491 	 * present.
492 	 */
493 	(*sc->sc_mediasw->tmsw_init)(sc);
494 
495 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
496 	ifp->if_softc = sc;
497 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
498 	ifp->if_ioctl = tlp_ioctl;
499 	ifp->if_start = tlp_start;
500 	ifp->if_watchdog = tlp_watchdog;
501 
502 	/*
503 	 * Attach the interface.
504 	 */
505 	if_attach(ifp);
506 	ether_ifattach(ifp, enaddr);
507 #if NBPFILTER > 0
508 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
509 	    sizeof(struct ether_header));
510 #endif
511 
512 	/*
513 	 * Make sure the interface is shutdown during reboot.
514 	 */
515 	sc->sc_sdhook = shutdownhook_establish(tlp_shutdown, sc);
516 	if (sc->sc_sdhook == NULL)
517 		printf("%s: WARNING: unable to establish shutdown hook\n",
518 		    sc->sc_dev.dv_xname);
519 
520 	/*
521 	 * Add a suspend hook to make sure we come back up after a
522 	 * resume.
523 	 */
524 	sc->sc_powerhook = powerhook_establish(tlp_power, sc);
525 	if (sc->sc_powerhook == NULL)
526 		printf("%s: WARNING: unable to establish power hook\n",
527 		    sc->sc_dev.dv_xname);
528 	return;
529 
530 	/*
531 	 * Free any resources we've allocated during the failed attach
532 	 * attempt.  Do this in reverse order and fall through.
533 	 */
534  fail_5:
535 	for (i = 0; i < TULIP_NRXDESC; i++) {
536 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
537 			bus_dmamap_destroy(sc->sc_dmat,
538 			    sc->sc_rxsoft[i].rxs_dmamap);
539 	}
540  fail_4:
541 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
542 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
543 			bus_dmamap_destroy(sc->sc_dmat,
544 			    sc->sc_txsoft[i].txs_dmamap);
545 	}
546 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
547  fail_3:
548 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
549  fail_2:
550 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
551 	    sizeof(struct tulip_control_data));
552  fail_1:
553 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
554  fail_0:
555 	return;
556 }
557 
558 /*
559  * tlp_activate:
560  *
561  *	Handle device activation/deactivation requests.
562  */
563 int
564 tlp_activate(self, act)
565 	struct device *self;
566 	enum devact act;
567 {
568 	struct tulip_softc *sc = (void *) self;
569 	int s, error = 0;
570 
571 	s = splnet();
572 	switch (act) {
573 	case DVACT_ACTIVATE:
574 		error = EOPNOTSUPP;
575 		break;
576 
577 	case DVACT_DEACTIVATE:
578 		if (sc->sc_flags & TULIPF_HAS_MII)
579 			mii_activate(&sc->sc_mii, act, MII_PHY_ANY,
580 			    MII_OFFSET_ANY);
581 		if_deactivate(&sc->sc_ethercom.ec_if);
582 		break;
583 	}
584 	splx(s);
585 
586 	return (error);
587 }
588 
589 /*
590  * tlp_detach:
591  *
592  *	Detach a Tulip interface.
593  */
594 int
595 tlp_detach(sc)
596 	struct tulip_softc *sc;
597 {
598 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
599 	struct tulip_rxsoft *rxs;
600 	struct tulip_txsoft *txs;
601 	int i;
602 
603 	/*
604 	 * Suceed now if there isn't any work to do.
605 	 */
606 	if ((sc->sc_flags & TULIPF_ATTACHED) == 0)
607 		return (0);
608 
609 	/* Unhook our tick handler. */
610 	if (sc->sc_tick)
611 		callout_stop(&sc->sc_tick_callout);
612 
613 	if (sc->sc_flags & TULIPF_HAS_MII) {
614 		/* Detach all PHYs */
615 		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
616 	}
617 
618 	/* Delete all remaining media. */
619 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
620 
621 #if NBPFILTER > 0
622 	bpfdetach(ifp);
623 #endif
624 	ether_ifdetach(ifp);
625 	if_detach(ifp);
626 
627 	for (i = 0; i < TULIP_NRXDESC; i++) {
628 		rxs = &sc->sc_rxsoft[i];
629 		if (rxs->rxs_mbuf != NULL) {
630 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
631 			m_freem(rxs->rxs_mbuf);
632 			rxs->rxs_mbuf = NULL;
633 		}
634 		bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap);
635 	}
636 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
637 		txs = &sc->sc_txsoft[i];
638 		if (txs->txs_mbuf != NULL) {
639 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
640 			m_freem(txs->txs_mbuf);
641 			txs->txs_mbuf = NULL;
642 		}
643 		bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap);
644 	}
645 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
646 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
647 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
648 	    sizeof(struct tulip_control_data));
649 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
650 
651 	shutdownhook_disestablish(sc->sc_sdhook);
652 	powerhook_disestablish(sc->sc_powerhook);
653 
654 	if (sc->sc_srom)
655 		free(sc->sc_srom, M_DEVBUF);
656 
657 	return (0);
658 }
659 
660 /*
661  * tlp_shutdown:
662  *
663  *	Make sure the interface is stopped at reboot time.
664  */
665 void
666 tlp_shutdown(arg)
667 	void *arg;
668 {
669 	struct tulip_softc *sc = arg;
670 
671 	tlp_stop(sc, 1);
672 }
673 
674 /*
675  * tlp_start:		[ifnet interface function]
676  *
677  *	Start packet transmission on the interface.
678  */
679 void
680 tlp_start(ifp)
681 	struct ifnet *ifp;
682 {
683 	struct tulip_softc *sc = ifp->if_softc;
684 	struct mbuf *m0, *m;
685 	struct tulip_txsoft *txs, *last_txs;
686 	bus_dmamap_t dmamap;
687 	int error, firsttx, nexttx, lasttx, ofree, seg;
688 
689 	DPRINTF(sc, ("%s: tlp_start: sc_flags 0x%08x, if_flags 0x%08x\n",
690 	    sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
691 
692 	/*
693 	 * If we want a filter setup, it means no more descriptors were
694 	 * available for the setup routine.  Let it get a chance to wedge
695 	 * itself into the ring.
696 	 */
697 	if (sc->sc_flags & TULIPF_WANT_SETUP)
698 		ifp->if_flags |= IFF_OACTIVE;
699 
700 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
701 		return;
702 
703 	/*
704 	 * Remember the previous number of free descriptors and
705 	 * the first descriptor we'll use.
706 	 */
707 	ofree = sc->sc_txfree;
708 	firsttx = sc->sc_txnext;
709 
710 	DPRINTF(sc, ("%s: tlp_start: txfree %d, txnext %d\n",
711 	    sc->sc_dev.dv_xname, ofree, firsttx));
712 
713 	/*
714 	 * Loop through the send queue, setting up transmit descriptors
715 	 * until we drain the queue, or use up all available transmit
716 	 * descriptors.
717 	 */
718 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
719 	       sc->sc_txfree != 0) {
720 		/*
721 		 * Grab a packet off the queue.
722 		 */
723 		IF_DEQUEUE(&ifp->if_snd, m0);
724 		if (m0 == NULL)
725 			break;
726 
727 		dmamap = txs->txs_dmamap;
728 
729 		/*
730 		 * Load the DMA map.  If this fails, the packet either
731 		 * didn't fit in the alloted number of segments, or we were
732 		 * short on resources.  In this case, we'll copy and try
733 		 * again.
734 		 *
735 		 * Note that if we're only allowed 1 Tx segment, we
736 		 * have an alignment restriction.  Do this test before
737 		 * attempting to load the DMA map, because it's more
738 		 * likely we'll trip the alignment test than the
739 		 * more-than-one-segment test.
740 		 */
741 		if ((sc->sc_ntxsegs == 1 && (mtod(m0, bus_addr_t) & 3) != 0) ||
742 		    bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
743 		      BUS_DMA_NOWAIT) != 0) {
744 			MGETHDR(m, M_DONTWAIT, MT_DATA);
745 			if (m == NULL) {
746 				printf("%s: unable to allocate Tx mbuf\n",
747 				    sc->sc_dev.dv_xname);
748 				IF_PREPEND(&ifp->if_snd, m0);
749 				break;
750 			}
751 			if (m0->m_pkthdr.len > MHLEN) {
752 				MCLGET(m, M_DONTWAIT);
753 				if ((m->m_flags & M_EXT) == 0) {
754 					printf("%s: unable to allocate Tx "
755 					    "cluster\n", sc->sc_dev.dv_xname);
756 					m_freem(m);
757 					IF_PREPEND(&ifp->if_snd, m0);
758 					break;
759 				}
760 			}
761 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
762 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
763 			m_freem(m0);
764 			m0 = m;
765 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
766 			    m0, BUS_DMA_NOWAIT);
767 			if (error) {
768 				printf("%s: unable to load Tx buffer, "
769 				    "error = %d\n", sc->sc_dev.dv_xname, error);
770 				IF_PREPEND(&ifp->if_snd, m0);
771 				break;
772 			}
773 		}
774 
775 		/*
776 		 * Ensure we have enough descriptors free to describe
777 		 * the packet.
778 		 */
779 		if (dmamap->dm_nsegs > sc->sc_txfree) {
780 			/*
781 			 * Not enough free descriptors to transmit this
782 			 * packet.  We haven't committed to anything yet,
783 			 * so just unload the DMA map, put the packet
784 			 * back on the queue, and punt.  Notify the upper
785 			 * layer that there are no more slots left.
786 			 *
787 			 * XXX We could allocate an mbuf and copy, but
788 			 * XXX it is worth it?
789 			 */
790 			ifp->if_flags |= IFF_OACTIVE;
791 			bus_dmamap_unload(sc->sc_dmat, dmamap);
792 			IF_PREPEND(&ifp->if_snd, m0);
793 			break;
794 		}
795 
796 		/*
797 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
798 		 */
799 
800 		/* Sync the DMA map. */
801 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
802 		    BUS_DMASYNC_PREWRITE);
803 
804 		/*
805 		 * Initialize the transmit descriptors.
806 		 */
807 		for (nexttx = sc->sc_txnext, seg = 0;
808 		     seg < dmamap->dm_nsegs;
809 		     seg++, nexttx = TULIP_NEXTTX(nexttx)) {
810 			/*
811 			 * If this is the first descriptor we're
812 			 * enqueueing, don't set the OWN bit just
813 			 * yet.  That could cause a race condition.
814 			 * We'll do it below.
815 			 */
816 			sc->sc_txdescs[nexttx].td_status =
817 			    (nexttx == firsttx) ? 0 : htole32(TDSTAT_OWN);
818 			sc->sc_txdescs[nexttx].td_bufaddr1 =
819 			    htole32(dmamap->dm_segs[seg].ds_addr);
820 			sc->sc_txdescs[nexttx].td_ctl =
821 			    htole32((dmamap->dm_segs[seg].ds_len <<
822 			        TDCTL_SIZE1_SHIFT) | sc->sc_tdctl_ch |
823 				(nexttx == (TULIP_NTXDESC - 1) ?
824 				 sc->sc_tdctl_er : 0));
825 			lasttx = nexttx;
826 		}
827 
828 		/* Set `first segment' and `last segment' appropriately. */
829 		sc->sc_txdescs[sc->sc_txnext].td_ctl |= htole32(TDCTL_Tx_FS);
830 		sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_LS);
831 
832 #ifdef TLP_DEBUG
833 		if (ifp->if_flags & IFF_DEBUG) {
834 			printf("     txsoft %p transmit chain:\n", txs);
835 			for (seg = sc->sc_txnext;; seg = TULIP_NEXTTX(seg)) {
836 				printf("     descriptor %d:\n", seg);
837 				printf("       td_status:   0x%08x\n",
838 				    le32toh(sc->sc_txdescs[seg].td_status));
839 				printf("       td_ctl:      0x%08x\n",
840 				    le32toh(sc->sc_txdescs[seg].td_ctl));
841 				printf("       td_bufaddr1: 0x%08x\n",
842 				    le32toh(sc->sc_txdescs[seg].td_bufaddr1));
843 				printf("       td_bufaddr2: 0x%08x\n",
844 				    le32toh(sc->sc_txdescs[seg].td_bufaddr2));
845 				if (seg == lasttx)
846 					break;
847 			}
848 		}
849 #endif
850 
851 		/* Sync the descriptors we're using. */
852 		TULIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
853 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
854 
855 		/*
856 		 * Store a pointer to the packet so we can free it later,
857 		 * and remember what txdirty will be once the packet is
858 		 * done.
859 		 */
860 		txs->txs_mbuf = m0;
861 		txs->txs_firstdesc = sc->sc_txnext;
862 		txs->txs_lastdesc = lasttx;
863 		txs->txs_ndescs = dmamap->dm_nsegs;
864 
865 		/* Advance the tx pointer. */
866 		sc->sc_txfree -= dmamap->dm_nsegs;
867 		sc->sc_txnext = nexttx;
868 
869 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
870 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
871 
872 		last_txs = txs;
873 
874 #if NBPFILTER > 0
875 		/*
876 		 * Pass the packet to any BPF listeners.
877 		 */
878 		if (ifp->if_bpf)
879 			bpf_mtap(ifp->if_bpf, m0);
880 #endif /* NBPFILTER > 0 */
881 	}
882 
883 	if (txs == NULL || sc->sc_txfree == 0) {
884 		/* No more slots left; notify upper layer. */
885 		ifp->if_flags |= IFF_OACTIVE;
886 	}
887 
888 	if (sc->sc_txfree != ofree) {
889 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
890 		    sc->sc_dev.dv_xname, lasttx, firsttx));
891 		/*
892 		 * Cause a transmit interrupt to happen on the
893 		 * last packet we enqueued.
894 		 */
895 		sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_IC);
896 		TULIP_CDTXSYNC(sc, lasttx, 1,
897 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
898 
899 		/*
900 		 * Some clone chips want IC on the *first* segment in
901 		 * the packet.  Appease them.
902 		 */
903 		if ((sc->sc_flags & TULIPF_IC_FS) != 0 &&
904 		    last_txs->txs_firstdesc != lasttx) {
905 			sc->sc_txdescs[last_txs->txs_firstdesc].td_ctl |=
906 			    htole32(TDCTL_Tx_IC);
907 			TULIP_CDTXSYNC(sc, last_txs->txs_firstdesc, 1,
908 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
909 		}
910 
911 		/*
912 		 * The entire packet chain is set up.  Give the
913 		 * first descriptor to the chip now.
914 		 */
915 		sc->sc_txdescs[firsttx].td_status |= htole32(TDSTAT_OWN);
916 		TULIP_CDTXSYNC(sc, firsttx, 1,
917 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
918 
919 		/* Wake up the transmitter. */
920 		/* XXX USE AUTOPOLLING? */
921 		TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
922 
923 		/* Set a watchdog timer in case the chip flakes out. */
924 		ifp->if_timer = 5;
925 	}
926 }
927 
928 /*
929  * tlp_watchdog:	[ifnet interface function]
930  *
931  *	Watchdog timer handler.
932  */
933 void
934 tlp_watchdog(ifp)
935 	struct ifnet *ifp;
936 {
937 	struct tulip_softc *sc = ifp->if_softc;
938 	int doing_setup, doing_transmit;
939 
940 	doing_setup = (sc->sc_flags & TULIPF_DOING_SETUP);
941 	doing_transmit = (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL);
942 
943 	if (doing_setup && doing_transmit) {
944 		printf("%s: filter setup and transmit timeout\n",
945 		    sc->sc_dev.dv_xname);
946 		ifp->if_oerrors++;
947 	} else if (doing_transmit) {
948 		printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
949 		ifp->if_oerrors++;
950 	} else if (doing_setup)
951 		printf("%s: filter setup timeout\n", sc->sc_dev.dv_xname);
952 	else
953 		printf("%s: spurious watchdog timeout\n", sc->sc_dev.dv_xname);
954 
955 	(void) tlp_init(sc);
956 
957 	/* Try to get more packets going. */
958 	tlp_start(ifp);
959 }
960 
961 /*
962  * tlp_ioctl:		[ifnet interface function]
963  *
964  *	Handle control requests from the operator.
965  */
966 int
967 tlp_ioctl(ifp, cmd, data)
968 	struct ifnet *ifp;
969 	u_long cmd;
970 	caddr_t data;
971 {
972 	struct tulip_softc *sc = ifp->if_softc;
973 	struct ifreq *ifr = (struct ifreq *)data;
974 	struct ifaddr *ifa = (struct ifaddr *)data;
975 	int s, error = 0;
976 
977 	s = splnet();
978 
979 	switch (cmd) {
980 	case SIOCSIFADDR:
981 		if ((error = tlp_enable(sc)) != 0)
982 			break;
983 		ifp->if_flags |= IFF_UP;
984 
985 		switch (ifa->ifa_addr->sa_family) {
986 #ifdef INET
987 		case AF_INET:
988 			if ((error = tlp_init(sc)) != 0)
989 				break;
990 			arp_ifinit(ifp, ifa);
991 			break;
992 #endif /* INET */
993 #ifdef NS
994 		case AF_NS:
995 		    {
996 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
997 
998 			if (ns_nullhost(*ina))
999 				ina->x_host = *(union ns_host *)
1000 				    LLADDR(ifp->if_sadl);
1001 			else
1002 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
1003 				    ifp->if_addrlen);
1004 			/* Set new address. */
1005 			error = tlp_init(sc);
1006 			break;
1007 		    }
1008 #endif /* NS */
1009 		default:
1010 			error = tlp_init(sc);
1011 			break;
1012 		}
1013 		break;
1014 
1015 	case SIOCSIFMTU:
1016 		if (ifr->ifr_mtu > ETHERMTU)
1017 			error = EINVAL;
1018 		else
1019 			ifp->if_mtu = ifr->ifr_mtu;
1020 		break;
1021 
1022 	case SIOCSIFFLAGS:
1023 #ifdef TLP_STATS
1024 		if (ifp->if_flags & IFF_DEBUG)
1025 			tlp_print_stats(sc);
1026 #endif
1027 		if ((ifp->if_flags & IFF_UP) == 0 &&
1028 		    (ifp->if_flags & IFF_RUNNING) != 0) {
1029 			/*
1030 			 * If interface is marked down and it is running, then
1031 			 * stop it.
1032 			 */
1033 			tlp_stop(sc, 1);
1034 			tlp_disable(sc);
1035 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
1036 			   (ifp->if_flags & IFF_RUNNING) == 0) {
1037 			/*
1038 			 * If interfase it marked up and it is stopped, then
1039 			 * start it.
1040 			 */
1041 			if ((error = tlp_enable(sc)) != 0)
1042 				break;
1043 			error = tlp_init(sc);
1044 		} else if ((ifp->if_flags & IFF_UP) != 0) {
1045 			/*
1046 			 * Reset the interface to pick up changes in any other
1047 			 * flags that affect the hardware state.
1048 			 */
1049 			if ((error = tlp_enable(sc)) != 0)
1050 				break;
1051 			error = tlp_init(sc);
1052 		}
1053 		break;
1054 
1055 	case SIOCADDMULTI:
1056 	case SIOCDELMULTI:
1057 		error = (cmd == SIOCADDMULTI) ?
1058 		    ether_addmulti(ifr, &sc->sc_ethercom) :
1059 		    ether_delmulti(ifr, &sc->sc_ethercom);
1060 
1061 		if (TULIP_IS_ENABLED(sc) && error == ENETRESET) {
1062 			/*
1063 			 * Multicast list has changed.  Set the filter
1064 			 * accordingly.
1065 			 */
1066 			(*sc->sc_filter_setup)(sc);
1067 			error = 0;
1068 		}
1069 		break;
1070 
1071 	case SIOCSIFMEDIA:
1072 	case SIOCGIFMEDIA:
1073 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1074 		break;
1075 
1076 	default:
1077 		error = EINVAL;
1078 		break;
1079 	}
1080 
1081 	/* Try to get more packets going. */
1082 	if (TULIP_IS_ENABLED(sc))
1083 		tlp_start(ifp);
1084 
1085 	splx(s);
1086 	return (error);
1087 }
1088 
1089 /*
1090  * tlp_intr:
1091  *
1092  *	Interrupt service routine.
1093  */
1094 int
1095 tlp_intr(arg)
1096 	void *arg;
1097 {
1098 	struct tulip_softc *sc = arg;
1099 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1100 	u_int32_t status, rxstatus, txstatus;
1101 	int handled = 0, txthresh;
1102 
1103 	DPRINTF(sc, ("%s: tlp_intr\n", sc->sc_dev.dv_xname));
1104 
1105 #ifdef DEBUG
1106 	if (TULIP_IS_ENABLED(sc) == 0)
1107 		panic("%s: tlp_intr: not enabled\n", sc->sc_dev.dv_xname);
1108 #endif
1109 
1110 	/*
1111 	 * If the interface isn't running, the interrupt couldn't
1112 	 * possibly have come from us.
1113 	 */
1114 	if ((ifp->if_flags & IFF_RUNNING) == 0 ||
1115 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1116 		return (0);
1117 
1118 	/* Disable interrupts on the DM9102 (interrupt edge bug). */
1119 	switch (sc->sc_chip) {
1120 	case TULIP_CHIP_DM9102:
1121 	case TULIP_CHIP_DM9102A:
1122 		TULIP_WRITE(sc, CSR_INTEN, 0);
1123 		break;
1124 
1125 	default:
1126 		/* Nothing. */
1127 	}
1128 
1129 	for (;;) {
1130 		status = TULIP_READ(sc, CSR_STATUS);
1131 		if (status)
1132 			TULIP_WRITE(sc, CSR_STATUS, status);
1133 
1134 		if ((status & sc->sc_inten) == 0)
1135 			break;
1136 
1137 		handled = 1;
1138 
1139 		rxstatus = status & sc->sc_rxint_mask;
1140 		txstatus = status & sc->sc_txint_mask;
1141 
1142 		if (rxstatus) {
1143 			/* Grab new any new packets. */
1144 			tlp_rxintr(sc);
1145 
1146 			if (rxstatus & STATUS_RWT)
1147 				printf("%s: receive watchdog timeout\n",
1148 				    sc->sc_dev.dv_xname);
1149 
1150 			if (rxstatus & STATUS_RU) {
1151 				printf("%s: receive ring overrun\n",
1152 				    sc->sc_dev.dv_xname);
1153 				/* Get the receive process going again. */
1154 				if (sc->sc_tdctl_er != TDCTL_ER) {
1155 					tlp_idle(sc, OPMODE_SR);
1156 					TULIP_WRITE(sc, CSR_RXLIST,
1157 					    TULIP_CDRXADDR(sc, sc->sc_rxptr));
1158 					TULIP_WRITE(sc, CSR_OPMODE,
1159 					    sc->sc_opmode);
1160 				}
1161 				TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
1162 				break;
1163 			}
1164 		}
1165 
1166 		if (txstatus) {
1167 			/* Sweep up transmit descriptors. */
1168 			tlp_txintr(sc);
1169 
1170 			if (txstatus & STATUS_TJT)
1171 				printf("%s: transmit jabber timeout\n",
1172 				    sc->sc_dev.dv_xname);
1173 
1174 			if (txstatus & STATUS_UNF) {
1175 				/*
1176 				 * Increase our transmit threshold if
1177 				 * another is available.
1178 				 */
1179 				txthresh = sc->sc_txthresh + 1;
1180 				if (sc->sc_txth[txthresh].txth_name != NULL) {
1181 					/* Idle the transmit process. */
1182 					tlp_idle(sc, OPMODE_ST);
1183 
1184 					sc->sc_txthresh = txthresh;
1185 					sc->sc_opmode &= ~(OPMODE_TR|OPMODE_SF);
1186 					sc->sc_opmode |=
1187 					    sc->sc_txth[txthresh].txth_opmode;
1188 					printf("%s: transmit underrun; new "
1189 					    "threshold: %s\n",
1190 					    sc->sc_dev.dv_xname,
1191 					    sc->sc_txth[txthresh].txth_name);
1192 
1193 					/*
1194 					 * Set the new threshold and restart
1195 					 * the transmit process.
1196 					 */
1197 					TULIP_WRITE(sc, CSR_OPMODE,
1198 					    sc->sc_opmode);
1199 				}
1200 					/*
1201 					 * XXX Log every Nth underrun from
1202 					 * XXX now on?
1203 					 */
1204 			}
1205 		}
1206 
1207 		if (status & (STATUS_TPS|STATUS_RPS)) {
1208 			if (status & STATUS_TPS)
1209 				printf("%s: transmit process stopped\n",
1210 				    sc->sc_dev.dv_xname);
1211 			if (status & STATUS_RPS)
1212 				printf("%s: receive process stopped\n",
1213 				    sc->sc_dev.dv_xname);
1214 			(void) tlp_init(sc);
1215 			break;
1216 		}
1217 
1218 		if (status & STATUS_SE) {
1219 			const char *str;
1220 			switch (status & STATUS_EB) {
1221 			case STATUS_EB_PARITY:
1222 				str = "parity error";
1223 				break;
1224 
1225 			case STATUS_EB_MABT:
1226 				str = "master abort";
1227 				break;
1228 
1229 			case STATUS_EB_TABT:
1230 				str = "target abort";
1231 				break;
1232 
1233 			default:
1234 				str = "unknown error";
1235 				break;
1236 			}
1237 			printf("%s: fatal system error: %s\n",
1238 			    sc->sc_dev.dv_xname, str);
1239 			(void) tlp_init(sc);
1240 			break;
1241 		}
1242 
1243 		/*
1244 		 * Not handled:
1245 		 *
1246 		 *	Transmit buffer unavailable -- normal
1247 		 *	condition, nothing to do, really.
1248 		 *
1249 		 *	General purpose timer experied -- we don't
1250 		 *	use the general purpose timer.
1251 		 *
1252 		 *	Early receive interrupt -- not available on
1253 		 *	all chips, we just use RI.  We also only
1254 		 *	use single-segment receive DMA, so this
1255 		 *	is mostly useless.
1256 		 */
1257 	}
1258 
1259 	/* Bring interrupts back up on the DM9102. */
1260 	switch (sc->sc_chip) {
1261 	case TULIP_CHIP_DM9102:
1262 	case TULIP_CHIP_DM9102A:
1263 		TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
1264 		break;
1265 
1266 	default:
1267 		/* Nothing. */
1268 	}
1269 
1270 	/* Try to get more packets going. */
1271 	tlp_start(ifp);
1272 
1273 	return (handled);
1274 }
1275 
1276 /*
1277  * tlp_rxintr:
1278  *
1279  *	Helper; handle receive interrupts.
1280  */
1281 void
1282 tlp_rxintr(sc)
1283 	struct tulip_softc *sc;
1284 {
1285 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1286 	struct ether_header *eh;
1287 	struct tulip_rxsoft *rxs;
1288 	struct mbuf *m;
1289 	u_int32_t rxstat;
1290 	int i, len;
1291 
1292 	for (i = sc->sc_rxptr;; i = TULIP_NEXTRX(i)) {
1293 		rxs = &sc->sc_rxsoft[i];
1294 
1295 		TULIP_CDRXSYNC(sc, i,
1296 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1297 
1298 		rxstat = le32toh(sc->sc_rxdescs[i].td_status);
1299 
1300 		if (rxstat & TDSTAT_OWN) {
1301 			/*
1302 			 * We have processed all of the receive buffers.
1303 			 */
1304 			break;
1305 		}
1306 
1307 		/*
1308 		 * Make sure the packet fit in one buffer.  This should
1309 		 * always be the case.  But the Lite-On PNIC, rev 33
1310 		 * has an awful receive engine bug, which may require
1311 		 * a very icky work-around.
1312 		 */
1313 		if ((rxstat & (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) !=
1314 		    (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) {
1315 			printf("%s: incoming packet spilled, resetting\n",
1316 			    sc->sc_dev.dv_xname);
1317 			(void) tlp_init(sc);
1318 			return;
1319 		}
1320 
1321 		/*
1322 		 * If any collisions were seen on the wire, count one.
1323 		 */
1324 		if (rxstat & TDSTAT_Rx_CS)
1325 			ifp->if_collisions++;
1326 
1327 		/*
1328 		 * If an error occured, update stats, clear the status
1329 		 * word, and leave the packet buffer in place.  It will
1330 		 * simply be reused the next time the ring comes around.
1331 		 */
1332 		if (rxstat & TDSTAT_ES) {
1333 #define	PRINTERR(bit, str)						\
1334 			if (rxstat & (bit))				\
1335 				printf("%s: receive error: %s\n",	\
1336 				    sc->sc_dev.dv_xname, str)
1337 			ifp->if_ierrors++;
1338 			PRINTERR(TDSTAT_Rx_DE, "descriptor error");
1339 			PRINTERR(TDSTAT_Rx_RF, "runt frame");
1340 			PRINTERR(TDSTAT_Rx_TL, "frame too long");
1341 			PRINTERR(TDSTAT_Rx_RE, "MII error");
1342 			PRINTERR(TDSTAT_Rx_DB, "dribbling bit");
1343 			PRINTERR(TDSTAT_Rx_CE, "CRC error");
1344 #undef PRINTERR
1345 			TULIP_INIT_RXDESC(sc, i);
1346 			continue;
1347 		}
1348 
1349 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1350 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1351 
1352 		/*
1353 		 * No errors; receive the packet.  Note the Tulip
1354 		 * includes the CRC with every packet; trim it.
1355 		 */
1356 		len = TDSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;
1357 
1358 #ifdef __NO_STRICT_ALIGNMENT
1359 		/*
1360 		 * Allocate a new mbuf cluster.  If that fails, we are
1361 		 * out of memory, and must drop the packet and recycle
1362 		 * the buffer that's already attached to this descriptor.
1363 		 */
1364 		m = rxs->rxs_mbuf;
1365 		if (tlp_add_rxbuf(sc, i) != 0) {
1366 			ifp->if_ierrors++;
1367 			TULIP_INIT_RXDESC(sc, i);
1368 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1369 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1370 			continue;
1371 		}
1372 #else
1373 		/*
1374 		 * The Tulip's receive buffers must be 4-byte aligned.
1375 		 * But this means that the data after the Ethernet header
1376 		 * is misaligned.  We must allocate a new buffer and
1377 		 * copy the data, shifted forward 2 bytes.
1378 		 */
1379 		MGETHDR(m, M_DONTWAIT, MT_DATA);
1380 		if (m == NULL) {
1381  dropit:
1382 			ifp->if_ierrors++;
1383 			TULIP_INIT_RXDESC(sc, i);
1384 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1385 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1386 			continue;
1387 		}
1388 		if (len > (MHLEN - 2)) {
1389 			MCLGET(m, M_DONTWAIT);
1390 			if ((m->m_flags & M_EXT) == 0) {
1391 				m_freem(m);
1392 				goto dropit;
1393 			}
1394 		}
1395 		m->m_data += 2;
1396 
1397 		/*
1398 		 * Note that we use clusters for incoming frames, so the
1399 		 * buffer is virtually contiguous.
1400 		 */
1401 		memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
1402 
1403 		/* Allow the receive descriptor to continue using its mbuf. */
1404 		TULIP_INIT_RXDESC(sc, i);
1405 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1406 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1407 #endif /* __NO_STRICT_ALIGNMENT */
1408 
1409 		ifp->if_ipackets++;
1410 		eh = mtod(m, struct ether_header *);
1411 		m->m_pkthdr.rcvif = ifp;
1412 		m->m_pkthdr.len = m->m_len = len;
1413 
1414 #if NBPFILTER > 0
1415 		/*
1416 		 * Pass this up to any BPF listeners, but only
1417 		 * pass it up the stack if its for us.
1418 		 */
1419 		if (ifp->if_bpf)
1420 			bpf_mtap(ifp->if_bpf, m);
1421 #endif /* NPBFILTER > 0 */
1422 
1423 		/*
1424 		 * This test is outside the NBPFILTER block because
1425 		 * on the 21140 we have to use Hash-Only mode due to
1426 		 * a bug in the filter logic.
1427 		 */
1428 		if ((ifp->if_flags & IFF_PROMISC) != 0 ||
1429 		    sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
1430 			if (memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
1431 				 ETHER_ADDR_LEN) != 0 &&
1432 			    ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
1433 				m_freem(m);
1434 				continue;
1435 			}
1436 		}
1437 
1438 		/* Pass it on. */
1439 		(*ifp->if_input)(ifp, m);
1440 	}
1441 
1442 	/* Update the recieve pointer. */
1443 	sc->sc_rxptr = i;
1444 }
1445 
1446 /*
1447  * tlp_txintr:
1448  *
1449  *	Helper; handle transmit interrupts.
1450  */
1451 void
1452 tlp_txintr(sc)
1453 	struct tulip_softc *sc;
1454 {
1455 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1456 	struct tulip_txsoft *txs;
1457 	u_int32_t txstat;
1458 
1459 	DPRINTF(sc, ("%s: tlp_txintr: sc_flags 0x%08x\n",
1460 	    sc->sc_dev.dv_xname, sc->sc_flags));
1461 
1462 	ifp->if_flags &= ~IFF_OACTIVE;
1463 
1464 	/*
1465 	 * Go through our Tx list and free mbufs for those
1466 	 * frames that have been transmitted.
1467 	 */
1468 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1469 		TULIP_CDTXSYNC(sc, txs->txs_lastdesc,
1470 		    txs->txs_ndescs,
1471 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1472 
1473 #ifdef TLP_DEBUG
1474 		if (ifp->if_flags & IFF_DEBUG) {
1475 			int i;
1476 			printf("    txsoft %p transmit chain:\n", txs);
1477 			for (i = txs->txs_firstdesc;; i = TULIP_NEXTTX(i)) {
1478 				printf("     descriptor %d:\n", i);
1479 				printf("       td_status:   0x%08x\n",
1480 				    le32toh(sc->sc_txdescs[i].td_status));
1481 				printf("       td_ctl:      0x%08x\n",
1482 				    le32toh(sc->sc_txdescs[i].td_ctl));
1483 				printf("       td_bufaddr1: 0x%08x\n",
1484 				    le32toh(sc->sc_txdescs[i].td_bufaddr1));
1485 				printf("       td_bufaddr2: 0x%08x\n",
1486 				    le32toh(sc->sc_txdescs[i].td_bufaddr2));
1487 				if (i == txs->txs_lastdesc)
1488 					break;
1489 			}
1490 		}
1491 #endif
1492 
1493 		txstat = le32toh(sc->sc_txdescs[txs->txs_lastdesc].td_status);
1494 		if (txstat & TDSTAT_OWN)
1495 			break;
1496 
1497 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1498 
1499 		sc->sc_txfree += txs->txs_ndescs;
1500 
1501 		if (txs->txs_mbuf == NULL) {
1502 			/*
1503 			 * If we didn't have an mbuf, it was the setup
1504 			 * packet.
1505 			 */
1506 #ifdef DIAGNOSTIC
1507 			if ((sc->sc_flags & TULIPF_DOING_SETUP) == 0)
1508 				panic("tlp_txintr: null mbuf, not doing setup");
1509 #endif
1510 			TULIP_CDSPSYNC(sc, BUS_DMASYNC_POSTWRITE);
1511 			sc->sc_flags &= ~TULIPF_DOING_SETUP;
1512 			SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1513 			continue;
1514 		}
1515 
1516 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1517 		    0, txs->txs_dmamap->dm_mapsize,
1518 		    BUS_DMASYNC_POSTWRITE);
1519 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1520 		m_freem(txs->txs_mbuf);
1521 		txs->txs_mbuf = NULL;
1522 
1523 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1524 
1525 		/*
1526 		 * Check for errors and collisions.
1527 		 */
1528 #ifdef TLP_STATS
1529 		if (txstat & TDSTAT_Tx_UF)
1530 			sc->sc_stats.ts_tx_uf++;
1531 		if (txstat & TDSTAT_Tx_TO)
1532 			sc->sc_stats.ts_tx_to++;
1533 		if (txstat & TDSTAT_Tx_EC)
1534 			sc->sc_stats.ts_tx_ec++;
1535 		if (txstat & TDSTAT_Tx_LC)
1536 			sc->sc_stats.ts_tx_lc++;
1537 #endif
1538 
1539 		if (txstat & (TDSTAT_Tx_UF|TDSTAT_Tx_TO))
1540 			ifp->if_oerrors++;
1541 
1542 		if (txstat & TDSTAT_Tx_EC)
1543 			ifp->if_collisions += 16;
1544 		else
1545 			ifp->if_collisions += TDSTAT_Tx_COLLISIONS(txstat);
1546 		if (txstat & TDSTAT_Tx_LC)
1547 			ifp->if_collisions++;
1548 
1549 		ifp->if_opackets++;
1550 	}
1551 
1552 	/*
1553 	 * If there are no more pending transmissions, cancel the watchdog
1554 	 * timer.
1555 	 */
1556 	if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0)
1557 		ifp->if_timer = 0;
1558 
1559 	/*
1560 	 * If we have a receive filter setup pending, do it now.
1561 	 */
1562 	if (sc->sc_flags & TULIPF_WANT_SETUP)
1563 		(*sc->sc_filter_setup)(sc);
1564 }
1565 
1566 #ifdef TLP_STATS
1567 void
1568 tlp_print_stats(sc)
1569 	struct tulip_softc *sc;
1570 {
1571 
1572 	printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n",
1573 	    sc->sc_dev.dv_xname,
1574 	    sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to,
1575 	    sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc);
1576 }
1577 #endif
1578 
1579 /*
1580  * tlp_reset:
1581  *
1582  *	Perform a soft reset on the Tulip.
1583  */
1584 void
1585 tlp_reset(sc)
1586 	struct tulip_softc *sc;
1587 {
1588 	int i;
1589 
1590 	TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
1591 
1592 	/*
1593 	 * Xircom clone doesn't bring itself out of reset automatically.
1594 	 * Instead, we have to wait at least 50 PCI cycles, and then
1595 	 * clear SWR.
1596 	 */
1597 	if (sc->sc_chip == TULIP_CHIP_X3201_3) {
1598 		delay(10);
1599 		TULIP_WRITE(sc, CSR_BUSMODE, 0);
1600 	}
1601 
1602 	for (i = 0; i < 1000; i++) {
1603 		/*
1604 		 * Wait at least 50 PCI cycles for the reset to
1605 		 * complete before peeking at the Tulip again.
1606 		 * 10 uSec is a bit longer than 50 PCI cycles
1607 		 * (at 33MHz), but it doesn't hurt have the extra
1608 		 * wait.
1609 		 */
1610 		delay(10);
1611 		if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
1612 			break;
1613 	}
1614 
1615 	if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
1616 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
1617 
1618 	delay(1000);
1619 
1620 	/*
1621 	 * If the board has any GPIO reset sequences to issue, do them now.
1622 	 */
1623 	if (sc->sc_reset != NULL)
1624 		(*sc->sc_reset)(sc);
1625 }
1626 
1627 /*
1628  * tlp_init:
1629  *
1630  *	Initialize the interface.  Must be called at splnet().
1631  */
1632 int
1633 tlp_init(sc)
1634 	struct tulip_softc *sc;
1635 {
1636 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1637 	struct tulip_txsoft *txs;
1638 	struct tulip_rxsoft *rxs;
1639 	int i, error = 0;
1640 
1641 	/*
1642 	 * Cancel any pending I/O.
1643 	 */
1644 	tlp_stop(sc, 0);
1645 
1646 	/*
1647 	 * Initialize `opmode' to 0, and call the pre-init routine, if
1648 	 * any.  This is required because the 2114x and some of the
1649 	 * clones require that the media-related bits in `opmode' be
1650 	 * set before performing a soft-reset in order to get internal
1651 	 * chip pathways are correct.  Yay!
1652 	 */
1653 	sc->sc_opmode = 0;
1654 	if (sc->sc_preinit != NULL)
1655 		(*sc->sc_preinit)(sc);
1656 
1657 	/*
1658 	 * Reset the Tulip to a known state.
1659 	 */
1660 	tlp_reset(sc);
1661 
1662 	/*
1663 	 * Initialize the BUSMODE register.
1664 	 */
1665 	sc->sc_busmode = BUSMODE_BAR;
1666 	switch (sc->sc_chip) {
1667 	case TULIP_CHIP_21140:
1668 	case TULIP_CHIP_21140A:
1669 	case TULIP_CHIP_21142:
1670 	case TULIP_CHIP_21143:
1671 	case TULIP_CHIP_82C115:
1672 	case TULIP_CHIP_MX98725:
1673 		/*
1674 		 * If we're allowed to do so, use Memory Read Line
1675 		 * and Memory Read Multiple.
1676 		 *
1677 		 * XXX Should we use Memory Write and Invalidate?
1678 		 */
1679 		if (sc->sc_flags & TULIPF_MRL)
1680 			sc->sc_busmode |= BUSMODE_RLE;
1681 		if (sc->sc_flags & TULIPF_MRM)
1682 			sc->sc_busmode |= BUSMODE_RME;
1683 #if 0
1684 		if (sc->sc_flags & TULIPF_MWI)
1685 			sc->sc_busmode |= BUSMODE_WLE;
1686 #endif
1687 
1688 	default:
1689 		/* Nothing. */
1690 	}
1691 	switch (sc->sc_cacheline) {
1692 	default:
1693 		/*
1694 		 * Note: We must *always* set these bits; a cache
1695 		 * alignment of 0 is RESERVED.
1696 		 */
1697 	case 8:
1698 		sc->sc_busmode |= BUSMODE_CAL_8LW;
1699 		break;
1700 	case 16:
1701 		sc->sc_busmode |= BUSMODE_CAL_16LW;
1702 		break;
1703 	case 32:
1704 		sc->sc_busmode |= BUSMODE_CAL_32LW;
1705 		break;
1706 	}
1707 	switch (sc->sc_chip) {
1708 	case TULIP_CHIP_82C168:
1709 	case TULIP_CHIP_82C169:
1710 		sc->sc_busmode |= BUSMODE_PBL_16LW | BUSMODE_PNIC_MBO;
1711 		break;
1712 	default:
1713 		sc->sc_busmode |= BUSMODE_PBL_DEFAULT;
1714 		break;
1715 	}
1716 #if BYTE_ORDER == BIG_ENDIAN
1717 	/*
1718 	 * Can't use BUSMODE_BLE or BUSMODE_DBO; not all chips
1719 	 * support them, and even on ones that do, it doesn't
1720 	 * always work.
1721 	 */
1722 #endif
1723 
1724 	/*
1725 	 * Some chips have a broken bus interface.
1726 	 */
1727 	switch (sc->sc_chip) {
1728 	case TULIP_CHIP_DM9102:
1729 	case TULIP_CHIP_DM9102A:
1730 		sc->sc_busmode = 0;
1731 		break;
1732 
1733 	default:
1734 		/* Nothing. */
1735 	}
1736 
1737 	TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);
1738 
1739 	/*
1740 	 * Initialize the OPMODE register.  We don't write it until
1741 	 * we're ready to begin the transmit and receive processes.
1742 	 *
1743 	 * Media-related OPMODE bits are set in the media callbacks
1744 	 * for each specific chip/board.
1745 	 */
1746 	sc->sc_opmode |= OPMODE_SR | OPMODE_ST |
1747 	    sc->sc_txth[sc->sc_txthresh].txth_opmode;
1748 
1749 	/*
1750 	 * Magical mystery initialization on the Macronix chips.
1751 	 * The MX98713 uses its own magic value, the rest share
1752 	 * a common one.
1753 	 */
1754 	switch (sc->sc_chip) {
1755 	case TULIP_CHIP_MX98713:
1756 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
1757 		break;
1758 
1759 	case TULIP_CHIP_MX98713A:
1760 	case TULIP_CHIP_MX98715:
1761 	case TULIP_CHIP_MX98715A:
1762 	case TULIP_CHIP_MX98725:
1763 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
1764 		break;
1765 
1766 	default:
1767 		/* Nothing. */
1768 	}
1769 
1770 	/*
1771 	 * Initialize the transmit descriptor ring.
1772 	 */
1773 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1774 	for (i = 0; i < TULIP_NTXDESC; i++) {
1775 		sc->sc_txdescs[i].td_ctl = htole32(sc->sc_tdctl_ch);
1776 		sc->sc_txdescs[i].td_bufaddr2 =
1777 		    htole32(TULIP_CDTXADDR(sc, TULIP_NEXTTX(i)));
1778 	}
1779 	sc->sc_txdescs[TULIP_NTXDESC - 1].td_ctl |= htole32(sc->sc_tdctl_er);
1780 	TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
1781 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1782 	sc->sc_txfree = TULIP_NTXDESC;
1783 	sc->sc_txnext = 0;
1784 
1785 	/*
1786 	 * Initialize the transmit job descriptors.
1787 	 */
1788 	SIMPLEQ_INIT(&sc->sc_txfreeq);
1789 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
1790 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
1791 		txs = &sc->sc_txsoft[i];
1792 		txs->txs_mbuf = NULL;
1793 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1794 	}
1795 
1796 	/*
1797 	 * Initialize the receive descriptor and receive job
1798 	 * descriptor rings.
1799 	 */
1800 	for (i = 0; i < TULIP_NRXDESC; i++) {
1801 		rxs = &sc->sc_rxsoft[i];
1802 		if (rxs->rxs_mbuf == NULL) {
1803 			if ((error = tlp_add_rxbuf(sc, i)) != 0) {
1804 				printf("%s: unable to allocate or map rx "
1805 				    "buffer %d, error = %d\n",
1806 				    sc->sc_dev.dv_xname, i, error);
1807 				/*
1808 				 * XXX Should attempt to run with fewer receive
1809 				 * XXX buffers instead of just failing.
1810 				 */
1811 				tlp_rxdrain(sc);
1812 				goto out;
1813 			}
1814 		}
1815 	}
1816 	sc->sc_rxptr = 0;
1817 
1818 	/*
1819 	 * Initialize the interrupt mask and enable interrupts.
1820 	 */
1821 	/* normal interrupts */
1822 	sc->sc_inten =  STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
1823 
1824 	/* abnormal interrupts */
1825 	sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
1826 	    STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;
1827 
1828 	sc->sc_rxint_mask = STATUS_RI|STATUS_RU|STATUS_RWT;
1829 	sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
1830 
1831 	switch (sc->sc_chip) {
1832 	case TULIP_CHIP_WB89C840F:
1833 		/*
1834 		 * Clear bits that we don't want that happen to
1835 		 * overlap or don't exist.
1836 		 */
1837 		sc->sc_inten &= ~(STATUS_WINB_REI|STATUS_RWT);
1838 		break;
1839 
1840 	default:
1841 		/* Nothing. */
1842 	}
1843 
1844 	sc->sc_rxint_mask &= sc->sc_inten;
1845 	sc->sc_txint_mask &= sc->sc_inten;
1846 
1847 	TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
1848 	TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);
1849 
1850 	/*
1851 	 * Give the transmit and receive rings to the Tulip.
1852 	 */
1853 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
1854 	TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));
1855 
1856 	/*
1857 	 * On chips that do this differently, set the station address.
1858 	 */
1859 	switch (sc->sc_chip) {
1860 	case TULIP_CHIP_WB89C840F:
1861 	    {
1862 		/* XXX Do this with stream writes? */
1863 		bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0);
1864 
1865 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
1866 			bus_space_write_1(sc->sc_st, sc->sc_sh,
1867 			    cpa + i, LLADDR(ifp->if_sadl)[i]);
1868 		}
1869 		break;
1870 	    }
1871 
1872 	case TULIP_CHIP_AL981:
1873 	    {
1874 		u_int32_t reg;
1875 		u_int8_t *enaddr = LLADDR(ifp->if_sadl);
1876 
1877 		reg = enaddr[0] |
1878 		      (enaddr[1] << 8) |
1879 		      (enaddr[2] << 16) |
1880 		      (enaddr[3] << 24);
1881 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR0, reg);
1882 
1883 		reg = enaddr[4] |
1884 		      (enaddr[5] << 8);
1885 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR1, reg);
1886 	    }
1887 
1888 	default:
1889 		/* Nothing. */
1890 	}
1891 
1892 	/*
1893 	 * Set the receive filter.  This will start the transmit and
1894 	 * receive processes.
1895 	 */
1896 	(*sc->sc_filter_setup)(sc);
1897 
1898 	/*
1899 	 * Set the current media.
1900 	 */
1901 	(void) (*sc->sc_mediasw->tmsw_set)(sc);
1902 
1903 	/*
1904 	 * Start the receive process.
1905 	 */
1906 	TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
1907 
1908 	if (sc->sc_tick != NULL) {
1909 		/* Start the one second clock. */
1910 		callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
1911 	}
1912 
1913 	/*
1914 	 * Note that the interface is now running.
1915 	 */
1916 	ifp->if_flags |= IFF_RUNNING;
1917 	ifp->if_flags &= ~IFF_OACTIVE;
1918 
1919  out:
1920 	if (error)
1921 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1922 	return (error);
1923 }
1924 
1925 /*
1926  * tlp_enable:
1927  *
1928  *	Enable the Tulip chip.
1929  */
1930 int
1931 tlp_enable(sc)
1932 	struct tulip_softc *sc;
1933 {
1934 
1935 	if (TULIP_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
1936 		if ((*sc->sc_enable)(sc) != 0) {
1937 			printf("%s: device enable failed\n",
1938 			    sc->sc_dev.dv_xname);
1939 			return (EIO);
1940 		}
1941 		sc->sc_flags |= TULIPF_ENABLED;
1942 	}
1943 	return (0);
1944 }
1945 
1946 /*
1947  * tlp_disable:
1948  *
1949  *	Disable the Tulip chip.
1950  */
1951 void
1952 tlp_disable(sc)
1953 	struct tulip_softc *sc;
1954 {
1955 
1956 	if (TULIP_IS_ENABLED(sc) && sc->sc_disable != NULL) {
1957 		(*sc->sc_disable)(sc);
1958 		sc->sc_flags &= ~TULIPF_ENABLED;
1959 	}
1960 }
1961 
1962 /*
1963  * tlp_power:
1964  *
1965  *	Power management (suspend/resume) hook.
1966  */
1967 void
1968 tlp_power(why, arg)
1969 	int why;
1970 	void *arg;
1971 {
1972 	struct tulip_softc *sc = arg;
1973 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1974 	int s;
1975 
1976 	s = splnet();
1977 	if (why != PWR_RESUME) {
1978 		tlp_stop(sc, 0);
1979 		if (sc->sc_power != NULL)
1980 			(*sc->sc_power)(sc, why);
1981 	} else if (ifp->if_flags & IFF_UP) {
1982 		if (sc->sc_power != NULL)
1983 			(*sc->sc_power)(sc, why);
1984 		tlp_init(sc);
1985 	}
1986 	splx(s);
1987 }
1988 
1989 /*
1990  * tlp_rxdrain:
1991  *
1992  *	Drain the receive queue.
1993  */
1994 void
1995 tlp_rxdrain(sc)
1996 	struct tulip_softc *sc;
1997 {
1998 	struct tulip_rxsoft *rxs;
1999 	int i;
2000 
2001 	for (i = 0; i < TULIP_NRXDESC; i++) {
2002 		rxs = &sc->sc_rxsoft[i];
2003 		if (rxs->rxs_mbuf != NULL) {
2004 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2005 			m_freem(rxs->rxs_mbuf);
2006 			rxs->rxs_mbuf = NULL;
2007 		}
2008 	}
2009 }
2010 
2011 /*
2012  * tlp_stop:
2013  *
2014  *	Stop transmission on the interface.
2015  */
2016 void
2017 tlp_stop(sc, drain)
2018 	struct tulip_softc *sc;
2019 	int drain;
2020 {
2021 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2022 	struct tulip_txsoft *txs;
2023 
2024 	if (sc->sc_tick != NULL) {
2025 		/* Stop the one second clock. */
2026 		callout_stop(&sc->sc_tick_callout);
2027 	}
2028 
2029 	if (sc->sc_flags & TULIPF_HAS_MII) {
2030 		/* Down the MII. */
2031 		mii_down(&sc->sc_mii);
2032 	}
2033 
2034 	/* Disable interrupts. */
2035 	TULIP_WRITE(sc, CSR_INTEN, 0);
2036 
2037 	/* Stop the transmit and receive processes. */
2038 	TULIP_WRITE(sc, CSR_OPMODE, 0);
2039 	TULIP_WRITE(sc, CSR_RXLIST, 0);
2040 	TULIP_WRITE(sc, CSR_TXLIST, 0);
2041 
2042 	/*
2043 	 * Release any queued transmit buffers.
2044 	 */
2045 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
2046 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
2047 		if (txs->txs_mbuf != NULL) {
2048 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
2049 			m_freem(txs->txs_mbuf);
2050 			txs->txs_mbuf = NULL;
2051 		}
2052 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
2053 	}
2054 
2055 	if (drain) {
2056 		/*
2057 		 * Release the receive buffers.
2058 		 */
2059 		tlp_rxdrain(sc);
2060 	}
2061 
2062 	sc->sc_flags &= ~(TULIPF_WANT_SETUP|TULIPF_DOING_SETUP);
2063 
2064 	/*
2065 	 * Mark the interface down and cancel the watchdog timer.
2066 	 */
2067 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2068 	ifp->if_timer = 0;
2069 }
2070 
2071 #define	SROM_EMIT(sc, x)						\
2072 do {									\
2073 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
2074 	delay(2);							\
2075 } while (0)
2076 
2077 /*
2078  * tlp_srom_idle:
2079  *
2080  *	Put the SROM in idle state.
2081  */
2082 void
2083 tlp_srom_idle(sc)
2084 	struct tulip_softc *sc;
2085 {
2086 	u_int32_t miirom;
2087 	int i;
2088 
2089 	miirom = MIIROM_SR;
2090 	SROM_EMIT(sc, miirom);
2091 
2092 	miirom |= MIIROM_RD;
2093 	SROM_EMIT(sc, miirom);
2094 
2095 	miirom |= MIIROM_SROMCS;
2096 	SROM_EMIT(sc, miirom);
2097 
2098 	SROM_EMIT(sc, miirom|MIIROM_SROMSK);
2099 
2100 	/* Strobe the clock 32 times. */
2101 	for (i = 0; i < 32; i++) {
2102 		SROM_EMIT(sc, miirom);
2103 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
2104 	}
2105 
2106 	SROM_EMIT(sc, miirom);
2107 
2108 	miirom &= ~MIIROM_SROMCS;
2109 	SROM_EMIT(sc, miirom);
2110 
2111 	SROM_EMIT(sc, 0);
2112 }
2113 
2114 /*
2115  * tlp_srom_size:
2116  *
2117  *	Determine the number of address bits in the SROM.
2118  */
2119 int
2120 tlp_srom_size(sc)
2121 	struct tulip_softc *sc;
2122 {
2123 	u_int32_t miirom;
2124 	int x;
2125 
2126 	/* Select the SROM. */
2127 	miirom = MIIROM_SR;
2128 	SROM_EMIT(sc, miirom);
2129 
2130 	miirom |= MIIROM_RD;
2131 	SROM_EMIT(sc, miirom);
2132 
2133 	/* Send CHIP SELECT for one clock tick. */
2134 	miirom |= MIIROM_SROMCS;
2135 	SROM_EMIT(sc, miirom);
2136 
2137 	/* Shift in the READ opcode. */
2138 	for (x = 3; x > 0; x--) {
2139 		if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
2140 			miirom |= MIIROM_SROMDI;
2141 		else
2142 			miirom &= ~MIIROM_SROMDI;
2143 		SROM_EMIT(sc, miirom);
2144 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
2145 		SROM_EMIT(sc, miirom);
2146 	}
2147 
2148 	/* Shift in address and look for dummy 0 bit. */
2149 	for (x = 1; x <= 12; x++) {
2150 		miirom &= ~MIIROM_SROMDI;
2151 		SROM_EMIT(sc, miirom);
2152 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
2153 		if (!TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
2154 			break;
2155 		SROM_EMIT(sc, miirom);
2156 	}
2157 
2158 	/* Clear CHIP SELECT. */
2159 	miirom &= ~MIIROM_SROMCS;
2160 	SROM_EMIT(sc, miirom);
2161 
2162 	/* Deselect the SROM. */
2163 	SROM_EMIT(sc, 0);
2164 
2165 	if (x < 4 || x > 12) {
2166 		printf("%s: broken MicroWire interface detected; "
2167 		    "setting SROM size to 1Kb\n", sc->sc_dev.dv_xname);
2168 		return (6);
2169 	} else {
2170 		if (tlp_srom_debug)
2171 			printf("%s: SROM size is 2^%d*16 bits (%d bytes)\n",
2172 			    sc->sc_dev.dv_xname, x, (1 << (x + 4)) >> 3);
2173 		return (x);
2174 	}
2175 }
2176 
2177 /*
2178  * tlp_read_srom:
2179  *
2180  *	Read the Tulip SROM.
2181  */
2182 int
2183 tlp_read_srom(sc)
2184 	struct tulip_softc *sc;
2185 {
2186 	int size;
2187 	u_int32_t miirom;
2188 	u_int16_t datain;
2189 	int i, x;
2190 
2191 	tlp_srom_idle(sc);
2192 
2193 	sc->sc_srom_addrbits = tlp_srom_size(sc);
2194 	if (sc->sc_srom_addrbits == 0)
2195 		return (0);
2196 	size = TULIP_ROM_SIZE(sc->sc_srom_addrbits);
2197 	sc->sc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
2198 
2199 	/* Select the SROM. */
2200 	miirom = MIIROM_SR;
2201 	SROM_EMIT(sc, miirom);
2202 
2203 	miirom |= MIIROM_RD;
2204 	SROM_EMIT(sc, miirom);
2205 
2206 	for (i = 0; i < size; i += 2) {
2207 		/* Send CHIP SELECT for one clock tick. */
2208 		miirom |= MIIROM_SROMCS;
2209 		SROM_EMIT(sc, miirom);
2210 
2211 		/* Shift in the READ opcode. */
2212 		for (x = 3; x > 0; x--) {
2213 			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
2214 				miirom |= MIIROM_SROMDI;
2215 			else
2216 				miirom &= ~MIIROM_SROMDI;
2217 			SROM_EMIT(sc, miirom);
2218 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
2219 			SROM_EMIT(sc, miirom);
2220 		}
2221 
2222 		/* Shift in address. */
2223 		for (x = sc->sc_srom_addrbits; x > 0; x--) {
2224 			if (i & (1 << x))
2225 				miirom |= MIIROM_SROMDI;
2226 			else
2227 				miirom &= ~MIIROM_SROMDI;
2228 			SROM_EMIT(sc, miirom);
2229 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
2230 			SROM_EMIT(sc, miirom);
2231 		}
2232 
2233 		/* Shift out data. */
2234 		miirom &= ~MIIROM_SROMDI;
2235 		datain = 0;
2236 		for (x = 16; x > 0; x--) {
2237 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
2238 			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
2239 				datain |= (1 << (x - 1));
2240 			SROM_EMIT(sc, miirom);
2241 		}
2242 		sc->sc_srom[i] = datain & 0xff;
2243 		sc->sc_srom[i + 1] = datain >> 8;
2244 
2245 		/* Clear CHIP SELECT. */
2246 		miirom &= ~MIIROM_SROMCS;
2247 		SROM_EMIT(sc, miirom);
2248 	}
2249 
2250 	/* Deselect the SROM. */
2251 	SROM_EMIT(sc, 0);
2252 
2253 	/* ...and idle it. */
2254 	tlp_srom_idle(sc);
2255 
2256 	if (tlp_srom_debug) {
2257 		printf("SROM CONTENTS:");
2258 		for (i = 0; i < size; i++) {
2259 			if ((i % 8) == 0)
2260 				printf("\n\t");
2261 			printf("0x%02x ", sc->sc_srom[i]);
2262 		}
2263 		printf("\n");
2264 	}
2265 
2266 	return (1);
2267 }
2268 
2269 #undef SROM_EMIT
2270 
2271 /*
2272  * tlp_add_rxbuf:
2273  *
2274  *	Add a receive buffer to the indicated descriptor.
2275  */
2276 int
2277 tlp_add_rxbuf(sc, idx)
2278 	struct tulip_softc *sc;
2279 	int idx;
2280 {
2281 	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
2282 	struct mbuf *m;
2283 	int error;
2284 
2285 	MGETHDR(m, M_DONTWAIT, MT_DATA);
2286 	if (m == NULL)
2287 		return (ENOBUFS);
2288 
2289 	MCLGET(m, M_DONTWAIT);
2290 	if ((m->m_flags & M_EXT) == 0) {
2291 		m_freem(m);
2292 		return (ENOBUFS);
2293 	}
2294 
2295 	if (rxs->rxs_mbuf != NULL)
2296 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2297 
2298 	rxs->rxs_mbuf = m;
2299 
2300 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
2301 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
2302 	if (error) {
2303 		printf("%s: can't load rx DMA map %d, error = %d\n",
2304 		    sc->sc_dev.dv_xname, idx, error);
2305 		panic("tlp_add_rxbuf");	/* XXX */
2306 	}
2307 
2308 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
2309 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
2310 
2311 	TULIP_INIT_RXDESC(sc, idx);
2312 
2313 	return (0);
2314 }
2315 
2316 /*
2317  * tlp_srom_crcok:
2318  *
2319  *	Check the CRC of the Tulip SROM.
2320  */
2321 int
2322 tlp_srom_crcok(romdata)
2323 	const u_int8_t *romdata;
2324 {
2325 	u_int32_t crc;
2326 
2327 	crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM);
2328 	crc = (crc & 0xffff) ^ 0xffff;
2329 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
2330 		return (1);
2331 
2332 	/*
2333 	 * Try an alternate checksum.
2334 	 */
2335 	crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM1);
2336 	crc = (crc & 0xffff) ^ 0xffff;
2337 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM1))
2338 		return (1);
2339 
2340 	return (0);
2341 }
2342 
2343 /*
2344  * tlp_isv_srom:
2345  *
2346  *	Check to see if the SROM is in the new standardized format.
2347  */
2348 int
2349 tlp_isv_srom(romdata)
2350 	const u_int8_t *romdata;
2351 {
2352 	int i;
2353 	u_int16_t cksum;
2354 
2355 	if (tlp_srom_crcok(romdata)) {
2356 		/*
2357 		 * SROM CRC checks out; must be in the new format.
2358 		 */
2359 		return (1);
2360 	}
2361 
2362 	cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM);
2363 	if (cksum == 0xffff || cksum == 0) {
2364 		/*
2365 		 * No checksum present.  Check the SROM ID; 18 bytes of 0
2366 		 * followed by 1 (version) followed by the number of
2367 		 * adapters which use this SROM (should be non-zero).
2368 		 */
2369 		for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) {
2370 			if (romdata[i] != 0)
2371 				return (0);
2372 		}
2373 		if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1)
2374 			return (0);
2375 		if (romdata[TULIP_ROM_CHIP_COUNT] == 0)
2376 			return (0);
2377 		return (1);
2378 	}
2379 
2380 	return (0);
2381 }
2382 
2383 /*
2384  * tlp_isv_srom_enaddr:
2385  *
2386  *	Get the Ethernet address from an ISV SROM.
2387  */
2388 int
2389 tlp_isv_srom_enaddr(sc, enaddr)
2390 	struct tulip_softc *sc;
2391 	u_int8_t *enaddr;
2392 {
2393 	int i, devcnt;
2394 
2395 	if (tlp_isv_srom(sc->sc_srom) == 0)
2396 		return (0);
2397 
2398 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
2399 	for (i = 0; i < devcnt; i++) {
2400 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
2401 			break;
2402 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
2403 		    sc->sc_devno)
2404 			break;
2405 	}
2406 
2407 	if (i == devcnt)
2408 		return (0);
2409 
2410 	memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS],
2411 	    ETHER_ADDR_LEN);
2412 	enaddr[5] += i;
2413 
2414 	return (1);
2415 }
2416 
2417 /*
2418  * tlp_parse_old_srom:
2419  *
2420  *	Parse old-format SROMs.
2421  *
2422  *	This routine is largely lifted from Matt Thomas's `de' driver.
2423  */
2424 int
2425 tlp_parse_old_srom(sc, enaddr)
2426 	struct tulip_softc *sc;
2427 	u_int8_t *enaddr;
2428 {
2429 	static const u_int8_t testpat[] =
2430 	    { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
2431 	int i;
2432 	u_int32_t cksum;
2433 
2434 	if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
2435 		/*
2436 		 * Some vendors (e.g. ZNYX) don't use the standard
2437 		 * DEC Address ROM format, but rather just have an
2438 		 * Ethernet address in the first 6 bytes, maybe a
2439 		 * 2 byte checksum, and then all 0xff's.
2440 		 *
2441 		 * On the other hand, Cobalt Networks interfaces
2442 		 * simply have the address in the first six bytes
2443 		 * with the rest zeroed out.
2444 		 */
2445 		for (i = 8; i < 32; i++) {
2446 			if (sc->sc_srom[i] != 0xff &&
2447 			    sc->sc_srom[i] != 0)
2448 				return (0);
2449 		}
2450 
2451 		/*
2452 		 * Sanity check the Ethernet address:
2453 		 *
2454 		 *	- Make sure it's not multicast or locally
2455 		 *	  assigned
2456 		 *	- Make sure it has a non-0 OUI
2457 		 */
2458 		if (sc->sc_srom[0] & 3)
2459 			return (0);
2460 		if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
2461 		    sc->sc_srom[2] == 0)
2462 			return (0);
2463 
2464 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
2465 		return (1);
2466 	}
2467 
2468 	/*
2469 	 * Standard DEC Address ROM test.
2470 	 */
2471 
2472 	if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
2473 		return (0);
2474 
2475 	for (i = 0; i < 8; i++) {
2476 		if (sc->sc_srom[i] != sc->sc_srom[15 - i])
2477 			return (0);
2478 	}
2479 
2480 	memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
2481 
2482 	cksum = *(u_int16_t *) &enaddr[0];
2483 
2484 	cksum <<= 1;
2485 	if (cksum > 0xffff)
2486 		cksum -= 0xffff;
2487 
2488 	cksum += *(u_int16_t *) &enaddr[2];
2489 	if (cksum > 0xffff)
2490 		cksum -= 0xffff;
2491 
2492 	cksum <<= 1;
2493 	if (cksum > 0xffff)
2494 		cksum -= 0xffff;
2495 
2496 	cksum += *(u_int16_t *) &enaddr[4];
2497 	if (cksum >= 0xffff)
2498 		cksum -= 0xffff;
2499 
2500 	if (cksum != *(u_int16_t *) &sc->sc_srom[6])
2501 		return (0);
2502 
2503 	return (1);
2504 }
2505 
2506 /*
2507  * tlp_filter_setup:
2508  *
2509  *	Set the Tulip's receive filter.
2510  */
2511 void
2512 tlp_filter_setup(sc)
2513 	struct tulip_softc *sc;
2514 {
2515 	struct ethercom *ec = &sc->sc_ethercom;
2516 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2517 	struct ether_multi *enm;
2518 	struct ether_multistep step;
2519 	__volatile u_int32_t *sp;
2520 	struct tulip_txsoft *txs;
2521 	u_int8_t enaddr[ETHER_ADDR_LEN];
2522 	u_int32_t hash, hashsize;
2523 	int cnt;
2524 
2525 	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
2526 	    sc->sc_dev.dv_xname, sc->sc_flags));
2527 
2528 	memcpy(enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
2529 
2530 	/*
2531 	 * If there are transmissions pending, wait until they have
2532 	 * completed.
2533 	 */
2534 	if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL ||
2535 	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
2536 		sc->sc_flags |= TULIPF_WANT_SETUP;
2537 		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
2538 		    sc->sc_dev.dv_xname));
2539 		return;
2540 	}
2541 	sc->sc_flags &= ~TULIPF_WANT_SETUP;
2542 
2543 	switch (sc->sc_chip) {
2544 	case TULIP_CHIP_82C115:
2545 		hashsize = TULIP_PNICII_HASHSIZE;
2546 		break;
2547 
2548 	default:
2549 		hashsize = TULIP_MCHASHSIZE;
2550 	}
2551 
2552 	/*
2553 	 * If we're running, idle the transmit and receive engines.  If
2554 	 * we're NOT running, we're being called from tlp_init(), and our
2555 	 * writing OPMODE will start the transmit and receive processes
2556 	 * in motion.
2557 	 */
2558 	if (ifp->if_flags & IFF_RUNNING) {
2559 		/*
2560 		 * Actually, some chips seem to need a really hard
2561 		 * kick in the head for this to work.  The genuine
2562 		 * DEC chips can just be idled, but some of the
2563 		 * clones seem to REALLY want a reset here.  Doing
2564 		 * the reset will end up here again, but with
2565 		 * IFF_RUNNING cleared.
2566 		 */
2567 		switch (sc->sc_chip) {
2568 		case TULIP_CHIP_82C168:
2569 		case TULIP_CHIP_82C169:
2570 			tlp_init(sc);
2571 			return;
2572 
2573 		default:
2574 			tlp_idle(sc, OPMODE_ST|OPMODE_SR);
2575 		}
2576 	}
2577 
2578 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
2579 
2580 	if (ifp->if_flags & IFF_PROMISC) {
2581 		sc->sc_opmode |= OPMODE_PR;
2582 		goto allmulti;
2583 	}
2584 
2585 	/*
2586 	 * Try Perfect filtering first.
2587 	 */
2588 
2589 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
2590 	sp = TULIP_CDSP(sc);
2591 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
2592 	cnt = 0;
2593 	ETHER_FIRST_MULTI(step, ec, enm);
2594 	while (enm != NULL) {
2595 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2596 			/*
2597 			 * We must listen to a range of multicast addresses.
2598 			 * For now, just accept all multicasts, rather than
2599 			 * trying to set only those filter bits needed to match
2600 			 * the range.  (At this time, the only use of address
2601 			 * ranges is for IP multicast routing, for which the
2602 			 * range is big enough to require all bits set.)
2603 			 */
2604 			goto allmulti;
2605 		}
2606 		if (cnt == (TULIP_MAXADDRS - 2)) {
2607 			/*
2608 			 * We already have our multicast limit (still need
2609 			 * our station address and broadcast).  Go to
2610 			 * Hash-Perfect mode.
2611 			 */
2612 			goto hashperfect;
2613 		}
2614 		cnt++;
2615 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 0);
2616 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 1);
2617 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 2);
2618 		ETHER_NEXT_MULTI(step, enm);
2619 	}
2620 
2621 	if (ifp->if_flags & IFF_BROADCAST) {
2622 		/* ...and the broadcast address. */
2623 		cnt++;
2624 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2625 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2626 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2627 	}
2628 
2629 	/* Pad the rest with our station address. */
2630 	for (; cnt < TULIP_MAXADDRS; cnt++) {
2631 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
2632 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
2633 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
2634 	}
2635 	ifp->if_flags &= ~IFF_ALLMULTI;
2636 	goto setit;
2637 
2638  hashperfect:
2639 	/*
2640 	 * Try Hash-Perfect mode.
2641 	 */
2642 
2643 	/*
2644 	 * Some 21140 chips have broken Hash-Perfect modes.  On these
2645 	 * chips, we simply use Hash-Only mode, and put our station
2646 	 * address into the filter.
2647 	 */
2648 	if (sc->sc_chip == TULIP_CHIP_21140)
2649 		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
2650 	else
2651 		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
2652 	sp = TULIP_CDSP(sc);
2653 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
2654 	ETHER_FIRST_MULTI(step, ec, enm);
2655 	while (enm != NULL) {
2656 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2657 			/*
2658 			 * We must listen to a range of multicast addresses.
2659 			 * For now, just accept all multicasts, rather than
2660 			 * trying to set only those filter bits needed to match
2661 			 * the range.  (At this time, the only use of address
2662 			 * ranges is for IP multicast routing, for which the
2663 			 * range is big enough to require all bits set.)
2664 			 */
2665 			goto allmulti;
2666 		}
2667 		hash = tlp_mchash(enm->enm_addrlo, hashsize);
2668 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
2669 		ETHER_NEXT_MULTI(step, enm);
2670 	}
2671 
2672 	if (ifp->if_flags & IFF_BROADCAST) {
2673 		/* ...and the broadcast address. */
2674 		hash = tlp_mchash(etherbroadcastaddr, hashsize);
2675 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
2676 	}
2677 
2678 	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
2679 		/* ...and our station address. */
2680 		hash = tlp_mchash(enaddr, hashsize);
2681 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
2682 	} else {
2683 		/*
2684 		 * Hash-Perfect mode; put our station address after
2685 		 * the hash table.
2686 		 */
2687 		sp[39] = TULIP_SP_FIELD(enaddr, 0);
2688 		sp[40] = TULIP_SP_FIELD(enaddr, 1);
2689 		sp[41] = TULIP_SP_FIELD(enaddr, 2);
2690 	}
2691 	ifp->if_flags &= ~IFF_ALLMULTI;
2692 	goto setit;
2693 
2694  allmulti:
2695 	/*
2696 	 * Use Perfect filter mode.  First address is the broadcast address,
2697 	 * and pad the rest with our station address.  We'll set Pass-all-
2698 	 * multicast in OPMODE below.
2699 	 */
2700 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
2701 	sp = TULIP_CDSP(sc);
2702 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
2703 	cnt = 0;
2704 	if (ifp->if_flags & IFF_BROADCAST) {
2705 		cnt++;
2706 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2707 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2708 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2709 	}
2710 	for (; cnt < TULIP_MAXADDRS; cnt++) {
2711 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
2712 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
2713 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
2714 	}
2715 	ifp->if_flags |= IFF_ALLMULTI;
2716 
2717  setit:
2718 	if (ifp->if_flags & IFF_ALLMULTI)
2719 		sc->sc_opmode |= OPMODE_PM;
2720 
2721 	/* Sync the setup packet buffer. */
2722 	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
2723 
2724 	/*
2725 	 * Fill in the setup packet descriptor.
2726 	 */
2727 	txs = SIMPLEQ_FIRST(&sc->sc_txfreeq);
2728 
2729 	txs->txs_firstdesc = sc->sc_txnext;
2730 	txs->txs_lastdesc = sc->sc_txnext;
2731 	txs->txs_ndescs = 1;
2732 	txs->txs_mbuf = NULL;
2733 
2734 	sc->sc_txdescs[sc->sc_txnext].td_bufaddr1 =
2735 	    htole32(TULIP_CDSPADDR(sc));
2736 	sc->sc_txdescs[sc->sc_txnext].td_ctl =
2737 	    htole32((TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
2738 	    sc->sc_filtmode | TDCTL_Tx_SET | sc->sc_setup_fsls |
2739 	    TDCTL_Tx_IC | sc->sc_tdctl_ch |
2740 	    (sc->sc_txnext == (TULIP_NTXDESC - 1) ? sc->sc_tdctl_er : 0));
2741 	sc->sc_txdescs[sc->sc_txnext].td_status = htole32(TDSTAT_OWN);
2742 	TULIP_CDTXSYNC(sc, sc->sc_txnext, txs->txs_ndescs,
2743 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2744 
2745 	/* Advance the tx pointer. */
2746 	sc->sc_txfree -= 1;
2747 	sc->sc_txnext = TULIP_NEXTTX(sc->sc_txnext);
2748 
2749 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
2750 	SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
2751 
2752 	/*
2753 	 * Set the OPMODE register.  This will also resume the
2754 	 * transmit transmit process we idled above.
2755 	 */
2756 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2757 
2758 	sc->sc_flags |= TULIPF_DOING_SETUP;
2759 
2760 	/*
2761 	 * Kick the transmitter; this will cause the Tulip to
2762 	 * read the setup descriptor.
2763 	 */
2764 	/* XXX USE AUTOPOLLING? */
2765 	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
2766 
2767 	/* Set up a watchdog timer in case the chip flakes out. */
2768 	ifp->if_timer = 5;
2769 
2770 	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
2771 }
2772 
2773 /*
2774  * tlp_winb_filter_setup:
2775  *
2776  *	Set the Winbond 89C840F's receive filter.
2777  */
2778 void
2779 tlp_winb_filter_setup(sc)
2780 	struct tulip_softc *sc;
2781 {
2782 	struct ethercom *ec = &sc->sc_ethercom;
2783 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2784 	struct ether_multi *enm;
2785 	struct ether_multistep step;
2786 	u_int32_t hash, mchash[2];
2787 
2788 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
2789 	    sc->sc_dev.dv_xname, sc->sc_flags));
2790 
2791 	sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
2792 
2793 	if (ifp->if_flags & IFF_MULTICAST)
2794 		sc->sc_opmode |= OPMODE_WINB_AMP;
2795 
2796 	if (ifp->if_flags & IFF_BROADCAST)
2797 		sc->sc_opmode |= OPMODE_WINB_ABP;
2798 
2799 	if (ifp->if_flags & IFF_PROMISC) {
2800 		sc->sc_opmode |= OPMODE_WINB_APP;
2801 		goto allmulti;
2802 	}
2803 
2804 	mchash[0] = mchash[1] = 0;
2805 
2806 	ETHER_FIRST_MULTI(step, ec, enm);
2807 	while (enm != NULL) {
2808 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2809 			/*
2810 			 * We must listen to a range of multicast addresses.
2811 			 * For now, just accept all multicasts, rather than
2812 			 * trying to set only those filter bits needed to match
2813 			 * the range.  (At this time, the only use of address
2814 			 * ranges is for IP multicast routing, for which the
2815 			 * range is big enough to require all bits set.)
2816 			 */
2817 			goto allmulti;
2818 		}
2819 
2820 		/*
2821 		 * According to the FreeBSD `wb' driver, yes, you
2822 		 * really do invert the hash.
2823 		 */
2824 		hash =
2825 		    (~(ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
2826 		    & 0x3f;
2827 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
2828 		ETHER_NEXT_MULTI(step, enm);
2829 	}
2830 	ifp->if_flags &= ~IFF_ALLMULTI;
2831 	goto setit;
2832 
2833  allmulti:
2834 	ifp->if_flags |= IFF_ALLMULTI;
2835 	mchash[0] = mchash[1] = 0xffffffff;
2836 
2837  setit:
2838 	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
2839 	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
2840 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2841 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
2842 	    sc->sc_dev.dv_xname));
2843 }
2844 
2845 /*
2846  * tlp_al981_filter_setup:
2847  *
2848  *	Set the ADMtek AL981's receive filter.
2849  */
2850 void
2851 tlp_al981_filter_setup(sc)
2852 	struct tulip_softc *sc;
2853 {
2854 	struct ethercom *ec = &sc->sc_ethercom;
2855 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2856 	struct ether_multi *enm;
2857 	struct ether_multistep step;
2858 	u_int32_t hash, mchash[2];
2859 
2860 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n",
2861 	    sc->sc_dev.dv_xname, sc->sc_flags));
2862 
2863 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
2864 
2865 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
2866 
2867 	if (ifp->if_flags & IFF_PROMISC) {
2868 		sc->sc_opmode |= OPMODE_PR;
2869 		goto allmulti;
2870 	}
2871 
2872 	mchash[0] = mchash[1] = 0;
2873 
2874 	ETHER_FIRST_MULTI(step, ec, enm);
2875 	while (enm != NULL) {
2876 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2877 			/*
2878 			 * We must listen to a range of multicast addresses.
2879 			 * For now, just accept all multicasts, rather than
2880 			 * trying to set only those filter bits needed to match
2881 			 * the range.  (At this time, the only use of address
2882 			 * ranges is for IP multicast routing, for which the
2883 			 * range is big enough to require all bits set.)
2884 			 */
2885 			goto allmulti;
2886 		}
2887 
2888 		hash = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
2889 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
2890 		ETHER_NEXT_MULTI(step, enm);
2891 	}
2892 	ifp->if_flags &= ~IFF_ALLMULTI;
2893 	goto setit;
2894 
2895  allmulti:
2896 	ifp->if_flags |= IFF_ALLMULTI;
2897 	mchash[0] = mchash[1] = 0xffffffff;
2898 
2899  setit:
2900 	TULIP_WRITE(sc, CSR_ADM_MAR0, mchash[0]);
2901 	TULIP_WRITE(sc, CSR_ADM_MAR1, mchash[1]);
2902 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2903 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n",
2904 	    sc->sc_dev.dv_xname));
2905 }
2906 
2907 /*
2908  * tlp_idle:
2909  *
2910  *	Cause the transmit and/or receive processes to go idle.
2911  */
2912 void
2913 tlp_idle(sc, bits)
2914 	struct tulip_softc *sc;
2915 	u_int32_t bits;
2916 {
2917 	static const char *tlp_tx_state_names[] = {
2918 		"STOPPED",
2919 		"RUNNING - FETCH",
2920 		"RUNNING - WAIT",
2921 		"RUNNING - READING",
2922 		"-- RESERVED --",
2923 		"RUNNING - SETUP",
2924 		"SUSPENDED",
2925 		"RUNNING - CLOSE",
2926 	};
2927 	static const char *tlp_rx_state_names[] = {
2928 		"STOPPED",
2929 		"RUNNING - FETCH",
2930 		"RUNNING - CHECK",
2931 		"RUNNING - WAIT",
2932 		"SUSPENDED",
2933 		"RUNNING - CLOSE",
2934 		"RUNNING - FLUSH",
2935 		"RUNNING - QUEUE",
2936 	};
2937 	static const char *dm9102_tx_state_names[] = {
2938 		"STOPPED",
2939 		"RUNNING - FETCH",
2940 		"RUNNING - SETUP",
2941 		"RUNNING - READING",
2942 		"RUNNING - CLOSE - CLEAR OWNER",
2943 		"RUNNING - WAIT",
2944 		"RUNNING - CLOSE - WRITE STATUS",
2945 		"SUSPENDED",
2946 	};
2947 	static const char *dm9102_rx_state_names[] = {
2948 		"STOPPED",
2949 		"RUNNING - FETCH",
2950 		"RUNNING - WAIT",
2951 		"RUNNING - QUEUE",
2952 		"RUNNING - CLOSE - CLEAR OWNER",
2953 		"RUNNING - CLOSE - WRITE STATUS",
2954 		"SUSPENDED",
2955 		"RUNNING - FLUSH",
2956 	};
2957 
2958 	const char **tx_state_names, **rx_state_names;
2959 	u_int32_t csr, ackmask = 0;
2960 	int i;
2961 
2962 	switch (sc->sc_chip) {
2963 	case TULIP_CHIP_DM9102:
2964 	case TULIP_CHIP_DM9102A:
2965 		tx_state_names = dm9102_tx_state_names;
2966 		rx_state_names = dm9102_rx_state_names;
2967 		break;
2968 
2969 	default:
2970 		tx_state_names = tlp_tx_state_names;
2971 		rx_state_names = tlp_rx_state_names;
2972 		break;
2973 	}
2974 
2975 	if (bits & OPMODE_ST)
2976 		ackmask |= STATUS_TPS;
2977 
2978 	if (bits & OPMODE_SR)
2979 		ackmask |= STATUS_RPS;
2980 
2981 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
2982 
2983 	for (i = 0; i < 1000; i++) {
2984 		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
2985 			break;
2986 		delay(10);
2987 	}
2988 
2989 	csr = TULIP_READ(sc, CSR_STATUS);
2990 	if ((csr & ackmask) != ackmask) {
2991 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
2992 		    (csr & STATUS_TS) != STATUS_TS_STOPPED)
2993 			printf("%s: transmit process failed to idle: "
2994 			    "state %s\n", sc->sc_dev.dv_xname,
2995 			    tx_state_names[(csr & STATUS_TS) >> 20]);
2996 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
2997 		    (csr & STATUS_RS) != STATUS_RS_STOPPED)
2998 			printf("%s: receive process failed to idle: "
2999 			    "state %s\n", sc->sc_dev.dv_xname,
3000 			    rx_state_names[(csr & STATUS_RS) >> 17]);
3001 	}
3002 	TULIP_WRITE(sc, CSR_STATUS, ackmask);
3003 }
3004 
3005 /*****************************************************************************
3006  * Generic media support functions.
3007  *****************************************************************************/
3008 
3009 /*
3010  * tlp_mediastatus:	[ifmedia interface function]
3011  *
3012  *	Query the current media.
3013  */
3014 void
3015 tlp_mediastatus(ifp, ifmr)
3016 	struct ifnet *ifp;
3017 	struct ifmediareq *ifmr;
3018 {
3019 	struct tulip_softc *sc = ifp->if_softc;
3020 
3021 	if (TULIP_IS_ENABLED(sc) == 0) {
3022 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
3023 		ifmr->ifm_status = 0;
3024 		return;
3025 	}
3026 
3027 	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
3028 }
3029 
3030 /*
3031  * tlp_mediachange:	[ifmedia interface function]
3032  *
3033  *	Update the current media.
3034  */
3035 int
3036 tlp_mediachange(ifp)
3037 	struct ifnet *ifp;
3038 {
3039 	struct tulip_softc *sc = ifp->if_softc;
3040 
3041 	return ((*sc->sc_mediasw->tmsw_set)(sc));
3042 }
3043 
3044 /*****************************************************************************
3045  * Support functions for MII-attached media.
3046  *****************************************************************************/
3047 
3048 /*
3049  * tlp_mii_tick:
3050  *
3051  *	One second timer, used to tick the MII.
3052  */
3053 void
3054 tlp_mii_tick(arg)
3055 	void *arg;
3056 {
3057 	struct tulip_softc *sc = arg;
3058 	int s;
3059 
3060 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
3061 		return;
3062 
3063 	s = splnet();
3064 	mii_tick(&sc->sc_mii);
3065 	splx(s);
3066 
3067 	callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
3068 }
3069 
3070 /*
3071  * tlp_mii_statchg:	[mii interface function]
3072  *
3073  *	Callback from PHY when media changes.
3074  */
3075 void
3076 tlp_mii_statchg(self)
3077 	struct device *self;
3078 {
3079 	struct tulip_softc *sc = (struct tulip_softc *)self;
3080 
3081 	/* Idle the transmit and receive processes. */
3082 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
3083 
3084 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_HBD);
3085 
3086 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
3087 		sc->sc_opmode |= OPMODE_TTM;
3088 	else
3089 		sc->sc_opmode |= OPMODE_HBD;
3090 
3091 	if (sc->sc_mii.mii_media_active & IFM_FDX)
3092 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
3093 
3094 	/*
3095 	 * Write new OPMODE bits.  This also restarts the transmit
3096 	 * and receive processes.
3097 	 */
3098 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3099 }
3100 
3101 /*
3102  * tlp_winb_mii_statchg: [mii interface function]
3103  *
3104  *	Callback from PHY when media changes.  This version is
3105  *	for the Winbond 89C840F, which has different OPMODE bits.
3106  */
3107 void
3108 tlp_winb_mii_statchg(self)
3109 	struct device *self;
3110 {
3111 	struct tulip_softc *sc = (struct tulip_softc *)self;
3112 
3113 	/* Idle the transmit and receive processes. */
3114 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
3115 
3116 	sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD);
3117 
3118 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
3119 		sc->sc_opmode |= OPMODE_WINB_FES;
3120 
3121 	if (sc->sc_mii.mii_media_active & IFM_FDX)
3122 		sc->sc_opmode |= OPMODE_FD;
3123 
3124 	/*
3125 	 * Write new OPMODE bits.  This also restarts the transmit
3126 	 * and receive processes.
3127 	 */
3128 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3129 }
3130 
3131 /*
3132  * tlp_dm9102_mii_statchg: [mii interface function]
3133  *
3134  *	Callback from PHY when media changes.  This version is
3135  *	for the DM9102.
3136  */
3137 void
3138 tlp_dm9102_mii_statchg(self)
3139 	struct device *self;
3140 {
3141 	struct tulip_softc *sc = (struct tulip_softc *)self;
3142 
3143 	/*
3144 	 * Don't idle the transmit and receive processes, here.  It
3145 	 * seems to fail, and just causes excess noise.
3146 	 */
3147 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD);
3148 
3149 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) != IFM_100_TX)
3150 		sc->sc_opmode |= OPMODE_TTM;
3151 
3152 	if (sc->sc_mii.mii_media_active & IFM_FDX)
3153 		sc->sc_opmode |= OPMODE_FD;
3154 
3155 	/*
3156 	 * Write new OPMODE bits.
3157 	 */
3158 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3159 }
3160 
3161 /*
3162  * tlp_mii_getmedia:
3163  *
3164  *	Callback from ifmedia to request current media status.
3165  */
3166 void
3167 tlp_mii_getmedia(sc, ifmr)
3168 	struct tulip_softc *sc;
3169 	struct ifmediareq *ifmr;
3170 {
3171 
3172 	mii_pollstat(&sc->sc_mii);
3173 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
3174 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
3175 }
3176 
3177 /*
3178  * tlp_mii_setmedia:
3179  *
3180  *	Callback from ifmedia to request new media setting.
3181  */
3182 int
3183 tlp_mii_setmedia(sc)
3184 	struct tulip_softc *sc;
3185 {
3186 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
3187 
3188 	if (ifp->if_flags & IFF_UP) {
3189 		switch (sc->sc_chip) {
3190 		case TULIP_CHIP_21142:
3191 		case TULIP_CHIP_21143:
3192 			/* Disable the internal Nway engine. */
3193 			TULIP_WRITE(sc, CSR_SIATXRX, 0);
3194 			break;
3195 
3196 		default:
3197 			/* Nothing. */
3198 		}
3199 		mii_mediachg(&sc->sc_mii);
3200 	}
3201 	return (0);
3202 }
3203 
3204 /*
3205  * tlp_bitbang_mii_readreg:
3206  *
3207  *	Read a PHY register via bit-bang'ing the MII.
3208  */
3209 int
3210 tlp_bitbang_mii_readreg(self, phy, reg)
3211 	struct device *self;
3212 	int phy, reg;
3213 {
3214 	struct tulip_softc *sc = (void *) self;
3215 
3216 	return (mii_bitbang_readreg(self, sc->sc_bitbang_ops, phy, reg));
3217 }
3218 
3219 /*
3220  * tlp_bitbang_mii_writereg:
3221  *
3222  *	Write a PHY register via bit-bang'ing the MII.
3223  */
3224 void
3225 tlp_bitbang_mii_writereg(self, phy, reg, val)
3226 	struct device *self;
3227 	int phy, reg, val;
3228 {
3229 	struct tulip_softc *sc = (void *) self;
3230 
3231 	mii_bitbang_writereg(self, sc->sc_bitbang_ops, phy, reg, val);
3232 }
3233 
3234 /*
3235  * tlp_sio_mii_bitbang_read:
3236  *
3237  *	Read the MII serial port for the MII bit-bang module.
3238  */
3239 u_int32_t
3240 tlp_sio_mii_bitbang_read(self)
3241 	struct device *self;
3242 {
3243 	struct tulip_softc *sc = (void *) self;
3244 
3245 	return (TULIP_READ(sc, CSR_MIIROM));
3246 }
3247 
3248 /*
3249  * tlp_sio_mii_bitbang_write:
3250  *
3251  *	Write the MII serial port for the MII bit-bang module.
3252  */
3253 void
3254 tlp_sio_mii_bitbang_write(self, val)
3255 	struct device *self;
3256 	u_int32_t val;
3257 {
3258 	struct tulip_softc *sc = (void *) self;
3259 
3260 	TULIP_WRITE(sc, CSR_MIIROM, val);
3261 }
3262 
3263 /*
3264  * tlp_pnic_mii_readreg:
3265  *
3266  *	Read a PHY register on the Lite-On PNIC.
3267  */
3268 int
3269 tlp_pnic_mii_readreg(self, phy, reg)
3270 	struct device *self;
3271 	int phy, reg;
3272 {
3273 	struct tulip_softc *sc = (void *) self;
3274 	u_int32_t val;
3275 	int i;
3276 
3277 	TULIP_WRITE(sc, CSR_PNIC_MII,
3278 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
3279 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
3280 	    (reg << PNIC_MII_REGSHIFT));
3281 
3282 	for (i = 0; i < 1000; i++) {
3283 		delay(10);
3284 		val = TULIP_READ(sc, CSR_PNIC_MII);
3285 		if ((val & PNIC_MII_BUSY) == 0) {
3286 			if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
3287 				return (0);
3288 			else
3289 				return (val & PNIC_MII_DATA);
3290 		}
3291 	}
3292 	printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
3293 	return (0);
3294 }
3295 
3296 /*
3297  * tlp_pnic_mii_writereg:
3298  *
3299  *	Write a PHY register on the Lite-On PNIC.
3300  */
3301 void
3302 tlp_pnic_mii_writereg(self, phy, reg, val)
3303 	struct device *self;
3304 	int phy, reg, val;
3305 {
3306 	struct tulip_softc *sc = (void *) self;
3307 	int i;
3308 
3309 	TULIP_WRITE(sc, CSR_PNIC_MII,
3310 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
3311 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
3312 	    (reg << PNIC_MII_REGSHIFT) | val);
3313 
3314 	for (i = 0; i < 1000; i++) {
3315 		delay(10);
3316 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
3317 			return;
3318 	}
3319 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
3320 }
3321 
3322 const bus_addr_t tlp_al981_phy_regmap[] = {
3323 	CSR_ADM_BMCR,
3324 	CSR_ADM_BMSR,
3325 	CSR_ADM_PHYIDR1,
3326 	CSR_ADM_PHYIDR2,
3327 	CSR_ADM_ANAR,
3328 	CSR_ADM_ANLPAR,
3329 	CSR_ADM_ANER,
3330 
3331 	CSR_ADM_XMC,
3332 	CSR_ADM_XCIIS,
3333 	CSR_ADM_XIE,
3334 	CSR_ADM_100CTR,
3335 };
3336 const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
3337     sizeof(tlp_al981_phy_regmap[0]);
3338 
3339 /*
3340  * tlp_al981_mii_readreg:
3341  *
3342  *	Read a PHY register on the ADMtek AL981.
3343  */
3344 int
3345 tlp_al981_mii_readreg(self, phy, reg)
3346 	struct device *self;
3347 	int phy, reg;
3348 {
3349 	struct tulip_softc *sc = (struct tulip_softc *)self;
3350 
3351 	/* AL981 only has an internal PHY. */
3352 	if (phy != 0)
3353 		return (0);
3354 
3355 	if (reg >= tlp_al981_phy_regmap_size)
3356 		return (0);
3357 
3358 	return (bus_space_read_4(sc->sc_st, sc->sc_sh,
3359 	    tlp_al981_phy_regmap[reg]) & 0xffff);
3360 }
3361 
3362 /*
3363  * tlp_al981_mii_writereg:
3364  *
3365  *	Write a PHY register on the ADMtek AL981.
3366  */
3367 void
3368 tlp_al981_mii_writereg(self, phy, reg, val)
3369 	struct device *self;
3370 	int phy, reg, val;
3371 {
3372 	struct tulip_softc *sc = (struct tulip_softc *)self;
3373 
3374 	/* AL981 only has an internal PHY. */
3375 	if (phy != 0)
3376 		return;
3377 
3378 	if (reg >= tlp_al981_phy_regmap_size)
3379 		return;
3380 
3381 	bus_space_write_4(sc->sc_st, sc->sc_sh,
3382 	    tlp_al981_phy_regmap[reg], val);
3383 }
3384 
3385 /*****************************************************************************
3386  * Chip-specific pre-init and reset functions.
3387  *****************************************************************************/
3388 
3389 /*
3390  * tlp_2114x_preinit:
3391  *
3392  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
3393  */
3394 void
3395 tlp_2114x_preinit(sc)
3396 	struct tulip_softc *sc;
3397 {
3398 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3399 	struct tulip_21x4x_media *tm = ife->ifm_aux;
3400 
3401 	/*
3402 	 * Whether or not we're in MII or SIA/SYM mode, the media info
3403 	 * contains the appropriate OPMODE bits.
3404 	 *
3405 	 * Note that if we have no media info, we are are doing
3406 	 * non-MII `auto'.
3407 	 *
3408 	 * Also, we always set the Must-Be-One bit.
3409 	 */
3410 	if (tm == NULL) {
3411 #ifdef DIAGNOSTIC
3412 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
3413 			panic("tlp_2114x_preinit: not IFM_AUTO");
3414 		if (sc->sc_nway_active == NULL)
3415 			panic("tlp_2114x_preinit: nway_active NULL");
3416 #endif
3417 		tm = sc->sc_nway_active->ifm_aux;
3418 	}
3419 	sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode;
3420 
3421 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3422 }
3423 
3424 /*
3425  * tlp_2114x_mii_preinit:
3426  *
3427  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
3428  *	This version is used by boards which only have MII and don't have
3429  *	an ISV SROM.
3430  */
3431 void
3432 tlp_2114x_mii_preinit(sc)
3433 	struct tulip_softc *sc;
3434 {
3435 
3436 	/*
3437 	 * Always set the Must-Be-One bit, and Port Select (to select MII).
3438 	 * We'll never be called during a media change.
3439 	 */
3440 	sc->sc_opmode |= OPMODE_MBO|OPMODE_PS;
3441 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3442 }
3443 
3444 /*
3445  * tlp_pnic_preinit:
3446  *
3447  *	Pre-init function for the Lite-On 82c168 and 82c169.
3448  */
3449 void
3450 tlp_pnic_preinit(sc)
3451 	struct tulip_softc *sc;
3452 {
3453 
3454 	if (sc->sc_flags & TULIPF_HAS_MII) {
3455 		/*
3456 		 * MII case: just set the port-select bit; we will never
3457 		 * be called during a media change.
3458 		 */
3459 		sc->sc_opmode |= OPMODE_PS;
3460 	} else {
3461 		/*
3462 		 * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
3463 		 */
3464 		sc->sc_opmode |= OPMODE_PNIC_TBEN;
3465 	}
3466 }
3467 
3468 /*
3469  * tlp_dm9102_preinit:
3470  *
3471  *	Pre-init function for the Davicom DM9102.
3472  */
3473 void
3474 tlp_dm9102_preinit(sc)
3475 	struct tulip_softc *sc;
3476 {
3477 
3478 	switch (sc->sc_chip) {
3479 	case TULIP_CHIP_DM9102:
3480 		sc->sc_opmode |= OPMODE_MBO|OPMODE_HBD|OPMODE_PS;
3481 		break;
3482 
3483 	case TULIP_CHIP_DM9102A:
3484 		/*
3485 		 * XXX Figure out how to actually deal with the HomePNA
3486 		 * XXX portion of the DM9102A.
3487 		 */
3488 		sc->sc_opmode |= OPMODE_MBO|OPMODE_HBD;
3489 		break;
3490 
3491 	default:
3492 		/* Nothing. */
3493 	}
3494 
3495 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3496 }
3497 
3498 /*
3499  * tlp_21140_reset:
3500  *
3501  *	Issue a reset sequence on the 21140 via the GPIO facility.
3502  */
3503 void
3504 tlp_21140_reset(sc)
3505 	struct tulip_softc *sc;
3506 {
3507 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3508 	struct tulip_21x4x_media *tm = ife->ifm_aux;
3509 	int i;
3510 
3511 	/* First, set the direction on the GPIO pins. */
3512 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
3513 
3514 	/* Now, issue the reset sequence. */
3515 	for (i = 0; i < tm->tm_reset_length; i++) {
3516 		delay(10);
3517 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
3518 	}
3519 
3520 	/* Now, issue the selection sequence. */
3521 	for (i = 0; i < tm->tm_gp_length; i++) {
3522 		delay(10);
3523 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
3524 	}
3525 
3526 	/* If there were no sequences, just lower the pins. */
3527 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0)
3528 		TULIP_WRITE(sc, CSR_GPP, 0);
3529 }
3530 
3531 /*
3532  * tlp_21142_reset:
3533  *
3534  *	Issue a reset sequence on the 21142 via the GPIO facility.
3535  */
3536 void
3537 tlp_21142_reset(sc)
3538 	struct tulip_softc *sc;
3539 {
3540 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3541 	struct tulip_21x4x_media *tm = ife->ifm_aux;
3542 	const u_int8_t *ncp;
3543 	int i;
3544 
3545 	ncp = &sc->sc_srom[tm->tm_reset_offset];
3546 	for (i = 0; i < tm->tm_reset_length; i++, ncp += 2) {
3547 		delay(10);
3548 		TULIP_WRITE(sc, CSR_SIAGEN,
3549 		    TULIP_ROM_GETW(ncp, 0) << 16);
3550 	}
3551 
3552 	ncp = &sc->sc_srom[tm->tm_gp_offset];
3553 	for (i = 0; i < tm->tm_gp_length; i++, ncp += 2) {
3554 		delay(10);
3555 		TULIP_WRITE(sc, CSR_SIAGEN,
3556 		    TULIP_ROM_GETW(ncp, 0) << 16);
3557 	}
3558 
3559 	/* If there were no sequences, just lower the pins. */
3560 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
3561 		delay(10);
3562 		TULIP_WRITE(sc, CSR_SIAGEN, 0);
3563 	}
3564 }
3565 
3566 /*
3567  * tlp_pmac_reset:
3568  *
3569  *	Reset routine for Macronix chips.
3570  */
3571 void
3572 tlp_pmac_reset(sc)
3573 	struct tulip_softc *sc;
3574 {
3575 
3576 	switch (sc->sc_chip) {
3577 	case TULIP_CHIP_82C115:
3578 	case TULIP_CHIP_MX98715:
3579 	case TULIP_CHIP_MX98715A:
3580 	case TULIP_CHIP_MX98725:
3581 		/*
3582 		 * Set the LED operating mode.  This information is located
3583 		 * in the EEPROM at byte offset 0x77, per the MX98715A and
3584 		 * MX98725 application notes.
3585 		 */
3586 		TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
3587 		break;
3588 
3589 	default:
3590 		/* Nothing. */
3591 	}
3592 }
3593 
3594 /*
3595  * tlp_dm9102_reset:
3596  *
3597  *	Reset routine for the Davicom DM9102.
3598  */
3599 void
3600 tlp_dm9102_reset(sc)
3601 	struct tulip_softc *sc;
3602 {
3603 
3604 	TULIP_WRITE(sc, CSR_DM_PHYSTAT, DM_PHYSTAT_GEPC|DM_PHYSTAT_GPED);
3605 	delay(100);
3606 	TULIP_WRITE(sc, CSR_DM_PHYSTAT, 0);
3607 }
3608 
3609 /*****************************************************************************
3610  * Chip/board-specific media switches.  The ones here are ones that
3611  * are potentially common to multiple front-ends.
3612  *****************************************************************************/
3613 
3614 /*
3615  * This table is a common place for all sorts of media information,
3616  * keyed off of the SROM media code for that media.
3617  *
3618  * Note that we explicitly configure the 21142/21143 to always advertise
3619  * NWay capabilities when using the UTP port.
3620  * XXX Actually, we don't yet.
3621  */
3622 const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
3623 	{ TULIP_ROM_MB_MEDIA_TP,	IFM_10_T,	0,
3624 	  "10baseT",
3625 	  0,
3626 	  { SIACONN_21040_10BASET,
3627 	    SIATXRX_21040_10BASET,
3628 	    SIAGEN_21040_10BASET },
3629 
3630 	  { SIACONN_21041_10BASET,
3631 	    SIATXRX_21041_10BASET,
3632 	    SIAGEN_21041_10BASET },
3633 
3634 	  { SIACONN_21142_10BASET,
3635 	    SIATXRX_21142_10BASET,
3636 	    SIAGEN_21142_10BASET } },
3637 
3638 	{ TULIP_ROM_MB_MEDIA_BNC,	IFM_10_2,	0,
3639 	  "10base2",
3640 	  0,
3641 	  { 0,
3642 	    0,
3643 	    0 },
3644 
3645 	  { SIACONN_21041_BNC,
3646 	    SIATXRX_21041_BNC,
3647 	    SIAGEN_21041_BNC },
3648 
3649 	  { SIACONN_21142_BNC,
3650 	    SIATXRX_21142_BNC,
3651 	    SIAGEN_21142_BNC } },
3652 
3653 	{ TULIP_ROM_MB_MEDIA_AUI,	IFM_10_5,	0,
3654 	  "10base5",
3655 	  0,
3656 	  { SIACONN_21040_AUI,
3657 	    SIATXRX_21040_AUI,
3658 	    SIAGEN_21040_AUI },
3659 
3660 	  { SIACONN_21041_AUI,
3661 	    SIATXRX_21041_AUI,
3662 	    SIAGEN_21041_AUI },
3663 
3664 	  { SIACONN_21142_AUI,
3665 	    SIATXRX_21142_AUI,
3666 	    SIAGEN_21142_AUI } },
3667 
3668 	{ TULIP_ROM_MB_MEDIA_100TX,	IFM_100_TX,	0,
3669 	  "100baseTX",
3670 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
3671 	  { 0,
3672 	    0,
3673 	    0 },
3674 
3675 	  { 0,
3676 	    0,
3677 	    0 },
3678 
3679 	  { 0,
3680 	    0,
3681 	    SIAGEN_ABM } },
3682 
3683 	{ TULIP_ROM_MB_MEDIA_TP_FDX,	IFM_10_T,	IFM_FDX,
3684 	  "10baseT-FDX",
3685 	  OPMODE_FD|OPMODE_HBD,
3686 	  { SIACONN_21040_10BASET_FDX,
3687 	    SIATXRX_21040_10BASET_FDX,
3688 	    SIAGEN_21040_10BASET_FDX },
3689 
3690 	  { SIACONN_21041_10BASET_FDX,
3691 	    SIATXRX_21041_10BASET_FDX,
3692 	    SIAGEN_21041_10BASET_FDX },
3693 
3694 	  { SIACONN_21142_10BASET_FDX,
3695 	    SIATXRX_21142_10BASET_FDX,
3696 	    SIAGEN_21142_10BASET_FDX } },
3697 
3698 	{ TULIP_ROM_MB_MEDIA_100TX_FDX,	IFM_100_TX,	IFM_FDX,
3699 	  "100baseTX-FDX",
3700 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD|OPMODE_HBD,
3701 	  { 0,
3702 	    0,
3703 	    0 },
3704 
3705 	  { 0,
3706 	    0,
3707 	    0 },
3708 
3709 	  { 0,
3710 	    0,
3711 	    SIAGEN_ABM } },
3712 
3713 	{ TULIP_ROM_MB_MEDIA_100T4,	IFM_100_T4,	0,
3714 	  "100baseT4",
3715 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
3716 	  { 0,
3717 	    0,
3718 	    0 },
3719 
3720 	  { 0,
3721 	    0,
3722 	    0 },
3723 
3724 	  { 0,
3725 	    0,
3726 	    SIAGEN_ABM } },
3727 
3728 	{ TULIP_ROM_MB_MEDIA_100FX,	IFM_100_FX,	0,
3729 	  "100baseFX",
3730 	  OPMODE_PS|OPMODE_PCS|OPMODE_HBD,
3731 	  { 0,
3732 	    0,
3733 	    0 },
3734 
3735 	  { 0,
3736 	    0,
3737 	    0 },
3738 
3739 	  { 0,
3740 	    0,
3741 	    SIAGEN_ABM } },
3742 
3743 	{ TULIP_ROM_MB_MEDIA_100FX_FDX,	IFM_100_FX,	IFM_FDX,
3744 	  "100baseFX-FDX",
3745 	  OPMODE_PS|OPMODE_PCS|OPMODE_FD|OPMODE_HBD,
3746 	  { 0,
3747 	    0,
3748 	    0 },
3749 
3750 	  { 0,
3751 	    0,
3752 	    0 },
3753 
3754 	  { 0,
3755 	    0,
3756 	    SIAGEN_ABM } },
3757 
3758 	{ 0,				0,		0,
3759 	  NULL,
3760 	  0,
3761 	  { 0,
3762 	    0,
3763 	    0 },
3764 
3765 	  { 0,
3766 	    0,
3767 	    0 },
3768 
3769 	  { 0,
3770 	    0,
3771 	    0 } },
3772 };
3773 
3774 const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia __P((u_int8_t));
3775 void	tlp_srom_media_info __P((struct tulip_softc *,
3776 	    const struct tulip_srom_to_ifmedia *, struct tulip_21x4x_media *));
3777 void	tlp_add_srom_media __P((struct tulip_softc *, int,
3778 	    void (*)(struct tulip_softc *, struct ifmediareq *),
3779 	    int (*)(struct tulip_softc *), const u_int8_t *, int));
3780 void	tlp_print_media __P((struct tulip_softc *));
3781 void	tlp_nway_activate __P((struct tulip_softc *, int));
3782 void	tlp_get_minst __P((struct tulip_softc *));
3783 
3784 const struct tulip_srom_to_ifmedia *
3785 tlp_srom_to_ifmedia(sm)
3786 	u_int8_t sm;
3787 {
3788 	const struct tulip_srom_to_ifmedia *tsti;
3789 
3790 	for (tsti = tulip_srom_to_ifmedia_table;
3791 	     tsti->tsti_name != NULL; tsti++) {
3792 		if (tsti->tsti_srom == sm)
3793 			return (tsti);
3794 	}
3795 
3796 	return (NULL);
3797 }
3798 
3799 void
3800 tlp_srom_media_info(sc, tsti, tm)
3801 	struct tulip_softc *sc;
3802 	const struct tulip_srom_to_ifmedia *tsti;
3803 	struct tulip_21x4x_media *tm;
3804 {
3805 
3806 	tm->tm_name = tsti->tsti_name;
3807 	tm->tm_opmode = tsti->tsti_opmode;
3808 
3809 	switch (sc->sc_chip) {
3810 	case TULIP_CHIP_DE425:
3811 	case TULIP_CHIP_21040:
3812 		tm->tm_sia = tsti->tsti_21040;	/* struct assignment */
3813 		break;
3814 
3815 	case TULIP_CHIP_21041:
3816 		tm->tm_sia = tsti->tsti_21041;	/* struct assignment */
3817 		break;
3818 
3819 	case TULIP_CHIP_21142:
3820 	case TULIP_CHIP_21143:
3821 	case TULIP_CHIP_82C115:
3822 	case TULIP_CHIP_MX98715:
3823 	case TULIP_CHIP_MX98715A:
3824 	case TULIP_CHIP_MX98725:
3825 		tm->tm_sia = tsti->tsti_21142;	/* struct assignment */
3826 		break;
3827 
3828 	default:
3829 		/* Nothing. */
3830 	}
3831 }
3832 
3833 void
3834 tlp_add_srom_media(sc, type, get, set, list, cnt)
3835 	struct tulip_softc *sc;
3836 	int type;
3837 	void (*get) __P((struct tulip_softc *, struct ifmediareq *));
3838 	int (*set) __P((struct tulip_softc *));
3839 	const u_int8_t *list;
3840 	int cnt;
3841 {
3842 	struct tulip_21x4x_media *tm;
3843 	const struct tulip_srom_to_ifmedia *tsti;
3844 	int i;
3845 
3846 	for (i = 0; i < cnt; i++) {
3847 		tsti = tlp_srom_to_ifmedia(list[i]);
3848 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
3849 		memset(tm, 0, sizeof(*tm));
3850 		tlp_srom_media_info(sc, tsti, tm);
3851 		tm->tm_type = type;
3852 		tm->tm_get = get;
3853 		tm->tm_set = set;
3854 
3855 		ifmedia_add(&sc->sc_mii.mii_media,
3856 		    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
3857 		    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
3858 	}
3859 }
3860 
3861 void
3862 tlp_print_media(sc)
3863 	struct tulip_softc *sc;
3864 {
3865 	struct ifmedia_entry *ife;
3866 	struct tulip_21x4x_media *tm;
3867 	const char *sep = "";
3868 
3869 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
3870 
3871 	printf("%s: ", sc->sc_dev.dv_xname);
3872 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
3873 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
3874 		tm = ife->ifm_aux;
3875 		if (tm == NULL) {
3876 #ifdef DIAGNOSTIC
3877 			if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
3878 				panic("tlp_print_media");
3879 #endif
3880 			PRINT("auto");
3881 		} else if (tm->tm_type != TULIP_ROM_MB_21140_MII &&
3882 			   tm->tm_type != TULIP_ROM_MB_21142_MII) {
3883 			PRINT(tm->tm_name);
3884 		}
3885 	}
3886 	printf("\n");
3887 
3888 #undef PRINT
3889 }
3890 
3891 void
3892 tlp_nway_activate(sc, media)
3893 	struct tulip_softc *sc;
3894 	int media;
3895 {
3896 	struct ifmedia_entry *ife;
3897 
3898 	ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0);
3899 #ifdef DIAGNOSTIC
3900 	if (ife == NULL)
3901 		panic("tlp_nway_activate");
3902 #endif
3903 	sc->sc_nway_active = ife;
3904 }
3905 
3906 void
3907 tlp_get_minst(sc)
3908 	struct tulip_softc *sc;
3909 {
3910 
3911 	if ((sc->sc_media_seen &
3912 	    ~((1 << TULIP_ROM_MB_21140_MII) |
3913 	      (1 << TULIP_ROM_MB_21142_MII))) == 0) {
3914 		/*
3915 		 * We have not yet seen any SIA/SYM media (but are
3916 		 * about to; that's why we're called!), so assign
3917 		 * the current media instance to be the `internal media'
3918 		 * instance, and advance it so any MII media gets a
3919 		 * fresh one (used to selecting/isolating a PHY).
3920 		 */
3921 		sc->sc_tlp_minst = sc->sc_mii.mii_instance++;
3922 	}
3923 }
3924 
3925 /*
3926  * SIA Utility functions.
3927  */
3928 void	tlp_sia_update_link __P((struct tulip_softc *));
3929 void	tlp_sia_get __P((struct tulip_softc *, struct ifmediareq *));
3930 int	tlp_sia_set __P((struct tulip_softc *));
3931 void	tlp_sia_fixup __P((struct tulip_softc *));
3932 
3933 void
3934 tlp_sia_update_link(sc)
3935 	struct tulip_softc *sc;
3936 {
3937 	struct ifmedia_entry *ife;
3938 	struct tulip_21x4x_media *tm;
3939 	u_int32_t siastat;
3940 
3941 	ife = TULIP_CURRENT_MEDIA(sc);
3942 	tm = ife->ifm_aux;
3943 
3944 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
3945 
3946 	siastat = TULIP_READ(sc, CSR_SIASTAT);
3947 
3948 	/*
3949 	 * Note that when we do SIA link tests, we are assuming that
3950 	 * the chip is really in the mode that the current media setting
3951 	 * reflects.  If we're not, then the link tests will not be
3952 	 * accurate!
3953 	 */
3954 	switch (IFM_SUBTYPE(ife->ifm_media)) {
3955 	case IFM_10_T:
3956 		sc->sc_flags |= TULIPF_LINK_VALID;
3957 		if ((siastat & SIASTAT_LS10) == 0)
3958 			sc->sc_flags |= TULIPF_LINK_UP;
3959 		break;
3960 
3961 	case IFM_100_TX:
3962 	case IFM_100_T4:
3963 		sc->sc_flags |= TULIPF_LINK_VALID;
3964 		if ((siastat & SIASTAT_LS100) == 0)
3965 			sc->sc_flags |= TULIPF_LINK_UP;
3966 		break;
3967 	}
3968 
3969 	switch (sc->sc_chip) {
3970 	case TULIP_CHIP_21142:
3971 	case TULIP_CHIP_21143:
3972 		/*
3973 		 * On these chips, we can tell more information about
3974 		 * AUI/BNC.  Note that the AUI/BNC selection is made
3975 		 * in a different register; for our purpose, it's all
3976 		 * AUI.
3977 		 */
3978 		switch (IFM_SUBTYPE(ife->ifm_media)) {
3979 		case IFM_10_2:
3980 		case IFM_10_5:
3981 			sc->sc_flags |= TULIPF_LINK_VALID;
3982 			if (siastat & SIASTAT_ARA) {
3983 				TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA);
3984 				sc->sc_flags |= TULIPF_LINK_UP;
3985 			}
3986 			break;
3987 
3988 		default:
3989 			/*
3990 			 * If we're SYM media and can detect the link
3991 			 * via the GPIO facility, prefer that status
3992 			 * over LS100.
3993 			 */
3994 			if (tm->tm_type == TULIP_ROM_MB_21143_SYM &&
3995 			    tm->tm_actmask != 0) {
3996 				sc->sc_flags = (sc->sc_flags &
3997 				    ~TULIPF_LINK_UP) | TULIPF_LINK_VALID;
3998 				if (TULIP_ISSET(sc, CSR_SIAGEN,
3999 				    tm->tm_actmask) == tm->tm_actdata)
4000 					sc->sc_flags |= TULIPF_LINK_UP;
4001 			}
4002 		}
4003 		break;
4004 
4005 	default:
4006 		/* Nothing. */
4007 	}
4008 }
4009 
4010 void
4011 tlp_sia_get(sc, ifmr)
4012 	struct tulip_softc *sc;
4013 	struct ifmediareq *ifmr;
4014 {
4015 	struct ifmedia_entry *ife;
4016 
4017 	ifmr->ifm_status = 0;
4018 
4019 	tlp_sia_update_link(sc);
4020 
4021 	ife = TULIP_CURRENT_MEDIA(sc);
4022 
4023 	if (sc->sc_flags & TULIPF_LINK_VALID)
4024 		ifmr->ifm_status |= IFM_AVALID;
4025 	if (sc->sc_flags & TULIPF_LINK_UP)
4026 		ifmr->ifm_status |= IFM_ACTIVE;
4027 	ifmr->ifm_active = ife->ifm_media;
4028 }
4029 
4030 void
4031 tlp_sia_fixup(sc)
4032 	struct tulip_softc *sc;
4033 {
4034 	struct ifmedia_entry *ife;
4035 	struct tulip_21x4x_media *tm;
4036 	u_int32_t siaconn, siatxrx, siagen;
4037 
4038 	switch (sc->sc_chip) {
4039 	case TULIP_CHIP_82C115:
4040 	case TULIP_CHIP_MX98713A:
4041 	case TULIP_CHIP_MX98715:
4042 	case TULIP_CHIP_MX98715A:
4043 	case TULIP_CHIP_MX98725:
4044 		siaconn = PMAC_SIACONN_MASK;
4045 		siatxrx = PMAC_SIATXRX_MASK;
4046 		siagen  = PMAC_SIAGEN_MASK;
4047 		break;
4048 
4049 	default:
4050 		/* No fixups required on any other chips. */
4051 		return;
4052 	}
4053 
4054 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
4055 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
4056 		tm = ife->ifm_aux;
4057 		if (tm == NULL)
4058 			continue;
4059 
4060 		tm->tm_siaconn &= siaconn;
4061 		tm->tm_siatxrx &= siatxrx;
4062 		tm->tm_siagen  &= siagen;
4063 	}
4064 }
4065 
4066 int
4067 tlp_sia_set(sc)
4068 	struct tulip_softc *sc;
4069 {
4070 	struct ifmedia_entry *ife;
4071 	struct tulip_21x4x_media *tm;
4072 
4073 	ife = TULIP_CURRENT_MEDIA(sc);
4074 	tm = ife->ifm_aux;
4075 
4076 	/*
4077 	 * XXX This appears to be necessary on a bunch of the clone chips.
4078 	 */
4079 	delay(20000);
4080 
4081 	/*
4082 	 * Idle the chip.
4083 	 */
4084 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
4085 
4086 	/*
4087 	 * Program the SIA.  It's important to write in this order,
4088 	 * resetting the SIA first.
4089 	 */
4090 	TULIP_WRITE(sc, CSR_SIACONN, 0);		/* SRL bit clear */
4091 	delay(1000);
4092 
4093 	TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx);
4094 
4095 	switch (sc->sc_chip) {
4096 	case TULIP_CHIP_21142:
4097 	case TULIP_CHIP_21143:
4098 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl);
4099 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata);
4100 		break;
4101 	default:
4102 		TULIP_WRITE(sc, CSR_SIAGEN,  tm->tm_siagen);
4103 	}
4104 
4105 	TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn);
4106 
4107 	/*
4108 	 * Set the OPMODE bits for this media and write OPMODE.
4109 	 * This will resume the transmit and receive processes.
4110 	 */
4111 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
4112 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
4113 
4114 	return (0);
4115 }
4116 
4117 /*
4118  * 21140 GPIO utility functions.
4119  */
4120 void	tlp_21140_gpio_update_link __P((struct tulip_softc *));
4121 void	tlp_21140_gpio_get __P((struct tulip_softc *sc,
4122 	    struct ifmediareq *ifmr));
4123 int	tlp_21140_gpio_set __P((struct tulip_softc *sc));
4124 
4125 void
4126 tlp_21140_gpio_update_link(sc)
4127 	struct tulip_softc *sc;
4128 {
4129 	struct ifmedia_entry *ife;
4130 	struct tulip_21x4x_media *tm;
4131 
4132 	ife = TULIP_CURRENT_MEDIA(sc);
4133 	tm = ife->ifm_aux;
4134 
4135 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
4136 
4137 	if (tm->tm_actmask != 0) {
4138 		sc->sc_flags |= TULIPF_LINK_VALID;
4139 		if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
4140 		    tm->tm_actdata)
4141 			sc->sc_flags |= TULIPF_LINK_UP;
4142 	}
4143 }
4144 
4145 void
4146 tlp_21140_gpio_get(sc, ifmr)
4147 	struct tulip_softc *sc;
4148 	struct ifmediareq *ifmr;
4149 {
4150 	struct ifmedia_entry *ife;
4151 
4152 	ifmr->ifm_status = 0;
4153 
4154 	tlp_21140_gpio_update_link(sc);
4155 
4156 	ife = TULIP_CURRENT_MEDIA(sc);
4157 
4158 	if (sc->sc_flags & TULIPF_LINK_VALID)
4159 		ifmr->ifm_status |= IFM_AVALID;
4160 	if (sc->sc_flags & TULIPF_LINK_UP)
4161 		ifmr->ifm_status |= IFM_ACTIVE;
4162 	ifmr->ifm_active = ife->ifm_media;
4163 }
4164 
4165 int
4166 tlp_21140_gpio_set(sc)
4167 	struct tulip_softc *sc;
4168 {
4169 	struct ifmedia_entry *ife;
4170 	struct tulip_21x4x_media *tm;
4171 
4172 	ife = TULIP_CURRENT_MEDIA(sc);
4173 	tm = ife->ifm_aux;
4174 
4175 	/*
4176 	 * Idle the chip.
4177 	 */
4178 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
4179 
4180 	/*
4181 	 * Set the GPIO pins for this media, to flip any
4182 	 * relays, etc.
4183 	 */
4184 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
4185 	delay(10);
4186 	TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);
4187 
4188 	/*
4189 	 * Set the OPMODE bits for this media and write OPMODE.
4190 	 * This will resume the transmit and receive processes.
4191 	 */
4192 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
4193 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
4194 
4195 	return (0);
4196 }
4197 
4198 /*
4199  * 21040 and 21041 media switches.
4200  */
4201 void	tlp_21040_tmsw_init __P((struct tulip_softc *));
4202 void	tlp_21040_tp_tmsw_init __P((struct tulip_softc *));
4203 void	tlp_21040_auibnc_tmsw_init __P((struct tulip_softc *));
4204 void	tlp_21041_tmsw_init __P((struct tulip_softc *));
4205 
4206 const struct tulip_mediasw tlp_21040_mediasw = {
4207 	tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set
4208 };
4209 
4210 const struct tulip_mediasw tlp_21040_tp_mediasw = {
4211 	tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set
4212 };
4213 
4214 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
4215 	tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set
4216 };
4217 
4218 const struct tulip_mediasw tlp_21041_mediasw = {
4219 	tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set
4220 };
4221 
4222 
4223 void
4224 tlp_21040_tmsw_init(sc)
4225 	struct tulip_softc *sc;
4226 {
4227 	static const u_int8_t media[] = {
4228 		TULIP_ROM_MB_MEDIA_TP,
4229 		TULIP_ROM_MB_MEDIA_TP_FDX,
4230 		TULIP_ROM_MB_MEDIA_AUI,
4231 	};
4232 	struct tulip_21x4x_media *tm;
4233 
4234 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4235 	    tlp_mediastatus);
4236 
4237 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 3);
4238 
4239 	/*
4240 	 * No SROM type for External SIA.
4241 	 */
4242 	tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
4243 	memset(tm, 0, sizeof(*tm));
4244 	tm->tm_name = "manual";
4245 	tm->tm_opmode = 0;
4246 	tm->tm_siaconn = SIACONN_21040_EXTSIA;
4247 	tm->tm_siatxrx = SIATXRX_21040_EXTSIA;
4248 	tm->tm_siagen  = SIAGEN_21040_EXTSIA;
4249 	ifmedia_add(&sc->sc_mii.mii_media,
4250 	    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm);
4251 
4252 	/*
4253 	 * XXX Autosense not yet supported.
4254 	 */
4255 
4256 	/* XXX This should be auto-sense. */
4257 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
4258 
4259 	tlp_print_media(sc);
4260 }
4261 
4262 void
4263 tlp_21040_tp_tmsw_init(sc)
4264 	struct tulip_softc *sc;
4265 {
4266 	static const u_int8_t media[] = {
4267 		TULIP_ROM_MB_MEDIA_TP,
4268 		TULIP_ROM_MB_MEDIA_TP_FDX,
4269 	};
4270 
4271 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4272 	    tlp_mediastatus);
4273 
4274 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 2);
4275 
4276 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
4277 
4278 	tlp_print_media(sc);
4279 }
4280 
4281 void
4282 tlp_21040_auibnc_tmsw_init(sc)
4283 	struct tulip_softc *sc;
4284 {
4285 	static const u_int8_t media[] = {
4286 		TULIP_ROM_MB_MEDIA_AUI,
4287 	};
4288 
4289 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4290 	    tlp_mediastatus);
4291 
4292 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 1);
4293 
4294 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
4295 
4296 	tlp_print_media(sc);
4297 }
4298 
4299 void
4300 tlp_21041_tmsw_init(sc)
4301 	struct tulip_softc *sc;
4302 {
4303 	static const u_int8_t media[] = {
4304 		TULIP_ROM_MB_MEDIA_TP,
4305 		TULIP_ROM_MB_MEDIA_TP_FDX,
4306 		TULIP_ROM_MB_MEDIA_BNC,
4307 		TULIP_ROM_MB_MEDIA_AUI,
4308 	};
4309 	int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
4310 	const struct tulip_srom_to_ifmedia *tsti;
4311 	struct tulip_21x4x_media *tm;
4312 	u_int16_t romdef;
4313 	u_int8_t mb;
4314 
4315 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4316 	    tlp_mediastatus);
4317 
4318 	if (tlp_isv_srom(sc->sc_srom) == 0) {
4319  not_isv_srom:
4320 		/*
4321 		 * If we have a board without the standard 21041 SROM format,
4322 		 * we just assume all media are present and try and pick a
4323 		 * reasonable default.
4324 		 */
4325 		tlp_add_srom_media(sc, 0, NULL, NULL, media, 4);
4326 
4327 		/*
4328 		 * XXX Autosense not yet supported.
4329 		 */
4330 
4331 		/* XXX This should be auto-sense. */
4332 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
4333 
4334 		tlp_print_media(sc);
4335 		return;
4336 	}
4337 
4338 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
4339 	for (i = 0; i < devcnt; i++) {
4340 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
4341 			break;
4342 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
4343 		    sc->sc_devno)
4344 			break;
4345 	}
4346 
4347 	if (i == devcnt)
4348 		goto not_isv_srom;
4349 
4350 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
4351 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
4352 	mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
4353 	m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
4354 
4355 	for (; m_cnt != 0;
4356 	     m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
4357 		mb = sc->sc_srom[mb_offset];
4358 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
4359 		memset(tm, 0, sizeof(*tm));
4360 		switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
4361 		case TULIP_ROM_MB_MEDIA_TP_FDX:
4362 		case TULIP_ROM_MB_MEDIA_TP:
4363 		case TULIP_ROM_MB_MEDIA_BNC:
4364 		case TULIP_ROM_MB_MEDIA_AUI:
4365 			tsti = tlp_srom_to_ifmedia(mb &
4366 			    TULIP_ROM_MB_MEDIA_CODE);
4367 
4368 			tlp_srom_media_info(sc, tsti, tm);
4369 
4370 			/*
4371 			 * Override our default SIA settings if the
4372 			 * SROM contains its own.
4373 			 */
4374 			if (mb & TULIP_ROM_MB_EXT) {
4375 				tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom,
4376 				    mb_offset + TULIP_ROM_MB_CSR13);
4377 				tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom,
4378 				    mb_offset + TULIP_ROM_MB_CSR14);
4379 				tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom,
4380 				    mb_offset + TULIP_ROM_MB_CSR15);
4381 			}
4382 
4383 			ifmedia_add(&sc->sc_mii.mii_media,
4384 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4385 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4386 			break;
4387 
4388 		default:
4389 			printf("%s: unknown media code 0x%02x\n",
4390 			    sc->sc_dev.dv_xname,
4391 			    mb & TULIP_ROM_MB_MEDIA_CODE);
4392 			free(tm, M_DEVBUF);
4393 		}
4394 	}
4395 
4396 	/*
4397 	 * XXX Autosense not yet supported.
4398 	 */
4399 
4400 	romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
4401 	    TULIP_ROM_IL_SELECT_CONN_TYPE);
4402 	switch (romdef) {
4403 	case SELECT_CONN_TYPE_TP:
4404 	case SELECT_CONN_TYPE_TP_AUTONEG:
4405 	case SELECT_CONN_TYPE_TP_NOLINKPASS:
4406 		defmedia = IFM_ETHER|IFM_10_T;
4407 		break;
4408 
4409 	case SELECT_CONN_TYPE_TP_FDX:
4410 		defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
4411 		break;
4412 
4413 	case SELECT_CONN_TYPE_BNC:
4414 		defmedia = IFM_ETHER|IFM_10_2;
4415 		break;
4416 
4417 	case SELECT_CONN_TYPE_AUI:
4418 		defmedia = IFM_ETHER|IFM_10_5;
4419 		break;
4420 #if 0 /* XXX */
4421 	case SELECT_CONN_TYPE_ASENSE:
4422 	case SELECT_CONN_TYPE_ASENSE_AUTONEG:
4423 		defmedia = IFM_ETHER|IFM_AUTO;
4424 		break;
4425 #endif
4426 	default:
4427 		defmedia = 0;
4428 	}
4429 
4430 	if (defmedia == 0) {
4431 		/*
4432 		 * XXX We should default to auto-sense.
4433 		 */
4434 		defmedia = IFM_ETHER|IFM_10_T;
4435 	}
4436 
4437 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
4438 
4439 	tlp_print_media(sc);
4440 }
4441 
4442 /*
4443  * DECchip 2114x ISV media switch.
4444  */
4445 void	tlp_2114x_isv_tmsw_init __P((struct tulip_softc *));
4446 void	tlp_2114x_isv_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
4447 int	tlp_2114x_isv_tmsw_set __P((struct tulip_softc *));
4448 
4449 const struct tulip_mediasw tlp_2114x_isv_mediasw = {
4450 	tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
4451 };
4452 
4453 void
4454 tlp_2114x_isv_tmsw_init(sc)
4455 	struct tulip_softc *sc;
4456 {
4457 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
4458 	struct ifmedia_entry *ife;
4459 	struct mii_softc *phy;
4460 	struct tulip_21x4x_media *tm;
4461 	const struct tulip_srom_to_ifmedia *tsti;
4462 	int i, devcnt, leaf_offset, m_cnt, type, length;
4463 	int defmedia, miidef;
4464 	u_int16_t word;
4465 	u_int8_t *cp, *ncp;
4466 
4467 	defmedia = miidef = 0;
4468 
4469 	sc->sc_mii.mii_ifp = ifp;
4470 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
4471 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
4472 	sc->sc_mii.mii_statchg = sc->sc_statchg;
4473 
4474 	/*
4475 	 * Ignore `instance'; we may get a mixture of SIA and MII
4476 	 * media, and `instance' is used to isolate or select the
4477 	 * PHY on the MII as appropriate.  Note that duplicate media
4478 	 * are disallowed, so ignoring `instance' is safe.
4479 	 */
4480 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, tlp_mediachange,
4481 	    tlp_mediastatus);
4482 
4483 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
4484 	for (i = 0; i < devcnt; i++) {
4485 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
4486 			break;
4487 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
4488 		    sc->sc_devno)
4489 			break;
4490 	}
4491 
4492 	if (i == devcnt) {
4493 		printf("%s: unable to locate info leaf in SROM\n",
4494 		    sc->sc_dev.dv_xname);
4495 		return;
4496 	}
4497 
4498 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
4499 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
4500 
4501 	/* XXX SELECT CONN TYPE */
4502 
4503 	cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
4504 
4505 	/*
4506 	 * On some chips, the first thing in the Info Leaf is the
4507 	 * GPIO pin direction data.
4508 	 */
4509 	switch (sc->sc_chip) {
4510 	case TULIP_CHIP_21140:
4511 	case TULIP_CHIP_21140A:
4512 	case TULIP_CHIP_MX98713:
4513 	case TULIP_CHIP_AX88140:
4514 	case TULIP_CHIP_AX88141:
4515 		sc->sc_gp_dir = *cp++;
4516 		break;
4517 
4518 	default:
4519 		/* Nothing. */
4520 	}
4521 
4522 	/* Get the media count. */
4523 	m_cnt = *cp++;
4524 
4525 	for (; m_cnt != 0; cp = ncp, m_cnt--) {
4526 		/*
4527 		 * Determine the type and length of this media block.
4528 		 */
4529 		if ((*cp & 0x80) == 0) {
4530 			length = 4;
4531 			type = TULIP_ROM_MB_21140_GPR;
4532 		} else {
4533 			length = (*cp++ & 0x7f) - 1;
4534 			type = *cp++ & 0x3f;
4535 		}
4536 
4537 		/* Compute the start of the next block. */
4538 		ncp = cp + length;
4539 
4540 		/* Now, parse the block. */
4541 		switch (type) {
4542 		case TULIP_ROM_MB_21140_GPR:
4543 			tlp_get_minst(sc);
4544 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR;
4545 
4546 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
4547 			memset(tm, 0, sizeof(*tm));
4548 
4549 			tm->tm_type = TULIP_ROM_MB_21140_GPR;
4550 			tm->tm_get = tlp_21140_gpio_get;
4551 			tm->tm_set = tlp_21140_gpio_set;
4552 
4553 			/* First is the media type code. */
4554 			tsti = tlp_srom_to_ifmedia(cp[0] &
4555 			    TULIP_ROM_MB_MEDIA_CODE);
4556 			if (tsti == NULL) {
4557 				/* Invalid media code. */
4558 				free(tm, M_DEVBUF);
4559 				break;
4560 			}
4561 
4562 			/* Get defaults. */
4563 			tlp_srom_media_info(sc, tsti, tm);
4564 
4565 			/* Next is any GPIO info for this media. */
4566 			tm->tm_gpdata = cp[1];
4567 
4568 			/*
4569 			 * Next is a word containing OPMODE information
4570 			 * and info on how to detect if this media is
4571 			 * active.
4572 			 */
4573 			word = TULIP_ROM_GETW(cp, 2);
4574 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
4575 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
4576 				tm->tm_actmask =
4577 				    TULIP_ROM_MB_BITPOS(word);
4578 				tm->tm_actdata =
4579 				    (word & TULIP_ROM_MB_POLARITY) ?
4580 				    0 : tm->tm_actmask;
4581 			}
4582 
4583 			ifmedia_add(&sc->sc_mii.mii_media,
4584 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4585 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4586 			break;
4587 
4588 		case TULIP_ROM_MB_21140_MII:
4589 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII;
4590 
4591 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
4592 			memset(tm, 0, sizeof(*tm));
4593 
4594 			tm->tm_type = TULIP_ROM_MB_21140_MII;
4595 			tm->tm_get = tlp_mii_getmedia;
4596 			tm->tm_set = tlp_mii_setmedia;
4597 			tm->tm_opmode = OPMODE_PS;
4598 
4599 			if (sc->sc_reset == NULL)
4600 				sc->sc_reset = tlp_21140_reset;
4601 
4602 			/* First is the PHY number. */
4603 			tm->tm_phyno = *cp++;
4604 
4605 			/* Next is the MII select sequence length and offset. */
4606 			tm->tm_gp_length = *cp++;
4607 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
4608 			cp += tm->tm_gp_length;
4609 
4610 			/* Next is the MII reset sequence length and offset. */
4611 			tm->tm_reset_length = *cp++;
4612 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
4613 			cp += tm->tm_reset_length;
4614 
4615 			/*
4616 			 * The following items are left in the media block
4617 			 * that we don't particularly care about:
4618 			 *
4619 			 *	capabilities		W
4620 			 *	advertisement		W
4621 			 *	full duplex		W
4622 			 *	tx threshold		W
4623 			 *
4624 			 * These appear to be bits in the PHY registers,
4625 			 * which our MII code handles on its own.
4626 			 */
4627 
4628 			/*
4629 			 * Before we probe the MII bus, we need to reset
4630 			 * it and issue the selection sequence.
4631 			 */
4632 
4633 			/* Set the direction of the pins... */
4634 			TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
4635 
4636 			for (i = 0; i < tm->tm_reset_length; i++) {
4637 				delay(10);
4638 				TULIP_WRITE(sc, CSR_GPP,
4639 				    sc->sc_srom[tm->tm_reset_offset + i]);
4640 			}
4641 
4642 			for (i = 0; i < tm->tm_gp_length; i++) {
4643 				delay(10);
4644 				TULIP_WRITE(sc, CSR_GPP,
4645 				    sc->sc_srom[tm->tm_gp_offset + i]);
4646 			}
4647 
4648 			/* If there were no sequences, just lower the pins. */
4649 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
4650 				delay(10);
4651 				TULIP_WRITE(sc, CSR_GPP, 0);
4652 			}
4653 
4654 			/*
4655 			 * Now, probe the MII for the PHY.  Note, we know
4656 			 * the location of the PHY on the bus, but we don't
4657 			 * particularly care; the MII code just likes to
4658 			 * search the whole thing anyhow.
4659 			 */
4660 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
4661 			    MII_PHY_ANY, tm->tm_phyno, 0);
4662 
4663 			/*
4664 			 * Now, search for the PHY we hopefully just
4665 			 * configured.  If it's not configured into the
4666 			 * kernel, we lose.  The PHY's default media always
4667 			 * takes priority.
4668 			 */
4669 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
4670 			     phy != NULL;
4671 			     phy = LIST_NEXT(phy, mii_list))
4672 				if (phy->mii_offset == tm->tm_phyno)
4673 					break;
4674 			if (phy == NULL) {
4675 				printf("%s: unable to configure MII\n",
4676 				    sc->sc_dev.dv_xname);
4677 				break;
4678 			}
4679 
4680 			sc->sc_flags |= TULIPF_HAS_MII;
4681 			sc->sc_tick = tlp_mii_tick;
4682 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
4683 			    phy->mii_inst);
4684 
4685 			/*
4686 			 * Okay, now that we've found the PHY and the MII
4687 			 * layer has added all of the media associated
4688 			 * with that PHY, we need to traverse the media
4689 			 * list, and add our `tm' to each entry's `aux'
4690 			 * pointer.
4691 			 *
4692 			 * We do this by looking for media with our
4693 			 * PHY's `instance'.
4694 			 */
4695 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
4696 			     ife != NULL;
4697 			     ife = TAILQ_NEXT(ife, ifm_list)) {
4698 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
4699 					continue;
4700 				ife->ifm_aux = tm;
4701 			}
4702 			break;
4703 
4704 		case TULIP_ROM_MB_21142_SIA:
4705 			tlp_get_minst(sc);
4706 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA;
4707 
4708 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
4709 			memset(tm, 0, sizeof(*tm));
4710 
4711 			tm->tm_type = TULIP_ROM_MB_21142_SIA;
4712 			tm->tm_get = tlp_sia_get;
4713 			tm->tm_set = tlp_sia_set;
4714 
4715 			/* First is the media type code. */
4716 			tsti = tlp_srom_to_ifmedia(cp[0] &
4717 			    TULIP_ROM_MB_MEDIA_CODE);
4718 			if (tsti == NULL) {
4719 				/* Invalid media code. */
4720 				free(tm, M_DEVBUF);
4721 				break;
4722 			}
4723 
4724 			/* Get defaults. */
4725 			tlp_srom_media_info(sc, tsti, tm);
4726 
4727 			/*
4728 			 * Override our default SIA settings if the
4729 			 * SROM contains its own.
4730 			 */
4731 			if (cp[0] & 0x40) {
4732 				tm->tm_siaconn = TULIP_ROM_GETW(cp, 1);
4733 				tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3);
4734 				tm->tm_siagen  = TULIP_ROM_GETW(cp, 5);
4735 				cp += 7;
4736 			} else
4737 				cp++;
4738 
4739 			/* Next is GPIO control/data. */
4740 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 0);
4741 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 2);
4742 
4743 			ifmedia_add(&sc->sc_mii.mii_media,
4744 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4745 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4746 			break;
4747 
4748 		case TULIP_ROM_MB_21142_MII:
4749 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_MII;
4750 
4751 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
4752 			memset(tm, 0, sizeof(*tm));
4753 
4754 			tm->tm_type = TULIP_ROM_MB_21142_MII;
4755 			tm->tm_get = tlp_mii_getmedia;
4756 			tm->tm_set = tlp_mii_setmedia;
4757 			tm->tm_opmode = OPMODE_PS;
4758 
4759 			if (sc->sc_reset == NULL)
4760 				sc->sc_reset = tlp_21142_reset;
4761 
4762 			/* First is the PHY number. */
4763 			tm->tm_phyno = *cp++;
4764 
4765 			/* Next is the MII select sequence length and offset. */
4766 			tm->tm_gp_length = *cp++;
4767 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
4768 			cp += tm->tm_gp_length * 2;
4769 
4770 			/* Next is the MII reset sequence length and offset. */
4771 			tm->tm_reset_length = *cp++;
4772 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
4773 			cp += tm->tm_reset_length * 2;
4774 
4775 			/*
4776 			 * The following items are left in the media block
4777 			 * that we don't particularly care about:
4778 			 *
4779 			 *	capabilities		W
4780 			 *	advertisement		W
4781 			 *	full duplex		W
4782 			 *	tx threshold		W
4783 			 *	MII interrupt		W
4784 			 *
4785 			 * These appear to be bits in the PHY registers,
4786 			 * which our MII code handles on its own.
4787 			 */
4788 
4789 			/*
4790 			 * Before we probe the MII bus, we need to reset
4791 			 * it and issue the selection sequence.
4792 			 */
4793 
4794 			ncp = &sc->sc_srom[tm->tm_reset_offset];
4795 			for (i = 0; i < tm->tm_reset_length; i++, ncp += 2) {
4796 				delay(10);
4797 				TULIP_WRITE(sc, CSR_SIAGEN,
4798 				    TULIP_ROM_GETW(ncp, 0) << 16);
4799 			}
4800 
4801 			ncp = &sc->sc_srom[tm->tm_gp_offset];
4802 			for (i = 0; i < tm->tm_gp_length; i++, ncp += 2) {
4803 				delay(10);
4804 				TULIP_WRITE(sc, CSR_SIAGEN,
4805 				    TULIP_ROM_GETW(ncp, 0) << 16);
4806 			}
4807 
4808 			/* If there were no sequences, just lower the pins. */
4809 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
4810 				delay(10);
4811 				TULIP_WRITE(sc, CSR_SIAGEN, 0);
4812 			}
4813 
4814 			/*
4815 			 * Now, probe the MII for the PHY.  Note, we know
4816 			 * the location of the PHY on the bus, but we don't
4817 			 * particularly care; the MII code just likes to
4818 			 * search the whole thing anyhow.
4819 			 */
4820 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
4821 			    MII_PHY_ANY, tm->tm_phyno, 0);
4822 
4823 			/*
4824 			 * Now, search for the PHY we hopefully just
4825 			 * configured.  If it's not configured into the
4826 			 * kernel, we lose.  The PHY's default media always
4827 			 * takes priority.
4828 			 */
4829 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
4830 			     phy != NULL;
4831 			     phy = LIST_NEXT(phy, mii_list))
4832 				if (phy->mii_offset == tm->tm_phyno)
4833 					break;
4834 			if (phy == NULL) {
4835 				printf("%s: unable to configure MII\n",
4836 				    sc->sc_dev.dv_xname);
4837 				break;
4838 			}
4839 
4840 			sc->sc_flags |= TULIPF_HAS_MII;
4841 			sc->sc_tick = tlp_mii_tick;
4842 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
4843 			    phy->mii_inst);
4844 
4845 			/*
4846 			 * Okay, now that we've found the PHY and the MII
4847 			 * layer has added all of the media associated
4848 			 * with that PHY, we need to traverse the media
4849 			 * list, and add our `tm' to each entry's `aux'
4850 			 * pointer.
4851 			 *
4852 			 * We do this by looking for media with our
4853 			 * PHY's `instance'.
4854 			 */
4855 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
4856 			     ife != NULL;
4857 			     ife = TAILQ_NEXT(ife, ifm_list)) {
4858 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
4859 					continue;
4860 				ife->ifm_aux = tm;
4861 			}
4862 			break;
4863 
4864 		case TULIP_ROM_MB_21143_SYM:
4865 			tlp_get_minst(sc);
4866 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM;
4867 
4868 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
4869 			memset(tm, 0, sizeof(*tm));
4870 
4871 			tm->tm_type = TULIP_ROM_MB_21143_SYM;
4872 			tm->tm_get = tlp_sia_get;
4873 			tm->tm_set = tlp_sia_set;
4874 
4875 			/* First is the media type code. */
4876 			tsti = tlp_srom_to_ifmedia(cp[0] &
4877 			    TULIP_ROM_MB_MEDIA_CODE);
4878 			if (tsti == NULL) {
4879 				/* Invalid media code. */
4880 				free(tm, M_DEVBUF);
4881 				break;
4882 			}
4883 
4884 			/* Get defaults. */
4885 			tlp_srom_media_info(sc, tsti, tm);
4886 
4887 			/* Next is GPIO control/data. */
4888 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 1);
4889 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 3);
4890 
4891 			/*
4892 			 * Next is a word containing OPMODE information
4893 			 * and info on how to detect if this media is
4894 			 * active.
4895 			 */
4896 			word = TULIP_ROM_GETW(cp, 5);
4897 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
4898 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
4899 				tm->tm_actmask =
4900 				    TULIP_ROM_MB_BITPOS(word);
4901 				tm->tm_actdata =
4902 				    (word & TULIP_ROM_MB_POLARITY) ?
4903 				    0 : tm->tm_actmask;
4904 			}
4905 
4906 			ifmedia_add(&sc->sc_mii.mii_media,
4907 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4908 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4909 			break;
4910 
4911 		case TULIP_ROM_MB_21143_RESET:
4912 			printf("%s: 21143 reset block\n", sc->sc_dev.dv_xname);
4913 			break;
4914 
4915 		default:
4916 			printf("%s: unknown ISV media block type 0x%02x\n",
4917 			    sc->sc_dev.dv_xname, type);
4918 		}
4919 	}
4920 
4921 	/*
4922 	 * Deal with the case where no media is configured.
4923 	 */
4924 	if (TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list) == NULL) {
4925 		printf("%s: no media found!\n", sc->sc_dev.dv_xname);
4926 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
4927 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
4928 		return;
4929 	}
4930 
4931 	/*
4932 	 * Pick the default media.
4933 	 */
4934 	if (miidef != 0)
4935 		defmedia = miidef;
4936 	else {
4937 		/*
4938 		 * XXX Pick a better default.  Should come from SROM
4939 		 * XXX on 21140[A], and should be "auto" on 21142,
4940 		 * XXX 21143, and Macronix chips.
4941 		 */
4942 		defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
4943 	}
4944 
4945 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
4946 
4947 	/*
4948 	 * Display any non-MII media we've located.
4949 	 */
4950 	if (sc->sc_media_seen &
4951 	    ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII)))
4952 		tlp_print_media(sc);
4953 
4954 	tlp_sia_fixup(sc);
4955 }
4956 
4957 void
4958 tlp_2114x_isv_tmsw_get(sc, ifmr)
4959 	struct tulip_softc *sc;
4960 	struct ifmediareq *ifmr;
4961 {
4962 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
4963 	struct tulip_21x4x_media *tm = ife->ifm_aux;
4964 
4965 	/*
4966 	 * We might be polling a non-MII autosense; check for that.
4967 	 */
4968 	if (tm == NULL) {
4969 #ifdef DIAGNOSTIC
4970 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
4971 			panic("tlp_2114x_isv_tmsw_get");
4972 #endif
4973 		tm = sc->sc_nway_active->ifm_aux;
4974 	}
4975 
4976 	(*tm->tm_get)(sc, ifmr);
4977 }
4978 
4979 int
4980 tlp_2114x_isv_tmsw_set(sc)
4981 	struct tulip_softc *sc;
4982 {
4983 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
4984 	struct tulip_21x4x_media *tm = ife->ifm_aux;
4985 
4986 	/*
4987 	 * We might be setting a non-MII autosense; check for that.
4988 	 */
4989 	if (tm == NULL) {
4990 #ifdef DIAGNOSTIC
4991 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
4992 			panic("tlp_2114x_isv_tmsw_set");
4993 #endif
4994 		/* XXX XXX XXX */
4995 	}
4996 
4997 	/*
4998 	 * Check to see if we need to reset the chip, and do it.  The
4999 	 * reset path will get the OPMODE register right the next
5000 	 * time through.
5001 	 */
5002 	if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode))
5003 		return (tlp_init(sc));
5004 
5005 	return ((*tm->tm_set)(sc));
5006 }
5007 
5008 /*
5009  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
5010  */
5011 void	tlp_sio_mii_tmsw_init __P((struct tulip_softc *));
5012 
5013 const struct tulip_mediasw tlp_sio_mii_mediasw = {
5014 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
5015 };
5016 
5017 void
5018 tlp_sio_mii_tmsw_init(sc)
5019 	struct tulip_softc *sc;
5020 {
5021 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5022 
5023 	/*
5024 	 * We don't attach any media info structures to the ifmedia
5025 	 * entries, so if we're using a pre-init function that needs
5026 	 * that info, override it to one that doesn't.
5027 	 */
5028 	if (sc->sc_preinit == tlp_2114x_preinit)
5029 		sc->sc_preinit = tlp_2114x_mii_preinit;
5030 
5031 	sc->sc_mii.mii_ifp = ifp;
5032 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
5033 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
5034 	sc->sc_mii.mii_statchg = sc->sc_statchg;
5035 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
5036 	    tlp_mediastatus);
5037 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
5038 	    MII_OFFSET_ANY, 0);
5039 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
5040 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
5041 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
5042 	} else {
5043 		sc->sc_flags |= TULIPF_HAS_MII;
5044 		sc->sc_tick = tlp_mii_tick;
5045 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
5046 	}
5047 }
5048 
5049 /*
5050  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
5051  */
5052 void	tlp_pnic_tmsw_init __P((struct tulip_softc *));
5053 void	tlp_pnic_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
5054 int	tlp_pnic_tmsw_set __P((struct tulip_softc *));
5055 
5056 const struct tulip_mediasw tlp_pnic_mediasw = {
5057 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
5058 };
5059 
5060 void	tlp_pnic_nway_statchg __P((struct device *));
5061 void	tlp_pnic_nway_tick __P((void *));
5062 int	tlp_pnic_nway_service __P((struct tulip_softc *, int));
5063 void	tlp_pnic_nway_reset __P((struct tulip_softc *));
5064 int	tlp_pnic_nway_auto __P((struct tulip_softc *, int));
5065 void	tlp_pnic_nway_auto_timeout __P((void *));
5066 void	tlp_pnic_nway_status __P((struct tulip_softc *));
5067 void	tlp_pnic_nway_acomp __P((struct tulip_softc *));
5068 
5069 void
5070 tlp_pnic_tmsw_init(sc)
5071 	struct tulip_softc *sc;
5072 {
5073 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5074 	const char *sep = "";
5075 
5076 #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
5077 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
5078 
5079 	sc->sc_mii.mii_ifp = ifp;
5080 	sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
5081 	sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
5082 	sc->sc_mii.mii_statchg = sc->sc_statchg;
5083 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
5084 	    tlp_mediastatus);
5085 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
5086 	    MII_OFFSET_ANY, 0);
5087 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
5088 		/* XXX What about AUI/BNC support? */
5089 		printf("%s: ", sc->sc_dev.dv_xname);
5090 
5091 		tlp_pnic_nway_reset(sc);
5092 
5093 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
5094 		    PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
5095 		PRINT("10baseT");
5096 
5097 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
5098 		    PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
5099 		PRINT("10baseT-FDX");
5100 
5101 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
5102 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
5103 		PRINT("100baseTX");
5104 
5105 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
5106 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
5107 		    PNIC_NWAY_CAP100TXFDX);
5108 		PRINT("100baseTX-FDX");
5109 
5110 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
5111 		    PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
5112 		    PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
5113 		    PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
5114 		PRINT("auto");
5115 
5116 		printf("\n");
5117 
5118 		sc->sc_statchg = tlp_pnic_nway_statchg;
5119 		sc->sc_tick = tlp_pnic_nway_tick;
5120 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
5121 	} else {
5122 		sc->sc_flags |= TULIPF_HAS_MII;
5123 		sc->sc_tick = tlp_mii_tick;
5124 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
5125 	}
5126 
5127 #undef ADD
5128 #undef PRINT
5129 }
5130 
5131 void
5132 tlp_pnic_tmsw_get(sc, ifmr)
5133 	struct tulip_softc *sc;
5134 	struct ifmediareq *ifmr;
5135 {
5136 	struct mii_data *mii = &sc->sc_mii;
5137 
5138 	if (sc->sc_flags & TULIPF_HAS_MII)
5139 		tlp_mii_getmedia(sc, ifmr);
5140 	else {
5141 		mii->mii_media_status = 0;
5142 		mii->mii_media_active = IFM_NONE;
5143 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
5144 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
5145 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
5146 	}
5147 }
5148 
5149 int
5150 tlp_pnic_tmsw_set(sc)
5151 	struct tulip_softc *sc;
5152 {
5153 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5154 	struct mii_data *mii = &sc->sc_mii;
5155 
5156 	if (sc->sc_flags & TULIPF_HAS_MII) {
5157 		/*
5158 		 * Make sure the built-in Tx jabber timer is disabled.
5159 		 */
5160 		TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
5161 
5162 		return (tlp_mii_setmedia(sc));
5163 	}
5164 
5165 	if (ifp->if_flags & IFF_UP) {
5166 		mii->mii_media_status = 0;
5167 		mii->mii_media_active = IFM_NONE;
5168 		return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
5169 	}
5170 
5171 	return (0);
5172 }
5173 
5174 void
5175 tlp_pnic_nway_statchg(self)
5176 	struct device *self;
5177 {
5178 	struct tulip_softc *sc = (struct tulip_softc *)self;
5179 
5180 	/* Idle the transmit and receive processes. */
5181 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
5182 
5183 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
5184 	    OPMODE_SCR|OPMODE_HBD);
5185 
5186 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
5187 		sc->sc_opmode |= OPMODE_TTM;
5188 		TULIP_WRITE(sc, CSR_GPP,
5189 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
5190 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
5191 	} else {
5192 		sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD;
5193 		TULIP_WRITE(sc, CSR_GPP,
5194 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
5195 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
5196 	}
5197 
5198 	if (sc->sc_mii.mii_media_active & IFM_FDX)
5199 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
5200 
5201 	/*
5202 	 * Write new OPMODE bits.  This also restarts the transmit
5203 	 * and receive processes.
5204 	 */
5205 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
5206 }
5207 
5208 void
5209 tlp_pnic_nway_tick(arg)
5210 	void *arg;
5211 {
5212 	struct tulip_softc *sc = arg;
5213 	int s;
5214 
5215 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
5216 		return;
5217 
5218 	s = splnet();
5219 	tlp_pnic_nway_service(sc, MII_TICK);
5220 	splx(s);
5221 
5222 	callout_reset(&sc->sc_tick_callout, hz, tlp_pnic_nway_tick, sc);
5223 }
5224 
5225 /*
5226  * Support for the Lite-On PNIC internal NWay block.  This is constructed
5227  * somewhat like a PHY driver for simplicity.
5228  */
5229 
5230 int
5231 tlp_pnic_nway_service(sc, cmd)
5232 	struct tulip_softc *sc;
5233 	int cmd;
5234 {
5235 	struct mii_data *mii = &sc->sc_mii;
5236 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
5237 
5238 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
5239 		return (0);
5240 
5241 	switch (cmd) {
5242 	case MII_POLLSTAT:
5243 		/* Nothing special to do here. */
5244 		break;
5245 
5246 	case MII_MEDIACHG:
5247 		switch (IFM_SUBTYPE(ife->ifm_media)) {
5248 		case IFM_AUTO:
5249 			(void) tlp_pnic_nway_auto(sc, 1);
5250 			break;
5251 		case IFM_100_T4:
5252 			/*
5253 			 * XXX Not supported as a manual setting right now.
5254 			 */
5255 			return (EINVAL);
5256 		default:
5257 			/*
5258 			 * NWAY register data is stored in the ifmedia entry.
5259 			 */
5260 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
5261 		}
5262 		break;
5263 
5264 	case MII_TICK:
5265 		/*
5266 		 * Only used for autonegotiation.
5267 		 */
5268 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
5269 			return (0);
5270 
5271 		/*
5272 		 * Check to see if we have link.  If we do, we don't
5273 		 * need to restart the autonegotiation process.
5274 		 */
5275 		if (sc->sc_flags & TULIPF_LINK_UP)
5276 			return (0);
5277 
5278 		/*
5279 		 * Only retry autonegotiation every 5 seconds.
5280 		 */
5281 		if (++sc->sc_nway_ticks != 5)
5282 			return (0);
5283 
5284 		sc->sc_nway_ticks = 0;
5285 		tlp_pnic_nway_reset(sc);
5286 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
5287 			return (0);
5288 		break;
5289 	}
5290 
5291 	/* Update the media status. */
5292 	tlp_pnic_nway_status(sc);
5293 
5294 	/* Callback if something changed. */
5295 	if ((sc->sc_nway_active == NULL ||
5296 	     sc->sc_nway_active->ifm_media != mii->mii_media_active) ||
5297 	    cmd == MII_MEDIACHG) {
5298 		(*sc->sc_statchg)(&sc->sc_dev);
5299 		tlp_nway_activate(sc, mii->mii_media_active);
5300 	}
5301 	return (0);
5302 }
5303 
5304 void
5305 tlp_pnic_nway_reset(sc)
5306 	struct tulip_softc *sc;
5307 {
5308 
5309 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
5310 	delay(100);
5311 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
5312 }
5313 
5314 int
5315 tlp_pnic_nway_auto(sc, waitfor)
5316 	struct tulip_softc *sc;
5317 	int waitfor;
5318 {
5319 	struct mii_data *mii = &sc->sc_mii;
5320 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
5321 	u_int32_t reg;
5322 	int i;
5323 
5324 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
5325 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
5326 
5327 	if (waitfor) {
5328 		/* Wait 500ms for it to complete. */
5329 		for (i = 0; i < 500; i++) {
5330 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5331 			if (reg & PNIC_NWAY_LPAR_MASK) {
5332 				tlp_pnic_nway_acomp(sc);
5333 				return (0);
5334 			}
5335 			delay(1000);
5336 		}
5337 #if 0
5338 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
5339 			printf("%s: autonegotiation failed to complete\n",
5340 			    sc->sc_dev.dv_xname);
5341 #endif
5342 
5343 		/*
5344 		 * Don't need to worry about clearing DOINGAUTO.
5345 		 * If that's set, a timeout is pending, and it will
5346 		 * clear the flag.
5347 		 */
5348 		return (EIO);
5349 	}
5350 
5351 	/*
5352 	 * Just let it finish asynchronously.  This is for the benefit of
5353 	 * the tick handler driving autonegotiation.  Don't want 500ms
5354 	 * delays all the time while the system is running!
5355 	 */
5356 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
5357 		sc->sc_flags |= TULIPF_DOINGAUTO;
5358 		callout_reset(&sc->sc_nway_callout, hz >> 1,
5359 		    tlp_pnic_nway_auto_timeout, sc);
5360 	}
5361 	return (EJUSTRETURN);
5362 }
5363 
5364 void
5365 tlp_pnic_nway_auto_timeout(arg)
5366 	void *arg;
5367 {
5368 	struct tulip_softc *sc = arg;
5369 	u_int32_t reg;
5370 	int s;
5371 
5372 	s = splnet();
5373 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
5374 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5375 #if 0
5376 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
5377 		printf("%s: autonegotiation failed to complete\n",
5378 		    sc->sc_dev.dv_xname);
5379 #endif
5380 
5381 	tlp_pnic_nway_acomp(sc);
5382 
5383 	/* Update the media status. */
5384 	(void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
5385 	splx(s);
5386 }
5387 
5388 void
5389 tlp_pnic_nway_status(sc)
5390 	struct tulip_softc *sc;
5391 {
5392 	struct mii_data *mii = &sc->sc_mii;
5393 	u_int32_t reg;
5394 
5395 	mii->mii_media_status = IFM_AVALID;
5396 	mii->mii_media_active = IFM_ETHER;
5397 
5398 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5399 
5400 	if (sc->sc_flags & TULIPF_LINK_UP)
5401 		mii->mii_media_status |= IFM_ACTIVE;
5402 
5403 	if (reg & PNIC_NWAY_NW) {
5404 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
5405 			/* Erg, still trying, I guess... */
5406 			mii->mii_media_active |= IFM_NONE;
5407 			return;
5408 		}
5409 
5410 #if 0
5411 		if (reg & PNIC_NWAY_LPAR100T4)
5412 			mii->mii_media_active |= IFM_100_T4;
5413 		else
5414 #endif
5415 		if (reg & PNIC_NWAY_LPAR100TXFDX)
5416 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
5417 		else if (reg & PNIC_NWAY_LPAR100TX)
5418 			mii->mii_media_active |= IFM_100_TX;
5419 		else if (reg & PNIC_NWAY_LPAR10TFDX)
5420 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
5421 		else if (reg & PNIC_NWAY_LPAR10T)
5422 			mii->mii_media_active |= IFM_10_T;
5423 		else
5424 			mii->mii_media_active |= IFM_NONE;
5425 	} else {
5426 		if (reg & PNIC_NWAY_100)
5427 			mii->mii_media_active |= IFM_100_TX;
5428 		else
5429 			mii->mii_media_active |= IFM_10_T;
5430 		if (reg & PNIC_NWAY_FD)
5431 			mii->mii_media_active |= IFM_FDX;
5432 	}
5433 }
5434 
5435 void
5436 tlp_pnic_nway_acomp(sc)
5437 	struct tulip_softc *sc;
5438 {
5439 	u_int32_t reg;
5440 
5441 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5442 	reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
5443 
5444 	if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
5445 		reg |= PNIC_NWAY_100;
5446 	if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
5447 		reg |= PNIC_NWAY_FD;
5448 
5449 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
5450 }
5451 
5452 /*
5453  * Macronix PMAC and Lite-On PNIC-II media switch:
5454  *
5455  *	MX98713 and MX98713A		21140-like MII or GPIO media.
5456  *
5457  *	MX98713A			21143-like MII or SIA/SYM media.
5458  *
5459  *	MX98715, MX98715A, MX98725,	21143-like SIA/SYM media.
5460  *	82C115
5461  *
5462  * So, what we do here is fake MII-on-SIO or ISV media info, and
5463  * use the ISV media switch get/set functions to handle the rest.
5464  */
5465 
5466 void	tlp_pmac_tmsw_init __P((struct tulip_softc *));
5467 
5468 const struct tulip_mediasw tlp_pmac_mediasw = {
5469 	tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
5470 };
5471 
5472 const struct tulip_mediasw tlp_pmac_mii_mediasw = {
5473 	tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
5474 };
5475 
5476 void
5477 tlp_pmac_tmsw_init(sc)
5478 	struct tulip_softc *sc;
5479 {
5480 	static const u_int8_t media[] = {
5481 		TULIP_ROM_MB_MEDIA_TP,
5482 		TULIP_ROM_MB_MEDIA_TP_FDX,
5483 		TULIP_ROM_MB_MEDIA_100TX,
5484 		TULIP_ROM_MB_MEDIA_100TX_FDX,
5485 	};
5486 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5487 
5488 	sc->sc_mii.mii_ifp = ifp;
5489 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
5490 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
5491 	sc->sc_mii.mii_statchg = sc->sc_statchg;
5492 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
5493 	    tlp_mediastatus);
5494 	if (sc->sc_chip == TULIP_CHIP_MX98713 ||
5495 	    sc->sc_chip == TULIP_CHIP_MX98713A) {
5496 		mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
5497 		    MII_PHY_ANY, MII_OFFSET_ANY, 0);
5498 		if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) {
5499 			sc->sc_flags |= TULIPF_HAS_MII;
5500 			sc->sc_tick = tlp_mii_tick;
5501 			sc->sc_preinit = tlp_2114x_mii_preinit;
5502 			sc->sc_mediasw = &tlp_pmac_mii_mediasw;
5503 			ifmedia_set(&sc->sc_mii.mii_media,
5504 			    IFM_ETHER|IFM_AUTO);
5505 			return;
5506 		}
5507 	}
5508 
5509 	switch (sc->sc_chip) {
5510 	case TULIP_CHIP_MX98713:
5511 		tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR,
5512 		    tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4);
5513 
5514 		/*
5515 		 * XXX Should implement auto-sense for this someday,
5516 		 * XXX when we do the same for the 21140.
5517 		 */
5518 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
5519 		break;
5520 
5521 	default:
5522 		tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA,
5523 		    tlp_sia_get, tlp_sia_set, media, 2);
5524 		tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM,
5525 		    tlp_sia_get, tlp_sia_set, media + 2, 2);
5526 
5527 		/*
5528 		 * XXX Autonegotiation not yet supported.
5529 		 */
5530 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
5531 		break;
5532 	}
5533 
5534 	tlp_print_media(sc);
5535 	tlp_sia_fixup(sc);
5536 
5537 	/* Set the LED modes. */
5538 	tlp_pmac_reset(sc);
5539 
5540 	sc->sc_reset = tlp_pmac_reset;
5541 }
5542 
5543 /*
5544  * ADMtek AL981 media switch.  Only has internal PHY.
5545  */
5546 void	tlp_al981_tmsw_init __P((struct tulip_softc *));
5547 
5548 const struct tulip_mediasw tlp_al981_mediasw = {
5549 	tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
5550 };
5551 
5552 void
5553 tlp_al981_tmsw_init(sc)
5554 	struct tulip_softc *sc;
5555 {
5556 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5557 
5558 	sc->sc_mii.mii_ifp = ifp;
5559 	sc->sc_mii.mii_readreg = tlp_al981_mii_readreg;
5560 	sc->sc_mii.mii_writereg = tlp_al981_mii_writereg;
5561 	sc->sc_mii.mii_statchg = sc->sc_statchg;
5562 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
5563 	    tlp_mediastatus);
5564 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
5565 	    MII_OFFSET_ANY, 0);
5566 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
5567 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
5568 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
5569 	} else {
5570 		sc->sc_flags |= TULIPF_HAS_MII;
5571 		sc->sc_tick = tlp_mii_tick;
5572 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
5573 	}
5574 }
5575 
5576 /*
5577  * Davicom DM9102 media switch.  Internal PHY and possibly HomePNA.
5578  */
5579 void	tlp_dm9102_tmsw_init __P((struct tulip_softc *));
5580 void	tlp_dm9102_tmsw_getmedia __P((struct tulip_softc *,
5581 	    struct ifmediareq *));
5582 int	tlp_dm9102_tmsw_setmedia __P((struct tulip_softc *));
5583 
5584 const struct tulip_mediasw tlp_dm9102_mediasw = {
5585 	tlp_dm9102_tmsw_init, tlp_dm9102_tmsw_getmedia,
5586 	    tlp_dm9102_tmsw_setmedia
5587 };
5588 
5589 void
5590 tlp_dm9102_tmsw_init(sc)
5591 	struct tulip_softc *sc;
5592 {
5593 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5594 	u_int32_t opmode;
5595 
5596 	sc->sc_mii.mii_ifp = ifp;
5597 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
5598 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
5599 	sc->sc_mii.mii_statchg = sc->sc_statchg;
5600 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
5601 	    tlp_mediastatus);
5602 
5603 	/* PHY block already reset via tlp_reset(). */
5604 
5605 	/*
5606 	 * Configure OPMODE properly for the internal MII interface.
5607 	 */
5608 	switch (sc->sc_chip) {
5609 	case TULIP_CHIP_DM9102:
5610 		opmode = OPMODE_MBO|OPMODE_HBD|OPMODE_PS;
5611 		break;
5612 
5613 	case TULIP_CHIP_DM9102A:
5614 		opmode = OPMODE_MBO|OPMODE_HBD;
5615 		break;
5616 
5617 	default:
5618 		/* Nothing. */
5619 	}
5620 
5621 	TULIP_WRITE(sc, CSR_OPMODE, opmode);
5622 
5623 	/* Now, probe the internal MII for the internal PHY. */
5624 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
5625 	    MII_OFFSET_ANY, 0);
5626 
5627 	/*
5628 	 * XXX Figure out what to do about the HomePNA portion
5629 	 * XXX of the DM9102A.
5630 	 */
5631 
5632 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
5633 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
5634 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
5635 	} else {
5636 		sc->sc_flags |= TULIPF_HAS_MII;
5637 		sc->sc_tick = tlp_mii_tick;
5638 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
5639 	}
5640 }
5641 
5642 void
5643 tlp_dm9102_tmsw_getmedia(sc, ifmr)
5644 	struct tulip_softc *sc;
5645 	struct ifmediareq *ifmr;
5646 {
5647 
5648 	/* XXX HomePNA on DM9102A. */
5649 	tlp_mii_getmedia(sc, ifmr);
5650 }
5651 
5652 int
5653 tlp_dm9102_tmsw_setmedia(sc)
5654 	struct tulip_softc *sc;
5655 {
5656 
5657 	/* XXX HomePNA on DM9102A. */
5658 	return (tlp_mii_setmedia(sc));
5659 }
5660