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