xref: /netbsd-src/sys/dev/ic/tulip.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: tulip.c,v 1.154 2007/10/19 12:00:03 ad 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.154 2007/10/19 12:00:03 ad 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 its 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 		printf("%s: broken MicroWire interface detected; "
2251 		    "setting SROM size to 1Kb\n", sc->sc_dev.dv_xname);
2252 		return (6);
2253 	} else {
2254 		if (tlp_srom_debug)
2255 			printf("%s: SROM size is 2^%d*16 bits (%d bytes)\n",
2256 			    sc->sc_dev.dv_xname, x, (1 << (x + 4)) >> 3);
2257 		return (x);
2258 	}
2259 }
2260 
2261 /*
2262  * tlp_read_srom:
2263  *
2264  *	Read the Tulip SROM.
2265  */
2266 int
2267 tlp_read_srom(struct tulip_softc *sc)
2268 {
2269 	int size;
2270 	u_int32_t miirom;
2271 	u_int16_t datain;
2272 	int i, x;
2273 
2274 	tlp_srom_idle(sc);
2275 
2276 	sc->sc_srom_addrbits = tlp_srom_size(sc);
2277 	if (sc->sc_srom_addrbits == 0)
2278 		return (0);
2279 	size = TULIP_ROM_SIZE(sc->sc_srom_addrbits);
2280 	sc->sc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
2281 
2282 	/* Select the SROM. */
2283 	miirom = MIIROM_SR;
2284 	SROM_EMIT(sc, miirom);
2285 
2286 	miirom |= MIIROM_RD;
2287 	SROM_EMIT(sc, miirom);
2288 
2289 	for (i = 0; i < size; i += 2) {
2290 		/* Send CHIP SELECT for one clock tick. */
2291 		miirom |= MIIROM_SROMCS;
2292 		SROM_EMIT(sc, miirom);
2293 
2294 		/* Shift in the READ opcode. */
2295 		for (x = 3; x > 0; x--) {
2296 			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
2297 				miirom |= MIIROM_SROMDI;
2298 			else
2299 				miirom &= ~MIIROM_SROMDI;
2300 			SROM_EMIT(sc, miirom);
2301 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
2302 			SROM_EMIT(sc, miirom);
2303 		}
2304 
2305 		/* Shift in address. */
2306 		for (x = sc->sc_srom_addrbits; x > 0; x--) {
2307 			if (i & (1 << x))
2308 				miirom |= MIIROM_SROMDI;
2309 			else
2310 				miirom &= ~MIIROM_SROMDI;
2311 			SROM_EMIT(sc, miirom);
2312 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
2313 			SROM_EMIT(sc, miirom);
2314 		}
2315 
2316 		/* Shift out data. */
2317 		miirom &= ~MIIROM_SROMDI;
2318 		datain = 0;
2319 		for (x = 16; x > 0; x--) {
2320 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
2321 			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
2322 				datain |= (1 << (x - 1));
2323 			SROM_EMIT(sc, miirom);
2324 		}
2325 		sc->sc_srom[i] = datain & 0xff;
2326 		sc->sc_srom[i + 1] = datain >> 8;
2327 
2328 		/* Clear CHIP SELECT. */
2329 		miirom &= ~MIIROM_SROMCS;
2330 		SROM_EMIT(sc, miirom);
2331 	}
2332 
2333 	/* Deselect the SROM. */
2334 	SROM_EMIT(sc, 0);
2335 
2336 	/* ...and idle it. */
2337 	tlp_srom_idle(sc);
2338 
2339 	if (tlp_srom_debug) {
2340 		printf("SROM CONTENTS:");
2341 		for (i = 0; i < size; i++) {
2342 			if ((i % 8) == 0)
2343 				printf("\n\t");
2344 			printf("0x%02x ", sc->sc_srom[i]);
2345 		}
2346 		printf("\n");
2347 	}
2348 
2349 	return (1);
2350 }
2351 
2352 #undef SROM_EMIT
2353 
2354 /*
2355  * tlp_add_rxbuf:
2356  *
2357  *	Add a receive buffer to the indicated descriptor.
2358  */
2359 static int
2360 tlp_add_rxbuf(struct tulip_softc *sc, int idx)
2361 {
2362 	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
2363 	struct mbuf *m;
2364 	int error;
2365 
2366 	MGETHDR(m, M_DONTWAIT, MT_DATA);
2367 	if (m == NULL)
2368 		return (ENOBUFS);
2369 
2370 	MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
2371 	MCLGET(m, M_DONTWAIT);
2372 	if ((m->m_flags & M_EXT) == 0) {
2373 		m_freem(m);
2374 		return (ENOBUFS);
2375 	}
2376 
2377 	if (rxs->rxs_mbuf != NULL)
2378 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2379 
2380 	rxs->rxs_mbuf = m;
2381 
2382 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
2383 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
2384 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
2385 	if (error) {
2386 		printf("%s: can't load rx DMA map %d, error = %d\n",
2387 		    sc->sc_dev.dv_xname, idx, error);
2388 		panic("tlp_add_rxbuf");	/* XXX */
2389 	}
2390 
2391 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
2392 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
2393 
2394 	TULIP_INIT_RXDESC(sc, idx);
2395 
2396 	return (0);
2397 }
2398 
2399 /*
2400  * tlp_srom_crcok:
2401  *
2402  *	Check the CRC of the Tulip SROM.
2403  */
2404 int
2405 tlp_srom_crcok(const u_int8_t *romdata)
2406 {
2407 	u_int32_t crc;
2408 
2409 	crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM);
2410 	crc = (crc & 0xffff) ^ 0xffff;
2411 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
2412 		return (1);
2413 
2414 	/*
2415 	 * Try an alternate checksum.
2416 	 */
2417 	crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM1);
2418 	crc = (crc & 0xffff) ^ 0xffff;
2419 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM1))
2420 		return (1);
2421 
2422 	return (0);
2423 }
2424 
2425 /*
2426  * tlp_isv_srom:
2427  *
2428  *	Check to see if the SROM is in the new standardized format.
2429  */
2430 int
2431 tlp_isv_srom(const u_int8_t *romdata)
2432 {
2433 	int i;
2434 	u_int16_t cksum;
2435 
2436 	if (tlp_srom_crcok(romdata)) {
2437 		/*
2438 		 * SROM CRC checks out; must be in the new format.
2439 		 */
2440 		return (1);
2441 	}
2442 
2443 	cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM);
2444 	if (cksum == 0xffff || cksum == 0) {
2445 		/*
2446 		 * No checksum present.  Check the SROM ID; 18 bytes of 0
2447 		 * followed by 1 (version) followed by the number of
2448 		 * adapters which use this SROM (should be non-zero).
2449 		 */
2450 		for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) {
2451 			if (romdata[i] != 0)
2452 				return (0);
2453 		}
2454 		if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1)
2455 			return (0);
2456 		if (romdata[TULIP_ROM_CHIP_COUNT] == 0)
2457 			return (0);
2458 		return (1);
2459 	}
2460 
2461 	return (0);
2462 }
2463 
2464 /*
2465  * tlp_isv_srom_enaddr:
2466  *
2467  *	Get the Ethernet address from an ISV SROM.
2468  */
2469 int
2470 tlp_isv_srom_enaddr(struct tulip_softc *sc, u_int8_t *enaddr)
2471 {
2472 	int i, devcnt;
2473 
2474 	if (tlp_isv_srom(sc->sc_srom) == 0)
2475 		return (0);
2476 
2477 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
2478 	for (i = 0; i < devcnt; i++) {
2479 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
2480 			break;
2481 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
2482 		    sc->sc_devno)
2483 			break;
2484 	}
2485 
2486 	if (i == devcnt)
2487 		return (0);
2488 
2489 	memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS],
2490 	    ETHER_ADDR_LEN);
2491 	enaddr[5] += i;
2492 
2493 	return (1);
2494 }
2495 
2496 /*
2497  * tlp_parse_old_srom:
2498  *
2499  *	Parse old-format SROMs.
2500  *
2501  *	This routine is largely lifted from Matt Thomas's `de' driver.
2502  */
2503 int
2504 tlp_parse_old_srom(struct tulip_softc *sc, u_int8_t *enaddr)
2505 {
2506 	static const u_int8_t testpat[] =
2507 	    { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
2508 	int i;
2509 	u_int32_t cksum;
2510 
2511 	if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
2512 		/*
2513 		 * Phobos 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 
3398 	if (ifp->if_flags & IFF_UP) {
3399 		switch (sc->sc_chip) {
3400 		case TULIP_CHIP_21142:
3401 		case TULIP_CHIP_21143:
3402 			/* Disable the internal Nway engine. */
3403 			TULIP_WRITE(sc, CSR_SIATXRX, 0);
3404 			break;
3405 
3406 		default:
3407 			/* Nothing. */
3408 			break;
3409 		}
3410 		mii_mediachg(&sc->sc_mii);
3411 	}
3412 	return (0);
3413 }
3414 
3415 /*
3416  * tlp_bitbang_mii_readreg:
3417  *
3418  *	Read a PHY register via bit-bang'ing the MII.
3419  */
3420 static int
3421 tlp_bitbang_mii_readreg(struct device *self, int phy, int reg)
3422 {
3423 	struct tulip_softc *sc = (void *) self;
3424 
3425 	return (mii_bitbang_readreg(self, sc->sc_bitbang_ops, phy, reg));
3426 }
3427 
3428 /*
3429  * tlp_bitbang_mii_writereg:
3430  *
3431  *	Write a PHY register via bit-bang'ing the MII.
3432  */
3433 static void
3434 tlp_bitbang_mii_writereg(struct device *self, int phy, int reg, int val)
3435 {
3436 	struct tulip_softc *sc = (void *) self;
3437 
3438 	mii_bitbang_writereg(self, sc->sc_bitbang_ops, phy, reg, val);
3439 }
3440 
3441 /*
3442  * tlp_sio_mii_bitbang_read:
3443  *
3444  *	Read the MII serial port for the MII bit-bang module.
3445  */
3446 static u_int32_t
3447 tlp_sio_mii_bitbang_read(struct device *self)
3448 {
3449 	struct tulip_softc *sc = (void *) self;
3450 
3451 	return (TULIP_READ(sc, CSR_MIIROM));
3452 }
3453 
3454 /*
3455  * tlp_sio_mii_bitbang_write:
3456  *
3457  *	Write the MII serial port for the MII bit-bang module.
3458  */
3459 static void
3460 tlp_sio_mii_bitbang_write(struct device *self, u_int32_t val)
3461 {
3462 	struct tulip_softc *sc = (void *) self;
3463 
3464 	TULIP_WRITE(sc, CSR_MIIROM, val);
3465 }
3466 
3467 /*
3468  * tlp_pnic_mii_readreg:
3469  *
3470  *	Read a PHY register on the Lite-On PNIC.
3471  */
3472 static int
3473 tlp_pnic_mii_readreg(struct device *self, int phy, int reg)
3474 {
3475 	struct tulip_softc *sc = (void *) self;
3476 	u_int32_t val;
3477 	int i;
3478 
3479 	TULIP_WRITE(sc, CSR_PNIC_MII,
3480 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
3481 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
3482 	    (reg << PNIC_MII_REGSHIFT));
3483 
3484 	for (i = 0; i < 1000; i++) {
3485 		delay(10);
3486 		val = TULIP_READ(sc, CSR_PNIC_MII);
3487 		if ((val & PNIC_MII_BUSY) == 0) {
3488 			if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
3489 				return (0);
3490 			else
3491 				return (val & PNIC_MII_DATA);
3492 		}
3493 	}
3494 	printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
3495 	return (0);
3496 }
3497 
3498 /*
3499  * tlp_pnic_mii_writereg:
3500  *
3501  *	Write a PHY register on the Lite-On PNIC.
3502  */
3503 static void
3504 tlp_pnic_mii_writereg(struct device *self, int phy, int reg, int val)
3505 {
3506 	struct tulip_softc *sc = (void *) self;
3507 	int i;
3508 
3509 	TULIP_WRITE(sc, CSR_PNIC_MII,
3510 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
3511 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
3512 	    (reg << PNIC_MII_REGSHIFT) | val);
3513 
3514 	for (i = 0; i < 1000; i++) {
3515 		delay(10);
3516 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
3517 			return;
3518 	}
3519 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
3520 }
3521 
3522 static const bus_addr_t tlp_al981_phy_regmap[] = {
3523 	CSR_ADM_BMCR,
3524 	CSR_ADM_BMSR,
3525 	CSR_ADM_PHYIDR1,
3526 	CSR_ADM_PHYIDR2,
3527 	CSR_ADM_ANAR,
3528 	CSR_ADM_ANLPAR,
3529 	CSR_ADM_ANER,
3530 
3531 	CSR_ADM_XMC,
3532 	CSR_ADM_XCIIS,
3533 	CSR_ADM_XIE,
3534 	CSR_ADM_100CTR,
3535 };
3536 static const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
3537     sizeof(tlp_al981_phy_regmap[0]);
3538 
3539 /*
3540  * tlp_al981_mii_readreg:
3541  *
3542  *	Read a PHY register on the ADMtek AL981.
3543  */
3544 static int
3545 tlp_al981_mii_readreg(struct device *self, int phy, int reg)
3546 {
3547 	struct tulip_softc *sc = (struct tulip_softc *)self;
3548 
3549 	/* AL981 only has an internal PHY. */
3550 	if (phy != 0)
3551 		return (0);
3552 
3553 	if (reg >= tlp_al981_phy_regmap_size)
3554 		return (0);
3555 
3556 	return (bus_space_read_4(sc->sc_st, sc->sc_sh,
3557 	    tlp_al981_phy_regmap[reg]) & 0xffff);
3558 }
3559 
3560 /*
3561  * tlp_al981_mii_writereg:
3562  *
3563  *	Write a PHY register on the ADMtek AL981.
3564  */
3565 static void
3566 tlp_al981_mii_writereg(struct device *self, int phy, int reg, int val)
3567 {
3568 	struct tulip_softc *sc = (struct tulip_softc *)self;
3569 
3570 	/* AL981 only has an internal PHY. */
3571 	if (phy != 0)
3572 		return;
3573 
3574 	if (reg >= tlp_al981_phy_regmap_size)
3575 		return;
3576 
3577 	bus_space_write_4(sc->sc_st, sc->sc_sh,
3578 	    tlp_al981_phy_regmap[reg], val);
3579 }
3580 
3581 /*****************************************************************************
3582  * Chip-specific pre-init and reset functions.
3583  *****************************************************************************/
3584 
3585 /*
3586  * tlp_2114x_preinit:
3587  *
3588  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
3589  */
3590 static void
3591 tlp_2114x_preinit(struct tulip_softc *sc)
3592 {
3593 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3594 	struct tulip_21x4x_media *tm = ife->ifm_aux;
3595 
3596 	/*
3597 	 * Whether or not we're in MII or SIA/SYM mode, the media info
3598 	 * contains the appropriate OPMODE bits.
3599 	 *
3600 	 * Also, we always set the Must-Be-One bit.
3601 	 */
3602 	sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode;
3603 
3604 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3605 }
3606 
3607 /*
3608  * tlp_2114x_mii_preinit:
3609  *
3610  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
3611  *	This version is used by boards which only have MII and don't have
3612  *	an ISV SROM.
3613  */
3614 static void
3615 tlp_2114x_mii_preinit(struct tulip_softc *sc)
3616 {
3617 
3618 	/*
3619 	 * Always set the Must-Be-One bit, and Port Select (to select MII).
3620 	 * We'll never be called during a media change.
3621 	 */
3622 	sc->sc_opmode |= OPMODE_MBO|OPMODE_PS;
3623 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3624 }
3625 
3626 /*
3627  * tlp_pnic_preinit:
3628  *
3629  *	Pre-init function for the Lite-On 82c168 and 82c169.
3630  */
3631 static void
3632 tlp_pnic_preinit(struct tulip_softc *sc)
3633 {
3634 
3635 	if (sc->sc_flags & TULIPF_HAS_MII) {
3636 		/*
3637 		 * MII case: just set the port-select bit; we will never
3638 		 * be called during a media change.
3639 		 */
3640 		sc->sc_opmode |= OPMODE_PS;
3641 	} else {
3642 		/*
3643 		 * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
3644 		 */
3645 		sc->sc_opmode |= OPMODE_PNIC_TBEN;
3646 	}
3647 }
3648 
3649 /*
3650  * tlp_asix_preinit:
3651  *
3652  * 	Pre-init function for the ASIX chipsets.
3653  */
3654 static void
3655 tlp_asix_preinit(struct tulip_softc *sc)
3656 {
3657 
3658 	switch (sc->sc_chip) {
3659 		case TULIP_CHIP_AX88140:
3660 		case TULIP_CHIP_AX88141:
3661 			/* XXX Handle PHY. */
3662 			sc->sc_opmode |= OPMODE_HBD|OPMODE_PS;
3663 			break;
3664 		default:
3665 			/* Nothing */
3666 			break;
3667 	}
3668 
3669 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3670 }
3671 
3672 /*
3673  * tlp_dm9102_preinit:
3674  *
3675  *	Pre-init function for the Davicom DM9102.
3676  */
3677 static void
3678 tlp_dm9102_preinit(struct tulip_softc *sc)
3679 {
3680 
3681 	switch (sc->sc_chip) {
3682 	case TULIP_CHIP_DM9102:
3683 		sc->sc_opmode |= OPMODE_MBO|OPMODE_HBD|OPMODE_PS;
3684 		break;
3685 
3686 	case TULIP_CHIP_DM9102A:
3687 		/*
3688 		 * XXX Figure out how to actually deal with the HomePNA
3689 		 * XXX portion of the DM9102A.
3690 		 */
3691 		sc->sc_opmode |= OPMODE_MBO|OPMODE_HBD;
3692 		break;
3693 
3694 	default:
3695 		/* Nothing. */
3696 		break;
3697 	}
3698 
3699 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3700 }
3701 
3702 /*
3703  * tlp_21140_reset:
3704  *
3705  *	Issue a reset sequence on the 21140 via the GPIO facility.
3706  */
3707 static void
3708 tlp_21140_reset(struct tulip_softc *sc)
3709 {
3710 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3711 	struct tulip_21x4x_media *tm = ife->ifm_aux;
3712 	int i;
3713 
3714 	/* First, set the direction on the GPIO pins. */
3715 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
3716 
3717 	/* Now, issue the reset sequence. */
3718 	for (i = 0; i < tm->tm_reset_length; i++) {
3719 		delay(10);
3720 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
3721 	}
3722 
3723 	/* Now, issue the selection sequence. */
3724 	for (i = 0; i < tm->tm_gp_length; i++) {
3725 		delay(10);
3726 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
3727 	}
3728 
3729 	/* If there were no sequences, just lower the pins. */
3730 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
3731 		delay(10);
3732 		TULIP_WRITE(sc, CSR_GPP, 0);
3733 	}
3734 }
3735 
3736 /*
3737  * tlp_21142_reset:
3738  *
3739  *	Issue a reset sequence on the 21142 via the GPIO facility.
3740  */
3741 static void
3742 tlp_21142_reset(struct tulip_softc *sc)
3743 {
3744 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3745 	struct tulip_21x4x_media *tm = ife->ifm_aux;
3746 	const u_int8_t *cp;
3747 	int i;
3748 
3749 	cp = &sc->sc_srom[tm->tm_reset_offset];
3750 	for (i = 0; i < tm->tm_reset_length; i++, cp += 2) {
3751 		delay(10);
3752 		TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16);
3753 	}
3754 
3755 	cp = &sc->sc_srom[tm->tm_gp_offset];
3756 	for (i = 0; i < tm->tm_gp_length; i++, cp += 2) {
3757 		delay(10);
3758 		TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16);
3759 	}
3760 
3761 	/* If there were no sequences, just lower the pins. */
3762 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
3763 		delay(10);
3764 		TULIP_WRITE(sc, CSR_SIAGEN, 0);
3765 	}
3766 }
3767 
3768 /*
3769  * tlp_pmac_reset:
3770  *
3771  *	Reset routine for Macronix chips.
3772  */
3773 static void
3774 tlp_pmac_reset(struct tulip_softc *sc)
3775 {
3776 
3777 	switch (sc->sc_chip) {
3778 	case TULIP_CHIP_82C115:
3779 	case TULIP_CHIP_MX98715:
3780 	case TULIP_CHIP_MX98715A:
3781 	case TULIP_CHIP_MX98725:
3782 		/*
3783 		 * Set the LED operating mode.  This information is located
3784 		 * in the EEPROM at byte offset 0x77, per the MX98715A and
3785 		 * MX98725 application notes.
3786 		 */
3787 		TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
3788 		break;
3789 	case TULIP_CHIP_MX98715AEC_X:
3790 		/*
3791 		 * Set the LED operating mode.  This information is located
3792 		 * in the EEPROM at byte offset 0x76, per the MX98715AEC
3793 		 * application note.
3794 		 */
3795 		TULIP_WRITE(sc, CSR_MIIROM, ((0xf & sc->sc_srom[0x76]) << 28)
3796 		    | ((0xf0 & sc->sc_srom[0x76]) << 20));
3797 		break;
3798 
3799 	default:
3800 		/* Nothing. */
3801 		break;
3802 	}
3803 }
3804 
3805 #if 0
3806 /*
3807  * tlp_dm9102_reset:
3808  *
3809  *	Reset routine for the Davicom DM9102.
3810  */
3811 static void
3812 tlp_dm9102_reset(struct tulip_softc *sc)
3813 {
3814 
3815 	TULIP_WRITE(sc, CSR_DM_PHYSTAT, DM_PHYSTAT_GEPC|DM_PHYSTAT_GPED);
3816 	delay(100);
3817 	TULIP_WRITE(sc, CSR_DM_PHYSTAT, 0);
3818 }
3819 #endif
3820 
3821 /*****************************************************************************
3822  * Chip/board-specific media switches.  The ones here are ones that
3823  * are potentially common to multiple front-ends.
3824  *****************************************************************************/
3825 
3826 /*
3827  * This table is a common place for all sorts of media information,
3828  * keyed off of the SROM media code for that media.
3829  *
3830  * Note that we explicitly configure the 21142/21143 to always advertise
3831  * NWay capabilities when using the UTP port.
3832  * XXX Actually, we don't yet.
3833  */
3834 static const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
3835 	{ TULIP_ROM_MB_MEDIA_TP,	IFM_10_T,	0,
3836 	  "10baseT",
3837 	  OPMODE_TTM,
3838 	  BMSR_10THDX,
3839 	  { SIACONN_21040_10BASET,
3840 	    SIATXRX_21040_10BASET,
3841 	    SIAGEN_21040_10BASET },
3842 
3843 	  { SIACONN_21041_10BASET,
3844 	    SIATXRX_21041_10BASET,
3845 	    SIAGEN_21041_10BASET },
3846 
3847 	  { SIACONN_21142_10BASET,
3848 	    SIATXRX_21142_10BASET,
3849 	    SIAGEN_21142_10BASET } },
3850 
3851 	{ TULIP_ROM_MB_MEDIA_BNC,	IFM_10_2,	0,
3852 	  "10base2",
3853 	  0,
3854 	  0,
3855 	  { 0,
3856 	    0,
3857 	    0 },
3858 
3859 	  { SIACONN_21041_BNC,
3860 	    SIATXRX_21041_BNC,
3861 	    SIAGEN_21041_BNC },
3862 
3863 	  { SIACONN_21142_BNC,
3864 	    SIATXRX_21142_BNC,
3865 	    SIAGEN_21142_BNC } },
3866 
3867 	{ TULIP_ROM_MB_MEDIA_AUI,	IFM_10_5,	0,
3868 	  "10base5",
3869 	  0,
3870 	  0,
3871 	  { SIACONN_21040_AUI,
3872 	    SIATXRX_21040_AUI,
3873 	    SIAGEN_21040_AUI },
3874 
3875 	  { SIACONN_21041_AUI,
3876 	    SIATXRX_21041_AUI,
3877 	    SIAGEN_21041_AUI },
3878 
3879 	  { SIACONN_21142_AUI,
3880 	    SIATXRX_21142_AUI,
3881 	    SIAGEN_21142_AUI } },
3882 
3883 	{ TULIP_ROM_MB_MEDIA_100TX,	IFM_100_TX,	0,
3884 	  "100baseTX",
3885 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
3886 	  BMSR_100TXHDX,
3887 	  { 0,
3888 	    0,
3889 	    0 },
3890 
3891 	  { 0,
3892 	    0,
3893 	    0 },
3894 
3895 	  { 0,
3896 	    0,
3897 	    SIAGEN_ABM } },
3898 
3899 	{ TULIP_ROM_MB_MEDIA_TP_FDX,	IFM_10_T,	IFM_FDX,
3900 	  "10baseT-FDX",
3901 	  OPMODE_TTM|OPMODE_FD|OPMODE_HBD,
3902 	  BMSR_10TFDX,
3903 	  { SIACONN_21040_10BASET_FDX,
3904 	    SIATXRX_21040_10BASET_FDX,
3905 	    SIAGEN_21040_10BASET_FDX },
3906 
3907 	  { SIACONN_21041_10BASET_FDX,
3908 	    SIATXRX_21041_10BASET_FDX,
3909 	    SIAGEN_21041_10BASET_FDX },
3910 
3911 	  { SIACONN_21142_10BASET_FDX,
3912 	    SIATXRX_21142_10BASET_FDX,
3913 	    SIAGEN_21142_10BASET_FDX } },
3914 
3915 	{ TULIP_ROM_MB_MEDIA_100TX_FDX,	IFM_100_TX,	IFM_FDX,
3916 	  "100baseTX-FDX",
3917 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD|OPMODE_HBD,
3918 	  BMSR_100TXFDX,
3919 	  { 0,
3920 	    0,
3921 	    0 },
3922 
3923 	  { 0,
3924 	    0,
3925 	    0 },
3926 
3927 	  { 0,
3928 	    0,
3929 	    SIAGEN_ABM } },
3930 
3931 	{ TULIP_ROM_MB_MEDIA_100T4,	IFM_100_T4,	0,
3932 	  "100baseT4",
3933 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
3934 	  BMSR_100T4,
3935 	  { 0,
3936 	    0,
3937 	    0 },
3938 
3939 	  { 0,
3940 	    0,
3941 	    0 },
3942 
3943 	  { 0,
3944 	    0,
3945 	    SIAGEN_ABM } },
3946 
3947 	{ TULIP_ROM_MB_MEDIA_100FX,	IFM_100_FX,	0,
3948 	  "100baseFX",
3949 	  OPMODE_PS|OPMODE_PCS|OPMODE_HBD,
3950 	  0,
3951 	  { 0,
3952 	    0,
3953 	    0 },
3954 
3955 	  { 0,
3956 	    0,
3957 	    0 },
3958 
3959 	  { 0,
3960 	    0,
3961 	    SIAGEN_ABM } },
3962 
3963 	{ TULIP_ROM_MB_MEDIA_100FX_FDX,	IFM_100_FX,	IFM_FDX,
3964 	  "100baseFX-FDX",
3965 	  OPMODE_PS|OPMODE_PCS|OPMODE_FD|OPMODE_HBD,
3966 	  0,
3967 	  { 0,
3968 	    0,
3969 	    0 },
3970 
3971 	  { 0,
3972 	    0,
3973 	    0 },
3974 
3975 	  { 0,
3976 	    0,
3977 	    SIAGEN_ABM } },
3978 
3979 	{ 0,				0,		0,
3980 	  NULL,
3981 	  0,
3982 	  0,
3983 	  { 0,
3984 	    0,
3985 	    0 },
3986 
3987 	  { 0,
3988 	    0,
3989 	    0 },
3990 
3991 	  { 0,
3992 	    0,
3993 	    0 } },
3994 };
3995 
3996 static const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia(u_int8_t);
3997 static void	tlp_srom_media_info(struct tulip_softc *,
3998 		    const struct tulip_srom_to_ifmedia *,
3999 		    struct tulip_21x4x_media *);
4000 static void	tlp_add_srom_media(struct tulip_softc *, int,
4001 		    void (*)(struct tulip_softc *, struct ifmediareq *),
4002 		    int (*)(struct tulip_softc *), const u_int8_t *, int);
4003 static void	tlp_print_media(struct tulip_softc *);
4004 static void	tlp_nway_activate(struct tulip_softc *, int);
4005 static void	tlp_get_minst(struct tulip_softc *);
4006 
4007 static const struct tulip_srom_to_ifmedia *
4008 tlp_srom_to_ifmedia(u_int8_t sm)
4009 {
4010 	const struct tulip_srom_to_ifmedia *tsti;
4011 
4012 	for (tsti = tulip_srom_to_ifmedia_table;
4013 	     tsti->tsti_name != NULL; tsti++) {
4014 		if (tsti->tsti_srom == sm)
4015 			return (tsti);
4016 	}
4017 
4018 	return (NULL);
4019 }
4020 
4021 static void
4022 tlp_srom_media_info(struct tulip_softc *sc,
4023     const struct tulip_srom_to_ifmedia *tsti, struct tulip_21x4x_media *tm)
4024 {
4025 
4026 	tm->tm_name = tsti->tsti_name;
4027 	tm->tm_opmode = tsti->tsti_opmode;
4028 
4029 	sc->sc_sia_cap |= tsti->tsti_sia_cap;
4030 
4031 	switch (sc->sc_chip) {
4032 	case TULIP_CHIP_DE425:
4033 	case TULIP_CHIP_21040:
4034 		tm->tm_sia = tsti->tsti_21040;	/* struct assignment */
4035 		break;
4036 
4037 	case TULIP_CHIP_21041:
4038 		tm->tm_sia = tsti->tsti_21041;	/* struct assignment */
4039 		break;
4040 
4041 	case TULIP_CHIP_21142:
4042 	case TULIP_CHIP_21143:
4043 	case TULIP_CHIP_82C115:
4044 	case TULIP_CHIP_MX98715:
4045 	case TULIP_CHIP_MX98715A:
4046 	case TULIP_CHIP_MX98715AEC_X:
4047 	case TULIP_CHIP_MX98725:
4048 		tm->tm_sia = tsti->tsti_21142;	/* struct assignment */
4049 		break;
4050 
4051 	default:
4052 		/* Nothing. */
4053 		break;
4054 	}
4055 }
4056 
4057 static void
4058 tlp_add_srom_media(struct tulip_softc *sc, int type,
4059     void (*get)(struct tulip_softc *, struct ifmediareq *),
4060     int (*set)(struct tulip_softc *), const u_int8_t *list,
4061     int cnt)
4062 {
4063 	struct tulip_21x4x_media *tm;
4064 	const struct tulip_srom_to_ifmedia *tsti;
4065 	int i;
4066 
4067 	for (i = 0; i < cnt; i++) {
4068 		tsti = tlp_srom_to_ifmedia(list[i]);
4069 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4070 		tlp_srom_media_info(sc, tsti, tm);
4071 		tm->tm_type = type;
4072 		tm->tm_get = get;
4073 		tm->tm_set = set;
4074 
4075 		ifmedia_add(&sc->sc_mii.mii_media,
4076 		    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4077 		    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4078 	}
4079 }
4080 
4081 static void
4082 tlp_print_media(struct tulip_softc *sc)
4083 {
4084 	struct ifmedia_entry *ife;
4085 	struct tulip_21x4x_media *tm;
4086 	const char *sep = "";
4087 
4088 #define	PRINT(str)	printf("%s%s", sep, str); sep = ", "
4089 
4090 	printf("%s: ", sc->sc_dev.dv_xname);
4091 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
4092 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
4093 		tm = ife->ifm_aux;
4094 		if (tm == NULL) {
4095 #ifdef DIAGNOSTIC
4096 			if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
4097 				panic("tlp_print_media");
4098 #endif
4099 			PRINT("auto");
4100 		} else if (tm->tm_type != TULIP_ROM_MB_21140_MII &&
4101 			   tm->tm_type != TULIP_ROM_MB_21142_MII) {
4102 			PRINT(tm->tm_name);
4103 		}
4104 	}
4105 	printf("\n");
4106 
4107 #undef PRINT
4108 }
4109 
4110 static void
4111 tlp_nway_activate(struct tulip_softc *sc, int media)
4112 {
4113 	struct ifmedia_entry *ife;
4114 
4115 	ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0);
4116 #ifdef DIAGNOSTIC
4117 	if (ife == NULL)
4118 		panic("tlp_nway_activate");
4119 #endif
4120 	sc->sc_nway_active = ife;
4121 }
4122 
4123 static void
4124 tlp_get_minst(struct tulip_softc *sc)
4125 {
4126 
4127 	if ((sc->sc_media_seen &
4128 	    ~((1 << TULIP_ROM_MB_21140_MII) |
4129 	      (1 << TULIP_ROM_MB_21142_MII))) == 0) {
4130 		/*
4131 		 * We have not yet seen any SIA/SYM media (but are
4132 		 * about to; that's why we're called!), so assign
4133 		 * the current media instance to be the `internal media'
4134 		 * instance, and advance it so any MII media gets a
4135 		 * fresh one (used to selecting/isolating a PHY).
4136 		 */
4137 		sc->sc_tlp_minst = sc->sc_mii.mii_instance++;
4138 	}
4139 }
4140 
4141 /*
4142  * SIA Utility functions.
4143  */
4144 static void	tlp_sia_update_link(struct tulip_softc *);
4145 static void	tlp_sia_get(struct tulip_softc *, struct ifmediareq *);
4146 static int	tlp_sia_set(struct tulip_softc *);
4147 static int	tlp_sia_media(struct tulip_softc *, struct ifmedia_entry *);
4148 static void	tlp_sia_fixup(struct tulip_softc *);
4149 
4150 static void
4151 tlp_sia_update_link(struct tulip_softc *sc)
4152 {
4153 	struct ifmedia_entry *ife;
4154 	struct tulip_21x4x_media *tm;
4155 	u_int32_t siastat;
4156 
4157 	ife = TULIP_CURRENT_MEDIA(sc);
4158 	tm = ife->ifm_aux;
4159 
4160 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
4161 
4162 	siastat = TULIP_READ(sc, CSR_SIASTAT);
4163 
4164 	/*
4165 	 * Note that when we do SIA link tests, we are assuming that
4166 	 * the chip is really in the mode that the current media setting
4167 	 * reflects.  If we're not, then the link tests will not be
4168 	 * accurate!
4169 	 */
4170 	switch (IFM_SUBTYPE(ife->ifm_media)) {
4171 	case IFM_10_T:
4172 		sc->sc_flags |= TULIPF_LINK_VALID;
4173 		if ((siastat & SIASTAT_LS10) == 0)
4174 			sc->sc_flags |= TULIPF_LINK_UP;
4175 		break;
4176 
4177 	case IFM_100_TX:
4178 	case IFM_100_T4:
4179 		sc->sc_flags |= TULIPF_LINK_VALID;
4180 		if ((siastat & SIASTAT_LS100) == 0)
4181 			sc->sc_flags |= TULIPF_LINK_UP;
4182 		break;
4183 	}
4184 
4185 	switch (sc->sc_chip) {
4186 	case TULIP_CHIP_21142:
4187 	case TULIP_CHIP_21143:
4188 		/*
4189 		 * On these chips, we can tell more information about
4190 		 * AUI/BNC.  Note that the AUI/BNC selection is made
4191 		 * in a different register; for our purpose, it's all
4192 		 * AUI.
4193 		 */
4194 		switch (IFM_SUBTYPE(ife->ifm_media)) {
4195 		case IFM_10_2:
4196 		case IFM_10_5:
4197 			sc->sc_flags |= TULIPF_LINK_VALID;
4198 			if (siastat & SIASTAT_ARA) {
4199 				TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA);
4200 				sc->sc_flags |= TULIPF_LINK_UP;
4201 			}
4202 			break;
4203 
4204 		default:
4205 			/*
4206 			 * If we're SYM media and can detect the link
4207 			 * via the GPIO facility, prefer that status
4208 			 * over LS100.
4209 			 */
4210 			if (tm->tm_type == TULIP_ROM_MB_21143_SYM &&
4211 			    tm->tm_actmask != 0) {
4212 				sc->sc_flags = (sc->sc_flags &
4213 				    ~TULIPF_LINK_UP) | TULIPF_LINK_VALID;
4214 				if (TULIP_ISSET(sc, CSR_SIAGEN,
4215 				    tm->tm_actmask) == tm->tm_actdata)
4216 					sc->sc_flags |= TULIPF_LINK_UP;
4217 			}
4218 		}
4219 		break;
4220 
4221 	default:
4222 		/* Nothing. */
4223 		break;
4224 	}
4225 }
4226 
4227 static void
4228 tlp_sia_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
4229 {
4230 	struct ifmedia_entry *ife;
4231 
4232 	ifmr->ifm_status = 0;
4233 
4234 	tlp_sia_update_link(sc);
4235 
4236 	ife = TULIP_CURRENT_MEDIA(sc);
4237 
4238 	if (sc->sc_flags & TULIPF_LINK_VALID)
4239 		ifmr->ifm_status |= IFM_AVALID;
4240 	if (sc->sc_flags & TULIPF_LINK_UP)
4241 		ifmr->ifm_status |= IFM_ACTIVE;
4242 	ifmr->ifm_active = ife->ifm_media;
4243 }
4244 
4245 static void
4246 tlp_sia_fixup(struct tulip_softc *sc)
4247 {
4248 	struct ifmedia_entry *ife;
4249 	struct tulip_21x4x_media *tm;
4250 	u_int32_t siaconn, siatxrx, siagen;
4251 
4252 	switch (sc->sc_chip) {
4253 	case TULIP_CHIP_82C115:
4254 	case TULIP_CHIP_MX98713A:
4255 	case TULIP_CHIP_MX98715:
4256 	case TULIP_CHIP_MX98715A:
4257 	case TULIP_CHIP_MX98715AEC_X:
4258 	case TULIP_CHIP_MX98725:
4259 		siaconn = PMAC_SIACONN_MASK;
4260 		siatxrx = PMAC_SIATXRX_MASK;
4261 		siagen  = PMAC_SIAGEN_MASK;
4262 		break;
4263 
4264 	default:
4265 		/* No fixups required on any other chips. */
4266 		return;
4267 	}
4268 
4269 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
4270 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
4271 		tm = ife->ifm_aux;
4272 		if (tm == NULL)
4273 			continue;
4274 
4275 		tm->tm_siaconn &= siaconn;
4276 		tm->tm_siatxrx &= siatxrx;
4277 		tm->tm_siagen  &= siagen;
4278 	}
4279 }
4280 
4281 static int
4282 tlp_sia_set(struct tulip_softc *sc)
4283 {
4284 
4285 	return (tlp_sia_media(sc, TULIP_CURRENT_MEDIA(sc)));
4286 }
4287 
4288 static int
4289 tlp_sia_media(struct tulip_softc *sc, struct ifmedia_entry *ife)
4290 {
4291 	struct tulip_21x4x_media *tm;
4292 
4293 	tm = ife->ifm_aux;
4294 
4295 	/*
4296 	 * XXX This appears to be necessary on a bunch of the clone chips.
4297 	 */
4298 	delay(20000);
4299 
4300 	/*
4301 	 * Idle the chip.
4302 	 */
4303 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
4304 
4305 	/*
4306 	 * Program the SIA.  It's important to write in this order,
4307 	 * resetting the SIA first.
4308 	 */
4309 	TULIP_WRITE(sc, CSR_SIACONN, 0);		/* SRL bit clear */
4310 	delay(1000);
4311 
4312 	TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx);
4313 
4314 	switch (sc->sc_chip) {
4315 	case TULIP_CHIP_21142:
4316 	case TULIP_CHIP_21143:
4317 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl);
4318 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata);
4319 		break;
4320 	default:
4321 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen);
4322 	}
4323 
4324 	TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn);
4325 
4326 	/*
4327 	 * Set the OPMODE bits for this media and write OPMODE.
4328 	 * This will resume the transmit and receive processes.
4329 	 */
4330 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
4331 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
4332 
4333 	return (0);
4334 }
4335 
4336 /*
4337  * 21140 GPIO utility functions.
4338  */
4339 static void	tlp_21140_gpio_update_link(struct tulip_softc *);
4340 
4341 static void
4342 tlp_21140_gpio_update_link(struct tulip_softc *sc)
4343 {
4344 	struct ifmedia_entry *ife;
4345 	struct tulip_21x4x_media *tm;
4346 
4347 	ife = TULIP_CURRENT_MEDIA(sc);
4348 	tm = ife->ifm_aux;
4349 
4350 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
4351 
4352 	if (tm->tm_actmask != 0) {
4353 		sc->sc_flags |= TULIPF_LINK_VALID;
4354 		if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
4355 		    tm->tm_actdata)
4356 			sc->sc_flags |= TULIPF_LINK_UP;
4357 	}
4358 }
4359 
4360 void
4361 tlp_21140_gpio_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
4362 {
4363 	struct ifmedia_entry *ife;
4364 
4365 	ifmr->ifm_status = 0;
4366 
4367 	tlp_21140_gpio_update_link(sc);
4368 
4369 	ife = TULIP_CURRENT_MEDIA(sc);
4370 
4371 	if (sc->sc_flags & TULIPF_LINK_VALID)
4372 		ifmr->ifm_status |= IFM_AVALID;
4373 	if (sc->sc_flags & TULIPF_LINK_UP)
4374 		ifmr->ifm_status |= IFM_ACTIVE;
4375 	ifmr->ifm_active = ife->ifm_media;
4376 }
4377 
4378 int
4379 tlp_21140_gpio_set(struct tulip_softc *sc)
4380 {
4381 	struct ifmedia_entry *ife;
4382 	struct tulip_21x4x_media *tm;
4383 
4384 	ife = TULIP_CURRENT_MEDIA(sc);
4385 	tm = ife->ifm_aux;
4386 
4387 	/*
4388 	 * Idle the chip.
4389 	 */
4390 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
4391 
4392 	/*
4393 	 * Set the GPIO pins for this media, to flip any
4394 	 * relays, etc.
4395 	 */
4396 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
4397 	delay(10);
4398 	TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);
4399 
4400 	/*
4401 	 * Set the OPMODE bits for this media and write OPMODE.
4402 	 * This will resume the transmit and receive processes.
4403 	 */
4404 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
4405 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
4406 
4407 	return (0);
4408 }
4409 
4410 /*
4411  * 21040 and 21041 media switches.
4412  */
4413 static void	tlp_21040_tmsw_init(struct tulip_softc *);
4414 static void	tlp_21040_tp_tmsw_init(struct tulip_softc *);
4415 static void	tlp_21040_auibnc_tmsw_init(struct tulip_softc *);
4416 static void	tlp_21041_tmsw_init(struct tulip_softc *);
4417 
4418 const struct tulip_mediasw tlp_21040_mediasw = {
4419 	tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set
4420 };
4421 
4422 const struct tulip_mediasw tlp_21040_tp_mediasw = {
4423 	tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set
4424 };
4425 
4426 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
4427 	tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set
4428 };
4429 
4430 const struct tulip_mediasw tlp_21041_mediasw = {
4431 	tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set
4432 };
4433 
4434 static void
4435 tlp_21040_tmsw_init(struct tulip_softc *sc)
4436 {
4437 	static const u_int8_t media[] = {
4438 		TULIP_ROM_MB_MEDIA_TP,
4439 		TULIP_ROM_MB_MEDIA_TP_FDX,
4440 		TULIP_ROM_MB_MEDIA_AUI,
4441 	};
4442 	struct tulip_21x4x_media *tm;
4443 
4444 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4445 	    tlp_mediastatus);
4446 
4447 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 3);
4448 
4449 	/*
4450 	 * No SROM type for External SIA.
4451 	 */
4452 	tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4453 	tm->tm_name = "manual";
4454 	tm->tm_opmode = 0;
4455 	tm->tm_siaconn = SIACONN_21040_EXTSIA;
4456 	tm->tm_siatxrx = SIATXRX_21040_EXTSIA;
4457 	tm->tm_siagen  = SIAGEN_21040_EXTSIA;
4458 	ifmedia_add(&sc->sc_mii.mii_media,
4459 	    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm);
4460 
4461 	/*
4462 	 * XXX Autosense not yet supported.
4463 	 */
4464 
4465 	/* XXX This should be auto-sense. */
4466 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
4467 
4468 	tlp_print_media(sc);
4469 }
4470 
4471 static void
4472 tlp_21040_tp_tmsw_init(struct tulip_softc *sc)
4473 {
4474 	static const u_int8_t media[] = {
4475 		TULIP_ROM_MB_MEDIA_TP,
4476 		TULIP_ROM_MB_MEDIA_TP_FDX,
4477 	};
4478 
4479 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4480 	    tlp_mediastatus);
4481 
4482 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 2);
4483 
4484 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
4485 
4486 	tlp_print_media(sc);
4487 }
4488 
4489 static void
4490 tlp_21040_auibnc_tmsw_init(struct tulip_softc *sc)
4491 {
4492 	static const u_int8_t media[] = {
4493 		TULIP_ROM_MB_MEDIA_AUI,
4494 	};
4495 
4496 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4497 	    tlp_mediastatus);
4498 
4499 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 1);
4500 
4501 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
4502 
4503 	tlp_print_media(sc);
4504 }
4505 
4506 static void
4507 tlp_21041_tmsw_init(struct tulip_softc *sc)
4508 {
4509 	static const u_int8_t media[] = {
4510 		TULIP_ROM_MB_MEDIA_TP,
4511 		TULIP_ROM_MB_MEDIA_TP_FDX,
4512 		TULIP_ROM_MB_MEDIA_BNC,
4513 		TULIP_ROM_MB_MEDIA_AUI,
4514 	};
4515 	int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
4516 	const struct tulip_srom_to_ifmedia *tsti;
4517 	struct tulip_21x4x_media *tm;
4518 	u_int16_t romdef;
4519 	u_int8_t mb;
4520 
4521 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4522 	    tlp_mediastatus);
4523 
4524 	if (tlp_isv_srom(sc->sc_srom) == 0) {
4525  not_isv_srom:
4526 		/*
4527 		 * If we have a board without the standard 21041 SROM format,
4528 		 * we just assume all media are present and try and pick a
4529 		 * reasonable default.
4530 		 */
4531 		tlp_add_srom_media(sc, 0, NULL, NULL, media, 4);
4532 
4533 		/*
4534 		 * XXX Autosense not yet supported.
4535 		 */
4536 
4537 		/* XXX This should be auto-sense. */
4538 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
4539 
4540 		tlp_print_media(sc);
4541 		return;
4542 	}
4543 
4544 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
4545 	for (i = 0; i < devcnt; i++) {
4546 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
4547 			break;
4548 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
4549 		    sc->sc_devno)
4550 			break;
4551 	}
4552 
4553 	if (i == devcnt)
4554 		goto not_isv_srom;
4555 
4556 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
4557 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
4558 	mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
4559 	m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
4560 
4561 	for (; m_cnt != 0;
4562 	     m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
4563 		mb = sc->sc_srom[mb_offset];
4564 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4565 		switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
4566 		case TULIP_ROM_MB_MEDIA_TP_FDX:
4567 		case TULIP_ROM_MB_MEDIA_TP:
4568 		case TULIP_ROM_MB_MEDIA_BNC:
4569 		case TULIP_ROM_MB_MEDIA_AUI:
4570 			tsti = tlp_srom_to_ifmedia(mb &
4571 			    TULIP_ROM_MB_MEDIA_CODE);
4572 
4573 			tlp_srom_media_info(sc, tsti, tm);
4574 
4575 			/*
4576 			 * Override our default SIA settings if the
4577 			 * SROM contains its own.
4578 			 */
4579 			if (mb & TULIP_ROM_MB_EXT) {
4580 				tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom,
4581 				    mb_offset + TULIP_ROM_MB_CSR13);
4582 				tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom,
4583 				    mb_offset + TULIP_ROM_MB_CSR14);
4584 				tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom,
4585 				    mb_offset + TULIP_ROM_MB_CSR15);
4586 			}
4587 
4588 			ifmedia_add(&sc->sc_mii.mii_media,
4589 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4590 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4591 			break;
4592 
4593 		default:
4594 			printf("%s: unknown media code 0x%02x\n",
4595 			    sc->sc_dev.dv_xname,
4596 			    mb & TULIP_ROM_MB_MEDIA_CODE);
4597 			free(tm, M_DEVBUF);
4598 		}
4599 	}
4600 
4601 	/*
4602 	 * XXX Autosense not yet supported.
4603 	 */
4604 
4605 	romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
4606 	    TULIP_ROM_IL_SELECT_CONN_TYPE);
4607 	switch (romdef) {
4608 	case SELECT_CONN_TYPE_TP:
4609 	case SELECT_CONN_TYPE_TP_AUTONEG:
4610 	case SELECT_CONN_TYPE_TP_NOLINKPASS:
4611 		defmedia = IFM_ETHER|IFM_10_T;
4612 		break;
4613 
4614 	case SELECT_CONN_TYPE_TP_FDX:
4615 		defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
4616 		break;
4617 
4618 	case SELECT_CONN_TYPE_BNC:
4619 		defmedia = IFM_ETHER|IFM_10_2;
4620 		break;
4621 
4622 	case SELECT_CONN_TYPE_AUI:
4623 		defmedia = IFM_ETHER|IFM_10_5;
4624 		break;
4625 #if 0 /* XXX */
4626 	case SELECT_CONN_TYPE_ASENSE:
4627 	case SELECT_CONN_TYPE_ASENSE_AUTONEG:
4628 		defmedia = IFM_ETHER|IFM_AUTO;
4629 		break;
4630 #endif
4631 	default:
4632 		defmedia = 0;
4633 	}
4634 
4635 	if (defmedia == 0) {
4636 		/*
4637 		 * XXX We should default to auto-sense.
4638 		 */
4639 		defmedia = IFM_ETHER|IFM_10_T;
4640 	}
4641 
4642 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
4643 
4644 	tlp_print_media(sc);
4645 }
4646 
4647 /*
4648  * DECchip 2114x ISV media switch.
4649  */
4650 static void	tlp_2114x_isv_tmsw_init(struct tulip_softc *);
4651 static void	tlp_2114x_isv_tmsw_get(struct tulip_softc *,
4652 		    struct ifmediareq *);
4653 static int	tlp_2114x_isv_tmsw_set(struct tulip_softc *);
4654 
4655 const struct tulip_mediasw tlp_2114x_isv_mediasw = {
4656 	tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
4657 };
4658 
4659 static void	tlp_2114x_nway_get(struct tulip_softc *, struct ifmediareq *);
4660 static int	tlp_2114x_nway_set(struct tulip_softc *);
4661 
4662 static void	tlp_2114x_nway_statchg(struct device *);
4663 static int	tlp_2114x_nway_service(struct tulip_softc *, int);
4664 static void	tlp_2114x_nway_auto(struct tulip_softc *);
4665 static void	tlp_2114x_nway_status(struct tulip_softc *);
4666 
4667 static void
4668 tlp_2114x_isv_tmsw_init(struct tulip_softc *sc)
4669 {
4670 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
4671 	struct ifmedia_entry *ife;
4672 	struct mii_softc *phy;
4673 	struct tulip_21x4x_media *tm;
4674 	const struct tulip_srom_to_ifmedia *tsti;
4675 	int i, devcnt, leaf_offset, m_cnt, type, length;
4676 	int defmedia, miidef;
4677 	u_int16_t word;
4678 	u_int8_t *cp, *ncp;
4679 
4680 	defmedia = miidef = 0;
4681 
4682 	sc->sc_mii.mii_ifp = ifp;
4683 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
4684 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
4685 	sc->sc_mii.mii_statchg = sc->sc_statchg;
4686 
4687 	/*
4688 	 * Ignore `instance'; we may get a mixture of SIA and MII
4689 	 * media, and `instance' is used to isolate or select the
4690 	 * PHY on the MII as appropriate.  Note that duplicate media
4691 	 * are disallowed, so ignoring `instance' is safe.
4692 	 */
4693 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, tlp_mediachange,
4694 	    tlp_mediastatus);
4695 
4696 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
4697 	for (i = 0; i < devcnt; i++) {
4698 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
4699 			break;
4700 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
4701 		    sc->sc_devno)
4702 			break;
4703 	}
4704 
4705 	if (i == devcnt) {
4706 		printf("%s: unable to locate info leaf in SROM\n",
4707 		    sc->sc_dev.dv_xname);
4708 		return;
4709 	}
4710 
4711 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
4712 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
4713 
4714 	/* XXX SELECT CONN TYPE */
4715 
4716 	cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
4717 
4718 	/*
4719 	 * On some chips, the first thing in the Info Leaf is the
4720 	 * GPIO pin direction data.
4721 	 */
4722 	switch (sc->sc_chip) {
4723 	case TULIP_CHIP_21140:
4724 	case TULIP_CHIP_21140A:
4725 	case TULIP_CHIP_MX98713:
4726 	case TULIP_CHIP_AX88140:
4727 	case TULIP_CHIP_AX88141:
4728 		sc->sc_gp_dir = *cp++;
4729 		break;
4730 
4731 	default:
4732 		/* Nothing. */
4733 		break;
4734 	}
4735 
4736 	/* Get the media count. */
4737 	m_cnt = *cp++;
4738 
4739 	if (m_cnt == 0) {
4740 		sc->sc_mediasw = &tlp_sio_mii_mediasw;
4741 		(*sc->sc_mediasw->tmsw_init)(sc);
4742 		return;
4743 	}
4744 
4745 	for (; m_cnt != 0; cp = ncp, m_cnt--) {
4746 		/*
4747 		 * Determine the type and length of this media block.
4748 		 * The 21143 is spec'd to always use extended format blocks,
4749 		 * but some cards don't set the bit to indicate this.
4750 		 * Hopefully there are no cards which really don't use
4751 		 * extended format blocks.
4752 		 */
4753 		if ((*cp & 0x80) == 0 && sc->sc_chip != TULIP_CHIP_21143) {
4754 			length = 4;
4755 			type = TULIP_ROM_MB_21140_GPR;
4756 		} else {
4757 			length = (*cp++ & 0x7f) - 1;
4758 			type = *cp++ & 0x3f;
4759 		}
4760 
4761 		/* Compute the start of the next block. */
4762 		ncp = cp + length;
4763 
4764 		/* Now, parse the block. */
4765 		switch (type) {
4766 		case TULIP_ROM_MB_21140_GPR:
4767 			tlp_get_minst(sc);
4768 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR;
4769 
4770 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4771 
4772 			tm->tm_type = TULIP_ROM_MB_21140_GPR;
4773 			tm->tm_get = tlp_21140_gpio_get;
4774 			tm->tm_set = tlp_21140_gpio_set;
4775 
4776 			/* First is the media type code. */
4777 			tsti = tlp_srom_to_ifmedia(cp[0] &
4778 			    TULIP_ROM_MB_MEDIA_CODE);
4779 			if (tsti == NULL) {
4780 				/* Invalid media code. */
4781 				free(tm, M_DEVBUF);
4782 				break;
4783 			}
4784 
4785 			/* Get defaults. */
4786 			tlp_srom_media_info(sc, tsti, tm);
4787 
4788 			/* Next is any GPIO info for this media. */
4789 			tm->tm_gpdata = cp[1];
4790 
4791 			/*
4792 			 * Next is a word containing OPMODE information
4793 			 * and info on how to detect if this media is
4794 			 * active.
4795 			 */
4796 			word = TULIP_ROM_GETW(cp, 2);
4797 			tm->tm_opmode &= OPMODE_FD;
4798 			tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word);
4799 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
4800 				tm->tm_actmask =
4801 				    TULIP_ROM_MB_BITPOS(word);
4802 				tm->tm_actdata =
4803 				    (word & TULIP_ROM_MB_POLARITY) ?
4804 				    0 : tm->tm_actmask;
4805 			}
4806 
4807 			ifmedia_add(&sc->sc_mii.mii_media,
4808 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4809 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4810 			break;
4811 
4812 		case TULIP_ROM_MB_21140_MII:
4813 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII;
4814 
4815 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4816 
4817 			tm->tm_type = TULIP_ROM_MB_21140_MII;
4818 			tm->tm_get = tlp_mii_getmedia;
4819 			tm->tm_set = tlp_mii_setmedia;
4820 			tm->tm_opmode = OPMODE_PS;
4821 
4822 			if (sc->sc_reset == NULL)
4823 				sc->sc_reset = tlp_21140_reset;
4824 
4825 			/* First is the PHY number. */
4826 			tm->tm_phyno = *cp++;
4827 
4828 			/* Next is the MII select sequence length and offset. */
4829 			tm->tm_gp_length = *cp++;
4830 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
4831 			cp += tm->tm_gp_length;
4832 
4833 			/* Next is the MII reset sequence length and offset. */
4834 			tm->tm_reset_length = *cp++;
4835 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
4836 			cp += tm->tm_reset_length;
4837 
4838 			/*
4839 			 * The following items are left in the media block
4840 			 * that we don't particularly care about:
4841 			 *
4842 			 *	capabilities		W
4843 			 *	advertisement		W
4844 			 *	full duplex		W
4845 			 *	tx threshold		W
4846 			 *
4847 			 * These appear to be bits in the PHY registers,
4848 			 * which our MII code handles on its own.
4849 			 */
4850 
4851 			/*
4852 			 * Before we probe the MII bus, we need to reset
4853 			 * it and issue the selection sequence.
4854 			 */
4855 
4856 			/* Set the direction of the pins... */
4857 			TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
4858 
4859 			for (i = 0; i < tm->tm_reset_length; i++) {
4860 				delay(10);
4861 				TULIP_WRITE(sc, CSR_GPP,
4862 				    sc->sc_srom[tm->tm_reset_offset + i]);
4863 			}
4864 
4865 			for (i = 0; i < tm->tm_gp_length; i++) {
4866 				delay(10);
4867 				TULIP_WRITE(sc, CSR_GPP,
4868 				    sc->sc_srom[tm->tm_gp_offset + i]);
4869 			}
4870 
4871 			/* If there were no sequences, just lower the pins. */
4872 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
4873 				delay(10);
4874 				TULIP_WRITE(sc, CSR_GPP, 0);
4875 			}
4876 
4877 			/*
4878 			 * Now, probe the MII for the PHY.  Note, we know
4879 			 * the location of the PHY on the bus, but we don't
4880 			 * particularly care; the MII code just likes to
4881 			 * search the whole thing anyhow.
4882 			 */
4883 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
4884 			    MII_PHY_ANY, tm->tm_phyno, 0);
4885 
4886 			/*
4887 			 * Now, search for the PHY we hopefully just
4888 			 * configured.  If it's not configured into the
4889 			 * kernel, we lose.  The PHY's default media always
4890 			 * takes priority.
4891 			 */
4892 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
4893 			     phy != NULL;
4894 			     phy = LIST_NEXT(phy, mii_list))
4895 				if (phy->mii_offset == tm->tm_phyno)
4896 					break;
4897 			if (phy == NULL) {
4898 				printf("%s: unable to configure MII\n",
4899 				    sc->sc_dev.dv_xname);
4900 				break;
4901 			}
4902 
4903 			sc->sc_flags |= TULIPF_HAS_MII;
4904 			sc->sc_tick = tlp_mii_tick;
4905 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
4906 			    phy->mii_inst);
4907 
4908 			/*
4909 			 * Okay, now that we've found the PHY and the MII
4910 			 * layer has added all of the media associated
4911 			 * with that PHY, we need to traverse the media
4912 			 * list, and add our `tm' to each entry's `aux'
4913 			 * pointer.
4914 			 *
4915 			 * We do this by looking for media with our
4916 			 * PHY's `instance'.
4917 			 */
4918 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
4919 			     ife != NULL;
4920 			     ife = TAILQ_NEXT(ife, ifm_list)) {
4921 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
4922 					continue;
4923 				ife->ifm_aux = tm;
4924 			}
4925 			break;
4926 
4927 		case TULIP_ROM_MB_21142_SIA:
4928 			tlp_get_minst(sc);
4929 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA;
4930 
4931 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4932 
4933 			tm->tm_type = TULIP_ROM_MB_21142_SIA;
4934 			tm->tm_get = tlp_sia_get;
4935 			tm->tm_set = tlp_sia_set;
4936 
4937 			/* First is the media type code. */
4938 			tsti = tlp_srom_to_ifmedia(cp[0] &
4939 			    TULIP_ROM_MB_MEDIA_CODE);
4940 			if (tsti == NULL) {
4941 				/* Invalid media code. */
4942 				free(tm, M_DEVBUF);
4943 				break;
4944 			}
4945 
4946 			/* Get defaults. */
4947 			tlp_srom_media_info(sc, tsti, tm);
4948 
4949 			/*
4950 			 * Override our default SIA settings if the
4951 			 * SROM contains its own.
4952 			 */
4953 			if (cp[0] & 0x40) {
4954 				tm->tm_siaconn = TULIP_ROM_GETW(cp, 1);
4955 				tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3);
4956 				tm->tm_siagen  = TULIP_ROM_GETW(cp, 5);
4957 				cp += 7;
4958 			} else
4959 				cp++;
4960 
4961 			/* Next is GPIO control/data. */
4962 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 0) << 16;
4963 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 2) << 16;
4964 
4965 			ifmedia_add(&sc->sc_mii.mii_media,
4966 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
4967 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
4968 			break;
4969 
4970 		case TULIP_ROM_MB_21142_MII:
4971 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_MII;
4972 
4973 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
4974 
4975 			tm->tm_type = TULIP_ROM_MB_21142_MII;
4976 			tm->tm_get = tlp_mii_getmedia;
4977 			tm->tm_set = tlp_mii_setmedia;
4978 			tm->tm_opmode = OPMODE_PS;
4979 
4980 			if (sc->sc_reset == NULL)
4981 				sc->sc_reset = tlp_21142_reset;
4982 
4983 			/* First is the PHY number. */
4984 			tm->tm_phyno = *cp++;
4985 
4986 			/* Next is the MII select sequence length and offset. */
4987 			tm->tm_gp_length = *cp++;
4988 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
4989 			cp += tm->tm_gp_length * 2;
4990 
4991 			/* Next is the MII reset sequence length and offset. */
4992 			tm->tm_reset_length = *cp++;
4993 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
4994 			cp += tm->tm_reset_length * 2;
4995 
4996 			/*
4997 			 * The following items are left in the media block
4998 			 * that we don't particularly care about:
4999 			 *
5000 			 *	capabilities		W
5001 			 *	advertisement		W
5002 			 *	full duplex		W
5003 			 *	tx threshold		W
5004 			 *	MII interrupt		W
5005 			 *
5006 			 * These appear to be bits in the PHY registers,
5007 			 * which our MII code handles on its own.
5008 			 */
5009 
5010 			/*
5011 			 * Before we probe the MII bus, we need to reset
5012 			 * it and issue the selection sequence.
5013 			 */
5014 
5015 			cp = &sc->sc_srom[tm->tm_reset_offset];
5016 			for (i = 0; i < tm->tm_reset_length; i++, cp += 2) {
5017 				delay(10);
5018 				TULIP_WRITE(sc, CSR_SIAGEN,
5019 				    TULIP_ROM_GETW(cp, 0) << 16);
5020 			}
5021 
5022 			cp = &sc->sc_srom[tm->tm_gp_offset];
5023 			for (i = 0; i < tm->tm_gp_length; i++, cp += 2) {
5024 				delay(10);
5025 				TULIP_WRITE(sc, CSR_SIAGEN,
5026 				    TULIP_ROM_GETW(cp, 0) << 16);
5027 			}
5028 
5029 			/* If there were no sequences, just lower the pins. */
5030 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
5031 				delay(10);
5032 				TULIP_WRITE(sc, CSR_SIAGEN, 0);
5033 			}
5034 
5035 			/*
5036 			 * Now, probe the MII for the PHY.  Note, we know
5037 			 * the location of the PHY on the bus, but we don't
5038 			 * particularly care; the MII code just likes to
5039 			 * search the whole thing anyhow.
5040 			 */
5041 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
5042 			    MII_PHY_ANY, tm->tm_phyno, 0);
5043 
5044 			/*
5045 			 * Now, search for the PHY we hopefully just
5046 			 * configured.  If it's not configured into the
5047 			 * kernel, we lose.  The PHY's default media always
5048 			 * takes priority.
5049 			 */
5050 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
5051 			     phy != NULL;
5052 			     phy = LIST_NEXT(phy, mii_list))
5053 				if (phy->mii_offset == tm->tm_phyno)
5054 					break;
5055 			if (phy == NULL) {
5056 				printf("%s: unable to configure MII\n",
5057 				    sc->sc_dev.dv_xname);
5058 				break;
5059 			}
5060 
5061 			sc->sc_flags |= TULIPF_HAS_MII;
5062 			sc->sc_tick = tlp_mii_tick;
5063 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
5064 			    phy->mii_inst);
5065 
5066 			/*
5067 			 * Okay, now that we've found the PHY and the MII
5068 			 * layer has added all of the media associated
5069 			 * with that PHY, we need to traverse the media
5070 			 * list, and add our `tm' to each entry's `aux'
5071 			 * pointer.
5072 			 *
5073 			 * We do this by looking for media with our
5074 			 * PHY's `instance'.
5075 			 */
5076 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
5077 			     ife != NULL;
5078 			     ife = TAILQ_NEXT(ife, ifm_list)) {
5079 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
5080 					continue;
5081 				ife->ifm_aux = tm;
5082 			}
5083 			break;
5084 
5085 		case TULIP_ROM_MB_21143_SYM:
5086 			tlp_get_minst(sc);
5087 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM;
5088 
5089 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
5090 
5091 			tm->tm_type = TULIP_ROM_MB_21143_SYM;
5092 			tm->tm_get = tlp_sia_get;
5093 			tm->tm_set = tlp_sia_set;
5094 
5095 			/* First is the media type code. */
5096 			tsti = tlp_srom_to_ifmedia(cp[0] &
5097 			    TULIP_ROM_MB_MEDIA_CODE);
5098 			if (tsti == NULL) {
5099 				/* Invalid media code. */
5100 				free(tm, M_DEVBUF);
5101 				break;
5102 			}
5103 
5104 			/* Get defaults. */
5105 			tlp_srom_media_info(sc, tsti, tm);
5106 
5107 			/* Next is GPIO control/data. */
5108 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 1) << 16;
5109 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 3) << 16;
5110 
5111 			/*
5112 			 * Next is a word containing OPMODE information
5113 			 * and info on how to detect if this media is
5114 			 * active.
5115 			 */
5116 			word = TULIP_ROM_GETW(cp, 5);
5117 			tm->tm_opmode &= OPMODE_FD;
5118 			tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word);
5119 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
5120 				tm->tm_actmask =
5121 				    TULIP_ROM_MB_BITPOS(word);
5122 				tm->tm_actdata =
5123 				    (word & TULIP_ROM_MB_POLARITY) ?
5124 				    0 : tm->tm_actmask;
5125 			}
5126 
5127 			ifmedia_add(&sc->sc_mii.mii_media,
5128 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
5129 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
5130 			break;
5131 
5132 		case TULIP_ROM_MB_21143_RESET:
5133 			printf("%s: 21143 reset block\n", sc->sc_dev.dv_xname);
5134 			break;
5135 
5136 		default:
5137 			printf("%s: unknown ISV media block type 0x%02x\n",
5138 			    sc->sc_dev.dv_xname, type);
5139 		}
5140 	}
5141 
5142 	/*
5143 	 * Deal with the case where no media is configured.
5144 	 */
5145 	if (TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list) == NULL) {
5146 		printf("%s: no media found!\n", sc->sc_dev.dv_xname);
5147 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
5148 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
5149 		return;
5150 	}
5151 
5152 	/*
5153 	 * Pick the default media.
5154 	 */
5155 	if (miidef != 0)
5156 		defmedia = miidef;
5157 	else {
5158 		switch (sc->sc_chip) {
5159 		case TULIP_CHIP_21140:
5160 		case TULIP_CHIP_21140A:
5161 			/* XXX should come from SROM */
5162 			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
5163 			if (ifmedia_match(&sc->sc_mii.mii_media, defmedia,
5164 				sc->sc_mii.mii_media.ifm_mask) == NULL) {
5165 				/*
5166 				 * There is not a 10baseT media.
5167 				 * Fall back to the first found one.
5168 				 */
5169 				ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
5170 				defmedia = ife->ifm_media;
5171 			}
5172 			break;
5173 
5174 		case TULIP_CHIP_21142:
5175 		case TULIP_CHIP_21143:
5176 		case TULIP_CHIP_MX98713A:
5177 		case TULIP_CHIP_MX98715:
5178 		case TULIP_CHIP_MX98715A:
5179 		case TULIP_CHIP_MX98715AEC_X:
5180 		case TULIP_CHIP_MX98725:
5181 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
5182 			tm->tm_name = "auto";
5183 			tm->tm_get = tlp_2114x_nway_get;
5184 			tm->tm_set = tlp_2114x_nway_set;
5185 
5186 			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0);
5187 			ifmedia_add(&sc->sc_mii.mii_media, defmedia, 0, tm);
5188 
5189 			sc->sc_statchg = tlp_2114x_nway_statchg;
5190 			sc->sc_tick = tlp_2114x_nway_tick;
5191 			break;
5192 
5193 		default:
5194 			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
5195 			break;
5196 		}
5197 	}
5198 
5199 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
5200 
5201 	/*
5202 	 * Display any non-MII media we've located.
5203 	 */
5204 	if (sc->sc_media_seen &
5205 	    ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII)))
5206 		tlp_print_media(sc);
5207 
5208 	tlp_sia_fixup(sc);
5209 }
5210 
5211 static void
5212 tlp_2114x_nway_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
5213 {
5214 
5215 	(void) tlp_2114x_nway_service(sc, MII_POLLSTAT);
5216 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
5217 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
5218 }
5219 
5220 static int
5221 tlp_2114x_nway_set(struct tulip_softc *sc)
5222 {
5223 
5224 	return (tlp_2114x_nway_service(sc, MII_MEDIACHG));
5225 }
5226 
5227 static void
5228 tlp_2114x_nway_statchg(struct device *self)
5229 {
5230 	struct tulip_softc *sc = (struct tulip_softc *)self;
5231 	struct mii_data *mii = &sc->sc_mii;
5232 	struct ifmedia_entry *ife;
5233 
5234 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)
5235 		return;
5236 
5237 	if ((ife = ifmedia_match(&mii->mii_media, mii->mii_media_active,
5238 	    mii->mii_media.ifm_mask)) == NULL) {
5239 		printf("tlp_2114x_nway_statchg: no match for media 0x%x/0x%x\n",
5240 		    mii->mii_media_active, ~mii->mii_media.ifm_mask);
5241 		panic("tlp_2114x_nway_statchg");
5242 	}
5243 
5244 	tlp_sia_media(sc, ife);
5245 }
5246 
5247 static void
5248 tlp_2114x_nway_tick(void *arg)
5249 {
5250 	struct tulip_softc *sc = arg;
5251 	struct mii_data *mii = &sc->sc_mii;
5252 	int s, ticks;
5253 
5254 	if (!device_is_active(&sc->sc_dev))
5255 		return;
5256 
5257 	s = splnet();
5258 	tlp_2114x_nway_service(sc, MII_TICK);
5259 	if ((sc->sc_flags & TULIPF_LINK_UP) == 0 &&
5260 	    (mii->mii_media_status & IFM_ACTIVE) != 0 &&
5261 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
5262 		sc->sc_flags |= TULIPF_LINK_UP;
5263 		tlp_start(&sc->sc_ethercom.ec_if);
5264 	} else if ((sc->sc_flags & TULIPF_LINK_UP) != 0 &&
5265 	    (mii->mii_media_status & IFM_ACTIVE) == 0) {
5266 		sc->sc_flags &= ~TULIPF_LINK_UP;
5267 	}
5268 	splx(s);
5269 
5270 	if ((sc->sc_flags & TULIPF_LINK_UP) == 0)
5271 		ticks = hz >> 3;
5272 	else
5273 		ticks = hz;
5274 	callout_reset(&sc->sc_tick_callout, ticks, tlp_2114x_nway_tick, sc);
5275 }
5276 
5277 /*
5278  * Support for the 2114X internal NWay block.  This is constructed
5279  * somewhat like a PHY driver for simplicity.
5280  */
5281 
5282 static int
5283 tlp_2114x_nway_service(struct tulip_softc *sc, int cmd)
5284 {
5285 	struct mii_data *mii = &sc->sc_mii;
5286 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
5287 
5288 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
5289 		return (0);
5290 
5291 	switch (cmd) {
5292 	case MII_POLLSTAT:
5293 		/* Nothing special to do here. */
5294 		break;
5295 
5296 	case MII_MEDIACHG:
5297 		switch (IFM_SUBTYPE(ife->ifm_media)) {
5298 		case IFM_AUTO:
5299 			goto restart;
5300 		default:
5301 			/* Manual setting doesn't go through here. */
5302 			printf("tlp_2114x_nway_service: oops!\n");
5303 			return (EINVAL);
5304 		}
5305 		break;
5306 
5307 	case MII_TICK:
5308 		/*
5309 		 * Only used for autonegotiation.
5310 		 */
5311 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
5312 			break;
5313 
5314 		/*
5315 		 * Check to see if we have link.  If we do, we don't
5316 		 * need to restart the autonegotiation process.
5317 		 */
5318 #if 0
5319 		if (mii->mii_media_status & IFM_ACTIVE)
5320 #else
5321 		if (sc->sc_flags & TULIPF_LINK_UP)
5322 #endif
5323 			break;
5324 
5325 		/*
5326 		 * Only retry autonegotiation every 5 seconds.
5327 		 */
5328 		if (++sc->sc_nway_ticks != (5 << 3))
5329 			break;
5330 
5331 	restart:
5332 		sc->sc_nway_ticks = 0;
5333 		ife->ifm_data = IFM_NONE;
5334 		tlp_2114x_nway_auto(sc);
5335 		break;
5336 	}
5337 
5338 	/* Update the media status. */
5339 	tlp_2114x_nway_status(sc);
5340 
5341 	/*
5342 	 * Callback if something changed.  Manually configuration goes through
5343 	 * tlp_sia_set() anyway, so ignore that here.
5344 	 */
5345 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO &&
5346 	    ife->ifm_data != mii->mii_media_active) {
5347 		(*sc->sc_statchg)(&sc->sc_dev);
5348 		ife->ifm_data = mii->mii_media_active;
5349 	}
5350 	return (0);
5351 }
5352 
5353 static void
5354 tlp_2114x_nway_auto(struct tulip_softc *sc)
5355 {
5356 	uint32_t siastat, siatxrx;
5357 
5358 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
5359 
5360 	sc->sc_opmode &= ~(OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD);
5361 	sc->sc_opmode |= OPMODE_TTM|OPMODE_HBD;
5362 	siatxrx = 0xffbf;		/* XXX magic number */
5363 
5364 	/* Compute the link code word to advertise. */
5365 	if (sc->sc_sia_cap & BMSR_100T4)
5366 		siatxrx |= SIATXRX_T4;
5367 	if (sc->sc_sia_cap & BMSR_100TXFDX)
5368 		siatxrx |= SIATXRX_TXF;
5369 	if (sc->sc_sia_cap & BMSR_100TXHDX)
5370 		siatxrx |= SIATXRX_THX;
5371 	if (sc->sc_sia_cap & BMSR_10TFDX)
5372 		sc->sc_opmode |= OPMODE_FD;
5373 	if (sc->sc_sia_cap & BMSR_10THDX)
5374 		siatxrx |= SIATXRX_TH;
5375 
5376 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
5377 
5378 	TULIP_WRITE(sc, CSR_SIACONN, 0);
5379 	delay(1000);
5380 	TULIP_WRITE(sc, CSR_SIATXRX, siatxrx);
5381 	TULIP_WRITE(sc, CSR_SIACONN, SIACONN_SRL);
5382 
5383 	siastat = TULIP_READ(sc, CSR_SIASTAT);
5384 	siastat &= ~(SIASTAT_ANS|SIASTAT_LPC|SIASTAT_TRA|SIASTAT_ARA|
5385 		     SIASTAT_LS100|SIASTAT_LS10|SIASTAT_MRA);
5386 	siastat |= SIASTAT_ANS_TXDIS;
5387 	TULIP_WRITE(sc, CSR_SIASTAT, siastat);
5388 }
5389 
5390 static void
5391 tlp_2114x_nway_status(struct tulip_softc *sc)
5392 {
5393 	struct mii_data *mii = &sc->sc_mii;
5394 	uint32_t siatxrx, siastat, anlpar;
5395 
5396 	mii->mii_media_status = IFM_AVALID;
5397 	mii->mii_media_active = IFM_ETHER;
5398 
5399 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
5400 		return;
5401 
5402 	siastat = TULIP_READ(sc, CSR_SIASTAT);
5403 	siatxrx = TULIP_READ(sc, CSR_SIATXRX);
5404 
5405 	if (siatxrx & SIATXRX_ANE) {
5406 		if ((siastat & SIASTAT_ANS) != SIASTAT_ANS_FLPGOOD) {
5407 			/* Erg, still trying, I guess... */
5408 			mii->mii_media_active |= IFM_NONE;
5409 			return;
5410 		}
5411 
5412 		if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100))
5413 			mii->mii_media_status |= IFM_ACTIVE;
5414 
5415 		if (siastat & SIASTAT_LPN) {
5416 			anlpar = SIASTAT_GETLPC(siastat);
5417 			if (anlpar & ANLPAR_T4 &&
5418 			    sc->sc_sia_cap & BMSR_100T4)
5419 				mii->mii_media_active |= IFM_100_T4;
5420 			else if (anlpar & ANLPAR_TX_FD &&
5421 				 sc->sc_sia_cap & BMSR_100TXFDX)
5422 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
5423 			else if (anlpar & ANLPAR_TX &&
5424 				 sc->sc_sia_cap & BMSR_100TXHDX)
5425 				mii->mii_media_active |= IFM_100_TX;
5426 			else if (anlpar & ANLPAR_10_FD &&
5427 				 sc->sc_sia_cap & BMSR_10TFDX)
5428 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
5429 			else if (anlpar & ANLPAR_10 &&
5430 				 sc->sc_sia_cap & BMSR_10THDX)
5431 				mii->mii_media_active |= IFM_10_T;
5432 			else
5433 				mii->mii_media_active |= IFM_NONE;
5434 		} else {
5435 			/*
5436 			 * If the other side doesn't support NWAY, then the
5437 			 * best we can do is determine if we have a 10Mbps or
5438 			 * 100Mbps link. There's no way to know if the link
5439 			 * is full or half duplex, so we default to half duplex
5440 			 * and hope that the user is clever enough to manually
5441 			 * change the media settings if we're wrong.
5442 			 */
5443 			if ((siastat & SIASTAT_LS100) == 0)
5444 				mii->mii_media_active |= IFM_100_TX;
5445 			else if ((siastat & SIASTAT_LS10) == 0)
5446 				mii->mii_media_active |= IFM_10_T;
5447 			else
5448 				mii->mii_media_active |= IFM_NONE;
5449 		}
5450 	} else {
5451 		if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100))
5452 			mii->mii_media_status |= IFM_ACTIVE;
5453 
5454 		if (sc->sc_opmode & OPMODE_TTM)
5455 			mii->mii_media_active |= IFM_10_T;
5456 		else
5457 			mii->mii_media_active |= IFM_100_TX;
5458 		if (sc->sc_opmode & OPMODE_FD)
5459 			mii->mii_media_active |= IFM_FDX;
5460 	}
5461 }
5462 
5463 static void
5464 tlp_2114x_isv_tmsw_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
5465 {
5466 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
5467 	struct tulip_21x4x_media *tm = ife->ifm_aux;
5468 
5469 	(*tm->tm_get)(sc, ifmr);
5470 }
5471 
5472 static int
5473 tlp_2114x_isv_tmsw_set(struct tulip_softc *sc)
5474 {
5475 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
5476 	struct tulip_21x4x_media *tm = ife->ifm_aux;
5477 
5478 	/*
5479 	 * Check to see if we need to reset the chip, and do it.  The
5480 	 * reset path will get the OPMODE register right the next
5481 	 * time through.
5482 	 */
5483 	if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode))
5484 		return (tlp_init(&sc->sc_ethercom.ec_if));
5485 
5486 	return ((*tm->tm_set)(sc));
5487 }
5488 
5489 /*
5490  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
5491  */
5492 static void	tlp_sio_mii_tmsw_init(struct tulip_softc *);
5493 
5494 const struct tulip_mediasw tlp_sio_mii_mediasw = {
5495 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
5496 };
5497 
5498 static void
5499 tlp_sio_mii_tmsw_init(struct tulip_softc *sc)
5500 {
5501 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5502 
5503 	/*
5504 	 * We don't attach any media info structures to the ifmedia
5505 	 * entries, so if we're using a pre-init function that needs
5506 	 * that info, override it to one that doesn't.
5507 	 */
5508 	if (sc->sc_preinit == tlp_2114x_preinit)
5509 		sc->sc_preinit = tlp_2114x_mii_preinit;
5510 
5511 	sc->sc_mii.mii_ifp = ifp;
5512 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
5513 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
5514 	sc->sc_mii.mii_statchg = sc->sc_statchg;
5515 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
5516 	    tlp_mediastatus);
5517 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
5518 	    MII_OFFSET_ANY, 0);
5519 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
5520 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
5521 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
5522 	} else {
5523 		sc->sc_flags |= TULIPF_HAS_MII;
5524 		sc->sc_tick = tlp_mii_tick;
5525 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
5526 	}
5527 }
5528 
5529 /*
5530  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
5531  */
5532 static void	tlp_pnic_tmsw_init(struct tulip_softc *);
5533 static void	tlp_pnic_tmsw_get(struct tulip_softc *, struct ifmediareq *);
5534 static int	tlp_pnic_tmsw_set(struct tulip_softc *);
5535 
5536 const struct tulip_mediasw tlp_pnic_mediasw = {
5537 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
5538 };
5539 
5540 static void	tlp_pnic_nway_statchg(struct device *);
5541 static void	tlp_pnic_nway_tick(void *);
5542 static int	tlp_pnic_nway_service(struct tulip_softc *, int);
5543 static void	tlp_pnic_nway_reset(struct tulip_softc *);
5544 static int	tlp_pnic_nway_auto(struct tulip_softc *, int);
5545 static void	tlp_pnic_nway_auto_timeout(void *);
5546 static void	tlp_pnic_nway_status(struct tulip_softc *);
5547 static void	tlp_pnic_nway_acomp(struct tulip_softc *);
5548 
5549 static void
5550 tlp_pnic_tmsw_init(struct tulip_softc *sc)
5551 {
5552 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5553 	const char *sep = "";
5554 
5555 #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
5556 #define	PRINT(str)	printf("%s%s", sep, str); sep = ", "
5557 
5558 	sc->sc_mii.mii_ifp = ifp;
5559 	sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
5560 	sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
5561 	sc->sc_mii.mii_statchg = sc->sc_statchg;
5562 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
5563 	    tlp_mediastatus);
5564 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
5565 	    MII_OFFSET_ANY, 0);
5566 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
5567 		/* XXX What about AUI/BNC support? */
5568 		printf("%s: ", sc->sc_dev.dv_xname);
5569 
5570 		tlp_pnic_nway_reset(sc);
5571 
5572 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
5573 		    PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
5574 		PRINT("10baseT");
5575 
5576 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
5577 		    PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
5578 		PRINT("10baseT-FDX");
5579 
5580 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
5581 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
5582 		PRINT("100baseTX");
5583 
5584 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
5585 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
5586 		    PNIC_NWAY_CAP100TXFDX);
5587 		PRINT("100baseTX-FDX");
5588 
5589 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
5590 		    PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
5591 		    PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
5592 		    PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
5593 		PRINT("auto");
5594 
5595 		printf("\n");
5596 
5597 		sc->sc_statchg = tlp_pnic_nway_statchg;
5598 		sc->sc_tick = tlp_pnic_nway_tick;
5599 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
5600 	} else {
5601 		sc->sc_flags |= TULIPF_HAS_MII;
5602 		sc->sc_tick = tlp_mii_tick;
5603 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
5604 	}
5605 
5606 #undef ADD
5607 #undef PRINT
5608 }
5609 
5610 static void
5611 tlp_pnic_tmsw_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
5612 {
5613 	struct mii_data *mii = &sc->sc_mii;
5614 
5615 	if (sc->sc_flags & TULIPF_HAS_MII)
5616 		tlp_mii_getmedia(sc, ifmr);
5617 	else {
5618 		mii->mii_media_status = 0;
5619 		mii->mii_media_active = IFM_NONE;
5620 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
5621 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
5622 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
5623 	}
5624 }
5625 
5626 static int
5627 tlp_pnic_tmsw_set(struct tulip_softc *sc)
5628 {
5629 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5630 	struct mii_data *mii = &sc->sc_mii;
5631 
5632 	if (sc->sc_flags & TULIPF_HAS_MII) {
5633 		/*
5634 		 * Make sure the built-in Tx jabber timer is disabled.
5635 		 */
5636 		TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
5637 
5638 		return (tlp_mii_setmedia(sc));
5639 	}
5640 
5641 	if (ifp->if_flags & IFF_UP) {
5642 		mii->mii_media_status = 0;
5643 		mii->mii_media_active = IFM_NONE;
5644 		return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
5645 	}
5646 
5647 	return (0);
5648 }
5649 
5650 static void
5651 tlp_pnic_nway_statchg(struct device *self)
5652 {
5653 	struct tulip_softc *sc = (struct tulip_softc *)self;
5654 
5655 	/* Idle the transmit and receive processes. */
5656 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
5657 
5658 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
5659 	    OPMODE_SCR|OPMODE_HBD);
5660 
5661 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
5662 		sc->sc_opmode |= OPMODE_TTM;
5663 		TULIP_WRITE(sc, CSR_GPP,
5664 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
5665 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
5666 	} else {
5667 		sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD;
5668 		TULIP_WRITE(sc, CSR_GPP,
5669 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
5670 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
5671 	}
5672 
5673 	if (sc->sc_mii.mii_media_active & IFM_FDX)
5674 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
5675 
5676 	/*
5677 	 * Write new OPMODE bits.  This also restarts the transmit
5678 	 * and receive processes.
5679 	 */
5680 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
5681 }
5682 
5683 static void
5684 tlp_pnic_nway_tick(void *arg)
5685 {
5686 	struct tulip_softc *sc = arg;
5687 	int s;
5688 
5689 	if (!device_is_active(&sc->sc_dev))
5690 		return;
5691 
5692 	s = splnet();
5693 	tlp_pnic_nway_service(sc, MII_TICK);
5694 	splx(s);
5695 
5696 	callout_reset(&sc->sc_tick_callout, hz, tlp_pnic_nway_tick, sc);
5697 }
5698 
5699 /*
5700  * Support for the Lite-On PNIC internal NWay block.  This is constructed
5701  * somewhat like a PHY driver for simplicity.
5702  */
5703 
5704 static int
5705 tlp_pnic_nway_service(struct tulip_softc *sc, int cmd)
5706 {
5707 	struct mii_data *mii = &sc->sc_mii;
5708 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
5709 
5710 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
5711 		return (0);
5712 
5713 	switch (cmd) {
5714 	case MII_POLLSTAT:
5715 		/* Nothing special to do here. */
5716 		break;
5717 
5718 	case MII_MEDIACHG:
5719 		switch (IFM_SUBTYPE(ife->ifm_media)) {
5720 		case IFM_AUTO:
5721 			(void) tlp_pnic_nway_auto(sc, 1);
5722 			break;
5723 		case IFM_100_T4:
5724 			/*
5725 			 * XXX Not supported as a manual setting right now.
5726 			 */
5727 			return (EINVAL);
5728 		default:
5729 			/*
5730 			 * NWAY register data is stored in the ifmedia entry.
5731 			 */
5732 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
5733 		}
5734 		break;
5735 
5736 	case MII_TICK:
5737 		/*
5738 		 * Only used for autonegotiation.
5739 		 */
5740 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
5741 			return (0);
5742 
5743 		/*
5744 		 * Check to see if we have link.  If we do, we don't
5745 		 * need to restart the autonegotiation process.
5746 		 */
5747 		if (sc->sc_flags & TULIPF_LINK_UP)
5748 			return (0);
5749 
5750 		/*
5751 		 * Only retry autonegotiation every 5 seconds.
5752 		 */
5753 		if (++sc->sc_nway_ticks != 5)
5754 			return (0);
5755 
5756 		sc->sc_nway_ticks = 0;
5757 		tlp_pnic_nway_reset(sc);
5758 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
5759 			return (0);
5760 		break;
5761 	}
5762 
5763 	/* Update the media status. */
5764 	tlp_pnic_nway_status(sc);
5765 
5766 	/* Callback if something changed. */
5767 	if ((sc->sc_nway_active == NULL ||
5768 	     sc->sc_nway_active->ifm_media != mii->mii_media_active) ||
5769 	    cmd == MII_MEDIACHG) {
5770 		(*sc->sc_statchg)(&sc->sc_dev);
5771 		tlp_nway_activate(sc, mii->mii_media_active);
5772 	}
5773 	return (0);
5774 }
5775 
5776 static void
5777 tlp_pnic_nway_reset(struct tulip_softc *sc)
5778 {
5779 
5780 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
5781 	delay(100);
5782 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
5783 }
5784 
5785 static int
5786 tlp_pnic_nway_auto(struct tulip_softc *sc, int waitfor)
5787 {
5788 	struct mii_data *mii = &sc->sc_mii;
5789 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
5790 	u_int32_t reg;
5791 	int i;
5792 
5793 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
5794 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
5795 
5796 	if (waitfor) {
5797 		/* Wait 500ms for it to complete. */
5798 		for (i = 0; i < 500; i++) {
5799 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5800 			if (reg & PNIC_NWAY_LPAR_MASK) {
5801 				tlp_pnic_nway_acomp(sc);
5802 				return (0);
5803 			}
5804 			delay(1000);
5805 		}
5806 #if 0
5807 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
5808 			printf("%s: autonegotiation failed to complete\n",
5809 			    sc->sc_dev.dv_xname);
5810 #endif
5811 
5812 		/*
5813 		 * Don't need to worry about clearing DOINGAUTO.
5814 		 * If that's set, a timeout is pending, and it will
5815 		 * clear the flag.
5816 		 */
5817 		return (EIO);
5818 	}
5819 
5820 	/*
5821 	 * Just let it finish asynchronously.  This is for the benefit of
5822 	 * the tick handler driving autonegotiation.  Don't want 500ms
5823 	 * delays all the time while the system is running!
5824 	 */
5825 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
5826 		sc->sc_flags |= TULIPF_DOINGAUTO;
5827 		callout_reset(&sc->sc_nway_callout, hz >> 1,
5828 		    tlp_pnic_nway_auto_timeout, sc);
5829 	}
5830 	return (EJUSTRETURN);
5831 }
5832 
5833 static void
5834 tlp_pnic_nway_auto_timeout(void *arg)
5835 {
5836 	struct tulip_softc *sc = arg;
5837 	u_int32_t reg;
5838 	int s;
5839 
5840 	s = splnet();
5841 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
5842 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5843 #if 0
5844 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
5845 		printf("%s: autonegotiation failed to complete\n",
5846 		    sc->sc_dev.dv_xname);
5847 #endif
5848 
5849 	tlp_pnic_nway_acomp(sc);
5850 
5851 	/* Update the media status. */
5852 	(void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
5853 	splx(s);
5854 }
5855 
5856 static void
5857 tlp_pnic_nway_status(struct tulip_softc *sc)
5858 {
5859 	struct mii_data *mii = &sc->sc_mii;
5860 	u_int32_t reg;
5861 
5862 	mii->mii_media_status = IFM_AVALID;
5863 	mii->mii_media_active = IFM_ETHER;
5864 
5865 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5866 
5867 	if (sc->sc_flags & TULIPF_LINK_UP)
5868 		mii->mii_media_status |= IFM_ACTIVE;
5869 
5870 	if (reg & PNIC_NWAY_NW) {
5871 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
5872 			/* Erg, still trying, I guess... */
5873 			mii->mii_media_active |= IFM_NONE;
5874 			return;
5875 		}
5876 
5877 #if 0
5878 		if (reg & PNIC_NWAY_LPAR100T4)
5879 			mii->mii_media_active |= IFM_100_T4;
5880 		else
5881 #endif
5882 		if (reg & PNIC_NWAY_LPAR100TXFDX)
5883 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
5884 		else if (reg & PNIC_NWAY_LPAR100TX)
5885 			mii->mii_media_active |= IFM_100_TX;
5886 		else if (reg & PNIC_NWAY_LPAR10TFDX)
5887 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
5888 		else if (reg & PNIC_NWAY_LPAR10T)
5889 			mii->mii_media_active |= IFM_10_T;
5890 		else
5891 			mii->mii_media_active |= IFM_NONE;
5892 	} else {
5893 		if (reg & PNIC_NWAY_100)
5894 			mii->mii_media_active |= IFM_100_TX;
5895 		else
5896 			mii->mii_media_active |= IFM_10_T;
5897 		if (reg & PNIC_NWAY_FD)
5898 			mii->mii_media_active |= IFM_FDX;
5899 	}
5900 }
5901 
5902 static void
5903 tlp_pnic_nway_acomp(struct tulip_softc *sc)
5904 {
5905 	u_int32_t reg;
5906 
5907 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
5908 	reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
5909 
5910 	if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
5911 		reg |= PNIC_NWAY_100;
5912 	if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
5913 		reg |= PNIC_NWAY_FD;
5914 
5915 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
5916 }
5917 
5918 /*
5919  * Macronix PMAC and Lite-On PNIC-II media switch:
5920  *
5921  *	MX98713 and MX98713A		21140-like MII or GPIO media.
5922  *
5923  *	MX98713A			21143-like MII or SIA/SYM media.
5924  *
5925  *	MX98715, MX98715A, MX98725,	21143-like SIA/SYM media.
5926  *	82C115, MX98715AEC-C, -E
5927  *
5928  * So, what we do here is fake MII-on-SIO or ISV media info, and
5929  * use the ISV media switch get/set functions to handle the rest.
5930  */
5931 
5932 static void	tlp_pmac_tmsw_init(struct tulip_softc *);
5933 
5934 const struct tulip_mediasw tlp_pmac_mediasw = {
5935 	tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
5936 };
5937 
5938 const struct tulip_mediasw tlp_pmac_mii_mediasw = {
5939 	tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
5940 };
5941 
5942 static void
5943 tlp_pmac_tmsw_init(struct tulip_softc *sc)
5944 {
5945 	static const u_int8_t media[] = {
5946 		TULIP_ROM_MB_MEDIA_TP,
5947 		TULIP_ROM_MB_MEDIA_TP_FDX,
5948 		TULIP_ROM_MB_MEDIA_100TX,
5949 		TULIP_ROM_MB_MEDIA_100TX_FDX,
5950 	};
5951 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
5952 	struct tulip_21x4x_media *tm;
5953 
5954 	sc->sc_mii.mii_ifp = ifp;
5955 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
5956 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
5957 	sc->sc_mii.mii_statchg = sc->sc_statchg;
5958 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
5959 	    tlp_mediastatus);
5960 	if (sc->sc_chip == TULIP_CHIP_MX98713 ||
5961 	    sc->sc_chip == TULIP_CHIP_MX98713A) {
5962 		mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
5963 		    MII_PHY_ANY, MII_OFFSET_ANY, 0);
5964 		if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) {
5965 			sc->sc_flags |= TULIPF_HAS_MII;
5966 			sc->sc_tick = tlp_mii_tick;
5967 			sc->sc_preinit = tlp_2114x_mii_preinit;
5968 			sc->sc_mediasw = &tlp_pmac_mii_mediasw;
5969 			ifmedia_set(&sc->sc_mii.mii_media,
5970 			    IFM_ETHER|IFM_AUTO);
5971 			return;
5972 		}
5973 	}
5974 
5975 	switch (sc->sc_chip) {
5976 	case TULIP_CHIP_MX98713:
5977 		tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR,
5978 		    tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4);
5979 
5980 		/*
5981 		 * XXX Should implement auto-sense for this someday,
5982 		 * XXX when we do the same for the 21140.
5983 		 */
5984 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
5985 		break;
5986 
5987 	default:
5988 		tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA,
5989 		    tlp_sia_get, tlp_sia_set, media, 2);
5990 		tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM,
5991 		    tlp_sia_get, tlp_sia_set, media + 2, 2);
5992 
5993 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
5994 		tm->tm_name = "auto";
5995 		tm->tm_get = tlp_2114x_nway_get;
5996 		tm->tm_set = tlp_2114x_nway_set;
5997 		ifmedia_add(&sc->sc_mii.mii_media,
5998 		    IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 0, tm);
5999 
6000 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6001 		sc->sc_statchg = tlp_2114x_nway_statchg;
6002 		sc->sc_tick = tlp_2114x_nway_tick;
6003 		break;
6004 	}
6005 
6006 	tlp_print_media(sc);
6007 	tlp_sia_fixup(sc);
6008 
6009 	/* Set the LED modes. */
6010 	tlp_pmac_reset(sc);
6011 
6012 	sc->sc_reset = tlp_pmac_reset;
6013 }
6014 
6015 /*
6016  * ADMtek AL981 media switch.  Only has internal PHY.
6017  */
6018 static void	tlp_al981_tmsw_init(struct tulip_softc *);
6019 
6020 const struct tulip_mediasw tlp_al981_mediasw = {
6021 	tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
6022 };
6023 
6024 static void
6025 tlp_al981_tmsw_init(struct tulip_softc *sc)
6026 {
6027 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
6028 
6029 	sc->sc_mii.mii_ifp = ifp;
6030 	sc->sc_mii.mii_readreg = tlp_al981_mii_readreg;
6031 	sc->sc_mii.mii_writereg = tlp_al981_mii_writereg;
6032 	sc->sc_mii.mii_statchg = sc->sc_statchg;
6033 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
6034 	    tlp_mediastatus);
6035 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
6036 	    MII_OFFSET_ANY, 0);
6037 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
6038 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
6039 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
6040 	} else {
6041 		sc->sc_flags |= TULIPF_HAS_MII;
6042 		sc->sc_tick = tlp_mii_tick;
6043 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6044 	}
6045 }
6046 
6047 /*
6048  * ADMtek AN983/985 media switch.  Only has internal PHY, but
6049  * on an SIO-like interface.  Unfortunately, we can't use the
6050  * standard SIO media switch, because the AN985 "ghosts" the
6051  * singly PHY at every address.
6052  */
6053 static void	tlp_an985_tmsw_init(struct tulip_softc *);
6054 
6055 const struct tulip_mediasw tlp_an985_mediasw = {
6056 	tlp_an985_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
6057 };
6058 
6059 static void
6060 tlp_an985_tmsw_init(struct tulip_softc *sc)
6061 {
6062 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
6063 
6064 	sc->sc_mii.mii_ifp = ifp;
6065 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
6066 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
6067 	sc->sc_mii.mii_statchg = sc->sc_statchg;
6068 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
6069 	    tlp_mediastatus);
6070 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, 1,
6071 	    MII_OFFSET_ANY, 0);
6072 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
6073 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
6074 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
6075 	} else {
6076 		sc->sc_flags |= TULIPF_HAS_MII;
6077 		sc->sc_tick = tlp_mii_tick;
6078 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6079 	}
6080 }
6081 
6082 /*
6083  * Davicom DM9102 media switch.  Internal PHY and possibly HomePNA.
6084  */
6085 static void	tlp_dm9102_tmsw_init(struct tulip_softc *);
6086 static void	tlp_dm9102_tmsw_getmedia(struct tulip_softc *,
6087 		    struct ifmediareq *);
6088 static int	tlp_dm9102_tmsw_setmedia(struct tulip_softc *);
6089 
6090 const struct tulip_mediasw tlp_dm9102_mediasw = {
6091 	tlp_dm9102_tmsw_init, tlp_dm9102_tmsw_getmedia,
6092 	    tlp_dm9102_tmsw_setmedia
6093 };
6094 
6095 static void
6096 tlp_dm9102_tmsw_init(struct tulip_softc *sc)
6097 {
6098 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
6099 	u_int32_t opmode;
6100 
6101 	sc->sc_mii.mii_ifp = ifp;
6102 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
6103 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
6104 	sc->sc_mii.mii_statchg = sc->sc_statchg;
6105 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
6106 	    tlp_mediastatus);
6107 
6108 	/* PHY block already reset via tlp_reset(). */
6109 
6110 	/*
6111 	 * Configure OPMODE properly for the internal MII interface.
6112 	 */
6113 	switch (sc->sc_chip) {
6114 	case TULIP_CHIP_DM9102:
6115 		opmode = OPMODE_MBO|OPMODE_HBD|OPMODE_PS;
6116 		break;
6117 
6118 	case TULIP_CHIP_DM9102A:
6119 		opmode = OPMODE_MBO|OPMODE_HBD;
6120 		break;
6121 
6122 	default:
6123 		opmode = 0;
6124 		break;
6125 	}
6126 
6127 	TULIP_WRITE(sc, CSR_OPMODE, opmode);
6128 
6129 	/* Now, probe the internal MII for the internal PHY. */
6130 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
6131 	    MII_OFFSET_ANY, 0);
6132 
6133 	/*
6134 	 * XXX Figure out what to do about the HomePNA portion
6135 	 * XXX of the DM9102A.
6136 	 */
6137 
6138 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
6139 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
6140 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
6141 	} else {
6142 		sc->sc_flags |= TULIPF_HAS_MII;
6143 		sc->sc_tick = tlp_mii_tick;
6144 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6145 	}
6146 }
6147 
6148 static void
6149 tlp_dm9102_tmsw_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
6150 {
6151 
6152 	/* XXX HomePNA on DM9102A. */
6153 	tlp_mii_getmedia(sc, ifmr);
6154 }
6155 
6156 static int
6157 tlp_dm9102_tmsw_setmedia(struct tulip_softc *sc)
6158 {
6159 
6160 	/* XXX HomePNA on DM9102A. */
6161 	return (tlp_mii_setmedia(sc));
6162 }
6163 
6164 /*
6165  * ASIX AX88140A/AX88141 media switch. Internal PHY or MII.
6166  */
6167 
6168 static void	tlp_asix_tmsw_init(struct tulip_softc *);
6169 static void	tlp_asix_tmsw_getmedia(struct tulip_softc *,
6170 		    struct ifmediareq *);
6171 static int	tlp_asix_tmsw_setmedia(struct tulip_softc *);
6172 
6173 const struct tulip_mediasw tlp_asix_mediasw = {
6174 	tlp_asix_tmsw_init, tlp_asix_tmsw_getmedia,
6175 	tlp_asix_tmsw_setmedia
6176 };
6177 
6178 static void
6179 tlp_asix_tmsw_init(struct tulip_softc *sc)
6180 {
6181 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
6182 	u_int32_t opmode;
6183 
6184 	sc->sc_mii.mii_ifp = ifp;
6185         sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
6186         sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
6187 	sc->sc_mii.mii_statchg = sc->sc_statchg;
6188 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
6189             tlp_mediastatus);
6190 
6191 	/*
6192 	 * Configure OPMODE properly for the internal MII interface.
6193 	 */
6194 	switch (sc->sc_chip) {
6195 	case TULIP_CHIP_AX88140:
6196 	case TULIP_CHIP_AX88141:
6197 		opmode = OPMODE_HBD|OPMODE_PS;
6198 		break;
6199         default:
6200                 opmode = 0;
6201                 break;
6202         }
6203 
6204 	TULIP_WRITE(sc, CSR_OPMODE, opmode);
6205 
6206 	/* Now, probe the internal MII for the internal PHY. */
6207 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
6208 	    MII_OFFSET_ANY, 0);
6209 
6210 	/* XXX Figure how to handle the PHY. */
6211 
6212 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
6213 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
6214 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
6215 	} else {
6216 		sc->sc_flags |= TULIPF_HAS_MII;
6217 		sc->sc_tick = tlp_mii_tick;
6218 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6219 	}
6220 
6221 
6222 }
6223 
6224 static void
6225 tlp_asix_tmsw_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
6226 {
6227 
6228 	/* XXX PHY handling. */
6229 	tlp_mii_getmedia(sc, ifmr);
6230 }
6231 
6232 static int
6233 tlp_asix_tmsw_setmedia(struct tulip_softc *sc)
6234 {
6235 
6236 	/* XXX PHY handling. */
6237 	return (tlp_mii_setmedia(sc));
6238 }
6239 
6240 /*
6241  * RS7112 media switch.  Handles only MII attached to the SIO.
6242  * We only have a PHY at 1.
6243  */
6244 void   tlp_rs7112_tmsw_init(struct tulip_softc *);
6245 
6246 const struct tulip_mediasw tlp_rs7112_mediasw = {
6247 	tlp_rs7112_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
6248 };
6249 
6250 void
6251 tlp_rs7112_tmsw_init(struct tulip_softc *sc)
6252 {
6253 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
6254 
6255 	/*
6256 	 * We don't attach any media info structures to the ifmedia
6257 	 * entries, so if we're using a pre-init function that needs
6258 	 * that info, override it to one that doesn't.
6259 	 */
6260 	if (sc->sc_preinit == tlp_2114x_preinit)
6261 		sc->sc_preinit = tlp_2114x_mii_preinit;
6262 
6263 	sc->sc_mii.mii_ifp = ifp;
6264 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
6265 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
6266 	sc->sc_mii.mii_statchg = sc->sc_statchg;
6267 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
6268 	    tlp_mediastatus);
6269 
6270 	/*
6271 	 * The RS7112 reports a PHY at 0 (possibly HomePNA?)
6272 	 * and 1 (ethernet). We attach ethernet only.
6273 	 */
6274 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, 1,
6275 	    MII_OFFSET_ANY, 0);
6276 
6277 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
6278 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
6279 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
6280 	} else {
6281 		sc->sc_flags |= TULIPF_HAS_MII;
6282 		sc->sc_tick = tlp_mii_tick;
6283 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
6284 	}
6285 }
6286