tulip.c revision 1.12 1 /* $NetBSD: tulip.c,v 1.12 1999/09/19 20:51:09 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
86 #include <dev/ic/tulipreg.h>
87 #include <dev/ic/tulipvar.h>
88
89 /*
90 * The following tables compute the transmit threshold mode. We start
91 * at index 0. When ever we get a transmit underrun, we increment our
92 * index, falling back if we encounter the NULL terminator.
93 *
94 * Note: Store and forward mode is only available on the 100mbps chips
95 * (21140 and higher).
96 */
97 const struct tulip_txthresh_tab tlp_10_txthresh_tab[] = {
98 { OPMODE_TR_72, "72 bytes" },
99 { OPMODE_TR_96, "96 bytes" },
100 { OPMODE_TR_128, "128 bytes" },
101 { OPMODE_TR_160, "160 bytes" },
102 { 0, NULL },
103 };
104
105 const struct tulip_txthresh_tab tlp_10_100_txthresh_tab[] = {
106 { OPMODE_TR_72, "72/128 bytes" },
107 { OPMODE_TR_96, "96/256 bytes" },
108 { OPMODE_TR_128, "128/512 bytes" },
109 { OPMODE_TR_160, "160/1024 bytes" },
110 { OPMODE_SF, "store and forward mode" },
111 { 0, NULL },
112 };
113
114 #define TXTH_72 0
115 #define TXTH_96 1
116 #define TXTH_128 2
117 #define TXTH_160 3
118 #define TXTH_SF 4
119
120 /*
121 * The Winbond 89C840F does transmit threshold control totally
122 * differently. It simply has a 7-bit field which indicates
123 * the threshold:
124 *
125 * txth = ((OPMODE & OPMODE_WINB_TTH) >> OPMODE_WINB_TTH_SHIFT) * 16;
126 *
127 * However, we just do Store-and-Forward mode on these chips, since
128 * the DMA engines seem to be flaky.
129 */
130 const struct tulip_txthresh_tab tlp_winb_txthresh_tab[] = {
131 { 0, "store and forward mode" },
132 { 0, NULL },
133 };
134
135 #define TXTH_WINB_SF 0
136
137 void tlp_start __P((struct ifnet *));
138 void tlp_watchdog __P((struct ifnet *));
139 int tlp_ioctl __P((struct ifnet *, u_long, caddr_t));
140
141 void tlp_shutdown __P((void *));
142
143 void tlp_reset __P((struct tulip_softc *));
144 int tlp_init __P((struct tulip_softc *));
145 void tlp_rxdrain __P((struct tulip_softc *));
146 void tlp_stop __P((struct tulip_softc *, int));
147 int tlp_add_rxbuf __P((struct tulip_softc *, int));
148 void tlp_idle __P((struct tulip_softc *, u_int32_t));
149 void tlp_srom_idle __P((struct tulip_softc *));
150
151 void tlp_filter_setup __P((struct tulip_softc *));
152 void tlp_winb_filter_setup __P((struct tulip_softc *));
153
154 void tlp_rxintr __P((struct tulip_softc *));
155 void tlp_txintr __P((struct tulip_softc *));
156
157 void tlp_mii_tick __P((void *));
158 void tlp_mii_statchg __P((struct device *));
159 void tlp_winb_mii_statchg __P((struct device *));
160
161 void tlp_mii_getmedia __P((struct tulip_softc *, struct ifmediareq *));
162 int tlp_mii_setmedia __P((struct tulip_softc *));
163
164 void tlp_sio_mii_sync __P((struct tulip_softc *));
165 void tlp_sio_mii_sendbits __P((struct tulip_softc *, u_int32_t, int));
166 int tlp_sio_mii_readreg __P((struct device *, int, int));
167 void tlp_sio_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 u_int32_t tlp_crc32 __P((const u_int8_t *, size_t));
173 #define tlp_mchash(addr) (tlp_crc32((addr), ETHER_ADDR_LEN) & \
174 (TULIP_MCHASHSIZE - 1))
175
176 #ifdef TLP_DEBUG
177 #define DPRINTF(sc, x) if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
178 printf x
179 #else
180 #define DPRINTF(sc, x) /* nothing */
181 #endif
182
183 /*
184 * tlp_attach:
185 *
186 * Attach a Tulip interface to the system.
187 */
188 void
189 tlp_attach(sc, enaddr)
190 struct tulip_softc *sc;
191 const u_int8_t *enaddr;
192 {
193 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
194 int i, rseg, error;
195 bus_dma_segment_t seg;
196
197 /*
198 * NOTE: WE EXPECT THE FRONT-END TO INITIALIZE sc_regshift!
199 */
200
201 /*
202 * Setup the transmit threshold table.
203 */
204 switch (sc->sc_chip) {
205 case TULIP_CHIP_DE425:
206 case TULIP_CHIP_21040:
207 case TULIP_CHIP_21041:
208 sc->sc_txth = tlp_10_txthresh_tab;
209 break;
210
211 default:
212 sc->sc_txth = tlp_10_100_txthresh_tab;
213 break;
214 }
215
216 /*
217 * Setup the filter setup function.
218 */
219 switch (sc->sc_chip) {
220 case TULIP_CHIP_WB89C840F:
221 sc->sc_filter_setup = tlp_winb_filter_setup;
222 break;
223
224 default:
225 sc->sc_filter_setup = tlp_filter_setup;
226 break;
227 }
228
229 /*
230 * Set up the media status change function.
231 */
232 switch (sc->sc_chip) {
233 case TULIP_CHIP_WB89C840F:
234 sc->sc_statchg = tlp_winb_mii_statchg;
235 break;
236
237 default:
238 /*
239 * We may override this if we have special media
240 * handling requirements (e.g. flipping GPIO pins).
241 *
242 * The pure-MII statchg function covers the basics.
243 */
244 sc->sc_statchg = tlp_mii_statchg;
245 break;
246 }
247
248 /*
249 * Set up various chip-specific quirks.
250 */
251 switch (sc->sc_chip) {
252 case TULIP_CHIP_82C168:
253 case TULIP_CHIP_82C169:
254 /*
255 * These chips seem to have busted DMA engines; just put them
256 * in Store-and-Forward mode from the get-go.
257 */
258 sc->sc_txthresh = TXTH_SF;
259 break;
260
261 case TULIP_CHIP_WB89C840F:
262 sc->sc_flags |= TULIPF_IC_FS;
263 break;
264
265 default:
266 /* Nothing. */
267 }
268
269 SIMPLEQ_INIT(&sc->sc_txfreeq);
270 SIMPLEQ_INIT(&sc->sc_txdirtyq);
271
272 /*
273 * Allocate the control data structures, and create and load the
274 * DMA map for it.
275 */
276 if ((error = bus_dmamem_alloc(sc->sc_dmat,
277 sizeof(struct tulip_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
278 0)) != 0) {
279 printf("%s: unable to allocate control data, error = %d\n",
280 sc->sc_dev.dv_xname, error);
281 goto fail_0;
282 }
283
284 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
285 sizeof(struct tulip_control_data), (caddr_t *)&sc->sc_control_data,
286 BUS_DMA_COHERENT)) != 0) {
287 printf("%s: unable to map control data, error = %d\n",
288 sc->sc_dev.dv_xname, error);
289 goto fail_1;
290 }
291
292 if ((error = bus_dmamap_create(sc->sc_dmat,
293 sizeof(struct tulip_control_data), 1,
294 sizeof(struct tulip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
295 printf("%s: unable to create control data DMA map, "
296 "error = %d\n", sc->sc_dev.dv_xname, error);
297 goto fail_2;
298 }
299
300 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
301 sc->sc_control_data, sizeof(struct tulip_control_data), NULL,
302 0)) != 0) {
303 printf("%s: unable to load control data DMA map, error = %d\n",
304 sc->sc_dev.dv_xname, error);
305 goto fail_3;
306 }
307
308 /*
309 * Create the transmit buffer DMA maps.
310 */
311 for (i = 0; i < TULIP_TXQUEUELEN; i++) {
312 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
313 TULIP_NTXSEGS, MCLBYTES, 0, 0,
314 &sc->sc_txsoft[i].txs_dmamap)) != 0) {
315 printf("%s: unable to create tx DMA map %d, "
316 "error = %d\n", sc->sc_dev.dv_xname, i, error);
317 goto fail_4;
318 }
319 }
320
321 /*
322 * Create the recieve buffer DMA maps.
323 */
324 for (i = 0; i < TULIP_NRXDESC; i++) {
325 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
326 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
327 printf("%s: unable to create rx DMA map %d, "
328 "error = %d\n", sc->sc_dev.dv_xname, i, error);
329 goto fail_5;
330 }
331 sc->sc_rxsoft[i].rxs_mbuf = NULL;
332 }
333
334 /*
335 * Reset the chip to a known state.
336 */
337 tlp_reset(sc);
338
339 /* Announce ourselves. */
340 printf("%s: %s%sEthernet address %s\n", sc->sc_dev.dv_xname,
341 sc->sc_name[0] != '\0' ? sc->sc_name : "",
342 sc->sc_name[0] != '\0' ? ", " : "",
343 ether_sprintf(enaddr));
344
345 /*
346 * Initialize our media structures. This may probe the MII, if
347 * present.
348 */
349 (*sc->sc_mediasw->tmsw_init)(sc);
350
351 ifp = &sc->sc_ethercom.ec_if;
352 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
353 ifp->if_softc = sc;
354 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
355 ifp->if_ioctl = tlp_ioctl;
356 ifp->if_start = tlp_start;
357 ifp->if_watchdog = tlp_watchdog;
358
359 /*
360 * Attach the interface.
361 */
362 if_attach(ifp);
363 ether_ifattach(ifp, enaddr);
364 #if NBPFILTER > 0
365 bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
366 sizeof(struct ether_header));
367 #endif
368
369 /*
370 * Make sure the interface is shutdown during reboot.
371 */
372 sc->sc_sdhook = shutdownhook_establish(tlp_shutdown, sc);
373 if (sc->sc_sdhook == NULL)
374 printf("%s: WARNING: unable to establish shutdown hook\n",
375 sc->sc_dev.dv_xname);
376 return;
377
378 /*
379 * Free any resources we've allocated during the failed attach
380 * attempt. Do this in reverse order and fall through.
381 */
382 fail_5:
383 for (i = 0; i < TULIP_NRXDESC; i++) {
384 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
385 bus_dmamap_destroy(sc->sc_dmat,
386 sc->sc_rxsoft[i].rxs_dmamap);
387 }
388 fail_4:
389 for (i = 0; i < TULIP_TXQUEUELEN; i++) {
390 if (sc->sc_txsoft[i].txs_dmamap != NULL)
391 bus_dmamap_destroy(sc->sc_dmat,
392 sc->sc_txsoft[i].txs_dmamap);
393 }
394 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
395 fail_3:
396 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
397 fail_2:
398 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
399 sizeof(struct tulip_control_data));
400 fail_1:
401 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
402 fail_0:
403 return;
404 }
405
406 /*
407 * tlp_shutdown:
408 *
409 * Make sure the interface is stopped at reboot time.
410 */
411 void
412 tlp_shutdown(arg)
413 void *arg;
414 {
415 struct tulip_softc *sc = arg;
416
417 tlp_stop(sc, 1);
418 }
419
420 /*
421 * tlp_start: [ifnet interface function]
422 *
423 * Start packet transmission on the interface.
424 */
425 void
426 tlp_start(ifp)
427 struct ifnet *ifp;
428 {
429 struct tulip_softc *sc = ifp->if_softc;
430 struct mbuf *m0, *m;
431 struct tulip_txsoft *txs, *last_txs;
432 bus_dmamap_t dmamap;
433 int error, firsttx, nexttx, lasttx, ofree, seg;
434
435 DPRINTF(sc, ("%s: tlp_start: sc_flags 0x%08x, if_flags 0x%08x\n",
436 sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
437
438 /*
439 * If we want a filter setup, it means no more descriptors were
440 * available for the setup routine. Let it get a chance to wedge
441 * itself into the ring.
442 */
443 if (sc->sc_flags & TULIPF_WANT_SETUP)
444 ifp->if_flags |= IFF_OACTIVE;
445
446 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
447 return;
448
449 /*
450 * Remember the previous number of free descriptors and
451 * the first descriptor we'll use.
452 */
453 ofree = sc->sc_txfree;
454 firsttx = sc->sc_txnext;
455
456 DPRINTF(sc, ("%s: tlp_start: txfree %d, txnext %d\n",
457 sc->sc_dev.dv_xname, ofree, firsttx));
458
459 /*
460 * Loop through the send queue, setting up transmit descriptors
461 * until we drain the queue, or use up all available transmit
462 * descriptors.
463 */
464 while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
465 sc->sc_txfree != 0) {
466 /*
467 * Grab a packet off the queue.
468 */
469 IF_DEQUEUE(&ifp->if_snd, m0);
470 if (m0 == NULL)
471 break;
472
473 dmamap = txs->txs_dmamap;
474
475 /*
476 * Load the DMA map. If this fails, the packet either
477 * didn't fit in the alloted number of segments, or we were
478 * short on resources. In this case, we'll copy and try
479 * again.
480 */
481 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
482 BUS_DMA_NOWAIT) != 0) {
483 MGETHDR(m, M_DONTWAIT, MT_DATA);
484 if (m == NULL) {
485 printf("%s: unable to allocate Tx mbuf\n",
486 sc->sc_dev.dv_xname);
487 IF_PREPEND(&ifp->if_snd, m0);
488 break;
489 }
490 if (m0->m_pkthdr.len > MHLEN) {
491 MCLGET(m, M_DONTWAIT);
492 if ((m->m_flags & M_EXT) == 0) {
493 printf("%s: unable to allocate Tx "
494 "cluster\n", sc->sc_dev.dv_xname);
495 m_freem(m);
496 IF_PREPEND(&ifp->if_snd, m0);
497 break;
498 }
499 }
500 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
501 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
502 m_freem(m0);
503 m0 = m;
504 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
505 m0, BUS_DMA_NOWAIT);
506 if (error) {
507 printf("%s: unable to load Tx buffer, "
508 "error = %d\n", sc->sc_dev.dv_xname, error);
509 IF_PREPEND(&ifp->if_snd, m0);
510 break;
511 }
512 }
513
514 /*
515 * Ensure we have enough descriptors free to describe
516 * the packet.
517 */
518 if (dmamap->dm_nsegs > sc->sc_txfree) {
519 /*
520 * Not enough free descriptors to transmit this
521 * packet. We haven't committed to anything yet,
522 * so just unload the DMA map, put the packet
523 * back on the queue, and punt. Notify the upper
524 * layer that there are no more slots left.
525 *
526 * XXX We could allocate an mbuf and copy, but
527 * XXX it is worth it?
528 */
529 ifp->if_flags |= IFF_OACTIVE;
530 bus_dmamap_unload(sc->sc_dmat, dmamap);
531 IF_PREPEND(&ifp->if_snd, m0);
532 break;
533 }
534
535 /*
536 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
537 */
538
539 /* Sync the DMA map. */
540 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
541 BUS_DMASYNC_PREWRITE);
542
543 /*
544 * Initialize the transmit descriptors.
545 */
546 for (nexttx = sc->sc_txnext, seg = 0;
547 seg < dmamap->dm_nsegs;
548 seg++, nexttx = TULIP_NEXTTX(nexttx)) {
549 /*
550 * If this is the first descriptor we're
551 * enqueueing, don't set the OWN bit just
552 * yet. That could cause a race condition.
553 * We'll do it below.
554 */
555 sc->sc_txdescs[nexttx].td_status =
556 (nexttx == firsttx) ? 0 : TDSTAT_OWN;
557 sc->sc_txdescs[nexttx].td_bufaddr1 =
558 dmamap->dm_segs[seg].ds_addr;
559 sc->sc_txdescs[nexttx].td_ctl =
560 (dmamap->dm_segs[seg].ds_len << TDCTL_SIZE1_SHIFT) |
561 TDCTL_CH;
562 lasttx = nexttx;
563 }
564
565 /* Set `first segment' and `last segment' appropriately. */
566 sc->sc_txdescs[sc->sc_txnext].td_ctl |= TDCTL_Tx_FS;
567 sc->sc_txdescs[lasttx].td_ctl |= TDCTL_Tx_LS;
568
569 #ifdef TLP_DEBUG
570 if (ifp->if_flags & IFF_DEBUG) {
571 printf(" txsoft %p trainsmit chain:\n", txs);
572 for (seg = sc->sc_txnext;; seg = TULIP_NEXTTX(seg)) {
573 printf(" descriptor %d:\n", seg);
574 printf(" td_status: 0x%08x\n",
575 sc->sc_txdescs[seg].td_status);
576 printf(" td_ctl: 0x%08x\n",
577 sc->sc_txdescs[seg].td_ctl);
578 printf(" td_bufaddr1: 0x%08x\n",
579 sc->sc_txdescs[seg].td_bufaddr1);
580 printf(" td_bufaddr2: 0x%08x\n",
581 sc->sc_txdescs[seg].td_bufaddr2);
582 if (seg == lasttx)
583 break;
584 }
585 }
586 #endif
587
588 /* Sync the descriptors we're using. */
589 TULIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
590 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
591
592 /*
593 * Store a pointer to the packet so we can free it later,
594 * and remember what txdirty will be once the packet is
595 * done.
596 */
597 txs->txs_mbuf = m0;
598 txs->txs_firstdesc = sc->sc_txnext;
599 txs->txs_lastdesc = lasttx;
600
601 /* Advance the tx pointer. */
602 sc->sc_txfree -= dmamap->dm_nsegs;
603 sc->sc_txnext = nexttx;
604
605 SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
606 SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
607
608 last_txs = txs;
609
610 #if NBPFILTER > 0
611 /*
612 * Pass the packet to any BPF listeners.
613 */
614 if (ifp->if_bpf)
615 bpf_mtap(ifp->if_bpf, m0);
616 #endif /* NBPFILTER > 0 */
617 }
618
619 if (txs == NULL || sc->sc_txfree == 0) {
620 /* No more slots left; notify upper layer. */
621 ifp->if_flags |= IFF_OACTIVE;
622 }
623
624 if (sc->sc_txfree != ofree) {
625 DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
626 sc->sc_dev.dv_xname, lasttx, firsttx));
627 /*
628 * Cause a transmit interrupt to happen on the
629 * last packet we enqueued.
630 */
631 sc->sc_txdescs[lasttx].td_ctl |= TDCTL_Tx_IC;
632 TULIP_CDTXSYNC(sc, lasttx, 1,
633 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
634
635 /*
636 * Some clone chips want IC on the *first* segment in
637 * the packet. Appease them.
638 */
639 if ((sc->sc_flags & TULIPF_IC_FS) != 0 &&
640 last_txs->txs_firstdesc != lasttx) {
641 sc->sc_txdescs[last_txs->txs_firstdesc].td_ctl |=
642 TDCTL_Tx_IC;
643 TULIP_CDTXSYNC(sc, last_txs->txs_firstdesc, 1,
644 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
645 }
646
647 /*
648 * The entire packet chain is set up. Give the
649 * first descriptor to the chip now.
650 */
651 sc->sc_txdescs[firsttx].td_status |= TDSTAT_OWN;
652 TULIP_CDTXSYNC(sc, firsttx, 1,
653 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
654
655 /* Wake up the transmitter. */
656 /* XXX USE AUTOPOLLING? */
657 TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
658
659 /* Set a watchdog timer in case the chip flakes out. */
660 ifp->if_timer = 5;
661 }
662 }
663
664 /*
665 * tlp_watchdog: [ifnet interface function]
666 *
667 * Watchdog timer handler.
668 */
669 void
670 tlp_watchdog(ifp)
671 struct ifnet *ifp;
672 {
673 struct tulip_softc *sc = ifp->if_softc;
674 int doing_setup, doing_transmit;
675
676 doing_setup = (sc->sc_flags & TULIPF_DOING_SETUP);
677 doing_transmit = (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL);
678
679 if (doing_setup && doing_transmit) {
680 printf("%s: filter setup and transmit timeout\n",
681 sc->sc_dev.dv_xname);
682 ifp->if_oerrors++;
683 } else if (doing_transmit) {
684 printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
685 ifp->if_oerrors++;
686 } else if (doing_setup)
687 printf("%s: filter setup timeout\n", sc->sc_dev.dv_xname);
688 else
689 printf("%s: spurious watchdog timeout\n", sc->sc_dev.dv_xname);
690
691 (void) tlp_init(sc);
692
693 /* Try to get more packets going. */
694 tlp_start(ifp);
695 }
696
697 /*
698 * tlp_ioctl: [ifnet interface function]
699 *
700 * Handle control requests from the operator.
701 */
702 int
703 tlp_ioctl(ifp, cmd, data)
704 struct ifnet *ifp;
705 u_long cmd;
706 caddr_t data;
707 {
708 struct tulip_softc *sc = ifp->if_softc;
709 struct ifreq *ifr = (struct ifreq *)data;
710 struct ifaddr *ifa = (struct ifaddr *)data;
711 int s, error = 0;
712
713 s = splnet();
714
715 switch (cmd) {
716 case SIOCSIFADDR:
717 ifp->if_flags |= IFF_UP;
718
719 switch (ifa->ifa_addr->sa_family) {
720 #ifdef INET
721 case AF_INET:
722 if ((error = tlp_init(sc)) != 0)
723 break;
724 arp_ifinit(ifp, ifa);
725 break;
726 #endif /* INET */
727 #ifdef NS
728 case AF_NS:
729 {
730 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
731
732 if (ns_nullhost(*ina))
733 ina->x_host = *(union ns_host *)
734 LLADDR(ifp->if_sadl);
735 else
736 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
737 ifp->if_addrlen);
738 /* Set new address. */
739 error = tlp_init(sc);
740 break;
741 }
742 #endif /* NS */
743 default:
744 error = tlp_init(sc);
745 break;
746 }
747 break;
748
749 case SIOCSIFMTU:
750 if (ifr->ifr_mtu > ETHERMTU)
751 error = EINVAL;
752 else
753 ifp->if_mtu = ifr->ifr_mtu;
754 break;
755
756 case SIOCSIFFLAGS:
757 if ((ifp->if_flags & IFF_UP) == 0 &&
758 (ifp->if_flags & IFF_RUNNING) != 0) {
759 /*
760 * If interface is marked down and it is running, then
761 * stop it.
762 */
763 tlp_stop(sc, 1);
764 } else if ((ifp->if_flags & IFF_UP) != 0 &&
765 (ifp->if_flags & IFF_RUNNING) == 0) {
766 /*
767 * If interfase it marked up and it is stopped, then
768 * start it.
769 */
770 error = tlp_init(sc);
771 } else if ((ifp->if_flags & IFF_UP) != 0) {
772 /*
773 * Reset the interface to pick up changes in any other
774 * flags that affect the hardware state.
775 */
776 error = tlp_init(sc);
777 }
778 break;
779
780 case SIOCADDMULTI:
781 case SIOCDELMULTI:
782 error = (cmd == SIOCADDMULTI) ?
783 ether_addmulti(ifr, &sc->sc_ethercom) :
784 ether_delmulti(ifr, &sc->sc_ethercom);
785
786 if (error == ENETRESET) {
787 /*
788 * Multicast list has changed. Set the filter
789 * accordingly.
790 */
791 (*sc->sc_filter_setup)(sc);
792 error = 0;
793 }
794 break;
795
796 case SIOCSIFMEDIA:
797 case SIOCGIFMEDIA:
798 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
799 break;
800
801 default:
802 error = EINVAL;
803 break;
804 }
805
806 /* Try to get more packets going. */
807 tlp_start(ifp);
808
809 splx(s);
810 return (error);
811 }
812
813 /*
814 * tlp_intr:
815 *
816 * Interrupt service routine.
817 */
818 int
819 tlp_intr(arg)
820 void *arg;
821 {
822 struct tulip_softc *sc = arg;
823 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
824 u_int32_t status, rxstatus, txstatus;
825 int handled = 0, txthresh;
826
827 DPRINTF(sc, ("%s: tlp_intr\n", sc->sc_dev.dv_xname));
828
829 /*
830 * If the interface isn't running, the interrupt couldn't
831 * possibly have come from us.
832 */
833 if ((ifp->if_flags & IFF_RUNNING) == 0)
834 return (0);
835
836 for (;;) {
837 status = TULIP_READ(sc, CSR_STATUS);
838 if (status)
839 TULIP_WRITE(sc, CSR_STATUS, status);
840
841 if ((status & sc->sc_inten) == 0)
842 break;
843
844 handled = 1;
845
846 rxstatus = status & sc->sc_rxint_mask;
847 txstatus = status & sc->sc_txint_mask;
848
849 if (rxstatus) {
850 /* Grab new any new packets. */
851 tlp_rxintr(sc);
852
853 if (rxstatus & STATUS_RWT)
854 printf("%s: receive watchdog timeout\n",
855 sc->sc_dev.dv_xname);
856
857 if (rxstatus & STATUS_RU) {
858 printf("%s: receive ring overrun\n",
859 sc->sc_dev.dv_xname);
860 /* Get the receive process going again. */
861 tlp_idle(sc, OPMODE_SR);
862 TULIP_WRITE(sc, CSR_RXLIST,
863 TULIP_CDRXADDR(sc, sc->sc_rxptr));
864 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
865 TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
866 break;
867 }
868 }
869
870 if (txstatus) {
871 /* Sweep up transmit descriptors. */
872 tlp_txintr(sc);
873
874 if (txstatus & STATUS_TJT)
875 printf("%s: transmit jabber timeout\n",
876 sc->sc_dev.dv_xname);
877
878 if (txstatus & STATUS_UNF) {
879 /*
880 * Increase our transmit threshold if
881 * another is available.
882 */
883 txthresh = sc->sc_txthresh + 1;
884 if (sc->sc_txth[txthresh].txth_name != NULL) {
885 /* Idle the transmit process. */
886 tlp_idle(sc, OPMODE_ST);
887
888 sc->sc_txthresh = txthresh;
889 sc->sc_opmode &= ~(OPMODE_TR|OPMODE_SF);
890 sc->sc_opmode |=
891 sc->sc_txth[txthresh].txth_opmode;
892 printf("%s: transmit underrun; new "
893 "threshold: %s\n",
894 sc->sc_dev.dv_xname,
895 sc->sc_txth[txthresh].txth_name);
896
897 /*
898 * Set the new threshold and restart
899 * the transmit process.
900 */
901 TULIP_WRITE(sc, CSR_OPMODE,
902 sc->sc_opmode);
903 }
904 /*
905 * XXX Log every Nth underrun from
906 * XXX now on?
907 */
908 }
909 }
910
911 if (status & (STATUS_TPS|STATUS_RPS)) {
912 if (status & STATUS_TPS)
913 printf("%s: transmit process stopped\n",
914 sc->sc_dev.dv_xname);
915 if (status & STATUS_RPS)
916 printf("%s: receive process stopped\n",
917 sc->sc_dev.dv_xname);
918 (void) tlp_init(sc);
919 break;
920 }
921
922 if (status & STATUS_SE) {
923 const char *str;
924 switch (status & STATUS_EB) {
925 case STATUS_EB_PARITY:
926 str = "parity error";
927 break;
928
929 case STATUS_EB_MABT:
930 str = "master abort";
931 break;
932
933 case STATUS_EB_TABT:
934 str = "target abort";
935 break;
936
937 default:
938 str = "unknown error";
939 break;
940 }
941 printf("%s: fatal system error: %s\n",
942 sc->sc_dev.dv_xname, str);
943 (void) tlp_init(sc);
944 break;
945 }
946
947 /*
948 * Not handled:
949 *
950 * Transmit buffer unavailable -- normal
951 * condition, nothing to do, really.
952 *
953 * General purpose timer experied -- we don't
954 * use the general purpose timer.
955 *
956 * Early receive interrupt -- not available on
957 * all chips, we just use RI. We also only
958 * use single-segment receive DMA, so this
959 * is mostly useless.
960 */
961 }
962
963 /* Try to get more packets going. */
964 tlp_start(ifp);
965
966 return (handled);
967 }
968
969 /*
970 * tlp_rxintr:
971 *
972 * Helper; handle receive interrupts.
973 */
974 void
975 tlp_rxintr(sc)
976 struct tulip_softc *sc;
977 {
978 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
979 struct ether_header *eh;
980 struct tulip_rxsoft *rxs;
981 struct mbuf *m;
982 u_int32_t rxstat;
983 int i, len;
984
985 for (i = sc->sc_rxptr;; i = TULIP_NEXTRX(i)) {
986 rxs = &sc->sc_rxsoft[i];
987
988 TULIP_CDRXSYNC(sc, i,
989 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
990
991 rxstat = sc->sc_rxdescs[i].td_status;
992
993 if (rxstat & TDSTAT_OWN) {
994 /*
995 * We have processed all of the receive buffers.
996 */
997 break;
998 }
999
1000 /*
1001 * Make sure the packet fit in one buffer. This should
1002 * always be the case. But the Lite-On PNIC, rev 33
1003 * has an awful receive engine bug, which may require
1004 * a very icky work-around.
1005 */
1006 if ((rxstat & (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) !=
1007 (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) {
1008 printf("%s: incoming packet spilled, resetting\n",
1009 sc->sc_dev.dv_xname);
1010 (void) tlp_init(sc);
1011 return;
1012 }
1013
1014 /*
1015 * If any collisions were seen on the wire, count one.
1016 */
1017 if (rxstat & TDSTAT_Rx_CS)
1018 ifp->if_collisions++;
1019
1020 /*
1021 * If an error occured, update stats, clear the status
1022 * word, and leave the packet buffer in place. It will
1023 * simply be reused the next time the ring comes around.
1024 */
1025 if (rxstat & TDSTAT_ES) {
1026 #define PRINTERR(bit, str) \
1027 if (rxstat & (bit)) \
1028 printf("%s: receive error: %s\n", \
1029 sc->sc_dev.dv_xname, str)
1030 ifp->if_ierrors++;
1031 PRINTERR(TDSTAT_Rx_DE, "descriptor error");
1032 PRINTERR(TDSTAT_Rx_RF, "runt frame");
1033 PRINTERR(TDSTAT_Rx_TL, "frame too long");
1034 PRINTERR(TDSTAT_Rx_RE, "MII error");
1035 PRINTERR(TDSTAT_Rx_DB, "dribbling bit");
1036 PRINTERR(TDSTAT_Rx_CE, "CRC error");
1037 #undef PRINTERR
1038 TULIP_INIT_RXDESC(sc, i);
1039 continue;
1040 }
1041
1042 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1043 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1044
1045 /*
1046 * No errors; receive the packet. Note the Tulip
1047 * includes the CRC with every packet; trim it.
1048 */
1049 len = TDSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;
1050
1051 #ifdef __NO_STRICT_ALIGNMENT
1052 /*
1053 * Allocate a new mbuf cluster. If that fails, we are
1054 * out of memory, and must drop the packet and recycle
1055 * the buffer that's already attached to this descriptor.
1056 */
1057 m = rxs->rxs_mbuf;
1058 if (tlp_add_rxbuf(sc, i) != 0) {
1059 ifp->if_ierrors++;
1060 TULIP_INIT_RXDESC(sc, i);
1061 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1062 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1063 continue;
1064 }
1065 #else
1066 /*
1067 * The Tulip's receive buffers must be 4-byte aligned.
1068 * But this means that the data after the Ethernet header
1069 * is misaligned. We must allocate a new buffer and
1070 * copy the data, shifted forward 2 bytes.
1071 */
1072 MGETHDR(m, M_DONTWAIT, MT_DATA);
1073 if (m == NULL) {
1074 dropit:
1075 ifp->if_ierrors++;
1076 TULIP_INIT_RXDESC(sc, i);
1077 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1078 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1079 continue;
1080 }
1081 if (len > (MHLEN - 2)) {
1082 MCLGET(m, M_DONTWAIT);
1083 if ((m->m_flags & M_EXT) == 0) {
1084 m_freem(m);
1085 goto dropit;
1086 }
1087 }
1088 m->m_data += 2;
1089
1090 /*
1091 * Note that we use clusters for incoming frames, so the
1092 * buffer is virtually contiguous.
1093 */
1094 memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
1095
1096 /* Allow the receive descriptor to continue using its mbuf. */
1097 TULIP_INIT_RXDESC(sc, i);
1098 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1099 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1100 #endif /* __NO_STRICT_ALIGNMENT */
1101
1102 ifp->if_ipackets++;
1103 eh = mtod(m, struct ether_header *);
1104 m->m_pkthdr.rcvif = ifp;
1105 m->m_pkthdr.len = m->m_len = len;
1106
1107 #if NBPFILTER > 0
1108 /*
1109 * Pass this up to any BPF listeners, but only
1110 * pass it up the stack if its for us.
1111 */
1112 if (ifp->if_bpf)
1113 bpf_mtap(ifp->if_bpf, m);
1114 #endif /* NPBFILTER > 0 */
1115
1116 /*
1117 * This test is outside the NBPFILTER block because
1118 * on the 21140 we have to use Hash-Only mode due to
1119 * a bug in the filter logic.
1120 */
1121 if ((ifp->if_flags & IFF_PROMISC) != 0 ||
1122 sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
1123 if (memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
1124 ETHER_ADDR_LEN) != 0 &&
1125 ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
1126 m_freem(m);
1127 continue;
1128 }
1129 }
1130
1131 /* Pass it on. */
1132 (*ifp->if_input)(ifp, m);
1133 }
1134
1135 /* Update the recieve pointer. */
1136 sc->sc_rxptr = i;
1137 }
1138
1139 /*
1140 * tlp_txintr:
1141 *
1142 * Helper; handle transmit interrupts.
1143 */
1144 void
1145 tlp_txintr(sc)
1146 struct tulip_softc *sc;
1147 {
1148 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1149 struct tulip_txsoft *txs;
1150 u_int32_t txstat;
1151
1152 DPRINTF(sc, ("%s: tlp_txintr: sc_flags 0x%08x\n",
1153 sc->sc_dev.dv_xname, sc->sc_flags));
1154
1155 ifp->if_flags &= ~IFF_OACTIVE;
1156
1157 /*
1158 * If we were doing a filter setup, check to see if it completed.
1159 */
1160 if (sc->sc_flags & TULIPF_DOING_SETUP) {
1161 TULIP_CDSDSYNC(sc, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1162 if ((sc->sc_setup_desc.td_status & TDSTAT_OWN) == 0)
1163 sc->sc_flags &= ~TULIPF_DOING_SETUP;
1164 }
1165
1166 /*
1167 * Go through our Tx list and free mbufs for those
1168 * frames that have been transmitted.
1169 */
1170 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1171 TULIP_CDTXSYNC(sc, txs->txs_firstdesc,
1172 txs->txs_dmamap->dm_nsegs,
1173 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1174
1175 #ifdef TLP_DEBUG
1176 if (ifp->if_flags & IFF_DEBUG) {
1177 int i;
1178 printf(" txsoft %p trainsmit chain:\n", txs);
1179 for (i = txs->txs_firstdesc;; i = TULIP_NEXTTX(i)) {
1180 printf(" descriptor %d:\n", i);
1181 printf(" td_status: 0x%08x\n",
1182 sc->sc_txdescs[i].td_status);
1183 printf(" td_ctl: 0x%08x\n",
1184 sc->sc_txdescs[i].td_ctl);
1185 printf(" td_bufaddr1: 0x%08x\n",
1186 sc->sc_txdescs[i].td_bufaddr1);
1187 printf(" td_bufaddr2: 0x%08x\n",
1188 sc->sc_txdescs[i].td_bufaddr2);
1189 if (i == txs->txs_lastdesc)
1190 break;
1191 }
1192 }
1193 #endif
1194
1195 txstat = sc->sc_txdescs[txs->txs_firstdesc].td_status;
1196 if (txstat & TDSTAT_OWN)
1197 break;
1198
1199 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1200
1201 sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
1202
1203 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1204 0, txs->txs_dmamap->dm_mapsize,
1205 BUS_DMASYNC_POSTWRITE);
1206 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1207 m_freem(txs->txs_mbuf);
1208 txs->txs_mbuf = NULL;
1209
1210 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1211
1212 /*
1213 * Check for errors and collisions.
1214 */
1215 if (txstat &
1216 (TDSTAT_Tx_UF|TDSTAT_Tx_NC|TDSTAT_Tx_LO|TDSTAT_Tx_TO)) {
1217 ifp->if_oerrors++;
1218 #if 0
1219 /*
1220 * XXX Can't check for late or excessive collisions;
1221 * XXX Some 21040s seem to register those even on
1222 * XXX successful transmissions!
1223 */
1224 if (txstat & TDSTAT_Tx_EC)
1225 ifp->if_collisions += 16;
1226 if (txstat & TDSTAT_Tx_LC)
1227 ifp->if_collisions++;
1228 #endif
1229 } else {
1230 /* Packet was transmitted successfully. */
1231 ifp->if_opackets++;
1232 ifp->if_collisions += TDSTAT_Tx_COLLISIONS(txstat);
1233 }
1234 }
1235
1236 /*
1237 * If there are no more pending transmissions, cancel the watchdog
1238 * timer.
1239 */
1240 if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0)
1241 ifp->if_timer = 0;
1242
1243 /*
1244 * If we have a receive filter setup pending, do it now.
1245 */
1246 if (sc->sc_flags & TULIPF_WANT_SETUP)
1247 (*sc->sc_filter_setup)(sc);
1248 }
1249
1250 /*
1251 * tlp_reset:
1252 *
1253 * Perform a soft reset on the Tulip.
1254 */
1255 void
1256 tlp_reset(sc)
1257 struct tulip_softc *sc;
1258 {
1259 int i;
1260
1261 TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
1262
1263 for (i = 0; i < 1000; i++) {
1264 /*
1265 * Wait at least 50 PCI cycles for the reset to
1266 * complete before peeking at the Tulip again.
1267 * 10 uSec is a bit longer than 50 PCI cycles
1268 * (at 33MHz), but it doesn't hurt have the extra
1269 * wait.
1270 */
1271 delay(10);
1272 if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
1273 break;
1274 }
1275
1276 if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
1277 printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
1278
1279 delay(1000);
1280 }
1281
1282 /*
1283 * tlp_init:
1284 *
1285 * Initialize the interface. Must be called at splnet().
1286 */
1287 int
1288 tlp_init(sc)
1289 struct tulip_softc *sc;
1290 {
1291 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1292 struct tulip_txsoft *txs;
1293 struct tulip_rxsoft *rxs;
1294 int i, error = 0;
1295
1296 /*
1297 * Cancel any pending I/O.
1298 */
1299 tlp_stop(sc, 0);
1300
1301 /*
1302 * Reset the Tulip to a known state.
1303 */
1304 tlp_reset(sc);
1305
1306 /*
1307 * Initialize the BUSMODE register.
1308 *
1309 * XXX What about read-multiple/read-line/write-line on
1310 * XXX the 21140 and up?
1311 */
1312 sc->sc_busmode = BUSMODE_BAR | BUSMODE_PBL_DEFAULT;
1313 switch (sc->sc_cacheline) {
1314 default:
1315 /*
1316 * Note: We must *always* set these bits; a cache
1317 * alignment of 0 is RESERVED.
1318 */
1319 case 8:
1320 sc->sc_busmode |= BUSMODE_CAL_8LW;
1321 break;
1322 case 16:
1323 sc->sc_busmode |= BUSMODE_CAL_16LW;
1324 break;
1325 case 32:
1326 sc->sc_busmode |= BUSMODE_CAL_32LW;
1327 break;
1328 }
1329 switch (sc->sc_chip) {
1330 case TULIP_CHIP_82C168:
1331 case TULIP_CHIP_82C169:
1332 sc->sc_busmode |= BUSMODE_PNIC_MBO;
1333 break;
1334 default:
1335 /* Nothing. */
1336 break;
1337 }
1338 #if BYTE_ORDER == BIG_ENDIAN
1339 /*
1340 * XXX There are reports that this doesn't work properly
1341 * in the old Tulip driver, but BUSMODE_DBO does. However,
1342 * BUSMODE_DBO is not available on the 21040, and requires
1343 * us to byte-swap the setup packet. What to do?
1344 */
1345 sc->sc_busmode |= BUSMODE_BLE;
1346 #endif
1347 TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);
1348
1349 /*
1350 * Initialize the OPMODE register. We don't write it until
1351 * we're ready to begin the transmit and receive processes.
1352 *
1353 * Media-related OPMODE bits are set in the media callbacks
1354 * for each specific chip/board.
1355 */
1356 sc->sc_opmode = OPMODE_SR | OPMODE_ST |
1357 sc->sc_txth[sc->sc_txthresh].txth_opmode;
1358 switch (sc->sc_chip) {
1359 case TULIP_CHIP_21140:
1360 case TULIP_CHIP_21140A:
1361 case TULIP_CHIP_21142:
1362 case TULIP_CHIP_21143:
1363 sc->sc_opmode |= OPMODE_MBO;
1364 break;
1365
1366 default:
1367 /* Nothing. */
1368 }
1369
1370 if (sc->sc_flags & TULIPF_HAS_MII) {
1371 switch (sc->sc_chip) {
1372 case TULIP_CHIP_82C168:
1373 case TULIP_CHIP_82C169:
1374 /* Enable the MII port. */
1375 sc->sc_opmode |= OPMODE_PS;
1376
1377 TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
1378 break;
1379
1380 case TULIP_CHIP_WB89C840F:
1381 /* Nothing. */
1382 break;
1383
1384 default:
1385 /* Enable the MII port. */
1386 sc->sc_opmode |= OPMODE_PS;
1387 break;
1388 }
1389 } else {
1390 switch (sc->sc_chip) {
1391 case TULIP_CHIP_82C168:
1392 case TULIP_CHIP_82C169:
1393 sc->sc_opmode |= OPMODE_PNIC_TBEN;
1394 break;
1395
1396 default:
1397 /* Nothing. */
1398 }
1399 }
1400
1401 /*
1402 * Magical mystery initialization on the Macronix chips.
1403 * The MX98713 uses its own magic value, the rest share
1404 * a common one.
1405 */
1406 switch (sc->sc_chip) {
1407 case TULIP_CHIP_MX98713:
1408 TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
1409 break;
1410
1411 case TULIP_CHIP_MX98713A:
1412 case TULIP_CHIP_MX98715:
1413 case TULIP_CHIP_MX98725:
1414 TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
1415 break;
1416
1417 default:
1418 /* Nothing. */
1419 }
1420
1421 /*
1422 * Initialize the transmit descriptor ring.
1423 */
1424 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1425 for (i = 0; i < TULIP_NTXDESC; i++) {
1426 sc->sc_txdescs[i].td_ctl = TDCTL_CH;
1427 sc->sc_txdescs[i].td_bufaddr2 =
1428 TULIP_CDTXADDR(sc, TULIP_NEXTTX(i));
1429 }
1430 TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
1431 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1432 sc->sc_txfree = TULIP_NTXDESC;
1433 sc->sc_txnext = 0;
1434
1435 /*
1436 * Initialize the transmit job descriptors.
1437 */
1438 SIMPLEQ_INIT(&sc->sc_txfreeq);
1439 SIMPLEQ_INIT(&sc->sc_txdirtyq);
1440 for (i = 0; i < TULIP_TXQUEUELEN; i++) {
1441 txs = &sc->sc_txsoft[i];
1442 txs->txs_mbuf = NULL;
1443 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1444 }
1445
1446 /*
1447 * Initialize the receive descriptor and receive job
1448 * descriptor rings.
1449 */
1450 for (i = 0; i < TULIP_NRXDESC; i++) {
1451 rxs = &sc->sc_rxsoft[i];
1452 if (rxs->rxs_mbuf == NULL) {
1453 if ((error = tlp_add_rxbuf(sc, i)) != 0) {
1454 printf("%s: unable to allocate or map rx "
1455 "buffer %d, error = %d\n",
1456 sc->sc_dev.dv_xname, i, error);
1457 /*
1458 * XXX Should attempt to run with fewer receive
1459 * XXX buffers instead of just failing.
1460 */
1461 tlp_rxdrain(sc);
1462 goto out;
1463 }
1464 }
1465 }
1466 sc->sc_rxptr = 0;
1467
1468 /*
1469 * Initialize the interrupt mask and enable interrupts.
1470 */
1471 /* normal interrupts */
1472 sc->sc_inten = STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
1473
1474 /* abnormal interrupts */
1475 sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
1476 STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;
1477
1478 sc->sc_rxint_mask = STATUS_RI|STATUS_RU|STATUS_RWT;
1479 sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
1480
1481 switch (sc->sc_chip) {
1482 case TULIP_CHIP_WB89C840F:
1483 /*
1484 * Clear bits that we don't want that happen to
1485 * overlap or don't exist.
1486 */
1487 sc->sc_inten &= ~(STATUS_WINB_REI|STATUS_RWT);
1488 break;
1489
1490 default:
1491 /* Nothing. */
1492 }
1493
1494 sc->sc_rxint_mask &= sc->sc_inten;
1495 sc->sc_txint_mask &= sc->sc_inten;
1496
1497 TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
1498 TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);
1499
1500 /*
1501 * Give the transmit and receive rings to the Tulip.
1502 */
1503 TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
1504 TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));
1505
1506 /*
1507 * On chips that do this differently, set the station address.
1508 */
1509 switch (sc->sc_chip) {
1510 case TULIP_CHIP_WB89C840F:
1511 {
1512 /* XXX Do this with stream writes? */
1513 bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0);
1514
1515 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1516 bus_space_write_1(sc->sc_st, sc->sc_sh,
1517 cpa + i, LLADDR(ifp->if_sadl)[i]);
1518 }
1519 break;
1520 }
1521
1522 default:
1523 /* Nothing. */
1524 }
1525
1526 /*
1527 * Set the receive filter. This will start the transmit and
1528 * receive processes.
1529 */
1530 (*sc->sc_filter_setup)(sc);
1531
1532 /*
1533 * Start the receive process.
1534 */
1535 TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
1536
1537 if (sc->sc_tick != NULL) {
1538 /* Start the one second clock. */
1539 timeout(sc->sc_tick, sc, hz);
1540 }
1541
1542 /*
1543 * Note that the interface is now running.
1544 */
1545 ifp->if_flags |= IFF_RUNNING;
1546 ifp->if_flags &= ~IFF_OACTIVE;
1547
1548 /*
1549 * Set the media. We must do this after the transmit process is
1550 * running, since we may actually have to transmit packets on
1551 * our board to test link integrity.
1552 */
1553 (void) (*sc->sc_mediasw->tmsw_set)(sc);
1554
1555 out:
1556 if (error)
1557 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1558 return (error);
1559 }
1560
1561 /*
1562 * tlp_rxdrain:
1563 *
1564 * Drain the receive queue.
1565 */
1566 void
1567 tlp_rxdrain(sc)
1568 struct tulip_softc *sc;
1569 {
1570 struct tulip_rxsoft *rxs;
1571 int i;
1572
1573 for (i = 0; i < TULIP_NRXDESC; i++) {
1574 rxs = &sc->sc_rxsoft[i];
1575 if (rxs->rxs_mbuf != NULL) {
1576 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1577 m_freem(rxs->rxs_mbuf);
1578 rxs->rxs_mbuf = NULL;
1579 }
1580 }
1581 }
1582
1583 /*
1584 * tlp_stop:
1585 *
1586 * Stop transmission on the interface.
1587 */
1588 void
1589 tlp_stop(sc, drain)
1590 struct tulip_softc *sc;
1591 int drain;
1592 {
1593 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1594 struct tulip_txsoft *txs;
1595
1596 if (sc->sc_tick != NULL) {
1597 /* Stop the one second clock. */
1598 untimeout(sc->sc_tick, sc);
1599 }
1600
1601 /* Disable interrupts. */
1602 TULIP_WRITE(sc, CSR_INTEN, 0);
1603
1604 /* Stop the transmit and receive processes. */
1605 TULIP_WRITE(sc, CSR_OPMODE, 0);
1606 TULIP_WRITE(sc, CSR_RXLIST, 0);
1607 TULIP_WRITE(sc, CSR_TXLIST, 0);
1608
1609 /*
1610 * Release any queued transmit buffers.
1611 */
1612 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1613 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1614 if (txs->txs_mbuf != NULL) {
1615 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1616 m_freem(txs->txs_mbuf);
1617 txs->txs_mbuf = NULL;
1618 }
1619 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1620 }
1621
1622 if (drain) {
1623 /*
1624 * Release the receive buffers.
1625 */
1626 tlp_rxdrain(sc);
1627 }
1628
1629 sc->sc_flags &= ~(TULIPF_WANT_SETUP|TULIPF_DOING_SETUP);
1630
1631 /*
1632 * Mark the interface down and cancel the watchdog timer.
1633 */
1634 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1635 ifp->if_timer = 0;
1636 }
1637
1638 #define SROM_EMIT(sc, x) \
1639 do { \
1640 TULIP_WRITE((sc), CSR_MIIROM, (x)); \
1641 delay(1); \
1642 } while (0)
1643
1644 /*
1645 * tlp_srom_idle:
1646 *
1647 * Put the SROM in idle state.
1648 */
1649 void
1650 tlp_srom_idle(sc)
1651 struct tulip_softc *sc;
1652 {
1653 u_int32_t miirom;
1654 int i;
1655
1656 miirom = MIIROM_SR;
1657 SROM_EMIT(sc, miirom);
1658
1659 miirom |= MIIROM_RD;
1660 SROM_EMIT(sc, miirom);
1661
1662 miirom |= MIIROM_SROMCS;
1663 SROM_EMIT(sc, miirom);
1664
1665 SROM_EMIT(sc, miirom|MIIROM_SROMSK);
1666
1667 /* Strobe the clock 25 times. */
1668 for (i = 0; i < 25; i++) {
1669 SROM_EMIT(sc, miirom);
1670 SROM_EMIT(sc, miirom|MIIROM_SROMSK);
1671 }
1672
1673 SROM_EMIT(sc, miirom);
1674
1675 miirom &= ~MIIROM_SROMCS;
1676 SROM_EMIT(sc, miirom);
1677
1678 SROM_EMIT(sc, 0);
1679 }
1680
1681 /*
1682 * tlp_read_srom:
1683 *
1684 * Read the Tulip SROM.
1685 */
1686 void
1687 tlp_read_srom(sc, word, wordcnt, data)
1688 struct tulip_softc *sc;
1689 int word, wordcnt;
1690 u_int16_t *data;
1691 {
1692 u_int32_t miirom;
1693 int i, x;
1694
1695 tlp_srom_idle(sc);
1696
1697 /* Select the SROM. */
1698 miirom = MIIROM_SR;
1699 SROM_EMIT(sc, miirom);
1700
1701 miirom |= MIIROM_RD;
1702 SROM_EMIT(sc, miirom);
1703
1704 for (i = 0; i < wordcnt; i++) {
1705 /* Send CHIP SELECT for one clock tick. */
1706 miirom |= MIIROM_SROMCS;
1707 SROM_EMIT(sc, miirom);
1708
1709 /* Shift in the READ opcode. */
1710 for (x = 3; x > 0; x--) {
1711 if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
1712 miirom |= MIIROM_SROMDI;
1713 else
1714 miirom &= ~MIIROM_SROMDI;
1715 SROM_EMIT(sc, miirom);
1716 SROM_EMIT(sc, miirom|MIIROM_SROMSK);
1717 SROM_EMIT(sc, miirom);
1718 }
1719
1720 /* Shift in address. */
1721 for (x = 6; x > 0; x--) {
1722 if ((word + i) & (1 << (x - 1)))
1723 miirom |= MIIROM_SROMDI;
1724 else
1725 miirom &= ~MIIROM_SROMDI;
1726 SROM_EMIT(sc, miirom);
1727 SROM_EMIT(sc, miirom|MIIROM_SROMSK);
1728 SROM_EMIT(sc, miirom);
1729 }
1730
1731 /* Shift out data. */
1732 miirom &= ~MIIROM_SROMDI;
1733 data[i] = 0;
1734 for (x = 16; x > 0; x--) {
1735 SROM_EMIT(sc, miirom|MIIROM_SROMSK);
1736 if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
1737 data[i] |= (1 << (x - 1));
1738 SROM_EMIT(sc, miirom);
1739 }
1740
1741 /* Clear CHIP SELECT. */
1742 miirom &= ~MIIROM_SROMCS;
1743 SROM_EMIT(sc, miirom);
1744 }
1745
1746 /* Deselect the SROM. */
1747 SROM_EMIT(sc, 0);
1748
1749 /* ...and idle it. */
1750 tlp_srom_idle(sc);
1751 }
1752
1753 #undef SROM_EMIT
1754
1755 /*
1756 * tlp_add_rxbuf:
1757 *
1758 * Add a receive buffer to the indicated descriptor.
1759 */
1760 int
1761 tlp_add_rxbuf(sc, idx)
1762 struct tulip_softc *sc;
1763 int idx;
1764 {
1765 struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
1766 struct mbuf *m;
1767 int error;
1768
1769 MGETHDR(m, M_DONTWAIT, MT_DATA);
1770 if (m == NULL)
1771 return (ENOBUFS);
1772
1773 MCLGET(m, M_DONTWAIT);
1774 if ((m->m_flags & M_EXT) == 0) {
1775 m_freem(m);
1776 return (ENOBUFS);
1777 }
1778
1779 if (rxs->rxs_mbuf != NULL)
1780 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1781
1782 rxs->rxs_mbuf = m;
1783
1784 error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
1785 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
1786 if (error) {
1787 printf("%s: can't load rx DMA map %d, error = %d\n",
1788 sc->sc_dev.dv_xname, idx, error);
1789 panic("tlp_add_rxbuf"); /* XXX */
1790 }
1791
1792 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1793 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1794
1795 TULIP_INIT_RXDESC(sc, idx);
1796
1797 return (0);
1798 }
1799
1800 /*
1801 * tlp_crc32:
1802 *
1803 * Compute the 32-bit CRC of the provided buffer.
1804 */
1805 u_int32_t
1806 tlp_crc32(buf, len)
1807 const u_int8_t *buf;
1808 size_t len;
1809 {
1810 static const u_int32_t crctab[] = {
1811 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1812 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1813 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1814 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1815 };
1816 u_int32_t crc;
1817 int i;
1818
1819 crc = 0xffffffff;
1820 for (i = 0; i < len; i++) {
1821 crc ^= buf[i];
1822 crc = (crc >> 4) ^ crctab[crc & 0xf];
1823 crc = (crc >> 4) ^ crctab[crc & 0xf];
1824 }
1825 return (crc);
1826 }
1827
1828 /*
1829 * tlp_srom_crcok:
1830 *
1831 * Check the CRC of the Tulip SROM.
1832 */
1833 int
1834 tlp_srom_crcok(romdata)
1835 u_int8_t *romdata;
1836 {
1837 u_int32_t crc;
1838
1839 crc = tlp_crc32(romdata, TULIP_ROM_CRC32_CHECKSUM);
1840 crc = (crc & 0xffff) ^ 0xffff;
1841 if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
1842 return (1);
1843 return (0);
1844 }
1845
1846 /*
1847 * tlp_parse_old_srom:
1848 *
1849 * Parse old-format SROMs.
1850 *
1851 * This routine is largely lifted from Matt Thomas's `de' driver.
1852 */
1853 int
1854 tlp_parse_old_srom(sc, enaddr)
1855 struct tulip_softc *sc;
1856 u_int8_t *enaddr;
1857 {
1858 static const u_int8_t testpat[] =
1859 { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
1860 int i;
1861 u_int32_t cksum;
1862
1863 if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
1864 /*
1865 * Some vendors (e.g. ZNYX) don't use the standard
1866 * DEC Address ROM format, but rather just have an
1867 * Ethernet address in the first 6 bytes, maybe a
1868 * 2 byte checksum, and then all 0xff's.
1869 */
1870 for (i = 8; i < 32; i++) {
1871 if (sc->sc_srom[i] != 0xff)
1872 return (0);
1873 }
1874
1875 /*
1876 * Sanity check the Ethernet address:
1877 *
1878 * - Make sure it's not multicast or locally
1879 * assigned
1880 * - Make sure it has a non-0 OUI
1881 */
1882 if (sc->sc_srom[0] & 3)
1883 return (0);
1884 if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
1885 sc->sc_srom[2] == 0)
1886 return (0);
1887
1888 memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
1889 return (1);
1890 }
1891
1892 /*
1893 * Standard DEC Address ROM test.
1894 */
1895
1896 if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
1897 return (0);
1898
1899 for (i = 0; i < 8; i++) {
1900 if (sc->sc_srom[i] != sc->sc_srom[15 - i])
1901 return (0);
1902 }
1903
1904 memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
1905
1906 cksum = *(u_int16_t *) &enaddr[0];
1907
1908 cksum <<= 1;
1909 if (cksum > 0xffff)
1910 cksum -= 0xffff;
1911
1912 cksum += *(u_int16_t *) &enaddr[2];
1913 if (cksum > 0xffff)
1914 cksum -= 0xffff;
1915
1916 cksum <<= 1;
1917 if (cksum > 0xffff)
1918 cksum -= 0xffff;
1919
1920 cksum += *(u_int16_t *) &enaddr[4];
1921 if (cksum >= 0xffff)
1922 cksum -= 0xffff;
1923
1924 if (cksum != *(u_int16_t *) &sc->sc_srom[6])
1925 return (0);
1926
1927 return (1);
1928 }
1929
1930 /*
1931 * tlp_filter_setup:
1932 *
1933 * Set the Tulip's receive filter.
1934 */
1935 void
1936 tlp_filter_setup(sc)
1937 struct tulip_softc *sc;
1938 {
1939 struct ethercom *ec = &sc->sc_ethercom;
1940 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1941 struct ether_multi *enm;
1942 struct ether_multistep step;
1943 __volatile u_int32_t *sp;
1944 u_int8_t enaddr[ETHER_ADDR_LEN];
1945 u_int32_t hash;
1946 int cnt;
1947
1948 DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
1949 sc->sc_dev.dv_xname, sc->sc_flags));
1950
1951 memcpy(enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1952
1953 /*
1954 * If there are transmissions pending, wait until they have
1955 * completed.
1956 */
1957 if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL ||
1958 (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
1959 sc->sc_flags |= TULIPF_WANT_SETUP;
1960 DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
1961 sc->sc_dev.dv_xname));
1962 return;
1963 }
1964 sc->sc_flags &= ~TULIPF_WANT_SETUP;
1965
1966 /*
1967 * If we're running, idle the transmit and receive engines. If
1968 * we're NOT running, we're being called from tlp_init(), and our
1969 * writing OPMODE will start the transmit and receive processes
1970 * in motion.
1971 */
1972 if (ifp->if_flags & IFF_RUNNING)
1973 tlp_idle(sc, OPMODE_ST|OPMODE_SR);
1974
1975 sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
1976
1977 if (ifp->if_flags & IFF_PROMISC) {
1978 sc->sc_opmode |= OPMODE_PR;
1979 goto allmulti;
1980 }
1981
1982 /*
1983 * Try Perfect filtering first.
1984 */
1985
1986 sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
1987 sp = TULIP_CDSP(sc);
1988 memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
1989 cnt = 0;
1990 ETHER_FIRST_MULTI(step, ec, enm);
1991 while (enm != NULL) {
1992 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1993 /*
1994 * We must listen to a range of multicast addresses.
1995 * For now, just accept all multicasts, rather than
1996 * trying to set only those filter bits needed to match
1997 * the range. (At this time, the only use of address
1998 * ranges is for IP multicast routing, for which the
1999 * range is big enough to require all bits set.)
2000 */
2001 goto allmulti;
2002 }
2003 if (cnt == (TULIP_MAXADDRS - 2)) {
2004 /*
2005 * We already have our multicast limit (still need
2006 * our station address and broadcast). Go to
2007 * Hash-Perfect mode.
2008 */
2009 goto hashperfect;
2010 }
2011 *sp++ = ((u_int16_t *) enm->enm_addrlo)[0];
2012 *sp++ = ((u_int16_t *) enm->enm_addrlo)[1];
2013 *sp++ = ((u_int16_t *) enm->enm_addrlo)[2];
2014 ETHER_NEXT_MULTI(step, enm);
2015 }
2016
2017 if (ifp->if_flags & IFF_BROADCAST) {
2018 /* ...and the broadcast address. */
2019 cnt++;
2020 *sp++ = 0xffff;
2021 *sp++ = 0xffff;
2022 *sp++ = 0xffff;
2023 }
2024
2025 /* Pad the rest with our station address. */
2026 for (; cnt < TULIP_MAXADDRS; cnt++) {
2027 *sp++ = ((u_int16_t *) enaddr)[0];
2028 *sp++ = ((u_int16_t *) enaddr)[1];
2029 *sp++ = ((u_int16_t *) enaddr)[2];
2030 }
2031 ifp->if_flags &= ~IFF_ALLMULTI;
2032 goto setit;
2033
2034 hashperfect:
2035 /*
2036 * Try Hash-Perfect mode.
2037 */
2038
2039 /*
2040 * Some 21140 chips have broken Hash-Perfect modes. On these
2041 * chips, we simply use Hash-Only mode, and put our station
2042 * address into the filter.
2043 */
2044 if (sc->sc_chip == TULIP_CHIP_21140)
2045 sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
2046 else
2047 sc->sc_filtmode = TDCTL_Tx_FT_HASH;
2048 sp = TULIP_CDSP(sc);
2049 memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
2050 ETHER_FIRST_MULTI(step, ec, enm);
2051 while (enm != NULL) {
2052 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2053 /*
2054 * We must listen to a range of multicast addresses.
2055 * For now, just accept all multicasts, rather than
2056 * trying to set only those filter bits needed to match
2057 * the range. (At this time, the only use of address
2058 * ranges is for IP multicast routing, for which the
2059 * range is big enough to require all bits set.)
2060 */
2061 goto allmulti;
2062 }
2063 hash = tlp_mchash(enm->enm_addrlo);
2064 sp[hash >> 4] |= 1 << (hash & 0xf);
2065 ETHER_NEXT_MULTI(step, enm);
2066 }
2067
2068 if (ifp->if_flags & IFF_BROADCAST) {
2069 /* ...and the broadcast address. */
2070 hash = tlp_mchash(etherbroadcastaddr);
2071 sp[hash >> 4] |= 1 << (hash & 0xf);
2072 }
2073
2074 if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
2075 /* ...and our station address. */
2076 hash = tlp_mchash(enaddr);
2077 sp[hash >> 4] |= 1 << (hash & 0xf);
2078 } else {
2079 /*
2080 * Hash-Perfect mode; put our station address after
2081 * the hash table.
2082 */
2083 sp[39] = ((u_int16_t *) enaddr)[0];
2084 sp[40] = ((u_int16_t *) enaddr)[1];
2085 sp[41] = ((u_int16_t *) enaddr)[2];
2086 }
2087 ifp->if_flags &= ~IFF_ALLMULTI;
2088 goto setit;
2089
2090 allmulti:
2091 /*
2092 * Use Perfect filter mode. First address is the broadcast address,
2093 * and pad the rest with our station address. We'll set Pass-all-
2094 * multicast in OPMODE below.
2095 */
2096 sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
2097 sp = TULIP_CDSP(sc);
2098 memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
2099 cnt = 0;
2100 if (ifp->if_flags & IFF_BROADCAST) {
2101 cnt++;
2102 *sp++ = 0xffff;
2103 *sp++ = 0xffff;
2104 *sp++ = 0xffff;
2105 }
2106 for (; cnt < TULIP_MAXADDRS; cnt++) {
2107 *sp++ = ((u_int16_t *) enaddr)[0];
2108 *sp++ = ((u_int16_t *) enaddr)[1];
2109 *sp++ = ((u_int16_t *) enaddr)[2];
2110 }
2111 ifp->if_flags |= IFF_ALLMULTI;
2112
2113 setit:
2114 if (ifp->if_flags & IFF_ALLMULTI)
2115 sc->sc_opmode |= OPMODE_PM;
2116
2117 /* Sync the setup packet buffer. */
2118 TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
2119
2120 /*
2121 * Fill in the setup packet descriptor.
2122 */
2123 sc->sc_setup_desc.td_bufaddr1 = TULIP_CDSPADDR(sc);
2124 sc->sc_setup_desc.td_bufaddr2 = TULIP_CDTXADDR(sc, sc->sc_txnext);
2125 sc->sc_setup_desc.td_ctl =
2126 (TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
2127 sc->sc_filtmode | TDCTL_Tx_SET | TDCTL_Tx_FS | TDCTL_Tx_LS |
2128 TDCTL_Tx_IC | TDCTL_CH;
2129 sc->sc_setup_desc.td_status = TDSTAT_OWN;
2130 TULIP_CDSDSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2131
2132 /*
2133 * Write the address of the setup descriptor. This also has
2134 * the side effect of giving the transmit ring to the chip,
2135 * since the setup descriptor points to the next available
2136 * descriptor in the ring.
2137 */
2138 TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDSDADDR(sc));
2139
2140 /*
2141 * Set the OPMODE register. This will also resume the
2142 * transmit transmit process we idled above.
2143 */
2144 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2145
2146 sc->sc_flags |= TULIPF_DOING_SETUP;
2147
2148 /*
2149 * Kick the transmitter; this will cause the Tulip to
2150 * read the setup descriptor.
2151 */
2152 /* XXX USE AUTOPOLLING? */
2153 TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
2154
2155 /* Set up a watchdog timer in case the chip flakes out. */
2156 ifp->if_timer = 5;
2157
2158 DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
2159 }
2160
2161 /*
2162 * tlp_winb_filter_setup:
2163 *
2164 * Set the Winbond 89C840F's receive filter.
2165 */
2166 void
2167 tlp_winb_filter_setup(sc)
2168 struct tulip_softc *sc;
2169 {
2170 struct ethercom *ec = &sc->sc_ethercom;
2171 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2172 struct ether_multi *enm;
2173 struct ether_multistep step;
2174 u_int32_t hash, mchash[2];
2175
2176 DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
2177 sc->sc_dev.dv_xname, sc->sc_flags));
2178
2179 sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
2180
2181 if (ifp->if_flags & IFF_MULTICAST)
2182 sc->sc_opmode |= OPMODE_WINB_AMP;
2183
2184 if (ifp->if_flags & IFF_BROADCAST)
2185 sc->sc_opmode |= OPMODE_WINB_ABP;
2186
2187 if (ifp->if_flags & IFF_PROMISC) {
2188 sc->sc_opmode |= OPMODE_WINB_APP;
2189 goto allmulti;
2190 }
2191
2192 mchash[0] = mchash[1] = 0;
2193
2194 ETHER_FIRST_MULTI(step, ec, enm);
2195 while (enm != NULL) {
2196 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2197 /*
2198 * We must listen to a range of multicast addresses.
2199 * For now, just accept all multicasts, rather than
2200 * trying to set only those filter bits needed to match
2201 * the range. (At this time, the only use of address
2202 * ranges is for IP multicast routing, for which the
2203 * range is big enough to require all bits set.)
2204 */
2205 goto allmulti;
2206 }
2207
2208 /*
2209 * According to the FreeBSD `wb' driver, yes, you
2210 * really do invert the hash.
2211 */
2212 hash = (~(tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
2213 & 0x3f;
2214 mchash[hash >> 5] |= 1 << (hash & 0x1f);
2215 ETHER_NEXT_MULTI(step, enm);
2216 }
2217 ifp->if_flags &= ~IFF_ALLMULTI;
2218 goto setit;
2219
2220 allmulti:
2221 ifp->if_flags |= IFF_ALLMULTI;
2222 mchash[0] = mchash[1] = 0xffffffff;
2223
2224 setit:
2225 TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
2226 TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
2227 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2228 DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
2229 sc->sc_dev.dv_xname));
2230 }
2231
2232 /*
2233 * tlp_idle:
2234 *
2235 * Cause the transmit and/or receive processes to go idle.
2236 */
2237 void
2238 tlp_idle(sc, bits)
2239 struct tulip_softc *sc;
2240 u_int32_t bits;
2241 {
2242 static const char *tx_state_names[] = {
2243 "STOPPED",
2244 "RUNNING - FETCH",
2245 "RUNNING - WAIT",
2246 "RUNNING - READING",
2247 "-- RESERVED --",
2248 "RUNNING - SETUP",
2249 "SUSPENDED",
2250 "RUNNING - CLOSE",
2251 };
2252 static const char *rx_state_names[] = {
2253 "STOPPED",
2254 "RUNNING - FETCH",
2255 "RUNNING - CHECK",
2256 "RUNNING - WAIT",
2257 "SUSPENDED",
2258 "RUNNING - CLOSE",
2259 "RUNNING - FLUSH",
2260 "RUNNING - QUEUE",
2261 };
2262 u_int32_t csr, ackmask = 0;
2263 int i;
2264
2265 if (bits & OPMODE_ST)
2266 ackmask |= STATUS_TPS;
2267
2268 if (bits & OPMODE_SR)
2269 ackmask |= STATUS_RPS;
2270
2271 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
2272
2273 for (i = 0; i < 1000; i++) {
2274 if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
2275 break;
2276 delay(10);
2277 }
2278
2279 csr = TULIP_READ(sc, CSR_STATUS);
2280 if ((csr & ackmask) != ackmask) {
2281 if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
2282 (csr & STATUS_TS) != STATUS_TS_STOPPED)
2283 printf("%s: transmit process failed to idle: "
2284 "state %s\n", sc->sc_dev.dv_xname,
2285 tx_state_names[(csr & STATUS_TS) >> 20]);
2286 if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
2287 (csr & STATUS_RS) != STATUS_RS_STOPPED)
2288 printf("%s: receive process failed to idle: "
2289 "state %s\n", sc->sc_dev.dv_xname,
2290 rx_state_names[(csr & STATUS_RS) >> 17]);
2291 }
2292 TULIP_WRITE(sc, CSR_STATUS, ackmask);
2293 }
2294
2295 /*****************************************************************************
2296 * Generic media support functions.
2297 *****************************************************************************/
2298
2299 /*
2300 * tlp_mediastatus: [ifmedia interface function]
2301 *
2302 * Query the current media.
2303 */
2304 void
2305 tlp_mediastatus(ifp, ifmr)
2306 struct ifnet *ifp;
2307 struct ifmediareq *ifmr;
2308 {
2309 struct tulip_softc *sc = ifp->if_softc;
2310
2311 (*sc->sc_mediasw->tmsw_get)(sc, ifmr);
2312 }
2313
2314 /*
2315 * tlp_mediachange: [ifmedia interface function]
2316 *
2317 * Update the current media.
2318 */
2319 int
2320 tlp_mediachange(ifp)
2321 struct ifnet *ifp;
2322 {
2323 struct tulip_softc *sc = ifp->if_softc;
2324
2325 return ((*sc->sc_mediasw->tmsw_set)(sc));
2326 }
2327
2328 /*****************************************************************************
2329 * Support functions for MII-attached media.
2330 *****************************************************************************/
2331
2332 /*
2333 * tlp_mii_tick:
2334 *
2335 * One second timer, used to tick the MII.
2336 */
2337 void
2338 tlp_mii_tick(arg)
2339 void *arg;
2340 {
2341 struct tulip_softc *sc = arg;
2342 int s;
2343
2344 s = splnet();
2345 mii_tick(&sc->sc_mii);
2346 splx(s);
2347
2348 timeout(sc->sc_tick, sc, hz);
2349 }
2350
2351 /*
2352 * tlp_mii_statchg: [mii interface function]
2353 *
2354 * Callback from PHY when media changes.
2355 */
2356 void
2357 tlp_mii_statchg(self)
2358 struct device *self;
2359 {
2360 struct tulip_softc *sc = (struct tulip_softc *)self;
2361
2362 /* Idle the transmit and receive processes. */
2363 tlp_idle(sc, OPMODE_ST|OPMODE_SR);
2364
2365 /*
2366 * XXX What about Heartbeat Disable? Is it magically frobbed
2367 * XXX by the PHY? I hope so...
2368 */
2369
2370 sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD);
2371
2372 if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
2373 sc->sc_opmode |= OPMODE_TTM;
2374
2375 if (sc->sc_mii.mii_media_active & IFM_FDX)
2376 sc->sc_opmode |= OPMODE_FD;
2377
2378 /*
2379 * Write new OPMODE bits. This also restarts the transmit
2380 * and receive processes.
2381 */
2382 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2383
2384 /* XXX Update ifp->if_baudrate */
2385 }
2386
2387 /*
2388 * tlp_winb_mii_statchg: [mii interface function]
2389 *
2390 * Callback from PHY when media changes. This version is
2391 * for the Winbond 89C840F, which has different OPMODE bits.
2392 */
2393 void
2394 tlp_winb_mii_statchg(self)
2395 struct device *self;
2396 {
2397 struct tulip_softc *sc = (struct tulip_softc *)self;
2398
2399 /* Idle the transmit and receive processes. */
2400 tlp_idle(sc, OPMODE_ST|OPMODE_SR);
2401
2402 /*
2403 * XXX What about Heartbeat Disable? Is it magically frobbed
2404 * XXX by the PHY? I hope so...
2405 */
2406
2407 sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD);
2408
2409 if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
2410 sc->sc_opmode |= OPMODE_WINB_FES;
2411
2412 if (sc->sc_mii.mii_media_active & IFM_FDX)
2413 sc->sc_opmode |= OPMODE_FD;
2414
2415 /*
2416 * Write new OPMODE bits. This also restarts the transmit
2417 * and receive processes.
2418 */
2419 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2420
2421 /* XXX Update ifp->if_baudrate */
2422 }
2423
2424 /*
2425 * tlp_mii_getmedia:
2426 *
2427 * Callback from ifmedia to request current media status.
2428 */
2429 void
2430 tlp_mii_getmedia(sc, ifmr)
2431 struct tulip_softc *sc;
2432 struct ifmediareq *ifmr;
2433 {
2434
2435 mii_pollstat(&sc->sc_mii);
2436 ifmr->ifm_status = sc->sc_mii.mii_media_status;
2437 ifmr->ifm_active = sc->sc_mii.mii_media_active;
2438 }
2439
2440 /*
2441 * tlp_mii_setmedia:
2442 *
2443 * Callback from ifmedia to request new media setting.
2444 */
2445 int
2446 tlp_mii_setmedia(sc)
2447 struct tulip_softc *sc;
2448 {
2449 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2450
2451 if (ifp->if_flags & IFF_UP)
2452 mii_mediachg(&sc->sc_mii);
2453 return (0);
2454 }
2455
2456 #define MII_EMIT(sc, x) \
2457 do { \
2458 TULIP_WRITE((sc), CSR_MIIROM, (x)); \
2459 delay(1); \
2460 } while (0)
2461
2462 /*
2463 * tlp_sio_mii_sync:
2464 *
2465 * Synchronize the SIO-attached MII.
2466 */
2467 void
2468 tlp_sio_mii_sync(sc)
2469 struct tulip_softc *sc;
2470 {
2471 u_int32_t miirom;
2472 int i;
2473
2474 miirom = MIIROM_MIIDIR|MIIROM_MDO;
2475
2476 MII_EMIT(sc, miirom);
2477 for (i = 0; i < 32; i++) {
2478 MII_EMIT(sc, miirom | MIIROM_MDC);
2479 MII_EMIT(sc, miirom);
2480 }
2481 }
2482
2483 /*
2484 * tlp_sio_mii_sendbits:
2485 *
2486 * Send a series of bits out the SIO to the MII.
2487 */
2488 void
2489 tlp_sio_mii_sendbits(sc, data, nbits)
2490 struct tulip_softc *sc;
2491 u_int32_t data;
2492 int nbits;
2493 {
2494 u_int32_t miirom, i;
2495
2496 miirom = MIIROM_MIIDIR;
2497 MII_EMIT(sc, miirom);
2498
2499 for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
2500 if (data & i)
2501 miirom |= MIIROM_MDO;
2502 else
2503 miirom &= ~MIIROM_MDO;
2504 MII_EMIT(sc, miirom);
2505 MII_EMIT(sc, miirom|MIIROM_MDC);
2506 MII_EMIT(sc, miirom);
2507 }
2508 }
2509
2510 /*
2511 * tlp_sio_mii_readreg:
2512 *
2513 * Read a PHY register via SIO-attached MII.
2514 */
2515 int
2516 tlp_sio_mii_readreg(self, phy, reg)
2517 struct device *self;
2518 int phy, reg;
2519 {
2520 struct tulip_softc *sc = (void *) self;
2521 int val = 0, err = 0, i;
2522
2523 tlp_sio_mii_sync(sc);
2524
2525 tlp_sio_mii_sendbits(sc, MII_COMMAND_START, 2);
2526 tlp_sio_mii_sendbits(sc, MII_COMMAND_READ, 2);
2527 tlp_sio_mii_sendbits(sc, phy, 5);
2528 tlp_sio_mii_sendbits(sc, reg, 5);
2529
2530 MII_EMIT(sc, MIIROM_MIIDIR);
2531 MII_EMIT(sc, MIIROM_MIIDIR|MIIROM_MDC);
2532
2533 MII_EMIT(sc, 0);
2534 MII_EMIT(sc, MIIROM_MDC);
2535
2536 err = TULIP_ISSET(sc, CSR_MIIROM, MIIROM_MDI);
2537
2538 MII_EMIT(sc, 0);
2539 MII_EMIT(sc, MIIROM_MDC);
2540
2541 for (i = 0; i < 16; i++) {
2542 val <<= 1;
2543 MII_EMIT(sc, 0);
2544 if (err == 0 && TULIP_ISSET(sc, CSR_MIIROM, MIIROM_MDI))
2545 val |= 1;
2546 MII_EMIT(sc, MIIROM_MDC);
2547 }
2548
2549 MII_EMIT(sc, 0);
2550
2551 return (err ? 0 : val);
2552 }
2553
2554 /*
2555 * tlp_sio_mii_writereg:
2556 *
2557 * Write a PHY register via SIO-attached MII.
2558 */
2559 void
2560 tlp_sio_mii_writereg(self, phy, reg, val)
2561 struct device *self;
2562 int phy, reg, val;
2563 {
2564 struct tulip_softc *sc = (void *) self;
2565
2566 tlp_sio_mii_sync(sc);
2567
2568 tlp_sio_mii_sendbits(sc, MII_COMMAND_START, 2);
2569 tlp_sio_mii_sendbits(sc, MII_COMMAND_WRITE, 2);
2570 tlp_sio_mii_sendbits(sc, phy, 5);
2571 tlp_sio_mii_sendbits(sc, reg, 5);
2572 tlp_sio_mii_sendbits(sc, MII_COMMAND_ACK, 2);
2573 tlp_sio_mii_sendbits(sc, val, 16);
2574
2575 MII_EMIT(sc, 0);
2576 }
2577
2578 #undef MII_EMIT
2579
2580 /*
2581 * tlp_pnic_mii_readreg:
2582 *
2583 * Read a PHY register on the Lite-On PNIC.
2584 */
2585 int
2586 tlp_pnic_mii_readreg(self, phy, reg)
2587 struct device *self;
2588 int phy, reg;
2589 {
2590 struct tulip_softc *sc = (void *) self;
2591 u_int32_t val;
2592 int i;
2593
2594 TULIP_WRITE(sc, CSR_PNIC_MII,
2595 PNIC_MII_MBO | PNIC_MII_RESERVED |
2596 PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
2597 (reg << PNIC_MII_REGSHIFT));
2598
2599 for (i = 0; i < 1000; i++) {
2600 delay(10);
2601 val = TULIP_READ(sc, CSR_PNIC_MII);
2602 if ((val & PNIC_MII_BUSY) == 0) {
2603 if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
2604 return (0);
2605 else
2606 return (val & PNIC_MII_DATA);
2607 }
2608 }
2609 printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
2610 return (0);
2611 }
2612
2613 /*
2614 * tlp_pnic_mii_writereg:
2615 *
2616 * Write a PHY register on the Lite-On PNIC.
2617 */
2618 void
2619 tlp_pnic_mii_writereg(self, phy, reg, val)
2620 struct device *self;
2621 int phy, reg, val;
2622 {
2623 struct tulip_softc *sc = (void *) self;
2624 int i;
2625
2626 TULIP_WRITE(sc, CSR_PNIC_MII,
2627 PNIC_MII_MBO | PNIC_MII_RESERVED |
2628 PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
2629 (reg << PNIC_MII_REGSHIFT) | val);
2630
2631 for (i = 0; i < 1000; i++) {
2632 delay(10);
2633 if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
2634 return;
2635 }
2636 printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
2637 }
2638
2639 /*****************************************************************************
2640 * Chip/board-specific media switches. The ones here are ones that
2641 * are potentially common to multiple front-ends.
2642 *****************************************************************************/
2643
2644 /*
2645 * 21040 and 21041 media switches.
2646 */
2647 void tlp_21040_tmsw_init __P((struct tulip_softc *));
2648 void tlp_21040_tp_tmsw_init __P((struct tulip_softc *));
2649 void tlp_21040_auibnc_tmsw_init __P((struct tulip_softc *));
2650 void tlp_21040_21041_tmsw_get __P((struct tulip_softc *,
2651 struct ifmediareq *));
2652 int tlp_21040_21041_tmsw_set __P((struct tulip_softc *));
2653
2654 const struct tulip_mediasw tlp_21040_mediasw = {
2655 tlp_21040_tmsw_init, tlp_21040_21041_tmsw_get, tlp_21040_21041_tmsw_set
2656 };
2657
2658 const struct tulip_mediasw tlp_21040_tp_mediasw = {
2659 tlp_21040_tp_tmsw_init, tlp_21040_21041_tmsw_get,
2660 tlp_21040_21041_tmsw_set
2661 };
2662
2663 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
2664 tlp_21040_auibnc_tmsw_init, tlp_21040_21041_tmsw_get,
2665 tlp_21040_21041_tmsw_set
2666 };
2667
2668 #define ADD(m, t) ifmedia_add(&sc->sc_mii.mii_media, (m), 0, (t))
2669 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
2670
2671 void
2672 tlp_21040_tmsw_init(sc)
2673 struct tulip_softc *sc;
2674 {
2675 struct tulip_21040_21041_sia_media *tsm;
2676 const char *sep = "";
2677
2678 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
2679 tlp_mediastatus);
2680
2681 printf("%s: ", sc->sc_dev.dv_xname);
2682
2683 tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
2684 M_WAITOK);
2685 tsm->tsm_siaconn = SIACONN_21040_10BASET;
2686 tsm->tsm_siatxrx = SIATXRX_21040_10BASET;
2687 tsm->tsm_siagen = SIAGEN_21040_10BASET;
2688 ADD(IFM_ETHER|IFM_10_T, tsm);
2689 PRINT("10baseT");
2690
2691 tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
2692 M_WAITOK);
2693 tsm->tsm_siaconn = SIACONN_21040_10BASET_FDX;
2694 tsm->tsm_siatxrx = SIATXRX_21040_10BASET_FDX;
2695 tsm->tsm_siagen = SIAGEN_21040_10BASET_FDX;
2696 ADD(IFM_ETHER|IFM_10_T|IFM_FDX, tsm);
2697 PRINT("10baseT-FDX");
2698
2699 tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
2700 M_WAITOK);
2701 tsm->tsm_siaconn = SIACONN_21040_AUI;
2702 tsm->tsm_siatxrx = SIATXRX_21040_AUI;
2703 tsm->tsm_siagen = SIAGEN_21040_AUI;
2704 ADD(IFM_ETHER|IFM_10_5, tsm);
2705 PRINT("10base5");
2706
2707 tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
2708 M_WAITOK);
2709 tsm->tsm_siaconn = SIACONN_21040_EXTSIA;
2710 tsm->tsm_siatxrx = SIATXRX_21040_EXTSIA;
2711 tsm->tsm_siagen = SIAGEN_21040_EXTSIA;
2712 ADD(IFM_ETHER|IFM_MANUAL, tsm);
2713 PRINT("manual");
2714
2715 /*
2716 * XXX Autosense not yet supported.
2717 */
2718
2719 /* XXX This should be auto-sense. */
2720 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
2721 printf(", default 10baseT");
2722
2723 printf("\n");
2724 }
2725
2726 void
2727 tlp_21040_tp_tmsw_init(sc)
2728 struct tulip_softc *sc;
2729 {
2730 struct tulip_21040_21041_sia_media *tsm;
2731 const char *sep = "";
2732
2733 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
2734 tlp_mediastatus);
2735
2736 printf("%s: ", sc->sc_dev.dv_xname);
2737
2738 tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
2739 M_WAITOK);
2740 tsm->tsm_siaconn = SIACONN_21040_10BASET;
2741 tsm->tsm_siatxrx = SIATXRX_21040_10BASET;
2742 tsm->tsm_siagen = SIAGEN_21040_10BASET;
2743 ADD(IFM_ETHER|IFM_10_T, tsm);
2744 PRINT("10baseT");
2745
2746 tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
2747 M_WAITOK);
2748 tsm->tsm_siaconn = SIACONN_21040_10BASET_FDX;
2749 tsm->tsm_siatxrx = SIATXRX_21040_10BASET_FDX;
2750 tsm->tsm_siagen = SIAGEN_21040_10BASET_FDX;
2751 ADD(IFM_ETHER|IFM_10_T|IFM_FDX, tsm);
2752 PRINT("10baseT-FDX");
2753
2754 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
2755 printf(", default 10baseT");
2756
2757 printf("\n");
2758 }
2759
2760 void
2761 tlp_21040_auibnc_tmsw_init(sc)
2762 struct tulip_softc *sc;
2763 {
2764 struct tulip_21040_21041_sia_media *tsm;
2765 const char *sep = "";
2766
2767 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
2768 tlp_mediastatus);
2769
2770 printf("%s: ", sc->sc_dev.dv_xname);
2771
2772 tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
2773 M_WAITOK);
2774 tsm->tsm_siaconn = SIACONN_21040_AUI;
2775 tsm->tsm_siatxrx = SIATXRX_21040_AUI;
2776 tsm->tsm_siagen = SIAGEN_21040_AUI;
2777 ADD(IFM_ETHER|IFM_10_5, tsm);
2778 PRINT("10base5");
2779
2780 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
2781
2782 printf("\n");
2783 }
2784
2785 #undef ADD
2786 #undef PRINT
2787
2788 void
2789 tlp_21040_21041_tmsw_get(sc, ifmr)
2790 struct tulip_softc *sc;
2791 struct ifmediareq *ifmr;
2792 {
2793 struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
2794
2795 ifmr->ifm_status = 0;
2796
2797 switch (IFM_SUBTYPE(ife->ifm_media)) {
2798 case IFM_AUTO:
2799 /*
2800 * XXX Implement autosensing case.
2801 */
2802 break;
2803
2804 case IFM_10_T:
2805 /*
2806 * We're able to detect link directly on twisted pair.
2807 */
2808 ifmr->ifm_status = IFM_AVALID;
2809 if (TULIP_ISSET(sc, CSR_SIASTAT, SIASTAT_LKF) == 0)
2810 ifmr->ifm_status |= IFM_ACTIVE;
2811 /* FALLTHROUGH */
2812 default:
2813 /*
2814 * If not autosensing, active media is the currently
2815 * selected media.
2816 */
2817 ifmr->ifm_active = ife->ifm_media;
2818 }
2819 }
2820
2821 int
2822 tlp_21040_21041_tmsw_set(sc)
2823 struct tulip_softc *sc;
2824 {
2825 struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
2826 struct tulip_21040_21041_sia_media *tsm;
2827
2828 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
2829 /*
2830 * If not autosensing, just pull the SIA settings out
2831 * of the media entry.
2832 */
2833 tsm = ife->ifm_aux;
2834 TULIP_WRITE(sc, CSR_SIACONN, SIACONN_SRL);
2835 TULIP_WRITE(sc, CSR_SIATXRX, tsm->tsm_siatxrx);
2836 TULIP_WRITE(sc, CSR_SIAGEN, tsm->tsm_siagen);
2837 TULIP_WRITE(sc, CSR_SIACONN, tsm->tsm_siaconn);
2838
2839 tlp_idle(sc, OPMODE_ST|OPMODE_SR);
2840 sc->sc_opmode &= ~OPMODE_FD;
2841 if (ife->ifm_media & IFM_FDX)
2842 sc->sc_opmode |= OPMODE_FD;
2843 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
2844 } else {
2845 /*
2846 * XXX Implement autosensing case.
2847 */
2848 }
2849
2850 return (0);
2851 }
2852
2853 /*
2854 * MII-on-SIO media switch. Handles only MII attached to the SIO.
2855 */
2856 void tlp_sio_mii_tmsw_init __P((struct tulip_softc *));
2857
2858 const struct tulip_mediasw tlp_sio_mii_mediasw = {
2859 tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
2860 };
2861
2862 void
2863 tlp_sio_mii_tmsw_init(sc)
2864 struct tulip_softc *sc;
2865 {
2866 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2867
2868 sc->sc_mii.mii_ifp = ifp;
2869 sc->sc_mii.mii_readreg = tlp_sio_mii_readreg;
2870 sc->sc_mii.mii_writereg = tlp_sio_mii_writereg;
2871 sc->sc_mii.mii_statchg = sc->sc_statchg;
2872 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
2873 tlp_mediastatus);
2874 mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
2875 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
2876 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
2877 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
2878 } else {
2879 sc->sc_flags |= TULIPF_HAS_MII;
2880 sc->sc_tick = tlp_mii_tick;
2881 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
2882 }
2883 }
2884
2885 /*
2886 * Lite-On PNIC media switch. Must handle MII or internal NWAY.
2887 */
2888 void tlp_pnic_tmsw_init __P((struct tulip_softc *));
2889 void tlp_pnic_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
2890 int tlp_pnic_tmsw_set __P((struct tulip_softc *));
2891
2892 const struct tulip_mediasw tlp_pnic_mediasw = {
2893 tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
2894 };
2895
2896 void tlp_pnic_nway_statchg __P((struct device *));
2897 void tlp_pnic_nway_tick __P((void *));
2898 int tlp_pnic_nway_service __P((struct tulip_softc *, int));
2899 void tlp_pnic_nway_reset __P((struct tulip_softc *));
2900 int tlp_pnic_nway_auto __P((struct tulip_softc *, int));
2901 void tlp_pnic_nway_auto_timeout __P((void *));
2902 void tlp_pnic_nway_status __P((struct tulip_softc *));
2903 void tlp_pnic_nway_acomp __P((struct tulip_softc *));
2904
2905 void
2906 tlp_pnic_tmsw_init(sc)
2907 struct tulip_softc *sc;
2908 {
2909 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2910 const char *sep = "";
2911
2912 #define ADD(m, c) ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
2913 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
2914
2915 sc->sc_mii.mii_ifp = ifp;
2916 sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
2917 sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
2918 sc->sc_mii.mii_statchg = sc->sc_statchg;
2919 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
2920 tlp_mediastatus);
2921 mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
2922 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
2923 /* XXX What about AUI/BNC support? */
2924 printf("%s: ", sc->sc_dev.dv_xname);
2925
2926 tlp_pnic_nway_reset(sc);
2927
2928 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
2929 PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
2930 PRINT("10baseT");
2931
2932 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
2933 PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
2934 PRINT("10baseT-FDX");
2935
2936 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
2937 PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
2938 PRINT("100baseTX");
2939
2940 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
2941 PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
2942 PNIC_NWAY_CAP100TXFDX);
2943 PRINT("100baseTX-FDX");
2944
2945 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
2946 PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
2947 PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
2948 PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
2949 PRINT("auto");
2950
2951 printf("\n");
2952
2953 sc->sc_statchg = tlp_pnic_nway_statchg;
2954 sc->sc_tick = tlp_pnic_nway_tick;
2955 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
2956 } else {
2957 sc->sc_flags |= TULIPF_HAS_MII;
2958 sc->sc_tick = tlp_mii_tick;
2959 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
2960 }
2961
2962 #undef ADD
2963 #undef PRINT
2964 }
2965
2966 void
2967 tlp_pnic_tmsw_get(sc, ifmr)
2968 struct tulip_softc *sc;
2969 struct ifmediareq *ifmr;
2970 {
2971 struct mii_data *mii = &sc->sc_mii;
2972
2973 if (sc->sc_flags & TULIPF_HAS_MII)
2974 tlp_mii_getmedia(sc, ifmr);
2975 else {
2976 mii->mii_media_status = 0;
2977 mii->mii_media_active = IFM_NONE;
2978 tlp_pnic_nway_service(sc, MII_POLLSTAT);
2979 ifmr->ifm_status = sc->sc_mii.mii_media_status;
2980 ifmr->ifm_active = sc->sc_mii.mii_media_active;
2981 }
2982 }
2983
2984 int
2985 tlp_pnic_tmsw_set(sc)
2986 struct tulip_softc *sc;
2987 {
2988 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2989 struct mii_data *mii = &sc->sc_mii;
2990
2991 if (sc->sc_flags & TULIPF_HAS_MII)
2992 return (tlp_mii_setmedia(sc));
2993
2994 if (ifp->if_flags & IFF_UP) {
2995 mii->mii_media_status = 0;
2996 mii->mii_media_active = IFM_NONE;
2997 return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
2998 }
2999
3000 return (0);
3001 }
3002
3003 void
3004 tlp_pnic_nway_statchg(self)
3005 struct device *self;
3006 {
3007 struct tulip_softc *sc = (struct tulip_softc *)self;
3008
3009 /* Idle the transmit and receive processes. */
3010 tlp_idle(sc, OPMODE_ST|OPMODE_SR);
3011
3012 /*
3013 * XXX What about Heartbeat Disable? Is it magically frobbed
3014 * XXX by the PHY? I hope so...
3015 */
3016
3017 sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
3018 OPMODE_SCR);
3019
3020 if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
3021 sc->sc_opmode |= OPMODE_TTM;
3022 TULIP_WRITE(sc, CSR_GPP,
3023 GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
3024 GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
3025 } else {
3026 sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR;
3027 TULIP_WRITE(sc, CSR_GPP,
3028 GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
3029 GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
3030 }
3031
3032 if (sc->sc_mii.mii_media_active & IFM_FDX)
3033 sc->sc_opmode |= OPMODE_FD;
3034
3035 /*
3036 * Write new OPMODE bits. This also restarts the transmit
3037 * and receive processes.
3038 */
3039 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
3040
3041 /* XXX Update ifp->if_baudrate */
3042 }
3043
3044 void
3045 tlp_pnic_nway_tick(arg)
3046 void *arg;
3047 {
3048 struct tulip_softc *sc = arg;
3049 int s;
3050
3051 s = splnet();
3052 tlp_pnic_nway_service(sc, MII_TICK);
3053 splx(s);
3054
3055 timeout(tlp_pnic_nway_tick, sc, hz);
3056 }
3057
3058 /*
3059 * Support for the Lite-On PNIC internal NWay block. This is constructed
3060 * somewhat like a PHY driver for simplicity.
3061 */
3062
3063 int
3064 tlp_pnic_nway_service(sc, cmd)
3065 struct tulip_softc *sc;
3066 int cmd;
3067 {
3068 struct mii_data *mii = &sc->sc_mii;
3069 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
3070
3071 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
3072 return (0);
3073
3074 switch (cmd) {
3075 case MII_POLLSTAT:
3076 /* Nothing special to do here. */
3077 break;
3078
3079 case MII_MEDIACHG:
3080 switch (IFM_SUBTYPE(ife->ifm_media)) {
3081 case IFM_AUTO:
3082 (void) tlp_pnic_nway_auto(sc, 1);
3083 break;
3084 case IFM_100_T4:
3085 /*
3086 * XXX Not supported as a manual setting right now.
3087 */
3088 return (EINVAL);
3089 default:
3090 /*
3091 * NWAY register data is stored in the ifmedia entry.
3092 */
3093 TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
3094 }
3095 break;
3096
3097 case MII_TICK:
3098 /*
3099 * Only used for autonegotiation.
3100 */
3101 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
3102 return (0);
3103
3104 /*
3105 * Check to see if we have link. If we do, we don't
3106 * need to restart the autonegotiation process.
3107 */
3108 if (sc->sc_flags & TULIPF_LINK_UP)
3109 return (0);
3110
3111 /*
3112 * Only retry autonegotiation every 5 seconds.
3113 */
3114 if (++sc->sc_nway_ticks != 5)
3115 return (0);
3116
3117 sc->sc_nway_ticks = 0;
3118 tlp_pnic_nway_reset(sc);
3119 if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
3120 return (0);
3121 break;
3122 }
3123
3124 /* Update the media status. */
3125 tlp_pnic_nway_status(sc);
3126
3127 /* Callback if something changed. */
3128 if (sc->sc_nway_active != mii->mii_media_active ||
3129 cmd == MII_MEDIACHG) {
3130 (*sc->sc_statchg)(&sc->sc_dev);
3131 sc->sc_nway_active = mii->mii_media_active;
3132 }
3133 return (0);
3134 }
3135
3136 void
3137 tlp_pnic_nway_reset(sc)
3138 struct tulip_softc *sc;
3139 {
3140
3141 TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
3142 delay(100);
3143 TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
3144 }
3145
3146 int
3147 tlp_pnic_nway_auto(sc, waitfor)
3148 struct tulip_softc *sc;
3149 int waitfor;
3150 {
3151 struct mii_data *mii = &sc->sc_mii;
3152 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
3153 u_int32_t reg;
3154 int i;
3155
3156 if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
3157 TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
3158
3159 if (waitfor) {
3160 /* Wait 500ms for it to complete. */
3161 for (i = 0; i < 500; i++) {
3162 reg = TULIP_READ(sc, CSR_PNIC_NWAY);
3163 if (reg & PNIC_NWAY_LPAR_MASK) {
3164 tlp_pnic_nway_acomp(sc);
3165 return (0);
3166 }
3167 delay(1000);
3168 }
3169 #if 0
3170 if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
3171 printf("%s: autonegotiation failed to complete\n",
3172 sc->sc_dev.dv_xname);
3173 #endif
3174
3175 /*
3176 * Don't need to worry about clearing DOINGAUTO.
3177 * If that's set, a timeout is pending, and it will
3178 * clear the flag.
3179 */
3180 return (EIO);
3181 }
3182
3183 /*
3184 * Just let it finish asynchronously. This is for the benefit of
3185 * the tick handler driving autonegotiation. Don't want 500ms
3186 * delays all the time while the system is running!
3187 */
3188 if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
3189 sc->sc_flags |= TULIPF_DOINGAUTO;
3190 timeout(tlp_pnic_nway_auto_timeout, sc, hz >> 1);
3191 }
3192 return (EJUSTRETURN);
3193 }
3194
3195 void
3196 tlp_pnic_nway_auto_timeout(arg)
3197 void *arg;
3198 {
3199 struct tulip_softc *sc = arg;
3200 u_int32_t reg;
3201 int s;
3202
3203 s = splnet();
3204 sc->sc_flags &= ~TULIPF_DOINGAUTO;
3205 reg = TULIP_READ(sc, CSR_PNIC_NWAY);
3206 #if 0
3207 if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
3208 printf("%s: autonegotiation failed to complete\n",
3209 sc->sc_dev.dv_xname);
3210 #endif
3211
3212 tlp_pnic_nway_acomp(sc);
3213
3214 /* Update the media status. */
3215 (void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
3216 splx(s);
3217 }
3218
3219 void
3220 tlp_pnic_nway_status(sc)
3221 struct tulip_softc *sc;
3222 {
3223 struct mii_data *mii = &sc->sc_mii;
3224 u_int32_t reg;
3225
3226 mii->mii_media_status = IFM_AVALID;
3227 mii->mii_media_active = IFM_ETHER;
3228
3229 reg = TULIP_READ(sc, CSR_PNIC_NWAY);
3230
3231 if (sc->sc_flags & TULIPF_LINK_UP)
3232 mii->mii_media_status |= IFM_ACTIVE;
3233
3234 if (reg & PNIC_NWAY_NW) {
3235 if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
3236 /* Erg, still trying, I guess... */
3237 mii->mii_media_active |= IFM_NONE;
3238 return;
3239 }
3240
3241 #if 0
3242 if (reg & PNIC_NWAY_LPAR100T4)
3243 mii->mii_media_active |= IFM_100_T4;
3244 else
3245 #endif
3246 if (reg & PNIC_NWAY_LPAR100TXFDX)
3247 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
3248 else if (reg & PNIC_NWAY_LPAR100TX)
3249 mii->mii_media_active |= IFM_100_TX;
3250 else if (reg & PNIC_NWAY_LPAR10TFDX)
3251 mii->mii_media_active |= IFM_10_T|IFM_FDX;
3252 else if (reg & PNIC_NWAY_LPAR10T)
3253 mii->mii_media_active |= IFM_10_T;
3254 else
3255 mii->mii_media_active |= IFM_NONE;
3256 } else {
3257 if (reg & PNIC_NWAY_100)
3258 mii->mii_media_active |= IFM_100_TX;
3259 else
3260 mii->mii_media_active |= IFM_10_T;
3261 if (reg & PNIC_NWAY_FD)
3262 mii->mii_media_active |= IFM_FDX;
3263 }
3264 }
3265
3266 void
3267 tlp_pnic_nway_acomp(sc)
3268 struct tulip_softc *sc;
3269 {
3270 u_int32_t reg;
3271
3272 reg = TULIP_READ(sc, CSR_PNIC_NWAY);
3273 reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
3274
3275 if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
3276 reg |= PNIC_NWAY_100;
3277 if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
3278 reg |= PNIC_NWAY_FD;
3279
3280 TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
3281 }
3282