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