am7990.c revision 1.35 1 /* $NetBSD: am7990.c,v 1.35 1997/04/28 17:04:03 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1997 Jason R. Thorpe. All rights reserved.
5 * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Ralph Campbell and Rick Macklem.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
41 */
42
43 #include "bpfilter.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/syslog.h>
49 #include <sys/socket.h>
50 #include <sys/device.h>
51 #include <sys/malloc.h>
52 #include <sys/ioctl.h>
53 #include <sys/errno.h>
54
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_ether.h>
58 #include <net/if_media.h>
59
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/if_inarp.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66 #endif
67
68 #ifdef NS
69 #include <netns/ns.h>
70 #include <netns/ns_if.h>
71 #endif
72
73 #if defined(CCITT) && defined(LLC)
74 #include <sys/socketvar.h>
75 #include <netccitt/x25.h>
76 #include <netccitt/pk.h>
77 #include <netccitt/pk_var.h>
78 #include <netccitt/pk_extern.h>
79 #endif
80
81 #if NBPFILTER > 0
82 #include <net/bpf.h>
83 #include <net/bpfdesc.h>
84 #endif
85
86 #include <dev/ic/am7990reg.h>
87 #include <dev/ic/am7990var.h>
88
89 #ifdef LEDEBUG
90 void am7990_recv_print __P((struct am7990_softc *, int));
91 void am7990_xmit_print __P((struct am7990_softc *, int));
92 #endif
93
94 integrate void am7990_rint __P((struct am7990_softc *));
95 integrate void am7990_tint __P((struct am7990_softc *));
96
97 integrate int am7990_put __P((struct am7990_softc *, int, struct mbuf *));
98 integrate struct mbuf *am7990_get __P((struct am7990_softc *, int, int));
99 integrate void am7990_read __P((struct am7990_softc *, int, int));
100
101 hide void am7990_shutdown __P((void *));
102
103 int am7990_mediachange __P((struct ifnet *));
104 void am7990_mediastatus __P((struct ifnet *, struct ifmediareq *));
105
106 #define ifp (&sc->sc_ethercom.ec_if)
107
108 static inline int ether_cmp __P((char *, char *));
109
110 /*
111 * Compare two Ether/802 addresses for equality, inlined and
112 * unrolled for speed.
113 * Note: test last byte of ethernet address first, as it will
114 * likely have the most random distribution.
115 */
116 static inline int
117 ether_cmp(a, b)
118 char *a, *b;
119 {
120
121 return ((a[5] != b[5]) || (a[4] != b[4]) || (a[3] != b[3]) ||
122 (a[2] != b[2]) || (a[1] != b[1]) || (a[0] != b[0]));
123 }
124
125 #define ETHER_CMP ether_cmp
126
127 /*
128 * am7990 configuration driver. Attachments are provided by
129 * machine-dependent driver front-ends.
130 */
131 struct cfdriver le_cd = {
132 NULL, "le", DV_IFNET
133 };
134
135 void
136 am7990_config(sc)
137 struct am7990_softc *sc;
138 {
139 int mem, i;
140
141 /* Make sure the chip is stopped. */
142 am7990_stop(sc);
143
144 /* Initialize ifnet structure. */
145 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
146 ifp->if_softc = sc;
147 ifp->if_start = am7990_start;
148 ifp->if_ioctl = am7990_ioctl;
149 ifp->if_watchdog = am7990_watchdog;
150 ifp->if_flags =
151 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
152 #ifdef LANCE_REVC_BUG
153 ifp->if_flags &= ~IFF_MULTICAST;
154 #endif
155
156 /* Initialize ifmedia structures. */
157 ifmedia_init(&sc->sc_media, 0, am7990_mediachange, am7990_mediastatus);
158 if (sc->sc_supmedia != NULL) {
159 for (i = 0; i < sc->sc_nsupmedia; i++)
160 ifmedia_add(&sc->sc_media, sc->sc_supmedia[i],
161 0, NULL);
162 ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
163 } else {
164 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
165 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
166 }
167
168 /* Attach the interface. */
169 if_attach(ifp);
170 ether_ifattach(ifp, sc->sc_enaddr);
171
172 #if NBPFILTER > 0
173 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
174 #endif
175
176 switch (sc->sc_memsize) {
177 case 8192:
178 sc->sc_nrbuf = 4;
179 sc->sc_ntbuf = 1;
180 break;
181 case 16384:
182 sc->sc_nrbuf = 8;
183 sc->sc_ntbuf = 2;
184 break;
185 case 32768:
186 sc->sc_nrbuf = 16;
187 sc->sc_ntbuf = 4;
188 break;
189 case 65536:
190 sc->sc_nrbuf = 32;
191 sc->sc_ntbuf = 8;
192 break;
193 case 131072:
194 sc->sc_nrbuf = 64;
195 sc->sc_ntbuf = 16;
196 break;
197 default:
198 panic("am7990_config: weird memory size");
199 }
200
201 printf(": address %s\n", ether_sprintf(sc->sc_enaddr));
202 printf("%s: %d receive buffers, %d transmit buffers\n",
203 sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf);
204
205 sc->sc_sh = shutdownhook_establish(am7990_shutdown, sc);
206 if (sc->sc_sh == NULL)
207 panic("am7990_config: can't establish shutdownhook");
208 sc->sc_rbufaddr = malloc(sc->sc_nrbuf * sizeof(int), M_DEVBUF,
209 M_WAITOK);
210 sc->sc_tbufaddr = malloc(sc->sc_ntbuf * sizeof(int), M_DEVBUF,
211 M_WAITOK);
212
213 mem = 0;
214 sc->sc_initaddr = mem;
215 mem += sizeof(struct leinit);
216 sc->sc_rmdaddr = mem;
217 mem += sizeof(struct lermd) * sc->sc_nrbuf;
218 sc->sc_tmdaddr = mem;
219 mem += sizeof(struct letmd) * sc->sc_ntbuf;
220 for (i = 0; i < sc->sc_nrbuf; i++, mem += LEBLEN)
221 sc->sc_rbufaddr[i] = mem;
222 for (i = 0; i < sc->sc_ntbuf; i++, mem += LEBLEN)
223 sc->sc_tbufaddr[i] = mem;
224 #ifdef notyet
225 if (mem > ...)
226 panic(...);
227 #endif
228 }
229
230 void
231 am7990_reset(sc)
232 struct am7990_softc *sc;
233 {
234 int s;
235
236 s = splimp();
237 am7990_init(sc);
238 splx(s);
239 }
240
241 /*
242 * Set up the initialization block and the descriptor rings.
243 */
244 void
245 am7990_meminit(sc)
246 register struct am7990_softc *sc;
247 {
248 u_long a;
249 int bix;
250 struct leinit init;
251 struct lermd rmd;
252 struct letmd tmd;
253 u_int8_t *myaddr;
254
255 #if NBPFILTER > 0
256 if (ifp->if_flags & IFF_PROMISC)
257 init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
258 else
259 #endif
260 init.init_mode = LE_MODE_NORMAL;
261 if (sc->sc_initmodemedia == 1)
262 init.init_mode |= LE_MODE_PSEL0;
263
264 myaddr = LLADDR(ifp->if_sadl);
265 init.init_padr[0] = (myaddr[1] << 8) | myaddr[0];
266 init.init_padr[1] = (myaddr[3] << 8) | myaddr[2];
267 init.init_padr[2] = (myaddr[5] << 8) | myaddr[4];
268 am7990_setladrf(&sc->sc_ethercom, init.init_ladrf);
269
270 sc->sc_last_rd = 0;
271 sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
272
273 a = sc->sc_addr + LE_RMDADDR(sc, 0);
274 init.init_rdra = a;
275 init.init_rlen = (a >> 16) | ((ffs(sc->sc_nrbuf) - 1) << 13);
276
277 a = sc->sc_addr + LE_TMDADDR(sc, 0);
278 init.init_tdra = a;
279 init.init_tlen = (a >> 16) | ((ffs(sc->sc_ntbuf) - 1) << 13);
280
281 (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
282
283 /*
284 * Set up receive ring descriptors.
285 */
286 for (bix = 0; bix < sc->sc_nrbuf; bix++) {
287 a = sc->sc_addr + LE_RBUFADDR(sc, bix);
288 rmd.rmd0 = a;
289 rmd.rmd1_hadr = a >> 16;
290 rmd.rmd1_bits = LE_R1_OWN;
291 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
292 rmd.rmd3 = 0;
293 (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
294 sizeof(rmd));
295 }
296
297 /*
298 * Set up transmit ring descriptors.
299 */
300 for (bix = 0; bix < sc->sc_ntbuf; bix++) {
301 a = sc->sc_addr + LE_TBUFADDR(sc, bix);
302 tmd.tmd0 = a;
303 tmd.tmd1_hadr = a >> 16;
304 tmd.tmd1_bits = 0;
305 tmd.tmd2 = 0 | LE_XMD2_ONES;
306 tmd.tmd3 = 0;
307 (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
308 sizeof(tmd));
309 }
310 }
311
312 void
313 am7990_stop(sc)
314 struct am7990_softc *sc;
315 {
316
317 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
318 }
319
320 /*
321 * Initialization of interface; set up initialization block
322 * and transmit/receive descriptor rings.
323 */
324 void
325 am7990_init(sc)
326 register struct am7990_softc *sc;
327 {
328 register int timo;
329 u_long a;
330
331 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
332 DELAY(100);
333
334 /* Newer LANCE chips have a reset register */
335 if (sc->sc_hwreset)
336 (*sc->sc_hwreset)(sc);
337
338 /* Set the correct byte swapping mode, etc. */
339 (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
340
341 /* Set up LANCE init block. */
342 am7990_meminit(sc);
343
344 /* Give LANCE the physical address of its init block. */
345 a = sc->sc_addr + LE_INITADDR(sc);
346 (*sc->sc_wrcsr)(sc, LE_CSR1, a);
347 (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
348
349 /* Try to initialize the LANCE. */
350 DELAY(100);
351 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
352
353 /* Wait for initialization to finish. */
354 for (timo = 100000; timo; timo--)
355 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
356 break;
357
358 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
359 /* Start the LANCE. */
360 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT |
361 LE_C0_IDON);
362 ifp->if_flags |= IFF_RUNNING;
363 ifp->if_flags &= ~IFF_OACTIVE;
364 ifp->if_timer = 0;
365 am7990_start(ifp);
366 } else
367 printf("%s: card failed to initialize\n", sc->sc_dev.dv_xname);
368 if (sc->sc_hwinit)
369 (*sc->sc_hwinit)(sc);
370 }
371
372 /*
373 * Routine to copy from mbuf chain to transmit buffer in
374 * network buffer memory.
375 */
376 integrate int
377 am7990_put(sc, boff, m)
378 struct am7990_softc *sc;
379 int boff;
380 register struct mbuf *m;
381 {
382 register struct mbuf *n;
383 register int len, tlen = 0;
384
385 for (; m; m = n) {
386 len = m->m_len;
387 if (len == 0) {
388 MFREE(m, n);
389 continue;
390 }
391 (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
392 boff += len;
393 tlen += len;
394 MFREE(m, n);
395 }
396 if (tlen < LEMINSIZE) {
397 (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
398 tlen = LEMINSIZE;
399 }
400 return (tlen);
401 }
402
403 /*
404 * Pull data off an interface.
405 * Len is length of data, with local net header stripped.
406 * We copy the data into mbufs. When full cluster sized units are present
407 * we copy into clusters.
408 */
409 integrate struct mbuf *
410 am7990_get(sc, boff, totlen)
411 struct am7990_softc *sc;
412 int boff, totlen;
413 {
414 register struct mbuf *m;
415 struct mbuf *top, **mp;
416 int len;
417
418 MGETHDR(m, M_DONTWAIT, MT_DATA);
419 if (m == 0)
420 return (0);
421 m->m_pkthdr.rcvif = ifp;
422 m->m_pkthdr.len = totlen;
423 len = MHLEN;
424 top = 0;
425 mp = ⊤
426
427 while (totlen > 0) {
428 if (top) {
429 MGET(m, M_DONTWAIT, MT_DATA);
430 if (m == 0) {
431 m_freem(top);
432 return 0;
433 }
434 len = MLEN;
435 }
436 if (totlen >= MINCLSIZE) {
437 MCLGET(m, M_DONTWAIT);
438 if ((m->m_flags & M_EXT) == 0) {
439 m_free(m);
440 m_freem(top);
441 return 0;
442 }
443 len = MCLBYTES;
444 }
445 if (!top) {
446 register int pad =
447 ALIGN(sizeof(struct ether_header)) -
448 sizeof(struct ether_header);
449 m->m_data += pad;
450 len -= pad;
451 }
452 m->m_len = len = min(totlen, len);
453 (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
454 boff += len;
455 totlen -= len;
456 *mp = m;
457 mp = &m->m_next;
458 }
459
460 return (top);
461 }
462
463 /*
464 * Pass a packet to the higher levels.
465 */
466 integrate void
467 am7990_read(sc, boff, len)
468 register struct am7990_softc *sc;
469 int boff, len;
470 {
471 struct mbuf *m;
472 struct ether_header *eh;
473
474 if (len <= sizeof(struct ether_header) ||
475 len > ETHERMTU + sizeof(struct ether_header)) {
476 #ifdef LEDEBUG
477 printf("%s: invalid packet size %d; dropping\n",
478 sc->sc_dev.dv_xname, len);
479 #endif
480 ifp->if_ierrors++;
481 return;
482 }
483
484 /* Pull packet off interface. */
485 m = am7990_get(sc, boff, len);
486 if (m == 0) {
487 ifp->if_ierrors++;
488 return;
489 }
490
491 ifp->if_ipackets++;
492
493 /* We assume that the header fit entirely in one mbuf. */
494 eh = mtod(m, struct ether_header *);
495
496 #if NBPFILTER > 0
497 /*
498 * Check if there's a BPF listener on this interface.
499 * If so, hand off the raw packet to BPF.
500 */
501 if (ifp->if_bpf) {
502 bpf_mtap(ifp->if_bpf, m);
503
504 #ifndef LANCE_REVC_BUG
505 /*
506 * Note that the interface cannot be in promiscuous mode if
507 * there are no BPF listeners. And if we are in promiscuous
508 * mode, we have to check if this packet is really ours.
509 */
510 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
511 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
512 ETHER_CMP(eh->ether_dhost, LLADDR(ifp->if_sadl))) {
513 m_freem(m);
514 return;
515 }
516 #endif
517 }
518 #endif
519
520 #ifdef LANCE_REVC_BUG
521 /*
522 * The old LANCE (Rev. C) chips have a bug which causes
523 * garbage to be inserted in front of the received packet.
524 * The work-around is to ignore packets with an invalid
525 * destination address (garbage will usually not match).
526 * Of course, this precludes multicast support...
527 */
528 if (ETHER_CMP(eh->ether_dhost, LLADDR(ifp->if_sadl)) &&
529 ETHER_CMP(eh->ether_dhost, etherbroadcastaddr)) {
530 m_freem(m);
531 return;
532 }
533 #endif
534
535 /* Pass the packet up, with the ether header sort-of removed. */
536 m_adj(m, sizeof(struct ether_header));
537 ether_input(ifp, eh, m);
538 }
539
540 integrate void
541 am7990_rint(sc)
542 struct am7990_softc *sc;
543 {
544 register int bix;
545 int rp;
546 struct lermd rmd;
547
548 bix = sc->sc_last_rd;
549
550 /* Process all buffers with valid data. */
551 for (;;) {
552 rp = LE_RMDADDR(sc, bix);
553 (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
554
555 if (rmd.rmd1_bits & LE_R1_OWN)
556 break;
557
558 if (rmd.rmd1_bits & LE_R1_ERR) {
559 if (rmd.rmd1_bits & LE_R1_ENP) {
560 #ifdef LEDEBUG
561 if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) {
562 if (rmd.rmd1_bits & LE_R1_FRAM)
563 printf("%s: framing error\n",
564 sc->sc_dev.dv_xname);
565 if (rmd.rmd1_bits & LE_R1_CRC)
566 printf("%s: crc mismatch\n",
567 sc->sc_dev.dv_xname);
568 }
569 #endif
570 } else {
571 if (rmd.rmd1_bits & LE_R1_OFLO)
572 printf("%s: overflow\n",
573 sc->sc_dev.dv_xname);
574 }
575 if (rmd.rmd1_bits & LE_R1_BUFF)
576 printf("%s: receive buffer error\n",
577 sc->sc_dev.dv_xname);
578 ifp->if_ierrors++;
579 } else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) !=
580 (LE_R1_STP | LE_R1_ENP)) {
581 printf("%s: dropping chained buffer\n",
582 sc->sc_dev.dv_xname);
583 ifp->if_ierrors++;
584 } else {
585 #ifdef LEDEBUG
586 if (sc->sc_debug)
587 am7990_recv_print(sc, sc->sc_last_rd);
588 #endif
589 am7990_read(sc, LE_RBUFADDR(sc, bix),
590 (int)rmd.rmd3 - 4);
591 }
592
593 rmd.rmd1_bits = LE_R1_OWN;
594 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
595 rmd.rmd3 = 0;
596 (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
597
598 #ifdef LEDEBUG
599 if (sc->sc_debug)
600 printf("sc->sc_last_rd = %x, rmd: "
601 "ladr %04x, hadr %02x, flags %02x, "
602 "bcnt %04x, mcnt %04x\n",
603 sc->sc_last_rd,
604 rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits,
605 rmd.rmd2, rmd.rmd3);
606 #endif
607
608 if (++bix == sc->sc_nrbuf)
609 bix = 0;
610 }
611
612 sc->sc_last_rd = bix;
613 }
614
615 integrate void
616 am7990_tint(sc)
617 register struct am7990_softc *sc;
618 {
619 register int bix;
620 struct letmd tmd;
621
622 bix = sc->sc_first_td;
623
624 for (;;) {
625 if (sc->sc_no_td <= 0)
626 break;
627
628 #ifdef LEDEBUG
629 if (sc->sc_debug)
630 printf("trans tmd: "
631 "ladr %04x, hadr %02x, flags %02x, "
632 "bcnt %04x, mcnt %04x\n",
633 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits,
634 tmd.tmd2, tmd.tmd3);
635 #endif
636
637 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
638 sizeof(tmd));
639
640 if (tmd.tmd1_bits & LE_T1_OWN)
641 break;
642
643 ifp->if_flags &= ~IFF_OACTIVE;
644
645 if (tmd.tmd1_bits & LE_T1_ERR) {
646 if (tmd.tmd3 & LE_T3_BUFF)
647 printf("%s: transmit buffer error\n",
648 sc->sc_dev.dv_xname);
649 else if (tmd.tmd3 & LE_T3_UFLO)
650 printf("%s: underflow\n", sc->sc_dev.dv_xname);
651 if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) {
652 am7990_reset(sc);
653 return;
654 }
655 if (tmd.tmd3 & LE_T3_LCAR) {
656 sc->sc_havecarrier = 0;
657 if (sc->sc_nocarrier)
658 (*sc->sc_nocarrier)(sc);
659 else
660 printf("%s: lost carrier\n",
661 sc->sc_dev.dv_xname);
662 }
663 if (tmd.tmd3 & LE_T3_LCOL)
664 ifp->if_collisions++;
665 if (tmd.tmd3 & LE_T3_RTRY) {
666 printf("%s: excessive collisions, tdr %d\n",
667 sc->sc_dev.dv_xname,
668 tmd.tmd3 & LE_T3_TDR_MASK);
669 ifp->if_collisions += 16;
670 }
671 ifp->if_oerrors++;
672 } else {
673 if (tmd.tmd1_bits & LE_T1_ONE)
674 ifp->if_collisions++;
675 else if (tmd.tmd1_bits & LE_T1_MORE)
676 /* Real number is unknown. */
677 ifp->if_collisions += 2;
678 ifp->if_opackets++;
679 }
680
681 if (++bix == sc->sc_ntbuf)
682 bix = 0;
683
684 --sc->sc_no_td;
685 }
686
687 sc->sc_first_td = bix;
688
689 am7990_start(ifp);
690
691 if (sc->sc_no_td == 0)
692 ifp->if_timer = 0;
693 }
694
695 /*
696 * Controller interrupt.
697 */
698 int
699 am7990_intr(arg)
700 register void *arg;
701 {
702 register struct am7990_softc *sc = arg;
703 register u_int16_t isr;
704
705 isr = (*sc->sc_rdcsr)(sc, LE_CSR0) | sc->sc_saved_csr0;
706 sc->sc_saved_csr0 = 0;
707 #ifdef LEDEBUG
708 if (sc->sc_debug)
709 printf("%s: am7990_intr entering with isr=%04x\n",
710 sc->sc_dev.dv_xname, isr);
711 #endif
712 if ((isr & LE_C0_INTR) == 0)
713 return (0);
714
715 (*sc->sc_wrcsr)(sc, LE_CSR0,
716 isr & (LE_C0_INEA | LE_C0_BABL | LE_C0_MISS | LE_C0_MERR |
717 LE_C0_RINT | LE_C0_TINT | LE_C0_IDON));
718 if (isr & LE_C0_ERR) {
719 if (isr & LE_C0_BABL) {
720 #ifdef LEDEBUG
721 printf("%s: babble\n", sc->sc_dev.dv_xname);
722 #endif
723 ifp->if_oerrors++;
724 }
725 #if 0
726 if (isr & LE_C0_CERR) {
727 printf("%s: collision error\n", sc->sc_dev.dv_xname);
728 ifp->if_collisions++;
729 }
730 #endif
731 if (isr & LE_C0_MISS) {
732 #ifdef LEDEBUG
733 printf("%s: missed packet\n", sc->sc_dev.dv_xname);
734 #endif
735 ifp->if_ierrors++;
736 }
737 if (isr & LE_C0_MERR) {
738 printf("%s: memory error\n", sc->sc_dev.dv_xname);
739 am7990_reset(sc);
740 return (1);
741 }
742 }
743
744 if ((isr & LE_C0_RXON) == 0) {
745 printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
746 ifp->if_ierrors++;
747 am7990_reset(sc);
748 return (1);
749 }
750 if ((isr & LE_C0_TXON) == 0) {
751 printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
752 ifp->if_oerrors++;
753 am7990_reset(sc);
754 return (1);
755 }
756
757 /*
758 * Pretend we have carrier; if we don't this will be cleared
759 * shortly.
760 */
761 sc->sc_havecarrier = 1;
762
763 if (isr & LE_C0_RINT)
764 am7990_rint(sc);
765 if (isr & LE_C0_TINT)
766 am7990_tint(sc);
767
768 return (1);
769 }
770
771 #undef ifp
772
773 void
774 am7990_watchdog(ifp)
775 struct ifnet *ifp;
776 {
777 struct am7990_softc *sc = ifp->if_softc;
778
779 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
780 ++ifp->if_oerrors;
781
782 am7990_reset(sc);
783 }
784
785 int
786 am7990_mediachange(ifp)
787 struct ifnet *ifp;
788 {
789 struct am7990_softc *sc = ifp->if_softc;
790
791 if (sc->sc_mediachange)
792 return ((*sc->sc_mediachange)(sc));
793 return (EINVAL);
794 }
795
796 void
797 am7990_mediastatus(ifp, ifmr)
798 struct ifnet *ifp;
799 struct ifmediareq *ifmr;
800 {
801 struct am7990_softc *sc = ifp->if_softc;
802
803 if ((ifp->if_flags & IFF_UP) == 0)
804 return;
805
806 ifmr->ifm_status = IFM_AVALID;
807 if (sc->sc_havecarrier)
808 ifmr->ifm_status |= IFM_ACTIVE;
809
810 if (sc->sc_mediastatus)
811 (*sc->sc_mediastatus)(sc, ifmr);
812 }
813
814 /*
815 * Setup output on interface.
816 * Get another datagram to send off of the interface queue, and map it to the
817 * interface before starting the output.
818 * Called only at splimp or interrupt level.
819 */
820 void
821 am7990_start(ifp)
822 register struct ifnet *ifp;
823 {
824 register struct am7990_softc *sc = ifp->if_softc;
825 register int bix;
826 register struct mbuf *m;
827 struct letmd tmd;
828 int rp;
829 int len;
830
831 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
832 return;
833
834 bix = sc->sc_last_td;
835
836 for (;;) {
837 rp = LE_TMDADDR(sc, bix);
838 (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
839
840 if (tmd.tmd1_bits & LE_T1_OWN) {
841 ifp->if_flags |= IFF_OACTIVE;
842 printf("missing buffer, no_td = %d, last_td = %d\n",
843 sc->sc_no_td, sc->sc_last_td);
844 }
845
846 IF_DEQUEUE(&ifp->if_snd, m);
847 if (m == 0)
848 break;
849
850 #if NBPFILTER > 0
851 /*
852 * If BPF is listening on this interface, let it see the packet
853 * before we commit it to the wire.
854 */
855 if (ifp->if_bpf)
856 bpf_mtap(ifp->if_bpf, m);
857 #endif
858
859 /*
860 * Copy the mbuf chain into the transmit buffer.
861 */
862 len = am7990_put(sc, LE_TBUFADDR(sc, bix), m);
863
864 #ifdef LEDEBUG
865 if (len > ETHERMTU + sizeof(struct ether_header))
866 printf("packet length %d\n", len);
867 #endif
868
869 ifp->if_timer = 5;
870
871 /*
872 * Init transmit registers, and set transmit start flag.
873 */
874 tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
875 tmd.tmd2 = -len | LE_XMD2_ONES;
876 tmd.tmd3 = 0;
877
878 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
879
880 #ifdef LEDEBUG
881 if (sc->sc_debug)
882 am7990_xmit_print(sc, sc->sc_last_td);
883 #endif
884
885 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
886
887 if (++bix == sc->sc_ntbuf)
888 bix = 0;
889
890 if (++sc->sc_no_td == sc->sc_ntbuf) {
891 ifp->if_flags |= IFF_OACTIVE;
892 break;
893 }
894
895 }
896
897 sc->sc_last_td = bix;
898 }
899
900 /*
901 * Process an ioctl request.
902 */
903 int
904 am7990_ioctl(ifp, cmd, data)
905 register struct ifnet *ifp;
906 u_long cmd;
907 caddr_t data;
908 {
909 register struct am7990_softc *sc = ifp->if_softc;
910 struct ifaddr *ifa = (struct ifaddr *)data;
911 struct ifreq *ifr = (struct ifreq *)data;
912 int s, error = 0;
913
914 s = splimp();
915
916 switch (cmd) {
917
918 case SIOCSIFADDR:
919 ifp->if_flags |= IFF_UP;
920
921 switch (ifa->ifa_addr->sa_family) {
922 #ifdef INET
923 case AF_INET:
924 am7990_init(sc);
925 arp_ifinit(ifp, ifa);
926 break;
927 #endif
928 #ifdef NS
929 case AF_NS:
930 {
931 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
932
933 if (ns_nullhost(*ina))
934 ina->x_host =
935 *(union ns_host *)LLADDR(ifp->if_sadl);
936 else {
937 bcopy(ina->x_host.c_host,
938 LLADDR(ifp->if_sadl),
939 sizeof(sc->sc_enaddr));
940 }
941 /* Set new address. */
942 am7990_init(sc);
943 break;
944 }
945 #endif
946 default:
947 am7990_init(sc);
948 break;
949 }
950 break;
951
952 #if defined(CCITT) && defined(LLC)
953 case SIOCSIFCONF_X25:
954 ifp->if_flags |= IFF_UP;
955 ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
956 error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
957 if (error == 0)
958 am7990_init(sc);
959 break;
960 #endif /* CCITT && LLC */
961
962 case SIOCSIFFLAGS:
963 if ((ifp->if_flags & IFF_UP) == 0 &&
964 (ifp->if_flags & IFF_RUNNING) != 0) {
965 /*
966 * If interface is marked down and it is running, then
967 * stop it.
968 */
969 am7990_stop(sc);
970 ifp->if_flags &= ~IFF_RUNNING;
971 } else if ((ifp->if_flags & IFF_UP) != 0 &&
972 (ifp->if_flags & IFF_RUNNING) == 0) {
973 /*
974 * If interface is marked up and it is stopped, then
975 * start it.
976 */
977 am7990_init(sc);
978 } else {
979 /*
980 * Reset the interface to pick up changes in any other
981 * flags that affect hardware registers.
982 */
983 /*am7990_stop(sc);*/
984 am7990_init(sc);
985 }
986 #ifdef LEDEBUG
987 if (ifp->if_flags & IFF_DEBUG)
988 sc->sc_debug = 1;
989 else
990 sc->sc_debug = 0;
991 #endif
992 break;
993
994 case SIOCADDMULTI:
995 case SIOCDELMULTI:
996 error = (cmd == SIOCADDMULTI) ?
997 ether_addmulti(ifr, &sc->sc_ethercom) :
998 ether_delmulti(ifr, &sc->sc_ethercom);
999
1000 if (error == ENETRESET) {
1001 /*
1002 * Multicast list has changed; set the hardware filter
1003 * accordingly.
1004 */
1005 am7990_reset(sc);
1006 error = 0;
1007 }
1008 break;
1009
1010 case SIOCGIFMEDIA:
1011 case SIOCSIFMEDIA:
1012 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1013 break;
1014
1015 default:
1016 error = EINVAL;
1017 break;
1018 }
1019
1020 splx(s);
1021 return (error);
1022 }
1023
1024 hide void
1025 am7990_shutdown(arg)
1026 void *arg;
1027 {
1028
1029 am7990_stop((struct am7990_softc *)arg);
1030 }
1031
1032 #ifdef LEDEBUG
1033 void
1034 am7990_recv_print(sc, no)
1035 struct am7990_softc *sc;
1036 int no;
1037 {
1038 struct lermd rmd;
1039 u_int16_t len;
1040 struct ether_header eh;
1041
1042 (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
1043 len = rmd.rmd3;
1044 printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
1045 len);
1046 printf("%s: status %04x\n", sc->sc_dev.dv_xname,
1047 (*sc->sc_rdcsr)(sc, LE_CSR0));
1048 printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
1049 sc->sc_dev.dv_xname,
1050 rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
1051 if (len >= sizeof(eh)) {
1052 (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
1053 printf("%s: dst %s", sc->sc_dev.dv_xname,
1054 ether_sprintf(eh.ether_dhost));
1055 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
1056 ntohs(eh.ether_type));
1057 }
1058 }
1059
1060 void
1061 am7990_xmit_print(sc, no)
1062 struct am7990_softc *sc;
1063 int no;
1064 {
1065 struct letmd tmd;
1066 u_int16_t len;
1067 struct ether_header eh;
1068
1069 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
1070 len = -tmd.tmd2;
1071 printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
1072 len);
1073 printf("%s: status %04x\n", sc->sc_dev.dv_xname,
1074 (*sc->sc_rdcsr)(sc, LE_CSR0));
1075 printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
1076 sc->sc_dev.dv_xname,
1077 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
1078 if (len >= sizeof(eh)) {
1079 (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
1080 printf("%s: dst %s", sc->sc_dev.dv_xname,
1081 ether_sprintf(eh.ether_dhost));
1082 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
1083 ntohs(eh.ether_type));
1084 }
1085 }
1086 #endif /* LEDEBUG */
1087
1088 /*
1089 * Set up the logical address filter.
1090 */
1091 void
1092 am7990_setladrf(ac, af)
1093 struct ethercom *ac;
1094 u_int16_t *af;
1095 {
1096 struct ifnet *ifp = &ac->ec_if;
1097 struct ether_multi *enm;
1098 register u_char *cp, c;
1099 register u_int32_t crc;
1100 register int i, len;
1101 struct ether_multistep step;
1102
1103 /*
1104 * Set up multicast address filter by passing all multicast addresses
1105 * through a crc generator, and then using the high order 6 bits as an
1106 * index into the 64 bit logical address filter. The high order bit
1107 * selects the word, while the rest of the bits select the bit within
1108 * the word.
1109 */
1110
1111 if (ifp->if_flags & IFF_PROMISC)
1112 goto allmulti;
1113
1114 af[0] = af[1] = af[2] = af[3] = 0x0000;
1115 ETHER_FIRST_MULTI(step, ac, enm);
1116 while (enm != NULL) {
1117 if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
1118 /*
1119 * We must listen to a range of multicast addresses.
1120 * For now, just accept all multicasts, rather than
1121 * trying to set only those filter bits needed to match
1122 * the range. (At this time, the only use of address
1123 * ranges is for IP multicast routing, for which the
1124 * range is big enough to require all bits set.)
1125 */
1126 goto allmulti;
1127 }
1128
1129 cp = enm->enm_addrlo;
1130 crc = 0xffffffff;
1131 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1132 c = *cp++;
1133 for (i = 8; --i >= 0;) {
1134 if ((crc & 0x01) ^ (c & 0x01)) {
1135 crc >>= 1;
1136 crc ^= 0xedb88320;
1137 } else
1138 crc >>= 1;
1139 c >>= 1;
1140 }
1141 }
1142 /* Just want the 6 most significant bits. */
1143 crc >>= 26;
1144
1145 /* Set the corresponding bit in the filter. */
1146 af[crc >> 4] |= 1 << (crc & 0xf);
1147
1148 ETHER_NEXT_MULTI(step, enm);
1149 }
1150 ifp->if_flags &= ~IFF_ALLMULTI;
1151 return;
1152
1153 allmulti:
1154 ifp->if_flags |= IFF_ALLMULTI;
1155 af[0] = af[1] = af[2] = af[3] = 0xffff;
1156 }
1157
1158
1159 /*
1160 * Routines for accessing the transmit and receive buffers.
1161 * The various CPU and adapter configurations supported by this
1162 * driver require three different access methods for buffers
1163 * and descriptors:
1164 * (1) contig (contiguous data; no padding),
1165 * (2) gap2 (two bytes of data followed by two bytes of padding),
1166 * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
1167 */
1168
1169 /*
1170 * contig: contiguous data with no padding.
1171 *
1172 * Buffers may have any alignment.
1173 */
1174
1175 void
1176 am7990_copytobuf_contig(sc, from, boff, len)
1177 struct am7990_softc *sc;
1178 void *from;
1179 int boff, len;
1180 {
1181 volatile caddr_t buf = sc->sc_mem;
1182
1183 /*
1184 * Just call bcopy() to do the work.
1185 */
1186 bcopy(from, buf + boff, len);
1187 }
1188
1189 void
1190 am7990_copyfrombuf_contig(sc, to, boff, len)
1191 struct am7990_softc *sc;
1192 void *to;
1193 int boff, len;
1194 {
1195 volatile caddr_t buf = sc->sc_mem;
1196
1197 /*
1198 * Just call bcopy() to do the work.
1199 */
1200 bcopy(buf + boff, to, len);
1201 }
1202
1203 void
1204 am7990_zerobuf_contig(sc, boff, len)
1205 struct am7990_softc *sc;
1206 int boff, len;
1207 {
1208 volatile caddr_t buf = sc->sc_mem;
1209
1210 /*
1211 * Just let bzero() do the work
1212 */
1213 bzero(buf + boff, len);
1214 }
1215
1216 #if 0
1217 /*
1218 * Examples only; duplicate these and tweak (if necessary) in
1219 * machine-specific front-ends.
1220 */
1221
1222 /*
1223 * gap2: two bytes of data followed by two bytes of pad.
1224 *
1225 * Buffers must be 4-byte aligned. The code doesn't worry about
1226 * doing an extra byte.
1227 */
1228
1229 void
1230 am7990_copytobuf_gap2(sc, fromv, boff, len)
1231 struct am7990_softc *sc;
1232 void *fromv;
1233 int boff;
1234 register int len;
1235 {
1236 volatile caddr_t buf = sc->sc_mem;
1237 register caddr_t from = fromv;
1238 register volatile u_int16_t *bptr;
1239
1240 if (boff & 0x1) {
1241 /* handle unaligned first byte */
1242 bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1243 *bptr = (*from++ << 8) | (*bptr & 0xff);
1244 bptr += 2;
1245 len--;
1246 } else
1247 bptr = ((volatile u_int16_t *)buf) + boff;
1248 while (len > 1) {
1249 *bptr = (from[1] << 8) | (from[0] & 0xff);
1250 bptr += 2;
1251 from += 2;
1252 len -= 2;
1253 }
1254 if (len == 1)
1255 *bptr = (u_int16_t)*from;
1256 }
1257
1258 void
1259 am7990_copyfrombuf_gap2(sc, tov, boff, len)
1260 struct am7990_softc *sc;
1261 void *tov;
1262 int boff, len;
1263 {
1264 volatile caddr_t buf = sc->sc_mem;
1265 register caddr_t to = tov;
1266 register volatile u_int16_t *bptr;
1267 register u_int16_t tmp;
1268
1269 if (boff & 0x1) {
1270 /* handle unaligned first byte */
1271 bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1272 *to++ = (*bptr >> 8) & 0xff;
1273 bptr += 2;
1274 len--;
1275 } else
1276 bptr = ((volatile u_int16_t *)buf) + boff;
1277 while (len > 1) {
1278 tmp = *bptr;
1279 *to++ = tmp & 0xff;
1280 *to++ = (tmp >> 8) & 0xff;
1281 bptr += 2;
1282 len -= 2;
1283 }
1284 if (len == 1)
1285 *to = *bptr & 0xff;
1286 }
1287
1288 void
1289 am7990_zerobuf_gap2(sc, boff, len)
1290 struct am7990_softc *sc;
1291 int boff, len;
1292 {
1293 volatile caddr_t buf = sc->sc_mem;
1294 register volatile u_int16_t *bptr;
1295
1296 if ((unsigned)boff & 0x1) {
1297 bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1298 *bptr &= 0xff;
1299 bptr += 2;
1300 len--;
1301 } else
1302 bptr = ((volatile u_int16_t *)buf) + boff;
1303 while (len > 0) {
1304 *bptr = 0;
1305 bptr += 2;
1306 len -= 2;
1307 }
1308 }
1309
1310 /*
1311 * gap16: 16 bytes of data followed by 16 bytes of pad.
1312 *
1313 * Buffers must be 32-byte aligned.
1314 */
1315
1316 void
1317 am7990_copytobuf_gap16(sc, fromv, boff, len)
1318 struct am7990_softc *sc;
1319 void *fromv;
1320 int boff;
1321 register int len;
1322 {
1323 volatile caddr_t buf = sc->sc_mem;
1324 register caddr_t from = fromv;
1325 register caddr_t bptr;
1326 register int xfer;
1327
1328 bptr = buf + ((boff << 1) & ~0x1f);
1329 boff &= 0xf;
1330 xfer = min(len, 16 - boff);
1331 while (len > 0) {
1332 bcopy(from, bptr + boff, xfer);
1333 from += xfer;
1334 bptr += 32;
1335 boff = 0;
1336 len -= xfer;
1337 xfer = min(len, 16);
1338 }
1339 }
1340
1341 void
1342 am7990_copyfrombuf_gap16(sc, tov, boff, len)
1343 struct am7990_softc *sc;
1344 void *tov;
1345 int boff, len;
1346 {
1347 volatile caddr_t buf = sc->sc_mem;
1348 register caddr_t to = tov;
1349 register caddr_t bptr;
1350 register int xfer;
1351
1352 bptr = buf + ((boff << 1) & ~0x1f);
1353 boff &= 0xf;
1354 xfer = min(len, 16 - boff);
1355 while (len > 0) {
1356 bcopy(bptr + boff, to, xfer);
1357 to += xfer;
1358 bptr += 32;
1359 boff = 0;
1360 len -= xfer;
1361 xfer = min(len, 16);
1362 }
1363 }
1364
1365 void
1366 am7990_zerobuf_gap16(sc, boff, len)
1367 struct am7990_softc *sc;
1368 int boff, len;
1369 {
1370 volatile caddr_t buf = sc->sc_mem;
1371 register caddr_t bptr;
1372 register int xfer;
1373
1374 bptr = buf + ((boff << 1) & ~0x1f);
1375 boff &= 0xf;
1376 xfer = min(len, 16 - boff);
1377 while (len > 0) {
1378 bzero(bptr + boff, xfer);
1379 bptr += 32;
1380 boff = 0;
1381 len -= xfer;
1382 xfer = min(len, 16);
1383 }
1384 }
1385 #endif /* Example only */
1386