Deleted Added
full compact
if_de.c (61040) if_de.c (63090)
1/* $NetBSD: if_de.c,v 1.86 1999/06/01 19:17:59 thorpej Exp $ */
2
1/* $NetBSD: if_de.c,v 1.86 1999/06/01 19:17:59 thorpej Exp $ */
2
3/* $FreeBSD: head/sys/dev/de/if_de.c 61040 2000-05-28 16:06:56Z peter $ */
3/* $FreeBSD: head/sys/dev/de/if_de.c 63090 2000-07-13 22:54:34Z archie $ */
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
1506/*
1507 ********************************************************************
1508 * Start of 21140/21140A support which does not use the MII interface
1509 */
1510
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
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
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
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
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
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
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
1814/*
1815 * ******** END of chip-specific handlers. ***********
1816 */
1817
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
4009 */
4010 softintr_mask ^= mask;
4011 tulip_intr_handler(sc, &progress);
4012 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4013 }
4014 }
4015
4016 /*
4017 * Save where we ending up.
4018 */
4019 tulip_softintr_last_unit = unit;
4020}
4021#endif /* TULIP_USE_SOFTINTR */
4022
4023static void
4024tulip_intr_shared(
4025 void *arg)
4026{
4027 tulip_softc_t * sc = arg;
4028 int progress = 0;
4029
4030 for (; sc != NULL; sc = sc->tulip_slaves) {
4031#if defined(TULIP_DEBUG)
4032 sc->tulip_dbg.dbg_intrs++;
4033#endif
4034#if defined(TULIP_USE_SOFTINTR)
4035 tulip_hardintr_handler(sc, &progress);
4036#else
4037 tulip_intr_handler(sc, &progress);
4038#endif
4039 }
4040#if defined(TULIP_USE_SOFTINTR)
4041 if (progress)
4042 schednetisr(NETISR_DE);
4043#endif
4044}
4045
4046static void
4047tulip_intr_normal(
4048 void *arg)
4049{
4050 tulip_softc_t * sc = (tulip_softc_t *) arg;
4051 int progress = 0;
4052
4053#if defined(TULIP_DEBUG)
4054 sc->tulip_dbg.dbg_intrs++;
4055#endif
4056#if defined(TULIP_USE_SOFTINTR)
4057 tulip_hardintr_handler(sc, &progress);
4058 if (progress)
4059 schednetisr(NETISR_DE);
4060#else
4061 tulip_intr_handler(sc, &progress);
4062#endif
4063}
4064
4065static struct mbuf *
4066tulip_mbuf_compress(
4067 struct mbuf *m)
4068{
4069 struct mbuf *m0;
4070#if MCLBYTES >= ETHERMTU + 18 && !defined(BIG_PACKET)
4071 MGETHDR(m0, M_DONTWAIT, MT_DATA);
4072 if (m0 != NULL) {
4073 if (m->m_pkthdr.len > MHLEN) {
4074 MCLGET(m0, M_DONTWAIT);
4075 if ((m0->m_flags & M_EXT) == 0) {
4076 m_freem(m);
4077 m_freem(m0);
4078 return NULL;
4079 }
4080 }
4081 m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t));
4082 m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
4083 }
4084#else
4085 int mlen = MHLEN;
4086 int len = m->m_pkthdr.len;
4087 struct mbuf **mp = &m0;
4088
4089 while (len > 0) {
4090 if (mlen == MHLEN) {
4091 MGETHDR(*mp, M_DONTWAIT, MT_DATA);
4092 } else {
4093 MGET(*mp, M_DONTWAIT, MT_DATA);
4094 }
4095 if (*mp == NULL) {
4096 m_freem(m0);
4097 m0 = NULL;
4098 break;
4099 }
4100 if (len > MLEN) {
4101 MCLGET(*mp, M_DONTWAIT);
4102 if (((*mp)->m_flags & M_EXT) == 0) {
4103 m_freem(m0);
4104 m0 = NULL;
4105 break;
4106 }
4107 (*mp)->m_len = len <= MCLBYTES ? len : MCLBYTES;
4108 } else {
4109 (*mp)->m_len = len <= mlen ? len : mlen;
4110 }
4111 m_copydata(m, m->m_pkthdr.len - len,
4112 (*mp)->m_len, mtod((*mp), caddr_t));
4113 len -= (*mp)->m_len;
4114 mp = &(*mp)->m_next;
4115 mlen = MLEN;
4116 }
4117#endif
4118 m_freem(m);
4119 return m0;
4120}
4121
4122static struct mbuf *
4123tulip_txput(
4124 tulip_softc_t * const sc,
4125 struct mbuf *m)
4126{
4127 TULIP_PERFSTART(txput)
4128 tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
4129 tulip_desc_t *eop, *nextout;
4130 int segcnt, free;
4131 u_int32_t d_status;
4132#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
4133 bus_dmamap_t map;
4134 int error;
4135#else
4136 struct mbuf *m0;
4137#endif
4138
4139#if defined(TULIP_DEBUG)
4140 if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
4141 printf("%s%d: txput%s: tx not running\n",
4142 sc->tulip_name, sc->tulip_unit,
4143 (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) ? "(probe)" : "");
4144 sc->tulip_flags |= TULIP_WANTTXSTART;
4145 sc->tulip_dbg.dbg_txput_finishes[0]++;
4146 goto finish;
4147 }
4148#endif
4149
4150 /*
4151 * Now we try to fill in our transmit descriptors. This is
4152 * a bit reminiscent of going on the Ark two by two
4153 * since each descriptor for the TULIP can describe
4154 * two buffers. So we advance through packet filling
4155 * each of the two entries at a time to to fill each
4156 * descriptor. Clear the first and last segment bits
4157 * in each descriptor (actually just clear everything
4158 * but the end-of-ring or chain bits) to make sure
4159 * we don't get messed up by previously sent packets.
4160 *
4161 * We may fail to put the entire packet on the ring if
4162 * there is either not enough ring entries free or if the
4163 * packet has more than MAX_TXSEG segments. In the former
4164 * case we will just wait for the ring to empty. In the
4165 * latter case we have to recopy.
4166 */
4167#if !defined(TULIP_BUS_DMA) || defined(TULIP_BUS_DMA_NOTX)
4168 again:
4169 m0 = m;
4170#endif
4171 d_status = 0;
4172 eop = nextout = ri->ri_nextout;
4173 segcnt = 0;
4174 free = ri->ri_free;
4175
4176#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
4177 /*
4178 * Reclaim some dma maps from if we are out.
4179 */
4180 if (sc->tulip_txmaps_free == 0) {
4181#if defined(TULIP_DEBUG)
4182 sc->tulip_dbg.dbg_no_txmaps++;
4183#endif
4184 free += tulip_tx_intr(sc);
4185 }
4186 if (sc->tulip_txmaps_free > 0) {
4187 map = sc->tulip_txmaps[sc->tulip_txmaps_free-1];
4188 } else {
4189 sc->tulip_flags |= TULIP_WANTTXSTART;
4190#if defined(TULIP_DEBUG)
4191 sc->tulip_dbg.dbg_txput_finishes[1]++;
4192#endif
4193 goto finish;
4194 }
4195 error = bus_dmamap_load_mbuf(sc->tulip_dmatag, map, m, BUS_DMA_NOWAIT);
4196 if (error != 0) {
4197 if (error == EFBIG) {
4198 /*
4199 * The packet exceeds the number of transmit buffer
4200 * entries that we can use for one packet, so we have
4201 * to recopy it into one mbuf and then try again.
4202 */
4203 m = tulip_mbuf_compress(m);
4204 if (m == NULL) {
4205#if defined(TULIP_DEBUG)
4206 sc->tulip_dbg.dbg_txput_finishes[2]++;
4207#endif
4208 goto finish;
4209 }
4210 error = bus_dmamap_load_mbuf(sc->tulip_dmatag, map, m, BUS_DMA_NOWAIT);
4211 }
4212 if (error != 0) {
4213 printf("%s%d: unable to load tx map, "
4214 "error = %d\n", sc->tulip_name, sc->tulip_unit, error);
4215#if defined(TULIP_DEBUG)
4216 sc->tulip_dbg.dbg_txput_finishes[3]++;
4217#endif
4218 goto finish;
4219 }
4220 }
4221 if ((free -= (map->dm_nsegs + 1) / 2) <= 0
4222 /*
4223 * See if there's any unclaimed space in the transmit ring.
4224 */
4225 && (free += tulip_tx_intr(sc)) <= 0) {
4226 /*
4227 * There's no more room but since nothing
4228 * has been committed at this point, just
4229 * show output is active, put back the
4230 * mbuf and return.
4231 */
4232 sc->tulip_flags |= TULIP_WANTTXSTART;
4233#if defined(TULIP_DEBUG)
4234 sc->tulip_dbg.dbg_txput_finishes[4]++;
4235#endif
4236 bus_dmamap_unload(sc->tulip_dmatag, map);
4237 goto finish;
4238 }
4239 for (; map->dm_nsegs - segcnt > 1; segcnt += 2) {
4240 eop = nextout;
4241 eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4242 eop->d_status = d_status;
4243 eop->d_addr1 = map->dm_segs[segcnt].ds_addr;
4244 eop->d_length1 = map->dm_segs[segcnt].ds_len;
4245 eop->d_addr2 = map->dm_segs[segcnt+1].ds_addr;
4246 eop->d_length2 = map->dm_segs[segcnt+1].ds_len;
4247 d_status = TULIP_DSTS_OWNER;
4248 if (++nextout == ri->ri_last)
4249 nextout = ri->ri_first;
4250 }
4251 if (segcnt < map->dm_nsegs) {
4252 eop = nextout;
4253 eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4254 eop->d_status = d_status;
4255 eop->d_addr1 = map->dm_segs[segcnt].ds_addr;
4256 eop->d_length1 = map->dm_segs[segcnt].ds_len;
4257 eop->d_addr2 = 0;
4258 eop->d_length2 = 0;
4259 if (++nextout == ri->ri_last)
4260 nextout = ri->ri_first;
4261 }
4262 TULIP_TXMAP_PRESYNC(sc, map);
4263 M_SETCTX(m, map);
4264 map = NULL;
4265 --sc->tulip_txmaps_free; /* commit to using the dmamap */
4266
4267#else /* !TULIP_BUS_DMA */
4268
4269 do {
4270 int len = m0->m_len;
4271 caddr_t addr = mtod(m0, caddr_t);
4272 unsigned clsize = PAGE_SIZE - (((uintptr_t) addr) & (PAGE_SIZE-1));
4273
4274 while (len > 0) {
4275 unsigned slen = min(len, clsize);
4276#ifdef BIG_PACKET
4277 int partial = 0;
4278 if (slen >= 2048)
4279 slen = 2040, partial = 1;
4280#endif
4281 segcnt++;
4282 if (segcnt > TULIP_MAX_TXSEG) {
4283 /*
4284 * The packet exceeds the number of transmit buffer
4285 * entries that we can use for one packet, so we have
4286 * recopy it into one mbuf and then try again.
4287 */
4288 m = tulip_mbuf_compress(m);
4289 if (m == NULL)
4290 goto finish;
4291 goto again;
4292 }
4293 if (segcnt & 1) {
4294 if (--free == 0) {
4295 /*
4296 * See if there's any unclaimed space in the
4297 * transmit ring.
4298 */
4299 if ((free += tulip_tx_intr(sc)) == 0) {
4300 /*
4301 * There's no more room but since nothing
4302 * has been committed at this point, just
4303 * show output is active, put back the
4304 * mbuf and return.
4305 */
4306 sc->tulip_flags |= TULIP_WANTTXSTART;
4307#if defined(TULIP_DEBUG)
4308 sc->tulip_dbg.dbg_txput_finishes[1]++;
4309#endif
4310 goto finish;
4311 }
4312 }
4313 eop = nextout;
4314 if (++nextout == ri->ri_last)
4315 nextout = ri->ri_first;
4316 eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4317 eop->d_status = d_status;
4318 eop->d_addr1 = TULIP_KVATOPHYS(sc, addr);
4319 eop->d_length1 = slen;
4320 } else {
4321 /*
4322 * Fill in second half of descriptor
4323 */
4324 eop->d_addr2 = TULIP_KVATOPHYS(sc, addr);
4325 eop->d_length2 = slen;
4326 }
4327 d_status = TULIP_DSTS_OWNER;
4328 len -= slen;
4329 addr += slen;
4330#ifdef BIG_PACKET
4331 if (partial)
4332 continue;
4333#endif
4334 clsize = PAGE_SIZE;
4335 }
4336 } while ((m0 = m0->m_next) != NULL);
4337#endif /* TULIP_BUS_DMA */
4338
4339 /*
4340 * bounce a copy to the bpf listener, if any.
4341 */
4342 if (sc->tulip_if.if_bpf != NULL)
4343 bpf_mtap(&sc->tulip_if, m);
4344
4345 /*
4346 * The descriptors have been filled in. Now get ready
4347 * to transmit.
4348 */
4349 IF_ENQUEUE(&sc->tulip_txq, m);
4350 m = NULL;
4351
4352 /*
4353 * Make sure the next descriptor after this packet is owned
4354 * by us since it may have been set up above if we ran out
4355 * of room in the ring.
4356 */
4357 nextout->d_status = 0;
4358 TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(u_int32_t));
4359
4360#if !defined(TULIP_BUS_DMA) || defined(TULIP_BUS_DMA_NOTX)
4361 /*
4362 * If we only used the first segment of the last descriptor,
4363 * make sure the second segment will not be used.
4364 */
4365 if (segcnt & 1) {
4366 eop->d_addr2 = 0;
4367 eop->d_length2 = 0;
4368 }
4369#endif /* TULIP_BUS_DMA */
4370
4371 /*
4372 * Mark the last and first segments, indicate we want a transmit
4373 * complete interrupt, and tell it to transmit!
4374 */
4375 eop->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
4376
4377 /*
4378 * Note that ri->ri_nextout is still the start of the packet
4379 * and until we set the OWNER bit, we can still back out of
4380 * everything we have done.
4381 */
4382 ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
4383#if defined(TULIP_BUS_MAP) && !defined(TULIP_BUS_DMA_NOTX)
4384 if (eop < ri->ri_nextout) {
4385 TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout,
4386 (caddr_t) ri->ri_last - (caddr_t) ri->ri_nextout);
4387 TULIP_TXDESC_PRESYNC(sc, ri->ri_first,
4388 (caddr_t) (eop + 1) - (caddr_t) ri->ri_first);
4389 } else {
4390 TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout,
4391 (caddr_t) (eop + 1) - (caddr_t) ri->ri_nextout);
4392 }
4393#endif
4394 ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
4395 TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout, sizeof(u_int32_t));
4396
4397 /*
4398 * This advances the ring for us.
4399 */
4400 ri->ri_nextout = nextout;
4401 ri->ri_free = free;
4402
4403 TULIP_PERFEND(txput);
4404
4405 if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
4406 TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4407 sc->tulip_if.if_flags |= IFF_OACTIVE;
4408 sc->tulip_if.if_start = tulip_ifstart;
4409 TULIP_PERFEND(txput);
4410 return NULL;
4411 }
4412
4413 /*
4414 * switch back to the single queueing ifstart.
4415 */
4416 sc->tulip_flags &= ~TULIP_WANTTXSTART;
4417 if (sc->tulip_txtimer == 0)
4418 sc->tulip_txtimer = TULIP_TXTIMER;
4419#if defined(TULIP_DEBUG)
4420 sc->tulip_dbg.dbg_txput_finishes[5]++;
4421#endif
4422
4423 /*
4424 * If we want a txstart, there must be not enough space in the
4425 * transmit ring. So we want to enable transmit done interrupts
4426 * so we can immediately reclaim some space. When the transmit
4427 * interrupt is posted, the interrupt handler will call tx_intr
4428 * to reclaim space and then txstart (since WANTTXSTART is set).
4429 * txstart will move the packet into the transmit ring and clear
4430 * WANTTXSTART thereby causing TXINTR to be cleared.
4431 */
4432 finish:
4433#if defined(TULIP_DEBUG)
4434 sc->tulip_dbg.dbg_txput_finishes[6]++;
4435#endif
4436 if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_DOINGSETUP)) {
4437 sc->tulip_if.if_flags |= IFF_OACTIVE;
4438 sc->tulip_if.if_start = tulip_ifstart;
4439 if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
4440 sc->tulip_intrmask |= TULIP_STS_TXINTR;
4441 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4442 }
4443 } else if ((sc->tulip_flags & TULIP_PROMISC) == 0) {
4444 if (sc->tulip_intrmask & TULIP_STS_TXINTR) {
4445 sc->tulip_intrmask &= ~TULIP_STS_TXINTR;
4446 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4447 }
4448 }
4449 TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4450 TULIP_PERFEND(txput);
4451 return m;
4452}
4453
4454static void
4455tulip_txput_setup(
4456 tulip_softc_t * const sc)
4457{
4458 tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
4459 tulip_desc_t *nextout;
4460
4461 /*
4462 * We will transmit, at most, one setup packet per call to ifstart.
4463 */
4464
4465#if defined(TULIP_DEBUG)
4466 if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
4467 printf("%s%d: txput_setup: tx not running\n",
4468 sc->tulip_name, sc->tulip_unit);
4469 sc->tulip_flags |= TULIP_WANTTXSTART;
4470 sc->tulip_if.if_start = tulip_ifstart;
4471 return;
4472 }
4473#endif
4474 /*
4475 * Try to reclaim some free descriptors..
4476 */
4477 if (ri->ri_free < 2)
4478 tulip_tx_intr(sc);
4479 if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
4480 sc->tulip_flags |= TULIP_WANTTXSTART;
4481 sc->tulip_if.if_start = tulip_ifstart;
4482 return;
4483 }
4484 bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
4485 sizeof(sc->tulip_setupbuf));
4486 /*
4487 * Clear WANTSETUP and set DOINGSETUP. Set know that WANTSETUP is
4488 * set and DOINGSETUP is clear doing an XOR of the two will DTRT.
4489 */
4490 sc->tulip_flags ^= TULIP_WANTSETUP|TULIP_DOINGSETUP;
4491 ri->ri_free--;
4492 nextout = ri->ri_nextout;
4493 nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4494 nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
4495 |TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
4496 if (sc->tulip_flags & TULIP_WANTHASHPERFECT)
4497 nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
4498 else if (sc->tulip_flags & TULIP_WANTHASHONLY)
4499 nextout->d_flag |= TULIP_DFLAG_TxHASHFILT|TULIP_DFLAG_TxINVRSFILT;
4500
4501 nextout->d_length2 = 0;
4502 nextout->d_addr2 = 0;
4503#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
4504 nextout->d_length1 = sc->tulip_setupmap->dm_segs[0].ds_len;
4505 nextout->d_addr1 = sc->tulip_setupmap->dm_segs[0].ds_addr;
4506 if (sc->tulip_setupmap->dm_nsegs == 2) {
4507 nextout->d_length2 = sc->tulip_setupmap->dm_segs[1].ds_len;
4508 nextout->d_addr2 = sc->tulip_setupmap->dm_segs[1].ds_addr;
4509 }
4510 TULIP_TXMAP_PRESYNC(sc, sc->tulip_setupmap);
4511 TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(*nextout));
4512#else
4513 nextout->d_length1 = sizeof(sc->tulip_setupbuf);
4514 nextout->d_addr1 = TULIP_KVATOPHYS(sc, sc->tulip_setupbuf);
4515#endif
4516
4517 /*
4518 * Advance the ring for the next transmit packet.
4519 */
4520 if (++ri->ri_nextout == ri->ri_last)
4521 ri->ri_nextout = ri->ri_first;
4522
4523 /*
4524 * Make sure the next descriptor is owned by us since it
4525 * may have been set up above if we ran out of room in the
4526 * ring.
4527 */
4528 ri->ri_nextout->d_status = 0;
4529 TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout, sizeof(u_int32_t));
4530 nextout->d_status = TULIP_DSTS_OWNER;
4531 /*
4532 * Flush the ownwership of the current descriptor
4533 */
4534 TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(u_int32_t));
4535 TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4536 if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
4537 sc->tulip_intrmask |= TULIP_STS_TXINTR;
4538 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4539 }
4540}
4541
4542
4543/*
4544 * This routine is entered at splnet() (splsoftnet() on NetBSD)
4545 * and thereby imposes no problems when TULIP_USE_SOFTINTR is
4546 * defined or not.
4547 */
4548static int
4549tulip_ifioctl(
4550 struct ifnet * ifp,
4551 u_long cmd,
4552 caddr_t data)
4553{
4554 TULIP_PERFSTART(ifioctl)
4555 tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
4556 struct ifaddr *ifa = (struct ifaddr *)data;
4557 struct ifreq *ifr = (struct ifreq *) data;
4558 int s;
4559 int error = 0;
4560
4561#if defined(TULIP_USE_SOFTINTR)
4562 s = splnet();
4563#else
4564 s = splimp();
4565#endif
4566 switch (cmd) {
4567 case SIOCSIFADDR: {
4568 ifp->if_flags |= IFF_UP;
4569 switch(ifa->ifa_addr->sa_family) {
4570#ifdef INET
4571 case AF_INET: {
4572 tulip_init(sc);
4573 arp_ifinit(&(sc)->tulip_ac, ifa);
4574 break;
4575 }
4576#endif /* INET */
4577
4578#ifdef IPX
4579 case AF_IPX: {
4580 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
4581 if (ipx_nullhost(*ina)) {
4582 ina->x_host = *(union ipx_host *)(sc->tulip_enaddr);
4583 } else {
4584 ifp->if_flags &= ~IFF_RUNNING;
4585 bcopy((caddr_t)ina->x_host.c_host,
4586 (caddr_t)sc->tulip_enaddr,
4587 sizeof(sc->tulip_enaddr));
4588 }
4589 tulip_init(sc);
4590 break;
4591 }
4592#endif /* IPX */
4593
4594#ifdef NS
4595 /*
4596 * This magic copied from if_is.c; I don't use XNS,
4597 * so I have no way of telling if this actually
4598 * works or not.
4599 */
4600 case AF_NS: {
4601 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
4602 if (ns_nullhost(*ina)) {
4603 ina->x_host = *(union ns_host *)(sc->tulip_enaddr);
4604 } else {
4605 ifp->if_flags &= ~IFF_RUNNING;
4606 bcopy((caddr_t)ina->x_host.c_host,
4607 (caddr_t)sc->tulip_enaddr,
4608 sizeof(sc->tulip_enaddr));
4609 }
4610 tulip_init(sc);
4611 break;
4612 }
4613#endif /* NS */
4614
4615 default: {
4616 tulip_init(sc);
4617 break;
4618 }
4619 }
4620 break;
4621 }
4622 case SIOCGIFADDR: {
4623 bcopy((caddr_t) sc->tulip_enaddr,
4624 (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
4625 6);
4626 break;
4627 }
4628
4629 case SIOCSIFFLAGS: {
4630 tulip_addr_filter(sc); /* reinit multicast filter */
4631 tulip_init(sc);
4632 break;
4633 }
4634
4635 case SIOCSIFMEDIA:
4636 case SIOCGIFMEDIA: {
4637 error = ifmedia_ioctl(ifp, ifr, &sc->tulip_ifmedia, cmd);
4638 break;
4639 }
4640
4641 case SIOCADDMULTI:
4642 case SIOCDELMULTI: {
4643 /*
4644 * Update multicast listeners
4645 */
4646 tulip_addr_filter(sc); /* reset multicast filtering */
4647 tulip_init(sc);
4648 error = 0;
4649 break;
4650 }
4651
4652 case SIOCSIFMTU:
4653 /*
4654 * Set the interface MTU.
4655 */
4656 if (ifr->ifr_mtu > ETHERMTU
4657#ifdef BIG_PACKET
4658 && sc->tulip_chipid != TULIP_21140
4659 && sc->tulip_chipid != TULIP_21140A
4660 && sc->tulip_chipid != TULIP_21041
4661#endif
4662 ) {
4663 error = EINVAL;
4664 break;
4665 }
4666 ifp->if_mtu = ifr->ifr_mtu;
4667#ifdef BIG_PACKET
4668 tulip_reset(sc);
4669 tulip_init(sc);
4670#endif
4671 break;
4672
4673#ifdef SIOCGADDRROM
4674 case SIOCGADDRROM: {
4675 error = copyout(sc->tulip_rombuf, ifr->ifr_data, sizeof(sc->tulip_rombuf));
4676 break;
4677 }
4678#endif
4679#ifdef SIOCGCHIPID
4680 case SIOCGCHIPID: {
4681 ifr->ifr_metric = (int) sc->tulip_chipid;
4682 break;
4683 }
4684#endif
4685 default: {
4686 error = EINVAL;
4687 break;
4688 }
4689 }
4690
4691 splx(s);
4692 TULIP_PERFEND(ifioctl);
4693 return error;
4694}
4695
4696/*
4697 * These routines gets called at device spl (from ether_output). This might
4698 * pose a problem for TULIP_USE_SOFTINTR if ether_output is called at
4699 * device spl from another driver.
4700 */
4701
4702static void
4703tulip_ifstart(
4704 struct ifnet * const ifp)
4705{
4706 TULIP_PERFSTART(ifstart)
4707 tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
4708
4709 if (sc->tulip_if.if_flags & IFF_RUNNING) {
4710
4711 if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
4712 tulip_txput_setup(sc);
4713
4714 while (sc->tulip_if.if_snd.ifq_head != NULL) {
4715 struct mbuf *m;
4716 IF_DEQUEUE(&sc->tulip_if.if_snd, m);
4717 if ((m = tulip_txput(sc, m)) != NULL) {
4718 IF_PREPEND(&sc->tulip_if.if_snd, m);
4719 break;
4720 }
4721 }
4722 if (sc->tulip_if.if_snd.ifq_head == NULL)
4723 sc->tulip_if.if_start = tulip_ifstart_one;
4724 }
4725
4726 TULIP_PERFEND(ifstart);
4727}
4728
4729static void
4730tulip_ifstart_one(
4731 struct ifnet * const ifp)
4732{
4733 TULIP_PERFSTART(ifstart_one)
4734 tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
4735
4736 if ((sc->tulip_if.if_flags & IFF_RUNNING)
4737 && sc->tulip_if.if_snd.ifq_head != NULL) {
4738 struct mbuf *m;
4739 IF_DEQUEUE(&sc->tulip_if.if_snd, m);
4740 if ((m = tulip_txput(sc, m)) != NULL)
4741 IF_PREPEND(&sc->tulip_if.if_snd, m);
4742 }
4743 TULIP_PERFEND(ifstart_one);
4744}
4745
4746/*
4747 * Even though this routine runs at device spl, it does not break
4748 * our use of splnet (splsoftnet under NetBSD) for the majority
4749 * of this driver (if TULIP_USE_SOFTINTR defined) since
4750 * if_watcbog is called from if_watchdog which is called from
4751 * splsoftclock which is below spl[soft]net.
4752 */
4753static void
4754tulip_ifwatchdog(
4755 struct ifnet *ifp)
4756{
4757 TULIP_PERFSTART(ifwatchdog)
4758 tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
4759
4760#if defined(TULIP_DEBUG)
4761 u_int32_t rxintrs = sc->tulip_dbg.dbg_rxintrs - sc->tulip_dbg.dbg_last_rxintrs;
4762 if (rxintrs > sc->tulip_dbg.dbg_high_rxintrs_hz)
4763 sc->tulip_dbg.dbg_high_rxintrs_hz = rxintrs;
4764 sc->tulip_dbg.dbg_last_rxintrs = sc->tulip_dbg.dbg_rxintrs;
4765#endif /* TULIP_DEBUG */
4766
4767 sc->tulip_if.if_timer = 1;
4768 /*
4769 * These should be rare so do a bulk test up front so we can just skip
4770 * them if needed.
4771 */
4772 if (sc->tulip_flags & (TULIP_SYSTEMERROR|TULIP_RXBUFSLOW|TULIP_NOMESSAGES)) {
4773 /*
4774 * If the number of receive buffer is low, try to refill
4775 */
4776 if (sc->tulip_flags & TULIP_RXBUFSLOW)
4777 tulip_rx_intr(sc);
4778
4779 if (sc->tulip_flags & TULIP_SYSTEMERROR) {
4780 printf("%s%d: %d system errors: last was %s\n",
4781 sc->tulip_name, sc->tulip_unit, sc->tulip_system_errors,
4782 tulip_system_errors[sc->tulip_last_system_error]);
4783 }
4784 if (sc->tulip_statusbits) {
4785 tulip_print_abnormal_interrupt(sc, sc->tulip_statusbits);
4786 sc->tulip_statusbits = 0;
4787 }
4788
4789 sc->tulip_flags &= ~(TULIP_NOMESSAGES|TULIP_SYSTEMERROR);
4790 }
4791
4792 if (sc->tulip_txtimer)
4793 tulip_tx_intr(sc);
4794 if (sc->tulip_txtimer && --sc->tulip_txtimer == 0) {
4795 printf("%s%d: transmission timeout\n", sc->tulip_name, sc->tulip_unit);
4796 if (TULIP_DO_AUTOSENSE(sc)) {
4797 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
4798 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
4799 sc->tulip_flags &= ~(TULIP_WANTRXACT|TULIP_LINKUP);
4800 }
4801 tulip_reset(sc);
4802 tulip_init(sc);
4803 }
4804
4805 TULIP_PERFEND(ifwatchdog);
4806 TULIP_PERFMERGE(sc, perf_intr_cycles);
4807 TULIP_PERFMERGE(sc, perf_ifstart_cycles);
4808 TULIP_PERFMERGE(sc, perf_ifioctl_cycles);
4809 TULIP_PERFMERGE(sc, perf_ifwatchdog_cycles);
4810 TULIP_PERFMERGE(sc, perf_timeout_cycles);
4811 TULIP_PERFMERGE(sc, perf_ifstart_one_cycles);
4812 TULIP_PERFMERGE(sc, perf_txput_cycles);
4813 TULIP_PERFMERGE(sc, perf_txintr_cycles);
4814 TULIP_PERFMERGE(sc, perf_rxintr_cycles);
4815 TULIP_PERFMERGE(sc, perf_rxget_cycles);
4816 TULIP_PERFMERGE(sc, perf_intr);
4817 TULIP_PERFMERGE(sc, perf_ifstart);
4818 TULIP_PERFMERGE(sc, perf_ifioctl);
4819 TULIP_PERFMERGE(sc, perf_ifwatchdog);
4820 TULIP_PERFMERGE(sc, perf_timeout);
4821 TULIP_PERFMERGE(sc, perf_ifstart_one);
4822 TULIP_PERFMERGE(sc, perf_txput);
4823 TULIP_PERFMERGE(sc, perf_txintr);
4824 TULIP_PERFMERGE(sc, perf_rxintr);
4825 TULIP_PERFMERGE(sc, perf_rxget);
4826}
4827
4828/*
4829 * All printf's are real as of now!
4830 */
4831#ifdef printf
4832#undef printf
4833#endif
4834
4835static void
4836tulip_attach(
4837 tulip_softc_t * const sc)
4838{
4839 struct ifnet * const ifp = &sc->tulip_if;
4840
4841 ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
4842 ifp->if_ioctl = tulip_ifioctl;
4843 ifp->if_start = tulip_ifstart;
4844 ifp->if_watchdog = tulip_ifwatchdog;
4845 ifp->if_timer = 1;
4846 ifp->if_output = ether_output;
4847
4848 printf("%s%d: %s%s pass %d.%d%s\n",
4849 sc->tulip_name, sc->tulip_unit,
4850 sc->tulip_boardid,
4851 tulip_chipdescs[sc->tulip_chipid],
4852 (sc->tulip_revinfo & 0xF0) >> 4,
4853 sc->tulip_revinfo & 0x0F,
4854 (sc->tulip_features & (TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM))
4855 == TULIP_HAVE_ISVSROM ? " (invalid EESPROM checksum)" : "");
4856 printf("%s%d: address %6D\n",
4857 sc->tulip_name, sc->tulip_unit, sc->tulip_enaddr, ":");
4858
4859#if defined(__alpha__)
4860 /*
4861 * In case the SRM console told us about a bogus media,
4862 * we need to check to be safe.
4863 */
4864 if (sc->tulip_mediums[sc->tulip_media] == NULL)
4865 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
4866#endif
4867
4868 (*sc->tulip_boardsw->bd_media_probe)(sc);
4869 ifmedia_init(&sc->tulip_ifmedia, 0,
4870 tulip_ifmedia_change,
4871 tulip_ifmedia_status);
4872 sc->tulip_flags &= ~TULIP_DEVICEPROBE;
4873 tulip_ifmedia_add(sc);
4874
4875 tulip_reset(sc);
4876
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
1506/*
1507 ********************************************************************
1508 * Start of 21140/21140A support which does not use the MII interface
1509 */
1510
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
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
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
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
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
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
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
1814/*
1815 * ******** END of chip-specific handlers. ***********
1816 */
1817
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
4009 */
4010 softintr_mask ^= mask;
4011 tulip_intr_handler(sc, &progress);
4012 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4013 }
4014 }
4015
4016 /*
4017 * Save where we ending up.
4018 */
4019 tulip_softintr_last_unit = unit;
4020}
4021#endif /* TULIP_USE_SOFTINTR */
4022
4023static void
4024tulip_intr_shared(
4025 void *arg)
4026{
4027 tulip_softc_t * sc = arg;
4028 int progress = 0;
4029
4030 for (; sc != NULL; sc = sc->tulip_slaves) {
4031#if defined(TULIP_DEBUG)
4032 sc->tulip_dbg.dbg_intrs++;
4033#endif
4034#if defined(TULIP_USE_SOFTINTR)
4035 tulip_hardintr_handler(sc, &progress);
4036#else
4037 tulip_intr_handler(sc, &progress);
4038#endif
4039 }
4040#if defined(TULIP_USE_SOFTINTR)
4041 if (progress)
4042 schednetisr(NETISR_DE);
4043#endif
4044}
4045
4046static void
4047tulip_intr_normal(
4048 void *arg)
4049{
4050 tulip_softc_t * sc = (tulip_softc_t *) arg;
4051 int progress = 0;
4052
4053#if defined(TULIP_DEBUG)
4054 sc->tulip_dbg.dbg_intrs++;
4055#endif
4056#if defined(TULIP_USE_SOFTINTR)
4057 tulip_hardintr_handler(sc, &progress);
4058 if (progress)
4059 schednetisr(NETISR_DE);
4060#else
4061 tulip_intr_handler(sc, &progress);
4062#endif
4063}
4064
4065static struct mbuf *
4066tulip_mbuf_compress(
4067 struct mbuf *m)
4068{
4069 struct mbuf *m0;
4070#if MCLBYTES >= ETHERMTU + 18 && !defined(BIG_PACKET)
4071 MGETHDR(m0, M_DONTWAIT, MT_DATA);
4072 if (m0 != NULL) {
4073 if (m->m_pkthdr.len > MHLEN) {
4074 MCLGET(m0, M_DONTWAIT);
4075 if ((m0->m_flags & M_EXT) == 0) {
4076 m_freem(m);
4077 m_freem(m0);
4078 return NULL;
4079 }
4080 }
4081 m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t));
4082 m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
4083 }
4084#else
4085 int mlen = MHLEN;
4086 int len = m->m_pkthdr.len;
4087 struct mbuf **mp = &m0;
4088
4089 while (len > 0) {
4090 if (mlen == MHLEN) {
4091 MGETHDR(*mp, M_DONTWAIT, MT_DATA);
4092 } else {
4093 MGET(*mp, M_DONTWAIT, MT_DATA);
4094 }
4095 if (*mp == NULL) {
4096 m_freem(m0);
4097 m0 = NULL;
4098 break;
4099 }
4100 if (len > MLEN) {
4101 MCLGET(*mp, M_DONTWAIT);
4102 if (((*mp)->m_flags & M_EXT) == 0) {
4103 m_freem(m0);
4104 m0 = NULL;
4105 break;
4106 }
4107 (*mp)->m_len = len <= MCLBYTES ? len : MCLBYTES;
4108 } else {
4109 (*mp)->m_len = len <= mlen ? len : mlen;
4110 }
4111 m_copydata(m, m->m_pkthdr.len - len,
4112 (*mp)->m_len, mtod((*mp), caddr_t));
4113 len -= (*mp)->m_len;
4114 mp = &(*mp)->m_next;
4115 mlen = MLEN;
4116 }
4117#endif
4118 m_freem(m);
4119 return m0;
4120}
4121
4122static struct mbuf *
4123tulip_txput(
4124 tulip_softc_t * const sc,
4125 struct mbuf *m)
4126{
4127 TULIP_PERFSTART(txput)
4128 tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
4129 tulip_desc_t *eop, *nextout;
4130 int segcnt, free;
4131 u_int32_t d_status;
4132#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
4133 bus_dmamap_t map;
4134 int error;
4135#else
4136 struct mbuf *m0;
4137#endif
4138
4139#if defined(TULIP_DEBUG)
4140 if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
4141 printf("%s%d: txput%s: tx not running\n",
4142 sc->tulip_name, sc->tulip_unit,
4143 (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) ? "(probe)" : "");
4144 sc->tulip_flags |= TULIP_WANTTXSTART;
4145 sc->tulip_dbg.dbg_txput_finishes[0]++;
4146 goto finish;
4147 }
4148#endif
4149
4150 /*
4151 * Now we try to fill in our transmit descriptors. This is
4152 * a bit reminiscent of going on the Ark two by two
4153 * since each descriptor for the TULIP can describe
4154 * two buffers. So we advance through packet filling
4155 * each of the two entries at a time to to fill each
4156 * descriptor. Clear the first and last segment bits
4157 * in each descriptor (actually just clear everything
4158 * but the end-of-ring or chain bits) to make sure
4159 * we don't get messed up by previously sent packets.
4160 *
4161 * We may fail to put the entire packet on the ring if
4162 * there is either not enough ring entries free or if the
4163 * packet has more than MAX_TXSEG segments. In the former
4164 * case we will just wait for the ring to empty. In the
4165 * latter case we have to recopy.
4166 */
4167#if !defined(TULIP_BUS_DMA) || defined(TULIP_BUS_DMA_NOTX)
4168 again:
4169 m0 = m;
4170#endif
4171 d_status = 0;
4172 eop = nextout = ri->ri_nextout;
4173 segcnt = 0;
4174 free = ri->ri_free;
4175
4176#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
4177 /*
4178 * Reclaim some dma maps from if we are out.
4179 */
4180 if (sc->tulip_txmaps_free == 0) {
4181#if defined(TULIP_DEBUG)
4182 sc->tulip_dbg.dbg_no_txmaps++;
4183#endif
4184 free += tulip_tx_intr(sc);
4185 }
4186 if (sc->tulip_txmaps_free > 0) {
4187 map = sc->tulip_txmaps[sc->tulip_txmaps_free-1];
4188 } else {
4189 sc->tulip_flags |= TULIP_WANTTXSTART;
4190#if defined(TULIP_DEBUG)
4191 sc->tulip_dbg.dbg_txput_finishes[1]++;
4192#endif
4193 goto finish;
4194 }
4195 error = bus_dmamap_load_mbuf(sc->tulip_dmatag, map, m, BUS_DMA_NOWAIT);
4196 if (error != 0) {
4197 if (error == EFBIG) {
4198 /*
4199 * The packet exceeds the number of transmit buffer
4200 * entries that we can use for one packet, so we have
4201 * to recopy it into one mbuf and then try again.
4202 */
4203 m = tulip_mbuf_compress(m);
4204 if (m == NULL) {
4205#if defined(TULIP_DEBUG)
4206 sc->tulip_dbg.dbg_txput_finishes[2]++;
4207#endif
4208 goto finish;
4209 }
4210 error = bus_dmamap_load_mbuf(sc->tulip_dmatag, map, m, BUS_DMA_NOWAIT);
4211 }
4212 if (error != 0) {
4213 printf("%s%d: unable to load tx map, "
4214 "error = %d\n", sc->tulip_name, sc->tulip_unit, error);
4215#if defined(TULIP_DEBUG)
4216 sc->tulip_dbg.dbg_txput_finishes[3]++;
4217#endif
4218 goto finish;
4219 }
4220 }
4221 if ((free -= (map->dm_nsegs + 1) / 2) <= 0
4222 /*
4223 * See if there's any unclaimed space in the transmit ring.
4224 */
4225 && (free += tulip_tx_intr(sc)) <= 0) {
4226 /*
4227 * There's no more room but since nothing
4228 * has been committed at this point, just
4229 * show output is active, put back the
4230 * mbuf and return.
4231 */
4232 sc->tulip_flags |= TULIP_WANTTXSTART;
4233#if defined(TULIP_DEBUG)
4234 sc->tulip_dbg.dbg_txput_finishes[4]++;
4235#endif
4236 bus_dmamap_unload(sc->tulip_dmatag, map);
4237 goto finish;
4238 }
4239 for (; map->dm_nsegs - segcnt > 1; segcnt += 2) {
4240 eop = nextout;
4241 eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4242 eop->d_status = d_status;
4243 eop->d_addr1 = map->dm_segs[segcnt].ds_addr;
4244 eop->d_length1 = map->dm_segs[segcnt].ds_len;
4245 eop->d_addr2 = map->dm_segs[segcnt+1].ds_addr;
4246 eop->d_length2 = map->dm_segs[segcnt+1].ds_len;
4247 d_status = TULIP_DSTS_OWNER;
4248 if (++nextout == ri->ri_last)
4249 nextout = ri->ri_first;
4250 }
4251 if (segcnt < map->dm_nsegs) {
4252 eop = nextout;
4253 eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4254 eop->d_status = d_status;
4255 eop->d_addr1 = map->dm_segs[segcnt].ds_addr;
4256 eop->d_length1 = map->dm_segs[segcnt].ds_len;
4257 eop->d_addr2 = 0;
4258 eop->d_length2 = 0;
4259 if (++nextout == ri->ri_last)
4260 nextout = ri->ri_first;
4261 }
4262 TULIP_TXMAP_PRESYNC(sc, map);
4263 M_SETCTX(m, map);
4264 map = NULL;
4265 --sc->tulip_txmaps_free; /* commit to using the dmamap */
4266
4267#else /* !TULIP_BUS_DMA */
4268
4269 do {
4270 int len = m0->m_len;
4271 caddr_t addr = mtod(m0, caddr_t);
4272 unsigned clsize = PAGE_SIZE - (((uintptr_t) addr) & (PAGE_SIZE-1));
4273
4274 while (len > 0) {
4275 unsigned slen = min(len, clsize);
4276#ifdef BIG_PACKET
4277 int partial = 0;
4278 if (slen >= 2048)
4279 slen = 2040, partial = 1;
4280#endif
4281 segcnt++;
4282 if (segcnt > TULIP_MAX_TXSEG) {
4283 /*
4284 * The packet exceeds the number of transmit buffer
4285 * entries that we can use for one packet, so we have
4286 * recopy it into one mbuf and then try again.
4287 */
4288 m = tulip_mbuf_compress(m);
4289 if (m == NULL)
4290 goto finish;
4291 goto again;
4292 }
4293 if (segcnt & 1) {
4294 if (--free == 0) {
4295 /*
4296 * See if there's any unclaimed space in the
4297 * transmit ring.
4298 */
4299 if ((free += tulip_tx_intr(sc)) == 0) {
4300 /*
4301 * There's no more room but since nothing
4302 * has been committed at this point, just
4303 * show output is active, put back the
4304 * mbuf and return.
4305 */
4306 sc->tulip_flags |= TULIP_WANTTXSTART;
4307#if defined(TULIP_DEBUG)
4308 sc->tulip_dbg.dbg_txput_finishes[1]++;
4309#endif
4310 goto finish;
4311 }
4312 }
4313 eop = nextout;
4314 if (++nextout == ri->ri_last)
4315 nextout = ri->ri_first;
4316 eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4317 eop->d_status = d_status;
4318 eop->d_addr1 = TULIP_KVATOPHYS(sc, addr);
4319 eop->d_length1 = slen;
4320 } else {
4321 /*
4322 * Fill in second half of descriptor
4323 */
4324 eop->d_addr2 = TULIP_KVATOPHYS(sc, addr);
4325 eop->d_length2 = slen;
4326 }
4327 d_status = TULIP_DSTS_OWNER;
4328 len -= slen;
4329 addr += slen;
4330#ifdef BIG_PACKET
4331 if (partial)
4332 continue;
4333#endif
4334 clsize = PAGE_SIZE;
4335 }
4336 } while ((m0 = m0->m_next) != NULL);
4337#endif /* TULIP_BUS_DMA */
4338
4339 /*
4340 * bounce a copy to the bpf listener, if any.
4341 */
4342 if (sc->tulip_if.if_bpf != NULL)
4343 bpf_mtap(&sc->tulip_if, m);
4344
4345 /*
4346 * The descriptors have been filled in. Now get ready
4347 * to transmit.
4348 */
4349 IF_ENQUEUE(&sc->tulip_txq, m);
4350 m = NULL;
4351
4352 /*
4353 * Make sure the next descriptor after this packet is owned
4354 * by us since it may have been set up above if we ran out
4355 * of room in the ring.
4356 */
4357 nextout->d_status = 0;
4358 TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(u_int32_t));
4359
4360#if !defined(TULIP_BUS_DMA) || defined(TULIP_BUS_DMA_NOTX)
4361 /*
4362 * If we only used the first segment of the last descriptor,
4363 * make sure the second segment will not be used.
4364 */
4365 if (segcnt & 1) {
4366 eop->d_addr2 = 0;
4367 eop->d_length2 = 0;
4368 }
4369#endif /* TULIP_BUS_DMA */
4370
4371 /*
4372 * Mark the last and first segments, indicate we want a transmit
4373 * complete interrupt, and tell it to transmit!
4374 */
4375 eop->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
4376
4377 /*
4378 * Note that ri->ri_nextout is still the start of the packet
4379 * and until we set the OWNER bit, we can still back out of
4380 * everything we have done.
4381 */
4382 ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
4383#if defined(TULIP_BUS_MAP) && !defined(TULIP_BUS_DMA_NOTX)
4384 if (eop < ri->ri_nextout) {
4385 TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout,
4386 (caddr_t) ri->ri_last - (caddr_t) ri->ri_nextout);
4387 TULIP_TXDESC_PRESYNC(sc, ri->ri_first,
4388 (caddr_t) (eop + 1) - (caddr_t) ri->ri_first);
4389 } else {
4390 TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout,
4391 (caddr_t) (eop + 1) - (caddr_t) ri->ri_nextout);
4392 }
4393#endif
4394 ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
4395 TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout, sizeof(u_int32_t));
4396
4397 /*
4398 * This advances the ring for us.
4399 */
4400 ri->ri_nextout = nextout;
4401 ri->ri_free = free;
4402
4403 TULIP_PERFEND(txput);
4404
4405 if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
4406 TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4407 sc->tulip_if.if_flags |= IFF_OACTIVE;
4408 sc->tulip_if.if_start = tulip_ifstart;
4409 TULIP_PERFEND(txput);
4410 return NULL;
4411 }
4412
4413 /*
4414 * switch back to the single queueing ifstart.
4415 */
4416 sc->tulip_flags &= ~TULIP_WANTTXSTART;
4417 if (sc->tulip_txtimer == 0)
4418 sc->tulip_txtimer = TULIP_TXTIMER;
4419#if defined(TULIP_DEBUG)
4420 sc->tulip_dbg.dbg_txput_finishes[5]++;
4421#endif
4422
4423 /*
4424 * If we want a txstart, there must be not enough space in the
4425 * transmit ring. So we want to enable transmit done interrupts
4426 * so we can immediately reclaim some space. When the transmit
4427 * interrupt is posted, the interrupt handler will call tx_intr
4428 * to reclaim space and then txstart (since WANTTXSTART is set).
4429 * txstart will move the packet into the transmit ring and clear
4430 * WANTTXSTART thereby causing TXINTR to be cleared.
4431 */
4432 finish:
4433#if defined(TULIP_DEBUG)
4434 sc->tulip_dbg.dbg_txput_finishes[6]++;
4435#endif
4436 if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_DOINGSETUP)) {
4437 sc->tulip_if.if_flags |= IFF_OACTIVE;
4438 sc->tulip_if.if_start = tulip_ifstart;
4439 if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
4440 sc->tulip_intrmask |= TULIP_STS_TXINTR;
4441 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4442 }
4443 } else if ((sc->tulip_flags & TULIP_PROMISC) == 0) {
4444 if (sc->tulip_intrmask & TULIP_STS_TXINTR) {
4445 sc->tulip_intrmask &= ~TULIP_STS_TXINTR;
4446 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4447 }
4448 }
4449 TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4450 TULIP_PERFEND(txput);
4451 return m;
4452}
4453
4454static void
4455tulip_txput_setup(
4456 tulip_softc_t * const sc)
4457{
4458 tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
4459 tulip_desc_t *nextout;
4460
4461 /*
4462 * We will transmit, at most, one setup packet per call to ifstart.
4463 */
4464
4465#if defined(TULIP_DEBUG)
4466 if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
4467 printf("%s%d: txput_setup: tx not running\n",
4468 sc->tulip_name, sc->tulip_unit);
4469 sc->tulip_flags |= TULIP_WANTTXSTART;
4470 sc->tulip_if.if_start = tulip_ifstart;
4471 return;
4472 }
4473#endif
4474 /*
4475 * Try to reclaim some free descriptors..
4476 */
4477 if (ri->ri_free < 2)
4478 tulip_tx_intr(sc);
4479 if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
4480 sc->tulip_flags |= TULIP_WANTTXSTART;
4481 sc->tulip_if.if_start = tulip_ifstart;
4482 return;
4483 }
4484 bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
4485 sizeof(sc->tulip_setupbuf));
4486 /*
4487 * Clear WANTSETUP and set DOINGSETUP. Set know that WANTSETUP is
4488 * set and DOINGSETUP is clear doing an XOR of the two will DTRT.
4489 */
4490 sc->tulip_flags ^= TULIP_WANTSETUP|TULIP_DOINGSETUP;
4491 ri->ri_free--;
4492 nextout = ri->ri_nextout;
4493 nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4494 nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
4495 |TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
4496 if (sc->tulip_flags & TULIP_WANTHASHPERFECT)
4497 nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
4498 else if (sc->tulip_flags & TULIP_WANTHASHONLY)
4499 nextout->d_flag |= TULIP_DFLAG_TxHASHFILT|TULIP_DFLAG_TxINVRSFILT;
4500
4501 nextout->d_length2 = 0;
4502 nextout->d_addr2 = 0;
4503#if defined(TULIP_BUS_DMA) && !defined(TULIP_BUS_DMA_NOTX)
4504 nextout->d_length1 = sc->tulip_setupmap->dm_segs[0].ds_len;
4505 nextout->d_addr1 = sc->tulip_setupmap->dm_segs[0].ds_addr;
4506 if (sc->tulip_setupmap->dm_nsegs == 2) {
4507 nextout->d_length2 = sc->tulip_setupmap->dm_segs[1].ds_len;
4508 nextout->d_addr2 = sc->tulip_setupmap->dm_segs[1].ds_addr;
4509 }
4510 TULIP_TXMAP_PRESYNC(sc, sc->tulip_setupmap);
4511 TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(*nextout));
4512#else
4513 nextout->d_length1 = sizeof(sc->tulip_setupbuf);
4514 nextout->d_addr1 = TULIP_KVATOPHYS(sc, sc->tulip_setupbuf);
4515#endif
4516
4517 /*
4518 * Advance the ring for the next transmit packet.
4519 */
4520 if (++ri->ri_nextout == ri->ri_last)
4521 ri->ri_nextout = ri->ri_first;
4522
4523 /*
4524 * Make sure the next descriptor is owned by us since it
4525 * may have been set up above if we ran out of room in the
4526 * ring.
4527 */
4528 ri->ri_nextout->d_status = 0;
4529 TULIP_TXDESC_PRESYNC(sc, ri->ri_nextout, sizeof(u_int32_t));
4530 nextout->d_status = TULIP_DSTS_OWNER;
4531 /*
4532 * Flush the ownwership of the current descriptor
4533 */
4534 TULIP_TXDESC_PRESYNC(sc, nextout, sizeof(u_int32_t));
4535 TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4536 if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
4537 sc->tulip_intrmask |= TULIP_STS_TXINTR;
4538 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4539 }
4540}
4541
4542
4543/*
4544 * This routine is entered at splnet() (splsoftnet() on NetBSD)
4545 * and thereby imposes no problems when TULIP_USE_SOFTINTR is
4546 * defined or not.
4547 */
4548static int
4549tulip_ifioctl(
4550 struct ifnet * ifp,
4551 u_long cmd,
4552 caddr_t data)
4553{
4554 TULIP_PERFSTART(ifioctl)
4555 tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
4556 struct ifaddr *ifa = (struct ifaddr *)data;
4557 struct ifreq *ifr = (struct ifreq *) data;
4558 int s;
4559 int error = 0;
4560
4561#if defined(TULIP_USE_SOFTINTR)
4562 s = splnet();
4563#else
4564 s = splimp();
4565#endif
4566 switch (cmd) {
4567 case SIOCSIFADDR: {
4568 ifp->if_flags |= IFF_UP;
4569 switch(ifa->ifa_addr->sa_family) {
4570#ifdef INET
4571 case AF_INET: {
4572 tulip_init(sc);
4573 arp_ifinit(&(sc)->tulip_ac, ifa);
4574 break;
4575 }
4576#endif /* INET */
4577
4578#ifdef IPX
4579 case AF_IPX: {
4580 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
4581 if (ipx_nullhost(*ina)) {
4582 ina->x_host = *(union ipx_host *)(sc->tulip_enaddr);
4583 } else {
4584 ifp->if_flags &= ~IFF_RUNNING;
4585 bcopy((caddr_t)ina->x_host.c_host,
4586 (caddr_t)sc->tulip_enaddr,
4587 sizeof(sc->tulip_enaddr));
4588 }
4589 tulip_init(sc);
4590 break;
4591 }
4592#endif /* IPX */
4593
4594#ifdef NS
4595 /*
4596 * This magic copied from if_is.c; I don't use XNS,
4597 * so I have no way of telling if this actually
4598 * works or not.
4599 */
4600 case AF_NS: {
4601 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
4602 if (ns_nullhost(*ina)) {
4603 ina->x_host = *(union ns_host *)(sc->tulip_enaddr);
4604 } else {
4605 ifp->if_flags &= ~IFF_RUNNING;
4606 bcopy((caddr_t)ina->x_host.c_host,
4607 (caddr_t)sc->tulip_enaddr,
4608 sizeof(sc->tulip_enaddr));
4609 }
4610 tulip_init(sc);
4611 break;
4612 }
4613#endif /* NS */
4614
4615 default: {
4616 tulip_init(sc);
4617 break;
4618 }
4619 }
4620 break;
4621 }
4622 case SIOCGIFADDR: {
4623 bcopy((caddr_t) sc->tulip_enaddr,
4624 (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
4625 6);
4626 break;
4627 }
4628
4629 case SIOCSIFFLAGS: {
4630 tulip_addr_filter(sc); /* reinit multicast filter */
4631 tulip_init(sc);
4632 break;
4633 }
4634
4635 case SIOCSIFMEDIA:
4636 case SIOCGIFMEDIA: {
4637 error = ifmedia_ioctl(ifp, ifr, &sc->tulip_ifmedia, cmd);
4638 break;
4639 }
4640
4641 case SIOCADDMULTI:
4642 case SIOCDELMULTI: {
4643 /*
4644 * Update multicast listeners
4645 */
4646 tulip_addr_filter(sc); /* reset multicast filtering */
4647 tulip_init(sc);
4648 error = 0;
4649 break;
4650 }
4651
4652 case SIOCSIFMTU:
4653 /*
4654 * Set the interface MTU.
4655 */
4656 if (ifr->ifr_mtu > ETHERMTU
4657#ifdef BIG_PACKET
4658 && sc->tulip_chipid != TULIP_21140
4659 && sc->tulip_chipid != TULIP_21140A
4660 && sc->tulip_chipid != TULIP_21041
4661#endif
4662 ) {
4663 error = EINVAL;
4664 break;
4665 }
4666 ifp->if_mtu = ifr->ifr_mtu;
4667#ifdef BIG_PACKET
4668 tulip_reset(sc);
4669 tulip_init(sc);
4670#endif
4671 break;
4672
4673#ifdef SIOCGADDRROM
4674 case SIOCGADDRROM: {
4675 error = copyout(sc->tulip_rombuf, ifr->ifr_data, sizeof(sc->tulip_rombuf));
4676 break;
4677 }
4678#endif
4679#ifdef SIOCGCHIPID
4680 case SIOCGCHIPID: {
4681 ifr->ifr_metric = (int) sc->tulip_chipid;
4682 break;
4683 }
4684#endif
4685 default: {
4686 error = EINVAL;
4687 break;
4688 }
4689 }
4690
4691 splx(s);
4692 TULIP_PERFEND(ifioctl);
4693 return error;
4694}
4695
4696/*
4697 * These routines gets called at device spl (from ether_output). This might
4698 * pose a problem for TULIP_USE_SOFTINTR if ether_output is called at
4699 * device spl from another driver.
4700 */
4701
4702static void
4703tulip_ifstart(
4704 struct ifnet * const ifp)
4705{
4706 TULIP_PERFSTART(ifstart)
4707 tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
4708
4709 if (sc->tulip_if.if_flags & IFF_RUNNING) {
4710
4711 if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
4712 tulip_txput_setup(sc);
4713
4714 while (sc->tulip_if.if_snd.ifq_head != NULL) {
4715 struct mbuf *m;
4716 IF_DEQUEUE(&sc->tulip_if.if_snd, m);
4717 if ((m = tulip_txput(sc, m)) != NULL) {
4718 IF_PREPEND(&sc->tulip_if.if_snd, m);
4719 break;
4720 }
4721 }
4722 if (sc->tulip_if.if_snd.ifq_head == NULL)
4723 sc->tulip_if.if_start = tulip_ifstart_one;
4724 }
4725
4726 TULIP_PERFEND(ifstart);
4727}
4728
4729static void
4730tulip_ifstart_one(
4731 struct ifnet * const ifp)
4732{
4733 TULIP_PERFSTART(ifstart_one)
4734 tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
4735
4736 if ((sc->tulip_if.if_flags & IFF_RUNNING)
4737 && sc->tulip_if.if_snd.ifq_head != NULL) {
4738 struct mbuf *m;
4739 IF_DEQUEUE(&sc->tulip_if.if_snd, m);
4740 if ((m = tulip_txput(sc, m)) != NULL)
4741 IF_PREPEND(&sc->tulip_if.if_snd, m);
4742 }
4743 TULIP_PERFEND(ifstart_one);
4744}
4745
4746/*
4747 * Even though this routine runs at device spl, it does not break
4748 * our use of splnet (splsoftnet under NetBSD) for the majority
4749 * of this driver (if TULIP_USE_SOFTINTR defined) since
4750 * if_watcbog is called from if_watchdog which is called from
4751 * splsoftclock which is below spl[soft]net.
4752 */
4753static void
4754tulip_ifwatchdog(
4755 struct ifnet *ifp)
4756{
4757 TULIP_PERFSTART(ifwatchdog)
4758 tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
4759
4760#if defined(TULIP_DEBUG)
4761 u_int32_t rxintrs = sc->tulip_dbg.dbg_rxintrs - sc->tulip_dbg.dbg_last_rxintrs;
4762 if (rxintrs > sc->tulip_dbg.dbg_high_rxintrs_hz)
4763 sc->tulip_dbg.dbg_high_rxintrs_hz = rxintrs;
4764 sc->tulip_dbg.dbg_last_rxintrs = sc->tulip_dbg.dbg_rxintrs;
4765#endif /* TULIP_DEBUG */
4766
4767 sc->tulip_if.if_timer = 1;
4768 /*
4769 * These should be rare so do a bulk test up front so we can just skip
4770 * them if needed.
4771 */
4772 if (sc->tulip_flags & (TULIP_SYSTEMERROR|TULIP_RXBUFSLOW|TULIP_NOMESSAGES)) {
4773 /*
4774 * If the number of receive buffer is low, try to refill
4775 */
4776 if (sc->tulip_flags & TULIP_RXBUFSLOW)
4777 tulip_rx_intr(sc);
4778
4779 if (sc->tulip_flags & TULIP_SYSTEMERROR) {
4780 printf("%s%d: %d system errors: last was %s\n",
4781 sc->tulip_name, sc->tulip_unit, sc->tulip_system_errors,
4782 tulip_system_errors[sc->tulip_last_system_error]);
4783 }
4784 if (sc->tulip_statusbits) {
4785 tulip_print_abnormal_interrupt(sc, sc->tulip_statusbits);
4786 sc->tulip_statusbits = 0;
4787 }
4788
4789 sc->tulip_flags &= ~(TULIP_NOMESSAGES|TULIP_SYSTEMERROR);
4790 }
4791
4792 if (sc->tulip_txtimer)
4793 tulip_tx_intr(sc);
4794 if (sc->tulip_txtimer && --sc->tulip_txtimer == 0) {
4795 printf("%s%d: transmission timeout\n", sc->tulip_name, sc->tulip_unit);
4796 if (TULIP_DO_AUTOSENSE(sc)) {
4797 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
4798 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
4799 sc->tulip_flags &= ~(TULIP_WANTRXACT|TULIP_LINKUP);
4800 }
4801 tulip_reset(sc);
4802 tulip_init(sc);
4803 }
4804
4805 TULIP_PERFEND(ifwatchdog);
4806 TULIP_PERFMERGE(sc, perf_intr_cycles);
4807 TULIP_PERFMERGE(sc, perf_ifstart_cycles);
4808 TULIP_PERFMERGE(sc, perf_ifioctl_cycles);
4809 TULIP_PERFMERGE(sc, perf_ifwatchdog_cycles);
4810 TULIP_PERFMERGE(sc, perf_timeout_cycles);
4811 TULIP_PERFMERGE(sc, perf_ifstart_one_cycles);
4812 TULIP_PERFMERGE(sc, perf_txput_cycles);
4813 TULIP_PERFMERGE(sc, perf_txintr_cycles);
4814 TULIP_PERFMERGE(sc, perf_rxintr_cycles);
4815 TULIP_PERFMERGE(sc, perf_rxget_cycles);
4816 TULIP_PERFMERGE(sc, perf_intr);
4817 TULIP_PERFMERGE(sc, perf_ifstart);
4818 TULIP_PERFMERGE(sc, perf_ifioctl);
4819 TULIP_PERFMERGE(sc, perf_ifwatchdog);
4820 TULIP_PERFMERGE(sc, perf_timeout);
4821 TULIP_PERFMERGE(sc, perf_ifstart_one);
4822 TULIP_PERFMERGE(sc, perf_txput);
4823 TULIP_PERFMERGE(sc, perf_txintr);
4824 TULIP_PERFMERGE(sc, perf_rxintr);
4825 TULIP_PERFMERGE(sc, perf_rxget);
4826}
4827
4828/*
4829 * All printf's are real as of now!
4830 */
4831#ifdef printf
4832#undef printf
4833#endif
4834
4835static void
4836tulip_attach(
4837 tulip_softc_t * const sc)
4838{
4839 struct ifnet * const ifp = &sc->tulip_if;
4840
4841 ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
4842 ifp->if_ioctl = tulip_ifioctl;
4843 ifp->if_start = tulip_ifstart;
4844 ifp->if_watchdog = tulip_ifwatchdog;
4845 ifp->if_timer = 1;
4846 ifp->if_output = ether_output;
4847
4848 printf("%s%d: %s%s pass %d.%d%s\n",
4849 sc->tulip_name, sc->tulip_unit,
4850 sc->tulip_boardid,
4851 tulip_chipdescs[sc->tulip_chipid],
4852 (sc->tulip_revinfo & 0xF0) >> 4,
4853 sc->tulip_revinfo & 0x0F,
4854 (sc->tulip_features & (TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM))
4855 == TULIP_HAVE_ISVSROM ? " (invalid EESPROM checksum)" : "");
4856 printf("%s%d: address %6D\n",
4857 sc->tulip_name, sc->tulip_unit, sc->tulip_enaddr, ":");
4858
4859#if defined(__alpha__)
4860 /*
4861 * In case the SRM console told us about a bogus media,
4862 * we need to check to be safe.
4863 */
4864 if (sc->tulip_mediums[sc->tulip_media] == NULL)
4865 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
4866#endif
4867
4868 (*sc->tulip_boardsw->bd_media_probe)(sc);
4869 ifmedia_init(&sc->tulip_ifmedia, 0,
4870 tulip_ifmedia_change,
4871 tulip_ifmedia_status);
4872 sc->tulip_flags &= ~TULIP_DEVICEPROBE;
4873 tulip_ifmedia_add(sc);
4874
4875 tulip_reset(sc);
4876
4877 if_attach(ifp);
4877 ether_ifattach(&(sc)->tulip_if, ETHER_BPF_SUPPORTED);
4878 ifp->if_snd.ifq_maxlen = ifqmaxlen;
4878 ifp->if_snd.ifq_maxlen = ifqmaxlen;
4879 ether_ifattach(&(sc)->tulip_if);
4880
4881 bpfattach(&sc->tulip_if, DLT_EN10MB, sizeof(struct ether_header));
4882}
4883
4884#if defined(TULIP_BUS_DMA)
4885#if !defined(TULIP_BUS_DMA_NOTX) || !defined(TULIP_BUS_DMA_NORX)
4886static int
4887tulip_busdma_allocmem(
4888 tulip_softc_t * const sc,
4889 size_t size,
4890 bus_dmamap_t *map_p,
4891 tulip_desc_t **desc_p)
4892{
4893 bus_dma_segment_t segs[1];
4894 int nsegs, error;
4895 error = bus_dmamem_alloc(sc->tulip_dmatag, size, 1, PAGE_SIZE,
4896 segs, sizeof(segs)/sizeof(segs[0]),
4897 &nsegs, BUS_DMA_NOWAIT);
4898 if (error == 0) {
4899 void *desc;
4900 error = bus_dmamem_map(sc->tulip_dmatag, segs, nsegs, size,
4901 (void *) &desc, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
4902 if (error == 0) {
4903 bus_dmamap_t map;
4904 error = bus_dmamap_create(sc->tulip_dmatag, size, 1, size, 0,
4905 BUS_DMA_NOWAIT, &map);
4906 if (error == 0) {
4907 error = bus_dmamap_load(sc->tulip_dmatag, map, desc,
4908 size, NULL, BUS_DMA_NOWAIT);
4909 if (error)
4910 bus_dmamap_destroy(sc->tulip_dmatag, map);
4911 else
4912 *map_p = map;
4913 }
4914 if (error)
4915 bus_dmamem_unmap(sc->tulip_dmatag, desc, size);
4916 }
4917 if (error)
4918 bus_dmamem_free(sc->tulip_dmatag, segs, nsegs);
4919 else
4920 *desc_p = desc;
4921 }
4922 return error;
4923}
4924#endif
4925
4926static int
4927tulip_busdma_init(
4928 tulip_softc_t * const sc)
4929{
4930 int error = 0;
4931
4932#if !defined(TULIP_BUS_DMA_NOTX)
4933 /*
4934 * Allocate dmamap for setup descriptor
4935 */
4936 error = bus_dmamap_create(sc->tulip_dmatag, sizeof(sc->tulip_setupbuf), 2,
4937 sizeof(sc->tulip_setupbuf), 0, BUS_DMA_NOWAIT,
4938 &sc->tulip_setupmap);
4939 if (error == 0) {
4940 error = bus_dmamap_load(sc->tulip_dmatag, sc->tulip_setupmap,
4941 sc->tulip_setupbuf, sizeof(sc->tulip_setupbuf),
4942 NULL, BUS_DMA_NOWAIT);
4943 if (error)
4944 bus_dmamap_destroy(sc->tulip_dmatag, sc->tulip_setupmap);
4945 }
4946 /*
4947 * Allocate space and dmamap for transmit ring
4948 */
4949 if (error == 0) {
4950 error = tulip_busdma_allocmem(sc, sizeof(tulip_desc_t) * TULIP_TXDESCS,
4951 &sc->tulip_txdescmap,
4952 &sc->tulip_txdescs);
4953 }
4954
4955 /*
4956 * Allocate dmamaps for each transmit descriptors
4957 */
4958 if (error == 0) {
4959 while (error == 0 && sc->tulip_txmaps_free < TULIP_TXDESCS) {
4960 bus_dmamap_t map;
4961 if ((error = TULIP_TXMAP_CREATE(sc, &map)) == 0)
4962 sc->tulip_txmaps[sc->tulip_txmaps_free++] = map;
4963 }
4964 if (error) {
4965 while (sc->tulip_txmaps_free > 0)
4966 bus_dmamap_destroy(sc->tulip_dmatag,
4967 sc->tulip_txmaps[--sc->tulip_txmaps_free]);
4968 }
4969 }
4970#else
4971 if (error == 0) {
4972 sc->tulip_txdescs = (tulip_desc_t *) malloc(TULIP_TXDESCS * sizeof(tulip_desc_t), M_DEVBUF, M_NOWAIT);
4973 if (sc->tulip_txdescs == NULL)
4974 error = ENOMEM;
4975 }
4976#endif
4977#if !defined(TULIP_BUS_DMA_NORX)
4978 /*
4979 * Allocate space and dmamap for receive ring
4980 */
4981 if (error == 0) {
4982 error = tulip_busdma_allocmem(sc, sizeof(tulip_desc_t) * TULIP_RXDESCS,
4983 &sc->tulip_rxdescmap,
4984 &sc->tulip_rxdescs);
4985 }
4986
4987 /*
4988 * Allocate dmamaps for each receive descriptors
4989 */
4990 if (error == 0) {
4991 while (error == 0 && sc->tulip_rxmaps_free < TULIP_RXDESCS) {
4992 bus_dmamap_t map;
4993 if ((error = TULIP_RXMAP_CREATE(sc, &map)) == 0)
4994 sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
4995 }
4996 if (error) {
4997 while (sc->tulip_rxmaps_free > 0)
4998 bus_dmamap_destroy(sc->tulip_dmatag,
4999 sc->tulip_rxmaps[--sc->tulip_rxmaps_free]);
5000 }
5001 }
5002#else
5003 if (error == 0) {
5004 sc->tulip_rxdescs = (tulip_desc_t *) malloc(TULIP_RXDESCS * sizeof(tulip_desc_t), M_DEVBUF, M_NOWAIT);
5005 if (sc->tulip_rxdescs == NULL)
5006 error = ENOMEM;
5007 }
5008#endif
5009 return error;
5010}
5011#endif /* TULIP_BUS_DMA */
5012
5013static void
5014tulip_initcsrs(
5015 tulip_softc_t * const sc,
5016 tulip_csrptr_t csr_base,
5017 size_t csr_size)
5018{
5019 sc->tulip_csrs.csr_busmode = csr_base + 0 * csr_size;
5020 sc->tulip_csrs.csr_txpoll = csr_base + 1 * csr_size;
5021 sc->tulip_csrs.csr_rxpoll = csr_base + 2 * csr_size;
5022 sc->tulip_csrs.csr_rxlist = csr_base + 3 * csr_size;
5023 sc->tulip_csrs.csr_txlist = csr_base + 4 * csr_size;
5024 sc->tulip_csrs.csr_status = csr_base + 5 * csr_size;
5025 sc->tulip_csrs.csr_command = csr_base + 6 * csr_size;
5026 sc->tulip_csrs.csr_intr = csr_base + 7 * csr_size;
5027 sc->tulip_csrs.csr_missed_frames = csr_base + 8 * csr_size;
5028 sc->tulip_csrs.csr_9 = csr_base + 9 * csr_size;
5029 sc->tulip_csrs.csr_10 = csr_base + 10 * csr_size;
5030 sc->tulip_csrs.csr_11 = csr_base + 11 * csr_size;
5031 sc->tulip_csrs.csr_12 = csr_base + 12 * csr_size;
5032 sc->tulip_csrs.csr_13 = csr_base + 13 * csr_size;
5033 sc->tulip_csrs.csr_14 = csr_base + 14 * csr_size;
5034 sc->tulip_csrs.csr_15 = csr_base + 15 * csr_size;
5035}
5036
5037static void
5038tulip_initring(
5039 tulip_softc_t * const sc,
5040 tulip_ringinfo_t * const ri,
5041 tulip_desc_t *descs,
5042 int ndescs)
5043{
5044 ri->ri_max = ndescs;
5045 ri->ri_first = descs;
5046 ri->ri_last = ri->ri_first + ri->ri_max;
5047 bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
5048 ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
5049}
5050
5051/*
5052 * This is the PCI configuration support.
5053 */
5054
5055#define PCI_CBIO 0x10 /* Configuration Base IO Address */
5056#define PCI_CBMA 0x14 /* Configuration Base Memory Address */
5057#define PCI_CFDA 0x40 /* Configuration Driver Area */
5058
5059static int
5060tulip_pci_probe(device_t dev)
5061{
5062 const char *name = NULL;
5063
5064 if (pci_get_vendor(dev) != DEC_VENDORID)
5065 return ENXIO;
5066
5067 /*
5068 * Some LanMedia WAN cards use the Tulip chip, but they have
5069 * their own driver, and we should not recognize them
5070 */
5071 if (pci_get_subvendor(dev) == 0x1376)
5072 return ENXIO;
5073
5074 switch (pci_get_device(dev)) {
5075 case CHIPID_21040:
5076 name = "Digital 21040 Ethernet";
5077 break;
5078 case CHIPID_21041:
5079 name = "Digital 21041 Ethernet";
5080 break;
5081 case CHIPID_21140:
5082 if (pci_get_revid(dev) >= 0x20)
5083 name = "Digital 21140A Fast Ethernet";
5084 else
5085 name = "Digital 21140 Fast Ethernet";
5086 break;
5087 case CHIPID_21142:
5088 if (pci_get_revid(dev) >= 0x20)
5089 name = "Digital 21143 Fast Ethernet";
5090 else
5091 name = "Digital 21142 Fast Ethernet";
5092 break;
5093 }
5094 if (name) {
5095 device_set_desc(dev, name);
5096 return -200;
5097 }
5098 return ENXIO;
5099}
5100
5101static int
5102tulip_shutdown(device_t dev)
5103{
5104 tulip_softc_t * const sc = device_get_softc(dev);
5105 TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
5106 DELAY(10); /* Wait 10 microseconds (actually 50 PCI cycles but at
5107 33MHz that comes to two microseconds but wait a
5108 bit longer anyways) */
5109 return 0;
5110}
5111
5112static int
5113tulip_pci_attach(device_t dev)
5114{
5115 tulip_softc_t *sc;
5116#if defined(__alpha__)
5117 tulip_media_t media = TULIP_MEDIA_UNKNOWN;
5118#endif
5119 int retval, idx;
5120 u_int32_t revinfo, cfdainfo, cfcsinfo;
5121 unsigned csroffset = TULIP_PCI_CSROFFSET;
5122 unsigned csrsize = TULIP_PCI_CSRSIZE;
5123 tulip_csrptr_t csr_base;
5124 tulip_chipid_t chipid = TULIP_CHIPID_UNKNOWN;
5125 struct resource *res;
5126 int rid, unit;
5127
5128 unit = device_get_unit(dev);
5129
5130 if (unit >= TULIP_MAX_DEVICES) {
5131 printf("de%d", unit);
5132 printf(": not configured; limit of %d reached or exceeded\n",
5133 TULIP_MAX_DEVICES);
5134 return ENXIO;
5135 }
5136
5137 revinfo = pci_get_revid(dev);
5138 cfdainfo = pci_read_config(dev, PCI_CFDA, 4);
5139 cfcsinfo = pci_read_config(dev, PCIR_COMMAND, 4);
5140
5141 /* turn busmaster on in case BIOS doesn't set it */
5142 if(!(cfcsinfo & PCIM_CMD_BUSMASTEREN)) {
5143 cfcsinfo |= PCIM_CMD_BUSMASTEREN;
5144 pci_write_config(dev, PCIR_COMMAND, cfcsinfo, 4);
5145 }
5146
5147 if (pci_get_vendor(dev) == DEC_VENDORID) {
5148 if (pci_get_device(dev) == CHIPID_21040)
5149 chipid = TULIP_21040;
5150 else if (pci_get_device(dev) == CHIPID_21041)
5151 chipid = TULIP_21041;
5152 else if (pci_get_device(dev) == CHIPID_21140)
5153 chipid = (revinfo >= 0x20) ? TULIP_21140A : TULIP_21140;
5154 else if (pci_get_device(dev) == CHIPID_21142)
5155 chipid = (revinfo >= 0x20) ? TULIP_21143 : TULIP_21142;
5156 }
5157 if (chipid == TULIP_CHIPID_UNKNOWN)
5158 return ENXIO;
5159
5160 if (chipid == TULIP_21040 && revinfo < 0x20) {
5161 printf("de%d", unit);
5162 printf(": not configured; 21040 pass 2.0 required (%d.%d found)\n",
5163 revinfo >> 4, revinfo & 0x0f);
5164 return ENXIO;
5165 } else if (chipid == TULIP_21140 && revinfo < 0x11) {
5166 printf("de%d: not configured; 21140 pass 1.1 required (%d.%d found)\n",
5167 unit, revinfo >> 4, revinfo & 0x0f);
5168 return ENXIO;
5169 }
5170
5171 sc = device_get_softc(dev);
5172 sc->tulip_pci_busno = pci_get_bus(dev);
5173 sc->tulip_pci_devno = pci_get_slot(dev);
5174 sc->tulip_chipid = chipid;
5175 sc->tulip_flags |= TULIP_DEVICEPROBE;
5176 if (chipid == TULIP_21140 || chipid == TULIP_21140A)
5177 sc->tulip_features |= TULIP_HAVE_GPR|TULIP_HAVE_STOREFWD;
5178 if (chipid == TULIP_21140A && revinfo <= 0x22)
5179 sc->tulip_features |= TULIP_HAVE_RXBADOVRFLW;
5180 if (chipid == TULIP_21140)
5181 sc->tulip_features |= TULIP_HAVE_BROKEN_HASH;
5182 if (chipid != TULIP_21040 && chipid != TULIP_21140)
5183 sc->tulip_features |= TULIP_HAVE_POWERMGMT;
5184 if (chipid == TULIP_21041 || chipid == TULIP_21142 || chipid == TULIP_21143) {
5185 sc->tulip_features |= TULIP_HAVE_DUALSENSE;
5186 if (chipid != TULIP_21041 || revinfo >= 0x20)
5187 sc->tulip_features |= TULIP_HAVE_SIANWAY;
5188 if (chipid != TULIP_21041)
5189 sc->tulip_features |= TULIP_HAVE_SIAGP|TULIP_HAVE_RXBADOVRFLW|TULIP_HAVE_STOREFWD;
5190 if (chipid != TULIP_21041 && revinfo >= 0x20)
5191 sc->tulip_features |= TULIP_HAVE_SIA100;
5192 }
5193
5194 if (sc->tulip_features & TULIP_HAVE_POWERMGMT
5195 && (cfdainfo & (TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE))) {
5196 cfdainfo &= ~(TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE);
5197 pci_write_config(dev, PCI_CFDA, cfdainfo, 4);
5198 DELAY(11*1000);
5199 }
5200#if defined(__alpha__)
5201 /*
5202 * The Alpha SRM console encodes a console set media in the driver
5203 * part of the CFDA register. Note that the Multia presents a
5204 * problem in that its BNC mode is really EXTSIA. So in that case
5205 * force a probe.
5206 */
5207 switch ((cfdainfo >> 8) & 0xff) {
5208 case 1: media = chipid > TULIP_21040 ? TULIP_MEDIA_AUI : TULIP_MEDIA_AUIBNC; break;
5209 case 2: media = chipid > TULIP_21040 ? TULIP_MEDIA_BNC : TULIP_MEDIA_UNKNOWN; break;
5210 case 3: media = TULIP_MEDIA_10BASET; break;
5211 case 4: media = TULIP_MEDIA_10BASET_FD; break;
5212 case 5: media = TULIP_MEDIA_100BASETX; break;
5213 case 6: media = TULIP_MEDIA_100BASETX_FD; break;
5214 default: media = TULIP_MEDIA_UNKNOWN; break;
5215 }
5216#endif
5217
5218 sc->tulip_unit = unit;
5219 sc->tulip_name = "de";
5220 sc->tulip_revinfo = revinfo;
5221 sc->tulip_if.if_softc = sc;
5222#if defined(TULIP_IOMAPPED)
5223 rid = PCI_CBIO;
5224 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
5225 0, ~0, 1, RF_ACTIVE);
5226#else
5227 rid = PCI_CBMA;
5228 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
5229 0, ~0, 1, RF_ACTIVE);
5230#endif
5231 if (!res)
5232 return ENXIO;
5233 sc->tulip_csrs_bst = rman_get_bustag(res);
5234 sc->tulip_csrs_bsh = rman_get_bushandle(res);
5235 csr_base = 0;
5236
5237 tulips[unit] = sc;
5238
5239 tulip_initcsrs(sc, csr_base + csroffset, csrsize);
5240
5241#if defined(TULIP_BUS_DMA)
5242 if ((retval = tulip_busdma_init(sc)) != 0) {
5243 printf("error initing bus_dma: %d\n", retval);
5244 return ENXIO;
5245 }
5246#else
5247 sc->tulip_rxdescs = (tulip_desc_t *) malloc(sizeof(tulip_desc_t) * TULIP_RXDESCS, M_DEVBUF, M_NOWAIT);
5248 sc->tulip_txdescs = (tulip_desc_t *) malloc(sizeof(tulip_desc_t) * TULIP_TXDESCS, M_DEVBUF, M_NOWAIT);
5249 if (sc->tulip_rxdescs == NULL || sc->tulip_txdescs == NULL) {
5250 printf("malloc failed\n");
5251 if (sc->tulip_rxdescs)
5252 free((caddr_t) sc->tulip_rxdescs, M_DEVBUF);
5253 if (sc->tulip_txdescs)
5254 free((caddr_t) sc->tulip_txdescs, M_DEVBUF);
5255 return ENXIO;
5256 }
5257#endif
5258
5259 tulip_initring(sc, &sc->tulip_rxinfo, sc->tulip_rxdescs, TULIP_RXDESCS);
5260 tulip_initring(sc, &sc->tulip_txinfo, sc->tulip_txdescs, TULIP_TXDESCS);
5261
5262 /*
5263 * Make sure there won't be any interrupts or such...
5264 */
5265 TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
5266 DELAY(100); /* Wait 10 microseconds (actually 50 PCI cycles but at
5267 33MHz that comes to two microseconds but wait a
5268 bit longer anyways) */
5269
5270 if ((retval = tulip_read_macaddr(sc)) < 0) {
5271 printf("%s%d", sc->tulip_name, sc->tulip_unit);
5272 printf(": can't read ENET ROM (why=%d) (", retval);
5273 for (idx = 0; idx < 32; idx++)
5274 printf("%02x", sc->tulip_rombuf[idx]);
5275 printf("\n");
5276 printf("%s%d: %s%s pass %d.%d\n",
5277 sc->tulip_name, sc->tulip_unit,
5278 sc->tulip_boardid, tulip_chipdescs[sc->tulip_chipid],
5279 (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F);
5280 printf("%s%d: address unknown\n", sc->tulip_name, sc->tulip_unit);
5281 } else {
5282 int s;
5283 void (*intr_rtn)(void *) = tulip_intr_normal;
5284
5285 if (sc->tulip_features & TULIP_HAVE_SHAREDINTR)
5286 intr_rtn = tulip_intr_shared;
5287
5288 if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
5289 void *ih;
5290
5291 rid = 0;
5292 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
5293 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
5294 if (res == 0 || bus_setup_intr(dev, res, INTR_TYPE_NET,
5295 intr_rtn, sc, &ih)) {
5296 printf("%s%d: couldn't map interrupt\n",
5297 sc->tulip_name, sc->tulip_unit);
5298 free((caddr_t) sc->tulip_rxdescs, M_DEVBUF);
5299 free((caddr_t) sc->tulip_txdescs, M_DEVBUF);
5300 return ENXIO;
5301 }
5302 }
5303#if defined(TULIP_USE_SOFTINTR)
5304 if (sc->tulip_unit > tulip_softintr_max_unit)
5305 tulip_softintr_max_unit = sc->tulip_unit;
5306#endif
5307
5308 s = splimp();
5309#if defined(__alpha__)
5310 sc->tulip_media = media;
5311#endif
5312 tulip_attach(sc);
5313#if defined(__alpha__)
5314 if (sc->tulip_media != TULIP_MEDIA_UNKNOWN)
5315 tulip_linkup(sc, media);
5316#endif
5317 splx(s);
5318 }
5319 return 0;
5320}
5321
5322static device_method_t tulip_pci_methods[] = {
5323 /* Device interface */
5324 DEVMETHOD(device_probe, tulip_pci_probe),
5325 DEVMETHOD(device_attach, tulip_pci_attach),
5326 DEVMETHOD(device_shutdown, tulip_shutdown),
5327 { 0, 0 }
5328};
5329static driver_t tulip_pci_driver = {
5330 "de",
5331 tulip_pci_methods,
5332 sizeof(tulip_softc_t),
5333};
5334static devclass_t tulip_devclass;
5335DRIVER_MODULE(if_de, pci, tulip_pci_driver, tulip_devclass, 0, 0);
4879}
4880
4881#if defined(TULIP_BUS_DMA)
4882#if !defined(TULIP_BUS_DMA_NOTX) || !defined(TULIP_BUS_DMA_NORX)
4883static int
4884tulip_busdma_allocmem(
4885 tulip_softc_t * const sc,
4886 size_t size,
4887 bus_dmamap_t *map_p,
4888 tulip_desc_t **desc_p)
4889{
4890 bus_dma_segment_t segs[1];
4891 int nsegs, error;
4892 error = bus_dmamem_alloc(sc->tulip_dmatag, size, 1, PAGE_SIZE,
4893 segs, sizeof(segs)/sizeof(segs[0]),
4894 &nsegs, BUS_DMA_NOWAIT);
4895 if (error == 0) {
4896 void *desc;
4897 error = bus_dmamem_map(sc->tulip_dmatag, segs, nsegs, size,
4898 (void *) &desc, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
4899 if (error == 0) {
4900 bus_dmamap_t map;
4901 error = bus_dmamap_create(sc->tulip_dmatag, size, 1, size, 0,
4902 BUS_DMA_NOWAIT, &map);
4903 if (error == 0) {
4904 error = bus_dmamap_load(sc->tulip_dmatag, map, desc,
4905 size, NULL, BUS_DMA_NOWAIT);
4906 if (error)
4907 bus_dmamap_destroy(sc->tulip_dmatag, map);
4908 else
4909 *map_p = map;
4910 }
4911 if (error)
4912 bus_dmamem_unmap(sc->tulip_dmatag, desc, size);
4913 }
4914 if (error)
4915 bus_dmamem_free(sc->tulip_dmatag, segs, nsegs);
4916 else
4917 *desc_p = desc;
4918 }
4919 return error;
4920}
4921#endif
4922
4923static int
4924tulip_busdma_init(
4925 tulip_softc_t * const sc)
4926{
4927 int error = 0;
4928
4929#if !defined(TULIP_BUS_DMA_NOTX)
4930 /*
4931 * Allocate dmamap for setup descriptor
4932 */
4933 error = bus_dmamap_create(sc->tulip_dmatag, sizeof(sc->tulip_setupbuf), 2,
4934 sizeof(sc->tulip_setupbuf), 0, BUS_DMA_NOWAIT,
4935 &sc->tulip_setupmap);
4936 if (error == 0) {
4937 error = bus_dmamap_load(sc->tulip_dmatag, sc->tulip_setupmap,
4938 sc->tulip_setupbuf, sizeof(sc->tulip_setupbuf),
4939 NULL, BUS_DMA_NOWAIT);
4940 if (error)
4941 bus_dmamap_destroy(sc->tulip_dmatag, sc->tulip_setupmap);
4942 }
4943 /*
4944 * Allocate space and dmamap for transmit ring
4945 */
4946 if (error == 0) {
4947 error = tulip_busdma_allocmem(sc, sizeof(tulip_desc_t) * TULIP_TXDESCS,
4948 &sc->tulip_txdescmap,
4949 &sc->tulip_txdescs);
4950 }
4951
4952 /*
4953 * Allocate dmamaps for each transmit descriptors
4954 */
4955 if (error == 0) {
4956 while (error == 0 && sc->tulip_txmaps_free < TULIP_TXDESCS) {
4957 bus_dmamap_t map;
4958 if ((error = TULIP_TXMAP_CREATE(sc, &map)) == 0)
4959 sc->tulip_txmaps[sc->tulip_txmaps_free++] = map;
4960 }
4961 if (error) {
4962 while (sc->tulip_txmaps_free > 0)
4963 bus_dmamap_destroy(sc->tulip_dmatag,
4964 sc->tulip_txmaps[--sc->tulip_txmaps_free]);
4965 }
4966 }
4967#else
4968 if (error == 0) {
4969 sc->tulip_txdescs = (tulip_desc_t *) malloc(TULIP_TXDESCS * sizeof(tulip_desc_t), M_DEVBUF, M_NOWAIT);
4970 if (sc->tulip_txdescs == NULL)
4971 error = ENOMEM;
4972 }
4973#endif
4974#if !defined(TULIP_BUS_DMA_NORX)
4975 /*
4976 * Allocate space and dmamap for receive ring
4977 */
4978 if (error == 0) {
4979 error = tulip_busdma_allocmem(sc, sizeof(tulip_desc_t) * TULIP_RXDESCS,
4980 &sc->tulip_rxdescmap,
4981 &sc->tulip_rxdescs);
4982 }
4983
4984 /*
4985 * Allocate dmamaps for each receive descriptors
4986 */
4987 if (error == 0) {
4988 while (error == 0 && sc->tulip_rxmaps_free < TULIP_RXDESCS) {
4989 bus_dmamap_t map;
4990 if ((error = TULIP_RXMAP_CREATE(sc, &map)) == 0)
4991 sc->tulip_rxmaps[sc->tulip_rxmaps_free++] = map;
4992 }
4993 if (error) {
4994 while (sc->tulip_rxmaps_free > 0)
4995 bus_dmamap_destroy(sc->tulip_dmatag,
4996 sc->tulip_rxmaps[--sc->tulip_rxmaps_free]);
4997 }
4998 }
4999#else
5000 if (error == 0) {
5001 sc->tulip_rxdescs = (tulip_desc_t *) malloc(TULIP_RXDESCS * sizeof(tulip_desc_t), M_DEVBUF, M_NOWAIT);
5002 if (sc->tulip_rxdescs == NULL)
5003 error = ENOMEM;
5004 }
5005#endif
5006 return error;
5007}
5008#endif /* TULIP_BUS_DMA */
5009
5010static void
5011tulip_initcsrs(
5012 tulip_softc_t * const sc,
5013 tulip_csrptr_t csr_base,
5014 size_t csr_size)
5015{
5016 sc->tulip_csrs.csr_busmode = csr_base + 0 * csr_size;
5017 sc->tulip_csrs.csr_txpoll = csr_base + 1 * csr_size;
5018 sc->tulip_csrs.csr_rxpoll = csr_base + 2 * csr_size;
5019 sc->tulip_csrs.csr_rxlist = csr_base + 3 * csr_size;
5020 sc->tulip_csrs.csr_txlist = csr_base + 4 * csr_size;
5021 sc->tulip_csrs.csr_status = csr_base + 5 * csr_size;
5022 sc->tulip_csrs.csr_command = csr_base + 6 * csr_size;
5023 sc->tulip_csrs.csr_intr = csr_base + 7 * csr_size;
5024 sc->tulip_csrs.csr_missed_frames = csr_base + 8 * csr_size;
5025 sc->tulip_csrs.csr_9 = csr_base + 9 * csr_size;
5026 sc->tulip_csrs.csr_10 = csr_base + 10 * csr_size;
5027 sc->tulip_csrs.csr_11 = csr_base + 11 * csr_size;
5028 sc->tulip_csrs.csr_12 = csr_base + 12 * csr_size;
5029 sc->tulip_csrs.csr_13 = csr_base + 13 * csr_size;
5030 sc->tulip_csrs.csr_14 = csr_base + 14 * csr_size;
5031 sc->tulip_csrs.csr_15 = csr_base + 15 * csr_size;
5032}
5033
5034static void
5035tulip_initring(
5036 tulip_softc_t * const sc,
5037 tulip_ringinfo_t * const ri,
5038 tulip_desc_t *descs,
5039 int ndescs)
5040{
5041 ri->ri_max = ndescs;
5042 ri->ri_first = descs;
5043 ri->ri_last = ri->ri_first + ri->ri_max;
5044 bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
5045 ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
5046}
5047
5048/*
5049 * This is the PCI configuration support.
5050 */
5051
5052#define PCI_CBIO 0x10 /* Configuration Base IO Address */
5053#define PCI_CBMA 0x14 /* Configuration Base Memory Address */
5054#define PCI_CFDA 0x40 /* Configuration Driver Area */
5055
5056static int
5057tulip_pci_probe(device_t dev)
5058{
5059 const char *name = NULL;
5060
5061 if (pci_get_vendor(dev) != DEC_VENDORID)
5062 return ENXIO;
5063
5064 /*
5065 * Some LanMedia WAN cards use the Tulip chip, but they have
5066 * their own driver, and we should not recognize them
5067 */
5068 if (pci_get_subvendor(dev) == 0x1376)
5069 return ENXIO;
5070
5071 switch (pci_get_device(dev)) {
5072 case CHIPID_21040:
5073 name = "Digital 21040 Ethernet";
5074 break;
5075 case CHIPID_21041:
5076 name = "Digital 21041 Ethernet";
5077 break;
5078 case CHIPID_21140:
5079 if (pci_get_revid(dev) >= 0x20)
5080 name = "Digital 21140A Fast Ethernet";
5081 else
5082 name = "Digital 21140 Fast Ethernet";
5083 break;
5084 case CHIPID_21142:
5085 if (pci_get_revid(dev) >= 0x20)
5086 name = "Digital 21143 Fast Ethernet";
5087 else
5088 name = "Digital 21142 Fast Ethernet";
5089 break;
5090 }
5091 if (name) {
5092 device_set_desc(dev, name);
5093 return -200;
5094 }
5095 return ENXIO;
5096}
5097
5098static int
5099tulip_shutdown(device_t dev)
5100{
5101 tulip_softc_t * const sc = device_get_softc(dev);
5102 TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
5103 DELAY(10); /* Wait 10 microseconds (actually 50 PCI cycles but at
5104 33MHz that comes to two microseconds but wait a
5105 bit longer anyways) */
5106 return 0;
5107}
5108
5109static int
5110tulip_pci_attach(device_t dev)
5111{
5112 tulip_softc_t *sc;
5113#if defined(__alpha__)
5114 tulip_media_t media = TULIP_MEDIA_UNKNOWN;
5115#endif
5116 int retval, idx;
5117 u_int32_t revinfo, cfdainfo, cfcsinfo;
5118 unsigned csroffset = TULIP_PCI_CSROFFSET;
5119 unsigned csrsize = TULIP_PCI_CSRSIZE;
5120 tulip_csrptr_t csr_base;
5121 tulip_chipid_t chipid = TULIP_CHIPID_UNKNOWN;
5122 struct resource *res;
5123 int rid, unit;
5124
5125 unit = device_get_unit(dev);
5126
5127 if (unit >= TULIP_MAX_DEVICES) {
5128 printf("de%d", unit);
5129 printf(": not configured; limit of %d reached or exceeded\n",
5130 TULIP_MAX_DEVICES);
5131 return ENXIO;
5132 }
5133
5134 revinfo = pci_get_revid(dev);
5135 cfdainfo = pci_read_config(dev, PCI_CFDA, 4);
5136 cfcsinfo = pci_read_config(dev, PCIR_COMMAND, 4);
5137
5138 /* turn busmaster on in case BIOS doesn't set it */
5139 if(!(cfcsinfo & PCIM_CMD_BUSMASTEREN)) {
5140 cfcsinfo |= PCIM_CMD_BUSMASTEREN;
5141 pci_write_config(dev, PCIR_COMMAND, cfcsinfo, 4);
5142 }
5143
5144 if (pci_get_vendor(dev) == DEC_VENDORID) {
5145 if (pci_get_device(dev) == CHIPID_21040)
5146 chipid = TULIP_21040;
5147 else if (pci_get_device(dev) == CHIPID_21041)
5148 chipid = TULIP_21041;
5149 else if (pci_get_device(dev) == CHIPID_21140)
5150 chipid = (revinfo >= 0x20) ? TULIP_21140A : TULIP_21140;
5151 else if (pci_get_device(dev) == CHIPID_21142)
5152 chipid = (revinfo >= 0x20) ? TULIP_21143 : TULIP_21142;
5153 }
5154 if (chipid == TULIP_CHIPID_UNKNOWN)
5155 return ENXIO;
5156
5157 if (chipid == TULIP_21040 && revinfo < 0x20) {
5158 printf("de%d", unit);
5159 printf(": not configured; 21040 pass 2.0 required (%d.%d found)\n",
5160 revinfo >> 4, revinfo & 0x0f);
5161 return ENXIO;
5162 } else if (chipid == TULIP_21140 && revinfo < 0x11) {
5163 printf("de%d: not configured; 21140 pass 1.1 required (%d.%d found)\n",
5164 unit, revinfo >> 4, revinfo & 0x0f);
5165 return ENXIO;
5166 }
5167
5168 sc = device_get_softc(dev);
5169 sc->tulip_pci_busno = pci_get_bus(dev);
5170 sc->tulip_pci_devno = pci_get_slot(dev);
5171 sc->tulip_chipid = chipid;
5172 sc->tulip_flags |= TULIP_DEVICEPROBE;
5173 if (chipid == TULIP_21140 || chipid == TULIP_21140A)
5174 sc->tulip_features |= TULIP_HAVE_GPR|TULIP_HAVE_STOREFWD;
5175 if (chipid == TULIP_21140A && revinfo <= 0x22)
5176 sc->tulip_features |= TULIP_HAVE_RXBADOVRFLW;
5177 if (chipid == TULIP_21140)
5178 sc->tulip_features |= TULIP_HAVE_BROKEN_HASH;
5179 if (chipid != TULIP_21040 && chipid != TULIP_21140)
5180 sc->tulip_features |= TULIP_HAVE_POWERMGMT;
5181 if (chipid == TULIP_21041 || chipid == TULIP_21142 || chipid == TULIP_21143) {
5182 sc->tulip_features |= TULIP_HAVE_DUALSENSE;
5183 if (chipid != TULIP_21041 || revinfo >= 0x20)
5184 sc->tulip_features |= TULIP_HAVE_SIANWAY;
5185 if (chipid != TULIP_21041)
5186 sc->tulip_features |= TULIP_HAVE_SIAGP|TULIP_HAVE_RXBADOVRFLW|TULIP_HAVE_STOREFWD;
5187 if (chipid != TULIP_21041 && revinfo >= 0x20)
5188 sc->tulip_features |= TULIP_HAVE_SIA100;
5189 }
5190
5191 if (sc->tulip_features & TULIP_HAVE_POWERMGMT
5192 && (cfdainfo & (TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE))) {
5193 cfdainfo &= ~(TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE);
5194 pci_write_config(dev, PCI_CFDA, cfdainfo, 4);
5195 DELAY(11*1000);
5196 }
5197#if defined(__alpha__)
5198 /*
5199 * The Alpha SRM console encodes a console set media in the driver
5200 * part of the CFDA register. Note that the Multia presents a
5201 * problem in that its BNC mode is really EXTSIA. So in that case
5202 * force a probe.
5203 */
5204 switch ((cfdainfo >> 8) & 0xff) {
5205 case 1: media = chipid > TULIP_21040 ? TULIP_MEDIA_AUI : TULIP_MEDIA_AUIBNC; break;
5206 case 2: media = chipid > TULIP_21040 ? TULIP_MEDIA_BNC : TULIP_MEDIA_UNKNOWN; break;
5207 case 3: media = TULIP_MEDIA_10BASET; break;
5208 case 4: media = TULIP_MEDIA_10BASET_FD; break;
5209 case 5: media = TULIP_MEDIA_100BASETX; break;
5210 case 6: media = TULIP_MEDIA_100BASETX_FD; break;
5211 default: media = TULIP_MEDIA_UNKNOWN; break;
5212 }
5213#endif
5214
5215 sc->tulip_unit = unit;
5216 sc->tulip_name = "de";
5217 sc->tulip_revinfo = revinfo;
5218 sc->tulip_if.if_softc = sc;
5219#if defined(TULIP_IOMAPPED)
5220 rid = PCI_CBIO;
5221 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
5222 0, ~0, 1, RF_ACTIVE);
5223#else
5224 rid = PCI_CBMA;
5225 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
5226 0, ~0, 1, RF_ACTIVE);
5227#endif
5228 if (!res)
5229 return ENXIO;
5230 sc->tulip_csrs_bst = rman_get_bustag(res);
5231 sc->tulip_csrs_bsh = rman_get_bushandle(res);
5232 csr_base = 0;
5233
5234 tulips[unit] = sc;
5235
5236 tulip_initcsrs(sc, csr_base + csroffset, csrsize);
5237
5238#if defined(TULIP_BUS_DMA)
5239 if ((retval = tulip_busdma_init(sc)) != 0) {
5240 printf("error initing bus_dma: %d\n", retval);
5241 return ENXIO;
5242 }
5243#else
5244 sc->tulip_rxdescs = (tulip_desc_t *) malloc(sizeof(tulip_desc_t) * TULIP_RXDESCS, M_DEVBUF, M_NOWAIT);
5245 sc->tulip_txdescs = (tulip_desc_t *) malloc(sizeof(tulip_desc_t) * TULIP_TXDESCS, M_DEVBUF, M_NOWAIT);
5246 if (sc->tulip_rxdescs == NULL || sc->tulip_txdescs == NULL) {
5247 printf("malloc failed\n");
5248 if (sc->tulip_rxdescs)
5249 free((caddr_t) sc->tulip_rxdescs, M_DEVBUF);
5250 if (sc->tulip_txdescs)
5251 free((caddr_t) sc->tulip_txdescs, M_DEVBUF);
5252 return ENXIO;
5253 }
5254#endif
5255
5256 tulip_initring(sc, &sc->tulip_rxinfo, sc->tulip_rxdescs, TULIP_RXDESCS);
5257 tulip_initring(sc, &sc->tulip_txinfo, sc->tulip_txdescs, TULIP_TXDESCS);
5258
5259 /*
5260 * Make sure there won't be any interrupts or such...
5261 */
5262 TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
5263 DELAY(100); /* Wait 10 microseconds (actually 50 PCI cycles but at
5264 33MHz that comes to two microseconds but wait a
5265 bit longer anyways) */
5266
5267 if ((retval = tulip_read_macaddr(sc)) < 0) {
5268 printf("%s%d", sc->tulip_name, sc->tulip_unit);
5269 printf(": can't read ENET ROM (why=%d) (", retval);
5270 for (idx = 0; idx < 32; idx++)
5271 printf("%02x", sc->tulip_rombuf[idx]);
5272 printf("\n");
5273 printf("%s%d: %s%s pass %d.%d\n",
5274 sc->tulip_name, sc->tulip_unit,
5275 sc->tulip_boardid, tulip_chipdescs[sc->tulip_chipid],
5276 (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F);
5277 printf("%s%d: address unknown\n", sc->tulip_name, sc->tulip_unit);
5278 } else {
5279 int s;
5280 void (*intr_rtn)(void *) = tulip_intr_normal;
5281
5282 if (sc->tulip_features & TULIP_HAVE_SHAREDINTR)
5283 intr_rtn = tulip_intr_shared;
5284
5285 if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
5286 void *ih;
5287
5288 rid = 0;
5289 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
5290 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
5291 if (res == 0 || bus_setup_intr(dev, res, INTR_TYPE_NET,
5292 intr_rtn, sc, &ih)) {
5293 printf("%s%d: couldn't map interrupt\n",
5294 sc->tulip_name, sc->tulip_unit);
5295 free((caddr_t) sc->tulip_rxdescs, M_DEVBUF);
5296 free((caddr_t) sc->tulip_txdescs, M_DEVBUF);
5297 return ENXIO;
5298 }
5299 }
5300#if defined(TULIP_USE_SOFTINTR)
5301 if (sc->tulip_unit > tulip_softintr_max_unit)
5302 tulip_softintr_max_unit = sc->tulip_unit;
5303#endif
5304
5305 s = splimp();
5306#if defined(__alpha__)
5307 sc->tulip_media = media;
5308#endif
5309 tulip_attach(sc);
5310#if defined(__alpha__)
5311 if (sc->tulip_media != TULIP_MEDIA_UNKNOWN)
5312 tulip_linkup(sc, media);
5313#endif
5314 splx(s);
5315 }
5316 return 0;
5317}
5318
5319static device_method_t tulip_pci_methods[] = {
5320 /* Device interface */
5321 DEVMETHOD(device_probe, tulip_pci_probe),
5322 DEVMETHOD(device_attach, tulip_pci_attach),
5323 DEVMETHOD(device_shutdown, tulip_shutdown),
5324 { 0, 0 }
5325};
5326static driver_t tulip_pci_driver = {
5327 "de",
5328 tulip_pci_methods,
5329 sizeof(tulip_softc_t),
5330};
5331static devclass_t tulip_devclass;
5332DRIVER_MODULE(if_de, pci, tulip_pci_driver, tulip_devclass, 0, 0);