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