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