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