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