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