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