am7990.c revision 1.22 1 /* $NetBSD: am7990.c,v 1.22 1996/10/13 01:37:19 christos 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 if (sc->sc_nocarrier)
629 (*sc->sc_nocarrier)(sc);
630 else
631 printf("%s: lost carrier\n",
632 sc->sc_dev.dv_xname);
633 }
634 if (tmd.tmd3 & LE_T3_LCOL)
635 ifp->if_collisions++;
636 if (tmd.tmd3 & LE_T3_RTRY) {
637 printf("%s: excessive collisions, tdr %d\n",
638 sc->sc_dev.dv_xname,
639 tmd.tmd3 & LE_T3_TDR_MASK);
640 ifp->if_collisions += 16;
641 }
642 ifp->if_oerrors++;
643 } else {
644 if (tmd.tmd1_bits & LE_T1_ONE)
645 ifp->if_collisions++;
646 else if (tmd.tmd1_bits & LE_T1_MORE)
647 /* Real number is unknown. */
648 ifp->if_collisions += 2;
649 ifp->if_opackets++;
650 }
651
652 if (++bix == sc->sc_ntbuf)
653 bix = 0;
654
655 --sc->sc_no_td;
656 }
657
658 sc->sc_first_td = bix;
659
660 am7990_start(ifp);
661
662 if (sc->sc_no_td == 0)
663 ifp->if_timer = 0;
664 }
665
666 /*
667 * Controller interrupt.
668 */
669 int
670 am7990_intr(arg)
671 register void *arg;
672 {
673 register struct am7990_softc *sc = arg;
674 register u_int16_t isr;
675
676 isr = (*sc->sc_rdcsr)(sc, LE_CSR0);
677 #ifdef LEDEBUG
678 if (sc->sc_debug)
679 printf("%s: am7990_intr entering with isr=%04x\n",
680 sc->sc_dev.dv_xname, isr);
681 #endif
682 if ((isr & LE_C0_INTR) == 0)
683 return (0);
684
685 (*sc->sc_wrcsr)(sc, LE_CSR0,
686 isr & (LE_C0_INEA | LE_C0_BABL | LE_C0_MISS | LE_C0_MERR |
687 LE_C0_RINT | LE_C0_TINT | LE_C0_IDON));
688 if (isr & LE_C0_ERR) {
689 if (isr & LE_C0_BABL) {
690 #ifdef LEDEBUG
691 printf("%s: babble\n", sc->sc_dev.dv_xname);
692 #endif
693 ifp->if_oerrors++;
694 }
695 #if 0
696 if (isr & LE_C0_CERR) {
697 printf("%s: collision error\n", sc->sc_dev.dv_xname);
698 ifp->if_collisions++;
699 }
700 #endif
701 if (isr & LE_C0_MISS) {
702 #ifdef LEDEBUG
703 printf("%s: missed packet\n", sc->sc_dev.dv_xname);
704 #endif
705 ifp->if_ierrors++;
706 }
707 if (isr & LE_C0_MERR) {
708 printf("%s: memory error\n", sc->sc_dev.dv_xname);
709 am7990_reset(sc);
710 return (1);
711 }
712 }
713
714 if ((isr & LE_C0_RXON) == 0) {
715 printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
716 ifp->if_ierrors++;
717 am7990_reset(sc);
718 return (1);
719 }
720 if ((isr & LE_C0_TXON) == 0) {
721 printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
722 ifp->if_oerrors++;
723 am7990_reset(sc);
724 return (1);
725 }
726
727 if (isr & LE_C0_RINT)
728 am7990_rint(sc);
729 if (isr & LE_C0_TINT)
730 am7990_tint(sc);
731
732 return (1);
733 }
734
735 #undef ifp
736
737 void
738 am7990_watchdog(ifp)
739 struct ifnet *ifp;
740 {
741 struct am7990_softc *sc = ifp->if_softc;
742
743 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
744 ++ifp->if_oerrors;
745
746 am7990_reset(sc);
747 }
748
749 /*
750 * Setup output on interface.
751 * Get another datagram to send off of the interface queue, and map it to the
752 * interface before starting the output.
753 * Called only at splimp or interrupt level.
754 */
755 void
756 am7990_start(ifp)
757 register struct ifnet *ifp;
758 {
759 register struct am7990_softc *sc = ifp->if_softc;
760 register int bix;
761 register struct mbuf *m;
762 struct letmd tmd;
763 int rp;
764 int len;
765
766 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
767 return;
768
769 bix = sc->sc_last_td;
770
771 for (;;) {
772 rp = LE_TMDADDR(sc, bix);
773 (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
774
775 if (tmd.tmd1_bits & LE_T1_OWN) {
776 ifp->if_flags |= IFF_OACTIVE;
777 printf("missing buffer, no_td = %d, last_td = %d\n",
778 sc->sc_no_td, sc->sc_last_td);
779 }
780
781 IF_DEQUEUE(&ifp->if_snd, m);
782 if (m == 0)
783 break;
784
785 #if NBPFILTER > 0
786 /*
787 * If BPF is listening on this interface, let it see the packet
788 * before we commit it to the wire.
789 */
790 if (ifp->if_bpf)
791 bpf_mtap(ifp->if_bpf, m);
792 #endif
793
794 /*
795 * Copy the mbuf chain into the transmit buffer.
796 */
797 len = am7990_put(sc, LE_TBUFADDR(sc, bix), m);
798
799 #ifdef LEDEBUG
800 if (len > ETHERMTU + sizeof(struct ether_header))
801 printf("packet length %d\n", len);
802 #endif
803
804 ifp->if_timer = 5;
805
806 /*
807 * Init transmit registers, and set transmit start flag.
808 */
809 tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
810 tmd.tmd2 = -len | LE_XMD2_ONES;
811 tmd.tmd3 = 0;
812
813 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
814
815 #ifdef LEDEBUG
816 if (sc->sc_debug)
817 am7990_xmit_print(sc, sc->sc_last_td);
818 #endif
819
820 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
821
822 if (++bix == sc->sc_ntbuf)
823 bix = 0;
824
825 if (++sc->sc_no_td == sc->sc_ntbuf) {
826 ifp->if_flags |= IFF_OACTIVE;
827 break;
828 }
829
830 }
831
832 sc->sc_last_td = bix;
833 }
834
835 /*
836 * Process an ioctl request.
837 */
838 int
839 am7990_ioctl(ifp, cmd, data)
840 register struct ifnet *ifp;
841 u_long cmd;
842 caddr_t data;
843 {
844 register struct am7990_softc *sc = ifp->if_softc;
845 struct ifaddr *ifa = (struct ifaddr *)data;
846 struct ifreq *ifr = (struct ifreq *)data;
847 int s, error = 0;
848
849 s = splimp();
850
851 switch (cmd) {
852
853 case SIOCSIFADDR:
854 ifp->if_flags |= IFF_UP;
855
856 switch (ifa->ifa_addr->sa_family) {
857 #ifdef INET
858 case AF_INET:
859 am7990_init(sc);
860 arp_ifinit(&sc->sc_arpcom, ifa);
861 break;
862 #endif
863 #ifdef NS
864 case AF_NS:
865 {
866 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
867
868 if (ns_nullhost(*ina))
869 ina->x_host =
870 *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
871 else
872 bcopy(ina->x_host.c_host,
873 sc->sc_arpcom.ac_enaddr,
874 sizeof(sc->sc_arpcom.ac_enaddr));
875 /* Set new address. */
876 am7990_init(sc);
877 break;
878 }
879 #endif
880 default:
881 am7990_init(sc);
882 break;
883 }
884 break;
885
886 #if defined(CCITT) && defined(LLC)
887 case SIOCSIFCONF_X25:
888 ifp->if_flags |= IFF_UP;
889 ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
890 error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
891 if (error == 0)
892 am7990_init(sc);
893 break;
894 #endif /* CCITT && LLC */
895
896 case SIOCSIFFLAGS:
897 if ((ifp->if_flags & IFF_UP) == 0 &&
898 (ifp->if_flags & IFF_RUNNING) != 0) {
899 /*
900 * If interface is marked down and it is running, then
901 * stop it.
902 */
903 am7990_stop(sc);
904 ifp->if_flags &= ~IFF_RUNNING;
905 } else if ((ifp->if_flags & IFF_UP) != 0 &&
906 (ifp->if_flags & IFF_RUNNING) == 0) {
907 /*
908 * If interface is marked up and it is stopped, then
909 * start it.
910 */
911 am7990_init(sc);
912 } else {
913 /*
914 * Reset the interface to pick up changes in any other
915 * flags that affect hardware registers.
916 */
917 /*am7990_stop(sc);*/
918 am7990_init(sc);
919 }
920 #ifdef LEDEBUG
921 if (ifp->if_flags & IFF_DEBUG)
922 sc->sc_debug = 1;
923 else
924 sc->sc_debug = 0;
925 #endif
926 break;
927
928 case SIOCADDMULTI:
929 case SIOCDELMULTI:
930 error = (cmd == SIOCADDMULTI) ?
931 ether_addmulti(ifr, &sc->sc_arpcom) :
932 ether_delmulti(ifr, &sc->sc_arpcom);
933
934 if (error == ENETRESET) {
935 /*
936 * Multicast list has changed; set the hardware filter
937 * accordingly.
938 */
939 am7990_reset(sc);
940 error = 0;
941 }
942 break;
943
944 default:
945 error = EINVAL;
946 break;
947 }
948
949 splx(s);
950 return (error);
951 }
952
953 hide void
954 am7990_shutdown(arg)
955 void *arg;
956 {
957
958 am7990_stop((struct am7990_softc *)arg);
959 }
960
961 #ifdef LEDEBUG
962 void
963 am7990_recv_print(sc, no)
964 struct am7990_softc *sc;
965 int no;
966 {
967 struct lermd rmd;
968 u_int16_t len;
969 struct ether_header eh;
970
971 (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
972 len = rmd.rmd3;
973 printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
974 len);
975 printf("%s: status %04x\n", sc->sc_dev.dv_xname,
976 (*sc->sc_rdcsr)(sc, LE_CSR0));
977 printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
978 sc->sc_dev.dv_xname,
979 rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
980 if (len >= sizeof(eh)) {
981 (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
982 printf("%s: dst %s", sc->sc_dev.dv_xname,
983 ether_sprintf(eh.ether_dhost));
984 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
985 ntohs(eh.ether_type));
986 }
987 }
988
989 void
990 am7990_xmit_print(sc, no)
991 struct am7990_softc *sc;
992 int no;
993 {
994 struct letmd tmd;
995 u_int16_t len;
996 struct ether_header eh;
997
998 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
999 len = -tmd.tmd2;
1000 printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
1001 len);
1002 printf("%s: status %04x\n", sc->sc_dev.dv_xname,
1003 (*sc->sc_rdcsr)(sc, LE_CSR0));
1004 printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
1005 sc->sc_dev.dv_xname,
1006 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
1007 if (len >= sizeof(eh)) {
1008 (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
1009 printf("%s: dst %s", sc->sc_dev.dv_xname,
1010 ether_sprintf(eh.ether_dhost));
1011 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
1012 ntohs(eh.ether_type));
1013 }
1014 }
1015 #endif /* LEDEBUG */
1016
1017 /*
1018 * Set up the logical address filter.
1019 */
1020 void
1021 am7990_setladrf(ac, af)
1022 struct arpcom *ac;
1023 u_int16_t *af;
1024 {
1025 struct ifnet *ifp = &ac->ac_if;
1026 struct ether_multi *enm;
1027 register u_char *cp, c;
1028 register u_int32_t crc;
1029 register int i, len;
1030 struct ether_multistep step;
1031
1032 /*
1033 * Set up multicast address filter by passing all multicast addresses
1034 * through a crc generator, and then using the high order 6 bits as an
1035 * index into the 64 bit logical address filter. The high order bit
1036 * selects the word, while the rest of the bits select the bit within
1037 * the word.
1038 */
1039
1040 if (ifp->if_flags & IFF_PROMISC)
1041 goto allmulti;
1042
1043 af[0] = af[1] = af[2] = af[3] = 0x0000;
1044 ETHER_FIRST_MULTI(step, ac, enm);
1045 while (enm != NULL) {
1046 if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
1047 /*
1048 * We must listen to a range of multicast addresses.
1049 * For now, just accept all multicasts, rather than
1050 * trying to set only those filter bits needed to match
1051 * the range. (At this time, the only use of address
1052 * ranges is for IP multicast routing, for which the
1053 * range is big enough to require all bits set.)
1054 */
1055 goto allmulti;
1056 }
1057
1058 cp = enm->enm_addrlo;
1059 crc = 0xffffffff;
1060 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1061 c = *cp++;
1062 for (i = 8; --i >= 0;) {
1063 if ((crc & 0x01) ^ (c & 0x01)) {
1064 crc >>= 1;
1065 crc ^= 0xedb88320;
1066 } else
1067 crc >>= 1;
1068 c >>= 1;
1069 }
1070 }
1071 /* Just want the 6 most significant bits. */
1072 crc >>= 26;
1073
1074 /* Set the corresponding bit in the filter. */
1075 af[crc >> 4] |= 1 << (crc & 0xf);
1076
1077 ETHER_NEXT_MULTI(step, enm);
1078 }
1079 ifp->if_flags &= ~IFF_ALLMULTI;
1080 return;
1081
1082 allmulti:
1083 ifp->if_flags |= IFF_ALLMULTI;
1084 af[0] = af[1] = af[2] = af[3] = 0xffff;
1085 }
1086
1087
1088 /*
1089 * Routines for accessing the transmit and receive buffers.
1090 * The various CPU and adapter configurations supported by this
1091 * driver require three different access methods for buffers
1092 * and descriptors:
1093 * (1) contig (contiguous data; no padding),
1094 * (2) gap2 (two bytes of data followed by two bytes of padding),
1095 * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
1096 */
1097
1098 /*
1099 * contig: contiguous data with no padding.
1100 *
1101 * Buffers may have any alignment.
1102 */
1103
1104 void
1105 am7990_copytobuf_contig(sc, from, boff, len)
1106 struct am7990_softc *sc;
1107 void *from;
1108 int boff, len;
1109 {
1110 volatile caddr_t buf = sc->sc_mem;
1111
1112 /*
1113 * Just call bcopy() to do the work.
1114 */
1115 bcopy(from, buf + boff, len);
1116 }
1117
1118 void
1119 am7990_copyfrombuf_contig(sc, to, boff, len)
1120 struct am7990_softc *sc;
1121 void *to;
1122 int boff, len;
1123 {
1124 volatile caddr_t buf = sc->sc_mem;
1125
1126 /*
1127 * Just call bcopy() to do the work.
1128 */
1129 bcopy(buf + boff, to, len);
1130 }
1131
1132 void
1133 am7990_zerobuf_contig(sc, boff, len)
1134 struct am7990_softc *sc;
1135 int boff, len;
1136 {
1137 volatile caddr_t buf = sc->sc_mem;
1138
1139 /*
1140 * Just let bzero() do the work
1141 */
1142 bzero(buf + boff, len);
1143 }
1144
1145 #if 0
1146 /*
1147 * Examples only; duplicate these and tweak (if necessary) in
1148 * machine-specific front-ends.
1149 */
1150
1151 /*
1152 * gap2: two bytes of data followed by two bytes of pad.
1153 *
1154 * Buffers must be 4-byte aligned. The code doesn't worry about
1155 * doing an extra byte.
1156 */
1157
1158 void
1159 am7990_copytobuf_gap2(sc, fromv, boff, len)
1160 struct am7990_softc *sc;
1161 void *fromv;
1162 int boff;
1163 register int len;
1164 {
1165 volatile caddr_t buf = sc->sc_mem;
1166 register caddr_t from = fromv;
1167 register volatile u_int16_t *bptr;
1168
1169 if (boff & 0x1) {
1170 /* handle unaligned first byte */
1171 bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1172 *bptr = (*from++ << 8) | (*bptr & 0xff);
1173 bptr += 2;
1174 len--;
1175 } else
1176 bptr = ((volatile u_int16_t *)buf) + boff;
1177 while (len > 1) {
1178 *bptr = (from[1] << 8) | (from[0] & 0xff);
1179 bptr += 2;
1180 from += 2;
1181 len -= 2;
1182 }
1183 if (len == 1)
1184 *bptr = (u_int16_t)*from;
1185 }
1186
1187 void
1188 am7990_copyfrombuf_gap2(sc, tov, boff, len)
1189 struct am7990_softc *sc;
1190 void *tov;
1191 int boff, len;
1192 {
1193 volatile caddr_t buf = sc->sc_mem;
1194 register caddr_t to = tov;
1195 register volatile u_int16_t *bptr;
1196 register u_int16_t tmp;
1197
1198 if (boff & 0x1) {
1199 /* handle unaligned first byte */
1200 bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1201 *to++ = (*bptr >> 8) & 0xff;
1202 bptr += 2;
1203 len--;
1204 } else
1205 bptr = ((volatile u_int16_t *)buf) + boff;
1206 while (len > 1) {
1207 tmp = *bptr;
1208 *to++ = tmp & 0xff;
1209 *to++ = (tmp >> 8) & 0xff;
1210 bptr += 2;
1211 len -= 2;
1212 }
1213 if (len == 1)
1214 *to = *bptr & 0xff;
1215 }
1216
1217 void
1218 am7990_zerobuf_gap2(sc, boff, len)
1219 struct am7990_softc *sc;
1220 int boff, len;
1221 {
1222 volatile caddr_t buf = sc->sc_mem;
1223 register volatile u_int16_t *bptr;
1224
1225 if ((unsigned)boff & 0x1) {
1226 bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1227 *bptr &= 0xff;
1228 bptr += 2;
1229 len--;
1230 } else
1231 bptr = ((volatile u_int16_t *)buf) + boff;
1232 while (len > 0) {
1233 *bptr = 0;
1234 bptr += 2;
1235 len -= 2;
1236 }
1237 }
1238
1239 /*
1240 * gap16: 16 bytes of data followed by 16 bytes of pad.
1241 *
1242 * Buffers must be 32-byte aligned.
1243 */
1244
1245 void
1246 am7990_copytobuf_gap16(sc, fromv, boff, len)
1247 struct am7990_softc *sc;
1248 void *fromv;
1249 int boff;
1250 register int len;
1251 {
1252 volatile caddr_t buf = sc->sc_mem;
1253 register caddr_t from = fromv;
1254 register caddr_t bptr;
1255 register int xfer;
1256
1257 bptr = buf + ((boff << 1) & ~0x1f);
1258 boff &= 0xf;
1259 xfer = min(len, 16 - boff);
1260 while (len > 0) {
1261 bcopy(from, bptr + boff, xfer);
1262 from += xfer;
1263 bptr += 32;
1264 boff = 0;
1265 len -= xfer;
1266 xfer = min(len, 16);
1267 }
1268 }
1269
1270 void
1271 am7990_copyfrombuf_gap16(sc, tov, boff, len)
1272 struct am7990_softc *sc;
1273 void *tov;
1274 int boff, len;
1275 {
1276 volatile caddr_t buf = sc->sc_mem;
1277 register caddr_t to = tov;
1278 register caddr_t bptr;
1279 register int xfer;
1280
1281 bptr = buf + ((boff << 1) & ~0x1f);
1282 boff &= 0xf;
1283 xfer = min(len, 16 - boff);
1284 while (len > 0) {
1285 bcopy(bptr + boff, to, xfer);
1286 to += xfer;
1287 bptr += 32;
1288 boff = 0;
1289 len -= xfer;
1290 xfer = min(len, 16);
1291 }
1292 }
1293
1294 void
1295 am7990_zerobuf_gap16(sc, boff, len)
1296 struct am7990_softc *sc;
1297 int boff, len;
1298 {
1299 volatile caddr_t buf = sc->sc_mem;
1300 register caddr_t bptr;
1301 register int xfer;
1302
1303 bptr = buf + ((boff << 1) & ~0x1f);
1304 boff &= 0xf;
1305 xfer = min(len, 16 - boff);
1306 while (len > 0) {
1307 bzero(bptr + boff, xfer);
1308 bptr += 32;
1309 boff = 0;
1310 len -= xfer;
1311 xfer = min(len, 16);
1312 }
1313 }
1314 #endif /* Example only */
1315