xref: /netbsd-src/sys/dev/ic/tulip.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: tulip.c,v 1.157 2008/01/19 22:10:17 dyoung 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.157 2008/01/19 22:10:17 dyoung 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 <sys/bus.h>
75 #include <sys/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, void *);
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, 0);
205 	callout_init(&sc->sc_tick_callout, 0);
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), (void **)&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, (void *)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, (void *)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, void *));
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, void *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, void *), mtod(rxs->rxs_mbuf, void *), 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 it's for us.
1416 		 */
1417 		if (ifp->if_bpf)
1418 			bpf_mtap(ifp->if_bpf, m);
1419 #endif /* NBPFILTER > 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(CLLADDR(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, CLLADDR(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 		const u_int8_t *enaddr = CLLADDR(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 		const u_int8_t *enaddr = CLLADDR(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 		aprint_debug("%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 G100 interfaces have the address at
2514 		 * offsets 0 and 20, but each pair of bytes is
2515 		 * swapped.
2516 		 */
2517 		if (sc->sc_srom_addrbits == 6 &&
2518 		    sc->sc_srom[1] == 0x00 &&
2519 		    sc->sc_srom[0] == 0x60 &&
2520 		    sc->sc_srom[3] == 0xf5 &&
2521 		    memcmp(&sc->sc_srom[0], &sc->sc_srom[20], 6) == 0) {
2522 			for (i = 0; i < 6; i += 2) {
2523 				enaddr[i] = sc->sc_srom[i + 1];
2524 				enaddr[i + 1] = sc->sc_srom[i];
2525 			}
2526 			return (1);
2527 		}
2528 
2529 		/*
2530 		 * Phobos G130/G160 interfaces have the address at
2531 		 * offsets 20 and 84, but each pair of bytes is
2532 		 * swapped.
2533 		 */
2534 		if (sc->sc_srom_addrbits == 6 &&
2535 		    sc->sc_srom[21] == 0x00 &&
2536 		    sc->sc_srom[20] == 0x60 &&
2537 		    sc->sc_srom[23] == 0xf5 &&
2538 		    memcmp(&sc->sc_srom[20], &sc->sc_srom[84], 6) == 0) {
2539 			for (i = 0; i < 6; i += 2) {
2540 				enaddr[i] = sc->sc_srom[20 + i + 1];
2541 				enaddr[i + 1] = sc->sc_srom[20 + i];
2542 			}
2543 			return (1);
2544 		}
2545 
2546 		/*
2547 		 * Cobalt Networks interfaces simply have the address
2548 		 * in the first six bytes. The rest is zeroed out
2549 		 * on some models, but others contain unknown data.
2550 		 */
2551 		if (sc->sc_srom[0] == 0x00 &&
2552 		    sc->sc_srom[1] == 0x10 &&
2553 		    sc->sc_srom[2] == 0xe0) {
2554 			memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
2555 			return (1);
2556 		}
2557 
2558 		/*
2559 		 * Some vendors (e.g. ZNYX) don't use the standard
2560 		 * DEC Address ROM format, but rather just have an
2561 		 * Ethernet address in the first 6 bytes, maybe a
2562 		 * 2 byte checksum, and then all 0xff's.
2563 		 */
2564 		for (i = 8; i < 32; i++) {
2565 			if (sc->sc_srom[i] != 0xff &&
2566 			    sc->sc_srom[i] != 0)
2567 				return (0);
2568 		}
2569 
2570 		/*
2571 		 * Sanity check the Ethernet address:
2572 		 *
2573 		 *	- Make sure it's not multicast or locally
2574 		 *	  assigned
2575 		 *	- Make sure it has a non-0 OUI
2576 		 */
2577 		if (sc->sc_srom[0] & 3)
2578 			return (0);
2579 		if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
2580 		    sc->sc_srom[2] == 0)
2581 			return (0);
2582 
2583 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
2584 		return (1);
2585 	}
2586 
2587 	/*
2588 	 * Standard DEC Address ROM test.
2589 	 */
2590 
2591 	if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
2592 		return (0);
2593 
2594 	for (i = 0; i < 8; i++) {
2595 		if (sc->sc_srom[i] != sc->sc_srom[15 - i])
2596 			return (0);
2597 	}
2598 
2599 	memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
2600 
2601 	cksum = *(u_int16_t *) &enaddr[0];
2602 
2603 	cksum <<= 1;
2604 	if (cksum > 0xffff)
2605 		cksum -= 0xffff;
2606 
2607 	cksum += *(u_int16_t *) &enaddr[2];
2608 	if (cksum > 0xffff)
2609 		cksum -= 0xffff;
2610 
2611 	cksum <<= 1;
2612 	if (cksum > 0xffff)
2613 		cksum -= 0xffff;
2614 
2615 	cksum += *(u_int16_t *) &enaddr[4];
2616 	if (cksum >= 0xffff)
2617 		cksum -= 0xffff;
2618 
2619 	if (cksum != *(u_int16_t *) &sc->sc_srom[6])
2620 		return (0);
2621 
2622 	return (1);
2623 }
2624 
2625 /*
2626  * tlp_filter_setup:
2627  *
2628  *	Set the Tulip's receive filter.
2629  */
2630 static void
2631 tlp_filter_setup(struct tulip_softc *sc)
2632 {
2633 	struct ethercom *ec = &sc->sc_ethercom;
2634 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2635 	struct ether_multi *enm;
2636 	struct ether_multistep step;
2637 	volatile u_int32_t *sp;
2638 	struct tulip_txsoft *txs;
2639 	u_int8_t enaddr[ETHER_ADDR_LEN];
2640 	u_int32_t hash, hashsize;
2641 	int cnt, nexttx;
2642 
2643 	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
2644 	    sc->sc_dev.dv_xname, sc->sc_flags));
2645 
2646 	memcpy(enaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
2647 
2648 	/*
2649 	 * If there are transmissions pending, wait until they have
2650 	 * completed.
2651 	 */
2652 	if (! SIMPLEQ_EMPTY(&sc->sc_txdirtyq) ||
2653 	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
2654 		sc->sc_flags |= TULIPF_WANT_SETUP;
2655 		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
2656 		    sc->sc_dev.dv_xname));
2657 		return;
2658 	}
2659 	sc->sc_flags &= ~TULIPF_WANT_SETUP;
2660 
2661 	switch (sc->sc_chip) {
2662 	case TULIP_CHIP_82C115:
2663 		hashsize = TULIP_PNICII_HASHSIZE;
2664 		break;
2665 
2666 	default:
2667 		hashsize = TULIP_MCHASHSIZE;
2668 	}
2669 
2670 	/*
2671 	 * If we're running, idle the transmit and receive engines.  If
2672 	 * we're NOT running, we're being called from tlp_init(), and our
2673 	 * writing OPMODE will start the transmit and receive processes
2674 	 * in motion.
2675 	 */
2676 	if (ifp->if_flags & IFF_RUNNING)
2677 		tlp_idle(sc, OPMODE_ST|OPMODE_SR);
2678 
2679 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
2680 
2681 	if (ifp->if_flags & IFF_PROMISC) {
2682 		sc->sc_opmode |= OPMODE_PR;
2683 		goto allmulti;
2684 	}
2685 
2686 	/*
2687 	 * Try Perfect filtering first.
2688 	 */
2689 
2690 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
2691 	sp = TULIP_CDSP(sc);
2692 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
2693 	cnt = 0;
2694 	ETHER_FIRST_MULTI(step, ec, enm);
2695 	while (enm != NULL) {
2696 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2697 			/*
2698 			 * We must listen to a range of multicast addresses.
2699 			 * For now, just accept all multicasts, rather than
2700 			 * trying to set only those filter bits needed to match
2701 			 * the range.  (At this time, the only use of address
2702 			 * ranges is for IP multicast routing, for which the
2703 			 * range is big enough to require all bits set.)
2704 			 */
2705 			goto allmulti;
2706 		}
2707 		if (cnt == (TULIP_MAXADDRS - 2)) {
2708 			/*
2709 			 * We already have our multicast limit (still need
2710 			 * our station address and broadcast).  Go to
2711 			 * Hash-Perfect mode.
2712 			 */
2713 			goto hashperfect;
2714 		}
2715 		cnt++;
2716 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 0);
2717 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 1);
2718 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 2);
2719 		ETHER_NEXT_MULTI(step, enm);
2720 	}
2721 
2722 	if (ifp->if_flags & IFF_BROADCAST) {
2723 		/* ...and the broadcast address. */
2724 		cnt++;
2725 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2726 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2727 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2728 	}
2729 
2730 	/* Pad the rest with our station address. */
2731 	for (; cnt < TULIP_MAXADDRS; cnt++) {
2732 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
2733 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
2734 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
2735 	}
2736 	ifp->if_flags &= ~IFF_ALLMULTI;
2737 	goto setit;
2738 
2739  hashperfect:
2740 	/*
2741 	 * Try Hash-Perfect mode.
2742 	 */
2743 
2744 	/*
2745 	 * Some 21140 chips have broken Hash-Perfect modes.  On these
2746 	 * chips, we simply use Hash-Only mode, and put our station
2747 	 * address into the filter.
2748 	 */
2749 	if (sc->sc_chip == TULIP_CHIP_21140)
2750 		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
2751 	else
2752 		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
2753 	sp = TULIP_CDSP(sc);
2754 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
2755 	ETHER_FIRST_MULTI(step, ec, enm);
2756 	while (enm != NULL) {
2757 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2758 			/*
2759 			 * We must listen to a range of multicast addresses.
2760 			 * For now, just accept all multicasts, rather than
2761 			 * trying to set only those filter bits needed to match
2762 			 * the range.  (At this time, the only use of address
2763 			 * ranges is for IP multicast routing, for which the
2764 			 * range is big enough to require all bits set.)
2765 			 */
2766 			goto allmulti;
2767 		}
2768 		hash = tlp_mchash(enm->enm_addrlo, hashsize);
2769 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
2770 		ETHER_NEXT_MULTI(step, enm);
2771 	}
2772 
2773 	if (ifp->if_flags & IFF_BROADCAST) {
2774 		/* ...and the broadcast address. */
2775 		hash = tlp_mchash(etherbroadcastaddr, hashsize);
2776 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
2777 	}
2778 
2779 	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
2780 		/* ...and our station address. */
2781 		hash = tlp_mchash(enaddr, hashsize);
2782 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
2783 	} else {
2784 		/*
2785 		 * Hash-Perfect mode; put our station address after
2786 		 * the hash table.
2787 		 */
2788 		sp[39] = TULIP_SP_FIELD(enaddr, 0);
2789 		sp[40] = TULIP_SP_FIELD(enaddr, 1);
2790 		sp[41] = TULIP_SP_FIELD(enaddr, 2);
2791 	}
2792 	ifp->if_flags &= ~IFF_ALLMULTI;
2793 	goto setit;
2794 
2795  allmulti:
2796 	/*
2797 	 * Use Perfect filter mode.  First address is the broadcast address,
2798 	 * and pad the rest with our station address.  We'll set Pass-all-
2799 	 * multicast in OPMODE below.
2800 	 */
2801 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
2802 	sp = TULIP_CDSP(sc);
2803 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
2804 	cnt = 0;
2805 	if (ifp->if_flags & IFF_BROADCAST) {
2806 		cnt++;
2807 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2808 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2809 		*sp++ = TULIP_SP_FIELD_C(0xffff);
2810 	}
2811 	for (; cnt < TULIP_MAXADDRS; cnt++) {
2812 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
2813 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
2814 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
2815 	}
2816 	ifp->if_flags |= IFF_ALLMULTI;
2817 
2818  setit:
2819 	if (ifp->if_flags & IFF_ALLMULTI)
2820 		sc->sc_opmode |= OPMODE_PM;
2821 
2822 	/* Sync the setup packet buffer. */
2823 	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
2824 
2825 	/*
2826 	 * Fill in the setup packet descriptor.
2827 	 */
2828 	txs = SIMPLEQ_FIRST(&sc->sc_txfreeq);
2829 
2830 	txs->txs_firstdesc = sc->sc_txnext;
2831 	txs->txs_lastdesc = sc->sc_txnext;
2832 	txs->txs_ndescs = 1;
2833 	txs->txs_mbuf = NULL;
2834 
2835 	nexttx = sc->sc_txnext;
2836 	sc->sc_txdescs[nexttx].td_status = 0;
2837 	sc->sc_txdescs[nexttx].td_bufaddr1 = htole32(TULIP_CDSPADDR(sc));
2838 	sc->sc_txdescs[nexttx].td_ctl =
2839 	    htole32((TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
2840 	    sc->sc_filtmode | TDCTL_Tx_SET | sc->sc_setup_fsls |
2841 	    TDCTL_Tx_IC | sc->sc_tdctl_ch |
2842 	    (nexttx == (TULIP_NTXDESC - 1) ? sc->sc_tdctl_er : 0));
2843 	TULIP_CDTXSYNC(sc, nexttx, 1,
2844 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2845 
2846 #ifdef TLP_DEBUG
2847 	if (ifp->if_flags & IFF_DEBUG) {
2848 		printf("     filter_setup %p transmit chain:\n", txs);
2849 		printf("     descriptor %d:\n", nexttx);
2850 		printf("       td_status:   0x%08x\n",
2851 		    le32toh(sc->sc_txdescs[nexttx].td_status));
2852 		printf("       td_ctl:      0x%08x\n",
2853 		    le32toh(sc->sc_txdescs[nexttx].td_ctl));
2854 		printf("       td_bufaddr1: 0x%08x\n",
2855 		    le32toh(sc->sc_txdescs[nexttx].td_bufaddr1));
2856 		printf("       td_bufaddr2: 0x%08x\n",
2857 		    le32toh(sc->sc_txdescs[nexttx].td_bufaddr2));
2858 	}
2859 #endif
2860 
2861 	sc->sc_txdescs[nexttx].td_status = htole32(TDSTAT_OWN);
2862 	TULIP_CDTXSYNC(sc, nexttx, 1,
2863 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2864 
2865 	/* Advance the tx pointer. */
2866 	sc->sc_txfree -= 1;
2867 	sc->sc_txnext = TULIP_NEXTTX(nexttx);
2868 
2869 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
2870 	SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
2871 
2872 	/*
2873 	 * Set the OPMODE register.  This will also resume the
2874 	 * transmit process we idled above.
2875 	 */
2876 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2877 
2878 	sc->sc_flags |= TULIPF_DOING_SETUP;
2879 
2880 	/*
2881 	 * Kick the transmitter; this will cause the Tulip to
2882 	 * read the setup descriptor.
2883 	 */
2884 	/* XXX USE AUTOPOLLING? */
2885 	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
2886 
2887 	/* Set up a watchdog timer in case the chip flakes out. */
2888 	ifp->if_timer = 5;
2889 
2890 	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
2891 }
2892 
2893 /*
2894  * tlp_winb_filter_setup:
2895  *
2896  *	Set the Winbond 89C840F's receive filter.
2897  */
2898 static void
2899 tlp_winb_filter_setup(struct tulip_softc *sc)
2900 {
2901 	struct ethercom *ec = &sc->sc_ethercom;
2902 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2903 	struct ether_multi *enm;
2904 	struct ether_multistep step;
2905 	u_int32_t hash, mchash[2];
2906 
2907 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
2908 	    sc->sc_dev.dv_xname, sc->sc_flags));
2909 
2910 	sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
2911 
2912 	if (ifp->if_flags & IFF_MULTICAST)
2913 		sc->sc_opmode |= OPMODE_WINB_AMP;
2914 
2915 	if (ifp->if_flags & IFF_BROADCAST)
2916 		sc->sc_opmode |= OPMODE_WINB_ABP;
2917 
2918 	if (ifp->if_flags & IFF_PROMISC) {
2919 		sc->sc_opmode |= OPMODE_WINB_APP;
2920 		goto allmulti;
2921 	}
2922 
2923 	mchash[0] = mchash[1] = 0;
2924 
2925 	ETHER_FIRST_MULTI(step, ec, enm);
2926 	while (enm != NULL) {
2927 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2928 			/*
2929 			 * We must listen to a range of multicast addresses.
2930 			 * For now, just accept all multicasts, rather than
2931 			 * trying to set only those filter bits needed to match
2932 			 * the range.  (At this time, the only use of address
2933 			 * ranges is for IP multicast routing, for which the
2934 			 * range is big enough to require all bits set.)
2935 			 */
2936 			goto allmulti;
2937 		}
2938 
2939 		/*
2940 		 * According to the FreeBSD `wb' driver, yes, you
2941 		 * really do invert the hash.
2942 		 */
2943 		hash =
2944 		    (~(ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
2945 		    & 0x3f;
2946 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
2947 		ETHER_NEXT_MULTI(step, enm);
2948 	}
2949 	ifp->if_flags &= ~IFF_ALLMULTI;
2950 	goto setit;
2951 
2952  allmulti:
2953 	ifp->if_flags |= IFF_ALLMULTI;
2954 	mchash[0] = mchash[1] = 0xffffffff;
2955 
2956  setit:
2957 	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
2958 	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
2959 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2960 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
2961 	    sc->sc_dev.dv_xname));
2962 }
2963 
2964 /*
2965  * tlp_al981_filter_setup:
2966  *
2967  *	Set the ADMtek AL981's receive filter.
2968  */
2969 static void
2970 tlp_al981_filter_setup(struct tulip_softc *sc)
2971 {
2972 	struct ethercom *ec = &sc->sc_ethercom;
2973 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2974 	struct ether_multi *enm;
2975 	struct ether_multistep step;
2976 	u_int32_t hash, mchash[2];
2977 
2978 	/*
2979 	 * If the chip is running, we need to reset the interface,
2980 	 * and will revisit here (with IFF_RUNNING) clear.  The
2981 	 * chip seems to really not like to have its multicast
2982 	 * filter programmed without a reset.
2983 	 */
2984 	if (ifp->if_flags & IFF_RUNNING) {
2985 		(void) tlp_init(ifp);
2986 		return;
2987 	}
2988 
2989 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n",
2990 	    sc->sc_dev.dv_xname, sc->sc_flags));
2991 
2992 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
2993 
2994 	if (ifp->if_flags & IFF_PROMISC) {
2995 		sc->sc_opmode |= OPMODE_PR;
2996 		goto allmulti;
2997 	}
2998 
2999 	mchash[0] = mchash[1] = 0;
3000 
3001 	ETHER_FIRST_MULTI(step, ec, enm);
3002 	while (enm != NULL) {
3003 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
3004 			/*
3005 			 * We must listen to a range of multicast addresses.
3006 			 * For now, just accept all multicasts, rather than
3007 			 * trying to set only those filter bits needed to match
3008 			 * the range.  (At this time, the only use of address
3009 			 * ranges is for IP multicast routing, for which the
3010 			 * range is big enough to require all bits set.)
3011 			 */
3012 			goto allmulti;
3013 		}
3014 
3015 		hash = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) & 0x3f;
3016 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
3017 		ETHER_NEXT_MULTI(step, enm);
3018 	}
3019 	ifp->if_flags &= ~IFF_ALLMULTI;
3020 	goto setit;
3021 
3022  allmulti:
3023 	ifp->if_flags |= IFF_ALLMULTI;
3024 	mchash[0] = mchash[1] = 0xffffffff;
3025 
3026  setit:
3027 	bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_MAR0, mchash[0]);
3028 	bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_MAR1, mchash[1]);
3029 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3030 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n",
3031 	    sc->sc_dev.dv_xname));
3032 }
3033 
3034 /*
3035  * tlp_asix_filter_setup:
3036  *
3037  * 	Set the ASIX AX8814x recieve filter.
3038  */
3039 static void
3040 tlp_asix_filter_setup(struct tulip_softc *sc)
3041 {
3042 	struct ethercom *ec = &sc->sc_ethercom;
3043 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
3044 	struct ether_multi *enm;
3045 	struct ether_multistep step;
3046 	u_int32_t hash, mchash[2];
3047 
3048 	DPRINTF(sc, ("%s: tlp_asix_filter_setup: sc_flags 0x%08x\n",
3049 		sc->sc_dev.dv_xname, sc->sc_flags));
3050 
3051 	sc->sc_opmode &= ~(OPMODE_PM|OPMODE_AX_RB|OPMODE_PR);
3052 
3053 	if (ifp->if_flags & IFF_MULTICAST)
3054 		sc->sc_opmode |= OPMODE_PM;
3055 
3056 	if (ifp->if_flags & IFF_BROADCAST)
3057 		sc->sc_opmode |= OPMODE_AX_RB;
3058 
3059 	if (ifp->if_flags & IFF_PROMISC) {
3060 		sc->sc_opmode |= OPMODE_PR;
3061 		goto allmulti;
3062 	}
3063 
3064 	mchash[0] = mchash[1] = 0;
3065 
3066 	ETHER_FIRST_MULTI(step, ec, enm);
3067 	while (enm != NULL) {
3068 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
3069 			/*
3070 			 * We must listen to a range of multicast addresses.
3071 			 * For now, just accept all multicasts, rather than
3072 			 * trying to set only those filter bits needed to match
3073 			 * the range.  (At this time, the only use of address
3074 			 * ranges is for IP multicast routing, for which the
3075 			 * range is big enough to require all bits set.)
3076 			 */
3077 			goto allmulti;
3078 		}
3079 		hash = (ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26)
3080 		       & 0x3f;
3081 		if (hash < 32)
3082 			mchash[0] |= (1 << hash);
3083 		else
3084 			mchash[1] |= (1 << (hash - 32));
3085 		ETHER_NEXT_MULTI(step, enm);
3086 	}
3087 	ifp->if_flags &= ~IFF_ALLMULTI;
3088 	goto setit;
3089 
3090 allmulti:
3091 	ifp->if_flags |= IFF_ALLMULTI;
3092 	mchash[0] = mchash[1] = 0xffffffff;
3093 
3094 setit:
3095 	TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_MAR0);
3096 	TULIP_WRITE(sc, CSR_AX_FILTDATA, mchash[0]);
3097 	TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_MAR1);
3098 	TULIP_WRITE(sc, CSR_AX_FILTDATA, mchash[1]);
3099 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3100 	DPRINTF(sc, ("%s: tlp_asix_filter_setup: returning\n",
3101 		sc->sc_dev.dv_xname));
3102 }
3103 
3104 
3105 /*
3106  * tlp_idle:
3107  *
3108  *	Cause the transmit and/or receive processes to go idle.
3109  */
3110 void
3111 tlp_idle(struct tulip_softc *sc, u_int32_t bits)
3112 {
3113 	static const char * const tlp_tx_state_names[] = {
3114 		"STOPPED",
3115 		"RUNNING - FETCH",
3116 		"RUNNING - WAIT",
3117 		"RUNNING - READING",
3118 		"-- RESERVED --",
3119 		"RUNNING - SETUP",
3120 		"SUSPENDED",
3121 		"RUNNING - CLOSE",
3122 	};
3123 	static const char * const tlp_rx_state_names[] = {
3124 		"STOPPED",
3125 		"RUNNING - FETCH",
3126 		"RUNNING - CHECK",
3127 		"RUNNING - WAIT",
3128 		"SUSPENDED",
3129 		"RUNNING - CLOSE",
3130 		"RUNNING - FLUSH",
3131 		"RUNNING - QUEUE",
3132 	};
3133 	static const char * const dm9102_tx_state_names[] = {
3134 		"STOPPED",
3135 		"RUNNING - FETCH",
3136 		"RUNNING - SETUP",
3137 		"RUNNING - READING",
3138 		"RUNNING - CLOSE - CLEAR OWNER",
3139 		"RUNNING - WAIT",
3140 		"RUNNING - CLOSE - WRITE STATUS",
3141 		"SUSPENDED",
3142 	};
3143 	static const char * const dm9102_rx_state_names[] = {
3144 		"STOPPED",
3145 		"RUNNING - FETCH",
3146 		"RUNNING - WAIT",
3147 		"RUNNING - QUEUE",
3148 		"RUNNING - CLOSE - CLEAR OWNER",
3149 		"RUNNING - CLOSE - WRITE STATUS",
3150 		"SUSPENDED",
3151 		"RUNNING - FLUSH",
3152 	};
3153 
3154 	const char * const *tx_state_names, * const *rx_state_names;
3155 	u_int32_t csr, ackmask = 0;
3156 	int i;
3157 
3158 	switch (sc->sc_chip) {
3159 	case TULIP_CHIP_DM9102:
3160 	case TULIP_CHIP_DM9102A:
3161 		tx_state_names = dm9102_tx_state_names;
3162 		rx_state_names = dm9102_rx_state_names;
3163 		break;
3164 
3165 	default:
3166 		tx_state_names = tlp_tx_state_names;
3167 		rx_state_names = tlp_rx_state_names;
3168 		break;
3169 	}
3170 
3171 	if (bits & OPMODE_ST)
3172 		ackmask |= STATUS_TPS;
3173 
3174 	if (bits & OPMODE_SR)
3175 		ackmask |= STATUS_RPS;
3176 
3177 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
3178 
3179 	for (i = 0; i < 1000; i++) {
3180 		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
3181 			break;
3182 		delay(10);
3183 	}
3184 
3185 	csr = TULIP_READ(sc, CSR_STATUS);
3186 	if ((csr & ackmask) != ackmask) {
3187 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
3188 		    (csr & STATUS_TS) != STATUS_TS_STOPPED) {
3189 			switch (sc->sc_chip) {
3190 			case TULIP_CHIP_AX88140:
3191 			case TULIP_CHIP_AX88141:
3192 				/*
3193 				 * Filter the message out on noisy chips.
3194 				 */
3195 				break;
3196 			default:
3197 				printf("%s: transmit process failed to idle: "
3198 				    "state %s\n", sc->sc_dev.dv_xname,
3199 				    tx_state_names[(csr & STATUS_TS) >> 20]);
3200 			}
3201 		}
3202 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
3203 		    (csr & STATUS_RS) != STATUS_RS_STOPPED) {
3204 			switch (sc->sc_chip) {
3205 			case TULIP_CHIP_AN983:
3206 			case TULIP_CHIP_AN985:
3207 			case TULIP_CHIP_DM9102A:
3208 			case TULIP_CHIP_RS7112:
3209 				/*
3210 				 * Filter the message out on noisy chips.
3211 				 */
3212 				break;
3213 			default:
3214 				printf("%s: receive process failed to idle: "
3215 				    "state %s\n", sc->sc_dev.dv_xname,
3216 				    rx_state_names[(csr & STATUS_RS) >> 17]);
3217 			}
3218 		}
3219 	}
3220 	TULIP_WRITE(sc, CSR_STATUS, ackmask);
3221 }
3222 
3223 /*****************************************************************************
3224  * Generic media support functions.
3225  *****************************************************************************/
3226 
3227 /*
3228  * tlp_mediastatus:	[ifmedia interface function]
3229  *
3230  *	Query the current media.
3231  */
3232 void
3233 tlp_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
3234 {
3235 	struct tulip_softc *sc = ifp->if_softc;
3236 
3237 	if (TULIP_IS_ENABLED(sc) == 0) {
3238 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
3239 		ifmr->ifm_status = 0;
3240 		return;
3241 	}
3242 
3243 	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
3244 }
3245 
3246 /*
3247  * tlp_mediachange:	[ifmedia interface function]
3248  *
3249  *	Update the current media.
3250  */
3251 int
3252 tlp_mediachange(struct ifnet *ifp)
3253 {
3254 	struct tulip_softc *sc = ifp->if_softc;
3255 
3256 	if ((ifp->if_flags & IFF_UP) == 0)
3257 		return (0);
3258 	return ((*sc->sc_mediasw->tmsw_set)(sc));
3259 }
3260 
3261 /*****************************************************************************
3262  * Support functions for MII-attached media.
3263  *****************************************************************************/
3264 
3265 /*
3266  * tlp_mii_tick:
3267  *
3268  *	One second timer, used to tick the MII.
3269  */
3270 static void
3271 tlp_mii_tick(void *arg)
3272 {
3273 	struct tulip_softc *sc = arg;
3274 	int s;
3275 
3276 	if (!device_is_active(&sc->sc_dev))
3277 		return;
3278 
3279 	s = splnet();
3280 	mii_tick(&sc->sc_mii);
3281 	splx(s);
3282 
3283 	callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
3284 }
3285 
3286 /*
3287  * tlp_mii_statchg:	[mii interface function]
3288  *
3289  *	Callback from PHY when media changes.
3290  */
3291 static void
3292 tlp_mii_statchg(struct device *self)
3293 {
3294 	struct tulip_softc *sc = (struct tulip_softc *)self;
3295 
3296 	/* Idle the transmit and receive processes. */
3297 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
3298 
3299 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_HBD);
3300 
3301 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
3302 		sc->sc_opmode |= OPMODE_TTM;
3303 	else
3304 		sc->sc_opmode |= OPMODE_HBD;
3305 
3306 	if (sc->sc_mii.mii_media_active & IFM_FDX)
3307 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
3308 
3309 	/*
3310 	 * Write new OPMODE bits.  This also restarts the transmit
3311 	 * and receive processes.
3312 	 */
3313 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3314 }
3315 
3316 /*
3317  * tlp_winb_mii_statchg: [mii interface function]
3318  *
3319  *	Callback from PHY when media changes.  This version is
3320  *	for the Winbond 89C840F, which has different OPMODE bits.
3321  */
3322 static void
3323 tlp_winb_mii_statchg(struct device *self)
3324 {
3325 	struct tulip_softc *sc = (struct tulip_softc *)self;
3326 
3327 	/* Idle the transmit and receive processes. */
3328 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
3329 
3330 	sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD);
3331 
3332 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
3333 		sc->sc_opmode |= OPMODE_WINB_FES;
3334 
3335 	if (sc->sc_mii.mii_media_active & IFM_FDX)
3336 		sc->sc_opmode |= OPMODE_FD;
3337 
3338 	/*
3339 	 * Write new OPMODE bits.  This also restarts the transmit
3340 	 * and receive processes.
3341 	 */
3342 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3343 }
3344 
3345 /*
3346  * tlp_dm9102_mii_statchg: [mii interface function]
3347  *
3348  *	Callback from PHY when media changes.  This version is
3349  *	for the DM9102.
3350  */
3351 static void
3352 tlp_dm9102_mii_statchg(struct device *self)
3353 {
3354 	struct tulip_softc *sc = (struct tulip_softc *)self;
3355 
3356 	/*
3357 	 * Don't idle the transmit and receive processes, here.  It
3358 	 * seems to fail, and just causes excess noise.
3359 	 */
3360 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD);
3361 
3362 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) != IFM_100_TX)
3363 		sc->sc_opmode |= OPMODE_TTM;
3364 
3365 	if (sc->sc_mii.mii_media_active & IFM_FDX)
3366 		sc->sc_opmode |= OPMODE_FD;
3367 
3368 	/*
3369 	 * Write new OPMODE bits.
3370 	 */
3371 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3372 }
3373 
3374 /*
3375  * tlp_mii_getmedia:
3376  *
3377  *	Callback from ifmedia to request current media status.
3378  */
3379 static void
3380 tlp_mii_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
3381 {
3382 
3383 	mii_pollstat(&sc->sc_mii);
3384 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
3385 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
3386 }
3387 
3388 /*
3389  * tlp_mii_setmedia:
3390  *
3391  *	Callback from ifmedia to request new media setting.
3392  */
3393 static int
3394 tlp_mii_setmedia(struct tulip_softc *sc)
3395 {
3396 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
3397 	int rc;
3398 
3399 	if ((ifp->if_flags & IFF_UP) == 0)
3400 		return 0;
3401 	switch (sc->sc_chip) {
3402 	case TULIP_CHIP_21142:
3403 	case TULIP_CHIP_21143:
3404 		/* Disable the internal Nway engine. */
3405 		TULIP_WRITE(sc, CSR_SIATXRX, 0);
3406 		break;
3407 
3408 	default:
3409 		/* Nothing. */
3410 		break;
3411 	}
3412 	if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
3413 		return 0;
3414 	return rc;
3415 }
3416 
3417 /*
3418  * tlp_bitbang_mii_readreg:
3419  *
3420  *	Read a PHY register via bit-bang'ing the MII.
3421  */
3422 static int
3423 tlp_bitbang_mii_readreg(struct device *self, int phy, int reg)
3424 {
3425 	struct tulip_softc *sc = (void *) self;
3426 
3427 	return (mii_bitbang_readreg(self, sc->sc_bitbang_ops, phy, reg));
3428 }
3429 
3430 /*
3431  * tlp_bitbang_mii_writereg:
3432  *
3433  *	Write a PHY register via bit-bang'ing the MII.
3434  */
3435 static void
3436 tlp_bitbang_mii_writereg(struct device *self, int phy, int reg, int val)
3437 {
3438 	struct tulip_softc *sc = (void *) self;
3439 
3440 	mii_bitbang_writereg(self, sc->sc_bitbang_ops, phy, reg, val);
3441 }
3442 
3443 /*
3444  * tlp_sio_mii_bitbang_read:
3445  *
3446  *	Read the MII serial port for the MII bit-bang module.
3447  */
3448 static u_int32_t
3449 tlp_sio_mii_bitbang_read(struct device *self)
3450 {
3451 	struct tulip_softc *sc = (void *) self;
3452 
3453 	return (TULIP_READ(sc, CSR_MIIROM));
3454 }
3455 
3456 /*
3457  * tlp_sio_mii_bitbang_write:
3458  *
3459  *	Write the MII serial port for the MII bit-bang module.
3460  */
3461 static void
3462 tlp_sio_mii_bitbang_write(struct device *self, u_int32_t val)
3463 {
3464 	struct tulip_softc *sc = (void *) self;
3465 
3466 	TULIP_WRITE(sc, CSR_MIIROM, val);
3467 }
3468 
3469 /*
3470  * tlp_pnic_mii_readreg:
3471  *
3472  *	Read a PHY register on the Lite-On PNIC.
3473  */
3474 static int
3475 tlp_pnic_mii_readreg(struct device *self, int phy, int reg)
3476 {
3477 	struct tulip_softc *sc = (void *) self;
3478 	u_int32_t val;
3479 	int i;
3480 
3481 	TULIP_WRITE(sc, CSR_PNIC_MII,
3482 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
3483 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
3484 	    (reg << PNIC_MII_REGSHIFT));
3485 
3486 	for (i = 0; i < 1000; i++) {
3487 		delay(10);
3488 		val = TULIP_READ(sc, CSR_PNIC_MII);
3489 		if ((val & PNIC_MII_BUSY) == 0) {
3490 			if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
3491 				return (0);
3492 			else
3493 				return (val & PNIC_MII_DATA);
3494 		}
3495 	}
3496 	printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
3497 	return (0);
3498 }
3499 
3500 /*
3501  * tlp_pnic_mii_writereg:
3502  *
3503  *	Write a PHY register on the Lite-On PNIC.
3504  */
3505 static void
3506 tlp_pnic_mii_writereg(struct device *self, int phy, int reg, int val)
3507 {
3508 	struct tulip_softc *sc = (void *) self;
3509 	int i;
3510 
3511 	TULIP_WRITE(sc, CSR_PNIC_MII,
3512 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
3513 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
3514 	    (reg << PNIC_MII_REGSHIFT) | val);
3515 
3516 	for (i = 0; i < 1000; i++) {
3517 		delay(10);
3518 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
3519 			return;
3520 	}
3521 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
3522 }
3523 
3524 static const bus_addr_t tlp_al981_phy_regmap[] = {
3525 	CSR_ADM_BMCR,
3526 	CSR_ADM_BMSR,
3527 	CSR_ADM_PHYIDR1,
3528 	CSR_ADM_PHYIDR2,
3529 	CSR_ADM_ANAR,
3530 	CSR_ADM_ANLPAR,
3531 	CSR_ADM_ANER,
3532 
3533 	CSR_ADM_XMC,
3534 	CSR_ADM_XCIIS,
3535 	CSR_ADM_XIE,
3536 	CSR_ADM_100CTR,
3537 };
3538 static const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
3539     sizeof(tlp_al981_phy_regmap[0]);
3540 
3541 /*
3542  * tlp_al981_mii_readreg:
3543  *
3544  *	Read a PHY register on the ADMtek AL981.
3545  */
3546 static int
3547 tlp_al981_mii_readreg(struct device *self, int phy, int reg)
3548 {
3549 	struct tulip_softc *sc = (struct tulip_softc *)self;
3550 
3551 	/* AL981 only has an internal PHY. */
3552 	if (phy != 0)
3553 		return (0);
3554 
3555 	if (reg >= tlp_al981_phy_regmap_size)
3556 		return (0);
3557 
3558 	return (bus_space_read_4(sc->sc_st, sc->sc_sh,
3559 	    tlp_al981_phy_regmap[reg]) & 0xffff);
3560 }
3561 
3562 /*
3563  * tlp_al981_mii_writereg:
3564  *
3565  *	Write a PHY register on the ADMtek AL981.
3566  */
3567 static void
3568 tlp_al981_mii_writereg(struct device *self, int phy, int reg, int val)
3569 {
3570 	struct tulip_softc *sc = (struct tulip_softc *)self;
3571 
3572 	/* AL981 only has an internal PHY. */
3573 	if (phy != 0)
3574 		return;
3575 
3576 	if (reg >= tlp_al981_phy_regmap_size)
3577 		return;
3578 
3579 	bus_space_write_4(sc->sc_st, sc->sc_sh,
3580 	    tlp_al981_phy_regmap[reg], val);
3581 }
3582 
3583 /*****************************************************************************
3584  * Chip-specific pre-init and reset functions.
3585  *****************************************************************************/
3586 
3587 /*
3588  * tlp_2114x_preinit:
3589  *
3590  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
3591  */
3592 static void
3593 tlp_2114x_preinit(struct tulip_softc *sc)
3594 {
3595 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3596 	struct tulip_21x4x_media *tm = ife->ifm_aux;
3597 
3598 	/*
3599 	 * Whether or not we're in MII or SIA/SYM mode, the media info
3600 	 * contains the appropriate OPMODE bits.
3601 	 *
3602 	 * Also, we always set the Must-Be-One bit.
3603 	 */
3604 	sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode;
3605 
3606 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3607 }
3608 
3609 /*
3610  * tlp_2114x_mii_preinit:
3611  *
3612  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
3613  *	This version is used by boards which only have MII and don't have
3614  *	an ISV SROM.
3615  */
3616 static void
3617 tlp_2114x_mii_preinit(struct tulip_softc *sc)
3618 {
3619 
3620 	/*
3621 	 * Always set the Must-Be-One bit, and Port Select (to select MII).
3622 	 * We'll never be called during a media change.
3623 	 */
3624 	sc->sc_opmode |= OPMODE_MBO|OPMODE_PS;
3625 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3626 }
3627 
3628 /*
3629  * tlp_pnic_preinit:
3630  *
3631  *	Pre-init function for the Lite-On 82c168 and 82c169.
3632  */
3633 static void
3634 tlp_pnic_preinit(struct tulip_softc *sc)
3635 {
3636 
3637 	if (sc->sc_flags & TULIPF_HAS_MII) {
3638 		/*
3639 		 * MII case: just set the port-select bit; we will never
3640 		 * be called during a media change.
3641 		 */
3642 		sc->sc_opmode |= OPMODE_PS;
3643 	} else {
3644 		/*
3645 		 * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
3646 		 */
3647 		sc->sc_opmode |= OPMODE_PNIC_TBEN;
3648 	}
3649 }
3650 
3651 /*
3652  * tlp_asix_preinit:
3653  *
3654  * 	Pre-init function for the ASIX chipsets.
3655  */
3656 static void
3657 tlp_asix_preinit(struct tulip_softc *sc)
3658 {
3659 
3660 	switch (sc->sc_chip) {
3661 		case TULIP_CHIP_AX88140:
3662 		case TULIP_CHIP_AX88141:
3663 			/* XXX Handle PHY. */
3664 			sc->sc_opmode |= OPMODE_HBD|OPMODE_PS;
3665 			break;
3666 		default:
3667 			/* Nothing */
3668 			break;
3669 	}
3670 
3671 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3672 }
3673 
3674 /*
3675  * tlp_dm9102_preinit:
3676  *
3677  *	Pre-init function for the Davicom DM9102.
3678  */
3679 static void
3680 tlp_dm9102_preinit(struct tulip_softc *sc)
3681 {
3682 
3683 	switch (sc->sc_chip) {
3684 	case TULIP_CHIP_DM9102:
3685 		sc->sc_opmode |= OPMODE_MBO|OPMODE_HBD|OPMODE_PS;
3686 		break;
3687 
3688 	case TULIP_CHIP_DM9102A:
3689 		/*
3690 		 * XXX Figure out how to actually deal with the HomePNA
3691 		 * XXX portion of the DM9102A.
3692 		 */
3693 		sc->sc_opmode |= OPMODE_MBO|OPMODE_HBD;
3694 		break;
3695 
3696 	default:
3697 		/* Nothing. */
3698 		break;
3699 	}
3700 
3701 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3702 }
3703 
3704 /*
3705  * tlp_21140_reset:
3706  *
3707  *	Issue a reset sequence on the 21140 via the GPIO facility.
3708  */
3709 static void
3710 tlp_21140_reset(struct tulip_softc *sc)
3711 {
3712 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3713 	struct tulip_21x4x_media *tm = ife->ifm_aux;
3714 	int i;
3715 
3716 	/* First, set the direction on the GPIO pins. */
3717 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
3718 
3719 	/* Now, issue the reset sequence. */
3720 	for (i = 0; i < tm->tm_reset_length; i++) {
3721 		delay(10);
3722 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
3723 	}
3724 
3725 	/* Now, issue the selection sequence. */
3726 	for (i = 0; i < tm->tm_gp_length; i++) {
3727 		delay(10);
3728 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
3729 	}
3730 
3731 	/* If there were no sequences, just lower the pins. */
3732 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
3733 		delay(10);
3734 		TULIP_WRITE(sc, CSR_GPP, 0);
3735 	}
3736 }
3737 
3738 /*
3739  * tlp_21142_reset:
3740  *
3741  *	Issue a reset sequence on the 21142 via the GPIO facility.
3742  */
3743 static void
3744 tlp_21142_reset(struct tulip_softc *sc)
3745 {
3746 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3747 	struct tulip_21x4x_media *tm = ife->ifm_aux;
3748 	const u_int8_t *cp;
3749 	int i;
3750 
3751 	cp = &sc->sc_srom[tm->tm_reset_offset];
3752 	for (i = 0; i < tm->tm_reset_length; i++, cp += 2) {
3753 		delay(10);
3754 		TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16);
3755 	}
3756 
3757 	cp = &sc->sc_srom[tm->tm_gp_offset];
3758 	for (i = 0; i < tm->tm_gp_length; i++, cp += 2) {
3759 		delay(10);
3760 		TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16);
3761 	}
3762 
3763 	/* If there were no sequences, just lower the pins. */
3764 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
3765 		delay(10);
3766 		TULIP_WRITE(sc, CSR_SIAGEN, 0);
3767 	}
3768 }
3769 
3770 /*
3771  * tlp_pmac_reset:
3772  *
3773  *	Reset routine for Macronix chips.
3774  */
3775 static void
3776 tlp_pmac_reset(struct tulip_softc *sc)
3777 {
3778 
3779 	switch (sc->sc_chip) {
3780 	case TULIP_CHIP_82C115:
3781 	case TULIP_CHIP_MX98715:
3782 	case TULIP_CHIP_MX98715A:
3783 	case TULIP_CHIP_MX98725:
3784 		/*
3785 		 * Set the LED operating mode.  This information is located
3786 		 * in the EEPROM at byte offset 0x77, per the MX98715A and
3787 		 * MX98725 application notes.
3788 		 */
3789 		TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
3790 		break;
3791 	case TULIP_CHIP_MX98715AEC_X:
3792 		/*
3793 		 * Set the LED operating mode.  This information is located
3794 		 * in the EEPROM at byte offset 0x76, per the MX98715AEC
3795 		 * application note.
3796 		 */
3797 		TULIP_WRITE(sc, CSR_MIIROM, ((0xf & sc->sc_srom[0x76]) << 28)
3798 		    | ((0xf0 & sc->sc_srom[0x76]) << 20));
3799 		break;
3800 
3801 	default:
3802 		/* Nothing. */
3803 		break;
3804 	}
3805 }
3806 
3807 #if 0
3808 /*
3809  * tlp_dm9102_reset:
3810  *
3811  *	Reset routine for the Davicom DM9102.
3812  */
3813 static void
3814 tlp_dm9102_reset(struct tulip_softc *sc)
3815 {
3816 
3817 	TULIP_WRITE(sc, CSR_DM_PHYSTAT, DM_PHYSTAT_GEPC|DM_PHYSTAT_GPED);
3818 	delay(100);
3819 	TULIP_WRITE(sc, CSR_DM_PHYSTAT, 0);
3820 }
3821 #endif
3822 
3823 /*****************************************************************************
3824  * Chip/board-specific media switches.  The ones here are ones that
3825  * are potentially common to multiple front-ends.
3826  *****************************************************************************/
3827 
3828 /*
3829  * This table is a common place for all sorts of media information,
3830  * keyed off of the SROM media code for that media.
3831  *
3832  * Note that we explicitly configure the 21142/21143 to always advertise
3833  * NWay capabilities when using the UTP port.
3834  * XXX Actually, we don't yet.
3835  */
3836 static const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
3837 	{ TULIP_ROM_MB_MEDIA_TP,	IFM_10_T,	0,
3838 	  "10baseT",
3839 	  OPMODE_TTM,
3840 	  BMSR_10THDX,
3841 	  { SIACONN_21040_10BASET,
3842 	    SIATXRX_21040_10BASET,
3843 	    SIAGEN_21040_10BASET },
3844 
3845 	  { SIACONN_21041_10BASET,
3846 	    SIATXRX_21041_10BASET,
3847 	    SIAGEN_21041_10BASET },
3848 
3849 	  { SIACONN_21142_10BASET,
3850 	    SIATXRX_21142_10BASET,
3851 	    SIAGEN_21142_10BASET } },
3852 
3853 	{ TULIP_ROM_MB_MEDIA_BNC,	IFM_10_2,	0,
3854 	  "10base2",
3855 	  0,
3856 	  0,
3857 	  { 0,
3858 	    0,
3859 	    0 },
3860 
3861 	  { SIACONN_21041_BNC,
3862 	    SIATXRX_21041_BNC,
3863 	    SIAGEN_21041_BNC },
3864 
3865 	  { SIACONN_21142_BNC,
3866 	    SIATXRX_21142_BNC,
3867 	    SIAGEN_21142_BNC } },
3868 
3869 	{ TULIP_ROM_MB_MEDIA_AUI,	IFM_10_5,	0,
3870 	  "10base5",
3871 	  0,
3872 	  0,
3873 	  { SIACONN_21040_AUI,
3874 	    SIATXRX_21040_AUI,
3875 	    SIAGEN_21040_AUI },
3876 
3877 	  { SIACONN_21041_AUI,
3878 	    SIATXRX_21041_AUI,
3879 	    SIAGEN_21041_AUI },
3880 
3881 	  { SIACONN_21142_AUI,
3882 	    SIATXRX_21142_AUI,
3883 	    SIAGEN_21142_AUI } },
3884 
3885 	{ TULIP_ROM_MB_MEDIA_100TX,	IFM_100_TX,	0,
3886 	  "100baseTX",
3887 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
3888 	  BMSR_100TXHDX,
3889 	  { 0,
3890 	    0,
3891 	    0 },
3892 
3893 	  { 0,
3894 	    0,
3895 	    0 },
3896 
3897 	  { 0,
3898 	    0,
3899 	    SIAGEN_ABM } },
3900 
3901 	{ TULIP_ROM_MB_MEDIA_TP_FDX,	IFM_10_T,	IFM_FDX,
3902 	  "10baseT-FDX",
3903 	  OPMODE_TTM|OPMODE_FD|OPMODE_HBD,
3904 	  BMSR_10TFDX,
3905 	  { SIACONN_21040_10BASET_FDX,
3906 	    SIATXRX_21040_10BASET_FDX,
3907 	    SIAGEN_21040_10BASET_FDX },
3908 
3909 	  { SIACONN_21041_10BASET_FDX,
3910 	    SIATXRX_21041_10BASET_FDX,
3911 	    SIAGEN_21041_10BASET_FDX },
3912 
3913 	  { SIACONN_21142_10BASET_FDX,
3914 	    SIATXRX_21142_10BASET_FDX,
3915 	    SIAGEN_21142_10BASET_FDX } },
3916 
3917 	{ TULIP_ROM_MB_MEDIA_100TX_FDX,	IFM_100_TX,	IFM_FDX,
3918 	  "100baseTX-FDX",
3919 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD|OPMODE_HBD,
3920 	  BMSR_100TXFDX,
3921 	  { 0,
3922 	    0,
3923 	    0 },
3924 
3925 	  { 0,
3926 	    0,
3927 	    0 },
3928 
3929 	  { 0,
3930 	    0,
3931 	    SIAGEN_ABM } },
3932 
3933 	{ TULIP_ROM_MB_MEDIA_100T4,	IFM_100_T4,	0,
3934 	  "100baseT4",
3935 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
3936 	  BMSR_100T4,
3937 	  { 0,
3938 	    0,
3939 	    0 },
3940 
3941 	  { 0,
3942 	    0,
3943 	    0 },
3944 
3945 	  { 0,
3946 	    0,
3947 	    SIAGEN_ABM } },
3948 
3949 	{ TULIP_ROM_MB_MEDIA_100FX,	IFM_100_FX,	0,
3950 	  "100baseFX",
3951 	  OPMODE_PS|OPMODE_PCS|OPMODE_HBD,
3952 	  0,
3953 	  { 0,
3954 	    0,
3955 	    0 },
3956 
3957 	  { 0,
3958 	    0,
3959 	    0 },
3960 
3961 	  { 0,
3962 	    0,
3963 	    SIAGEN_ABM } },
3964 
3965 	{ TULIP_ROM_MB_MEDIA_100FX_FDX,	IFM_100_FX,	IFM_FDX,
3966 	  "100baseFX-FDX",
3967 	  OPMODE_PS|OPMODE_PCS|OPMODE_FD|OPMODE_HBD,
3968 	  0,
3969 	  { 0,
3970 	    0,
3971 	    0 },
3972 
3973 	  { 0,
3974 	    0,
3975 	    0 },
3976 
3977 	  { 0,
3978 	    0,
3979 	    SIAGEN_ABM } },
3980 
3981 	{ 0,				0,		0,
3982 	  NULL,
3983 	  0,
3984 	  0,
3985 	  { 0,
3986 	    0,
3987 	    0 },
3988 
3989 	  { 0,
3990 	    0,
3991 	    0 },
3992 
3993 	  { 0,
3994 	    0,
3995 	    0 } },
3996 };
3997 
3998 static const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia(u_int8_t);
3999 static void	tlp_srom_media_info(struct tulip_softc *,
4000 		    const struct tulip_srom_to_ifmedia *,
4001 		    struct tulip_21x4x_media *);
4002 static void	tlp_add_srom_media(struct tulip_softc *, int,
4003 		    void (*)(struct tulip_softc *, struct ifmediareq *),
4004 		    int (*)(struct tulip_softc *), const u_int8_t *, int);
4005 static void	tlp_print_media(struct tulip_softc *);
4006 static void	tlp_nway_activate(struct tulip_softc *, int);
4007 static void	tlp_get_minst(struct tulip_softc *);
4008 
4009 static const struct tulip_srom_to_ifmedia *
4010 tlp_srom_to_ifmedia(u_int8_t sm)
4011 {
4012 	const struct tulip_srom_to_ifmedia *tsti;
4013 
4014 	for (tsti = tulip_srom_to_ifmedia_table;
4015 	     tsti->tsti_name != NULL; tsti++) {
4016 		if (tsti->tsti_srom == sm)
4017 			return (tsti);
4018 	}
4019 
4020 	return (NULL);
4021 }
4022 
4023 static void
4024 tlp_srom_media_info(struct tulip_softc *sc,
4025     const struct tulip_srom_to_ifmedia *tsti, struct tulip_21x4x_media *tm)
4026 {
4027 
4028 	tm->tm_name = tsti->tsti_name;
4029 	tm->tm_opmode = tsti->tsti_opmode;
4030 
4031 	sc->sc_sia_cap |= tsti->tsti_sia_cap;
4032 
4033 	switch (sc->sc_chip) {
4034 	case TULIP_CHIP_DE425:
4035 	case TULIP_CHIP_21040:
4036 		tm->tm_sia = tsti->tsti_21040;	/* struct assignment */
4037 		break;
4038 
4039 	case TULIP_CHIP_21041:
4040 		tm->tm_sia = tsti->tsti_21041;	/* struct assignment */
4041 		break;
4042 
4043 	case TULIP_CHIP_21142:
4044 	case TULIP_CHIP_21143:
4045 	case TULIP_CHIP_82C115:
4046 	case TULIP_CHIP_MX98715:
4047 	case TULIP_CHIP_MX98715A:
4048 	case TULIP_CHIP_MX98715AEC_X:
4049 	case TULIP_CHIP_MX98725:
4050 		tm->tm_sia = tsti->tsti_21142;	/* struct assignment */
4051 		break;
4052 
4053 	default:
4054 		/* Nothing. */
4055 		break;
4056 	}
4057 }
4058 
4059 static void
4060 tlp_add_srom_media(struct tulip_softc *sc, int type,
4061     void (*get)(struct tulip_softc *, struct ifmediareq *),
4062     int (*set)(struct tulip_softc *), const u_int8_t *list,
4063     int cnt)
4064 {
4065 	struct tulip_21x4x_media *tm;
4066 	const struct tulip_srom_to_ifmedia *tsti;
4067 	int i;
4068 
4069 	for (i = 0; i < cnt; i++) {
4070 		tsti = tlp_srom_to_ifmedia(list[i]);
4071 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4072 		tlp_srom_media_info(sc, tsti, tm);
4073 		tm->tm_type = type;
4074 		tm->tm_get = get;
4075 		tm->tm_set = set;
4076 
4077 		ifmedia_add(&sc->sc_mii.mii_media,
4078 		    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4079 		    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4080 	}
4081 }
4082 
4083 static void
4084 tlp_print_media(struct tulip_softc *sc)
4085 {
4086 	struct ifmedia_entry *ife;
4087 	struct tulip_21x4x_media *tm;
4088 	const char *sep = "";
4089 
4090 #define	PRINT(str)	printf("%s%s", sep, str); sep = ", "
4091 
4092 	printf("%s: ", sc->sc_dev.dv_xname);
4093 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
4094 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
4095 		tm = ife->ifm_aux;
4096 		if (tm == NULL) {
4097 #ifdef DIAGNOSTIC
4098 			if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
4099 				panic("tlp_print_media");
4100 #endif
4101 			PRINT("auto");
4102 		} else if (tm->tm_type != TULIP_ROM_MB_21140_MII &&
4103 			   tm->tm_type != TULIP_ROM_MB_21142_MII) {
4104 			PRINT(tm->tm_name);
4105 		}
4106 	}
4107 	printf("\n");
4108 
4109 #undef PRINT
4110 }
4111 
4112 static void
4113 tlp_nway_activate(struct tulip_softc *sc, int media)
4114 {
4115 	struct ifmedia_entry *ife;
4116 
4117 	ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0);
4118 #ifdef DIAGNOSTIC
4119 	if (ife == NULL)
4120 		panic("tlp_nway_activate");
4121 #endif
4122 	sc->sc_nway_active = ife;
4123 }
4124 
4125 static void
4126 tlp_get_minst(struct tulip_softc *sc)
4127 {
4128 
4129 	if ((sc->sc_media_seen &
4130 	    ~((1 << TULIP_ROM_MB_21140_MII) |
4131 	      (1 << TULIP_ROM_MB_21142_MII))) == 0) {
4132 		/*
4133 		 * We have not yet seen any SIA/SYM media (but are
4134 		 * about to; that's why we're called!), so assign
4135 		 * the current media instance to be the `internal media'
4136 		 * instance, and advance it so any MII media gets a
4137 		 * fresh one (used to selecting/isolating a PHY).
4138 		 */
4139 		sc->sc_tlp_minst = sc->sc_mii.mii_instance++;
4140 	}
4141 }
4142 
4143 /*
4144  * SIA Utility functions.
4145  */
4146 static void	tlp_sia_update_link(struct tulip_softc *);
4147 static void	tlp_sia_get(struct tulip_softc *, struct ifmediareq *);
4148 static int	tlp_sia_set(struct tulip_softc *);
4149 static int	tlp_sia_media(struct tulip_softc *, struct ifmedia_entry *);
4150 static void	tlp_sia_fixup(struct tulip_softc *);
4151 
4152 static void
4153 tlp_sia_update_link(struct tulip_softc *sc)
4154 {
4155 	struct ifmedia_entry *ife;
4156 	struct tulip_21x4x_media *tm;
4157 	u_int32_t siastat;
4158 
4159 	ife = TULIP_CURRENT_MEDIA(sc);
4160 	tm = ife->ifm_aux;
4161 
4162 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
4163 
4164 	siastat = TULIP_READ(sc, CSR_SIASTAT);
4165 
4166 	/*
4167 	 * Note that when we do SIA link tests, we are assuming that
4168 	 * the chip is really in the mode that the current media setting
4169 	 * reflects.  If we're not, then the link tests will not be
4170 	 * accurate!
4171 	 */
4172 	switch (IFM_SUBTYPE(ife->ifm_media)) {
4173 	case IFM_10_T:
4174 		sc->sc_flags |= TULIPF_LINK_VALID;
4175 		if ((siastat & SIASTAT_LS10) == 0)
4176 			sc->sc_flags |= TULIPF_LINK_UP;
4177 		break;
4178 
4179 	case IFM_100_TX:
4180 	case IFM_100_T4:
4181 		sc->sc_flags |= TULIPF_LINK_VALID;
4182 		if ((siastat & SIASTAT_LS100) == 0)
4183 			sc->sc_flags |= TULIPF_LINK_UP;
4184 		break;
4185 	}
4186 
4187 	switch (sc->sc_chip) {
4188 	case TULIP_CHIP_21142:
4189 	case TULIP_CHIP_21143:
4190 		/*
4191 		 * On these chips, we can tell more information about
4192 		 * AUI/BNC.  Note that the AUI/BNC selection is made
4193 		 * in a different register; for our purpose, it's all
4194 		 * AUI.
4195 		 */
4196 		switch (IFM_SUBTYPE(ife->ifm_media)) {
4197 		case IFM_10_2:
4198 		case IFM_10_5:
4199 			sc->sc_flags |= TULIPF_LINK_VALID;
4200 			if (siastat & SIASTAT_ARA) {
4201 				TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA);
4202 				sc->sc_flags |= TULIPF_LINK_UP;
4203 			}
4204 			break;
4205 
4206 		default:
4207 			/*
4208 			 * If we're SYM media and can detect the link
4209 			 * via the GPIO facility, prefer that status
4210 			 * over LS100.
4211 			 */
4212 			if (tm->tm_type == TULIP_ROM_MB_21143_SYM &&
4213 			    tm->tm_actmask != 0) {
4214 				sc->sc_flags = (sc->sc_flags &
4215 				    ~TULIPF_LINK_UP) | TULIPF_LINK_VALID;
4216 				if (TULIP_ISSET(sc, CSR_SIAGEN,
4217 				    tm->tm_actmask) == tm->tm_actdata)
4218 					sc->sc_flags |= TULIPF_LINK_UP;
4219 			}
4220 		}
4221 		break;
4222 
4223 	default:
4224 		/* Nothing. */
4225 		break;
4226 	}
4227 }
4228 
4229 static void
4230 tlp_sia_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
4231 {
4232 	struct ifmedia_entry *ife;
4233 
4234 	ifmr->ifm_status = 0;
4235 
4236 	tlp_sia_update_link(sc);
4237 
4238 	ife = TULIP_CURRENT_MEDIA(sc);
4239 
4240 	if (sc->sc_flags & TULIPF_LINK_VALID)
4241 		ifmr->ifm_status |= IFM_AVALID;
4242 	if (sc->sc_flags & TULIPF_LINK_UP)
4243 		ifmr->ifm_status |= IFM_ACTIVE;
4244 	ifmr->ifm_active = ife->ifm_media;
4245 }
4246 
4247 static void
4248 tlp_sia_fixup(struct tulip_softc *sc)
4249 {
4250 	struct ifmedia_entry *ife;
4251 	struct tulip_21x4x_media *tm;
4252 	u_int32_t siaconn, siatxrx, siagen;
4253 
4254 	switch (sc->sc_chip) {
4255 	case TULIP_CHIP_82C115:
4256 	case TULIP_CHIP_MX98713A:
4257 	case TULIP_CHIP_MX98715:
4258 	case TULIP_CHIP_MX98715A:
4259 	case TULIP_CHIP_MX98715AEC_X:
4260 	case TULIP_CHIP_MX98725:
4261 		siaconn = PMAC_SIACONN_MASK;
4262 		siatxrx = PMAC_SIATXRX_MASK;
4263 		siagen  = PMAC_SIAGEN_MASK;
4264 		break;
4265 
4266 	default:
4267 		/* No fixups required on any other chips. */
4268 		return;
4269 	}
4270 
4271 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
4272 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
4273 		tm = ife->ifm_aux;
4274 		if (tm == NULL)
4275 			continue;
4276 
4277 		tm->tm_siaconn &= siaconn;
4278 		tm->tm_siatxrx &= siatxrx;
4279 		tm->tm_siagen  &= siagen;
4280 	}
4281 }
4282 
4283 static int
4284 tlp_sia_set(struct tulip_softc *sc)
4285 {
4286 
4287 	return (tlp_sia_media(sc, TULIP_CURRENT_MEDIA(sc)));
4288 }
4289 
4290 static int
4291 tlp_sia_media(struct tulip_softc *sc, struct ifmedia_entry *ife)
4292 {
4293 	struct tulip_21x4x_media *tm;
4294 
4295 	tm = ife->ifm_aux;
4296 
4297 	/*
4298 	 * XXX This appears to be necessary on a bunch of the clone chips.
4299 	 */
4300 	delay(20000);
4301 
4302 	/*
4303 	 * Idle the chip.
4304 	 */
4305 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
4306 
4307 	/*
4308 	 * Program the SIA.  It's important to write in this order,
4309 	 * resetting the SIA first.
4310 	 */
4311 	TULIP_WRITE(sc, CSR_SIACONN, 0);		/* SRL bit clear */
4312 	delay(1000);
4313 
4314 	TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx);
4315 
4316 	switch (sc->sc_chip) {
4317 	case TULIP_CHIP_21142:
4318 	case TULIP_CHIP_21143:
4319 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl);
4320 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata);
4321 		break;
4322 	default:
4323 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen);
4324 	}
4325 
4326 	TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn);
4327 
4328 	/*
4329 	 * Set the OPMODE bits for this media and write OPMODE.
4330 	 * This will resume the transmit and receive processes.
4331 	 */
4332 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
4333 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
4334 
4335 	return (0);
4336 }
4337 
4338 /*
4339  * 21140 GPIO utility functions.
4340  */
4341 static void	tlp_21140_gpio_update_link(struct tulip_softc *);
4342 
4343 static void
4344 tlp_21140_gpio_update_link(struct tulip_softc *sc)
4345 {
4346 	struct ifmedia_entry *ife;
4347 	struct tulip_21x4x_media *tm;
4348 
4349 	ife = TULIP_CURRENT_MEDIA(sc);
4350 	tm = ife->ifm_aux;
4351 
4352 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
4353 
4354 	if (tm->tm_actmask != 0) {
4355 		sc->sc_flags |= TULIPF_LINK_VALID;
4356 		if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
4357 		    tm->tm_actdata)
4358 			sc->sc_flags |= TULIPF_LINK_UP;
4359 	}
4360 }
4361 
4362 void
4363 tlp_21140_gpio_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
4364 {
4365 	struct ifmedia_entry *ife;
4366 
4367 	ifmr->ifm_status = 0;
4368 
4369 	tlp_21140_gpio_update_link(sc);
4370 
4371 	ife = TULIP_CURRENT_MEDIA(sc);
4372 
4373 	if (sc->sc_flags & TULIPF_LINK_VALID)
4374 		ifmr->ifm_status |= IFM_AVALID;
4375 	if (sc->sc_flags & TULIPF_LINK_UP)
4376 		ifmr->ifm_status |= IFM_ACTIVE;
4377 	ifmr->ifm_active = ife->ifm_media;
4378 }
4379 
4380 int
4381 tlp_21140_gpio_set(struct tulip_softc *sc)
4382 {
4383 	struct ifmedia_entry *ife;
4384 	struct tulip_21x4x_media *tm;
4385 
4386 	ife = TULIP_CURRENT_MEDIA(sc);
4387 	tm = ife->ifm_aux;
4388 
4389 	/*
4390 	 * Idle the chip.
4391 	 */
4392 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
4393 
4394 	/*
4395 	 * Set the GPIO pins for this media, to flip any
4396 	 * relays, etc.
4397 	 */
4398 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
4399 	delay(10);
4400 	TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);
4401 
4402 	/*
4403 	 * Set the OPMODE bits for this media and write OPMODE.
4404 	 * This will resume the transmit and receive processes.
4405 	 */
4406 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
4407 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
4408 
4409 	return (0);
4410 }
4411 
4412 /*
4413  * 21040 and 21041 media switches.
4414  */
4415 static void	tlp_21040_tmsw_init(struct tulip_softc *);
4416 static void	tlp_21040_tp_tmsw_init(struct tulip_softc *);
4417 static void	tlp_21040_auibnc_tmsw_init(struct tulip_softc *);
4418 static void	tlp_21041_tmsw_init(struct tulip_softc *);
4419 
4420 const struct tulip_mediasw tlp_21040_mediasw = {
4421 	tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set
4422 };
4423 
4424 const struct tulip_mediasw tlp_21040_tp_mediasw = {
4425 	tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set
4426 };
4427 
4428 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
4429 	tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set
4430 };
4431 
4432 const struct tulip_mediasw tlp_21041_mediasw = {
4433 	tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set
4434 };
4435 
4436 static void
4437 tlp_21040_tmsw_init(struct tulip_softc *sc)
4438 {
4439 	static const u_int8_t media[] = {
4440 		TULIP_ROM_MB_MEDIA_TP,
4441 		TULIP_ROM_MB_MEDIA_TP_FDX,
4442 		TULIP_ROM_MB_MEDIA_AUI,
4443 	};
4444 	struct tulip_21x4x_media *tm;
4445 
4446 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4447 	    tlp_mediastatus);
4448 
4449 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 3);
4450 
4451 	/*
4452 	 * No SROM type for External SIA.
4453 	 */
4454 	tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4455 	tm->tm_name = "manual";
4456 	tm->tm_opmode = 0;
4457 	tm->tm_siaconn = SIACONN_21040_EXTSIA;
4458 	tm->tm_siatxrx = SIATXRX_21040_EXTSIA;
4459 	tm->tm_siagen  = SIAGEN_21040_EXTSIA;
4460 	ifmedia_add(&sc->sc_mii.mii_media,
4461 	    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm);
4462 
4463 	/*
4464 	 * XXX Autosense not yet supported.
4465 	 */
4466 
4467 	/* XXX This should be auto-sense. */
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_tp_tmsw_init(struct tulip_softc *sc)
4475 {
4476 	static const u_int8_t media[] = {
4477 		TULIP_ROM_MB_MEDIA_TP,
4478 		TULIP_ROM_MB_MEDIA_TP_FDX,
4479 	};
4480 
4481 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4482 	    tlp_mediastatus);
4483 
4484 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 2);
4485 
4486 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
4487 
4488 	tlp_print_media(sc);
4489 }
4490 
4491 static void
4492 tlp_21040_auibnc_tmsw_init(struct tulip_softc *sc)
4493 {
4494 	static const u_int8_t media[] = {
4495 		TULIP_ROM_MB_MEDIA_AUI,
4496 	};
4497 
4498 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4499 	    tlp_mediastatus);
4500 
4501 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 1);
4502 
4503 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
4504 
4505 	tlp_print_media(sc);
4506 }
4507 
4508 static void
4509 tlp_21041_tmsw_init(struct tulip_softc *sc)
4510 {
4511 	static const u_int8_t media[] = {
4512 		TULIP_ROM_MB_MEDIA_TP,
4513 		TULIP_ROM_MB_MEDIA_TP_FDX,
4514 		TULIP_ROM_MB_MEDIA_BNC,
4515 		TULIP_ROM_MB_MEDIA_AUI,
4516 	};
4517 	int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
4518 	const struct tulip_srom_to_ifmedia *tsti;
4519 	struct tulip_21x4x_media *tm;
4520 	u_int16_t romdef;
4521 	u_int8_t mb;
4522 
4523 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4524 	    tlp_mediastatus);
4525 
4526 	if (tlp_isv_srom(sc->sc_srom) == 0) {
4527  not_isv_srom:
4528 		/*
4529 		 * If we have a board without the standard 21041 SROM format,
4530 		 * we just assume all media are present and try and pick a
4531 		 * reasonable default.
4532 		 */
4533 		tlp_add_srom_media(sc, 0, NULL, NULL, media, 4);
4534 
4535 		/*
4536 		 * XXX Autosense not yet supported.
4537 		 */
4538 
4539 		/* XXX This should be auto-sense. */
4540 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
4541 
4542 		tlp_print_media(sc);
4543 		return;
4544 	}
4545 
4546 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
4547 	for (i = 0; i < devcnt; i++) {
4548 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
4549 			break;
4550 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
4551 		    sc->sc_devno)
4552 			break;
4553 	}
4554 
4555 	if (i == devcnt)
4556 		goto not_isv_srom;
4557 
4558 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
4559 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
4560 	mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
4561 	m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
4562 
4563 	for (; m_cnt != 0;
4564 	     m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
4565 		mb = sc->sc_srom[mb_offset];
4566 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4567 		switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
4568 		case TULIP_ROM_MB_MEDIA_TP_FDX:
4569 		case TULIP_ROM_MB_MEDIA_TP:
4570 		case TULIP_ROM_MB_MEDIA_BNC:
4571 		case TULIP_ROM_MB_MEDIA_AUI:
4572 			tsti = tlp_srom_to_ifmedia(mb &
4573 			    TULIP_ROM_MB_MEDIA_CODE);
4574 
4575 			tlp_srom_media_info(sc, tsti, tm);
4576 
4577 			/*
4578 			 * Override our default SIA settings if the
4579 			 * SROM contains its own.
4580 			 */
4581 			if (mb & TULIP_ROM_MB_EXT) {
4582 				tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom,
4583 				    mb_offset + TULIP_ROM_MB_CSR13);
4584 				tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom,
4585 				    mb_offset + TULIP_ROM_MB_CSR14);
4586 				tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom,
4587 				    mb_offset + TULIP_ROM_MB_CSR15);
4588 			}
4589 
4590 			ifmedia_add(&sc->sc_mii.mii_media,
4591 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4592 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4593 			break;
4594 
4595 		default:
4596 			printf("%s: unknown media code 0x%02x\n",
4597 			    sc->sc_dev.dv_xname,
4598 			    mb & TULIP_ROM_MB_MEDIA_CODE);
4599 			free(tm, M_DEVBUF);
4600 		}
4601 	}
4602 
4603 	/*
4604 	 * XXX Autosense not yet supported.
4605 	 */
4606 
4607 	romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
4608 	    TULIP_ROM_IL_SELECT_CONN_TYPE);
4609 	switch (romdef) {
4610 	case SELECT_CONN_TYPE_TP:
4611 	case SELECT_CONN_TYPE_TP_AUTONEG:
4612 	case SELECT_CONN_TYPE_TP_NOLINKPASS:
4613 		defmedia = IFM_ETHER|IFM_10_T;
4614 		break;
4615 
4616 	case SELECT_CONN_TYPE_TP_FDX:
4617 		defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
4618 		break;
4619 
4620 	case SELECT_CONN_TYPE_BNC:
4621 		defmedia = IFM_ETHER|IFM_10_2;
4622 		break;
4623 
4624 	case SELECT_CONN_TYPE_AUI:
4625 		defmedia = IFM_ETHER|IFM_10_5;
4626 		break;
4627 #if 0 /* XXX */
4628 	case SELECT_CONN_TYPE_ASENSE:
4629 	case SELECT_CONN_TYPE_ASENSE_AUTONEG:
4630 		defmedia = IFM_ETHER|IFM_AUTO;
4631 		break;
4632 #endif
4633 	default:
4634 		defmedia = 0;
4635 	}
4636 
4637 	if (defmedia == 0) {
4638 		/*
4639 		 * XXX We should default to auto-sense.
4640 		 */
4641 		defmedia = IFM_ETHER|IFM_10_T;
4642 	}
4643 
4644 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
4645 
4646 	tlp_print_media(sc);
4647 }
4648 
4649 /*
4650  * DECchip 2114x ISV media switch.
4651  */
4652 static void	tlp_2114x_isv_tmsw_init(struct tulip_softc *);
4653 static void	tlp_2114x_isv_tmsw_get(struct tulip_softc *,
4654 		    struct ifmediareq *);
4655 static int	tlp_2114x_isv_tmsw_set(struct tulip_softc *);
4656 
4657 const struct tulip_mediasw tlp_2114x_isv_mediasw = {
4658 	tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
4659 };
4660 
4661 static void	tlp_2114x_nway_get(struct tulip_softc *, struct ifmediareq *);
4662 static int	tlp_2114x_nway_set(struct tulip_softc *);
4663 
4664 static void	tlp_2114x_nway_statchg(struct device *);
4665 static int	tlp_2114x_nway_service(struct tulip_softc *, int);
4666 static void	tlp_2114x_nway_auto(struct tulip_softc *);
4667 static void	tlp_2114x_nway_status(struct tulip_softc *);
4668 
4669 static void
4670 tlp_2114x_isv_tmsw_init(struct tulip_softc *sc)
4671 {
4672 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
4673 	struct ifmedia_entry *ife;
4674 	struct mii_softc *phy;
4675 	struct tulip_21x4x_media *tm;
4676 	const struct tulip_srom_to_ifmedia *tsti;
4677 	int i, devcnt, leaf_offset, m_cnt, type, length;
4678 	int defmedia, miidef;
4679 	u_int16_t word;
4680 	u_int8_t *cp, *ncp;
4681 
4682 	defmedia = miidef = 0;
4683 
4684 	sc->sc_mii.mii_ifp = ifp;
4685 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
4686 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
4687 	sc->sc_mii.mii_statchg = sc->sc_statchg;
4688 
4689 	/*
4690 	 * Ignore `instance'; we may get a mixture of SIA and MII
4691 	 * media, and `instance' is used to isolate or select the
4692 	 * PHY on the MII as appropriate.  Note that duplicate media
4693 	 * are disallowed, so ignoring `instance' is safe.
4694 	 */
4695 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, tlp_mediachange,
4696 	    tlp_mediastatus);
4697 
4698 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
4699 	for (i = 0; i < devcnt; i++) {
4700 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
4701 			break;
4702 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
4703 		    sc->sc_devno)
4704 			break;
4705 	}
4706 
4707 	if (i == devcnt) {
4708 		printf("%s: unable to locate info leaf in SROM\n",
4709 		    sc->sc_dev.dv_xname);
4710 		return;
4711 	}
4712 
4713 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
4714 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
4715 
4716 	/* XXX SELECT CONN TYPE */
4717 
4718 	cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
4719 
4720 	/*
4721 	 * On some chips, the first thing in the Info Leaf is the
4722 	 * GPIO pin direction data.
4723 	 */
4724 	switch (sc->sc_chip) {
4725 	case TULIP_CHIP_21140:
4726 	case TULIP_CHIP_21140A:
4727 	case TULIP_CHIP_MX98713:
4728 	case TULIP_CHIP_AX88140:
4729 	case TULIP_CHIP_AX88141:
4730 		sc->sc_gp_dir = *cp++;
4731 		break;
4732 
4733 	default:
4734 		/* Nothing. */
4735 		break;
4736 	}
4737 
4738 	/* Get the media count. */
4739 	m_cnt = *cp++;
4740 
4741 	if (m_cnt == 0) {
4742 		sc->sc_mediasw = &tlp_sio_mii_mediasw;
4743 		(*sc->sc_mediasw->tmsw_init)(sc);
4744 		return;
4745 	}
4746 
4747 	for (; m_cnt != 0; cp = ncp, m_cnt--) {
4748 		/*
4749 		 * Determine the type and length of this media block.
4750 		 * The 21143 is spec'd to always use extended format blocks,
4751 		 * but some cards don't set the bit to indicate this.
4752 		 * Hopefully there are no cards which really don't use
4753 		 * extended format blocks.
4754 		 */
4755 		if ((*cp & 0x80) == 0 && sc->sc_chip != TULIP_CHIP_21143) {
4756 			length = 4;
4757 			type = TULIP_ROM_MB_21140_GPR;
4758 		} else {
4759 			length = (*cp++ & 0x7f) - 1;
4760 			type = *cp++ & 0x3f;
4761 		}
4762 
4763 		/* Compute the start of the next block. */
4764 		ncp = cp + length;
4765 
4766 		/* Now, parse the block. */
4767 		switch (type) {
4768 		case TULIP_ROM_MB_21140_GPR:
4769 			tlp_get_minst(sc);
4770 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR;
4771 
4772 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4773 
4774 			tm->tm_type = TULIP_ROM_MB_21140_GPR;
4775 			tm->tm_get = tlp_21140_gpio_get;
4776 			tm->tm_set = tlp_21140_gpio_set;
4777 
4778 			/* First is the media type code. */
4779 			tsti = tlp_srom_to_ifmedia(cp[0] &
4780 			    TULIP_ROM_MB_MEDIA_CODE);
4781 			if (tsti == NULL) {
4782 				/* Invalid media code. */
4783 				free(tm, M_DEVBUF);
4784 				break;
4785 			}
4786 
4787 			/* Get defaults. */
4788 			tlp_srom_media_info(sc, tsti, tm);
4789 
4790 			/* Next is any GPIO info for this media. */
4791 			tm->tm_gpdata = cp[1];
4792 
4793 			/*
4794 			 * Next is a word containing OPMODE information
4795 			 * and info on how to detect if this media is
4796 			 * active.
4797 			 */
4798 			word = TULIP_ROM_GETW(cp, 2);
4799 			tm->tm_opmode &= OPMODE_FD;
4800 			tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word);
4801 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
4802 				tm->tm_actmask =
4803 				    TULIP_ROM_MB_BITPOS(word);
4804 				tm->tm_actdata =
4805 				    (word & TULIP_ROM_MB_POLARITY) ?
4806 				    0 : tm->tm_actmask;
4807 			}
4808 
4809 			ifmedia_add(&sc->sc_mii.mii_media,
4810 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4811 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4812 			break;
4813 
4814 		case TULIP_ROM_MB_21140_MII:
4815 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII;
4816 
4817 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4818 
4819 			tm->tm_type = TULIP_ROM_MB_21140_MII;
4820 			tm->tm_get = tlp_mii_getmedia;
4821 			tm->tm_set = tlp_mii_setmedia;
4822 			tm->tm_opmode = OPMODE_PS;
4823 
4824 			if (sc->sc_reset == NULL)
4825 				sc->sc_reset = tlp_21140_reset;
4826 
4827 			/* First is the PHY number. */
4828 			tm->tm_phyno = *cp++;
4829 
4830 			/* Next is the MII select sequence length and offset. */
4831 			tm->tm_gp_length = *cp++;
4832 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
4833 			cp += tm->tm_gp_length;
4834 
4835 			/* Next is the MII reset sequence length and offset. */
4836 			tm->tm_reset_length = *cp++;
4837 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
4838 			cp += tm->tm_reset_length;
4839 
4840 			/*
4841 			 * The following items are left in the media block
4842 			 * that we don't particularly care about:
4843 			 *
4844 			 *	capabilities		W
4845 			 *	advertisement		W
4846 			 *	full duplex		W
4847 			 *	tx threshold		W
4848 			 *
4849 			 * These appear to be bits in the PHY registers,
4850 			 * which our MII code handles on its own.
4851 			 */
4852 
4853 			/*
4854 			 * Before we probe the MII bus, we need to reset
4855 			 * it and issue the selection sequence.
4856 			 */
4857 
4858 			/* Set the direction of the pins... */
4859 			TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
4860 
4861 			for (i = 0; i < tm->tm_reset_length; i++) {
4862 				delay(10);
4863 				TULIP_WRITE(sc, CSR_GPP,
4864 				    sc->sc_srom[tm->tm_reset_offset + i]);
4865 			}
4866 
4867 			for (i = 0; i < tm->tm_gp_length; i++) {
4868 				delay(10);
4869 				TULIP_WRITE(sc, CSR_GPP,
4870 				    sc->sc_srom[tm->tm_gp_offset + i]);
4871 			}
4872 
4873 			/* If there were no sequences, just lower the pins. */
4874 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
4875 				delay(10);
4876 				TULIP_WRITE(sc, CSR_GPP, 0);
4877 			}
4878 
4879 			/*
4880 			 * Now, probe the MII for the PHY.  Note, we know
4881 			 * the location of the PHY on the bus, but we don't
4882 			 * particularly care; the MII code just likes to
4883 			 * search the whole thing anyhow.
4884 			 */
4885 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
4886 			    MII_PHY_ANY, tm->tm_phyno, 0);
4887 
4888 			/*
4889 			 * Now, search for the PHY we hopefully just
4890 			 * configured.  If it's not configured into the
4891 			 * kernel, we lose.  The PHY's default media always
4892 			 * takes priority.
4893 			 */
4894 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
4895 			     phy != NULL;
4896 			     phy = LIST_NEXT(phy, mii_list))
4897 				if (phy->mii_offset == tm->tm_phyno)
4898 					break;
4899 			if (phy == NULL) {
4900 				printf("%s: unable to configure MII\n",
4901 				    sc->sc_dev.dv_xname);
4902 				break;
4903 			}
4904 
4905 			sc->sc_flags |= TULIPF_HAS_MII;
4906 			sc->sc_tick = tlp_mii_tick;
4907 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
4908 			    phy->mii_inst);
4909 
4910 			/*
4911 			 * Okay, now that we've found the PHY and the MII
4912 			 * layer has added all of the media associated
4913 			 * with that PHY, we need to traverse the media
4914 			 * list, and add our `tm' to each entry's `aux'
4915 			 * pointer.
4916 			 *
4917 			 * We do this by looking for media with our
4918 			 * PHY's `instance'.
4919 			 */
4920 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
4921 			     ife != NULL;
4922 			     ife = TAILQ_NEXT(ife, ifm_list)) {
4923 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
4924 					continue;
4925 				ife->ifm_aux = tm;
4926 			}
4927 			break;
4928 
4929 		case TULIP_ROM_MB_21142_SIA:
4930 			tlp_get_minst(sc);
4931 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA;
4932 
4933 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4934 
4935 			tm->tm_type = TULIP_ROM_MB_21142_SIA;
4936 			tm->tm_get = tlp_sia_get;
4937 			tm->tm_set = tlp_sia_set;
4938 
4939 			/* First is the media type code. */
4940 			tsti = tlp_srom_to_ifmedia(cp[0] &
4941 			    TULIP_ROM_MB_MEDIA_CODE);
4942 			if (tsti == NULL) {
4943 				/* Invalid media code. */
4944 				free(tm, M_DEVBUF);
4945 				break;
4946 			}
4947 
4948 			/* Get defaults. */
4949 			tlp_srom_media_info(sc, tsti, tm);
4950 
4951 			/*
4952 			 * Override our default SIA settings if the
4953 			 * SROM contains its own.
4954 			 */
4955 			if (cp[0] & 0x40) {
4956 				tm->tm_siaconn = TULIP_ROM_GETW(cp, 1);
4957 				tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3);
4958 				tm->tm_siagen  = TULIP_ROM_GETW(cp, 5);
4959 				cp += 7;
4960 			} else
4961 				cp++;
4962 
4963 			/* Next is GPIO control/data. */
4964 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 0) << 16;
4965 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 2) << 16;
4966 
4967 			ifmedia_add(&sc->sc_mii.mii_media,
4968 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4969 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4970 			break;
4971 
4972 		case TULIP_ROM_MB_21142_MII:
4973 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_MII;
4974 
4975 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4976 
4977 			tm->tm_type = TULIP_ROM_MB_21142_MII;
4978 			tm->tm_get = tlp_mii_getmedia;
4979 			tm->tm_set = tlp_mii_setmedia;
4980 			tm->tm_opmode = OPMODE_PS;
4981 
4982 			if (sc->sc_reset == NULL)
4983 				sc->sc_reset = tlp_21142_reset;
4984 
4985 			/* First is the PHY number. */
4986 			tm->tm_phyno = *cp++;
4987 
4988 			/* Next is the MII select sequence length and offset. */
4989 			tm->tm_gp_length = *cp++;
4990 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
4991 			cp += tm->tm_gp_length * 2;
4992 
4993 			/* Next is the MII reset sequence length and offset. */
4994 			tm->tm_reset_length = *cp++;
4995 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
4996 			cp += tm->tm_reset_length * 2;
4997 
4998 			/*
4999 			 * The following items are left in the media block
5000 			 * that we don't particularly care about:
5001 			 *
5002 			 *	capabilities		W
5003 			 *	advertisement		W
5004 			 *	full duplex		W
5005 			 *	tx threshold		W
5006 			 *	MII interrupt		W
5007 			 *
5008 			 * These appear to be bits in the PHY registers,
5009 			 * which our MII code handles on its own.
5010 			 */
5011 
5012 			/*
5013 			 * Before we probe the MII bus, we need to reset
5014 			 * it and issue the selection sequence.
5015 			 */
5016 
5017 			cp = &sc->sc_srom[tm->tm_reset_offset];
5018 			for (i = 0; i < tm->tm_reset_length; i++, cp += 2) {
5019 				delay(10);
5020 				TULIP_WRITE(sc, CSR_SIAGEN,
5021 				    TULIP_ROM_GETW(cp, 0) << 16);
5022 			}
5023 
5024 			cp = &sc->sc_srom[tm->tm_gp_offset];
5025 			for (i = 0; i < tm->tm_gp_length; i++, cp += 2) {
5026 				delay(10);
5027 				TULIP_WRITE(sc, CSR_SIAGEN,
5028 				    TULIP_ROM_GETW(cp, 0) << 16);
5029 			}
5030 
5031 			/* If there were no sequences, just lower the pins. */
5032 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
5033 				delay(10);
5034 				TULIP_WRITE(sc, CSR_SIAGEN, 0);
5035 			}
5036 
5037 			/*
5038 			 * Now, probe the MII for the PHY.  Note, we know
5039 			 * the location of the PHY on the bus, but we don't
5040 			 * particularly care; the MII code just likes to
5041 			 * search the whole thing anyhow.
5042 			 */
5043 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
5044 			    MII_PHY_ANY, tm->tm_phyno, 0);
5045 
5046 			/*
5047 			 * Now, search for the PHY we hopefully just
5048 			 * configured.  If it's not configured into the
5049 			 * kernel, we lose.  The PHY's default media always
5050 			 * takes priority.
5051 			 */
5052 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
5053 			     phy != NULL;
5054 			     phy = LIST_NEXT(phy, mii_list))
5055 				if (phy->mii_offset == tm->tm_phyno)
5056 					break;
5057 			if (phy == NULL) {
5058 				printf("%s: unable to configure MII\n",
5059 				    sc->sc_dev.dv_xname);
5060 				break;
5061 			}
5062 
5063 			sc->sc_flags |= TULIPF_HAS_MII;
5064 			sc->sc_tick = tlp_mii_tick;
5065 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
5066 			    phy->mii_inst);
5067 
5068 			/*
5069 			 * Okay, now that we've found the PHY and the MII
5070 			 * layer has added all of the media associated
5071 			 * with that PHY, we need to traverse the media
5072 			 * list, and add our `tm' to each entry's `aux'
5073 			 * pointer.
5074 			 *
5075 			 * We do this by looking for media with our
5076 			 * PHY's `instance'.
5077 			 */
5078 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
5079 			     ife != NULL;
5080 			     ife = TAILQ_NEXT(ife, ifm_list)) {
5081 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
5082 					continue;
5083 				ife->ifm_aux = tm;
5084 			}
5085 			break;
5086 
5087 		case TULIP_ROM_MB_21143_SYM:
5088 			tlp_get_minst(sc);
5089 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM;
5090 
5091 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
5092 
5093 			tm->tm_type = TULIP_ROM_MB_21143_SYM;
5094 			tm->tm_get = tlp_sia_get;
5095 			tm->tm_set = tlp_sia_set;
5096 
5097 			/* First is the media type code. */
5098 			tsti = tlp_srom_to_ifmedia(cp[0] &
5099 			    TULIP_ROM_MB_MEDIA_CODE);
5100 			if (tsti == NULL) {
5101 				/* Invalid media code. */
5102 				free(tm, M_DEVBUF);
5103 				break;
5104 			}
5105 
5106 			/* Get defaults. */
5107 			tlp_srom_media_info(sc, tsti, tm);
5108 
5109 			/* Next is GPIO control/data. */
5110 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 1) << 16;
5111 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 3) << 16;
5112 
5113 			/*
5114 			 * Next is a word containing OPMODE information
5115 			 * and info on how to detect if this media is
5116 			 * active.
5117 			 */
5118 			word = TULIP_ROM_GETW(cp, 5);
5119 			tm->tm_opmode &= OPMODE_FD;
5120 			tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word);
5121 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
5122 				tm->tm_actmask =
5123 				    TULIP_ROM_MB_BITPOS(word);
5124 				tm->tm_actdata =
5125 				    (word & TULIP_ROM_MB_POLARITY) ?
5126 				    0 : tm->tm_actmask;
5127 			}
5128 
5129 			ifmedia_add(&sc->sc_mii.mii_media,
5130 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
5131 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
5132 			break;
5133 
5134 		case TULIP_ROM_MB_21143_RESET:
5135 			printf("%s: 21143 reset block\n", sc->sc_dev.dv_xname);
5136 			break;
5137 
5138 		default:
5139 			printf("%s: unknown ISV media block type 0x%02x\n",
5140 			    sc->sc_dev.dv_xname, type);
5141 		}
5142 	}
5143 
5144 	/*
5145 	 * Deal with the case where no media is configured.
5146 	 */
5147 	if (TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list) == NULL) {
5148 		printf("%s: no media found!\n", sc->sc_dev.dv_xname);
5149 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
5150 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
5151 		return;
5152 	}
5153 
5154 	/*
5155 	 * Pick the default media.
5156 	 */
5157 	if (miidef != 0)
5158 		defmedia = miidef;
5159 	else {
5160 		switch (sc->sc_chip) {
5161 		case TULIP_CHIP_21140:
5162 		case TULIP_CHIP_21140A:
5163 			/* XXX should come from SROM */
5164 			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
5165 			if (ifmedia_match(&sc->sc_mii.mii_media, defmedia,
5166 				sc->sc_mii.mii_media.ifm_mask) == NULL) {
5167 				/*
5168 				 * There is not a 10baseT media.
5169 				 * Fall back to the first found one.
5170 				 */
5171 				ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
5172 				defmedia = ife->ifm_media;
5173 			}
5174 			break;
5175 
5176 		case TULIP_CHIP_21142:
5177 		case TULIP_CHIP_21143:
5178 		case TULIP_CHIP_MX98713A:
5179 		case TULIP_CHIP_MX98715:
5180 		case TULIP_CHIP_MX98715A:
5181 		case TULIP_CHIP_MX98715AEC_X:
5182 		case TULIP_CHIP_MX98725:
5183 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
5184 			tm->tm_name = "auto";
5185 			tm->tm_get = tlp_2114x_nway_get;
5186 			tm->tm_set = tlp_2114x_nway_set;
5187 
5188 			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0);
5189 			ifmedia_add(&sc->sc_mii.mii_media, defmedia, 0, tm);
5190 
5191 			sc->sc_statchg = tlp_2114x_nway_statchg;
5192 			sc->sc_tick = tlp_2114x_nway_tick;
5193 			break;
5194 
5195 		default:
5196 			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
5197 			break;
5198 		}
5199 	}
5200 
5201 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
5202 
5203 	/*
5204 	 * Display any non-MII media we've located.
5205 	 */
5206 	if (sc->sc_media_seen &
5207 	    ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII)))
5208 		tlp_print_media(sc);
5209 
5210 	tlp_sia_fixup(sc);
5211 }
5212 
5213 static void
5214 tlp_2114x_nway_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
5215 {
5216 
5217 	(void) tlp_2114x_nway_service(sc, MII_POLLSTAT);
5218 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
5219 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
5220 }
5221 
5222 static int
5223 tlp_2114x_nway_set(struct tulip_softc *sc)
5224 {
5225 
5226 	return (tlp_2114x_nway_service(sc, MII_MEDIACHG));
5227 }
5228 
5229 static void
5230 tlp_2114x_nway_statchg(struct device *self)
5231 {
5232 	struct tulip_softc *sc = (struct tulip_softc *)self;
5233 	struct mii_data *mii = &sc->sc_mii;
5234 	struct ifmedia_entry *ife;
5235 
5236 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)
5237 		return;
5238 
5239 	if ((ife = ifmedia_match(&mii->mii_media, mii->mii_media_active,
5240 	    mii->mii_media.ifm_mask)) == NULL) {
5241 		printf("tlp_2114x_nway_statchg: no match for media 0x%x/0x%x\n",
5242 		    mii->mii_media_active, ~mii->mii_media.ifm_mask);
5243 		panic("tlp_2114x_nway_statchg");
5244 	}
5245 
5246 	tlp_sia_media(sc, ife);
5247 }
5248 
5249 static void
5250 tlp_2114x_nway_tick(void *arg)
5251 {
5252 	struct tulip_softc *sc = arg;
5253 	struct mii_data *mii = &sc->sc_mii;
5254 	int s, ticks;
5255 
5256 	if (!device_is_active(&sc->sc_dev))
5257 		return;
5258 
5259 	s = splnet();
5260 	tlp_2114x_nway_service(sc, MII_TICK);
5261 	if ((sc->sc_flags & TULIPF_LINK_UP) == 0 &&
5262 	    (mii->mii_media_status & IFM_ACTIVE) != 0 &&
5263 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
5264 		sc->sc_flags |= TULIPF_LINK_UP;
5265 		tlp_start(&sc->sc_ethercom.ec_if);
5266 	} else if ((sc->sc_flags & TULIPF_LINK_UP) != 0 &&
5267 	    (mii->mii_media_status & IFM_ACTIVE) == 0) {
5268 		sc->sc_flags &= ~TULIPF_LINK_UP;
5269 	}
5270 	splx(s);
5271 
5272 	if ((sc->sc_flags & TULIPF_LINK_UP) == 0)
5273 		ticks = hz >> 3;
5274 	else
5275 		ticks = hz;
5276 	callout_reset(&sc->sc_tick_callout, ticks, tlp_2114x_nway_tick, sc);
5277 }
5278 
5279 /*
5280  * Support for the 2114X internal NWay block.  This is constructed
5281  * somewhat like a PHY driver for simplicity.
5282  */
5283 
5284 static int
5285 tlp_2114x_nway_service(struct tulip_softc *sc, int cmd)
5286 {
5287 	struct mii_data *mii = &sc->sc_mii;
5288 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
5289 
5290 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
5291 		return (0);
5292 
5293 	switch (cmd) {
5294 	case MII_POLLSTAT:
5295 		/* Nothing special to do here. */
5296 		break;
5297 
5298 	case MII_MEDIACHG:
5299 		switch (IFM_SUBTYPE(ife->ifm_media)) {
5300 		case IFM_AUTO:
5301 			goto restart;
5302 		default:
5303 			/* Manual setting doesn't go through here. */
5304 			printf("tlp_2114x_nway_service: oops!\n");
5305 			return (EINVAL);
5306 		}
5307 		break;
5308 
5309 	case MII_TICK:
5310 		/*
5311 		 * Only used for autonegotiation.
5312 		 */
5313 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
5314 			break;
5315 
5316 		/*
5317 		 * Check to see if we have link.  If we do, we don't
5318 		 * need to restart the autonegotiation process.
5319 		 */
5320 #if 0
5321 		if (mii->mii_media_status & IFM_ACTIVE)
5322 #else
5323 		if (sc->sc_flags & TULIPF_LINK_UP)
5324 #endif
5325 			break;
5326 
5327 		/*
5328 		 * Only retry autonegotiation every 5 seconds.
5329 		 */
5330 		if (++sc->sc_nway_ticks != (5 << 3))
5331 			break;
5332 
5333 	restart:
5334 		sc->sc_nway_ticks = 0;
5335 		ife->ifm_data = IFM_NONE;
5336 		tlp_2114x_nway_auto(sc);
5337 		break;
5338 	}
5339 
5340 	/* Update the media status. */
5341 	tlp_2114x_nway_status(sc);
5342 
5343 	/*
5344 	 * Callback if something changed.  Manually configuration goes through
5345 	 * tlp_sia_set() anyway, so ignore that here.
5346 	 */
5347 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO &&
5348 	    ife->ifm_data != mii->mii_media_active) {
5349 		(*sc->sc_statchg)(&sc->sc_dev);
5350 		ife->ifm_data = mii->mii_media_active;
5351 	}
5352 	return (0);
5353 }
5354 
5355 static void
5356 tlp_2114x_nway_auto(struct tulip_softc *sc)
5357 {
5358 	uint32_t siastat, siatxrx;
5359 
5360 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
5361 
5362 	sc->sc_opmode &= ~(OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD);
5363 	sc->sc_opmode |= OPMODE_TTM|OPMODE_HBD;
5364 	siatxrx = 0xffbf;		/* XXX magic number */
5365 
5366 	/* Compute the link code word to advertise. */
5367 	if (sc->sc_sia_cap & BMSR_100T4)
5368 		siatxrx |= SIATXRX_T4;
5369 	if (sc->sc_sia_cap & BMSR_100TXFDX)
5370 		siatxrx |= SIATXRX_TXF;
5371 	if (sc->sc_sia_cap & BMSR_100TXHDX)
5372 		siatxrx |= SIATXRX_THX;
5373 	if (sc->sc_sia_cap & BMSR_10TFDX)
5374 		sc->sc_opmode |= OPMODE_FD;
5375 	if (sc->sc_sia_cap & BMSR_10THDX)
5376 		siatxrx |= SIATXRX_TH;
5377 
5378 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
5379 
5380 	TULIP_WRITE(sc, CSR_SIACONN, 0);
5381 	delay(1000);
5382 	TULIP_WRITE(sc, CSR_SIATXRX, siatxrx);
5383 	TULIP_WRITE(sc, CSR_SIACONN, SIACONN_SRL);
5384 
5385 	siastat = TULIP_READ(sc, CSR_SIASTAT);
5386 	siastat &= ~(SIASTAT_ANS|SIASTAT_LPC|SIASTAT_TRA|SIASTAT_ARA|
5387 		     SIASTAT_LS100|SIASTAT_LS10|SIASTAT_MRA);
5388 	siastat |= SIASTAT_ANS_TXDIS;
5389 	TULIP_WRITE(sc, CSR_SIASTAT, siastat);
5390 }
5391 
5392 static void
5393 tlp_2114x_nway_status(struct tulip_softc *sc)
5394 {
5395 	struct mii_data *mii = &sc->sc_mii;
5396 	uint32_t siatxrx, siastat, anlpar;
5397 
5398 	mii->mii_media_status = IFM_AVALID;
5399 	mii->mii_media_active = IFM_ETHER;
5400 
5401 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
5402 		return;
5403 
5404 	siastat = TULIP_READ(sc, CSR_SIASTAT);
5405 	siatxrx = TULIP_READ(sc, CSR_SIATXRX);
5406 
5407 	if (siatxrx & SIATXRX_ANE) {
5408 		if ((siastat & SIASTAT_ANS) != SIASTAT_ANS_FLPGOOD) {
5409 			/* Erg, still trying, I guess... */
5410 			mii->mii_media_active |= IFM_NONE;
5411 			return;
5412 		}
5413 
5414 		if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100))
5415 			mii->mii_media_status |= IFM_ACTIVE;
5416 
5417 		if (siastat & SIASTAT_LPN) {
5418 			anlpar = SIASTAT_GETLPC(siastat);
5419 			if (anlpar & ANLPAR_T4 &&
5420 			    sc->sc_sia_cap & BMSR_100T4)
5421 				mii->mii_media_active |= IFM_100_T4;
5422 			else if (anlpar & ANLPAR_TX_FD &&
5423 				 sc->sc_sia_cap & BMSR_100TXFDX)
5424 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
5425 			else if (anlpar & ANLPAR_TX &&
5426 				 sc->sc_sia_cap & BMSR_100TXHDX)
5427 				mii->mii_media_active |= IFM_100_TX;
5428 			else if (anlpar & ANLPAR_10_FD &&
5429 				 sc->sc_sia_cap & BMSR_10TFDX)
5430 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
5431 			else if (anlpar & ANLPAR_10 &&
5432 				 sc->sc_sia_cap & BMSR_10THDX)
5433 				mii->mii_media_active |= IFM_10_T;
5434 			else
5435 				mii->mii_media_active |= IFM_NONE;
5436 		} else {
5437 			/*
5438 			 * If the other side doesn't support NWAY, then the
5439 			 * best we can do is determine if we have a 10Mbps or
5440 			 * 100Mbps link. There's no way to know if the link
5441 			 * is full or half duplex, so we default to half duplex
5442 			 * and hope that the user is clever enough to manually
5443 			 * change the media settings if we're wrong.
5444 			 */
5445 			if ((siastat & SIASTAT_LS100) == 0)
5446 				mii->mii_media_active |= IFM_100_TX;
5447 			else if ((siastat & SIASTAT_LS10) == 0)
5448 				mii->mii_media_active |= IFM_10_T;
5449 			else
5450 				mii->mii_media_active |= IFM_NONE;
5451 		}
5452 	} else {
5453 		if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100))
5454 			mii->mii_media_status |= IFM_ACTIVE;
5455 
5456 		if (sc->sc_opmode & OPMODE_TTM)
5457 			mii->mii_media_active |= IFM_10_T;
5458 		else
5459 			mii->mii_media_active |= IFM_100_TX;
5460 		if (sc->sc_opmode & OPMODE_FD)
5461 			mii->mii_media_active |= IFM_FDX;
5462 	}
5463 }
5464 
5465 static void
5466 tlp_2114x_isv_tmsw_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
5467 {
5468 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
5469 	struct tulip_21x4x_media *tm = ife->ifm_aux;
5470 
5471 	(*tm->tm_get)(sc, ifmr);
5472 }
5473 
5474 static int
5475 tlp_2114x_isv_tmsw_set(struct tulip_softc *sc)
5476 {
5477 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
5478 	struct tulip_21x4x_media *tm = ife->ifm_aux;
5479 
5480 	/*
5481 	 * Check to see if we need to reset the chip, and do it.  The
5482 	 * reset path will get the OPMODE register right the next
5483 	 * time through.
5484 	 */
5485 	if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode))
5486 		return (tlp_init(&sc->sc_ethercom.ec_if));
5487 
5488 	return ((*tm->tm_set)(sc));
5489 }
5490 
5491 /*
5492  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
5493  */
5494 static void	tlp_sio_mii_tmsw_init(struct tulip_softc *);
5495 
5496 const struct tulip_mediasw tlp_sio_mii_mediasw = {
5497 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
5498 };
5499 
5500 static void
5501 tlp_sio_mii_tmsw_init(struct tulip_softc *sc)
5502 {
5503 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5504 
5505 	/*
5506 	 * We don't attach any media info structures to the ifmedia
5507 	 * entries, so if we're using a pre-init function that needs
5508 	 * that info, override it to one that doesn't.
5509 	 */
5510 	if (sc->sc_preinit == tlp_2114x_preinit)
5511 		sc->sc_preinit = tlp_2114x_mii_preinit;
5512 
5513 	sc->sc_mii.mii_ifp = ifp;
5514 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
5515 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
5516 	sc->sc_mii.mii_statchg = sc->sc_statchg;
5517 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
5518 	    tlp_mediastatus);
5519 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
5520 	    MII_OFFSET_ANY, 0);
5521 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
5522 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
5523 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
5524 	} else {
5525 		sc->sc_flags |= TULIPF_HAS_MII;
5526 		sc->sc_tick = tlp_mii_tick;
5527 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
5528 	}
5529 }
5530 
5531 /*
5532  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
5533  */
5534 static void	tlp_pnic_tmsw_init(struct tulip_softc *);
5535 static void	tlp_pnic_tmsw_get(struct tulip_softc *, struct ifmediareq *);
5536 static int	tlp_pnic_tmsw_set(struct tulip_softc *);
5537 
5538 const struct tulip_mediasw tlp_pnic_mediasw = {
5539 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
5540 };
5541 
5542 static void	tlp_pnic_nway_statchg(struct device *);
5543 static void	tlp_pnic_nway_tick(void *);
5544 static int	tlp_pnic_nway_service(struct tulip_softc *, int);
5545 static void	tlp_pnic_nway_reset(struct tulip_softc *);
5546 static int	tlp_pnic_nway_auto(struct tulip_softc *, int);
5547 static void	tlp_pnic_nway_auto_timeout(void *);
5548 static void	tlp_pnic_nway_status(struct tulip_softc *);
5549 static void	tlp_pnic_nway_acomp(struct tulip_softc *);
5550 
5551 static void
5552 tlp_pnic_tmsw_init(struct tulip_softc *sc)
5553 {
5554 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5555 	const char *sep = "";
5556 
5557 #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
5558 #define	PRINT(str)	printf("%s%s", sep, str); sep = ", "
5559 
5560 	sc->sc_mii.mii_ifp = ifp;
5561 	sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
5562 	sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
5563 	sc->sc_mii.mii_statchg = sc->sc_statchg;
5564 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
5565 	    tlp_mediastatus);
5566 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
5567 	    MII_OFFSET_ANY, 0);
5568 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
5569 		/* XXX What about AUI/BNC support? */
5570 		printf("%s: ", sc->sc_dev.dv_xname);
5571 
5572 		tlp_pnic_nway_reset(sc);
5573 
5574 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
5575 		    PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
5576 		PRINT("10baseT");
5577 
5578 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
5579 		    PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
5580 		PRINT("10baseT-FDX");
5581 
5582 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
5583 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
5584 		PRINT("100baseTX");
5585 
5586 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
5587 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
5588 		    PNIC_NWAY_CAP100TXFDX);
5589 		PRINT("100baseTX-FDX");
5590 
5591 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
5592 		    PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
5593 		    PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
5594 		    PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
5595 		PRINT("auto");
5596 
5597 		printf("\n");
5598 
5599 		sc->sc_statchg = tlp_pnic_nway_statchg;
5600 		sc->sc_tick = tlp_pnic_nway_tick;
5601 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
5602 	} else {
5603 		sc->sc_flags |= TULIPF_HAS_MII;
5604 		sc->sc_tick = tlp_mii_tick;
5605 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
5606 	}
5607 
5608 #undef ADD
5609 #undef PRINT
5610 }
5611 
5612 static void
5613 tlp_pnic_tmsw_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
5614 {
5615 	struct mii_data *mii = &sc->sc_mii;
5616 
5617 	if (sc->sc_flags & TULIPF_HAS_MII)
5618 		tlp_mii_getmedia(sc, ifmr);
5619 	else {
5620 		mii->mii_media_status = 0;
5621 		mii->mii_media_active = IFM_NONE;
5622 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
5623 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
5624 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
5625 	}
5626 }
5627 
5628 static int
5629 tlp_pnic_tmsw_set(struct tulip_softc *sc)
5630 {
5631 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5632 	struct mii_data *mii = &sc->sc_mii;
5633 
5634 	if (sc->sc_flags & TULIPF_HAS_MII) {
5635 		/*
5636 		 * Make sure the built-in Tx jabber timer is disabled.
5637 		 */
5638 		TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
5639 
5640 		return (tlp_mii_setmedia(sc));
5641 	}
5642 
5643 	if (ifp->if_flags & IFF_UP) {
5644 		mii->mii_media_status = 0;
5645 		mii->mii_media_active = IFM_NONE;
5646 		return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
5647 	}
5648 
5649 	return (0);
5650 }
5651 
5652 static void
5653 tlp_pnic_nway_statchg(struct device *self)
5654 {
5655 	struct tulip_softc *sc = (struct tulip_softc *)self;
5656 
5657 	/* Idle the transmit and receive processes. */
5658 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
5659 
5660 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
5661 	    OPMODE_SCR|OPMODE_HBD);
5662 
5663 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
5664 		sc->sc_opmode |= OPMODE_TTM;
5665 		TULIP_WRITE(sc, CSR_GPP,
5666 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
5667 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
5668 	} else {
5669 		sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD;
5670 		TULIP_WRITE(sc, CSR_GPP,
5671 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
5672 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
5673 	}
5674 
5675 	if (sc->sc_mii.mii_media_active & IFM_FDX)
5676 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
5677 
5678 	/*
5679 	 * Write new OPMODE bits.  This also restarts the transmit
5680 	 * and receive processes.
5681 	 */
5682 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
5683 }
5684 
5685 static void
5686 tlp_pnic_nway_tick(void *arg)
5687 {
5688 	struct tulip_softc *sc = arg;
5689 	int s;
5690 
5691 	if (!device_is_active(&sc->sc_dev))
5692 		return;
5693 
5694 	s = splnet();
5695 	tlp_pnic_nway_service(sc, MII_TICK);
5696 	splx(s);
5697 
5698 	callout_reset(&sc->sc_tick_callout, hz, tlp_pnic_nway_tick, sc);
5699 }
5700 
5701 /*
5702  * Support for the Lite-On PNIC internal NWay block.  This is constructed
5703  * somewhat like a PHY driver for simplicity.
5704  */
5705 
5706 static int
5707 tlp_pnic_nway_service(struct tulip_softc *sc, int cmd)
5708 {
5709 	struct mii_data *mii = &sc->sc_mii;
5710 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
5711 
5712 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
5713 		return (0);
5714 
5715 	switch (cmd) {
5716 	case MII_POLLSTAT:
5717 		/* Nothing special to do here. */
5718 		break;
5719 
5720 	case MII_MEDIACHG:
5721 		switch (IFM_SUBTYPE(ife->ifm_media)) {
5722 		case IFM_AUTO:
5723 			(void) tlp_pnic_nway_auto(sc, 1);
5724 			break;
5725 		case IFM_100_T4:
5726 			/*
5727 			 * XXX Not supported as a manual setting right now.
5728 			 */
5729 			return (EINVAL);
5730 		default:
5731 			/*
5732 			 * NWAY register data is stored in the ifmedia entry.
5733 			 */
5734 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
5735 		}
5736 		break;
5737 
5738 	case MII_TICK:
5739 		/*
5740 		 * Only used for autonegotiation.
5741 		 */
5742 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
5743 			return (0);
5744 
5745 		/*
5746 		 * Check to see if we have link.  If we do, we don't
5747 		 * need to restart the autonegotiation process.
5748 		 */
5749 		if (sc->sc_flags & TULIPF_LINK_UP)
5750 			return (0);
5751 
5752 		/*
5753 		 * Only retry autonegotiation every 5 seconds.
5754 		 */
5755 		if (++sc->sc_nway_ticks != 5)
5756 			return (0);
5757 
5758 		sc->sc_nway_ticks = 0;
5759 		tlp_pnic_nway_reset(sc);
5760 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
5761 			return (0);
5762 		break;
5763 	}
5764 
5765 	/* Update the media status. */
5766 	tlp_pnic_nway_status(sc);
5767 
5768 	/* Callback if something changed. */
5769 	if ((sc->sc_nway_active == NULL ||
5770 	     sc->sc_nway_active->ifm_media != mii->mii_media_active) ||
5771 	    cmd == MII_MEDIACHG) {
5772 		(*sc->sc_statchg)(&sc->sc_dev);
5773 		tlp_nway_activate(sc, mii->mii_media_active);
5774 	}
5775 	return (0);
5776 }
5777 
5778 static void
5779 tlp_pnic_nway_reset(struct tulip_softc *sc)
5780 {
5781 
5782 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
5783 	delay(100);
5784 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
5785 }
5786 
5787 static int
5788 tlp_pnic_nway_auto(struct tulip_softc *sc, int waitfor)
5789 {
5790 	struct mii_data *mii = &sc->sc_mii;
5791 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
5792 	u_int32_t reg;
5793 	int i;
5794 
5795 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
5796 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
5797 
5798 	if (waitfor) {
5799 		/* Wait 500ms for it to complete. */
5800 		for (i = 0; i < 500; i++) {
5801 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5802 			if (reg & PNIC_NWAY_LPAR_MASK) {
5803 				tlp_pnic_nway_acomp(sc);
5804 				return (0);
5805 			}
5806 			delay(1000);
5807 		}
5808 #if 0
5809 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
5810 			printf("%s: autonegotiation failed to complete\n",
5811 			    sc->sc_dev.dv_xname);
5812 #endif
5813 
5814 		/*
5815 		 * Don't need to worry about clearing DOINGAUTO.
5816 		 * If that's set, a timeout is pending, and it will
5817 		 * clear the flag.
5818 		 */
5819 		return (EIO);
5820 	}
5821 
5822 	/*
5823 	 * Just let it finish asynchronously.  This is for the benefit of
5824 	 * the tick handler driving autonegotiation.  Don't want 500ms
5825 	 * delays all the time while the system is running!
5826 	 */
5827 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
5828 		sc->sc_flags |= TULIPF_DOINGAUTO;
5829 		callout_reset(&sc->sc_nway_callout, hz >> 1,
5830 		    tlp_pnic_nway_auto_timeout, sc);
5831 	}
5832 	return (EJUSTRETURN);
5833 }
5834 
5835 static void
5836 tlp_pnic_nway_auto_timeout(void *arg)
5837 {
5838 	struct tulip_softc *sc = arg;
5839 	u_int32_t reg;
5840 	int s;
5841 
5842 	s = splnet();
5843 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
5844 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5845 #if 0
5846 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
5847 		printf("%s: autonegotiation failed to complete\n",
5848 		    sc->sc_dev.dv_xname);
5849 #endif
5850 
5851 	tlp_pnic_nway_acomp(sc);
5852 
5853 	/* Update the media status. */
5854 	(void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
5855 	splx(s);
5856 }
5857 
5858 static void
5859 tlp_pnic_nway_status(struct tulip_softc *sc)
5860 {
5861 	struct mii_data *mii = &sc->sc_mii;
5862 	u_int32_t reg;
5863 
5864 	mii->mii_media_status = IFM_AVALID;
5865 	mii->mii_media_active = IFM_ETHER;
5866 
5867 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5868 
5869 	if (sc->sc_flags & TULIPF_LINK_UP)
5870 		mii->mii_media_status |= IFM_ACTIVE;
5871 
5872 	if (reg & PNIC_NWAY_NW) {
5873 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
5874 			/* Erg, still trying, I guess... */
5875 			mii->mii_media_active |= IFM_NONE;
5876 			return;
5877 		}
5878 
5879 #if 0
5880 		if (reg & PNIC_NWAY_LPAR100T4)
5881 			mii->mii_media_active |= IFM_100_T4;
5882 		else
5883 #endif
5884 		if (reg & PNIC_NWAY_LPAR100TXFDX)
5885 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
5886 		else if (reg & PNIC_NWAY_LPAR100TX)
5887 			mii->mii_media_active |= IFM_100_TX;
5888 		else if (reg & PNIC_NWAY_LPAR10TFDX)
5889 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
5890 		else if (reg & PNIC_NWAY_LPAR10T)
5891 			mii->mii_media_active |= IFM_10_T;
5892 		else
5893 			mii->mii_media_active |= IFM_NONE;
5894 	} else {
5895 		if (reg & PNIC_NWAY_100)
5896 			mii->mii_media_active |= IFM_100_TX;
5897 		else
5898 			mii->mii_media_active |= IFM_10_T;
5899 		if (reg & PNIC_NWAY_FD)
5900 			mii->mii_media_active |= IFM_FDX;
5901 	}
5902 }
5903 
5904 static void
5905 tlp_pnic_nway_acomp(struct tulip_softc *sc)
5906 {
5907 	u_int32_t reg;
5908 
5909 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5910 	reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
5911 
5912 	if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
5913 		reg |= PNIC_NWAY_100;
5914 	if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
5915 		reg |= PNIC_NWAY_FD;
5916 
5917 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
5918 }
5919 
5920 /*
5921  * Macronix PMAC and Lite-On PNIC-II media switch:
5922  *
5923  *	MX98713 and MX98713A		21140-like MII or GPIO media.
5924  *
5925  *	MX98713A			21143-like MII or SIA/SYM media.
5926  *
5927  *	MX98715, MX98715A, MX98725,	21143-like SIA/SYM media.
5928  *	82C115, MX98715AEC-C, -E
5929  *
5930  * So, what we do here is fake MII-on-SIO or ISV media info, and
5931  * use the ISV media switch get/set functions to handle the rest.
5932  */
5933 
5934 static void	tlp_pmac_tmsw_init(struct tulip_softc *);
5935 
5936 const struct tulip_mediasw tlp_pmac_mediasw = {
5937 	tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
5938 };
5939 
5940 const struct tulip_mediasw tlp_pmac_mii_mediasw = {
5941 	tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
5942 };
5943 
5944 static void
5945 tlp_pmac_tmsw_init(struct tulip_softc *sc)
5946 {
5947 	static const u_int8_t media[] = {
5948 		TULIP_ROM_MB_MEDIA_TP,
5949 		TULIP_ROM_MB_MEDIA_TP_FDX,
5950 		TULIP_ROM_MB_MEDIA_100TX,
5951 		TULIP_ROM_MB_MEDIA_100TX_FDX,
5952 	};
5953 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5954 	struct tulip_21x4x_media *tm;
5955 
5956 	sc->sc_mii.mii_ifp = ifp;
5957 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
5958 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
5959 	sc->sc_mii.mii_statchg = sc->sc_statchg;
5960 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
5961 	    tlp_mediastatus);
5962 	if (sc->sc_chip == TULIP_CHIP_MX98713 ||
5963 	    sc->sc_chip == TULIP_CHIP_MX98713A) {
5964 		mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
5965 		    MII_PHY_ANY, MII_OFFSET_ANY, 0);
5966 		if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) {
5967 			sc->sc_flags |= TULIPF_HAS_MII;
5968 			sc->sc_tick = tlp_mii_tick;
5969 			sc->sc_preinit = tlp_2114x_mii_preinit;
5970 			sc->sc_mediasw = &tlp_pmac_mii_mediasw;
5971 			ifmedia_set(&sc->sc_mii.mii_media,
5972 			    IFM_ETHER|IFM_AUTO);
5973 			return;
5974 		}
5975 	}
5976 
5977 	switch (sc->sc_chip) {
5978 	case TULIP_CHIP_MX98713:
5979 		tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR,
5980 		    tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4);
5981 
5982 		/*
5983 		 * XXX Should implement auto-sense for this someday,
5984 		 * XXX when we do the same for the 21140.
5985 		 */
5986 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
5987 		break;
5988 
5989 	default:
5990 		tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA,
5991 		    tlp_sia_get, tlp_sia_set, media, 2);
5992 		tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM,
5993 		    tlp_sia_get, tlp_sia_set, media + 2, 2);
5994 
5995 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
5996 		tm->tm_name = "auto";
5997 		tm->tm_get = tlp_2114x_nway_get;
5998 		tm->tm_set = tlp_2114x_nway_set;
5999 		ifmedia_add(&sc->sc_mii.mii_media,
6000 		    IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 0, tm);
6001 
6002 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6003 		sc->sc_statchg = tlp_2114x_nway_statchg;
6004 		sc->sc_tick = tlp_2114x_nway_tick;
6005 		break;
6006 	}
6007 
6008 	tlp_print_media(sc);
6009 	tlp_sia_fixup(sc);
6010 
6011 	/* Set the LED modes. */
6012 	tlp_pmac_reset(sc);
6013 
6014 	sc->sc_reset = tlp_pmac_reset;
6015 }
6016 
6017 /*
6018  * ADMtek AL981 media switch.  Only has internal PHY.
6019  */
6020 static void	tlp_al981_tmsw_init(struct tulip_softc *);
6021 
6022 const struct tulip_mediasw tlp_al981_mediasw = {
6023 	tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
6024 };
6025 
6026 static void
6027 tlp_al981_tmsw_init(struct tulip_softc *sc)
6028 {
6029 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
6030 
6031 	sc->sc_mii.mii_ifp = ifp;
6032 	sc->sc_mii.mii_readreg = tlp_al981_mii_readreg;
6033 	sc->sc_mii.mii_writereg = tlp_al981_mii_writereg;
6034 	sc->sc_mii.mii_statchg = sc->sc_statchg;
6035 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
6036 	    tlp_mediastatus);
6037 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
6038 	    MII_OFFSET_ANY, 0);
6039 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
6040 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
6041 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
6042 	} else {
6043 		sc->sc_flags |= TULIPF_HAS_MII;
6044 		sc->sc_tick = tlp_mii_tick;
6045 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6046 	}
6047 }
6048 
6049 /*
6050  * ADMtek AN983/985 media switch.  Only has internal PHY, but
6051  * on an SIO-like interface.  Unfortunately, we can't use the
6052  * standard SIO media switch, because the AN985 "ghosts" the
6053  * singly PHY at every address.
6054  */
6055 static void	tlp_an985_tmsw_init(struct tulip_softc *);
6056 
6057 const struct tulip_mediasw tlp_an985_mediasw = {
6058 	tlp_an985_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
6059 };
6060 
6061 static void
6062 tlp_an985_tmsw_init(struct tulip_softc *sc)
6063 {
6064 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
6065 
6066 	sc->sc_mii.mii_ifp = ifp;
6067 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
6068 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
6069 	sc->sc_mii.mii_statchg = sc->sc_statchg;
6070 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
6071 	    tlp_mediastatus);
6072 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, 1,
6073 	    MII_OFFSET_ANY, 0);
6074 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
6075 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
6076 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
6077 	} else {
6078 		sc->sc_flags |= TULIPF_HAS_MII;
6079 		sc->sc_tick = tlp_mii_tick;
6080 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6081 	}
6082 }
6083 
6084 /*
6085  * Davicom DM9102 media switch.  Internal PHY and possibly HomePNA.
6086  */
6087 static void	tlp_dm9102_tmsw_init(struct tulip_softc *);
6088 static void	tlp_dm9102_tmsw_getmedia(struct tulip_softc *,
6089 		    struct ifmediareq *);
6090 static int	tlp_dm9102_tmsw_setmedia(struct tulip_softc *);
6091 
6092 const struct tulip_mediasw tlp_dm9102_mediasw = {
6093 	tlp_dm9102_tmsw_init, tlp_dm9102_tmsw_getmedia,
6094 	    tlp_dm9102_tmsw_setmedia
6095 };
6096 
6097 static void
6098 tlp_dm9102_tmsw_init(struct tulip_softc *sc)
6099 {
6100 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
6101 	u_int32_t opmode;
6102 
6103 	sc->sc_mii.mii_ifp = ifp;
6104 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
6105 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
6106 	sc->sc_mii.mii_statchg = sc->sc_statchg;
6107 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
6108 	    tlp_mediastatus);
6109 
6110 	/* PHY block already reset via tlp_reset(). */
6111 
6112 	/*
6113 	 * Configure OPMODE properly for the internal MII interface.
6114 	 */
6115 	switch (sc->sc_chip) {
6116 	case TULIP_CHIP_DM9102:
6117 		opmode = OPMODE_MBO|OPMODE_HBD|OPMODE_PS;
6118 		break;
6119 
6120 	case TULIP_CHIP_DM9102A:
6121 		opmode = OPMODE_MBO|OPMODE_HBD;
6122 		break;
6123 
6124 	default:
6125 		opmode = 0;
6126 		break;
6127 	}
6128 
6129 	TULIP_WRITE(sc, CSR_OPMODE, opmode);
6130 
6131 	/* Now, probe the internal MII for the internal PHY. */
6132 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
6133 	    MII_OFFSET_ANY, 0);
6134 
6135 	/*
6136 	 * XXX Figure out what to do about the HomePNA portion
6137 	 * XXX of the DM9102A.
6138 	 */
6139 
6140 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
6141 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
6142 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
6143 	} else {
6144 		sc->sc_flags |= TULIPF_HAS_MII;
6145 		sc->sc_tick = tlp_mii_tick;
6146 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6147 	}
6148 }
6149 
6150 static void
6151 tlp_dm9102_tmsw_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
6152 {
6153 
6154 	/* XXX HomePNA on DM9102A. */
6155 	tlp_mii_getmedia(sc, ifmr);
6156 }
6157 
6158 static int
6159 tlp_dm9102_tmsw_setmedia(struct tulip_softc *sc)
6160 {
6161 
6162 	/* XXX HomePNA on DM9102A. */
6163 	return (tlp_mii_setmedia(sc));
6164 }
6165 
6166 /*
6167  * ASIX AX88140A/AX88141 media switch. Internal PHY or MII.
6168  */
6169 
6170 static void	tlp_asix_tmsw_init(struct tulip_softc *);
6171 static void	tlp_asix_tmsw_getmedia(struct tulip_softc *,
6172 		    struct ifmediareq *);
6173 static int	tlp_asix_tmsw_setmedia(struct tulip_softc *);
6174 
6175 const struct tulip_mediasw tlp_asix_mediasw = {
6176 	tlp_asix_tmsw_init, tlp_asix_tmsw_getmedia,
6177 	tlp_asix_tmsw_setmedia
6178 };
6179 
6180 static void
6181 tlp_asix_tmsw_init(struct tulip_softc *sc)
6182 {
6183 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
6184 	u_int32_t opmode;
6185 
6186 	sc->sc_mii.mii_ifp = ifp;
6187         sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
6188         sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
6189 	sc->sc_mii.mii_statchg = sc->sc_statchg;
6190 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
6191             tlp_mediastatus);
6192 
6193 	/*
6194 	 * Configure OPMODE properly for the internal MII interface.
6195 	 */
6196 	switch (sc->sc_chip) {
6197 	case TULIP_CHIP_AX88140:
6198 	case TULIP_CHIP_AX88141:
6199 		opmode = OPMODE_HBD|OPMODE_PS;
6200 		break;
6201         default:
6202                 opmode = 0;
6203                 break;
6204         }
6205 
6206 	TULIP_WRITE(sc, CSR_OPMODE, opmode);
6207 
6208 	/* Now, probe the internal MII for the internal PHY. */
6209 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
6210 	    MII_OFFSET_ANY, 0);
6211 
6212 	/* XXX Figure how to handle the PHY. */
6213 
6214 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
6215 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
6216 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
6217 	} else {
6218 		sc->sc_flags |= TULIPF_HAS_MII;
6219 		sc->sc_tick = tlp_mii_tick;
6220 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6221 	}
6222 
6223 
6224 }
6225 
6226 static void
6227 tlp_asix_tmsw_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
6228 {
6229 
6230 	/* XXX PHY handling. */
6231 	tlp_mii_getmedia(sc, ifmr);
6232 }
6233 
6234 static int
6235 tlp_asix_tmsw_setmedia(struct tulip_softc *sc)
6236 {
6237 
6238 	/* XXX PHY handling. */
6239 	return (tlp_mii_setmedia(sc));
6240 }
6241 
6242 /*
6243  * RS7112 media switch.  Handles only MII attached to the SIO.
6244  * We only have a PHY at 1.
6245  */
6246 void   tlp_rs7112_tmsw_init(struct tulip_softc *);
6247 
6248 const struct tulip_mediasw tlp_rs7112_mediasw = {
6249 	tlp_rs7112_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
6250 };
6251 
6252 void
6253 tlp_rs7112_tmsw_init(struct tulip_softc *sc)
6254 {
6255 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
6256 
6257 	/*
6258 	 * We don't attach any media info structures to the ifmedia
6259 	 * entries, so if we're using a pre-init function that needs
6260 	 * that info, override it to one that doesn't.
6261 	 */
6262 	if (sc->sc_preinit == tlp_2114x_preinit)
6263 		sc->sc_preinit = tlp_2114x_mii_preinit;
6264 
6265 	sc->sc_mii.mii_ifp = ifp;
6266 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
6267 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
6268 	sc->sc_mii.mii_statchg = sc->sc_statchg;
6269 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
6270 	    tlp_mediastatus);
6271 
6272 	/*
6273 	 * The RS7112 reports a PHY at 0 (possibly HomePNA?)
6274 	 * and 1 (ethernet). We attach ethernet only.
6275 	 */
6276 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, 1,
6277 	    MII_OFFSET_ANY, 0);
6278 
6279 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
6280 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
6281 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
6282 	} else {
6283 		sc->sc_flags |= TULIPF_HAS_MII;
6284 		sc->sc_tick = tlp_mii_tick;
6285 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6286 	}
6287 }
6288