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