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