am79900.c revision 1.2 1 /* $NetBSD: am79900.c,v 1.2 1998/07/24 13:42:29 drochner Exp $ */
2
3 /*-
4 * Copyright (c) 1998
5 * Matthias Drochner. All rights reserved.
6 * Copyright (c) 1997 Jason R. Thorpe. All rights reserved.
7 * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
8 * Copyright (c) 1992, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * This code is derived from software contributed to Berkeley by
12 * Ralph Campbell and Rick Macklem.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
43 */
44
45 #include "bpfilter.h"
46 #include "rnd.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/mbuf.h>
51 #include <sys/syslog.h>
52 #include <sys/socket.h>
53 #include <sys/device.h>
54 #include <sys/malloc.h>
55 #include <sys/ioctl.h>
56 #include <sys/errno.h>
57 #if NRND > 0
58 #include <sys/rnd.h>
59 #endif
60
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_ether.h>
64 #include <net/if_media.h>
65
66 #if NBPFILTER > 0
67 #include <net/bpf.h>
68 #include <net/bpfdesc.h>
69 #endif
70
71 #include <dev/ic/lancereg.h>
72 #include <dev/ic/lancevar.h>
73 #include <dev/ic/am79900reg.h>
74 #include <dev/ic/am79900var.h>
75
76 void am79900_meminit __P((struct lance_softc *));
77 void am79900_start __P((struct ifnet *));
78
79 #if defined(_KERNEL) && !defined(_LKM)
80 #include "opt_ddb.h"
81 #endif
82
83 #ifdef DDB
84 #define integrate
85 #define hide
86 #else
87 #define integrate static __inline
88 #define hide static
89 #endif
90
91 integrate void am79900_rint __P((struct lance_softc *));
92 integrate void am79900_tint __P((struct lance_softc *));
93
94 #ifdef LEDEBUG
95 void am79900_recv_print __P((struct lance_softc *, int));
96 void am79900_xmit_print __P((struct lance_softc *, int));
97 #endif
98
99 #define ifp (&sc->sc_ethercom.ec_if)
100
101 void
102 am79900_config(sc)
103 struct am79900_softc *sc;
104 {
105 int mem, i;
106
107 sc->lsc.sc_meminit = am79900_meminit;
108 sc->lsc.sc_start = am79900_start;
109
110 lance_config(&sc->lsc);
111
112 mem = 0;
113 sc->lsc.sc_initaddr = mem;
114 mem += sizeof(struct leinit);
115 sc->lsc.sc_rmdaddr = mem;
116 mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf;
117 sc->lsc.sc_tmdaddr = mem;
118 mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf;
119 for (i = 0; i < sc->lsc.sc_nrbuf; i++, mem += LEBLEN)
120 sc->lsc.sc_rbufaddr[i] = mem;
121 for (i = 0; i < sc->lsc.sc_ntbuf; i++, mem += LEBLEN)
122 sc->lsc.sc_tbufaddr[i] = mem;
123
124 if (mem > sc->lsc.sc_memsize)
125 panic("%s: memsize", sc->lsc.sc_dev.dv_xname);
126 }
127
128 /*
129 * Set up the initialization block and the descriptor rings.
130 */
131 void
132 am79900_meminit(sc)
133 register struct lance_softc *sc;
134 {
135 u_long a;
136 int bix;
137 struct leinit init;
138 struct lermd rmd;
139 struct letmd tmd;
140 u_int8_t *myaddr;
141
142 #if NBPFILTER > 0
143 if (ifp->if_flags & IFF_PROMISC)
144 init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
145 else
146 #endif
147 init.init_mode = LE_MODE_NORMAL;
148 if (sc->sc_initmodemedia == 1)
149 init.init_mode |= LE_MODE_PSEL0;
150
151 init.init_mode |= ((ffs(sc->sc_ntbuf) - 1) << 28)
152 | ((ffs(sc->sc_nrbuf) - 1) << 20);
153
154 /*
155 * Update our private copy of the Ethernet address.
156 * We NEED the copy so we can ensure its alignment!
157 */
158 bcopy(LLADDR(ifp->if_sadl), sc->sc_enaddr, 6);
159 myaddr = sc->sc_enaddr;
160
161 init.init_padr[0] = myaddr[0] | (myaddr[1] << 8)
162 | (myaddr[2] << 16) | (myaddr[3] << 24);
163 init.init_padr[1] = myaddr[4] | (myaddr[5] << 8);
164 lance_setladrf(&sc->sc_ethercom, init.init_ladrf);
165
166 sc->sc_last_rd = 0;
167 sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
168
169 a = sc->sc_addr + LE_RMDADDR(sc, 0);
170 init.init_rdra = a;
171
172 a = sc->sc_addr + LE_TMDADDR(sc, 0);
173 init.init_tdra = a;
174
175 (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
176
177 /*
178 * Set up receive ring descriptors.
179 */
180 for (bix = 0; bix < sc->sc_nrbuf; bix++) {
181 a = sc->sc_addr + LE_RBUFADDR(sc, bix);
182 rmd.rmd0 = a;
183 rmd.rmd1 = LE_R1_OWN | LE_R1_ONES | (-LEBLEN & 0xfff);
184 rmd.rmd2 = 0;
185 rmd.rmd3 = 0;
186 (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
187 sizeof(rmd));
188 }
189
190 /*
191 * Set up transmit ring descriptors.
192 */
193 for (bix = 0; bix < sc->sc_ntbuf; bix++) {
194 a = sc->sc_addr + LE_TBUFADDR(sc, bix);
195 tmd.tmd0 = a;
196 tmd.tmd1 = LE_T1_ONES;
197 tmd.tmd2 = 0;
198 tmd.tmd3 = 0;
199 (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
200 sizeof(tmd));
201 }
202 }
203
204 integrate void
205 am79900_rint(sc)
206 struct lance_softc *sc;
207 {
208 register int bix;
209 int rp;
210 struct lermd rmd;
211
212 bix = sc->sc_last_rd;
213
214 /* Process all buffers with valid data. */
215 for (;;) {
216 rp = LE_RMDADDR(sc, bix);
217 (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
218
219 if (rmd.rmd1 & LE_R1_OWN)
220 break;
221
222 if (rmd.rmd1 & LE_R1_ERR) {
223 if (rmd.rmd1 & LE_R1_ENP) {
224 #ifdef LEDEBUG
225 if ((rmd.rmd1 & LE_R1_OFLO) == 0) {
226 if (rmd.rmd1 & LE_R1_FRAM)
227 printf("%s: framing error\n",
228 sc->sc_dev.dv_xname);
229 if (rmd.rmd1 & LE_R1_CRC)
230 printf("%s: crc mismatch\n",
231 sc->sc_dev.dv_xname);
232 }
233 #endif
234 } else {
235 if (rmd.rmd1 & LE_R1_OFLO)
236 printf("%s: overflow\n",
237 sc->sc_dev.dv_xname);
238 }
239 if (rmd.rmd1 & LE_R1_BUFF)
240 printf("%s: receive buffer error\n",
241 sc->sc_dev.dv_xname);
242 ifp->if_ierrors++;
243 } else if ((rmd.rmd1 & (LE_R1_STP | LE_R1_ENP)) !=
244 (LE_R1_STP | LE_R1_ENP)) {
245 printf("%s: dropping chained buffer\n",
246 sc->sc_dev.dv_xname);
247 ifp->if_ierrors++;
248 } else {
249 #ifdef LEDEBUG
250 if (sc->sc_debug)
251 am79900_recv_print(sc, sc->sc_last_rd);
252 #endif
253 lance_read(sc, LE_RBUFADDR(sc, bix),
254 (rmd.rmd2 & 0xfff) - 4);
255 }
256
257 rmd.rmd1 = LE_R1_OWN | LE_R1_ONES | (-LEBLEN & 0xfff);
258 rmd.rmd2 = 0;
259 rmd.rmd3 = 0;
260 (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
261
262 #ifdef LEDEBUG
263 if (sc->sc_debug)
264 printf("sc->sc_last_rd = %x, rmd: "
265 "adr %08x, flags/blen %08x\n",
266 sc->sc_last_rd,
267 rmd.rmd0, rmd.rmd1);
268 #endif
269
270 if (++bix == sc->sc_nrbuf)
271 bix = 0;
272 }
273
274 sc->sc_last_rd = bix;
275 }
276
277 integrate void
278 am79900_tint(sc)
279 register struct lance_softc *sc;
280 {
281 register int bix;
282 struct letmd tmd;
283
284 bix = sc->sc_first_td;
285
286 for (;;) {
287 if (sc->sc_no_td <= 0)
288 break;
289
290 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
291 sizeof(tmd));
292
293 #ifdef LEDEBUG
294 if (sc->sc_debug)
295 printf("trans tmd: "
296 "adr %08x, flags/blen %08x\n",
297 tmd.tmd0, tmd.tmd1);
298 #endif
299
300 if (tmd.tmd1 & LE_T1_OWN)
301 break;
302
303 ifp->if_flags &= ~IFF_OACTIVE;
304
305 if (tmd.tmd1 & LE_T1_ERR) {
306 if (tmd.tmd2 & LE_T2_BUFF)
307 printf("%s: transmit buffer error\n",
308 sc->sc_dev.dv_xname);
309 else if (tmd.tmd2 & LE_T2_UFLO)
310 printf("%s: underflow\n", sc->sc_dev.dv_xname);
311 if (tmd.tmd2 & (LE_T2_BUFF | LE_T2_UFLO)) {
312 lance_reset(sc);
313 return;
314 }
315 if (tmd.tmd2 & LE_T2_LCAR) {
316 sc->sc_havecarrier = 0;
317 if (sc->sc_nocarrier)
318 (*sc->sc_nocarrier)(sc);
319 else
320 printf("%s: lost carrier\n",
321 sc->sc_dev.dv_xname);
322 }
323 if (tmd.tmd2 & LE_T2_LCOL)
324 ifp->if_collisions++;
325 if (tmd.tmd2 & LE_T2_RTRY) {
326 printf("%s: excessive collisions\n",
327 sc->sc_dev.dv_xname);
328 ifp->if_collisions += 16;
329 }
330 ifp->if_oerrors++;
331 } else {
332 if (tmd.tmd1 & LE_T1_ONE)
333 ifp->if_collisions++;
334 else if (tmd.tmd1 & LE_T1_MORE)
335 /* Real number is unknown. */
336 ifp->if_collisions += 2;
337 ifp->if_opackets++;
338 }
339
340 if (++bix == sc->sc_ntbuf)
341 bix = 0;
342
343 --sc->sc_no_td;
344 }
345
346 sc->sc_first_td = bix;
347
348 am79900_start(ifp);
349
350 if (sc->sc_no_td == 0)
351 ifp->if_timer = 0;
352 }
353
354 /*
355 * Controller interrupt.
356 */
357 int
358 am79900_intr(arg)
359 register void *arg;
360 {
361 register struct lance_softc *sc = arg;
362 register u_int16_t isr;
363
364 isr = (*sc->sc_rdcsr)(sc, LE_CSR0) | sc->sc_saved_csr0;
365 sc->sc_saved_csr0 = 0;
366 #ifdef LEDEBUG
367 if (sc->sc_debug)
368 printf("%s: am79900_intr entering with isr=%04x\n",
369 sc->sc_dev.dv_xname, isr);
370 #endif
371 if ((isr & LE_C0_INTR) == 0)
372 return (0);
373
374 (*sc->sc_wrcsr)(sc, LE_CSR0,
375 isr & (LE_C0_INEA | LE_C0_BABL | LE_C0_MISS | LE_C0_MERR |
376 LE_C0_RINT | LE_C0_TINT | LE_C0_IDON));
377 if (isr & LE_C0_ERR) {
378 if (isr & LE_C0_BABL) {
379 #ifdef LEDEBUG
380 printf("%s: babble\n", sc->sc_dev.dv_xname);
381 #endif
382 ifp->if_oerrors++;
383 }
384 #if 0
385 if (isr & LE_C0_CERR) {
386 printf("%s: collision error\n", sc->sc_dev.dv_xname);
387 ifp->if_collisions++;
388 }
389 #endif
390 if (isr & LE_C0_MISS) {
391 #ifdef LEDEBUG
392 printf("%s: missed packet\n", sc->sc_dev.dv_xname);
393 #endif
394 ifp->if_ierrors++;
395 }
396 if (isr & LE_C0_MERR) {
397 printf("%s: memory error\n", sc->sc_dev.dv_xname);
398 lance_reset(sc);
399 return (1);
400 }
401 }
402
403 if ((isr & LE_C0_RXON) == 0) {
404 printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
405 ifp->if_ierrors++;
406 lance_reset(sc);
407 return (1);
408 }
409 if ((isr & LE_C0_TXON) == 0) {
410 printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
411 ifp->if_oerrors++;
412 lance_reset(sc);
413 return (1);
414 }
415
416 /*
417 * Pretend we have carrier; if we don't this will be cleared
418 * shortly.
419 */
420 sc->sc_havecarrier = 1;
421
422 if (isr & LE_C0_RINT)
423 am79900_rint(sc);
424 if (isr & LE_C0_TINT)
425 am79900_tint(sc);
426
427 #if NRND > 0
428 rnd_add_uint32(&sc->rnd_source, isr);
429 #endif
430
431 return (1);
432 }
433
434 #undef ifp
435
436 /*
437 * Setup output on interface.
438 * Get another datagram to send off of the interface queue, and map it to the
439 * interface before starting the output.
440 * Called only at splimp or interrupt level.
441 */
442 void
443 am79900_start(ifp)
444 register struct ifnet *ifp;
445 {
446 register struct lance_softc *sc = ifp->if_softc;
447 register int bix;
448 register struct mbuf *m;
449 struct letmd tmd;
450 int rp;
451 int len;
452
453 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
454 return;
455
456 bix = sc->sc_last_td;
457
458 for (;;) {
459 rp = LE_TMDADDR(sc, bix);
460 (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
461
462 if (tmd.tmd1 & LE_T1_OWN) {
463 ifp->if_flags |= IFF_OACTIVE;
464 printf("missing buffer, no_td = %d, last_td = %d\n",
465 sc->sc_no_td, sc->sc_last_td);
466 }
467
468 IF_DEQUEUE(&ifp->if_snd, m);
469 if (m == 0)
470 break;
471
472 #if NBPFILTER > 0
473 /*
474 * If BPF is listening on this interface, let it see the packet
475 * before we commit it to the wire.
476 */
477 if (ifp->if_bpf)
478 bpf_mtap(ifp->if_bpf, m);
479 #endif
480
481 /*
482 * Copy the mbuf chain into the transmit buffer.
483 */
484 len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
485
486 #ifdef LEDEBUG
487 if (len > ETHERMTU + sizeof(struct ether_header))
488 printf("packet length %d\n", len);
489 #endif
490
491 ifp->if_timer = 5;
492
493 /*
494 * Init transmit registers, and set transmit start flag.
495 */
496 tmd.tmd1 = LE_T1_OWN | LE_T1_STP | LE_T1_ENP | LE_T1_ONES | (-len & 0xfff);
497 tmd.tmd2 = 0;
498 tmd.tmd3 = 0;
499
500 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
501
502 #ifdef LEDEBUG
503 if (sc->sc_debug)
504 am79900_xmit_print(sc, sc->sc_last_td);
505 #endif
506
507 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
508
509 if (++bix == sc->sc_ntbuf)
510 bix = 0;
511
512 if (++sc->sc_no_td == sc->sc_ntbuf) {
513 ifp->if_flags |= IFF_OACTIVE;
514 break;
515 }
516
517 }
518
519 sc->sc_last_td = bix;
520 }
521
522 #ifdef LEDEBUG
523 void
524 am79900_recv_print(sc, no)
525 struct lance_softc *sc;
526 int no;
527 {
528 struct lermd rmd;
529 u_int16_t len;
530 struct ether_header eh;
531
532 (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
533 len = (rmd.rmd2 & 0xfff) - 4;
534 printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
535 len);
536 printf("%s: status %04x\n", sc->sc_dev.dv_xname,
537 (*sc->sc_rdcsr)(sc, LE_CSR0));
538 printf("%s: adr %08x, flags/blen %08x\n",
539 sc->sc_dev.dv_xname, rmd.rmd0, rmd.rmd1);
540 if (len >= sizeof(eh)) {
541 (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
542 printf("%s: dst %s", sc->sc_dev.dv_xname,
543 ether_sprintf(eh.ether_dhost));
544 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
545 ntohs(eh.ether_type));
546 }
547 }
548
549 void
550 am79900_xmit_print(sc, no)
551 struct lance_softc *sc;
552 int no;
553 {
554 struct letmd tmd;
555 u_int16_t len;
556 struct ether_header eh;
557
558 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
559 len = -(tmd.tmd1 & 0xfff);
560 printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
561 len);
562 printf("%s: status %04x\n", sc->sc_dev.dv_xname,
563 (*sc->sc_rdcsr)(sc, LE_CSR0));
564 printf("%s: adr %08x, flags/blen %08x\n",
565 sc->sc_dev.dv_xname, tmd.tmd0, tmd.tmd1);
566 if (len >= sizeof(eh)) {
567 (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
568 printf("%s: dst %s", sc->sc_dev.dv_xname,
569 ether_sprintf(eh.ether_dhost));
570 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
571 ntohs(eh.ether_type));
572 }
573 }
574 #endif /* LEDEBUG */
575