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