1 /* $OpenBSD: if_de.c,v 1.59 2004/05/12 06:35:11 tedu Exp $ */
2 /* $NetBSD: if_de.c,v 1.45 1997/06/09 00:34:18 thorpej Exp $ */
3
4 /*-
5 * Copyright (c) 1994-1997 Matt Thomas (matt@3am-software.com)
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Id: if_de.c,v 1.89 1997/06/03 19:19:55 thomas Exp
28 *
29 */
30
31 /*
32 * DEC 21040 PCI Ethernet Controller
33 *
34 * Written by Matt Thomas
35 * BPF support code stolen directly from if_ec.c
36 *
37 * This driver supports the DEC DE435 or any other PCI
38 * board which support 21040, 21041, or 21140 (mostly).
39 */
40 #define TULIP_HDR_DATA
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
51 #include <sys/proc.h> /* only for declaration of wakeup() used by vm.h */
52 #if defined(__FreeBSD__)
53 #include <machine/clock.h>
54 #elif defined(__bsdi__) || defined(__NetBSD__) || defined(__OpenBSD__)
55 #include <sys/device.h>
56 #endif
57 #ifdef __OpenBSD__
58 #include <sys/timeout.h>
59 #endif
60
61 #include <net/if.h>
62 #if defined(SIOCSIFMEDIA) && !defined(TULIP_NOIFMEDIA)
63 #include <net/if_media.h>
64 #endif
65 #include <net/if_types.h>
66 #include <net/if_dl.h>
67 #include <net/route.h>
68 #include <net/netisr.h>
69
70 #if defined(__bsdi__) && _BSDI_VERSION >= 199701
71 #include <dev/mii/mii.h>
72 #include <dev/mii/miivar.h>
73 #endif
74
75 #include "bpfilter.h"
76 #if NBPFILTER > 0
77 #include <net/bpf.h>
78 #endif
79
80 #ifdef INET
81 #include <netinet/in.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip.h>
85 #endif
86
87 #ifdef NS
88 #include <netns/ns.h>
89 #include <netns/ns_if.h>
90 #endif
91
92 #include <uvm/uvm_extern.h>
93
94 #if defined(__FreeBSD__)
95 #include <vm/pmap.h>
96 #include <pci.h>
97 #include <netinet/if_ether.h>
98 #if NPCI > 0
99 #include <pci/pcivar.h>
100 #include <pci/dc21040reg.h>
101 #define DEVAR_INCLUDE "pci/if_devar.h"
102 #endif
103 #endif /* __FreeBSD__ */
104
105 #if defined(__bsdi__)
106 #include <netinet/if_ether.h>
107 #include <i386/pci/ic/dc21040reg.h>
108 #include <i386/isa/isa.h>
109 #include <i386/isa/icu.h>
110 #include <i386/isa/dma.h>
111 #include <i386/isa/isavar.h>
112 #include <i386/pci/pci.h>
113 #if _BSDI_VERSION < 199510
114 #include <eisa.h>
115 #else
116 #define NEISA 0
117 #endif
118 #if NEISA > 0 && _BSDI_VERSION >= 199401
119 #include <i386/eisa/eisa.h>
120 #define TULIP_EISA
121 #endif
122 #define DEVAR_INCLUDE "i386/pci/if_devar.h"
123 #endif /* __bsdi__ */
124
125 #if defined(__NetBSD__)
126 #include <net/if_ether.h>
127 #if defined(INET)
128 #include <netinet/if_inarp.h>
129 #endif
130 #endif
131
132 #if defined(__OpenBSD__)
133 #include <dev/rndvar.h>
134 #include <netinet/if_ether.h>
135 #endif
136
137 #if defined(__NetBSD__) || defined(__OpenBSD__)
138 #include <machine/bus.h>
139 #if defined(__alpha__)
140 #include <machine/intr.h>
141 #endif
142 #include <dev/pci/pcireg.h>
143 #include <dev/pci/pcivar.h>
144 #include <dev/pci/pcidevs.h>
145 #include <dev/ic/dc21040reg.h>
146 #define DEVAR_INCLUDE "dev/pci/if_devar.h"
147 #endif /* __NetBSD__ */
148
149 /*
150 * Intel CPUs should use I/O mapped access.
151 */
152 #if defined(__i386__) || defined(TULIP_EISA)
153 #define TULIP_IOMAPPED
154 #endif
155
156 /*
157 * This turns on all sort of debugging stuff and make the
158 * driver much larger.
159 */
160 #if 0
161 #define TULIP_DEBUG
162 #endif
163
164 #if 0
165 #define TULIP_USE_SOFTINTR
166 #endif
167
168 #define TULIP_HZ 10
169
170 #include DEVAR_INCLUDE
171 /*
172 * This module supports
173 * the DEC 21040 PCI Ethernet Controller.
174 * the DEC 21041 PCI Ethernet Controller.
175 * the DEC 21140 PCI Fast Ethernet Controller.
176 */
177 static void tulip_mii_autonegotiate(tulip_softc_t * const sc, const unsigned phyaddr);
178 static tulip_intrfunc_t tulip_intr_shared(void *arg);
179 static tulip_intrfunc_t tulip_intr_normal(void *arg);
180 static void tulip_init(tulip_softc_t * const sc);
181 static void tulip_reset(tulip_softc_t * const sc);
182 static ifnet_ret_t tulip_ifstart_one(struct ifnet *ifp);
183 static ifnet_ret_t tulip_ifstart(struct ifnet *ifp);
184 static struct mbuf *tulip_txput(tulip_softc_t * const sc, struct mbuf *m);
185 static void tulip_txput_setup(tulip_softc_t * const sc);
186 static void tulip_rx_intr(tulip_softc_t * const sc);
187 static void tulip_addr_filter(tulip_softc_t * const sc);
188 static unsigned tulip_mii_readreg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno);
189 static void tulip_mii_writereg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno, unsigned data);
190 static int tulip_mii_map_abilities(tulip_softc_t * const sc, unsigned abilities);
191 static tulip_media_t tulip_mii_phy_readspecific(tulip_softc_t * const sc);
192 static int tulip_srom_decode(tulip_softc_t * const sc);
193 #if defined(IFM_ETHER)
194 static int tulip_ifmedia_change(struct ifnet * const ifp);
195 static void tulip_ifmedia_status(struct ifnet * const ifp, struct ifmediareq *req);
196 #endif
197 /* static void tulip_21140_map_media(tulip_softc_t *sc); */
198
199 static void
tulip_timeout_callback(void * arg)200 tulip_timeout_callback(
201 void *arg)
202 {
203 tulip_softc_t * const sc = arg;
204 tulip_spl_t s = TULIP_RAISESPL();
205
206 sc->tulip_flags &= ~TULIP_TIMEOUTPENDING;
207 sc->tulip_probe_timeout -= 1000 / TULIP_HZ;
208 (sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_TIMER);
209 TULIP_RESTORESPL(s);
210 }
211
212 static void
tulip_timeout(tulip_softc_t * const sc)213 tulip_timeout(
214 tulip_softc_t * const sc)
215 {
216 if (sc->tulip_flags & TULIP_TIMEOUTPENDING)
217 return;
218 sc->tulip_flags |= TULIP_TIMEOUTPENDING;
219 #ifdef __OpenBSD__
220 timeout_add(&sc->tulip_stmo, (hz + TULIP_HZ / 2) / TULIP_HZ);
221 #else
222 timeout(tulip_timeout_callback, sc, (hz + TULIP_HZ / 2) / TULIP_HZ);
223 #endif
224 }
225
226 #if defined(TULIP_NEED_FASTTIMEOUT)
227 static void
tulip_fasttimeout_callback(void * arg)228 tulip_fasttimeout_callback(
229 void *arg)
230 {
231 tulip_softc_t * const sc = arg;
232 tulip_spl_t s = TULIP_RAISESPL();
233
234 sc->tulip_flags &= ~TULIP_FASTTIMEOUTPENDING;
235 (sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_FASTTIMER);
236 TULIP_RESTORESPL(s);
237 }
238
239 static void
tulip_fasttimeout(tulip_softc_t * const sc)240 tulip_fasttimeout(
241 tulip_softc_t * const sc)
242 {
243 if (sc->tulip_flags & TULIP_FASTTIMEOUTPENDING)
244 return;
245 sc->tulip_flags |= TULIP_FASTTIMEOUTPENDING;
246 #ifdef __OpenBSD__
247 timeout_add(&sc->tulip_ftmo, 1);
248 #else
249 timeout(tulip_fasttimeout_callback, sc, 1);
250 #endif
251 }
252 #endif
253
254 static int
tulip_txprobe(tulip_softc_t * const sc)255 tulip_txprobe(
256 tulip_softc_t * const sc)
257 {
258 struct mbuf *m;
259 /*
260 * Before we are sure this is the right media we need
261 * to send a small packet to make sure there's carrier.
262 * Strangely, BNC and AUI will "see" receive data if
263 * either is connected so the transmit is the only way
264 * to verify the connectivity.
265 */
266 MGETHDR(m, M_DONTWAIT, MT_DATA);
267 if (m == NULL)
268 return 0;
269 /*
270 * Construct a LLC TEST message which will point to ourselves.
271 */
272 bcopy(sc->tulip_enaddr, mtod(m, struct ether_header *)->ether_dhost, 6);
273 bcopy(sc->tulip_enaddr, mtod(m, struct ether_header *)->ether_shost, 6);
274 mtod(m, struct ether_header *)->ether_type = htons(3);
275 mtod(m, unsigned char *)[14] = 0;
276 mtod(m, unsigned char *)[15] = 0;
277 mtod(m, unsigned char *)[16] = 0xE3; /* LLC Class1 TEST (no poll) */
278 m->m_len = m->m_pkthdr.len = sizeof(struct ether_header) + 3;
279 /*
280 * send it!
281 */
282 sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
283 sc->tulip_intrmask |= TULIP_STS_TXINTR;
284 sc->tulip_flags |= TULIP_TXPROBE_ACTIVE;
285 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
286 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
287 if ((m = tulip_txput(sc, m)) != NULL)
288 m_freem(m);
289 sc->tulip_probe.probe_txprobes++;
290 return 1;
291 }
292
293 #ifdef BIG_PACKET
294 #define TULIP_SIAGEN_WATCHDOG (sc->tulip_if.if_mtu > ETHERMTU ? TULIP_WATCHDOG_RXDISABLE|TULIP_WATCHDOG_TXDISABLE : 0)
295 #else
296 #define TULIP_SIAGEN_WATCHDOG 0
297 #endif
298
299 static void
tulip_media_set(tulip_softc_t * const sc,tulip_media_t media)300 tulip_media_set(
301 tulip_softc_t * const sc,
302 tulip_media_t media)
303 {
304 const tulip_media_info_t *mi = sc->tulip_mediums[media];
305
306 if (mi == NULL)
307 return;
308
309 /*
310 * If we are switching media, make sure we don't think there's
311 * any stale RX activity
312 */
313 sc->tulip_flags &= ~TULIP_RXACT;
314 if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
315 TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
316 TULIP_CSR_WRITE(sc, csr_sia_tx_rx, mi->mi_sia_tx_rx);
317 if (sc->tulip_features & TULIP_HAVE_SIAGP) {
318 TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_sia_gp_control|mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
319 DELAY(50);
320 TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_sia_gp_data|mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
321 } else {
322 TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_sia_general|TULIP_SIAGEN_WATCHDOG);
323 }
324 TULIP_CSR_WRITE(sc, csr_sia_connectivity, mi->mi_sia_connectivity);
325 } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
326 #define TULIP_GPR_CMDBITS (TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER|TULIP_CMD_TXTHRSHLDCTL)
327 /*
328 * If the cmdmode bits don't match the currently operating mode,
329 * set the cmdmode appropriately and reset the chip.
330 */
331 if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
332 sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
333 sc->tulip_cmdmode |= mi->mi_cmdmode;
334 tulip_reset(sc);
335 }
336 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
337 DELAY(10);
338 TULIP_CSR_WRITE(sc, csr_gp, (u_int8_t) mi->mi_gpdata);
339 } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
340 /*
341 * If the cmdmode bits don't match the currently operating mode,
342 * set the cmdmode appropriately and reset the chip.
343 */
344 if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
345 sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
346 sc->tulip_cmdmode |= mi->mi_cmdmode;
347 tulip_reset(sc);
348 }
349 TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpcontrol);
350 TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpdata);
351 } else if (mi->mi_type == TULIP_MEDIAINFO_MII
352 && sc->tulip_probe_state != TULIP_PROBE_INACTIVE) {
353 int idx;
354 if (sc->tulip_features & TULIP_HAVE_SIAGP) {
355 const u_int8_t *dp;
356 dp = &sc->tulip_rombuf[mi->mi_reset_offset];
357 for (idx = 0; idx < mi->mi_reset_length; idx++, dp += 2) {
358 DELAY(10);
359 TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
360 }
361 sc->tulip_phyaddr = mi->mi_phyaddr;
362 dp = &sc->tulip_rombuf[mi->mi_gpr_offset];
363 for (idx = 0; idx < mi->mi_gpr_length; idx++, dp += 2) {
364 DELAY(10);
365 TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
366 }
367 } else {
368 for (idx = 0; idx < mi->mi_reset_length; idx++) {
369 DELAY(10);
370 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx]);
371 }
372 sc->tulip_phyaddr = mi->mi_phyaddr;
373 for (idx = 0; idx < mi->mi_gpr_length; idx++) {
374 DELAY(10);
375 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx]);
376 }
377 }
378 if (sc->tulip_flags & TULIP_TRYNWAY) {
379 tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
380 } else if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
381 u_int32_t data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_CONTROL);
382 data &= ~(PHYCTL_SELECT_100MB|PHYCTL_FULL_DUPLEX|PHYCTL_AUTONEG_ENABLE);
383 sc->tulip_flags &= ~TULIP_DIDNWAY;
384 if (TULIP_IS_MEDIA_FD(media))
385 data |= PHYCTL_FULL_DUPLEX;
386 if (TULIP_IS_MEDIA_100MB(media))
387 data |= PHYCTL_SELECT_100MB;
388 tulip_mii_writereg(sc, sc->tulip_phyaddr, PHYREG_CONTROL, data);
389 }
390 }
391 }
392
393 static void
tulip_linkup(tulip_softc_t * const sc,tulip_media_t media)394 tulip_linkup(
395 tulip_softc_t * const sc,
396 tulip_media_t media)
397 {
398 if ((sc->tulip_flags & TULIP_LINKUP) == 0)
399 sc->tulip_flags |= TULIP_PRINTLINKUP;
400 sc->tulip_flags |= TULIP_LINKUP;
401 sc->tulip_if.if_flags &= ~IFF_OACTIVE;
402 #if 0 /* XXX how does with work with ifmedia? */
403 if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
404 if (sc->tulip_if.if_flags & IFF_FULLDUPLEX) {
405 if (TULIP_CAN_MEDIA_FD(media)
406 && sc->tulip_mediums[TULIP_FD_MEDIA_OF(media)] != NULL)
407 media = TULIP_FD_MEDIA_OF(media);
408 } else {
409 if (TULIP_IS_MEDIA_FD(media)
410 && sc->tulip_mediums[TULIP_HD_MEDIA_OF(media)] != NULL)
411 media = TULIP_HD_MEDIA_OF(media);
412 }
413 }
414 #endif
415 if (sc->tulip_media != media) {
416 #ifdef TULIP_DEBUG
417 sc->tulip_dbg.dbg_last_media = sc->tulip_media;
418 #endif
419 sc->tulip_media = media;
420 sc->tulip_flags |= TULIP_PRINTMEDIA;
421 if (TULIP_IS_MEDIA_FD(sc->tulip_media)) {
422 sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
423 } else if (sc->tulip_chipid != TULIP_21041 || (sc->tulip_flags & TULIP_DIDNWAY) == 0) {
424 sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
425 }
426 }
427 /*
428 * We could set probe_timeout to 0 but setting to 3000 puts this
429 * in one central place and the only matters is tulip_link is
430 * followed by a tulip_timeout. Therefore setting it should not
431 * result in aberrant behavour.
432 */
433 sc->tulip_probe_timeout = 3000;
434 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
435 sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_TRYNWAY);
436 if (sc->tulip_flags & TULIP_INRESET) {
437 tulip_media_set(sc, sc->tulip_media);
438 } else if (sc->tulip_probe_media != sc->tulip_media) {
439 /*
440 * No reason to change media if we have the right media.
441 */
442 tulip_reset(sc);
443 }
444 tulip_init(sc);
445 }
446
447 static void
tulip_media_print(tulip_softc_t * const sc)448 tulip_media_print(
449 tulip_softc_t * const sc)
450 {
451 if ((sc->tulip_flags & TULIP_LINKUP) == 0)
452 return;
453 if (sc->tulip_flags & TULIP_PRINTMEDIA) {
454 printf(TULIP_PRINTF_FMT ": enabling %s port\n",
455 TULIP_PRINTF_ARGS,
456 tulip_mediums[sc->tulip_media]);
457 sc->tulip_flags &= ~(TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
458 } else if (sc->tulip_flags & TULIP_PRINTLINKUP) {
459 printf(TULIP_PRINTF_FMT ": link up\n", TULIP_PRINTF_ARGS);
460 sc->tulip_flags &= ~TULIP_PRINTLINKUP;
461 }
462 }
463
464 #if defined(TULIP_DO_GPR_SENSE)
465 static tulip_media_t
tulip_21140_gpr_media_sense(tulip_softc_t * const sc)466 tulip_21140_gpr_media_sense(
467 tulip_softc_t * const sc)
468 {
469 tulip_media_t maybe_media = TULIP_MEDIA_UNKNOWN;
470 tulip_media_t last_media = TULIP_MEDIA_UNKNOWN;
471 tulip_media_t media;
472
473 /*
474 * If one of the media blocks contained a default media flag,
475 * use that.
476 */
477 for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
478 const tulip_media_info_t *mi;
479 /*
480 * Media is not supported (or is full-duplex).
481 */
482 if ((mi = sc->tulip_mediums[media]) == NULL || TULIP_IS_MEDIA_FD(media))
483 continue;
484 if (mi->mi_type != TULIP_MEDIAINFO_GPR)
485 continue;
486
487 /*
488 * Remember the media is this is the "default" media.
489 */
490 if (mi->mi_default && maybe_media == TULIP_MEDIA_UNKNOWN)
491 maybe_media = media;
492
493 /*
494 * No activity mask? Can't see if it is active if there's no mask.
495 */
496 if (mi->mi_actmask == 0)
497 continue;
498
499 /*
500 * Does the activity data match?
501 */
502 if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) != mi->mi_actdata)
503 continue;
504
505 #if defined(TULIP_DEBUG)
506 printf(TULIP_PRINTF_FMT ": gpr_media_sense: %s: 0x%02x & 0x%02x == 0x%02x\n",
507 TULIP_PRINTF_ARGS, tulip_mediums[media],
508 TULIP_CSR_READ(sc, csr_gp) & 0xFF,
509 mi->mi_actmask, mi->mi_actdata);
510 #endif
511 /*
512 * It does! If this is the first media we detected, then
513 * remember this media. If isn't the first, then there were
514 * multiple matches which we equate to no match (since we don't
515 * which to select (if any).
516 */
517 if (last_media == TULIP_MEDIA_UNKNOWN) {
518 last_media = media;
519 } else if (last_media != media) {
520 last_media = TULIP_MEDIA_UNKNOWN;
521 }
522 }
523 return (last_media != TULIP_MEDIA_UNKNOWN) ? last_media : maybe_media;
524 }
525 #endif /* TULIP_DO_GPR_SENSE */
526
527 static tulip_link_status_t
tulip_media_link_monitor(tulip_softc_t * const sc)528 tulip_media_link_monitor(
529 tulip_softc_t * const sc)
530 {
531 const tulip_media_info_t * const mi = sc->tulip_mediums[sc->tulip_media];
532 tulip_link_status_t linkup = TULIP_LINK_DOWN;
533
534 if (mi == NULL) {
535 #if defined(DIAGNOSTIC) || defined(TULIP_DEBUG)
536 printf("tulip_media_link_monitor: %s: botch at line %d\n",
537 tulip_mediums[sc->tulip_media],__LINE__);
538 #endif
539 return TULIP_LINK_UNKNOWN;
540 }
541
542
543 /*
544 * Have we seen some packets? If so, the link must be good.
545 */
546 if ((sc->tulip_flags & (TULIP_RXACT|TULIP_LINKUP)) == (TULIP_RXACT|TULIP_LINKUP)) {
547 sc->tulip_flags &= ~TULIP_RXACT;
548 sc->tulip_probe_timeout = 3000;
549 return TULIP_LINK_UP;
550 }
551
552 sc->tulip_flags &= ~TULIP_RXACT;
553 if (mi->mi_type == TULIP_MEDIAINFO_MII) {
554 u_int32_t status;
555 /*
556 * Read the PHY status register.
557 */
558 status = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
559 if (status & PHYSTS_AUTONEG_DONE) {
560 /*
561 * If the PHY has completed autonegotiation, see the if the
562 * remote systems abilities have changed. If so, upgrade or
563 * downgrade as appropriate.
564 */
565 u_int32_t abilities = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_AUTONEG_ABILITIES);
566 abilities = (abilities << 6) & status;
567 if (abilities != sc->tulip_abilities) {
568 #if defined(TULIP_DEBUG)
569 loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation changed: 0x%04x -> 0x%04x\n",
570 TULIP_PRINTF_ARGS, sc->tulip_phyaddr,
571 sc->tulip_abilities, abilities);
572 #endif
573 if (tulip_mii_map_abilities(sc, abilities)) {
574 tulip_linkup(sc, sc->tulip_probe_media);
575 return TULIP_LINK_UP;
576 }
577 /*
578 * if we had selected media because of autonegotiation,
579 * we need to probe for the new media.
580 */
581 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
582 if (sc->tulip_flags & TULIP_DIDNWAY)
583 return TULIP_LINK_DOWN;
584 }
585 }
586 /*
587 * The link is now up. If was down, say its back up.
588 */
589 if ((status & (PHYSTS_LINK_UP|PHYSTS_REMOTE_FAULT)) == PHYSTS_LINK_UP)
590 linkup = TULIP_LINK_UP;
591 } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
592 /*
593 * No activity sensor? Assume all's well.
594 */
595 if (mi->mi_actmask == 0)
596 return TULIP_LINK_UNKNOWN;
597 /*
598 * Does the activity data match?
599 */
600 if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) == mi->mi_actdata)
601 linkup = TULIP_LINK_UP;
602 } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
603 /*
604 * Assume non TP ok for now.
605 */
606 if (!TULIP_IS_MEDIA_TP(sc->tulip_media))
607 return TULIP_LINK_UNKNOWN;
608 if ((TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) == 0)
609 linkup = TULIP_LINK_UP;
610 #if defined(TULIP_DEBUG)
611 if (sc->tulip_probe_timeout <= 0)
612 printf(TULIP_PRINTF_FMT ": sia status = 0x%08x\n", TULIP_PRINTF_ARGS, TULIP_CSR_READ(sc, csr_sia_status));
613 #endif
614 } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
615 return TULIP_LINK_UNKNOWN;
616 }
617 /*
618 * We will wait for 3 seconds until the link goes into suspect mode.
619 */
620 if (sc->tulip_flags & TULIP_LINKUP) {
621 if (linkup == TULIP_LINK_UP)
622 sc->tulip_probe_timeout = 3000;
623 if (sc->tulip_probe_timeout > 0)
624 return TULIP_LINK_UP;
625
626 sc->tulip_flags &= ~TULIP_LINKUP;
627 }
628 #if defined(TULIP_DEBUG)
629 sc->tulip_dbg.dbg_link_downed++;
630 #endif
631 return TULIP_LINK_DOWN;
632 }
633
634 static void
tulip_media_poll(tulip_softc_t * const sc,tulip_mediapoll_event_t event)635 tulip_media_poll(
636 tulip_softc_t * const sc,
637 tulip_mediapoll_event_t event)
638 {
639 #if defined(TULIP_DEBUG)
640 sc->tulip_dbg.dbg_events[event]++;
641 #endif
642 if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE
643 && event == TULIP_MEDIAPOLL_TIMER) {
644 switch (tulip_media_link_monitor(sc)) {
645 case TULIP_LINK_DOWN: {
646 /*
647 * Link Monitor failed. Probe for new media.
648 */
649 event = TULIP_MEDIAPOLL_LINKFAIL;
650 break;
651 }
652 case TULIP_LINK_UP: {
653 /*
654 * Check again soon.
655 */
656 tulip_timeout(sc);
657 return;
658 }
659 case TULIP_LINK_UNKNOWN: {
660 /*
661 * We can't tell so don't bother.
662 */
663 return;
664 }
665 }
666 }
667
668 if (event == TULIP_MEDIAPOLL_LINKFAIL) {
669 if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE) {
670 if (TULIP_DO_AUTOSENSE(sc)) {
671 #if defined(TULIP_DEBUG)
672 sc->tulip_dbg.dbg_link_failures++;
673 #endif
674 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
675 if (sc->tulip_if.if_flags & IFF_UP)
676 tulip_reset(sc); /* restart probe */
677 }
678 return;
679 }
680 #if defined(TULIP_DEBUG)
681 sc->tulip_dbg.dbg_link_pollintrs++;
682 #endif
683 }
684
685 if (event == TULIP_MEDIAPOLL_START) {
686 sc->tulip_if.if_flags |= IFF_OACTIVE;
687 if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE)
688 return;
689 sc->tulip_probe_mediamask = 0;
690 sc->tulip_probe_passes = 0;
691 #if defined(TULIP_DEBUG)
692 sc->tulip_dbg.dbg_media_probes++;
693 #endif
694 /*
695 * If the SROM contained an explicit media to use, use it.
696 */
697 sc->tulip_cmdmode &= ~(TULIP_CMD_RXRUN|TULIP_CMD_FULLDUPLEX);
698 sc->tulip_flags |= TULIP_TRYNWAY|TULIP_PROBE1STPASS;
699 sc->tulip_flags &= ~(TULIP_DIDNWAY|TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
700 /*
701 * connidx is defaulted to a media_unknown type.
702 */
703 sc->tulip_probe_media = tulip_srom_conninfo[sc->tulip_connidx].sc_media;
704 if (sc->tulip_probe_media != TULIP_MEDIA_UNKNOWN) {
705 tulip_linkup(sc, sc->tulip_probe_media);
706 tulip_timeout(sc);
707 return;
708 }
709
710 if (sc->tulip_features & TULIP_HAVE_GPR) {
711 sc->tulip_probe_state = TULIP_PROBE_GPRTEST;
712 sc->tulip_probe_timeout = 2000;
713 } else {
714 sc->tulip_probe_media = TULIP_MEDIA_MAX;
715 sc->tulip_probe_timeout = 0;
716 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
717 }
718 }
719
720 /*
721 * Ignore txprobe failures or spurious callbacks.
722 */
723 if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED
724 && sc->tulip_probe_state != TULIP_PROBE_MEDIATEST) {
725 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
726 return;
727 }
728
729 /*
730 * If we really transmitted a packet, then that's the media we'll use.
731 */
732 if (event == TULIP_MEDIAPOLL_TXPROBE_OK || event == TULIP_MEDIAPOLL_LINKPASS) {
733 if (event == TULIP_MEDIAPOLL_LINKPASS) {
734 /* XXX check media status just to be sure */
735 sc->tulip_probe_media = TULIP_MEDIA_10BASET;
736 #if defined(TULIP_DEBUG)
737 } else {
738 sc->tulip_dbg.dbg_txprobes_ok[sc->tulip_probe_media]++;
739 #endif
740 }
741 tulip_linkup(sc, sc->tulip_probe_media);
742 tulip_timeout(sc);
743 return;
744 }
745
746 if (sc->tulip_probe_state == TULIP_PROBE_GPRTEST) {
747 #if defined(TULIP_DO_GPR_SENSE)
748 /*
749 * Check for media via the general purpose register.
750 *
751 * Try to sense the media via the GPR. If the same value
752 * occurs 3 times in a row then just use that.
753 */
754 if (sc->tulip_probe_timeout > 0) {
755 tulip_media_t new_probe_media = tulip_21140_gpr_media_sense(sc);
756 #if defined(TULIP_DEBUG)
757 printf(TULIP_PRINTF_FMT ": media_poll: gpr sensing = %s\n",
758 TULIP_PRINTF_ARGS, tulip_mediums[new_probe_media]);
759 #endif
760 if (new_probe_media != TULIP_MEDIA_UNKNOWN) {
761 if (new_probe_media == sc->tulip_probe_media) {
762 if (--sc->tulip_probe_count == 0)
763 tulip_linkup(sc, sc->tulip_probe_media);
764 } else {
765 sc->tulip_probe_count = 10;
766 }
767 }
768 sc->tulip_probe_media = new_probe_media;
769 tulip_timeout(sc);
770 return;
771 }
772 #endif /* TULIP_DO_GPR_SENSE */
773 /*
774 * Brute force. We cycle through each of the media types
775 * and try to transmit a packet.
776 */
777 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
778 sc->tulip_probe_media = TULIP_MEDIA_MAX;
779 sc->tulip_probe_timeout = 0;
780 tulip_timeout(sc);
781 return;
782 }
783
784 if (sc->tulip_probe_state != TULIP_PROBE_MEDIATEST
785 && (sc->tulip_features & TULIP_HAVE_MII)) {
786 tulip_media_t old_media = sc->tulip_probe_media;
787 tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
788 switch (sc->tulip_probe_state) {
789 case TULIP_PROBE_FAILED:
790 case TULIP_PROBE_MEDIATEST: {
791 /*
792 * Try the next media.
793 */
794 sc->tulip_probe_mediamask |= sc->tulip_mediums[sc->tulip_probe_media]->mi_mediamask;
795 sc->tulip_probe_timeout = 0;
796 #ifdef notyet
797 if (sc->tulip_probe_state == TULIP_PROBE_FAILED)
798 break;
799 if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
800 break;
801 sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 300;
802 #endif
803 break;
804 }
805 case TULIP_PROBE_PHYAUTONEG: {
806 return;
807 }
808 case TULIP_PROBE_INACTIVE: {
809 /*
810 * Only probe if we autonegotiated a media that hasn't failed.
811 */
812 sc->tulip_probe_timeout = 0;
813 if (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media)) {
814 sc->tulip_probe_media = old_media;
815 break;
816 }
817 tulip_linkup(sc, sc->tulip_probe_media);
818 tulip_timeout(sc);
819 return;
820 }
821 default: {
822 #if defined(DIAGNOSTIC) || defined(TULIP_DEBUG)
823 printf("tulip_media_poll: botch at line %d\n", __LINE__);
824 #endif
825 break;
826 }
827 }
828 }
829
830 if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED) {
831 #if defined(TULIP_DEBUG)
832 sc->tulip_dbg.dbg_txprobes_failed[sc->tulip_probe_media]++;
833 #endif
834 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
835 return;
836 }
837
838 /*
839 * switch to another media if we tried this one enough.
840 */
841 if (/* event == TULIP_MEDIAPOLL_TXPROBE_FAILED || */ sc->tulip_probe_timeout <= 0) {
842 #if defined(TULIP_DEBUG)
843 if (sc->tulip_probe_media == TULIP_MEDIA_UNKNOWN) {
844 printf(TULIP_PRINTF_FMT ": poll media unknown!\n",
845 TULIP_PRINTF_ARGS);
846 sc->tulip_probe_media = TULIP_MEDIA_MAX;
847 }
848 #endif
849 /*
850 * Find the next media type to check for. Full Duplex
851 * types are not allowed.
852 */
853 do {
854 sc->tulip_probe_media -= 1;
855 if (sc->tulip_probe_media == TULIP_MEDIA_UNKNOWN) {
856 if (++sc->tulip_probe_passes == 3) {
857 if ((sc->tulip_if.if_flags & IFF_UP) == 0) {
858 sc->tulip_if.if_flags &= ~IFF_RUNNING;
859 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
860 return;
861 }
862 }
863 sc->tulip_flags ^= TULIP_TRYNWAY; /* XXX */
864 sc->tulip_probe_mediamask = 0;
865 sc->tulip_probe_media = TULIP_MEDIA_MAX - 1;
866 }
867 } while (sc->tulip_mediums[sc->tulip_probe_media] == NULL
868 || (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media))
869 || TULIP_IS_MEDIA_FD(sc->tulip_probe_media));
870
871 #if defined(TULIP_DEBUG)
872 printf(TULIP_PRINTF_FMT ": %s: probing %s\n", TULIP_PRINTF_ARGS,
873 event == TULIP_MEDIAPOLL_TXPROBE_FAILED ? "txprobe failed" : "timeout",
874 tulip_mediums[sc->tulip_probe_media]);
875 #endif
876 sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 1000;
877 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
878 sc->tulip_probe.probe_txprobes = 0;
879 tulip_reset(sc);
880 tulip_media_set(sc, sc->tulip_probe_media);
881 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
882 }
883 tulip_timeout(sc);
884
885 /*
886 * If this is hanging off a phy, we know are doing NWAY and we have
887 * forced the phy to a specific speed. Wait for link up before
888 * before sending a packet.
889 */
890 switch (sc->tulip_mediums[sc->tulip_probe_media]->mi_type) {
891 case TULIP_MEDIAINFO_MII: {
892 if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
893 return;
894 break;
895 }
896 case TULIP_MEDIAINFO_SIA: {
897 if (TULIP_IS_MEDIA_TP(sc->tulip_probe_media)) {
898 if (TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL)
899 return;
900 tulip_linkup(sc, sc->tulip_probe_media);
901 #ifdef notyet
902 if (sc->tulip_features & TULIP_HAVE_MII)
903 tulip_timeout(sc);
904 #endif
905 return;
906 }
907 break;
908 }
909 case TULIP_MEDIAINFO_RESET:
910 case TULIP_MEDIAINFO_SYM:
911 case TULIP_MEDIAINFO_NONE:
912 case TULIP_MEDIAINFO_GPR: {
913 break;
914 }
915 }
916 /*
917 * Try to send a packet.
918 */
919 tulip_txprobe(sc);
920 }
921
922 static void
tulip_media_select(tulip_softc_t * const sc)923 tulip_media_select(
924 tulip_softc_t * const sc)
925 {
926 if (sc->tulip_features & TULIP_HAVE_GPR) {
927 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
928 DELAY(10);
929 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpdata);
930 }
931 /*
932 * If this board has no media, just return
933 */
934 if (sc->tulip_features & TULIP_HAVE_NOMEDIA)
935 return;
936
937 if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
938 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
939 (*sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_START);
940 } else {
941 tulip_media_set(sc, sc->tulip_media);
942 }
943 }
944
945 static void
tulip_21040_mediainfo_init(tulip_softc_t * const sc,tulip_media_t media)946 tulip_21040_mediainfo_init(
947 tulip_softc_t * const sc,
948 tulip_media_t media)
949 {
950 sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_THRSHLD160
951 |TULIP_CMD_BACKOFFCTR;
952 sc->tulip_if.if_baudrate = 10000000;
953
954 if (media == TULIP_MEDIA_10BASET || media == TULIP_MEDIA_UNKNOWN) {
955 TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[0], 21040, 10BASET);
956 TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[1], 21040, 10BASET_FD);
957
958 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
959 }
960
961 if (media == TULIP_MEDIA_AUIBNC || media == TULIP_MEDIA_UNKNOWN) {
962 TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[2], 21040, AUIBNC);
963 }
964
965 if (media == TULIP_MEDIA_UNKNOWN) {
966 TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[3], 21040, EXTSIA);
967 }
968 }
969
970 static void
tulip_21040_media_probe(tulip_softc_t * const sc)971 tulip_21040_media_probe(
972 tulip_softc_t * const sc)
973 {
974 tulip_21040_mediainfo_init(sc, TULIP_MEDIA_UNKNOWN);
975 return;
976 }
977
978 static void
tulip_21040_10baset_only_media_probe(tulip_softc_t * const sc)979 tulip_21040_10baset_only_media_probe(
980 tulip_softc_t * const sc)
981 {
982 tulip_21040_mediainfo_init(sc, TULIP_MEDIA_10BASET);
983 tulip_media_set(sc, TULIP_MEDIA_10BASET);
984 sc->tulip_media = TULIP_MEDIA_10BASET;
985 }
986
987 static void
tulip_21040_10baset_only_media_select(tulip_softc_t * const sc)988 tulip_21040_10baset_only_media_select(
989 tulip_softc_t * const sc)
990 {
991 sc->tulip_flags |= TULIP_LINKUP;
992 if (sc->tulip_media == TULIP_MEDIA_10BASET_FD) {
993 sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
994 sc->tulip_flags &= ~TULIP_SQETEST;
995 } else {
996 sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
997 sc->tulip_flags |= TULIP_SQETEST;
998 }
999 tulip_media_set(sc, sc->tulip_media);
1000 }
1001
1002 static void
tulip_21040_auibnc_only_media_probe(tulip_softc_t * const sc)1003 tulip_21040_auibnc_only_media_probe(
1004 tulip_softc_t * const sc)
1005 {
1006 tulip_21040_mediainfo_init(sc, TULIP_MEDIA_AUIBNC);
1007 sc->tulip_flags |= TULIP_SQETEST|TULIP_LINKUP;
1008 tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
1009 sc->tulip_media = TULIP_MEDIA_AUIBNC;
1010 }
1011
1012 static void
tulip_21040_auibnc_only_media_select(tulip_softc_t * const sc)1013 tulip_21040_auibnc_only_media_select(
1014 tulip_softc_t * const sc)
1015 {
1016 tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
1017 sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
1018 }
1019
1020 static const tulip_boardsw_t tulip_21040_boardsw = {
1021 TULIP_21040_GENERIC,
1022 tulip_21040_media_probe,
1023 tulip_media_select,
1024 tulip_media_poll,
1025 };
1026
1027 static const tulip_boardsw_t tulip_21040_10baset_only_boardsw = {
1028 TULIP_21040_GENERIC,
1029 tulip_21040_10baset_only_media_probe,
1030 tulip_21040_10baset_only_media_select,
1031 NULL,
1032 };
1033
1034 static const tulip_boardsw_t tulip_21040_auibnc_only_boardsw = {
1035 TULIP_21040_GENERIC,
1036 tulip_21040_auibnc_only_media_probe,
1037 tulip_21040_auibnc_only_media_select,
1038 NULL,
1039 };
1040
1041 static void
tulip_21041_mediainfo_init(tulip_softc_t * const sc)1042 tulip_21041_mediainfo_init(
1043 tulip_softc_t * const sc)
1044 {
1045 tulip_media_info_t * const mi = sc->tulip_mediainfo;
1046
1047 #ifdef notyet
1048 if (sc->tulip_revinfo >= 0x20) {
1049 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, 10BASET);
1050 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, 10BASET_FD);
1051 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, AUI);
1052 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, BNC);
1053 return;
1054 }
1055 #endif
1056 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041, 10BASET);
1057 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041, 10BASET_FD);
1058 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[2], 21041, AUI);
1059 TULIP_MEDIAINFO_SIA_INIT(sc, &mi[3], 21041, BNC);
1060 }
1061
1062 static void
tulip_21041_media_noprobe(tulip_softc_t * const sc)1063 tulip_21041_media_noprobe(
1064 tulip_softc_t * const sc)
1065 {
1066 sc->tulip_if.if_baudrate = 10000000;
1067 sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_ENHCAPTEFFCT
1068 |TULIP_CMD_THRSHLD160|TULIP_CMD_BACKOFFCTR;
1069 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
1070 }
1071
1072 static void
tulip_21041_media_probe(tulip_softc_t * const sc)1073 tulip_21041_media_probe(
1074 tulip_softc_t * const sc)
1075 {
1076 sc->tulip_if.if_baudrate = 10000000;
1077 sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_ENHCAPTEFFCT
1078 |TULIP_CMD_THRSHLD160|TULIP_CMD_BACKOFFCTR;
1079 sc->tulip_intrmask |= TULIP_STS_LINKPASS;
1080 tulip_21041_mediainfo_init(sc);
1081 }
1082
1083 static void
tulip_21041_media_poll(tulip_softc_t * const sc,const tulip_mediapoll_event_t event)1084 tulip_21041_media_poll(
1085 tulip_softc_t * const sc,
1086 const tulip_mediapoll_event_t event)
1087 {
1088 u_int32_t sia_status;
1089
1090 #if defined(TULIP_DEBUG)
1091 sc->tulip_dbg.dbg_events[event]++;
1092 #endif
1093
1094 if (event == TULIP_MEDIAPOLL_LINKFAIL) {
1095 if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE
1096 || !TULIP_DO_AUTOSENSE(sc))
1097 return;
1098 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
1099 tulip_reset(sc); /* start probe */
1100 return;
1101 }
1102
1103 /*
1104 * If we've been been asked to start a poll or link change interrupt
1105 * restart the probe (and reset the tulip to a known state).
1106 */
1107 if (event == TULIP_MEDIAPOLL_START) {
1108 sc->tulip_if.if_flags |= IFF_OACTIVE;
1109 sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_RXRUN);
1110 #ifdef notyet
1111 if (sc->tulip_revinfo >= 0x20) {
1112 sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
1113 sc->tulip_flags |= TULIP_DIDNWAY;
1114 }
1115 #endif
1116 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1117 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1118 sc->tulip_probe_media = TULIP_MEDIA_10BASET;
1119 sc->tulip_probe_timeout = TULIP_21041_PROBE_10BASET_TIMEOUT;
1120 tulip_media_set(sc, TULIP_MEDIA_10BASET);
1121 tulip_timeout(sc);
1122 return;
1123 }
1124
1125 if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
1126 return;
1127
1128 if (event == TULIP_MEDIAPOLL_TXPROBE_OK) {
1129 #if defined(TULIP_DEBUG)
1130 sc->tulip_dbg.dbg_txprobes_ok[sc->tulip_probe_media]++;
1131 #endif
1132 tulip_linkup(sc, sc->tulip_probe_media);
1133 return;
1134 }
1135
1136 sia_status = TULIP_CSR_READ(sc, csr_sia_status);
1137 TULIP_CSR_WRITE(sc, csr_sia_status, sia_status);
1138 if ((sia_status & TULIP_SIASTS_LINKFAIL) == 0) {
1139 if (sc->tulip_revinfo >= 0x20) {
1140 if (sia_status & (PHYSTS_10BASET_FD << (16 - 6)))
1141 sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
1142 }
1143 /*
1144 * If the link has passed LinkPass, 10baseT is the
1145 * proper media to use.
1146 */
1147 tulip_linkup(sc, sc->tulip_probe_media);
1148 return;
1149 }
1150
1151 /*
1152 * wait for up to 2.4 seconds for the link to reach pass state.
1153 * Only then start scanning the other media for activity.
1154 * choose media with receive activity over those without.
1155 */
1156 if (sc->tulip_probe_media == TULIP_MEDIA_10BASET) {
1157 if (event != TULIP_MEDIAPOLL_TIMER)
1158 return;
1159 if (sc->tulip_probe_timeout > 0
1160 && (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) == 0) {
1161 tulip_timeout(sc);
1162 return;
1163 }
1164 sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1165 sc->tulip_flags |= TULIP_WANTRXACT;
1166 if (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) {
1167 sc->tulip_probe_media = TULIP_MEDIA_BNC;
1168 } else {
1169 sc->tulip_probe_media = TULIP_MEDIA_AUI;
1170 }
1171 tulip_media_set(sc, sc->tulip_probe_media);
1172 tulip_timeout(sc);
1173 return;
1174 }
1175
1176 /*
1177 * If we failed, clear the txprobe active flag.
1178 */
1179 if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED)
1180 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1181
1182
1183 if (event == TULIP_MEDIAPOLL_TIMER) {
1184 /*
1185 * If we've received something, then that's our link!
1186 */
1187 if (sc->tulip_flags & TULIP_RXACT) {
1188 tulip_linkup(sc, sc->tulip_probe_media);
1189 return;
1190 }
1191 /*
1192 * if no txprobe active
1193 */
1194 if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0
1195 && ((sc->tulip_flags & TULIP_WANTRXACT) == 0
1196 || (sia_status & TULIP_SIASTS_RXACTIVITY))) {
1197 sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1198 tulip_txprobe(sc);
1199 tulip_timeout(sc);
1200 return;
1201 }
1202 /*
1203 * Take 2 passes through before deciding to not
1204 * wait for receive activity. Then take another
1205 * two passes before spitting out a warning.
1206 */
1207 if (sc->tulip_probe_timeout <= 0) {
1208 if (sc->tulip_flags & TULIP_WANTRXACT) {
1209 sc->tulip_flags &= ~TULIP_WANTRXACT;
1210 sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1211 } else {
1212 if ((sc->tulip_if.if_flags & IFF_UP) == 0) {
1213 sc->tulip_if.if_flags &= ~IFF_RUNNING;
1214 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1215 return;
1216 }
1217 }
1218 }
1219 }
1220
1221 /*
1222 * Since this media failed to probe, try the other one.
1223 */
1224 sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
1225 if (sc->tulip_probe_media == TULIP_MEDIA_AUI) {
1226 sc->tulip_probe_media = TULIP_MEDIA_BNC;
1227 } else {
1228 sc->tulip_probe_media = TULIP_MEDIA_AUI;
1229 }
1230 tulip_media_set(sc, sc->tulip_probe_media);
1231 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1232 tulip_timeout(sc);
1233 }
1234
1235 static const tulip_boardsw_t tulip_21041_boardsw = {
1236 TULIP_21041_GENERIC,
1237 tulip_21041_media_probe,
1238 tulip_media_select,
1239 tulip_21041_media_poll
1240 };
1241
1242 static const tulip_boardsw_t tulip_21041np_boardsw = {
1243 TULIP_21041_GENERIC,
1244 tulip_21041_media_noprobe,
1245 tulip_media_select,
1246 tulip_21041_media_poll
1247 };
1248
1249 static const tulip_phy_attr_t tulip_mii_phy_attrlist[] = {
1250 { 0x20005c00, 0, /* 08-00-17 */
1251 {
1252 { 0x19, 0x0040, 0x0040 }, /* 10TX */
1253 { 0x19, 0x0040, 0x0000 }, /* 100TX */
1254 },
1255 #if defined(TULIP_DEBUG)
1256 "NS DP83840",
1257 #endif
1258 },
1259 { 0x0281F400, 0, /* 00-A0-7D */
1260 {
1261 { 0x12, 0x0010, 0x0000 }, /* 10T */
1262 { }, /* 100TX */
1263 { 0x12, 0x0010, 0x0010 }, /* 100T4 */
1264 { 0x12, 0x0008, 0x0008 }, /* FULL_DUPLEX */
1265 },
1266 #if defined(TULIP_DEBUG)
1267 "Seeq 80C240"
1268 #endif
1269 },
1270 #if 0
1271 { 0x0015F420, 0, /* 00-A0-7D */
1272 {
1273 { 0x12, 0x0010, 0x0000 }, /* 10T */
1274 { }, /* 100TX */
1275 { 0x12, 0x0010, 0x0010 }, /* 100T4 */
1276 { 0x12, 0x0008, 0x0008 }, /* FULL_DUPLEX */
1277 },
1278 #if defined(TULIP_DEBUG)
1279 "Broadcom BCM5000"
1280 #endif
1281 },
1282 #endif
1283 { 0x0281F400, 0, /* 00-A0-BE */
1284 {
1285 { 0x11, 0x8000, 0x0000 }, /* 10T */
1286 { 0x11, 0x8000, 0x8000 }, /* 100TX */
1287 { }, /* 100T4 */
1288 { 0x11, 0x4000, 0x4000 }, /* FULL_DUPLEX */
1289 },
1290 #if defined(TULIP_DEBUG)
1291 "ICS 1890"
1292 #endif
1293 },
1294 { 0x78100000, 0, /* 00-A0-CC */
1295 {
1296 { 0x14, 0x0800, 0x0000 }, /* 10TX */
1297 { 0x14, 0x0800, 0x0800 }, /* 100TX */
1298 { }, /* 100T4 */
1299 { 0x14, 0x1000, 0x1000 }, /* FULL_DUPLEX */
1300 },
1301 #if defined(TULIP_DEBUG)
1302 "LEVEL1 LXT970"
1303 #endif
1304 },
1305 { 0 }
1306 };
1307
1308 static tulip_media_t
tulip_mii_phy_readspecific(tulip_softc_t * const sc)1309 tulip_mii_phy_readspecific(
1310 tulip_softc_t * const sc)
1311 {
1312 const tulip_phy_attr_t *attr;
1313 u_int16_t data;
1314 u_int32_t id;
1315 unsigned idx = 0;
1316 static const tulip_media_t table[] = {
1317 TULIP_MEDIA_UNKNOWN,
1318 TULIP_MEDIA_10BASET,
1319 TULIP_MEDIA_100BASETX,
1320 TULIP_MEDIA_100BASET4,
1321 TULIP_MEDIA_UNKNOWN,
1322 TULIP_MEDIA_10BASET_FD,
1323 TULIP_MEDIA_100BASETX_FD,
1324 TULIP_MEDIA_UNKNOWN
1325 };
1326
1327 /*
1328 * Don't read phy specific registers if link is not up.
1329 */
1330 data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
1331 if ((data & (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS)) != (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS))
1332 return TULIP_MEDIA_UNKNOWN;
1333
1334 id = (tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDLOW) << 16) |
1335 tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDHIGH);
1336 for (attr = tulip_mii_phy_attrlist;; attr++) {
1337 if (attr->attr_id == 0)
1338 return TULIP_MEDIA_UNKNOWN;
1339 if ((id & ~0x0F) == attr->attr_id)
1340 break;
1341 }
1342
1343 if (attr->attr_modes[PHY_MODE_100TX].pm_regno) {
1344 const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100TX];
1345 data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1346 if ((data & pm->pm_mask) == pm->pm_value)
1347 idx = 2;
1348 }
1349 if (idx == 0 && attr->attr_modes[PHY_MODE_100T4].pm_regno) {
1350 const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100T4];
1351 data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1352 if ((data & pm->pm_mask) == pm->pm_value)
1353 idx = 3;
1354 }
1355 if (idx == 0 && attr->attr_modes[PHY_MODE_10T].pm_regno) {
1356 const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_10T];
1357 data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1358 if ((data & pm->pm_mask) == pm->pm_value)
1359 idx = 1;
1360 }
1361 if (idx != 0 && attr->attr_modes[PHY_MODE_FULLDUPLEX].pm_regno) {
1362 const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_FULLDUPLEX];
1363 data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
1364 idx += ((data & pm->pm_mask) == pm->pm_value ? 4 : 0);
1365 }
1366 return table[idx];
1367 }
1368
1369 static unsigned
tulip_mii_get_phyaddr(tulip_softc_t * const sc,unsigned offset)1370 tulip_mii_get_phyaddr(
1371 tulip_softc_t * const sc,
1372 unsigned offset)
1373 {
1374 unsigned phyaddr;
1375
1376 for (phyaddr = 1; phyaddr < 32; phyaddr++) {
1377 unsigned status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1378 if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
1379 continue;
1380 if (offset == 0)
1381 return phyaddr;
1382 offset--;
1383 }
1384 if (offset == 0) {
1385 unsigned status = tulip_mii_readreg(sc, 0, PHYREG_STATUS);
1386 if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
1387 return TULIP_MII_NOPHY;
1388 return 0;
1389 }
1390 return TULIP_MII_NOPHY;
1391 }
1392
1393 static int
tulip_mii_map_abilities(tulip_softc_t * const sc,unsigned abilities)1394 tulip_mii_map_abilities(
1395 tulip_softc_t * const sc,
1396 unsigned abilities)
1397 {
1398 sc->tulip_abilities = abilities;
1399 if (abilities & PHYSTS_100BASETX_FD) {
1400 sc->tulip_probe_media = TULIP_MEDIA_100BASETX_FD;
1401 } else if (abilities & PHYSTS_100BASET4) {
1402 sc->tulip_probe_media = TULIP_MEDIA_100BASET4;
1403 } else if (abilities & PHYSTS_100BASETX) {
1404 sc->tulip_probe_media = TULIP_MEDIA_100BASETX;
1405 } else if (abilities & PHYSTS_10BASET_FD) {
1406 sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
1407 } else if (abilities & PHYSTS_10BASET) {
1408 sc->tulip_probe_media = TULIP_MEDIA_10BASET;
1409 } else {
1410 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1411 return 0;
1412 }
1413 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1414 return 1;
1415 }
1416
1417 static void
tulip_mii_autonegotiate(tulip_softc_t * const sc,const unsigned phyaddr)1418 tulip_mii_autonegotiate(
1419 tulip_softc_t * const sc,
1420 const unsigned phyaddr)
1421 {
1422 switch (sc->tulip_probe_state) {
1423 case TULIP_PROBE_MEDIATEST:
1424 case TULIP_PROBE_INACTIVE: {
1425 sc->tulip_flags |= TULIP_DIDNWAY;
1426 tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, PHYCTL_RESET);
1427 sc->tulip_probe_timeout = 3000;
1428 sc->tulip_intrmask |= TULIP_STS_ABNRMLINTR|TULIP_STS_NORMALINTR;
1429 sc->tulip_probe_state = TULIP_PROBE_PHYRESET;
1430 /* FALL THROUGH */
1431 }
1432 case TULIP_PROBE_PHYRESET: {
1433 u_int32_t status;
1434 u_int32_t data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
1435 if (data & PHYCTL_RESET) {
1436 if (sc->tulip_probe_timeout > 0) {
1437 tulip_timeout(sc);
1438 return;
1439 }
1440 printf(TULIP_PRINTF_FMT "(phy%d): error: reset of PHY never completed!\n",
1441 TULIP_PRINTF_ARGS, phyaddr);
1442 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1443 sc->tulip_probe_state = TULIP_PROBE_FAILED;
1444 sc->tulip_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1445 return;
1446 }
1447 status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1448 if ((status & PHYSTS_CAN_AUTONEG) == 0) {
1449 #if defined(TULIP_DEBUG)
1450 loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation disabled\n",
1451 TULIP_PRINTF_ARGS, phyaddr);
1452 #endif
1453 sc->tulip_flags &= ~TULIP_DIDNWAY;
1454 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1455 return;
1456 }
1457 if (tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT) != ((status >> 6) | 0x01))
1458 tulip_mii_writereg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT, (status >> 6) | 0x01);
1459 tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, data|PHYCTL_AUTONEG_RESTART|PHYCTL_AUTONEG_ENABLE);
1460 data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
1461 #if defined(TULIP_DEBUG)
1462 if ((data & PHYCTL_AUTONEG_ENABLE) == 0)
1463 loudprintf(TULIP_PRINTF_FMT "(phy%d): oops: enable autonegotiation failed: 0x%04x\n",
1464 TULIP_PRINTF_ARGS, phyaddr, data);
1465 else
1466 loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation restarted: 0x%04x\n",
1467 TULIP_PRINTF_ARGS, phyaddr, data);
1468 sc->tulip_dbg.dbg_nway_starts++;
1469 #endif
1470 sc->tulip_probe_state = TULIP_PROBE_PHYAUTONEG;
1471 sc->tulip_probe_timeout = 3000;
1472 /* FALL THROUGH */
1473 }
1474 case TULIP_PROBE_PHYAUTONEG: {
1475 u_int32_t status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
1476 u_int32_t data;
1477 if ((status & PHYSTS_AUTONEG_DONE) == 0) {
1478 if (sc->tulip_probe_timeout > 0) {
1479 tulip_timeout(sc);
1480 return;
1481 }
1482 #if defined(TULIP_DEBUG)
1483 loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation timeout: sts=0x%04x, ctl=0x%04x\n",
1484 TULIP_PRINTF_ARGS, phyaddr, status,
1485 tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL));
1486 #endif
1487 sc->tulip_flags &= ~TULIP_DIDNWAY;
1488 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1489 return;
1490 }
1491 data = tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ABILITIES);
1492 #if defined(TULIP_DEBUG)
1493 loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation complete: 0x%04x\n",
1494 TULIP_PRINTF_ARGS, phyaddr, data);
1495 #endif
1496 data = (data << 6) & status;
1497 if (!tulip_mii_map_abilities(sc, data))
1498 sc->tulip_flags &= ~TULIP_DIDNWAY;
1499 return;
1500 }
1501 default: {
1502 #if defined(DIAGNOSTIC)
1503 printf("tulip_media_poll: botch at line %d\n", __LINE__);
1504 #endif
1505 break;
1506 }
1507 }
1508 #if defined(TULIP_DEBUG)
1509 loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation failure: state = %d\n",
1510 TULIP_PRINTF_ARGS, phyaddr, sc->tulip_probe_state);
1511 sc->tulip_dbg.dbg_nway_failures++;
1512 #endif
1513 }
1514
1515 static void
tulip_2114x_media_preset(tulip_softc_t * const sc)1516 tulip_2114x_media_preset(
1517 tulip_softc_t * const sc)
1518 {
1519 const tulip_media_info_t *mi = NULL;
1520 tulip_media_t media = sc->tulip_media;
1521
1522 if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
1523 media = sc->tulip_media;
1524 else
1525 media = sc->tulip_probe_media;
1526
1527 sc->tulip_cmdmode &= ~TULIP_CMD_PORTSELECT;
1528 sc->tulip_flags &= ~TULIP_SQETEST;
1529 if (media != TULIP_MEDIA_UNKNOWN && media != TULIP_MEDIA_MAX) {
1530 #if defined(TULIP_DEBUG)
1531 if (media < TULIP_MEDIA_MAX && sc->tulip_mediums[media] != NULL) {
1532 #endif
1533 mi = sc->tulip_mediums[media];
1534 if (mi->mi_type == TULIP_MEDIAINFO_MII) {
1535 sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
1536 } else if (mi->mi_type == TULIP_MEDIAINFO_GPR
1537 || mi->mi_type == TULIP_MEDIAINFO_SYM) {
1538 sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
1539 sc->tulip_cmdmode |= mi->mi_cmdmode;
1540 } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
1541 TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
1542 }
1543 #if defined(TULIP_DEBUG)
1544 } else {
1545 printf(TULIP_PRINTF_FMT ": preset: bad media %d!\n",
1546 TULIP_PRINTF_ARGS, media);
1547 }
1548 #endif
1549 }
1550 switch (media) {
1551 case TULIP_MEDIA_BNC:
1552 case TULIP_MEDIA_AUI:
1553 case TULIP_MEDIA_10BASET: {
1554 sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
1555 sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
1556 sc->tulip_if.if_baudrate = 10000000;
1557 sc->tulip_flags |= TULIP_SQETEST;
1558 break;
1559 }
1560 case TULIP_MEDIA_10BASET_FD: {
1561 sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL;
1562 sc->tulip_if.if_baudrate = 10000000;
1563 break;
1564 }
1565 case TULIP_MEDIA_100BASEFX:
1566 case TULIP_MEDIA_100BASET4:
1567 case TULIP_MEDIA_100BASETX: {
1568 sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL);
1569 sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
1570 sc->tulip_if.if_baudrate = 100000000;
1571 break;
1572 }
1573 case TULIP_MEDIA_100BASEFX_FD:
1574 case TULIP_MEDIA_100BASETX_FD: {
1575 sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_PORTSELECT;
1576 sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
1577 sc->tulip_if.if_baudrate = 100000000;
1578 break;
1579 }
1580 default: {
1581 break;
1582 }
1583 }
1584 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1585 }
1586
1587 /*
1588 ********************************************************************
1589 * Start of 21140/21140A support which does not use the MII interface
1590 */
1591
1592 static void
tulip_null_media_poll(tulip_softc_t * const sc,tulip_mediapoll_event_t event)1593 tulip_null_media_poll(
1594 tulip_softc_t * const sc,
1595 tulip_mediapoll_event_t event)
1596 {
1597 #if defined(TULIP_DEBUG)
1598 sc->tulip_dbg.dbg_events[event]++;
1599 #endif
1600 #if defined(DIAGNOSTIC)
1601 printf(TULIP_PRINTF_FMT ": botch(media_poll) at line %d\n",
1602 TULIP_PRINTF_ARGS, __LINE__);
1603 #endif
1604 }
1605
1606 __inline__ static void
tulip_21140_mediainit(tulip_softc_t * const sc,tulip_media_info_t * const mip,tulip_media_t const media,unsigned gpdata,unsigned cmdmode)1607 tulip_21140_mediainit(
1608 tulip_softc_t * const sc,
1609 tulip_media_info_t * const mip,
1610 tulip_media_t const media,
1611 unsigned gpdata,
1612 unsigned cmdmode)
1613 {
1614 sc->tulip_mediums[media] = mip;
1615 mip->mi_type = TULIP_MEDIAINFO_GPR;
1616 mip->mi_cmdmode = cmdmode;
1617 mip->mi_gpdata = gpdata;
1618 }
1619
1620 static void
tulip_21140_evalboard_media_probe(tulip_softc_t * const sc)1621 tulip_21140_evalboard_media_probe(
1622 tulip_softc_t * const sc)
1623 {
1624 tulip_media_info_t *mip = sc->tulip_mediainfo;
1625
1626 sc->tulip_gpinit = TULIP_GP_EB_PINS;
1627 sc->tulip_gpdata = TULIP_GP_EB_INIT;
1628 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
1629 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
1630 TULIP_CSR_WRITE(sc, csr_command,
1631 TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1632 TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1633 TULIP_CSR_WRITE(sc, csr_command,
1634 TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1635 DELAY(1000000);
1636 if ((TULIP_CSR_READ(sc, csr_gp) & TULIP_GP_EB_OK100) != 0) {
1637 sc->tulip_media = TULIP_MEDIA_10BASET;
1638 } else {
1639 sc->tulip_media = TULIP_MEDIA_100BASETX;
1640 }
1641 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1642 TULIP_GP_EB_INIT,
1643 TULIP_CMD_TXTHRSHLDCTL);
1644 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1645 TULIP_GP_EB_INIT,
1646 TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1647 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1648 TULIP_GP_EB_INIT,
1649 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1650 |TULIP_CMD_SCRAMBLER);
1651 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1652 TULIP_GP_EB_INIT,
1653 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1654 |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1655 }
1656
1657 static const tulip_boardsw_t tulip_21140_eb_boardsw = {
1658 TULIP_21140_DEC_EB,
1659 tulip_21140_evalboard_media_probe,
1660 tulip_media_select,
1661 tulip_null_media_poll,
1662 tulip_2114x_media_preset,
1663 };
1664
1665 static void
tulip_21140_accton_media_probe(tulip_softc_t * const sc)1666 tulip_21140_accton_media_probe(
1667 tulip_softc_t * const sc)
1668 {
1669 tulip_media_info_t *mip = sc->tulip_mediainfo;
1670 unsigned gpdata;
1671
1672 sc->tulip_gpinit = TULIP_GP_EB_PINS;
1673 sc->tulip_gpdata = TULIP_GP_EB_INIT;
1674 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
1675 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
1676 TULIP_CSR_WRITE(sc, csr_command,
1677 TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1678 TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1679 TULIP_CSR_WRITE(sc, csr_command,
1680 TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1681 DELAY(1000000);
1682 gpdata = TULIP_CSR_READ(sc, csr_gp);
1683 if ((gpdata & TULIP_GP_EN1207_UTP_INIT) == 0) {
1684 sc->tulip_media = TULIP_MEDIA_10BASET;
1685 } else {
1686 if ((gpdata & TULIP_GP_EN1207_BNC_INIT) == 0) {
1687 sc->tulip_media = TULIP_MEDIA_BNC;
1688 } else {
1689 sc->tulip_media = TULIP_MEDIA_100BASETX;
1690 }
1691 }
1692 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_BNC,
1693 TULIP_GP_EN1207_BNC_INIT,
1694 TULIP_CMD_TXTHRSHLDCTL);
1695 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1696 TULIP_GP_EN1207_UTP_INIT,
1697 TULIP_CMD_TXTHRSHLDCTL);
1698 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1699 TULIP_GP_EN1207_UTP_INIT,
1700 TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1701 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1702 TULIP_GP_EN1207_100_INIT,
1703 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1704 |TULIP_CMD_SCRAMBLER);
1705 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1706 TULIP_GP_EN1207_100_INIT,
1707 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1708 |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1709 }
1710
1711 static const tulip_boardsw_t tulip_21140_accton_boardsw = {
1712 TULIP_21140_EN1207,
1713 tulip_21140_accton_media_probe,
1714 tulip_media_select,
1715 tulip_null_media_poll,
1716 tulip_2114x_media_preset,
1717 };
1718
1719 static void
tulip_21140_smc9332_media_probe(tulip_softc_t * const sc)1720 tulip_21140_smc9332_media_probe(
1721 tulip_softc_t * const sc)
1722 {
1723 tulip_media_info_t *mip = sc->tulip_mediainfo;
1724 int idx, cnt = 0;
1725
1726 TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT|TULIP_CMD_MUSTBEONE);
1727 TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
1728 DELAY(10); /* Wait 10 microseconds (actually 50 PCI cycles but at
1729 33MHz that comes to two microseconds but wait a
1730 bit longer anyways) */
1731 TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT |
1732 TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1733 sc->tulip_gpinit = TULIP_GP_SMC_9332_PINS;
1734 sc->tulip_gpdata = TULIP_GP_SMC_9332_INIT;
1735 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_PINS|TULIP_GP_PINSET);
1736 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_INIT);
1737 DELAY(200000);
1738 for (idx = 1000; idx > 0; idx--) {
1739 u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
1740 if ((csr & (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) == (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) {
1741 if (++cnt > 100)
1742 break;
1743 } else if ((csr & TULIP_GP_SMC_9332_OK10) == 0) {
1744 break;
1745 } else {
1746 cnt = 0;
1747 }
1748 DELAY(1000);
1749 }
1750 sc->tulip_media = cnt > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
1751 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1752 TULIP_GP_SMC_9332_INIT,
1753 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1754 |TULIP_CMD_SCRAMBLER);
1755 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1756 TULIP_GP_SMC_9332_INIT,
1757 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1758 |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1759 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1760 TULIP_GP_SMC_9332_INIT,
1761 TULIP_CMD_TXTHRSHLDCTL);
1762 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1763 TULIP_GP_SMC_9332_INIT,
1764 TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1765 }
1766
1767 static const tulip_boardsw_t tulip_21140_smc9332_boardsw = {
1768 TULIP_21140_SMC_9332,
1769 tulip_21140_smc9332_media_probe,
1770 tulip_media_select,
1771 tulip_null_media_poll,
1772 tulip_2114x_media_preset,
1773 };
1774
1775 static void
tulip_21140_cogent_em100_media_probe(tulip_softc_t * const sc)1776 tulip_21140_cogent_em100_media_probe(
1777 tulip_softc_t * const sc)
1778 {
1779 tulip_media_info_t *mip = sc->tulip_mediainfo;
1780 u_int32_t cmdmode = TULIP_CSR_READ(sc, csr_command);
1781
1782 sc->tulip_gpinit = TULIP_GP_EM100_PINS;
1783 sc->tulip_gpdata = TULIP_GP_EM100_INIT;
1784 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_PINS);
1785 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_INIT);
1786
1787 cmdmode = TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_MUSTBEONE;
1788 cmdmode &= ~(TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_SCRAMBLER);
1789 if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
1790 TULIP_CSR_WRITE(sc, csr_command, cmdmode);
1791 sc->tulip_media = TULIP_MEDIA_100BASEFX;
1792
1793 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX,
1794 TULIP_GP_EM100_INIT,
1795 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION);
1796 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX_FD,
1797 TULIP_GP_EM100_INIT,
1798 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1799 |TULIP_CMD_FULLDUPLEX);
1800 } else {
1801 TULIP_CSR_WRITE(sc, csr_command, cmdmode|TULIP_CMD_SCRAMBLER);
1802 sc->tulip_media = TULIP_MEDIA_100BASETX;
1803 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1804 TULIP_GP_EM100_INIT,
1805 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1806 |TULIP_CMD_SCRAMBLER);
1807 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1808 TULIP_GP_EM100_INIT,
1809 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1810 |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1811 }
1812 }
1813
1814 static const tulip_boardsw_t tulip_21140_cogent_em100_boardsw = {
1815 TULIP_21140_COGENT_EM100,
1816 tulip_21140_cogent_em100_media_probe,
1817 tulip_media_select,
1818 tulip_null_media_poll,
1819 tulip_2114x_media_preset
1820 };
1821
1822 static void
tulip_21140_znyx_zx34x_media_probe(tulip_softc_t * const sc)1823 tulip_21140_znyx_zx34x_media_probe(
1824 tulip_softc_t * const sc)
1825 {
1826 tulip_media_info_t *mip = sc->tulip_mediainfo;
1827 int cnt10 = 0, cnt100 = 0, idx;
1828
1829 sc->tulip_gpinit = TULIP_GP_ZX34X_PINS;
1830 sc->tulip_gpdata = TULIP_GP_ZX34X_INIT;
1831 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_PINS);
1832 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_INIT);
1833 TULIP_CSR_WRITE(sc, csr_command,
1834 TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1835 TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1836 TULIP_CSR_WRITE(sc, csr_command,
1837 TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1838
1839 DELAY(200000);
1840 for (idx = 1000; idx > 0; idx--) {
1841 u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
1842 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)) {
1843 if (++cnt100 > 100)
1844 break;
1845 } else if ((csr & TULIP_GP_ZX34X_LNKFAIL) == 0) {
1846 if (++cnt10 > 100)
1847 break;
1848 } else {
1849 cnt10 = 0;
1850 cnt100 = 0;
1851 }
1852 DELAY(1000);
1853 }
1854 sc->tulip_media = cnt100 > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
1855 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
1856 TULIP_GP_ZX34X_INIT,
1857 TULIP_CMD_TXTHRSHLDCTL);
1858 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
1859 TULIP_GP_ZX34X_INIT,
1860 TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
1861 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
1862 TULIP_GP_ZX34X_INIT,
1863 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1864 |TULIP_CMD_SCRAMBLER);
1865 tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
1866 TULIP_GP_ZX34X_INIT,
1867 TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1868 |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
1869 }
1870
1871 static const tulip_boardsw_t tulip_21140_znyx_zx34x_boardsw = {
1872 TULIP_21140_ZNYX_ZX34X,
1873 tulip_21140_znyx_zx34x_media_probe,
1874 tulip_media_select,
1875 tulip_null_media_poll,
1876 tulip_2114x_media_preset,
1877 };
1878
1879 static void
tulip_2114x_media_probe(tulip_softc_t * const sc)1880 tulip_2114x_media_probe(
1881 tulip_softc_t * const sc)
1882 {
1883 sc->tulip_cmdmode |= TULIP_CMD_MUSTBEONE
1884 |TULIP_CMD_BACKOFFCTR|TULIP_CMD_THRSHLD72;
1885 }
1886
1887 static const tulip_boardsw_t tulip_2114x_isv_boardsw = {
1888 TULIP_21140_ISV,
1889 tulip_2114x_media_probe,
1890 tulip_media_select,
1891 tulip_media_poll,
1892 tulip_2114x_media_preset,
1893 };
1894
1895 /*
1896 * ******** END of chip-specific handlers. ***********
1897 */
1898
1899 /*
1900 * Code the read the SROM and MII bit streams (I2C)
1901 */
1902 static void
tulip_delay_300ns(tulip_softc_t * const sc)1903 tulip_delay_300ns(
1904 tulip_softc_t * const sc)
1905 {
1906 int idx;
1907 for (idx = (300 / 33) + 1; idx > 0; idx--)
1908 (void) TULIP_CSR_READ(sc, csr_busmode);
1909 }
1910
1911 #define EMIT do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); tulip_delay_300ns(sc); } while (0)
1912
1913 static void
tulip_srom_idle(tulip_softc_t * const sc)1914 tulip_srom_idle(
1915 tulip_softc_t * const sc)
1916 {
1917 unsigned bit, csr;
1918
1919 csr = SROMSEL ; EMIT;
1920 csr = SROMSEL | SROMRD; EMIT;
1921 csr ^= SROMCS; EMIT;
1922 csr ^= SROMCLKON; EMIT;
1923
1924 /*
1925 * Write 25 cycles of 0 which will force the SROM to be idle.
1926 */
1927 for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
1928 csr ^= SROMCLKOFF; EMIT; /* clock low; data not valid */
1929 csr ^= SROMCLKON; EMIT; /* clock high; data valid */
1930 }
1931 csr ^= SROMCLKOFF; EMIT;
1932 csr ^= SROMCS; EMIT;
1933 csr = 0; EMIT;
1934 }
1935
1936
1937 static void
tulip_srom_read(tulip_softc_t * const sc)1938 tulip_srom_read(
1939 tulip_softc_t * const sc)
1940 {
1941 unsigned idx;
1942 const unsigned bitwidth = SROM_BITWIDTH;
1943 const unsigned cmdmask = (SROMCMD_RD << bitwidth);
1944 const unsigned msb = 1 << (bitwidth + 3 - 1);
1945 unsigned lastidx = (1 << bitwidth) - 1;
1946
1947 tulip_srom_idle(sc);
1948
1949 for (idx = 0; idx <= lastidx; idx++) {
1950 unsigned lastbit, data, bits, bit, csr;
1951 csr = SROMSEL ; EMIT;
1952 csr = SROMSEL | SROMRD; EMIT;
1953 csr ^= SROMCSON; EMIT;
1954 csr ^= SROMCLKON; EMIT;
1955
1956 lastbit = 0;
1957 for (bits = idx|cmdmask, bit = bitwidth + 3; bit > 0; bit--, bits <<= 1) {
1958 const unsigned thisbit = bits & msb;
1959 csr ^= SROMCLKOFF; EMIT; /* clock low; data not valid */
1960 if (thisbit != lastbit) {
1961 csr ^= SROMDOUT; EMIT; /* clock low; invert data */
1962 } else {
1963 EMIT;
1964 }
1965 csr ^= SROMCLKON; EMIT; /* clock high; data valid */
1966 lastbit = thisbit;
1967 }
1968 csr ^= SROMCLKOFF; EMIT;
1969
1970 for (data = 0, bits = 0; bits < 16; bits++) {
1971 data <<= 1;
1972 csr ^= SROMCLKON; EMIT; /* clock high; data valid */
1973 data |= TULIP_CSR_READ(sc, csr_srom_mii) & SROMDIN ? 1 : 0;
1974 csr ^= SROMCLKOFF; EMIT; /* clock low; data not valid */
1975 }
1976 sc->tulip_rombuf[idx*2] = data & 0xFF;
1977 sc->tulip_rombuf[idx*2+1] = data >> 8;
1978 csr = SROMSEL | SROMRD; EMIT;
1979 csr = 0; EMIT;
1980 }
1981 tulip_srom_idle(sc);
1982 }
1983
1984 #define MII_EMIT do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); tulip_delay_300ns(sc); } while (0)
1985
1986 static void
tulip_mii_writebits(tulip_softc_t * const sc,unsigned data,unsigned bits)1987 tulip_mii_writebits(
1988 tulip_softc_t * const sc,
1989 unsigned data,
1990 unsigned bits)
1991 {
1992 unsigned msb = 1 << (bits - 1);
1993 unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
1994 unsigned lastbit = (csr & MII_DOUT) ? msb : 0;
1995
1996 csr |= MII_WR; MII_EMIT; /* clock low; assert write */
1997
1998 for (; bits > 0; bits--, data <<= 1) {
1999 const unsigned thisbit = data & msb;
2000 if (thisbit != lastbit) {
2001 csr ^= MII_DOUT; MII_EMIT; /* clock low; invert data */
2002 }
2003 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
2004 lastbit = thisbit;
2005 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
2006 }
2007 }
2008
2009 static void
tulip_mii_turnaround(tulip_softc_t * const sc,unsigned cmd)2010 tulip_mii_turnaround(
2011 tulip_softc_t * const sc,
2012 unsigned cmd)
2013 {
2014 unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2015
2016 if (cmd == MII_WRCMD) {
2017 csr |= MII_DOUT; MII_EMIT; /* clock low; change data */
2018 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
2019 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
2020 csr ^= MII_DOUT; MII_EMIT; /* clock low; change data */
2021 } else {
2022 csr |= MII_RD; MII_EMIT; /* clock low; switch to read */
2023 }
2024 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
2025 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
2026 }
2027
2028 static unsigned
tulip_mii_readbits(tulip_softc_t * const sc)2029 tulip_mii_readbits(
2030 tulip_softc_t * const sc)
2031 {
2032 unsigned data;
2033 unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2034 int idx;
2035
2036 for (idx = 0, data = 0; idx < 16; idx++) {
2037 data <<= 1; /* this is NOOP on the first pass through */
2038 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
2039 if (TULIP_CSR_READ(sc, csr_srom_mii) & MII_DIN)
2040 data |= 1;
2041 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
2042 }
2043 csr ^= MII_RD; MII_EMIT; /* clock low; turn off read */
2044
2045 return data;
2046 }
2047
2048 static unsigned
tulip_mii_readreg(tulip_softc_t * const sc,unsigned devaddr,unsigned regno)2049 tulip_mii_readreg(
2050 tulip_softc_t * const sc,
2051 unsigned devaddr,
2052 unsigned regno)
2053 {
2054 unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2055 unsigned data;
2056
2057 csr &= ~(MII_RD|MII_CLK); MII_EMIT;
2058 tulip_mii_writebits(sc, MII_PREAMBLE, 32);
2059 tulip_mii_writebits(sc, MII_RDCMD, 8);
2060 tulip_mii_writebits(sc, devaddr, 5);
2061 tulip_mii_writebits(sc, regno, 5);
2062 tulip_mii_turnaround(sc, MII_RDCMD);
2063
2064 data = tulip_mii_readbits(sc);
2065 #if defined(TULIP_DEBUG)
2066 sc->tulip_dbg.dbg_phyregs[regno][0] = data;
2067 sc->tulip_dbg.dbg_phyregs[regno][1]++;
2068 #endif
2069 return data;
2070 }
2071
2072 static void
tulip_mii_writereg(tulip_softc_t * const sc,unsigned devaddr,unsigned regno,unsigned data)2073 tulip_mii_writereg(
2074 tulip_softc_t * const sc,
2075 unsigned devaddr,
2076 unsigned regno,
2077 unsigned data)
2078 {
2079 unsigned csr;
2080
2081 csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2082 csr &= ~(MII_RD|MII_CLK); MII_EMIT;
2083 tulip_mii_writebits(sc, MII_PREAMBLE, 32);
2084 tulip_mii_writebits(sc, MII_WRCMD, 8);
2085 tulip_mii_writebits(sc, devaddr, 5);
2086 tulip_mii_writebits(sc, regno, 5);
2087 tulip_mii_turnaround(sc, MII_WRCMD);
2088 tulip_mii_writebits(sc, data, 16);
2089 #if defined(TULIP_DEBUG)
2090 sc->tulip_dbg.dbg_phyregs[regno][2] = data;
2091 sc->tulip_dbg.dbg_phyregs[regno][3]++;
2092 #endif
2093 }
2094
2095 #define tulip_mchash(mca) (tulip_crc32(mca, 6) & 0x1FF)
2096 #define tulip_srom_crcok(databuf) ( \
2097 ((tulip_crc32(databuf, 126) & 0xFFFFU) ^ 0xFFFFU) == \
2098 ((databuf)[126] | ((databuf)[127] << 8)))
2099
2100 static unsigned
tulip_crc32(const unsigned char * databuf,size_t datalen)2101 tulip_crc32(
2102 const unsigned char *databuf,
2103 size_t datalen)
2104 {
2105 u_int idx, crc = 0xFFFFFFFFUL;
2106 static const u_int crctab[] = {
2107 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
2108 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
2109 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
2110 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
2111 };
2112
2113 for (idx = 0; idx < datalen; idx++) {
2114 crc ^= *databuf++;
2115 crc = (crc >> 4) ^ crctab[crc & 0xf];
2116 crc = (crc >> 4) ^ crctab[crc & 0xf];
2117 }
2118
2119 return crc;
2120 }
2121
2122 static void
tulip_identify_dec_nic(tulip_softc_t * const sc)2123 tulip_identify_dec_nic(
2124 tulip_softc_t * const sc)
2125 {
2126 strlcpy(sc->tulip_boardid, "DEC ", sizeof sc->tulip_boardid);
2127 #define D0 4
2128 if (sc->tulip_chipid <= TULIP_DE425)
2129 return;
2130 if (bcmp(sc->tulip_rombuf + 29, "DE500", 5) == 0
2131 || bcmp(sc->tulip_rombuf + 29, "DE450", 5) == 0) {
2132 bcopy(sc->tulip_rombuf + 29, &sc->tulip_boardid[D0], 8);
2133 sc->tulip_boardid[D0+8] = ' ';
2134 }
2135 #undef D0
2136 }
2137
2138 static void
tulip_identify_znyx_nic(tulip_softc_t * const sc)2139 tulip_identify_znyx_nic(
2140 tulip_softc_t * const sc)
2141 {
2142 unsigned id = 0;
2143 strlcpy(sc->tulip_boardid, "ZNYX ZX3XX ", sizeof sc->tulip_boardid);
2144 if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
2145 unsigned znyx_ptr;
2146 sc->tulip_boardid[8] = '4';
2147 znyx_ptr = sc->tulip_rombuf[124] + 256 * sc->tulip_rombuf[125];
2148 if (znyx_ptr < 26 || znyx_ptr > 116) {
2149 sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2150 return;
2151 }
2152 /* ZX344 = 0010 .. 0013FF
2153 */
2154 if (sc->tulip_rombuf[znyx_ptr] == 0x4A
2155 && sc->tulip_rombuf[znyx_ptr + 1] == 0x52
2156 && sc->tulip_rombuf[znyx_ptr + 2] == 0x01) {
2157 id = sc->tulip_rombuf[znyx_ptr + 5] + 256 * sc->tulip_rombuf[znyx_ptr + 4];
2158 if ((id >> 8) == (TULIP_ZNYX_ID_ZX342 >> 8)) {
2159 sc->tulip_boardid[9] = '2';
2160 if (id == TULIP_ZNYX_ID_ZX342B) {
2161 sc->tulip_boardid[10] = 'B';
2162 sc->tulip_boardid[11] = ' ';
2163 }
2164 sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2165 } else if (id == TULIP_ZNYX_ID_ZX344) {
2166 sc->tulip_boardid[10] = '4';
2167 sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2168 } else if (id == TULIP_ZNYX_ID_ZX345) {
2169 sc->tulip_boardid[9] = (sc->tulip_rombuf[19] > 1) ? '8' : '5';
2170 } else if (id == TULIP_ZNYX_ID_ZX346) {
2171 sc->tulip_boardid[9] = '6';
2172 } else if (id == TULIP_ZNYX_ID_ZX351) {
2173 sc->tulip_boardid[8] = '5';
2174 sc->tulip_boardid[9] = '1';
2175 }
2176 }
2177 if (id == 0) {
2178 /*
2179 * Assume it's a ZX342...
2180 */
2181 sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
2182 }
2183 return;
2184 }
2185 sc->tulip_boardid[8] = '1';
2186 if (sc->tulip_chipid == TULIP_21041) {
2187 sc->tulip_boardid[10] = '1';
2188 return;
2189 }
2190 if (sc->tulip_rombuf[32] == 0x4A && sc->tulip_rombuf[33] == 0x52) {
2191 id = sc->tulip_rombuf[37] + 256 * sc->tulip_rombuf[36];
2192 if (id == TULIP_ZNYX_ID_ZX312T) {
2193 sc->tulip_boardid[9] = '2';
2194 sc->tulip_boardid[10] = 'T';
2195 sc->tulip_boardid[11] = ' ';
2196 sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2197 } else if (id == TULIP_ZNYX_ID_ZX314_INTA) {
2198 sc->tulip_boardid[9] = '4';
2199 sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2200 sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2201 } else if (id == TULIP_ZNYX_ID_ZX314) {
2202 sc->tulip_boardid[9] = '4';
2203 sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2204 sc->tulip_features |= TULIP_HAVE_BASEROM;
2205 } else if (id == TULIP_ZNYX_ID_ZX315_INTA) {
2206 sc->tulip_boardid[9] = '5';
2207 sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2208 } else if (id == TULIP_ZNYX_ID_ZX315) {
2209 sc->tulip_boardid[9] = '5';
2210 sc->tulip_features |= TULIP_HAVE_BASEROM;
2211 } else {
2212 id = 0;
2213 }
2214 }
2215 if (id == 0) {
2216 if ((sc->tulip_enaddr[3] & ~3) == 0xF0 && (sc->tulip_enaddr[5] & 3) == 0) {
2217 sc->tulip_boardid[9] = '4';
2218 sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2219 sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2220 } else if ((sc->tulip_enaddr[3] & ~3) == 0xF4 && (sc->tulip_enaddr[5] & 1) == 0) {
2221 sc->tulip_boardid[9] = '5';
2222 sc->tulip_boardsw = &tulip_21040_boardsw;
2223 sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2224 } else if ((sc->tulip_enaddr[3] & ~3) == 0xEC) {
2225 sc->tulip_boardid[9] = '2';
2226 sc->tulip_boardsw = &tulip_21040_boardsw;
2227 }
2228 }
2229 }
2230
2231 static void
tulip_identify_smc_nic(tulip_softc_t * const sc)2232 tulip_identify_smc_nic(
2233 tulip_softc_t * const sc)
2234 {
2235 u_int32_t id1, id2, ei;
2236 int auibnc = 0, utp = 0;
2237 char *cp;
2238
2239 strlcpy(sc->tulip_boardid, "SMC ", sizeof sc->tulip_boardid);
2240 if (sc->tulip_chipid == TULIP_21041)
2241 return;
2242 if (sc->tulip_chipid != TULIP_21040) {
2243 if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
2244 strlcat(sc->tulip_boardid, "9332DST ", sizeof sc->tulip_boardid);
2245 sc->tulip_boardsw = &tulip_21140_smc9332_boardsw;
2246 } else if (sc->tulip_features & (TULIP_HAVE_BASEROM|TULIP_HAVE_SLAVEDROM)) {
2247 strlcat(sc->tulip_boardid, "9334BDT ", sizeof sc->tulip_boardid);
2248 } else {
2249 strlcat(sc->tulip_boardid, "9332BDT ", sizeof sc->tulip_boardid);
2250 }
2251 return;
2252 }
2253 id1 = sc->tulip_rombuf[0x60] | (sc->tulip_rombuf[0x61] << 8);
2254 id2 = sc->tulip_rombuf[0x62] | (sc->tulip_rombuf[0x63] << 8);
2255 ei = sc->tulip_rombuf[0x66] | (sc->tulip_rombuf[0x67] << 8);
2256
2257 strlcat(sc->tulip_boardid, "8432", sizeof sc->tulip_boardid);
2258 cp = &sc->tulip_boardid[8];
2259 if ((id1 & 1) == 0)
2260 *cp++ = 'B', auibnc = 1;
2261 if ((id1 & 0xFF) > 0x32)
2262 *cp++ = 'T', utp = 1;
2263 if ((id1 & 0x4000) == 0)
2264 *cp++ = 'A', auibnc = 1;
2265 if (id2 == 0x15) {
2266 sc->tulip_boardid[7] = '4';
2267 *cp++ = '-';
2268 *cp++ = 'C';
2269 *cp++ = 'H';
2270 *cp++ = (ei ? '2' : '1');
2271 }
2272 *cp++ = ' ';
2273 *cp = '\0';
2274 if (utp && !auibnc)
2275 sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
2276 else if (!utp && auibnc)
2277 sc->tulip_boardsw = &tulip_21040_auibnc_only_boardsw;
2278 }
2279
2280 static void
tulip_identify_cogent_nic(tulip_softc_t * const sc)2281 tulip_identify_cogent_nic(
2282 tulip_softc_t * const sc)
2283 {
2284 strlcpy(sc->tulip_boardid, "Cogent ", sizeof sc->tulip_boardid);
2285 if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
2286 if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100TX_ID) {
2287 strlcat(sc->tulip_boardid, "EM100TX ", sizeof sc->tulip_boardid);
2288 sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
2289 #if defined(TULIP_COGENT_EM110TX_ID)
2290 } else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM110TX_ID) {
2291 strlcat(sc->tulip_boardid, "EM110TX ", sizeof sc->tulip_boardid);
2292 sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
2293 #endif
2294 } else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
2295 strlcat(sc->tulip_boardid, "EM100FX ", sizeof sc->tulip_boardid);
2296 sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
2297 }
2298 /*
2299 * Magic number (0x24001109U) is the SubVendor (0x2400) and
2300 * SubDevId (0x1109) for the ANA6944TX (EM440TX).
2301 */
2302 if (*(u_int32_t *) sc->tulip_rombuf == 0x24001109U
2303 && (sc->tulip_features & TULIP_HAVE_BASEROM)) {
2304 /*
2305 * Cogent (Adaptec) is still mapping all INTs to INTA of
2306 * first 21140. Dumb! Dumb!
2307 */
2308 strlcat(sc->tulip_boardid, "EM440TX ", sizeof sc->tulip_boardid);
2309 sc->tulip_features |= TULIP_HAVE_SHAREDINTR;
2310 }
2311 } else if (sc->tulip_chipid == TULIP_21040) {
2312 sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
2313 }
2314 }
2315
2316 static void
tulip_identify_accton_nic(tulip_softc_t * const sc)2317 tulip_identify_accton_nic(
2318 tulip_softc_t * const sc)
2319 {
2320 strlcpy(sc->tulip_boardid, "ACCTON ", sizeof sc->tulip_boardid);
2321 switch (sc->tulip_chipid) {
2322 case TULIP_21140A:
2323 strlcat(sc->tulip_boardid, "EN1207 ", sizeof sc->tulip_boardid);
2324 if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
2325 sc->tulip_boardsw = &tulip_21140_accton_boardsw;
2326 break;
2327 case TULIP_21140:
2328 strlcat(sc->tulip_boardid, "EN1207TX ", sizeof sc->tulip_boardid);
2329 if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
2330 sc->tulip_boardsw = &tulip_21140_eb_boardsw;
2331 break;
2332 case TULIP_21040:
2333 strlcat(sc->tulip_boardid, "EN1203 ", sizeof sc->tulip_boardid);
2334 sc->tulip_boardsw = &tulip_21040_boardsw;
2335 break;
2336 case TULIP_21041:
2337 strlcat(sc->tulip_boardid, "EN1203 ", sizeof sc->tulip_boardid);
2338 sc->tulip_boardsw = &tulip_21041_boardsw;
2339 break;
2340 default:
2341 sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2342 break;
2343 }
2344 }
2345
2346 static void
tulip_identify_asante_nic(tulip_softc_t * const sc)2347 tulip_identify_asante_nic(
2348 tulip_softc_t * const sc)
2349 {
2350 strlcpy(sc->tulip_boardid, "Asante ", sizeof sc->tulip_boardid);
2351 if ((sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A)
2352 && sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
2353 tulip_media_info_t *mi = sc->tulip_mediainfo;
2354 int idx;
2355 /*
2356 * The Asante Fast Ethernet doesn't always ship with a valid
2357 * new format SROM. So if isn't in the new format, we cheat
2358 * set it up as if we had.
2359 */
2360
2361 sc->tulip_gpinit = TULIP_GP_ASANTE_PINS;
2362 sc->tulip_gpdata = 0;
2363
2364 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PINS|TULIP_GP_PINSET);
2365 TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PHYRESET);
2366 DELAY(100);
2367 TULIP_CSR_WRITE(sc, csr_gp, 0);
2368
2369 mi->mi_type = TULIP_MEDIAINFO_MII;
2370 mi->mi_gpr_length = 0;
2371 mi->mi_gpr_offset = 0;
2372 mi->mi_reset_length = 0;
2373 mi->mi_reset_offset = 0;
2374
2375 mi->mi_phyaddr = TULIP_MII_NOPHY;
2376 for (idx = 20; idx > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx--) {
2377 DELAY(10000);
2378 mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, 0);
2379 }
2380 if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
2381 printf(TULIP_PRINTF_FMT ": can't find phy 0\n", TULIP_PRINTF_ARGS);
2382 return;
2383 }
2384
2385 sc->tulip_features |= TULIP_HAVE_MII;
2386 mi->mi_capabilities = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
2387 mi->mi_advertisement = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
2388 mi->mi_full_duplex = PHYSTS_10BASET_FD|PHYSTS_100BASETX_FD;
2389 mi->mi_tx_threshold = PHYSTS_10BASET|PHYSTS_10BASET_FD;
2390 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2391 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2392 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2393 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2394 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2395 mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2396 tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2397
2398 sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2399 }
2400 }
2401
2402 static int
tulip_srom_decode(tulip_softc_t * const sc)2403 tulip_srom_decode(
2404 tulip_softc_t * const sc)
2405 {
2406 unsigned idx1, idx2, idx3;
2407
2408 const tulip_srom_header_t *shp = (tulip_srom_header_t *) &sc->tulip_rombuf[0];
2409 const tulip_srom_adapter_info_t *saip = (tulip_srom_adapter_info_t *) (shp + 1);
2410 tulip_srom_media_t srom_media;
2411 tulip_media_info_t *mi = sc->tulip_mediainfo;
2412 const u_int8_t *dp;
2413 u_int32_t leaf_offset, blocks, data;
2414
2415 for (idx1 = 0; idx1 < shp->sh_adapter_count; idx1++, saip++) {
2416 if (shp->sh_adapter_count == 1)
2417 break;
2418 if (saip->sai_device == sc->tulip_pci_devno)
2419 break;
2420 }
2421 /*
2422 * Didn't find the right media block for this card.
2423 */
2424 if (idx1 == shp->sh_adapter_count)
2425 return 0;
2426
2427 /*
2428 * Save the hardware address.
2429 */
2430 bcopy((caddr_t) shp->sh_ieee802_address, (caddr_t) sc->tulip_enaddr, 6);
2431 /*
2432 * If this is a multiple port card, add the adapter index to the last
2433 * byte of the hardware address. (if it isn't multiport, adding 0
2434 * won't hurt.
2435 */
2436 sc->tulip_enaddr[5] += idx1;
2437
2438 leaf_offset = saip->sai_leaf_offset_lowbyte
2439 + saip->sai_leaf_offset_highbyte * 256;
2440 dp = sc->tulip_rombuf + leaf_offset;
2441
2442 sc->tulip_conntype = (tulip_srom_connection_t) (dp[0] + dp[1] * 256); dp += 2;
2443
2444 for (idx2 = 0;; idx2++) {
2445 if (tulip_srom_conninfo[idx2].sc_type == sc->tulip_conntype
2446 || tulip_srom_conninfo[idx2].sc_type == TULIP_SROM_CONNTYPE_NOT_USED)
2447 break;
2448 }
2449 sc->tulip_connidx = idx2;
2450
2451 if (sc->tulip_chipid == TULIP_21041) {
2452 blocks = *dp++;
2453 for (idx2 = 0; idx2 < blocks; idx2++) {
2454 tulip_media_t media;
2455 data = *dp++;
2456 srom_media = (tulip_srom_media_t) (data & 0x3F);
2457 for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2458 if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2459 break;
2460 }
2461 media = tulip_srom_mediums[idx3].sm_type;
2462 if (media != TULIP_MEDIA_UNKNOWN) {
2463 if (data & TULIP_SROM_21041_EXTENDED) {
2464 mi->mi_type = TULIP_MEDIAINFO_SIA;
2465 sc->tulip_mediums[media] = mi;
2466 mi->mi_sia_connectivity = dp[0] + dp[1] * 256;
2467 mi->mi_sia_tx_rx = dp[2] + dp[3] * 256;
2468 mi->mi_sia_general = dp[4] + dp[5] * 256;
2469 mi++;
2470 } else {
2471 switch (media) {
2472 case TULIP_MEDIA_BNC: {
2473 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC);
2474 mi++;
2475 break;
2476 }
2477 case TULIP_MEDIA_AUI: {
2478 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI);
2479 mi++;
2480 break;
2481 }
2482 case TULIP_MEDIA_10BASET: {
2483 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET);
2484 mi++;
2485 break;
2486 }
2487 case TULIP_MEDIA_10BASET_FD: {
2488 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD);
2489 mi++;
2490 break;
2491 }
2492 default: {
2493 break;
2494 }
2495 }
2496 }
2497 }
2498 if (data & TULIP_SROM_21041_EXTENDED)
2499 dp += 6;
2500 }
2501 #ifdef notdef
2502 if (blocks == 0) {
2503 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC); mi++;
2504 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI); mi++;
2505 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET); mi++;
2506 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD); mi++;
2507 }
2508 #endif
2509 } else {
2510 unsigned length, type;
2511 tulip_media_t gp_media = TULIP_MEDIA_UNKNOWN;
2512 if (sc->tulip_features & TULIP_HAVE_GPR)
2513 sc->tulip_gpinit = *dp++;
2514 blocks = *dp++;
2515 for (idx2 = 0; idx2 < blocks; idx2++) {
2516 const u_int8_t *ep;
2517 if ((*dp & 0x80) == 0) {
2518 length = 4;
2519 type = 0;
2520 } else {
2521 length = (*dp++ & 0x7f) - 1;
2522 type = *dp++ & 0x3f;
2523 }
2524 ep = dp + length;
2525 switch (type & 0x3f) {
2526 case 0: { /* 21140[A] GPR block */
2527 tulip_media_t media;
2528 srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
2529 for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2530 if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2531 break;
2532 }
2533 media = tulip_srom_mediums[idx3].sm_type;
2534 if (media == TULIP_MEDIA_UNKNOWN)
2535 break;
2536 mi->mi_type = TULIP_MEDIAINFO_GPR;
2537 sc->tulip_mediums[media] = mi;
2538 mi->mi_gpdata = dp[1];
2539 if (media > gp_media && !TULIP_IS_MEDIA_FD(media)) {
2540 sc->tulip_gpdata = mi->mi_gpdata;
2541 gp_media = media;
2542 }
2543 data = dp[2] + dp[3] * 256;
2544 mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
2545 if (data & TULIP_SROM_2114X_NOINDICATOR) {
2546 mi->mi_actmask = 0;
2547 } else {
2548 #if 0
2549 mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
2550 #endif
2551 mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
2552 mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
2553 }
2554 mi++;
2555 break;
2556 }
2557 case 1: { /* 21140[A] MII block */
2558 const unsigned phyno = *dp++;
2559 mi->mi_type = TULIP_MEDIAINFO_MII;
2560 mi->mi_gpr_length = *dp++;
2561 mi->mi_gpr_offset = dp - sc->tulip_rombuf;
2562 dp += mi->mi_gpr_length;
2563 mi->mi_reset_length = *dp++;
2564 mi->mi_reset_offset = dp - sc->tulip_rombuf;
2565 dp += mi->mi_reset_length;
2566
2567 /*
2568 * Before we probe for a PHY, use the GPR information
2569 * to select it. If we don't, it may be inaccessible.
2570 */
2571 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpinit|TULIP_GP_PINSET);
2572 for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++) {
2573 DELAY(10);
2574 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx3]);
2575 }
2576 sc->tulip_phyaddr = mi->mi_phyaddr;
2577 for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++) {
2578 DELAY(10);
2579 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx3]);
2580 }
2581
2582 /*
2583 * At least write something!
2584 */
2585 if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
2586 TULIP_CSR_WRITE(sc, csr_gp, 0);
2587
2588 mi->mi_phyaddr = TULIP_MII_NOPHY;
2589 for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
2590 DELAY(10000);
2591 mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
2592 }
2593 if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
2594 #if defined(TULIP_DEBUG)
2595 printf(TULIP_PRINTF_FMT ": can't find phy %d\n",
2596 TULIP_PRINTF_ARGS, phyno);
2597 #endif
2598 break;
2599 }
2600 sc->tulip_features |= TULIP_HAVE_MII;
2601 mi->mi_capabilities = dp[0] + dp[1] * 256; dp += 2;
2602 mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
2603 mi->mi_full_duplex = dp[0] + dp[1] * 256; dp += 2;
2604 mi->mi_tx_threshold = dp[0] + dp[1] * 256; dp += 2;
2605 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2606 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2607 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2608 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2609 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2610 mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2611 tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2612 mi++;
2613 break;
2614 }
2615 case 2: { /* 2114[23] SIA block */
2616 tulip_media_t media;
2617 srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
2618 for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2619 if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2620 break;
2621 }
2622 media = tulip_srom_mediums[idx3].sm_type;
2623 if (media == TULIP_MEDIA_UNKNOWN)
2624 break;
2625 mi->mi_type = TULIP_MEDIAINFO_SIA;
2626 sc->tulip_mediums[media] = mi;
2627 if (dp[0] & 0x40) {
2628 mi->mi_sia_connectivity = dp[1] + dp[2] * 256;
2629 mi->mi_sia_tx_rx = dp[3] + dp[4] * 256;
2630 mi->mi_sia_general = dp[5] + dp[6] * 256;
2631 dp += 6;
2632 } else {
2633 switch (media) {
2634 case TULIP_MEDIA_BNC: {
2635 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, BNC);
2636 break;
2637 }
2638 case TULIP_MEDIA_AUI: {
2639 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, AUI);
2640 break;
2641 }
2642 case TULIP_MEDIA_10BASET: {
2643 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET);
2644 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2645 break;
2646 }
2647 case TULIP_MEDIA_10BASET_FD: {
2648 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET_FD);
2649 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2650 break;
2651 }
2652 default: {
2653 goto bad_media;
2654 }
2655 }
2656 }
2657 mi->mi_sia_gp_control = (dp[1] + dp[2] * 256) << 16;
2658 mi->mi_sia_gp_data = (dp[3] + dp[4] * 256) << 16;
2659 mi++;
2660 bad_media:
2661 break;
2662 }
2663 case 3: { /* 2114[23] MII PHY block */
2664 const unsigned phyno = *dp++;
2665 const u_int8_t *dp0;
2666 mi->mi_type = TULIP_MEDIAINFO_MII;
2667 mi->mi_gpr_length = *dp++;
2668 mi->mi_gpr_offset = dp - sc->tulip_rombuf;
2669 dp += 2 * mi->mi_gpr_length;
2670 mi->mi_reset_length = *dp++;
2671 mi->mi_reset_offset = dp - sc->tulip_rombuf;
2672 dp += 2 * mi->mi_reset_length;
2673
2674 dp0 = &sc->tulip_rombuf[mi->mi_reset_offset];
2675 for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++, dp0 += 2) {
2676 DELAY(10);
2677 TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
2678 }
2679 sc->tulip_phyaddr = mi->mi_phyaddr;
2680 dp0 = &sc->tulip_rombuf[mi->mi_gpr_offset];
2681 for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++, dp0 += 2) {
2682 DELAY(10);
2683 TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
2684 }
2685
2686 if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
2687 TULIP_CSR_WRITE(sc, csr_sia_general, 0);
2688
2689 mi->mi_phyaddr = TULIP_MII_NOPHY;
2690 for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
2691 DELAY(10000);
2692 mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
2693 }
2694 if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
2695 #if defined(TULIP_DEBUG)
2696 printf(TULIP_PRINTF_FMT ": can't find phy %d\n",
2697 TULIP_PRINTF_ARGS, phyno);
2698 #endif
2699 break;
2700 }
2701 sc->tulip_features |= TULIP_HAVE_MII;
2702 mi->mi_capabilities = dp[0] + dp[1] * 256; dp += 2;
2703 mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
2704 mi->mi_full_duplex = dp[0] + dp[1] * 256; dp += 2;
2705 mi->mi_tx_threshold = dp[0] + dp[1] * 256; dp += 2;
2706 mi->mi_mii_interrupt = dp[0] + dp[1] * 256; dp += 2;
2707 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
2708 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
2709 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
2710 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
2711 TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
2712 mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
2713 tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
2714 mi++;
2715 break;
2716 }
2717 case 4: { /* 21143 SYM block */
2718 tulip_media_t media;
2719 srom_media = (tulip_srom_media_t) dp[0];
2720 for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
2721 if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
2722 break;
2723 }
2724 media = tulip_srom_mediums[idx3].sm_type;
2725 if (media == TULIP_MEDIA_UNKNOWN)
2726 break;
2727 mi->mi_type = TULIP_MEDIAINFO_SYM;
2728 sc->tulip_mediums[media] = mi;
2729 mi->mi_gpcontrol = (dp[1] + dp[2] * 256) << 16;
2730 mi->mi_gpdata = (dp[3] + dp[4] * 256) << 16;
2731 data = dp[5] + dp[6] * 256;
2732 mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
2733 if (data & TULIP_SROM_2114X_NOINDICATOR) {
2734 mi->mi_actmask = 0;
2735 } else {
2736 mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
2737 mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
2738 mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
2739 }
2740 if (TULIP_IS_MEDIA_TP(media))
2741 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
2742 mi++;
2743 break;
2744 }
2745 #if 0
2746 case 5: { /* 21143 Reset block */
2747 mi->mi_type = TULIP_MEDIAINFO_RESET;
2748 mi->mi_reset_length = *dp++;
2749 mi->mi_reset_offset = dp - sc->tulip_rombuf;
2750 dp += 2 * mi->mi_reset_length;
2751 mi++;
2752 break;
2753 }
2754 #endif
2755 default: {
2756 }
2757 }
2758 dp = ep;
2759 }
2760 }
2761 return mi - sc->tulip_mediainfo;
2762 }
2763
2764 static const struct {
2765 void (*vendor_identify_nic)(tulip_softc_t * const sc);
2766 unsigned char vendor_oui[3];
2767 } tulip_vendors[] = {
2768 { tulip_identify_dec_nic, { 0x08, 0x00, 0x2B } },
2769 { tulip_identify_dec_nic, { 0x00, 0x00, 0xF8 } },
2770 { tulip_identify_smc_nic, { 0x00, 0x00, 0xC0 } },
2771 { tulip_identify_smc_nic, { 0x00, 0xE0, 0x29 } },
2772 { tulip_identify_znyx_nic, { 0x00, 0xC0, 0x95 } },
2773 { tulip_identify_cogent_nic, { 0x00, 0x00, 0x92 } },
2774 { tulip_identify_cogent_nic, { 0x00, 0x00, 0xD1 } },
2775 { tulip_identify_asante_nic, { 0x00, 0x00, 0x94 } },
2776 { tulip_identify_accton_nic, { 0x00, 0x00, 0xE8 } },
2777 { NULL }
2778 };
2779
2780 /*
2781 * This deals with the vagaries of the address roms and the
2782 * brain-deadness that various vendors commit in using them.
2783 */
2784 static int
tulip_read_macaddr(tulip_softc_t * const sc)2785 tulip_read_macaddr(
2786 tulip_softc_t * const sc)
2787 {
2788 unsigned cksum, rom_cksum, idx;
2789 u_int32_t csr;
2790 unsigned char tmpbuf[8];
2791 static const u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
2792
2793 sc->tulip_connidx = TULIP_SROM_LASTCONNIDX;
2794
2795 if (sc->tulip_chipid == TULIP_21040) {
2796 TULIP_CSR_WRITE(sc, csr_enetrom, 1);
2797 for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
2798 int cnt = 0;
2799 while (((csr = TULIP_CSR_READ(sc, csr_enetrom)) & 0x80000000L) && cnt < 10000)
2800 cnt++;
2801 sc->tulip_rombuf[idx] = csr & 0xFF;
2802 }
2803 sc->tulip_boardsw = &tulip_21040_boardsw;
2804 #if defined(TULIP_EISA)
2805 } else if (sc->tulip_chipid == TULIP_DE425) {
2806 int cnt;
2807 for (idx = 0, cnt = 0; idx < sizeof(testpat) && cnt < 32; cnt++) {
2808 tmpbuf[idx] = TULIP_CSR_READBYTE(sc, csr_enetrom);
2809 if (tmpbuf[idx] == testpat[idx])
2810 ++idx;
2811 else
2812 idx = 0;
2813 }
2814 for (idx = 0; idx < 32; idx++)
2815 sc->tulip_rombuf[idx] = TULIP_CSR_READBYTE(sc, csr_enetrom);
2816 sc->tulip_boardsw = &tulip_21040_boardsw;
2817 #endif /* TULIP_EISA */
2818 } else {
2819 if (sc->tulip_chipid == TULIP_21041) {
2820 /*
2821 * Thankfully all 21041's act the same.
2822 */
2823 sc->tulip_boardsw = &tulip_21041_boardsw;
2824 } else {
2825 /*
2826 * Assume all 21140 board are compatible with the
2827 * DEC 10/100 evaluation board. Not really valid but
2828 * it's the best we can do until every one switches to
2829 * the new SROM format.
2830 */
2831
2832 sc->tulip_boardsw = &tulip_21140_eb_boardsw;
2833 }
2834 #ifdef NEED_PCI_ETHER_HW_ADDR_FUNC
2835 if(pci_ether_hw_addr(sc->tulip_pc, (u_char *)(&sc->tulip_rombuf),
2836 sc->tulip_pci_busno, sc->tulip_pci_devno)) {
2837 if(sc->tulip_boardsw == &tulip_21041_boardsw)
2838 sc->tulip_boardsw = &tulip_21041np_boardsw;
2839 }
2840 else {
2841 tulip_srom_read(sc);
2842 }
2843 #else
2844 tulip_srom_read(sc);
2845 #endif
2846 if (tulip_srom_crcok(sc->tulip_rombuf)) {
2847 /*
2848 * SROM CRC is valid therefore it must be in the
2849 * new format.
2850 */
2851 sc->tulip_features |= TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM;
2852 } else if (sc->tulip_rombuf[126] == 0xff && sc->tulip_rombuf[127] == 0xFF) {
2853 /*
2854 * No checksum is present. See if the SROM id checks out;
2855 * the first 18 bytes should be 0 followed by a 1 followed
2856 * by the number of adapters (which we don't deal with yet).
2857 */
2858 for (idx = 0; idx < 18; idx++) {
2859 if (sc->tulip_rombuf[idx] != 0)
2860 break;
2861 }
2862 if (idx == 18 && sc->tulip_rombuf[18] == 1 && sc->tulip_rombuf[19] != 0)
2863 sc->tulip_features |= TULIP_HAVE_ISVSROM;
2864 } else if (sc->tulip_chipid >= TULIP_21142) {
2865 sc->tulip_features |= TULIP_HAVE_ISVSROM;
2866 sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2867 }
2868 if ((sc->tulip_features & TULIP_HAVE_ISVSROM) && tulip_srom_decode(sc)) {
2869 if (sc->tulip_chipid != TULIP_21041)
2870 sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
2871
2872 /*
2873 * If the SROM specifies more than one adapter, tag this as a
2874 * BASE rom.
2875 */
2876 if (sc->tulip_rombuf[19] > 1)
2877 sc->tulip_features |= TULIP_HAVE_BASEROM;
2878 if (sc->tulip_boardsw == NULL)
2879 return -6;
2880 goto check_oui;
2881 }
2882 }
2883
2884
2885 if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) {
2886 /*
2887 * Some folks don't use the standard ethernet rom format
2888 * but instead just put the address in the first 6 bytes
2889 * of the rom and let the rest be all 0xffs. (Can we say
2890 * ZNYX???) (well sometimes they put in a checksum so we'll
2891 * start at 8).
2892 */
2893 for (idx = 8; idx < 32; idx++) {
2894 if (sc->tulip_rombuf[idx] != 0xFF)
2895 return -4;
2896 }
2897 /*
2898 * Make sure the address is not multicast or locally assigned
2899 * that the OUI is not 00-00-00.
2900 */
2901 if ((sc->tulip_rombuf[0] & 3) != 0)
2902 return -4;
2903 if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0
2904 && sc->tulip_rombuf[2] == 0)
2905 return -4;
2906 bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
2907 sc->tulip_features |= TULIP_HAVE_OKROM;
2908 goto check_oui;
2909 } else {
2910 /*
2911 * A number of makers of multiport boards (ZNYX and Cogent)
2912 * only put on one address ROM on their 21040 boards. So
2913 * if the ROM is all zeros (or all 0xFFs), look at the
2914 * previous configured boards (as long as they are on the same
2915 * PCI bus and the bus number is non-zero) until we find the
2916 * master board with address ROM. We then use its address ROM
2917 * as the base for this board. (we add our relative board
2918 * to the last byte of its address).
2919 */
2920 for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
2921 if (sc->tulip_rombuf[idx] != 0 && sc->tulip_rombuf[idx] != 0xFF)
2922 break;
2923 }
2924 if (idx == sizeof(sc->tulip_rombuf)) {
2925 int root_unit;
2926 tulip_softc_t *root_sc = NULL;
2927 for (root_unit = sc->tulip_unit - 1; root_unit >= 0; root_unit--) {
2928 root_sc = TULIP_UNIT_TO_SOFTC(root_unit);
2929 if (root_sc == NULL || (root_sc->tulip_features & (TULIP_HAVE_OKROM|TULIP_HAVE_SLAVEDROM)) == TULIP_HAVE_OKROM)
2930 break;
2931 root_sc = NULL;
2932 }
2933 if (root_sc != NULL && (root_sc->tulip_features & TULIP_HAVE_BASEROM)
2934 && root_sc->tulip_chipid == sc->tulip_chipid
2935 && root_sc->tulip_pci_busno == sc->tulip_pci_busno) {
2936 sc->tulip_features |= TULIP_HAVE_SLAVEDROM;
2937 sc->tulip_boardsw = root_sc->tulip_boardsw;
2938 strlcpy(sc->tulip_boardid, root_sc->tulip_boardid,
2939 sizeof sc->tulip_boardid);
2940 if (sc->tulip_boardsw->bd_type == TULIP_21140_ISV) {
2941 bcopy(root_sc->tulip_rombuf, sc->tulip_rombuf,
2942 sizeof(sc->tulip_rombuf));
2943 if (!tulip_srom_decode(sc))
2944 return -5;
2945 } else {
2946 bcopy(root_sc->tulip_enaddr, sc->tulip_enaddr, 6);
2947 sc->tulip_enaddr[5] += sc->tulip_unit - root_sc->tulip_unit;
2948 }
2949 /*
2950 * Now for a truly disgusting kludge: all 4 21040s on
2951 * the ZX314 share the same INTA line so the mapping
2952 * setup by the BIOS on the PCI bridge is worthless.
2953 * Rather than reprogramming the value in the config
2954 * register, we will handle this internally.
2955 */
2956 if (root_sc->tulip_features & TULIP_HAVE_SHAREDINTR) {
2957 sc->tulip_slaves = root_sc->tulip_slaves;
2958 root_sc->tulip_slaves = sc;
2959 sc->tulip_features |= TULIP_HAVE_SLAVEDINTR;
2960 }
2961 return 0;
2962 }
2963 }
2964 }
2965
2966 /*
2967 * This is the standard DEC address ROM test.
2968 */
2969
2970 if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0)
2971 return -3;
2972
2973 tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14];
2974 tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12];
2975 tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10];
2976 tmpbuf[6] = sc->tulip_rombuf[9]; tmpbuf[7] = sc->tulip_rombuf[8];
2977 if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0)
2978 return -2;
2979
2980 bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
2981
2982 cksum = *(u_int16_t *) &sc->tulip_enaddr[0];
2983 cksum *= 2;
2984 if (cksum > 65535) cksum -= 65535;
2985 cksum += *(u_int16_t *) &sc->tulip_enaddr[2];
2986 if (cksum > 65535) cksum -= 65535;
2987 cksum *= 2;
2988 if (cksum > 65535) cksum -= 65535;
2989 cksum += *(u_int16_t *) &sc->tulip_enaddr[4];
2990 if (cksum >= 65535) cksum -= 65535;
2991
2992 rom_cksum = *(u_int16_t *) &sc->tulip_rombuf[6];
2993
2994 if (cksum != rom_cksum)
2995 return -1;
2996
2997 check_oui:
2998 /*
2999 * Check for various boards based on OUI. Did I say braindead?
3000 */
3001 for (idx = 0; tulip_vendors[idx].vendor_identify_nic != NULL; idx++) {
3002 if (bcmp((caddr_t) sc->tulip_enaddr,
3003 (caddr_t) tulip_vendors[idx].vendor_oui, 3) == 0) {
3004 (*tulip_vendors[idx].vendor_identify_nic)(sc);
3005 break;
3006 }
3007 }
3008
3009 sc->tulip_features |= TULIP_HAVE_OKROM;
3010 return 0;
3011 }
3012
3013 #if defined(IFM_ETHER)
3014 static void
tulip_ifmedia_add(tulip_softc_t * const sc)3015 tulip_ifmedia_add(
3016 tulip_softc_t * const sc)
3017 {
3018 tulip_media_t media;
3019 int medias = 0;
3020
3021 for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
3022 if (sc->tulip_mediums[media] != NULL) {
3023 ifmedia_add(&sc->tulip_ifmedia, tulip_media_to_ifmedia[media],
3024 0, 0);
3025 medias++;
3026 }
3027 }
3028 if (medias == 0) {
3029 sc->tulip_features |= TULIP_HAVE_NOMEDIA;
3030 ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE, 0, 0);
3031 ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE);
3032 } else if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
3033 ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO, 0, 0);
3034 ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO);
3035 } else {
3036 ifmedia_set(&sc->tulip_ifmedia, tulip_media_to_ifmedia[sc->tulip_media]);
3037 sc->tulip_flags |= TULIP_PRINTMEDIA;
3038 tulip_linkup(sc, sc->tulip_media);
3039 }
3040 }
3041
3042 static int
tulip_ifmedia_change(struct ifnet * const ifp)3043 tulip_ifmedia_change(
3044 struct ifnet * const ifp)
3045 {
3046 tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
3047
3048 sc->tulip_flags |= TULIP_NEEDRESET;
3049 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
3050 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
3051 if (IFM_SUBTYPE(sc->tulip_ifmedia.ifm_media) != IFM_AUTO) {
3052 tulip_media_t media;
3053 for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
3054 if (sc->tulip_mediums[media] != NULL
3055 && sc->tulip_ifmedia.ifm_media == tulip_media_to_ifmedia[media]) {
3056 sc->tulip_flags |= TULIP_PRINTMEDIA;
3057 sc->tulip_flags &= ~TULIP_DIDNWAY;
3058 tulip_linkup(sc, media);
3059 return 0;
3060 }
3061 }
3062 }
3063 sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_WANTRXACT);
3064 tulip_reset(sc);
3065 tulip_init(sc);
3066 return 0;
3067 }
3068
3069 /*
3070 * Media status callback
3071 */
3072 static void
tulip_ifmedia_status(struct ifnet * const ifp,struct ifmediareq * req)3073 tulip_ifmedia_status(
3074 struct ifnet * const ifp,
3075 struct ifmediareq *req)
3076 {
3077 tulip_softc_t *sc = TULIP_IFP_TO_SOFTC(ifp);
3078
3079 #if defined(__bsdi__)
3080 if (sc->tulip_mii.mii_instance != 0) {
3081 mii_pollstat(&sc->tulip_mii);
3082 req->ifm_active = sc->tulip_mii.mii_media_active;
3083 req->ifm_status = sc->tulip_mii.mii_media_status;
3084 return;
3085 }
3086 #endif
3087 if (sc->tulip_media == TULIP_MEDIA_UNKNOWN)
3088 return;
3089
3090 req->ifm_status = IFM_AVALID;
3091 if (sc->tulip_flags & TULIP_LINKUP)
3092 req->ifm_status |= IFM_ACTIVE;
3093
3094 req->ifm_active = tulip_media_to_ifmedia[sc->tulip_media];
3095 }
3096 #endif
3097
3098 static void
tulip_addr_filter(tulip_softc_t * const sc)3099 tulip_addr_filter(
3100 tulip_softc_t * const sc)
3101 {
3102 struct ether_multistep step;
3103 struct ether_multi *enm;
3104
3105 sc->tulip_flags &= ~(TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY|TULIP_ALLMULTI);
3106 sc->tulip_flags |= TULIP_WANTSETUP|TULIP_WANTTXSTART;
3107 sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
3108 sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
3109 #if defined(IFF_ALLMULTI)
3110 sc->tulip_if.if_flags &= ~IFF_ALLMULTI;
3111 #endif
3112 sc->tulip_if.if_start = tulip_ifstart; /* so the setup packet gets queued */
3113 if (sc->tulip_multicnt > 14) {
3114 u_int32_t *sp = sc->tulip_setupdata;
3115 unsigned hash;
3116 /*
3117 * Some early passes of the 21140 have broken implementations of
3118 * hash-perfect mode. When we get too many multicasts for perfect
3119 * filtering with these chips, we need to switch into hash-only
3120 * mode (this is better than all-multicast on network with lots
3121 * of multicast traffic).
3122 */
3123 if (sc->tulip_features & TULIP_HAVE_BROKEN_HASH)
3124 sc->tulip_flags |= TULIP_WANTHASHONLY;
3125 else
3126 sc->tulip_flags |= TULIP_WANTHASHPERFECT;
3127 /*
3128 * If we have more than 14 multicasts, we have
3129 * go into hash perfect mode (512 bit multicast
3130 * hash and one perfect hardware).
3131 */
3132 bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata));
3133 ETHER_FIRST_MULTI(step, TULIP_ETHERCOM(sc), enm);
3134 while (enm != NULL) {
3135 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) {
3136 hash = tulip_mchash(enm->enm_addrlo);
3137 sp[hash >> 4] |= FILT_BO(1 << (hash & 0xF));
3138 } else {
3139 sc->tulip_flags |= TULIP_ALLMULTI;
3140 sc->tulip_flags &= ~(TULIP_WANTHASHONLY|TULIP_WANTHASHPERFECT);
3141 break;
3142 }
3143 ETHER_NEXT_MULTI(step, enm);
3144 }
3145 /*
3146 * No reason to use a hash if we are going to be
3147 * receiving every multicast.
3148 */
3149 if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
3150 hash = tulip_mchash(etherbroadcastaddr);
3151 sp[hash >> 4] |= FILT_BO(1 << (hash & 0xF));
3152 if (sc->tulip_flags & TULIP_WANTHASHONLY) {
3153 hash = tulip_mchash(sc->tulip_enaddr);
3154 sp[hash >> 4] |= FILT_BO(1 << (hash & 0xF));
3155 } else {
3156 sp[39] = FILT_BO(((u_int16_t *) sc->tulip_enaddr)[0]);
3157 sp[40] = FILT_BO(((u_int16_t *) sc->tulip_enaddr)[1]);
3158 sp[41] = FILT_BO(((u_int16_t *) sc->tulip_enaddr)[2]);
3159 }
3160 }
3161 }
3162 if ((sc->tulip_flags & (TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY)) == 0) {
3163 u_int32_t *sp = sc->tulip_setupdata;
3164 int idx = 0;
3165 if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
3166 /*
3167 * Else can get perfect filtering for 16 addresses.
3168 */
3169 ETHER_FIRST_MULTI(step, TULIP_ETHERCOM(sc), enm);
3170 for (; enm != NULL; idx++) {
3171 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) {
3172 *sp++ = FILT_BO(((u_int16_t *) enm->enm_addrlo)[0]);
3173 *sp++ = FILT_BO(((u_int16_t *) enm->enm_addrlo)[1]);
3174 *sp++ = FILT_BO(((u_int16_t *) enm->enm_addrlo)[2]);
3175 } else {
3176 sc->tulip_flags |= TULIP_ALLMULTI;
3177 break;
3178 }
3179 ETHER_NEXT_MULTI(step, enm);
3180 }
3181 /*
3182 * Add the broadcast address.
3183 */
3184 idx++;
3185 *sp++ = FILT_BO(0xFFFF);
3186 *sp++ = FILT_BO(0xFFFF);
3187 *sp++ = FILT_BO(0xFFFF);
3188 }
3189 /*
3190 * Pad the rest with our hardware address
3191 */
3192 for (; idx < 16; idx++) {
3193 *sp++ = FILT_BO(((u_int16_t *) sc->tulip_enaddr)[0]);
3194 *sp++ = FILT_BO(((u_int16_t *) sc->tulip_enaddr)[1]);
3195 *sp++ = FILT_BO(((u_int16_t *) sc->tulip_enaddr)[2]);
3196 }
3197 }
3198 #if defined(IFF_ALLMULTI)
3199 if (sc->tulip_flags & TULIP_ALLMULTI)
3200 sc->tulip_if.if_flags |= IFF_ALLMULTI;
3201 #endif
3202 }
3203
3204 static void
tulip_reset(tulip_softc_t * const sc)3205 tulip_reset(
3206 tulip_softc_t * const sc)
3207 {
3208 tulip_ringinfo_t *ri;
3209 tulip_desc_t *di;
3210 u_int32_t inreset = (sc->tulip_flags & TULIP_INRESET);
3211
3212 #if defined(TULIP_DEBUG)
3213 printf ("de0: resetting...\n");
3214 #endif
3215
3216 /*
3217 * Brilliant. Simply brilliant. When switching modes/speeds
3218 * on a 2114*, you need to set the appriopriate MII/PCS/SCL/PS
3219 * bits in CSR6 and then do a software reset to get the 21140
3220 * to properly reset its internal pathways to the right places.
3221 * Grrrr.
3222 */
3223 if (sc->tulip_boardsw->bd_media_preset != NULL)
3224 (*sc->tulip_boardsw->bd_media_preset)(sc);
3225
3226 TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
3227 DELAY(10); /* Wait 10 microseconds (actually 50 PCI cycles but at
3228 33MHz that comes to two microseconds but wait a
3229 bit longer anyways) */
3230
3231 if (!inreset) {
3232 sc->tulip_flags |= TULIP_INRESET;
3233 sc->tulip_flags &= ~(TULIP_NEEDRESET|TULIP_RXBUFSLOW);
3234 sc->tulip_if.if_flags &= ~IFF_OACTIVE;
3235 sc->tulip_if.if_start = tulip_ifstart;
3236 }
3237
3238 TULIP_CSR_WRITE(sc, csr_txlist, TULIP_KVATOPHYS(sc, &sc->tulip_txinfo.ri_first[0]));
3239 TULIP_CSR_WRITE(sc, csr_rxlist, TULIP_KVATOPHYS(sc, &sc->tulip_rxinfo.ri_first[0]));
3240 #ifdef powerpc
3241 TULIP_CSR_WRITE(sc, csr_busmode,
3242 (4 << 2) /* Descriptor skip length */
3243 |TULIP_BUSMODE_CACHE_ALIGN8
3244 |TULIP_BUSMODE_READMULTIPLE
3245 /*
3246 |(BYTE_ORDER != LITTLE_ENDIAN ? (TULIP_BUSMODE_DESC_BIGENDIAN) : 0)
3247 */
3248 );
3249 #else
3250 TULIP_CSR_WRITE(sc, csr_busmode,
3251 (1 << (TULIP_BURSTSIZE(sc->tulip_unit) + 8))
3252 |TULIP_BUSMODE_CACHE_ALIGN8
3253 |TULIP_BUSMODE_READMULTIPLE
3254 |(BYTE_ORDER != LITTLE_ENDIAN ? TULIP_BUSMODE_DESC_BIGENDIAN : 0));
3255 #endif
3256
3257 sc->tulip_txtimer = 0;
3258 sc->tulip_txq.ifq_maxlen = TULIP_TXDESCS;
3259 /*
3260 * Free all the mbufs that were on the transmit ring.
3261 */
3262 for (;;) {
3263 struct mbuf *m;
3264 IF_DEQUEUE(&sc->tulip_txq, m);
3265 if (m == NULL)
3266 break;
3267 m_freem(m);
3268 }
3269
3270 ri = &sc->tulip_txinfo;
3271 ri->ri_nextin = ri->ri_nextout = ri->ri_first;
3272 ri->ri_free = ri->ri_max;
3273 for (di = ri->ri_first; di < ri->ri_last; di++)
3274 di->d_status = 0; /* no swabbing necessary -dsr */
3275
3276 /*
3277 * We need to collect all the mbufs were on the
3278 * receive ring before we reinit it either to put
3279 * them back on or to know if we have to allocate
3280 * more.
3281 */
3282 ri = &sc->tulip_rxinfo;
3283 ri->ri_nextin = ri->ri_nextout = ri->ri_first;
3284 ri->ri_free = ri->ri_max;
3285 for (di = ri->ri_first; di < ri->ri_last; di++) {
3286 di->d_status = 0; /* no swabbing necessary -dsr */
3287 {
3288 tulip_desc_bitfield_t u;
3289
3290 u.f = DESC_BO(di->u.f); /* copy the bitfields */
3291 u.bd_length1 = 0;
3292 u.bd_length2 = 0;
3293 di->u.f = DESC_BO(u.f);
3294 }
3295 di->d_addr1 = 0; /* no swabbing necessary -dsr */
3296 di->d_addr2 = 0; /* no swabbing necessary -dsr */
3297 }
3298 for (;;) {
3299 struct mbuf *m;
3300 IF_DEQUEUE(&sc->tulip_rxq, m);
3301 if (m == NULL)
3302 break;
3303 m_freem(m);
3304 }
3305
3306 /*
3307 * If tulip_reset is being called recurisvely, exit quickly knowing
3308 * that when the outer tulip_reset returns all the right stuff will
3309 * have happened.
3310 */
3311 if (inreset)
3312 return;
3313
3314 sc->tulip_intrmask |= TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
3315 |TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
3316 |TULIP_STS_TXUNDERFLOW|TULIP_STS_TXBABBLE
3317 |TULIP_STS_RXSTOPPED;
3318
3319 if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0)
3320 (*sc->tulip_boardsw->bd_media_select)(sc);
3321 #if defined(TULIP_DEBUG)
3322 if ((sc->tulip_flags & TULIP_NEEDRESET) == TULIP_NEEDRESET)
3323 printf(TULIP_PRINTF_FMT ": tulip_reset: additional reset needed?!?\n",
3324 TULIP_PRINTF_ARGS);
3325 #endif
3326 tulip_media_print(sc);
3327 if (sc->tulip_features & TULIP_HAVE_DUALSENSE)
3328 TULIP_CSR_WRITE(sc, csr_sia_status, TULIP_CSR_READ(sc, csr_sia_status));
3329
3330 sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP|TULIP_INRESET
3331 |TULIP_RXACT);
3332 tulip_addr_filter(sc);
3333 }
3334
3335 static void
tulip_init(tulip_softc_t * const sc)3336 tulip_init(
3337 tulip_softc_t * const sc)
3338 {
3339 if (sc->tulip_if.if_flags & IFF_UP) {
3340 if ((sc->tulip_if.if_flags & IFF_RUNNING) == 0) {
3341 /* initialize the media */
3342 tulip_reset(sc);
3343 }
3344 sc->tulip_if.if_flags |= IFF_RUNNING;
3345 if (sc->tulip_if.if_flags & IFF_PROMISC) {
3346 sc->tulip_flags |= TULIP_PROMISC;
3347 sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
3348 sc->tulip_intrmask |= TULIP_STS_TXINTR;
3349 } else {
3350 sc->tulip_flags &= ~TULIP_PROMISC;
3351 sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
3352 if (sc->tulip_flags & TULIP_ALLMULTI) {
3353 sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
3354 } else {
3355 sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
3356 }
3357 }
3358 sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
3359 if ((sc->tulip_flags & (TULIP_TXPROBE_ACTIVE|TULIP_WANTSETUP)) == 0) {
3360 tulip_rx_intr(sc);
3361 sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
3362 sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
3363 } else {
3364 sc->tulip_if.if_flags |= IFF_OACTIVE;
3365 sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
3366 sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
3367 }
3368 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3369 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3370 if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
3371 tulip_txput_setup(sc);
3372 } else {
3373 sc->tulip_if.if_flags &= ~IFF_RUNNING;
3374 tulip_reset(sc);
3375 }
3376 }
3377
3378 static void
tulip_rx_intr(tulip_softc_t * const sc)3379 tulip_rx_intr(
3380 tulip_softc_t * const sc)
3381 {
3382 tulip_ringinfo_t * const ri = &sc->tulip_rxinfo;
3383 struct ifnet * const ifp = &sc->tulip_if;
3384 int fillok = 1;
3385 #if defined(TULIP_DEBUG)
3386 int cnt = 0;
3387 #endif
3388
3389 for (;;) {
3390 struct ether_header eh;
3391 tulip_desc_t *eop = ri->ri_nextin;
3392 int total_len = 0, last_offset = 0;
3393 struct mbuf *ms = NULL, *me = NULL;
3394 int accept = 0;
3395
3396 if (fillok && sc->tulip_rxq.ifq_len < TULIP_RXQ_TARGET)
3397 goto queue_mbuf;
3398
3399 #if defined(TULIP_DEBUG)
3400 if (cnt == ri->ri_max)
3401 break;
3402 #endif
3403 /*
3404 * If the TULIP has no descriptors, there can't be any receive
3405 * descriptors to process.
3406 */
3407 if (eop == ri->ri_nextout)
3408 break;
3409
3410 /*
3411 * 90% of the packets will fit in one descriptor. So we optimize
3412 * for that case.
3413 */
3414 if ((DESC_BO(((volatile tulip_desc_t *) eop)->d_status) & (TULIP_DSTS_OWNER|TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) == (TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) {
3415 IF_DEQUEUE(&sc->tulip_rxq, ms);
3416 me = ms;
3417 } else {
3418 /*
3419 * If still owned by the TULIP, don't touch it.
3420 */
3421 if (DESC_BO(((volatile tulip_desc_t *) eop)->d_status) & TULIP_DSTS_OWNER)
3422 break;
3423
3424 /*
3425 * It is possible (though improbable unless the BIG_PACKET support
3426 * is enabled or MCLBYTES < 1518) for a received packet to cross
3427 * more than one receive descriptor.
3428 */
3429 while ((DESC_BO(((volatile tulip_desc_t *) eop)->d_status) & TULIP_DSTS_RxLASTDESC) == 0) {
3430 if (++eop == ri->ri_last)
3431 eop = ri->ri_first;
3432 if (eop == ri->ri_nextout || (DESC_BO(((volatile tulip_desc_t *) eop)->d_status) & TULIP_DSTS_OWNER)) {
3433 #if defined(TULIP_DEBUG)
3434 sc->tulip_dbg.dbg_rxintrs++;
3435 sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
3436 #endif
3437 return;
3438 }
3439 total_len++;
3440 }
3441
3442 /*
3443 * Dequeue the first buffer for the start of the packet. Hopefully
3444 * this will be the only one we need to dequeue. However, if the
3445 * packet consumed multiple descriptors, then we need to dequeue
3446 * those buffers and chain to the starting mbuf. All buffers but
3447 * the last buffer have the same length so we can set that now.
3448 * (we add to last_offset instead of multiplying since we normally
3449 * won't go into the loop and thereby saving a ourselves from
3450 * doing a multiplication by 0 in the normal case).
3451 */
3452 IF_DEQUEUE(&sc->tulip_rxq, ms);
3453 for (me = ms; total_len > 0; total_len--) {
3454 me->m_len = TULIP_RX_BUFLEN;
3455 last_offset += TULIP_RX_BUFLEN;
3456 IF_DEQUEUE(&sc->tulip_rxq, me->m_next);
3457 me = me->m_next;
3458 }
3459 }
3460
3461 /*
3462 * Now get the size of received packet (minus the CRC).
3463 */
3464 total_len = ((DESC_BO(eop->d_status) >> 16) & 0x7FFF) - 4;
3465 if ((sc->tulip_flags & TULIP_RXIGNORE) == 0
3466 && ((DESC_BO(eop->d_status) & TULIP_DSTS_ERRSUM) == 0
3467 #ifdef BIG_PACKET
3468 || (total_len <= sc->tulip_if.if_mtu + sizeof(struct ether_header) &&
3469 (DESC_BO(eop->d_status) & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxRUNT|
3470 TULIP_DSTS_RxCOLLSEEN|TULIP_DSTS_RxBADCRC|
3471 TULIP_DSTS_RxOVERFLOW)) == 0)
3472 #endif
3473 )) {
3474 me->m_len = total_len - last_offset;
3475 eh = *mtod(ms, struct ether_header *);
3476 #if NBPFILTER > 0
3477 if (sc->tulip_bpf != NULL) {
3478 if (me == ms)
3479 TULIP_BPF_TAP(sc, mtod(ms, caddr_t), total_len);
3480 else
3481 TULIP_BPF_MTAP(sc, ms);
3482 }
3483 #endif
3484 sc->tulip_flags |= TULIP_RXACT;
3485 accept = 1;
3486 total_len -= sizeof(struct ether_header);
3487 } else {
3488 ifp->if_ierrors++;
3489 if (DESC_BO(eop->d_status) & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxOVERFLOW|TULIP_DSTS_RxWATCHDOG)) {
3490 sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
3491 } else {
3492 const char *error = NULL;
3493 if (DESC_BO(eop->d_status) & TULIP_DSTS_RxTOOLONG) {
3494 sc->tulip_dot3stats.dot3StatsFrameTooLongs++;
3495 error = "frame too long";
3496 }
3497 if (DESC_BO(eop->d_status) & TULIP_DSTS_RxBADCRC) {
3498 if (DESC_BO(eop->d_status) & TULIP_DSTS_RxDRBBLBIT) {
3499 sc->tulip_dot3stats.dot3StatsAlignmentErrors++;
3500 error = "alignment error";
3501 } else {
3502 sc->tulip_dot3stats.dot3StatsFCSErrors++;
3503 error = "bad crc";
3504 }
3505 }
3506 if (error != NULL && (sc->tulip_flags & TULIP_NOMESSAGES) == 0) {
3507 printf(TULIP_PRINTF_FMT ": receive: " TULIP_EADDR_FMT ": %s\n",
3508 TULIP_PRINTF_ARGS,
3509 TULIP_EADDR_ARGS(mtod(ms, u_char *) + 6),
3510 error);
3511 sc->tulip_flags |= TULIP_NOMESSAGES;
3512 }
3513 }
3514 }
3515 #if defined(TULIP_DEBUG)
3516 cnt++;
3517 #endif
3518 ifp->if_ipackets++;
3519 if (++eop == ri->ri_last)
3520 eop = ri->ri_first;
3521 ri->ri_nextin = eop;
3522 queue_mbuf:
3523 /*
3524 * Either we are priming the TULIP with mbufs (m == NULL)
3525 * or we are about to accept an mbuf for the upper layers
3526 * so we need to allocate an mbuf to replace it. If we
3527 * can't replace it, send up it anyways. This may cause
3528 * us to drop packets in the future but that's better than
3529 * being caught in livelock.
3530 *
3531 * Note that if this packet crossed multiple descriptors
3532 * we don't even try to reallocate all the mbufs here.
3533 * Instead we rely on the test of the beginning of
3534 * the loop to refill for the extra consumed mbufs.
3535 */
3536 if (accept || ms == NULL) {
3537 struct mbuf *m0;
3538 MGETHDR(m0, M_DONTWAIT, MT_DATA);
3539 if (m0 != NULL) {
3540 #if defined(TULIP_COPY_RXDATA)
3541 if (!accept || total_len >= MHLEN) {
3542 #endif
3543 MCLGET(m0, M_DONTWAIT);
3544 if ((m0->m_flags & M_EXT) == 0) {
3545 m_freem(m0);
3546 m0 = NULL;
3547 }
3548 #if defined(TULIP_COPY_RXDATA)
3549 }
3550 #endif
3551 }
3552 if (accept
3553 #if defined(TULIP_COPY_RXDATA)
3554 && m0 != NULL
3555 #endif
3556 ) {
3557 #if defined(__bsdi__)
3558 eh.ether_type = ntohs(eh.ether_type);
3559 #endif
3560 #if !defined(TULIP_COPY_RXDATA)
3561 ms->m_data += sizeof(struct ether_header);
3562 ms->m_len -= sizeof(struct ether_header);
3563 ms->m_pkthdr.len = total_len;
3564 ms->m_pkthdr.rcvif = ifp;
3565 ether_input(ifp, &eh, ms);
3566 #else
3567 #ifdef BIG_PACKET
3568 #error BIG_PACKET is incompatible with TULIP_COPY_RXDATA
3569 #endif
3570 if (ms == me)
3571 bcopy(mtod(ms, caddr_t) + sizeof(struct ether_header),
3572 mtod(m0, caddr_t), total_len);
3573 else
3574 m_copydata(ms, 0, total_len, mtod(m0, caddr_t));
3575 m0->m_len = m0->m_pkthdr.len = total_len;
3576 m0->m_pkthdr.rcvif = ifp;
3577 ether_input(ifp, &eh, m0);
3578 m0 = ms;
3579 #endif
3580 }
3581 ms = m0;
3582 }
3583 if (ms == NULL) {
3584 /*
3585 * Couldn't allocate a new buffer. Don't bother
3586 * trying to replenish the receive queue.
3587 */
3588 fillok = 0;
3589 sc->tulip_flags |= TULIP_RXBUFSLOW;
3590 #if defined(TULIP_DEBUG)
3591 sc->tulip_dbg.dbg_rxlowbufs++;
3592 #endif
3593 continue;
3594 }
3595 /*
3596 * Now give the buffer(s) to the TULIP and save in our
3597 * receive queue.
3598 */
3599 do {
3600 tulip_desc_bitfield_t u;
3601 u.f = DESC_BO ( ri->ri_nextout->u.f );
3602 u.bd_length1 = TULIP_RX_BUFLEN;
3603 ri->ri_nextout->u.f = DESC_BO ( u.f );
3604
3605 ri->ri_nextout->d_addr1 =
3606 DESC_BO(TULIP_KVATOPHYS(sc, mtod(ms, caddr_t)));
3607 ri->ri_nextout->d_status = DESC_BO(TULIP_DSTS_OWNER);
3608 #if defined(__mips__)
3609 pci_sync_cache(sc->tulip_pc, (vm_offset_t)mtod(ms, caddr_t),TULIP_RX_BUFLEN);
3610 #endif
3611 if (++ri->ri_nextout == ri->ri_last)
3612 ri->ri_nextout = ri->ri_first;
3613 me = ms->m_next;
3614 ms->m_next = NULL;
3615 IF_ENQUEUE(&sc->tulip_rxq, ms);
3616 } while ((ms = me) != NULL);
3617
3618 if (sc->tulip_rxq.ifq_len >= TULIP_RXQ_TARGET)
3619 sc->tulip_flags &= ~TULIP_RXBUFSLOW;
3620 }
3621
3622 #if defined(TULIP_DEBUG)
3623 sc->tulip_dbg.dbg_rxintrs++;
3624 sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
3625 #endif
3626 }
3627
3628
3629 static int
tulip_tx_intr(tulip_softc_t * const sc)3630 tulip_tx_intr(
3631 tulip_softc_t * const sc)
3632 {
3633 tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
3634 struct mbuf *m;
3635 int xmits = 0;
3636 int descs = 0;
3637
3638 while (ri->ri_free < ri->ri_max) {
3639 u_int32_t d_flag;
3640 if (DESC_BO(((volatile tulip_desc_t *) ri->ri_nextin)->d_status) & TULIP_DSTS_OWNER)
3641 break;
3642
3643 {
3644 tulip_desc_bitfield_t u;
3645
3646 u.f = DESC_BO(ri->ri_nextin->u.f); /* copy the bitfields */
3647 d_flag = u.bd_flag;
3648 }
3649 if (d_flag & TULIP_DFLAG_TxLASTSEG) {
3650 if (d_flag & TULIP_DFLAG_TxSETUPPKT) {
3651 /*
3652 * We've just finished processing a setup packet.
3653 * Mark that we finished it. If there's not
3654 * another pending, startup the TULIP receiver.
3655 * Make sure we ack the RXSTOPPED so we won't get
3656 * an abormal interrupt indication.
3657 */
3658 sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_HASHONLY);
3659 if (d_flag & TULIP_DFLAG_TxINVRSFILT)
3660 sc->tulip_flags |= TULIP_HASHONLY;
3661 if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == 0) {
3662 tulip_rx_intr(sc);
3663 sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
3664 sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
3665 TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
3666 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3667 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3668 }
3669 } else {
3670 const u_int32_t d_status = DESC_BO(ri->ri_nextin->d_status);
3671 IF_DEQUEUE(&sc->tulip_txq, m);
3672 if (m != NULL) {
3673 #if NBPFILTER > 0
3674 if (sc->tulip_bpf != NULL)
3675 TULIP_BPF_MTAP(sc, m);
3676 #endif
3677 m_freem(m);
3678 #if defined(TULIP_DEBUG)
3679 } else {
3680 printf(TULIP_PRINTF_FMT ": tx_intr: failed to dequeue mbuf?!?\n", TULIP_PRINTF_ARGS);
3681 #endif
3682 }
3683 if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
3684 tulip_mediapoll_event_t event = TULIP_MEDIAPOLL_TXPROBE_OK;
3685 if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxEXCCOLL)) {
3686 #if defined(TULIP_DEBUG)
3687 if (d_status & TULIP_DSTS_TxNOCARR)
3688 sc->tulip_dbg.dbg_txprobe_nocarr++;
3689 if (d_status & TULIP_DSTS_TxEXCCOLL)
3690 sc->tulip_dbg.dbg_txprobe_exccoll++;
3691 #endif
3692 event = TULIP_MEDIAPOLL_TXPROBE_FAILED;
3693 }
3694 (*sc->tulip_boardsw->bd_media_poll)(sc, event);
3695 /*
3696 * Escape from the loop before media poll has reset the TULIP!
3697 */
3698 break;
3699 } else {
3700 xmits++;
3701 if (d_status & TULIP_DSTS_ERRSUM) {
3702 sc->tulip_if.if_oerrors++;
3703 if (d_status & TULIP_DSTS_TxEXCCOLL)
3704 sc->tulip_dot3stats.dot3StatsExcessiveCollisions++;
3705 if (d_status & TULIP_DSTS_TxLATECOLL)
3706 sc->tulip_dot3stats.dot3StatsLateCollisions++;
3707 if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxCARRLOSS))
3708 sc->tulip_dot3stats.dot3StatsCarrierSenseErrors++;
3709 if (d_status & (TULIP_DSTS_TxUNDERFLOW|TULIP_DSTS_TxBABBLE))
3710 sc->tulip_dot3stats.dot3StatsInternalMacTransmitErrors++;
3711 if (d_status & TULIP_DSTS_TxUNDERFLOW)
3712 sc->tulip_dot3stats.dot3StatsInternalTransmitUnderflows++;
3713 if (d_status & TULIP_DSTS_TxBABBLE)
3714 sc->tulip_dot3stats.dot3StatsInternalTransmitBabbles++;
3715 } else {
3716 u_int32_t collisions =
3717 (d_status & TULIP_DSTS_TxCOLLMASK)
3718 >> TULIP_DSTS_V_TxCOLLCNT;
3719 sc->tulip_if.if_collisions += collisions;
3720 if (collisions == 1)
3721 sc->tulip_dot3stats.dot3StatsSingleCollisionFrames++;
3722 else if (collisions > 1)
3723 sc->tulip_dot3stats.dot3StatsMultipleCollisionFrames++;
3724 else if (d_status & TULIP_DSTS_TxDEFERRED)
3725 sc->tulip_dot3stats.dot3StatsDeferredTransmissions++;
3726 /*
3727 * SQE is only valid for 10baseT/BNC/AUI when not
3728 * running in full-duplex. In order to speed up the
3729 * test, the corresponding bit in tulip_flags needs to
3730 * set as well to get us to count SQE Test Errors.
3731 */
3732 if (d_status & TULIP_DSTS_TxNOHRTBT & sc->tulip_flags)
3733 sc->tulip_dot3stats.dot3StatsSQETestErrors++;
3734 }
3735 }
3736 }
3737 }
3738
3739 if (++ri->ri_nextin == ri->ri_last)
3740 ri->ri_nextin = ri->ri_first;
3741
3742 ri->ri_free++;
3743 descs++;
3744 if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
3745 sc->tulip_if.if_flags &= ~IFF_OACTIVE;
3746 }
3747 /*
3748 * If nothing left to transmit, disable the timer.
3749 * Else if progress, reset the timer back to 2 ticks.
3750 */
3751 if (ri->ri_free == ri->ri_max || (sc->tulip_flags & TULIP_TXPROBE_ACTIVE))
3752 sc->tulip_txtimer = 0;
3753 else if (xmits > 0)
3754 sc->tulip_txtimer = TULIP_TXTIMER;
3755 sc->tulip_if.if_opackets += xmits;
3756 return descs;
3757 }
3758
3759 static void
tulip_print_abnormal_interrupt(tulip_softc_t * const sc,u_int32_t csr)3760 tulip_print_abnormal_interrupt(
3761 tulip_softc_t * const sc,
3762 u_int32_t csr)
3763 {
3764 const char * const *msgp = tulip_status_bits;
3765 const char *sep;
3766 u_int32_t mask;
3767 const char thrsh[] = "72|128\0\0\0" "96|256\0\0\0" "128|512\0\0" "160|1024\0";
3768
3769 csr &= (1 << (sizeof(tulip_status_bits)/sizeof(tulip_status_bits[0]))) - 1;
3770 printf(TULIP_PRINTF_FMT ": abnormal interrupt:", TULIP_PRINTF_ARGS);
3771 for (sep = " ", mask = 1; mask <= csr; mask <<= 1, msgp++) {
3772 if ((csr & mask) && *msgp != NULL) {
3773 printf("%s%s", sep, *msgp);
3774 if (mask == TULIP_STS_TXUNDERFLOW && (sc->tulip_flags & TULIP_NEWTXTHRESH)) {
3775 sc->tulip_flags &= ~TULIP_NEWTXTHRESH;
3776 if (sc->tulip_cmdmode & TULIP_CMD_STOREFWD) {
3777 printf(" (switching to store-and-forward mode)");
3778 } else {
3779 printf(" (raising TX threshold to %s)",
3780 &thrsh[9 * ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) >> 14)]);
3781 }
3782 }
3783 sep = ", ";
3784 }
3785 }
3786 printf("\n");
3787 }
3788
3789 static void
tulip_intr_handler(tulip_softc_t * const sc,int * progress_p)3790 tulip_intr_handler(
3791 tulip_softc_t * const sc,
3792 int *progress_p)
3793 {
3794 TULIP_PERFSTART(intr)
3795 u_int32_t csr;
3796
3797 while ((csr = TULIP_CSR_READ(sc, csr_status)) & sc->tulip_intrmask) {
3798 *progress_p = 1;
3799 TULIP_CSR_WRITE(sc, csr_status, csr);
3800
3801 if (csr & TULIP_STS_SYSERROR) {
3802 sc->tulip_last_system_error = (csr & TULIP_STS_ERRORMASK) >> TULIP_STS_ERR_SHIFT;
3803 if (sc->tulip_flags & TULIP_NOMESSAGES) {
3804 sc->tulip_flags |= TULIP_SYSTEMERROR;
3805 } else {
3806 printf(TULIP_PRINTF_FMT ": system error: %s\n",
3807 TULIP_PRINTF_ARGS,
3808 tulip_system_errors[sc->tulip_last_system_error]);
3809 }
3810 sc->tulip_flags |= TULIP_NEEDRESET;
3811 sc->tulip_system_errors++;
3812 break;
3813 }
3814 if (csr & (TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL) & sc->tulip_intrmask) {
3815 #if defined(TULIP_DEBUG)
3816 sc->tulip_dbg.dbg_link_intrs++;
3817 #endif
3818 if (sc->tulip_boardsw->bd_media_poll != NULL) {
3819 (*sc->tulip_boardsw->bd_media_poll)(sc, csr & TULIP_STS_LINKFAIL
3820 ? TULIP_MEDIAPOLL_LINKFAIL
3821 : TULIP_MEDIAPOLL_LINKPASS);
3822 csr &= ~TULIP_STS_ABNRMLINTR;
3823 }
3824 tulip_media_print(sc);
3825 }
3826 if (csr & (TULIP_STS_RXINTR|TULIP_STS_RXNOBUF)) {
3827 u_int32_t misses = TULIP_CSR_READ(sc, csr_missed_frames);
3828 if (csr & TULIP_STS_RXNOBUF)
3829 sc->tulip_dot3stats.dot3StatsMissedFrames += misses & 0xFFFF;
3830 /*
3831 * Pass 2.[012] of the 21140A-A[CDE] may hang and/or corrupt data
3832 * on receive overflows.
3833 */
3834 if ((misses & 0x0FFE0000) && (sc->tulip_features & TULIP_HAVE_RXBADOVRFLW)) {
3835 sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
3836 /*
3837 * Stop the receiver process and spin until it's stopped.
3838 * Tell rx_intr to drop the packets it dequeues.
3839 */
3840 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode & ~TULIP_CMD_RXRUN);
3841 while ((TULIP_CSR_READ(sc, csr_status) & TULIP_STS_RXSTOPPED) == 0)
3842 ;
3843 TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
3844 sc->tulip_flags |= TULIP_RXIGNORE;
3845 }
3846 tulip_rx_intr(sc);
3847 if (sc->tulip_flags & TULIP_RXIGNORE) {
3848 /*
3849 * Restart the receiver.
3850 */
3851 sc->tulip_flags &= ~TULIP_RXIGNORE;
3852 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3853 }
3854 }
3855 if (csr & TULIP_STS_ABNRMLINTR) {
3856 u_int32_t tmp = csr & sc->tulip_intrmask
3857 & ~(TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR);
3858 if (csr & TULIP_STS_TXUNDERFLOW) {
3859 #if defined(TULIP_DEBUG)
3860 printf ("Underflow interrupt\n");
3861 #endif
3862 if ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) != TULIP_CMD_THRSHLD160) {
3863 sc->tulip_cmdmode += TULIP_CMD_THRSHLD96;
3864 sc->tulip_flags |= TULIP_NEWTXTHRESH;
3865 } else if (sc->tulip_features & TULIP_HAVE_STOREFWD) {
3866 sc->tulip_cmdmode |= TULIP_CMD_STOREFWD;
3867 sc->tulip_flags |= TULIP_NEWTXTHRESH;
3868 }
3869 }
3870 if (sc->tulip_flags & TULIP_NOMESSAGES) {
3871 sc->tulip_statusbits |= tmp;
3872 } else {
3873 tulip_print_abnormal_interrupt(sc, tmp);
3874 sc->tulip_flags |= TULIP_NOMESSAGES;
3875 }
3876 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
3877 }
3878 if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_TXPROBE_ACTIVE|TULIP_DOINGSETUP|TULIP_PROMISC)) {
3879 tulip_tx_intr(sc);
3880 if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
3881 tulip_ifstart(&sc->tulip_if);
3882 }
3883 }
3884 if (sc->tulip_flags & TULIP_NEEDRESET) {
3885 tulip_reset(sc);
3886 tulip_init(sc);
3887 }
3888 TULIP_PERFEND(intr);
3889 }
3890
3891 #if defined(TULIP_USE_SOFTINTR)
3892 /*
3893 * This is a experimental idea to alleviate problems due to interrupt
3894 * livelock. What is interrupt livelock? It's when you spend all your
3895 * time servicing device interrupts and never drop below device ipl
3896 * to do "useful" work.
3897 *
3898 * So what we do here is see if the device needs service and if so,
3899 * disable interrupts (dismiss the interrupt), place it in a list of devices
3900 * needing service, and issue a network software interrupt.
3901 *
3902 * When our network software interrupt routine gets called, we simply
3903 * walk done the list of devices that we have created and deal with them
3904 * at splnet/splsoftnet.
3905 *
3906 */
3907 static void
tulip_hardintr_handler(tulip_softc_t * const sc,int * progress_p)3908 tulip_hardintr_handler(
3909 tulip_softc_t * const sc,
3910 int *progress_p)
3911 {
3912 if (TULIP_CSR_READ(sc, csr_status) & (TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR) == 0)
3913 return;
3914 *progress_p = 1;
3915 /*
3916 * disable interrupts
3917 */
3918 TULIP_CSR_WRITE(sc, csr_intr, 0);
3919 /*
3920 * mark it as needing a software interrupt
3921 */
3922 tulip_softintr_mask |= (1U << sc->tulip_unit);
3923
3924 #ifdef __MirBSD__
3925 rnd_lopool_addv(sc->tulip_flags);
3926 #endif
3927 #if defined(__NetBSD__) && NRND > 0
3928 /*
3929 * This isn't all that random (the value we feed in) but it is
3930 * better than a constant probably. It isn't used in entropy
3931 * calculation anyway, just to add something to the pool.
3932 */
3933 rnd_add_uint32(&sc->tulip_rndsource, sc->tulip_flags);
3934 #endif
3935 }
3936
3937 static void
tulip_softintr(void)3938 tulip_softintr(
3939 void)
3940 {
3941 u_int32_t softintr_mask, mask;
3942 int progress = 0;
3943 int unit;
3944 tulip_spl_t s;
3945
3946 /*
3947 * Copy mask to local copy and reset global one to 0.
3948 */
3949 s = TULIP_RAISESPL();
3950 softintr_mask = tulip_softintr_mask;
3951 tulip_softintr_mask = 0;
3952 TULIP_RESTORESPL(s);
3953
3954 /*
3955 * Optimize for the single unit case.
3956 */
3957 if (tulip_softintr_max_unit == 0) {
3958 if (softintr_mask & 1) {
3959 tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(0);
3960 /*
3961 * Handle the "interrupt" and then reenable interrupts
3962 */
3963 softintr_mask = 0;
3964 tulip_intr_handler(sc, &progress);
3965 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3966 }
3967 return;
3968 }
3969
3970 /*
3971 * Handle all "queued" interrupts in a round robin fashion.
3972 * This is done so as not to favor a particular interface.
3973 */
3974 unit = tulip_softintr_last_unit;
3975 mask = (1U << unit);
3976 while (softintr_mask != 0) {
3977 if (tulip_softintr_max_unit == unit) {
3978 unit = 0; mask = 1;
3979 } else {
3980 unit += 1; mask <<= 1;
3981 }
3982 if (softintr_mask & mask) {
3983 tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(unit);
3984 /*
3985 * Handle the "interrupt" and then reenable interrupts
3986 */
3987 softintr_mask ^= mask;
3988 tulip_intr_handler(sc, &progress);
3989 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
3990 }
3991 }
3992
3993 /*
3994 * Save where we ending up.
3995 */
3996 tulip_softintr_last_unit = unit;
3997 }
3998 #endif /* TULIP_USE_SOFTINTR */
3999
4000 static tulip_intrfunc_t
tulip_intr_shared(void * arg)4001 tulip_intr_shared(
4002 void *arg)
4003 {
4004 tulip_softc_t * sc = arg;
4005 int progress = 0;
4006
4007 for (; sc != NULL; sc = sc->tulip_slaves) {
4008 #if defined(TULIP_DEBUG)
4009 sc->tulip_dbg.dbg_intrs++;
4010 #endif
4011 #if defined(TULIP_USE_SOFTINTR)
4012 tulip_hardintr_handler(sc, &progress);
4013 #else
4014 tulip_intr_handler(sc, &progress);
4015 #endif
4016 }
4017 #if defined(TULIP_USE_SOFTINTR)
4018 if (progress)
4019 schednetisr(NETISR_DE);
4020 #endif
4021 #if !defined(TULIP_VOID_INTRFUNC)
4022 return progress;
4023 #endif
4024 }
4025
4026 static tulip_intrfunc_t
tulip_intr_normal(void * arg)4027 tulip_intr_normal(
4028 void *arg)
4029 {
4030 tulip_softc_t * sc = (tulip_softc_t *) arg;
4031 int progress = 0;
4032
4033 #if defined(TULIP_DEBUG)
4034 sc->tulip_dbg.dbg_intrs++;
4035 #endif
4036 #if defined(TULIP_USE_SOFTINTR)
4037 tulip_hardintr_handler(sc, &progress);
4038 if (progress)
4039 schednetisr(NETISR_DE);
4040 #else
4041 tulip_intr_handler(sc, &progress);
4042 #endif
4043 #if !defined(TULIP_VOID_INTRFUNC)
4044 return progress;
4045 #endif
4046 }
4047
4048 static struct mbuf *
tulip_mbuf_compress(struct mbuf * m)4049 tulip_mbuf_compress(
4050 struct mbuf *m)
4051 {
4052 struct mbuf *m0;
4053 #if MCLBYTES >= ETHERMTU + 18 && !defined(BIG_PACKET)
4054 MGETHDR(m0, M_DONTWAIT, MT_DATA);
4055 if (m0 != NULL) {
4056 if (m->m_pkthdr.len > MHLEN) {
4057 MCLGET(m0, M_DONTWAIT);
4058 if ((m0->m_flags & M_EXT) == 0) {
4059 m_freem(m);
4060 m_freem(m0);
4061 return NULL;
4062 }
4063 }
4064 m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t));
4065 m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
4066 }
4067 #else
4068 int mlen = MHLEN;
4069 int len = m->m_pkthdr.len;
4070 struct mbuf **mp = &m0;
4071
4072 while (len > 0) {
4073 if (mlen == MHLEN) {
4074 MGETHDR(*mp, M_DONTWAIT, MT_DATA);
4075 } else {
4076 MGET(*mp, M_DONTWAIT, MT_DATA);
4077 }
4078 if (*mp == NULL) {
4079 m_freem(m0);
4080 m0 = NULL;
4081 break;
4082 }
4083 if (len > MLEN) {
4084 MCLGET(*mp, M_DONTWAIT);
4085 if (((*mp)->m_flags & M_EXT) == 0) {
4086 m_freem(m0);
4087 m0 = NULL;
4088 break;
4089 }
4090 (*mp)->m_len = len <= MCLBYTES ? len : MCLBYTES;
4091 } else {
4092 (*mp)->m_len = len <= mlen ? len : mlen;
4093 }
4094 m_copydata(m, m->m_pkthdr.len - len,
4095 (*mp)->m_len, mtod((*mp), caddr_t));
4096 len -= (*mp)->m_len;
4097 mp = &(*mp)->m_next;
4098 mlen = MLEN;
4099 }
4100 #endif
4101 m_freem(m);
4102 return m0;
4103 }
4104
4105 static struct mbuf *
tulip_txput(tulip_softc_t * const sc,struct mbuf * m)4106 tulip_txput(
4107 tulip_softc_t * const sc,
4108 struct mbuf *m)
4109 {
4110 TULIP_PERFSTART(txput)
4111 tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
4112 tulip_desc_t *eop, *nextout;
4113 int segcnt, free;
4114 u_int32_t d_status;
4115 struct mbuf *m0;
4116 #if 1 /* ALTQ */
4117 struct ifnet *ifp = &sc->tulip_if;
4118 struct mbuf *ombuf = m;
4119 int compressed = 0;
4120 #endif
4121
4122 #if defined(TULIP_DEBUG)
4123 if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
4124 printf(TULIP_PRINTF_FMT ": txput%s: tx not running\n",
4125 TULIP_PRINTF_ARGS,
4126 (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) ? "(probe)" : "");
4127 sc->tulip_flags |= TULIP_WANTTXSTART;
4128 goto finish;
4129 }
4130 #endif
4131
4132 /*
4133 * Now we try to fill in our transmit descriptors. This is
4134 * a bit reminiscent of going on the Ark two by two
4135 * since each descriptor for the TULIP can describe
4136 * two buffers. So we advance through packet filling
4137 * each of the two entries at a time to to fill each
4138 * descriptor. Clear the first and last segment bits
4139 * in each descriptor (actually just clear everything
4140 * but the end-of-ring or chain bits) to make sure
4141 * we don't get messed up by previously sent packets.
4142 *
4143 * We may fail to put the entire packet on the ring if
4144 * there is either not enough ring entries free or if the
4145 * packet has more than MAX_TXSEG segments. In the former
4146 * case we will just wait for the ring to empty. In the
4147 * latter case we have to recopy.
4148 */
4149 again:
4150 d_status = 0;
4151 eop = nextout = ri->ri_nextout;
4152 m0 = m;
4153 segcnt = 0;
4154 free = ri->ri_free;
4155 do {
4156 int len = m0->m_len;
4157 caddr_t addr = mtod(m0, caddr_t);
4158 unsigned clsize = PAGE_SIZE - (((u_long) addr) & PAGE_MASK);
4159
4160 while (len > 0) {
4161 unsigned slen = min(len, clsize);
4162 #ifdef BIG_PACKET
4163 int partial = 0;
4164 if (slen >= 2048)
4165 slen = 2040, partial = 1;
4166 #endif
4167 segcnt++;
4168 if (segcnt > TULIP_MAX_TXSEG) {
4169 /*
4170 * The packet exceeds the number of transmit buffer
4171 * entries that we can use for one packet, so we have
4172 * recopy it into one mbuf and then try again.
4173 */
4174 #if 1 /* ALTQ */
4175 struct mbuf *tmp;
4176 /*
4177 * tulip_mbuf_compress() frees the original mbuf.
4178 * thus, we have to remove the mbuf from the queue
4179 * before calling it.
4180 * we don't have to worry about space shortage
4181 * after compressing the mbuf since the compressed
4182 * mbuf will take only two segs.
4183 */
4184 if (compressed) {
4185 /* should not happen */
4186 printf("tulip_txput: compress called twice!\n");
4187 goto finish;
4188 }
4189 IFQ_DEQUEUE(&ifp->if_snd, tmp);
4190 if (tmp != ombuf)
4191 panic("tulip_txput: different mbuf dequeued!");
4192 compressed = 1;
4193 #endif
4194 m = tulip_mbuf_compress(m);
4195 if (m == NULL)
4196 goto finish;
4197 goto again;
4198 }
4199 if (segcnt & 1) {
4200 if (--free == 0) {
4201 /*
4202 * See if there's any unclaimed space in the
4203 * transmit ring.
4204 */
4205 if ((free += tulip_tx_intr(sc)) == 0) {
4206 /*
4207 * There's no more room but since nothing
4208 * has been committed at this point, just
4209 * show output is active, put back the
4210 * mbuf and return.
4211 */
4212 sc->tulip_flags |= TULIP_WANTTXSTART;
4213 goto finish;
4214 }
4215 }
4216 eop = nextout;
4217 if (++nextout == ri->ri_last)
4218 nextout = ri->ri_first;
4219 {
4220 tulip_desc_bitfield_t u;
4221 u.f = DESC_BO(eop->u.f); /* copy the bitfields */
4222 u.bd_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4223 u.bd_length1 = slen;
4224
4225 eop->d_status = DESC_BO(d_status);
4226 eop->d_addr1 = DESC_BO( TULIP_KVATOPHYS(sc, addr) );
4227 eop->u.f = DESC_BO(u.f); /* copy the bitfields */
4228 }
4229 } else {
4230 /*
4231 * Fill in second half of descriptor
4232 */
4233 eop->d_addr2 = DESC_BO(TULIP_KVATOPHYS(sc, addr));
4234 {
4235 tulip_desc_bitfield_t u;
4236 u.f = DESC_BO(eop->u.f); /* copy the bitfields */
4237 u.bd_length2 = slen;
4238 eop->u.f = DESC_BO(u.f); /* copy the bitfields */
4239 }
4240 }
4241 #if defined(__mips__)
4242 pci_sync_cache(sc->tulip_pc, (vm_offset_t)addr, slen);
4243 #endif
4244 d_status = TULIP_DSTS_OWNER;
4245 len -= slen;
4246 addr += slen;
4247 #ifdef BIG_PACKET
4248 if (partial)
4249 continue;
4250 #endif
4251 clsize = PAGE_SIZE;
4252 }
4253 } while ((m0 = m0->m_next) != NULL);
4254
4255
4256 /*
4257 * The descriptors have been filled in. Now get ready
4258 * to transmit.
4259 */
4260 #if 1 /* ALTQ */
4261 if (!compressed && (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0) {
4262 /* remove the mbuf from the queue */
4263 struct mbuf *tmp;
4264 IFQ_DEQUEUE(&ifp->if_snd, tmp);
4265 if (tmp != ombuf)
4266 panic("tulip_txput: different mbuf dequeued!");
4267 }
4268 #endif
4269
4270 IF_ENQUEUE(&sc->tulip_txq, m);
4271 m = NULL;
4272
4273 /*
4274 * Make sure the next descriptor after this packet is owned
4275 * by us since it may have been set up above if we ran out
4276 * of room in the ring.
4277 */
4278 nextout->d_status = 0; /* doesn't need swab - dsr */
4279
4280 /*
4281 * If we only used the first segment of the last descriptor,
4282 * make sure the second segment will not be used.
4283 */
4284 if (segcnt & 1) {
4285 tulip_desc_bitfield_t u;
4286 u.f = DESC_BO(eop->u.f); /* copy the bitfields */
4287 u.bd_length2 = 0;
4288 eop->d_addr2 = 0; /* no swab necessary - dsr */
4289 eop->u.f = DESC_BO(u.f);
4290 }
4291
4292 /*
4293 * Mark the last and first segments, indicate we want a transmit
4294 * complete interrupt, and tell it to transmit!
4295 */
4296 {
4297 tulip_desc_bitfield_t u;
4298
4299 u.f = DESC_BO(eop->u.f); /* copy the bitfields */
4300 u.bd_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
4301 eop->u.f = DESC_BO(u.f);
4302 }
4303
4304 /*
4305 * Note that ri->ri_nextout is still the start of the packet
4306 * and until we set the OWNER bit, we can still back out of
4307 * everything we have done.
4308 */
4309 {
4310 tulip_desc_bitfield_t u;
4311
4312 u.f = DESC_BO(ri->ri_nextout->u.f); /* copy the bitfields */
4313 u.bd_flag |= TULIP_DFLAG_TxFIRSTSEG;
4314 ri->ri_nextout->u.f = DESC_BO(u.f);
4315 ri->ri_nextout->d_status = DESC_BO(TULIP_DSTS_OWNER);
4316 }
4317
4318 TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4319
4320 /*
4321 * This advances the ring for us.
4322 */
4323 ri->ri_nextout = nextout;
4324 ri->ri_free = free;
4325
4326 TULIP_PERFEND(txput);
4327
4328 if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
4329 sc->tulip_if.if_flags |= IFF_OACTIVE;
4330 sc->tulip_if.if_start = tulip_ifstart;
4331 TULIP_PERFEND(txput);
4332 return NULL;
4333 }
4334
4335 /*
4336 * switch back to the single queueing ifstart.
4337 */
4338 sc->tulip_flags &= ~TULIP_WANTTXSTART;
4339 if (sc->tulip_txtimer == 0)
4340 sc->tulip_txtimer = TULIP_TXTIMER;
4341
4342 /*
4343 * If we want a txstart, there must be not enough space in the
4344 * transmit ring. So we want to enable transmit done interrupts
4345 * so we can immediately reclaim some space. When the transmit
4346 * interrupt is posted, the interrupt handler will call tx_intr
4347 * to reclaim space and then txstart (since WANTTXSTART is set).
4348 * txstart will move the packet into the transmit ring and clear
4349 * WANTTXSTART thereby causing TXINTR to be cleared.
4350 */
4351 finish:
4352 if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_DOINGSETUP)) {
4353 sc->tulip_if.if_flags |= IFF_OACTIVE;
4354 sc->tulip_if.if_start = tulip_ifstart;
4355 if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
4356 sc->tulip_intrmask |= TULIP_STS_TXINTR;
4357 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4358 }
4359 } else if ((sc->tulip_flags & TULIP_PROMISC) == 0) {
4360 if (sc->tulip_intrmask & TULIP_STS_TXINTR) {
4361 sc->tulip_intrmask &= ~TULIP_STS_TXINTR;
4362 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4363 }
4364 }
4365 TULIP_PERFEND(txput);
4366 return m;
4367 }
4368
4369 static void
tulip_txput_setup(tulip_softc_t * const sc)4370 tulip_txput_setup(
4371 tulip_softc_t * const sc)
4372 {
4373 tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
4374 tulip_desc_t *nextout;
4375
4376 /*
4377 * We will transmit, at most, one setup packet per call to ifstart.
4378 */
4379
4380 #if defined(TULIP_DEBUG)
4381 if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
4382 printf(TULIP_PRINTF_FMT ": txput_setup: tx not running\n",
4383 TULIP_PRINTF_ARGS);
4384 sc->tulip_flags |= TULIP_WANTTXSTART;
4385 sc->tulip_if.if_start = tulip_ifstart;
4386 return;
4387 }
4388 #endif
4389 /*
4390 * Try to reclaim some free descriptors..
4391 */
4392 if (ri->ri_free < 2)
4393 tulip_tx_intr(sc);
4394 if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
4395 sc->tulip_flags |= TULIP_WANTTXSTART;
4396 sc->tulip_if.if_start = tulip_ifstart;
4397 return;
4398 }
4399 bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
4400 sizeof(sc->tulip_setupbuf));
4401 /*
4402 * Clear WANTSETUP and set DOINGSETUP. Set know that WANTSETUP is
4403 * set and DOINGSETUP is clear doing an XOR of the two will DTRT.
4404 */
4405 sc->tulip_flags ^= TULIP_WANTSETUP|TULIP_DOINGSETUP;
4406 ri->ri_free--;
4407 #if defined(__mips__)
4408 pci_sync_cache(sc->tulip_pc, (vm_offset_t)sc->tulip_setupbuf, sizeof(sc->tulip_setupbuf));
4409 #endif
4410 nextout = ri->ri_nextout;
4411 {
4412 u_int32_t d_flag;
4413 tulip_desc_bitfield_t u;
4414
4415 u.f = DESC_BO(nextout->u.f); /* copy the bitfields */
4416 d_flag = u.bd_flag;
4417
4418 d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
4419 d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
4420 |TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
4421 if (sc->tulip_flags & TULIP_WANTHASHPERFECT)
4422 d_flag |= TULIP_DFLAG_TxHASHFILT;
4423 else if (sc->tulip_flags & TULIP_WANTHASHONLY)
4424 d_flag |= TULIP_DFLAG_TxHASHFILT|TULIP_DFLAG_TxINVRSFILT;
4425
4426
4427 u.bd_flag = d_flag;
4428 u.bd_length1 = sizeof(sc->tulip_setupbuf);
4429 u.bd_length2 = 0;
4430
4431 nextout->u.f = DESC_BO(u.f);
4432 nextout->d_addr1 = DESC_BO(TULIP_KVATOPHYS(sc, sc->tulip_setupbuf));
4433 nextout->d_addr2 = 0; /* no need to swab */
4434 }
4435
4436 /*
4437 * Advance the ring for the next transmit packet.
4438 */
4439 if (++ri->ri_nextout == ri->ri_last)
4440 ri->ri_nextout = ri->ri_first;
4441
4442 /*
4443 * Make sure the next descriptor is owned by us since it
4444 * may have been set up above if we ran out of room in the
4445 * ring.
4446 */
4447 ri->ri_nextout->d_status = 0; /* doesn't need swab - dsr */
4448 nextout->d_status = DESC_BO(TULIP_DSTS_OWNER);
4449 TULIP_CSR_WRITE(sc, csr_txpoll, 1);
4450 if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
4451 sc->tulip_intrmask |= TULIP_STS_TXINTR;
4452 TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
4453 }
4454 }
4455
4456
4457 /*
4458 * This routine is entered at splnet() (splsoftnet() on NetBSD)
4459 * and thereby imposes no problems when TULIP_USE_SOFTINTR is
4460 * defined or not.
4461 */
4462 static int
tulip_ifioctl(struct ifnet * ifp,ioctl_cmd_t cmd,caddr_t data)4463 tulip_ifioctl(
4464 struct ifnet * ifp,
4465 ioctl_cmd_t cmd,
4466 caddr_t data)
4467 {
4468 TULIP_PERFSTART(ifioctl)
4469 tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
4470 struct ifaddr *ifa = (struct ifaddr *)data;
4471 struct ifreq *ifr = (struct ifreq *) data;
4472 tulip_spl_t s;
4473 int error = 0;
4474
4475 #if defined(TULIP_USE_SOFTINTR)
4476 s = TULIP_RAISESOFTSPL();
4477 #else
4478 s = TULIP_RAISESPL();
4479 #endif
4480 switch (cmd) {
4481 case SIOCSIFADDR: {
4482 ifp->if_flags |= IFF_UP;
4483 switch(ifa->ifa_addr->sa_family) {
4484 #ifdef INET
4485 case AF_INET: {
4486 tulip_init(sc);
4487 TULIP_ARP_IFINIT(sc, ifa);
4488 break;
4489 }
4490 #endif /* INET */
4491
4492 #ifdef NS
4493 /*
4494 * This magic copied from if_is.c; I don't use XNS,
4495 * so I have no way of telling if this actually
4496 * works or not.
4497 */
4498 case AF_NS: {
4499 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
4500 if (ns_nullhost(*ina)) {
4501 ina->x_host = *(union ns_host *)(sc->tulip_enaddr);
4502 } else {
4503 ifp->if_flags &= ~IFF_RUNNING;
4504 bcopy((caddr_t)ina->x_host.c_host,
4505 (caddr_t)sc->tulip_enaddr,
4506 sizeof(sc->tulip_enaddr));
4507 }
4508 tulip_init(sc);
4509 break;
4510 }
4511 #endif /* NS */
4512
4513 default: {
4514 tulip_init(sc);
4515 break;
4516 }
4517 }
4518 break;
4519 }
4520 case SIOCGIFADDR: {
4521 bcopy((caddr_t) sc->tulip_enaddr,
4522 (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
4523 6);
4524 break;
4525 }
4526
4527 case SIOCSIFFLAGS: {
4528 #if !defined(IFM_ETHER)
4529 int flags = 0;
4530 if (ifp->if_flags & IFF_LINK0) flags |= 1;
4531 if (ifp->if_flags & IFF_LINK1) flags |= 2;
4532 if (ifp->if_flags & IFF_LINK2) flags |= 4;
4533 if (flags == 7) {
4534 ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1|IFF_LINK2);
4535 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
4536 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
4537 sc->tulip_flags &= ~(TULIP_WANTRXACT|TULIP_LINKUP|TULIP_NOAUTOSENSE);
4538 tulip_reset(sc);
4539 } else if (flags) {
4540 tulip_media_t media;
4541 for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
4542 if (sc->tulip_mediums[media] != NULL && --flags == 0) {
4543 sc->tulip_flags |= TULIP_NOAUTOSENSE;
4544 if (sc->tulip_media != media || (sc->tulip_flags & TULIP_DIDNWAY)) {
4545 sc->tulip_flags &= ~TULIP_DIDNWAY;
4546 tulip_linkup(sc, media);
4547 }
4548 break;
4549 }
4550 }
4551 if (flags)
4552 printf(TULIP_PRINTF_FMT ": ignored invalid media request\n", TULIP_PRINTF_ARGS);
4553 }
4554 #endif
4555 tulip_init(sc);
4556 break;
4557 }
4558
4559 #if defined(SIOCSIFMEDIA)
4560 case SIOCSIFMEDIA:
4561 case SIOCGIFMEDIA: {
4562 error = ifmedia_ioctl(ifp, ifr, &sc->tulip_ifmedia, cmd);
4563 break;
4564 }
4565 #endif
4566
4567 case SIOCADDMULTI:
4568 case SIOCDELMULTI: {
4569 /*
4570 * Update multicast listeners
4571 */
4572 if (cmd == SIOCADDMULTI)
4573 error = ether_addmulti(ifr, TULIP_ETHERCOM(sc));
4574 else
4575 error = ether_delmulti(ifr, TULIP_ETHERCOM(sc));
4576
4577 if (error == ENETRESET) {
4578 tulip_addr_filter(sc); /* reset multicast filtering */
4579 tulip_init(sc);
4580 error = 0;
4581 }
4582 break;
4583 }
4584 case SIOCSIFMTU:
4585 /*
4586 * Set the interface MTU.
4587 */
4588 if (ifr->ifr_mtu > ETHERMTU
4589 #ifdef BIG_PACKET
4590 && sc->tulip_chipid != TULIP_21140
4591 && sc->tulip_chipid != TULIP_21140A
4592 && sc->tulip_chipid != TULIP_21041
4593 #endif
4594 ) {
4595 error = EINVAL;
4596 break;
4597 }
4598 ifp->if_mtu = ifr->ifr_mtu;
4599 #ifdef BIG_PACKET
4600 tulip_reset(sc);
4601 tulip_init(sc);
4602 #endif
4603 break;
4604
4605 #ifdef SIOCGADDRROM
4606 case SIOCGADDRROM: {
4607 error = copyout(sc->tulip_rombuf, ifr->ifr_data, sizeof(sc->tulip_rombuf));
4608 break;
4609 }
4610 #endif
4611 #ifdef SIOCGCHIPID
4612 case SIOCGCHIPID: {
4613 ifr->ifr_metric = (int) sc->tulip_chipid;
4614 break;
4615 }
4616 #endif
4617 default: {
4618 error = EINVAL;
4619 break;
4620 }
4621 }
4622
4623 TULIP_RESTORESPL(s);
4624 TULIP_PERFEND(ifioctl);
4625 return error;
4626 }
4627
4628 #if 1 /* ALTQ */
4629 /*
4630 * the original dequeueing policy is dequeue-and-prepend if something
4631 * goes wrong. when altq is used, it is changed to peek-and-dequeue.
4632 * the modification becomes a bit complicated since tulip_txput() might
4633 * copy and modify the mbuf passed.
4634 */
4635 #endif
4636 /*
4637 * These routines gets called at device spl (from ether_output). This might
4638 * pose a problem for TULIP_USE_SOFTINTR if ether_output is called at
4639 * device spl from another driver.
4640 */
4641
4642 static ifnet_ret_t
tulip_ifstart(struct ifnet * const ifp)4643 tulip_ifstart(
4644 struct ifnet * const ifp)
4645 {
4646 TULIP_PERFSTART(ifstart)
4647 tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
4648
4649 if (sc->tulip_if.if_flags & IFF_RUNNING) {
4650
4651 if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
4652 tulip_txput_setup(sc);
4653
4654 while (!IFQ_IS_EMPTY(&sc->tulip_if.if_snd)) {
4655 struct mbuf *m, *m0;
4656 IFQ_POLL(&sc->tulip_if.if_snd, m);
4657 if (m == NULL)
4658 break;
4659 if ((m0 = tulip_txput(sc, m)) != NULL) {
4660 if (m0 != m)
4661 /* should not happen */
4662 printf("tulip_if_start: txput failed!\n");
4663 break;
4664 }
4665 }
4666 #ifdef ALTQ
4667 if (0) /* don't switch to the one packet mode */
4668 #else
4669 if (IFQ_IS_EMPTY(&sc->tulip_if.if_snd))
4670 #endif
4671 sc->tulip_if.if_start = tulip_ifstart_one;
4672 }
4673
4674 TULIP_PERFEND(ifstart);
4675 }
4676
4677 static ifnet_ret_t
tulip_ifstart_one(struct ifnet * const ifp)4678 tulip_ifstart_one(
4679 struct ifnet * const ifp)
4680 {
4681 TULIP_PERFSTART(ifstart_one)
4682 tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
4683
4684 if ((sc->tulip_if.if_flags & IFF_RUNNING)
4685 && !IFQ_IS_EMPTY(&sc->tulip_if.if_snd)) {
4686 struct mbuf *m, *m0;
4687 IFQ_POLL(&sc->tulip_if.if_snd, m);
4688 if (m != NULL && (m0 = tulip_txput(sc, m)) != NULL)
4689 if (m0 != m)
4690 /* should not happen */
4691 printf("tulip_if_start_one: txput failed!\n");
4692 }
4693 TULIP_PERFEND(ifstart_one);
4694 }
4695
4696 /*
4697 * Even though this routine runs at device spl, it does not break
4698 * our use of splnet (splsoftnet under NetBSD) for the majority
4699 * of this driver (if TULIP_USE_SOFTINTR defined) since
4700 * if_watcbog is called from if_watchdog which is called from
4701 * splsoftclock which is below spl[soft]net.
4702 */
4703 static void
tulip_ifwatchdog(struct ifnet * ifp)4704 tulip_ifwatchdog(
4705 struct ifnet *ifp)
4706 {
4707 TULIP_PERFSTART(ifwatchdog)
4708 tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
4709
4710 #if defined(TULIP_DEBUG)
4711 u_int32_t rxintrs = sc->tulip_dbg.dbg_rxintrs - sc->tulip_dbg.dbg_last_rxintrs;
4712 if (rxintrs > sc->tulip_dbg.dbg_high_rxintrs_hz)
4713 sc->tulip_dbg.dbg_high_rxintrs_hz = rxintrs;
4714 sc->tulip_dbg.dbg_last_rxintrs = sc->tulip_dbg.dbg_rxintrs;
4715 #endif /* TULIP_DEBUG */
4716
4717 sc->tulip_if.if_timer = 1;
4718 /*
4719 * These should be rare so do a bulk test up front so we can just skip
4720 * them if needed.
4721 */
4722 if (sc->tulip_flags & (TULIP_SYSTEMERROR|TULIP_RXBUFSLOW|TULIP_NOMESSAGES)) {
4723 /*
4724 * If the number of receive buffer is low, try to refill
4725 */
4726 if (sc->tulip_flags & TULIP_RXBUFSLOW)
4727 tulip_rx_intr(sc);
4728
4729 if (sc->tulip_flags & TULIP_SYSTEMERROR) {
4730 printf(TULIP_PRINTF_FMT ": %d system errors: last was %s\n",
4731 TULIP_PRINTF_ARGS, sc->tulip_system_errors,
4732 tulip_system_errors[sc->tulip_last_system_error]);
4733 }
4734 if (sc->tulip_statusbits) {
4735 tulip_print_abnormal_interrupt(sc, sc->tulip_statusbits);
4736 sc->tulip_statusbits = 0;
4737 }
4738
4739 sc->tulip_flags &= ~(TULIP_NOMESSAGES|TULIP_SYSTEMERROR);
4740 }
4741
4742 if (sc->tulip_txtimer)
4743 tulip_tx_intr(sc);
4744 if (sc->tulip_txtimer && --sc->tulip_txtimer == 0) {
4745 printf(TULIP_PRINTF_FMT ": transmission timeout\n", TULIP_PRINTF_ARGS);
4746 if (TULIP_DO_AUTOSENSE(sc)) {
4747 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
4748 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
4749 sc->tulip_flags &= ~(TULIP_WANTRXACT|TULIP_LINKUP);
4750 }
4751 tulip_reset(sc);
4752 tulip_init(sc);
4753 }
4754
4755 TULIP_PERFEND(ifwatchdog);
4756 TULIP_PERFMERGE(sc, perf_intr_cycles);
4757 TULIP_PERFMERGE(sc, perf_ifstart_cycles);
4758 TULIP_PERFMERGE(sc, perf_ifioctl_cycles);
4759 TULIP_PERFMERGE(sc, perf_ifwatchdog_cycles);
4760 TULIP_PERFMERGE(sc, perf_timeout_cycles);
4761 TULIP_PERFMERGE(sc, perf_ifstart_one_cycles);
4762 TULIP_PERFMERGE(sc, perf_txput_cycles);
4763 TULIP_PERFMERGE(sc, perf_txintr_cycles);
4764 TULIP_PERFMERGE(sc, perf_rxintr_cycles);
4765 TULIP_PERFMERGE(sc, perf_rxget_cycles);
4766 TULIP_PERFMERGE(sc, perf_intr);
4767 TULIP_PERFMERGE(sc, perf_ifstart);
4768 TULIP_PERFMERGE(sc, perf_ifioctl);
4769 TULIP_PERFMERGE(sc, perf_ifwatchdog);
4770 TULIP_PERFMERGE(sc, perf_timeout);
4771 TULIP_PERFMERGE(sc, perf_ifstart_one);
4772 TULIP_PERFMERGE(sc, perf_txput);
4773 TULIP_PERFMERGE(sc, perf_txintr);
4774 TULIP_PERFMERGE(sc, perf_rxintr);
4775 TULIP_PERFMERGE(sc, perf_rxget);
4776 }
4777
4778 #if defined(__bsdi__) || (defined(__FreeBSD__) && BSD < 199506)
4779 static ifnet_ret_t
tulip_ifwatchdog_wrapper(int unit)4780 tulip_ifwatchdog_wrapper(
4781 int unit)
4782 {
4783 tulip_ifwatchdog(&TULIP_UNIT_TO_SOFTC(unit)->tulip_if);
4784 }
4785 #define tulip_ifwatchdog tulip_ifwatchdog_wrapper
4786 #endif
4787
4788 /*
4789 * All printf's are real as of now!
4790 */
4791 #ifdef printf
4792 #undef printf
4793 #endif
4794 #if !defined(IFF_NOTRAILERS)
4795 #define IFF_NOTRAILERS 0
4796 #endif
4797
4798 static void
tulip_attach(tulip_softc_t * const sc)4799 tulip_attach(
4800 tulip_softc_t * const sc)
4801 {
4802 struct ifnet * const ifp = &sc->tulip_if;
4803
4804 ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
4805 ifp->if_ioctl = tulip_ifioctl;
4806 ifp->if_start = tulip_ifstart;
4807 ifp->if_watchdog = tulip_ifwatchdog;
4808 ifp->if_timer = 1;
4809 #if !defined(__bsdi__) || _BSDI_VERSION < 199401
4810 ifp->if_output = ether_output;
4811 #endif
4812 #if defined(__bsdi__) && _BSDI_VERSION < 199401
4813 ifp->if_mtu = ETHERMTU;
4814 #endif
4815
4816 #ifdef __OpenBSD__
4817 timeout_set(&sc->tulip_stmo, tulip_timeout_callback, sc);
4818 #if defined(TULIP_NEED_FASTTIMEOUT)
4819 timeout_set(&sc->tulip_ftmo, tulip_fasttimeout_callback, sc);
4820 #endif
4821 #endif
4822
4823 #if defined(__bsdi__) && _BSDI_VERSION >= 199510
4824 aprint_naive(": DEC Ethernet");
4825 aprint_normal(": %s%s", sc->tulip_boardid,
4826 tulip_chipdescs[sc->tulip_chipid]);
4827 aprint_verbose(" pass %d.%d", (sc->tulip_revinfo & 0xF0) >> 4,
4828 sc->tulip_revinfo & 0x0F);
4829 printf("\n");
4830 sc->tulip_pf = aprint_normal;
4831 aprint_normal(TULIP_PRINTF_FMT ": address " TULIP_EADDR_FMT "\n",
4832 TULIP_PRINTF_ARGS,
4833 TULIP_EADDR_ARGS(sc->tulip_enaddr));
4834 #else
4835 printf(
4836 #if defined(__bsdi__)
4837 "\n"
4838 #endif
4839 TULIP_PRINTF_FMT ": %s%s pass %d.%d%s address " TULIP_EADDR_FMT "\n",
4840 TULIP_PRINTF_ARGS,
4841 sc->tulip_boardid,
4842 tulip_chipdescs[sc->tulip_chipid],
4843 (sc->tulip_revinfo & 0xF0) >> 4,
4844 sc->tulip_revinfo & 0x0F,
4845 (sc->tulip_features & (TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM))
4846 == TULIP_HAVE_ISVSROM ? " (invalid EESPROM checksum)" : "",
4847 TULIP_EADDR_ARGS(sc->tulip_enaddr));
4848 #endif
4849
4850 #if defined(__alpha__)
4851 /*
4852 * In case the SRM console told us about a bogus media,
4853 * we need to check to be safe.
4854 */
4855 if (sc->tulip_mediums[sc->tulip_media] == NULL)
4856 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
4857 #endif
4858
4859 (*sc->tulip_boardsw->bd_media_probe)(sc);
4860 #if defined(IFM_ETHER)
4861 ifmedia_init(&sc->tulip_ifmedia, 0,
4862 tulip_ifmedia_change,
4863 tulip_ifmedia_status);
4864 #else
4865 {
4866 tulip_media_t media;
4867 int cnt;
4868 printf(TULIP_PRINTF_FMT ": media:", TULIP_PRINTF_ARGS);
4869 for (media = TULIP_MEDIA_UNKNOWN, cnt = 1; cnt < 7 && media < TULIP_MEDIA_MAX; media++) {
4870 if (sc->tulip_mediums[media] != NULL) {
4871 printf(" %d=\"%s\"", cnt, tulip_mediums[media]);
4872 cnt++;
4873 }
4874 }
4875 if (cnt == 1) {
4876 sc->tulip_features |= TULIP_HAVE_NOMEDIA;
4877 printf(" none\n");
4878 } else {
4879 printf("\n");
4880 }
4881 }
4882 #endif
4883 sc->tulip_flags &= ~TULIP_DEVICEPROBE;
4884 #if defined(IFM_ETHER)
4885 tulip_ifmedia_add(sc);
4886 #endif
4887
4888 tulip_reset(sc);
4889
4890 IFQ_SET_READY(&ifp->if_snd);
4891 #if defined(__bsdi__) && _BSDI_VERSION >= 199510
4892 sc->tulip_pf = printf;
4893 TULIP_ETHER_IFATTACH(sc);
4894 #else
4895 if_attach(ifp);
4896 #if defined(__NetBSD__) || defined(__OpenBSD__) || (defined(__FreeBSD__) && BSD >= 199506)
4897 TULIP_ETHER_IFATTACH(sc);
4898 #endif
4899 #endif /* __bsdi__ */
4900
4901 #if NBPFILTER > 0
4902 TULIP_BPF_ATTACH(sc);
4903 #endif
4904
4905 #if defined(__NetBSD__) && NRND > 0
4906 rnd_attach_source(&sc->tulip_rndsource, sc->tulip_dev.dv_xname,
4907 RND_TYPE_NET);
4908 #endif
4909 }
4910
4911 static void
tulip_initcsrs(tulip_softc_t * const sc,tulip_csrptr_t csr_base,size_t csr_size)4912 tulip_initcsrs(
4913 tulip_softc_t * const sc,
4914 tulip_csrptr_t csr_base,
4915 size_t csr_size)
4916 {
4917 sc->tulip_csrs.csr_busmode = csr_base + 0 * csr_size;
4918 sc->tulip_csrs.csr_txpoll = csr_base + 1 * csr_size;
4919 sc->tulip_csrs.csr_rxpoll = csr_base + 2 * csr_size;
4920 sc->tulip_csrs.csr_rxlist = csr_base + 3 * csr_size;
4921 sc->tulip_csrs.csr_txlist = csr_base + 4 * csr_size;
4922 sc->tulip_csrs.csr_status = csr_base + 5 * csr_size;
4923 sc->tulip_csrs.csr_command = csr_base + 6 * csr_size;
4924 sc->tulip_csrs.csr_intr = csr_base + 7 * csr_size;
4925 sc->tulip_csrs.csr_missed_frames = csr_base + 8 * csr_size;
4926 sc->tulip_csrs.csr_9 = csr_base + 9 * csr_size;
4927 sc->tulip_csrs.csr_10 = csr_base + 10 * csr_size;
4928 sc->tulip_csrs.csr_11 = csr_base + 11 * csr_size;
4929 sc->tulip_csrs.csr_12 = csr_base + 12 * csr_size;
4930 sc->tulip_csrs.csr_13 = csr_base + 13 * csr_size;
4931 sc->tulip_csrs.csr_14 = csr_base + 14 * csr_size;
4932 sc->tulip_csrs.csr_15 = csr_base + 15 * csr_size;
4933 #if defined(TULIP_EISA)
4934 sc->tulip_csrs.csr_enetrom = csr_base + DE425_ENETROM_OFFSET;
4935 #endif
4936 }
4937
4938 static void
tulip_initring(tulip_softc_t * const sc,tulip_ringinfo_t * const ri,tulip_desc_t * descs,int ndescs)4939 tulip_initring(
4940 tulip_softc_t * const sc,
4941 tulip_ringinfo_t * const ri,
4942 tulip_desc_t *descs,
4943 int ndescs)
4944 {
4945 #if defined(__mips__)
4946 tulip_desc_t *xdesc = descs;
4947 /*
4948 * Someone moved the descriptors into the softc struct.
4949 * Avoid cache line conflicts by aligning on cache line.
4950 */
4951 descs = (tulip_desc_t *)(roundup((int)descs, 16));
4952 if(xdesc != descs) {
4953 ndescs--;
4954 }
4955 pci_sync_cache(sc->tulip_pc, (vm_offset_t)descs, ndescs * sizeof(tulip_desc_t));
4956 descs = (tulip_desc_t *)TULIP_KVATOPHYS(sc, descs);
4957 descs = (tulip_desc_t *)PHYS_TO_UNCACHED((int)descs & 0x3fffffff);
4958 #endif
4959 #ifdef PPC_MPC106_BUG
4960 /*
4961 * This is a workaround for the powerpc MPC106 chip not snooping
4962 * accesses to the descriptor area correctly.
4963 */
4964 tulip_desc_t *xdesc = descs;
4965 xdesc = (tulip_desc_t *)(roundup((int)descs, 32) - 4);
4966 if(xdesc < descs) {
4967 ndescs--;
4968 xdesc++;
4969 }
4970 descs = xdesc;
4971 #endif
4972 ri->ri_max = ndescs;
4973 ri->ri_first = descs;
4974 ri->ri_last = ri->ri_first + ri->ri_max;
4975 bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
4976 {
4977 tulip_desc_bitfield_t u;
4978
4979 u.f = DESC_BO( ri->ri_last[-1].u.f );
4980 u.bd_flag = TULIP_DFLAG_ENDRING;
4981 ri->ri_last[-1].u.f = DESC_BO(u.f);
4982 }
4983 }
4984
4985 /*
4986 * This is the PCI configuration support. Since the 21040 is available
4987 * on both EISA and PCI boards, one must be careful in how defines the
4988 * 21040 in the config file.
4989 */
4990
4991 #define PCI_CFID 0x00 /* Configuration ID */
4992 #define PCI_CFCS 0x04 /* Configurtion Command/Status */
4993 #define PCI_CFRV 0x08 /* Configuration Revision */
4994 #define PCI_CFLT 0x0c /* Configuration Latency Timer */
4995 #define PCI_CBIO 0x10 /* Configuration Base IO Address */
4996 #define PCI_CBMA 0x14 /* Configuration Base Memory Address */
4997 #define PCI_CFIT 0x3c /* Configuration Interrupt */
4998 #define PCI_CFDA 0x40 /* Configuration Driver Area */
4999
5000 #if defined(TULIP_EISA)
5001 static const int tulip_eisa_irqs[4] = { IRQ5, IRQ9, IRQ10, IRQ11 };
5002 #endif
5003
5004 #if defined(__FreeBSD__)
5005
5006 #define TULIP_PCI_ATTACH_ARGS pcici_t config_id, int unit
5007 #define TULIP_SHUTDOWN_ARGS int howto, void * arg
5008
5009 #if defined(TULIP_DEVCONF)
5010 static void tulip_shutdown(TULIP_SHUTDOWN_ARGS);
5011
5012 static int
tulip_pci_shutdown(struct kern_devconf * const kdc,int force)5013 tulip_pci_shutdown(
5014 struct kern_devconf * const kdc,
5015 int force)
5016 {
5017 if (kdc->kdc_unit < TULIP_MAX_DEVICES) {
5018 tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(kdc->kdc_unit);
5019 if (sc != NULL)
5020 tulip_shutdown(0, sc);
5021 }
5022 (void) dev_detach(kdc);
5023 return 0;
5024 }
5025 #endif
5026
5027 static char *
tulip_pci_probe(pcici_t config_id,pcidi_t device_id)5028 tulip_pci_probe(
5029 pcici_t config_id,
5030 pcidi_t device_id)
5031 {
5032 if (PCI_VENDORID(device_id) != DEC_VENDORID)
5033 return NULL;
5034 if (PCI_CHIPID(device_id) == CHIPID_21040)
5035 return "Digital 21040 Ethernet";
5036 if (PCI_CHIPID(device_id) == CHIPID_21041)
5037 return "Digital 21041 Ethernet";
5038 if (PCI_CHIPID(device_id) == CHIPID_21140) {
5039 u_int32_t revinfo = pci_conf_read(config_id, PCI_CFRV) & 0xFF;
5040 if (revinfo >= 0x20)
5041 return "Digital 21140A Fast Ethernet";
5042 else
5043 return "Digital 21140 Fast Ethernet";
5044 }
5045 if (PCI_CHIPID(device_id) == CHIPID_21142) {
5046 u_int32_t revinfo = pci_conf_read(config_id, PCI_CFRV) & 0xFF;
5047 if (revinfo >= 0x20)
5048 return "Digital 21143 Fast Ethernet";
5049 else
5050 return "Digital 21142 Fast Ethernet";
5051 }
5052 return NULL;
5053 }
5054
5055 static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
5056 static u_long tulip_pci_count;
5057
5058 struct pci_device dedevice = {
5059 "de",
5060 tulip_pci_probe,
5061 tulip_pci_attach,
5062 &tulip_pci_count,
5063 #if defined(TULIP_DEVCONF)
5064 tulip_pci_shutdown,
5065 #endif
5066 };
5067
5068 DATA_SET (pcidevice_set, dedevice);
5069 #endif /* __FreeBSD__ */
5070
5071 #if defined(__bsdi__)
5072 #define TULIP_PCI_ATTACH_ARGS struct device * const parent, struct device * const self, void * const aux
5073 #define TULIP_SHUTDOWN_ARGS void *arg
5074
5075 static int
tulip_pci_match(pci_devaddr_t * pa)5076 tulip_pci_match(
5077 pci_devaddr_t *pa)
5078 {
5079 int irq;
5080 unsigned id;
5081
5082 id = pci_inl(pa, PCI_VENDOR_ID);
5083 if (PCI_VENDORID(id) != DEC_VENDORID)
5084 return 0;
5085 id = PCI_CHIPID(id);
5086 if (id != CHIPID_21040 && id != CHIPID_21041
5087 && id != CHIPID_21140 && id != CHIPID_21142)
5088 return 0;
5089 irq = pci_inl(pa, PCI_I_LINE) & 0xFF;
5090 if (irq == 0 || irq >= 16) {
5091 printf("de?: invalid IRQ %d; skipping\n", irq);
5092 return 0;
5093 }
5094 return 1;
5095 }
5096
5097 static int
tulip_probe(struct device * parent,struct cfdata * cf,void * aux)5098 tulip_probe(
5099 struct device *parent,
5100 struct cfdata *cf,
5101 void *aux)
5102 {
5103 struct isa_attach_args * const ia = (struct isa_attach_args *) aux;
5104 unsigned irq, slot;
5105 pci_devaddr_t *pa;
5106
5107 #if _BSDI_VERSION >= 199401
5108 switch (ia->ia_bustype) {
5109 case BUS_PCI:
5110 #endif
5111 pa = pci_scan(tulip_pci_match);
5112 if (pa == NULL)
5113 return 0;
5114
5115 irq = (1 << (pci_inl(pa, PCI_I_LINE) & 0xFF));
5116
5117 /* Get the base address; assume the BIOS set it up correctly */
5118 #if defined(TULIP_IOMAPPED)
5119 ia->ia_maddr = NULL;
5120 ia->ia_msize = 0;
5121 ia->ia_iobase = pci_inl(pa, PCI_CBIO) & ~7;
5122 pci_outl(pa, PCI_CBIO, 0xFFFFFFFF);
5123 ia->ia_iosize = ((~pci_inl(pa, PCI_CBIO)) | 7) + 1;
5124 pci_outl(pa, PCI_CBIO, (int) ia->ia_iobase);
5125
5126 /* Disable memory space access */
5127 pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~2);
5128 #else
5129 ia->ia_maddr = (caddr_t) (pci_inl(pa, PCI_CBMA) & ~7);
5130 pci_outl(pa, PCI_CBMA, 0xFFFFFFFF);
5131 ia->ia_msize = ((~pci_inl(pa, PCI_CBMA)) | 7) + 1;
5132 pci_outl(pa, PCI_CBMA, (int) ia->ia_maddr);
5133 ia->ia_iobase = 0;
5134 ia->ia_iosize = 0;
5135
5136 /* Disable I/O space access */
5137 pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~1);
5138 #endif /* TULIP_IOMAPPED */
5139
5140 ia->ia_aux = (void *) pa;
5141 #if _BSDI_VERSION >= 199401
5142 break;
5143
5144 #if defined(TULIP_EISA)
5145 case BUS_EISA: {
5146 unsigned tmp;
5147
5148 if ((slot = eisa_match(cf, ia)) == 0)
5149 return 0;
5150 ia->ia_iobase = slot << 12;
5151 ia->ia_iosize = EISA_NPORT;
5152 eisa_slotalloc(slot);
5153 tmp = inb(ia->ia_iobase + DE425_CFG0);
5154 irq = tulip_eisa_irqs[(tmp >> 1) & 0x03];
5155 /*
5156 * Until BSD/OS likes level interrupts, force
5157 * the DE425 into edge-triggered mode.
5158 */
5159 if ((tmp & 1) == 0)
5160 outb(ia->ia_iobase + DE425_CFG0, tmp | 1);
5161 /*
5162 * CBIO needs to map to the EISA slot
5163 * enable I/O access and Master
5164 */
5165 outl(ia->ia_iobase + DE425_CBIO, ia->ia_iobase);
5166 outl(ia->ia_iobase + DE425_CFCS, 5 | inl(ia->ia_iobase + DE425_CFCS));
5167 ia->ia_aux = NULL;
5168 break;
5169 }
5170 #endif /* TULIP_EISA */
5171 default:
5172 return 0;
5173 }
5174 #endif
5175
5176 /* PCI bus masters don't use host DMA channels */
5177 ia->ia_drq = DRQNONE;
5178
5179 if (ia->ia_irq != IRQUNK && irq != ia->ia_irq) {
5180 printf("de%d: error: desired IRQ of %d does not match device's "
5181 "actual IRQ of %d,\n",
5182 cf->cf_unit,
5183 ffs(ia->ia_irq) - 1, ffs(irq) - 1);
5184 return 0;
5185 }
5186 if (ia->ia_irq == IRQUNK)
5187 ia->ia_irq = irq;
5188 #ifdef IRQSHARE
5189 ia->ia_irq |= IRQSHARE;
5190 #endif
5191 return 1;
5192 }
5193
5194 static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
5195
5196 #if defined(TULIP_EISA)
5197 static char *tulip_eisa_ids[] = {
5198 "DEC4250",
5199 NULL
5200 };
5201 #endif
5202
5203 struct cfdriver decd = {
5204 0, "de", tulip_probe, tulip_pci_attach,
5205 #if _BSDI_VERSION >= 199401
5206 DV_IFNET,
5207 #endif
5208 sizeof(tulip_softc_t),
5209 #if defined(TULIP_EISA)
5210 tulip_eisa_ids
5211 #endif
5212 };
5213
5214 #endif /* __bsdi__ */
5215
5216
5217 #if defined(__NetBSD__) || defined(__OpenBSD__)
5218 #define TULIP_PCI_ATTACH_ARGS struct device * const parent, struct device * const self, void * const aux
5219 #define TULIP_SHUTDOWN_ARGS void *arg
5220 static int
tulip_pci_probe(struct device * parent,void * match,void * aux)5221 tulip_pci_probe(
5222 struct device *parent,
5223 #ifdef __BROKEN_INDIRECT_CONFIG
5224 void *match,
5225 #else
5226 struct cfdata *match,
5227 #endif
5228 void *aux)
5229 {
5230 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
5231
5232 if (PCI_VENDORID(pa->pa_id) != DEC_VENDORID)
5233 return 0;
5234 if (PCI_CHIPID(pa->pa_id) == CHIPID_21040
5235 || PCI_CHIPID(pa->pa_id) == CHIPID_21041
5236 || PCI_CHIPID(pa->pa_id) == CHIPID_21140
5237 || PCI_CHIPID(pa->pa_id) == CHIPID_21142)
5238 return 2;
5239
5240 return 0;
5241 }
5242
5243 static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
5244
5245 struct cfattach de_ca = {
5246 sizeof(tulip_softc_t), tulip_pci_probe, tulip_pci_attach
5247 };
5248
5249 struct cfdriver de_cd = {
5250 0, "de", DV_IFNET
5251 };
5252
5253 #endif /* __NetBSD__ */
5254
5255 static void
tulip_shutdown(TULIP_SHUTDOWN_ARGS)5256 tulip_shutdown(
5257 TULIP_SHUTDOWN_ARGS)
5258 {
5259 tulip_softc_t * const sc = arg;
5260 TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
5261 DELAY(10); /* Wait 10 microseconds (actually 50 PCI cycles but at
5262 33MHz that comes to two microseconds but wait a
5263 bit longer anyways) */
5264 }
5265
5266 static void
tulip_pci_attach(TULIP_PCI_ATTACH_ARGS)5267 tulip_pci_attach(
5268 TULIP_PCI_ATTACH_ARGS)
5269 {
5270 #if defined(__FreeBSD__)
5271 tulip_softc_t *sc;
5272 #define PCI_CONF_WRITE(r, v) pci_conf_write(config_id, (r), (v))
5273 #define PCI_CONF_READ(r) pci_conf_read(config_id, (r))
5274 #define PCI_GETBUSDEVINFO(sc) ((void)((sc)->tulip_pci_busno = ((config_id.cfg1 >> 16) & 0xFF), /* XXX */ \
5275 (sc)->tulip_pci_devno = ((config_id.cfg1 >> 11) & 0x1F))) /* XXX */
5276 #endif
5277 #if defined(__bsdi__)
5278 tulip_softc_t * const sc = (tulip_softc_t *) self;
5279 struct isa_attach_args * const ia = (struct isa_attach_args *) aux;
5280 pci_devaddr_t *pa = (pci_devaddr_t *) ia->ia_aux;
5281 const int unit = sc->tulip_dev.dv_unit;
5282 #define PCI_CONF_WRITE(r, v) pci_outl(pa, (r), (v))
5283 #define PCI_CONF_READ(r) pci_inl(pa, (r))
5284 #define PCI_GETBUSDEVINFO(sc) ((void)((sc)->tulip_pci_busno = pa->d_bus, \
5285 (sc)->tulip_pci_devno = pa->d_agent))
5286 #endif
5287 #if defined(__NetBSD__) || defined(__OpenBSD__)
5288 tulip_softc_t * const sc = (tulip_softc_t *) self;
5289 struct pci_attach_args * const pa = (struct pci_attach_args *) aux;
5290 const int unit = sc->tulip_dev.dv_unit;
5291 bus_space_tag_t iot, memt;
5292 bus_space_handle_t ioh, memh;
5293 int ioh_valid, memh_valid;
5294 #define PCI_CONF_WRITE(r, v) pci_conf_write(pa->pa_pc, pa->pa_tag, (r), (v))
5295 #define PCI_CONF_READ(r) pci_conf_read(pa->pa_pc, pa->pa_tag, (r))
5296 #define PCI_GETBUSDEVINFO(sc) do { \
5297 int busno, devno, funcno; \
5298 pci_decompose_tag(pa->pa_pc, pa->pa_tag, &busno, &devno, &funcno); \
5299 (sc)->tulip_pci_busno = busno; \
5300 (sc)->tulip_pci_devno = devno; \
5301 } while (0)
5302 #endif /* __NetBSD__ */
5303
5304 int retval, idx;
5305 u_int32_t revinfo, cfdainfo, id;
5306 #if !defined(TULIP_IOMAPPED) && defined(__FreeBSD__)
5307 vm_offset_t pa_csrs;
5308 #endif
5309 unsigned csroffset = TULIP_PCI_CSROFFSET;
5310 unsigned csrsize = TULIP_PCI_CSRSIZE;
5311 tulip_csrptr_t csr_base;
5312 tulip_chipid_t chipid = TULIP_CHIPID_UNKNOWN;
5313
5314 if (unit >= TULIP_MAX_DEVICES) {
5315 #ifdef __FreeBSD__
5316 printf("de%d", unit);
5317 #endif
5318 printf(": not configured; limit of %d reached or exceeded\n",
5319 TULIP_MAX_DEVICES);
5320 return;
5321 }
5322
5323 #if defined(__bsdi__)
5324 if (pa != NULL) {
5325 revinfo = pci_inl(pa, PCI_CFRV) & 0xFF;
5326 id = pci_inl(pa, PCI_CFID);
5327 cfdainfo = pci_inl(pa, PCI_CFDA);
5328 #if defined(TULIP_EISA)
5329 } else {
5330 revinfo = inl(ia->ia_iobase + DE425_CFRV) & 0xFF;
5331 csroffset = TULIP_EISA_CSROFFSET;
5332 csrsize = TULIP_EISA_CSRSIZE;
5333 chipid = TULIP_DE425;
5334 cfdainfo = 0;
5335 #endif /* TULIP_EISA */
5336 }
5337 #else /* __bsdi__ */
5338 revinfo = PCI_CONF_READ(PCI_CFRV) & 0xFF;
5339 id = PCI_CONF_READ(PCI_CFID);
5340 cfdainfo = PCI_CONF_READ(PCI_CFDA);
5341 #endif /* __bsdi__ */
5342
5343 if (PCI_VENDORID(id) == DEC_VENDORID) {
5344 if (PCI_CHIPID(id) == CHIPID_21040)
5345 chipid = TULIP_21040;
5346 else if (PCI_CHIPID(id) == CHIPID_21041)
5347 chipid = TULIP_21041;
5348 else if (PCI_CHIPID(id) == CHIPID_21140)
5349 chipid = (revinfo >= 0x20) ? TULIP_21140A : TULIP_21140;
5350 else if (PCI_CHIPID(id) == CHIPID_21142)
5351 chipid = (revinfo >= 0x20) ? TULIP_21143 : TULIP_21142;
5352 }
5353
5354 if (chipid == TULIP_CHIPID_UNKNOWN)
5355 return;
5356
5357 if ((chipid == TULIP_21040 || chipid == TULIP_DE425) && revinfo < 0x20) {
5358 #ifdef __FreeBSD__
5359 printf("de%d", unit);
5360 #endif
5361 printf(": not configured; 21040 pass 2.0 required (%d.%d found)\n",
5362 revinfo >> 4, revinfo & 0x0f);
5363 return;
5364 } else if (chipid == TULIP_21140 && revinfo < 0x11) {
5365 #ifndef __FreeBSD__
5366 printf("\n");
5367 #endif
5368 printf("de%d: not configured; 21140 pass 1.1 required (%d.%d found)\n",
5369 unit, revinfo >> 4, revinfo & 0x0f);
5370 return;
5371 }
5372
5373 #if defined(__FreeBSD__)
5374 sc = (tulip_softc_t *) malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
5375 if (sc == NULL)
5376 return;
5377 bzero(sc, sizeof(*sc)); /* Zero out the softc*/
5378 #endif
5379
5380 PCI_GETBUSDEVINFO(sc);
5381 sc->tulip_chipid = chipid;
5382 sc->tulip_flags |= TULIP_DEVICEPROBE;
5383 if (chipid == TULIP_21140 || chipid == TULIP_21140A)
5384 sc->tulip_features |= TULIP_HAVE_GPR|TULIP_HAVE_STOREFWD;
5385 if (chipid == TULIP_21140A && revinfo <= 0x22)
5386 sc->tulip_features |= TULIP_HAVE_RXBADOVRFLW;
5387 if (chipid == TULIP_21140)
5388 sc->tulip_features |= TULIP_HAVE_BROKEN_HASH;
5389 if (chipid != TULIP_21040 && chipid != TULIP_DE425 && chipid != TULIP_21140)
5390 sc->tulip_features |= TULIP_HAVE_POWERMGMT;
5391 if (chipid == TULIP_21041 || chipid == TULIP_21142 || chipid == TULIP_21143) {
5392 sc->tulip_features |= TULIP_HAVE_DUALSENSE;
5393 if (chipid != TULIP_21041 || revinfo >= 0x20)
5394 sc->tulip_features |= TULIP_HAVE_SIANWAY;
5395 if (chipid != TULIP_21041)
5396 sc->tulip_features |= TULIP_HAVE_SIAGP|TULIP_HAVE_RXBADOVRFLW|TULIP_HAVE_STOREFWD;
5397 if (chipid != TULIP_21041 && revinfo >= 0x20)
5398 sc->tulip_features |= TULIP_HAVE_SIA100;
5399 }
5400
5401 if (sc->tulip_features & TULIP_HAVE_POWERMGMT
5402 && (cfdainfo & (TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE))) {
5403 cfdainfo &= ~(TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE);
5404 PCI_CONF_WRITE(PCI_CFDA, cfdainfo);
5405 DELAY(11*1000);
5406 }
5407
5408 #if defined(__OpenBSD__)
5409 /* XXX - csapuntz@lcs.mit.edu
5410 OpenBSD + our PCI motherboard underflows consistently */
5411 if (sc->tulip_features & TULIP_HAVE_STOREFWD)
5412 sc->tulip_cmdmode |= TULIP_CMD_STOREFWD;
5413 #endif
5414
5415
5416 #if defined(__alpha__) && defined(__NetBSD__)
5417 /*
5418 * The Alpha SRM console encodes a console set media in the driver
5419 * part of the CFDA register. Note that the Multia presents a
5420 * problem in that its BNC mode is really EXTSIA. So in that case
5421 * force a probe.
5422 */
5423 switch ((cfdainfo >> 8) & 0xff) {
5424 case 1: media = chipid > TULIP_DE425 ?
5425 TULIP_MEDIA_AUI : TULIP_MEDIA_AUIBNC; break;
5426 case 2: media = chipid > TULIP_DE425 ?
5427 TULIP_MEDIA_BNC : TULIP_MEDIA_UNKNOWN; break;
5428 case 3: media = TULIP_MEDIA_10BASET; break;
5429 case 4: media = TULIP_MEDIA_10BASET_FD; break;
5430 case 5: media = TULIP_MEDIA_100BASETX; break;
5431 case 6: media = TULIP_MEDIA_100BASETX_FD; break;
5432 }
5433 #endif
5434
5435 #if defined(__NetBSD__) || defined(__OpenBSD__)
5436 bcopy(self->dv_xname, sc->tulip_if.if_xname, IFNAMSIZ);
5437 sc->tulip_if.if_softc = sc;
5438 sc->tulip_pc = pa->pa_pc;
5439 #else
5440 sc->tulip_unit = unit;
5441 sc->tulip_name = "de";
5442 #endif
5443 sc->tulip_revinfo = revinfo;
5444 #if defined(__FreeBSD__)
5445 #if BSD >= 199506
5446 sc->tulip_if.if_softc = sc;
5447 #endif
5448 #if defined(TULIP_IOMAPPED)
5449 retval = pci_map_port(config_id, PCI_CBIO, &csr_base);
5450 #else
5451 retval = pci_map_mem(config_id, PCI_CBMA, (vm_offset_t *) &csr_base, &pa_csrs);
5452 #endif
5453 if (!retval) {
5454 free((caddr_t) sc, M_DEVBUF);
5455 return;
5456 }
5457 tulips[unit] = sc;
5458 #endif /* __FreeBSD__ */
5459
5460 #if defined(__bsdi__)
5461 sc->tulip_pf = printf;
5462 #if defined(TULIP_IOMAPPED)
5463 csr_base = ia->ia_iobase;
5464 #else
5465 csr_base = (vm_offset_t) mapphys((vm_offset_t) ia->ia_maddr, ia->ia_msize);
5466 #endif
5467 #endif /* __bsdi__ */
5468
5469 #if defined(__NetBSD__) || defined(__OpenBSD__)
5470 csr_base = 0;
5471
5472 ioh_valid = (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
5473 &iot, &ioh, NULL, NULL, 0) == 0);
5474 memh_valid = (pci_mapreg_map(pa, PCI_CBMA,
5475 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
5476 &memt, &memh, NULL, NULL, 0) == 0);
5477 #endif
5478
5479 #if defined(__OpenBSD__) || defined(__NetBSD__)
5480 #if defined(TULIP_IOMAPPED)
5481 if (ioh_valid) {
5482 sc->tulip_bustag = iot;
5483 sc->tulip_bushandle = ioh;
5484 } else if (memh_valid) {
5485 sc->tulip_bustag = memt;
5486 sc->tulip_bushandle = memh;
5487 }
5488 #else /* defined(TULIP_IOMAPPED) */
5489 if (memh_valid) {
5490 sc->tulip_bustag = memt;
5491 sc->tulip_bushandle = memh;
5492 } else if (ioh_valid) {
5493 sc->tulip_bustag = iot;
5494 sc->tulip_bushandle = ioh;
5495 }
5496 #endif /* TULIP_IOMAPPED */
5497 else {
5498 printf(": unable to map device registers\n");
5499 return;
5500 }
5501 #endif /* __NetBSD__ */
5502
5503 tulip_initcsrs(sc, csr_base + csroffset, csrsize);
5504 tulip_initring(sc, &sc->tulip_rxinfo, sc->tulip_rxdescs, TULIP_RXDESCS);
5505 tulip_initring(sc, &sc->tulip_txinfo, sc->tulip_txdescs, TULIP_TXDESCS);
5506
5507 /*
5508 * Make sure there won't be any interrupts or such...
5509 */
5510 TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
5511 DELAY(100); /* Wait 10 microseconds (actually 50 PCI cycles but at
5512 33MHz that comes to two microseconds but wait a
5513 bit longer anyways) */
5514
5515 if ((retval = tulip_read_macaddr(sc)) < 0) {
5516 #if defined(__FreeBSD__)
5517 printf(TULIP_PRINTF_FMT, TULIP_PRINTF_ARGS);
5518 #endif
5519 printf(": can't read ENET ROM (why=%d) (", retval);
5520 for (idx = 0; idx < 32; idx++)
5521 printf("%02x", sc->tulip_rombuf[idx]);
5522 printf("\n");
5523 printf(TULIP_PRINTF_FMT ": %s%s pass %d.%d address unknown",
5524 TULIP_PRINTF_ARGS,
5525 sc->tulip_boardid, tulip_chipdescs[sc->tulip_chipid],
5526 (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F);
5527 } else {
5528 tulip_spl_t s;
5529 tulip_intrfunc_t (*intr_rtn)(void *) = tulip_intr_normal;
5530
5531 if (sc->tulip_features & TULIP_HAVE_SHAREDINTR)
5532 intr_rtn = tulip_intr_shared;
5533
5534 #if defined(__NetBSD__) || defined(__OpenBSD__)
5535 if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
5536 pci_intr_handle_t intrhandle;
5537 const char *intrstr;
5538
5539 if (pci_intr_map(pa, &intrhandle)) {
5540 printf(", couldn't map interrupt\n");
5541 return;
5542 }
5543 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
5544 sc->tulip_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
5545 intr_rtn, sc, self->dv_xname);
5546 if (sc->tulip_ih == NULL) {
5547 printf(", couldn't establish interrupt");
5548 if (intrstr != NULL)
5549 printf(" at %s", intrstr);
5550 printf("\n");
5551 return;
5552 }
5553 printf(": %s", intrstr);
5554 }
5555 printf("\n");
5556 sc->tulip_ats = shutdownhook_establish(tulip_shutdown, sc);
5557 if (sc->tulip_ats == NULL)
5558 printf("%s: warning: couldn't establish shutdown hook\n",
5559 sc->tulip_xname);
5560 #endif
5561 #if defined(__FreeBSD__)
5562 if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
5563 if (!pci_map_int (config_id, intr_rtn, (void *) sc, &net_imask)) {
5564 printf(TULIP_PRINTF_FMT ": couldn't map interrupt\n",
5565 TULIP_PRINTF_ARGS);
5566 return;
5567 }
5568 }
5569 #if !defined(TULIP_DEVCONF)
5570 at_shutdown(tulip_shutdown, sc, SHUTDOWN_POST_SYNC);
5571 #endif
5572 #endif
5573 #if defined(__bsdi__)
5574 if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
5575 isa_establish(&sc->tulip_id, &sc->tulip_dev);
5576
5577 sc->tulip_ih.ih_fun = intr_rtn;
5578 sc->tulip_ih.ih_arg = (void *) sc;
5579 intr_establish(ia->ia_irq, &sc->tulip_ih, DV_NET);
5580 }
5581
5582 sc->tulip_ats.func = tulip_shutdown;
5583 sc->tulip_ats.arg = (void *) sc;
5584 atshutdown(&sc->tulip_ats, ATSH_ADD);
5585 #endif
5586 #if defined(TULIP_USE_SOFTINTR)
5587 if (sc->tulip_unit > tulip_softintr_max_unit)
5588 tulip_softintr_max_unit = sc->tulip_unit;
5589 #endif
5590
5591 s = TULIP_RAISESPL();
5592 tulip_reset(sc);
5593 tulip_attach(sc);
5594 #if defined(__alpha__) && defined(__NetBSD__)
5595 if (media != TULIP_MEDIA_UNKNOWN)
5596 tulip_linkup(sc, media);
5597 #endif
5598 TULIP_RESTORESPL(s);
5599 }
5600 }
5601