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