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