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