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