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