xref: /netbsd-src/sys/dev/ic/smc90cx6.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /*	$NetBSD: smc90cx6.c,v 1.67 2016/06/10 13:27: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.67 2016/06/10 13:27: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 
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 	bpf_mtap(ifp, head);
600 
601 	if_percpuq_enqueue((&sc->sc_arccom.ac_if)->if_percpuq, head);
602 
603 	head = NULL;
604 	ifp->if_ipackets++;
605 
606 cleanup:
607 
608 	if (head != NULL)
609 		m_freem(head);
610 
611 	/* mark buffer as invalid by source id 0 */
612 	bus_space_write_1(bst_m, mem, buffer*512, 0);
613 	s = splnet();
614 
615 	if (--sc->sc_rx_fillcount == 2 - 1) {
616 
617 		/* was off, restart it on buffer just emptied */
618 		sc->sc_rx_act = buffer;
619 		sc->sc_intmask |= BAH_RI;
620 
621 		/* this also clears the RI flag interrupt: */
622 		PUTREG(BAHCMD, BAH_RXBC(buffer));
623 		PUTREG(BAHSTAT, sc->sc_intmask);
624 
625 #ifdef BAH_DEBUG
626 		printf("%s: srint: restarted rx on buf %d\n",
627 		    device_xname(sc->sc_dev), buffer);
628 #endif
629 	}
630 	splx(s);
631 }
632 
633 inline static void
634 bah_tint(struct bah_softc *sc, int isr)
635 {
636 	struct ifnet *ifp;
637 
638 	bus_space_tag_t bst_r = sc->sc_bst_r;
639 	bus_space_handle_t regs = sc->sc_regs;
640 
641 
642 	int buffer;
643 #ifdef BAHTIMINGS
644 	int clknow;
645 #endif
646 
647 	ifp = &(sc->sc_arccom.ac_if);
648 	buffer = sc->sc_tx_act;
649 
650 	/*
651 	 * retransmit code:
652 	 * Normal situations first for fast path:
653 	 * If acknowledgement received ok or broadcast, we're ok.
654 	 * else if
655 	 */
656 
657 	if (isr & BAH_TMA || sc->sc_broadcast[buffer])
658 		sc->sc_arccom.ac_if.if_opackets++;
659 #ifdef BAHRETRANSMIT
660 	else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
661 	    && --sc->sc_retransmits[buffer] > 0) {
662 		/* retransmit same buffer */
663 		PUTREG(BAHCMD, BAH_TX(buffer));
664 		return;
665 	}
666 #endif
667 	else
668 		ifp->if_oerrors++;
669 
670 
671 	/* We know we can accept another buffer at this point. */
672 	ifp->if_flags &= ~IFF_OACTIVE;
673 
674 	if (--sc->sc_tx_fillcount > 0) {
675 
676 		/*
677 		 * start tx on other buffer.
678 		 * This also clears the int flag
679 		 */
680 		buffer ^= 1;
681 		sc->sc_tx_act = buffer;
682 
683 		/*
684 		 * already given:
685 		 * sc->sc_intmask |= BAH_TA;
686 		 * PUTREG(BAHSTAT, sc->sc_intmask);
687 		 */
688 		PUTREG(BAHCMD, BAH_TX(buffer));
689 		/* init watchdog timer */
690 		ifp->if_timer = ARCTIMEOUT;
691 
692 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
693 		printf("%s: tint: starting tx on buffer %d, status 0x%02x\n",
694 		    device_xname(sc->sc_dev), buffer, GETREG(BAHSTAT));
695 #endif
696 	} else {
697 		/* have to disable TX interrupt */
698 		sc->sc_intmask &= ~BAH_TA;
699 		PUTREG(BAHSTAT, sc->sc_intmask);
700 		/* ... and watchdog timer */
701 		ifp->if_timer = 0;
702 
703 #ifdef BAH_DEBUG
704 		printf("%s: tint: no more buffers to send, status 0x%02x\n",
705 		    device_xname(sc->sc_dev), GETREG(BAHSTAT));
706 #endif
707 	}
708 
709 	/* XXXX TODO */
710 #ifdef BAHSOFTCOPY
711 	/* schedule soft int to fill a new buffer for us */
712 	softint_schedule(sc->sc_txcookie);
713 #else
714 	/* call it directly */
715 	bah_start(ifp);
716 #endif
717 }
718 
719 /*
720  * Our interrupt routine
721  */
722 int
723 bahintr(void *arg)
724 {
725 	struct bah_softc *sc = arg;
726 
727 	bus_space_tag_t bst_r = sc->sc_bst_r;
728         bus_space_tag_t bst_m = sc->sc_bst_m;
729 	bus_space_handle_t regs = sc->sc_regs;
730 	bus_space_handle_t mem = sc->sc_mem;
731 
732 	u_char isr, maskedisr;
733 	int buffer;
734 	u_long newsec;
735 
736 	isr = GETREG(BAHSTAT);
737 	maskedisr = isr & sc->sc_intmask;
738 	if (!maskedisr)
739 		return (0);
740 	do {
741 
742 #if defined(BAH_DEBUG) && (BAH_DEBUG>1)
743 		printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
744 		    device_xname(sc->sc_dev), isr, sc->sc_intmask);
745 #endif
746 
747 		if (maskedisr & BAH_POR) {
748 		  	/*
749 			 * XXX We should never see this. Don't bother to store
750 			 * the address.
751 			 * sc->sc_arccom.ac_anaddr = GETMEM(BAHMACOFF);
752 			 */
753 			PUTREG(BAHCMD, BAH_CLR(CLR_POR));
754 			log(LOG_WARNING,
755 			    "%s: intr: got spurious power on reset int\n",
756 			    device_xname(sc->sc_dev));
757 		}
758 
759 		if (maskedisr & BAH_RECON) {
760 			/*
761 			 * we dont need to:
762 			 * PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
763 			 */
764 			PUTREG(BAHCMD, BAH_CLR(CLR_RECONFIG));
765 			sc->sc_arccom.ac_if.if_collisions++;
766 
767 			/*
768 			 * If less than 2 seconds per reconfig:
769 			 *	If ARC_EXCESSIVE_RECONFIGS
770 			 *	since last burst, complain and set threshold for
771 			 *	warnings to ARC_EXCESSIVE_RECONS_REWARN.
772 			 *
773 			 * This allows for, e.g., new stations on the cable, or
774 			 * cable switching as long as it is over after
775 			 * (normally) 16 seconds.
776 			 *
777 			 * XXX TODO: check timeout bits in status word and
778 			 * double time if necessary.
779 			 */
780 
781 			callout_stop(&sc->sc_recon_ch);
782 			newsec = time_second;
783 			if ((newsec - sc->sc_recontime <= 2) &&
784 			    (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
785 				log(LOG_WARNING,
786 				    "%s: excessive token losses, "
787 				    "cable problem?\n", device_xname(sc->sc_dev));
788 			}
789 			sc->sc_recontime = newsec;
790 			callout_reset(&sc->sc_recon_ch, 15 * hz,
791 			    bah_reconwatch, (void *)sc);
792 		}
793 
794 		if (maskedisr & BAH_RI) {
795 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
796 			printf("%s: intr: hard rint, act %d\n",
797 			    device_xname(sc->sc_dev), sc->sc_rx_act);
798 #endif
799 
800 			buffer = sc->sc_rx_act;
801 			/* look if buffer is marked invalid: */
802 			if (GETMEM(buffer*512) == 0) {
803 				/*
804 				 * invalid marked buffer (or illegally
805 				 * configured sender)
806 				 */
807 				log(LOG_WARNING,
808 				    "%s: spurious RX interrupt or sender 0 "
809 				    " (ignored)\n", device_xname(sc->sc_dev));
810 				/*
811 				 * restart receiver on same buffer.
812 				 * XXX maybe better reset interface?
813 				 */
814 				PUTREG(BAHCMD, BAH_RXBC(buffer));
815 			} else {
816 				if (++sc->sc_rx_fillcount > 1) {
817 					sc->sc_intmask &= ~BAH_RI;
818 					PUTREG(BAHSTAT, sc->sc_intmask);
819 				} else {
820 					buffer ^= 1;
821 					sc->sc_rx_act = buffer;
822 
823 					/*
824 					 * Start receiver on other receive
825 					 * buffer. This also clears the RI
826 					 * interrupt flag.
827 					 */
828 					PUTREG(BAHCMD, BAH_RXBC(buffer));
829 					/* in RX intr, so mask is ok for RX */
830 
831 #ifdef BAH_DEBUG
832 					printf("%s: strt rx for buf %u, "
833 					    "stat 0x%02x\n",
834 					    device_xname(sc->sc_dev), sc->sc_rx_act,
835 					    GETREG(BAHSTAT));
836 #endif
837 				}
838 
839 #ifdef BAHSOFTCOPY
840 				/*
841 				 * this one starts a soft int to copy out
842 				 * of the hw
843 				 */
844 				softint_schedule(sc->sc_rxcookie);
845 #else
846 				/* this one does the copy here */
847 				bah_srint(sc);
848 #endif
849 			}
850 		}
851 		if (maskedisr & BAH_TA) {
852 			bah_tint(sc, isr);
853 		}
854 		isr = GETREG(BAHSTAT);
855 		maskedisr = isr & sc->sc_intmask;
856 	} while (maskedisr);
857 
858 	return (1);
859 }
860 
861 void
862 bah_reconwatch(void *arg)
863 {
864 	struct bah_softc *sc = arg;
865 
866 	if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
867 		sc->sc_reconcount = 0;
868 		log(LOG_WARNING, "%s: token valid again.\n",
869 		    device_xname(sc->sc_dev));
870 	}
871 	sc->sc_reconcount = 0;
872 }
873 
874 
875 /*
876  * Process an ioctl request.
877  * This code needs some work - it looks pretty ugly.
878  */
879 int
880 bah_ioctl(struct ifnet *ifp, u_long cmd, void *data)
881 {
882 	struct bah_softc *sc;
883 	struct ifaddr *ifa;
884 	struct ifreq *ifr;
885 	int s, error;
886 
887 	error = 0;
888 	sc = ifp->if_softc;
889 	ifa = (struct ifaddr *)data;
890 	ifr = (struct ifreq *)data;
891 	s = splnet();
892 
893 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
894 	printf("%s: ioctl() called, cmd = 0x%lx\n",
895 	    device_xname(sc->sc_dev), cmd);
896 #endif
897 
898 	switch (cmd) {
899 	case SIOCINITIFADDR:
900 		ifp->if_flags |= IFF_UP;
901 		bah_init(sc);
902 		switch (ifa->ifa_addr->sa_family) {
903 #ifdef INET
904 		case AF_INET:
905 			arp_ifinit(ifp, ifa);
906 			break;
907 #endif
908 		default:
909 			break;
910 		}
911 
912 	case SIOCSIFFLAGS:
913 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
914 			break;
915 		/* XXX re-use ether_ioctl() */
916 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
917 		case IFF_RUNNING:
918 			/*
919 			 * If interface is marked down and it is running,
920 			 * then stop it.
921 			 */
922 			bah_stop(sc);
923 			ifp->if_flags &= ~IFF_RUNNING;
924 			break;
925 		case IFF_UP:
926 			/*
927 			 * If interface is marked up and it is stopped, then
928 			 * start it.
929 			 */
930 			bah_init(sc);
931 			break;
932 		}
933 		break;
934 
935 	case SIOCADDMULTI:
936 	case SIOCDELMULTI:
937 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
938 		case AF_INET:
939 		case AF_INET6:
940 			error = 0;
941 			break;
942 		default:
943 			error = EAFNOSUPPORT;
944 			break;
945 		}
946 		break;
947 
948 	default:
949 		error = ether_ioctl(ifp, cmd, data);
950 	}
951 
952 	splx(s);
953 	return (error);
954 }
955 
956 /*
957  * watchdog routine for transmitter.
958  *
959  * We need this, because else a receiver whose hardware is alive, but whose
960  * software has not enabled the Receiver, would make our hardware wait forever
961  * Discovered this after 20 times reading the docs.
962  *
963  * Only thing we do is disable transmitter. We'll get an transmit timeout,
964  * and the int handler will have to decide not to retransmit (in case
965  * retransmission is implemented).
966  *
967  * This one assumes being called inside splnet()
968  */
969 
970 void
971 bah_watchdog(struct ifnet *ifp)
972 {
973 	struct bah_softc *sc = ifp->if_softc;
974 
975 	bus_space_tag_t bst_r = sc->sc_bst_r;
976 	bus_space_handle_t regs = sc->sc_regs;
977 
978 	PUTREG(BAHCMD, BAH_TXDIS);
979 	return;
980 }
981 
982