smc90cx6.c revision 1.21 1 /* $NetBSD: smc90cx6.c,v 1.21 1997/04/24 08:05:15 mycroft 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 if ((m->m_flags & M_EXT) == 0) {
846 ifp->if_ierrors++;
847 goto cleanup;
848 }
849 }
850
851 m->m_len = 0;
852 dst->m_next = m;
853 amount = M_TRAILINGSPACE(m);
854 }
855
856 if (amount < len1)
857 len1 = amount;
858
859 #ifdef BAHTIMINGS
860 lencopy = len;
861 copystart = clkread();
862 #endif
863
864 movepin(bah_ram_ptr, mtod(m, u_char *) + m->m_len, len1);
865
866 #ifdef BAHTIMINGS
867 perbyte = 1000 * (clkread() - copystart) / lencopy;
868 sc->sc_stats.mincopyin =
869 ulmin(sc->sc_stats.mincopyin, perbyte);
870 sc->sc_stats.maxcopyin =
871 ulmax(sc->sc_stats.maxcopyin, perbyte);
872 #endif
873
874 m->m_len += len1;
875 bah_ram_ptr += len1*2;
876 len -= len1;
877 }
878
879 #if NBPFILTER > 0
880 if (ifp->if_bpf)
881 bpf_mtap(ifp->if_bpf, head);
882 #endif
883
884 arc_input(&sc->sc_arccom.ac_if, head);
885
886 /* arc_input has freed it, we dont need to... */
887
888 head = NULL;
889 ifp->if_ipackets++;
890
891 cleanup:
892
893 if (head != NULL)
894 m_freem(head);
895
896 /* mark buffer as invalid by source id 0 */
897 sc->sc_base->buffers[buffer*512*2] = 0;
898 s = splnet();
899
900 if (--sc->sc_rx_fillcount == 2 - 1) {
901
902 /* was off, restart it on buffer just emptied */
903 sc->sc_rx_act = buffer;
904 sc->sc_intmask |= ARC_RI;
905
906 /* this also clears the RI flag interupt: */
907 sc->sc_base->command = ARC_RXBC(buffer);
908 sc->sc_base->status = sc->sc_intmask;
909
910 #ifdef BAH_DEBUG
911 printf("%s: srint: restarted rx on buf %ld\n",
912 sc->sc_dev.dv_xname, buffer);
913 #endif
914 }
915 splx(s);
916 }
917
918 __inline static void
919 bah_tint(sc, isr)
920 struct bah_softc *sc;
921 int isr;
922 {
923 struct ifnet *ifp;
924
925 int buffer;
926 #ifdef BAHTIMINGS
927 int clknow;
928 #endif
929
930 ifp = &(sc->sc_arccom.ac_if);
931 buffer = sc->sc_tx_act;
932
933 /*
934 * retransmit code:
935 * Normal situtations first for fast path:
936 * If acknowledgement received ok or broadcast, we're ok.
937 * else if
938 */
939
940 if (isr & ARC_TMA || sc->sc_broadcast[buffer])
941 sc->sc_arccom.ac_if.if_opackets++;
942 #ifdef BAHRETRANSMIT
943 else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
944 && --sc->sc_retransmits[buffer] > 0) {
945 /* retransmit same buffer */
946 sc->sc_base->command = ARC_TX(buffer);
947 return;
948 }
949 #endif
950 else
951 ifp->if_oerrors++;
952
953
954 #ifdef BAHTIMINGS
955 clknow = clkread();
956
957 sc->sc_stats.minsend = ulmin(sc->sc_stats.minsend,
958 clknow - sc->sc_stats.lasttxstart_mics);
959
960 sc->sc_stats.maxsend = ulmax(sc->sc_stats.maxsend,
961 clknow - sc->sc_stats.lasttxstart_mics);
962 #endif
963
964 /* We know we can accept another buffer at this point. */
965 ifp->if_flags &= ~IFF_OACTIVE;
966
967 if (--sc->sc_tx_fillcount > 0) {
968
969 /*
970 * start tx on other buffer.
971 * This also clears the int flag
972 */
973 buffer ^= 1;
974 sc->sc_tx_act = buffer;
975
976 /*
977 * already given:
978 * sc->sc_intmask |= ARC_TA;
979 * sc->sc_base->status = sc->sc_intmask;
980 */
981 sc->sc_base->command = ARC_TX(buffer);
982 /* init watchdog timer */
983 ifp->if_timer = ARCTIMEOUT;
984
985 #ifdef BAHTIMINGS
986 bcopy((caddr_t)&time,
987 (caddr_t)&(sc->sc_stats.lasttxstart_tv),
988 sizeof(struct timeval));
989
990 sc->sc_stats.lasttxstart_mics = clkread();
991 #endif
992
993 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
994 printf("%s: tint: starting tx on buffer %d, status 0x%02x\n",
995 sc->sc_dev.dv_xname, buffer, sc->sc_base->status);
996 #endif
997 } else {
998 /* have to disable TX interrupt */
999 sc->sc_intmask &= ~ARC_TA;
1000 sc->sc_base->status = sc->sc_intmask;
1001 /* ... and watchdog timer */
1002 ifp->if_timer = 0;
1003
1004 #ifdef BAH_DEBUG
1005 printf("%s: tint: no more buffers to send, status 0x%02x\n",
1006 sc->sc_dev.dv_xname, sc->sc_base->status);
1007 #endif
1008 }
1009
1010 #ifdef BAHSOFTCOPY
1011 /* schedule soft int to fill a new buffer for us */
1012 add_sicallback((sifunc_t)callstart, sc, NULL);
1013 #else
1014 /* call it directly */
1015 callstart(sc, NULL);
1016 #endif
1017 }
1018
1019 /*
1020 * Our interrupt routine
1021 */
1022 int
1023 bahintr(sc)
1024 struct bah_softc *sc;
1025 {
1026 u_char isr, maskedisr;
1027 int buffer;
1028 u_long newsec;
1029
1030 isr = sc->sc_base->status;
1031 maskedisr = isr & sc->sc_intmask;
1032 if (!maskedisr)
1033 return (0);
1034
1035 #if defined(BAH_DEBUG) && (BAH_DEBUG>1)
1036 printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
1037 sc->sc_dev.dv_xname, isr, sc->sc_intmask);
1038 #endif
1039
1040 if (maskedisr & ARC_POR) {
1041 sc->sc_arccom.ac_anaddr = sc->sc_base->dipswitches;
1042 sc->sc_base->command = ARC_CLR(CLR_POR);
1043 log(LOG_WARNING, "%s: intr: got spurious power on reset int\n",
1044 sc->sc_dev.dv_xname);
1045 }
1046
1047 if (maskedisr & ARC_RECON) {
1048 /*
1049 * we dont need to:
1050 * sc->sc_base->command = ARC_CONF(CONF_LONG);
1051 */
1052 sc->sc_base->command = ARC_CLR(CLR_RECONFIG);
1053 sc->sc_arccom.ac_if.if_collisions++;
1054
1055 /*
1056 * If more than 2 seconds per reconfig:
1057 * Reset time and counter.
1058 * else:
1059 * If more than ARC_EXCESSIVE_RECONFIGS reconfigs
1060 * since last burst, complain and set treshold for
1061 * warnings to ARC_EXCESSIVE_RECONS_REWARN.
1062 *
1063 * This allows for, e.g., new stations on the cable, or
1064 * cable switching as long as it is over after (normally)
1065 * 16 seconds.
1066 *
1067 * XXX TODO: check timeout bits in status word and double
1068 * time if necessary.
1069 */
1070
1071 newsec = time.tv_sec;
1072 if (newsec - sc->sc_recontime > 2 * sc->sc_reconcount) {
1073 sc->sc_recontime = newsec;
1074 sc->sc_reconcount = 0;
1075 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
1076 } else if (++sc->sc_reconcount > sc->sc_reconcount_excessive) {
1077 sc->sc_reconcount_excessive =
1078 ARC_EXCESSIVE_RECONS_REWARN;
1079 log(LOG_WARNING,
1080 "%s: excessive token losses, cable problem?\n",
1081 sc->sc_dev.dv_xname);
1082 sc->sc_recontime = newsec;
1083 sc->sc_reconcount = 0;
1084 }
1085 }
1086
1087 if (maskedisr & ARC_RI) {
1088
1089 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
1090 printf("%s: intr: hard rint, act %ld\n",
1091 sc->sc_dev.dv_xname, sc->sc_rx_act);
1092 #endif
1093
1094 buffer = sc->sc_rx_act;
1095 /* look if buffer is marked invalid: */
1096 if (sc->sc_base->buffers[buffer*512*2] == 0) {
1097 /* invalid marked buffer (or illegally configured sender) */
1098 log(LOG_WARNING,
1099 "%s: spurious RX interrupt or sender 0 (ignored)\n",
1100 sc->sc_dev.dv_xname);
1101 /*
1102 * restart receiver on same buffer.
1103 */
1104 sc->sc_base->command = ARC_RXBC(buffer);
1105
1106 } else if (++sc->sc_rx_fillcount > 1) {
1107 sc->sc_intmask &= ~ARC_RI;
1108 sc->sc_base->status = sc->sc_intmask;
1109 } else {
1110
1111 buffer ^= 1;
1112 sc->sc_rx_act = buffer;
1113
1114 /*
1115 * Start receiver on other receive buffer.
1116 * This also clears the RI interupt flag.
1117 */
1118 sc->sc_base->command = ARC_RXBC(buffer);
1119 /* we are in the RX intr, so mask is ok for RX */
1120
1121 #ifdef BAH_DEBUG
1122 printf("%s: started rx for buffer %ld, status 0x%02x\n",
1123 sc->sc_dev.dv_xname, sc->sc_rx_act,
1124 sc->sc_base->status);
1125 #endif
1126 }
1127
1128 #ifdef BAHSOFTCOPY
1129 /* this one starts a soft int to copy out of the hw */
1130 add_sicallback((sifunc_t)bah_srint, sc,NULL);
1131 #else
1132 /* this one does the copy here */
1133 bah_srint(sc,NULL);
1134 #endif
1135 }
1136
1137 if (maskedisr & ARC_TA)
1138 bah_tint(sc, isr);
1139
1140 return (1);
1141 }
1142
1143 /*
1144 * Process an ioctl request.
1145 * This code needs some work - it looks pretty ugly.
1146 */
1147 int
1148 bah_ioctl(ifp, command, data)
1149 register struct ifnet *ifp;
1150 u_long command;
1151 caddr_t data;
1152 {
1153 struct bah_softc *sc;
1154 register struct ifaddr *ifa;
1155 int s, error;
1156
1157 error = 0;
1158 sc = ifp->if_softc;
1159 ifa = (struct ifaddr *)data;
1160 s = splnet();
1161
1162 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
1163 printf("%s: ioctl() called, cmd = 0x%x\n",
1164 sc->sc_dev.dv_xname, command);
1165 #endif
1166
1167 switch (command) {
1168 case SIOCSIFADDR:
1169 ifp->if_flags |= IFF_UP;
1170 switch (ifa->ifa_addr->sa_family) {
1171 #ifdef INET
1172 case AF_INET:
1173 bah_init(sc);
1174 break;
1175 #endif
1176 default:
1177 bah_init(sc);
1178 break;
1179 }
1180
1181 case SIOCSIFFLAGS:
1182 if ((ifp->if_flags & IFF_UP) == 0 &&
1183 (ifp->if_flags & IFF_RUNNING) != 0) {
1184 /*
1185 * If interface is marked down and it is running,
1186 * then stop it.
1187 */
1188 bah_stop(sc);
1189 ifp->if_flags &= ~IFF_RUNNING;
1190 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1191 (ifp->if_flags & IFF_RUNNING) == 0) {
1192 /*
1193 * If interface is marked up and it is stopped, then
1194 * start it.
1195 */
1196 bah_init(sc);
1197 }
1198 break;
1199
1200 /* Multicast not supported */
1201
1202 default:
1203 error = EINVAL;
1204 }
1205
1206 splx(s);
1207 return (error);
1208 }
1209
1210 /*
1211 * watchdog routine for transmitter.
1212 *
1213 * We need this, because else a receiver whose hardware is alive, but whose
1214 * software has not enabled the Receiver, would make our hardware wait forever
1215 * Discovered this after 20 times reading the docs.
1216 *
1217 * Only thing we do is disable transmitter. We'll get an transmit timeout,
1218 * and the int handler will have to decide not to retransmit (in case
1219 * retransmission is implemented).
1220 *
1221 * This one assumes being called inside splnet(), and that net >= ipl2
1222 */
1223
1224 void
1225 bah_watchdog(ifp)
1226 struct ifnet *ifp;
1227 {
1228 struct bah_softc *sc = ifp->if_softc;
1229
1230 sc->sc_base->command = ARC_TXDIS;
1231 return;
1232 }
1233