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