smc90cx6.c revision 1.29 1 /* $NetBSD: smc90cx6.c,v 1.29 1999/02/16 23:34:13 is Exp $ */
2
3 /*-
4 * Copyright (c) 1994, 1995, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ignatios Souvatzis.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Chip core driver for the SMC90c26 / SMC90c56 (and SMC90c66 in '56
41 * compatibility mode) boards
42 */
43
44 #define BAHSOFTCOPY /**/
45 #define BAHRETRANSMIT /**/
46
47 #include "opt_inet.h"
48 #include "bpfilter.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/mbuf.h>
53 #include <sys/buf.h>
54 #include <sys/device.h>
55 #include <sys/protosw.h>
56 #include <sys/socket.h>
57 #include <sys/syslog.h>
58 #include <sys/ioctl.h>
59 #include <sys/errno.h>
60
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 #include <net/if_arc.h>
65
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip.h>
71 #include <netinet/if_inarp.h>
72 #endif
73
74 #if NBPFILTER > 0
75 #include <net/bpf.h>
76 #include <net/bpfdesc.h>
77 #endif
78
79 #include <sys/kernel.h>
80 #include <machine/bus.h>
81 #include <machine/cpu.h>
82 #include <machine/intr.h>
83 #include <machine/mtpr.h>
84
85 #include <dev/ic/smc90cx6reg.h>
86 #include <dev/ic/smc90cx6var.h>
87
88 /* these should be elsewhere */
89
90 #define ARC_MIN_LEN 1
91 #define ARC_MIN_FORBID_LEN 254
92 #define ARC_MAX_FORBID_LEN 256
93 #define ARC_MAX_LEN 508
94 #define ARC_ADDR_LEN 1
95
96 /* for watchdog timer. This should be more than enough. */
97 #define ARCTIMEOUT (5*IFNET_SLOWHZ)
98
99 /*
100 * This currently uses 2 bufs for tx, 2 for rx
101 *
102 * New rx protocol:
103 *
104 * rx has a fillcount variable. If fillcount > (NRXBUF-1),
105 * rx can be switched off from rx hard int.
106 * Else rx is restarted on the other receiver.
107 * rx soft int counts down. if it is == (NRXBUF-1), it restarts
108 * the receiver.
109 * To ensure packet ordering (we need that for 1201 later), we have a counter
110 * which is incremented modulo 256 on each receive and a per buffer
111 * variable, which is set to the counter on filling. The soft int can
112 * compare both values to determine the older packet.
113 *
114 * Transmit direction:
115 *
116 * bah_start checks tx_fillcount
117 * case 2: return
118 *
119 * else fill tx_act ^ 1 && inc tx_fillcount
120 *
121 * check tx_fillcount again.
122 * case 2: set IFF_OACTIVE to stop arc_output from filling us.
123 * case 1: start tx
124 *
125 * tint clears IFF_OCATIVE, decrements and checks tx_fillcount
126 * case 1: start tx on tx_act ^ 1, softcall bah_start
127 * case 0: softcall bah_start
128 *
129 * #define fill(i) get mbuf && copy mbuf to chip(i)
130 */
131
132 void bah_init __P((struct bah_softc *));
133 void bah_reset __P((struct bah_softc *));
134 void bah_stop __P((struct bah_softc *));
135 void bah_start __P((struct ifnet *));
136 int bahintr __P((void *));
137 int bah_ioctl __P((struct ifnet *, unsigned long, caddr_t));
138 void bah_watchdog __P((struct ifnet *));
139 void bah_srint __P((void *vsc));
140 static void bah_tint __P((struct bah_softc *, int));
141 void bah_reconwatch(void *);
142
143 /* short notation */
144
145 #define GETREG(off) bus_space_read_1(bst_r, regs, (off))
146 #define PUTREG(off, v) bus_space_write_1(bst_r, regs, (off), (v))
147 #define GETMEM(off) bus_space_read_1(bst_m, mem, (off))
148 #define PUTMEM(off, v) bus_space_write_1(bst_m, mem, (off), (v))
149
150 void
151 bah_attach_subr(sc)
152 struct bah_softc *sc;
153 {
154 struct ifnet *ifp = &sc->sc_arccom.ac_if;
155 int s;
156 u_int8_t linkaddress;
157
158 bus_space_tag_t bst_r = sc->sc_bst_r;
159 bus_space_tag_t bst_m = sc->sc_bst_m;
160 bus_space_handle_t regs = sc->sc_regs;
161 bus_space_handle_t mem = sc->sc_mem;
162
163 #if (defined(BAH_DEBUG) && (BAH_DEBUG > 2))
164 printf("\n%s: attach(0x%x, 0x%x, 0x%x)\n",
165 sc->sc_dev.dv_xname, parent, self, aux);
166 #endif
167 s = splhigh();
168
169 /*
170 * read the arcnet address from the board
171 */
172
173 (*sc->sc_reset)(sc, 1);
174
175 do {
176 delay(200);
177 } while (!(GETREG(BAHSTAT) & BAH_POR));
178
179 linkaddress = GETMEM(BAHMACOFF);
180
181 printf(": link addr 0x%02x(%d)\n", linkaddress, linkaddress);
182
183 /* clear the int mask... */
184
185 sc->sc_intmask = 0;
186 PUTREG(BAHSTAT, 0);
187
188 PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
189 PUTREG(BAHCMD, BAH_CLR(CLR_POR|CLR_RECONFIG));
190 sc->sc_recontime = sc->sc_reconcount = 0;
191
192 /* and reenable kernel int level */
193 splx(s);
194
195 /*
196 * set interface to stopped condition (reset)
197 */
198 bah_stop(sc);
199
200 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
201 ifp->if_softc = sc;
202 ifp->if_output = arc_output;
203 ifp->if_start = bah_start;
204 ifp->if_ioctl = bah_ioctl;
205 ifp->if_timer = 0;
206 ifp->if_watchdog = bah_watchdog;
207
208 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
209
210 ifp->if_mtu = ARCMTU;
211
212 if_attach(ifp);
213 arc_ifattach(ifp, linkaddress);
214
215 #if NBPFILTER > 0
216 bpfattach(&ifp->if_bpf, ifp, DLT_ARCNET, ARC_HDRLEN);
217 #endif
218 #ifdef BAHSOFTCOPY
219 sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, bah_srint, sc);
220 sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
221 (void (*) __P((void *)))bah_start, ifp);
222 #endif
223
224 }
225
226 /*
227 * Initialize device
228 *
229 */
230 void
231 bah_init(sc)
232 struct bah_softc *sc;
233 {
234 struct ifnet *ifp;
235 int s;
236
237 ifp = &sc->sc_arccom.ac_if;
238
239 if ((ifp->if_flags & IFF_RUNNING) == 0) {
240 s = splnet();
241 ifp->if_flags |= IFF_RUNNING;
242 bah_reset(sc);
243 bah_start(ifp);
244 splx(s);
245 }
246 }
247
248 /*
249 * Reset the interface...
250 *
251 * this assumes that it is called inside a critical section...
252 *
253 */
254 void
255 bah_reset(sc)
256 struct bah_softc *sc;
257 {
258 struct ifnet *ifp;
259 int linkaddress;
260
261 bus_space_tag_t bst_r = sc->sc_bst_r;
262 bus_space_tag_t bst_m = sc->sc_bst_m;
263 bus_space_handle_t regs = sc->sc_regs;
264 bus_space_handle_t mem = sc->sc_mem;
265
266 ifp = &sc->sc_arccom.ac_if;
267
268 #ifdef BAH_DEBUG
269 printf("%s: reset\n", sc->sc_dev.dv_xname);
270 #endif
271 /* stop and restart hardware */
272
273 (*sc->sc_reset)(sc, 1);
274 do {
275 DELAY(200);
276 } while (!(GETREG(BAHSTAT) & BAH_POR));
277
278 linkaddress = GETMEM(BAHMACOFF);
279
280 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
281 printf("%s: reset: card reset, link addr = 0x%02x (%ld)\n",
282 sc->sc_dev.dv_xname, linkaddress, linkaddress);
283 #endif
284
285 /* tell the routing level about the (possibly changed) link address */
286 arc_ifattach(ifp, linkaddress);
287
288 /* POR is NMI, but we need it below: */
289 sc->sc_intmask = BAH_RECON|BAH_POR;
290 PUTREG(BAHSTAT, sc->sc_intmask);
291 PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
292
293 #ifdef BAH_DEBUG
294 printf("%s: reset: chip configured, status=0x%02x\n",
295 sc->sc_dev.dv_xname, GETREG(BAHSTAT));
296 #endif
297 PUTREG(BAHCMD, BAH_CLR(CLR_POR|CLR_RECONFIG));
298
299 #ifdef BAH_DEBUG
300 printf("%s: reset: bits cleared, status=0x%02x\n",
301 sc->sc_dev.dv_xname, GETREG(BAHSTAT);
302 #endif
303
304 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
305
306 /* start receiver */
307
308 sc->sc_intmask |= BAH_RI;
309 sc->sc_rx_fillcount = 0;
310 sc->sc_rx_act = 2;
311
312 PUTREG(BAHCMD, BAH_RXBC(2));
313 PUTREG(BAHSTAT, sc->sc_intmask);
314
315 #ifdef BAH_DEBUG
316 printf("%s: reset: started receiver, status=0x%02x\n",
317 sc->sc_dev.dv_xname, GETREG(BAHSTAT);
318 #endif
319
320 /* and init transmitter status */
321 sc->sc_tx_act = 0;
322 sc->sc_tx_fillcount = 0;
323
324 ifp->if_flags |= IFF_RUNNING;
325 ifp->if_flags &= ~IFF_OACTIVE;
326
327 bah_start(ifp);
328 }
329
330 /*
331 * Take interface offline
332 */
333 void
334 bah_stop(sc)
335 struct bah_softc *sc;
336 {
337 bus_space_tag_t bst_r = sc->sc_bst_r;
338 bus_space_handle_t regs = sc->sc_regs;
339
340 /* Stop the interrupts */
341 PUTREG(BAHSTAT, 0);
342
343 /* Stop the interface */
344 (*sc->sc_reset)(sc, 0);
345
346 /* Stop watchdog timer */
347 sc->sc_arccom.ac_if.if_timer = 0;
348 }
349
350 /*
351 * Start output on interface. Get another datagram to send
352 * off the interface queue, and copy it to the
353 * interface becore starting the output
354 *
355 * this assumes that it is called inside a critical section...
356 * XXX hm... does it still?
357 *
358 */
359 void
360 bah_start(ifp)
361 struct ifnet *ifp;
362 {
363 struct bah_softc *sc = ifp->if_softc;
364 struct mbuf *m,*mp;
365
366 bus_space_tag_t bst_r = sc->sc_bst_r;
367 bus_space_handle_t regs = sc->sc_regs;
368 bus_space_tag_t bst_m = sc->sc_bst_m;
369 bus_space_handle_t mem = sc->sc_mem;
370
371 int bah_ram_ptr;
372 int len, tlen, offset, s, buffer;
373 #ifdef BAHTIMINGS
374 u_long copystart, lencopy, perbyte;
375 #endif
376
377 #if defined(BAH_DEBUG) && (BAH_DEBUG > 3)
378 printf("%s: start(0x%x)\n", sc->sc_dev.dv_xname, ifp);
379 #endif
380
381 if ((ifp->if_flags & IFF_RUNNING) == 0)
382 return;
383
384 s = splnet();
385
386 if (sc->sc_tx_fillcount >= 2) {
387 splx(s);
388 return;
389 }
390
391 IF_DEQUEUE(&ifp->if_snd, m);
392 buffer = sc->sc_tx_act ^ 1;
393
394 splx(s);
395
396 if (m == 0)
397 return;
398
399 #if NBPFILTER > 0
400 /*
401 * If bpf is listening on this interface, let it
402 * see the packet before we commit it to the wire
403 *
404 * (can't give the copy in A2060 card RAM to bpf, because
405 * that RAM is just accessed as on every other byte)
406 */
407 if (ifp->if_bpf)
408 bpf_mtap(ifp->if_bpf, m);
409 #endif
410
411 #ifdef BAH_DEBUG
412 m = m_pullup(m, 3); /* gcc does structure padding */
413 printf("%s: start: filling %ld from %ld to %ld type %ld\n",
414 sc->sc_dev.dv_xname, buffer, mtod(m, u_char *)[0],
415 mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
416 #else
417 m = m_pullup(m, 2);
418 #endif
419 bah_ram_ptr = buffer*512;
420
421 /* write the addresses to RAM and throw them away */
422
423 /*
424 * Hardware does this: Yet Another Microsecond Saved.
425 * (btw, timing code says usually 2 microseconds)
426 * PUTMEM(bah_ram_ptr + 0, mtod(m, u_char *)[0]);
427 */
428
429 PUTMEM(bah_ram_ptr + 1, mtod(m, u_char *)[1]);
430 m_adj(m, 2);
431
432 /* get total length left at this point */
433 tlen = m->m_pkthdr.len;
434 if (tlen < ARC_MIN_FORBID_LEN) {
435 offset = 256 - tlen;
436 PUTMEM(bah_ram_ptr + 2, offset);
437 } else {
438 PUTMEM(bah_ram_ptr + 2, 0);
439 if (tlen <= ARC_MAX_FORBID_LEN)
440 offset = 255; /* !!! */
441 else {
442 if (tlen > ARC_MAX_LEN)
443 tlen = ARC_MAX_LEN;
444 offset = 512 - tlen;
445 }
446 PUTMEM(bah_ram_ptr + 3, offset);
447
448 }
449 bah_ram_ptr += offset;
450
451 /* lets loop through the mbuf chain */
452
453 for (mp = m; mp; mp = mp->m_next) {
454 if ((len = mp->m_len)) { /* YAMS */
455 bus_space_write_region_1(bst_m, mem, bah_ram_ptr,
456 mtod(mp, caddr_t), len);
457
458 bah_ram_ptr += len;
459 }
460 }
461
462 sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
463 sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
464
465 /* actually transmit the packet */
466 s = splnet();
467
468 if (++sc->sc_tx_fillcount > 1) {
469 /*
470 * We are filled up to the rim. No more bufs for the moment,
471 * please.
472 */
473 ifp->if_flags |= IFF_OACTIVE;
474 } else {
475 #ifdef BAH_DEBUG
476 printf("%s: start: starting transmitter on buffer %d\n",
477 sc->sc_dev.dv_xname, buffer);
478 #endif
479 /* Transmitter was off, start it */
480 sc->sc_tx_act = buffer;
481
482 /*
483 * We still can accept another buf, so don't:
484 * ifp->if_flags |= IFF_OACTIVE;
485 */
486 sc->sc_intmask |= BAH_TA;
487 PUTREG(BAHCMD, BAH_TX(buffer));
488 PUTREG(BAHSTAT, sc->sc_intmask);
489
490 sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
491 }
492 splx(s);
493 m_freem(m);
494
495 /*
496 * After 10 times reading the docs, I realized
497 * that in the case the receiver NAKs the buffer request,
498 * the hardware retries till shutdown.
499 * This is integrated now in the code above.
500 */
501
502 return;
503 }
504
505 /*
506 * Arcnet interface receiver soft interrupt:
507 * get the stuff out of any filled buffer we find.
508 */
509 void
510 bah_srint(vsc)
511 void *vsc;
512 {
513 struct bah_softc *sc = (struct bah_softc *)vsc;
514 int buffer, len, len1, amount, offset, s, type;
515 int bah_ram_ptr;
516 struct mbuf *m, *dst, *head;
517 struct arc_header *ah;
518 struct ifnet *ifp;
519
520 bus_space_tag_t bst_r = sc->sc_bst_r;
521 bus_space_tag_t bst_m = sc->sc_bst_m;
522 bus_space_handle_t regs = sc->sc_regs;
523 bus_space_handle_t mem = sc->sc_mem;
524
525 ifp = &sc->sc_arccom.ac_if;
526 head = 0;
527
528 s = splnet();
529 buffer = sc->sc_rx_act ^ 1;
530 splx(s);
531
532 /* Allocate header mbuf */
533 MGETHDR(m, M_DONTWAIT, MT_DATA);
534
535 if (m == 0) {
536 /*
537 * in case s.th. goes wrong with mem, drop it
538 * to make sure the receiver can be started again
539 * count it as input error (we dont have any other
540 * detectable)
541 */
542 ifp->if_ierrors++;
543 goto cleanup;
544 }
545
546 m->m_pkthdr.rcvif = ifp;
547
548 /*
549 * Align so that IP packet will be longword aligned. Here we
550 * assume that m_data of new packet is longword aligned.
551 * When implementing PHDS, we might have to change it to 2,
552 * (2*sizeof(ulong) - ARC_HDRNEWLEN)), packet type dependent.
553 */
554
555 bah_ram_ptr = buffer*512;
556 offset = GETMEM(bah_ram_ptr + 2);
557 if (offset)
558 len = 256 - offset;
559 else {
560 offset = GETMEM(bah_ram_ptr + 3);
561 len = 512 - offset;
562 }
563 type = GETMEM(bah_ram_ptr + offset);
564 m->m_data += 1 + arc_isphds(type);
565
566 head = m;
567 ah = mtod(head, struct arc_header *);
568
569 ah->arc_shost = GETMEM(bah_ram_ptr + 0);
570 ah->arc_dhost = GETMEM(bah_ram_ptr + 1);
571
572 m->m_pkthdr.len = len+2; /* whole packet length */
573 m->m_len = 2; /* mbuf filled with ARCnet addresses */
574 bah_ram_ptr += offset; /* ram buffer continues there */
575
576 while (len > 0) {
577
578 len1 = len;
579 amount = M_TRAILINGSPACE(m);
580
581 if (amount == 0) {
582 dst = m;
583 MGET(m, M_DONTWAIT, MT_DATA);
584
585 if (m == 0) {
586 ifp->if_ierrors++;
587 goto cleanup;
588 }
589
590 if (len1 >= MINCLSIZE)
591 MCLGET(m, M_DONTWAIT);
592
593 m->m_len = 0;
594 dst->m_next = m;
595 amount = M_TRAILINGSPACE(m);
596 }
597
598 if (amount < len1)
599 len1 = amount;
600
601 bus_space_read_region_1(bst_m, mem, bah_ram_ptr,
602 mtod(m, u_char *) + m->m_len, len1);
603
604 m->m_len += len1;
605 bah_ram_ptr += len1;
606 len -= len1;
607 }
608
609 #if NBPFILTER > 0
610 if (ifp->if_bpf)
611 bpf_mtap(ifp->if_bpf, head);
612 #endif
613
614 arc_input(&sc->sc_arccom.ac_if, head);
615
616 /* arc_input has freed it, we dont need to... */
617
618 head = NULL;
619 ifp->if_ipackets++;
620
621 cleanup:
622
623 if (head != NULL)
624 m_freem(head);
625
626 /* mark buffer as invalid by source id 0 */
627 bus_space_write_1(bst_m, mem, buffer*512, 0);
628 s = splnet();
629
630 if (--sc->sc_rx_fillcount == 2 - 1) {
631
632 /* was off, restart it on buffer just emptied */
633 sc->sc_rx_act = buffer;
634 sc->sc_intmask |= BAH_RI;
635
636 /* this also clears the RI flag interupt: */
637 PUTREG(BAHCMD, BAH_RXBC(buffer));
638 PUTREG(BAHSTAT, sc->sc_intmask);
639
640 #ifdef BAH_DEBUG
641 printf("%s: srint: restarted rx on buf %ld\n",
642 sc->sc_dev.dv_xname, buffer);
643 #endif
644 }
645 splx(s);
646 }
647
648 __inline static void
649 bah_tint(sc, isr)
650 struct bah_softc *sc;
651 int isr;
652 {
653 struct ifnet *ifp;
654
655 bus_space_tag_t bst_r = sc->sc_bst_r;
656 bus_space_handle_t regs = sc->sc_regs;
657
658
659 int buffer;
660 #ifdef BAHTIMINGS
661 int clknow;
662 #endif
663
664 ifp = &(sc->sc_arccom.ac_if);
665 buffer = sc->sc_tx_act;
666
667 /*
668 * retransmit code:
669 * Normal situtations first for fast path:
670 * If acknowledgement received ok or broadcast, we're ok.
671 * else if
672 */
673
674 if (isr & BAH_TMA || sc->sc_broadcast[buffer])
675 sc->sc_arccom.ac_if.if_opackets++;
676 #ifdef BAHRETRANSMIT
677 else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
678 && --sc->sc_retransmits[buffer] > 0) {
679 /* retransmit same buffer */
680 PUTREG(BAHCMD, BAH_TX(buffer));
681 return;
682 }
683 #endif
684 else
685 ifp->if_oerrors++;
686
687
688 /* We know we can accept another buffer at this point. */
689 ifp->if_flags &= ~IFF_OACTIVE;
690
691 if (--sc->sc_tx_fillcount > 0) {
692
693 /*
694 * start tx on other buffer.
695 * This also clears the int flag
696 */
697 buffer ^= 1;
698 sc->sc_tx_act = buffer;
699
700 /*
701 * already given:
702 * sc->sc_intmask |= BAH_TA;
703 * PUTREG(BAHSTAT, sc->sc_intmask);
704 */
705 PUTREG(BAHCMD, BAH_TX(buffer));
706 /* init watchdog timer */
707 ifp->if_timer = ARCTIMEOUT;
708
709 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
710 printf("%s: tint: starting tx on buffer %d, status 0x%02x\n",
711 sc->sc_dev.dv_xname, buffer, GETREG(BAHSTAT));
712 #endif
713 } else {
714 /* have to disable TX interrupt */
715 sc->sc_intmask &= ~BAH_TA;
716 PUTREG(BAHSTAT, sc->sc_intmask);
717 /* ... and watchdog timer */
718 ifp->if_timer = 0;
719
720 #ifdef BAH_DEBUG
721 printf("%s: tint: no more buffers to send, status 0x%02x\n",
722 sc->sc_dev.dv_xname, GETREG(BAHSTAT));
723 #endif
724 }
725
726 /* XXXX TODO */
727 #ifdef BAHSOFTCOPY
728 /* schedule soft int to fill a new buffer for us */
729 softintr_schedule(sc->sc_txcookie);
730 #else
731 /* call it directly */
732 bah_start(ifp);
733 #endif
734 }
735
736 /*
737 * Our interrupt routine
738 */
739 int
740 bahintr(arg)
741 void *arg;
742 {
743 struct bah_softc *sc = arg;
744
745 bus_space_tag_t bst_r = sc->sc_bst_r;
746 bus_space_tag_t bst_m = sc->sc_bst_m;
747 bus_space_handle_t regs = sc->sc_regs;
748 bus_space_handle_t mem = sc->sc_mem;
749
750 u_char isr, maskedisr;
751 int buffer;
752 u_long newsec;
753
754 isr = GETREG(BAHSTAT);
755 maskedisr = isr & sc->sc_intmask;
756 if (!maskedisr)
757 return (0);
758 do {
759
760 #if defined(BAH_DEBUG) && (BAH_DEBUG>1)
761 printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
762 sc->sc_dev.dv_xname, isr, sc->sc_intmask);
763 #endif
764
765 if (maskedisr & BAH_POR) {
766 /*
767 * XXX We should never see this. Don't bother to store
768 * the address.
769 * sc->sc_arccom.ac_anaddr = GETMEM(BAHMACOFF);
770 */
771 PUTREG(BAHCMD, BAH_CLR(CLR_POR));
772 log(LOG_WARNING,
773 "%s: intr: got spurious power on reset int\n",
774 sc->sc_dev.dv_xname);
775 }
776
777 if (maskedisr & BAH_RECON) {
778 /*
779 * we dont need to:
780 * PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
781 */
782 PUTREG(BAHCMD, BAH_CLR(CLR_RECONFIG));
783 sc->sc_arccom.ac_if.if_collisions++;
784
785 /*
786 * If less than 2 seconds per reconfig:
787 * If ARC_EXCESSIVE_RECONFIGS
788 * since last burst, complain and set treshold for
789 * warnings to ARC_EXCESSIVE_RECONS_REWARN.
790 *
791 * This allows for, e.g., new stations on the cable, or
792 * cable switching as long as it is over after
793 * (normally) 16 seconds.
794 *
795 * XXX TODO: check timeout bits in status word and
796 * double time if necessary.
797 */
798
799 untimeout(bah_reconwatch, (void *)sc);
800 newsec = time.tv_sec;
801 if ((newsec - sc->sc_recontime <= 2) &&
802 (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
803 log(LOG_WARNING,
804 "%s: excessive token losses, "
805 "cable problem?\n", sc->sc_dev.dv_xname);
806 }
807 sc->sc_recontime = newsec;
808 timeout(bah_reconwatch, (void *)sc, 15*hz);
809 }
810
811 if (maskedisr & BAH_RI) {
812 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
813 printf("%s: intr: hard rint, act %ld\n",
814 sc->sc_dev.dv_xname, sc->sc_rx_act);
815 #endif
816
817 buffer = sc->sc_rx_act;
818 /* look if buffer is marked invalid: */
819 if (GETMEM(buffer*512) == 0) {
820 /*
821 * invalid marked buffer (or illegally
822 * configured sender)
823 */
824 log(LOG_WARNING,
825 "%s: spurious RX interupt or sender 0 "
826 " (ignored)\n", sc->sc_dev.dv_xname);
827 /*
828 * restart receiver on same buffer.
829 * XXX maybe better reset interface?
830 */
831 PUTREG(BAHCMD, BAH_RXBC(buffer));
832 } else {
833 if (++sc->sc_rx_fillcount > 1) {
834 sc->sc_intmask &= ~BAH_RI;
835 PUTREG(BAHSTAT, sc->sc_intmask);
836 } else {
837 buffer ^= 1;
838 sc->sc_rx_act = buffer;
839
840 /*
841 * Start receiver on other receive
842 * buffer. This also clears the RI
843 * interupt flag.
844 */
845 PUTREG(BAHCMD, BAH_RXBC(buffer));
846 /* in RX intr, so mask is ok for RX */
847
848 #ifdef BAH_DEBUG
849 printf("%s: strt rx for buf %ld, "
850 "stat 0x%02x\n",
851 sc->sc_dev.dv_xname, sc->sc_rx_act,
852 GETREG(BAHSTAT);
853 #endif
854 }
855
856 #ifdef BAHSOFTCOPY
857 /*
858 * this one starts a soft int to copy out
859 * of the hw
860 */
861 softintr_schedule(sc->sc_rxcookie);
862 #else
863 /* this one does the copy here */
864 bah_srint(sc);
865 #endif
866 }
867 }
868 if (maskedisr & BAH_TA) {
869 bah_tint(sc, isr);
870 }
871 isr = GETREG(BAHSTAT);
872 maskedisr = isr & sc->sc_intmask;
873 } while (maskedisr);
874
875 return (1);
876 }
877
878 void
879 bah_reconwatch(arg)
880 void *arg;
881 {
882 struct bah_softc *sc = arg;
883
884 if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
885 sc->sc_reconcount = 0;
886 log(LOG_WARNING, "%s: token valid again.\n",
887 sc->sc_dev.dv_xname);
888 }
889 sc->sc_reconcount = 0;
890 }
891
892
893 /*
894 * Process an ioctl request.
895 * This code needs some work - it looks pretty ugly.
896 */
897 int
898 bah_ioctl(ifp, command, data)
899 register struct ifnet *ifp;
900 u_long command;
901 caddr_t data;
902 {
903 struct bah_softc *sc;
904 register struct ifaddr *ifa;
905 struct ifreq *ifr;
906 int s, error;
907
908 error = 0;
909 sc = ifp->if_softc;
910 ifa = (struct ifaddr *)data;
911 ifr = (struct ifreq *)data;
912 s = splnet();
913
914 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
915 printf("%s: ioctl() called, cmd = 0x%x\n",
916 sc->sc_dev.dv_xname, command);
917 #endif
918
919 switch (command) {
920 case SIOCSIFADDR:
921 ifp->if_flags |= IFF_UP;
922 switch (ifa->ifa_addr->sa_family) {
923 #ifdef INET
924 case AF_INET:
925 bah_init(sc);
926 arp_ifinit(ifp, ifa);
927 break;
928 #endif
929 default:
930 bah_init(sc);
931 break;
932 }
933
934 case SIOCSIFFLAGS:
935 if ((ifp->if_flags & IFF_UP) == 0 &&
936 (ifp->if_flags & IFF_RUNNING) != 0) {
937 /*
938 * If interface is marked down and it is running,
939 * then stop it.
940 */
941 bah_stop(sc);
942 ifp->if_flags &= ~IFF_RUNNING;
943 } else if ((ifp->if_flags & IFF_UP) != 0 &&
944 (ifp->if_flags & IFF_RUNNING) == 0) {
945 /*
946 * If interface is marked up and it is stopped, then
947 * start it.
948 */
949 bah_init(sc);
950 }
951 break;
952
953 case SIOCADDMULTI:
954 case SIOCDELMULTI:
955 if (ifr->ifr_addr.sa_family == AF_INET)
956 error = 0;
957 else
958 error = EAFNOSUPPORT;
959 break;
960
961 default:
962 error = EINVAL;
963 }
964
965 splx(s);
966 return (error);
967 }
968
969 /*
970 * watchdog routine for transmitter.
971 *
972 * We need this, because else a receiver whose hardware is alive, but whose
973 * software has not enabled the Receiver, would make our hardware wait forever
974 * Discovered this after 20 times reading the docs.
975 *
976 * Only thing we do is disable transmitter. We'll get an transmit timeout,
977 * and the int handler will have to decide not to retransmit (in case
978 * retransmission is implemented).
979 *
980 * This one assumes being called inside splnet()
981 */
982
983 void
984 bah_watchdog(ifp)
985 struct ifnet *ifp;
986 {
987 struct bah_softc *sc = ifp->if_softc;
988
989 bus_space_tag_t bst_r = sc->sc_bst_r;
990 bus_space_handle_t regs = sc->sc_regs;
991
992 PUTREG(BAHCMD, BAH_TXDIS);
993 return;
994 }
995
996