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