smc90cx6.c revision 147256
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 147256 2005-06-10 16:49:24Z 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
233127135Snjl	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
23489099Sfjoe	if (res) {
23589099Sfjoe		sc->irq_rid = rid;
23689099Sfjoe		sc->irq_res = res;
23789099Sfjoe		return (0);
23889099Sfjoe	} else {
23989099Sfjoe		return (ENOENT);
24089099Sfjoe	}
24189099Sfjoe}
24289099Sfjoe
24389099Sfjoe/*
24489099Sfjoe * Release all resources
24589099Sfjoe */
24689099Sfjoevoid
24789099Sfjoecm_release_resources(dev)
24889099Sfjoe	device_t dev;
24989099Sfjoe{
25089099Sfjoe	struct cm_softc *sc = device_get_softc(dev);
25189099Sfjoe
25289099Sfjoe	if (sc->port_res) {
25389099Sfjoe		bus_deactivate_resource(dev, SYS_RES_IOPORT,
25489099Sfjoe				     sc->port_rid, sc->port_res);
25589099Sfjoe		bus_release_resource(dev, SYS_RES_IOPORT,
25689099Sfjoe				     sc->port_rid, sc->port_res);
25789099Sfjoe		sc->port_res = 0;
25889099Sfjoe	}
25989099Sfjoe	if (sc->mem_res) {
26089099Sfjoe		bus_deactivate_resource(dev, SYS_RES_MEMORY,
26189099Sfjoe				     sc->mem_rid, sc->mem_res);
26289099Sfjoe		bus_release_resource(dev, SYS_RES_MEMORY,
26389099Sfjoe				     sc->mem_rid, sc->mem_res);
26489099Sfjoe		sc->mem_res = 0;
26589099Sfjoe	}
26689099Sfjoe	if (sc->irq_res) {
26789099Sfjoe		bus_deactivate_resource(dev, SYS_RES_IRQ,
26889099Sfjoe				     sc->irq_rid, sc->irq_res);
26989099Sfjoe		bus_release_resource(dev, SYS_RES_IRQ,
27089099Sfjoe				     sc->irq_rid, sc->irq_res);
27189099Sfjoe		sc->irq_res = 0;
27289099Sfjoe	}
27389099Sfjoe}
27489099Sfjoe
27589099Sfjoeint
276121816Sbrookscm_attach(dev)
277121816Sbrooks	device_t dev;
27889099Sfjoe{
279121816Sbrooks	struct cm_softc *sc = device_get_softc(dev);
280147256Sbrooks	struct ifnet *ifp;
28189099Sfjoe	int s;
28289099Sfjoe	u_int8_t linkaddress;
28389099Sfjoe
284147256Sbrooks	ifp = sc->sc_ifp = if_alloc(IFT_ARCNET);
285147256Sbrooks	if (ifp == NULL) {
286147256Sbrooks		return (ENOSPC);
287147256Sbrooks	}
288147256Sbrooks
28989099Sfjoe	s = splhigh();
29089099Sfjoe
29189099Sfjoe	/*
29289099Sfjoe	 * read the arcnet address from the board
29389099Sfjoe	 */
29489099Sfjoe
29589099Sfjoe	GETREG(CMRESET);
29689099Sfjoe	do {
29789099Sfjoe		DELAY(200);
29889099Sfjoe	} while (!(GETREG(CMSTAT) & CM_POR));
29989099Sfjoe
30089099Sfjoe	linkaddress = GETMEM(CMMACOFF);
30189099Sfjoe
30289099Sfjoe	/* clear the int mask... */
30389099Sfjoe
30489099Sfjoe	sc->sc_intmask = 0;
30589099Sfjoe	PUTREG(CMSTAT, 0);
30689099Sfjoe
30789099Sfjoe	PUTREG(CMCMD, CM_CONF(CONF_LONG));
30889099Sfjoe	PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
30989099Sfjoe	sc->sc_recontime = sc->sc_reconcount = 0;
31089099Sfjoe
31189099Sfjoe	/* and reenable kernel int level */
31289099Sfjoe	splx(s);
31389099Sfjoe
31489099Sfjoe	/*
31589099Sfjoe	 * set interface to stopped condition (reset)
31689099Sfjoe	 */
31789099Sfjoe	cm_stop(sc);
31889099Sfjoe
319121752Sbrooks	ifp->if_softc = sc;
320121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
321121752Sbrooks	ifp->if_output = arc_output;
322121752Sbrooks	ifp->if_start = cm_start;
323121752Sbrooks	ifp->if_ioctl = cm_ioctl;
324121752Sbrooks	ifp->if_watchdog  = cm_watchdog;
325121752Sbrooks	ifp->if_init = cm_init;
326121752Sbrooks	/* XXX IFQ_SET_READY(&ifp->if_snd); */
327121752Sbrooks	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
328121752Sbrooks	ifp->if_timer = 0;
329133678Srwatson	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NEEDSGIANT;
33089099Sfjoe
331121752Sbrooks	arc_ifattach(ifp, linkaddress);
33289099Sfjoe
33389099Sfjoe#ifdef CMSOFTCOPY
334121752Sbrooks	sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, cm_srint, sc);
335121752Sbrooks	sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
336121752Sbrooks		(void (*)(void *))cm_start, ifp);
33789099Sfjoe#endif
33889099Sfjoe
33989099Sfjoe#if __FreeBSD_version < 500000
340121752Sbrooks	callout_init(&sc->sc_recon_ch);
34189099Sfjoe#else
342121752Sbrooks	callout_init(&sc->sc_recon_ch, 0);
34389099Sfjoe#endif
34489099Sfjoe
345109771Sfjoe	if_printf(ifp, "link addr 0x%02x (%d)\n", linkaddress, linkaddress);
34689099Sfjoe	return 0;
34789099Sfjoe}
34889099Sfjoe
34989099Sfjoe/*
35089099Sfjoe * Initialize device
35189099Sfjoe *
35289099Sfjoe */
35389099Sfjoevoid
35489099Sfjoecm_init(xsc)
35589099Sfjoe	void *xsc;
35689099Sfjoe{
35789099Sfjoe	struct cm_softc *sc = (struct cm_softc *)xsc;
35889099Sfjoe	struct ifnet *ifp;
35989099Sfjoe	int s;
36089099Sfjoe
361147256Sbrooks	ifp = sc->sc_ifp;
36289099Sfjoe
36389099Sfjoe	if ((ifp->if_flags & IFF_RUNNING) == 0) {
36489099Sfjoe		s = splimp();
36589099Sfjoe		ifp->if_flags |= IFF_RUNNING;
36689099Sfjoe		cm_reset(sc);
36789099Sfjoe		cm_start(ifp);
36889099Sfjoe		splx(s);
36989099Sfjoe	}
37089099Sfjoe}
37189099Sfjoe
37289099Sfjoe/*
37389099Sfjoe * Reset the interface...
37489099Sfjoe *
37589099Sfjoe * this assumes that it is called inside a critical section...
37689099Sfjoe *
37789099Sfjoe */
37889099Sfjoevoid
37989099Sfjoecm_reset(sc)
38089099Sfjoe	struct cm_softc *sc;
38189099Sfjoe{
38289099Sfjoe	struct ifnet *ifp;
38389099Sfjoe	int linkaddress;
38489099Sfjoe
385147256Sbrooks	ifp = sc->sc_ifp;
38689099Sfjoe
38789099Sfjoe#ifdef CM_DEBUG
388104251Sbrooks	if_printf(ifp, "reset\n");
38989099Sfjoe#endif
39089099Sfjoe	/* stop and restart hardware */
39189099Sfjoe
39289099Sfjoe	GETREG(CMRESET);
39389099Sfjoe	do {
39489099Sfjoe		DELAY(200);
39589099Sfjoe	} while (!(GETREG(CMSTAT) & CM_POR));
39689099Sfjoe
39789099Sfjoe	linkaddress = GETMEM(CMMACOFF);
39889099Sfjoe
39989099Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 2)
400104251Sbrooks	if_printf(ifp, "reset: card reset, link addr = 0x%02x (%d)\n",
401104251Sbrooks	    linkaddress, linkaddress);
40289099Sfjoe#endif
40389099Sfjoe
40489099Sfjoe	/* tell the routing level about the (possibly changed) link address */
40589099Sfjoe	arc_storelladdr(ifp, linkaddress);
40689099Sfjoe	arc_frag_init(ifp);
40789099Sfjoe
40889099Sfjoe	/* POR is NMI, but we need it below: */
40989099Sfjoe	sc->sc_intmask = CM_RECON|CM_POR;
41089099Sfjoe	PUTREG(CMSTAT, sc->sc_intmask);
41189099Sfjoe	PUTREG(CMCMD, CM_CONF(CONF_LONG));
41289099Sfjoe
41389099Sfjoe#ifdef CM_DEBUG
414104251Sbrooks	if_printf(ifp, "reset: chip configured, status=0x%02x\n",
415104251Sbrooks	    GETREG(CMSTAT));
41689099Sfjoe#endif
41789099Sfjoe	PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
41889099Sfjoe
41989099Sfjoe#ifdef CM_DEBUG
420104251Sbrooks	if_printf(ifp, "reset: bits cleared, status=0x%02x\n",
421104251Sbrooks	     GETREG(CMSTAT));
42289099Sfjoe#endif
42389099Sfjoe
42489099Sfjoe	sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
42589099Sfjoe
42689099Sfjoe	/* start receiver */
42789099Sfjoe
42889099Sfjoe	sc->sc_intmask  |= CM_RI;
42989099Sfjoe	sc->sc_rx_fillcount = 0;
43089099Sfjoe	sc->sc_rx_act = 2;
43189099Sfjoe
43289099Sfjoe	PUTREG(CMCMD, CM_RXBC(2));
43389099Sfjoe	PUTREG(CMSTAT, sc->sc_intmask);
43489099Sfjoe
43589099Sfjoe#ifdef CM_DEBUG
436104251Sbrooks	if_printf(ifp, "reset: started receiver, status=0x%02x\n",
437104251Sbrooks	    GETREG(CMSTAT));
43889099Sfjoe#endif
43989099Sfjoe
44089099Sfjoe	/* and init transmitter status */
44189099Sfjoe	sc->sc_tx_act = 0;
44289099Sfjoe	sc->sc_tx_fillcount = 0;
44389099Sfjoe
44489099Sfjoe	ifp->if_flags |= IFF_RUNNING;
44589099Sfjoe	ifp->if_flags &= ~IFF_OACTIVE;
44689099Sfjoe
44789099Sfjoe	cm_start(ifp);
44889099Sfjoe}
44989099Sfjoe
45089099Sfjoe/*
45189099Sfjoe * Take interface offline
45289099Sfjoe */
45389099Sfjoevoid
45489099Sfjoecm_stop(sc)
45589099Sfjoe	struct cm_softc *sc;
45689099Sfjoe{
45789099Sfjoe	/* Stop the interrupts */
45889099Sfjoe	PUTREG(CMSTAT, 0);
45989099Sfjoe
46089099Sfjoe	/* Stop the interface */
46189099Sfjoe	GETREG(CMRESET);
46289099Sfjoe
46389099Sfjoe	/* Stop watchdog timer */
464147256Sbrooks	sc->sc_ifp->if_timer = 0;
46589099Sfjoe}
46689099Sfjoe
46789099Sfjoe/*
46889099Sfjoe * Start output on interface. Get another datagram to send
46989099Sfjoe * off the interface queue, and copy it to the
47089099Sfjoe * interface becore starting the output
47189099Sfjoe *
47289099Sfjoe * this assumes that it is called inside a critical section...
47389099Sfjoe * XXX hm... does it still?
47489099Sfjoe *
47589099Sfjoe */
47689099Sfjoevoid
47789099Sfjoecm_start(ifp)
47889099Sfjoe	struct ifnet *ifp;
47989099Sfjoe{
48089099Sfjoe	struct cm_softc *sc = ifp->if_softc;
48189099Sfjoe	struct mbuf *m,*mp;
48289099Sfjoe
48389099Sfjoe	int cm_ram_ptr;
48489099Sfjoe	int len, tlen, offset, s, buffer;
48589099Sfjoe#ifdef CMTIMINGS
48689099Sfjoe	u_long copystart, lencopy, perbyte;
48789099Sfjoe#endif
48889099Sfjoe
48989099Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 3)
490104251Sbrooks	if_printf(ifp, "start(%p)\n", ifp);
49189099Sfjoe#endif
49289099Sfjoe
49389099Sfjoe	if ((ifp->if_flags & IFF_RUNNING) == 0)
49489099Sfjoe		return;
49589099Sfjoe
49689099Sfjoe	s = splimp();
49789099Sfjoe
49889099Sfjoe	if (sc->sc_tx_fillcount >= 2) {
49989099Sfjoe		splx(s);
50089099Sfjoe		return;
50189099Sfjoe	}
50289099Sfjoe
50389099Sfjoe	m = arc_frag_next(ifp);
50489099Sfjoe	buffer = sc->sc_tx_act ^ 1;
50589099Sfjoe
50689099Sfjoe	splx(s);
50789099Sfjoe
50889099Sfjoe	if (m == 0)
50989099Sfjoe		return;
51089099Sfjoe
51189099Sfjoe#ifdef CM_DEBUG
51289099Sfjoe	if (m->m_len < ARC_HDRLEN)
51389099Sfjoe		m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
514104251Sbrooks	if_printf(ifp, "start: filling %d from %d to %d type %d\n",
515104251Sbrooks	    buffer, mtod(m, u_char *)[0],
51689099Sfjoe	    mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
51789099Sfjoe#else
51889099Sfjoe	if (m->m_len < 2)
51989099Sfjoe		m = m_pullup(m, 2);
52089099Sfjoe#endif
521109771Sfjoe	cm_ram_ptr = buffer * 512;
52289099Sfjoe
52389099Sfjoe	if (m == 0)
52489099Sfjoe		return;
52589099Sfjoe
52689099Sfjoe	/* write the addresses to RAM and throw them away */
52789099Sfjoe
52889099Sfjoe	/*
52989099Sfjoe	 * Hardware does this: Yet Another Microsecond Saved.
53089099Sfjoe	 * (btw, timing code says usually 2 microseconds)
53189099Sfjoe	 * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
53289099Sfjoe	 */
53389099Sfjoe
53489099Sfjoe	PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
53589099Sfjoe	m_adj(m, 2);
53689099Sfjoe
53789099Sfjoe	/* get total length left at this point */
53889099Sfjoe	tlen = m->m_pkthdr.len;
53989099Sfjoe	if (tlen < ARC_MIN_FORBID_LEN) {
54089099Sfjoe		offset = 256 - tlen;
54189099Sfjoe		PUTMEM(cm_ram_ptr + 2, offset);
54289099Sfjoe	} else {
54389099Sfjoe		PUTMEM(cm_ram_ptr + 2, 0);
54489099Sfjoe		if (tlen <= ARC_MAX_FORBID_LEN)
54589099Sfjoe			offset = 255;		/* !!! */
54689099Sfjoe		else {
54789099Sfjoe			if (tlen > ARC_MAX_LEN)
54889099Sfjoe				tlen = ARC_MAX_LEN;
54989099Sfjoe			offset = 512 - tlen;
55089099Sfjoe		}
55189099Sfjoe		PUTMEM(cm_ram_ptr + 3, offset);
55289099Sfjoe
55389099Sfjoe	}
55489099Sfjoe	cm_ram_ptr += offset;
55589099Sfjoe
55689099Sfjoe	/* lets loop through the mbuf chain */
55789099Sfjoe
55889099Sfjoe	for (mp = m; mp; mp = mp->m_next) {
55989099Sfjoe		if ((len = mp->m_len)) {		/* YAMS */
56089099Sfjoe			bus_space_write_region_1(
56189099Sfjoe			    rman_get_bustag(sc->mem_res),
56289099Sfjoe			    rman_get_bushandle(sc->mem_res),
56389099Sfjoe			    cm_ram_ptr, mtod(mp, caddr_t), len);
56489099Sfjoe
56589099Sfjoe			cm_ram_ptr += len;
56689099Sfjoe		}
56789099Sfjoe	}
56889099Sfjoe
56989099Sfjoe	sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
57089099Sfjoe	sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
57189099Sfjoe
57289099Sfjoe	/* actually transmit the packet */
57389099Sfjoe	s = splimp();
57489099Sfjoe
57589099Sfjoe	if (++sc->sc_tx_fillcount > 1) {
57689099Sfjoe		/*
57789099Sfjoe		 * We are filled up to the rim. No more bufs for the moment,
57889099Sfjoe		 * please.
57989099Sfjoe		 */
58089099Sfjoe		ifp->if_flags |= IFF_OACTIVE;
58189099Sfjoe	} else {
58289099Sfjoe#ifdef CM_DEBUG
583104251Sbrooks		if_printf(ifp, "start: starting transmitter on buffer %d\n",
584104251Sbrooks		    buffer);
58589099Sfjoe#endif
58689099Sfjoe		/* Transmitter was off, start it */
58789099Sfjoe		sc->sc_tx_act = buffer;
58889099Sfjoe
58989099Sfjoe		/*
59089099Sfjoe		 * We still can accept another buf, so don't:
59189099Sfjoe		 * ifp->if_flags |= IFF_OACTIVE;
59289099Sfjoe		 */
59389099Sfjoe		sc->sc_intmask |= CM_TA;
59489099Sfjoe		PUTREG(CMCMD, CM_TX(buffer));
59589099Sfjoe		PUTREG(CMSTAT, sc->sc_intmask);
59689099Sfjoe
597147256Sbrooks		ifp->if_timer = ARCTIMEOUT;
59889099Sfjoe	}
59989099Sfjoe	splx(s);
60089099Sfjoe	m_freem(m);
60189099Sfjoe
60289099Sfjoe	/*
60389099Sfjoe	 * After 10 times reading the docs, I realized
60489099Sfjoe	 * that in the case the receiver NAKs the buffer request,
60589099Sfjoe	 * the hardware retries till shutdown.
60689099Sfjoe	 * This is integrated now in the code above.
60789099Sfjoe	 */
60889099Sfjoe
60989099Sfjoe	return;
61089099Sfjoe}
61189099Sfjoe
61289099Sfjoe/*
61389099Sfjoe * Arcnet interface receiver soft interrupt:
61489099Sfjoe * get the stuff out of any filled buffer we find.
61589099Sfjoe */
61689099Sfjoevoid
61789099Sfjoecm_srint(vsc)
61889099Sfjoe	void *vsc;
61989099Sfjoe{
62089099Sfjoe	struct cm_softc *sc = (struct cm_softc *)vsc;
62189099Sfjoe	int buffer, len, offset, s, type;
62289099Sfjoe	int cm_ram_ptr;
62389099Sfjoe	struct mbuf *m;
62489099Sfjoe	struct arc_header *ah;
62589099Sfjoe	struct ifnet *ifp;
62689099Sfjoe
627147256Sbrooks	ifp = sc->sc_ifp;
62889099Sfjoe
62989099Sfjoe	s = splimp();
63089099Sfjoe	buffer = sc->sc_rx_act ^ 1;
63189099Sfjoe	splx(s);
63289099Sfjoe
63389099Sfjoe	/* Allocate header mbuf */
634111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
63589099Sfjoe
63689099Sfjoe	if (m == 0) {
63789099Sfjoe		/*
63889099Sfjoe		 * in case s.th. goes wrong with mem, drop it
63989099Sfjoe		 * to make sure the receiver can be started again
64089099Sfjoe		 * count it as input error (we dont have any other
64189099Sfjoe		 * detectable)
64289099Sfjoe		 */
64389099Sfjoe		ifp->if_ierrors++;
64489099Sfjoe		goto cleanup;
64589099Sfjoe	}
64689099Sfjoe
64789099Sfjoe	m->m_pkthdr.rcvif = ifp;
64889099Sfjoe
64989099Sfjoe	/*
65089099Sfjoe	 * Align so that IP packet will be longword aligned. Here we
65189099Sfjoe	 * assume that m_data of new packet is longword aligned.
65289099Sfjoe	 * When implementing PHDS, we might have to change it to 2,
65389099Sfjoe	 * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
65489099Sfjoe	 */
65589099Sfjoe
656109771Sfjoe	cm_ram_ptr = buffer * 512;
65789099Sfjoe	offset = GETMEM(cm_ram_ptr + 2);
65889099Sfjoe	if (offset)
65989099Sfjoe		len = 256 - offset;
66089099Sfjoe	else {
66189099Sfjoe		offset = GETMEM(cm_ram_ptr + 3);
66289099Sfjoe		len = 512 - offset;
66389099Sfjoe	}
66489099Sfjoe
66589099Sfjoe	/*
66689099Sfjoe	 * first +2 bytes for align fixup below
66789099Sfjoe	 * second +2 bytes are for src/dst addresses
66889099Sfjoe	 */
66989099Sfjoe	if ((len + 2 + 2) > MHLEN) {
67089099Sfjoe		/* attach an mbuf cluster */
671111119Simp		MCLGET(m, M_DONTWAIT);
67289099Sfjoe
67389099Sfjoe		/* Insist on getting a cluster */
67489099Sfjoe		if ((m->m_flags & M_EXT) == 0) {
67589099Sfjoe			ifp->if_ierrors++;
67689099Sfjoe			goto cleanup;
67789099Sfjoe		}
67889099Sfjoe	}
67989099Sfjoe
68089099Sfjoe	if (m == 0) {
68189099Sfjoe		ifp->if_ierrors++;
68289099Sfjoe		goto cleanup;
68389099Sfjoe	}
68489099Sfjoe
68589099Sfjoe	type = GETMEM(cm_ram_ptr + offset);
68689099Sfjoe	m->m_data += 1 + arc_isphds(type);
68789099Sfjoe	/* mbuf filled with ARCnet addresses */
68889099Sfjoe	m->m_pkthdr.len = m->m_len = len + 2;
68989099Sfjoe
69089099Sfjoe	ah = mtod(m, struct arc_header *);
69189099Sfjoe	ah->arc_shost = GETMEM(cm_ram_ptr + 0);
69289099Sfjoe	ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
69389099Sfjoe
69489099Sfjoe	bus_space_read_region_1(
69589099Sfjoe	    rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
69689099Sfjoe	    cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
69789099Sfjoe
69889099Sfjoe	arc_input(ifp, m);
69989099Sfjoe
70089099Sfjoe	m = NULL;
70189099Sfjoe	ifp->if_ipackets++;
70289099Sfjoe
70389099Sfjoecleanup:
70489099Sfjoe
70589099Sfjoe	if (m != NULL)
70689099Sfjoe		m_freem(m);
70789099Sfjoe
70889099Sfjoe	/* mark buffer as invalid by source id 0 */
70989099Sfjoe	PUTMEM(buffer << 9, 0);
71089099Sfjoe	s = splimp();
71189099Sfjoe
71289099Sfjoe	if (--sc->sc_rx_fillcount == 2 - 1) {
71389099Sfjoe
71489099Sfjoe		/* was off, restart it on buffer just emptied */
71589099Sfjoe		sc->sc_rx_act = buffer;
71689099Sfjoe		sc->sc_intmask |= CM_RI;
71789099Sfjoe
71889099Sfjoe		/* this also clears the RI flag interupt: */
71989099Sfjoe		PUTREG(CMCMD, CM_RXBC(buffer));
72089099Sfjoe		PUTREG(CMSTAT, sc->sc_intmask);
72189099Sfjoe
72289099Sfjoe#ifdef CM_DEBUG
723104251Sbrooks		if_printf(ifp, "srint: restarted rx on buf %d\n", buffer);
72489099Sfjoe#endif
72589099Sfjoe	}
72689099Sfjoe	splx(s);
72789099Sfjoe}
72889099Sfjoe
72989099Sfjoe__inline static void
73089099Sfjoecm_tint(sc, isr)
73189099Sfjoe	struct cm_softc *sc;
73289099Sfjoe	int isr;
73389099Sfjoe{
73489099Sfjoe	struct ifnet *ifp;
73589099Sfjoe
73689099Sfjoe	int buffer;
73789099Sfjoe#ifdef CMTIMINGS
73889099Sfjoe	int clknow;
73989099Sfjoe#endif
74089099Sfjoe
741147256Sbrooks	ifp = sc->sc_ifp;
74289099Sfjoe	buffer = sc->sc_tx_act;
74389099Sfjoe
74489099Sfjoe	/*
74589099Sfjoe	 * retransmit code:
74689099Sfjoe	 * Normal situtations first for fast path:
74789099Sfjoe	 * If acknowledgement received ok or broadcast, we're ok.
74889099Sfjoe	 * else if
74989099Sfjoe	 */
75089099Sfjoe
75189099Sfjoe	if (isr & CM_TMA || sc->sc_broadcast[buffer])
752147256Sbrooks		ifp->if_opackets++;
75389099Sfjoe#ifdef CMRETRANSMIT
75489099Sfjoe	else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
75589099Sfjoe	    && --sc->sc_retransmits[buffer] > 0) {
75689099Sfjoe		/* retransmit same buffer */
75789099Sfjoe		PUTREG(CMCMD, CM_TX(buffer));
75889099Sfjoe		return;
75989099Sfjoe	}
76089099Sfjoe#endif
76189099Sfjoe	else
76289099Sfjoe		ifp->if_oerrors++;
76389099Sfjoe
76489099Sfjoe
76589099Sfjoe	/* We know we can accept another buffer at this point. */
76689099Sfjoe	ifp->if_flags &= ~IFF_OACTIVE;
76789099Sfjoe
76889099Sfjoe	if (--sc->sc_tx_fillcount > 0) {
76989099Sfjoe
77089099Sfjoe		/*
77189099Sfjoe		 * start tx on other buffer.
77289099Sfjoe		 * This also clears the int flag
77389099Sfjoe		 */
77489099Sfjoe		buffer ^= 1;
77589099Sfjoe		sc->sc_tx_act = buffer;
77689099Sfjoe
77789099Sfjoe		/*
77889099Sfjoe		 * already given:
77989099Sfjoe		 * sc->sc_intmask |= CM_TA;
78089099Sfjoe		 * PUTREG(CMSTAT, sc->sc_intmask);
78189099Sfjoe		 */
78289099Sfjoe		PUTREG(CMCMD, CM_TX(buffer));
78389099Sfjoe		/* init watchdog timer */
78489099Sfjoe		ifp->if_timer = ARCTIMEOUT;
78589099Sfjoe
78689099Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 1)
787104251Sbrooks		if_printf(ifp,
788104251Sbrooks		    "tint: starting tx on buffer %d, status 0x%02x\n",
789104251Sbrooks		    buffer, GETREG(CMSTAT));
79089099Sfjoe#endif
79189099Sfjoe	} else {
79289099Sfjoe		/* have to disable TX interrupt */
79389099Sfjoe		sc->sc_intmask &= ~CM_TA;
79489099Sfjoe		PUTREG(CMSTAT, sc->sc_intmask);
79589099Sfjoe		/* ... and watchdog timer */
79689099Sfjoe		ifp->if_timer = 0;
79789099Sfjoe
79889099Sfjoe#ifdef CM_DEBUG
799104251Sbrooks		if_printf(ifp, "tint: no more buffers to send, status 0x%02x\n",
800104251Sbrooks		    GETREG(CMSTAT));
80189099Sfjoe#endif
80289099Sfjoe	}
80389099Sfjoe
80489099Sfjoe	/* XXXX TODO */
80589099Sfjoe#ifdef CMSOFTCOPY
80689099Sfjoe	/* schedule soft int to fill a new buffer for us */
80789099Sfjoe	softintr_schedule(sc->sc_txcookie);
80889099Sfjoe#else
80989099Sfjoe	/* call it directly */
81089099Sfjoe	cm_start(ifp);
81189099Sfjoe#endif
81289099Sfjoe}
81389099Sfjoe
81489099Sfjoe/*
81589099Sfjoe * Our interrupt routine
81689099Sfjoe */
81789099Sfjoevoid
81889099Sfjoecmintr(arg)
81989099Sfjoe	void *arg;
82089099Sfjoe{
82189099Sfjoe	struct cm_softc *sc = arg;
822147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
82389099Sfjoe
82489099Sfjoe	u_char isr, maskedisr;
82589099Sfjoe	int buffer;
82689099Sfjoe	u_long newsec;
82789099Sfjoe
82889099Sfjoe	isr = GETREG(CMSTAT);
82989099Sfjoe	maskedisr = isr & sc->sc_intmask;
83089099Sfjoe	if (!maskedisr)
83189099Sfjoe		return;
83289099Sfjoe	do {
83389099Sfjoe
834109771Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 1)
835104251Sbrooks		if_printf(ifp, "intr: status 0x%02x, intmask 0x%02x\n",
836104251Sbrooks		    isr, sc->sc_intmask);
83789099Sfjoe#endif
83889099Sfjoe
83989099Sfjoe		if (maskedisr & CM_POR) {
84089099Sfjoe			/*
84189099Sfjoe			 * XXX We should never see this. Don't bother to store
84289099Sfjoe			 * the address.
843147256Sbrooks			 * sc->sc_ifp->if_l2com->ac_anaddr = GETMEM(CMMACOFF);
84489099Sfjoe			 */
84589099Sfjoe			PUTREG(CMCMD, CM_CLR(CLR_POR));
84689099Sfjoe			log(LOG_WARNING,
847121816Sbrooks			    "%s: intr: got spurious power on reset int\n",
848121816Sbrooks			    ifp->if_xname);
84989099Sfjoe		}
85089099Sfjoe
85189099Sfjoe		if (maskedisr & CM_RECON) {
85289099Sfjoe			/*
85389099Sfjoe			 * we dont need to:
85489099Sfjoe			 * PUTREG(CMCMD, CM_CONF(CONF_LONG));
85589099Sfjoe			 */
85689099Sfjoe			PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
857147256Sbrooks			ifp->if_collisions++;
85889099Sfjoe
85989099Sfjoe			/*
86089099Sfjoe			 * If less than 2 seconds per reconfig:
86189099Sfjoe			 *	If ARC_EXCESSIVE_RECONFIGS
86289099Sfjoe			 *	since last burst, complain and set treshold for
86389099Sfjoe			 *	warnings to ARC_EXCESSIVE_RECONS_REWARN.
86489099Sfjoe			 *
86589099Sfjoe			 * This allows for, e.g., new stations on the cable, or
86689099Sfjoe			 * cable switching as long as it is over after
86789099Sfjoe			 * (normally) 16 seconds.
86889099Sfjoe			 *
86989099Sfjoe			 * XXX TODO: check timeout bits in status word and
87089099Sfjoe			 * double time if necessary.
87189099Sfjoe			 */
87289099Sfjoe
87389099Sfjoe			callout_stop(&sc->sc_recon_ch);
87489099Sfjoe			newsec = time_second;
87589099Sfjoe			if ((newsec - sc->sc_recontime <= 2) &&
87689099Sfjoe			    (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
87789099Sfjoe				log(LOG_WARNING,
878121816Sbrooks				    "%s: excessive token losses, "
87989099Sfjoe				    "cable problem?\n",
880121816Sbrooks				    ifp->if_xname);
88189099Sfjoe			}
88289099Sfjoe			sc->sc_recontime = newsec;
88389099Sfjoe			callout_reset(&sc->sc_recon_ch, 15 * hz,
88489099Sfjoe			    cm_reconwatch, (void *)sc);
88589099Sfjoe		}
88689099Sfjoe
88789099Sfjoe		if (maskedisr & CM_RI) {
88889099Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 1)
889104251Sbrooks			if_printf(ifp, "intr: hard rint, act %d\n",
890104251Sbrooks			    sc->sc_rx_act);
89189099Sfjoe#endif
89289099Sfjoe
89389099Sfjoe			buffer = sc->sc_rx_act;
89489099Sfjoe			/* look if buffer is marked invalid: */
895109771Sfjoe			if (GETMEM(buffer * 512) == 0) {
89689099Sfjoe				/*
89789099Sfjoe				 * invalid marked buffer (or illegally
89889099Sfjoe				 * configured sender)
89989099Sfjoe				 */
90089099Sfjoe				log(LOG_WARNING,
901121816Sbrooks				    "%s: spurious RX interupt or sender 0 "
902121816Sbrooks				    " (ignored)\n", ifp->if_xname);
90389099Sfjoe				/*
90489099Sfjoe				 * restart receiver on same buffer.
90589099Sfjoe				 * XXX maybe better reset interface?
90689099Sfjoe				 */
90789099Sfjoe				PUTREG(CMCMD, CM_RXBC(buffer));
90889099Sfjoe			} else {
90989099Sfjoe				if (++sc->sc_rx_fillcount > 1) {
91089099Sfjoe					sc->sc_intmask &= ~CM_RI;
91189099Sfjoe					PUTREG(CMSTAT, sc->sc_intmask);
91289099Sfjoe				} else {
91389099Sfjoe					buffer ^= 1;
91489099Sfjoe					sc->sc_rx_act = buffer;
91589099Sfjoe
91689099Sfjoe					/*
91789099Sfjoe					 * Start receiver on other receive
91889099Sfjoe					 * buffer. This also clears the RI
91989099Sfjoe					 * interupt flag.
92089099Sfjoe					 */
92189099Sfjoe					PUTREG(CMCMD, CM_RXBC(buffer));
92289099Sfjoe					/* in RX intr, so mask is ok for RX */
92389099Sfjoe
92489099Sfjoe#ifdef CM_DEBUG
925104251Sbrooks					if_printf(ifp, "strt rx for buf %d, "
92689099Sfjoe					    "stat 0x%02x\n",
92789099Sfjoe					    sc->sc_rx_act, GETREG(CMSTAT));
92889099Sfjoe#endif
92989099Sfjoe				}
93089099Sfjoe
93189099Sfjoe#ifdef CMSOFTCOPY
93289099Sfjoe				/*
93389099Sfjoe				 * this one starts a soft int to copy out
93489099Sfjoe				 * of the hw
93589099Sfjoe				 */
93689099Sfjoe				softintr_schedule(sc->sc_rxcookie);
93789099Sfjoe#else
93889099Sfjoe				/* this one does the copy here */
93989099Sfjoe				cm_srint(sc);
94089099Sfjoe#endif
94189099Sfjoe			}
94289099Sfjoe		}
94389099Sfjoe		if (maskedisr & CM_TA) {
94489099Sfjoe			cm_tint(sc, isr);
94589099Sfjoe		}
94689099Sfjoe		isr = GETREG(CMSTAT);
94789099Sfjoe		maskedisr = isr & sc->sc_intmask;
94889099Sfjoe	} while (maskedisr);
949109771Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 1)
950104251Sbrooks	if_printf(ifp, "intr (exit): status 0x%02x, intmask 0x%02x\n",
951104251Sbrooks	    isr, sc->sc_intmask);
95289099Sfjoe#endif
95389099Sfjoe}
95489099Sfjoe
95589099Sfjoevoid
95689099Sfjoecm_reconwatch(arg)
95789099Sfjoe	void *arg;
95889099Sfjoe{
95989099Sfjoe	struct cm_softc *sc = arg;
960147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
96189099Sfjoe
96289099Sfjoe	if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
96389099Sfjoe		sc->sc_reconcount = 0;
964121816Sbrooks		log(LOG_WARNING, "%s: token valid again.\n",
965121816Sbrooks		    ifp->if_xname);
96689099Sfjoe	}
96789099Sfjoe	sc->sc_reconcount = 0;
96889099Sfjoe}
96989099Sfjoe
97089099Sfjoe
97189099Sfjoe/*
97289099Sfjoe * Process an ioctl request.
97389099Sfjoe * This code needs some work - it looks pretty ugly.
97489099Sfjoe */
97589099Sfjoeint
97689099Sfjoecm_ioctl(ifp, command, data)
97789099Sfjoe	struct ifnet *ifp;
97889099Sfjoe	u_long command;
97989099Sfjoe	caddr_t data;
98089099Sfjoe{
98189099Sfjoe	struct cm_softc *sc;
98289099Sfjoe	struct ifaddr *ifa;
98389099Sfjoe	struct ifreq *ifr;
98489099Sfjoe	int s, error;
98589099Sfjoe
98689099Sfjoe	error = 0;
98789099Sfjoe	sc = ifp->if_softc;
98889099Sfjoe	ifa = (struct ifaddr *)data;
98989099Sfjoe	ifr = (struct ifreq *)data;
99089099Sfjoe	s = splimp();
99189099Sfjoe
99289099Sfjoe#if defined(CM_DEBUG) && (CM_DEBUG > 2)
993104251Sbrooks	if_printf(ifp, "ioctl() called, cmd = 0x%lx\n", command);
99489099Sfjoe#endif
99589099Sfjoe
99689099Sfjoe	switch (command) {
99789099Sfjoe	case SIOCSIFADDR:
998109771Sfjoe	case SIOCGIFADDR:
99989099Sfjoe	case SIOCADDMULTI:
100089099Sfjoe	case SIOCDELMULTI:
100189099Sfjoe	case SIOCSIFMTU:
100289099Sfjoe		error = arc_ioctl(ifp, command, data);
100389099Sfjoe		break;
100489099Sfjoe
100589099Sfjoe	case SIOCSIFFLAGS:
100689099Sfjoe		if ((ifp->if_flags & IFF_UP) == 0 &&
100789099Sfjoe		    (ifp->if_flags & IFF_RUNNING) != 0) {
100889099Sfjoe			/*
100989099Sfjoe			 * If interface is marked down and it is running,
101089099Sfjoe			 * then stop it.
101189099Sfjoe			 */
101289099Sfjoe			cm_stop(sc);
101389099Sfjoe			ifp->if_flags &= ~IFF_RUNNING;
101489099Sfjoe		} else if ((ifp->if_flags & IFF_UP) != 0 &&
101589099Sfjoe			   (ifp->if_flags & IFF_RUNNING) == 0) {
101689099Sfjoe			/*
101789099Sfjoe			 * If interface is marked up and it is stopped, then
101889099Sfjoe			 * start it.
101989099Sfjoe			 */
102089099Sfjoe			cm_init(sc);
102189099Sfjoe		}
102289099Sfjoe		break;
102389099Sfjoe
102489099Sfjoe	default:
102589099Sfjoe		error = EINVAL;
102689099Sfjoe		break;
102789099Sfjoe	}
102889099Sfjoe
102989099Sfjoe	splx(s);
103089099Sfjoe	return (error);
103189099Sfjoe}
103289099Sfjoe
103389099Sfjoe/*
103489099Sfjoe * watchdog routine for transmitter.
103589099Sfjoe *
103689099Sfjoe * We need this, because else a receiver whose hardware is alive, but whose
103789099Sfjoe * software has not enabled the Receiver, would make our hardware wait forever
103889099Sfjoe * Discovered this after 20 times reading the docs.
103989099Sfjoe *
1040108470Sschweikh * Only thing we do is disable transmitter. We'll get a transmit timeout,
104189099Sfjoe * and the int handler will have to decide not to retransmit (in case
104289099Sfjoe * retransmission is implemented).
104389099Sfjoe *
104489099Sfjoe * This one assumes being called inside splimp()
104589099Sfjoe */
104689099Sfjoe
104789099Sfjoevoid
104889099Sfjoecm_watchdog(ifp)
104989099Sfjoe	struct ifnet *ifp;
105089099Sfjoe{
105189099Sfjoe	struct cm_softc *sc = ifp->if_softc;
105289099Sfjoe
105389099Sfjoe	PUTREG(CMCMD, CM_TXDIS);
105489099Sfjoe	return;
105589099Sfjoe}
1056