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