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