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