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