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