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