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