No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / ic / smc90cx6.c
blob76a152e3fc1c913349b84ae73f87771adafc2c7a
1 /* $NetBSD: smc90cx6.c,v 1.59 2009/03/14 15:36:17 dsl Exp $ */
3 /*-
4 * Copyright (c) 1994, 1995, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ignatios Souvatzis.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Chip core driver for the SMC90c26 / SMC90c56 (and SMC90c66 in '56
34 * compatibility mode) boards
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: smc90cx6.c,v 1.59 2009/03/14 15:36:17 dsl Exp $");
40 /* #define BAHSOFTCOPY */
41 #define BAHRETRANSMIT /**/
43 #include "opt_inet.h"
44 #include "bpfilter.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/buf.h>
50 #include <sys/device.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 #include <sys/syslog.h>
54 #include <sys/ioctl.h>
55 #include <sys/errno.h>
56 #include <sys/kernel.h>
57 #include <sys/intr.h>
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_ether.h>
62 #include <net/if_types.h>
63 #include <net/if_arc.h>
65 #ifdef INET
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip.h>
70 #include <netinet/if_inarp.h>
71 #endif
73 #if NBPFILTER > 0
74 #include <net/bpf.h>
75 #include <net/bpfdesc.h>
76 #endif
78 #include <sys/bus.h>
79 #include <sys/cpu.h>
81 #include <dev/ic/smc90cx6reg.h>
82 #include <dev/ic/smc90cx6var.h>
84 /* these should be elsewhere */
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
92 /* for watchdog timer. This should be more than enough. */
93 #define ARCTIMEOUT (5*IFNET_SLOWHZ)
96 * This currently uses 2 bufs for tx, 2 for rx
98 * New rx protocol:
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.
110 * Transmit direction:
112 * bah_start checks tx_fillcount
113 * case 2: return
115 * else fill tx_act ^ 1 && inc tx_fillcount
117 * check tx_fillcount again.
118 * case 2: set IFF_OACTIVE to stop arc_output from filling us.
119 * case 1: start tx
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
125 * #define fill(i) get mbuf && copy mbuf to chip(i)
128 void bah_init(struct bah_softc *);
129 void bah_reset(struct bah_softc *);
130 void bah_stop(struct bah_softc *);
131 void bah_start(struct ifnet *);
132 int bahintr(void *);
133 int bah_ioctl(struct ifnet *, unsigned long, void *);
134 void bah_watchdog(struct ifnet *);
135 void bah_srint(void *vsc);
136 static void bah_tint(struct bah_softc *, int);
137 void bah_reconwatch(void *);
139 /* short notation */
141 #define GETREG(off) bus_space_read_1(bst_r, regs, (off))
142 #define PUTREG(off, v) bus_space_write_1(bst_r, regs, (off), (v))
143 #define GETMEM(off) bus_space_read_1(bst_m, mem, (off))
144 #define PUTMEM(off, v) bus_space_write_1(bst_m, mem, (off), (v))
146 void
147 bah_attach_subr(struct bah_softc *sc)
149 struct ifnet *ifp = &sc->sc_arccom.ac_if;
150 int s;
151 u_int8_t linkaddress;
153 bus_space_tag_t bst_r = sc->sc_bst_r;
154 bus_space_tag_t bst_m = sc->sc_bst_m;
155 bus_space_handle_t regs = sc->sc_regs;
156 bus_space_handle_t mem = sc->sc_mem;
158 #if (defined(BAH_DEBUG) && (BAH_DEBUG > 2))
159 printf("\n%s: attach(0x%x, 0x%x, 0x%x)\n",
160 device_xname(&sc->sc_dev), parent, self, aux);
161 #endif
162 s = splhigh();
165 * read the arcnet address from the board
168 (*sc->sc_reset)(sc, 1);
170 do {
171 delay(200);
172 } while (!(GETREG(BAHSTAT) & BAH_POR));
174 linkaddress = GETMEM(BAHMACOFF);
176 printf(": link addr 0x%02x(%d)\n", linkaddress, linkaddress);
178 /* clear the int mask... */
180 sc->sc_intmask = 0;
181 PUTREG(BAHSTAT, 0);
183 PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
184 PUTREG(BAHCMD, BAH_CLR(CLR_POR|CLR_RECONFIG));
185 sc->sc_recontime = sc->sc_reconcount = 0;
187 /* and reenable kernel int level */
188 splx(s);
191 * set interface to stopped condition (reset)
193 bah_stop(sc);
195 strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
196 ifp->if_softc = sc;
197 ifp->if_start = bah_start;
198 ifp->if_ioctl = bah_ioctl;
199 ifp->if_timer = 0;
200 ifp->if_watchdog = bah_watchdog;
201 IFQ_SET_READY(&ifp->if_snd);
203 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
205 ifp->if_mtu = ARCMTU;
207 arc_ifattach(ifp, linkaddress);
209 #ifdef BAHSOFTCOPY
210 sc->sc_rxcookie = softint_establish(SOFTINT_NET, bah_srint, sc);
211 sc->sc_txcookie = softint_establish(SOFTINT_NET,
212 (void (*)(void *))bah_start, ifp);
213 #endif
215 callout_init(&sc->sc_recon_ch, 0);
219 * Initialize device
222 void
223 bah_init(struct bah_softc *sc)
225 struct ifnet *ifp;
226 int s;
228 ifp = &sc->sc_arccom.ac_if;
230 if ((ifp->if_flags & IFF_RUNNING) == 0) {
231 s = splnet();
232 ifp->if_flags |= IFF_RUNNING;
233 bah_reset(sc);
234 bah_start(ifp);
235 splx(s);
240 * Reset the interface...
242 * this assumes that it is called inside a critical section...
245 void
246 bah_reset(struct bah_softc *sc)
248 struct ifnet *ifp;
249 uint8_t linkaddress;
251 bus_space_tag_t bst_r = sc->sc_bst_r;
252 bus_space_tag_t bst_m = sc->sc_bst_m;
253 bus_space_handle_t regs = sc->sc_regs;
254 bus_space_handle_t mem = sc->sc_mem;
256 ifp = &sc->sc_arccom.ac_if;
258 #ifdef BAH_DEBUG
259 printf("%s: reset\n", device_xname(&sc->sc_dev));
260 #endif
261 /* stop and restart hardware */
263 (*sc->sc_reset)(sc, 1);
264 do {
265 DELAY(200);
266 } while (!(GETREG(BAHSTAT) & BAH_POR));
268 linkaddress = GETMEM(BAHMACOFF);
270 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
271 printf("%s: reset: card reset, link addr = 0x%02x (%ld)\n",
272 device_xname(&sc->sc_dev), linkaddress, linkaddress);
273 #endif
275 /* tell the routing level about the (possibly changed) link address */
276 if_set_sadl(ifp, &linkaddress, sizeof(linkaddress), false);
278 /* POR is NMI, but we need it below: */
279 sc->sc_intmask = BAH_RECON|BAH_POR;
280 PUTREG(BAHSTAT, sc->sc_intmask);
281 PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
283 #ifdef BAH_DEBUG
284 printf("%s: reset: chip configured, status=0x%02x\n",
285 device_xname(&sc->sc_dev), GETREG(BAHSTAT));
286 #endif
287 PUTREG(BAHCMD, BAH_CLR(CLR_POR|CLR_RECONFIG));
289 #ifdef BAH_DEBUG
290 printf("%s: reset: bits cleared, status=0x%02x\n",
291 device_xname(&sc->sc_dev), GETREG(BAHSTAT);
292 #endif
294 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
296 /* start receiver */
298 sc->sc_intmask |= BAH_RI;
299 sc->sc_rx_fillcount = 0;
300 sc->sc_rx_act = 2;
302 PUTREG(BAHCMD, BAH_RXBC(2));
303 PUTREG(BAHSTAT, sc->sc_intmask);
305 #ifdef BAH_DEBUG
306 printf("%s: reset: started receiver, status=0x%02x\n",
307 device_xname(&sc->sc_dev), GETREG(BAHSTAT);
308 #endif
310 /* and init transmitter status */
311 sc->sc_tx_act = 0;
312 sc->sc_tx_fillcount = 0;
314 ifp->if_flags |= IFF_RUNNING;
315 ifp->if_flags &= ~IFF_OACTIVE;
317 bah_start(ifp);
321 * Take interface offline
323 void
324 bah_stop(struct bah_softc *sc)
326 bus_space_tag_t bst_r = sc->sc_bst_r;
327 bus_space_handle_t regs = sc->sc_regs;
329 /* Stop the interrupts */
330 PUTREG(BAHSTAT, 0);
332 /* Stop the interface */
333 (*sc->sc_reset)(sc, 0);
335 /* Stop watchdog timer */
336 sc->sc_arccom.ac_if.if_timer = 0;
340 * Start output on interface. Get another datagram to send
341 * off the interface queue, and copy it to the
342 * interface before starting the output
344 * this assumes that it is called inside a critical section...
345 * XXX hm... does it still?
348 void
349 bah_start(struct ifnet *ifp)
351 struct bah_softc *sc = ifp->if_softc;
352 struct mbuf *m,*mp;
354 bus_space_tag_t bst_r = sc->sc_bst_r;
355 bus_space_handle_t regs = sc->sc_regs;
356 bus_space_tag_t bst_m = sc->sc_bst_m;
357 bus_space_handle_t mem = sc->sc_mem;
359 int bah_ram_ptr;
360 int len, tlen, offset, s, buffer;
361 #ifdef BAHTIMINGS
362 u_long copystart, lencopy, perbyte;
363 #endif
365 #if defined(BAH_DEBUG) && (BAH_DEBUG > 3)
366 printf("%s: start(0x%x)\n", device_xname(&sc->sc_dev), ifp);
367 #endif
369 if ((ifp->if_flags & IFF_RUNNING) == 0)
370 return;
372 s = splnet();
374 if (sc->sc_tx_fillcount >= 2) {
375 splx(s);
376 return;
379 IFQ_DEQUEUE(&ifp->if_snd, m);
380 buffer = sc->sc_tx_act ^ 1;
382 splx(s);
384 if (m == 0)
385 return;
387 #if NBPFILTER > 0
389 * If bpf is listening on this interface, let it
390 * see the packet before we commit it to the wire
392 * (can't give the copy in A2060 card RAM to bpf, because
393 * that RAM is just accessed as on every other byte)
395 if (ifp->if_bpf)
396 bpf_mtap(ifp->if_bpf, m);
397 #endif
399 #ifdef BAH_DEBUG
400 if (m->m_len < ARC_HDRLEN)
401 m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
402 printf("%s: start: filling %ld from %ld to %ld type %ld\n",
403 device_xname(&sc->sc_dev), buffer, mtod(m, u_char *)[0],
404 mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
405 #else
406 if (m->m_len < 2)
407 m = m_pullup(m, 2);
408 #endif
409 bah_ram_ptr = buffer*512;
411 if (m == 0)
412 return;
414 /* write the addresses to RAM and throw them away */
417 * Hardware does this: Yet Another Microsecond Saved.
418 * (btw, timing code says usually 2 microseconds)
419 * PUTMEM(bah_ram_ptr + 0, mtod(m, u_char *)[0]);
422 PUTMEM(bah_ram_ptr + 1, mtod(m, u_char *)[1]);
423 m_adj(m, 2);
425 /* get total length left at this point */
426 tlen = m->m_pkthdr.len;
427 if (tlen < ARC_MIN_FORBID_LEN) {
428 offset = 256 - tlen;
429 PUTMEM(bah_ram_ptr + 2, offset);
430 } else {
431 PUTMEM(bah_ram_ptr + 2, 0);
432 if (tlen <= ARC_MAX_FORBID_LEN)
433 offset = 255; /* !!! */
434 else {
435 if (tlen > ARC_MAX_LEN)
436 tlen = ARC_MAX_LEN;
437 offset = 512 - tlen;
439 PUTMEM(bah_ram_ptr + 3, offset);
442 bah_ram_ptr += offset;
444 /* lets loop through the mbuf chain */
446 for (mp = m; mp; mp = mp->m_next) {
447 if ((len = mp->m_len)) { /* YAMS */
448 bus_space_write_region_1(bst_m, mem, bah_ram_ptr,
449 mtod(mp, void *), len);
451 bah_ram_ptr += len;
455 sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
456 sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
458 /* actually transmit the packet */
459 s = splnet();
461 if (++sc->sc_tx_fillcount > 1) {
463 * We are filled up to the rim. No more bufs for the moment,
464 * please.
466 ifp->if_flags |= IFF_OACTIVE;
467 } else {
468 #ifdef BAH_DEBUG
469 printf("%s: start: starting transmitter on buffer %d\n",
470 device_xname(&sc->sc_dev), buffer);
471 #endif
472 /* Transmitter was off, start it */
473 sc->sc_tx_act = buffer;
476 * We still can accept another buf, so don't:
477 * ifp->if_flags |= IFF_OACTIVE;
479 sc->sc_intmask |= BAH_TA;
480 PUTREG(BAHCMD, BAH_TX(buffer));
481 PUTREG(BAHSTAT, sc->sc_intmask);
483 sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
485 splx(s);
486 m_freem(m);
489 * After 10 times reading the docs, I realized
490 * that in the case the receiver NAKs the buffer request,
491 * the hardware retries till shutdown.
492 * This is integrated now in the code above.
495 return;
499 * Arcnet interface receiver soft interrupt:
500 * get the stuff out of any filled buffer we find.
502 void
503 bah_srint(void *vsc)
505 struct bah_softc *sc = (struct bah_softc *)vsc;
506 int buffer, len, len1, amount, offset, s, type;
507 int bah_ram_ptr;
508 struct mbuf *m, *dst, *head;
509 struct arc_header *ah;
510 struct ifnet *ifp;
512 bus_space_tag_t bst_r = sc->sc_bst_r;
513 bus_space_tag_t bst_m = sc->sc_bst_m;
514 bus_space_handle_t regs = sc->sc_regs;
515 bus_space_handle_t mem = sc->sc_mem;
517 ifp = &sc->sc_arccom.ac_if;
518 head = 0;
520 s = splnet();
521 buffer = sc->sc_rx_act ^ 1;
522 splx(s);
524 /* Allocate header mbuf */
525 MGETHDR(m, M_DONTWAIT, MT_DATA);
527 if (m == 0) {
529 * in case s.th. goes wrong with mem, drop it
530 * to make sure the receiver can be started again
531 * count it as input error (we dont have any other
532 * detectable)
534 ifp->if_ierrors++;
535 goto cleanup;
538 m->m_pkthdr.rcvif = ifp;
541 * Align so that IP packet will be longword aligned. Here we
542 * assume that m_data of new packet is longword aligned.
543 * When implementing PHDS, we might have to change it to 2,
544 * (2*sizeof(ulong) - ARC_HDRNEWLEN)), packet type dependent.
547 bah_ram_ptr = buffer*512;
548 offset = GETMEM(bah_ram_ptr + 2);
549 if (offset)
550 len = 256 - offset;
551 else {
552 offset = GETMEM(bah_ram_ptr + 3);
553 len = 512 - offset;
555 if (len+2 >= MINCLSIZE)
556 MCLGET(m, M_DONTWAIT);
558 if (m == 0) {
559 ifp->if_ierrors++;
560 goto cleanup;
563 type = GETMEM(bah_ram_ptr + offset);
564 m->m_data += 1 + arc_isphds(type);
566 head = m;
567 ah = mtod(head, struct arc_header *);
569 ah->arc_shost = GETMEM(bah_ram_ptr + 0);
570 ah->arc_dhost = GETMEM(bah_ram_ptr + 1);
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 */
576 while (len > 0) {
578 len1 = len;
579 amount = M_TRAILINGSPACE(m);
581 if (amount == 0) {
582 dst = m;
583 MGET(m, M_DONTWAIT, MT_DATA);
585 if (m == 0) {
586 ifp->if_ierrors++;
587 goto cleanup;
590 if (len1 >= MINCLSIZE)
591 MCLGET(m, M_DONTWAIT);
593 m->m_len = 0;
594 dst->m_next = m;
595 amount = M_TRAILINGSPACE(m);
598 if (amount < len1)
599 len1 = amount;
601 bus_space_read_region_1(bst_m, mem, bah_ram_ptr,
602 mtod(m, u_char *) + m->m_len, len1);
604 m->m_len += len1;
605 bah_ram_ptr += len1;
606 len -= len1;
609 #if NBPFILTER > 0
610 if (ifp->if_bpf)
611 bpf_mtap(ifp->if_bpf, head);
612 #endif
614 (*sc->sc_arccom.ac_if.if_input)(&sc->sc_arccom.ac_if, head);
616 head = NULL;
617 ifp->if_ipackets++;
619 cleanup:
621 if (head != NULL)
622 m_freem(head);
624 /* mark buffer as invalid by source id 0 */
625 bus_space_write_1(bst_m, mem, buffer*512, 0);
626 s = splnet();
628 if (--sc->sc_rx_fillcount == 2 - 1) {
630 /* was off, restart it on buffer just emptied */
631 sc->sc_rx_act = buffer;
632 sc->sc_intmask |= BAH_RI;
634 /* this also clears the RI flag interrupt: */
635 PUTREG(BAHCMD, BAH_RXBC(buffer));
636 PUTREG(BAHSTAT, sc->sc_intmask);
638 #ifdef BAH_DEBUG
639 printf("%s: srint: restarted rx on buf %ld\n",
640 device_xname(&sc->sc_dev), buffer);
641 #endif
643 splx(s);
646 inline static void
647 bah_tint(struct bah_softc *sc, int isr)
649 struct ifnet *ifp;
651 bus_space_tag_t bst_r = sc->sc_bst_r;
652 bus_space_handle_t regs = sc->sc_regs;
655 int buffer;
656 #ifdef BAHTIMINGS
657 int clknow;
658 #endif
660 ifp = &(sc->sc_arccom.ac_if);
661 buffer = sc->sc_tx_act;
664 * retransmit code:
665 * Normal situations first for fast path:
666 * If acknowledgement received ok or broadcast, we're ok.
667 * else if
670 if (isr & BAH_TMA || sc->sc_broadcast[buffer])
671 sc->sc_arccom.ac_if.if_opackets++;
672 #ifdef BAHRETRANSMIT
673 else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
674 && --sc->sc_retransmits[buffer] > 0) {
675 /* retransmit same buffer */
676 PUTREG(BAHCMD, BAH_TX(buffer));
677 return;
679 #endif
680 else
681 ifp->if_oerrors++;
684 /* We know we can accept another buffer at this point. */
685 ifp->if_flags &= ~IFF_OACTIVE;
687 if (--sc->sc_tx_fillcount > 0) {
690 * start tx on other buffer.
691 * This also clears the int flag
693 buffer ^= 1;
694 sc->sc_tx_act = buffer;
697 * already given:
698 * sc->sc_intmask |= BAH_TA;
699 * PUTREG(BAHSTAT, sc->sc_intmask);
701 PUTREG(BAHCMD, BAH_TX(buffer));
702 /* init watchdog timer */
703 ifp->if_timer = ARCTIMEOUT;
705 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
706 printf("%s: tint: starting tx on buffer %d, status 0x%02x\n",
707 device_xname(&sc->sc_dev), buffer, GETREG(BAHSTAT));
708 #endif
709 } else {
710 /* have to disable TX interrupt */
711 sc->sc_intmask &= ~BAH_TA;
712 PUTREG(BAHSTAT, sc->sc_intmask);
713 /* ... and watchdog timer */
714 ifp->if_timer = 0;
716 #ifdef BAH_DEBUG
717 printf("%s: tint: no more buffers to send, status 0x%02x\n",
718 device_xname(&sc->sc_dev), GETREG(BAHSTAT));
719 #endif
722 /* XXXX TODO */
723 #ifdef BAHSOFTCOPY
724 /* schedule soft int to fill a new buffer for us */
725 softint_schedule(sc->sc_txcookie);
726 #else
727 /* call it directly */
728 bah_start(ifp);
729 #endif
733 * Our interrupt routine
736 bahintr(void *arg)
738 struct bah_softc *sc = arg;
740 bus_space_tag_t bst_r = sc->sc_bst_r;
741 bus_space_tag_t bst_m = sc->sc_bst_m;
742 bus_space_handle_t regs = sc->sc_regs;
743 bus_space_handle_t mem = sc->sc_mem;
745 u_char isr, maskedisr;
746 int buffer;
747 u_long newsec;
749 isr = GETREG(BAHSTAT);
750 maskedisr = isr & sc->sc_intmask;
751 if (!maskedisr)
752 return (0);
753 do {
755 #if defined(BAH_DEBUG) && (BAH_DEBUG>1)
756 printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
757 device_xname(&sc->sc_dev), isr, sc->sc_intmask);
758 #endif
760 if (maskedisr & BAH_POR) {
762 * XXX We should never see this. Don't bother to store
763 * the address.
764 * sc->sc_arccom.ac_anaddr = GETMEM(BAHMACOFF);
766 PUTREG(BAHCMD, BAH_CLR(CLR_POR));
767 log(LOG_WARNING,
768 "%s: intr: got spurious power on reset int\n",
769 device_xname(&sc->sc_dev));
772 if (maskedisr & BAH_RECON) {
774 * we dont need to:
775 * PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
777 PUTREG(BAHCMD, BAH_CLR(CLR_RECONFIG));
778 sc->sc_arccom.ac_if.if_collisions++;
781 * If less than 2 seconds per reconfig:
782 * If ARC_EXCESSIVE_RECONFIGS
783 * since last burst, complain and set threshold for
784 * warnings to ARC_EXCESSIVE_RECONS_REWARN.
786 * This allows for, e.g., new stations on the cable, or
787 * cable switching as long as it is over after
788 * (normally) 16 seconds.
790 * XXX TODO: check timeout bits in status word and
791 * double time if necessary.
794 callout_stop(&sc->sc_recon_ch);
795 newsec = time_second;
796 if ((newsec - sc->sc_recontime <= 2) &&
797 (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
798 log(LOG_WARNING,
799 "%s: excessive token losses, "
800 "cable problem?\n", device_xname(&sc->sc_dev));
802 sc->sc_recontime = newsec;
803 callout_reset(&sc->sc_recon_ch, 15 * hz,
804 bah_reconwatch, (void *)sc);
807 if (maskedisr & BAH_RI) {
808 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
809 printf("%s: intr: hard rint, act %ld\n",
810 device_xname(&sc->sc_dev), sc->sc_rx_act);
811 #endif
813 buffer = sc->sc_rx_act;
814 /* look if buffer is marked invalid: */
815 if (GETMEM(buffer*512) == 0) {
817 * invalid marked buffer (or illegally
818 * configured sender)
820 log(LOG_WARNING,
821 "%s: spurious RX interrupt or sender 0 "
822 " (ignored)\n", device_xname(&sc->sc_dev));
824 * restart receiver on same buffer.
825 * XXX maybe better reset interface?
827 PUTREG(BAHCMD, BAH_RXBC(buffer));
828 } else {
829 if (++sc->sc_rx_fillcount > 1) {
830 sc->sc_intmask &= ~BAH_RI;
831 PUTREG(BAHSTAT, sc->sc_intmask);
832 } else {
833 buffer ^= 1;
834 sc->sc_rx_act = buffer;
837 * Start receiver on other receive
838 * buffer. This also clears the RI
839 * interrupt flag.
841 PUTREG(BAHCMD, BAH_RXBC(buffer));
842 /* in RX intr, so mask is ok for RX */
844 #ifdef BAH_DEBUG
845 printf("%s: strt rx for buf %ld, "
846 "stat 0x%02x\n",
847 device_xname(&sc->sc_dev), sc->sc_rx_act,
848 GETREG(BAHSTAT);
849 #endif
852 #ifdef BAHSOFTCOPY
854 * this one starts a soft int to copy out
855 * of the hw
857 softint_schedule(sc->sc_rxcookie);
858 #else
859 /* this one does the copy here */
860 bah_srint(sc);
861 #endif
864 if (maskedisr & BAH_TA) {
865 bah_tint(sc, isr);
867 isr = GETREG(BAHSTAT);
868 maskedisr = isr & sc->sc_intmask;
869 } while (maskedisr);
871 return (1);
874 void
875 bah_reconwatch(void *arg)
877 struct bah_softc *sc = arg;
879 if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
880 sc->sc_reconcount = 0;
881 log(LOG_WARNING, "%s: token valid again.\n",
882 device_xname(&sc->sc_dev));
884 sc->sc_reconcount = 0;
889 * Process an ioctl request.
890 * This code needs some work - it looks pretty ugly.
893 bah_ioctl(struct ifnet *ifp, u_long cmd, void *data)
895 struct bah_softc *sc;
896 struct ifaddr *ifa;
897 struct ifreq *ifr;
898 int s, error;
900 error = 0;
901 sc = ifp->if_softc;
902 ifa = (struct ifaddr *)data;
903 ifr = (struct ifreq *)data;
904 s = splnet();
906 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
907 printf("%s: ioctl() called, cmd = 0x%x\n",
908 device_xname(&sc->sc_dev), cmd);
909 #endif
911 switch (cmd) {
912 case SIOCINITIFADDR:
913 ifp->if_flags |= IFF_UP;
914 bah_init(sc);
915 switch (ifa->ifa_addr->sa_family) {
916 #ifdef INET
917 case AF_INET:
918 arp_ifinit(ifp, ifa);
919 break;
920 #endif
921 default:
922 break;
925 case SIOCSIFFLAGS:
926 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
927 break;
928 /* XXX re-use ether_ioctl() */
929 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
930 case IFF_RUNNING:
932 * If interface is marked down and it is running,
933 * then stop it.
935 bah_stop(sc);
936 ifp->if_flags &= ~IFF_RUNNING;
937 break;
938 case IFF_UP:
940 * If interface is marked up and it is stopped, then
941 * start it.
943 bah_init(sc);
944 break;
946 break;
948 case SIOCADDMULTI:
949 case SIOCDELMULTI:
950 switch (ifreq_getaddr(cmd, ifr)->sa_family) {
951 case AF_INET:
952 case AF_INET6:
953 error = 0;
954 break;
955 default:
956 error = EAFNOSUPPORT;
957 break;
959 break;
961 default:
962 error = ether_ioctl(ifp, cmd, data);
965 splx(s);
966 return (error);
970 * watchdog routine for transmitter.
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.
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).
980 * This one assumes being called inside splnet()
983 void
984 bah_watchdog(struct ifnet *ifp)
986 struct bah_softc *sc = ifp->if_softc;
988 bus_space_tag_t bst_r = sc->sc_bst_r;
989 bus_space_handle_t regs = sc->sc_regs;
991 PUTREG(BAHCMD, BAH_TXDIS);
992 return;