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