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