smc90cx6.c revision 1.5 1 /* $NetBSD: smc90cx6.c,v 1.5 1995/04/11 18:52:00 chopps Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Ignatios Souvatzis
5 * Copyright (c) 1994 Timo Rossi
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Timo Rossi
19 * This product includes software developed by Ignatios Souvatzis
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * Driver for the Commodore Busines Machines arcnet card
37 * written by Ignatios Souvatzis <is (at) beverly.rhein.de>,
38 * somewhat based on Amiga if_ed.c
39 */
40
41 #define BAHASMCOPY /**/
42 #define BAHSOFTCOPY /**/
43 /* #define BAHTIMINGS /**/
44 /* #define BAH_DEBUG 3 /**/
45
46 #include "bpfilter.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/mbuf.h>
51 #include <sys/buf.h>
52 #include <sys/device.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/syslog.h>
56 #include <sys/ioctl.h>
57 #include <sys/errno.h>
58
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_types.h>
62 #include <net/netisr.h>
63
64 #ifdef INET
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip.h>
69 #include <netinet/if_ether.h>
70 #include <netinet/if_arc.h>
71 #endif
72
73 #if NBPFILTER > 0
74 #include <net/bpf.h>
75 #include <net/bpfdesc.h>
76 #endif
77
78 #include <sys/kernel.h>
79 #include <machine/cpu.h>
80 #include <machine/mtpr.h>
81
82 #include <amiga/amiga/device.h>
83 #include <amiga/amiga/isr.h>
84 #include <amiga/dev/zbusvar.h>
85 #include <amiga/dev/if_bahreg.h>
86
87 /* these should be elsewhere */
88
89 #define ARC_MIN_LEN 1
90 #define ARC_MIN_FORBID_LEN 254
91 #define ARC_MAX_FORBID_LEN 256
92 #define ARC_MAX_LEN 508
93 #define ARC_ADDR_LEN 1
94
95 #define MIN(a,b) ((b)<0?(a):min((a),(b)))
96
97 /*
98 * This currently uses 2 bufs for tx, 2 for rx
99 *
100 * New rx protocol:
101 *
102 * rx has a fillcount variable. If fillcount > (NRXBUF-1),
103 * rx can be switched off from rx hard int.
104 * Else rx is restarted on the other receiver.
105 * rx soft int counts down. if it is == (NRXBUF-1), it restarts
106 * the receiver.
107 * To ensure packet ordering (we need that for 1201 later), we have a counter
108 * which is incremented modulo 256 on each receive and a per buffer
109 * variable, which is set to the counter on filling. The soft int can
110 * compare both values to determine the older packet.
111 *
112 * Transmit direction:
113 *
114 * bah_start checks tx_fillcount
115 * case 2: return
116 *
117 * else fill tx_act ^ 1 && inc tx_fillcount
118 *
119 * check tx_fillcount again.
120 * case 2: set IFF_OACTIVE to stop arc_output from filling us.
121 * case 1: start tx
122 *
123 * tint clears IFF_OCATIVE, decrements and checks tx_fillcount
124 * case 1: start tx on tx_act ^ 1, softcall bah_start
125 * case 0: softcall bah_start
126 *
127 * #define fill(i) get mbuf && copy mbuf to chip(i)
128 */
129
130 /*
131 * Arcnet software status per interface
132 *
133 */
134 struct bah_softc {
135 struct device sc_dev;
136 struct arccom sc_arccom; /* Common arcnet structures */
137 struct isr sc_isr;
138 struct a2060 *sc_base;
139 u_long sc_recontime; /* seconds only, I'm lazy */
140 u_long sc_reconcount; /* for the above */
141 u_long sc_reconcount_excessive; /* for the above */
142 #define ARC_EXCESSIVE_RECONS 20
143 #define ARC_EXCESSIVE_RECONS_REWARN 400
144 u_char sc_bufstat[4]; /* use for packet no for rx */
145 u_char sc_intmask;
146 u_char sc_rx_packetno;
147 u_char sc_rx_act; /* 2..3 */
148 u_char sc_tx_act; /* 0..1 */
149 u_char sc_rx_fillcount;
150 u_char sc_tx_fillcount;
151 u_char sc_broadcast[2]; /* is it a broadcast packet? */
152 u_char sc_retransmits[2]; /* unused at the moment */
153 #ifdef BAHTIMINGS
154 struct {
155 int mincopyin;
156 int maxcopyin; /* divided by byte count */
157 int mincopyout;
158 int maxcopyout;
159 int minsend;
160 int maxsend;
161 int lasttxstart_mics;
162 struct timeval lasttxstart_tv;
163
164 } sc_stats;
165 #endif
166 /* Add other fields as needed... -- IS */
167 };
168
169 int bahmatch __P((struct device *, void *, void *));
170 void bahattach __P((struct device *, struct device *, void *));
171 void bah_ini __P((struct bah_softc *));
172 void bah_reset __P((struct bah_softc *));
173 void bah_stop __P((struct bah_softc *));
174 void bah_start __P((struct ifnet *));
175 int bahintr __P((struct bah_softc *sc));
176 int bah_ioctl __P((struct ifnet *, unsigned long, caddr_t));
177 void movepout __P((u_char *from, u_char volatile *to, int len));
178 void movepin __P((u_char volatile *from, u_char *to, int len));
179 void bah_srint __P((struct bah_softc *sc, void *dummy));
180 void callstart __P((struct bah_softc *sc, void *dummy));
181
182 #ifdef BAHTIMINGS
183 int clkread();
184 #endif
185
186 struct cfdriver bahcd = {
187 NULL, "bah", bahmatch, bahattach, DV_IFNET, sizeof(struct bah_softc)
188 };
189
190 int
191 bahmatch(parent, match, aux)
192 struct device *parent;
193 void *match, *aux;
194 {
195 struct zbus_args *zap = aux;
196
197 if ((zap->manid == 514 || zap->manid == 1053) && zap->prodid == 9)
198 return (1);
199
200 return (0);
201 }
202
203 void
204 bahattach(parent, self, aux)
205 struct device *parent, *self;
206 void *aux;
207 {
208 struct bah_softc *sc = (void *)self;
209 struct zbus_args *zap = aux;
210 struct ifnet *ifp = &sc->sc_arccom.ac_if;
211 int i, s, linkaddress;
212
213 #if (defined(BAH_DEBUG) && (BAH_DEBUG > 2))
214 printf("\n%s: attach(0x%x, 0x%x, 0x%x)\n",
215 sc->sc_dev.dv_xname, parent, self, aux);
216 #endif
217 s = splhigh();
218 sc->sc_base = zap->va;
219
220 /*
221 * read the arcnet address from the board
222 */
223
224 sc->sc_base->kick1 = 0x0;
225 sc->sc_base->kick2 = 0x0;
226 DELAY(120);
227
228 sc->sc_base->kick1 = 0xFF;
229 sc->sc_base->kick2 = 0xFF;
230 do {
231 DELAY(120);
232 } while (!(sc->sc_base->status & ARC_POR));
233
234 linkaddress = sc->sc_base->dipswitches;
235
236 #ifdef BAHTIMINGS
237 printf(": link addr 0x%02x(%ld), with timer\n",
238 linkaddress, linkaddress);
239 #else
240 printf(": link addr 0x%02x(%ld)\n", linkaddress, linkaddress);
241 #endif
242
243 sc->sc_arccom.ac_anaddr = sc->sc_base->dipswitches;
244
245 /* clear the int mask... */
246
247 sc->sc_base->status = sc->sc_intmask = 0;
248
249 sc->sc_base->command = ARC_CONF(CONF_LONG);
250 sc->sc_base->command = ARC_CLR(CLR_POR|CLR_RECONFIG);
251 sc->sc_recontime = sc->sc_reconcount = 0;
252
253 /* and reenable kernel int level */
254 splx(s);
255
256 /*
257 * set interface to stopped condition (reset)
258 *
259 */
260 bah_stop(sc);
261
262 ifp->if_unit = sc->sc_dev.dv_unit;
263 ifp->if_name = bahcd.cd_name;
264 ifp->if_output = arc_output;
265 ifp->if_start = bah_start;
266 ifp->if_ioctl = bah_ioctl;
267 /* might need later: ifp->if_watchdog = bah_watchdog */
268 ifp->if_flags =
269 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_NOARP;
270
271 if_attach(ifp);
272 arc_ifattach(ifp);
273
274 #if NBPFILTER > 0
275 bpfattach(&ifp->if_bpf, ifp, DLT_ARCNET, ARC_HDRLEN);
276 #endif
277
278 sc->sc_isr.isr_intr = bahintr;
279 sc->sc_isr.isr_arg = sc;
280 sc->sc_isr.isr_ipl = 2;
281 add_isr(&sc->sc_isr);
282 }
283
284 /*
285 * Initialize device
286 *
287 */
288 void
289 bah_init(sc)
290 struct bah_softc *sc;
291 {
292 struct ifnet *ifp;
293 int s;
294
295 ifp = &sc->sc_arccom.ac_if;
296
297 /* Address not known. */
298 if (ifp->if_addrlist == 0)
299 return;
300
301 if ((ifp->if_flags & IFF_RUNNING) == 0) {
302 s = splimp();
303 ifp->if_flags |= IFF_RUNNING;
304 bah_reset(sc);
305 bah_start(ifp);
306 splx(s);
307 }
308 }
309
310 /*
311 * Reset the interface...
312 *
313 * this assumes that it is called inside a critical section...
314 *
315 */
316 void
317 bah_reset(sc)
318 struct bah_softc *sc;
319 {
320 struct ifnet *ifp;
321 int i, s;
322
323 ifp = &sc->sc_arccom.ac_if;
324
325 #ifdef BAH_DEBUG
326 printf("%s: reset\n", sc->sc_dev.dv_xname);
327 #endif
328 /* stop hardware in case it still runs */
329
330 sc->sc_base->kick1 = 0;
331 sc->sc_base->kick2 = 0;
332 DELAY(120);
333
334 /* and restart it */
335 sc->sc_base->kick1 = 0xFF;
336 sc->sc_base->kick2 = 0xFF;
337
338 do {
339 DELAY(120);
340 } while (!(sc->sc_base->status & ARC_POR));
341
342 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
343 printf("%s: reset: card reset, status=0x%02x\n",
344 sc->sc_dev.dv_xname, sc->sc_base->status);
345 #endif
346 /* POR is NMI, but we need it below: */
347 sc->sc_intmask = ARC_RECON|ARC_POR;
348 sc->sc_base->status = sc->sc_intmask;
349 sc->sc_base->command = ARC_CONF(CONF_LONG);
350
351 #ifdef BAH_DEBUG
352 printf("%s: reset: chip configured, status=0x%02x\n",
353 sc->sc_dev.dv_xname, sc->sc_base->status);
354 #endif
355
356 sc->sc_base->command = ARC_CLR(CLR_POR|CLR_RECONFIG);
357
358 #ifdef BAH_DEBUG
359 printf("%s: reset: bits cleared, status=0x%02x\n",
360 sc->sc_dev.dv_xname, sc->sc_base->status);
361 #endif
362
363 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
364
365 /* start receiver */
366
367 sc->sc_intmask |= ARC_RI;
368
369 sc->sc_bufstat[2] =
370 sc->sc_bufstat[3] =
371 sc->sc_rx_packetno =
372 sc->sc_rx_fillcount = 0;
373
374 sc->sc_rx_act = 2;
375
376 sc->sc_base->command = ARC_RXBC(2);
377 sc->sc_base->status = sc->sc_intmask;
378
379 #ifdef BAH_DEBUG
380 printf("%s: reset: started receiver, status=0x%02x\n",
381 sc->sc_dev.dv_xname, sc->sc_base->status);
382 #endif
383
384 /* and init transmitter status */
385 sc->sc_tx_act = 0;
386 sc->sc_tx_fillcount = 0;
387
388 ifp->if_flags |= IFF_RUNNING;
389 ifp->if_flags &= ~IFF_OACTIVE;
390
391 #ifdef BAHTIMINGS
392 bzero((caddr_t)&(sc->sc_stats),sizeof(sc->sc_stats));
393 sc->sc_stats.mincopyin =
394 sc->sc_stats.mincopyout =
395 sc->sc_stats.minsend = 999999L;
396 #endif
397
398 bah_start(ifp);
399 }
400
401 /*
402 * Take interface offline
403 */
404 void
405 bah_stop(sc)
406 struct bah_softc *sc;
407 {
408 int s;
409
410 /* Stop the interrupts */
411 sc->sc_base->status = 0;
412
413 /* Stop the interface */
414 sc->sc_base->kick1 = 0;
415 sc->sc_base->kick2 = 0;
416
417 #ifdef BAHTIMINGS
418 log(LOG_DEBUG,"%s\
419 To board: %6ld .. %6ld ns/byte\nFrom board: %6ld .. %6ld ns/byte\n\
420 Send time: %6ld .. %6ld mics\n",
421 sc->sc_dev.dv_xname,
422 sc->sc_stats.mincopyout,sc->sc_stats.maxcopyout,
423 sc->sc_stats.mincopyin, sc->sc_stats.maxcopyin,
424 sc->sc_stats.minsend, sc->sc_stats.maxsend);
425
426 sc->sc_stats.minsend =
427 sc->sc_stats.mincopyout =
428 sc->sc_stats.mincopyin = 999999L;
429 sc->sc_stats.maxsend =
430 sc->sc_stats.maxcopyout =
431 sc->sc_stats.maxcopyin = 0L;
432 #endif
433
434 }
435
436 inline void
437 movepout(from,to,len)
438 u_char *from;
439 volatile u_char *to;
440 int len;
441 {
442 #ifdef BAHASMCOPY
443 u_short shortd;
444 u_long longd,longd1,longd2,longd3,longd4;
445
446 if ((len>3) && ((int)from)&3) {
447 switch (((int)from) & 3) {
448 case 3:
449 *to = *from++;
450 to+=2;--len;
451 break;
452 case 1:
453 *to = *from++;
454 to += 2; --len;
455 case 2:
456 shortd = *((u_short *)from)++;
457 asm("movepw %0,%1@(0)" : : "d"(shortd), "a"(to));
458 to += 4; len -= 2;
459 break;
460 default:
461 }
462
463 while (len>=32) {
464 longd1 = *((u_long *)from)++;
465 longd2 = *((u_long *)from)++;
466 longd3 = *((u_long *)from)++;
467 longd4 = *((u_long *)from)++;
468 asm("movepl %0,%1@(0)" : : "d"(longd1), "a"(to));
469 asm("movepl %0,%1@(8)" : : "d"(longd2), "a"(to));
470 asm("movepl %0,%1@(16)" : : "d"(longd3), "a"(to));
471 asm("movepl %0,%1@(24)" : : "d"(longd4), "a"(to));
472
473 longd1 = *((u_long *)from)++;
474 longd2 = *((u_long *)from)++;
475 longd3 = *((u_long *)from)++;
476 longd4 = *((u_long *)from)++;
477 asm("movepl %0,%1@(32)" : : "d"(longd1), "a"(to));
478 asm("movepl %0,%1@(40)" : : "d"(longd2), "a"(to));
479 asm("movepl %0,%1@(48)" : : "d"(longd3), "a"(to));
480 asm("movepl %0,%1@(56)" : : "d"(longd4), "a"(to));
481
482 to += 64; len -= 32;
483 }
484 while (len>0) {
485 longd = *((u_long *)from)++;
486 asm("movepl %0,%1@(0)" : : "d"(longd), "a"(to));
487 to += 8; len -= 4;
488 }
489 }
490 #endif
491 while (len>0) {
492 *to = *from++;
493 to+=2;
494 --len;
495 }
496 }
497
498 /*
499 * Start output on interface. Get another datagram to send
500 * off the interface queue, and copy it to the
501 * interface becore starting the output
502 *
503 * this assumes that it is called inside a critical section...
504 * XXX hm... does it still?
505 *
506 */
507 void
508 bah_start(ifp)
509 struct ifnet *ifp;
510 {
511 struct bah_softc *sc;
512 struct mbuf *m,*mp;
513 volatile u_char *bah_ram_ptr;
514 int i, len, tlen, offset, s, buffer;
515
516 #ifdef BAHTIMINGS
517 int copystart,lencopy,perbyte;
518 #endif
519
520 sc = bahcd.cd_devs[ifp->if_unit];
521
522 #if defined(BAH_DEBUG) && (BAH_DEBUG > 3)
523 printf("%s: start(0x%x)\n", sc->sc_dev.dv_xname, ifp);
524 #endif
525
526 if ((ifp->if_flags & IFF_RUNNING) == 0)
527 return;
528
529 s = splimp();
530
531 if (sc->sc_tx_fillcount >= 2) {
532 splx(s);
533 return;
534 }
535
536 IF_DEQUEUE(&ifp->if_snd, m);
537 buffer = sc->sc_tx_act ^ 1;
538
539 splx(s);
540
541 if (m == 0)
542 return;
543
544 #if NBPFILTER > 0
545 /*
546 * If bpf is listening on this interface, let it
547 * see the packet before we commit it to the wire
548 *
549 * (can't give the copy in A2060 card RAM to bpf, because
550 * that RAM is just accessed as on every other byte)
551 */
552 if (ifp->if_bpf)
553 bpf_mtap(ifp->if_bpf, m);
554 #endif
555
556 /* we need the data length beforehand */
557 for (mp = m, tlen=0; mp; mp = mp->m_next)
558 tlen += mp->m_len;
559
560 #ifdef BAH_DEBUG
561 m = m_pullup(m,3); /* gcc does structure padding */
562 printf("%s: start: filling %ld from %ld to %ld type %ld\n",
563 sc->sc_dev.dv_xname,
564 buffer,
565 mtod(m, u_char *)[0],
566 mtod(m, u_char *)[1],
567 mtod(m, u_char *)[2]);
568 #else
569 m = m_pullup(m,2);
570 #endif
571 bah_ram_ptr = sc->sc_base->buffers + buffer*512*2;
572
573 /* write the addresses to RAM and throw them away */
574
575 /*
576 * Hardware does this: Yet Another Microsecond Saved.
577 * (btw, timing code says usually 2 microseconds)
578 * bah_ram_ptr[0*2] = mtod(m, u_char *)[0];
579 */
580 bah_ram_ptr[1*2] = mtod(m, u_char *)[1];
581 m_adj(m, 2);
582
583 /* correct total length for that */
584 tlen -= 2;
585 if (tlen < ARC_MIN_FORBID_LEN) {
586 offset = 256-tlen;
587 bah_ram_ptr[2*2] = offset;
588 } else {
589 if (tlen <= ARC_MAX_FORBID_LEN)
590 offset = 512-3-tlen;
591 else {
592 if (tlen > ARC_MAX_LEN)
593 tlen = ARC_MAX_LEN;
594 offset = 512-tlen;
595 }
596
597 bah_ram_ptr[2*2] = 0;
598 bah_ram_ptr[3*2] = offset;
599 }
600 bah_ram_ptr += offset*2;
601
602 /* lets loop again through the mbuf chain */
603
604 for (mp = m; mp; mp=mp->m_next) {
605 if (len = mp->m_len) { /* YAMS */
606 #ifdef BAHTIMINGS
607 lencopy = len;
608 copystart = clkread();
609 #endif
610 movepout(mtod(mp, caddr_t),bah_ram_ptr,len);
611
612 #ifdef BAHTIMINGS
613 perbyte = 1000*(clkread() - copystart) / lencopy;
614 sc->sc_stats.mincopyout =
615 MIN(sc->sc_stats.mincopyout,perbyte);
616 sc->sc_stats.maxcopyout =
617 max(sc->sc_stats.maxcopyout,perbyte);
618 #endif
619 bah_ram_ptr += len*2;
620 }
621 }
622
623 sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
624 sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
625
626 /* actually transmit the packet */
627 s = splimp();
628
629 if (++sc->sc_tx_fillcount > 1) {
630 /*
631 * We are filled up to the rim. No more bufs for the moment,
632 * please.
633 */
634 ifp->if_flags |= IFF_OACTIVE;
635 } else {
636 #ifdef BAH_DEBUG
637 printf("%s: start: starting transmitter on buffer %d\n",
638 sc->sc_dev.dv_xname, buffer);
639 #endif
640 /* Transmitter was off, start it */
641 sc->sc_tx_act = buffer;
642
643 /*
644 * We still can accept another buf, so don't:
645 * ifp->if_flags |= IFF_OACTIVE;
646 */
647 sc->sc_intmask |= ARC_TA;
648 sc->sc_base->command = ARC_TX(buffer);
649 sc->sc_base->status = sc->sc_intmask;
650
651 #ifdef BAHTIMINGS
652 bcopy((caddr_t)&time,
653 (caddr_t)&(sc->sc_stats.lasttxstart_tv),
654 sizeof(struct timeval));
655
656 sc->sc_stats.lasttxstart_mics = clkread();
657 #endif
658 }
659 splx(s);
660 m_freem(m);
661
662 /*
663 * We dont really need a transmit timeout timer, do we?
664 * XXX (sighing deeply) yes, after 10 times reading the docs,
665 * I realized that in the case the receiver NAKs the buffer request,
666 * the hardware retries till shutdown.
667 * TODO: Insert some reasonable transmit timeout timer.
668 */
669 }
670
671 void
672 callstart(sc, dummy)
673 struct bah_softc *sc;
674 void *dummy;
675 {
676
677 bah_start(&sc->sc_arccom.ac_if);
678 }
679
680 inline void
681 movepin(from,to,len)
682 volatile u_char *from;
683 u_char *to;
684 int len;
685 {
686 #ifdef BAHASMCOPY
687 unsigned long longd, longd1, longd2, longd3, longd4;
688 ushort shortd;
689
690 if ((len>3) && (((int)to) & 3)) {
691 switch (((int)to) & 3) {
692 case 3: *to++ = *from;
693 from+=2; --len;
694 break;
695 case 1: *to++ = *from;
696 from+=2; --len;
697 case 2: asm ("movepw %1@(0),%0": "=d" (shortd) : "a" (from));
698 *((ushort *)to)++ = shortd;
699 from += 4; len -= 2;
700 break;
701 default:
702 }
703
704 while (len>=32) {
705 asm("movepl %1@(0),%0" : "=d"(longd1) : "a" (from));
706 asm("movepl %1@(8),%0" : "=d"(longd2) : "a" (from));
707 asm("movepl %1@(16),%0" : "=d"(longd3) : "a" (from));
708 asm("movepl %1@(24),%0" : "=d"(longd4) : "a" (from));
709 *((unsigned long *)to)++ = longd1;
710 *((unsigned long *)to)++ = longd2;
711 *((unsigned long *)to)++ = longd3;
712 *((unsigned long *)to)++ = longd4;
713
714 asm("movepl %1@(32),%0" : "=d"(longd1) : "a" (from));
715 asm("movepl %1@(40),%0" : "=d"(longd2) : "a" (from));
716 asm("movepl %1@(48),%0" : "=d"(longd3) : "a" (from));
717 asm("movepl %1@(56),%0" : "=d"(longd4) : "a" (from));
718 *((unsigned long *)to)++ = longd1;
719 *((unsigned long *)to)++ = longd2;
720 *((unsigned long *)to)++ = longd3;
721 *((unsigned long *)to)++ = longd4;
722
723 from += 64; len -= 32;
724 }
725 while (len>0) {
726 asm("movepl %1@(0),%0" : "=d"(longd) : "a" (from));
727 *((unsigned long *)to)++ = longd;
728 from += 8; len -= 4;
729 }
730
731 }
732 #endif /* BAHASMCOPY */
733 while (len>0) {
734 *to++ = *from;
735 from+=2;
736 --len;
737 }
738
739 }
740
741 /*
742 * Arcnet interface receiver soft interrupt:
743 * get the stuff out of any filled buffer we find.
744 */
745 void
746 bah_srint(sc,dummy)
747 struct bah_softc *sc;
748 void *dummy;
749 {
750 int buffer, buffer1, len, len1, amount, offset, s, i;
751 u_char volatile *bah_ram_ptr;
752 struct mbuf *m, *dst, *head;
753 struct arc_header *ah;
754 struct ifnet *ifp;
755
756 head = 0;
757 ifp = &sc->sc_arccom.ac_if;
758
759 #ifdef BAHTIMINGS
760 int copystart,lencopy,perbyte;
761 #endif
762
763 s = splimp();
764 if (sc->sc_rx_fillcount <= 1)
765 buffer = sc->sc_rx_act ^ 1;
766 else {
767
768 i = ((unsigned)(sc->sc_bufstat[2] - sc->sc_bufstat[3])) % 256;
769 if (i < 64)
770 buffer = 3;
771 else if (i > 192)
772 buffer = 2;
773 else {
774 log(LOG_WARNING,
775 "%s: rx srint: which is older, %ld or %ld?\
776 (filled %ld)\n",
777 sc->sc_dev.dv_xname,
778 sc->sc_bufstat[2], sc->sc_bufstat[3],
779 sc->sc_rx_fillcount);
780 splx(s);
781 return;
782 }
783 }
784 splx(s);
785
786 /* Allocate header mbuf */
787 MGETHDR(m, M_DONTWAIT, MT_DATA);
788
789 if (m == 0) {
790 /*
791 * in case s.th. goes wrong with mem, drop it
792 * to make sure the receiver can be started again
793 * count it as input error (we dont have any other
794 * detectable)
795 */
796 ifp->if_ierrors++;
797 goto cleanup;
798 }
799
800 m->m_pkthdr.rcvif = ifp;
801 m->m_len = 0;
802
803 /*
804 * Align so that IP packet will be longword aligned. Here we
805 * assume that m_data of new packet is longword aligned.
806 * When implementing RFC1201, we might have to change it to 2,
807 * (2*sizeof(ulong) - ARC_HDRLEN - sizeof(splitflag) - sizeof(pckid))
808 * possibly packet type dependent.
809 */
810
811 m->m_data += 1; /* sizeof(ulong) - ARC_HDRLEN */
812
813 head = m;
814
815 ah = mtod(head, struct arc_header *);
816 bah_ram_ptr = sc->sc_base->buffers + buffer*512*2;
817
818 ah->arc_shost = bah_ram_ptr[0*2];
819 ah->arc_dhost = bah_ram_ptr[1*2];
820 offset = bah_ram_ptr[2*2];
821 if (offset)
822 len = 256 - offset;
823 else {
824 offset = bah_ram_ptr[3*2];
825 len = 512 - offset;
826 }
827 m->m_pkthdr.len = len+2; /* whole packet length */
828 m->m_len += 2; /* mbuf filled with ARCnet addresses */
829 bah_ram_ptr += offset*2; /* ram buffer continues there */
830
831 while (len > 0) {
832
833 len1 = len;
834 amount = M_TRAILINGSPACE(m);
835
836 if (amount == 0) {
837 dst = m;
838 MGET(m, M_DONTWAIT, MT_DATA);
839
840 if (m == 0) {
841 ifp->if_ierrors++;
842 goto cleanup;
843 }
844
845 if (len1 >= MINCLSIZE)
846 MCLGET(m, M_DONTWAIT);
847
848 m->m_len = 0;
849 dst->m_next = m;
850 amount = M_TRAILINGSPACE(m);
851 }
852
853 if (amount < len1)
854 len1 = amount;
855
856 #ifdef BAHTIMINGS
857 lencopy = len;
858 copystart = clkread();
859 #endif
860
861 movepin(bah_ram_ptr,mtod(m, u_char *) + m->m_len, len1);
862
863 #ifdef BAHTIMINGS
864 perbyte = 1000*(clkread() - copystart) / lencopy;
865 sc->sc_stats.mincopyin = MIN(sc->sc_stats.mincopyin,perbyte);
866 sc->sc_stats.maxcopyin = max(sc->sc_stats.maxcopyin,perbyte);
867 #endif
868
869 m->m_len += len1;
870 bah_ram_ptr += len1*2;
871 len -= len1;
872 }
873
874 #if NBPFILTER > 0
875 if (ifp->if_bpf)
876 bpf_mtap(ifp->if_bpf, head);
877 #endif
878
879 m_adj(head, 3); /* gcc does structure padding */
880 arc_input(ifp, ah, head);
881
882 /* arc_input has freed it, we dont need to... */
883
884 head = NULL;
885 ifp->if_ipackets++;
886
887 cleanup:
888
889 if (head == NULL)
890 m_freem(head);
891
892 s = splimp();
893
894 if (--sc->sc_rx_fillcount == 1) {
895
896 /* was off, restart it on buffer just emptied */
897 sc->sc_rx_act = buffer;
898 sc->sc_intmask |= ARC_RI;
899
900 /* this also clears the RI flag interupt: */
901 sc->sc_base->command = ARC_RXBC(buffer);
902 sc->sc_base->status = sc->sc_intmask;
903
904 #ifdef BAH_DEBUG
905 printf("%s: srint: restarted rx on buf %ld\n",
906 sc->sc_dev.dv_xname, buffer);
907 #endif
908 }
909 splx(s);
910 }
911
912 inline static void
913 bah_tint(sc)
914 struct bah_softc *sc;
915 {
916 int buffer;
917 u_char volatile *bah_ram_ptr;
918 int isr;
919 int clknow;
920
921 buffer = sc->sc_tx_act;
922 isr = sc->sc_base->status;
923
924 /* XXX insert retransmit code etc. here. For now just: */
925
926 if (!(isr & ARC_TMA) && !(sc->sc_broadcast[buffer]))
927 sc->sc_arccom.ac_if.if_oerrors++;
928 else
929 sc->sc_arccom.ac_if.if_opackets++;
930
931 #ifdef BAHTIMINGS
932 clknow = clkread();
933
934 sc->sc_stats.minsend = MIN(sc->sc_stats.minsend,
935 clknow - sc->sc_stats.lasttxstart_mics);
936
937 sc->sc_stats.maxsend = max(sc->sc_stats.maxsend,
938 clknow - sc->sc_stats.lasttxstart_mics);
939 #endif
940
941 /* We know we can accept another buffer at this point. */
942 sc->sc_arccom.ac_if.if_flags &= ~IFF_OACTIVE;
943
944 if (--sc->sc_tx_fillcount > 0) {
945
946 /*
947 * start tx on other buffer.
948 * This also clears the int flag
949 */
950 buffer ^= 1;
951 sc->sc_tx_act = buffer;
952
953 /*
954 * already given:
955 * sc->sc_intmask |= ARC_TA;
956 * sc->sc_base->status = sc->sc_intmask;
957 */
958 sc->sc_base->command = ARC_TX(buffer);
959
960 #ifdef BAHTIMINGS
961 bcopy((caddr_t)&time,
962 (caddr_t)&(sc->sc_stats.lasttxstart_tv),
963 sizeof(struct timeval));
964
965 sc->sc_stats.lasttxstart_mics = clkread();
966 #endif
967
968 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
969 printf("%s: tint: starting tx on buffer %d, status 0x%02x\n",
970 sc->sc_dev.dv_xname, buffer, sc->sc_base->status);
971 #endif
972 } else {
973 /* have to disable TX interrupt */
974 sc->sc_intmask &= ~ARC_TA;
975 sc->sc_base->status = sc->sc_intmask;
976
977 #ifdef BAH_DEBUG
978 printf("%s: tint: no more buffers to send, status 0x%02x\n",
979 sc->sc_dev.dv_xname, sc->sc_base->status);
980 #endif
981 }
982
983 #ifdef BAHSOFTCOPY
984 /* schedule soft int to fill a new buffer for us */
985 add_sicallback(callstart, sc, NULL);
986 #else
987 /* call it directly */
988 callstart(sc, NULL);
989 #endif
990 }
991
992 /*
993 * Our interrupt routine
994 */
995 int
996 bahintr(sc)
997 struct bah_softc *sc;
998 {
999 u_char isr;
1000 int buffer;
1001 int unit;
1002 u_long newsec;
1003
1004 isr = sc->sc_base->status;
1005 if (!(isr & sc->sc_intmask))
1006 return (0);
1007
1008 #if defined(BAH_DEBUG) && (BAH_DEBUG>1)
1009 printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
1010 sc->sc_dev.dv_xname, isr, sc->sc_intmask);
1011 #endif
1012
1013 if (isr & ARC_POR) {
1014 sc->sc_arccom.ac_anaddr = sc->sc_base->dipswitches;
1015 sc->sc_base->command = ARC_CLR(CLR_POR);
1016 log(LOG_WARNING, "%s: intr: got spurious power on reset int\n",
1017 sc->sc_dev.dv_xname);
1018 }
1019
1020 if (isr & ARC_RECON) {
1021 /*
1022 * we dont need to:
1023 * sc->sc_base->command = ARC_CONF(CONF_LONG);
1024 */
1025 sc->sc_base->command = ARC_CLR(CLR_RECONFIG);
1026 sc->sc_arccom.ac_if.if_collisions++;
1027 /*
1028 * if more than 2 seconds per reconfig, reset time and counter.
1029 * else
1030 * if more than ARC_EXCESSIVE_RECONFIGS reconfigs since last burst, complain
1031 * and set treshold for warnings to ARC_EXCESSIVE_RECONS_REWARN.
1032 * This allows for, e.g., new stations on the cable, or cable switching as long
1033 * as it is over after (normally) 16 seconds.
1034 * XXX Todo: check timeout bits in status word and double time if necessary.
1035 */
1036
1037 newsec = time.tv_sec;
1038 if (newsec - sc->sc_recontime > 2*sc->sc_reconcount) {
1039 sc->sc_recontime = newsec;
1040 sc->sc_reconcount = 0;
1041 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
1042 } else if (++sc->sc_reconcount > sc->sc_reconcount_excessive) {
1043 sc->sc_reconcount_excessive =
1044 ARC_EXCESSIVE_RECONS_REWARN;
1045 log(LOG_WARNING,
1046 "%s: excessive token losses, cable problem?\n",
1047 sc->sc_dev.dv_xname);
1048 sc->sc_recontime = newsec;
1049 sc->sc_reconcount = 0;
1050 }
1051 }
1052
1053 if (isr & ARC_RI) {
1054
1055 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
1056 printf("%s: intr: hard rint, act %ld 2:%ld 3:%ld\n",
1057 sc->sc_dev.dv_xname,
1058 sc->sc_rx_act, sc->sc_bufstat[2], sc->sc_bufstat[3]);
1059 #endif
1060
1061 buffer = sc->sc_rx_act;
1062 sc->sc_rx_packetno = (sc->sc_rx_packetno+1)%256;
1063 sc->sc_bufstat[buffer] = sc->sc_rx_packetno;
1064
1065 if (++sc->sc_rx_fillcount > 1) {
1066 sc->sc_intmask &= ~ARC_RI;
1067 sc->sc_base->status = sc->sc_intmask;
1068 } else {
1069
1070 buffer ^= 1;
1071 sc->sc_rx_act = buffer;
1072
1073 /*
1074 * Start receiver on other receive buffer.
1075 * This also clears the RI interupt flag.
1076 */
1077 sc->sc_base->command = ARC_RXBC(buffer);
1078 /* we are in the RX intr, so mask is ok for RX */
1079
1080 #ifdef BAH_DEBUG
1081 printf("%s: started rx for buffer %ld, status 0x%02x\n",
1082 sc->sc_dev.dv_xname, sc->sc_rx_act,
1083 sc->sc_base->status);
1084 #endif
1085 }
1086
1087 #ifdef BAHSOFTCOPY
1088 /* this one starts a soft int to copy out of the hw */
1089 add_sicallback(bah_srint,sc,NULL);
1090 #else
1091 /* this one does the copy here */
1092 bah_srint(sc,NULL);
1093 #endif
1094 }
1095
1096 if (isr & sc->sc_intmask & ARC_TA)
1097 bah_tint(sc);
1098
1099 return (1);
1100 }
1101
1102 /*
1103 * Process an ioctl request.
1104 * This code needs some work - it looks pretty ugly.
1105 */
1106 int
1107 bah_ioctl(ifp, command, data)
1108 register struct ifnet *ifp;
1109 unsigned long command;
1110 caddr_t data;
1111 {
1112 struct bah_softc *sc;
1113 register struct ifaddr *ifa;
1114 int s, error;
1115
1116 error = 0;
1117 sc = bahcd.cd_devs[ifp->if_unit];
1118 ifa = (struct ifaddr *)data;
1119 s = splimp();
1120
1121 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
1122 printf("%s: ioctl() called, cmd = 0x%x\n",
1123 sc->sc_dev.dv_xname, command);
1124 #endif
1125
1126 switch (command) {
1127 case SIOCSIFADDR:
1128 ifp->if_flags |= IFF_UP;
1129 switch (ifa->ifa_addr->sa_family) {
1130 #ifdef INET
1131 case AF_INET:
1132 bah_init(sc);
1133 sc->sc_arccom.ac_ipaddr = IA_SIN(ifa)->sin_addr;
1134 /*arpwhohas(&sc->sc_arccom, &IA_SIN(ifa)->sin_addr);*/
1135 break;
1136 #endif
1137 default:
1138 bah_init(sc);
1139 break;
1140 }
1141
1142 case SIOCSIFFLAGS:
1143 if ((ifp->if_flags & IFF_UP) == 0 &&
1144 (ifp->if_flags & IFF_RUNNING) != 0) {
1145 /*
1146 * If interface is marked down and it is running,
1147 * then stop it.
1148 */
1149 bah_stop(sc);
1150 ifp->if_flags &= ~IFF_RUNNING;
1151 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1152 (ifp->if_flags & IFF_RUNNING) == 0) {
1153 /*
1154 * If interface is marked up and it is stopped, then
1155 * start it.
1156 */
1157 bah_init(sc);
1158 }
1159 break;
1160
1161 /* Multicast not supported */
1162
1163 default:
1164 error = EINVAL;
1165 }
1166
1167 splx(s);
1168 return (error);
1169 }
1170