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