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