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