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