xref: /netbsd-src/sys/dev/ic/smc90cx6.c (revision 5b28f239895d55856221c590945769250e289f5f)
1 /*	$NetBSD: smc90cx6.c,v 1.77 2024/09/08 09:36:50 rillig 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.77 2024/09/08 09:36:50 rillig 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 #include <net/bpf.h>
64 
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
72 
73 #include <sys/bus.h>
74 #include <sys/cpu.h>
75 
76 #include <dev/ic/smc90cx6reg.h>
77 #include <dev/ic/smc90cx6var.h>
78 
79 /* these should be elsewhere */
80 
81 #define ARC_MIN_LEN 1
82 #define ARC_MIN_FORBID_LEN 254
83 #define ARC_MAX_FORBID_LEN 256
84 #define ARC_MAX_LEN 508
85 #define ARC_ADDR_LEN 1
86 
87 /* for watchdog timer. This should be more than enough. */
88 #define ARCTIMEOUT (5*IFNET_SLOWHZ)
89 
90 /*
91  * This currently uses 2 bufs for tx, 2 for rx
92  *
93  * New rx protocol:
94  *
95  * rx has a fillcount variable. If fillcount > (NRXBUF-1),
96  * rx can be switched off from rx hard int.
97  * Else rx is restarted on the other receiver.
98  * rx soft int counts down. if it is == (NRXBUF-1), it restarts
99  * the receiver.
100  * To ensure packet ordering (we need that for 1201 later), we have a counter
101  * which is incremented modulo 256 on each receive and a per buffer
102  * variable, which is set to the counter on filling. The soft int can
103  * compare both values to determine the older packet.
104  *
105  * Transmit direction:
106  *
107  * bah_start checks tx_fillcount
108  * case 2: return
109  *
110  * else fill tx_act ^ 1 && inc tx_fillcount
111  *
112  * check tx_fillcount again.
113  * case 2: set IFF_OACTIVE to stop arc_output from filling us.
114  * case 1: start tx
115  *
116  * tint clears IFF_OCATIVE, decrements and checks tx_fillcount
117  * case 1: start tx on tx_act ^ 1, softcall bah_start
118  * case 0: softcall bah_start
119  *
120  * #define fill(i) get mbuf && copy mbuf to chip(i)
121  */
122 
123 void	bah_init(struct bah_softc *);
124 void	bah_reset(struct bah_softc *);
125 void	bah_stop(struct bah_softc *);
126 void	bah_start(struct ifnet *);
127 int	bahintr(void *);
128 int	bah_ioctl(struct ifnet *, unsigned long, void *);
129 void	bah_watchdog(struct ifnet *);
130 void	bah_srint(void *vsc);
131 static	void bah_tint(struct bah_softc *, int);
132 void	bah_reconwatch(void *);
133 
134 /* short notation */
135 
136 #define GETREG(off)	bus_space_read_1(bst_r, regs, (off))
137 #define PUTREG(off, v)	bus_space_write_1(bst_r, regs, (off), (v))
138 #define GETMEM(off)	bus_space_read_1(bst_m, mem, (off))
139 #define PUTMEM(off, v)	bus_space_write_1(bst_m, mem, (off), (v))
140 
141 int
142 bah_attach_subr(struct bah_softc *sc)
143 {
144 	struct ifnet *ifp = &sc->sc_arccom.ac_if;
145 	int s, rv;
146 	u_int8_t linkaddress;
147 
148 	bus_space_tag_t bst_r = sc->sc_bst_r;
149 	bus_space_tag_t bst_m = sc->sc_bst_m;
150 	bus_space_handle_t regs = sc->sc_regs;
151 	bus_space_handle_t mem = sc->sc_mem;
152 
153 	s = splhigh();
154 
155 	/*
156 	 * read the arcnet address from the board
157 	 */
158 
159 	(*sc->sc_reset)(sc, 1);
160 
161 	do {
162 		delay(200);
163 	} while (!(GETREG(BAHSTAT) & BAH_POR));
164 
165 	linkaddress = GETMEM(BAHMACOFF);
166 
167 	printf(": link addr 0x%02x(%d)\n", linkaddress, linkaddress);
168 
169 	/* clear the int mask... */
170 
171 	sc->sc_intmask = 0;
172 	PUTREG(BAHSTAT, 0);
173 
174 	PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
175 	PUTREG(BAHCMD, BAH_CLR(CLR_POR|CLR_RECONFIG));
176 	sc->sc_recontime = sc->sc_reconcount = 0;
177 
178 	/* and reenable kernel int level */
179 	splx(s);
180 
181 	/*
182 	 * set interface to stopped condition (reset)
183 	 */
184 	bah_stop(sc);
185 
186 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
187 	ifp->if_softc = sc;
188 	ifp->if_start = bah_start;
189 	ifp->if_ioctl = bah_ioctl;
190 	ifp->if_timer = 0;
191 	ifp->if_watchdog  = bah_watchdog;
192 	IFQ_SET_READY(&ifp->if_snd);
193 
194 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
195 
196 	ifp->if_mtu = ARCMTU;
197 
198 	rv = arc_ifattach(ifp, linkaddress);
199 	if (rv != 0)
200 		return rv;
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 	return 0;
211 }
212 
213 /*
214  * Initialize device
215  *
216  */
217 void
218 bah_init(struct bah_softc *sc)
219 {
220 	struct ifnet *ifp;
221 	int s;
222 
223 	ifp = &sc->sc_arccom.ac_if;
224 
225 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
226 		s = splnet();
227 		ifp->if_flags |= IFF_RUNNING;
228 		bah_reset(sc);
229 		bah_start(ifp);
230 		splx(s);
231 	}
232 }
233 
234 /*
235  * Reset the interface...
236  *
237  * this assumes that it is called inside a critical section...
238  *
239  */
240 void
241 bah_reset(struct bah_softc *sc)
242 {
243 	struct ifnet *ifp;
244 	uint8_t linkaddress;
245 
246 	bus_space_tag_t bst_r = sc->sc_bst_r;
247         bus_space_tag_t bst_m = sc->sc_bst_m;
248 	bus_space_handle_t regs = sc->sc_regs;
249 	bus_space_handle_t mem = sc->sc_mem;
250 
251 	ifp = &sc->sc_arccom.ac_if;
252 
253 #ifdef BAH_DEBUG
254 	printf("%s: reset\n", device_xname(sc->sc_dev));
255 #endif
256 	/* stop and restart hardware */
257 
258 	(*sc->sc_reset)(sc, 1);
259 	do {
260 		DELAY(200);
261 	} while (!(GETREG(BAHSTAT) & BAH_POR));
262 
263 	linkaddress = GETMEM(BAHMACOFF);
264 
265 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
266 	printf("%s: reset: card reset, link addr = 0x%02x (%u)\n",
267 	    device_xname(sc->sc_dev), linkaddress, linkaddress);
268 #endif
269 
270 	/* tell the routing level about the (possibly changed) link address */
271 	if_set_sadl(ifp, &linkaddress, sizeof(linkaddress), false);
272 
273 	/* POR is NMI, but we need it below: */
274 	sc->sc_intmask = BAH_RECON|BAH_POR;
275 	PUTREG(BAHSTAT, sc->sc_intmask);
276 	PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
277 
278 #ifdef BAH_DEBUG
279 	printf("%s: reset: chip configured, status=0x%02x\n",
280 	    device_xname(sc->sc_dev), GETREG(BAHSTAT));
281 #endif
282 	PUTREG(BAHCMD, BAH_CLR(CLR_POR|CLR_RECONFIG));
283 
284 #ifdef BAH_DEBUG
285 	printf("%s: reset: bits cleared, status=0x%02x\n",
286 	    device_xname(sc->sc_dev), GETREG(BAHSTAT));
287 #endif
288 
289 	sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
290 
291 	/* start receiver */
292 
293 	sc->sc_intmask  |= BAH_RI;
294 	sc->sc_rx_fillcount = 0;
295 	sc->sc_rx_act = 2;
296 
297 	PUTREG(BAHCMD, BAH_RXBC(2));
298 	PUTREG(BAHSTAT, sc->sc_intmask);
299 
300 #ifdef BAH_DEBUG
301 	printf("%s: reset: started receiver, status=0x%02x\n",
302 	    device_xname(sc->sc_dev), GETREG(BAHSTAT));
303 #endif
304 
305 	/* and init transmitter status */
306 	sc->sc_tx_act = 0;
307 	sc->sc_tx_fillcount = 0;
308 
309 	ifp->if_flags |= IFF_RUNNING;
310 	ifp->if_flags &= ~IFF_OACTIVE;
311 
312 	bah_start(ifp);
313 }
314 
315 /*
316  * Take interface offline
317  */
318 void
319 bah_stop(struct bah_softc *sc)
320 {
321 	bus_space_tag_t bst_r = sc->sc_bst_r;
322 	bus_space_handle_t regs = sc->sc_regs;
323 
324 	/* Stop the interrupts */
325 	PUTREG(BAHSTAT, 0);
326 
327 	/* Stop the interface */
328 	(*sc->sc_reset)(sc, 0);
329 
330 	/* Stop watchdog timer */
331 	sc->sc_arccom.ac_if.if_timer = 0;
332 }
333 
334 /*
335  * Start output on interface. Get another datagram to send
336  * off the interface queue, and copy it to the
337  * interface before starting the output
338  *
339  * this assumes that it is called inside a critical section...
340  * XXX hm... does it still?
341  *
342  */
343 void
344 bah_start(struct ifnet *ifp)
345 {
346 	struct bah_softc *sc = ifp->if_softc;
347 	struct mbuf *m,*mp;
348 
349 	bus_space_tag_t bst_r = sc->sc_bst_r;
350 	bus_space_handle_t regs = sc->sc_regs;
351 	bus_space_tag_t bst_m = sc->sc_bst_m;
352 	bus_space_handle_t mem = sc->sc_mem;
353 
354 	int bah_ram_ptr;
355 	int len, tlen, offset, s, buffer;
356 #ifdef BAHTIMINGS
357 	u_long copystart, lencopy, perbyte;
358 #endif
359 
360 #if defined(BAH_DEBUG) && (BAH_DEBUG > 3)
361 	printf("%s: start(0x%x)\n", device_xname(sc->sc_dev), ifp);
362 #endif
363 
364 	if ((ifp->if_flags & IFF_RUNNING) == 0)
365 		return;
366 
367 	s = splnet();
368 
369 	if (sc->sc_tx_fillcount >= 2) {
370 		splx(s);
371 		return;
372 	}
373 
374 	IFQ_DEQUEUE(&ifp->if_snd, m);
375 	buffer = sc->sc_tx_act ^ 1;
376 
377 	splx(s);
378 
379 	if (m == 0)
380 		return;
381 
382 	/*
383 	 * If bpf is listening on this interface, let it
384 	 * see the packet before we commit it to the wire
385 	 *
386 	 * (can't give the copy in A2060 card RAM to bpf, because
387 	 * that RAM is just accessed as on every other byte)
388 	 */
389 	bpf_mtap(ifp, m, BPF_D_OUT);
390 
391 #ifdef BAH_DEBUG
392 	if (m->m_len < ARC_HDRLEN)
393 		m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
394 	printf("%s: start: filling %d from %u to %u type %u\n",
395 	    device_xname(sc->sc_dev), buffer, mtod(m, u_char *)[0],
396 	    mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
397 #else
398 	if (m->m_len < 2)
399 		m = m_pullup(m, 2);
400 #endif
401 	bah_ram_ptr = buffer*512;
402 
403 	if (m == 0)
404 		return;
405 
406 	/* write the addresses to RAM and throw them away */
407 
408 	/*
409 	 * Hardware does this: Yet Another Microsecond Saved.
410 	 * (btw, timing code says usually 2 microseconds)
411 	 * PUTMEM(bah_ram_ptr + 0, mtod(m, u_char *)[0]);
412 	 */
413 
414 	PUTMEM(bah_ram_ptr + 1, mtod(m, u_char *)[1]);
415 	m_adj(m, 2);
416 
417 	/* get total length left at this point */
418 	tlen = m->m_pkthdr.len;
419 	if (tlen < ARC_MIN_FORBID_LEN) {
420 		offset = 256 - tlen;
421 		PUTMEM(bah_ram_ptr + 2, offset);
422 	} else {
423 		PUTMEM(bah_ram_ptr + 2, 0);
424 		if (tlen <= ARC_MAX_FORBID_LEN)
425 			offset = 255;		/* !!! */
426 		else {
427 			if (tlen > ARC_MAX_LEN)
428 				tlen = ARC_MAX_LEN;
429 			offset = 512 - tlen;
430 		}
431 		PUTMEM(bah_ram_ptr + 3, offset);
432 
433 	}
434 	bah_ram_ptr += offset;
435 
436 	/* lets loop through the mbuf chain */
437 
438 	for (mp = m; mp; mp = mp->m_next) {
439 		if ((len = mp->m_len)) {		/* YAMS */
440 			bus_space_write_region_1(bst_m, mem, bah_ram_ptr,
441 			    mtod(mp, void *), len);
442 
443 			bah_ram_ptr += len;
444 		}
445 	}
446 
447 	sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
448 	sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
449 
450 	/* actually transmit the packet */
451 	s = splnet();
452 
453 	if (++sc->sc_tx_fillcount > 1) {
454 		/*
455 		 * We are filled up to the rim. No more bufs for the moment,
456 		 * please.
457 		 */
458 		ifp->if_flags |= IFF_OACTIVE;
459 	} else {
460 #ifdef BAH_DEBUG
461 		printf("%s: start: starting transmitter on buffer %d\n",
462 		    device_xname(sc->sc_dev), buffer);
463 #endif
464 		/* Transmitter was off, start it */
465 		sc->sc_tx_act = buffer;
466 
467 		/*
468 		 * We still can accept another buf, so don't:
469 		 * ifp->if_flags |= IFF_OACTIVE;
470 		 */
471 		sc->sc_intmask |= BAH_TA;
472 		PUTREG(BAHCMD, BAH_TX(buffer));
473 		PUTREG(BAHSTAT, sc->sc_intmask);
474 
475 		sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
476 	}
477 	splx(s);
478 	m_freem(m);
479 
480 	/*
481 	 * After 10 times reading the docs, I realized
482 	 * that in the case the receiver NAKs the buffer request,
483 	 * the hardware retries till shutdown.
484 	 * This is integrated now in the code above.
485 	 */
486 
487 	return;
488 }
489 
490 /*
491  * Arcnet interface receiver soft interrupt:
492  * get the stuff out of any filled buffer we find.
493  */
494 void
495 bah_srint(void *vsc)
496 {
497 	struct bah_softc *sc = (struct bah_softc *)vsc;
498 	int buffer, len, len1, amount, offset, s, type;
499 	int bah_ram_ptr;
500 	struct mbuf *m, *dst, *head;
501 	struct arc_header *ah;
502 	struct ifnet *ifp;
503 
504 	bus_space_tag_t bst_r = sc->sc_bst_r;
505         bus_space_tag_t bst_m = sc->sc_bst_m;
506 	bus_space_handle_t regs = sc->sc_regs;
507 	bus_space_handle_t mem = sc->sc_mem;
508 
509 	ifp = &sc->sc_arccom.ac_if;
510 	head = 0;
511 
512 	s = splnet();
513 	buffer = sc->sc_rx_act ^ 1;
514 	splx(s);
515 
516 	/* Allocate header mbuf */
517 	MGETHDR(m, M_DONTWAIT, MT_DATA);
518 
519 	if (m == 0) {
520 		/*
521 	 	 * in case s.th. goes wrong with mem, drop it
522 	 	 * to make sure the receiver can be started again
523 		 * count it as input error (we dont have any other
524 		 * detectable)
525 	 	 */
526 		if_statinc(ifp, if_ierrors);
527 		goto cleanup;
528 	}
529 
530 	m_set_rcvif(m, ifp);
531 
532 	/*
533 	 * Align so that IP packet will be longword aligned. Here we
534 	 * assume that m_data of new packet is longword aligned.
535 	 * When implementing PHDS, we might have to change it to 2,
536 	 * (2*sizeof(ulong) - ARC_HDRNEWLEN)), packet type dependent.
537 	 */
538 
539 	bah_ram_ptr = buffer*512;
540 	offset = GETMEM(bah_ram_ptr + 2);
541 	if (offset)
542 		len = 256 - offset;
543 	else {
544 		offset = GETMEM(bah_ram_ptr + 3);
545 		len = 512 - offset;
546 	}
547 	if (len+2 >= MINCLSIZE)
548 		MCLGET(m, M_DONTWAIT);
549 
550 	if (m == 0) {
551 		if_statinc(ifp, if_ierrors);
552 		goto cleanup;
553 	}
554 
555 	type = GETMEM(bah_ram_ptr + offset);
556 	m->m_data += 1 + arc_isphds(type);
557 
558 	head = m;
559 	ah = mtod(head, struct arc_header *);
560 
561 	ah->arc_shost = GETMEM(bah_ram_ptr + 0);
562 	ah->arc_dhost = GETMEM(bah_ram_ptr + 1);
563 
564 	m->m_pkthdr.len = len+2; /* whole packet length */
565 	m->m_len = 2;		 /* mbuf filled with ARCnet addresses */
566 	bah_ram_ptr += offset;	/* ram buffer continues there */
567 
568 	while (len > 0) {
569 
570 		len1 = len;
571 		amount = M_TRAILINGSPACE(m);
572 
573 		if (amount == 0) {
574 			dst = m;
575 			MGET(m, M_DONTWAIT, MT_DATA);
576 
577 			if (m == 0) {
578 				if_statinc(ifp, if_ierrors);
579 				goto cleanup;
580 			}
581 
582 			if (len1 >= MINCLSIZE)
583 				MCLGET(m, M_DONTWAIT);
584 
585 			m->m_len = 0;
586 			dst->m_next = m;
587 			amount = M_TRAILINGSPACE(m);
588 		}
589 
590 		if (amount < len1)
591 			len1 = amount;
592 
593 		bus_space_read_region_1(bst_m, mem, bah_ram_ptr,
594 		    mtod(m, u_char *) + m->m_len, len1);
595 
596 		m->m_len += len1;
597 		bah_ram_ptr += len1;
598 		len -= len1;
599 	}
600 
601 	if_percpuq_enqueue((&sc->sc_arccom.ac_if)->if_percpuq, head);
602 
603 	head = NULL;
604 
605 cleanup:
606 
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 		if_statinc(ifp, 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 		if_statinc(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 	if_schedule_deferred_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 			if_statinc(&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 a 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