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