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