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