xref: /csrg-svn/sys/i386/isa/if_apx.c (revision 52849)
150824Ssklower /*
250824Ssklower  * Copyright (c) 1982, 1990 The Regents of the University of California.
350824Ssklower  * All rights reserved.
450824Ssklower  *
550824Ssklower  * %sccs.include.redist.c%
650824Ssklower  *
7*52849Ssklower  *	@(#)if_apx.c	7.8 (Berkeley) 03/05/92
850824Ssklower  */
950824Ssklower 
1050824Ssklower /*
1150824Ssklower  * Driver for SGS-THOMSON MK5025 based Link level controller.
1250824Ssklower  * The chip will do LAPB in hardware, although this driver only
1350824Ssklower  * attempts to use it for HDLC framing.
1450824Ssklower  *
1550824Ssklower  * Driver written by Keith Sklower, based on lance AMD7990
1650824Ssklower  * driver by Van Jacobsen, and information graciously supplied
1750824Ssklower  * by the ADAX corporation of Berkeley, CA.
1850824Ssklower  */
1950824Ssklower 
2050824Ssklower #include "apx.h"
2150824Ssklower #if NAPX > 0
2250824Ssklower 
2350824Ssklower #include "param.h"
2450824Ssklower #include "mbuf.h"
2550824Ssklower #include "socket.h"
2650824Ssklower #include "ioctl.h"
2750824Ssklower #include "errno.h"
2850824Ssklower #include "syslog.h"
2950824Ssklower 
3050824Ssklower #include "net/if.h"
3150824Ssklower #include "net/netisr.h"
3250825Ssklower #include "net/if_types.h"
3352828Ssklower #ifdef CCITT
3450824Ssklower #include "netccitt/x25.h"
3552828Ssklower int x25_rtrequest(), x25_ifoutput();
3652828Ssklower #endif
3750824Ssklower 
3850825Ssklower #include "if_apxreg.h"
3950824Ssklower 
4050824Ssklower int	apxprobe(), apxattach(), apxstart(), apx_uprim(), apx_meminit();
41*52849Ssklower int	apxinit(), apxoutput(), apxioctl(), apxreset(), apxdebug = 0;
4252828Ssklower void	apx_ifattach(), apxtest(), apxinput(), apxintr(), apxtint(), apxrint();
4350824Ssklower 
4450824Ssklower struct apx_softc {
4550824Ssklower 	struct	ifnet apx_if;
4652828Ssklower 	caddr_t	apx_device;		/* e.g. isa_device, vme_device, etc. */
4750824Ssklower 	struct	apc_reg *apx_reg;	/* control regs for both subunits */
4850824Ssklower 	struct	apc_mem *apx_hmem;	/* Host addr for shared memory */
4950824Ssklower 	struct	apc_mem *apx_dmem;	/* Device (chip) addr for shared mem */
5050824Ssklower 	struct	sgcp *apx_sgcp;		/* IO control port for this subunit */
5152828Ssklower 	int	apx_flags;		/* Flags specific to this driver */
5252828Ssklower #define APXF_CHIPHERE	0x01		/* mk5025 present */
5350824Ssklower 	int	apx_rxnum;		/* Last receiver dx we looked at */
5450824Ssklower 	int	apx_txnum;		/* Last tranmistter dx we stomped on */
5550824Ssklower 	int	apx_txcnt;		/* Number of packets queued for tx*/
5652848Ssklower 	u_int	apx_msize;
5752842Ssklower 	struct	sgae apx_csr23;		/* 24 bit init addr, as seen by chip */
5852828Ssklower 	u_short	apx_csr4;		/* byte gender, set in mach dep code */
5952828Ssklower 	struct	apc_modes apx_modes;	/* Parameters, as amended by ioctls */
6052828Ssklower } apx_softc[2 * NAPX];
6150824Ssklower 
6250824Ssklower struct apxstat {
6352842Ssklower 	int	rxnull;			/* no rx bufs ready this interrupt */
6452842Ssklower 	int	rxnrdy;			/* expected rx buf not ready */
6552842Ssklower 	int	rx2big;			/* expected rx buf not ready */
6652842Ssklower 	int	txnull;
6752842Ssklower 	int	pint;			/* new primitive available interrupt */
6852842Ssklower 	int	rint;			/* receive interrupts */
6952842Ssklower 	int	tint;			/* transmit interrupts */
7052842Ssklower 	int	anyint;			/* note all interrupts */
7152842Ssklower 	int	queued;			/* got through apxinput */
7252842Ssklower 	int	nxpctd;			/* received while if was down */
7352848Ssklower 	int	rstfld;			/* reset didn't work */
7450825Ssklower } apxstat;
7550824Ssklower 
7650824Ssklower /* default operating paramters for devices */
7750824Ssklower struct	apc_modes apx_default_modes = {
7850824Ssklower  { 1,		/* apm_sgob.lsaddr; */
7950824Ssklower    3,		/* apm_sgob.rsaddr; */
8050824Ssklower    -SGMTU,	/* apm_sgob.n1; */
8150824Ssklower    ((-10)<<8),	/* apm_sgob.n2_scale; */
8250824Ssklower    -1250,	/* apm_sgob.t1; */
8350824Ssklower    -10000,	/* apm_sgob.t3; */
8450824Ssklower    -80,		/* apm_sgob.tp; */
8550824Ssklower  },
8650824Ssklower  2,		/* apm_txwin; */
8752842Ssklower  1,		/* apm_apxmode: RS_232 connector and modem clock; */
8852842Ssklower  0,		/* apm_apxaltmode: enable dtr, disable X.21 connector; */
8950824Ssklower  IFT_X25,	/* apm_iftype; */
9050824Ssklower };
9150824Ssklower 
9250824Ssklower /* Begin bus & endian dependence */
9350824Ssklower 
9450825Ssklower #include "isa_device.h"
9550824Ssklower 
9650824Ssklower struct	isa_driver apxdriver = {
9750824Ssklower 	apxprobe, apxattach, "apx",
9850824Ssklower };
9950824Ssklower 
10050824Ssklower #define SG_RCSR(apx, csrnum) \
10150825Ssklower 	 (outw(&(apx->apx_sgcp->sgcp_rap), csrnum << 1), \
10250825Ssklower 	  inw(&(apx->apx_sgcp->sgcp_rdp)))
10350824Ssklower 
10450824Ssklower #define SG_WCSR(apx, csrnum, data) \
10550825Ssklower 	 (outw(&(apx->apx_sgcp->sgcp_rap), csrnum << 1), \
10650824Ssklower 	  outw(&(apx->apx_sgcp->sgcp_rdp), data))
10750824Ssklower 
10850824Ssklower #define APX_RCSR(apx, csrname) inb(&(apx->apx_reg->csrname))
10950824Ssklower #define APX_WCSR(apx, csrname, data) outb(&(apx->apx_reg->csrname), data)
11050824Ssklower 
11150824Ssklower #define TIMO 10000 /* used in apx_uprim */
11250824Ssklower 
11350824Ssklower apxprobe(id)
11450824Ssklower 	register struct	isa_device *id;
11550824Ssklower {
11652848Ssklower 	int	moffset = 0, subunit, unit = id->id_unit << 1;
11750825Ssklower 	struct	apc_reg *reg = (struct apc_reg *)id->id_iobase;
11850824Ssklower 	register struct	apx_softc *apx = apx_softc + unit;
11950824Ssklower 
12052830Ssklower 	for (subunit = 0; subunit < 2; subunit++) {
12152848Ssklower 		apx->apx_msize	= id->id_msize >> 1;
12250824Ssklower 		apx->apx_hmem	= (struct apc_mem *) (id->id_maddr + moffset);
12352848Ssklower 		apx->apx_dmem	= (struct apc_mem *) moffset;
124*52849Ssklower 		apx->apx_device	= (caddr_t) id;
12550824Ssklower 		apx->apx_reg	= reg;
12652828Ssklower 		apx->apx_sgcp	= reg->axr_sgcp + subunit;
12752828Ssklower 		apx->apx_csr4	= 0x0210;	/* no byte swapping for PC-AT */
12852828Ssklower 		apx->apx_modes	= apx_default_modes;
12952828Ssklower 		apx->apx_if.if_unit = unit++;
13052830Ssklower 		apxtest(apx++);
13152848Ssklower 		moffset = apx->apx_msize;
13250824Ssklower 	}
13350824Ssklower 	return 1;
13450824Ssklower }
13550824Ssklower 
13650824Ssklower apxattach(id)
13752830Ssklower 	struct	isa_device *id;
13850824Ssklower {
13952830Ssklower 	register struct	apx_softc *apx = apx_softc + (id->id_unit << 1);
14050824Ssklower 
14152830Ssklower 	apx_ifattach(&((apx++)->apx_if));
14252830Ssklower 	apx_ifattach(&(apx->apx_if));
14352828Ssklower 	return 0;
14450824Ssklower }
14550824Ssklower /* End bus & endian dependence */
14650824Ssklower 
14750824Ssklower /*
14850824Ssklower  * Interface exists: make available by filling in network interface
14950824Ssklower  * record.  System will initialize the interface when it is ready
15050824Ssklower  * to accept packets.
15150824Ssklower  */
15250825Ssklower void
15352830Ssklower apx_ifattach(ifp)
15452830Ssklower 	register struct ifnet *ifp;
15550824Ssklower {
15650824Ssklower 	/*
15750824Ssklower 	 * Initialize ifnet structure
15850824Ssklower 	 */
15952828Ssklower 	ifp->if_name	= "apc";
16052828Ssklower 	ifp->if_mtu	= SGMTU;
16152828Ssklower 	ifp->if_init	= apxinit;
16252828Ssklower 	ifp->if_output	= apxoutput;
16352828Ssklower 	ifp->if_start	= apxstart;
16452828Ssklower 	ifp->if_ioctl	= apxioctl;
16552828Ssklower 	ifp->if_reset	= apxreset;
16652828Ssklower 	ifp->if_type	= apx_default_modes.apm_iftype;
16752828Ssklower 	ifp->if_hdrlen	= 5;
16852828Ssklower 	ifp->if_addrlen	= 8;
16950824Ssklower 	if_attach(ifp);
17050824Ssklower }
17150824Ssklower /*
17250824Ssklower  * Initialization of interface
17350824Ssklower  */
17450824Ssklower apxinit(unit)
17550824Ssklower 	int unit;
17650824Ssklower {
17750824Ssklower 	struct ifnet *ifp = &apx_softc[unit].apx_if;
17850824Ssklower 	int s = splimp();
17950824Ssklower 
18050824Ssklower 	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
18150824Ssklower 	if (apxreset(unit) && (ifp->if_flags & IFF_UP)) {
18250824Ssklower 		ifp->if_flags |= IFF_RUNNING;
18350824Ssklower 		(void)apxstart(ifp);
18450824Ssklower 	}
18550824Ssklower 	splx(s);
18650824Ssklower 	return 0;
18750824Ssklower }
18850824Ssklower 
18952672Ssklower apxctr(apx)
19052672Ssklower 	register struct apx_softc *apx;
19152672Ssklower {
19252828Ssklower 	APX_WCSR(apx, axr_ccr, 0xB0); /* select ctr 2, write lsb+msb, mode 0 */
19352672Ssklower 	APX_WCSR(apx, axr_cnt2, 0x1);
19452672Ssklower 	APX_WCSR(apx, axr_cnt2, 0x0);
19552672Ssklower 	DELAY(50);
19652828Ssklower 	APX_WCSR(apx, axr_ccr, 0xE8); /* latch status, ctr 2; */
19752828Ssklower 	return (APX_RCSR(apx, axr_cnt2));
19852672Ssklower }
19952672Ssklower 
20052828Ssklower void
20152828Ssklower apxtest(apx)
20252828Ssklower 	register struct apx_softc *apx;
20352828Ssklower {
20452828Ssklower 	int i =  0;
20552828Ssklower 
20652828Ssklower 	if ((apx->apx_if.if_unit & 1) == 0 && (i = apxctr(apx)) == 0)
20752828Ssklower 		apxerror(apx, "no response from timer chip", 0);
20852828Ssklower 	if (SG_RCSR(apx, 1) & 0x8000)
20952828Ssklower 		SG_WCSR(apx, 1, 0x8040);
21052828Ssklower 	SG_WCSR(apx, 4, apx->apx_csr4);
21152828Ssklower 	SG_WCSR(apx, 5, 0x08);		/* Set DTR mode in SGS thompson chip */
21252828Ssklower 	if (((i = SG_RCSR(apx, 5)) & 0xff08) != 0x08)
21352828Ssklower 		apxerror(apx, "no mk5025, csr5 high bits are", i);
21452828Ssklower 	else
21552828Ssklower 		apx->apx_flags |= APXF_CHIPHERE;
21652842Ssklower 	(void) apx_uprim(apx, SG_STOP, "stop after probing");
21752828Ssklower }
21852828Ssklower 
21950824Ssklower apxreset(unit)
22050824Ssklower 	int	unit;
22150824Ssklower {
22250824Ssklower 	register struct apx_softc *apx = &apx_softc[unit ^ 1];
22350824Ssklower 	u_char apm_apxmode = 0, apm_apxaltmode = 0;
22450824Ssklower #define MODE(m) (m |= apx->apx_modes.m << ((apx->apx_if.if_unit & 1) ? 1 : 0))
22550824Ssklower 
22650824Ssklower 	MODE(apm_apxmode);
22750824Ssklower 	MODE(apm_apxaltmode);
22850824Ssklower 	apx = apx_softc + unit;
22950824Ssklower 	MODE(apm_apxmode);
23050824Ssklower 	MODE(apm_apxaltmode);
23150824Ssklower 	APX_WCSR(apx, axr_mode, apm_apxmode);
23250824Ssklower 	APX_WCSR(apx, axr_altmode, apm_apxaltmode);
23352842Ssklower 	(void) apxctr(apx);
23452842Ssklower 	(void) apx_uprim(apx, SG_STOP, "stop to reset");
23552842Ssklower 	if ((apx->apx_if.if_flags & IFF_UP) == 0)
23650824Ssklower 		return 0;
23752842Ssklower 	apx_meminit(apx->apx_hmem, apx);
23850824Ssklower 	SG_WCSR(apx, 4, apx->apx_csr4);
23952842Ssklower 	SG_WCSR(apx, 2, apx->apx_csr23.f_hi);
24052842Ssklower 	SG_WCSR(apx, 3, apx->apx_csr23.lo);
24150825Ssklower 	if (apx_uprim(apx, SG_INIT, "init request") ||
24250824Ssklower 	    apx_uprim(apx, SG_STAT, "status request") ||
243*52849Ssklower 	    apx_uprim(apx, SG_TRANS, "transparent mode"))
24450824Ssklower 		return 0;
24550824Ssklower 	SG_WCSR(apx, 0, SG_INEA);
24650825Ssklower 	return 1;
24750824Ssklower }
24850824Ssklower 
24950824Ssklower apx_uprim(apx, request, ident)
25052830Ssklower 	register struct apx_softc *apx;
25150824Ssklower 	char *ident;
25250824Ssklower {
25350824Ssklower 	register int timo = 0;
25452830Ssklower 	int reply;
25550824Ssklower 
25652830Ssklower 	if ((apx->apx_flags & APXF_CHIPHERE) == 0)
25752830Ssklower 		return 1;	/* maybe even should panic . . . */
25852830Ssklower 	if ((reply = SG_RCSR(apx, 1)) & 0x8040)
25952828Ssklower 		SG_WCSR(apx, 1, 0x8040); /* Magic! */
260*52849Ssklower 	if (request == SG_STOP && (SG_RCSR(apx, 0) & SG_STOPPED))
26152848Ssklower 		return 0;
26250824Ssklower 	SG_WCSR(apx, 1, request | SG_UAV);
26350824Ssklower 	do {
264*52849Ssklower 		reply = SG_RCSR(apx, 1);
26552842Ssklower 		if (timo++ >= TIMO || (reply & 0x8000)) {
26652842Ssklower 				apxerror(apx, ident, reply);
26752842Ssklower 				return 1;
26850824Ssklower 		}
26950824Ssklower 	} while (reply & SG_UAV);
27050824Ssklower 	return 0;
27150824Ssklower }
27250824Ssklower 
27350824Ssklower apx_meminit(apc, apx)
27450824Ssklower 	register struct apc_mem *apc;
27550824Ssklower 	struct apx_softc *apx;
27650824Ssklower {
27750824Ssklower 	register struct apc_mem *apcbase = apx->apx_dmem;
27850824Ssklower 	register int i;
27950825Ssklower #define LOWADDR(e) (((u_long)&(apcbase->e)) & 0xffff)
28050825Ssklower #define HIADDR(e) ((((u_long)&(apcbase->e)) >> 16) & 0xff)
28152842Ssklower #define SET_SGAE(d, f, a) {(d).lo = LOWADDR(a); (d).f_hi = (f) | HIADDR(a);}
28252842Ssklower #define SET_SGDX(d, f, a, b) \
28352842Ssklower 	{SET_SGAE((d).sgdx_ae, f, a); (d).sgdx_mcnt = (d).sgdx_bcnt = (b);}
28450824Ssklower 
28552842Ssklower 	apx->apx_txnum = apx->apx_rxnum = apx->apx_txcnt = 0;
286*52849Ssklower 	bzero((caddr_t)apc, ((caddr_t)(&apc->apc_rxmd[0])) - (caddr_t)apc);
287*52849Ssklower 	apc->apc_mode = 0x0108;	/* 2 flag spacing, leave addr & ctl, do CRC16 */
28850824Ssklower 	apc->apc_sgop = apx->apx_modes.apm_sgop;
28952842Ssklower 	SET_SGAE(apx->apx_csr23, SG_UIE | SG_PROM, apc_mode);
29052842Ssklower 	SET_SGAE(apc->apc_rxdd, SG_RLEN, apc_rxmd[0]);
29152848Ssklower 	i = SG_TLEN | ((apx->apx_modes.apm_txwin)<< 8);
29252848Ssklower 	SET_SGAE(apc->apc_txdd, i, apc_txmd[0]);
29352842Ssklower 	SET_SGAE(apc->apc_stdd, 0, apc_sgsb);
29452842Ssklower 	SET_SGDX(apc->apc_rxtid, SG_OWN, apc_rxidbuf[0], -SGMTU);
29552842Ssklower 	SET_SGDX(apc->apc_txtid, 0, apc_txidbuf[0], 0);
29650824Ssklower 	for (i = 0; i < SGRBUF; i++)
29752842Ssklower 		 SET_SGDX(apc->apc_rxmd[i], SG_OWN, apc_rbuf[i][0], -SGMTU)
29850824Ssklower 	for (i = 0; i < SGTBUF; i++)
29952848Ssklower 		 SET_SGDX(apc->apc_txmd[i], 0, apc_tbuf[i][0], 0)
30050824Ssklower }
30150824Ssklower 
30250824Ssklower /*
30350824Ssklower  * Start output on interface.  Get another datagram to send
30450824Ssklower  * off of the interface queue, and copy it to the interface
30550824Ssklower  * before starting the output.
30650824Ssklower  */
30750824Ssklower apxstart(ifp)
30850824Ssklower 	struct ifnet *ifp;
30950824Ssklower {
31050824Ssklower 	register struct apx_softc *apx = &apx_softc[ifp->if_unit];
31150824Ssklower 	register struct sgdx *dx;
31250825Ssklower 	struct apc_mem *apc = apx->apx_hmem;
31350824Ssklower 	struct mbuf *m;
31450824Ssklower 	int len;
31550824Ssklower 
31650824Ssklower 	if ((ifp->if_flags & IFF_RUNNING) == 0)
31750824Ssklower 		return (0);
31850824Ssklower 	do {
31950825Ssklower 		dx = apc->apc_txmd + apx->apx_txnum;
32050824Ssklower 		if (dx->sgdx_flags & SG_OWN)
32150824Ssklower 			return (0);
32250824Ssklower 		IF_DEQUEUE(&ifp->if_snd, m);
32350824Ssklower 		if (m == 0)
32450824Ssklower 			return (0);
32550824Ssklower 		len = min(m->m_pkthdr.len, SGMTU);
32650825Ssklower 		m_copydata(m, 0, len, apc->apc_tbuf[apx->apx_txnum]);
32750824Ssklower 		dx->sgdx_mcnt = -len;
32852842Ssklower 		dx->sgdx_flags = (SG_OWN|SG_TUI|SG_SLF|SG_ELF) |
32952842Ssklower 			(0xff & dx->sgdx_flags);
33050824Ssklower 		SG_WCSR(apx, 0, SG_INEA | SG_TDMD);
33152848Ssklower 		DELAY(20);
33250824Ssklower 		if (++apx->apx_txnum >= SGTBUF)
33350824Ssklower 			apx->apx_txnum = 0;
33450824Ssklower 	} while (++apx->apx_txcnt < SGTBUF);
33552830Ssklower 	apx->apx_txcnt = SGTBUF; /* in case txcnt > SGTBUF by mistake */
33650824Ssklower 	ifp->if_flags |= IFF_OACTIVE;
33750824Ssklower 	return (0);
33850824Ssklower }
33950824Ssklower 
34050825Ssklower void
34150824Ssklower apxintr()
34250824Ssklower {
34352828Ssklower 	register struct apx_softc *apx;
34450824Ssklower 	int reply;
34550824Ssklower 
34652842Ssklower 	apxstat.anyint++;
34752828Ssklower 	for (apx = apx_softc + NAPX + NAPX; --apx >= apx_softc;) {
34852828Ssklower 		if (apx->apx_flags & APXF_CHIPHERE)
34950824Ssklower 		    /* Try to turn off interrupt cause */
350*52849Ssklower 		    while ((reply = SG_RCSR(apx, 0)) & 0xff) {
35150824Ssklower 			SG_WCSR(apx, 0, SG_INEA | 0xfe);
35250824Ssklower 			if (reply & (SG_MERR|SG_TUR|SG_ROR)) {
35350824Ssklower 				apxerror(apx, "mem, rx, or tx error", reply);
35450824Ssklower 				apxinit(apx->apx_if.if_unit);
35550824Ssklower 				break;
35650824Ssklower 			}
35750824Ssklower 			if (reply & SG_RINT)
35850824Ssklower 				apxrint(apx);
35950824Ssklower 			if (reply & SG_TINT)
36050824Ssklower 				apxtint(apx);
36150824Ssklower 			if (reply & SG_PINT)
36250824Ssklower 				apxstat.pint++;
36350824Ssklower 		}
36452828Ssklower 	}
36550824Ssklower }
36650824Ssklower 
36750825Ssklower void
36850824Ssklower apxtint(apx)
36950824Ssklower 	register struct apx_softc *apx;
37050824Ssklower {
37150825Ssklower 	register struct apc_mem *apc = apx->apx_hmem;
37250824Ssklower 	int i, loopcount = 0;
37350824Ssklower 
37452842Ssklower 	apxstat.tint++;
37550824Ssklower 	do {
37650824Ssklower 		if ((i = apx->apx_txnum - apx->apx_txcnt) < 0)
37750824Ssklower 			i += SGTBUF;
37850824Ssklower 		if (apc->apc_txmd[i].sgdx_flags & SG_OWN) {
37950824Ssklower 			if (loopcount)
38050824Ssklower 				break;
38152842Ssklower 			apxstat.txnull++;
38250824Ssklower 			return;
38350824Ssklower 		}
38450824Ssklower 		loopcount++;
38550824Ssklower 		apx->apx_if.if_flags &= ~IFF_OACTIVE;
38650824Ssklower 	} while (--apx->apx_txcnt > 0);
38750824Ssklower 	apxstart(&apx->apx_if);
38850824Ssklower }
38950824Ssklower 
39052828Ssklower void
39150824Ssklower apxrint(apx)
39250824Ssklower 	register struct apx_softc *apx;
39350824Ssklower {
39450825Ssklower 	register struct apc_mem *apc = apx->apx_hmem;
39550824Ssklower 	register struct sgdx *dx = apc->apc_rxmd + apx->apx_rxnum;
39652842Ssklower 	int i = 0;
39750824Ssklower #define SGNEXTRXMD \
39850824Ssklower dx = ++apx->apx_rxnum == SGRBUF ? &apc->apc_rxmd[apx->apx_rxnum = 0] : dx + 1;
39950824Ssklower 
40052842Ssklower 	apxstat.rint++;
40150824Ssklower 	/*
40250824Ssklower 	 * Out of sync with hardware, should never happen?
40350824Ssklower 	 */
40452842Ssklower 	while (dx->sgdx_flags & SG_OWN) {
40552842Ssklower 		apxstat.rxnrdy++;
40652842Ssklower 		if (++i == SGRBUF) {
40752842Ssklower 			apxstat.rxnull++;
40852842Ssklower 			return;
40952842Ssklower 		}
41052842Ssklower 		SGNEXTRXMD;
41150824Ssklower 	}
41250824Ssklower 	/*
41350824Ssklower 	 * Process all buffers with valid data
41450824Ssklower 	 */
41550824Ssklower 	while ((dx->sgdx_flags & SG_OWN) == 0) {
41650824Ssklower 		if ((dx->sgdx_flags & (SG_SLF|SG_ELF)) != (SG_SLF|SG_ELF)) {
41750824Ssklower 			/*
41852842Ssklower 			 * Find the end of the packet so we synch up.
41952842Ssklower 			 * We throw the data away.
42050824Ssklower 			 */
42150825Ssklower 			apxerror(apx, "chained buffer", dx->sgdx_flags);
42250824Ssklower 			do {
42352842Ssklower 				apxstat.rx2big++;
42450824Ssklower 				dx->sgdx_bcnt = 0;
42550824Ssklower 				dx->sgdx_flags = SG_OWN | (0xff&dx->sgdx_flags);
42650824Ssklower 				SGNEXTRXMD;
42750824Ssklower 			} while (!(dx->sgdx_flags & (SG_OWN|SG_SLF|SG_ELF)));
42850824Ssklower 			/*
42950824Ssklower 			 * If search terminated without successful completion
43050824Ssklower 			 * we reset the hardware (conservative).
43150824Ssklower 			 */
43250824Ssklower 			if ((dx->sgdx_flags & (SG_OWN|SG_SLF|SG_ELF)) !=
43350825Ssklower 			    SG_ELF) {
43450824Ssklower 				apxreset(apx->apx_if.if_unit);
43550824Ssklower 				return;
43650824Ssklower 			}
43750824Ssklower 		} else
43850824Ssklower 			apxinput(&apx->apx_if, apc->apc_rbuf[apx->apx_rxnum],
43952842Ssklower 					-dx->sgdx_mcnt);
44050824Ssklower 		dx->sgdx_bcnt = 0;
44150824Ssklower 		dx->sgdx_flags = SG_OWN | (0xff & dx->sgdx_flags);
44250824Ssklower 		SGNEXTRXMD;
44350824Ssklower 	}
44450824Ssklower }
44550824Ssklower 
44650825Ssklower void
44750824Ssklower apxinput(ifp, buffer, len)
44852830Ssklower 	register struct ifnet *ifp;
44952830Ssklower 	caddr_t buffer;
45050824Ssklower {
45152830Ssklower 	extern struct ifqueue hdintrq, ipintrq;
45250824Ssklower 	register struct ifqueue *inq;
45352830Ssklower 	register u_char *cp = (u_char *)buffer;
45452672Ssklower 	struct mbuf *m, *m_devget();
45550824Ssklower 	int isr;
45650824Ssklower 
45750824Ssklower 	ifp->if_ipackets++;
45852842Ssklower 	if ((ifp->if_flags & IFF_UP) == 0) {
45952842Ssklower 		apxstat.nxpctd++;
46052842Ssklower 		return;
46152842Ssklower 	}
46250824Ssklower 	if (cp[0] == 0xff && cp[1] == 0x3) {
46350824Ssklower 		/* This is a UI HDLC Packet, so we'll assume PPP
46450824Ssklower 		   protocol.  for now, IP only. */
46550824Ssklower 		buffer += 4;
46650824Ssklower 		len -= 4;
46750824Ssklower 		inq = &ipintrq;
46850824Ssklower 		isr = NETISR_IP;
46950824Ssklower 	} else {
47052830Ssklower #ifdef CCITT
47150824Ssklower 		inq = &hdintrq;
47250824Ssklower 		isr = NETISR_CCITT;
47350824Ssklower 	}
47452830Ssklower 	if (len <= 0) {
47552830Ssklower #endif
47650824Ssklower 		return;
47752830Ssklower 	}
47852672Ssklower 	m = m_devget(buffer, len, 0, ifp, (void (*)())0);
47950824Ssklower 	if (m == 0)
48050824Ssklower 		return;
48150824Ssklower 	if(IF_QFULL(inq)) {
48250824Ssklower 		IF_DROP(inq);
48350824Ssklower 		m_freem(m);
48450824Ssklower 	} else {
48552842Ssklower 		apxstat.queued++;
48650824Ssklower 		IF_ENQUEUE(inq, m);
48750824Ssklower 		schednetisr(isr);
48850824Ssklower 	}
48950824Ssklower }
49050824Ssklower 
49150824Ssklower /*
49250824Ssklower  * Process an ioctl request.
49350824Ssklower  */
49450824Ssklower apxioctl(ifp, cmd, data)
49550824Ssklower 	register struct ifnet *ifp;
49650824Ssklower 	int cmd;
49750824Ssklower 	caddr_t data;
49850824Ssklower {
49950824Ssklower 	register struct ifaddr *ifa = (struct ifaddr *)data;
50050824Ssklower 	int s = splimp(), error = 0;
50150824Ssklower 	struct apx_softc *apx = &apx_softc[ifp->if_unit];
50250824Ssklower 
50350824Ssklower 	switch (cmd) {
50452828Ssklower 
50552828Ssklower 	case SIOCSIFADDR:
50652828Ssklower #ifdef CCITT
507*52849Ssklower 		ifa->ifa_rtrequest = x25_rtrequest;
50852828Ssklower 		break;
50952828Ssklower 
51050824Ssklower 	case SIOCSIFCONF_X25:
51152828Ssklower 		ifp->if_output = x25_ifoutput;
51250824Ssklower 		ifp->if_flags |= IFF_UP;
51350824Ssklower 		error = hd_ctlinput(PRC_IFUP, ifa->ifa_addr);
51450824Ssklower 		if (error == 0)
51550824Ssklower 			apxinit(ifp->if_unit);
51652828Ssklower #endif
51750824Ssklower 		break;
51850824Ssklower 
51950824Ssklower 	case SIOCSIFFLAGS:
52050824Ssklower 		if (((ifp->if_flags & IFF_UP) == 0 &&
52150824Ssklower 		     (ifp->if_flags & IFF_RUNNING)) ||
52250824Ssklower 		    (ifp->if_flags & IFF_UP) &&
52350824Ssklower 		     (ifp->if_flags & IFF_RUNNING) == 0)
52450824Ssklower 			apxinit(ifp->if_unit);
52550824Ssklower 		break;
52650824Ssklower 
52750824Ssklower 	case SIOCSIFMODE:
52850824Ssklower 		if ((ifp->if_flags & IFF_UP) == 0)
52950825Ssklower 			apx->apx_modes = *(struct apc_modes *)data;
53050824Ssklower 		else
53150824Ssklower 	default:
53250824Ssklower 			error = EINVAL;
53350824Ssklower 
53450824Ssklower 	}
53550824Ssklower 	splx(s);
53650824Ssklower 	return (error);
53750824Ssklower }
53850824Ssklower 
53950824Ssklower apxerror(apx, msg, data)
54050824Ssklower 	register struct	apx_softc *apx;
54150824Ssklower 	char	*msg;
54250824Ssklower {
54350824Ssklower 	log(LOG_WARNING, "apc%d: %s, stat=0x%x\n",
54450824Ssklower 		apx->apx_if.if_unit, msg, data);
54550824Ssklower }
546*52849Ssklower 
54752828Ssklower /*
54852828Ssklower  * For debugging loopback activity.
54952828Ssklower  */
55052828Ssklower apxoutput(ifp, m, dst, rt)
55152828Ssklower register struct ifnet *ifp;
55252828Ssklower register struct mbuf *m;
55352828Ssklower struct sockaddr *dst;
55452828Ssklower struct rtentry *rt;
55552828Ssklower {
556*52849Ssklower 	int s = splimp(), error = 0;
557*52849Ssklower 	static char pppheader[4] = { -1, 3, 0, 0x21 };
55852828Ssklower 	/*
55952828Ssklower 	 * Queue message on interface, and start output if interface
56052828Ssklower 	 * not yet active.
56152828Ssklower 	 */
562*52849Ssklower 	ifp->if_opackets++;
56352828Ssklower 	M_PREPEND(m, sizeof pppheader, M_DONTWAIT);
56452828Ssklower 	if (m == 0) {
56552828Ssklower 		splx(s);
56652828Ssklower 		return ENOBUFS;
56752828Ssklower 	}
56852828Ssklower 	bcopy(pppheader, mtod(m, caddr_t), sizeof pppheader);
56952828Ssklower 	if (IF_QFULL(&ifp->if_snd)) {
57052828Ssklower 		IF_DROP(&ifp->if_snd);
57152828Ssklower 		m_freem(m);
57252828Ssklower 		error = ENOBUFS;
57352828Ssklower 	} else {
57452828Ssklower 		IF_ENQUEUE(&ifp->if_snd, m);
57552828Ssklower 		if ((ifp->if_flags & IFF_OACTIVE) == 0)
57652828Ssklower 			(*ifp->if_start)(ifp);
57752828Ssklower 	}
57852828Ssklower 	splx(s);
57952828Ssklower 	return (error);
58052828Ssklower }
58150824Ssklower #endif /* NAPX */
582