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