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