smc90cx6.c revision 121752
189099Sfjoe/*	$NetBSD: smc90cx6.c,v 1.38 2001/07/07 15:57:53 thorpej Exp $ */
289099Sfjoe
3119418Sobrien#include <sys/cdefs.h>
4119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/cm/smc90cx6.c 121752 2003-10-30 19:45:58Z brooks $");
5119418Sobrien
689099Sfjoe/*-
789099Sfjoe * Copyright (c) 1994, 1995, 1998 The NetBSD Foundation, Inc.
889099Sfjoe * All rights reserved.
989099Sfjoe *
1089099Sfjoe * This code is derived from software contributed to The NetBSD Foundation
1189099Sfjoe * by Ignatios Souvatzis.
1289099Sfjoe *
1389099Sfjoe * Redistribution and use in source and binary forms, with or without
1489099Sfjoe * modification, are permitted provided that the following conditions
1589099Sfjoe * are met:
1689099Sfjoe * 1. Redistributions of source code must retain the above copyright
1789099Sfjoe *    notice, this list of conditions and the following disclaimer.
1889099Sfjoe * 2. Redistributions in binary form must reproduce the above copyright
1989099Sfjoe *    notice, this list of conditions and the following disclaimer in the
2089099Sfjoe *    documentation and/or other materials provided with the distribution.
2189099Sfjoe * 3. All advertising materials mentioning features or use of this software
2289099Sfjoe *    must display the following acknowledgement:
2389099Sfjoe *        This product includes software developed by the NetBSD
2489099Sfjoe *        Foundation, Inc. and its contributors.
2589099Sfjoe * 4. Neither the name of The NetBSD Foundation nor the names of its
2689099Sfjoe *    contributors may be used to endorse or promote products derived
2789099Sfjoe *    from this software without specific prior written permission.
2889099Sfjoe *
2989099Sfjoe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
3089099Sfjoe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
3189099Sfjoe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3289099Sfjoe * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3389099Sfjoe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3489099Sfjoe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3589099Sfjoe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3689099Sfjoe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3789099Sfjoe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3889099Sfjoe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3989099Sfjoe * POSSIBILITY OF SUCH DAMAGE.
4089099Sfjoe */
4189099Sfjoe
4289099Sfjoe/*
4389099Sfjoe * Chip core driver for the SMC90c26 / SMC90c56 (and SMC90c66 in '56
4489099Sfjoe * compatibility mode) boards
4589099Sfjoe */
4689099Sfjoe
4789099Sfjoe/* #define CMSOFTCOPY */
4889099Sfjoe#define CMRETRANSMIT /**/
49109771Sfjoe/* #define CM_DEBUG */
5089099Sfjoe
5189099Sfjoe#include <sys/param.h>
5289099Sfjoe#include <sys/systm.h>
5389099Sfjoe#include <sys/sockio.h>
5489099Sfjoe#include <sys/mbuf.h>
5589099Sfjoe#include <sys/module.h>
5689099Sfjoe#include <sys/kernel.h>
5789099Sfjoe#include <sys/socket.h>
5889099Sfjoe#include <sys/syslog.h>
5989099Sfjoe#include <sys/bus.h>
6089099Sfjoe
6189099Sfjoe#include <machine/bus.h>
6289099Sfjoe#include <sys/rman.h>
6389099Sfjoe#include <machine/resource.h>
6489099Sfjoe
6589099Sfjoe#if __FreeBSD_version < 500000
6689099Sfjoe#include <machine/clock.h>
6789099Sfjoe#endif
6889099Sfjoe
6989099Sfjoe#include <net/if.h>
7089099Sfjoe#include <net/if_dl.h>
7189099Sfjoe#include <net/if_types.h>
7289099Sfjoe#include <net/if_arc.h>
7389099Sfjoe
7489099Sfjoe#include <dev/cm/smc90cx6reg.h>
7589099Sfjoe#include <dev/cm/smc90cx6var.h>
7689099Sfjoe
7789099SfjoeMODULE_DEPEND(if_cm, arcnet, 1, 1, 1);
7889099Sfjoe
7989099Sfjoe/* these should be elsewhere */
8089099Sfjoe
8189099Sfjoe#define ARC_MIN_LEN 1
8289099Sfjoe#define ARC_MIN_FORBID_LEN 254
8389099Sfjoe#define ARC_MAX_FORBID_LEN 256
8489099Sfjoe#define ARC_MAX_LEN 508
8589099Sfjoe#define ARC_ADDR_LEN 1
8689099Sfjoe
8789099Sfjoe/* for watchdog timer. This should be more than enough. */
8889099Sfjoe#define ARCTIMEOUT (5*IFNET_SLOWHZ)
8989099Sfjoe
9089099Sfjoe/* short notation */
9189099Sfjoe
9289099Sfjoe#define GETREG(off)							\
9389099Sfjoe	bus_space_read_1(rman_get_bustag((sc)->port_res),		\
9489099Sfjoe			 rman_get_bushandle((sc)->port_res),		\
9589099Sfjoe			 (off))
9689099Sfjoe#define PUTREG(off, value)						\
9789099Sfjoe	bus_space_write_1(rman_get_bustag((sc)->port_res),		\
9889099Sfjoe			  rman_get_bushandle((sc)->port_res),		\
9989099Sfjoe			  (off), (value))
10089099Sfjoe#define GETMEM(off)							\
10189099Sfjoe	bus_space_read_1(rman_get_bustag((sc)->mem_res),		\
10289099Sfjoe			 rman_get_bushandle((sc)->mem_res),		\
10389099Sfjoe			 (off))
10489099Sfjoe#define PUTMEM(off, value)						\
10589099Sfjoe	bus_space_write_1(rman_get_bustag((sc)->mem_res),		\
10689099Sfjoe			  rman_get_bushandle((sc)->mem_res),		\
10789099Sfjoe			  (off), (value))
10889099Sfjoe
10989099Sfjoedevclass_t cm_devclass;
11089099Sfjoe
11189099Sfjoe/*
11289099Sfjoe * This currently uses 2 bufs for tx, 2 for rx
11389099Sfjoe *
11489099Sfjoe * New rx protocol:
11589099Sfjoe *
11689099Sfjoe * rx has a fillcount variable. If fillcount > (NRXBUF-1),
11789099Sfjoe * rx can be switched off from rx hard int.
11889099Sfjoe * Else rx is restarted on the other receiver.
11989099Sfjoe * rx soft int counts down. if it is == (NRXBUF-1), it restarts
12089099Sfjoe * the receiver.
12189099Sfjoe * To ensure packet ordering (we need that for 1201 later), we have a counter
12289099Sfjoe * which is incremented modulo 256 on each receive and a per buffer
12389099Sfjoe * variable, which is set to the counter on filling. The soft int can
12489099Sfjoe * compare both values to determine the older packet.
12589099Sfjoe *
12689099Sfjoe * Transmit direction:
12789099Sfjoe *
12889099Sfjoe * cm_start checks tx_fillcount
12989099Sfjoe * case 2: return
13089099Sfjoe *
13189099Sfjoe * else fill tx_act ^ 1 && inc tx_fillcount
13289099Sfjoe *
13389099Sfjoe * check tx_fillcount again.
13489099Sfjoe * case 2: set IFF_OACTIVE to stop arc_output from filling us.
13589099Sfjoe * case 1: start tx
13689099Sfjoe *
13789099Sfjoe * tint clears IFF_OCATIVE, decrements and checks tx_fillcount
13889099Sfjoe * case 1: start tx on tx_act ^ 1, softcall cm_start
13989099Sfjoe * case 0: softcall cm_start
14089099Sfjoe *
14189099Sfjoe * #define fill(i) get mbuf && copy mbuf to chip(i)
14289099Sfjoe */
14389099Sfjoe
14492739Salfredvoid	cm_init(void *);
14592739Salfredvoid	cm_reset(struct cm_softc *);
14692739Salfredvoid	cm_start(struct ifnet *);
14792739Salfredint	cm_ioctl(struct ifnet *, unsigned long, caddr_t);
14892739Salfredvoid	cm_watchdog(struct ifnet *);
14992739Salfredvoid	cm_srint(void *vsc);
15092739Salfredstatic	void cm_tint(struct cm_softc *, int);
15189099Sfjoevoid	cm_reconwatch(void *);
15289099Sfjoe
15389099Sfjoeint
15489099Sfjoecm_probe(dev)
15589099Sfjoe	device_t dev;
15689099Sfjoe{
15789099Sfjoe	int error;
15889099Sfjoe	struct cm_softc *sc = device_get_softc(dev);
15989099Sfjoe
16089099Sfjoe	error = cm_alloc_port(dev, 0, CM_IO_PORTS);
16189099Sfjoe	if (error)
16289099Sfjoe		return error;
16389099Sfjoe
16489099Sfjoe	if (GETREG(CMSTAT) == 0xff)
16589099Sfjoe		return ENXIO;
16689099Sfjoe
16789099Sfjoe	error = cm_alloc_memory(dev, 0, 0x800);
16889099Sfjoe	if (error)
16989099Sfjoe		return error;
17089099Sfjoe
17189099Sfjoe	return 0;
17289099Sfjoe}
17389099Sfjoe
17489099Sfjoe/*
17589099Sfjoe * Allocate a port resource with the given resource id.
17689099Sfjoe */
17789099Sfjoeint
17889099Sfjoecm_alloc_port(dev, rid, size)
17989099Sfjoe	device_t dev;
18089099Sfjoe	int rid;
18189099Sfjoe	int size;
18289099Sfjoe{
18389099Sfjoe	struct cm_softc *sc = device_get_softc(dev);
18489099Sfjoe	struct resource *res;
18589099Sfjoe
18689099Sfjoe	res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
18789099Sfjoe				 0ul, ~0ul, size, RF_ACTIVE);
18889099Sfjoe	if (res) {
18989099Sfjoe		sc->port_rid = rid;
19089099Sfjoe		sc->port_res = res;
19189099Sfjoe		sc->port_used = size;
19289099Sfjoe		return (0);
19389099Sfjoe	} else {
19489099Sfjoe		return (ENOENT);
19589099Sfjoe	}
19689099Sfjoe}
19789099Sfjoe
19889099Sfjoe/*
19989099Sfjoe * Allocate a memory resource with the given resource id.
20089099Sfjoe */
20189099Sfjoeint
20289099Sfjoecm_alloc_memory(dev, rid, size)
20389099Sfjoe	device_t dev;
20489099Sfjoe	int rid;
20589099Sfjoe	int size;
20689099Sfjoe{
20789099Sfjoe	struct cm_softc *sc = device_get_softc(dev);
20889099Sfjoe	struct resource *res;
20989099Sfjoe
21089099Sfjoe	res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
21189099Sfjoe				 0ul, ~0ul, size, RF_ACTIVE);
21289099Sfjoe	if (res) {
21389099Sfjoe		sc->mem_rid = rid;
21489099Sfjoe		sc->mem_res = res;
21589099Sfjoe		sc->mem_used = size;
21689099Sfjoe		return (0);
21789099Sfjoe	} else {
21889099Sfjoe		return (ENOENT);
21989099Sfjoe	}
22089099Sfjoe}
22189099Sfjoe
22289099Sfjoe/*
22389099Sfjoe * Allocate an irq resource with the given resource id.
22489099Sfjoe */
22589099Sfjoeint
22689099Sfjoecm_alloc_irq(dev, rid)
22789099Sfjoe	device_t dev;
22889099Sfjoe	int rid;
22989099Sfjoe{
23089099Sfjoe	struct cm_softc *sc = device_get_softc(dev);
23189099Sfjoe	struct resource *res;
23289099Sfjoe
23389099Sfjoe	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
23489099Sfjoe				 0ul, ~0ul, 1, RF_ACTIVE);
23589099Sfjoe	if (res) {
23689099Sfjoe		sc->irq_rid = rid;
23789099Sfjoe		sc->irq_res = res;
23889099Sfjoe		return (0);
23989099Sfjoe	} else {
24089099Sfjoe		return (ENOENT);
24189099Sfjoe	}
24289099Sfjoe}
24389099Sfjoe
24489099Sfjoe/*
24589099Sfjoe * Release all resources
24689099Sfjoe */
24789099Sfjoevoid
24889099Sfjoecm_release_resources(dev)
24989099Sfjoe	device_t dev;
25089099Sfjoe{
25189099Sfjoe	struct cm_softc *sc = device_get_softc(dev);
25289099Sfjoe
25389099Sfjoe	if (sc->port_res) {
25489099Sfjoe		bus_deactivate_resource(dev, SYS_RES_IOPORT,
25589099Sfjoe				     sc->port_rid, sc->port_res);
25689099Sfjoe		bus_release_resource(dev, SYS_RES_IOPORT,
25789099Sfjoe				     sc->port_rid, sc->port_res);
25889099Sfjoe		sc->port_res = 0;
25989099Sfjoe	}
26089099Sfjoe	if (sc->mem_res) {
26189099Sfjoe		bus_deactivate_resource(dev, SYS_RES_MEMORY,
26289099Sfjoe				     sc->mem_rid, sc->mem_res);
26389099Sfjoe		bus_release_resource(dev, SYS_RES_MEMORY,
26489099Sfjoe				     sc->mem_rid, sc->mem_res);
26589099Sfjoe		sc->mem_res = 0;
26689099Sfjoe	}
26789099Sfjoe	if (sc->irq_res) {
26889099Sfjoe		bus_deactivate_resource(dev, SYS_RES_IRQ,
26989099Sfjoe				     sc->irq_rid, sc->irq_res);
27089099Sfjoe		bus_release_resource(dev, SYS_RES_IRQ,
27189099Sfjoe				     sc->irq_rid, sc->irq_res);
27289099Sfjoe		sc->irq_res = 0;
27389099Sfjoe	}
27489099Sfjoe}
27589099Sfjoe
27689099Sfjoeint
27789099Sfjoecm_attach(sc, unit)
27889099Sfjoe	struct cm_softc *sc;
27989099Sfjoe	int unit;
28089099Sfjoe{
28189099Sfjoe	struct ifnet *ifp = &sc->sc_arccom.ac_if;
28289099Sfjoe	int s;
28389099Sfjoe	u_int8_t linkaddress;
28489099Sfjoe
28589099Sfjoe	s = splhigh();
28689099Sfjoe
28789099Sfjoe	/*
28889099Sfjoe	 * read the arcnet address from the board
28989099Sfjoe	 */
29089099Sfjoe
29189099Sfjoe	GETREG(CMRESET);
29289099Sfjoe	do {
29389099Sfjoe		DELAY(200);
29489099Sfjoe	} while (!(GETREG(CMSTAT) & CM_POR));
29589099Sfjoe
29689099Sfjoe	linkaddress = GETMEM(CMMACOFF);
29789099Sfjoe
29889099Sfjoe	/* clear the int mask... */
29989099Sfjoe
30089099Sfjoe	sc->sc_intmask = 0;
30189099Sfjoe	PUTREG(CMSTAT, 0);
30289099Sfjoe
30389099Sfjoe	PUTREG(CMCMD, CM_CONF(CONF_LONG));
30489099Sfjoe	PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
30589099Sfjoe	sc->sc_recontime = sc->sc_reconcount = 0;
30689099Sfjoe
30789099Sfjoe	/* and reenable kernel int level */
30889099Sfjoe	splx(s);
30989099Sfjoe
31089099Sfjoe	/*
31189099Sfjoe	 * set interface to stopped condition (reset)
31289099Sfjoe	 */
31389099Sfjoe	cm_stop(sc);
31489099Sfjoe
315121752Sbrooks	ifp->if_softc = sc;
316121752Sbrooks	ifp->if_unit = unit;
317121752Sbrooks	ifp->if_name = "cm";
318121752Sbrooks	ifp->if_output = arc_output;
319121752Sbrooks	ifp->if_start = cm_start;
320121752Sbrooks	ifp->if_ioctl = cm_ioctl;
321121752Sbrooks	ifp->if_watchdog  = cm_watchdog;
322121752Sbrooks	ifp->if_init = cm_init;
323121752Sbrooks	/* XXX IFQ_SET_READY(&ifp->if_snd); */
324121752Sbrooks	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
325121752Sbrooks	ifp->if_timer = 0;
326121752Sbrooks	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
32789099Sfjoe
328121752Sbrooks	arc_ifattach(ifp, linkaddress);
32989099Sfjoe
33089099Sfjoe#ifdef CMSOFTCOPY
331121752Sbrooks	sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, cm_srint, sc);
332121752Sbrooks	sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
333121752Sbrooks		(void (*)(void *))cm_start, ifp);
33489099Sfjoe#endif
33589099Sfjoe
33689099Sfjoe#if __FreeBSD_version < 500000
337121752Sbrooks	callout_init(&sc->sc_recon_ch);
33889099Sfjoe#else
339121752Sbrooks	callout_init(&sc->sc_recon_ch, 0);
34089099Sfjoe#endif
34189099Sfjoe
342109771Sfjoe	if_printf(ifp, "link addr 0x%02x (%d)\n", linkaddress, linkaddress);
34389099Sfjoe	return 0;
34489099Sfjoe}
34589099Sfjoe
34689099Sfjoe/*
34789099Sfjoe * Initialize device
34889099Sfjoe *
34989099Sfjoe */
35089099Sfjoevoid
35189099Sfjoecm_init(xsc)
35289099Sfjoe	void *xsc;
35389099Sfjoe{
35489099Sfjoe	struct cm_softc *sc = (struct cm_softc *)xsc;
35589099Sfjoe	struct ifnet *ifp;
35689099Sfjoe	int s;
35789099Sfjoe
35889099Sfjoe	ifp = &sc->sc_arccom.ac_if;
35989099Sfjoe
36089099Sfjoe	if ((ifp->if_flags & IFF_RUNNING) == 0) {
36189099Sfjoe		s = splimp();
36289099Sfjoe		ifp->if_flags |= IFF_RUNNING;
36389099Sfjoe		cm_reset(sc);
36489099Sfjoe		cm_start(ifp);
36589099Sfjoe		splx(s);
36689099Sfjoe	}
36789099Sfjoe}
36889099Sfjoe
36989099Sfjoe/*
37089099Sfjoe * Reset the interface...
37189099Sfjoe *
37289099Sfjoe * this assumes that it is called inside a critical section...
37389099Sfjoe *
37489099Sfjoe */
37589099Sfjoevoid
37689099Sfjoecm_reset(sc)
37789099Sfjoe	struct cm_softc *sc;
37889099Sfjoe{
37989099Sfjoe	struct ifnet *ifp;
38089099Sfjoe	int linkaddress;
38189099Sfjoe
38289099Sfjoe	ifp = &sc->sc_arccom.ac_if;
38389099Sfjoe
38489099Sfjoe#ifdef CM_DEBUG
385104251Sbrooks	if_printf(ifp, "reset\n");
38689099Sfjoe#endif
38789099Sfjoe	/* stop and restart hardware */
38889099Sfjoe
38989099Sfjoe	GETREG(CMRESET);
39089099Sfjoe	do {
39189099Sfjoe		DELAY(200);
39289099Sfjoe	} while (!(GETREG(CMSTAT) & CM_POR));
39389099Sfjoe
39489099Sfjoe	linkaddress = GETMEM(CMMACOFF);
39589099Sfjoe
39689099Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 2)
397104251Sbrooks	if_printf(ifp, "reset: card reset, link addr = 0x%02x (%d)\n",
398104251Sbrooks	    linkaddress, linkaddress);
39989099Sfjoe#endif
40089099Sfjoe
40189099Sfjoe	/* tell the routing level about the (possibly changed) link address */
40289099Sfjoe	arc_storelladdr(ifp, linkaddress);
40389099Sfjoe	arc_frag_init(ifp);
40489099Sfjoe
40589099Sfjoe	/* POR is NMI, but we need it below: */
40689099Sfjoe	sc->sc_intmask = CM_RECON|CM_POR;
40789099Sfjoe	PUTREG(CMSTAT, sc->sc_intmask);
40889099Sfjoe	PUTREG(CMCMD, CM_CONF(CONF_LONG));
40989099Sfjoe
41089099Sfjoe#ifdef CM_DEBUG
411104251Sbrooks	if_printf(ifp, "reset: chip configured, status=0x%02x\n",
412104251Sbrooks	    GETREG(CMSTAT));
41389099Sfjoe#endif
41489099Sfjoe	PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
41589099Sfjoe
41689099Sfjoe#ifdef CM_DEBUG
417104251Sbrooks	if_printf(ifp, "reset: bits cleared, status=0x%02x\n",
418104251Sbrooks	     GETREG(CMSTAT));
41989099Sfjoe#endif
42089099Sfjoe
42189099Sfjoe	sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
42289099Sfjoe
42389099Sfjoe	/* start receiver */
42489099Sfjoe
42589099Sfjoe	sc->sc_intmask  |= CM_RI;
42689099Sfjoe	sc->sc_rx_fillcount = 0;
42789099Sfjoe	sc->sc_rx_act = 2;
42889099Sfjoe
42989099Sfjoe	PUTREG(CMCMD, CM_RXBC(2));
43089099Sfjoe	PUTREG(CMSTAT, sc->sc_intmask);
43189099Sfjoe
43289099Sfjoe#ifdef CM_DEBUG
433104251Sbrooks	if_printf(ifp, "reset: started receiver, status=0x%02x\n",
434104251Sbrooks	    GETREG(CMSTAT));
43589099Sfjoe#endif
43689099Sfjoe
43789099Sfjoe	/* and init transmitter status */
43889099Sfjoe	sc->sc_tx_act = 0;
43989099Sfjoe	sc->sc_tx_fillcount = 0;
44089099Sfjoe
44189099Sfjoe	ifp->if_flags |= IFF_RUNNING;
44289099Sfjoe	ifp->if_flags &= ~IFF_OACTIVE;
44389099Sfjoe
44489099Sfjoe	cm_start(ifp);
44589099Sfjoe}
44689099Sfjoe
44789099Sfjoe/*
44889099Sfjoe * Take interface offline
44989099Sfjoe */
45089099Sfjoevoid
45189099Sfjoecm_stop(sc)
45289099Sfjoe	struct cm_softc *sc;
45389099Sfjoe{
45489099Sfjoe	/* Stop the interrupts */
45589099Sfjoe	PUTREG(CMSTAT, 0);
45689099Sfjoe
45789099Sfjoe	/* Stop the interface */
45889099Sfjoe	GETREG(CMRESET);
45989099Sfjoe
46089099Sfjoe	/* Stop watchdog timer */
46189099Sfjoe	sc->sc_arccom.ac_if.if_timer = 0;
46289099Sfjoe}
46389099Sfjoe
46489099Sfjoe/*
46589099Sfjoe * Start output on interface. Get another datagram to send
46689099Sfjoe * off the interface queue, and copy it to the
46789099Sfjoe * interface becore starting the output
46889099Sfjoe *
46989099Sfjoe * this assumes that it is called inside a critical section...
47089099Sfjoe * XXX hm... does it still?
47189099Sfjoe *
47289099Sfjoe */
47389099Sfjoevoid
47489099Sfjoecm_start(ifp)
47589099Sfjoe	struct ifnet *ifp;
47689099Sfjoe{
47789099Sfjoe	struct cm_softc *sc = ifp->if_softc;
47889099Sfjoe	struct mbuf *m,*mp;
47989099Sfjoe
48089099Sfjoe	int cm_ram_ptr;
48189099Sfjoe	int len, tlen, offset, s, buffer;
48289099Sfjoe#ifdef CMTIMINGS
48389099Sfjoe	u_long copystart, lencopy, perbyte;
48489099Sfjoe#endif
48589099Sfjoe
48689099Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 3)
487104251Sbrooks	if_printf(ifp, "start(%p)\n", ifp);
48889099Sfjoe#endif
48989099Sfjoe
49089099Sfjoe	if ((ifp->if_flags & IFF_RUNNING) == 0)
49189099Sfjoe		return;
49289099Sfjoe
49389099Sfjoe	s = splimp();
49489099Sfjoe
49589099Sfjoe	if (sc->sc_tx_fillcount >= 2) {
49689099Sfjoe		splx(s);
49789099Sfjoe		return;
49889099Sfjoe	}
49989099Sfjoe
50089099Sfjoe	m = arc_frag_next(ifp);
50189099Sfjoe	buffer = sc->sc_tx_act ^ 1;
50289099Sfjoe
50389099Sfjoe	splx(s);
50489099Sfjoe
50589099Sfjoe	if (m == 0)
50689099Sfjoe		return;
50789099Sfjoe
50889099Sfjoe#ifdef CM_DEBUG
50989099Sfjoe	if (m->m_len < ARC_HDRLEN)
51089099Sfjoe		m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
511104251Sbrooks	if_printf(ifp, "start: filling %d from %d to %d type %d\n",
512104251Sbrooks	    buffer, mtod(m, u_char *)[0],
51389099Sfjoe	    mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
51489099Sfjoe#else
51589099Sfjoe	if (m->m_len < 2)
51689099Sfjoe		m = m_pullup(m, 2);
51789099Sfjoe#endif
518109771Sfjoe	cm_ram_ptr = buffer * 512;
51989099Sfjoe
52089099Sfjoe	if (m == 0)
52189099Sfjoe		return;
52289099Sfjoe
52389099Sfjoe	/* write the addresses to RAM and throw them away */
52489099Sfjoe
52589099Sfjoe	/*
52689099Sfjoe	 * Hardware does this: Yet Another Microsecond Saved.
52789099Sfjoe	 * (btw, timing code says usually 2 microseconds)
52889099Sfjoe	 * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
52989099Sfjoe	 */
53089099Sfjoe
53189099Sfjoe	PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
53289099Sfjoe	m_adj(m, 2);
53389099Sfjoe
53489099Sfjoe	/* get total length left at this point */
53589099Sfjoe	tlen = m->m_pkthdr.len;
53689099Sfjoe	if (tlen < ARC_MIN_FORBID_LEN) {
53789099Sfjoe		offset = 256 - tlen;
53889099Sfjoe		PUTMEM(cm_ram_ptr + 2, offset);
53989099Sfjoe	} else {
54089099Sfjoe		PUTMEM(cm_ram_ptr + 2, 0);
54189099Sfjoe		if (tlen <= ARC_MAX_FORBID_LEN)
54289099Sfjoe			offset = 255;		/* !!! */
54389099Sfjoe		else {
54489099Sfjoe			if (tlen > ARC_MAX_LEN)
54589099Sfjoe				tlen = ARC_MAX_LEN;
54689099Sfjoe			offset = 512 - tlen;
54789099Sfjoe		}
54889099Sfjoe		PUTMEM(cm_ram_ptr + 3, offset);
54989099Sfjoe
55089099Sfjoe	}
55189099Sfjoe	cm_ram_ptr += offset;
55289099Sfjoe
55389099Sfjoe	/* lets loop through the mbuf chain */
55489099Sfjoe
55589099Sfjoe	for (mp = m; mp; mp = mp->m_next) {
55689099Sfjoe		if ((len = mp->m_len)) {		/* YAMS */
55789099Sfjoe			bus_space_write_region_1(
55889099Sfjoe			    rman_get_bustag(sc->mem_res),
55989099Sfjoe			    rman_get_bushandle(sc->mem_res),
56089099Sfjoe			    cm_ram_ptr, mtod(mp, caddr_t), len);
56189099Sfjoe
56289099Sfjoe			cm_ram_ptr += len;
56389099Sfjoe		}
56489099Sfjoe	}
56589099Sfjoe
56689099Sfjoe	sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
56789099Sfjoe	sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
56889099Sfjoe
56989099Sfjoe	/* actually transmit the packet */
57089099Sfjoe	s = splimp();
57189099Sfjoe
57289099Sfjoe	if (++sc->sc_tx_fillcount > 1) {
57389099Sfjoe		/*
57489099Sfjoe		 * We are filled up to the rim. No more bufs for the moment,
57589099Sfjoe		 * please.
57689099Sfjoe		 */
57789099Sfjoe		ifp->if_flags |= IFF_OACTIVE;
57889099Sfjoe	} else {
57989099Sfjoe#ifdef CM_DEBUG
580104251Sbrooks		if_printf(ifp, "start: starting transmitter on buffer %d\n",
581104251Sbrooks		    buffer);
58289099Sfjoe#endif
58389099Sfjoe		/* Transmitter was off, start it */
58489099Sfjoe		sc->sc_tx_act = buffer;
58589099Sfjoe
58689099Sfjoe		/*
58789099Sfjoe		 * We still can accept another buf, so don't:
58889099Sfjoe		 * ifp->if_flags |= IFF_OACTIVE;
58989099Sfjoe		 */
59089099Sfjoe		sc->sc_intmask |= CM_TA;
59189099Sfjoe		PUTREG(CMCMD, CM_TX(buffer));
59289099Sfjoe		PUTREG(CMSTAT, sc->sc_intmask);
59389099Sfjoe
59489099Sfjoe		sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
59589099Sfjoe	}
59689099Sfjoe	splx(s);
59789099Sfjoe	m_freem(m);
59889099Sfjoe
59989099Sfjoe	/*
60089099Sfjoe	 * After 10 times reading the docs, I realized
60189099Sfjoe	 * that in the case the receiver NAKs the buffer request,
60289099Sfjoe	 * the hardware retries till shutdown.
60389099Sfjoe	 * This is integrated now in the code above.
60489099Sfjoe	 */
60589099Sfjoe
60689099Sfjoe	return;
60789099Sfjoe}
60889099Sfjoe
60989099Sfjoe/*
61089099Sfjoe * Arcnet interface receiver soft interrupt:
61189099Sfjoe * get the stuff out of any filled buffer we find.
61289099Sfjoe */
61389099Sfjoevoid
61489099Sfjoecm_srint(vsc)
61589099Sfjoe	void *vsc;
61689099Sfjoe{
61789099Sfjoe	struct cm_softc *sc = (struct cm_softc *)vsc;
61889099Sfjoe	int buffer, len, offset, s, type;
61989099Sfjoe	int cm_ram_ptr;
62089099Sfjoe	struct mbuf *m;
62189099Sfjoe	struct arc_header *ah;
62289099Sfjoe	struct ifnet *ifp;
62389099Sfjoe
62489099Sfjoe	ifp = &sc->sc_arccom.ac_if;
62589099Sfjoe
62689099Sfjoe	s = splimp();
62789099Sfjoe	buffer = sc->sc_rx_act ^ 1;
62889099Sfjoe	splx(s);
62989099Sfjoe
63089099Sfjoe	/* Allocate header mbuf */
631111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
63289099Sfjoe
63389099Sfjoe	if (m == 0) {
63489099Sfjoe		/*
63589099Sfjoe		 * in case s.th. goes wrong with mem, drop it
63689099Sfjoe		 * to make sure the receiver can be started again
63789099Sfjoe		 * count it as input error (we dont have any other
63889099Sfjoe		 * detectable)
63989099Sfjoe		 */
64089099Sfjoe		ifp->if_ierrors++;
64189099Sfjoe		goto cleanup;
64289099Sfjoe	}
64389099Sfjoe
64489099Sfjoe	m->m_pkthdr.rcvif = ifp;
64589099Sfjoe
64689099Sfjoe	/*
64789099Sfjoe	 * Align so that IP packet will be longword aligned. Here we
64889099Sfjoe	 * assume that m_data of new packet is longword aligned.
64989099Sfjoe	 * When implementing PHDS, we might have to change it to 2,
65089099Sfjoe	 * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
65189099Sfjoe	 */
65289099Sfjoe
653109771Sfjoe	cm_ram_ptr = buffer * 512;
65489099Sfjoe	offset = GETMEM(cm_ram_ptr + 2);
65589099Sfjoe	if (offset)
65689099Sfjoe		len = 256 - offset;
65789099Sfjoe	else {
65889099Sfjoe		offset = GETMEM(cm_ram_ptr + 3);
65989099Sfjoe		len = 512 - offset;
66089099Sfjoe	}
66189099Sfjoe
66289099Sfjoe	/*
66389099Sfjoe	 * first +2 bytes for align fixup below
66489099Sfjoe	 * second +2 bytes are for src/dst addresses
66589099Sfjoe	 */
66689099Sfjoe	if ((len + 2 + 2) > MHLEN) {
66789099Sfjoe		/* attach an mbuf cluster */
668111119Simp		MCLGET(m, M_DONTWAIT);
66989099Sfjoe
67089099Sfjoe		/* Insist on getting a cluster */
67189099Sfjoe		if ((m->m_flags & M_EXT) == 0) {
67289099Sfjoe			ifp->if_ierrors++;
67389099Sfjoe			goto cleanup;
67489099Sfjoe		}
67589099Sfjoe	}
67689099Sfjoe
67789099Sfjoe	if (m == 0) {
67889099Sfjoe		ifp->if_ierrors++;
67989099Sfjoe		goto cleanup;
68089099Sfjoe	}
68189099Sfjoe
68289099Sfjoe	type = GETMEM(cm_ram_ptr + offset);
68389099Sfjoe	m->m_data += 1 + arc_isphds(type);
68489099Sfjoe	/* mbuf filled with ARCnet addresses */
68589099Sfjoe	m->m_pkthdr.len = m->m_len = len + 2;
68689099Sfjoe
68789099Sfjoe	ah = mtod(m, struct arc_header *);
68889099Sfjoe	ah->arc_shost = GETMEM(cm_ram_ptr + 0);
68989099Sfjoe	ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
69089099Sfjoe
69189099Sfjoe	bus_space_read_region_1(
69289099Sfjoe	    rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
69389099Sfjoe	    cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
69489099Sfjoe
69589099Sfjoe	arc_input(ifp, m);
69689099Sfjoe
69789099Sfjoe	m = NULL;
69889099Sfjoe	ifp->if_ipackets++;
69989099Sfjoe
70089099Sfjoecleanup:
70189099Sfjoe
70289099Sfjoe	if (m != NULL)
70389099Sfjoe		m_freem(m);
70489099Sfjoe
70589099Sfjoe	/* mark buffer as invalid by source id 0 */
70689099Sfjoe	PUTMEM(buffer << 9, 0);
70789099Sfjoe	s = splimp();
70889099Sfjoe
70989099Sfjoe	if (--sc->sc_rx_fillcount == 2 - 1) {
71089099Sfjoe
71189099Sfjoe		/* was off, restart it on buffer just emptied */
71289099Sfjoe		sc->sc_rx_act = buffer;
71389099Sfjoe		sc->sc_intmask |= CM_RI;
71489099Sfjoe
71589099Sfjoe		/* this also clears the RI flag interupt: */
71689099Sfjoe		PUTREG(CMCMD, CM_RXBC(buffer));
71789099Sfjoe		PUTREG(CMSTAT, sc->sc_intmask);
71889099Sfjoe
71989099Sfjoe#ifdef CM_DEBUG
720104251Sbrooks		if_printf(ifp, "srint: restarted rx on buf %d\n", buffer);
72189099Sfjoe#endif
72289099Sfjoe	}
72389099Sfjoe	splx(s);
72489099Sfjoe}
72589099Sfjoe
72689099Sfjoe__inline static void
72789099Sfjoecm_tint(sc, isr)
72889099Sfjoe	struct cm_softc *sc;
72989099Sfjoe	int isr;
73089099Sfjoe{
73189099Sfjoe	struct ifnet *ifp;
73289099Sfjoe
73389099Sfjoe	int buffer;
73489099Sfjoe#ifdef CMTIMINGS
73589099Sfjoe	int clknow;
73689099Sfjoe#endif
73789099Sfjoe
73889099Sfjoe	ifp = &(sc->sc_arccom.ac_if);
73989099Sfjoe	buffer = sc->sc_tx_act;
74089099Sfjoe
74189099Sfjoe	/*
74289099Sfjoe	 * retransmit code:
74389099Sfjoe	 * Normal situtations first for fast path:
74489099Sfjoe	 * If acknowledgement received ok or broadcast, we're ok.
74589099Sfjoe	 * else if
74689099Sfjoe	 */
74789099Sfjoe
74889099Sfjoe	if (isr & CM_TMA || sc->sc_broadcast[buffer])
74989099Sfjoe		sc->sc_arccom.ac_if.if_opackets++;
75089099Sfjoe#ifdef CMRETRANSMIT
75189099Sfjoe	else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
75289099Sfjoe	    && --sc->sc_retransmits[buffer] > 0) {
75389099Sfjoe		/* retransmit same buffer */
75489099Sfjoe		PUTREG(CMCMD, CM_TX(buffer));
75589099Sfjoe		return;
75689099Sfjoe	}
75789099Sfjoe#endif
75889099Sfjoe	else
75989099Sfjoe		ifp->if_oerrors++;
76089099Sfjoe
76189099Sfjoe
76289099Sfjoe	/* We know we can accept another buffer at this point. */
76389099Sfjoe	ifp->if_flags &= ~IFF_OACTIVE;
76489099Sfjoe
76589099Sfjoe	if (--sc->sc_tx_fillcount > 0) {
76689099Sfjoe
76789099Sfjoe		/*
76889099Sfjoe		 * start tx on other buffer.
76989099Sfjoe		 * This also clears the int flag
77089099Sfjoe		 */
77189099Sfjoe		buffer ^= 1;
77289099Sfjoe		sc->sc_tx_act = buffer;
77389099Sfjoe
77489099Sfjoe		/*
77589099Sfjoe		 * already given:
77689099Sfjoe		 * sc->sc_intmask |= CM_TA;
77789099Sfjoe		 * PUTREG(CMSTAT, sc->sc_intmask);
77889099Sfjoe		 */
77989099Sfjoe		PUTREG(CMCMD, CM_TX(buffer));
78089099Sfjoe		/* init watchdog timer */
78189099Sfjoe		ifp->if_timer = ARCTIMEOUT;
78289099Sfjoe
78389099Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 1)
784104251Sbrooks		if_printf(ifp,
785104251Sbrooks		    "tint: starting tx on buffer %d, status 0x%02x\n",
786104251Sbrooks		    buffer, GETREG(CMSTAT));
78789099Sfjoe#endif
78889099Sfjoe	} else {
78989099Sfjoe		/* have to disable TX interrupt */
79089099Sfjoe		sc->sc_intmask &= ~CM_TA;
79189099Sfjoe		PUTREG(CMSTAT, sc->sc_intmask);
79289099Sfjoe		/* ... and watchdog timer */
79389099Sfjoe		ifp->if_timer = 0;
79489099Sfjoe
79589099Sfjoe#ifdef CM_DEBUG
796104251Sbrooks		if_printf(ifp, "tint: no more buffers to send, status 0x%02x\n",
797104251Sbrooks		    GETREG(CMSTAT));
79889099Sfjoe#endif
79989099Sfjoe	}
80089099Sfjoe
80189099Sfjoe	/* XXXX TODO */
80289099Sfjoe#ifdef CMSOFTCOPY
80389099Sfjoe	/* schedule soft int to fill a new buffer for us */
80489099Sfjoe	softintr_schedule(sc->sc_txcookie);
80589099Sfjoe#else
80689099Sfjoe	/* call it directly */
80789099Sfjoe	cm_start(ifp);
80889099Sfjoe#endif
80989099Sfjoe}
81089099Sfjoe
81189099Sfjoe/*
81289099Sfjoe * Our interrupt routine
81389099Sfjoe */
81489099Sfjoevoid
81589099Sfjoecmintr(arg)
81689099Sfjoe	void *arg;
81789099Sfjoe{
81889099Sfjoe	struct cm_softc *sc = arg;
81989099Sfjoe	struct ifnet *ifp = &sc->sc_arccom.ac_if;
82089099Sfjoe
82189099Sfjoe	u_char isr, maskedisr;
82289099Sfjoe	int buffer;
82389099Sfjoe	u_long newsec;
82489099Sfjoe
82589099Sfjoe	isr = GETREG(CMSTAT);
82689099Sfjoe	maskedisr = isr & sc->sc_intmask;
82789099Sfjoe	if (!maskedisr)
82889099Sfjoe		return;
82989099Sfjoe	do {
83089099Sfjoe
831109771Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 1)
832104251Sbrooks		if_printf(ifp, "intr: status 0x%02x, intmask 0x%02x\n",
833104251Sbrooks		    isr, sc->sc_intmask);
83489099Sfjoe#endif
83589099Sfjoe
83689099Sfjoe		if (maskedisr & CM_POR) {
83789099Sfjoe			/*
83889099Sfjoe			 * XXX We should never see this. Don't bother to store
83989099Sfjoe			 * the address.
84089099Sfjoe			 * sc->sc_arccom.ac_anaddr = GETMEM(CMMACOFF);
84189099Sfjoe			 */
84289099Sfjoe			PUTREG(CMCMD, CM_CLR(CLR_POR));
84389099Sfjoe			log(LOG_WARNING,
84489099Sfjoe			    "%s%d: intr: got spurious power on reset int\n",
84589099Sfjoe			    ifp->if_name, ifp->if_unit);
84689099Sfjoe		}
84789099Sfjoe
84889099Sfjoe		if (maskedisr & CM_RECON) {
84989099Sfjoe			/*
85089099Sfjoe			 * we dont need to:
85189099Sfjoe			 * PUTREG(CMCMD, CM_CONF(CONF_LONG));
85289099Sfjoe			 */
85389099Sfjoe			PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
85489099Sfjoe			sc->sc_arccom.ac_if.if_collisions++;
85589099Sfjoe
85689099Sfjoe			/*
85789099Sfjoe			 * If less than 2 seconds per reconfig:
85889099Sfjoe			 *	If ARC_EXCESSIVE_RECONFIGS
85989099Sfjoe			 *	since last burst, complain and set treshold for
86089099Sfjoe			 *	warnings to ARC_EXCESSIVE_RECONS_REWARN.
86189099Sfjoe			 *
86289099Sfjoe			 * This allows for, e.g., new stations on the cable, or
86389099Sfjoe			 * cable switching as long as it is over after
86489099Sfjoe			 * (normally) 16 seconds.
86589099Sfjoe			 *
86689099Sfjoe			 * XXX TODO: check timeout bits in status word and
86789099Sfjoe			 * double time if necessary.
86889099Sfjoe			 */
86989099Sfjoe
87089099Sfjoe			callout_stop(&sc->sc_recon_ch);
87189099Sfjoe			newsec = time_second;
87289099Sfjoe			if ((newsec - sc->sc_recontime <= 2) &&
87389099Sfjoe			    (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
87489099Sfjoe				log(LOG_WARNING,
87589099Sfjoe				    "%s%d: excessive token losses, "
87689099Sfjoe				    "cable problem?\n",
87789099Sfjoe				    ifp->if_name, ifp->if_unit);
87889099Sfjoe			}
87989099Sfjoe			sc->sc_recontime = newsec;
88089099Sfjoe			callout_reset(&sc->sc_recon_ch, 15 * hz,
88189099Sfjoe			    cm_reconwatch, (void *)sc);
88289099Sfjoe		}
88389099Sfjoe
88489099Sfjoe		if (maskedisr & CM_RI) {
88589099Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 1)
886104251Sbrooks			if_printf(ifp, "intr: hard rint, act %d\n",
887104251Sbrooks			    sc->sc_rx_act);
88889099Sfjoe#endif
88989099Sfjoe
89089099Sfjoe			buffer = sc->sc_rx_act;
89189099Sfjoe			/* look if buffer is marked invalid: */
892109771Sfjoe			if (GETMEM(buffer * 512) == 0) {
89389099Sfjoe				/*
89489099Sfjoe				 * invalid marked buffer (or illegally
89589099Sfjoe				 * configured sender)
89689099Sfjoe				 */
89789099Sfjoe				log(LOG_WARNING,
89889099Sfjoe				    "%s%d: spurious RX interupt or sender 0 "
89989099Sfjoe				    " (ignored)\n", ifp->if_name, ifp->if_unit);
90089099Sfjoe				/*
90189099Sfjoe				 * restart receiver on same buffer.
90289099Sfjoe				 * XXX maybe better reset interface?
90389099Sfjoe				 */
90489099Sfjoe				PUTREG(CMCMD, CM_RXBC(buffer));
90589099Sfjoe			} else {
90689099Sfjoe				if (++sc->sc_rx_fillcount > 1) {
90789099Sfjoe					sc->sc_intmask &= ~CM_RI;
90889099Sfjoe					PUTREG(CMSTAT, sc->sc_intmask);
90989099Sfjoe				} else {
91089099Sfjoe					buffer ^= 1;
91189099Sfjoe					sc->sc_rx_act = buffer;
91289099Sfjoe
91389099Sfjoe					/*
91489099Sfjoe					 * Start receiver on other receive
91589099Sfjoe					 * buffer. This also clears the RI
91689099Sfjoe					 * interupt flag.
91789099Sfjoe					 */
91889099Sfjoe					PUTREG(CMCMD, CM_RXBC(buffer));
91989099Sfjoe					/* in RX intr, so mask is ok for RX */
92089099Sfjoe
92189099Sfjoe#ifdef CM_DEBUG
922104251Sbrooks					if_printf(ifp, "strt rx for buf %d, "
92389099Sfjoe					    "stat 0x%02x\n",
92489099Sfjoe					    sc->sc_rx_act, GETREG(CMSTAT));
92589099Sfjoe#endif
92689099Sfjoe				}
92789099Sfjoe
92889099Sfjoe#ifdef CMSOFTCOPY
92989099Sfjoe				/*
93089099Sfjoe				 * this one starts a soft int to copy out
93189099Sfjoe				 * of the hw
93289099Sfjoe				 */
93389099Sfjoe				softintr_schedule(sc->sc_rxcookie);
93489099Sfjoe#else
93589099Sfjoe				/* this one does the copy here */
93689099Sfjoe				cm_srint(sc);
93789099Sfjoe#endif
93889099Sfjoe			}
93989099Sfjoe		}
94089099Sfjoe		if (maskedisr & CM_TA) {
94189099Sfjoe			cm_tint(sc, isr);
94289099Sfjoe		}
94389099Sfjoe		isr = GETREG(CMSTAT);
94489099Sfjoe		maskedisr = isr & sc->sc_intmask;
94589099Sfjoe	} while (maskedisr);
946109771Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 1)
947104251Sbrooks	if_printf(ifp, "intr (exit): status 0x%02x, intmask 0x%02x\n",
948104251Sbrooks	    isr, sc->sc_intmask);
94989099Sfjoe#endif
95089099Sfjoe}
95189099Sfjoe
95289099Sfjoevoid
95389099Sfjoecm_reconwatch(arg)
95489099Sfjoe	void *arg;
95589099Sfjoe{
95689099Sfjoe	struct cm_softc *sc = arg;
95789099Sfjoe	struct ifnet *ifp = &sc->sc_arccom.ac_if;
95889099Sfjoe
95989099Sfjoe	if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
96089099Sfjoe		sc->sc_reconcount = 0;
96189099Sfjoe		log(LOG_WARNING, "%s%d: token valid again.\n",
96289099Sfjoe		    ifp->if_name, ifp->if_unit);
96389099Sfjoe	}
96489099Sfjoe	sc->sc_reconcount = 0;
96589099Sfjoe}
96689099Sfjoe
96789099Sfjoe
96889099Sfjoe/*
96989099Sfjoe * Process an ioctl request.
97089099Sfjoe * This code needs some work - it looks pretty ugly.
97189099Sfjoe */
97289099Sfjoeint
97389099Sfjoecm_ioctl(ifp, command, data)
97489099Sfjoe	struct ifnet *ifp;
97589099Sfjoe	u_long command;
97689099Sfjoe	caddr_t data;
97789099Sfjoe{
97889099Sfjoe	struct cm_softc *sc;
97989099Sfjoe	struct ifaddr *ifa;
98089099Sfjoe	struct ifreq *ifr;
98189099Sfjoe	int s, error;
98289099Sfjoe
98389099Sfjoe	error = 0;
98489099Sfjoe	sc = ifp->if_softc;
98589099Sfjoe	ifa = (struct ifaddr *)data;
98689099Sfjoe	ifr = (struct ifreq *)data;
98789099Sfjoe	s = splimp();
98889099Sfjoe
98989099Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 2)
990104251Sbrooks	if_printf(ifp, "ioctl() called, cmd = 0x%lx\n", command);
99189099Sfjoe#endif
99289099Sfjoe
99389099Sfjoe	switch (command) {
99489099Sfjoe	case SIOCSIFADDR:
995109771Sfjoe	case SIOCGIFADDR:
99689099Sfjoe	case SIOCADDMULTI:
99789099Sfjoe	case SIOCDELMULTI:
99889099Sfjoe	case SIOCSIFMTU:
99989099Sfjoe		error = arc_ioctl(ifp, command, data);
100089099Sfjoe		break;
100189099Sfjoe
100289099Sfjoe	case SIOCSIFFLAGS:
100389099Sfjoe		if ((ifp->if_flags & IFF_UP) == 0 &&
100489099Sfjoe		    (ifp->if_flags & IFF_RUNNING) != 0) {
100589099Sfjoe			/*
100689099Sfjoe			 * If interface is marked down and it is running,
100789099Sfjoe			 * then stop it.
100889099Sfjoe			 */
100989099Sfjoe			cm_stop(sc);
101089099Sfjoe			ifp->if_flags &= ~IFF_RUNNING;
101189099Sfjoe		} else if ((ifp->if_flags & IFF_UP) != 0 &&
101289099Sfjoe			   (ifp->if_flags & IFF_RUNNING) == 0) {
101389099Sfjoe			/*
101489099Sfjoe			 * If interface is marked up and it is stopped, then
101589099Sfjoe			 * start it.
101689099Sfjoe			 */
101789099Sfjoe			cm_init(sc);
101889099Sfjoe		}
101989099Sfjoe		break;
102089099Sfjoe
102189099Sfjoe	default:
102289099Sfjoe		error = EINVAL;
102389099Sfjoe		break;
102489099Sfjoe	}
102589099Sfjoe
102689099Sfjoe	splx(s);
102789099Sfjoe	return (error);
102889099Sfjoe}
102989099Sfjoe
103089099Sfjoe/*
103189099Sfjoe * watchdog routine for transmitter.
103289099Sfjoe *
103389099Sfjoe * We need this, because else a receiver whose hardware is alive, but whose
103489099Sfjoe * software has not enabled the Receiver, would make our hardware wait forever
103589099Sfjoe * Discovered this after 20 times reading the docs.
103689099Sfjoe *
1037108470Sschweikh * Only thing we do is disable transmitter. We'll get a transmit timeout,
103889099Sfjoe * and the int handler will have to decide not to retransmit (in case
103989099Sfjoe * retransmission is implemented).
104089099Sfjoe *
104189099Sfjoe * This one assumes being called inside splimp()
104289099Sfjoe */
104389099Sfjoe
104489099Sfjoevoid
104589099Sfjoecm_watchdog(ifp)
104689099Sfjoe	struct ifnet *ifp;
104789099Sfjoe{
104889099Sfjoe	struct cm_softc *sc = ifp->if_softc;
104989099Sfjoe
105089099Sfjoe	PUTREG(CMCMD, CM_TXDIS);
105189099Sfjoe	return;
105289099Sfjoe}
1053