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