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