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