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