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