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