xref: /csrg-svn/sys/tahoe/vba/mp.c (revision 35055)
134506Skarels /*
234506Skarels  * Copyright (c) 1988 Regents of the University of California.
334506Skarels  * All rights reserved.
434506Skarels  *
5*35055Skarels  * This code is derived from software contributed to Berkeley by
6*35055Skarels  * Computer Consoles Inc.
7*35055Skarels  *
834506Skarels  * Redistribution and use in source and binary forms are permitted
934866Sbostic  * provided that the above copyright notice and this paragraph are
1034866Sbostic  * duplicated in all such forms and that any documentation,
1134866Sbostic  * advertising materials, and other materials related to such
1234866Sbostic  * distribution and use acknowledge that the software was developed
1334866Sbostic  * by the University of California, Berkeley.  The name of the
1434866Sbostic  * University may not be used to endorse or promote products derived
1534866Sbostic  * from this software without specific prior written permission.
1634866Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1734866Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1834866Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1934506Skarels  *
20*35055Skarels  *	@(#)mp.c	7.6 (Berkeley) 07/09/88
2134506Skarels  */
2232633Ssam 
2332633Ssam #include "mp.h"
2432633Ssam #if NMP > 0
2532633Ssam /*
2632633Ssam  * Multi Protocol Communications Controller (MPCC).
2732633Ssam  * Asynchronous Terminal Protocol Support.
2832633Ssam  */
2932633Ssam #include "param.h"
3032633Ssam #include "ioctl.h"
3132633Ssam #include "tty.h"
3232633Ssam #include "dir.h"
3332633Ssam #include "user.h"
3432633Ssam #include "map.h"
3532633Ssam #include "buf.h"
3632633Ssam #include "conf.h"
3732633Ssam #include "file.h"
3832633Ssam #include "uio.h"
3932633Ssam #include "errno.h"
4032633Ssam #include "syslog.h"
4132633Ssam #include "vmmac.h"
4232633Ssam #include "kernel.h"
4332633Ssam #include "clist.h"
4432633Ssam 
4534506Skarels #include "../machine/pte.h"
4634506Skarels #include "../machine/mtpr.h"
4734506Skarels 
4832633Ssam #include "../tahoevba/vbavar.h"
4932633Ssam #include "../tahoevba/mpreg.h"
5032633Ssam 
5132633Ssam #define	MPCHUNK	16
5232633Ssam #define	MPPORT(n)	((n) & 0xf)
5332633Ssam #define	MPUNIT(n)	((n) >> 4)
5432633Ssam 
5532633Ssam /*
5632633Ssam  * Driver information for auto-configuration stuff.
5732633Ssam  */
5832633Ssam int     mpprobe(), mpattach(), mpintr();
5932633Ssam struct  vba_device *mpinfo[NMP];
6032633Ssam long    mpstd[] = { 0 };
6132633Ssam struct  vba_driver mpdriver =
6232633Ssam     { mpprobe, 0, mpattach, 0, mpstd, "mp", mpinfo };
6332633Ssam 
6432633Ssam int	mpstart();
6532633Ssam struct	mpevent *mpparam();
6632633Ssam struct	mpevent *mp_getevent();
6732633Ssam 
6832633Ssam /*
6932633Ssam  * The following structure is needed to deal with mpcc's convoluted
7032633Ssam  * method for locating it's mblok structures (hold your stomach).
7132633Ssam  * When an mpcc is reset at boot time it searches host memory
7232633Ssam  * looking for a string that says ``ThIs Is MpCc''.  The mpcc
7332633Ssam  * then reads the structure to locate the pointer to it's mblok
7432633Ssam  * structure (you can wretch now).
7532633Ssam  */
7632633Ssam struct mpbogus {
7732633Ssam 	char	s[12];			/* `ThIs Is MpCc'' */
7832633Ssam 	u_char	status;
7932633Ssam 	u_char	unused;
8032633Ssam 	u_short	magic;
8132633Ssam 	struct	mblok *mb;
8232633Ssam 	struct	mblok *mbloks[NMP];	/* can support at most 16 mpcc's */
8332633Ssam } mpbogus = { 'T','h','I','s',' ','I','s',' ','M','p','C','c' };
8432633Ssam 
8532633Ssam /*
8632633Ssam  * Software state per unit.
8732633Ssam  */
8832633Ssam struct	mpsoftc {
8932633Ssam 	u_int	ms_ivec;		/* interrupt vector */
9032633Ssam 	u_int	ms_softCAR;		/* software carrier for async */
9132633Ssam 	struct	mblok *ms_mb;		/* mpcc status area */
9232633Ssam 	struct	vb_buf ms_buf;		/* vba resources for ms_mb */
9332633Ssam 	struct	hxmtl ms_hxl[MPMAXPORT];/* host transmit list */
9432633Ssam 	struct	asyncparam ms_async[MPMAXPORT][MPINSET];/* async structs */
9532633Ssam 	char	ms_cbuf[MPMAXPORT][MPOUTSET][CBSIZE];/* input character buffers */
9632633Ssam } mp_softc[NMP];
9732633Ssam 
9832633Ssam struct	tty mp_tty[NMP*MPCHUNK];
9932633Ssam #ifndef lint
10032633Ssam int	nmp = NMP*MPCHUNK;
10132633Ssam #endif
10232633Ssam 
10332633Ssam int	ttrstrt();
10432633Ssam 
10532633Ssam mpprobe(reg, vi)
10632633Ssam 	caddr_t reg;
10732633Ssam 	struct vba_device *vi;
10832633Ssam {
10932633Ssam 	register int br, cvec;
11032633Ssam 	register struct mpsoftc *ms;
11132633Ssam 
11232633Ssam #ifdef lint
11332633Ssam 	br = 0; cvec = br; br = cvec;
11432633Ssam 	mpintr(0);
11534506Skarels 	mpdlintr(0);
11632633Ssam #endif
11732633Ssam 	if (badaddr(reg, 2))
11832633Ssam 		return (0);
11932633Ssam 	ms = &mp_softc[vi->ui_unit];
12032633Ssam 	/*
12132633Ssam 	 * Allocate page tables and mblok
12232633Ssam 	 * structure (mblok in non-cached memory).
12332633Ssam 	 */
12432633Ssam 	if (vbainit(&ms->ms_buf, sizeof (struct mblok), VB_32BIT) == 0) {
12532633Ssam 		printf("mp%d: vbainit failed\n", vi->ui_unit);
12632633Ssam 		return (0);
12732633Ssam 	}
12832633Ssam 	ms->ms_mb = (struct mblok *)ms->ms_buf.vb_rawbuf;
12932633Ssam 	ms->ms_ivec = MPINTRBASE + 2*vi->ui_unit;	/* XXX */
13032633Ssam 	br = 0x14, cvec = ms->ms_ivec;			/* XXX */
13134287Skarels 	return (sizeof (*reg));
13232633Ssam }
13332633Ssam 
13432633Ssam mpattach(vi)
13532633Ssam 	register struct vba_device *vi;
13632633Ssam {
13732633Ssam 	register struct mpsoftc *ms = &mp_softc[vi->ui_unit];
13832633Ssam 
13932633Ssam 	ms->ms_softCAR = vi->ui_flags;
14032633Ssam 	/*
14132633Ssam 	 * Setup pointer to mblok, initialize bogus
14232633Ssam 	 * status block used by mpcc to locate the pointer
14332633Ssam 	 * and then poke the mpcc to get it to search host
14432633Ssam 	 * memory to find mblok pointer.
14532633Ssam 	 */
14632633Ssam 	mpbogus.mbloks[vi->ui_unit] = (struct mblok *)ms->ms_buf.vb_physbuf;
14732633Ssam 	*(short *)vi->ui_addr = 0x100;		/* magic */
14832633Ssam }
14932633Ssam 
15032633Ssam /*
15132633Ssam  * Open an mpcc port.
15232633Ssam  */
15334506Skarels /* ARGSUSED */
15432633Ssam mpopen(dev, mode)
15532633Ssam 	dev_t dev;
15632633Ssam {
15732633Ssam 	register struct tty *tp;
15832633Ssam 	register struct mpsoftc *ms;
15932633Ssam 	int error, s, port, unit, mpu;
16032633Ssam 	struct vba_device *vi;
16132633Ssam 	struct mpport *mp;
16232633Ssam 	struct mpevent *ev;
16332633Ssam 
16432633Ssam 	unit = minor(dev);
16532633Ssam 	mpu = MPUNIT(unit);
16632633Ssam 	if (mpu >= NMP || (vi = mpinfo[mpu]) == 0 || vi->ui_alive == 0)
16732633Ssam 		return (ENXIO);
16832633Ssam 	tp = &mp_tty[unit];
16932633Ssam 	if (tp->t_state & TS_XCLUDE && u.u_uid != 0)
17032633Ssam 		return (EBUSY);
17132633Ssam 	ms = &mp_softc[mpu];
17232633Ssam 	port = MPPORT(unit);
17332633Ssam 	if (ms->ms_mb->mb_proto[port] != MPPROTO_ASYNC ||
17432633Ssam 	    ms->ms_mb->mb_status != MP_OPOPEN)
17532633Ssam 		return (ENXIO);
17632633Ssam 	mp = &ms->ms_mb->mb_port[port];		/* host mpcc struct */
17732633Ssam 	s = spl8();
17832633Ssam 	while (mp->mp_flags & MP_PROGRESS)
17932633Ssam 		sleep((caddr_t)&tp->t_canq, TTIPRI);
18032633Ssam 	while (tp->t_state & TS_WOPEN)
18132633Ssam 		sleep((caddr_t)&tp->t_canq, TTIPRI);
18232633Ssam 	tp->t_state |= TS_WOPEN;
18332633Ssam 	tp->t_addr = (caddr_t)ms;
18432633Ssam 	tp->t_oproc = mpstart;
18532633Ssam 	tp->t_dev = dev;
18634978Sbostic 	if ((tp->t_state & TS_ISOPEN) == 0) {
18734978Sbostic 		ttychars(tp);
18834978Sbostic 		if (tp->t_ispeed == 0) {
18934978Sbostic 			tp->t_ispeed = B9600;
19034978Sbostic 			tp->t_ospeed = B9600;
19134978Sbostic 			tp->t_flags = ODDP|EVENP|ECHO;
19234978Sbostic 		}
19334978Sbostic 		/*
19434978Sbostic 		 * Initialize port state: init MPCC interface
19534978Sbostic 		 * structures for port and setup modem control.
19634978Sbostic 		 */
19734978Sbostic 		mp->mp_proto = MPPROTO_ASYNC;		/* XXX */
19834978Sbostic 		error = mpportinit(ms, mp, port);
19934978Sbostic 		if (error)
20034978Sbostic 			goto bad;
20134978Sbostic 		ev = mpparam(unit);
20234978Sbostic 		if (ev == 0) {
20334978Sbostic 			error = ENOBUFS;
20434978Sbostic 			goto bad;
20534978Sbostic 		}
20634978Sbostic 		mpcmd(ev, EVCMD_OPEN, 0, ms->ms_mb, port);
20732633Ssam 	}
20832633Ssam 	while ((tp->t_state & TS_CARR_ON) == 0)
20932633Ssam 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
21032633Ssam 	error = (*linesw[tp->t_line].l_open)(dev,tp);
21132633Ssam done:
21232633Ssam 	splx(s);
21332633Ssam 	/* wakeup anyone waiting for open to complete */
21432633Ssam 	wakeup((caddr_t)&tp->t_canq);
21532633Ssam 
21632633Ssam 	return (error);
21732633Ssam bad:
21832633Ssam 	tp->t_state &= ~TS_WOPEN;
21932633Ssam 	goto done;
22032633Ssam }
22132633Ssam 
22232633Ssam /*
22332633Ssam  * Close an mpcc port.
22432633Ssam  */
22534506Skarels /* ARGSUSED */
22634506Skarels mpclose(dev, flag)
22732633Ssam 	dev_t dev;
22832633Ssam {
22932633Ssam 	register struct tty *tp;
23032633Ssam 	register struct mpport *mp;
23132633Ssam 	register struct mpevent *ev;
23232633Ssam 	int s, port, unit, error;
23332633Ssam 	struct mblok *mb;
23432633Ssam 
23532633Ssam 	unit = minor(dev);
23632633Ssam 	tp = &mp_tty[unit];
23732633Ssam 	port = MPPORT(unit);
23832633Ssam 	mb = mp_softc[MPUNIT(unit)].ms_mb;
23932633Ssam 	mp = &mb->mb_port[port];
24032633Ssam 	s = spl8();
241*35055Skarels 	if (mp->mp_flags & MP_PROGRESS) {	/* close in progress??? */
24232633Ssam 		if (mp->mp_flags & MP_REMBSY) {
24332633Ssam 			mp->mp_flags &= ~MP_REMBSY;
24432633Ssam 			splx(s);
24532633Ssam 			return (0);
24632633Ssam 		}
24732633Ssam 		while (mp->mp_flags & MP_PROGRESS)
24832633Ssam 			sleep((caddr_t)&tp->t_canq,TTIPRI);
24932633Ssam 	}
25032633Ssam 	error = 0;
25132633Ssam 	mp->mp_flags |= MP_PROGRESS;
25232633Ssam 	(*linesw[tp->t_line].l_close)(tp);
25332633Ssam 	ev = mp_getevent(mp, unit);
25432633Ssam 	if (ev == 0) {
25534977Sbostic 		error = ENOBUFS;
25634977Sbostic 		mp->mp_flags &= ~MP_PROGRESS;
25734977Sbostic 		goto out;
25832633Ssam 	}
25934977Sbostic 	if (tp->t_state & TS_HUPCLS || (tp->t_state & TS_ISOPEN) == 0)
26034977Sbostic 		mpmodem(unit, MMOD_OFF);
26134977Sbostic 	else
26234977Sbostic 		mpmodem(unit, MMOD_ON);
26332633Ssam 	mpcmd(ev, EVCMD_CLOSE, 0, mb, port);
26434977Sbostic 	ttyclose(tp);
26532633Ssam out:
26632633Ssam 	if (mp->mp_flags & MP_REMBSY)
26732633Ssam 		mpclean(mb, port);
26832633Ssam 	splx(s);
26932633Ssam 	return (error);
27032633Ssam }
27132633Ssam 
27232633Ssam /*
27332633Ssam  * Read from an mpcc port.
27432633Ssam  */
27532633Ssam mpread(dev, uio)
27632633Ssam 	dev_t dev;
27732633Ssam 	struct uio *uio;
27832633Ssam {
27932633Ssam 	struct tty *tp;
28032633Ssam 
28132633Ssam 	tp = &mp_tty[minor(dev)];
28232633Ssam 	return ((*linesw[tp->t_line].l_read)(tp, uio));
28332633Ssam }
28432633Ssam 
28532633Ssam /*
28632633Ssam  * Write to an mpcc port.
28732633Ssam  */
28832633Ssam mpwrite(dev, uio)
28932633Ssam 	dev_t dev;
29032633Ssam 	struct uio *uio;
29132633Ssam {
29232633Ssam 	struct tty *tp;
29332633Ssam 
29432633Ssam 	tp = &mp_tty[minor(dev)];
29532633Ssam 	return ((*linesw[tp->t_line].l_write)(tp, uio));
29632633Ssam }
29732633Ssam 
29832633Ssam /*
29932633Ssam  * Ioctl for a mpcc port
30032633Ssam  */
30132633Ssam mpioctl(dev, cmd, data, flag)
30232633Ssam 	dev_t dev;
30332633Ssam 	caddr_t data;
30432633Ssam {
30532633Ssam 	register struct tty *tp;
30632633Ssam 	register struct mpsoftc *ms;
30732633Ssam 	register struct mpevent *ev;
30832633Ssam 	register struct mpport *mp;
30932633Ssam 	int s, port, error, unit;
31032633Ssam 	struct mblok *mb;
31132633Ssam 
31232633Ssam 	unit = minor(dev);
31332633Ssam 	tp = &mp_tty[unit];
31432633Ssam 	ms = &mp_softc[MPUNIT(unit)];
31532633Ssam 	mb = ms->ms_mb;
31632633Ssam 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
31732633Ssam 	if (error >= 0)
31832633Ssam 		return (error);
31932633Ssam 	error = ttioctl(tp, cmd, data, flag);
32032633Ssam 	if (error >= 0) {
32132633Ssam 		if (cmd == TIOCSETP || cmd == TIOCSETN || cmd == TIOCLBIS ||
32234796Sbostic 		    cmd == TIOCLBIC || cmd == TIOCLSET || cmd == TIOCSETC) {
32332633Ssam 			ev = mpparam(unit);
32432633Ssam 			if (ev == 0)
32532633Ssam 				error = ENOBUFS;
32632633Ssam 			else
32732633Ssam 				mpcmd(ev, EVCMD_IOCTL, A_CHGALL, mb,
32832633Ssam 				    MPPORT(unit));
32932633Ssam 		}
33032633Ssam 		return (error);
33132633Ssam 	}
33232633Ssam 	switch (cmd) {
33332633Ssam 	case TIOCSBRK:			/* send break */
33432633Ssam 	case TIOCCBRK:			/* clear break */
33532633Ssam 		port = MPPORT(unit);
33632633Ssam 		mp = &mb->mb_port[port];
33732633Ssam 		s = spl8();
33832633Ssam 		ev = mp_getevent(mp, unit);
33932633Ssam 		if (ev)
34032633Ssam 			mpcmd(ev, EVCMD_IOCTL,
34132633Ssam 			    (cmd == TIOCSBRK ? A_BRKON : A_BRKOFF),
34232633Ssam 			    mb, port);
34332633Ssam 		else
34432633Ssam 			error = ENOBUFS;
34532633Ssam 		splx(s);
34632633Ssam 		break;
34732633Ssam 	case TIOCSDTR:			/* set dtr control line */
34832633Ssam 		break;
34932633Ssam 	case TIOCCDTR:			/* clear dtr control line */
35032633Ssam 		break;
35132633Ssam 	default:
35232633Ssam 		error = ENOTTY;
35332633Ssam 		break;
35432633Ssam 	}
35532633Ssam 	return (error);
35632633Ssam }
35732633Ssam 
35832633Ssam struct mpevent *
35932633Ssam mpparam(unit)
36032633Ssam 	int unit;
36132633Ssam {
36232633Ssam 	register struct mpevent *ev;
36332633Ssam 	register struct mpport *mp;
36432633Ssam 	register struct tty *tp;
36532633Ssam 	struct mblok *mb;
36632633Ssam 	struct mpsoftc *ms;
36732633Ssam 	register struct asyncparam *asp;
36832633Ssam 	int port;
36932633Ssam 
37032633Ssam 	ms = &mp_softc[MPUNIT(unit)];
37132633Ssam 	mb = ms->ms_mb;
37232633Ssam 	port = MPPORT(unit);
37332633Ssam 	mp = &mb->mb_port[port];
37432633Ssam 	ev = mp_getevent(mp, unit);	/* XXX */
37532633Ssam 	if (ev == 0)
37632633Ssam 		return (ev);
37732633Ssam 	tp = &mp_tty[unit];
37832633Ssam 	/* YUCK */
37932633Ssam 	asp = &ms->ms_async[port][mp->mp_on?mp->mp_on-1:MPINSET-1];
38034796Sbostic 	asp->ap_xon = (u_char)tp->t_startc;
38134796Sbostic 	asp->ap_xoff = (u_char)tp->t_stopc;
38234796Sbostic 	if ((tp->t_flags & RAW) || (tp->t_stopc == -1) || (tp->t_startc == -1))
38334796Sbostic 		asp->ap_xena = MPA_DIS;
38434796Sbostic 	else
38534796Sbostic 		asp->ap_xena = MPA_ENA;
38633995Sbostic 	asp->ap_xany = ((tp->t_flags & DECCTQ) ? MPA_DIS : MPA_ENA);
38732633Ssam #ifdef notnow
38832633Ssam 	if (tp->t_flags & (RAW|LITOUT|PASS8)) {
38932633Ssam #endif
39032633Ssam 		asp->ap_data = MPCHAR_8;
39132633Ssam 		asp->ap_parity = MPPAR_NONE;
39232633Ssam #ifdef notnow
39332633Ssam 	} else {
39432633Ssam 		asp->ap_data = MPCHAR_7;
39532633Ssam 		if ((tp->t_flags & (EVENP|ODDP)) == ODDP)
39632633Ssam 			asp->ap_parity = MPPAR_ODD;
39732633Ssam 		else
39832633Ssam 			asp->ap_parity = MPPAR_EVEN;
39932633Ssam 	}
40032633Ssam #endif
40132633Ssam 	if (tp->t_ospeed == B110)
40232633Ssam 		asp->ap_stop = MPSTOP_2;
40332633Ssam 	else
40432633Ssam 		asp->ap_stop = MPSTOP_1;
40532633Ssam 	if (tp->t_ospeed == EXTA || tp->t_ospeed == EXTB)
40632633Ssam 		asp->ap_baud = M19200;
40732633Ssam 	else
40832633Ssam 		asp->ap_baud = tp->t_ospeed;
40932633Ssam 	asp->ap_loop = MPA_DIS;		/* disable loopback */
41032633Ssam 	asp->ap_rtimer = A_RCVTIM;	/* default receive timer */
41132633Ssam 	if (ms->ms_softCAR & (1<<port))
41232633Ssam 		setm(&asp->ap_modem, A_DTR, ASSERT);
41332633Ssam 	else
41432633Ssam 		setm(&asp->ap_modem, A_DTR, AUTO);
41532633Ssam 	seti(&asp->ap_intena, A_DCD);
41632633Ssam 	return (ev);
41732633Ssam }
41832633Ssam 
41932633Ssam mpstart(tp)
42032633Ssam 	register struct tty *tp;
42132633Ssam {
42232633Ssam 	register struct mpevent *ev;
42332633Ssam 	register struct mpport *mp;
42432633Ssam 	struct mblok *mb;
42532633Ssam 	struct mpsoftc *ms;
42632633Ssam 	int port, unit, xcnt, n, s, i;
42732633Ssam 	struct	hxmtl *hxp;
42832633Ssam 	struct clist outq;
42932633Ssam 
43032633Ssam 	s = spl8();
43132633Ssam 	unit = minor(tp->t_dev);
43232633Ssam 	ms = &mp_softc[MPUNIT(unit)];
43332633Ssam 	mb = ms->ms_mb;
43432633Ssam 	port = MPPORT(unit);
43532633Ssam 	mp = &mb->mb_port[port];
43632633Ssam 	hxp = &ms->ms_hxl[port];
43732633Ssam 	xcnt = 0;
43832633Ssam 	outq = tp->t_outq;
43932633Ssam 	for (i = 0; i < MPXMIT; i++) {
44032633Ssam 		if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
44132633Ssam 			break;
44232633Ssam 		if (outq.c_cc <= TTLOWAT(tp)) {
44332633Ssam 			if (tp->t_state & TS_ASLEEP) {
44432633Ssam 				tp->t_state &= ~TS_ASLEEP;
44532633Ssam 				wakeup((caddr_t)&tp->t_outq);
44632633Ssam 			}
44732633Ssam 			if (tp->t_wsel) {
44832633Ssam 				selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
44932633Ssam 				tp->t_wsel = 0;
45032633Ssam 				tp->t_state &= ~TS_WCOLL;
45132633Ssam 			}
45232633Ssam 		}
45332633Ssam 		if (outq.c_cc == 0)
45432633Ssam 			break;
45532633Ssam 		/*
45632633Ssam 		 * If we're not currently busy outputting,
45732633Ssam 		 * and there is data to be output, set up
45832633Ssam 		 * port transmit structure to send to mpcc.
45932633Ssam 		 */
46032633Ssam 		if (tp->t_flags & (RAW|LITOUT))
46132633Ssam 			n = ndqb(&outq, 0);
46232633Ssam 		else {
46332633Ssam 			n = ndqb(&outq, 0200);
46432633Ssam 			if (n == 0) {
46532633Ssam 				n = getc(&outq);
46632633Ssam 				timeout(ttrstrt, (caddr_t)tp, (n&0177)+6);
46732633Ssam 				tp->t_state |= TS_TIMEOUT;
46832633Ssam 				break;
46932633Ssam 			}
47032633Ssam 		}
47134506Skarels 		hxp->dblock[i] = (caddr_t)kvtophys(outq.c_cf);
47232633Ssam 		hxp->size[i] = n;
47332633Ssam 		xcnt++;		/* count of xmts to send */
47432633Ssam 		ndadvance(&outq, n);
47532633Ssam 	}
47632633Ssam 	/*
47732633Ssam 	 * If data to send, poke mpcc.
47832633Ssam 	 */
47932633Ssam 	if (xcnt) {
48032633Ssam 		ev = mp_getevent(mp, unit);
48132633Ssam 		if (ev == 0) {
48232633Ssam 			tp->t_state &= ~(TS_BUSY|TS_TIMEOUT);
48332633Ssam 		} else {
48432633Ssam 			tp->t_state |= TS_BUSY;
48532633Ssam 			ev->ev_count = xcnt;
48632633Ssam 			mpcmd(ev, EVCMD_WRITE, 0, mb, MPPORT(unit));
48732633Ssam 		}
48832633Ssam 	}
48932633Ssam 	splx(s);
49032633Ssam }
49132633Ssam 
49232633Ssam /*
49332633Ssam  * Advance cc bytes from q  but don't free memory.
49432633Ssam  */
49532633Ssam ndadvance(q, cc)
49632633Ssam 	register struct clist *q;
49732633Ssam 	register cc;
49832633Ssam {
49932633Ssam 	register struct cblock *bp;
50032633Ssam 	char *end;
50132633Ssam 	int rem, s;
50232633Ssam 
50332633Ssam 	s = spltty();
50432633Ssam 	if (q->c_cc <= 0)
50532633Ssam 		goto out;
50632633Ssam 	while (cc>0 && q->c_cc) {
50732633Ssam 		bp = (struct cblock *)((int)q->c_cf & ~CROUND);
50832633Ssam 		if ((int)bp == (((int)q->c_cl-1) & ~CROUND)) {
50932633Ssam 			end = q->c_cl;
51032633Ssam 		} else {
51132633Ssam 			end = (char *)((int)bp + sizeof (struct cblock));
51232633Ssam 		}
51332633Ssam 		rem = end - q->c_cf;
51432633Ssam 		if (cc >= rem) {
51532633Ssam 			cc -= rem;
51632633Ssam 			q->c_cc -= rem;
51732633Ssam 			q->c_cf = bp->c_next->c_info;
51832633Ssam 		} else {
51932633Ssam 			q->c_cc -= cc;
52032633Ssam 			q->c_cf += cc;
52132633Ssam 			break;
52232633Ssam 		}
52332633Ssam 	}
52432633Ssam 	if (q->c_cc <= 0) {
52532633Ssam 		q->c_cf = q->c_cl = NULL;
52632633Ssam 		q->c_cc = 0;
52732633Ssam 	}
52832633Ssam out:
52932633Ssam 	splx(s);
53032633Ssam }
53132633Ssam 
53232633Ssam /*
53332633Ssam  * Stop output on a line, e.g. for ^S/^Q or output flush.
53432633Ssam  */
53534506Skarels /* ARGSUSED */
53632633Ssam mpstop(tp, rw)
53732633Ssam 	register struct tty *tp;
53832633Ssam 	int rw;
53932633Ssam {
54034506Skarels 	int s;
54132633Ssam 
54232633Ssam 	s = spl8();
54332633Ssam 	/* XXX: DISABLE TRANSMITTER */
54432633Ssam 	if (tp->t_state & TS_BUSY) {
54532633Ssam 		if ((tp->t_state & TS_TTSTOP) == 0)
54632633Ssam 			tp->t_state |= TS_FLUSH;
54732633Ssam 	}
54832633Ssam 	splx(s);
54932633Ssam }
55032633Ssam 
55132633Ssam /*
55232633Ssam  * Initialize an async port's MPCC state.
55332633Ssam  */
55432633Ssam mpportinit(ms, mp, port)
55532633Ssam 	register struct mpsoftc *ms;
55632633Ssam 	register struct mpport *mp;
55732633Ssam 	int port;
55832633Ssam {
55932633Ssam 	register struct mpevent *ev;
56032633Ssam 	register int i;
56132633Ssam 	caddr_t ptr;
56232633Ssam 
56332633Ssam 	mp->mp_on = mp->mp_off = 0;
56432633Ssam 	mp->mp_nextrcv = 0;
56532633Ssam 	mp->mp_flags = 0;
56632633Ssam 	ev = &mp->mp_recvq[0];
56732633Ssam 	for (i = 0; ev < &mp->mp_recvq[MPINSET]; ev++, i++) {
56832633Ssam 		ev->ev_status = EVSTATUS_FREE;
56932633Ssam 		ev->ev_cmd = 0;
57032633Ssam 		ev->ev_opts = 0;
57132633Ssam 		ev->ev_error = 0;
57232633Ssam 		ev->ev_flags = 0;
57332633Ssam 		ev->ev_count = 0;
57434506Skarels 		ev->ev_un.hxl = (struct hxmtl *) kvtophys(&ms->ms_hxl[port]);
57534506Skarels 		ev->ev_params = (caddr_t) kvtophys(&ms->ms_async[port][i]);
57632633Ssam 	}
57732633Ssam 	ev = &mp->mp_sendq[0];
57832633Ssam 	for (i = 0; ev < &mp->mp_sendq[MPOUTSET]; ev++, i++) {
57932633Ssam 		/* init so that L2 can't send any events */
58032633Ssam 		/* to host until open has completed      */
58132633Ssam 		ev->ev_status = EVSTATUS_FREE;
58232633Ssam 		ev->ev_cmd = 0;
58332633Ssam 		ev->ev_error = 0;
58432633Ssam 		ev->ev_flags = 0;
58532633Ssam 		ev->ev_count = 0;
58632633Ssam 		ptr = (caddr_t) &ms->ms_cbuf[port][i][0];
58734506Skarels 		ev->ev_un.rcvblk = (u_char *)kvtophys(ptr);
58834506Skarels 		ev->ev_params = (caddr_t) kvtophys(ptr);
58932633Ssam 	}
59032633Ssam 	return (0);
59132633Ssam }
59232633Ssam 
59332633Ssam /*
59432633Ssam  * Send an event to an mpcc.
59532633Ssam  */
59632633Ssam mpcmd(ev, cmd, flags, mb, port)
59732633Ssam 	register struct mpevent *ev;
59832633Ssam 	struct mblok *mb;
59932633Ssam {
60032633Ssam 	int s;
60132633Ssam 
60232633Ssam 	s = spl8();
60332633Ssam 	/* move host values to inbound entry */
60432633Ssam 	ev->ev_cmd = cmd;
60532633Ssam 	ev->ev_opts = flags;
60632633Ssam 	/* show event ready for mpcc */
60732633Ssam 	ev->ev_status = EVSTATUS_GO;
60832633Ssam 	mpintmpcc(mb, port);
60932633Ssam 	splx(s);
61032633Ssam }
61132633Ssam 
61232633Ssam /*
61332633Ssam  * Return the next available event entry for the indicated port.
61432633Ssam  */
61532633Ssam struct mpevent *
61632633Ssam mp_getevent(mp, unit)
61732633Ssam 	register struct mpport *mp;
61832633Ssam 	int unit;
61932633Ssam {
62032633Ssam 	register struct mpevent *ev;
62132633Ssam 	int i, s;
62232633Ssam 
62332633Ssam 	s = spl8();
62432633Ssam 	ev = &mp->mp_recvq[mp->mp_on];
62532633Ssam 	if (ev->ev_status != EVSTATUS_FREE)
62632633Ssam 		goto bad;
62732633Ssam 	/*
62832633Ssam 	 * If not a close request, verify one extra
62932633Ssam 	 * event is available for closing the port.
63032633Ssam 	 */
63134506Skarels 	if ((mp->mp_flags & MP_PROGRESS) == 0) {
63232633Ssam 		if ((i = mp->mp_on + 1) >= MPINSET)
63332633Ssam 			i = 0;
63432633Ssam 		if (mp->mp_recvq[i].ev_status != EVSTATUS_FREE)
63532633Ssam 			goto bad;
63632633Ssam 	}
63732633Ssam 	/* init inbound fields marking this entry as busy */
63832633Ssam 	ev->ev_error = 0;
63932633Ssam 	ev->ev_flags = 0;
64032633Ssam 	ev->ev_count = 0;
64132633Ssam 	ev->ev_status = EVSTATUS_BUSY;
64232633Ssam 	/* adjust pointer to next available inbound entry */
64332633Ssam 	adjptr(mp->mp_on, MPINSET);
64432633Ssam 	splx(s);
64532633Ssam 	return (ev);
64632633Ssam bad:
64732633Ssam 	splx(s);
64832633Ssam 	log(LOG_ERR, "mp%d: port%d, out of events", MPUNIT(unit), MPPORT(unit));
64932633Ssam 	return ((struct mpevent *)0);
65032633Ssam }
65132633Ssam 
65232633Ssam mpmodem(unit, flag)
65332633Ssam 	int unit, flag;
65432633Ssam {
65532633Ssam 	struct mpsoftc *ms = &mp_softc[MPUNIT(unit)];
65632633Ssam 	int port = MPPORT(unit);
65732633Ssam 	register struct mpport *mp;
65832633Ssam 	register struct asyncparam *asp;
65932633Ssam 
66032633Ssam 	mp = &ms->ms_mb->mb_port[port];
66132633Ssam 	asp = &ms->ms_async[port][mp->mp_on?mp->mp_on-1:MPINSET-1];
66232633Ssam 	if (flag == MMOD_ON) {
66332633Ssam 		if (ms->ms_softCAR & (1 << port))
66432633Ssam 			setm(&asp->ap_modem, A_DTR, ASSERT);
66532633Ssam 		else
66632633Ssam 			setm(&asp->ap_modem, A_DTR, AUTO);
66732633Ssam 		seti(&asp->ap_intena, A_DCD);
66832633Ssam 	} else {
66932633Ssam 		setm(&asp->ap_modem, 0, DROP);
67032633Ssam 		seti(&asp->ap_intena, 0);
67132633Ssam 	}
67232633Ssam }
67332633Ssam 
67432633Ssam /*
67532633Ssam  * Set up the modem control structure according to mask.
67632633Ssam  * Each set bit in the mask means assert the corresponding
67732633Ssam  * modem control line, otherwise, it will be dropped.
67832633Ssam  * RTS is special since it can either be asserted, dropped
67932633Ssam  * or put in auto mode for auto modem control.
68032633Ssam  */
68132633Ssam static
68232633Ssam setm(mc, mask, rts)
68332633Ssam 	register struct mdmctl *mc;
68432633Ssam 	register int mask;
68532633Ssam {
68632633Ssam 
68732633Ssam 	mc->mc_rngdsr = (mask & A_RNGDSR) ? ASSERT : DROP;
68832633Ssam 	mc->mc_rate = (mask & A_RATE) ? ASSERT : DROP;
68932633Ssam 	mc->mc_dcd = (mask & A_DCD) ? ASSERT : DROP;
69032633Ssam 	mc->mc_sectx = (mask & A_SECTX) ? ASSERT : DROP;
69132633Ssam 	mc->mc_cts = (mask & A_CTS) ? ASSERT : DROP;
69232633Ssam 	mc->mc_secrx = (mask & A_SECRX) ? ASSERT : DROP;
69332633Ssam 	mc->mc_dtr = (mask & A_DTR) ? ASSERT : DROP;
69432633Ssam 	mc->mc_rts = rts;
69532633Ssam }
69632633Ssam 
69732633Ssam /*
69832633Ssam  * Set up the status change enable field from mask.
69932633Ssam  * When a signal is enabled in this structure and
70032633Ssam  * and a change in state on a corresponding modem
70132633Ssam  * control line occurs, a status change event will
70232633Ssam  * be delivered to the host.
70332633Ssam  */
70432633Ssam static
70532633Ssam seti(mc, mask)
70632633Ssam 	register struct mdmctl *mc;
70732633Ssam 	register int mask;
70832633Ssam {
70932633Ssam 
71032633Ssam 	mc->mc_rngdsr = (mask & A_RNGDSR) ? MDM_ON : MDM_OFF;
71132633Ssam 	mc->mc_rate = (mask & A_RATE) ? MDM_ON : MDM_OFF;
71232633Ssam 	mc->mc_dcd = (mask & A_DCD) ? MDM_ON : MDM_OFF;
71332633Ssam 	mc->mc_sectx = (mask & A_SECTX) ? MDM_ON : MDM_OFF;
71432633Ssam 	mc->mc_cts = (mask & A_CTS) ? MDM_ON : MDM_OFF;
71532633Ssam 	mc->mc_secrx = (mask & A_SECRX) ? MDM_ON : MDM_OFF;
71632633Ssam 	mc->mc_dtr = (mask & A_DTR) ? MDM_ON : MDM_OFF;
71732633Ssam 	mc->mc_rts = (mask & A_RTS) ? MDM_ON : MDM_OFF;
71832633Ssam }
71932633Ssam 
72032633Ssam mpcleanport(mb, port)
72132633Ssam 	struct mblok *mb;
72232633Ssam 	int port;
72332633Ssam {
72432633Ssam 	register struct mpport *mp;
72532633Ssam 	register struct tty *tp;
72632633Ssam 
72732633Ssam 	mp = &mb->mb_port[port];
72832633Ssam 	if (mp->mp_proto == MPPROTO_ASYNC) {
72932633Ssam 		mp->mp_flags = MP_REMBSY;
73034506Skarels 		/* signal loss of carrier and close */
73132633Ssam 		tp = &mp_tty[mb->mb_unit*MPCHUNK+port];
73232633Ssam 		ttyflush(tp, FREAD|FWRITE);
73334506Skarels 		(void) (*linesw[tp->t_line].l_modem)(tp, 0);
73434506Skarels 		(void) mpclose(tp->t_dev, 0);
73532633Ssam 	}
73632633Ssam }
73732633Ssam 
73832633Ssam mpclean(mb, port)
73932633Ssam 	register struct mblok *mb;
74032633Ssam 	int port;
74132633Ssam {
74232633Ssam 	register struct mpport *mp;
74332633Ssam 	register struct mpevent *ev;
74432633Ssam 	register int i;
74534506Skarels 	u_char list[2];
74632633Ssam 	int unit;
74732633Ssam 
74832633Ssam 	mp = &mb->mb_port[port];
74932633Ssam 	unit = mb->mb_unit;
75032633Ssam 	for (i = mp->mp_off; i != mp->mp_on; i = (i+1 % MPINSET)) {
75132633Ssam 		ev = &mp->mp_recvq[i];
75232633Ssam 		ev->ev_error = ENXIO;
75332633Ssam 		ev->ev_status = EVSTATUS_DONE;
75432633Ssam 	}
75532633Ssam 	list[0] = port, list[1] = MPPORT_EOL;
75632633Ssam 	mpxintr(unit, list);
75732633Ssam 	mprintr(unit, list);
75832633Ssam 	/* Clear async for port */
75932633Ssam 	mp->mp_proto = MPPROTO_UNUSED;
76032633Ssam 	mp->mp_flags = 0;
76132633Ssam 	mp->mp_on = 0;
76232633Ssam 	mp->mp_off = 0;
76332633Ssam 	mp->mp_nextrcv = 0;
76432633Ssam 
76532633Ssam 	mp_tty[unit*MPCHUNK + port].t_state = 0;
76632633Ssam 	for (ev = &mp->mp_sendq[0]; ev < &mp->mp_sendq[MPOUTSET]; ev++) {
76732633Ssam 		ev->ev_status = EVSTATUS_FREE;
76832633Ssam 		ev->ev_cmd = 0;
76932633Ssam 		ev->ev_error = 0;
77032633Ssam 		ev->ev_un.rcvblk = 0;
77132633Ssam 		ev->ev_params = 0;
77232633Ssam 	}
77332633Ssam 	for (ev = &mp->mp_recvq[0]; ev < &mp->mp_recvq[MPINSET]; ev++) {
77432633Ssam 		ev->ev_status = EVSTATUS_FREE;
77532633Ssam 		ev->ev_cmd = 0;
77632633Ssam 		ev->ev_error = 0;
77732633Ssam 		ev->ev_params = 0;
77832633Ssam 	}
77932633Ssam }
78032633Ssam 
78132633Ssam /*
78232633Ssam  * MPCC interrupt handler.
78332633Ssam  */
78432633Ssam mpintr(mpcc)
78532633Ssam 	int mpcc;
78632633Ssam {
78732633Ssam 	register struct mblok *mb;
78832633Ssam 	register struct his *his;
78932633Ssam 
79032633Ssam 	mb = mp_softc[mpcc].ms_mb;
79132633Ssam 	if (mb == 0) {
79232633Ssam 		printf("mp%d: stray interrupt\n", mpcc);
79332633Ssam 		return;
79432633Ssam 	}
79532633Ssam 	his = &mb->mb_hostint;
79632633Ssam 	his->semaphore &= ~MPSEMA_AVAILABLE;
79732633Ssam 	/*
79832633Ssam 	 * Check for events to be processed.
79932633Ssam 	 */
80032633Ssam 	if (his->proto[MPPROTO_ASYNC].outbdone[0] != MPPORT_EOL)
80132633Ssam 		mprintr(mpcc, his->proto[MPPROTO_ASYNC].outbdone);
80232633Ssam 	if (his->proto[MPPROTO_ASYNC].inbdone[0] != MPPORT_EOL)
80332633Ssam 		mpxintr(mpcc, his->proto[MPPROTO_ASYNC].inbdone);
80432633Ssam 	if (mb->mb_harderr || mb->mb_softerr)
80532633Ssam 		mperror(mb, mpcc);
80632633Ssam 	his->semaphore |= MPSEMA_AVAILABLE;
80732633Ssam }
80832633Ssam 
80932633Ssam /*
81032633Ssam  * Handler for processing completion of transmitted events.
81132633Ssam  */
81232633Ssam mpxintr(unit, list)
81334506Skarels 	register u_char *list;
81432633Ssam {
81532633Ssam 	register struct mpport *mp;
81632633Ssam 	register struct mpevent *ev;
81732633Ssam 	register struct mblok *mb;
81832633Ssam 	register struct tty *tp;
81932633Ssam 	register struct asyncparam *ap;
82032633Ssam 	struct mpsoftc *ms;
82132633Ssam 	int port, i, j;
82232633Ssam 
82332633Ssam 	ms = &mp_softc[unit];
82432633Ssam 	mb = mp_softc[unit].ms_mb;
82532633Ssam 	for (j = 0; j < MPMAXPORT && ((port = *list++) != MPPORT_EOL); j++) {
82632633Ssam 		/*
82732633Ssam 		 * Process each completed entry in the inbound queue.
82832633Ssam 		 */
82932633Ssam 		mp = &mb->mb_port[port];
83032633Ssam 		tp = &mp_tty[unit*MPCHUNK + port];
83132633Ssam #define	nextevent(mp)	&mp->mp_recvq[mp->mp_off]
83232633Ssam 		ev = nextevent(mp);
83332633Ssam 		for(; ev->ev_status & EVSTATUS_DONE; ev = nextevent(mp)) {
83432633Ssam 			/* YUCK */
83532633Ssam 			ap = &ms->ms_async[port][mp->mp_off];
83634506Skarels 			mppurge((caddr_t)ap, (int)sizeof (*ap));
83732633Ssam 			switch (ev->ev_cmd) {
83832633Ssam 			case EVCMD_OPEN:
83932633Ssam 				/*
84032633Ssam 				 * Open completion, start all reads and
84132633Ssam 				 * assert modem status information.
84232633Ssam 				 */
84332633Ssam 				for (i = 0; i < MPOUTSET; i++)
84432633Ssam 					mp->mp_sendq[i].ev_status = EVSTATUS_GO;
84532633Ssam 				(*linesw[tp->t_line].l_modem)
84632633Ssam 				    (tp, ap->ap_modem.mc_dcd == ASSERT);
84732633Ssam 				break;
84832633Ssam 			case EVCMD_CLOSE:
84932633Ssam 				/*
85032633Ssam 				 * Close completion, flush all pending
85132633Ssam 				 * transmissions, free resources, and
85232633Ssam 				 * cleanup mpcc port state.
85332633Ssam 				 */
85432633Ssam 				for (i = 0; i < MPOUTSET; i++) {
85532633Ssam 					mp->mp_sendq[i].ev_status =
85632633Ssam 					    EVSTATUS_FREE;
85732633Ssam 					mp->mp_sendq[i].ev_un.rcvblk = 0;
85832633Ssam 					mp->mp_sendq[i].ev_params = 0;
85932633Ssam 				}
86032633Ssam 				tp->t_state &= ~TS_CARR_ON;
86132633Ssam 				mp->mp_on = mp->mp_off = mp->mp_nextrcv = 0;
86232633Ssam 				mp->mp_flags &= ~MP_PROGRESS;
86332633Ssam 				mp->mp_proto = MPPROTO_UNUSED;
864*35055Skarels 				wakeup((caddr_t)&tp->t_canq);
86532633Ssam 				goto done;
86632633Ssam 			case EVCMD_IOCTL:
86732633Ssam 				/*
86832633Ssam 				 * Nothing to do, just pitch.
86932633Ssam 				 */
87032633Ssam 				break;
87132633Ssam 			case EVCMD_WRITE:
87232633Ssam 				/*
87332633Ssam 				 * Transmission completed, update tty
87432633Ssam 				 * state and restart output.
87532633Ssam 				 */
87632633Ssam 				tp->t_state &= ~TS_BUSY;
877*35055Skarels 				if (tp->t_state & TS_FLUSH)
87832633Ssam 					tp->t_state &= ~TS_FLUSH;
879*35055Skarels 				else {
88034506Skarels 					register int cc = 0, n;
88132633Ssam 					struct hxmtl *hxp;
88232633Ssam 
88332633Ssam 					hxp = &ms->ms_hxl[port];
88434506Skarels 					for(n = 0; n < ev->ev_count; n++)
88534506Skarels 						cc += hxp->size[n];
88632633Ssam 					ndflush(&tp->t_outq, cc);
88732633Ssam 				}
88832633Ssam 				switch (ev->ev_error) {
88932633Ssam 				case A_SIZERR:  /*# error in xmt data size */
89032633Ssam 					mplog(unit, port, A_XSIZE, 0);
89132633Ssam 					break;
89232633Ssam 				case A_NXBERR:  /*# no more xmt evt buffers */
89332633Ssam 					mplog(unit, port, A_NOXBUF, 0);
89432633Ssam 					break;
89532633Ssam 				}
89632633Ssam 				mpstart(tp);
89732633Ssam 				break;
89832633Ssam 			default:
89934506Skarels 				mplog(unit, port, A_INVCMD, (int)ev->ev_cmd);
90032633Ssam 				break;
90132633Ssam 			}
90232633Ssam 			/* re-init all values in this entry */
90332633Ssam 			ev->ev_cmd = 0;
90432633Ssam 			ev->ev_opts = 0;
90532633Ssam 			ev->ev_error = 0;
90632633Ssam 			ev->ev_flags = 0;
90732633Ssam 			ev->ev_count = 0;
90832633Ssam 			/* show this entry is available for use */
90932633Ssam 			ev->ev_status = EVSTATUS_FREE;
91032633Ssam 			adjptr(mp->mp_off, MPINSET);
91132633Ssam #undef	nextevent
91232633Ssam 		}
91332633Ssam done:
91432633Ssam 		;
91532633Ssam 	}
91632633Ssam }
91732633Ssam 
91832633Ssam /*
91932633Ssam  * Handler for processing received events.
92032633Ssam  */
92132633Ssam mprintr(unit, list)
92234506Skarels 	u_char *list;
92332633Ssam {
92432633Ssam 	register struct tty *tp;
92532633Ssam 	register struct mpport *mp;
92632633Ssam 	register struct mpevent *ev;
92732633Ssam 	struct mblok *mb;
92832633Ssam 	register int cc;
92932633Ssam 	register char *cp;
93032633Ssam 	struct mpsoftc *ms;
93132633Ssam 	caddr_t ptr;
93232633Ssam 	char *rcverr;
93332633Ssam 	int port, i;
93432633Ssam 
93532633Ssam 	ms = &mp_softc[unit];
93632633Ssam 	mb = mp_softc[unit].ms_mb;
93732633Ssam 	for (i = 0; i < MPMAXPORT && (port = *list++) != MPPORT_EOL; i++) {
93832633Ssam 		tp = &mp_tty[unit*MPCHUNK + port];
93932633Ssam 		mp = &mb->mb_port[port];
94032633Ssam 		ev = &mp->mp_sendq[mp->mp_nextrcv];
94132633Ssam 		while (ev->ev_status & EVSTATUS_DONE) {
94232633Ssam 			if (ev->ev_cmd != EVCMD_READ &&
94332633Ssam 			    ev->ev_cmd != EVCMD_STATUS) {
94432633Ssam 				mplog(unit, port, "unexpected command",
94534506Skarels 				    (int)ev->ev_cmd);
94632633Ssam 				goto next;
94732633Ssam 			}
94832633Ssam 			if (ev->ev_cmd == EVCMD_STATUS) {
94932633Ssam 				/*
95032633Ssam 				 * Status change, look for carrier changes.
95132633Ssam 				 */
95232633Ssam 				if (ev->ev_opts == DCDASRT ||
95332633Ssam 				    ev->ev_opts == DCDDROP)
95432633Ssam 					(*linesw[tp->t_line].l_modem)
95532633Ssam 					    (tp, ev->ev_opts == DCDASRT);
95632633Ssam 				else
95732633Ssam 					mplog(unit, port,
95832633Ssam 					    "unexpect status command",
95934506Skarels 					    (int)ev->ev_opts);
96032633Ssam 				goto next;
96132633Ssam 			}
96232633Ssam 			/*
96332633Ssam 			 * Process received data.
96432633Ssam 			 */
96532633Ssam 			if ((tp->t_state & (TS_ISOPEN|TS_WOPEN)) == 0)
96632633Ssam 				goto next;
96732633Ssam 			cc = ev->ev_count;
96832633Ssam 			if (cc == 0)
96932633Ssam 				goto next;
97032633Ssam 			/* YUCK */
97132633Ssam 			cp = ms->ms_cbuf[port][mp->mp_nextrcv];
97232633Ssam 			mppurge(cp, CBSIZE);
97332633Ssam 			while (cc-- > 0) {
97432633Ssam 				/*
97532633Ssam 				 * A null character is inserted, potentially
97632633Ssam 				 * when a break or framing error occurs.  If
97732633Ssam 				 * we're not in raw mode, substitute the
97832633Ssam 				 * interrupt character.
97932633Ssam 				 */
98032633Ssam 				if (*cp == 0 &&
98132633Ssam 				    (ev->ev_error == BRKASRT ||
98232633Ssam 				     ev->ev_error == FRAMERR))
98332633Ssam 					if ((tp->t_flags&RAW) == 0)
98432633Ssam 						*cp = tp->t_intrc;
98532633Ssam 				(*linesw[tp->t_line].l_rint)(*cp++, tp);
98632633Ssam 			}
98732633Ssam 			/* setup for next read */
98832633Ssam 			ptr = (caddr_t)&mp_softc[unit].ms_cbuf[port][mp->mp_nextrcv][0];
98934506Skarels 			ev->ev_un.rcvblk = (u_char *)kvtophys(ptr);
99034506Skarels 			ev->ev_params = (caddr_t) kvtophys(ptr);
99133673Sbostic 			switch(ev->ev_error) {
99233673Sbostic 			case RCVDTA:    /* Normal (good) rcv data */
99333673Sbostic 					/* do not report the following */
99433673Sbostic 					/* they are "normal" errors */
99533673Sbostic 			case FRAMERR:   /* frame error */
99633673Sbostic 			case BRKASRT:   /* Break condition */
99732633Ssam 			case PARERR:    /* parity error */
99833673Sbostic 				rcverr = (char *)0;
99932633Ssam 				break;
100032633Ssam 			case OVRNERR:   /* Overrun error */
100132633Ssam 				rcverr = "overrun error";
100232633Ssam 				break;
100332633Ssam 			case OVFERR:    /* Overflow error */
100432633Ssam 				rcverr = "overflow error";
100532633Ssam 				break;
100632633Ssam 			default:
100732633Ssam 				rcverr = "undefined rcv error";
100832633Ssam 			}
100932633Ssam 			if (rcverr != (char *)0)
101034506Skarels 				mplog(unit, port, rcverr, (int)ev->ev_error);
101132633Ssam 		next:
101232633Ssam 			ev->ev_cmd = 0;
101332633Ssam 			ev->ev_opts = 0;
101432633Ssam 			ev->ev_error = 0;
101532633Ssam 			ev->ev_flags = 0;
101632633Ssam 			ev->ev_status = EVSTATUS_GO;	/* start next read */
101732633Ssam 			adjptr(mp->mp_nextrcv, MPOUTSET);
101832633Ssam 			ev = &mp->mp_sendq[mp->mp_nextrcv];
101932633Ssam 		}
102032633Ssam 	}
102132633Ssam }
102232633Ssam 
102332633Ssam /*
102432633Ssam  * Log an mpcc diagnostic.
102532633Ssam  */
102632633Ssam mplog(unit, port, cp, flags)
102732633Ssam 	char *cp;
102832633Ssam {
102932633Ssam 
103032633Ssam 	if (flags)
103132633Ssam 		log(LOG_ERR, "mp%d: port%d, %s (%d)\n",
103232633Ssam 		    unit, port, cp, flags);
103332633Ssam 	else
103432633Ssam 		log(LOG_ERR, "mp%d: port%d, %s\n", unit, port, cp);
103532633Ssam }
103632633Ssam 
103732633Ssam int	MPHOSTINT = 1;
103832633Ssam 
103932633Ssam mptimeint(mb)
104032633Ssam 	register struct mblok *mb;
104132633Ssam {
104232633Ssam 
104332633Ssam         mb->mb_mpintcnt = 0;
104432633Ssam         mb->mb_mpintclk = (caddr_t)0;
104532633Ssam 	*(u_short *)mpinfo[mb->mb_unit]->ui_addr = 2;
104632633Ssam }
104732633Ssam 
104832633Ssam /*
104932633Ssam  * Interupt mpcc
105032633Ssam  */
105132633Ssam mpintmpcc(mb, port)
105232633Ssam 	register struct mblok *mb;
105332633Ssam {
105432633Ssam 
105532633Ssam         mb->mb_intr[port] |= MPSEMA_WORK;
105632633Ssam         if (++mb->mb_mpintcnt == MPHOSTINT) {
105732633Ssam                 mb->mb_mpintcnt = 0;
105832633Ssam 		*(u_short *)mpinfo[mb->mb_unit]->ui_addr = 2;
105932633Ssam                 if (mb->mb_mpintclk) {
106034506Skarels                         untimeout(mptimeint, (caddr_t)mb);
106132633Ssam                         mb->mb_mpintclk = 0;
106232633Ssam                 }
106332633Ssam         } else {
106432633Ssam                 if (mb->mb_mpintclk == 0) {
106534506Skarels                         timeout(mptimeint, (caddr_t)mb, 4);
106632633Ssam                         mb->mb_mpintclk = (caddr_t)1;
106732633Ssam                 }
106832633Ssam         }
106932633Ssam }
107032633Ssam 
107132633Ssam static char *mpherrmsg[] = {
107232633Ssam 	"",
107332633Ssam 	"Bus error",				/* MPBUSERR */
107432633Ssam 	"Address error",			/* ADDRERR */
107532633Ssam 	"Undefined ecc interrupt",		/* UNDECC */
107632633Ssam 	"Undefined interrupt",			/* UNDINT */
107732633Ssam 	"Power failure occurred",		/* PWRFL */
107832633Ssam 	"Stray transmit done interrupt",	/* NOXENTRY */
107932633Ssam 	"Two fast timers on one port",		/* TWOFTMRS */
108032633Ssam 	"Interrupt queue full",			/* INTQFULL */
108132633Ssam 	"Interrupt queue ack error",		/* INTQERR */
108232633Ssam 	"Uncorrectable dma parity error",	/* CBPERR */
108332633Ssam 	"32 port ACAP failed power up",		/* ACPDEAD */
108432633Ssam };
108532633Ssam #define	NHERRS	(sizeof (mpherrmsg) / sizeof (mpherrmsg[0]))
108632633Ssam 
108732633Ssam mperror(mb, unit)
108832633Ssam 	register struct mblok *mb;
108932633Ssam 	int unit;
109032633Ssam {
109132633Ssam 	register char *cp;
109232633Ssam 	register int i;
109332633Ssam 
109432633Ssam 	if (mb->mb_softerr) {
109532633Ssam 		switch (mb->mb_softerr) {
109632633Ssam 		case DMAPERR:   /* dma parity error */
109732633Ssam 			cp = "dma parity error";
109832633Ssam 			break;
109932633Ssam 		case ECCERR:
110032633Ssam 			cp = "local memory ecc error";
110132633Ssam 			break;
110232633Ssam 		default:
110332633Ssam 			cp = "unknown error";
110432633Ssam 			break;
110532633Ssam 		}
110632633Ssam 		log(LOG_ERR, "mp%d: soft error, %s", unit, cp);
110732633Ssam 		mb->mb_softerr = 0;
110832633Ssam 	}
110932633Ssam 	if (mb->mb_harderr) {
111032633Ssam 		if (mb->mb_harderr < NHERRS)
111132633Ssam 			cp = mpherrmsg[mb->mb_harderr];
111232633Ssam 		else
111332633Ssam 			cp = "unknown error";
111432633Ssam 		log(LOG_ERR, "mp%d: hard error, %s", unit, cp);
111532633Ssam 		if (mb->mb_status == MP_OPOPEN) {
111632633Ssam 			for (i = 0; i < MPMAXPORT; i++) {
111732633Ssam 				mpcleanport(mb, i);
111832633Ssam 				mb->mb_proto[i] = MPPROTO_UNUSED;
111932633Ssam 			}
112032633Ssam 		}
112132633Ssam 		mb->mb_harderr = 0;
112232633Ssam 		mb->mb_status = 0;
112332633Ssam 	}
112432633Ssam }
112532633Ssam 
112632633Ssam mppurge(addr, cc)
112732633Ssam 	register caddr_t addr;
112832633Ssam 	register int cc;
112932633Ssam {
113032633Ssam 
113132633Ssam 	for (; cc >= 0; addr += NBPG, cc -= NBPG)
113232633Ssam 		mtpr(P1DC, addr);
113332633Ssam }
113432633Ssam 
113532633Ssam /*
113632633Ssam  * MPCC Download Pseudo-device.
113732633Ssam  */
113832633Ssam char	mpdlbuf[MPDLBUFSIZE];
113932633Ssam int	mpdlbusy;		/* interlock on download buffer */
114032633Ssam int	mpdlerr;
114132633Ssam 
114232633Ssam mpdlopen(dev)
114332633Ssam 	dev_t dev;
114432633Ssam {
114532633Ssam 	int unit, mpu;
114632633Ssam 	struct vba_device *vi;
114732633Ssam 
114832633Ssam 	unit = minor(dev);
114932633Ssam 	mpu = MPUNIT(unit);
115032633Ssam 	if (mpu >= NMP || (vi = mpinfo[mpu]) == 0 || vi->ui_alive == 0)
115132633Ssam 		return (ENODEV);
115232633Ssam 	return (0);
115332633Ssam }
115432633Ssam 
115532633Ssam mpdlwrite(dev, uio)
115632633Ssam 	dev_t dev;
115732633Ssam 	struct uio *uio;
115832633Ssam {
115932633Ssam 	register struct mpsoftc *ms = &mp_softc[MPUNIT(minor(dev))];
116032633Ssam 	register struct mpdl *dl;
116132633Ssam 	int error;
116232633Ssam 
116332633Ssam 	if (ms->ms_mb == 0 || ms->ms_mb->mb_status != MP_DLOPEN)
116432633Ssam 		return (EFAULT);
116532633Ssam 	dl = &ms->ms_mb->mb_dl;
116632633Ssam 	dl->mpdl_count = uio->uio_iov->iov_len;
116734506Skarels 	dl->mpdl_data = (caddr_t) kvtophys(mpdlbuf);
116834506Skarels 	if (error = uiomove(mpdlbuf, (int)dl->mpdl_count, UIO_WRITE, uio))
116932633Ssam 		return (error);
117032633Ssam 	uio->uio_resid -= dl->mpdl_count;    /* set up return from write */
117132633Ssam 	dl->mpdl_cmd = MPDLCMD_NORMAL;
117232633Ssam 	error = mpdlwait(dl);
117332633Ssam 	return (error);
117432633Ssam }
117532633Ssam 
117632633Ssam mpdlclose(dev)
117732633Ssam 	dev_t dev;
117832633Ssam {
117932633Ssam 	register struct mblok *mb = mp_softc[MPUNIT(minor(dev))].ms_mb;
118032633Ssam 
118132633Ssam 	if (mb == 0 || mb->mb_status != MP_DLDONE) {
118232633Ssam 		mpbogus.status = 0;
118332633Ssam 		if (mpbogus.mb == mpbogus.mbloks[MPUNIT(minor(dev))])
118432633Ssam 			mpdlbusy--;
118532633Ssam 		return (EEXIST);
118632633Ssam 	}
118732633Ssam 	mb->mb_status = MP_OPOPEN;
118832633Ssam 	mpbogus.status = 0;
118932633Ssam 	/* set to dead, for board handshake */
119032633Ssam 	mb->mb_hostint.imok = MPIMOK_DEAD;
119132633Ssam 	return (0);
119232633Ssam }
119332633Ssam 
119432633Ssam int	mpdltimeout();
119532633Ssam 
119634506Skarels /* ARGSUSED */
119732633Ssam mpdlioctl(dev, cmd, data, flag)
119832633Ssam 	dev_t dev;
119932633Ssam 	caddr_t data;
120032633Ssam {
120132633Ssam 	register struct mblok *mb;
120232633Ssam 	register struct mpdl *dl;
120334506Skarels 	int unit, error, s, i;
120432633Ssam 
120532633Ssam 	mb = mp_softc[unit=MPUNIT(minor(dev))].ms_mb;
120632633Ssam 	if (mb == 0)
120732633Ssam 		return (EEXIST);
120832633Ssam 	dl = &mb->mb_dl;
120932633Ssam 	error = 0;
121032633Ssam 	switch (cmd) {
121132633Ssam 	case MPIOPORTMAP:
121232633Ssam 		bcopy(data, (caddr_t)mb->mb_proto, sizeof (mb->mb_proto));
121332633Ssam 		break;
121432633Ssam 	case MPIOHILO:
121532633Ssam 		bcopy(data, (caddr_t)&mb->mb_hiport, 2*(sizeof(mb->mb_hiport)));
121632633Ssam 		break;
121732633Ssam 	case MPIOENDDL:
121832633Ssam 		dl->mpdl_count = 0;
121932633Ssam 		dl->mpdl_data = 0;
122032633Ssam 		dl->mpdl_cmd = MPIOENDDL&IOCPARM_MASK;
122132633Ssam 		error = mpdlwait(dl);
122232633Ssam 		mpccinit(unit);
122332633Ssam 		mb->mb_status = MP_DLDONE;
122432633Ssam 		mpdlbusy--;
122532633Ssam 		break;
122632633Ssam 	case MPIOENDCODE:
122732633Ssam 		dl->mpdl_count = 0;
122832633Ssam 		dl->mpdl_data = 0;
122932633Ssam 		dl->mpdl_cmd = MPIOENDCODE&IOCPARM_MASK;
123032633Ssam 		error = mpdlwait(dl);
123132633Ssam 		break;
123232633Ssam 	case MPIOASYNCNF:
123332633Ssam 		bcopy(data, mpdlbuf, sizeof (struct abdcf));
123434506Skarels 		dl->mpdl_data = (caddr_t) kvtophys(mpdlbuf);
123532633Ssam 		dl->mpdl_count = sizeof (struct abdcf);
123632633Ssam 		dl->mpdl_cmd = MPIOASYNCNF&IOCPARM_MASK;
123732633Ssam 		error = mpdlwait(dl);
123832633Ssam 		break;
123932633Ssam 	case MPIOSTARTDL:
124032633Ssam 		while (mpdlbusy)
124132633Ssam 			sleep((caddr_t)&mpdlbusy, PZERO+1);
124232633Ssam 		mpdlbusy++;
124332633Ssam 		/* initialize the downloading interface */
124432633Ssam 		mpbogus.magic = MPMAGIC;
124532633Ssam 		mpbogus.mb = mpbogus.mbloks[unit];
124632633Ssam 		mpbogus.status = 1;
124732633Ssam 		dl->mpdl_status = EVSTATUS_FREE;
124832633Ssam 		dl->mpdl_count = 0;
124932633Ssam 		dl->mpdl_cmd = 0;
125032633Ssam 		dl->mpdl_data = (char *) 0;
125132633Ssam 		mpdlerr = 0;
125232633Ssam 		mb->mb_magic = MPMAGIC;
125332633Ssam         	mb->mb_ivec = mp_softc[unit].ms_ivec+1;	/* download vector */
125432633Ssam 		mb->mb_status = MP_DLPEND;
125532633Ssam 		mb->mb_diagswitch[0] = 'A';
125632633Ssam 		mb->mb_diagswitch[1] = 'P';
125732633Ssam 		s = spl8();
125832633Ssam 		*(u_short *)mpinfo[unit]->ui_addr = 2;
125934506Skarels 		timeout(mpdltimeout, (caddr_t)mb, 30*hz);
126032633Ssam 		sleep((caddr_t)&mb->mb_status, PZERO+1);
126132633Ssam 		splx(s);
126232633Ssam 		if (mb->mb_status == MP_DLOPEN) {
126334506Skarels 			untimeout(mpdltimeout, (caddr_t)mb);
126432633Ssam 		} else if (mb->mb_status == MP_DLTIME) {
126532633Ssam 			mpbogus.status = 0;
126632633Ssam 			error = ETIMEDOUT;
126732633Ssam 		} else {
126832633Ssam 			mpbogus.status = 0;
126932633Ssam 			error = ENXIO;
127032633Ssam 			log(LOG_ERR, "mp%d: start download: unknown status %x",
127132633Ssam 			    unit, mb->mb_status);
127232633Ssam 		}
127334506Skarels 		bzero((caddr_t)mb->mb_port, sizeof (mb->mb_port));
127432633Ssam 		break;
127532633Ssam 	case MPIORESETBOARD:
127632633Ssam 		s = spl8();
127732633Ssam 		if (mb->mb_imokclk)
127832633Ssam 			mb->mb_imokclk = 0;
127932633Ssam 		*(u_short *)mpinfo[unit]->ui_addr = 0x100;
128032633Ssam 		if (mb->mb_status == MP_DLOPEN || mb->mb_status == MP_DLDONE) {
128132633Ssam 			mpdlerr = MP_DLERROR;
128232633Ssam 			dl->mpdl_status = EVSTATUS_FREE;
128332633Ssam 			wakeup((caddr_t)&dl->mpdl_status);
128432633Ssam 			mpbogus.status = 0;
128532633Ssam 		}
128632633Ssam 		for (i = 0; i < MPMAXPORT; i++) {
128732633Ssam 			if (mb->mb_harderr || mb->mb_softerr)
128832633Ssam 				mperror(mb, i);
128932633Ssam 			mpcleanport(mb, i);
129032633Ssam 			mb->mb_proto[i] = MPPROTO_UNUSED;
129132633Ssam 		}
129232633Ssam 		mb->mb_status = 0;
129332633Ssam 		splx(s);
129432633Ssam 		break;
129532633Ssam 	default:
129632633Ssam 		error = EINVAL;
129732633Ssam 		break;
129832633Ssam 	}
129932633Ssam 	return (error);
130032633Ssam }
130132633Ssam 
130232633Ssam mpccinit(unit)
130332633Ssam 	int unit;
130432633Ssam {
130532633Ssam         register struct mblok *mb = mp_softc[unit].ms_mb;
130632633Ssam         register struct his *his;
130732633Ssam         register int i, j;
130832633Ssam 
130932633Ssam         mb->mb_status = MP_DLDONE;
131032633Ssam         mb->mb_ivec = mp_softc[unit].ms_ivec;
131132633Ssam         mb->mb_magic = MPMAGIC;
131232633Ssam         /* Init host interface structure */
131332633Ssam         his = &mb->mb_hostint;
131432633Ssam         his->semaphore = MPSEMA_AVAILABLE;
131532633Ssam         for (i = 0; i < NMPPROTO; i++)
131632633Ssam                 for (j = 0; j < MPMAXPORT; j++) {
131732633Ssam                         his->proto[i].inbdone[j] = MPPORT_EOL;
131832633Ssam                         his->proto[i].outbdone[j] = MPPORT_EOL;
131932633Ssam                 }
132032633Ssam         mb->mb_unit = unit;
132132633Ssam }
132232633Ssam 
132332633Ssam mpdlintr(mpcc)
132432633Ssam 	int mpcc;
132532633Ssam {
132632633Ssam 	register struct mblok *mb;
132732633Ssam 	register struct mpdl *dl;
132832633Ssam 
132932633Ssam 	mb = mp_softc[mpcc].ms_mb;
133032633Ssam 	if (mb == 0) {
133132633Ssam 		printf("mp%d: stray download interrupt\n", mpcc);
133232633Ssam 		return;
133332633Ssam 	}
133432633Ssam 	dl = &mb->mb_dl;
133532633Ssam 	switch (mb->mb_status) {
133632633Ssam 	case MP_DLOPEN:
133732633Ssam 		if (dl->mpdl_status != EVSTATUS_DONE)
133832633Ssam 			mpdlerr = MP_DLERROR;
133932633Ssam 		dl->mpdl_status = EVSTATUS_FREE;
134032633Ssam 		wakeup((caddr_t)&dl->mpdl_status);
134132633Ssam 		return;
134232633Ssam 	case MP_DLPEND:
134332633Ssam 		mb->mb_status = MP_DLOPEN;
134434506Skarels 		wakeup((caddr_t)&mb->mb_status);
134532633Ssam 		/* fall thru... */
134632633Ssam 	case MP_DLTIME:
134732633Ssam 		return;
134832633Ssam 	case MP_OPOPEN:
134932633Ssam 		if (mb->mb_imokclk)
135032633Ssam 			mb->mb_imokclk = 0;
135132633Ssam 		mb->mb_nointcnt = 0;		/* reset no interrupt count */
135232633Ssam 		mb->mb_hostint.imok = MPIMOK_DEAD;
135332633Ssam 		mb->mb_imokclk = (caddr_t)1;
135432633Ssam 		break;
135532633Ssam 	default:
135632633Ssam 		log(LOG_ERR, "mp%d: mpdlintr, status %x\n",
135732633Ssam 		    mpcc, mb->mb_status);
135832633Ssam 		break;
135932633Ssam 	}
136032633Ssam }
136132633Ssam 
136232633Ssam mpdltimeout(mp)
136332633Ssam 	struct mblok *mp;
136432633Ssam {
136532633Ssam 
136632633Ssam 	mp->mb_status = MP_DLTIME;
136732633Ssam 	wakeup((caddr_t)&mp->mb_status);
136832633Ssam }
136932633Ssam 
137032633Ssam /*
137132633Ssam  * Wait for a transfer to complete or a timeout to occur.
137232633Ssam  */
137332633Ssam mpdlwait(dl)
137432633Ssam 	register struct mpdl *dl;
137532633Ssam {
137632633Ssam 	int s, error = 0;
137732633Ssam 
137832633Ssam 	s = spl8();
137932633Ssam 	dl->mpdl_status = EVSTATUS_GO;
138032633Ssam 	while (dl->mpdl_status != EVSTATUS_FREE) {
138132633Ssam 		sleep((caddr_t)&dl->mpdl_status, PZERO+1);
138232633Ssam 		if (mpdlerr == MP_DLERROR)
138332633Ssam 			error = EIO;
138432633Ssam 	}
138532633Ssam 	splx(s);
138632633Ssam 	return (error);
138732633Ssam }
138832633Ssam #endif
1389