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