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