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