xref: /netbsd-src/sys/dev/ic/tulip.c (revision 3b435a73967be44dfb4a27315acd72bfacde430c)
1 /*	$NetBSD: tulip.c,v 1.26 1999/09/30 22:28:11 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Device driver for the Digital Semiconductor ``Tulip'' (21x4x)
42  * Ethernet controller family, and a variety of clone chips.
43  */
44 
45 #include "opt_inet.h"
46 #include "opt_ns.h"
47 #include "bpfilter.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/mbuf.h>
52 #include <sys/malloc.h>
53 #include <sys/kernel.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <sys/errno.h>
57 #include <sys/device.h>
58 
59 #include <vm/vm.h>		/* for PAGE_SIZE */
60 
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_media.h>
64 #include <net/if_ether.h>
65 
66 #if NBPFILTER > 0
67 #include <net/bpf.h>
68 #endif
69 
70 #ifdef INET
71 #include <netinet/in.h>
72 #include <netinet/if_inarp.h>
73 #endif
74 
75 #ifdef NS
76 #include <netns/ns.h>
77 #include <netns/ns_if.h>
78 #endif
79 
80 #include <machine/bus.h>
81 #include <machine/intr.h>
82 
83 #include <dev/mii/mii.h>
84 #include <dev/mii/miivar.h>
85 
86 #include <dev/ic/tulipreg.h>
87 #include <dev/ic/tulipvar.h>
88 
89 /*
90  * The following tables compute the transmit threshold mode.  We start
91  * at index 0.  When ever we get a transmit underrun, we increment our
92  * index, falling back if we encounter the NULL terminator.
93  *
94  * Note: Store and forward mode is only available on the 100mbps chips
95  * (21140 and higher).
96  */
97 const struct tulip_txthresh_tab tlp_10_txthresh_tab[] = {
98 	{ OPMODE_TR_72,		"72 bytes" },
99 	{ OPMODE_TR_96,		"96 bytes" },
100 	{ OPMODE_TR_128,	"128 bytes" },
101 	{ OPMODE_TR_160,	"160 bytes" },
102 	{ 0,			NULL },
103 };
104 
105 const struct tulip_txthresh_tab tlp_10_100_txthresh_tab[] = {
106 	{ OPMODE_TR_72,		"72/128 bytes" },
107 	{ OPMODE_TR_96,		"96/256 bytes" },
108 	{ OPMODE_TR_128,	"128/512 bytes" },
109 	{ OPMODE_TR_160,	"160/1024 bytes" },
110 	{ OPMODE_SF,		"store and forward mode" },
111 	{ 0,			NULL },
112 };
113 
114 #define	TXTH_72		0
115 #define	TXTH_96		1
116 #define	TXTH_128	2
117 #define	TXTH_160	3
118 #define	TXTH_SF		4
119 
120 /*
121  * The Winbond 89C840F does transmit threshold control totally
122  * differently.  It simply has a 7-bit field which indicates
123  * the threshold:
124  *
125  *	txth = ((OPMODE & OPMODE_WINB_TTH) >> OPMODE_WINB_TTH_SHIFT) * 16;
126  *
127  * However, we just do Store-and-Forward mode on these chips, since
128  * the DMA engines seem to be flaky.
129  */
130 const struct tulip_txthresh_tab tlp_winb_txthresh_tab[] = {
131 	{ 0,			"store and forward mode" },
132 	{ 0,			NULL },
133 };
134 
135 #define	TXTH_WINB_SF	0
136 
137 void	tlp_start __P((struct ifnet *));
138 void	tlp_watchdog __P((struct ifnet *));
139 int	tlp_ioctl __P((struct ifnet *, u_long, caddr_t));
140 
141 void	tlp_shutdown __P((void *));
142 
143 void	tlp_reset __P((struct tulip_softc *));
144 int	tlp_init __P((struct tulip_softc *));
145 void	tlp_rxdrain __P((struct tulip_softc *));
146 void	tlp_stop __P((struct tulip_softc *, int));
147 int	tlp_add_rxbuf __P((struct tulip_softc *, int));
148 void	tlp_idle __P((struct tulip_softc *, u_int32_t));
149 void	tlp_srom_idle __P((struct tulip_softc *));
150 
151 void	tlp_filter_setup __P((struct tulip_softc *));
152 void	tlp_winb_filter_setup __P((struct tulip_softc *));
153 void	tlp_al981_filter_setup __P((struct tulip_softc *));
154 
155 void	tlp_rxintr __P((struct tulip_softc *));
156 void	tlp_txintr __P((struct tulip_softc *));
157 
158 void	tlp_mii_tick __P((void *));
159 void	tlp_mii_statchg __P((struct device *));
160 void	tlp_winb_mii_statchg __P((struct device *));
161 
162 void	tlp_mii_getmedia __P((struct tulip_softc *, struct ifmediareq *));
163 int	tlp_mii_setmedia __P((struct tulip_softc *));
164 
165 void	tlp_sio_mii_sync __P((struct tulip_softc *));
166 void	tlp_sio_mii_sendbits __P((struct tulip_softc *, u_int32_t, int));
167 int	tlp_sio_mii_readreg __P((struct device *, int, int));
168 void	tlp_sio_mii_writereg __P((struct device *, int, int, int));
169 
170 int	tlp_pnic_mii_readreg __P((struct device *, int, int));
171 void	tlp_pnic_mii_writereg __P((struct device *, int, int, int));
172 
173 int	tlp_al981_mii_readreg __P((struct device *, int, int));
174 void	tlp_al981_mii_writereg __P((struct device *, int, int, int));
175 
176 void	tlp_2114x_preinit __P((struct tulip_softc *));
177 void	tlp_pnic_preinit __P((struct tulip_softc *));
178 
179 void	tlp_21140_reset __P((struct tulip_softc *));
180 void	tlp_pmac_reset __P((struct tulip_softc *));
181 
182 u_int32_t tlp_crc32 __P((const u_int8_t *, size_t));
183 #define	tlp_mchash(addr, sz) (tlp_crc32((addr), ETHER_ADDR_LEN) & ((sz) - 1))
184 
185 #ifdef TLP_DEBUG
186 #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
187 				printf x
188 #else
189 #define	DPRINTF(sc, x)	/* nothing */
190 #endif
191 
192 #ifdef TLP_STATS
193 void	tlp_print_stats __P((struct tulip_softc *));
194 #endif
195 
196 /*
197  * tlp_attach:
198  *
199  *	Attach a Tulip interface to the system.
200  */
201 void
202 tlp_attach(sc, enaddr)
203 	struct tulip_softc *sc;
204 	const u_int8_t *enaddr;
205 {
206 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
207 	int i, rseg, error;
208 	bus_dma_segment_t seg;
209 
210 	/*
211 	 * NOTE: WE EXPECT THE FRONT-END TO INITIALIZE sc_regshift!
212 	 */
213 
214 	/*
215 	 * Setup the transmit threshold table.
216 	 */
217 	switch (sc->sc_chip) {
218 	case TULIP_CHIP_DE425:
219 	case TULIP_CHIP_21040:
220 	case TULIP_CHIP_21041:
221 		sc->sc_txth = tlp_10_txthresh_tab;
222 		break;
223 
224 	default:
225 		sc->sc_txth = tlp_10_100_txthresh_tab;
226 		break;
227 	}
228 
229 	/*
230 	 * Setup the filter setup function.
231 	 */
232 	switch (sc->sc_chip) {
233 	case TULIP_CHIP_WB89C840F:
234 		sc->sc_filter_setup = tlp_winb_filter_setup;
235 		break;
236 
237 	case TULIP_CHIP_AL981:
238 		sc->sc_filter_setup = tlp_al981_filter_setup;
239 		break;
240 
241 	default:
242 		sc->sc_filter_setup = tlp_filter_setup;
243 		break;
244 	}
245 
246 	/*
247 	 * Set up the media status change function.
248 	 */
249 	switch (sc->sc_chip) {
250 	case TULIP_CHIP_WB89C840F:
251 		sc->sc_statchg = tlp_winb_mii_statchg;
252 		break;
253 
254 	default:
255 		/*
256 		 * We may override this if we have special media
257 		 * handling requirements (e.g. flipping GPIO pins).
258 		 *
259 		 * The pure-MII statchg function covers the basics.
260 		 */
261 		sc->sc_statchg = tlp_mii_statchg;
262 		break;
263 	}
264 
265 	/*
266 	 * Set up various chip-specific quirks.
267 	 */
268 	switch (sc->sc_chip) {
269 	case TULIP_CHIP_21140:
270 	case TULIP_CHIP_21140A:
271 	case TULIP_CHIP_21142:
272 	case TULIP_CHIP_21143:
273 		sc->sc_preinit = tlp_2114x_preinit;
274 		break;
275 
276 	case TULIP_CHIP_82C168:
277 	case TULIP_CHIP_82C169:
278 		sc->sc_preinit = tlp_pnic_preinit;
279 
280 		/*
281 		 * These chips seem to have busted DMA engines; just put them
282 		 * in Store-and-Forward mode from the get-go.
283 		 */
284 		sc->sc_txthresh = TXTH_SF;
285 		break;
286 
287 	case TULIP_CHIP_WB89C840F:
288 		sc->sc_flags |= TULIPF_IC_FS;
289 		break;
290 
291 	default:
292 		/* Nothing. */
293 	}
294 
295 	SIMPLEQ_INIT(&sc->sc_txfreeq);
296 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
297 
298 	/*
299 	 * Allocate the control data structures, and create and load the
300 	 * DMA map for it.
301 	 */
302 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
303 	    sizeof(struct tulip_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
304 	    0)) != 0) {
305 		printf("%s: unable to allocate control data, error = %d\n",
306 		    sc->sc_dev.dv_xname, error);
307 		goto fail_0;
308 	}
309 
310 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
311 	    sizeof(struct tulip_control_data), (caddr_t *)&sc->sc_control_data,
312 	    BUS_DMA_COHERENT)) != 0) {
313 		printf("%s: unable to map control data, error = %d\n",
314 		    sc->sc_dev.dv_xname, error);
315 		goto fail_1;
316 	}
317 
318 	if ((error = bus_dmamap_create(sc->sc_dmat,
319 	    sizeof(struct tulip_control_data), 1,
320 	    sizeof(struct tulip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
321 		printf("%s: unable to create control data DMA map, "
322 		    "error = %d\n", sc->sc_dev.dv_xname, error);
323 		goto fail_2;
324 	}
325 
326 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
327 	    sc->sc_control_data, sizeof(struct tulip_control_data), NULL,
328 	    0)) != 0) {
329 		printf("%s: unable to load control data DMA map, error = %d\n",
330 		    sc->sc_dev.dv_xname, error);
331 		goto fail_3;
332 	}
333 
334 	/*
335 	 * Create the transmit buffer DMA maps.
336 	 */
337 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
338 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
339 		    TULIP_NTXSEGS, MCLBYTES, 0, 0,
340 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
341 			printf("%s: unable to create tx DMA map %d, "
342 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
343 			goto fail_4;
344 		}
345 	}
346 
347 	/*
348 	 * Create the recieve buffer DMA maps.
349 	 */
350 	for (i = 0; i < TULIP_NRXDESC; i++) {
351 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
352 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
353 			printf("%s: unable to create rx DMA map %d, "
354 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
355 			goto fail_5;
356 		}
357 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
358 	}
359 
360 	/*
361 	 * Reset the chip to a known state.
362 	 */
363 	tlp_reset(sc);
364 
365 	/* Announce ourselves. */
366 	printf("%s: %s%sEthernet address %s\n", sc->sc_dev.dv_xname,
367 	    sc->sc_name[0] != '\0' ? sc->sc_name : "",
368 	    sc->sc_name[0] != '\0' ? ", " : "",
369 	    ether_sprintf(enaddr));
370 
371 	/*
372 	 * Initialize our media structures.  This may probe the MII, if
373 	 * present.
374 	 */
375 	(*sc->sc_mediasw->tmsw_init)(sc);
376 
377 	ifp = &sc->sc_ethercom.ec_if;
378 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
379 	ifp->if_softc = sc;
380 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
381 	ifp->if_ioctl = tlp_ioctl;
382 	ifp->if_start = tlp_start;
383 	ifp->if_watchdog = tlp_watchdog;
384 
385 	/*
386 	 * Attach the interface.
387 	 */
388 	if_attach(ifp);
389 	ether_ifattach(ifp, enaddr);
390 #if NBPFILTER > 0
391 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
392 	    sizeof(struct ether_header));
393 #endif
394 
395 	/*
396 	 * Make sure the interface is shutdown during reboot.
397 	 */
398 	sc->sc_sdhook = shutdownhook_establish(tlp_shutdown, sc);
399 	if (sc->sc_sdhook == NULL)
400 		printf("%s: WARNING: unable to establish shutdown hook\n",
401 		    sc->sc_dev.dv_xname);
402 	return;
403 
404 	/*
405 	 * Free any resources we've allocated during the failed attach
406 	 * attempt.  Do this in reverse order and fall through.
407 	 */
408  fail_5:
409 	for (i = 0; i < TULIP_NRXDESC; i++) {
410 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
411 			bus_dmamap_destroy(sc->sc_dmat,
412 			    sc->sc_rxsoft[i].rxs_dmamap);
413 	}
414  fail_4:
415 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
416 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
417 			bus_dmamap_destroy(sc->sc_dmat,
418 			    sc->sc_txsoft[i].txs_dmamap);
419 	}
420 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
421  fail_3:
422 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
423  fail_2:
424 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
425 	    sizeof(struct tulip_control_data));
426  fail_1:
427 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
428  fail_0:
429 	return;
430 }
431 
432 /*
433  * tlp_shutdown:
434  *
435  *	Make sure the interface is stopped at reboot time.
436  */
437 void
438 tlp_shutdown(arg)
439 	void *arg;
440 {
441 	struct tulip_softc *sc = arg;
442 
443 	tlp_stop(sc, 1);
444 }
445 
446 /*
447  * tlp_start:		[ifnet interface function]
448  *
449  *	Start packet transmission on the interface.
450  */
451 void
452 tlp_start(ifp)
453 	struct ifnet *ifp;
454 {
455 	struct tulip_softc *sc = ifp->if_softc;
456 	struct mbuf *m0, *m;
457 	struct tulip_txsoft *txs, *last_txs;
458 	bus_dmamap_t dmamap;
459 	int error, firsttx, nexttx, lasttx, ofree, seg;
460 
461 	DPRINTF(sc, ("%s: tlp_start: sc_flags 0x%08x, if_flags 0x%08x\n",
462 	    sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
463 
464 	/*
465 	 * If we want a filter setup, it means no more descriptors were
466 	 * available for the setup routine.  Let it get a chance to wedge
467 	 * itself into the ring.
468 	 */
469 	if (sc->sc_flags & TULIPF_WANT_SETUP)
470 		ifp->if_flags |= IFF_OACTIVE;
471 
472 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
473 		return;
474 
475 	/*
476 	 * Remember the previous number of free descriptors and
477 	 * the first descriptor we'll use.
478 	 */
479 	ofree = sc->sc_txfree;
480 	firsttx = sc->sc_txnext;
481 
482 	DPRINTF(sc, ("%s: tlp_start: txfree %d, txnext %d\n",
483 	    sc->sc_dev.dv_xname, ofree, firsttx));
484 
485 	/*
486 	 * Loop through the send queue, setting up transmit descriptors
487 	 * until we drain the queue, or use up all available transmit
488 	 * descriptors.
489 	 */
490 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
491 	       sc->sc_txfree != 0) {
492 		/*
493 		 * Grab a packet off the queue.
494 		 */
495 		IF_DEQUEUE(&ifp->if_snd, m0);
496 		if (m0 == NULL)
497 			break;
498 
499 		dmamap = txs->txs_dmamap;
500 
501 		/*
502 		 * Load the DMA map.  If this fails, the packet either
503 		 * didn't fit in the alloted number of segments, or we were
504 		 * short on resources.  In this case, we'll copy and try
505 		 * again.
506 		 */
507 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
508 		    BUS_DMA_NOWAIT) != 0) {
509 			MGETHDR(m, M_DONTWAIT, MT_DATA);
510 			if (m == NULL) {
511 				printf("%s: unable to allocate Tx mbuf\n",
512 				    sc->sc_dev.dv_xname);
513 				IF_PREPEND(&ifp->if_snd, m0);
514 				break;
515 			}
516 			if (m0->m_pkthdr.len > MHLEN) {
517 				MCLGET(m, M_DONTWAIT);
518 				if ((m->m_flags & M_EXT) == 0) {
519 					printf("%s: unable to allocate Tx "
520 					    "cluster\n", sc->sc_dev.dv_xname);
521 					m_freem(m);
522 					IF_PREPEND(&ifp->if_snd, m0);
523 					break;
524 				}
525 			}
526 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
527 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
528 			m_freem(m0);
529 			m0 = m;
530 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
531 			    m0, BUS_DMA_NOWAIT);
532 			if (error) {
533 				printf("%s: unable to load Tx buffer, "
534 				    "error = %d\n", sc->sc_dev.dv_xname, error);
535 				IF_PREPEND(&ifp->if_snd, m0);
536 				break;
537 			}
538 		}
539 
540 		/*
541 		 * Ensure we have enough descriptors free to describe
542 		 * the packet.
543 		 */
544 		if (dmamap->dm_nsegs > sc->sc_txfree) {
545 			/*
546 			 * Not enough free descriptors to transmit this
547 			 * packet.  We haven't committed to anything yet,
548 			 * so just unload the DMA map, put the packet
549 			 * back on the queue, and punt.  Notify the upper
550 			 * layer that there are no more slots left.
551 			 *
552 			 * XXX We could allocate an mbuf and copy, but
553 			 * XXX it is worth it?
554 			 */
555 			ifp->if_flags |= IFF_OACTIVE;
556 			bus_dmamap_unload(sc->sc_dmat, dmamap);
557 			IF_PREPEND(&ifp->if_snd, m0);
558 			break;
559 		}
560 
561 		/*
562 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
563 		 */
564 
565 		/* Sync the DMA map. */
566 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
567 		    BUS_DMASYNC_PREWRITE);
568 
569 		/*
570 		 * Initialize the transmit descriptors.
571 		 */
572 		for (nexttx = sc->sc_txnext, seg = 0;
573 		     seg < dmamap->dm_nsegs;
574 		     seg++, nexttx = TULIP_NEXTTX(nexttx)) {
575 			/*
576 			 * If this is the first descriptor we're
577 			 * enqueueing, don't set the OWN bit just
578 			 * yet.  That could cause a race condition.
579 			 * We'll do it below.
580 			 */
581 			sc->sc_txdescs[nexttx].td_status =
582 			    (nexttx == firsttx) ? 0 : TDSTAT_OWN;
583 			sc->sc_txdescs[nexttx].td_bufaddr1 =
584 			    dmamap->dm_segs[seg].ds_addr;
585 			sc->sc_txdescs[nexttx].td_ctl =
586 			    (dmamap->dm_segs[seg].ds_len << TDCTL_SIZE1_SHIFT) |
587 			    TDCTL_CH;
588 			lasttx = nexttx;
589 		}
590 
591 		/* Set `first segment' and `last segment' appropriately. */
592 		sc->sc_txdescs[sc->sc_txnext].td_ctl |= TDCTL_Tx_FS;
593 		sc->sc_txdescs[lasttx].td_ctl |= TDCTL_Tx_LS;
594 
595 #ifdef TLP_DEBUG
596 		if (ifp->if_flags & IFF_DEBUG) {
597 			printf("     txsoft %p trainsmit chain:\n", txs);
598 			for (seg = sc->sc_txnext;; seg = TULIP_NEXTTX(seg)) {
599 				printf("     descriptor %d:\n", seg);
600 				printf("       td_status:   0x%08x\n",
601 				    sc->sc_txdescs[seg].td_status);
602 				printf("       td_ctl:      0x%08x\n",
603 				    sc->sc_txdescs[seg].td_ctl);
604 				printf("       td_bufaddr1: 0x%08x\n",
605 				    sc->sc_txdescs[seg].td_bufaddr1);
606 				printf("       td_bufaddr2: 0x%08x\n",
607 				    sc->sc_txdescs[seg].td_bufaddr2);
608 				if (seg == lasttx)
609 					break;
610 			}
611 		}
612 #endif
613 
614 		/* Sync the descriptors we're using. */
615 		TULIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
616 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
617 
618 		/*
619 		 * Store a pointer to the packet so we can free it later,
620 		 * and remember what txdirty will be once the packet is
621 		 * done.
622 		 */
623 		txs->txs_mbuf = m0;
624 		txs->txs_firstdesc = sc->sc_txnext;
625 		txs->txs_lastdesc = lasttx;
626 
627 		/* Advance the tx pointer. */
628 		sc->sc_txfree -= dmamap->dm_nsegs;
629 		sc->sc_txnext = nexttx;
630 
631 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
632 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
633 
634 		last_txs = txs;
635 
636 #if NBPFILTER > 0
637 		/*
638 		 * Pass the packet to any BPF listeners.
639 		 */
640 		if (ifp->if_bpf)
641 			bpf_mtap(ifp->if_bpf, m0);
642 #endif /* NBPFILTER > 0 */
643 	}
644 
645 	if (txs == NULL || sc->sc_txfree == 0) {
646 		/* No more slots left; notify upper layer. */
647 		ifp->if_flags |= IFF_OACTIVE;
648 	}
649 
650 	if (sc->sc_txfree != ofree) {
651 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
652 		    sc->sc_dev.dv_xname, lasttx, firsttx));
653 		/*
654 		 * Cause a transmit interrupt to happen on the
655 		 * last packet we enqueued.
656 		 */
657 		sc->sc_txdescs[lasttx].td_ctl |= TDCTL_Tx_IC;
658 		TULIP_CDTXSYNC(sc, lasttx, 1,
659 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
660 
661 		/*
662 		 * Some clone chips want IC on the *first* segment in
663 		 * the packet.  Appease them.
664 		 */
665 		if ((sc->sc_flags & TULIPF_IC_FS) != 0 &&
666 		    last_txs->txs_firstdesc != lasttx) {
667 			sc->sc_txdescs[last_txs->txs_firstdesc].td_ctl |=
668 			    TDCTL_Tx_IC;
669 			TULIP_CDTXSYNC(sc, last_txs->txs_firstdesc, 1,
670 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
671 		}
672 
673 		/*
674 		 * The entire packet chain is set up.  Give the
675 		 * first descriptor to the chip now.
676 		 */
677 		sc->sc_txdescs[firsttx].td_status |= TDSTAT_OWN;
678 		TULIP_CDTXSYNC(sc, firsttx, 1,
679 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
680 
681 		/* Wake up the transmitter. */
682 		/* XXX USE AUTOPOLLING? */
683 		TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
684 
685 		/* Set a watchdog timer in case the chip flakes out. */
686 		ifp->if_timer = 5;
687 	}
688 }
689 
690 /*
691  * tlp_watchdog:	[ifnet interface function]
692  *
693  *	Watchdog timer handler.
694  */
695 void
696 tlp_watchdog(ifp)
697 	struct ifnet *ifp;
698 {
699 	struct tulip_softc *sc = ifp->if_softc;
700 	int doing_setup, doing_transmit;
701 
702 	doing_setup = (sc->sc_flags & TULIPF_DOING_SETUP);
703 	doing_transmit = (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL);
704 
705 	if (doing_setup && doing_transmit) {
706 		printf("%s: filter setup and transmit timeout\n",
707 		    sc->sc_dev.dv_xname);
708 		ifp->if_oerrors++;
709 	} else if (doing_transmit) {
710 		printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
711 		ifp->if_oerrors++;
712 	} else if (doing_setup)
713 		printf("%s: filter setup timeout\n", sc->sc_dev.dv_xname);
714 	else
715 		printf("%s: spurious watchdog timeout\n", sc->sc_dev.dv_xname);
716 
717 	(void) tlp_init(sc);
718 
719 	/* Try to get more packets going. */
720 	tlp_start(ifp);
721 }
722 
723 /*
724  * tlp_ioctl:		[ifnet interface function]
725  *
726  *	Handle control requests from the operator.
727  */
728 int
729 tlp_ioctl(ifp, cmd, data)
730 	struct ifnet *ifp;
731 	u_long cmd;
732 	caddr_t data;
733 {
734 	struct tulip_softc *sc = ifp->if_softc;
735 	struct ifreq *ifr = (struct ifreq *)data;
736 	struct ifaddr *ifa = (struct ifaddr *)data;
737 	int s, error = 0;
738 
739 	s = splnet();
740 
741 	switch (cmd) {
742 	case SIOCSIFADDR:
743 		ifp->if_flags |= IFF_UP;
744 
745 		switch (ifa->ifa_addr->sa_family) {
746 #ifdef INET
747 		case AF_INET:
748 			if ((error = tlp_init(sc)) != 0)
749 				break;
750 			arp_ifinit(ifp, ifa);
751 			break;
752 #endif /* INET */
753 #ifdef NS
754 		case AF_NS:
755 		    {
756 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
757 
758 			if (ns_nullhost(*ina))
759 				ina->x_host = *(union ns_host *)
760 				    LLADDR(ifp->if_sadl);
761 			else
762 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
763 				    ifp->if_addrlen);
764 			/* Set new address. */
765 			error = tlp_init(sc);
766 			break;
767 		    }
768 #endif /* NS */
769 		default:
770 			error = tlp_init(sc);
771 			break;
772 		}
773 		break;
774 
775 	case SIOCSIFMTU:
776 		if (ifr->ifr_mtu > ETHERMTU)
777 			error = EINVAL;
778 		else
779 			ifp->if_mtu = ifr->ifr_mtu;
780 		break;
781 
782 	case SIOCSIFFLAGS:
783 #ifdef TLP_STATS
784 		if (ifp->if_flags & IFF_DEBUG)
785 			tlp_print_stats(sc);
786 #endif
787 		if ((ifp->if_flags & IFF_UP) == 0 &&
788 		    (ifp->if_flags & IFF_RUNNING) != 0) {
789 			/*
790 			 * If interface is marked down and it is running, then
791 			 * stop it.
792 			 */
793 			tlp_stop(sc, 1);
794 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
795 			   (ifp->if_flags & IFF_RUNNING) == 0) {
796 			/*
797 			 * If interfase it marked up and it is stopped, then
798 			 * start it.
799 			 */
800 			error = tlp_init(sc);
801 		} else if ((ifp->if_flags & IFF_UP) != 0) {
802 			/*
803 			 * Reset the interface to pick up changes in any other
804 			 * flags that affect the hardware state.
805 			 */
806 			error = tlp_init(sc);
807 		}
808 		break;
809 
810 	case SIOCADDMULTI:
811 	case SIOCDELMULTI:
812 		error = (cmd == SIOCADDMULTI) ?
813 		    ether_addmulti(ifr, &sc->sc_ethercom) :
814 		    ether_delmulti(ifr, &sc->sc_ethercom);
815 
816 		if (error == ENETRESET) {
817 			/*
818 			 * Multicast list has changed.  Set the filter
819 			 * accordingly.
820 			 */
821 			(*sc->sc_filter_setup)(sc);
822 			error = 0;
823 		}
824 		break;
825 
826 	case SIOCSIFMEDIA:
827 	case SIOCGIFMEDIA:
828 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
829 		break;
830 
831 	default:
832 		error = EINVAL;
833 		break;
834 	}
835 
836 	/* Try to get more packets going. */
837 	tlp_start(ifp);
838 
839 	splx(s);
840 	return (error);
841 }
842 
843 /*
844  * tlp_intr:
845  *
846  *	Interrupt service routine.
847  */
848 int
849 tlp_intr(arg)
850 	void *arg;
851 {
852 	struct tulip_softc *sc = arg;
853 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
854 	u_int32_t status, rxstatus, txstatus;
855 	int handled = 0, txthresh;
856 
857 	DPRINTF(sc, ("%s: tlp_intr\n", sc->sc_dev.dv_xname));
858 
859 	/*
860 	 * If the interface isn't running, the interrupt couldn't
861 	 * possibly have come from us.
862 	 */
863 	if ((ifp->if_flags & IFF_RUNNING) == 0)
864 		return (0);
865 
866 	for (;;) {
867 		status = TULIP_READ(sc, CSR_STATUS);
868 		if (status)
869 			TULIP_WRITE(sc, CSR_STATUS, status);
870 
871 		if ((status & sc->sc_inten) == 0)
872 			break;
873 
874 		handled = 1;
875 
876 		rxstatus = status & sc->sc_rxint_mask;
877 		txstatus = status & sc->sc_txint_mask;
878 
879 		if (rxstatus) {
880 			/* Grab new any new packets. */
881 			tlp_rxintr(sc);
882 
883 			if (rxstatus & STATUS_RWT)
884 				printf("%s: receive watchdog timeout\n",
885 				    sc->sc_dev.dv_xname);
886 
887 			if (rxstatus & STATUS_RU) {
888 				printf("%s: receive ring overrun\n",
889 				    sc->sc_dev.dv_xname);
890 				/* Get the receive process going again. */
891 				tlp_idle(sc, OPMODE_SR);
892 				TULIP_WRITE(sc, CSR_RXLIST,
893 				    TULIP_CDRXADDR(sc, sc->sc_rxptr));
894 				TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
895 				TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
896 				break;
897 			}
898 		}
899 
900 		if (txstatus) {
901 			/* Sweep up transmit descriptors. */
902 			tlp_txintr(sc);
903 
904 			if (txstatus & STATUS_TJT)
905 				printf("%s: transmit jabber timeout\n",
906 				    sc->sc_dev.dv_xname);
907 
908 			if (txstatus & STATUS_UNF) {
909 				/*
910 				 * Increase our transmit threshold if
911 				 * another is available.
912 				 */
913 				txthresh = sc->sc_txthresh + 1;
914 				if (sc->sc_txth[txthresh].txth_name != NULL) {
915 					/* Idle the transmit process. */
916 					tlp_idle(sc, OPMODE_ST);
917 
918 					sc->sc_txthresh = txthresh;
919 					sc->sc_opmode &= ~(OPMODE_TR|OPMODE_SF);
920 					sc->sc_opmode |=
921 					    sc->sc_txth[txthresh].txth_opmode;
922 					printf("%s: transmit underrun; new "
923 					    "threshold: %s\n",
924 					    sc->sc_dev.dv_xname,
925 					    sc->sc_txth[txthresh].txth_name);
926 
927 					/*
928 					 * Set the new threshold and restart
929 					 * the transmit process.
930 					 */
931 					TULIP_WRITE(sc, CSR_OPMODE,
932 					    sc->sc_opmode);
933 				}
934 					/*
935 					 * XXX Log every Nth underrun from
936 					 * XXX now on?
937 					 */
938 			}
939 		}
940 
941 		if (status & (STATUS_TPS|STATUS_RPS)) {
942 			if (status & STATUS_TPS)
943 				printf("%s: transmit process stopped\n",
944 				    sc->sc_dev.dv_xname);
945 			if (status & STATUS_RPS)
946 				printf("%s: receive process stopped\n",
947 				    sc->sc_dev.dv_xname);
948 			(void) tlp_init(sc);
949 			break;
950 		}
951 
952 		if (status & STATUS_SE) {
953 			const char *str;
954 			switch (status & STATUS_EB) {
955 			case STATUS_EB_PARITY:
956 				str = "parity error";
957 				break;
958 
959 			case STATUS_EB_MABT:
960 				str = "master abort";
961 				break;
962 
963 			case STATUS_EB_TABT:
964 				str = "target abort";
965 				break;
966 
967 			default:
968 				str = "unknown error";
969 				break;
970 			}
971 			printf("%s: fatal system error: %s\n",
972 			    sc->sc_dev.dv_xname, str);
973 			(void) tlp_init(sc);
974 			break;
975 		}
976 
977 		/*
978 		 * Not handled:
979 		 *
980 		 *	Transmit buffer unavailable -- normal
981 		 *	condition, nothing to do, really.
982 		 *
983 		 *	General purpose timer experied -- we don't
984 		 *	use the general purpose timer.
985 		 *
986 		 *	Early receive interrupt -- not available on
987 		 *	all chips, we just use RI.  We also only
988 		 *	use single-segment receive DMA, so this
989 		 *	is mostly useless.
990 		 */
991 	}
992 
993 	/* Try to get more packets going. */
994 	tlp_start(ifp);
995 
996 	return (handled);
997 }
998 
999 /*
1000  * tlp_rxintr:
1001  *
1002  *	Helper; handle receive interrupts.
1003  */
1004 void
1005 tlp_rxintr(sc)
1006 	struct tulip_softc *sc;
1007 {
1008 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1009 	struct ether_header *eh;
1010 	struct tulip_rxsoft *rxs;
1011 	struct mbuf *m;
1012 	u_int32_t rxstat;
1013 	int i, len;
1014 
1015 	for (i = sc->sc_rxptr;; i = TULIP_NEXTRX(i)) {
1016 		rxs = &sc->sc_rxsoft[i];
1017 
1018 		TULIP_CDRXSYNC(sc, i,
1019 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1020 
1021 		rxstat = sc->sc_rxdescs[i].td_status;
1022 
1023 		if (rxstat & TDSTAT_OWN) {
1024 			/*
1025 			 * We have processed all of the receive buffers.
1026 			 */
1027 			break;
1028 		}
1029 
1030 		/*
1031 		 * Make sure the packet fit in one buffer.  This should
1032 		 * always be the case.  But the Lite-On PNIC, rev 33
1033 		 * has an awful receive engine bug, which may require
1034 		 * a very icky work-around.
1035 		 */
1036 		if ((rxstat & (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) !=
1037 		    (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) {
1038 			printf("%s: incoming packet spilled, resetting\n",
1039 			    sc->sc_dev.dv_xname);
1040 			(void) tlp_init(sc);
1041 			return;
1042 		}
1043 
1044 		/*
1045 		 * If any collisions were seen on the wire, count one.
1046 		 */
1047 		if (rxstat & TDSTAT_Rx_CS)
1048 			ifp->if_collisions++;
1049 
1050 		/*
1051 		 * If an error occured, update stats, clear the status
1052 		 * word, and leave the packet buffer in place.  It will
1053 		 * simply be reused the next time the ring comes around.
1054 		 */
1055 		if (rxstat & TDSTAT_ES) {
1056 #define	PRINTERR(bit, str)						\
1057 			if (rxstat & (bit))				\
1058 				printf("%s: receive error: %s\n",	\
1059 				    sc->sc_dev.dv_xname, str)
1060 			ifp->if_ierrors++;
1061 			PRINTERR(TDSTAT_Rx_DE, "descriptor error");
1062 			PRINTERR(TDSTAT_Rx_RF, "runt frame");
1063 			PRINTERR(TDSTAT_Rx_TL, "frame too long");
1064 			PRINTERR(TDSTAT_Rx_RE, "MII error");
1065 			PRINTERR(TDSTAT_Rx_DB, "dribbling bit");
1066 			PRINTERR(TDSTAT_Rx_CE, "CRC error");
1067 #undef PRINTERR
1068 			TULIP_INIT_RXDESC(sc, i);
1069 			continue;
1070 		}
1071 
1072 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1073 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1074 
1075 		/*
1076 		 * No errors; receive the packet.  Note the Tulip
1077 		 * includes the CRC with every packet; trim it.
1078 		 */
1079 		len = TDSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;
1080 
1081 #ifdef __NO_STRICT_ALIGNMENT
1082 		/*
1083 		 * Allocate a new mbuf cluster.  If that fails, we are
1084 		 * out of memory, and must drop the packet and recycle
1085 		 * the buffer that's already attached to this descriptor.
1086 		 */
1087 		m = rxs->rxs_mbuf;
1088 		if (tlp_add_rxbuf(sc, i) != 0) {
1089 			ifp->if_ierrors++;
1090 			TULIP_INIT_RXDESC(sc, i);
1091 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1092 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1093 			continue;
1094 		}
1095 #else
1096 		/*
1097 		 * The Tulip's receive buffers must be 4-byte aligned.
1098 		 * But this means that the data after the Ethernet header
1099 		 * is misaligned.  We must allocate a new buffer and
1100 		 * copy the data, shifted forward 2 bytes.
1101 		 */
1102 		MGETHDR(m, M_DONTWAIT, MT_DATA);
1103 		if (m == NULL) {
1104  dropit:
1105 			ifp->if_ierrors++;
1106 			TULIP_INIT_RXDESC(sc, i);
1107 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1108 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1109 			continue;
1110 		}
1111 		if (len > (MHLEN - 2)) {
1112 			MCLGET(m, M_DONTWAIT);
1113 			if ((m->m_flags & M_EXT) == 0) {
1114 				m_freem(m);
1115 				goto dropit;
1116 			}
1117 		}
1118 		m->m_data += 2;
1119 
1120 		/*
1121 		 * Note that we use clusters for incoming frames, so the
1122 		 * buffer is virtually contiguous.
1123 		 */
1124 		memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
1125 
1126 		/* Allow the receive descriptor to continue using its mbuf. */
1127 		TULIP_INIT_RXDESC(sc, i);
1128 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1129 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1130 #endif /* __NO_STRICT_ALIGNMENT */
1131 
1132 		ifp->if_ipackets++;
1133 		eh = mtod(m, struct ether_header *);
1134 		m->m_pkthdr.rcvif = ifp;
1135 		m->m_pkthdr.len = m->m_len = len;
1136 
1137 #if NBPFILTER > 0
1138 		/*
1139 		 * Pass this up to any BPF listeners, but only
1140 		 * pass it up the stack if its for us.
1141 		 */
1142 		if (ifp->if_bpf)
1143 			bpf_mtap(ifp->if_bpf, m);
1144 #endif /* NPBFILTER > 0 */
1145 
1146 		/*
1147 		 * This test is outside the NBPFILTER block because
1148 		 * on the 21140 we have to use Hash-Only mode due to
1149 		 * a bug in the filter logic.
1150 		 */
1151 		if ((ifp->if_flags & IFF_PROMISC) != 0 ||
1152 		    sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
1153 			if (memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
1154 				 ETHER_ADDR_LEN) != 0 &&
1155 			    ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
1156 				m_freem(m);
1157 				continue;
1158 			}
1159 		}
1160 
1161 		/* Pass it on. */
1162 		(*ifp->if_input)(ifp, m);
1163 	}
1164 
1165 	/* Update the recieve pointer. */
1166 	sc->sc_rxptr = i;
1167 }
1168 
1169 /*
1170  * tlp_txintr:
1171  *
1172  *	Helper; handle transmit interrupts.
1173  */
1174 void
1175 tlp_txintr(sc)
1176 	struct tulip_softc *sc;
1177 {
1178 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1179 	struct tulip_txsoft *txs;
1180 	u_int32_t txstat;
1181 
1182 	DPRINTF(sc, ("%s: tlp_txintr: sc_flags 0x%08x\n",
1183 	    sc->sc_dev.dv_xname, sc->sc_flags));
1184 
1185 	ifp->if_flags &= ~IFF_OACTIVE;
1186 
1187 	/*
1188 	 * If we were doing a filter setup, check to see if it completed.
1189 	 */
1190 	if (sc->sc_flags & TULIPF_DOING_SETUP) {
1191 		TULIP_CDSDSYNC(sc, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1192 		if ((sc->sc_setup_desc.td_status & TDSTAT_OWN) == 0)
1193 			sc->sc_flags &= ~TULIPF_DOING_SETUP;
1194 	}
1195 
1196 	/*
1197 	 * Go through our Tx list and free mbufs for those
1198 	 * frames that have been transmitted.
1199 	 */
1200 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1201 		TULIP_CDTXSYNC(sc, txs->txs_lastdesc,
1202 		    txs->txs_dmamap->dm_nsegs,
1203 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1204 
1205 #ifdef TLP_DEBUG
1206 		if (ifp->if_flags & IFF_DEBUG) {
1207 			int i;
1208 			printf("    txsoft %p trainsmit chain:\n", txs);
1209 			for (i = txs->txs_firstdesc;; i = TULIP_NEXTTX(i)) {
1210 				printf("     descriptor %d:\n", i);
1211 				printf("       td_status:   0x%08x\n",
1212 				    sc->sc_txdescs[i].td_status);
1213 				printf("       td_ctl:      0x%08x\n",
1214 				    sc->sc_txdescs[i].td_ctl);
1215 				printf("       td_bufaddr1: 0x%08x\n",
1216 				    sc->sc_txdescs[i].td_bufaddr1);
1217 				printf("       td_bufaddr2: 0x%08x\n",
1218 				    sc->sc_txdescs[i].td_bufaddr2);
1219 				if (i == txs->txs_lastdesc)
1220 					break;
1221 			}
1222 		}
1223 #endif
1224 
1225 		txstat = sc->sc_txdescs[txs->txs_lastdesc].td_status;
1226 		if (txstat & TDSTAT_OWN)
1227 			break;
1228 
1229 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1230 
1231 		sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
1232 
1233 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1234 		    0, txs->txs_dmamap->dm_mapsize,
1235 		    BUS_DMASYNC_POSTWRITE);
1236 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1237 		m_freem(txs->txs_mbuf);
1238 		txs->txs_mbuf = NULL;
1239 
1240 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1241 
1242 		/*
1243 		 * Check for errors and collisions.
1244 		 */
1245 #ifdef TLP_STATS
1246 		if (txstat & TDSTAT_Tx_UF)
1247 			sc->sc_stats.ts_tx_uf++;
1248 		if (txstat & TDSTAT_Tx_TO)
1249 			sc->sc_stats.ts_tx_to++;
1250 		if (txstat & TDSTAT_Tx_EC)
1251 			sc->sc_stats.ts_tx_ec++;
1252 		if (txstat & TDSTAT_Tx_LC)
1253 			sc->sc_stats.ts_tx_lc++;
1254 #endif
1255 
1256 		if (txstat & (TDSTAT_Tx_UF|TDSTAT_Tx_TO))
1257 			ifp->if_oerrors++;
1258 
1259 		if (txstat & TDSTAT_Tx_EC)
1260 			ifp->if_collisions += 16;
1261 		else
1262 			ifp->if_collisions += TDSTAT_Tx_COLLISIONS(txstat);
1263 		if (txstat & TDSTAT_Tx_LC)
1264 			ifp->if_collisions++;
1265 
1266 		ifp->if_opackets++;
1267 	}
1268 
1269 	/*
1270 	 * If there are no more pending transmissions, cancel the watchdog
1271 	 * timer.
1272 	 */
1273 	if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0)
1274 		ifp->if_timer = 0;
1275 
1276 	/*
1277 	 * If we have a receive filter setup pending, do it now.
1278 	 */
1279 	if (sc->sc_flags & TULIPF_WANT_SETUP)
1280 		(*sc->sc_filter_setup)(sc);
1281 }
1282 
1283 #ifdef TLP_STATS
1284 void
1285 tlp_print_stats(sc)
1286 	struct tulip_softc *sc;
1287 {
1288 
1289 	printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n",
1290 	    sc->sc_dev.dv_xname,
1291 	    sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to,
1292 	    sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc);
1293 }
1294 #endif
1295 
1296 /*
1297  * tlp_reset:
1298  *
1299  *	Perform a soft reset on the Tulip.
1300  */
1301 void
1302 tlp_reset(sc)
1303 	struct tulip_softc *sc;
1304 {
1305 	int i;
1306 
1307 	TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
1308 
1309 	for (i = 0; i < 1000; i++) {
1310 		/*
1311 		 * Wait at least 50 PCI cycles for the reset to
1312 		 * complete before peeking at the Tulip again.
1313 		 * 10 uSec is a bit longer than 50 PCI cycles
1314 		 * (at 33MHz), but it doesn't hurt have the extra
1315 		 * wait.
1316 		 */
1317 		delay(10);
1318 		if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
1319 			break;
1320 	}
1321 
1322 	if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
1323 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
1324 
1325 	delay(1000);
1326 
1327 	/*
1328 	 * If the board has any GPIO reset sequences to issue, do them now.
1329 	 */
1330 	if (sc->sc_reset != NULL)
1331 		(*sc->sc_reset)(sc);
1332 }
1333 
1334 /*
1335  * tlp_init:
1336  *
1337  *	Initialize the interface.  Must be called at splnet().
1338  */
1339 int
1340 tlp_init(sc)
1341 	struct tulip_softc *sc;
1342 {
1343 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1344 	struct tulip_txsoft *txs;
1345 	struct tulip_rxsoft *rxs;
1346 	int i, error = 0;
1347 
1348 	/*
1349 	 * Cancel any pending I/O.
1350 	 */
1351 	tlp_stop(sc, 0);
1352 
1353 	/*
1354 	 * Initialize `opmode' to 0, and call the pre-init routine, if
1355 	 * any.  This is required because the 2114x and some of the
1356 	 * clones require that the media-related bits in `opmode' be
1357 	 * set before performing a soft-reset in order to get internal
1358 	 * chip pathways are correct.  Yay!
1359 	 */
1360 	sc->sc_opmode = 0;
1361 	if (sc->sc_preinit != NULL)
1362 		(*sc->sc_preinit)(sc);
1363 
1364 	/*
1365 	 * Reset the Tulip to a known state.
1366 	 */
1367 	tlp_reset(sc);
1368 
1369 	/*
1370 	 * Initialize the BUSMODE register.
1371 	 *
1372 	 * XXX What about read-multiple/read-line/write-line on
1373 	 * XXX the 21140 and up?
1374 	 */
1375 	sc->sc_busmode = BUSMODE_BAR;
1376 	switch (sc->sc_cacheline) {
1377 	default:
1378 		/*
1379 		 * Note: We must *always* set these bits; a cache
1380 		 * alignment of 0 is RESERVED.
1381 		 */
1382 	case 8:
1383 		sc->sc_busmode |= BUSMODE_CAL_8LW;
1384 		break;
1385 	case 16:
1386 		sc->sc_busmode |= BUSMODE_CAL_16LW;
1387 		break;
1388 	case 32:
1389 		sc->sc_busmode |= BUSMODE_CAL_32LW;
1390 		break;
1391 	}
1392 	switch (sc->sc_chip) {
1393 	case TULIP_CHIP_82C168:
1394 	case TULIP_CHIP_82C169:
1395 		sc->sc_busmode |= BUSMODE_PBL_16LW | BUSMODE_PNIC_MBO;
1396 		break;
1397 	default:
1398 		sc->sc_busmode |= BUSMODE_PBL_DEFAULT;
1399 		break;
1400 	}
1401 #if BYTE_ORDER == BIG_ENDIAN
1402 	/*
1403 	 * XXX There are reports that this doesn't work properly
1404 	 * in the old Tulip driver, but BUSMODE_DBO does.  However,
1405 	 * BUSMODE_DBO is not available on the 21040, and requires
1406 	 * us to byte-swap the setup packet.  What to do?
1407 	 */
1408 	sc->sc_busmode |= BUSMODE_BLE;
1409 #endif
1410 	TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);
1411 
1412 	/*
1413 	 * Initialize the OPMODE register.  We don't write it until
1414 	 * we're ready to begin the transmit and receive processes.
1415 	 *
1416 	 * Media-related OPMODE bits are set in the media callbacks
1417 	 * for each specific chip/board.
1418 	 */
1419 	sc->sc_opmode |= OPMODE_SR | OPMODE_ST |
1420 	    sc->sc_txth[sc->sc_txthresh].txth_opmode;
1421 
1422 	/*
1423 	 * Magical mystery initialization on the Macronix chips.
1424 	 * The MX98713 uses its own magic value, the rest share
1425 	 * a common one.
1426 	 */
1427 	switch (sc->sc_chip) {
1428 	case TULIP_CHIP_MX98713:
1429 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
1430 		break;
1431 
1432 	case TULIP_CHIP_MX98713A:
1433 	case TULIP_CHIP_MX98715:
1434 	case TULIP_CHIP_MX98715A:
1435 	case TULIP_CHIP_MX98725:
1436 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
1437 		break;
1438 
1439 	default:
1440 		/* Nothing. */
1441 	}
1442 
1443 	/*
1444 	 * Initialize the transmit descriptor ring.
1445 	 */
1446 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1447 	for (i = 0; i < TULIP_NTXDESC; i++) {
1448 		sc->sc_txdescs[i].td_ctl = TDCTL_CH;
1449 		sc->sc_txdescs[i].td_bufaddr2 =
1450 		    TULIP_CDTXADDR(sc, TULIP_NEXTTX(i));
1451 	}
1452 	TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
1453 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1454 	sc->sc_txfree = TULIP_NTXDESC;
1455 	sc->sc_txnext = 0;
1456 
1457 	/*
1458 	 * Initialize the transmit job descriptors.
1459 	 */
1460 	SIMPLEQ_INIT(&sc->sc_txfreeq);
1461 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
1462 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
1463 		txs = &sc->sc_txsoft[i];
1464 		txs->txs_mbuf = NULL;
1465 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1466 	}
1467 
1468 	/*
1469 	 * Initialize the receive descriptor and receive job
1470 	 * descriptor rings.
1471 	 */
1472 	for (i = 0; i < TULIP_NRXDESC; i++) {
1473 		rxs = &sc->sc_rxsoft[i];
1474 		if (rxs->rxs_mbuf == NULL) {
1475 			if ((error = tlp_add_rxbuf(sc, i)) != 0) {
1476 				printf("%s: unable to allocate or map rx "
1477 				    "buffer %d, error = %d\n",
1478 				    sc->sc_dev.dv_xname, i, error);
1479 				/*
1480 				 * XXX Should attempt to run with fewer receive
1481 				 * XXX buffers instead of just failing.
1482 				 */
1483 				tlp_rxdrain(sc);
1484 				goto out;
1485 			}
1486 		}
1487 	}
1488 	sc->sc_rxptr = 0;
1489 
1490 	/*
1491 	 * Initialize the interrupt mask and enable interrupts.
1492 	 */
1493 	/* normal interrupts */
1494 	sc->sc_inten =  STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
1495 
1496 	/* abnormal interrupts */
1497 	sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
1498 	    STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;
1499 
1500 	sc->sc_rxint_mask = STATUS_RI|STATUS_RU|STATUS_RWT;
1501 	sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
1502 
1503 	switch (sc->sc_chip) {
1504 	case TULIP_CHIP_WB89C840F:
1505 		/*
1506 		 * Clear bits that we don't want that happen to
1507 		 * overlap or don't exist.
1508 		 */
1509 		sc->sc_inten &= ~(STATUS_WINB_REI|STATUS_RWT);
1510 		break;
1511 
1512 	default:
1513 		/* Nothing. */
1514 	}
1515 
1516 	sc->sc_rxint_mask &= sc->sc_inten;
1517 	sc->sc_txint_mask &= sc->sc_inten;
1518 
1519 	TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
1520 	TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);
1521 
1522 	/*
1523 	 * Give the transmit and receive rings to the Tulip.
1524 	 */
1525 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
1526 	TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));
1527 
1528 	/*
1529 	 * On chips that do this differently, set the station address.
1530 	 */
1531 	switch (sc->sc_chip) {
1532 	case TULIP_CHIP_WB89C840F:
1533 	    {
1534 		/* XXX Do this with stream writes? */
1535 		bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0);
1536 
1537 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
1538 			bus_space_write_1(sc->sc_st, sc->sc_sh,
1539 			    cpa + i, LLADDR(ifp->if_sadl)[i]);
1540 		}
1541 		break;
1542 	    }
1543 
1544 	case TULIP_CHIP_AL981:
1545 	    {
1546 		u_int32_t reg;
1547 		u_int8_t *enaddr = LLADDR(ifp->if_sadl);
1548 
1549 		reg = enaddr[0] |
1550 		      (enaddr[1] << 8) |
1551 		      (enaddr[2] << 16) |
1552 		      (enaddr[3] << 24);
1553 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR0, reg);
1554 
1555 		reg = enaddr[4] |
1556 		      (enaddr[5] << 8);
1557 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR1, reg);
1558 	    }
1559 
1560 	default:
1561 		/* Nothing. */
1562 	}
1563 
1564 	/*
1565 	 * Set the receive filter.  This will start the transmit and
1566 	 * receive processes.
1567 	 */
1568 	(*sc->sc_filter_setup)(sc);
1569 
1570 	/*
1571 	 * Set the current media.
1572 	 */
1573 	(void) (*sc->sc_mediasw->tmsw_set)(sc);
1574 
1575 	/*
1576 	 * Start the receive process.
1577 	 */
1578 	TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
1579 
1580 	if (sc->sc_tick != NULL) {
1581 		/* Start the one second clock. */
1582 		timeout(sc->sc_tick, sc, hz);
1583 	}
1584 
1585 	/*
1586 	 * Note that the interface is now running.
1587 	 */
1588 	ifp->if_flags |= IFF_RUNNING;
1589 	ifp->if_flags &= ~IFF_OACTIVE;
1590 
1591  out:
1592 	if (error)
1593 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1594 	return (error);
1595 }
1596 
1597 /*
1598  * tlp_rxdrain:
1599  *
1600  *	Drain the receive queue.
1601  */
1602 void
1603 tlp_rxdrain(sc)
1604 	struct tulip_softc *sc;
1605 {
1606 	struct tulip_rxsoft *rxs;
1607 	int i;
1608 
1609 	for (i = 0; i < TULIP_NRXDESC; i++) {
1610 		rxs = &sc->sc_rxsoft[i];
1611 		if (rxs->rxs_mbuf != NULL) {
1612 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1613 			m_freem(rxs->rxs_mbuf);
1614 			rxs->rxs_mbuf = NULL;
1615 		}
1616 	}
1617 }
1618 
1619 /*
1620  * tlp_stop:
1621  *
1622  *	Stop transmission on the interface.
1623  */
1624 void
1625 tlp_stop(sc, drain)
1626 	struct tulip_softc *sc;
1627 	int drain;
1628 {
1629 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1630 	struct tulip_txsoft *txs;
1631 
1632 	if (sc->sc_tick != NULL) {
1633 		/* Stop the one second clock. */
1634 		untimeout(sc->sc_tick, sc);
1635 	}
1636 
1637 	/* Disable interrupts. */
1638 	TULIP_WRITE(sc, CSR_INTEN, 0);
1639 
1640 	/* Stop the transmit and receive processes. */
1641 	TULIP_WRITE(sc, CSR_OPMODE, 0);
1642 	TULIP_WRITE(sc, CSR_RXLIST, 0);
1643 	TULIP_WRITE(sc, CSR_TXLIST, 0);
1644 
1645 	/*
1646 	 * Release any queued transmit buffers.
1647 	 */
1648 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1649 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1650 		if (txs->txs_mbuf != NULL) {
1651 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1652 			m_freem(txs->txs_mbuf);
1653 			txs->txs_mbuf = NULL;
1654 		}
1655 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1656 	}
1657 
1658 	if (drain) {
1659 		/*
1660 		 * Release the receive buffers.
1661 		 */
1662 		tlp_rxdrain(sc);
1663 	}
1664 
1665 	sc->sc_flags &= ~(TULIPF_WANT_SETUP|TULIPF_DOING_SETUP);
1666 
1667 	/*
1668 	 * Mark the interface down and cancel the watchdog timer.
1669 	 */
1670 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1671 	ifp->if_timer = 0;
1672 }
1673 
1674 #define	SROM_EMIT(sc, x)						\
1675 do {									\
1676 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
1677 	delay(1);							\
1678 } while (0)
1679 
1680 /*
1681  * tlp_srom_idle:
1682  *
1683  *	Put the SROM in idle state.
1684  */
1685 void
1686 tlp_srom_idle(sc)
1687 	struct tulip_softc *sc;
1688 {
1689 	u_int32_t miirom;
1690 	int i;
1691 
1692 	miirom = MIIROM_SR;
1693 	SROM_EMIT(sc, miirom);
1694 
1695 	miirom |= MIIROM_RD;
1696 	SROM_EMIT(sc, miirom);
1697 
1698 	miirom |= MIIROM_SROMCS;
1699 	SROM_EMIT(sc, miirom);
1700 
1701 	SROM_EMIT(sc, miirom|MIIROM_SROMSK);
1702 
1703 	/* Strobe the clock 25 times. */
1704 	for (i = 0; i < 25; i++) {
1705 		SROM_EMIT(sc, miirom);
1706 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
1707 	}
1708 
1709 	SROM_EMIT(sc, miirom);
1710 
1711 	miirom &= ~MIIROM_SROMCS;
1712 	SROM_EMIT(sc, miirom);
1713 
1714 	SROM_EMIT(sc, 0);
1715 }
1716 
1717 /*
1718  * tlp_read_srom:
1719  *
1720  *	Read the Tulip SROM.
1721  */
1722 void
1723 tlp_read_srom(sc, word, wordcnt, data)
1724 	struct tulip_softc *sc;
1725 	int word, wordcnt;
1726 	u_int8_t *data;
1727 {
1728 	u_int32_t miirom;
1729 	u_int16_t datain;
1730 	int i, x;
1731 
1732 	tlp_srom_idle(sc);
1733 
1734 	/* Select the SROM. */
1735 	miirom = MIIROM_SR;
1736 	SROM_EMIT(sc, miirom);
1737 
1738 	miirom |= MIIROM_RD;
1739 	SROM_EMIT(sc, miirom);
1740 
1741 	for (i = 0; i < wordcnt; i++) {
1742 		/* Send CHIP SELECT for one clock tick. */
1743 		miirom |= MIIROM_SROMCS;
1744 		SROM_EMIT(sc, miirom);
1745 
1746 		/* Shift in the READ opcode. */
1747 		for (x = 3; x > 0; x--) {
1748 			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
1749 				miirom |= MIIROM_SROMDI;
1750 			else
1751 				miirom &= ~MIIROM_SROMDI;
1752 			SROM_EMIT(sc, miirom);
1753 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
1754 			SROM_EMIT(sc, miirom);
1755 		}
1756 
1757 		/* Shift in address. */
1758 		for (x = 6; x > 0; x--) {
1759 			if ((word + i) & (1 << (x - 1)))
1760 				miirom |= MIIROM_SROMDI;
1761 			else
1762 				miirom &= ~MIIROM_SROMDI;
1763 			SROM_EMIT(sc, miirom);
1764 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
1765 			SROM_EMIT(sc, miirom);
1766 		}
1767 
1768 		/* Shift out data. */
1769 		miirom &= ~MIIROM_SROMDI;
1770 		datain = 0;
1771 		for (x = 16; x > 0; x--) {
1772 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
1773 			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
1774 				datain |= (1 << (x - 1));
1775 			SROM_EMIT(sc, miirom);
1776 		}
1777 		data[2 * i] = datain & 0xff;
1778 		data[(2 * i) + 1] = datain >> 8;
1779 
1780 		/* Clear CHIP SELECT. */
1781 		miirom &= ~MIIROM_SROMCS;
1782 		SROM_EMIT(sc, miirom);
1783 	}
1784 
1785 	/* Deselect the SROM. */
1786 	SROM_EMIT(sc, 0);
1787 
1788 	/* ...and idle it. */
1789 	tlp_srom_idle(sc);
1790 }
1791 
1792 #undef SROM_EMIT
1793 
1794 /*
1795  * tlp_add_rxbuf:
1796  *
1797  *	Add a receive buffer to the indicated descriptor.
1798  */
1799 int
1800 tlp_add_rxbuf(sc, idx)
1801 	struct tulip_softc *sc;
1802 	int idx;
1803 {
1804 	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
1805 	struct mbuf *m;
1806 	int error;
1807 
1808 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1809 	if (m == NULL)
1810 		return (ENOBUFS);
1811 
1812 	MCLGET(m, M_DONTWAIT);
1813 	if ((m->m_flags & M_EXT) == 0) {
1814 		m_freem(m);
1815 		return (ENOBUFS);
1816 	}
1817 
1818 	if (rxs->rxs_mbuf != NULL)
1819 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1820 
1821 	rxs->rxs_mbuf = m;
1822 
1823 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
1824 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
1825 	if (error) {
1826 		printf("%s: can't load rx DMA map %d, error = %d\n",
1827 		    sc->sc_dev.dv_xname, idx, error);
1828 		panic("tlp_add_rxbuf");	/* XXX */
1829 	}
1830 
1831 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1832 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1833 
1834 	TULIP_INIT_RXDESC(sc, idx);
1835 
1836 	return (0);
1837 }
1838 
1839 /*
1840  * tlp_crc32:
1841  *
1842  *	Compute the 32-bit CRC of the provided buffer.
1843  */
1844 u_int32_t
1845 tlp_crc32(buf, len)
1846 	const u_int8_t *buf;
1847 	size_t len;
1848 {
1849 	static const u_int32_t crctab[] = {
1850 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1851 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1852 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1853 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1854 	};
1855 	u_int32_t crc;
1856 	int i;
1857 
1858 	crc = 0xffffffff;
1859 	for (i = 0; i < len; i++) {
1860 		crc ^= buf[i];
1861 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1862 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1863 	}
1864 	return (crc);
1865 }
1866 
1867 /*
1868  * tlp_srom_crcok:
1869  *
1870  *	Check the CRC of the Tulip SROM.
1871  */
1872 int
1873 tlp_srom_crcok(romdata)
1874 	const u_int8_t *romdata;
1875 {
1876 	u_int32_t crc;
1877 
1878 	crc = tlp_crc32(romdata, TULIP_ROM_CRC32_CHECKSUM);
1879 	crc = (crc & 0xffff) ^ 0xffff;
1880 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
1881 		return (1);
1882 	return (0);
1883 }
1884 
1885 /*
1886  * tlp_isv_srom:
1887  *
1888  *	Check to see if the SROM is in the new standardized format.
1889  */
1890 int
1891 tlp_isv_srom(romdata)
1892 	const u_int8_t *romdata;
1893 {
1894 	int i;
1895 	u_int16_t cksum;
1896 
1897 	if (tlp_srom_crcok(romdata)) {
1898 		/*
1899 		 * SROM CRC checks out; must be in the new format.
1900 		 */
1901 		return (1);
1902 	}
1903 
1904 	cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM);
1905 	if (cksum == 0xffff || cksum == 0) {
1906 		/*
1907 		 * No checksum present.  Check the SROM ID; 18 bytes of 0
1908 		 * followed by 1 (version) followed by the number of
1909 		 * adapters which use this SROM (should be non-zero).
1910 		 */
1911 		for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) {
1912 			if (romdata[i] != 0)
1913 				return (0);
1914 		}
1915 		if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1)
1916 			return (0);
1917 		if (romdata[TULIP_ROM_CHIP_COUNT] == 0)
1918 			return (0);
1919 		return (1);
1920 	}
1921 
1922 	return (0);
1923 }
1924 
1925 /*
1926  * tlp_isv_srom_enaddr:
1927  *
1928  *	Get the Ethernet address from an ISV SROM.
1929  */
1930 int
1931 tlp_isv_srom_enaddr(sc, enaddr)
1932 	struct tulip_softc *sc;
1933 	u_int8_t *enaddr;
1934 {
1935 	int i, devcnt;
1936 
1937 	if (tlp_isv_srom(sc->sc_srom) == 0)
1938 		return (0);
1939 
1940 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
1941 	for (i = 0; i < devcnt; i++) {
1942 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
1943 			break;
1944 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
1945 		    sc->sc_devno)
1946 			break;
1947 	}
1948 
1949 	if (i == devcnt)
1950 		return (0);
1951 
1952 	memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS],
1953 	    ETHER_ADDR_LEN);
1954 	enaddr[5] += i;
1955 
1956 	return (1);
1957 }
1958 
1959 /*
1960  * tlp_parse_old_srom:
1961  *
1962  *	Parse old-format SROMs.
1963  *
1964  *	This routine is largely lifted from Matt Thomas's `de' driver.
1965  */
1966 int
1967 tlp_parse_old_srom(sc, enaddr)
1968 	struct tulip_softc *sc;
1969 	u_int8_t *enaddr;
1970 {
1971 	static const u_int8_t testpat[] =
1972 	    { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
1973 	int i;
1974 	u_int32_t cksum;
1975 
1976 	if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
1977 		/*
1978 		 * Some vendors (e.g. ZNYX) don't use the standard
1979 		 * DEC Address ROM format, but rather just have an
1980 		 * Ethernet address in the first 6 bytes, maybe a
1981 		 * 2 byte checksum, and then all 0xff's.
1982 		 */
1983 		for (i = 8; i < 32; i++) {
1984 			if (sc->sc_srom[i] != 0xff)
1985 				return (0);
1986 		}
1987 
1988 		/*
1989 		 * Sanity check the Ethernet address:
1990 		 *
1991 		 *	- Make sure it's not multicast or locally
1992 		 *	  assigned
1993 		 *	- Make sure it has a non-0 OUI
1994 		 */
1995 		if (sc->sc_srom[0] & 3)
1996 			return (0);
1997 		if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
1998 		    sc->sc_srom[2] == 0)
1999 			return (0);
2000 
2001 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
2002 		return (1);
2003 	}
2004 
2005 	/*
2006 	 * Standard DEC Address ROM test.
2007 	 */
2008 
2009 	if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
2010 		return (0);
2011 
2012 	for (i = 0; i < 8; i++) {
2013 		if (sc->sc_srom[i] != sc->sc_srom[15 - i])
2014 			return (0);
2015 	}
2016 
2017 	memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
2018 
2019 	cksum = *(u_int16_t *) &enaddr[0];
2020 
2021 	cksum <<= 1;
2022 	if (cksum > 0xffff)
2023 		cksum -= 0xffff;
2024 
2025 	cksum += *(u_int16_t *) &enaddr[2];
2026 	if (cksum > 0xffff)
2027 		cksum -= 0xffff;
2028 
2029 	cksum <<= 1;
2030 	if (cksum > 0xffff)
2031 		cksum -= 0xffff;
2032 
2033 	cksum += *(u_int16_t *) &enaddr[4];
2034 	if (cksum >= 0xffff)
2035 		cksum -= 0xffff;
2036 
2037 	if (cksum != *(u_int16_t *) &sc->sc_srom[6])
2038 		return (0);
2039 
2040 	return (1);
2041 }
2042 
2043 /*
2044  * tlp_filter_setup:
2045  *
2046  *	Set the Tulip's receive filter.
2047  */
2048 void
2049 tlp_filter_setup(sc)
2050 	struct tulip_softc *sc;
2051 {
2052 	struct ethercom *ec = &sc->sc_ethercom;
2053 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2054 	struct ether_multi *enm;
2055 	struct ether_multistep step;
2056 	__volatile u_int32_t *sp;
2057 	u_int8_t enaddr[ETHER_ADDR_LEN];
2058 	u_int32_t hash, hashsize;
2059 	int cnt;
2060 
2061 	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
2062 	    sc->sc_dev.dv_xname, sc->sc_flags));
2063 
2064 	memcpy(enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
2065 
2066 	/*
2067 	 * If there are transmissions pending, wait until they have
2068 	 * completed.
2069 	 */
2070 	if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL ||
2071 	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
2072 		sc->sc_flags |= TULIPF_WANT_SETUP;
2073 		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
2074 		    sc->sc_dev.dv_xname));
2075 		return;
2076 	}
2077 	sc->sc_flags &= ~TULIPF_WANT_SETUP;
2078 
2079 	switch (sc->sc_chip) {
2080 	case TULIP_CHIP_82C115:
2081 		hashsize = TULIP_PNICII_HASHSIZE;
2082 		break;
2083 
2084 	default:
2085 		hashsize = TULIP_MCHASHSIZE;
2086 	}
2087 
2088 	/*
2089 	 * If we're running, idle the transmit and receive engines.  If
2090 	 * we're NOT running, we're being called from tlp_init(), and our
2091 	 * writing OPMODE will start the transmit and receive processes
2092 	 * in motion.
2093 	 */
2094 	if (ifp->if_flags & IFF_RUNNING) {
2095 		/*
2096 		 * Actually, some chips seem to need a really hard
2097 		 * kick in the head for this to work.  The genuine
2098 		 * DEC chips can just be idled, but some of the
2099 		 * clones seem to REALLY want a reset here.  Doing
2100 		 * the reset will end up here again, but with
2101 		 * IFF_RUNNING cleared.
2102 		 */
2103 		switch (sc->sc_chip) {
2104 		case TULIP_CHIP_82C168:
2105 		case TULIP_CHIP_82C169:
2106 			tlp_init(sc);
2107 			return;
2108 
2109 		default:
2110 			tlp_idle(sc, OPMODE_ST|OPMODE_SR);
2111 		}
2112 	}
2113 
2114 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
2115 
2116 	if (ifp->if_flags & IFF_PROMISC) {
2117 		sc->sc_opmode |= OPMODE_PR;
2118 		goto allmulti;
2119 	}
2120 
2121 	/*
2122 	 * Try Perfect filtering first.
2123 	 */
2124 
2125 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
2126 	sp = TULIP_CDSP(sc);
2127 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
2128 	cnt = 0;
2129 	ETHER_FIRST_MULTI(step, ec, enm);
2130 	while (enm != NULL) {
2131 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2132 			/*
2133 			 * We must listen to a range of multicast addresses.
2134 			 * For now, just accept all multicasts, rather than
2135 			 * trying to set only those filter bits needed to match
2136 			 * the range.  (At this time, the only use of address
2137 			 * ranges is for IP multicast routing, for which the
2138 			 * range is big enough to require all bits set.)
2139 			 */
2140 			goto allmulti;
2141 		}
2142 		if (cnt == (TULIP_MAXADDRS - 2)) {
2143 			/*
2144 			 * We already have our multicast limit (still need
2145 			 * our station address and broadcast).  Go to
2146 			 * Hash-Perfect mode.
2147 			 */
2148 			goto hashperfect;
2149 		}
2150 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[0];
2151 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[1];
2152 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[2];
2153 		ETHER_NEXT_MULTI(step, enm);
2154 	}
2155 
2156 	if (ifp->if_flags & IFF_BROADCAST) {
2157 		/* ...and the broadcast address. */
2158 		cnt++;
2159 		*sp++ = 0xffff;
2160 		*sp++ = 0xffff;
2161 		*sp++ = 0xffff;
2162 	}
2163 
2164 	/* Pad the rest with our station address. */
2165 	for (; cnt < TULIP_MAXADDRS; cnt++) {
2166 		*sp++ = ((u_int16_t *) enaddr)[0];
2167 		*sp++ = ((u_int16_t *) enaddr)[1];
2168 		*sp++ = ((u_int16_t *) enaddr)[2];
2169 	}
2170 	ifp->if_flags &= ~IFF_ALLMULTI;
2171 	goto setit;
2172 
2173  hashperfect:
2174 	/*
2175 	 * Try Hash-Perfect mode.
2176 	 */
2177 
2178 	/*
2179 	 * Some 21140 chips have broken Hash-Perfect modes.  On these
2180 	 * chips, we simply use Hash-Only mode, and put our station
2181 	 * address into the filter.
2182 	 */
2183 	if (sc->sc_chip == TULIP_CHIP_21140)
2184 		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
2185 	else
2186 		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
2187 	sp = TULIP_CDSP(sc);
2188 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
2189 	ETHER_FIRST_MULTI(step, ec, enm);
2190 	while (enm != NULL) {
2191 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2192 			/*
2193 			 * We must listen to a range of multicast addresses.
2194 			 * For now, just accept all multicasts, rather than
2195 			 * trying to set only those filter bits needed to match
2196 			 * the range.  (At this time, the only use of address
2197 			 * ranges is for IP multicast routing, for which the
2198 			 * range is big enough to require all bits set.)
2199 			 */
2200 			goto allmulti;
2201 		}
2202 		hash = tlp_mchash(enm->enm_addrlo, hashsize);
2203 		sp[hash >> 4] |= 1 << (hash & 0xf);
2204 		ETHER_NEXT_MULTI(step, enm);
2205 	}
2206 
2207 	if (ifp->if_flags & IFF_BROADCAST) {
2208 		/* ...and the broadcast address. */
2209 		hash = tlp_mchash(etherbroadcastaddr, hashsize);
2210 		sp[hash >> 4] |= 1 << (hash & 0xf);
2211 	}
2212 
2213 	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
2214 		/* ...and our station address. */
2215 		hash = tlp_mchash(enaddr, hashsize);
2216 		sp[hash >> 4] |= 1 << (hash & 0xf);
2217 	} else {
2218 		/*
2219 		 * Hash-Perfect mode; put our station address after
2220 		 * the hash table.
2221 		 */
2222 		sp[39] = ((u_int16_t *) enaddr)[0];
2223 		sp[40] = ((u_int16_t *) enaddr)[1];
2224 		sp[41] = ((u_int16_t *) enaddr)[2];
2225 	}
2226 	ifp->if_flags &= ~IFF_ALLMULTI;
2227 	goto setit;
2228 
2229  allmulti:
2230 	/*
2231 	 * Use Perfect filter mode.  First address is the broadcast address,
2232 	 * and pad the rest with our station address.  We'll set Pass-all-
2233 	 * multicast in OPMODE below.
2234 	 */
2235 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
2236 	sp = TULIP_CDSP(sc);
2237 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
2238 	cnt = 0;
2239 	if (ifp->if_flags & IFF_BROADCAST) {
2240 		cnt++;
2241 		*sp++ = 0xffff;
2242 		*sp++ = 0xffff;
2243 		*sp++ = 0xffff;
2244 	}
2245 	for (; cnt < TULIP_MAXADDRS; cnt++) {
2246 		*sp++ = ((u_int16_t *) enaddr)[0];
2247 		*sp++ = ((u_int16_t *) enaddr)[1];
2248 		*sp++ = ((u_int16_t *) enaddr)[2];
2249 	}
2250 	ifp->if_flags |= IFF_ALLMULTI;
2251 
2252  setit:
2253 	if (ifp->if_flags & IFF_ALLMULTI)
2254 		sc->sc_opmode |= OPMODE_PM;
2255 
2256 	/* Sync the setup packet buffer. */
2257 	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
2258 
2259 	/*
2260 	 * Fill in the setup packet descriptor.
2261 	 */
2262 	sc->sc_setup_desc.td_bufaddr1 = TULIP_CDSPADDR(sc);
2263 	sc->sc_setup_desc.td_bufaddr2 = TULIP_CDTXADDR(sc, sc->sc_txnext);
2264 	sc->sc_setup_desc.td_ctl =
2265 	    (TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
2266 	    sc->sc_filtmode | TDCTL_Tx_SET | TDCTL_Tx_FS | TDCTL_Tx_LS |
2267 	    TDCTL_Tx_IC | TDCTL_CH;
2268 	sc->sc_setup_desc.td_status = TDSTAT_OWN;
2269 	TULIP_CDSDSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2270 
2271 	/*
2272 	 * Write the address of the setup descriptor.  This also has
2273 	 * the side effect of giving the transmit ring to the chip,
2274 	 * since the setup descriptor points to the next available
2275 	 * descriptor in the ring.
2276 	 */
2277 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDSDADDR(sc));
2278 
2279 	/*
2280 	 * Set the OPMODE register.  This will also resume the
2281 	 * transmit transmit process we idled above.
2282 	 */
2283 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2284 
2285 	sc->sc_flags |= TULIPF_DOING_SETUP;
2286 
2287 	/*
2288 	 * Kick the transmitter; this will cause the Tulip to
2289 	 * read the setup descriptor.
2290 	 */
2291 	/* XXX USE AUTOPOLLING? */
2292 	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
2293 
2294 	/* Set up a watchdog timer in case the chip flakes out. */
2295 	ifp->if_timer = 5;
2296 
2297 	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
2298 }
2299 
2300 /*
2301  * tlp_winb_filter_setup:
2302  *
2303  *	Set the Winbond 89C840F's receive filter.
2304  */
2305 void
2306 tlp_winb_filter_setup(sc)
2307 	struct tulip_softc *sc;
2308 {
2309 	struct ethercom *ec = &sc->sc_ethercom;
2310 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2311 	struct ether_multi *enm;
2312 	struct ether_multistep step;
2313 	u_int32_t hash, mchash[2];
2314 
2315 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
2316 	    sc->sc_dev.dv_xname, sc->sc_flags));
2317 
2318 	sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
2319 
2320 	if (ifp->if_flags & IFF_MULTICAST)
2321 		sc->sc_opmode |= OPMODE_WINB_AMP;
2322 
2323 	if (ifp->if_flags & IFF_BROADCAST)
2324 		sc->sc_opmode |= OPMODE_WINB_ABP;
2325 
2326 	if (ifp->if_flags & IFF_PROMISC) {
2327 		sc->sc_opmode |= OPMODE_WINB_APP;
2328 		goto allmulti;
2329 	}
2330 
2331 	mchash[0] = mchash[1] = 0;
2332 
2333 	ETHER_FIRST_MULTI(step, ec, enm);
2334 	while (enm != NULL) {
2335 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2336 			/*
2337 			 * We must listen to a range of multicast addresses.
2338 			 * For now, just accept all multicasts, rather than
2339 			 * trying to set only those filter bits needed to match
2340 			 * the range.  (At this time, the only use of address
2341 			 * ranges is for IP multicast routing, for which the
2342 			 * range is big enough to require all bits set.)
2343 			 */
2344 			goto allmulti;
2345 		}
2346 
2347 		/*
2348 		 * According to the FreeBSD `wb' driver, yes, you
2349 		 * really do invert the hash.
2350 		 */
2351 		hash = (~(tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
2352 		    & 0x3f;
2353 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
2354 		ETHER_NEXT_MULTI(step, enm);
2355 	}
2356 	ifp->if_flags &= ~IFF_ALLMULTI;
2357 	goto setit;
2358 
2359  allmulti:
2360 	ifp->if_flags |= IFF_ALLMULTI;
2361 	mchash[0] = mchash[1] = 0xffffffff;
2362 
2363  setit:
2364 	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
2365 	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
2366 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2367 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
2368 	    sc->sc_dev.dv_xname));
2369 }
2370 
2371 /*
2372  * tlp_al981_filter_setup:
2373  *
2374  *	Set the ADMtek AL981's receive filter.
2375  */
2376 void
2377 tlp_al981_filter_setup(sc)
2378 	struct tulip_softc *sc;
2379 {
2380 	struct ethercom *ec = &sc->sc_ethercom;
2381 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2382 	struct ether_multi *enm;
2383 	struct ether_multistep step;
2384 	u_int32_t hash, mchash[2];
2385 
2386 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n",
2387 	    sc->sc_dev.dv_xname, sc->sc_flags));
2388 
2389 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
2390 
2391 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
2392 
2393 	if (ifp->if_flags & IFF_PROMISC) {
2394 		sc->sc_opmode |= OPMODE_PR;
2395 		goto allmulti;
2396 	}
2397 
2398 	mchash[0] = mchash[1] = 0;
2399 
2400 	ETHER_FIRST_MULTI(step, ec, enm);
2401 	while (enm != NULL) {
2402 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2403 			/*
2404 			 * We must listen to a range of multicast addresses.
2405 			 * For now, just accept all multicasts, rather than
2406 			 * trying to set only those filter bits needed to match
2407 			 * the range.  (At this time, the only use of address
2408 			 * ranges is for IP multicast routing, for which the
2409 			 * range is big enough to require all bits set.)
2410 			 */
2411 			goto allmulti;
2412 		}
2413 
2414 		hash = (tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26)
2415 		    & 0x3f;
2416 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
2417 		ETHER_NEXT_MULTI(step, enm);
2418 	}
2419 	ifp->if_flags &= ~IFF_ALLMULTI;
2420 	goto setit;
2421 
2422  allmulti:
2423 	ifp->if_flags |= IFF_ALLMULTI;
2424 	mchash[0] = mchash[1] = 0xffffffff;
2425 
2426  setit:
2427 	TULIP_WRITE(sc, CSR_ADM_MAR0, mchash[0]);
2428 	TULIP_WRITE(sc, CSR_ADM_MAR1, mchash[1]);
2429 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2430 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n",
2431 	    sc->sc_dev.dv_xname));
2432 }
2433 
2434 /*
2435  * tlp_idle:
2436  *
2437  *	Cause the transmit and/or receive processes to go idle.
2438  */
2439 void
2440 tlp_idle(sc, bits)
2441 	struct tulip_softc *sc;
2442 	u_int32_t bits;
2443 {
2444 	static const char *tx_state_names[] = {
2445 		"STOPPED",
2446 		"RUNNING - FETCH",
2447 		"RUNNING - WAIT",
2448 		"RUNNING - READING",
2449 		"-- RESERVED --",
2450 		"RUNNING - SETUP",
2451 		"SUSPENDED",
2452 		"RUNNING - CLOSE",
2453 	};
2454 	static const char *rx_state_names[] = {
2455 		"STOPPED",
2456 		"RUNNING - FETCH",
2457 		"RUNNING - CHECK",
2458 		"RUNNING - WAIT",
2459 		"SUSPENDED",
2460 		"RUNNING - CLOSE",
2461 		"RUNNING - FLUSH",
2462 		"RUNNING - QUEUE",
2463 	};
2464 	u_int32_t csr, ackmask = 0;
2465 	int i;
2466 
2467 	if (bits & OPMODE_ST)
2468 		ackmask |= STATUS_TPS;
2469 
2470 	if (bits & OPMODE_SR)
2471 		ackmask |= STATUS_RPS;
2472 
2473 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
2474 
2475 	for (i = 0; i < 1000; i++) {
2476 		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
2477 			break;
2478 		delay(10);
2479 	}
2480 
2481 	csr = TULIP_READ(sc, CSR_STATUS);
2482 	if ((csr & ackmask) != ackmask) {
2483 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
2484 		    (csr & STATUS_TS) != STATUS_TS_STOPPED)
2485 			printf("%s: transmit process failed to idle: "
2486 			    "state %s\n", sc->sc_dev.dv_xname,
2487 			    tx_state_names[(csr & STATUS_TS) >> 20]);
2488 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
2489 		    (csr & STATUS_RS) != STATUS_RS_STOPPED)
2490 			printf("%s: receive process failed to idle: "
2491 			    "state %s\n", sc->sc_dev.dv_xname,
2492 			    rx_state_names[(csr & STATUS_RS) >> 17]);
2493 	}
2494 	TULIP_WRITE(sc, CSR_STATUS, ackmask);
2495 }
2496 
2497 /*****************************************************************************
2498  * Generic media support functions.
2499  *****************************************************************************/
2500 
2501 /*
2502  * tlp_mediastatus:	[ifmedia interface function]
2503  *
2504  *	Query the current media.
2505  */
2506 void
2507 tlp_mediastatus(ifp, ifmr)
2508 	struct ifnet *ifp;
2509 	struct ifmediareq *ifmr;
2510 {
2511 	struct tulip_softc *sc = ifp->if_softc;
2512 
2513 	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
2514 }
2515 
2516 /*
2517  * tlp_mediachange:	[ifmedia interface function]
2518  *
2519  *	Update the current media.
2520  */
2521 int
2522 tlp_mediachange(ifp)
2523 	struct ifnet *ifp;
2524 {
2525 	struct tulip_softc *sc = ifp->if_softc;
2526 
2527 	return ((*sc->sc_mediasw->tmsw_set)(sc));
2528 }
2529 
2530 /*****************************************************************************
2531  * Support functions for MII-attached media.
2532  *****************************************************************************/
2533 
2534 /*
2535  * tlp_mii_tick:
2536  *
2537  *	One second timer, used to tick the MII.
2538  */
2539 void
2540 tlp_mii_tick(arg)
2541 	void *arg;
2542 {
2543 	struct tulip_softc *sc = arg;
2544 	int s;
2545 
2546 	s = splnet();
2547 	mii_tick(&sc->sc_mii);
2548 	splx(s);
2549 
2550 	timeout(sc->sc_tick, sc, hz);
2551 }
2552 
2553 /*
2554  * tlp_mii_statchg:	[mii interface function]
2555  *
2556  *	Callback from PHY when media changes.
2557  */
2558 void
2559 tlp_mii_statchg(self)
2560 	struct device *self;
2561 {
2562 	struct tulip_softc *sc = (struct tulip_softc *)self;
2563 
2564 	/* Idle the transmit and receive processes. */
2565 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
2566 
2567 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_HBD);
2568 
2569 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
2570 		sc->sc_opmode |= OPMODE_TTM;
2571 	else
2572 		sc->sc_opmode |= OPMODE_HBD;
2573 
2574 	if (sc->sc_mii.mii_media_active & IFM_FDX)
2575 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
2576 
2577 	/*
2578 	 * Write new OPMODE bits.  This also restarts the transmit
2579 	 * and receive processes.
2580 	 */
2581 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2582 
2583 	/* XXX Update ifp->if_baudrate */
2584 }
2585 
2586 /*
2587  * tlp_winb_mii_statchg: [mii interface function]
2588  *
2589  *	Callback from PHY when media changes.  This version is
2590  *	for the Winbond 89C840F, which has different OPMODE bits.
2591  */
2592 void
2593 tlp_winb_mii_statchg(self)
2594 	struct device *self;
2595 {
2596 	struct tulip_softc *sc = (struct tulip_softc *)self;
2597 
2598 	/* Idle the transmit and receive processes. */
2599 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
2600 
2601 	sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD);
2602 
2603 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
2604 		sc->sc_opmode |= OPMODE_WINB_FES;
2605 
2606 	if (sc->sc_mii.mii_media_active & IFM_FDX)
2607 		sc->sc_opmode |= OPMODE_FD;
2608 
2609 	/*
2610 	 * Write new OPMODE bits.  This also restarts the transmit
2611 	 * and receive processes.
2612 	 */
2613 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2614 
2615 	/* XXX Update ifp->if_baudrate */
2616 }
2617 
2618 /*
2619  * tlp_mii_getmedia:
2620  *
2621  *	Callback from ifmedia to request current media status.
2622  */
2623 void
2624 tlp_mii_getmedia(sc, ifmr)
2625 	struct tulip_softc *sc;
2626 	struct ifmediareq *ifmr;
2627 {
2628 
2629 	mii_pollstat(&sc->sc_mii);
2630 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
2631 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
2632 }
2633 
2634 /*
2635  * tlp_mii_setmedia:
2636  *
2637  *	Callback from ifmedia to request new media setting.
2638  */
2639 int
2640 tlp_mii_setmedia(sc)
2641 	struct tulip_softc *sc;
2642 {
2643 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2644 
2645 	if (ifp->if_flags & IFF_UP)
2646 		mii_mediachg(&sc->sc_mii);
2647 	return (0);
2648 }
2649 
2650 #define	MII_EMIT(sc, x)							\
2651 do {									\
2652 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
2653 	delay(1);							\
2654 } while (0)
2655 
2656 /*
2657  * tlp_sio_mii_sync:
2658  *
2659  *	Synchronize the SIO-attached MII.
2660  */
2661 void
2662 tlp_sio_mii_sync(sc)
2663 	struct tulip_softc *sc;
2664 {
2665 	u_int32_t miirom;
2666 	int i;
2667 
2668 	miirom = MIIROM_MDO;
2669 
2670 	MII_EMIT(sc, miirom);
2671 	for (i = 0; i < 32; i++) {
2672 		MII_EMIT(sc, miirom | MIIROM_MDC);
2673 		MII_EMIT(sc, miirom);
2674 	}
2675 }
2676 
2677 /*
2678  * tlp_sio_mii_sendbits:
2679  *
2680  *	Send a series of bits out the SIO to the MII.
2681  */
2682 void
2683 tlp_sio_mii_sendbits(sc, data, nbits)
2684 	struct tulip_softc *sc;
2685 	u_int32_t data;
2686 	int nbits;
2687 {
2688 	u_int32_t miirom, i;
2689 
2690 	miirom = 0;
2691 	MII_EMIT(sc, miirom);
2692 
2693 	for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
2694 		if (data & i)
2695 			miirom |= MIIROM_MDO;
2696 		else
2697 			miirom &= ~MIIROM_MDO;
2698 		MII_EMIT(sc, miirom);
2699 		MII_EMIT(sc, miirom|MIIROM_MDC);
2700 		MII_EMIT(sc, miirom);
2701 	}
2702 }
2703 
2704 /*
2705  * tlp_sio_mii_readreg:
2706  *
2707  *	Read a PHY register via SIO-attached MII.
2708  */
2709 int
2710 tlp_sio_mii_readreg(self, phy, reg)
2711 	struct device *self;
2712 	int phy, reg;
2713 {
2714 	struct tulip_softc *sc = (void *) self;
2715 	int val = 0, err = 0, i;
2716 
2717 	tlp_sio_mii_sync(sc);
2718 
2719 	tlp_sio_mii_sendbits(sc, MII_COMMAND_START, 2);
2720 	tlp_sio_mii_sendbits(sc, MII_COMMAND_READ, 2);
2721 	tlp_sio_mii_sendbits(sc, phy, 5);
2722 	tlp_sio_mii_sendbits(sc, reg, 5);
2723 
2724 	/* Switch direction to PHY->host, without a clock transition. */
2725 	MII_EMIT(sc, MIIROM_MIIDIR);
2726 
2727 	MII_EMIT(sc, MIIROM_MIIDIR|MIIROM_MDC);
2728 	MII_EMIT(sc, MIIROM_MIIDIR);
2729 
2730 	err = TULIP_ISSET(sc, CSR_MIIROM, MIIROM_MDI);
2731 
2732 	MII_EMIT(sc, MIIROM_MIIDIR|MIIROM_MDC);
2733 	MII_EMIT(sc, MIIROM_MIIDIR);
2734 
2735 	for (i = 0; i < 16; i++) {
2736 		val <<= 1;
2737 		/* Read data prior to clock low-high transition. */
2738 		if (err == 0 && TULIP_ISSET(sc, CSR_MIIROM, MIIROM_MDI))
2739 			val |= 1;
2740 
2741 		MII_EMIT(sc, MIIROM_MIIDIR|MIIROM_MDC);
2742 		MII_EMIT(sc, MIIROM_MIIDIR);
2743 	}
2744 
2745 	/* Set direction to host->PHY, without a clock transition. */
2746 	MII_EMIT(sc, 0);
2747 
2748 	return (err ? 0 : val);
2749 }
2750 
2751 /*
2752  * tlp_sio_mii_writereg:
2753  *
2754  *	Write a PHY register via SIO-attached MII.
2755  */
2756 void
2757 tlp_sio_mii_writereg(self, phy, reg, val)
2758 	struct device *self;
2759 	int phy, reg, val;
2760 {
2761 	struct tulip_softc *sc = (void *) self;
2762 
2763 	tlp_sio_mii_sync(sc);
2764 
2765 	tlp_sio_mii_sendbits(sc, MII_COMMAND_START, 2);
2766 	tlp_sio_mii_sendbits(sc, MII_COMMAND_WRITE, 2);
2767 	tlp_sio_mii_sendbits(sc, phy, 5);
2768 	tlp_sio_mii_sendbits(sc, reg, 5);
2769 	tlp_sio_mii_sendbits(sc, MII_COMMAND_ACK, 2);
2770 	tlp_sio_mii_sendbits(sc, val, 16);
2771 
2772 	MII_EMIT(sc, 0);
2773 }
2774 
2775 #undef MII_EMIT
2776 
2777 /*
2778  * tlp_pnic_mii_readreg:
2779  *
2780  *	Read a PHY register on the Lite-On PNIC.
2781  */
2782 int
2783 tlp_pnic_mii_readreg(self, phy, reg)
2784 	struct device *self;
2785 	int phy, reg;
2786 {
2787 	struct tulip_softc *sc = (void *) self;
2788 	u_int32_t val;
2789 	int i;
2790 
2791 	TULIP_WRITE(sc, CSR_PNIC_MII,
2792 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
2793 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
2794 	    (reg << PNIC_MII_REGSHIFT));
2795 
2796 	for (i = 0; i < 1000; i++) {
2797 		delay(10);
2798 		val = TULIP_READ(sc, CSR_PNIC_MII);
2799 		if ((val & PNIC_MII_BUSY) == 0) {
2800 			if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
2801 				return (0);
2802 			else
2803 				return (val & PNIC_MII_DATA);
2804 		}
2805 	}
2806 	printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
2807 	return (0);
2808 }
2809 
2810 /*
2811  * tlp_pnic_mii_writereg:
2812  *
2813  *	Write a PHY register on the Lite-On PNIC.
2814  */
2815 void
2816 tlp_pnic_mii_writereg(self, phy, reg, val)
2817 	struct device *self;
2818 	int phy, reg, val;
2819 {
2820 	struct tulip_softc *sc = (void *) self;
2821 	int i;
2822 
2823 	TULIP_WRITE(sc, CSR_PNIC_MII,
2824 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
2825 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
2826 	    (reg << PNIC_MII_REGSHIFT) | val);
2827 
2828 	for (i = 0; i < 1000; i++) {
2829 		delay(10);
2830 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
2831 			return;
2832 	}
2833 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
2834 }
2835 
2836 bus_addr_t tlp_al981_phy_regmap[] = {
2837 	CSR_ADM_BMCR,
2838 	CSR_ADM_BMSR,
2839 	CSR_ADM_PHYIDR1,
2840 	CSR_ADM_PHYIDR2,
2841 	CSR_ADM_ANAR,
2842 	CSR_ADM_ANLPAR,
2843 	CSR_ADM_ANER,
2844 
2845 	CSR_ADM_XMC,
2846 	CSR_ADM_XCIIS,
2847 	CSR_ADM_XIE,
2848 	CSR_ADM_100CTR,
2849 };
2850 const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
2851     sizeof(tlp_al981_phy_regmap[0]);
2852 
2853 /*
2854  * tlp_al981_mii_readreg:
2855  *
2856  *	Read a PHY register on the ADMtek AL981.
2857  */
2858 int
2859 tlp_al981_mii_readreg(self, phy, reg)
2860 	struct device *self;
2861 	int phy, reg;
2862 {
2863 	struct tulip_softc *sc = (struct tulip_softc *)self;
2864 
2865 	/* AL981 only has an internal PHY. */
2866 	if (phy != 0)
2867 		return (0);
2868 
2869 	if (reg >= tlp_al981_phy_regmap_size)
2870 		return (0);
2871 
2872 	return (bus_space_read_4(sc->sc_st, sc->sc_sh,
2873 	    tlp_al981_phy_regmap[reg]) & 0xffff);
2874 }
2875 
2876 /*
2877  * tlp_al981_mii_writereg:
2878  *
2879  *	Write a PHY register on the ADMtek AL981.
2880  */
2881 void
2882 tlp_al981_mii_writereg(self, phy, reg, val)
2883 	struct device *self;
2884 	int phy, reg, val;
2885 {
2886 	struct tulip_softc *sc = (struct tulip_softc *)self;
2887 
2888 	/* AL981 only has an internal PHY. */
2889 	if (phy != 0)
2890 		return;
2891 
2892 	if (reg >= tlp_al981_phy_regmap_size)
2893 		return;
2894 
2895 	bus_space_write_4(sc->sc_st, sc->sc_sh,
2896 	    tlp_al981_phy_regmap[reg], val);
2897 }
2898 
2899 /*****************************************************************************
2900  * Chip-specific pre-init and reset functions.
2901  *****************************************************************************/
2902 
2903 /*
2904  * tlp_2114x_preinit:
2905  *
2906  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
2907  */
2908 void
2909 tlp_2114x_preinit(sc)
2910 	struct tulip_softc *sc;
2911 {
2912 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
2913 	struct tulip_2114x_media *tm = ife->ifm_aux;
2914 
2915 	/*
2916 	 * Always set the Must-Be-One bit.
2917 	 */
2918 	sc->sc_opmode |= OPMODE_MBO;
2919 
2920 	/*
2921 	 * If `tm' is NULL, we must be doing pure MII-over-SIO.
2922 	 */
2923 	if (tm == NULL ||
2924 	    (tm->tm_type == TULIP_ROM_MB_21140_MII ||
2925 	     tm->tm_type == TULIP_ROM_MB_21142_MII)) {
2926 		/*
2927 		 * MII case: just set the port-select bit; we will never
2928 		 * be called during a media change.
2929 		 */
2930 		sc->sc_opmode |= OPMODE_PS;
2931 		goto set_opmode;
2932 	}
2933 
2934 	/*
2935 	 * ENDEC/PCS mode; set according to selected media type.
2936 	 * XXX Auto-sense not supported yet.
2937 	 */
2938 	sc->sc_opmode |= tm->tm_opmode;
2939 
2940  set_opmode:
2941 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2942 }
2943 
2944 /*
2945  * tlp_pnic_preinit:
2946  *
2947  *	Pre-init function for the Lite-On 82c168 and 82c169.
2948  */
2949 void
2950 tlp_pnic_preinit(sc)
2951 	struct tulip_softc *sc;
2952 {
2953 
2954 	if (sc->sc_flags & TULIPF_HAS_MII) {
2955 		/*
2956 		 * MII case: just set the port-select bit; we will never
2957 		 * be called during a media change.
2958 		 */
2959 		sc->sc_opmode |= OPMODE_PS;
2960 	} else {
2961 		/*
2962 		 * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
2963 		 */
2964 		sc->sc_opmode |= OPMODE_PNIC_TBEN;
2965 	}
2966 }
2967 
2968 /*
2969  * tlp_21140_reset:
2970  *
2971  *	Issue a reset sequence on the 21140 via the GPIO facility.
2972  */
2973 void
2974 tlp_21140_reset(sc)
2975 	struct tulip_softc *sc;
2976 {
2977 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
2978 	struct tulip_2114x_media *tm = ife->ifm_aux;
2979 	int i;
2980 
2981 	/* First, set the direction on the GPIO pins. */
2982 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
2983 
2984 	/* Now, issue the reset sequence. */
2985 	for (i = 0; i < tm->tm_reset_length; i++) {
2986 		delay(10);
2987 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
2988 	}
2989 
2990 	/* Now, issue the selection sequence. */
2991 	for (i = 0; i < tm->tm_gp_length; i++) {
2992 		delay(10);
2993 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
2994 	}
2995 
2996 	/* If there were no sequences, just lower the pins. */
2997 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0)
2998 		TULIP_WRITE(sc, CSR_GPP, 0);
2999 }
3000 
3001 /*
3002  * tlp_pmac_reset:
3003  *
3004  *	Reset routine for Macronix chips.
3005  */
3006 void
3007 tlp_pmac_reset(sc)
3008 	struct tulip_softc *sc;
3009 {
3010 
3011 	switch (sc->sc_chip) {
3012 	case TULIP_CHIP_82C115:
3013 	case TULIP_CHIP_MX98715:
3014 	case TULIP_CHIP_MX98715A:
3015 	case TULIP_CHIP_MX98725:
3016 		/*
3017 		 * Set the LED operating mode.  This information is located
3018 		 * in the EEPROM at byte offset 0x77, per the MX98715A and
3019 		 * MX98725 application notes.
3020 		 */
3021 		TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
3022 		break;
3023 
3024 	default:
3025 		/* Nothing. */
3026 	}
3027 }
3028 
3029 /*****************************************************************************
3030  * Chip/board-specific media switches.  The ones here are ones that
3031  * are potentially common to multiple front-ends.
3032  *****************************************************************************/
3033 
3034 const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
3035 	{ TULIP_ROM_MB_MEDIA_TP,	IFM_10_T,	0,
3036 	  "10baseT",			SIACONN_21041_10BASET,
3037 	  SIATXRX_21041_10BASET,	SIAGEN_21041_10BASET },
3038 
3039 	{ TULIP_ROM_MB_MEDIA_BNC,	IFM_10_2,	0,
3040 	  "10base2",			SIACONN_21041_BNC,
3041 	  SIATXRX_21041_BNC,		SIAGEN_21041_BNC },
3042 
3043 	{ TULIP_ROM_MB_MEDIA_AUI,	IFM_10_5,	0,
3044 	  "10base5",			SIACONN_21041_AUI,
3045 	  SIATXRX_21041_AUI,		SIAGEN_21041_AUI },
3046 
3047 	{ TULIP_ROM_MB_MEDIA_100TX,	IFM_100_TX,	0,
3048 	  "100baseTX",			0,
3049 	  0,				0 },
3050 
3051 	{ TULIP_ROM_MB_MEDIA_TP_FDX,	IFM_10_T,	IFM_FDX,
3052 	  "10baseT-FDX",		SIACONN_21041_10BASET_FDX,
3053 	  SIATXRX_21041_10BASET_FDX,	SIAGEN_21041_10BASET_FDX },
3054 
3055 	{ TULIP_ROM_MB_MEDIA_100TX_FDX,	IFM_100_TX,	IFM_FDX,
3056 	  "100baseTX-FDX",		0,
3057 	  0,				0 },
3058 
3059 	{ TULIP_ROM_MB_MEDIA_100T4,	IFM_100_T4,	0,
3060 	  "100baseT4",			0,
3061 	  0,				0 },
3062 
3063 	{ TULIP_ROM_MB_MEDIA_100FX,	IFM_100_FX,	0,
3064 	  "100baseFX",			0,
3065 	  0,				0 },
3066 
3067 	{ TULIP_ROM_MB_MEDIA_100FX_FDX,	IFM_100_FX,	IFM_FDX,
3068 	  "100baseFX-FDX",		0,
3069 	  0,				0 },
3070 
3071 	{ 0,				0,		0,
3072 	  NULL,				0,
3073 	  0,				0 },
3074 };
3075 
3076 const struct tulip_srom_to_ifmedia *tulip_srom_to_ifmedia __P((u_int8_t));
3077 
3078 const struct tulip_srom_to_ifmedia *
3079 tulip_srom_to_ifmedia(sm)
3080 	u_int8_t sm;
3081 {
3082 	const struct tulip_srom_to_ifmedia *tsti;
3083 
3084 	for (tsti = tulip_srom_to_ifmedia_table;
3085 	     tsti->tsti_name != NULL; tsti++) {
3086 		if (tsti->tsti_srom == sm)
3087 			return (tsti);
3088 	}
3089 
3090 	return (NULL);
3091 }
3092 
3093 /*
3094  * 21040 and 21041 media switches.
3095  */
3096 void	tlp_21040_tmsw_init __P((struct tulip_softc *));
3097 void	tlp_21040_tp_tmsw_init __P((struct tulip_softc *));
3098 void	tlp_21040_auibnc_tmsw_init __P((struct tulip_softc *));
3099 void	tlp_21041_tmsw_init __P((struct tulip_softc *));
3100 void	tlp_21040_21041_tmsw_get __P((struct tulip_softc *,
3101 	    struct ifmediareq *));
3102 int	tlp_21040_21041_tmsw_set __P((struct tulip_softc *));
3103 
3104 const struct tulip_mediasw tlp_21040_mediasw = {
3105 	tlp_21040_tmsw_init, tlp_21040_21041_tmsw_get, tlp_21040_21041_tmsw_set
3106 };
3107 
3108 const struct tulip_mediasw tlp_21040_tp_mediasw = {
3109 	tlp_21040_tp_tmsw_init, tlp_21040_21041_tmsw_get,
3110 	    tlp_21040_21041_tmsw_set
3111 };
3112 
3113 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
3114 	tlp_21040_auibnc_tmsw_init, tlp_21040_21041_tmsw_get,
3115 	    tlp_21040_21041_tmsw_set
3116 };
3117 
3118 const struct tulip_mediasw tlp_21041_mediasw = {
3119 	tlp_21041_tmsw_init, tlp_21040_21041_tmsw_get, tlp_21040_21041_tmsw_set
3120 };
3121 
3122 #define	ADD(m, t)	ifmedia_add(&sc->sc_mii.mii_media, (m), 0, (t))
3123 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
3124 
3125 void
3126 tlp_21040_tmsw_init(sc)
3127 	struct tulip_softc *sc;
3128 {
3129 	struct tulip_21040_21041_sia_media *tsm;
3130 	const char *sep = "";
3131 
3132 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
3133 	    tlp_mediastatus);
3134 
3135 	printf("%s: ", sc->sc_dev.dv_xname);
3136 
3137 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
3138 	    M_WAITOK);
3139 	tsm->tsm_siaconn = SIACONN_21040_10BASET;
3140 	tsm->tsm_siatxrx = SIATXRX_21040_10BASET;
3141 	tsm->tsm_siagen  = SIAGEN_21040_10BASET;
3142 	ADD(IFM_ETHER|IFM_10_T, tsm);
3143 	PRINT("10baseT");
3144 
3145 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
3146 	    M_WAITOK);
3147 	tsm->tsm_siaconn = SIACONN_21040_10BASET_FDX;
3148 	tsm->tsm_siatxrx = SIATXRX_21040_10BASET_FDX;
3149 	tsm->tsm_siagen  = SIAGEN_21040_10BASET_FDX;
3150 	ADD(IFM_ETHER|IFM_10_T|IFM_FDX, tsm);
3151 	PRINT("10baseT-FDX");
3152 
3153 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
3154 	    M_WAITOK);
3155 	tsm->tsm_siaconn = SIACONN_21040_AUI;
3156 	tsm->tsm_siatxrx = SIATXRX_21040_AUI;
3157 	tsm->tsm_siagen  = SIAGEN_21040_AUI;
3158 	ADD(IFM_ETHER|IFM_10_5, tsm);
3159 	PRINT("10base5");
3160 
3161 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
3162 	    M_WAITOK);
3163 	tsm->tsm_siaconn = SIACONN_21040_EXTSIA;
3164 	tsm->tsm_siatxrx = SIATXRX_21040_EXTSIA;
3165 	tsm->tsm_siagen  = SIAGEN_21040_EXTSIA;
3166 	ADD(IFM_ETHER|IFM_MANUAL, tsm);
3167 	PRINT("manual");
3168 
3169 	/*
3170 	 * XXX Autosense not yet supported.
3171 	 */
3172 
3173 	/* XXX This should be auto-sense. */
3174 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
3175 	printf(", default 10baseT");
3176 
3177 	printf("\n");
3178 }
3179 
3180 void
3181 tlp_21040_tp_tmsw_init(sc)
3182 	struct tulip_softc *sc;
3183 {
3184 	struct tulip_21040_21041_sia_media *tsm;
3185 	const char *sep = "";
3186 
3187 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
3188 	    tlp_mediastatus);
3189 
3190 	printf("%s: ", sc->sc_dev.dv_xname);
3191 
3192 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
3193 	    M_WAITOK);
3194 	tsm->tsm_siaconn = SIACONN_21040_10BASET;
3195 	tsm->tsm_siatxrx = SIATXRX_21040_10BASET;
3196 	tsm->tsm_siagen  = SIAGEN_21040_10BASET;
3197 	ADD(IFM_ETHER|IFM_10_T, tsm);
3198 	PRINT("10baseT");
3199 
3200 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
3201 	    M_WAITOK);
3202 	tsm->tsm_siaconn = SIACONN_21040_10BASET_FDX;
3203 	tsm->tsm_siatxrx = SIATXRX_21040_10BASET_FDX;
3204 	tsm->tsm_siagen  = SIAGEN_21040_10BASET_FDX;
3205 	ADD(IFM_ETHER|IFM_10_T|IFM_FDX, tsm);
3206 	PRINT("10baseT-FDX");
3207 
3208 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
3209 	printf(", default 10baseT");
3210 
3211 	printf("\n");
3212 }
3213 
3214 void
3215 tlp_21040_auibnc_tmsw_init(sc)
3216 	struct tulip_softc *sc;
3217 {
3218 	struct tulip_21040_21041_sia_media *tsm;
3219 	const char *sep = "";
3220 
3221 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
3222 	    tlp_mediastatus);
3223 
3224 	printf("%s: ", sc->sc_dev.dv_xname);
3225 
3226 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
3227 	    M_WAITOK);
3228 	tsm->tsm_siaconn = SIACONN_21040_AUI;
3229 	tsm->tsm_siatxrx = SIATXRX_21040_AUI;
3230 	tsm->tsm_siagen  = SIAGEN_21040_AUI;
3231 	ADD(IFM_ETHER|IFM_10_5, tsm);
3232 	PRINT("10base5");
3233 
3234 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
3235 
3236 	printf("\n");
3237 }
3238 
3239 void
3240 tlp_21041_tmsw_init(sc)
3241 	struct tulip_softc *sc;
3242 {
3243 	int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
3244 	const struct tulip_srom_to_ifmedia *tsti;
3245 	struct tulip_21040_21041_sia_media *tsm;
3246 	const char *sep = "", *defstr;
3247 	u_int16_t romdef;
3248 	u_int8_t mb;
3249 
3250 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
3251 	    tlp_mediastatus);
3252 
3253 	printf("%s: ", sc->sc_dev.dv_xname);
3254 
3255 	if (tlp_isv_srom(sc->sc_srom) == 0)
3256 		goto not_isv_srom;
3257 
3258 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
3259 	for (i = 0; i < devcnt; i++) {
3260 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
3261 			break;
3262 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
3263 		    sc->sc_devno)
3264 			break;
3265 	}
3266 
3267 	if (i == devcnt)
3268 		goto not_isv_srom;
3269 
3270 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
3271 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
3272 	mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
3273 	m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
3274 
3275 	for (; m_cnt != 0;
3276 	     m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
3277 		mb = sc->sc_srom[mb_offset];
3278 		tsm = malloc(sizeof(struct tulip_21040_21041_sia_media),
3279 		    M_DEVBUF, M_WAITOK);
3280 		switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
3281 		case TULIP_ROM_MB_MEDIA_TP:
3282 		case TULIP_ROM_MB_MEDIA_BNC:
3283 		case TULIP_ROM_MB_MEDIA_AUI:
3284 		case TULIP_ROM_MB_MEDIA_TP_FDX:
3285 			tsti = tulip_srom_to_ifmedia(mb &
3286 			    TULIP_ROM_MB_MEDIA_CODE);
3287 
3288 			tsm->tsm_siaconn = (mb & TULIP_ROM_MB_EXT) ?
3289 			    TULIP_ROM_GETW(sc->sc_srom,
3290 			      mb_offset + TULIP_ROM_MB_CSR13) :
3291 			    tsti->tsti_21041_siaconn;
3292 			tsm->tsm_siatxrx = (mb & TULIP_ROM_MB_EXT) ?
3293 			    TULIP_ROM_GETW(sc->sc_srom,
3294 			      mb_offset + TULIP_ROM_MB_CSR14) :
3295 			    tsti->tsti_21041_siatxrx;
3296 			tsm->tsm_siagen  = (mb & TULIP_ROM_MB_EXT) ?
3297 			    TULIP_ROM_GETW(sc->sc_srom,
3298 			      mb_offset + TULIP_ROM_MB_CSR15) :
3299 			    tsti->tsti_21041_siagen;
3300 
3301 			ifmedia_add(&sc->sc_mii.mii_media,
3302 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
3303 			    tsti->tsti_options, 0), 0, tsm);
3304 			PRINT(tsti->tsti_name);
3305 			break;
3306 
3307 		default:
3308 			printf("%s<unknown 0x%02x>", sep,
3309 			    (mb & TULIP_ROM_MB_MEDIA_CODE));
3310 			sep = ", ";
3311 			free(tsm, M_DEVBUF);
3312 		}
3313 	}
3314 
3315 	/*
3316 	 * XXX Autosense not yet supported.
3317 	 */
3318 
3319 	romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
3320 	    TULIP_ROM_IL_SELECT_CONN_TYPE);
3321 	switch (romdef) {
3322 	case SELECT_CONN_TYPE_TP:
3323 	case SELECT_CONN_TYPE_TP_AUTONEG:
3324 	case SELECT_CONN_TYPE_TP_NOLINKPASS:
3325 		defmedia = IFM_ETHER|IFM_10_T;
3326 		defstr = "10baseT";
3327 		break;
3328 
3329 	case SELECT_CONN_TYPE_TP_FDX:
3330 		defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
3331 		defstr = "10baseT-FDX";
3332 		break;
3333 
3334 	case SELECT_CONN_TYPE_BNC:
3335 		defmedia = IFM_ETHER|IFM_10_2;
3336 		defstr = "10base2";
3337 		break;
3338 
3339 	case SELECT_CONN_TYPE_AUI:
3340 		defmedia = IFM_ETHER|IFM_10_5;
3341 		defstr = "10base5";
3342 		break;
3343 #if 0 /* XXX */
3344 	case SELECT_CONN_TYPE_ASENSE:
3345 	case SELECT_CONN_TYPE_ASENSE_AUTONEG:
3346 		defmedia = IFM_ETHER|IFM_AUTO;
3347 		defstr = "auto";
3348 		break;
3349 #endif
3350 	default:
3351 		defmedia = 0;
3352 		defstr = NULL;
3353 	}
3354 
3355 	if (defmedia != 0)
3356 		printf(", default %s\n", defstr);
3357 	else {
3358 		/*
3359 		 * XXX We should default to auto-sense.
3360 		 */
3361 		defmedia = IFM_ETHER|IFM_10_T;
3362 		defstr = "10baseT";
3363 
3364 		printf("\n%s: unknown default media in SROM (0x%04x), "
3365 		    "using %s\n", sc->sc_dev.dv_xname, romdef, defstr);
3366 	}
3367 
3368 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
3369 	return;
3370 
3371  not_isv_srom:
3372 	/*
3373 	 * If we have a board without the standard 21041 SROM format,
3374 	 * we just assume all media are present and try and pick a
3375 	 * reasonable default.
3376 	 */
3377 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
3378 	    M_WAITOK);
3379 	tsm->tsm_siaconn = SIACONN_21041_10BASET;
3380 	tsm->tsm_siatxrx = SIATXRX_21041_10BASET;
3381 	tsm->tsm_siagen  = SIAGEN_21041_10BASET;
3382 	ADD(IFM_ETHER|IFM_10_T, tsm);
3383 	PRINT("10baseT");
3384 
3385 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
3386 	    M_WAITOK);
3387 	tsm->tsm_siaconn = SIACONN_21041_10BASET_FDX;
3388 	tsm->tsm_siatxrx = SIATXRX_21041_10BASET_FDX;
3389 	tsm->tsm_siagen  = SIAGEN_21041_10BASET_FDX;
3390 	ADD(IFM_ETHER|IFM_10_T|IFM_FDX, tsm);
3391 	PRINT("10baseT-FDX");
3392 
3393 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
3394 	    M_WAITOK);
3395 	tsm->tsm_siaconn = SIACONN_21041_BNC;
3396 	tsm->tsm_siatxrx = SIATXRX_21041_BNC;
3397 	tsm->tsm_siagen  = SIAGEN_21041_BNC;
3398 	ADD(IFM_ETHER|IFM_10_2|IFM_FDX, tsm);
3399 	PRINT("10base2");
3400 
3401 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
3402 	    M_WAITOK);
3403 	tsm->tsm_siaconn = SIACONN_21041_AUI;
3404 	tsm->tsm_siatxrx = SIATXRX_21041_AUI;
3405 	tsm->tsm_siagen  = SIAGEN_21041_AUI;
3406 	ADD(IFM_ETHER|IFM_10_5|IFM_FDX, tsm);
3407 	PRINT("10base5");
3408 
3409 	/*
3410 	 * XXX Autosense not yet supported.
3411 	 */
3412 
3413 	/* XXX This should be auto-sense. */
3414 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
3415 	printf(", default 10baseT");
3416 
3417 	printf("\n");
3418 }
3419 
3420 #undef ADD
3421 #undef PRINT
3422 
3423 void
3424 tlp_21040_21041_tmsw_get(sc, ifmr)
3425 	struct tulip_softc *sc;
3426 	struct ifmediareq *ifmr;
3427 {
3428 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3429 
3430 	ifmr->ifm_status = 0;
3431 
3432 	switch (IFM_SUBTYPE(ife->ifm_media)) {
3433 	case IFM_AUTO:
3434 		/*
3435 		 * XXX Implement autosensing case.
3436 		 */
3437 		break;
3438 
3439 	case IFM_10_T:
3440 		/*
3441 		 * We're able to detect link directly on twisted pair.
3442 		 */
3443 		ifmr->ifm_status = IFM_AVALID;
3444 		if (TULIP_ISSET(sc, CSR_SIASTAT, SIASTAT_LKF) == 0)
3445 			ifmr->ifm_status |= IFM_ACTIVE;
3446 		/* FALLTHROUGH */
3447 	default:
3448 		/*
3449 		 * If not autosensing, active media is the currently
3450 		 * selected media.
3451 		 */
3452 		ifmr->ifm_active = ife->ifm_media;
3453 	}
3454 }
3455 
3456 int
3457 tlp_21040_21041_tmsw_set(sc)
3458 	struct tulip_softc *sc;
3459 {
3460 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3461 	struct tulip_21040_21041_sia_media *tsm;
3462 
3463 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
3464 		/*
3465 		 * If not autosensing, just pull the SIA settings out
3466 		 * of the media entry.
3467 		 */
3468 		tsm = ife->ifm_aux;
3469 		TULIP_WRITE(sc, CSR_SIACONN, SIACONN_SRL);
3470 		TULIP_WRITE(sc, CSR_SIATXRX, tsm->tsm_siatxrx);
3471 		TULIP_WRITE(sc, CSR_SIAGEN,  tsm->tsm_siagen);
3472 		TULIP_WRITE(sc, CSR_SIACONN, tsm->tsm_siaconn);
3473 
3474 		tlp_idle(sc, OPMODE_ST|OPMODE_SR);
3475 		sc->sc_opmode &= ~OPMODE_FD;
3476 		if (ife->ifm_media & IFM_FDX)
3477 			sc->sc_opmode |= OPMODE_FD;
3478 		TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3479 	} else {
3480 		/*
3481 		 * XXX Implement autosensing case.
3482 		 */
3483 	}
3484 
3485 	return (0);
3486 }
3487 
3488 /*
3489  * DECchip 2114x ISV media switch.
3490  * XXX Currently only handles 21140[A] GPR and MII.
3491  */
3492 void	tlp_2114x_isv_tmsw_init __P((struct tulip_softc *));
3493 void	tlp_2114x_isv_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
3494 int	tlp_2114x_isv_tmsw_set __P((struct tulip_softc *));
3495 
3496 const struct tulip_mediasw tlp_2114x_isv_mediasw = {
3497 	tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
3498 };
3499 
3500 void	tlp_21140_gpr_getmedia __P((struct tulip_softc *sc,
3501 	    struct ifmediareq *ifmr));
3502 int	tlp_21140_gpr_setmedia __P((struct tulip_softc *sc));
3503 
3504 void
3505 tlp_2114x_isv_tmsw_init(sc)
3506 	struct tulip_softc *sc;
3507 {
3508 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
3509 	struct ifmedia_entry *ife;
3510 	struct mii_softc *phy;
3511 	struct tulip_2114x_media *tm;
3512 	const struct tulip_srom_to_ifmedia *tsti;
3513 	int i, devcnt, leaf_offset, m_cnt, type, length, seen, defmedia, minst;
3514 	u_int16_t word;
3515 	u_int8_t *cp, *ncp;
3516 
3517 	seen = defmedia = 0;
3518 
3519 	sc->sc_mii.mii_ifp = ifp;
3520 	sc->sc_mii.mii_readreg = tlp_sio_mii_readreg;
3521 	sc->sc_mii.mii_writereg = tlp_sio_mii_writereg;
3522 	sc->sc_mii.mii_statchg = sc->sc_statchg;
3523 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
3524 	    tlp_mediastatus);
3525 
3526 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
3527 	for (i = 0; i < devcnt; i++) {
3528 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
3529 			break;
3530 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
3531 		    sc->sc_devno)
3532 			break;
3533 	}
3534 
3535 	if (i == devcnt) {
3536 		printf("%s: unable to locate info leaf in SROM\n",
3537 		    sc->sc_dev.dv_xname);
3538 		return;
3539 	}
3540 
3541 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
3542 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
3543 
3544 	/* XXX SELECT CONN TYPE */
3545 
3546 	cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
3547 
3548 	/*
3549 	 * On some chips, the first thing in the Info Leaf is the
3550 	 * GPIO pin direction data.
3551 	 */
3552 	switch (sc->sc_chip) {
3553 	case TULIP_CHIP_21140:
3554 	case TULIP_CHIP_21140A:
3555 	case TULIP_CHIP_MX98713:
3556 	case TULIP_CHIP_AX88140:
3557 	case TULIP_CHIP_AX88141:
3558 		sc->sc_gp_dir = *cp++;
3559 		break;
3560 
3561 	default:
3562 		/* Nothing. */
3563 	}
3564 
3565 	/* Get the media count. */
3566 	m_cnt = *cp++;
3567 
3568 	for (; m_cnt != 0; cp = ncp, m_cnt--) {
3569 		/*
3570 		 * Determine the type and length of this media block.
3571 		 */
3572 		if ((*cp & 0x80) == 0) {
3573 			length = 4;
3574 			type = TULIP_ROM_MB_21140_GPR;
3575 		} else {
3576 			length = (*cp++ & 0x7f) - 1;
3577 			type = *cp++ & 0x3f;
3578 		}
3579 
3580 		/* Compute the start of the next block. */
3581 		ncp = cp + length;
3582 
3583 		/* Now, parse the block. */
3584 		switch (type) {
3585 		case TULIP_ROM_MB_21140_GPR:
3586 			seen |= 1 << TULIP_ROM_MB_21140_GPR;
3587 
3588 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
3589 			memset(tm, 0, sizeof(*tm));
3590 
3591 			tm->tm_type = TULIP_ROM_MB_21140_GPR;
3592 			tm->tm_get = tlp_21140_gpr_getmedia;
3593 			tm->tm_set = tlp_21140_gpr_setmedia;
3594 
3595 			minst = 0;	/* XXX compute new instance */
3596 
3597 			/* First is the media type code. */
3598 			tsti = tulip_srom_to_ifmedia(cp[0] &
3599 			    TULIP_ROM_MB_MEDIA_CODE);
3600 			if (tsti == NULL) {
3601 				/* Invalid media code. */
3602 				free(tm, M_DEVBUF);
3603 				break;
3604 			}
3605 			tm->tm_name = tsti->tsti_name;
3606 
3607 			/* Next is any GPIO info for this media. */
3608 			tm->tm_gpdata = cp[1];
3609 
3610 			/*
3611 			 * Next is a word containing OPMODE information
3612 			 * and info on how to detect if this media is
3613 			 * active.
3614 			 */
3615 			word = TULIP_ROM_GETW(cp, 2);
3616 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
3617 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
3618 				tm->tm_actmask =
3619 				    TULIP_ROM_MB_BITPOS(word);
3620 				tm->tm_actdata =
3621 				    (word & TULIP_ROM_MB_POLARITY) ?
3622 				    0 : tm->tm_actmask;
3623 			}
3624 
3625 			/*
3626 			 * Now, add the media to our list.  We will
3627 			 * print them out later.
3628 			 */
3629 			ifmedia_add(&sc->sc_mii.mii_media,
3630 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
3631 			    tsti->tsti_options, minst), 0, tm);
3632 			break;
3633 
3634 		case TULIP_ROM_MB_21140_MII:
3635 			seen |= 1 << TULIP_ROM_MB_21140_MII;
3636 
3637 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
3638 			memset(tm, 0, sizeof(*tm));
3639 
3640 			tm->tm_type = TULIP_ROM_MB_21140_MII;
3641 			tm->tm_get = tlp_mii_getmedia;
3642 			tm->tm_set = tlp_mii_setmedia;
3643 
3644 			if (sc->sc_reset == NULL)
3645 				sc->sc_reset = tlp_21140_reset;
3646 
3647 			/* First is the PHY number. */
3648 			tm->tm_phyno = *cp++;
3649 
3650 			/* Next is the MII select sequence length and offset. */
3651 			tm->tm_gp_length = *cp++;
3652 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
3653 			cp += tm->tm_gp_length;
3654 
3655 			/* Next is the MII reset sequence length and offset. */
3656 			tm->tm_reset_length = *cp++;
3657 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
3658 			cp += tm->tm_reset_length;
3659 
3660 			/*
3661 			 * The following items are left in the media block
3662 			 * that we don't particularly care about:
3663 			 *
3664 			 *	capabilities		W
3665 			 *	advertisement		W
3666 			 *	full duplex		W
3667 			 *	tx threshold		W
3668 			 *
3669 			 * These appear to be bits in the PHY registers,
3670 			 * which our MII code handles on its own.
3671 			 */
3672 
3673 			/*
3674 			 * Before we probe the MII bus, we need to reset
3675 			 * it and issue the selection sequence.
3676 			 */
3677 
3678 			/* Set the direction of the pins... */
3679 			TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
3680 
3681 			for (i = 0; i < tm->tm_reset_length; i++) {
3682 				delay(10);
3683 				TULIP_WRITE(sc, CSR_GPP,
3684 				    sc->sc_srom[tm->tm_reset_offset + i]);
3685 			}
3686 
3687 			for (i = 0; i < tm->tm_gp_length; i++) {
3688 				delay(10);
3689 				TULIP_WRITE(sc, CSR_GPP,
3690 				    sc->sc_srom[tm->tm_gp_offset + i]);
3691 			}
3692 
3693 			/* If there were no sequences, just lower the pins. */
3694 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
3695 				delay(10);
3696 				TULIP_WRITE(sc, CSR_GPP, 0);
3697 			}
3698 
3699 			/*
3700 			 * Now, probe the MII for the PHY.  Note, we know
3701 			 * the location of the PHY on the bus, but we don't
3702 			 * particularly care; the MII code just likes to
3703 			 * search the whole thing anyhow.
3704 			 */
3705 			mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
3706 
3707 			/*
3708 			 * Now, search for the PHY we hopefully just
3709 			 * configured.  If it's not configured into the
3710 			 * kernel, we lose.  The PHY's default media always
3711 			 * takes priority.
3712 			 */
3713 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
3714 			     phy != NULL;
3715 			     phy = LIST_NEXT(phy, mii_list))
3716 				if (phy->mii_offset == tm->tm_phyno)
3717 					break;
3718 			if (phy == NULL) {
3719 				printf("%s: unable to configure MII\n",
3720 				    sc->sc_dev.dv_xname);
3721 				break;
3722 			}
3723 
3724 			sc->sc_flags |= TULIPF_HAS_MII;
3725 			sc->sc_tick = tlp_mii_tick;
3726 			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
3727 			    phy->mii_inst);
3728 
3729 			/*
3730 			 * Okay, now that we've found the PHY and the MII
3731 			 * layer has added all of the media associated
3732 			 * with that PHY, we need to traverse the media
3733 			 * list, and add our `tm' to each entry's `aux'
3734 			 * pointer.
3735 			 *
3736 			 * We do this by looking for media with our
3737 			 * PHY's `instance'.
3738 			 */
3739 			for (ife = LIST_FIRST(&sc->sc_mii.mii_media.ifm_list);
3740 			     ife != NULL;
3741 			     ife = LIST_NEXT(ife, ifm_list)) {
3742 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
3743 					continue;
3744 				ife->ifm_aux = tm;
3745 			}
3746 			break;
3747 
3748 		case TULIP_ROM_MB_21142_SIA:
3749 			printf("%s: 21142 SIA block\n", sc->sc_dev.dv_xname);
3750 			break;
3751 
3752 		case TULIP_ROM_MB_21142_MII:
3753 			printf("%s: 21142 MII block\n", sc->sc_dev.dv_xname);
3754 			break;
3755 
3756 		case TULIP_ROM_MB_21143_SYM:
3757 			printf("%s: 21143 SYM block\n", sc->sc_dev.dv_xname);
3758 			break;
3759 
3760 		case TULIP_ROM_MB_21143_RESET:
3761 			printf("%s: 21143 reset block\n", sc->sc_dev.dv_xname);
3762 			break;
3763 
3764 		default:
3765 			printf("%s: unknown ISV media block type 0x%02x\n",
3766 			    sc->sc_dev.dv_xname, type);
3767 		}
3768 	}
3769 
3770 	/*
3771 	 * Deal with the case where no media is configured.
3772 	 */
3773 	if (LIST_FIRST(&sc->sc_mii.mii_media.ifm_list) == NULL) {
3774 		printf("%s: no media found!\n", sc->sc_dev.dv_xname);
3775 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
3776 		defmedia = IFM_ETHER|IFM_NONE;
3777 		goto set_default;
3778 	}
3779 
3780 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
3781 
3782 	/*
3783 	 * Display any non-MII media we've located.
3784 	 */
3785 	if (seen & (1 << TULIP_ROM_MB_21140_GPR)) {
3786 		const char *sep = "";
3787 		printf("%s: GPR media: ", sc->sc_dev.dv_xname);
3788 		for (ife = LIST_FIRST(&sc->sc_mii.mii_media.ifm_list);
3789 		     ife != NULL;
3790 		     ife = LIST_NEXT(ife, ifm_list)) {
3791 			minst = IFM_INST(ife->ifm_media);
3792 			tm = ife->ifm_aux;
3793 			if (tm->tm_type != TULIP_ROM_MB_21140_GPR)
3794 				continue;
3795 			PRINT(tm->tm_name);
3796 		}
3797 
3798 		/*
3799 		 * XXX Pick a better default.  Should come
3800 		 * XXX from SROM on 21140[A], and should
3801 		 * XXX be "auto" on Macronix chips (which
3802 		 * XXX have an internal NWay block).
3803 		 */
3804 		if (defmedia == 0) {
3805 			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0,
3806 			    minst);
3807 			printf(", default 10baseT");
3808 		}
3809 		printf("\n");
3810 	}
3811 
3812 	if (seen & (1 << TULIP_ROM_MB_21142_SIA)) {
3813 		printf("%s: SIA media: ", sc->sc_dev.dv_xname);
3814 		/* XXX */
3815 		printf("\n");
3816 	}
3817 
3818 	if (seen & (1 << TULIP_ROM_MB_21143_SYM)) {
3819 		printf("%s: SYM media: ", sc->sc_dev.dv_xname);
3820 		/* XXX */
3821 		printf("\n");
3822 	}
3823 
3824 	/*
3825 	 * XXX Display default media if not MII.
3826 	 */
3827 
3828 #undef PRINT
3829 
3830  set_default:
3831 	/*
3832 	 * Set the default media.
3833 	 *
3834 	 * XXX Should make some attempt to care about the SROM default
3835 	 * setting, but we don't.
3836 	 */
3837 #ifdef DIAGNOSTIC
3838 	if (defmedia == 0)
3839 		panic("tlp_2114x_isv_tmsw_init: no default media");
3840 #endif
3841 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
3842 }
3843 
3844 void
3845 tlp_2114x_isv_tmsw_get(sc, ifmr)
3846 	struct tulip_softc *sc;
3847 	struct ifmediareq *ifmr;
3848 {
3849 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3850 	struct tulip_2114x_media *tm = ife->ifm_aux;
3851 
3852 	(*tm->tm_get)(sc, ifmr);
3853 }
3854 
3855 int
3856 tlp_2114x_isv_tmsw_set(sc)
3857 	struct tulip_softc *sc;
3858 {
3859 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3860 	struct tulip_2114x_media *tm = ife->ifm_aux;
3861 
3862 	return ((*tm->tm_set)(sc));
3863 }
3864 
3865 void
3866 tlp_21140_gpr_getmedia(sc, ifmr)
3867 	struct tulip_softc *sc;
3868 	struct ifmediareq *ifmr;
3869 {
3870 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3871 	struct tulip_2114x_media *tm = ife->ifm_aux;
3872 
3873 	ifmr->ifm_status = 0;
3874 
3875 	switch (IFM_SUBTYPE(ife->ifm_media)) {
3876 	case IFM_AUTO:
3877 		/*
3878 		 * XXX Implement autosensing case.
3879 		 */
3880 		break;
3881 
3882 	default:
3883 		/*
3884 		 * If not autosensing, active media is the currently
3885 		 * selected media.
3886 		 */
3887 		ifmr->ifm_active = ife->ifm_media;
3888 
3889 		/*
3890 		 * If we can sense the active status of the link,
3891 		 * so do.
3892 		 */
3893 		if (tm->tm_actmask != 0) {
3894 			ifmr->ifm_status |= IFM_AVALID;
3895 			if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
3896 			    tm->tm_actdata)
3897 				ifmr->ifm_status |= IFM_ACTIVE;
3898 		}
3899 	}
3900 }
3901 
3902 int
3903 tlp_21140_gpr_setmedia(sc)
3904 	struct tulip_softc *sc;
3905 {
3906 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
3907 	struct tulip_2114x_media *tm = ife->ifm_aux;
3908 
3909 	switch (IFM_SUBTYPE(ife->ifm_media)) {
3910 	case IFM_AUTO:
3911 		/*
3912 		 * XXX Implement autosensing case.
3913 		 */
3914 		break;
3915 
3916 	default:
3917 		/*
3918 		 * The ifmedia entry contains the OPMODE bits necessary
3919 		 * to enable this media type.  It may be necessary to
3920 		 * perform a reset of the chip; see tlp_21140_reset().
3921 		 */
3922 		if ((tm->tm_opmode & OPMODE_MEDIA_BITS) !=
3923 		    (sc->sc_opmode & OPMODE_MEDIA_BITS)) {
3924 			/*
3925 			 * We have to reset the chip.  Note that we
3926 			 * won't recurse into this path again as
3927 			 * the OPMODE bits will be correct this
3928 			 * next time through.
3929 			 */
3930 			return (tlp_init(sc));
3931 		}
3932 
3933 		/*
3934 		 * Set new OPMODE bits and write the OPMODE register.
3935 		 */
3936 		tlp_idle(sc, OPMODE_ST|OPMODE_SR);
3937 		sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) |
3938 		    tm->tm_opmode;
3939 		TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3940 
3941 		/*
3942 		 * Set the GPIO pins for this media, to flip any
3943 		 * relays, etc.
3944 		 */
3945 		TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
3946 		delay(10);
3947 		TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);
3948 		break;
3949 	}
3950 
3951 	return (0);
3952 }
3953 
3954 /*
3955  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
3956  */
3957 void	tlp_sio_mii_tmsw_init __P((struct tulip_softc *));
3958 
3959 const struct tulip_mediasw tlp_sio_mii_mediasw = {
3960 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
3961 };
3962 
3963 void
3964 tlp_sio_mii_tmsw_init(sc)
3965 	struct tulip_softc *sc;
3966 {
3967 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
3968 
3969 	sc->sc_mii.mii_ifp = ifp;
3970 	sc->sc_mii.mii_readreg = tlp_sio_mii_readreg;
3971 	sc->sc_mii.mii_writereg = tlp_sio_mii_writereg;
3972 	sc->sc_mii.mii_statchg = sc->sc_statchg;
3973 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
3974 	    tlp_mediastatus);
3975 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
3976 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
3977 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
3978 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
3979 	} else {
3980 		sc->sc_flags |= TULIPF_HAS_MII;
3981 		sc->sc_tick = tlp_mii_tick;
3982 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
3983 	}
3984 }
3985 
3986 /*
3987  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
3988  */
3989 void	tlp_pnic_tmsw_init __P((struct tulip_softc *));
3990 void	tlp_pnic_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
3991 int	tlp_pnic_tmsw_set __P((struct tulip_softc *));
3992 
3993 const struct tulip_mediasw tlp_pnic_mediasw = {
3994 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
3995 };
3996 
3997 void	tlp_pnic_nway_statchg __P((struct device *));
3998 void	tlp_pnic_nway_tick __P((void *));
3999 int	tlp_pnic_nway_service __P((struct tulip_softc *, int));
4000 void	tlp_pnic_nway_reset __P((struct tulip_softc *));
4001 int	tlp_pnic_nway_auto __P((struct tulip_softc *, int));
4002 void	tlp_pnic_nway_auto_timeout __P((void *));
4003 void	tlp_pnic_nway_status __P((struct tulip_softc *));
4004 void	tlp_pnic_nway_acomp __P((struct tulip_softc *));
4005 
4006 void
4007 tlp_pnic_tmsw_init(sc)
4008 	struct tulip_softc *sc;
4009 {
4010 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
4011 	const char *sep = "";
4012 
4013 #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
4014 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
4015 
4016 	sc->sc_mii.mii_ifp = ifp;
4017 	sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
4018 	sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
4019 	sc->sc_mii.mii_statchg = sc->sc_statchg;
4020 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4021 	    tlp_mediastatus);
4022 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
4023 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
4024 		/* XXX What about AUI/BNC support? */
4025 		printf("%s: ", sc->sc_dev.dv_xname);
4026 
4027 		tlp_pnic_nway_reset(sc);
4028 
4029 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
4030 		    PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
4031 		PRINT("10baseT");
4032 
4033 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
4034 		    PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
4035 		PRINT("10baseT-FDX");
4036 
4037 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
4038 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
4039 		PRINT("100baseTX");
4040 
4041 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
4042 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
4043 		    PNIC_NWAY_CAP100TXFDX);
4044 		PRINT("100baseTX-FDX");
4045 
4046 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
4047 		    PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
4048 		    PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
4049 		    PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
4050 		PRINT("auto");
4051 
4052 		printf("\n");
4053 
4054 		sc->sc_statchg = tlp_pnic_nway_statchg;
4055 		sc->sc_tick = tlp_pnic_nway_tick;
4056 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
4057 	} else {
4058 		sc->sc_flags |= TULIPF_HAS_MII;
4059 		sc->sc_tick = tlp_mii_tick;
4060 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
4061 	}
4062 
4063 #undef ADD
4064 #undef PRINT
4065 }
4066 
4067 void
4068 tlp_pnic_tmsw_get(sc, ifmr)
4069 	struct tulip_softc *sc;
4070 	struct ifmediareq *ifmr;
4071 {
4072 	struct mii_data *mii = &sc->sc_mii;
4073 
4074 	if (sc->sc_flags & TULIPF_HAS_MII)
4075 		tlp_mii_getmedia(sc, ifmr);
4076 	else {
4077 		mii->mii_media_status = 0;
4078 		mii->mii_media_active = IFM_NONE;
4079 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
4080 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
4081 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
4082 	}
4083 }
4084 
4085 int
4086 tlp_pnic_tmsw_set(sc)
4087 	struct tulip_softc *sc;
4088 {
4089 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
4090 	struct mii_data *mii = &sc->sc_mii;
4091 
4092 	if (sc->sc_flags & TULIPF_HAS_MII) {
4093 		/*
4094 		 * Make sure the built-in Tx jabber timer is disabled.
4095 		 */
4096 		TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
4097 
4098 		return (tlp_mii_setmedia(sc));
4099 	}
4100 
4101 	if (ifp->if_flags & IFF_UP) {
4102 		mii->mii_media_status = 0;
4103 		mii->mii_media_active = IFM_NONE;
4104 		return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
4105 	}
4106 
4107 	return (0);
4108 }
4109 
4110 void
4111 tlp_pnic_nway_statchg(self)
4112 	struct device *self;
4113 {
4114 	struct tulip_softc *sc = (struct tulip_softc *)self;
4115 
4116 	/* Idle the transmit and receive processes. */
4117 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
4118 
4119 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
4120 	    OPMODE_SCR|OPMODE_HBD);
4121 
4122 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
4123 		sc->sc_opmode |= OPMODE_TTM;
4124 		TULIP_WRITE(sc, CSR_GPP,
4125 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
4126 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
4127 	} else {
4128 		sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD;
4129 		TULIP_WRITE(sc, CSR_GPP,
4130 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
4131 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
4132 	}
4133 
4134 	if (sc->sc_mii.mii_media_active & IFM_FDX)
4135 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
4136 
4137 	/*
4138 	 * Write new OPMODE bits.  This also restarts the transmit
4139 	 * and receive processes.
4140 	 */
4141 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
4142 
4143 	/* XXX Update ifp->if_baudrate */
4144 }
4145 
4146 void
4147 tlp_pnic_nway_tick(arg)
4148 	void *arg;
4149 {
4150 	struct tulip_softc *sc = arg;
4151 	int s;
4152 
4153 	s = splnet();
4154 	tlp_pnic_nway_service(sc, MII_TICK);
4155 	splx(s);
4156 
4157 	timeout(tlp_pnic_nway_tick, sc, hz);
4158 }
4159 
4160 /*
4161  * Support for the Lite-On PNIC internal NWay block.  This is constructed
4162  * somewhat like a PHY driver for simplicity.
4163  */
4164 
4165 int
4166 tlp_pnic_nway_service(sc, cmd)
4167 	struct tulip_softc *sc;
4168 	int cmd;
4169 {
4170 	struct mii_data *mii = &sc->sc_mii;
4171 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
4172 
4173 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
4174 		return (0);
4175 
4176 	switch (cmd) {
4177 	case MII_POLLSTAT:
4178 		/* Nothing special to do here. */
4179 		break;
4180 
4181 	case MII_MEDIACHG:
4182 		switch (IFM_SUBTYPE(ife->ifm_media)) {
4183 		case IFM_AUTO:
4184 			(void) tlp_pnic_nway_auto(sc, 1);
4185 			break;
4186 		case IFM_100_T4:
4187 			/*
4188 			 * XXX Not supported as a manual setting right now.
4189 			 */
4190 			return (EINVAL);
4191 		default:
4192 			/*
4193 			 * NWAY register data is stored in the ifmedia entry.
4194 			 */
4195 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
4196 		}
4197 		break;
4198 
4199 	case MII_TICK:
4200 		/*
4201 		 * Only used for autonegotiation.
4202 		 */
4203 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
4204 			return (0);
4205 
4206 		/*
4207 		 * Check to see if we have link.  If we do, we don't
4208 		 * need to restart the autonegotiation process.
4209 		 */
4210 		if (sc->sc_flags & TULIPF_LINK_UP)
4211 			return (0);
4212 
4213 		/*
4214 		 * Only retry autonegotiation every 5 seconds.
4215 		 */
4216 		if (++sc->sc_nway_ticks != 5)
4217 			return (0);
4218 
4219 		sc->sc_nway_ticks = 0;
4220 		tlp_pnic_nway_reset(sc);
4221 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
4222 			return (0);
4223 		break;
4224 	}
4225 
4226 	/* Update the media status. */
4227 	tlp_pnic_nway_status(sc);
4228 
4229 	/* Callback if something changed. */
4230 	if (sc->sc_nway_active != mii->mii_media_active ||
4231 	    cmd == MII_MEDIACHG) {
4232 		(*sc->sc_statchg)(&sc->sc_dev);
4233 		sc->sc_nway_active = mii->mii_media_active;
4234 	}
4235 	return (0);
4236 }
4237 
4238 void
4239 tlp_pnic_nway_reset(sc)
4240 	struct tulip_softc *sc;
4241 {
4242 
4243 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
4244 	delay(100);
4245 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
4246 }
4247 
4248 int
4249 tlp_pnic_nway_auto(sc, waitfor)
4250 	struct tulip_softc *sc;
4251 	int waitfor;
4252 {
4253 	struct mii_data *mii = &sc->sc_mii;
4254 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
4255 	u_int32_t reg;
4256 	int i;
4257 
4258 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
4259 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
4260 
4261 	if (waitfor) {
4262 		/* Wait 500ms for it to complete. */
4263 		for (i = 0; i < 500; i++) {
4264 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
4265 			if (reg & PNIC_NWAY_LPAR_MASK) {
4266 				tlp_pnic_nway_acomp(sc);
4267 				return (0);
4268 			}
4269 			delay(1000);
4270 		}
4271 #if 0
4272 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
4273 			printf("%s: autonegotiation failed to complete\n",
4274 			    sc->sc_dev.dv_xname);
4275 #endif
4276 
4277 		/*
4278 		 * Don't need to worry about clearing DOINGAUTO.
4279 		 * If that's set, a timeout is pending, and it will
4280 		 * clear the flag.
4281 		 */
4282 		return (EIO);
4283 	}
4284 
4285 	/*
4286 	 * Just let it finish asynchronously.  This is for the benefit of
4287 	 * the tick handler driving autonegotiation.  Don't want 500ms
4288 	 * delays all the time while the system is running!
4289 	 */
4290 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
4291 		sc->sc_flags |= TULIPF_DOINGAUTO;
4292 		timeout(tlp_pnic_nway_auto_timeout, sc, hz >> 1);
4293 	}
4294 	return (EJUSTRETURN);
4295 }
4296 
4297 void
4298 tlp_pnic_nway_auto_timeout(arg)
4299 	void *arg;
4300 {
4301 	struct tulip_softc *sc = arg;
4302 	u_int32_t reg;
4303 	int s;
4304 
4305 	s = splnet();
4306 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
4307 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
4308 #if 0
4309 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
4310 		printf("%s: autonegotiation failed to complete\n",
4311 		    sc->sc_dev.dv_xname);
4312 #endif
4313 
4314 	tlp_pnic_nway_acomp(sc);
4315 
4316 	/* Update the media status. */
4317 	(void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
4318 	splx(s);
4319 }
4320 
4321 void
4322 tlp_pnic_nway_status(sc)
4323 	struct tulip_softc *sc;
4324 {
4325 	struct mii_data *mii = &sc->sc_mii;
4326 	u_int32_t reg;
4327 
4328 	mii->mii_media_status = IFM_AVALID;
4329 	mii->mii_media_active = IFM_ETHER;
4330 
4331 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
4332 
4333 	if (sc->sc_flags & TULIPF_LINK_UP)
4334 		mii->mii_media_status |= IFM_ACTIVE;
4335 
4336 	if (reg & PNIC_NWAY_NW) {
4337 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
4338 			/* Erg, still trying, I guess... */
4339 			mii->mii_media_active |= IFM_NONE;
4340 			return;
4341 		}
4342 
4343 #if 0
4344 		if (reg & PNIC_NWAY_LPAR100T4)
4345 			mii->mii_media_active |= IFM_100_T4;
4346 		else
4347 #endif
4348 		if (reg & PNIC_NWAY_LPAR100TXFDX)
4349 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
4350 		else if (reg & PNIC_NWAY_LPAR100TX)
4351 			mii->mii_media_active |= IFM_100_TX;
4352 		else if (reg & PNIC_NWAY_LPAR10TFDX)
4353 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
4354 		else if (reg & PNIC_NWAY_LPAR10T)
4355 			mii->mii_media_active |= IFM_10_T;
4356 		else
4357 			mii->mii_media_active |= IFM_NONE;
4358 	} else {
4359 		if (reg & PNIC_NWAY_100)
4360 			mii->mii_media_active |= IFM_100_TX;
4361 		else
4362 			mii->mii_media_active |= IFM_10_T;
4363 		if (reg & PNIC_NWAY_FD)
4364 			mii->mii_media_active |= IFM_FDX;
4365 	}
4366 }
4367 
4368 void
4369 tlp_pnic_nway_acomp(sc)
4370 	struct tulip_softc *sc;
4371 {
4372 	u_int32_t reg;
4373 
4374 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
4375 	reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
4376 
4377 	if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
4378 		reg |= PNIC_NWAY_100;
4379 	if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
4380 		reg |= PNIC_NWAY_FD;
4381 
4382 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
4383 }
4384 
4385 /*
4386  * Macronix PMAC media switch.  MX98713 and MX98713A have MII.
4387  * All have GPR media.  MX98713A, MX98715, MX98725 have internal
4388  * Nway blocks for autonegotiation.
4389  */
4390 void	tlp_pmac_tmsw_init __P((struct tulip_softc *));
4391 void	tlp_pmac_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
4392 int	tlp_pmac_tmsw_set __P((struct tulip_softc *));
4393 
4394 const struct tulip_mediasw tlp_pmac_mediasw = {
4395 	tlp_pmac_tmsw_init, tlp_pmac_tmsw_get, tlp_pmac_tmsw_set
4396 };
4397 
4398 void	tlp_pmac_nway_statchg __P((struct device *));
4399 void	tlp_pmac_nway_tick __P((void *));
4400 int	tlp_pmac_nway_service __P((struct tulip_softc *, int));
4401 void	tlp_pmac_nway_reset __P((struct tulip_softc *));
4402 int	tlp_pmac_nway_auto __P((struct tulip_softc *, int));
4403 void	tlp_pmac_nway_auto_timeout __P((void *));
4404 void	tlp_pmac_nway_status __P((struct tulip_softc *));
4405 void	tlp_pmac_nway_acomp __P((struct tulip_softc *));
4406 
4407 void	tlp_pmac_check_link __P((struct tulip_softc *));
4408 
4409 void
4410 tlp_pmac_tmsw_init(sc)
4411 	struct tulip_softc *sc;
4412 {
4413 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
4414 	const char *sep = "";
4415 
4416 #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
4417 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
4418 
4419 	sc->sc_mii.mii_ifp = ifp;
4420 	sc->sc_mii.mii_readreg = tlp_sio_mii_readreg;
4421 	sc->sc_mii.mii_writereg = tlp_sio_mii_writereg;
4422 	sc->sc_mii.mii_statchg = sc->sc_statchg;
4423 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4424 	    tlp_mediastatus);
4425 	if (sc->sc_chip == TULIP_CHIP_MX98713 ||
4426 	    sc->sc_chip == TULIP_CHIP_MX98713A) {
4427 		mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
4428 		if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) {
4429 			sc->sc_flags |= TULIPF_HAS_MII;
4430 			sc->sc_tick = tlp_mii_tick;
4431 			sc->sc_preinit = tlp_2114x_preinit;
4432 			ifmedia_set(&sc->sc_mii.mii_media,
4433 			    IFM_ETHER|IFM_AUTO);
4434 			return;
4435 		}
4436 	}
4437 
4438 	printf("%s: ", sc->sc_dev.dv_xname);
4439 
4440 	tlp_pmac_nway_reset(sc);
4441 
4442 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
4443 	    PMAC_10TCTL_LTE|PMAC_10TCTL_HDE);
4444 	PRINT("10baseT");
4445 
4446 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
4447 	    PMAC_10TCTL_LTE);
4448 	PRINT("10baseT-FDX");
4449 
4450 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
4451 	    PMAC_10TCTL_LTE|PMAC_10TCTL_TXH);
4452 	PRINT("100baseTX");
4453 
4454 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
4455 	    PMAC_10TCTL_LTE|PMAC_10TCTL_TXF);
4456 	PRINT("100baseTX-FDX");
4457 
4458 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
4459 	    PMAC_10TCTL_LTE|PMAC_10TCTL_HDE|PMAC_10TCTL_TXH|PMAC_10TCTL_TXF|
4460 	    PMAC_10TCTL_ANE);
4461 	PRINT("auto");
4462 
4463 	printf("\n");
4464 
4465 	/* Set the LED modes. */
4466 	tlp_pmac_reset(sc);
4467 
4468 	sc->sc_reset = tlp_pmac_reset;
4469 	sc->sc_statchg = tlp_pmac_nway_statchg;
4470 	sc->sc_tick = tlp_pmac_nway_tick;
4471 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
4472 
4473 #undef ADD
4474 #undef PRINT
4475 }
4476 
4477 void
4478 tlp_pmac_tmsw_get(sc, ifmr)
4479 	struct tulip_softc *sc;
4480 	struct ifmediareq *ifmr;
4481 {
4482 	struct mii_data *mii = &sc->sc_mii;
4483 
4484 	if (sc->sc_flags & TULIPF_HAS_MII)
4485 		tlp_mii_getmedia(sc, ifmr);
4486 	else {
4487 		mii->mii_media_status = 0;
4488 		mii->mii_media_active = IFM_NONE;
4489 		tlp_pmac_nway_service(sc, MII_POLLSTAT);
4490 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
4491 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
4492 	}
4493 }
4494 
4495 int
4496 tlp_pmac_tmsw_set(sc)
4497 	struct tulip_softc *sc;
4498 {
4499 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
4500 	struct mii_data *mii = &sc->sc_mii;
4501 
4502 	if (sc->sc_flags & TULIPF_HAS_MII)
4503 		return (tlp_mii_setmedia(sc));
4504 
4505 	if (ifp->if_flags & IFF_UP) {
4506 		mii->mii_media_status = 0;
4507 		mii->mii_media_active = IFM_NONE;
4508 		return (tlp_pmac_nway_service(sc, MII_MEDIACHG));
4509 	}
4510 
4511 	return (0);
4512 }
4513 
4514 void
4515 tlp_pmac_nway_statchg(self)
4516 	struct device *self;
4517 {
4518 	struct tulip_softc *sc = (struct tulip_softc *)self;
4519 
4520 	/* Idle the transmit and receive processes. */
4521 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
4522 
4523 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
4524 	    OPMODE_SCR|OPMODE_HBD);
4525 
4526 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
4527 		sc->sc_opmode |= OPMODE_TTM;
4528 	else
4529 		sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD;
4530 
4531 	if (sc->sc_mii.mii_media_active & IFM_FDX)
4532 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
4533 
4534 	/*
4535 	 * Write new OPMODE bits.  This also restarts the transmit
4536 	 * and receive processes.
4537 	 */
4538 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
4539 
4540 	/* XXX Update ifp->if_baudrate */
4541 }
4542 
4543 void
4544 tlp_pmac_nway_tick(arg)
4545 	void *arg;
4546 {
4547 	struct tulip_softc *sc = arg;
4548 	int s;
4549 
4550 	s = splnet();
4551 	tlp_pmac_nway_service(sc, MII_TICK);
4552 	splx(s);
4553 
4554 	timeout(tlp_pmac_nway_tick, sc, hz);
4555 }
4556 
4557 /*
4558  * Support for the Macronix PMAC internal NWay block.  This is constructed
4559  * somewhat like a PHY driver for simplicity.
4560  */
4561 
4562 int
4563 tlp_pmac_nway_service(sc, cmd)
4564 	struct tulip_softc *sc;
4565 	int cmd;
4566 {
4567 	struct mii_data *mii = &sc->sc_mii;
4568 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
4569 
4570 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
4571 		return (0);
4572 
4573 	tlp_pmac_check_link(sc);
4574 
4575 	switch (cmd) {
4576 	case MII_POLLSTAT:
4577 		/* Nothing special to do here. */
4578 		break;
4579 
4580 	case MII_MEDIACHG:
4581 		switch (IFM_SUBTYPE(ife->ifm_media)) {
4582 		case IFM_AUTO:
4583 			(void) tlp_pnic_nway_auto(sc, 1);
4584 			break;
4585 		case IFM_100_T4:
4586 			/*
4587 			 * XXX Not supported as a manual setting right now.
4588 			 */
4589 			return (EINVAL);
4590 		default:
4591 			/* Nothing to do in this case. */
4592 			break;
4593 		}
4594 		break;
4595 
4596 	case MII_TICK:
4597 		/*
4598 		 * Only used for autonegotiation.
4599 		 */
4600 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
4601 			return (0);
4602 
4603 		/*
4604 		 * Check to see if we have link.  If we do, we don't
4605 		 * need to restart the autonegotiation process.
4606 		 */
4607 		if (sc->sc_flags & TULIPF_LINK_UP)
4608 			return (0);
4609 
4610 		/*
4611 		 * Only retry autonegotiation every 5 seconds.
4612 		 */
4613 		if (++sc->sc_nway_ticks != 5)
4614 			return (0);
4615 
4616 		sc->sc_nway_ticks = 0;
4617 		tlp_pmac_nway_reset(sc);
4618 		if (tlp_pmac_nway_auto(sc, 0) == EJUSTRETURN)
4619 			return (0);
4620 		break;
4621 	}
4622 
4623 	/* Update the media status. */
4624 	tlp_pmac_nway_status(sc);
4625 
4626 	/* Callback if something changed. */
4627 	if (sc->sc_nway_active != mii->mii_media_active ||
4628 	    cmd == MII_MEDIACHG) {
4629 		(*sc->sc_statchg)(&sc->sc_dev);
4630 		sc->sc_nway_active = mii->mii_media_active;
4631 	}
4632 
4633 	tlp_pmac_check_link(sc);
4634 
4635 	return (0);
4636 }
4637 
4638 void
4639 tlp_pmac_nway_reset(sc)
4640 	struct tulip_softc *sc;
4641 {
4642 
4643 	TULIP_WRITE(sc, CSR_PMAC_NWAYRESET, 0);
4644 	delay(1000);
4645 }
4646 
4647 int
4648 tlp_pmac_nway_auto(sc, waitfor)
4649 	struct tulip_softc *sc;
4650 	int waitfor;
4651 {
4652 	int i;
4653 
4654 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
4655 		TULIP_WRITE(sc, CSR_STATUS, STATUS_LNPANC);
4656 		TULIP_SET(sc, CSR_PMAC_10TCTL, PMAC_10TCTL_ANE);
4657 	}
4658 
4659 	if (waitfor) {
4660 		/* Wait 500ms for it to complete. */
4661 		for (i = 0; i < 500; i++) {
4662 			if (TULIP_ISSET(sc, CSR_STATUS, STATUS_LNPANC)) {
4663 				tlp_pmac_nway_acomp(sc);
4664 				return (0);
4665 			}
4666 			delay(1000);
4667 		}
4668 #if 0
4669 		if (TULIP_ISSET(sc, CSR_STATUS, STATUS_LNPANC) == 0)
4670 			printf("%s: autonegotiation faild to complete\n",
4671 			    sc->sc_dev.dv_xname);
4672 #endif
4673 
4674 		/*
4675 		 * Don't need to worry about clearing DOINGAUTO.
4676 		 * If that's set, a timeout is pending, and it will
4677 		 * clear the flag.
4678 		 */
4679 		return (EIO);
4680 	}
4681 
4682 	/*
4683 	 * Just let it finish asynchronously.  This is for the benefit of
4684 	 * the tick handler driving autonegotiation.  Don't want 500ms
4685 	 * delays all the time while the system us running!
4686 	 */
4687 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
4688 		sc->sc_flags |= TULIPF_DOINGAUTO;
4689 		timeout(tlp_pmac_nway_auto_timeout, sc, hz >> 1);
4690 	}
4691 	return (EJUSTRETURN);
4692 }
4693 
4694 void
4695 tlp_pmac_nway_auto_timeout(arg)
4696 	void *arg;
4697 {
4698 	struct tulip_softc *sc = arg;
4699 	int s;
4700 
4701 	s = splnet();
4702 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
4703 #if 0
4704 	if (TULIP_ISSET(sc, CSR_STATUS, STATUS_LNPANC) == 0)
4705 		printf("%s: autonegotiation failed to complete\n",
4706 		    sc->sc_dev.dv_xname);
4707 #endif
4708 
4709 	tlp_pmac_nway_acomp(sc);
4710 
4711 	/* Update the media status. */
4712 	(void) tlp_pmac_nway_service(sc, MII_POLLSTAT);
4713 	splx(s);
4714 }
4715 
4716 void
4717 tlp_pmac_nway_status(sc)
4718 	struct tulip_softc *sc;
4719 {
4720 	struct mii_data *mii = &sc->sc_mii;
4721 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
4722 	u_int32_t reg;
4723 
4724 	mii->mii_media_status = IFM_AVALID;
4725 	mii->mii_media_active = IFM_ETHER;
4726 
4727 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
4728 		if (TULIP_ISSET(sc, CSR_STATUS, STATUS_LNPANC) == 0) {
4729 			/* Erg, still trying, I guess... */
4730 			mii->mii_media_active |= IFM_NONE;
4731 			sc->sc_flags &= ~TULIPF_LINK_UP;
4732 			return;
4733 		}
4734 
4735 		reg = TULIP_READ(sc, CSR_PMAC_NWAYSTAT);
4736 
4737 #if 0
4738 		if (reg & PMAC_NWAYSTAT_T4)
4739 			mii->mii_media_active |= IFM_100_T4;
4740 		else
4741 #endif
4742 		if (reg & PMAC_NWAYSTAT_100TXF)
4743 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
4744 		else if (reg & PMAC_NWAYSTAT_100TXH)
4745 			mii->mii_media_active |= IFM_100_TX;
4746 		else if (reg & PMAC_NWAYSTAT_10TXF)
4747 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
4748 		else if (reg & PMAC_NWAYSTAT_10TXH)
4749 			mii->mii_media_active |= IFM_10_T;
4750 		else
4751 			mii->mii_media_active |= IFM_NONE;
4752 	} else {
4753 		/*
4754 		 * Non-autosensing case; currently selected media
4755 		 * is the active media.
4756 		 */
4757 		mii->mii_media_active = ife->ifm_media;
4758 	}
4759 }
4760 
4761 void
4762 tlp_pmac_check_link(sc)
4763 	struct tulip_softc *sc;
4764 {
4765 	u_int32_t reg;
4766 
4767 	reg = TULIP_READ(sc, CSR_PMAC_10TSTAT);
4768 	if (IFM_SUBTYPE(sc->sc_nway_active) == IFM_10_T &&
4769 	    (reg & PMAC_10TSTAT_LS10) == 0)
4770 		sc->sc_flags |= TULIPF_LINK_UP;
4771 	else if (IFM_SUBTYPE(sc->sc_nway_active) == IFM_100_TX &&
4772 	         (reg & PMAC_10TSTAT_LS100) == 0)
4773 		sc->sc_flags |= TULIPF_LINK_UP;
4774 	else
4775 		sc->sc_flags &= ~TULIPF_LINK_UP;
4776 
4777 	sc->sc_mii.mii_media_status = IFM_AVALID;
4778 	if (sc->sc_flags & TULIPF_LINK_UP)
4779 		sc->sc_mii.mii_media_status |= IFM_ACTIVE;
4780 }
4781 
4782 void
4783 tlp_pmac_nway_acomp(sc)
4784 	struct tulip_softc *sc;
4785 {
4786 
4787 	TULIP_CLR(sc, CSR_PMAC_10TCTL, PMAC_10TCTL_ANE);
4788 }
4789 
4790 /*
4791  * ADMtek AL981 media switch.  Only has internal PHY.
4792  */
4793 void	tlp_al981_tmsw_init __P((struct tulip_softc *));
4794 
4795 const struct tulip_mediasw tlp_al981_mediasw = {
4796 	tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
4797 };
4798 
4799 void
4800 tlp_al981_tmsw_init(sc)
4801 	struct tulip_softc *sc;
4802 {
4803 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
4804 
4805 	sc->sc_mii.mii_ifp = ifp;
4806 	sc->sc_mii.mii_readreg = tlp_al981_mii_readreg;
4807 	sc->sc_mii.mii_writereg = tlp_al981_mii_writereg;
4808 	sc->sc_mii.mii_statchg = sc->sc_statchg;
4809 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
4810 	    tlp_mediastatus);
4811 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
4812 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
4813 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
4814 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
4815 	} else {
4816 		sc->sc_flags |= TULIPF_HAS_MII;
4817 		sc->sc_tick = tlp_mii_tick;
4818 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
4819 	}
4820 }
4821