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