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