Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / netif / de / if_de.c
CommitLineData
984263bc
MD
1/* $NetBSD: if_de.c,v 1.86 1999/06/01 19:17:59 thorpej Exp $ */
2
3/* $FreeBSD: src/sys/pci/if_de.c,v 1.123.2.4 2000/08/04 23:25:09 peter Exp $ */
4
5/*-
6 * Copyright (c) 1994-1997 Matt Thomas (matt@3am-software.com)
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software withough specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Id: if_de.c,v 1.94 1997/07/03 16:55:07 thomas Exp
29 *
30 */
31
32/*
33 * DEC 21040 PCI Ethernet Controller
34 *
35 * Written by Matt Thomas
36 * BPF support code stolen directly from if_ec.c
37 *
38 * This driver supports the DEC DE435 or any other PCI
39 * board which support 21040, 21041, or 21140 (mostly).
40 */
41#define TULIP_HDR_DATA
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/mbuf.h>
46#include <sys/socket.h>
47#include <sys/sockio.h>
48#include <sys/malloc.h>
49#include <sys/kernel.h>
50#include <sys/eventhandler.h>
51#include <machine/clock.h>
52#include <machine/bus.h>
53#include <machine/resource.h>
54#include <sys/bus.h>
55#include <sys/rman.h>
56
57#include "opt_inet.h"
58#include "opt_ipx.h"
59
60#include <net/if.h>
61#include <net/if_media.h>
62#include <net/if_dl.h>
63#ifdef TULIP_USE_SOFTINTR
64#include <net/netisr.h>
65#endif
66
67#include <net/bpf.h>
68
69#ifdef INET
70#include <netinet/in.h>
71#include <netinet/if_ether.h>
72#endif
73
74#ifdef IPX
75#include <netipx/ipx.h>
76#include <netipx/ipx_if.h>
77#endif
78
79#ifdef NS
80#include <netns/ns.h>
81#include <netns/ns_if.h>
82#endif
83
84#include <vm/vm.h>
85
86#include <net/if_var.h>
87#include <vm/pmap.h>
88#include <pci/pcivar.h>
89#include <pci/pcireg.h>
90#include <pci/dc21040reg.h>
91
92/*
93 * Intel CPUs should use I/O mapped access.
94 */
95#if defined(__i386__)
96#define TULIP_IOMAPPED
97#endif
98
99#if 0
100/*
101 * This turns on all sort of debugging stuff and make the
102 * driver much larger.
103 */
104#define TULIP_DEBUG
105#endif
106
107#if 0
108#define TULIP_PERFSTATS
109#endif
110
111#if 0
112#define TULIP_USE_SOFTINTR
113#endif
114
115#define TULIP_HZ 10
116
117#include <pci/if_devar.h>
118
119/*
120 * This module supports
121 * the DEC 21040 PCI Ethernet Controller.
122 * the DEC 21041 PCI Ethernet Controller.
123 * the DEC 21140 PCI Fast Ethernet Controller.
124 */
125static void tulip_mii_autonegotiate(tulip_softc_t * const sc, const unsigned phyaddr);
126static void tulip_intr_shared(void *arg);
127static void tulip_intr_normal(void *arg);
128static void tulip_init(tulip_softc_t * const sc);
129static void tulip_reset(tulip_softc_t * const sc);
130static void tulip_ifstart_one(struct ifnet *ifp);
131static void tulip_ifstart(struct ifnet *ifp);
132static struct mbuf *tulip_txput(tulip_softc_t * const sc, struct mbuf *m);
133static void tulip_txput_setup(tulip_softc_t * const sc);
134static void tulip_rx_intr(tulip_softc_t * const sc);
135static void tulip_addr_filter(tulip_softc_t * const sc);
136static unsigned tulip_mii_readreg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno);
137static void tulip_mii_writereg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno, unsigned data);
138static int tulip_mii_map_abilities(tulip_softc_t * const sc, unsigned abilities);
139static tulip_media_t tulip_mii_phy_readspecific(tulip_softc_t * const sc);
140static int tulip_srom_decode(tulip_softc_t * const sc);
141static int tulip_ifmedia_change(struct ifnet * const ifp);
142static void tulip_ifmedia_status(struct ifnet * const ifp, struct ifmediareq *req);
143/* static void tulip_21140_map_media(tulip_softc_t *sc); */
144\f
145static void
146tulip_timeout_callback(
147 void *arg)
148{
149 tulip_softc_t * const sc = arg;
150 int s = splimp();
151
152 TULIP_PERFSTART(timeout)
153
154 sc->tulip_flags &= ~TULIP_TIMEOUTPENDING;
155 sc->tulip_probe_timeout -= 1000 / TULIP_HZ;
156 (sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_TIMER);
157
158 TULIP_PERFEND(timeout);
159 splx(s);
160}
161
162static void
163tulip_timeout(
164 tulip_softc_t * const sc)
165{
166 if (sc->tulip_flags & TULIP_TIMEOUTPENDING)
167 return;
168 sc->tulip_flags |= TULIP_TIMEOUTPENDING;
169 timeout(tulip_timeout_callback, sc, (hz + TULIP_HZ / 2) / TULIP_HZ);
170}
171
172#if defined(TULIP_NEED_FASTTIMEOUT)
173static void
174tulip_fasttimeout_callback(
175 void *arg)
176{
177 tulip_softc_t * const sc = arg;
178 int s = splimp();
179
180 sc->tulip_flags &= ~TULIP_FASTTIMEOUTPENDING;
181 (sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_FASTTIMER);
182 splx(s);
183}
184
185static void
186tulip_fasttimeout(
187 tulip_softc_t * const sc)
188{
189 if (sc->tulip_flags & TULIP_FASTTIMEOUTPENDING)
190 return;
191 sc->tulip_flags |= TULIP_FASTTIMEOUTPENDING;
192 timeout(tulip_fasttimeout_callback, sc, 1);
193}
194#endif
195\f
196static int
197tulip_txprobe(
198 tulip_softc_t * const sc)
199{
200 struct mbuf *m;
201 /*
202 * Before we are sure this is the right media we need
203 * to send a small packet to make sure there's carrier.
204 * Strangely, BNC and AUI will "see" receive data if
205 * either is connected so the transmit is the only way
206 * to verify the connectivity.
207 */
208 MGETHDR(m, M_DONTWAIT, MT_DATA);
209 if (m == NULL)
210 return 0;
211 /*
212 * Construct a LLC TEST message which will point to ourselves.
213 */
214 bcopy(sc->tulip_enaddr, mtod(m, struct ether_header *)->ether_dhost, 6);
215 bcopy(sc->tulip_enaddr, mtod(m, struct ether_header *)->ether_shost, 6);
216 mtod(m, struct ether_header *)->ether_type = htons(3);
217 mtod(m, unsigned char *)[14] = 0;
218 mtod(m, unsigned char *)[15] = 0;
219 mtod(m, unsigned char *)[16] = 0xE3; /* LLC Class1 TEST (no poll) */
220 m->m_len = m->m_pkthdr.len = sizeof(struct ether_header) + 3;
221 /*
222 * send it!
223 */
224 sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
225 sc->tulip_intrmask |= TULIP_STS_TXINTR;
226 sc->tulip_flags |= TULIP_TXPROBE_ACTIVE;
227 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
228 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
229 if ((m = tulip_txput(sc, m)) != NULL)
230 m_freem(m);
231 sc->tulip_probe.probe_txprobes++;
232 return 1;
233}
234\f
235#ifdef BIG_PACKET
236#define TULIP_SIAGEN_WATCHDOG (sc->tulip_if.if_mtu > ETHERMTU ? TULIP_WATCHDOG_RXDISABLE|TULIP_WATCHDOG_TXDISABLE : 0)
237#else
238#define TULIP_SIAGEN_WATCHDOG 0
239#endif
240
241static void
242tulip_media_set(
243 tulip_softc_t * const sc,
244 tulip_media_t media)
245{
246 const tulip_media_info_t *mi = sc->tulip_mediums[media];
247
248 if (mi == NULL)
249 return;
250
251 /*
252 * If we are switching media, make sure we don't think there's
253 * any stale RX activity
254 */
255 sc->tulip_flags &= ~TULIP_RXACT;
256 if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
257 TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
258 TULIP_CSR_WRITE(sc, csr_sia_tx_rx, mi->mi_sia_tx_rx);
259 if (sc->tulip_features & TULIP_HAVE_SIAGP) {
260 TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_sia_gp_control|mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
261 DELAY(50);
262 TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_sia_gp_data|mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
263 } else {
264 TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
265 }
266 TULIP_CSR_WRITE(sc, csr_sia_connectivity, mi->mi_sia_connectivity);
267 } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
268#define TULIP_GPR_CMDBITS (TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER|TULIP_CMD_TXTHRSHLDCTL)
269 /*
270 * If the cmdmode bits don't match the currently operating mode,
271 * set the cmdmode appropriately and reset the chip.
272 */
273 if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
274 sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
275 sc->tulip_cmdmode |= mi->mi_cmdmode;
276 tulip_reset(sc);
277 }
278 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
279 DELAY(10);
280 TULIP_CSR_WRITE(sc, csr_gp, (u_int8_t) mi->mi_gpdata);
281 } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
282 /*
283 * If the cmdmode bits don't match the currently operating mode,
284 * set the cmdmode appropriately and reset the chip.
285 */
286 if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
287 sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
288 sc->tulip_cmdmode |= mi->mi_cmdmode;
289 tulip_reset(sc);
290 }
291 TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpcontrol);
292 TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpdata);
293 } else if (mi->mi_type == TULIP_MEDIAINFO_MII
294 && sc->tulip_probe_state != TULIP_PROBE_INACTIVE) {
295 int idx;
296 if (sc->tulip_features & TULIP_HAVE_SIAGP) {
297 const u_int8_t *dp;
298 dp = &sc->tulip_rombuf[mi->mi_reset_offset];
299 for (idx = 0; idx < mi->mi_reset_length; idx++, dp += 2) {
300 DELAY(10);
301 TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
302 }
303 sc->tulip_phyaddr = mi->mi_phyaddr;
304 dp = &sc->tulip_rombuf[mi->mi_gpr_offset];
305 for (idx = 0; idx < mi->mi_gpr_length; idx++, dp += 2) {
306 DELAY(10);
307 TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
308 }
309 } else {
310 for (idx = 0; idx < mi->mi_reset_length; idx++) {
311 DELAY(10);
312 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx]);
313 }
314 sc->tulip_phyaddr = mi->mi_phyaddr;
315 for (idx = 0; idx < mi->mi_gpr_length; idx++) {
316 DELAY(10);
317 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx]);
318 }
319 }
320 if (sc->tulip_flags & TULIP_TRYNWAY) {
321 tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
322 } else if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
323 u_int32_t data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_CONTROL);
324 data &= ~(PHYCTL_SELECT_100MB|PHYCTL_FULL_DUPLEX|PHYCTL_AUTONEG_ENABLE);
325 sc->tulip_flags &= ~TULIP_DIDNWAY;
326 if (TULIP_IS_MEDIA_FD(media))
327 data |= PHYCTL_FULL_DUPLEX;
328 if (TULIP_IS_MEDIA_100MB(media))
329 data |= PHYCTL_SELECT_100MB;
330 tulip_mii_writereg(sc, sc->tulip_phyaddr, PHYREG_CONTROL, data);
331 }
332 }
333}
334\f
335static void
336tulip_linkup(
337 tulip_softc_t * const sc,
338 tulip_media_t media)
339{
340 if ((sc->tulip_flags & TULIP_LINKUP) == 0)
341 sc->tulip_flags |= TULIP_PRINTLINKUP;
342 sc->tulip_flags |= TULIP_LINKUP;
343 sc->tulip_if.if_flags &= ~IFF_OACTIVE;
344#if 0 /* XXX how does with work with ifmedia? */
345 if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
346 if (sc->tulip_if.if_flags & IFF_FULLDUPLEX) {
347 if (TULIP_CAN_MEDIA_FD(media)
348 && sc->tulip_mediums[TULIP_FD_MEDIA_OF(media)] != NULL)
349 media = TULIP_FD_MEDIA_OF(media);
350 } else {
351 if (TULIP_IS_MEDIA_FD(media)
352 && sc->tulip_mediums[TULIP_HD_MEDIA_OF(media)] != NULL)
353 media = TULIP_HD_MEDIA_OF(media);
354 }
355 }
356#endif
357 if (sc->tulip_media != media) {
358#ifdef TULIP_DEBUG
359 sc->tulip_dbg.dbg_last_media = sc->tulip_media;
360#endif
361 sc->tulip_media = media;
362 sc->tulip_flags |= TULIP_PRINTMEDIA;
363 if (TULIP_IS_MEDIA_FD(sc->tulip_media)) {
364 sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
365 } else if (sc->tulip_chipid != TULIP_21041 || (sc->tulip_flags & TULIP_DIDNWAY) == 0) {
366 sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
367 }
368 }
369 /*
370 * We could set probe_timeout to 0 but setting to 3000 puts this
371 * in one central place and the only matters is tulip_link is
372 * followed by a tulip_timeout. Therefore setting it should not
373 * result in aberrant behavour.
374 */
375 sc->tulip_probe_timeout = 3000;
376 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
377 sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_TRYNWAY);
378 if (sc->tulip_flags & TULIP_INRESET) {
379 tulip_media_set(sc, sc->tulip_media);
380 } else if (sc->tulip_probe_media != sc->tulip_media) {
381 /*
382 * No reason to change media if we have the right media.
383 */
384 tulip_reset(sc);
385 }
386 tulip_init(sc);
387}
388\f
389static void
390tulip_media_print(
391 tulip_softc_t * const sc)
392{
393 if ((sc->tulip_flags & TULIP_LINKUP) == 0)
394 return;
395 if (sc->tulip_flags & TULIP_PRINTMEDIA) {
396 printf("%s%d: enabling %s port\n",
397 sc->tulip_name, sc->tulip_unit,
398 tulip_mediums[sc->tulip_media]);
399 sc->tulip_flags &= ~(TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
400 } else if (sc->tulip_flags & TULIP_PRINTLINKUP) {
401 printf("%s%d: link up\n", sc->tulip_name, sc->tulip_unit);
402 sc->tulip_flags &= ~TULIP_PRINTLINKUP;
403 }
404}
405\f
406#if defined(TULIP_DO_GPR_SENSE)
407static tulip_media_t
408tulip_21140_gpr_media_sense(
409 tulip_softc_t * const sc)
410{
411 tulip_media_t maybe_media = TULIP_MEDIA_UNKNOWN;
412 tulip_media_t last_media = TULIP_MEDIA_UNKNOWN;
413 tulip_media_t media;
414
415 /*
416 * If one of the media blocks contained a default media flag,
417 * use that.
418 */
419 for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
420 const tulip_media_info_t *mi;
421 /*
422 * Media is not supported (or is full-duplex).
423 */
424 if ((mi = sc->tulip_mediums[media]) == NULL || TULIP_IS_MEDIA_FD(media))
425 continue;
426 if (mi->mi_type != TULIP_MEDIAINFO_GPR)
427 continue;
428
429 /*
430 * Remember the media is this is the "default" media.
431 */
432 if (mi->mi_default && maybe_media == TULIP_MEDIA_UNKNOWN)
433 maybe_media = media;
434
435 /*
436 * No activity mask? Can't see if it is active if there's no mask.
437 */
438 if (mi->mi_actmask == 0)
439 continue;
440
441 /*
442 * Does the activity data match?
443 */
444 if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) != mi->mi_actdata)
445 continue;
446
447#if defined(TULIP_DEBUG)
448 printf("%s%d: gpr_media_sense: %s: 0x%02x & 0x%02x == 0x%02x\n",
449 sc->tulip_name, sc->tulip_unit, tulip_mediums[media],
450 TULIP_CSR_READ(sc, csr_gp) & 0xFF,
451 mi->mi_actmask, mi->mi_actdata);
452#endif
453 /*
454 * It does! If this is the first media we detected, then
455 * remember this media. If isn't the first, then there were
456 * multiple matches which we equate to no match (since we don't
457 * which to select (if any).
458 */
459 if (last_media == TULIP_MEDIA_UNKNOWN) {
460 last_media = media;
461 } else if (last_media != media) {
462 last_media = TULIP_MEDIA_UNKNOWN;
463 }
464 }
465 return (last_media != TULIP_MEDIA_UNKNOWN) ? last_media : maybe_media;
466}
467#endif /* TULIP_DO_GPR_SENSE */
468\f
469static tulip_link_status_t
470tulip_media_link_monitor(
471 tulip_softc_t * const sc)
472{
473 const tulip_media_info_t * const mi = sc->tulip_mediums[sc->tulip_media];
474 tulip_link_status_t linkup = TULIP_LINK_DOWN;
475
476 if (mi == NULL) {
477#if defined(DIAGNOSTIC) || defined(TULIP_DEBUG)
478 panic("tulip_media_link_monitor: %s: botch at line %d\n",
479 tulip_mediums[sc->tulip_media],__LINE__);
480#endif
481 return TULIP_LINK_UNKNOWN;
482 }
483
484
485 /*
486 * Have we seen some packets? If so, the link must be good.
487 */
488 if ((sc->tulip_flags & (TULIP_RXACT|TULIP_LINKUP)) == (TULIP_RXACT|TULIP_LINKUP)) {
489 sc->tulip_flags &= ~TULIP_RXACT;
490 sc->tulip_probe_timeout = 3000;
491 return TULIP_LINK_UP;
492 }
493
494 sc->tulip_flags &= ~TULIP_RXACT;
495 if (mi->mi_type == TULIP_MEDIAINFO_MII) {
496 u_int32_t status;
497 /*
498 * Read the PHY status register.
499 */
500 status = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
501 if (status & PHYSTS_AUTONEG_DONE) {
502 /*
503 * If the PHY has completed autonegotiation, see the if the
504 * remote systems abilities have changed. If so, upgrade or
505 * downgrade as appropriate.
506 */
507 u_int32_t abilities = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_AUTONEG_ABILITIES);
508 abilities = (abilities << 6) & status;
509 if (abilities != sc->tulip_abilities) {
510#if defined(TULIP_DEBUG)
511 loudprintf("%s%d(phy%d): autonegotiation changed: 0x%04x -> 0x%04x\n",
512 sc->tulip_name, sc->tulip_unit, sc->tulip_phyaddr,
513 sc->tulip_abilities, abilities);
514#endif
515 if (tulip_mii_map_abilities(sc, abilities)) {
516 tulip_linkup(sc, sc->tulip_probe_media);
517 return TULIP_LINK_UP;
518 }
519 /*
520 * if we had selected media because of autonegotiation,
521 * we need to probe for the new media.
522 */
523 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
524 if (sc->tulip_flags & TULIP_DIDNWAY)
525 return TULIP_LINK_DOWN;
526 }
527 }
528 /*
529 * The link is now up. If was down, say its back up.
530 */
531 if ((status & (PHYSTS_LINK_UP|PHYSTS_REMOTE_FAULT)) == PHYSTS_LINK_UP)
532 linkup = TULIP_LINK_UP;
533 } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
534 /*
535 * No activity sensor? Assume all's well.
536 */
537 if (mi->mi_actmask == 0)
538 return TULIP_LINK_UNKNOWN;
539 /*
540 * Does the activity data match?
541 */
542 if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) == mi->mi_actdata)
543 linkup = TULIP_LINK_UP;
544 } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
545 /*
546 * Assume non TP ok for now.
547 */
548 if (!TULIP_IS_MEDIA_TP(sc->tulip_media))
549 return TULIP_LINK_UNKNOWN;
550 if ((TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) == 0)
551 linkup = TULIP_LINK_UP;
552#if defined(TULIP_DEBUG)
553 if (sc->tulip_probe_timeout <= 0)
554 printf("%s%d: sia status = 0x%08x\n", sc->tulip_name,
555 sc->tulip_unit, TULIP_CSR_READ(sc, csr_sia_status));
556#endif
557 } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
558 return TULIP_LINK_UNKNOWN;
559 }
560 /*
561 * We will wait for 3 seconds until the link goes into suspect mode.
562 */
563 if (sc->tulip_flags & TULIP_LINKUP) {
564 if (linkup == TULIP_LINK_UP)
565 sc->tulip_probe_timeout = 3000;
566 if (sc->tulip_probe_timeout > 0)
567 return TULIP_LINK_UP;
568
569 sc->tulip_flags &= ~TULIP_LINKUP;
570 printf("%s%d: link down: cable problem?\n", sc->tulip_name, sc->tulip_unit);
571 }
572#if defined(TULIP_DEBUG)
573 sc->tulip_dbg.dbg_link_downed++;
574#endif
575 return TULIP_LINK_DOWN;
576}
577\f
578static void
579tulip_media_poll(
580 tulip_softc_t * const sc,
581 tulip_mediapoll_event_t event)
582{
583#if defined(TULIP_DEBUG)
584 sc->tulip_dbg.dbg_events[event]++;
585#endif
586 if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE
587 && event == TULIP_MEDIAPOLL_TIMER) {
588 switch (tulip_media_link_monitor(sc)) {
589 case TULIP_LINK_DOWN: {
590 /*
591 * Link Monitor failed. Probe for new media.
592 */
593 event = TULIP_MEDIAPOLL_LINKFAIL;
594 break;
595 }
596 case TULIP_LINK_UP: {
597 /*
598 * Check again soon.
599 */
600 tulip_timeout(sc);
601 return;
602 }
603 case TULIP_LINK_UNKNOWN: {
604 /*
605 * We can't tell so don't bother.
606 */
607 return;
608 }
609 }
610 }
611
612 if (event == TULIP_MEDIAPOLL_LINKFAIL) {
613 if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE) {
614 if (TULIP_DO_AUTOSENSE(sc)) {
615#if defined(TULIP_DEBUG)
616 sc->tulip_dbg.dbg_link_failures++;
617#endif
618 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
619 if (sc->tulip_if.if_flags & IFF_UP)
620 tulip_reset(sc); /* restart probe */
621 }
622 return;
623 }
624#if defined(TULIP_DEBUG)
625 sc->tulip_dbg.dbg_link_pollintrs++;
626#endif
627 }
628
629 if (event == TULIP_MEDIAPOLL_START) {
630 sc->tulip_if.if_flags |= IFF_OACTIVE;
631 if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE)
632 return;
633 sc->tulip_probe_mediamask = 0;
634 sc->tulip_probe_passes = 0;
635#if defined(TULIP_DEBUG)
636 sc->tulip_dbg.dbg_media_probes++;
637#endif
638 /*
639 * If the SROM contained an explicit media to use, use it.
640 */
641 sc->tulip_cmdmode &= ~(TULIP_CMD_RXRUN|TULIP_CMD_FULLDUPLEX);
642 sc->tulip_flags |= TULIP_TRYNWAY|TULIP_PROBE1STPASS;
643 sc->tulip_flags &= ~(TULIP_DIDNWAY|TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
644 /*
645 * connidx is defaulted to a media_unknown type.
646 */
647 sc->tulip_probe_media = tulip_srom_conninfo[sc->tulip_connidx].sc_media;
648 if (sc->tulip_probe_media != TULIP_MEDIA_UNKNOWN) {
649 tulip_linkup(sc, sc->tulip_probe_media);
650 tulip_timeout(sc);
651 return;
652 }
653
654 if (sc->tulip_features & TULIP_HAVE_GPR) {
655 sc->tulip_probe_state = TULIP_PROBE_GPRTEST;
656 sc->tulip_probe_timeout = 2000;
657 } else {
658 sc->tulip_probe_media = TULIP_MEDIA_MAX;
659 sc->tulip_probe_timeout = 0;
660 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
661 }
662 }
663
664 /*
665 * Ignore txprobe failures or spurious callbacks.
666 */
667 if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED
668 && sc->tulip_probe_state != TULIP_PROBE_MEDIATEST) {
669 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
670 return;
671 }
672
673 /*
674 * If we really transmitted a packet, then that's the media we'll use.
675 */
676 if (event == TULIP_MEDIAPOLL_TXPROBE_OK || event == TULIP_MEDIAPOLL_LINKPASS) {
677 if (event == TULIP_MEDIAPOLL_LINKPASS) {
678 /* XXX Check media status just to be sure */
679 sc->tulip_probe_media = TULIP_MEDIA_10BASET;
680#if defined(TULIP_DEBUG)
681 } else {
682 sc->tulip_dbg.dbg_txprobes_ok[sc->tulip_probe_media]++;
683#endif
684 }
685 tulip_linkup(sc, sc->tulip_probe_media);
686 tulip_timeout(sc);
687 return;
688 }
689
690 if (sc->tulip_probe_state == TULIP_PROBE_GPRTEST) {
691#if defined(TULIP_DO_GPR_SENSE)
692 /*
693 * Check for media via the general purpose register.
694 *
695 * Try to sense the media via the GPR. If the same value
696 * occurs 3 times in a row then just use that.
697 */
698 if (sc->tulip_probe_timeout > 0) {
699 tulip_media_t new_probe_media = tulip_21140_gpr_media_sense(sc);
700#if defined(TULIP_DEBUG)
701 printf("%s%d: media_poll: gpr sensing = %s\n",
702 sc->tulip_name, sc->tulip_unit, tulip_mediums[new_probe_media]);
703#endif
704 if (new_probe_media != TULIP_MEDIA_UNKNOWN) {
705 if (new_probe_media == sc->tulip_probe_media) {
706 if (--sc->tulip_probe_count == 0)
707 tulip_linkup(sc, sc->tulip_probe_media);
708 } else {
709 sc->tulip_probe_count = 10;
710 }
711 }
712 sc->tulip_probe_media = new_probe_media;
713 tulip_timeout(sc);
714 return;
715 }
716#endif /* TULIP_DO_GPR_SENSE */
717 /*
718 * Brute force. We cycle through each of the media types
719 * and try to transmit a packet.
720 */
721 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
722 sc->tulip_probe_media = TULIP_MEDIA_MAX;
723 sc->tulip_probe_timeout = 0;
724 tulip_timeout(sc);
725 return;
726 }
727
728 if (sc->tulip_probe_state != TULIP_PROBE_MEDIATEST
729 && (sc->tulip_features & TULIP_HAVE_MII)) {
730 tulip_media_t old_media = sc->tulip_probe_media;
731 tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
732 switch (sc->tulip_probe_state) {
733 case TULIP_PROBE_FAILED:
734 case TULIP_PROBE_MEDIATEST: {
735 /*
736 * Try the next media.
737 */
738 sc->tulip_probe_mediamask |= sc->tulip_mediums[sc->tulip_probe_media]->mi_mediamask;
739 sc->tulip_probe_timeout = 0;
740#ifdef notyet
741 if (sc->tulip_probe_state == TULIP_PROBE_FAILED)
742 break;
743 if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
744 break;
745 sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 300;
746#endif
747 break;
748 }
749 case TULIP_PROBE_PHYAUTONEG: {
750 return;
751 }
752 case TULIP_PROBE_INACTIVE: {
753 /*
754 * Only probe if we autonegotiated a media that hasn't failed.
755 */
756 sc->tulip_probe_timeout = 0;
757 if (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media)) {
758 sc->tulip_probe_media = old_media;
759 break;
760 }
761 tulip_linkup(sc, sc->tulip_probe_media);
762 tulip_timeout(sc);
763 return;
764 }
765 default: {
766#if defined(DIAGNOSTIC) || defined(TULIP_DEBUG)
767 panic("tulip_media_poll: botch at line %d\n", __LINE__);
768#endif
769 break;
770 }
771 }
772 }
773
774 if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED) {
775#if defined(TULIP_DEBUG)
776 sc->tulip_dbg.dbg_txprobes_failed[sc->tulip_probe_media]++;
777#endif
778 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
779 return;
780 }
781
782 /*
783 * switch to another media if we tried this one enough.
784 */
785 if (/* event == TULIP_MEDIAPOLL_TXPROBE_FAILED || */ sc->tulip_probe_timeout <= 0) {
786#if defined(TULIP_DEBUG)
787 if (sc->tulip_probe_media == TULIP_MEDIA_UNKNOWN) {
788 printf("%s%d: poll media unknown!\n",
789 sc->tulip_name, sc->tulip_unit);
790 sc->tulip_probe_media = TULIP_MEDIA_MAX;
791 }
792#endif
793 /*
794 * Find the next media type to check for. Full Duplex
795 * types are not allowed.
796 */
797 do {
798 sc->tulip_probe_media -= 1;
799 if (sc->tulip_probe_media == TULIP_MEDIA_UNKNOWN) {
800 if (++sc->tulip_probe_passes == 3) {
801 printf("%s%d: autosense failed: cable problem?\n",
802 sc->tulip_name, sc->tulip_unit);
803 if ((sc->tulip_if.if_flags & IFF_UP) == 0) {
804 sc->tulip_if.if_flags &= ~IFF_RUNNING;
805 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
806 return;
807 }
808 }
809 sc->tulip_flags ^= TULIP_TRYNWAY; /* XXX */
810 sc->tulip_probe_mediamask = 0;
811 sc->tulip_probe_media = TULIP_MEDIA_MAX - 1;
812 }
813 } while (sc->tulip_mediums[sc->tulip_probe_media] == NULL
814 || (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media))
815 || TULIP_IS_MEDIA_FD(sc->tulip_probe_media));
816
817#if defined(TULIP_DEBUG)
818 printf("%s%d: %s: probing %s\n", sc->tulip_name, sc->tulip_unit,
819 event == TULIP_MEDIAPOLL_TXPROBE_FAILED ? "txprobe failed" : "timeout",
820 tulip_mediums[sc->tulip_probe_media]);
821#endif
822 sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 1000;
823 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
824 sc->tulip_probe.probe_txprobes = 0;
825 tulip_reset(sc);
826 tulip_media_set(sc, sc->tulip_probe_media);
827 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
828 }
829 tulip_timeout(sc);
830
831 /*
832 * If this is hanging off a phy, we know are doing NWAY and we have
833 * forced the phy to a specific speed. Wait for link up before
834 * before sending a packet.
835 */
836 switch (sc->tulip_mediums[sc->tulip_probe_media]->mi_type) {
837 case TULIP_MEDIAINFO_MII: {
838 if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
839 return;
840 break;
841 }
842 case TULIP_MEDIAINFO_SIA: {
843 if (TULIP_IS_MEDIA_TP(sc->tulip_probe_media)) {
844 if (TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL)
845 return;
846 tulip_linkup(sc, sc->tulip_probe_media);
847#ifdef notyet
848 if (sc->tulip_features & TULIP_HAVE_MII)
849 tulip_timeout(sc);
850#endif
851 return;
852 }
853 break;
854 }
855 case TULIP_MEDIAINFO_RESET:
856 case TULIP_MEDIAINFO_SYM:
857 case TULIP_MEDIAINFO_NONE:
858 case TULIP_MEDIAINFO_GPR: {
859 break;
860 }
861 }
862 /*
863 * Try to send a packet.
864 */
865 tulip_txprobe(sc);
866}
867\f
868static void
869tulip_media_select(
870 tulip_softc_t * const sc)
871{
872 if (sc->tulip_features & TULIP_HAVE_GPR) {
873 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
874 DELAY(10);
875 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpdata);
876 }
877 /*
878 * If this board has no media, just return
879 */
880 if (sc->tulip_features & TULIP_HAVE_NOMEDIA)
881 return;
882
883 if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
884 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
885 (*sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_START);
886 } else {
887 tulip_media_set(sc, sc->tulip_media);
888 }
889}
890\f
891static void
892tulip_21040_mediainfo_init(
893 tulip_softc_t * const sc,
894 tulip_media_t media)
895{
896 sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_THRSHLD160
897 |TULIP_CMD_BACKOFFCTR;
898 sc->tulip_if.if_baudrate = 10000000;
899
900 if (media == TULIP_MEDIA_10BASET || media == TULIP_MEDIA_UNKNOWN) {
901 TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[0], 21040, 10BASET);
902 TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[1], 21040, 10BASET_FD);
903 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
904 }
905
906 if (media == TULIP_MEDIA_AUIBNC || media == TULIP_MEDIA_UNKNOWN) {
907 TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[2], 21040, AUIBNC);
908 }
909
910 if (media == TULIP_MEDIA_UNKNOWN) {
911 TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[3], 21040, EXTSIA);
912 }
913}
914
915static void
916tulip_21040_media_probe(
917 tulip_softc_t * const sc)
918{
919 tulip_21040_mediainfo_init(sc, TULIP_MEDIA_UNKNOWN);
920 return;
921}
922
923static void
924tulip_21040_10baset_only_media_probe(
925 tulip_softc_t * const sc)
926{
927 tulip_21040_mediainfo_init(sc, TULIP_MEDIA_10BASET);
928 tulip_media_set(sc, TULIP_MEDIA_10BASET);
929 sc->tulip_media = TULIP_MEDIA_10BASET;
930}
931
932static void
933tulip_21040_10baset_only_media_select(
934 tulip_softc_t * const sc)
935{
936 sc->tulip_flags |= TULIP_LINKUP;
937 if (sc->tulip_media == TULIP_MEDIA_10BASET_FD) {
938 sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
939 sc->tulip_flags &= ~TULIP_SQETEST;
940 } else {
941 sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
942 sc->tulip_flags |= TULIP_SQETEST;
943 }
944 tulip_media_set(sc, sc->tulip_media);
945}
946
947static void
948tulip_21040_auibnc_only_media_probe(
949 tulip_softc_t * const sc)
950{
951 tulip_21040_mediainfo_init(sc, TULIP_MEDIA_AUIBNC);
952 sc->tulip_flags |= TULIP_SQETEST|TULIP_LINKUP;
953 tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
954 sc->tulip_media = TULIP_MEDIA_AUIBNC;
955}
956
957static void
958tulip_21040_auibnc_only_media_select(
959 tulip_softc_t * const sc)
960{
961 tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
962 sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
963}
964
965static const tulip_boardsw_t tulip_21040_boardsw = {
966 TULIP_21040_GENERIC,
967 tulip_21040_media_probe,
968 tulip_media_select,
969 tulip_media_poll,
970};
971
972static const tulip_boardsw_t tulip_21040_10baset_only_boardsw = {
973 TULIP_21040_GENERIC,
974 tulip_21040_10baset_only_media_probe,
975 tulip_21040_10baset_only_media_select,
976 NULL,
977};
978
979static const tulip_boardsw_t tulip_21040_auibnc_only_boardsw = {
980 TULIP_21040_GENERIC,
981 tulip_21040_auibnc_only_media_probe,
982 tulip_21040_auibnc_only_media_select,
983 NULL,
984};
985\f
986static void
987tulip_21041_mediainfo_init(
988 tulip_softc_t * const sc)
989{
990 tulip_media_info_t * const mi = sc->tulip_mediainfo;
991
992#ifdef notyet
993 if (sc->tulip_revinfo >= 0x20) {
994 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, 10BASET);
995 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, 10BASET_FD);
996 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, AUI);
997 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, BNC);
998 return;
999 }
1000#endif
1001 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041, 10BASET);
1002 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041, 10BASET_FD);
1003 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[2], 21041, AUI);
1004 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[3], 21041, BNC);
1005}
1006\f
1007static void
1008tulip_21041_media_probe(
1009 tulip_softc_t * const sc)
1010{
1011 sc->tulip_if.if_baudrate = 10000000;
1012 sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_ENHCAPTEFFCT
1013 |TULIP_CMD_THRSHLD160|TULIP_CMD_BACKOFFCTR;
1014 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
1015 tulip_21041_mediainfo_init(sc);
1016}
1017
1018static void
1019tulip_21041_media_poll(
1020 tulip_softc_t * const sc,
1021 const tulip_mediapoll_event_t event)
1022{
1023 u_int32_t sia_status;
1024
1025#if defined(TULIP_DEBUG)
1026 sc->tulip_dbg.dbg_events[event]++;
1027#endif
1028
1029 if (event == TULIP_MEDIAPOLL_LINKFAIL) {
1030 if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE
1031 || !TULIP_DO_AUTOSENSE(sc))
1032 return;
1033 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
1034 tulip_reset(sc); /* start probe */
1035 return;
1036 }
1037
1038 /*
1039 * If we've been been asked to start a poll or link change interrupt
1040 * restart the probe (and reset the tulip to a known state).
1041 */
1042 if (event == TULIP_MEDIAPOLL_START) {
1043 sc->tulip_if.if_flags |= IFF_OACTIVE;
1044 sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_RXRUN);
1045#ifdef notyet
1046 if (sc->tulip_revinfo >= 0x20) {
1047 sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
1048 sc->tulip_flags |= TULIP_DIDNWAY;
1049 }
1050#endif
1051 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1052 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1053 sc->tulip_probe_media = TULIP_MEDIA_10BASET;
1054 sc->tulip_probe_timeout = TULIP_21041_PROBE_10BASET_TIMEOUT;
1055 tulip_media_set(sc, TULIP_MEDIA_10BASET);
1056 tulip_timeout(sc);
1057 return;
1058 }
1059
1060 if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
1061 return;
1062
1063 if (event == TULIP_MEDIAPOLL_TXPROBE_OK) {
1064#if defined(TULIP_DEBUG)
1065 sc->tulip_dbg.dbg_txprobes_ok[sc->tulip_probe_media]++;
1066#endif
1067 tulip_linkup(sc, sc->tulip_probe_media);
1068 return;
1069 }
1070
1071 sia_status = TULIP_CSR_READ(sc, csr_sia_status);
1072 TULIP_CSR_WRITE(sc, csr_sia_status, sia_status);
1073 if ((sia_status & TULIP_SIASTS_LINKFAIL) == 0) {
1074 if (sc->tulip_revinfo >= 0x20) {
1075 if (sia_status & (PHYSTS_10BASET_FD << (16 - 6)))
1076 sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
1077 }
1078 /*
1079 * If the link has passed LinkPass, 10baseT is the
1080 * proper media to use.
1081 */
1082 tulip_linkup(sc, sc->tulip_probe_media);
1083 return;
1084 }
1085
1086 /*
1087 * wait for up to 2.4 seconds for the link to reach pass state.
1088 * Only then start scanning the other media for activity.
1089 * choose media with receive activity over those without.
1090 */
1091 if (sc->tulip_probe_media == TULIP_MEDIA_10BASET) {
1092 if (event != TULIP_MEDIAPOLL_TIMER)
1093 return;
1094 if (sc->tulip_probe_timeout > 0
1095 && (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) == 0) {
1096 tulip_timeout(sc);
1097 return;
1098 }
1099 sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1100 sc->tulip_flags |= TULIP_WANTRXACT;
1101 if (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) {
1102 sc->tulip_probe_media = TULIP_MEDIA_BNC;
1103 } else {
1104 sc->tulip_probe_media = TULIP_MEDIA_AUI;
1105 }
1106 tulip_media_set(sc, sc->tulip_probe_media);
1107 tulip_timeout(sc);
1108 return;
1109 }
1110
1111 /*
1112 * If we failed, clear the txprobe active flag.
1113 */
1114 if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED)
1115 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1116
1117
1118 if (event == TULIP_MEDIAPOLL_TIMER) {
1119 /*
1120 * If we've received something, then that's our link!
1121 */
1122 if (sc->tulip_flags & TULIP_RXACT) {
1123 tulip_linkup(sc, sc->tulip_probe_media);
1124 return;
1125 }
1126 /*
1127 * if no txprobe active
1128 */
1129 if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0
1130 && ((sc->tulip_flags & TULIP_WANTRXACT) == 0
1131 || (sia_status & TULIP_SIASTS_RXACTIVITY))) {
1132 sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1133 tulip_txprobe(sc);
1134 tulip_timeout(sc);
1135 return;
1136 }
1137 /*
1138 * Take 2 passes through before deciding to not
1139 * wait for receive activity. Then take another
1140 * two passes before spitting out a warning.
1141 */
1142 if (sc->tulip_probe_timeout <= 0) {
1143 if (sc->tulip_flags & TULIP_WANTRXACT) {
1144 sc->tulip_flags &= ~TULIP_WANTRXACT;
1145 sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1146 } else {
1147 printf("%s%d: autosense failed: cable problem?\n",
1148 sc->tulip_name, sc->tulip_unit);
1149 if ((sc->tulip_if.if_flags & IFF_UP) == 0) {
1150 sc->tulip_if.if_flags &= ~IFF_RUNNING;
1151 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1152 return;
1153 }
1154 }
1155 }
1156 }
1157
1158 /*
1159 * Since this media failed to probe, try the other one.
1160 */
1161 sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1162 if (sc->tulip_probe_media == TULIP_MEDIA_AUI) {
1163 sc->tulip_probe_media = TULIP_MEDIA_BNC;
1164 } else {
1165 sc->tulip_probe_media = TULIP_MEDIA_AUI;
1166 }
1167 tulip_media_set(sc, sc->tulip_probe_media);
1168 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1169 tulip_timeout(sc);
1170}
1171
1172static const tulip_boardsw_t tulip_21041_boardsw = {
1173 TULIP_21041_GENERIC,
1174 tulip_21041_media_probe,
1175 tulip_media_select,
1176 tulip_21041_media_poll
1177};
1178\f
1179static const tulip_phy_attr_t tulip_mii_phy_attrlist[] = {
1180 { 0x20005c00, 0, /* 08-00-17 */
1181 {
1182 { 0x19, 0x0040, 0x0040 }, /* 10TX */
1183 { 0x19, 0x0040, 0x0000 }, /* 100TX */
1184 },
1185#if defined(TULIP_DEBUG)
1186 "NS DP83840",
1187#endif
1188 },
1189 { 0x0281F400, 0, /* 00-A0-7D */
1190 {
1191 { 0x12, 0x0010, 0x0000 }, /* 10T */
1192 { }, /* 100TX */
1193 { 0x12, 0x0010, 0x0010 }, /* 100T4 */
1194 { 0x12, 0x0008, 0x0008 }, /* FULL_DUPLEX */
1195 },
1196#if defined(TULIP_DEBUG)
1197 "Seeq 80C240"
1198#endif
1199 },
1200#if 0
1201 { 0x0015F420, 0, /* 00-A0-7D */
1202 {
1203 { 0x12, 0x0010, 0x0000 }, /* 10T */
1204 { }, /* 100TX */
1205 { 0x12, 0x0010, 0x0010 }, /* 100T4 */
1206 { 0x12, 0x0008, 0x0008 }, /* FULL_DUPLEX */
1207 },
1208#if defined(TULIP_DEBUG)
1209 "Broadcom BCM5000"
1210#endif
1211 },
1212#endif
1213 { 0x0281F400, 0, /* 00-A0-BE */
1214 {
1215 { 0x11, 0x8000, 0x0000 }, /* 10T */
1216 { 0x11, 0x8000, 0x8000 }, /* 100TX */
1217 { }, /* 100T4 */
1218 { 0x11, 0x4000, 0x4000 }, /* FULL_DUPLEX */
1219 },
1220#if defined(TULIP_DEBUG)
1221 "ICS 1890"
1222#endif
1223 },
1224 { 0 }
1225};
1226\f
1227static tulip_media_t
1228tulip_mii_phy_readspecific(
1229 tulip_softc_t * const sc)
1230{
1231 const tulip_phy_attr_t *attr;
1232 u_int16_t data;
1233 u_int32_t id;
1234 unsigned idx = 0;
1235 static const tulip_media_t table[] = {
1236 TULIP_MEDIA_UNKNOWN,
1237 TULIP_MEDIA_10BASET,
1238 TULIP_MEDIA_100BASETX,
1239 TULIP_MEDIA_100BASET4,
1240 TULIP_MEDIA_UNKNOWN,
1241 TULIP_MEDIA_10BASET_FD,
1242 TULIP_MEDIA_100BASETX_FD,
1243 TULIP_MEDIA_UNKNOWN
1244 };
1245
1246 /*
1247 * Don't read phy specific registers if link is not up.
1248 */
1249 data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
1250 if ((data & (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS)) != (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS))
1251 return TULIP_MEDIA_UNKNOWN;
1252
1253 id = (tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDLOW) << 16) |
1254 tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDHIGH);
1255 for (attr = tulip_mii_phy_attrlist;; attr++) {
1256 if (attr->attr_id == 0)
1257 return TULIP_MEDIA_UNKNOWN;
1258 if ((id & ~0x0F) == attr->attr_id)
1259 break;
1260 }
1261
1262 if (attr->attr_modes[PHY_MODE_100TX].pm_regno) {
1263 const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100TX];
1264 data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1265 if ((data & pm->pm_mask) == pm->pm_value)
1266 idx = 2;
1267 }
1268 if (idx == 0 && attr->attr_modes[PHY_MODE_100T4].pm_regno) {
1269 const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100T4];
1270 data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1271 if ((data & pm->pm_mask) == pm->pm_value)
1272 idx = 3;
1273 }
1274 if (idx == 0 && attr->attr_modes[PHY_MODE_10T].pm_regno) {
1275 const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_10T];
1276 data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1277 if ((data & pm->pm_mask) == pm->pm_value)
1278 idx = 1;
1279 }
1280 if (idx != 0 && attr->attr_modes[PHY_MODE_FULLDUPLEX].pm_regno) {
1281 const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_FULLDUPLEX];
1282 data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1283 idx += ((data & pm->pm_mask) == pm->pm_value ? 4 : 0);
1284 }
1285 return table[idx];
1286}
1287\f
1288static unsigned
1289tulip_mii_get_phyaddr(
1290 tulip_softc_t * const sc,
1291 unsigned offset)
1292{
1293 unsigned phyaddr;
1294
1295 for (phyaddr = 1; phyaddr < 32; phyaddr++) {
1296 unsigned status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1297 if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
1298 continue;
1299 if (offset == 0)
1300 return phyaddr;
1301 offset--;
1302 }
1303 if (offset == 0) {
1304 unsigned status = tulip_mii_readreg(sc, 0, PHYREG_STATUS);
1305 if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
1306 return TULIP_MII_NOPHY;
1307 return 0;
1308 }
1309 return TULIP_MII_NOPHY;
1310}
1311\f
1312static int
1313tulip_mii_map_abilities(
1314 tulip_softc_t * const sc,
1315 unsigned abilities)
1316{
1317 sc->tulip_abilities = abilities;
1318 if (abilities & PHYSTS_100BASETX_FD) {
1319 sc->tulip_probe_media = TULIP_MEDIA_100BASETX_FD;
1320 } else if (abilities & PHYSTS_100BASET4) {
1321 sc->tulip_probe_media = TULIP_MEDIA_100BASET4;
1322 } else if (abilities & PHYSTS_100BASETX) {
1323 sc->tulip_probe_media = TULIP_MEDIA_100BASETX;
1324 } else if (abilities & PHYSTS_10BASET_FD) {
1325 sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
1326 } else if (abilities & PHYSTS_10BASET) {
1327 sc->tulip_probe_media = TULIP_MEDIA_10BASET;
1328 } else {
1329 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1330 return 0;
1331 }
1332 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1333 return 1;
1334}
1335
1336static void
1337tulip_mii_autonegotiate(
1338 tulip_softc_t * const sc,
1339 const unsigned phyaddr)
1340{
1341 switch (sc->tulip_probe_state) {
1342 case TULIP_PROBE_MEDIATEST:
1343 case TULIP_PROBE_INACTIVE: {
1344 sc->tulip_flags |= TULIP_DIDNWAY;
1345 tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, PHYCTL_RESET);
1346 sc->tulip_probe_timeout = 3000;
1347 sc->tulip_intrmask |= TULIP_STS_ABNRMLINTR|TULIP_STS_NORMALINTR;
1348 sc->tulip_probe_state = TULIP_PROBE_PHYRESET;
1349 /* FALL THROUGH */
1350 }
1351 case TULIP_PROBE_PHYRESET: {
1352 u_int32_t status;
1353 u_int32_t data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
1354 if (data & PHYCTL_RESET) {
1355 if (sc->tulip_probe_timeout > 0) {
1356 tulip_timeout(sc);
1357 return;
1358 }
1359 printf("%s%d(phy%d): error: reset of PHY never completed!\n",
1360 sc->tulip_name, sc->tulip_unit, phyaddr);
1361 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1362 sc->tulip_probe_state = TULIP_PROBE_FAILED;
1363 sc->tulip_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1364 return;
1365 }
1366 status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1367 if ((status & PHYSTS_CAN_AUTONEG) == 0) {
1368#if defined(TULIP_DEBUG)
1369 loudprintf("%s%d(phy%d): autonegotiation disabled\n",
1370 sc->tulip_name, sc->tulip_unit, phyaddr);
1371#endif
1372 sc->tulip_flags &= ~TULIP_DIDNWAY;
1373 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1374 return;
1375 }
1376 if (tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT) != ((status >> 6) | 0x01))
1377 tulip_mii_writereg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT, (status >> 6) | 0x01);
1378 tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, data|PHYCTL_AUTONEG_RESTART|PHYCTL_AUTONEG_ENABLE);
1379 data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
1380#if defined(TULIP_DEBUG)
1381 if ((data & PHYCTL_AUTONEG_ENABLE) == 0)
1382 loudprintf("%s%d(phy%d): oops: enable autonegotiation failed: 0x%04x\n",
1383 sc->tulip_name, sc->tulip_unit, phyaddr, data);
1384 else
1385 loudprintf("%s%d(phy%d): autonegotiation restarted: 0x%04x\n",
1386 sc->tulip_name, sc->tulip_unit, phyaddr, data);
1387 sc->tulip_dbg.dbg_nway_starts++;
1388#endif
1389 sc->tulip_probe_state = TULIP_PROBE_PHYAUTONEG;
1390 sc->tulip_probe_timeout = 3000;
1391 /* FALL THROUGH */
1392 }
1393 case TULIP_PROBE_PHYAUTONEG: {
1394 u_int32_t status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1395 u_int32_t data;
1396 if ((status & PHYSTS_AUTONEG_DONE) == 0) {
1397 if (sc->tulip_probe_timeout > 0) {
1398 tulip_timeout(sc);
1399 return;
1400 }
1401#if defined(TULIP_DEBUG)
1402 loudprintf("%s%d(phy%d): autonegotiation timeout: sts=0x%04x, ctl=0x%04x\n",
1403 sc->tulip_name, sc->tulip_unit, phyaddr, status,
1404 tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL));
1405#endif
1406 sc->tulip_flags &= ~TULIP_DIDNWAY;
1407 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1408 return;
1409 }
1410 data = tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ABILITIES);
1411#if defined(TULIP_DEBUG)
1412 loudprintf("%s%d(phy%d): autonegotiation complete: 0x%04x\n",
1413 sc->tulip_name, sc->tulip_unit, phyaddr, data);
1414#endif
1415 data = (data << 6) & status;
1416 if (!tulip_mii_map_abilities(sc, data))
1417 sc->tulip_flags &= ~TULIP_DIDNWAY;
1418 return;
1419 }
1420 default: {
1421#if defined(DIAGNOSTIC)
1422 panic("tulip_media_poll: botch at line %d\n", __LINE__);
1423#endif
1424 break;
1425 }
1426 }
1427#if defined(TULIP_DEBUG)
1428 loudprintf("%s%d(phy%d): autonegotiation failure: state = %d\n",
1429 sc->tulip_name, sc->tulip_unit, phyaddr, sc->tulip_probe_state);
1430 sc->tulip_dbg.dbg_nway_failures++;
1431#endif
1432}
1433\f
1434static void
1435tulip_2114x_media_preset(
1436 tulip_softc_t * const sc)
1437{
1438 const tulip_media_info_t *mi = NULL;
1439 tulip_media_t media = sc->tulip_media;
1440
1441 if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
1442 media = sc->tulip_media;
1443 else
1444 media = sc->tulip_probe_media;
1445
1446 sc->tulip_cmdmode &= ~TULIP_CMD_PORTSELECT;
1447 sc->tulip_flags &= ~TULIP_SQETEST;
1448 if (media != TULIP_MEDIA_UNKNOWN && media != TULIP_MEDIA_MAX) {
1449#if defined(TULIP_DEBUG)
1450 if (media < TULIP_MEDIA_MAX && sc->tulip_mediums[media] != NULL) {
1451#endif
1452 mi = sc->tulip_mediums[media];
1453 if (mi->mi_type == TULIP_MEDIAINFO_MII) {
1454 sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
1455 } else if (mi->mi_type == TULIP_MEDIAINFO_GPR
1456 || mi->mi_type == TULIP_MEDIAINFO_SYM) {
1457 sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
1458 sc->tulip_cmdmode |= mi->mi_cmdmode;
1459 } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
1460 TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
1461 }
1462#if defined(TULIP_DEBUG)
1463 } else {
1464 printf("%s%d: preset: bad media %d!\n",
1465 sc->tulip_name, sc->tulip_unit, media);
1466 }
1467#endif
1468 }
1469 switch (media) {
1470 case TULIP_MEDIA_BNC:
1471 case TULIP_MEDIA_AUI:
1472 case TULIP_MEDIA_10BASET: {
1473 sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
1474 sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
1475 sc->tulip_if.if_baudrate = 10000000;
1476 sc->tulip_flags |= TULIP_SQETEST;
1477 break;
1478 }
1479 case TULIP_MEDIA_10BASET_FD: {
1480 sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL;
1481 sc->tulip_if.if_baudrate = 10000000;
1482 break;
1483 }
1484 case TULIP_MEDIA_100BASEFX:
1485 case TULIP_MEDIA_100BASET4:
1486 case TULIP_MEDIA_100BASETX: {
1487 sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL);
1488 sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
1489 sc->tulip_if.if_baudrate = 100000000;
1490 break;
1491 }
1492 case TULIP_MEDIA_100BASEFX_FD:
1493 case TULIP_MEDIA_100BASETX_FD: {
1494 sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_PORTSELECT;
1495 sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
1496 sc->tulip_if.if_baudrate = 100000000;
1497 break;
1498 }
1499 default: {
1500 break;
1501 }
1502 }
1503 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1504}
1505\f
1506/*
1507 ********************************************************************
1508 * Start of 21140/21140A support which does not use the MII interface
1509 */
1510\f
1511static void
1512tulip_null_media_poll(
1513 tulip_softc_t * const sc,
1514 tulip_mediapoll_event_t event)
1515{
1516#if defined(TULIP_DEBUG)
1517 sc->tulip_dbg.dbg_events[event]++;
1518#endif
1519#if defined(DIAGNOSTIC)
1520 printf("%s%d: botch(media_poll) at line %d\n",
1521 sc->tulip_name, sc->tulip_unit, __LINE__);
1522#endif
1523}
1524
1525__inline__ static void
1526tulip_21140_mediainit(
1527 tulip_softc_t * const sc,
1528 tulip_media_info_t * const mip,
1529 tulip_media_t const media,
1530 unsigned gpdata,
1531 unsigned cmdmode)
1532{
1533 sc->tulip_mediums[media] = mip;
1534 mip->mi_type = TULIP_MEDIAINFO_GPR;
1535 mip->mi_cmdmode = cmdmode;
1536 mip->mi_gpdata = gpdata;
1537}
1538\f
1539static void
1540tulip_21140_evalboard_media_probe(
1541 tulip_softc_t * const sc)
1542{
1543 tulip_media_info_t *mip = sc->tulip_mediainfo;
1544
1545 sc->tulip_gpinit = TULIP_GP_EB_PINS;
1546 sc->tulip_gpdata = TULIP_GP_EB_INIT;
1547 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
1548 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
1549 TULIP_CSR_WRITE(sc, csr_command,
1550 TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1551 TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1552 TULIP_CSR_WRITE(sc, csr_command,
1553 TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1554 DELAY(1000000);
1555 if ((TULIP_CSR_READ(sc, csr_gp) & TULIP_GP_EB_OK100) != 0) {
1556 sc->tulip_media = TULIP_MEDIA_10BASET;
1557 } else {
1558 sc->tulip_media = TULIP_MEDIA_100BASETX;
1559 }
1560 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1561 TULIP_GP_EB_INIT,
1562 TULIP_CMD_TXTHRSHLDCTL);
1563 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1564 TULIP_GP_EB_INIT,
1565 TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1566 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1567 TULIP_GP_EB_INIT,
1568 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1569 |TULIP_CMD_SCRAMBLER);
1570 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1571 TULIP_GP_EB_INIT,
1572 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1573 |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1574}
1575
1576static const tulip_boardsw_t tulip_21140_eb_boardsw = {
1577 TULIP_21140_DEC_EB,
1578 tulip_21140_evalboard_media_probe,
1579 tulip_media_select,
1580 tulip_null_media_poll,
1581 tulip_2114x_media_preset,
1582};
1583\f
1584static void
1585tulip_21140_accton_media_probe(
1586 tulip_softc_t * const sc)
1587{
1588 tulip_media_info_t *mip = sc->tulip_mediainfo;
1589 unsigned gpdata;
1590
1591 sc->tulip_gpinit = TULIP_GP_EB_PINS;
1592 sc->tulip_gpdata = TULIP_GP_EB_INIT;
1593 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
1594 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
1595 TULIP_CSR_WRITE(sc, csr_command,
1596 TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1597 TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1598 TULIP_CSR_WRITE(sc, csr_command,
1599 TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1600 DELAY(1000000);
1601 gpdata = TULIP_CSR_READ(sc, csr_gp);
1602 if ((gpdata & TULIP_GP_EN1207_UTP_INIT) == 0) {
1603 sc->tulip_media = TULIP_MEDIA_10BASET;
1604 } else {
1605 if ((gpdata & TULIP_GP_EN1207_BNC_INIT) == 0) {
1606 sc->tulip_media = TULIP_MEDIA_BNC;
1607 } else {
1608 sc->tulip_media = TULIP_MEDIA_100BASETX;
1609 }
1610 }
1611 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_BNC,
1612 TULIP_GP_EN1207_BNC_INIT,
1613 TULIP_CMD_TXTHRSHLDCTL);
1614 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1615 TULIP_GP_EN1207_UTP_INIT,
1616 TULIP_CMD_TXTHRSHLDCTL);
1617 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1618 TULIP_GP_EN1207_UTP_INIT,
1619 TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1620 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1621 TULIP_GP_EN1207_100_INIT,
1622 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1623 |TULIP_CMD_SCRAMBLER);
1624 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1625 TULIP_GP_EN1207_100_INIT,
1626 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1627 |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1628}
1629
1630static const tulip_boardsw_t tulip_21140_accton_boardsw = {
1631 TULIP_21140_EN1207,
1632 tulip_21140_accton_media_probe,
1633 tulip_media_select,
1634 tulip_null_media_poll,
1635 tulip_2114x_media_preset,
1636};
1637\f
1638static void
1639tulip_21140_smc9332_media_probe(
1640 tulip_softc_t * const sc)
1641{
1642 tulip_media_info_t *mip = sc->tulip_mediainfo;
1643 int idx, cnt = 0;
1644
1645 TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT|TULIP_CMD_MUSTBEONE);
1646 TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
1647 DELAY(10); /* Wait 10 microseconds (actually 50 PCI cycles but at
1648 33MHz that comes to two microseconds but wait a
1649 bit longer anyways) */
1650 TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT |
1651 TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1652 sc->tulip_gpinit = TULIP_GP_SMC_9332_PINS;
1653 sc->tulip_gpdata = TULIP_GP_SMC_9332_INIT;
1654 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_PINS|TULIP_GP_PINSET);
1655 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_INIT);
1656 DELAY(200000);
1657 for (idx = 1000; idx > 0; idx--) {
1658 u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
1659 if ((csr & (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) == (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) {
1660 if (++cnt > 100)
1661 break;
1662 } else if ((csr & TULIP_GP_SMC_9332_OK10) == 0) {
1663 break;
1664 } else {
1665 cnt = 0;
1666 }
1667 DELAY(1000);
1668 }
1669 sc->tulip_media = cnt > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
1670 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1671 TULIP_GP_SMC_9332_INIT,
1672 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1673 |TULIP_CMD_SCRAMBLER);
1674 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1675 TULIP_GP_SMC_9332_INIT,
1676 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1677 |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1678 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1679 TULIP_GP_SMC_9332_INIT,
1680 TULIP_CMD_TXTHRSHLDCTL);
1681 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1682 TULIP_GP_SMC_9332_INIT,
1683 TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1684}
1685
1686static const tulip_boardsw_t tulip_21140_smc9332_boardsw = {
1687 TULIP_21140_SMC_9332,
1688 tulip_21140_smc9332_media_probe,
1689 tulip_media_select,
1690 tulip_null_media_poll,
1691 tulip_2114x_media_preset,
1692};
1693\f
1694static void
1695tulip_21140_cogent_em100_media_probe(
1696 tulip_softc_t * const sc)
1697{
1698 tulip_media_info_t *mip = sc->tulip_mediainfo;
1699 u_int32_t cmdmode = TULIP_CSR_READ(sc, csr_command);
1700
1701 sc->tulip_gpinit = TULIP_GP_EM100_PINS;
1702 sc->tulip_gpdata = TULIP_GP_EM100_INIT;
1703 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_PINS);
1704 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_INIT);
1705
1706 cmdmode = TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_MUSTBEONE;
1707 cmdmode &= ~(TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_SCRAMBLER);
1708 if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
1709 TULIP_CSR_WRITE(sc, csr_command, cmdmode);
1710 sc->tulip_media = TULIP_MEDIA_100BASEFX;
1711
1712 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX,
1713 TULIP_GP_EM100_INIT,
1714 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION);
1715 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX_FD,
1716 TULIP_GP_EM100_INIT,
1717 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1718 |TULIP_CMD_FULLDUPLEX);
1719 } else {
1720 TULIP_CSR_WRITE(sc, csr_command, cmdmode|TULIP_CMD_SCRAMBLER);
1721 sc->tulip_media = TULIP_MEDIA_100BASETX;
1722 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1723 TULIP_GP_EM100_INIT,
1724 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1725 |TULIP_CMD_SCRAMBLER);
1726 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1727 TULIP_GP_EM100_INIT,
1728 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1729 |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1730 }
1731}
1732
1733static const tulip_boardsw_t tulip_21140_cogent_em100_boardsw = {
1734 TULIP_21140_COGENT_EM100,
1735 tulip_21140_cogent_em100_media_probe,
1736 tulip_media_select,
1737 tulip_null_media_poll,
1738 tulip_2114x_media_preset
1739};
1740\f
1741static void
1742tulip_21140_znyx_zx34x_media_probe(
1743 tulip_softc_t * const sc)
1744{
1745 tulip_media_info_t *mip = sc->tulip_mediainfo;
1746 int cnt10 = 0, cnt100 = 0, idx;
1747
1748 sc->tulip_gpinit = TULIP_GP_ZX34X_PINS;
1749 sc->tulip_gpdata = TULIP_GP_ZX34X_INIT;
1750 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_PINS);
1751 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_INIT);
1752 TULIP_CSR_WRITE(sc, csr_command,
1753 TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1754 TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1755 TULIP_CSR_WRITE(sc, csr_command,
1756 TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1757
1758 DELAY(200000);
1759 for (idx = 1000; idx > 0; idx--) {
1760 u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
1761 if ((csr & (TULIP_GP_ZX34X_LNKFAIL|TULIP_GP_ZX34X_SYMDET|TULIP_GP_ZX34X_SIGDET)) == (TULIP_GP_ZX34X_LNKFAIL|TULIP_GP_ZX34X_SYMDET|TULIP_GP_ZX34X_SIGDET)) {
1762 if (++cnt100 > 100)
1763 break;
1764 } else if ((csr & TULIP_GP_ZX34X_LNKFAIL) == 0) {
1765 if (++cnt10 > 100)
1766 break;
1767 } else {
1768 cnt10 = 0;
1769 cnt100 = 0;
1770 }
1771 DELAY(1000);
1772 }
1773 sc->tulip_media = cnt100 > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
1774 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1775 TULIP_GP_ZX34X_INIT,
1776 TULIP_CMD_TXTHRSHLDCTL);
1777 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1778 TULIP_GP_ZX34X_INIT,
1779 TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1780 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1781 TULIP_GP_ZX34X_INIT,
1782 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1783 |TULIP_CMD_SCRAMBLER);
1784 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1785 TULIP_GP_ZX34X_INIT,
1786 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1787 |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1788}
1789
1790static const tulip_boardsw_t tulip_21140_znyx_zx34x_boardsw = {
1791 TULIP_21140_ZNYX_ZX34X,
1792 tulip_21140_znyx_zx34x_media_probe,
1793 tulip_media_select,
1794 tulip_null_media_poll,
1795 tulip_2114x_media_preset,
1796};
1797\f
1798static void
1799tulip_2114x_media_probe(
1800 tulip_softc_t * const sc)
1801{
1802 sc->tulip_cmdmode |= TULIP_CMD_MUSTBEONE
1803 |TULIP_CMD_BACKOFFCTR|TULIP_CMD_THRSHLD72;
1804}
1805
1806static const tulip_boardsw_t tulip_2114x_isv_boardsw = {
1807 TULIP_21140_ISV,
1808 tulip_2114x_media_probe,
1809 tulip_media_select,
1810 tulip_media_poll,
1811 tulip_2114x_media_preset,
1812};
1813\f
1814/*
1815 * ******** END of chip-specific handlers. ***********
1816 */
1817\f
1818/*
1819 * Code the read the SROM and MII bit streams (I2C)
1820 */
1821#define EMIT do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); DELAY(1); } while (0)
1822
1823static void
1824tulip_srom_idle(
1825 tulip_softc_t * const sc)
1826{
1827 unsigned bit, csr;
1828
1829 csr = SROMSEL ; EMIT;
1830 csr = SROMSEL | SROMRD; EMIT;
1831 csr ^= SROMCS; EMIT;
1832 csr ^= SROMCLKON; EMIT;
1833
1834 /*
1835 * Write 25 cycles of 0 which will force the SROM to be idle.
1836 */
1837 for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
1838 csr ^= SROMCLKOFF; EMIT; /* clock low; data not valid */
1839 csr ^= SROMCLKON; EMIT; /* clock high; data valid */
1840 }
1841 csr ^= SROMCLKOFF; EMIT;
1842 csr ^= SROMCS; EMIT;
1843 csr = 0; EMIT;
1844}
1845
1846
1847static void
1848tulip_srom_read(
1849 tulip_softc_t * const sc)
1850{
1851 unsigned idx;
1852 const unsigned bitwidth = SROM_BITWIDTH;
1853 const unsigned cmdmask = (SROMCMD_RD << bitwidth);
1854 const unsigned msb = 1 << (bitwidth + 3 - 1);
1855 unsigned lastidx = (1 << bitwidth) - 1;
1856
1857 tulip_srom_idle(sc);
1858
1859 for (idx = 0; idx <= lastidx; idx++) {
1860 unsigned lastbit, data, bits, bit, csr;
1861 csr = SROMSEL ; EMIT;
1862 csr = SROMSEL | SROMRD; EMIT;
1863 csr ^= SROMCSON; EMIT;
1864 csr ^= SROMCLKON; EMIT;
1865
1866 lastbit = 0;
1867 for (bits = idx|cmdmask, bit = bitwidth + 3; bit > 0; bit--, bits <<= 1) {
1868 const unsigned thisbit = bits & msb;
1869 csr ^= SROMCLKOFF; EMIT; /* clock low; data not valid */
1870 if (thisbit != lastbit) {
1871 csr ^= SROMDOUT; EMIT; /* clock low; invert data */
1872 } else {
1873 EMIT;
1874 }
1875 csr ^= SROMCLKON; EMIT; /* clock high; data valid */
1876 lastbit = thisbit;
1877 }
1878 csr ^= SROMCLKOFF; EMIT;
1879
1880 for (data = 0, bits = 0; bits < 16; bits++) {
1881 data <<= 1;
1882 csr ^= SROMCLKON; EMIT; /* clock high; data valid */
1883 data |= TULIP_CSR_READ(sc, csr_srom_mii) & SROMDIN ? 1 : 0;
1884 csr ^= SROMCLKOFF; EMIT; /* clock low; data not valid */
1885 }
1886 sc->tulip_rombuf[idx*2] = data & 0xFF;
1887 sc->tulip_rombuf[idx*2+1] = data >> 8;
1888 csr = SROMSEL | SROMRD; EMIT;
1889 csr = 0; EMIT;
1890 }
1891 tulip_srom_idle(sc);
1892}
1893\f
1894#define MII_EMIT do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); DELAY(1); } while (0)
1895
1896static void
1897tulip_mii_writebits(
1898 tulip_softc_t * const sc,
1899 unsigned data,
1900 unsigned bits)
1901{
1902 unsigned msb = 1 << (bits - 1);
1903 unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1904 unsigned lastbit = (csr & MII_DOUT) ? msb : 0;
1905
1906 csr |= MII_WR; MII_EMIT; /* clock low; assert write */
1907
1908 for (; bits > 0; bits--, data <<= 1) {
1909 const unsigned thisbit = data & msb;
1910 if (thisbit != lastbit) {
1911 csr ^= MII_DOUT; MII_EMIT; /* clock low; invert data */
1912 }
1913 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
1914 lastbit = thisbit;
1915 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
1916 }
1917}
1918
1919static void
1920tulip_mii_turnaround(
1921 tulip_softc_t * const sc,
1922 unsigned cmd)
1923{
1924 unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1925
1926 if (cmd == MII_WRCMD) {
1927 csr |= MII_DOUT; MII_EMIT; /* clock low; change data */
1928 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
1929 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
1930 csr ^= MII_DOUT; MII_EMIT; /* clock low; change data */
1931 } else {
1932 csr |= MII_RD; MII_EMIT; /* clock low; switch to read */
1933 }
1934 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
1935 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
1936}
1937
1938static unsigned
1939tulip_mii_readbits(
1940 tulip_softc_t * const sc)
1941{
1942 unsigned data;
1943 unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1944 int idx;
1945
1946 for (idx = 0, data = 0; idx < 16; idx++) {
1947 data <<= 1; /* this is NOOP on the first pass through */
1948 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
1949 if (TULIP_CSR_READ(sc, csr_srom_mii) & MII_DIN)
1950 data |= 1;
1951 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
1952 }
1953 csr ^= MII_RD; MII_EMIT; /* clock low; turn off read */
1954
1955 return data;
1956}
1957
1958static unsigned
1959tulip_mii_readreg(
1960 tulip_softc_t * const sc,
1961 unsigned devaddr,
1962 unsigned regno)
1963{
1964 unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1965 unsigned data;
1966
1967 csr &= ~(MII_RD|MII_CLK); MII_EMIT;
1968 tulip_mii_writebits(sc, MII_PREAMBLE, 32);
1969 tulip_mii_writebits(sc, MII_RDCMD, 8);
1970 tulip_mii_writebits(sc, devaddr, 5);
1971 tulip_mii_writebits(sc, regno, 5);
1972 tulip_mii_turnaround(sc, MII_RDCMD);
1973
1974 data = tulip_mii_readbits(sc);
1975#if defined(TULIP_DEBUG)
1976 sc->tulip_dbg.dbg_phyregs[regno][0] = data;
1977 sc->tulip_dbg.dbg_phyregs[regno][1]++;
1978#endif
1979 return data;
1980}
1981
1982static void
1983tulip_mii_writereg(
1984 tulip_softc_t * const sc,
1985 unsigned devaddr,
1986 unsigned regno,
1987 unsigned data)
1988{
1989 unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1990 csr &= ~(MII_RD|MII_CLK); MII_EMIT;
1991 tulip_mii_writebits(sc, MII_PREAMBLE, 32);
1992 tulip_mii_writebits(sc, MII_WRCMD, 8);
1993 tulip_mii_writebits(sc, devaddr, 5);
1994 tulip_mii_writebits(sc, regno, 5);
1995 tulip_mii_turnaround(sc, MII_WRCMD);
1996 tulip_mii_writebits(sc, data, 16);
1997#if defined(TULIP_DEBUG)
1998 sc->tulip_dbg.dbg_phyregs[regno][2] = data;
1999 sc->tulip_dbg.dbg_phyregs[regno][3]++;
2000#endif
2001}
2002\f
2003#define tulip_mchash(mca) (tulip_crc32(mca, 6) & 0x1FF)
2004#define tulip_srom_crcok(databuf) ( \
2005 ((tulip_crc32(databuf, 126) & 0xFFFFU) ^ 0xFFFFU) == \
2006 ((databuf)[126] | ((databuf)[127] << 8)))
2007
2008static unsigned
2009tulip_crc32(
2010 const unsigned char *databuf,
2011 size_t datalen)
2012{
2013 u_int idx, crc = 0xFFFFFFFFUL;
2014 static const u_int crctab[] = {
2015 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
2016 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
2017 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
2018 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
2019 };
2020
2021 for (idx = 0; idx < datalen; idx++) {
2022 crc ^= *databuf++;
2023 crc = (crc >> 4) ^ crctab[crc & 0xf];
2024 crc = (crc >> 4) ^ crctab[crc & 0xf];
2025 }
2026 return crc;
2027}
2028\f
2029static void
2030tulip_identify_dec_nic(
2031 tulip_softc_t * const sc)
2032{
2033 strcpy(sc->tulip_boardid, "DEC ");
2034#define D0 4
2035 if (sc->tulip_chipid <= TULIP_21040)
2036 return;
2037 if (bcmp(sc->tulip_rombuf + 29, "DE500", 5) == 0
2038 || bcmp(sc->tulip_rombuf + 29, "DE450", 5) == 0) {
2039 bcopy(sc->tulip_rombuf + 29, &sc->tulip_boardid[D0], 8);
2040 sc->tulip_boardid[D0+8] = ' ';
2041 }
2042#undef D0
2043}
2044\f
2045static void
2046tulip_identify_znyx_nic(
2047 tulip_softc_t * const sc)
2048{
2049 unsigned id = 0;
2050 strcpy(sc->tulip_boardid, "ZNYX ZX3XX ");
2051 if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
2052 unsigned znyx_ptr;
2053 sc->tulip_boardid[8] = '4';
2054 znyx_ptr = sc->tulip_rombuf[124] + 256 * sc->tulip_rombuf[125];
2055 if (znyx_ptr < 26 || znyx_ptr > 116) {
2056 sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2057 return;
2058 }
2059 /* ZX344 = 0010 .. 0013FF
2060 */
2061 if (sc->tulip_rombuf[znyx_ptr] == 0x4A
2062 && sc->tulip_rombuf[znyx_ptr + 1] == 0x52
2063 && sc->tulip_rombuf[znyx_ptr + 2] == 0x01) {
2064 id = sc->tulip_rombuf[znyx_ptr + 5] + 256 * sc->tulip_rombuf[znyx_ptr + 4];
2065 if ((id >> 8) == (TULIP_ZNYX_ID_ZX342 >> 8)) {
2066 sc->tulip_boardid[9] = '2';
2067 if (id == TULIP_ZNYX_ID_ZX342B) {
2068 sc->tulip_boardid[10] = 'B';
2069 sc->tulip_boardid[11] = ' ';
2070 }
2071 sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2072 } else if (id == TULIP_ZNYX_ID_ZX344) {
2073 sc->tulip_boardid[10] = '4';
2074 sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2075 } else if (id == TULIP_ZNYX_ID_ZX345) {
2076 sc->tulip_boardid[9] = (sc->tulip_rombuf[19] > 1) ? '8' : '5';
2077 } else if (id == TULIP_ZNYX_ID_ZX346) {
2078 sc->tulip_boardid[9] = '6';
2079 } else if (id == TULIP_ZNYX_ID_ZX351) {
2080 sc->tulip_boardid[8] = '5';
2081 sc->tulip_boardid[9] = '1';
2082 }
2083 }
2084 if (id == 0) {
2085 /*
2086 * Assume it's a ZX342...
2087 */
2088 sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2089 }
2090 return;
2091 }
2092 sc->tulip_boardid[8] = '1';
2093 if (sc->tulip_chipid == TULIP_21041) {
2094 sc->tulip_boardid[10] = '1';
2095 return;
2096 }
2097 if (sc->tulip_rombuf[32] == 0x4A && sc->tulip_rombuf[33] == 0x52) {
2098 id = sc->tulip_rombuf[37] + 256 * sc->tulip_rombuf[36];
2099 if (id == TULIP_ZNYX_ID_ZX312T) {
2100 sc->tulip_boardid[9] = '2';
2101 sc->tulip_boardid[10] = 'T';
2102 sc->tulip_boardid[11] = ' ';
2103 sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2104 } else if (id == TULIP_ZNYX_ID_ZX314_INTA) {
2105 sc->tulip_boardid[9] = '4';
2106 sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2107 sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2108 } else if (id == TULIP_ZNYX_ID_ZX314) {
2109 sc->tulip_boardid[9] = '4';
2110 sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2111 sc->tulip_features |= TULIP_HAVE_BASEROM;
2112 } else if (id == TULIP_ZNYX_ID_ZX315_INTA) {
2113 sc->tulip_boardid[9] = '5';
2114 sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2115 } else if (id == TULIP_ZNYX_ID_ZX315) {
2116 sc->tulip_boardid[9] = '5';
2117 sc->tulip_features |= TULIP_HAVE_BASEROM;
2118 } else {
2119 id = 0;
2120 }
2121 }
2122 if (id == 0) {
2123 if ((sc->tulip_enaddr[3] & ~3) == 0xF0 && (sc->tulip_enaddr[5] & 2) == 0) {
2124 sc->tulip_boardid[9] = '4';
2125 sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2126 sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2127 } else if ((sc->tulip_enaddr[3] & ~3) == 0xF4 && (sc->tulip_enaddr[5] & 1) == 0) {
2128 sc->tulip_boardid[9] = '5';
2129 sc->tulip_boardsw = &tulip_21040_boardsw;
2130 sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2131 } else if ((sc->tulip_enaddr[3] & ~3) == 0xEC) {
2132 sc->tulip_boardid[9] = '2';
2133 sc->tulip_boardsw = &tulip_21040_boardsw;
2134 }
2135 }
2136}
2137\f
2138static void
2139tulip_identify_smc_nic(
2140 tulip_softc_t * const sc)
2141{
2142 u_int32_t id1, id2, ei;
2143 int auibnc = 0, utp = 0;
2144 char *cp;
2145
2146 strcpy(sc->tulip_boardid, "SMC ");
2147 if (sc->tulip_chipid == TULIP_21041)
2148 return;
2149 if (sc->tulip_chipid != TULIP_21040) {
2150 if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
2151 strcpy(&sc->tulip_boardid[4], "9332DST ");
2152 sc->tulip_boardsw = &tulip_21140_smc9332_boardsw;
2153 } else if (sc->tulip_features & (TULIP_HAVE_BASEROM|TULIP_HAVE_SLAVEDROM)) {
2154 strcpy(&sc->tulip_boardid[4], "9334BDT ");
2155 } else {
2156 strcpy(&sc->tulip_boardid[4], "9332BDT ");
2157 }
2158 return;
2159 }
2160 id1 = sc->tulip_rombuf[0x60] | (sc->tulip_rombuf[0x61] << 8);
2161 id2 = sc->tulip_rombuf[0x62] | (sc->tulip_rombuf[0x63] << 8);
2162 ei = sc->tulip_rombuf[0x66] | (sc->tulip_rombuf[0x67] << 8);
2163
2164 strcpy(&sc->tulip_boardid[4], "8432");
2165 cp = &sc->tulip_boardid[8];
2166 if ((id1 & 1) == 0)
2167 *cp++ = 'B', auibnc = 1;
2168 if ((id1 & 0xFF) > 0x32)
2169 *cp++ = 'T', utp = 1;
2170 if ((id1 & 0x4000) == 0)
2171 *cp++ = 'A', auibnc = 1;
2172 if (id2 == 0x15) {
2173 sc->tulip_boardid[7] = '4';
2174 *cp++ = '-';
2175 *cp++ = 'C';
2176 *cp++ = 'H';
2177 *cp++ = (ei ? '2' : '1');
2178 }
2179 *cp++ = ' ';
2180 *cp = '\0';
2181 if (utp && !auibnc)
2182 sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2183 else if (!utp && auibnc)
2184 sc->tulip_boardsw = &tulip_21040_auibnc_only_boardsw;
2185}
2186\f
2187static void
2188tulip_identify_cogent_nic(
2189 tulip_softc_t * const sc)
2190{
2191 strcpy(sc->tulip_boardid, "Cogent ");
2192 if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
2193 if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100TX_ID) {
2194 strcat(sc->tulip_boardid, "EM100TX ");
2195 sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
2196#if defined(TULIP_COGENT_EM110TX_ID)
2197 } else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM110TX_ID) {
2198 strcat(sc->tulip_boardid, "EM110TX ");
2199 sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
2200#endif
2201 } else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
2202 strcat(sc->tulip_boardid, "EM100FX ");
2203 sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
2204 }
2205 /*
2206 * Magic number (0x24001109U) is the SubVendor (0x2400) and
2207 * SubDevId (0x1109) for the ANA6944TX (EM440TX).
2208 */
2209 if (*(u_int32_t *) sc->tulip_rombuf == 0x24001109U
2210 && (sc->tulip_features & TULIP_HAVE_BASEROM)) {
2211 /*
2212 * Cogent (Adaptec) is still mapping all INTs to INTA of
2213 * first 21140. Dumb! Dumb!
2214 */
2215 strcat(sc->tulip_boardid, "EM440TX ");
2216 sc->tulip_features |= TULIP_HAVE_SHAREDINTR;
2217 }
2218 } else if (sc->tulip_chipid == TULIP_21040) {
2219 sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2220 }
2221}
2222\f
2223static void
2224tulip_identify_accton_nic(
2225 tulip_softc_t * const sc)
2226{
2227 strcpy(sc->tulip_boardid, "ACCTON ");
2228 switch (sc->tulip_chipid) {
2229 case TULIP_21140A:
2230 strcat(sc->tulip_boardid, "EN1207 ");
2231 if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
2232 sc->tulip_boardsw = &tulip_21140_accton_boardsw;
2233 break;
2234 case TULIP_21140:
2235 strcat(sc->tulip_boardid, "EN1207TX ");
2236 if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
2237 sc->tulip_boardsw = &tulip_21140_eb_boardsw;
2238 break;
2239 case TULIP_21040:
2240 strcat(sc->tulip_boardid, "EN1203 ");
2241 sc->tulip_boardsw = &tulip_21040_boardsw;
2242 break;
2243 case TULIP_21041:
2244 strcat(sc->tulip_boardid, "EN1203 ");
2245 sc->tulip_boardsw = &tulip_21041_boardsw;
2246 break;
2247 default:
2248 sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2249 break;
2250 }
2251}
2252\f
2253static void
2254tulip_identify_asante_nic(
2255 tulip_softc_t * const sc)
2256{
2257 strcpy(sc->tulip_boardid, "Asante ");
2258 if ((sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A)
2259 && sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
2260 tulip_media_info_t *mi = sc->tulip_mediainfo;
2261 int idx;
2262 /*
2263 * The Asante Fast Ethernet doesn't always ship with a valid
2264 * new format SROM. So if isn't in the new format, we cheat
2265 * set it up as if we had.
2266 */
2267
2268 sc->tulip_gpinit = TULIP_GP_ASANTE_PINS;
2269 sc->tulip_gpdata = 0;
2270
2271 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PINS|TULIP_GP_PINSET);
2272 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PHYRESET);
2273 DELAY(100);
2274 TULIP_CSR_WRITE(sc, csr_gp, 0);
2275
2276 mi->mi_type = TULIP_MEDIAINFO_MII;
2277 mi->mi_gpr_length = 0;
2278 mi->mi_gpr_offset = 0;
2279 mi->mi_reset_length = 0;
2280 mi->mi_reset_offset = 0;;
2281
2282 mi->mi_phyaddr = TULIP_MII_NOPHY;
2283 for (idx = 20; idx > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx--) {
2284 DELAY(10000);
2285 mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, 0);
2286 }
2287 if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
2288 printf("%s%d: can't find phy 0\n", sc->tulip_name, sc->tulip_unit);
2289 return;
2290 }
2291
2292 sc->tulip_features |= TULIP_HAVE_MII;
2293 mi->mi_capabilities = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
2294 mi->mi_advertisement = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
2295 mi->mi_full_duplex = PHYSTS_10BASET_FD|PHYSTS_100BASETX_FD;
2296 mi->mi_tx_threshold = PHYSTS_10BASET|PHYSTS_10BASET_FD;
2297 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2298 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2299 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2300 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2301 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2302 mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2303 tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2304
2305 sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2306 }
2307}
2308\f
2309static void
2310tulip_identify_compex_nic(
2311 tulip_softc_t * const sc)
2312{
2313 strcpy(sc->tulip_boardid, "COMPEX ");
2314 if (sc->tulip_chipid == TULIP_21140A) {
2315 int root_unit;
2316 tulip_softc_t *root_sc = NULL;
2317
2318 strcat(sc->tulip_boardid, "400TX/PCI ");
2319 /*
2320 * All 4 chips on these boards share an interrupt. This code
2321 * copied from tulip_read_macaddr.
2322 */
2323 sc->tulip_features |= TULIP_HAVE_SHAREDINTR;
2324 for (root_unit = sc->tulip_unit - 1; root_unit >= 0; root_unit--) {
2325 root_sc = tulips[root_unit];
2326 if (root_sc == NULL
2327 || !(root_sc->tulip_features & TULIP_HAVE_SLAVEDINTR))
2328 break;
2329 root_sc = NULL;
2330 }
2331 if (root_sc != NULL
2332 && root_sc->tulip_chipid == sc->tulip_chipid
2333 && root_sc->tulip_pci_busno == sc->tulip_pci_busno) {
2334 sc->tulip_features |= TULIP_HAVE_SLAVEDINTR;
2335 sc->tulip_slaves = root_sc->tulip_slaves;
2336 root_sc->tulip_slaves = sc;
2337 } else if(sc->tulip_features & TULIP_HAVE_SLAVEDINTR) {
2338 printf("\nCannot find master device for de%d interrupts",
2339 sc->tulip_unit);
2340 }
2341 } else {
2342 strcat(sc->tulip_boardid, "unknown ");
2343 }
2344 /* sc->tulip_boardsw = &tulip_21140_eb_boardsw; */
2345 return;
2346}
2347\f
2348static int
2349tulip_srom_decode(
2350 tulip_softc_t * const sc)
2351{
2352 unsigned idx1, idx2, idx3;
2353
2354 const tulip_srom_header_t *shp = (const tulip_srom_header_t *) &sc->tulip_rombuf[0];
2355 const tulip_srom_adapter_info_t *saip = (const tulip_srom_adapter_info_t *) (shp + 1);
2356 tulip_srom_media_t srom_media;
2357 tulip_media_info_t *mi = sc->tulip_mediainfo;
2358 const u_int8_t *dp;
2359 u_int32_t leaf_offset, blocks, data;
2360
2361 for (idx1 = 0; idx1 < shp->sh_adapter_count; idx1++, saip++) {
2362 if (shp->sh_adapter_count == 1)
2363 break;
2364 if (saip->sai_device == sc->tulip_pci_devno)
2365 break;
2366 }
2367 /*
2368 * Didn't find the right media block for this card.
2369 */
2370 if (idx1 == shp->sh_adapter_count)
2371 return 0;
2372
2373 /*
2374 * Save the hardware address.
2375 */
2376 bcopy(shp->sh_ieee802_address, sc->tulip_enaddr, 6);
2377 /*
2378 * If this is a multiple port card, add the adapter index to the last
2379 * byte of the hardware address. (if it isn't multiport, adding 0
2380 * won't hurt.
2381 */
2382 sc->tulip_enaddr[5] += idx1;
2383
2384 leaf_offset = saip->sai_leaf_offset_lowbyte
2385 + saip->sai_leaf_offset_highbyte * 256;
2386 dp = sc->tulip_rombuf + leaf_offset;
2387
2388 sc->tulip_conntype = (tulip_srom_connection_t) (dp[0] + dp[1] * 256); dp += 2;
2389
2390 for (idx2 = 0;; idx2++) {
2391 if (tulip_srom_conninfo[idx2].sc_type == sc->tulip_conntype
2392 || tulip_srom_conninfo[idx2].sc_type == TULIP_SROM_CONNTYPE_NOT_USED)
2393 break;
2394 }
2395 sc->tulip_connidx = idx2;
2396
2397 if (sc->tulip_chipid == TULIP_21041) {
2398 blocks = *dp++;
2399 for (idx2 = 0; idx2 < blocks; idx2++) {
2400 tulip_media_t media;
2401 data = *dp++;
2402 srom_media = (tulip_srom_media_t) (data & 0x3F);
2403 for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2404 if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2405 break;
2406 }
2407 media = tulip_srom_mediums[idx3].sm_type;
2408 if (media != TULIP_MEDIA_UNKNOWN) {
2409 if (data & TULIP_SROM_21041_EXTENDED) {
2410 mi->mi_type = TULIP_MEDIAINFO_SIA;
2411 sc->tulip_mediums[media] = mi;
2412 mi->mi_sia_connectivity = dp[0] + dp[1] * 256;
2413 mi->mi_sia_tx_rx = dp[2] + dp[3] * 256;
2414 mi->mi_sia_general = dp[4] + dp[5] * 256;
2415 mi++;
2416 } else {
2417 switch (media) {
2418 case TULIP_MEDIA_BNC: {
2419 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC);
2420 mi++;
2421 break;
2422 }
2423 case TULIP_MEDIA_AUI: {
2424 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI);
2425 mi++;
2426 break;
2427 }
2428 case TULIP_MEDIA_10BASET: {
2429 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET);
2430 mi++;
2431 break;
2432 }
2433 case TULIP_MEDIA_10BASET_FD: {
2434 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD);
2435 mi++;
2436 break;
2437 }
2438 default: {
2439 break;
2440 }
2441 }
2442 }
2443 }
2444 if (data & TULIP_SROM_21041_EXTENDED)
2445 dp += 6;
2446 }
2447#ifdef notdef
2448 if (blocks == 0) {
2449 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC); mi++;
2450 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI); mi++;
2451 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET); mi++;
2452 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD); mi++;
2453 }
2454#endif
2455 } else {
2456 unsigned length, type;
2457 tulip_media_t gp_media = TULIP_MEDIA_UNKNOWN;
2458 if (sc->tulip_features & TULIP_HAVE_GPR)
2459 sc->tulip_gpinit = *dp++;
2460 blocks = *dp++;
2461 for (idx2 = 0; idx2 < blocks; idx2++) {
2462 const u_int8_t *ep;
2463 if ((*dp & 0x80) == 0) {
2464 length = 4;
2465 type = 0;
2466 } else {
2467 length = (*dp++ & 0x7f) - 1;
2468 type = *dp++ & 0x3f;
2469 }
2470 ep = dp + length;
2471 switch (type & 0x3f) {
2472 case 0: { /* 21140[A] GPR block */
2473 tulip_media_t media;
2474 srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
2475 for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2476 if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2477 break;
2478 }
2479 media = tulip_srom_mediums[idx3].sm_type;
2480 if (media == TULIP_MEDIA_UNKNOWN)
2481 break;
2482 mi->mi_type = TULIP_MEDIAINFO_GPR;
2483 sc->tulip_mediums[media] = mi;
2484 mi->mi_gpdata = dp[1];
2485 if (media > gp_media && !TULIP_IS_MEDIA_FD(media)) {
2486 sc->tulip_gpdata = mi->mi_gpdata;
2487 gp_media = media;
2488 }
2489 data = dp[2] + dp[3] * 256;
2490 mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
2491 if (data & TULIP_SROM_2114X_NOINDICATOR) {
2492 mi->mi_actmask = 0;
2493 } else {
2494#if 0
2495 mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
2496#endif
2497 mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
2498 mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
2499 }
2500 mi++;
2501 break;
2502 }
2503 case 1: { /* 21140[A] MII block */
2504 const unsigned phyno = *dp++;
2505 mi->mi_type = TULIP_MEDIAINFO_MII;
2506 mi->mi_gpr_length = *dp++;
2507 mi->mi_gpr_offset = dp - sc->tulip_rombuf;
2508 dp += mi->mi_gpr_length;
2509 mi->mi_reset_length = *dp++;
2510 mi->mi_reset_offset = dp - sc->tulip_rombuf;
2511 dp += mi->mi_reset_length;
2512
2513 /*
2514 * Before we probe for a PHY, use the GPR information
2515 * to select it. If we don't, it may be inaccessible.
2516 */
2517 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpinit|TULIP_GP_PINSET);
2518 for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++) {
2519 DELAY(10);
2520 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx3]);
2521 }
2522 sc->tulip_phyaddr = mi->mi_phyaddr;
2523 for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++) {
2524 DELAY(10);
2525 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx3]);
2526 }
2527
2528 /*
2529 * At least write something!
2530 */
2531 if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
2532 TULIP_CSR_WRITE(sc, csr_gp, 0);
2533
2534 mi->mi_phyaddr = TULIP_MII_NOPHY;
2535 for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
2536 DELAY(10000);
2537 mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
2538 }
2539 if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
2540#if defined(TULIP_DEBUG)
2541 printf("%s%d: can't find phy %d\n",
2542 sc->tulip_name, sc->tulip_unit, phyno);
2543#endif
2544 break;
2545 }
2546 sc->tulip_features |= TULIP_HAVE_MII;
2547 mi->mi_capabilities = dp[0] + dp[1] * 256; dp += 2;
2548 mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
2549 mi->mi_full_duplex = dp[0] + dp[1] * 256; dp += 2;
2550 mi->mi_tx_threshold = dp[0] + dp[1] * 256; dp += 2;
2551 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2552 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2553 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2554 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2555 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2556 mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2557 tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2558 mi++;
2559 break;
2560 }
2561 case 2: { /* 2114[23] SIA block */
2562 tulip_media_t media;
2563 srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
2564 for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2565 if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2566 break;
2567 }
2568 media = tulip_srom_mediums[idx3].sm_type;
2569 if (media == TULIP_MEDIA_UNKNOWN)
2570 break;
2571 mi->mi_type = TULIP_MEDIAINFO_SIA;
2572 sc->tulip_mediums[media] = mi;
2573 if (dp[0] & 0x40) {
2574 mi->mi_sia_connectivity = dp[1] + dp[2] * 256;
2575 mi->mi_sia_tx_rx = dp[3] + dp[4] * 256;
2576 mi->mi_sia_general = dp[5] + dp[6] * 256;
2577 dp += 6;
2578 } else {
2579 switch (media) {
2580 case TULIP_MEDIA_BNC: {
2581 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, BNC);
2582 break;
2583 }
2584 case TULIP_MEDIA_AUI: {
2585 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, AUI);
2586 break;
2587 }
2588 case TULIP_MEDIA_10BASET: {
2589 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET);
2590 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2591 break;
2592 }
2593 case TULIP_MEDIA_10BASET_FD: {
2594 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET_FD);
2595 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2596 break;
2597 }
2598 default: {
2599 goto bad_media;
2600 }
2601 }
2602 }
2603 mi->mi_sia_gp_control = (dp[1] + dp[2] * 256) << 16;
2604 mi->mi_sia_gp_data = (dp[3] + dp[4] * 256) << 16;
2605 mi++;
2606 bad_media:
2607 break;
2608 }
2609 case 3: { /* 2114[23] MII PHY block */
2610 const unsigned phyno = *dp++;
2611 const u_int8_t *dp0;
2612 mi->mi_type = TULIP_MEDIAINFO_MII;
2613 mi->mi_gpr_length = *dp++;
2614 mi->mi_gpr_offset = dp - sc->tulip_rombuf;
2615 dp += 2 * mi->mi_gpr_length;
2616 mi->mi_reset_length = *dp++;
2617 mi->mi_reset_offset = dp - sc->tulip_rombuf;
2618 dp += 2 * mi->mi_reset_length;
2619
2620 dp0 = &sc->tulip_rombuf[mi->mi_reset_offset];
2621 for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++, dp0 += 2) {
2622 DELAY(10);
2623 TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
2624 }
2625 sc->tulip_phyaddr = mi->mi_phyaddr;
2626 dp0 = &sc->tulip_rombuf[mi->mi_gpr_offset];
2627 for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++, dp0 += 2) {
2628 DELAY(10);
2629 TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
2630 }
2631
2632 if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
2633 TULIP_CSR_WRITE(sc, csr_sia_general, 0);
2634
2635 mi->mi_phyaddr = TULIP_MII_NOPHY;
2636 for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
2637 DELAY(10000);
2638 mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
2639 }
2640 if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
2641#if defined(TULIP_DEBUG)
2642 printf("%s%d: can't find phy %d\n",
2643 sc->tulip_name, sc->tulip_unit, phyno);
2644#endif
2645 break;
2646 }
2647 sc->tulip_features |= TULIP_HAVE_MII;
2648 mi->mi_capabilities = dp[0] + dp[1] * 256; dp += 2;
2649 mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
2650 mi->mi_full_duplex = dp[0] + dp[1] * 256; dp += 2;
2651 mi->mi_tx_threshold = dp[0] + dp[1] * 256; dp += 2;
2652 mi->mi_mii_interrupt = dp[0] + dp[1] * 256; dp += 2;
2653 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2654 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2655 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2656 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2657 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2658 mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2659 tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2660 mi++;
2661 break;
2662 }
2663 case 4: { /* 21143 SYM block */
2664 tulip_media_t media;
2665 srom_media = (tulip_srom_media_t) dp[0];
2666 for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2667 if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2668 break;
2669 }
2670 media = tulip_srom_mediums[idx3].sm_type;
2671 if (media == TULIP_MEDIA_UNKNOWN)
2672 break;
2673 mi->mi_type = TULIP_MEDIAINFO_SYM;
2674 sc->tulip_mediums[media] = mi;
2675 mi->mi_gpcontrol = (dp[1] + dp[2] * 256) << 16;
2676 mi->mi_gpdata = (dp[3] + dp[4] * 256) << 16;
2677 data = dp[5] + dp[6] * 256;
2678 mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
2679 if (data & TULIP_SROM_2114X_NOINDICATOR) {
2680 mi->mi_actmask = 0;
2681 } else {
2682 mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
2683 mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
2684 mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
2685 }
2686 if (TULIP_IS_MEDIA_TP(media))
2687 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2688 mi++;
2689 break;
2690 }
2691#if 0
2692 case 5: { /* 21143 Reset block */
2693 mi->mi_type = TULIP_MEDIAINFO_RESET;
2694 mi->mi_reset_length = *dp++;
2695 mi->mi_reset_offset = dp - sc->tulip_rombuf;
2696 dp += 2 * mi->mi_reset_length;
2697 mi++;
2698 break;
2699 }
2700#endif
2701 default: {
2702 }
2703 }
2704 dp = ep;
2705 }
2706 }
2707 return mi - sc->tulip_mediainfo;
2708}
2709\f
2710static const struct {
2711 void (*vendor_identify_nic)(tulip_softc_t * const sc);
2712 unsigned char vendor_oui[3];
2713} tulip_vendors[] = {
2714 { tulip_identify_dec_nic, { 0x08, 0x00, 0x2B } },
2715 { tulip_identify_dec_nic, { 0x00, 0x00, 0xF8 } },
2716 { tulip_identify_smc_nic, { 0x00, 0x00, 0xC0 } },
2717 { tulip_identify_smc_nic, { 0x00, 0xE0, 0x29 } },
2718 { tulip_identify_znyx_nic, { 0x00, 0xC0, 0x95 } },
2719 { tulip_identify_cogent_nic, { 0x00, 0x00, 0x92 } },
2720 { tulip_identify_asante_nic, { 0x00, 0x00, 0x94 } },
2721 { tulip_identify_cogent_nic, { 0x00, 0x00, 0xD1 } },
2722 { tulip_identify_accton_nic, { 0x00, 0x00, 0xE8 } },
2723 { tulip_identify_compex_nic, { 0x00, 0x80, 0x48 } },
2724 { NULL }
2725};
2726
2727/*
2728 * This deals with the vagaries of the address roms and the
2729 * brain-deadness that various vendors commit in using them.
2730 */
2731static int
2732tulip_read_macaddr(
2733 tulip_softc_t * const sc)
2734{
2735 unsigned cksum, rom_cksum, idx;
2736 u_int32_t csr;
2737 unsigned char tmpbuf[8];
2738 static const u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
2739
2740 sc->tulip_connidx = TULIP_SROM_LASTCONNIDX;
2741
2742 if (sc->tulip_chipid == TULIP_21040) {
2743 TULIP_CSR_WRITE(sc, csr_enetrom, 1);
2744 for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
2745 int cnt = 0;
2746 while (((csr = TULIP_CSR_READ(sc, csr_enetrom)) & 0x80000000L) && cnt < 10000)
2747 cnt++;
2748 sc->tulip_rombuf[idx] = csr & 0xFF;
2749 }
2750 sc->tulip_boardsw = &tulip_21040_boardsw;
2751 } else {
2752 if (sc->tulip_chipid == TULIP_21041) {
2753 /*
2754 * Thankfully all 21041's act the same.
2755 */
2756 sc->tulip_boardsw = &tulip_21041_boardsw;
2757 } else {
2758 /*
2759 * Assume all 21140 board are compatible with the
2760 * DEC 10/100 evaluation board. Not really valid but
2761 * it's the best we can do until every one switches to
2762 * the new SROM format.
2763 */
2764
2765 sc->tulip_boardsw = &tulip_21140_eb_boardsw;
2766 }
2767 tulip_srom_read(sc);
2768 if (tulip_srom_crcok(sc->tulip_rombuf)) {
2769 /*
2770 * SROM CRC is valid therefore it must be in the
2771 * new format.
2772 */
2773 sc->tulip_features |= TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM;
2774 } else if (sc->tulip_rombuf[126] == 0xff && sc->tulip_rombuf[127] == 0xFF) {
2775 /*
2776 * No checksum is present. See if the SROM id checks out;
2777 * the first 18 bytes should be 0 followed by a 1 followed
2778 * by the number of adapters (which we don't deal with yet).
2779 */
2780 for (idx = 0; idx < 18; idx++) {
2781 if (sc->tulip_rombuf[idx] != 0)
2782 break;
2783 }
2784 if (idx == 18 && sc->tulip_rombuf[18] == 1 && sc->tulip_rombuf[19] != 0)
2785 sc->tulip_features |= TULIP_HAVE_ISVSROM;
2786 } else if (sc->tulip_chipid >= TULIP_21142) {
2787 sc->tulip_features |= TULIP_HAVE_ISVSROM;
2788 sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2789 }
2790 if ((sc->tulip_features & TULIP_HAVE_ISVSROM) && tulip_srom_decode(sc)) {
2791 if (sc->tulip_chipid != TULIP_21041)
2792 sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2793
2794 /*
2795 * If the SROM specifies more than one adapter, tag this as a
2796 * BASE rom.
2797 */
2798 if (sc->tulip_rombuf[19] > 1)
2799 sc->tulip_features |= TULIP_HAVE_BASEROM;
2800 if (sc->tulip_boardsw == NULL)
2801 return -6;
2802 goto check_oui;
2803 }
2804 }
2805
2806
2807 if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) {
2808 /*
2809 * Some folks don't use the standard ethernet rom format
2810 * but instead just put the address in the first 6 bytes
2811 * of the rom and let the rest be all 0xffs. (Can we say
2812 * ZNYX?) (well sometimes they put in a checksum so we'll
2813 * start at 8).
2814 */
2815 for (idx = 8; idx < 32; idx++) {
2816 if (sc->tulip_rombuf[idx] != 0xFF)
2817 return -4;
2818 }
2819 /*
2820 * Make sure the address is not multicast or locally assigned
2821 * that the OUI is not 00-00-00.
2822 */
2823 if ((sc->tulip_rombuf[0] & 3) != 0)
2824 return -4;
2825 if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0
2826 && sc->tulip_rombuf[2] == 0)
2827 return -4;
2828 bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
2829 sc->tulip_features |= TULIP_HAVE_OKROM;
2830 goto check_oui;
2831 } else {
2832 /*
2833 * A number of makers of multiport boards (ZNYX and Cogent)
2834 * only put on one address ROM on their 21040 boards. So
2835 * if the ROM is all zeros (or all 0xFFs), look at the
2836 * previous configured boards (as long as they are on the same
2837 * PCI bus and the bus number is non-zero) until we find the
2838 * master board with address ROM. We then use its address ROM
2839 * as the base for this board. (we add our relative board
2840 * to the last byte of its address).
2841 */
2842 for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
2843 if (sc->tulip_rombuf[idx] != 0 && sc->tulip_rombuf[idx] != 0xFF)
2844 break;
2845 }
2846 if (idx == sizeof(sc->tulip_rombuf)) {
2847 int root_unit;
2848 tulip_softc_t *root_sc = NULL;
2849 for (root_unit = sc->tulip_unit - 1; root_unit >= 0; root_unit--) {
2850 root_sc = tulips[root_unit];
2851 if (root_sc == NULL || (root_sc->tulip_features & (TULIP_HAVE_OKROM|TULIP_HAVE_SLAVEDROM)) == TULIP_HAVE_OKROM)
2852 break;
2853 root_sc = NULL;
2854 }
2855 if (root_sc != NULL && (root_sc->tulip_features & TULIP_HAVE_BASEROM)
2856 && root_sc->tulip_chipid == sc->tulip_chipid
2857 && root_sc->tulip_pci_busno == sc->tulip_pci_busno) {
2858 sc->tulip_features |= TULIP_HAVE_SLAVEDROM;
2859 sc->tulip_boardsw = root_sc->tulip_boardsw;
2860 strcpy(sc->tulip_boardid, root_sc->tulip_boardid);
2861 if (sc->tulip_boardsw->bd_type == TULIP_21140_ISV) {
2862 bcopy(root_sc->tulip_rombuf, sc->tulip_rombuf,
2863 sizeof(sc->tulip_rombuf));
2864 if (!tulip_srom_decode(sc))
2865 return -5;
2866 } else {
2867 bcopy(root_sc->tulip_enaddr, sc->tulip_enaddr, 6);
2868 sc->tulip_enaddr[5] += sc->tulip_unit - root_sc->tulip_unit;
2869 }
2870 /*
2871 * Now for a truly disgusting kludge: all 4 21040s on
2872 * the ZX314 share the same INTA line so the mapping
2873 * setup by the BIOS on the PCI bridge is worthless.
2874 * Rather than reprogramming the value in the config
2875 * register, we will handle this internally.
2876 */
2877 if (root_sc->tulip_features & TULIP_HAVE_SHAREDINTR) {
2878 sc->tulip_slaves = root_sc->tulip_slaves;
2879 root_sc->tulip_slaves = sc;
2880 sc->tulip_features |= TULIP_HAVE_SLAVEDINTR;
2881 }
2882 return 0;
2883 }
2884 }
2885 }
2886
2887 /*
2888 * This is the standard DEC address ROM test.
2889 */
2890
2891 if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0)
2892 return -3;
2893
2894 tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14];
2895 tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12];
2896 tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10];
2897 tmpbuf[6] = sc->tulip_rombuf[9]; tmpbuf[7] = sc->tulip_rombuf[8];
2898 if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0)
2899 return -2;
2900
2901 bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
2902
2903 cksum = *(u_int16_t *) &sc->tulip_enaddr[0];
2904 cksum *= 2;
2905 if (cksum > 65535) cksum -= 65535;
2906 cksum += *(u_int16_t *) &sc->tulip_enaddr[2];
2907 if (cksum > 65535) cksum -= 65535;
2908 cksum *= 2;
2909 if (cksum > 65535) cksum -= 65535;
2910 cksum += *(u_int16_t *) &sc->tulip_enaddr[4];
2911 if (cksum >= 65535) cksum -= 65535;
2912
2913 rom_cksum = *(u_int16_t *) &sc->tulip_rombuf[6];
2914
2915 if (cksum != rom_cksum)
2916 return -1;
2917
2918 check_oui:
2919 /*
2920 * Check for various boards based on OUI. Did I say braindead?
2921 */
2922 for (idx = 0; tulip_vendors[idx].vendor_identify_nic != NULL; idx++) {
2923 if (bcmp(sc->tulip_enaddr, tulip_vendors[idx].vendor_oui, 3) == 0) {
2924 (*tulip_vendors[idx].vendor_identify_nic)(sc);
2925 break;
2926 }
2927 }
2928
2929 sc->tulip_features |= TULIP_HAVE_OKROM;
2930 return 0;
2931}
2932\f
2933static void
2934tulip_ifmedia_add(
2935 tulip_softc_t * const sc)
2936{
2937 tulip_media_t media;
2938 int medias = 0;
2939
2940 for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
2941 if (sc->tulip_mediums[media] != NULL) {
2942 ifmedia_add(&sc->tulip_ifmedia, tulip_media_to_ifmedia[media],
2943 0, 0);
2944 medias++;
2945 }
2946 }
2947 if (medias == 0) {
2948 sc->tulip_features |= TULIP_HAVE_NOMEDIA;
2949 ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE, 0, 0);
2950 ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE);
2951 } else if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
2952 ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO, 0, 0);
2953 ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO);
2954 } else {
2955 ifmedia_set(&sc->tulip_ifmedia, tulip_media_to_ifmedia[sc->tulip_media]);
2956 sc->tulip_flags |= TULIP_PRINTMEDIA;
2957 tulip_linkup(sc, sc->tulip_media);
2958 }
2959}
2960
2961static int
2962tulip_ifmedia_change(
2963 struct ifnet * const ifp)
2964{
2965 tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
2966
2967 sc->tulip_flags |= TULIP_NEEDRESET;
2968 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
2969 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
2970 if (IFM_SUBTYPE(sc->tulip_ifmedia.ifm_media) != IFM_AUTO) {
2971 tulip_media_t media;
2972 for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
2973 if (sc->tulip_mediums[media] != NULL
2974 && sc->tulip_ifmedia.ifm_media == tulip_media_to_ifmedia[media]) {
2975 sc->tulip_flags |= TULIP_PRINTMEDIA;
2976 sc->tulip_flags &= ~TULIP_DIDNWAY;
2977 tulip_linkup(sc, media);
2978 return 0;
2979 }
2980 }
2981 }
2982 sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_WANTRXACT);
2983 tulip_reset(sc);
2984 tulip_init(sc);
2985 return 0;
2986}
2987\f
2988/*
2989 * Media status callback
2990 */
2991static void
2992tulip_ifmedia_status(
2993 struct ifnet * const ifp,
2994 struct ifmediareq *req)
2995{
2996 tulip_softc_t *sc = (tulip_softc_t *)ifp->if_softc;
2997
2998 if (sc->tulip_media == TULIP_MEDIA_UNKNOWN)
2999 return;
3000
3001 req->ifm_status = IFM_AVALID;
3002 if (sc->tulip_flags & TULIP_LINKUP)
3003 req->ifm_status |= IFM_ACTIVE;
3004
3005 req->ifm_active = tulip_media_to_ifmedia[sc->tulip_media];
3006}
3007\f
3008static void
3009tulip_addr_filter(
3010 tulip_softc_t * const sc)
3011{
3012 struct ifmultiaddr *ifma;
3013 u_char *addrp;
3014 int multicnt;
3015
3016 sc->tulip_flags &= ~(TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY|TULIP_ALLMULTI);
3017 sc->tulip_flags |= TULIP_WANTSETUP|TULIP_WANTTXSTART;
3018 sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
3019 sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
3020#if defined(IFF_ALLMULTI)
3021 if (sc->tulip_if.if_flags & IFF_ALLMULTI)
3022 sc->tulip_flags |= TULIP_ALLMULTI ;
3023#endif
3024
3025 multicnt = 0;
3026 for (ifma = sc->tulip_if.if_multiaddrs.lh_first; ifma != NULL;
3027 ifma = ifma->ifma_link.le_next) {
3028
3029 if (ifma->ifma_addr->sa_family == AF_LINK)
3030 multicnt++;
3031 }
3032
3033 sc->tulip_if.if_start = tulip_ifstart; /* so the setup packet gets queued */
3034 if (multicnt > 14) {
3035 u_int32_t *sp = sc->tulip_setupdata;
3036 unsigned hash;
3037 /*
3038 * Some early passes of the 21140 have broken implementations of
3039 * hash-perfect mode. When we get too many multicasts for perfect
3040 * filtering with these chips, we need to switch into hash-only
3041 * mode (this is better than all-multicast on network with lots
3042 * of multicast traffic).
3043 */
3044 if (sc->tulip_features & TULIP_HAVE_BROKEN_HASH)
3045 sc->tulip_flags |= TULIP_WANTHASHONLY;
3046 else
3047 sc->tulip_flags |= TULIP_WANTHASHPERFECT;
3048 /*
3049 * If we have more than 14 multicasts, we have
3050 * go into hash perfect mode (512 bit multicast
3051 * hash and one perfect hardware).
3052 */
3053 bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata));
3054
3055 for (ifma = sc->tulip_if.if_multiaddrs.lh_first; ifma != NULL;
3056 ifma = ifma->ifma_link.le_next) {
3057
3058 if (ifma->ifma_addr->sa_family != AF_LINK)
3059 continue;
3060
3061 hash = tulip_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
3062#if BYTE_ORDER == BIG_ENDIAN
3063 sp[hash >> 4] |= bswap32(1 << (hash & 0xF));
3064#else
3065 sp[hash >> 4] |= 1 << (hash & 0xF);
3066#endif
3067 }
3068 /*
3069 * No reason to use a hash if we are going to be
3070 * receiving every multicast.
3071 */
3072 if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
3073 hash = tulip_mchash(etherbroadcastaddr);
3074#if BYTE_ORDER == BIG_ENDIAN
3075 sp[hash >> 4] |= bswap32(1 << (hash & 0xF));
3076#else
3077 sp[hash >> 4] |= 1 << (hash & 0xF);
3078#endif
3079 if (sc->tulip_flags & TULIP_WANTHASHONLY) {
3080 hash = tulip_mchash(sc->tulip_enaddr);
3081#if BYTE_ORDER == BIG_ENDIAN
3082 sp[hash >> 4] |= bswap32(1 << (hash & 0xF));
3083#else
3084 sp[hash >> 4] |= 1 << (hash & 0xF);
3085#endif
3086 } else {
3087#if BYTE_ORDER == BIG_ENDIAN
3088 sp[39] = ((u_int16_t *) sc->tulip_enaddr)[0] << 16;
3089 sp[40] = ((u_int16_t *) sc->tulip_enaddr)[1] << 16;
3090 sp[41] = ((u_int16_t *) sc->tulip_enaddr)[2] << 16;
3091#else
3092 sp[39] = ((u_int16_t *) sc->tulip_enaddr)[0];
3093 sp[40] = ((u_int16_t *) sc->tulip_enaddr)[1];
3094 sp[41] = ((u_int16_t *) sc->tulip_enaddr)[2];
3095#endif
3096 }
3097 }
3098 }
3099 if ((sc->tulip_flags & (TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY)) == 0) {
3100 u_int32_t *sp = sc->tulip_setupdata;
3101 int idx = 0;
3102 if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
3103 /*
3104 * Else can get perfect filtering for 16 addresses.
3105 */
3106 for (ifma = sc->tulip_if.if_multiaddrs.lh_first; ifma != NULL;
3107 ifma = ifma->ifma_link.le_next) {
3108 if (ifma->ifma_addr->sa_family != AF_LINK)
3109 continue;
3110 addrp = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
3111#if BYTE_ORDER == BIG_ENDIAN
3112 *sp++ = ((u_int16_t *) addrp)[0] << 16;
3113 *sp++ = ((u_int16_t *) addrp)[1] << 16;
3114 *sp++ = ((u_int16_t *) addrp)[2] << 16;
3115#else
3116 *sp++ = ((u_int16_t *) addrp)[0];
3117 *sp++ = ((u_int16_t *) addrp)[1];
3118 *sp++ = ((u_int16_t *) addrp)[2];
3119#endif
3120 idx++;
3121 }
3122 /*
3123 * Add the broadcast address.
3124 */
3125 idx++;
3126#if BYTE_ORDER == BIG_ENDIAN
3127 *sp++ = 0xFFFF << 16;
3128 *sp++ = 0xFFFF << 16;
3129 *sp++ = 0xFFFF << 16;
3130#else
3131 *sp++ = 0xFFFF;
3132 *sp++ = 0xFFFF;
3133 *sp++ = 0xFFFF;
3134#endif
3135 }
3136 /*
3137 * Pad the rest with our hardware address
3138 */
3139 for (; idx < 16; idx++) {
3140#if BYTE_ORDER == BIG_ENDIAN
3141 *sp++ = ((u_int16_t *) sc->tulip_enaddr)[0] << 16;
3142 *sp++ = ((u_int16_t *) sc->tulip_enaddr)[1] << 16;
3143 *sp++ = ((u_int16_t *) sc->tulip_enaddr)[2] << 16;
3144#else
3145 *sp++ = ((u_int16_t *) sc->tulip_enaddr)[0];
3146 *sp++ = ((u_int16_t *) sc->tulip_enaddr)[1];
3147 *sp++ = ((u_int16_t *) sc->tulip_enaddr)[2];
3148#endif
3149 }
3150 }
3151#if defined(IFF_ALLMULTI)
3152 if (sc->tulip_flags & TULIP_ALLMULTI)
3153 sc->tulip_if.if_flags |= IFF_ALLMULTI;
3154#endif
3155}
3156\f
3157static void
3158tulip_reset(
3159 tulip_softc_t * const sc)
3160{
3161 tulip_ringinfo_t *ri;
3162 tulip_desc_t *di;
3163 u_int32_t inreset = (sc->tulip_flags & TULIP_INRESET);
3164
3165 /*
3166 * Brilliant. Simply brilliant. When switching modes/speeds
3167 * on a 2114*, you need to set the appriopriate MII/PCS/SCL/PS
3168 * bits in CSR6 and then do a software reset to get the 21140
3169 * to properly reset its internal pathways to the right places.
3170 * Grrrr.
3171 */
3172 if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0
3173 && sc->tulip_boardsw->bd_media_preset != NULL)
3174 (*sc->tulip_boardsw->bd_media_preset)(sc);
3175
3176 TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
3177 DELAY(10); /* Wait 10 microseconds (actually 50 PCI cycles but at
3178 33MHz that comes to two microseconds but wait a
3179 bit longer anyways) */
3180
3181 if (!inreset) {
3182 sc->tulip_flags |= TULIP_INRESET;
3183 sc->tulip_flags &= ~(TULIP_NEEDRESET|TULIP_RXBUFSLOW);
3184 sc->tulip_if.if_flags &= ~IFF_OACTIVE;
3185 sc->tulip_if.if_start = tulip_ifstart;
3186 }
3187
3188#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
3189 TULIP_CSR_WRITE(sc, csr_txlist, sc->tulip_txdescmap->dm_segs[0].ds_addr);
3190#else
3191 TULIP_CSR_WRITE(sc, csr_txlist, TULIP_KVATOPHYS(sc, &sc->tulip_txinfo.ri_first[0]));
3192#endif
3193#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3194 TULIP_CSR_WRITE(sc, csr_rxlist, sc->tulip_rxdescmap->dm_segs[0].ds_addr);
3195#else
3196 TULIP_CSR_WRITE(sc, csr_rxlist, TULIP_KVATOPHYS(sc, &sc->tulip_rxinfo.ri_first[0]));
3197#endif
3198 TULIP_CSR_WRITE(sc, csr_busmode,
3199 (1 << (3 /*pci_max_burst_len*/ + 8))
3200 |TULIP_BUSMODE_CACHE_ALIGN8
3201 |TULIP_BUSMODE_READMULTIPLE
3202 |(BYTE_ORDER != LITTLE_ENDIAN ?
3203 TULIP_BUSMODE_DESC_BIGENDIAN : 0));
3204
3205 sc->tulip_txtimer = 0;
3206 sc->tulip_txq.ifq_maxlen = TULIP_TXDESCS;
3207 /*
3208 * Free all the mbufs that were on the transmit ring.
3209 */
3210 for (;;) {
3211#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
3212 bus_dmamap_t map;
3213#endif
3214 struct mbuf *m;
3215 IF_DEQUEUE(&sc->tulip_txq, m);
3216 if (m == NULL)
3217 break;
3218#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
3219 map = M_GETCTX(m, bus_dmamap_t);
3220 bus_dmamap_unload(sc->tulip_dmatag, map);
3221 sc->tulip_txmaps[sc->tulip_txmaps_free++] = map;
3222#endif
3223 m_freem(m);
3224 }
3225
3226 ri = &sc->tulip_txinfo;
3227 ri->ri_nextin = ri->ri_nextout = ri->ri_first;
3228 ri->ri_free = ri->ri_max;
3229 for (di = ri->ri_first; di < ri->ri_last; di++)
3230 di->d_status = 0;
3231#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
3232 bus_dmamap_sync(sc->tulip_dmatag, sc->tulip_txdescmap,
3233 0, sc->tulip_txdescmap->dm_mapsize,
3234 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3235#endif
3236
3237 /*
3238 * We need to collect all the mbufs were on the
3239 * receive ring before we reinit it either to put
3240 * them back on or to know if we have to allocate
3241 * more.
3242 */
3243 ri = &sc->tulip_rxinfo;
3244 ri->ri_nextin = ri->ri_nextout = ri->ri_first;
3245 ri->ri_free = ri->ri_max;
3246 for (di = ri->ri_first; di < ri->ri_last; di++) {
3247 di->d_status = 0;
3248 di->d_length1 = 0; di->d_addr1 = 0;
3249 di->d_length2 = 0; di->d_addr2 = 0;
3250 }
3251#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3252 bus_dmamap_sync(sc->tulip_dmatag, sc->tulip_rxdescmap,
3253 0, sc->tulip_rxdescmap->dm_mapsize,
3254 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3255#endif
3256 for (;;) {
3257#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3258 bus_dmamap_t map;
3259#endif
3260 struct mbuf *m;
3261 IF_DEQUEUE(&sc->tulip_rxq, m);
3262 if (m == NULL)
3263 break;
3264#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3265 map = M_GETCTX(m, bus_dmamap_t);
3266 bus_dmamap_unload(sc->tulip_dmatag, map);
3267 sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
3268#endif
3269 m_freem(m);
3270 }
3271
3272 /*
3273 * If tulip_reset is being called recurisvely, exit quickly knowing
3274 * that when the outer tulip_reset returns all the right stuff will
3275 * have happened.
3276 */
3277 if (inreset)
3278 return;
3279
3280 sc->tulip_intrmask |= TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
3281 |TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
3282 |TULIP_STS_TXUNDERFLOW|TULIP_STS_TXBABBLE
3283 |TULIP_STS_RXSTOPPED;
3284
3285 if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0)
3286 (*sc->tulip_boardsw->bd_media_select)(sc);
3287#if defined(TULIP_DEBUG)
3288 if ((sc->tulip_flags & TULIP_NEEDRESET) == TULIP_NEEDRESET)
3289 printf("%s%d: tulip_reset: additional reset needed?!?\n",
3290 sc->tulip_name, sc->tulip_unit);
3291#endif
3292 tulip_media_print(sc);
3293 if (sc->tulip_features & TULIP_HAVE_DUALSENSE)
3294 TULIP_CSR_WRITE(sc, csr_sia_status, TULIP_CSR_READ(sc, csr_sia_status));
3295
3296 sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP|TULIP_INRESET
3297 |TULIP_RXACT);
3298 tulip_addr_filter(sc);
3299}
3300\f
3301static void
3302tulip_init(
3303 tulip_softc_t * const sc)
3304{
3305 if (sc->tulip_if.if_flags & IFF_UP) {
3306 if ((sc->tulip_if.if_flags & IFF_RUNNING) == 0) {
3307 /* initialize the media */
3308 tulip_reset(sc);
3309 }
3310 sc->tulip_if.if_flags |= IFF_RUNNING;
3311 if (sc->tulip_if.if_flags & IFF_PROMISC) {
3312 sc->tulip_flags |= TULIP_PROMISC;
3313 sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
3314 sc->tulip_intrmask |= TULIP_STS_TXINTR;
3315 } else {
3316 sc->tulip_flags &= ~TULIP_PROMISC;
3317 sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
3318 if (sc->tulip_flags & TULIP_ALLMULTI) {
3319 sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
3320 } else {
3321 sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
3322 }
3323 }
3324 sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
3325 if ((sc->tulip_flags & (TULIP_TXPROBE_ACTIVE|TULIP_WANTSETUP)) == 0) {
3326 tulip_rx_intr(sc);
3327 sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
3328 sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
3329 } else {
3330 sc->tulip_if.if_flags |= IFF_OACTIVE;
3331 sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
3332 sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
3333 }
3334 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3335 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3336 if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
3337 tulip_txput_setup(sc);
3338 } else {
3339 sc->tulip_if.if_flags &= ~IFF_RUNNING;
3340 tulip_reset(sc);
3341 }
3342}
3343\f
3344static void
3345tulip_rx_intr(
3346 tulip_softc_t * const sc)
3347{
3348 TULIP_PERFSTART(rxintr)
3349 tulip_ringinfo_t * const ri = &sc->tulip_rxinfo;
3350 struct ifnet * const ifp = &sc->tulip_if;
3351 int fillok = 1;
3352#if defined(TULIP_DEBUG)
3353 int cnt = 0;
3354#endif
3355
3356 for (;;) {
3357 TULIP_PERFSTART(rxget)
3358 struct ether_header eh;
3359 tulip_desc_t *eop = ri->ri_nextin;
3360 int total_len = 0, last_offset = 0;
3361 struct mbuf *ms = NULL, *me = NULL;
3362 int accept = 0;
3363#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3364 bus_dmamap_t map;
3365 int error;
3366#endif
3367
3368 if (fillok && sc->tulip_rxq.ifq_len < TULIP_RXQ_TARGET)
3369 goto queue_mbuf;
3370
3371#if defined(TULIP_DEBUG)
3372 if (cnt == ri->ri_max)
3373 break;
3374#endif
3375 /*
3376 * If the TULIP has no descriptors, there can't be any receive
3377 * descriptors to process.
3378 */
3379 if (eop == ri->ri_nextout)
3380 break;
3381
3382 /*
3383 * 90% of the packets will fit in one descriptor. So we optimize
3384 * for that case.
3385 */
3386 TULIP_RXDESC_POSTSYNC(sc, eop, sizeof(*eop));
3387 if ((((volatile tulip_desc_t *) eop)->d_status & (TULIP_DSTS_OWNER|TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) == (TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) {
3388 IF_DEQUEUE(&sc->tulip_rxq, ms);
3389 me = ms;
3390 } else {
3391 /*
3392 * If still owned by the TULIP, don't touch it.
3393 */
3394 if (((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER)
3395 break;
3396
3397 /*
3398 * It is possible (though improbable unless the BIG_PACKET support
3399 * is enabled or MCLBYTES < 1518) for a received packet to cross
3400 * more than one receive descriptor.
3401 */
3402 while ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_RxLASTDESC) == 0) {
3403 if (++eop == ri->ri_last)
3404 eop = ri->ri_first;
3405 TULIP_RXDESC_POSTSYNC(sc, eop, sizeof(*eop));
3406 if (eop == ri->ri_nextout || ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER))) {
3407#if defined(TULIP_DEBUG)
3408 sc->tulip_dbg.dbg_rxintrs++;
3409 sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
3410#endif
3411 TULIP_PERFEND(rxget);
3412 TULIP_PERFEND(rxintr);
3413 return;
3414 }
3415 total_len++;
3416 }
3417
3418 /*
3419 * Dequeue the first buffer for the start of the packet. Hopefully
3420 * this will be the only one we need to dequeue. However, if the
3421 * packet consumed multiple descriptors, then we need to dequeue
3422 * those buffers and chain to the starting mbuf. All buffers but
3423 * the last buffer have the same length so we can set that now.
3424 * (we add to last_offset instead of multiplying since we normally
3425 * won't go into the loop and thereby saving a ourselves from
3426 * doing a multiplication by 0 in the normal case).
3427 */
3428 IF_DEQUEUE(&sc->tulip_rxq, ms);
3429 for (me = ms; total_len > 0; total_len--) {
3430#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3431 map = M_GETCTX(me, bus_dmamap_t);
3432 TULIP_RXMAP_POSTSYNC(sc, map);
3433 bus_dmamap_unload(sc->tulip_dmatag, map);
3434 sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
3435#if defined(DIAGNOSTIC)
3436 M_SETCTX(me, NULL);
3437#endif
3438#endif /* TULIP_BUS_DMA */
3439 me->m_len = TULIP_RX_BUFLEN;
3440 last_offset += TULIP_RX_BUFLEN;
3441 IF_DEQUEUE(&sc->tulip_rxq, me->m_next);
3442 me = me->m_next;
3443 }
3444 }
3445
3446 /*
3447 * Now get the size of received packet (minus the CRC).
3448 */
3449 total_len = ((eop->d_status >> 16) & 0x7FFF) - 4;
3450 if ((sc->tulip_flags & TULIP_RXIGNORE) == 0
3451 && ((eop->d_status & TULIP_DSTS_ERRSUM) == 0
3452#ifdef BIG_PACKET
3453 || (total_len <= sc->tulip_if.if_mtu + sizeof(struct ether_header) &&
3454 (eop->d_status & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxRUNT|
3455 TULIP_DSTS_RxCOLLSEEN|TULIP_DSTS_RxBADCRC|
3456 TULIP_DSTS_RxOVERFLOW)) == 0)
3457#endif
3458 )) {
3459 me->m_len = total_len - last_offset;
3460
3461#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3462 map = M_GETCTX(me, bus_dmamap_t);
3463 bus_dmamap_sync(sc->tulip_dmatag, map, 0, me->m_len,
3464 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
3465 bus_dmamap_unload(sc->tulip_dmatag, map);
3466 sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
3467#if defined(DIAGNOSTIC)
3468 M_SETCTX(me, NULL);
3469#endif
3470#endif /* TULIP_BUS_DMA */
3471
3472 eh = *mtod(ms, struct ether_header *);
3473#ifndef __FreeBSD__
3474 if (sc->tulip_if.if_bpf != NULL) {
3475 if (me == ms)
3476 bpf_tap(&sc->tulip_if, mtod(ms, caddr_t), total_len);
3477 else
3478 bpf_mtap(&sc->tulip_if, ms);
3479 }
3480#endif
3481 sc->tulip_flags |= TULIP_RXACT;
3482 accept = 1;
3483 } else {
3484 ifp->if_ierrors++;
3485 if (eop->d_status & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxOVERFLOW|TULIP_DSTS_RxWATCHDOG)) {
3486 sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
3487 } else {
3488#if defined(TULIP_VERBOSE)
3489 const char *error = NULL;
3490#endif
3491 if (eop->d_status & TULIP_DSTS_RxTOOLONG) {
3492 sc->tulip_dot3stats.dot3StatsFrameTooLongs++;
3493#if defined(TULIP_VERBOSE)
3494 error = "frame too long";
3495#endif
3496 }
3497 if (eop->d_status & TULIP_DSTS_RxBADCRC) {
3498 if (eop->d_status & TULIP_DSTS_RxDRBBLBIT) {
3499 sc->tulip_dot3stats.dot3StatsAlignmentErrors++;
3500#if defined(TULIP_VERBOSE)
3501 error = "alignment error";
3502#endif
3503 } else {
3504 sc->tulip_dot3stats.dot3StatsFCSErrors++;
3505#if defined(TULIP_VERBOSE)
3506 error = "bad crc";
3507#endif
3508 }
3509 }
3510#if defined(TULIP_VERBOSE)
3511 if (error != NULL && (sc->tulip_flags & TULIP_NOMESSAGES) == 0) {
3512 printf("%s%d: receive: %6D: %s\n",
3513 sc->tulip_name, sc->tulip_unit,
3514 mtod(ms, u_char *) + 6, ":",
3515 error);
3516 sc->tulip_flags |= TULIP_NOMESSAGES;
3517 }
3518#endif
3519 }
3520
3521#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3522 map = M_GETCTX(me, bus_dmamap_t);
3523 bus_dmamap_unload(sc->tulip_dmatag, map);
3524 sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
3525#if defined(DIAGNOSTIC)
3526 M_SETCTX(me, NULL);
3527#endif
3528#endif /* TULIP_BUS_DMA */
3529 }
3530#if defined(TULIP_DEBUG)
3531 cnt++;
3532#endif
3533 ifp->if_ipackets++;
3534 if (++eop == ri->ri_last)
3535 eop = ri->ri_first;
3536 ri->ri_nextin = eop;
3537 queue_mbuf:
3538 /*
3539 * Either we are priming the TULIP with mbufs (m == NULL)
3540 * or we are about to accept an mbuf for the upper layers
3541 * so we need to allocate an mbuf to replace it. If we
3542 * can't replace it, send up it anyways. This may cause
3543 * us to drop packets in the future but that's better than
3544 * being caught in livelock.
3545 *
3546 * Note that if this packet crossed multiple descriptors
3547 * we don't even try to reallocate all the mbufs here.
3548 * Instead we rely on the test of the beginning of
3549 * the loop to refill for the extra consumed mbufs.
3550 */
3551 if (accept || ms == NULL) {
3552 struct mbuf *m0;
3553 MGETHDR(m0, M_DONTWAIT, MT_DATA);
3554 if (m0 != NULL) {
3555#if defined(TULIP_COPY_RXDATA)
3556 if (!accept || total_len >= (MHLEN - 2)) {
3557#endif
3558 MCLGET(m0, M_DONTWAIT);
3559 if ((m0->m_flags & M_EXT) == 0) {
3560 m_freem(m0);
3561 m0 = NULL;
3562 }
3563#if defined(TULIP_COPY_RXDATA)
3564 }
3565#endif
3566 }
3567 if (accept
3568#if defined(TULIP_COPY_RXDATA)
3569 && m0 != NULL
3570#endif
3571 ) {
3572#if !defined(TULIP_COPY_RXDATA)
3573 ms->m_pkthdr.len = total_len;
3574 ms->m_pkthdr.rcvif = ifp;
3575 m_adj(ms, sizeof(struct ether_header));
3576 ether_input(ifp, &eh, ms);
3577#else
3578#ifdef BIG_PACKET
3579#error BIG_PACKET is incompatible with TULIP_COPY_RXDATA
3580#endif
3581 m0->m_data += 2; /* align data after header */
3582 m_copydata(ms, 0, total_len, mtod(m0, caddr_t));
3583 m0->m_len = m0->m_pkthdr.len = total_len;
3584 m0->m_pkthdr.rcvif = ifp;
3585 m_adj(m0, sizeof(struct ether_header));
3586 ether_input(ifp, &eh, m0);
3587 m0 = ms;
3588#endif /* ! TULIP_COPY_RXDATA */
3589 }
3590 ms = m0;
3591 }
3592 if (ms == NULL) {
3593 /*
3594 * Couldn't allocate a new buffer. Don't bother
3595 * trying to replenish the receive queue.
3596 */
3597 fillok = 0;
3598 sc->tulip_flags |= TULIP_RXBUFSLOW;
3599#if defined(TULIP_DEBUG)
3600 sc->tulip_dbg.dbg_rxlowbufs++;
3601#endif
3602 TULIP_PERFEND(rxget);
3603 continue;
3604 }
3605 /*
3606 * Now give the buffer(s) to the TULIP and save in our
3607 * receive queue.
3608 */
3609 do {
3610 tulip_desc_t * const nextout = ri->ri_nextout;
3611#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NORX)
3612 if (sc->tulip_rxmaps_free > 0) {
3613 map = sc->tulip_rxmaps[--sc->tulip_rxmaps_free];
3614 } else {
3615 m_freem(ms);
3616 sc->tulip_flags |= TULIP_RXBUFSLOW;
3617#if defined(TULIP_DEBUG)
3618 sc->tulip_dbg.dbg_rxlowbufs++;
3619#endif
3620 break;
3621 }
3622 M_SETCTX(ms, map);
3623 error = bus_dmamap_load(sc->tulip_dmatag, map, mtod(ms, void *),
3624 TULIP_RX_BUFLEN, NULL, BUS_DMA_NOWAIT);
3625 if (error) {
3626 printf("%s%d: unable to load rx map, "
3627 "error = %d\n", sc->tulip_name, sc->tulip_unit, error);
3628 panic("tulip_rx_intr"); /* XXX */
3629 }
3630 nextout->d_addr1 = map->dm_segs[0].ds_addr;
3631 nextout->d_length1 = map->dm_segs[0].ds_len;
3632 if (map->dm_nsegs == 2) {
3633 nextout->d_addr2 = map->dm_segs[1].ds_addr;
3634 nextout->d_length2 = map->dm_segs[1].ds_len;
3635 } else {
3636 nextout->d_addr2 = 0;
3637 nextout->d_length2 = 0;
3638 }
3639 TULIP_RXDESC_POSTSYNC(sc, nextout, sizeof(*nextout));
3640#else /* TULIP_BUS_DMA */
3641 nextout->d_addr1 = TULIP_KVATOPHYS(sc, mtod(ms, caddr_t));
3642 nextout->d_length1 = TULIP_RX_BUFLEN;
3643#endif /* TULIP_BUS_DMA */
3644 nextout->d_status = TULIP_DSTS_OWNER;
3645 TULIP_RXDESC_POSTSYNC(sc, nextout, sizeof(u_int32_t));
3646 if (++ri->ri_nextout == ri->ri_last)
3647 ri->ri_nextout = ri->ri_first;
3648 me = ms->m_next;
3649 ms->m_next = NULL;
3650 IF_ENQUEUE(&sc->tulip_rxq, ms);
3651 } while ((ms = me) != NULL);
3652
3653 if (sc->tulip_rxq.ifq_len >= TULIP_RXQ_TARGET)
3654 sc->tulip_flags &= ~TULIP_RXBUFSLOW;
3655 TULIP_PERFEND(rxget);
3656 }
3657
3658#if defined(TULIP_DEBUG)
3659 sc->tulip_dbg.dbg_rxintrs++;
3660 sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
3661#endif
3662 TULIP_PERFEND(rxintr);
3663}
3664\f
3665static int
3666tulip_tx_intr(
3667 tulip_softc_t * const sc)
3668{
3669 TULIP_PERFSTART(txintr)
3670 tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
3671 struct mbuf *m;
3672 int xmits = 0;
3673 int descs = 0;
3674
3675 while (ri->ri_free < ri->ri_max) {
3676 u_int32_t d_flag;
3677
3678 TULIP_TXDESC_POSTSYNC(sc, ri->ri_nextin, sizeof(*ri->ri_nextin));
3679 if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
3680 break;
3681
3682 ri->ri_free++;
3683 descs++;
3684 d_flag = ri->ri_nextin->d_flag;
3685 if (d_flag & TULIP_DFLAG_TxLASTSEG) {
3686 if (d_flag & TULIP_DFLAG_TxSETUPPKT) {
3687 /*
3688 * We've just finished processing a setup packet.
3689 * Mark that we finished it. If there's not
3690 * another pending, startup the TULIP receiver.
3691 * Make sure we ack the RXSTOPPED so we won't get
3692 * an abormal interrupt indication.
3693 */
3694 TULIP_TXMAP_POSTSYNC(sc, sc->tulip_setupmap);
3695 sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_HASHONLY);
3696 if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxINVRSFILT)
3697 sc->tulip_flags |= TULIP_HASHONLY;
3698 if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == 0) {
3699 tulip_rx_intr(sc);
3700 sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
3701 sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
3702 TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
3703 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3704 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3705 }
3706 } else {
3707 const u_int32_t d_status = ri->ri_nextin->d_status;
3708 IF_DEQUEUE(&sc->tulip_txq, m);
3709 if (m != NULL) {
3710#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
3711 bus_dmamap_t map = M_GETCTX(m, bus_dmamap_t);
3712 TULIP_TXMAP_POSTSYNC(sc, map);
3713 sc->tulip_txmaps[sc->tulip_txmaps_free++] = map;
3714#endif /* TULIP_BUS_DMA */
3715 m_freem(m);
3716#if defined(TULIP_DEBUG)
3717 } else {
3718 printf("%s%d: tx_intr: failed to dequeue mbuf?!?\n",
3719 sc->tulip_name, sc->tulip_unit);
3720#endif
3721 }
3722 if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
3723 tulip_mediapoll_event_t event = TULIP_MEDIAPOLL_TXPROBE_OK;
3724 if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxEXCCOLL)) {
3725#if defined(TULIP_DEBUG)
3726 if (d_status & TULIP_DSTS_TxNOCARR)
3727 sc->tulip_dbg.dbg_txprobe_nocarr++;
3728 if (d_status & TULIP_DSTS_TxEXCCOLL)
3729 sc->tulip_dbg.dbg_txprobe_exccoll++;
3730#endif
3731 event = TULIP_MEDIAPOLL_TXPROBE_FAILED;
3732 }
3733 (*sc->tulip_boardsw->bd_media_poll)(sc, event);
3734 /*
3735 * Escape from the loop before media poll has reset the TULIP!
3736 */
3737 break;
3738 } else {
3739 xmits++;
3740 if (d_status & TULIP_DSTS_ERRSUM) {
3741 sc->tulip_if.if_oerrors++;
3742 if (d_status & TULIP_DSTS_TxEXCCOLL)
3743 sc->tulip_dot3stats.dot3StatsExcessiveCollisions++;
3744 if (d_status & TULIP_DSTS_TxLATECOLL)
3745 sc->tulip_dot3stats.dot3StatsLateCollisions++;
3746 if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxCARRLOSS))
3747 sc->tulip_dot3stats.dot3StatsCarrierSenseErrors++;
3748 if (d_status & (TULIP_DSTS_TxUNDERFLOW|TULIP_DSTS_TxBABBLE))
3749 sc->tulip_dot3stats.dot3StatsInternalMacTransmitErrors++;
3750 if (d_status & TULIP_DSTS_TxUNDERFLOW)
3751 sc->tulip_dot3stats.dot3StatsInternalTransmitUnderflows++;
3752 if (d_status & TULIP_DSTS_TxBABBLE)
3753 sc->tulip_dot3stats.dot3StatsInternalTransmitBabbles++;
3754 } else {
3755 u_int32_t collisions =
3756 (d_status & TULIP_DSTS_TxCOLLMASK)
3757 >> TULIP_DSTS_V_TxCOLLCNT;
3758 sc->tulip_if.if_collisions += collisions;
3759 if (collisions == 1)
3760 sc->tulip_dot3stats.dot3StatsSingleCollisionFrames++;
3761 else if (collisions > 1)
3762 sc->tulip_dot3stats.dot3StatsMultipleCollisionFrames++;
3763 else if (d_status & TULIP_DSTS_TxDEFERRED)
3764 sc->tulip_dot3stats.dot3StatsDeferredTransmissions++;
3765 /*
3766 * SQE is only valid for 10baseT/BNC/AUI when not
3767 * running in full-duplex. In order to speed up the
3768 * test, the corresponding bit in tulip_flags needs to
3769 * set as well to get us to count SQE Test Errors.
3770 */
3771 if (d_status & TULIP_DSTS_TxNOHRTBT & sc->tulip_flags)
3772 sc->tulip_dot3stats.dot3StatsSQETestErrors++;
3773 }
3774 }
3775 }
3776 }
3777
3778 if (++ri->ri_nextin == ri->ri_last)
3779 ri->ri_nextin = ri->ri_first;
3780
3781 if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
3782 sc->tulip_if.if_flags &= ~IFF_OACTIVE;
3783 }
3784 /*
3785 * If nothing left to transmit, disable the timer.
3786 * Else if progress, reset the timer back to 2 ticks.
3787 */
3788 if (ri->ri_free == ri->ri_max || (sc->tulip_flags & TULIP_TXPROBE_ACTIVE))
3789 sc->tulip_txtimer = 0;
3790 else if (xmits > 0)
3791 sc->tulip_txtimer = TULIP_TXTIMER;
3792 sc->tulip_if.if_opackets += xmits;
3793 TULIP_PERFEND(txintr);
3794 return descs;
3795}
3796\f
3797static void
3798tulip_print_abnormal_interrupt(
3799 tulip_softc_t * const sc,
3800 u_int32_t csr)
3801{
3802 const char * const *msgp = tulip_status_bits;
3803 const char *sep;
3804 u_int32_t mask;
3805 const char thrsh[] = "72|128\0\0\0" "96|256\0\0\0" "128|512\0\0" "160|1024";
3806
3807 csr &= (1 << (sizeof(tulip_status_bits)/sizeof(tulip_status_bits[0]))) - 1;
3808 printf("%s%d: abnormal interrupt:", sc->tulip_name, sc->tulip_unit);
3809 for (sep = " ", mask = 1; mask <= csr; mask <<= 1, msgp++) {
3810 if ((csr & mask) && *msgp != NULL) {
3811 printf("%s%s", sep, *msgp);
3812 if (mask == TULIP_STS_TXUNDERFLOW && (sc->tulip_flags & TULIP_NEWTXTHRESH)) {
3813 sc->tulip_flags &= ~TULIP_NEWTXTHRESH;
3814 if (sc->tulip_cmdmode & TULIP_CMD_STOREFWD) {
3815 printf(" (switching to store-and-forward mode)");
3816 } else {
3817 printf(" (raising TX threshold to %s)",
3818 &thrsh[9 * ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) >> 14)]);
3819 }
3820 }
3821 sep = ", ";
3822 }
3823 }
3824 printf("\n");
3825}
3826
3827static void
3828tulip_intr_handler(
3829 tulip_softc_t * const sc,
3830 int *progress_p)
3831{
3832 TULIP_PERFSTART(intr)
3833 u_int32_t csr;
3834
3835 while ((csr = TULIP_CSR_READ(sc, csr_status)) & sc->tulip_intrmask) {
3836 *progress_p = 1;
3837 TULIP_CSR_WRITE(sc, csr_status, csr);
3838
3839 if (csr & TULIP_STS_SYSERROR) {
3840 sc->tulip_last_system_error = (csr & TULIP_STS_ERRORMASK) >> TULIP_STS_ERR_SHIFT;
3841 if (sc->tulip_flags & TULIP_NOMESSAGES) {
3842 sc->tulip_flags |= TULIP_SYSTEMERROR;
3843 } else {
3844 printf("%s%d: system error: %s\n",
3845 sc->tulip_name, sc->tulip_unit,
3846 tulip_system_errors[sc->tulip_last_system_error]);
3847 }
3848 sc->tulip_flags |= TULIP_NEEDRESET;
3849 sc->tulip_system_errors++;
3850 break;
3851 }
3852 if (csr & (TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL) & sc->tulip_intrmask) {
3853#if defined(TULIP_DEBUG)
3854 sc->tulip_dbg.dbg_link_intrs++;
3855#endif
3856 if (sc->tulip_boardsw->bd_media_poll != NULL) {
3857 (*sc->tulip_boardsw->bd_media_poll)(sc, csr & TULIP_STS_LINKFAIL
3858 ? TULIP_MEDIAPOLL_LINKFAIL
3859 : TULIP_MEDIAPOLL_LINKPASS);
3860 csr &= ~TULIP_STS_ABNRMLINTR;
3861 }
3862 tulip_media_print(sc);
3863 }
3864 if (csr & (TULIP_STS_RXINTR|TULIP_STS_RXNOBUF)) {
3865 u_int32_t misses = TULIP_CSR_READ(sc, csr_missed_frames);
3866 if (csr & TULIP_STS_RXNOBUF)
3867 sc->tulip_dot3stats.dot3StatsMissedFrames += misses & 0xFFFF;
3868 /*
3869 * Pass 2.[012] of the 21140A-A[CDE] may hang and/or corrupt data
3870 * on receive overflows.
3871 */
3872 if ((misses & 0x0FFE0000) && (sc->tulip_features & TULIP_HAVE_RXBADOVRFLW)) {
3873 sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
3874 /*
3875 * Stop the receiver process and spin until it's stopped.
3876 * Tell rx_intr to drop the packets it dequeues.
3877 */
3878 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode & ~TULIP_CMD_RXRUN);
3879 while ((TULIP_CSR_READ(sc, csr_status) & TULIP_STS_RXSTOPPED) == 0)
3880 ;
3881 TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
3882 sc->tulip_flags |= TULIP_RXIGNORE;
3883 }
3884 tulip_rx_intr(sc);
3885 if (sc->tulip_flags & TULIP_RXIGNORE) {
3886 /*
3887 * Restart the receiver.
3888 */
3889 sc->tulip_flags &= ~TULIP_RXIGNORE;
3890 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3891 }
3892 }
3893 if (csr & TULIP_STS_ABNRMLINTR) {
3894 u_int32_t tmp = csr & sc->tulip_intrmask
3895 & ~(TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR);
3896 if (csr & TULIP_STS_TXUNDERFLOW) {
3897 if ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) != TULIP_CMD_THRSHLD160) {
3898 sc->tulip_cmdmode += TULIP_CMD_THRSHLD96;
3899 sc->tulip_flags |= TULIP_NEWTXTHRESH;
3900 } else if (sc->tulip_features & TULIP_HAVE_STOREFWD) {
3901 sc->tulip_cmdmode |= TULIP_CMD_STOREFWD;
3902 sc->tulip_flags |= TULIP_NEWTXTHRESH;
3903 }
3904 }
3905 if (sc->tulip_flags & TULIP_NOMESSAGES) {
3906 sc->tulip_statusbits |= tmp;
3907 } else {
3908 tulip_print_abnormal_interrupt(sc, tmp);
3909 sc->tulip_flags |= TULIP_NOMESSAGES;
3910 }
3911 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3912 }
3913 if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_TXPROBE_ACTIVE|TULIP_DOINGSETUP|TULIP_PROMISC)) {
3914 tulip_tx_intr(sc);
3915 if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
3916 tulip_ifstart(&sc->tulip_if);
3917 }
3918 }
3919 if (sc->tulip_flags & TULIP_NEEDRESET) {
3920 tulip_reset(sc);
3921 tulip_init(sc);
3922 }
3923 TULIP_PERFEND(intr);
3924}
3925
3926#if defined(TULIP_USE_SOFTINTR)
3927/*
3928 * This is a experimental idea to alleviate problems due to interrupt
3929 * livelock. What is interrupt livelock? It's when you spend all your
3930 * time servicing device interrupts and never drop below device ipl
3931 * to do "useful" work.
3932 *
3933 * So what we do here is see if the device needs service and if so,
3934 * disable interrupts (dismiss the interrupt), place it in a list of devices
3935 * needing service, and issue a network software interrupt.
3936 *
3937 * When our network software interrupt routine gets called, we simply
3938 * walk done the list of devices that we have created and deal with them
3939 * at splnet/splsoftnet.
3940 *
3941 */
3942static void
3943tulip_hardintr_handler(
3944 tulip_softc_t * const sc,
3945 int *progress_p)
3946{
3947 if (TULIP_CSR_READ(sc, csr_status) & (TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR) == 0)
3948 return;
3949 *progress_p = 1;
3950 /*
3951 * disable interrupts
3952 */
3953 TULIP_CSR_WRITE(sc, csr_intr, 0);
3954 /*
3955 * mark it as needing a software interrupt
3956 */
3957 tulip_softintr_mask |= (1U << sc->tulip_unit);
3958}
3959
3960static void
3961tulip_softintr(
3962 void)
3963{
3964 u_int32_t softintr_mask, mask;
3965 int progress = 0;
3966 int unit;
3967 int s;
3968
3969 /*
3970 * Copy mask to local copy and reset global one to 0.
3971 */
3972 s = splimp();
3973 softintr_mask = tulip_softintr_mask;
3974 tulip_softintr_mask = 0;
3975 splx(s);
3976
3977 /*
3978 * Optimize for the single unit case.
3979 */
3980 if (tulip_softintr_max_unit == 0) {
3981 if (softintr_mask & 1) {
3982 tulip_softc_t * const sc = tulips[0];
3983 /*
3984 * Handle the "interrupt" and then reenable interrupts
3985 */
3986 softintr_mask = 0;
3987 tulip_intr_handler(sc, &progress);
3988 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3989 }
3990 return;
3991 }
3992
3993 /*
3994 * Handle all "queued" interrupts in a round robin fashion.
3995 * This is done so as not to favor a particular interface.
3996 */
3997 unit = tulip_softintr_last_unit;
3998 mask = (1U << unit);
3999 while (softintr_mask != 0) {
4000 if (tulip_softintr_max_unit == unit) {
4001 unit = 0; mask = 1;
4002 } else {
4003 unit += 1; mask <<= 1;
4004 }
4005 if (softintr_mask & mask) {
4006 tulip_softc_t * const sc = tulips[unit];
4007 /*
4008 * Handle the "interrupt" and then reenable interrupts