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