xref: /csrg-svn/sys/vax/uba/dh.c (revision 29209)
123320Smckusick /*
2*29209Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323320Smckusick  * All rights reserved.  The Berkeley software License Agreement
423320Smckusick  * specifies the terms and conditions for redistribution.
523320Smckusick  *
6*29209Smckusick  *	@(#)dh.c	7.1 (Berkeley) 06/05/86
723320Smckusick  */
813Sbill 
91934Swnj #include "dh.h"
102643Swnj #if NDH > 0
1113Sbill /*
122479Swnj  * DH-11/DM-11 driver
1313Sbill  */
149771Ssam #include "../machine/pte.h"
159771Ssam 
162730Swnj #include "bk.h"
1716062Skarels #include "uba.h"
1817122Sbloom #include "param.h"
1917122Sbloom #include "conf.h"
2017122Sbloom #include "dir.h"
2117122Sbloom #include "user.h"
2217122Sbloom #include "proc.h"
2317122Sbloom #include "ioctl.h"
2417122Sbloom #include "tty.h"
2517122Sbloom #include "map.h"
2617122Sbloom #include "buf.h"
2717122Sbloom #include "vm.h"
2817122Sbloom #include "kernel.h"
2918311Sralph #include "syslog.h"
308472Sroot 
3117122Sbloom #include "ubareg.h"
3217122Sbloom #include "ubavar.h"
3317122Sbloom #include "dhreg.h"
3417122Sbloom #include "dmreg.h"
358472Sroot 
3617122Sbloom #include "bkmac.h"
3717122Sbloom #include "clist.h"
3817122Sbloom #include "file.h"
3917122Sbloom #include "uio.h"
4013Sbill 
412468Swnj /*
422479Swnj  * Definition of the driver for the auto-configuration program.
432479Swnj  * There is one definition for the dh and one for the dm.
442468Swnj  */
4516190Skarels int	dhprobe(), dhattach(), dhrint(), dhxint(), dhtimer();
462974Swnj struct	uba_device *dhinfo[NDH];
472395Swnj u_short	dhstd[] = { 0 };
482395Swnj struct	uba_driver dhdriver =
492605Swnj 	{ dhprobe, 0, dhattach, 0, dhstd, "dh", dhinfo };
502395Swnj 
512605Swnj int	dmprobe(), dmattach(), dmintr();
522974Swnj struct	uba_device *dminfo[NDH];
532479Swnj u_short	dmstd[] = { 0 };
542479Swnj struct	uba_driver dmdriver =
552605Swnj 	{ dmprobe, 0, dmattach, 0, dmstd, "dm", dminfo };
5613Sbill 
576615Ssam #ifndef	PORTSELECTOR
5825394Skarels #define	ISPEED	B9600
596615Ssam #define	IFLAGS	(EVENP|ODDP|ECHO)
606615Ssam #else
616615Ssam #define	ISPEED	B4800
626615Ssam #define	IFLAGS	(EVENP|ODDP)
636615Ssam #endif
646615Ssam 
6516190Skarels #define	FASTTIMER	(hz/30)		/* scan rate with silos on */
6616190Skarels 
6713Sbill /*
682479Swnj  * Local variables for the driver
6913Sbill  */
702643Swnj short	dhsar[NDH];			/* software copy of last bar */
712643Swnj short	dhsoftCAR[NDH];
7213Sbill 
732643Swnj struct	tty dh11[NDH*16];
742643Swnj int	ndh11	= NDH*16;
752479Swnj int	dhact;				/* mask of active dh's */
7616190Skarels int	dhsilos;			/* mask of dh's with silo in use */
7716190Skarels int	dhchars[NDH];			/* recent input count */
7816190Skarels int	dhrate[NDH];			/* smoothed input count */
7916190Skarels int	dhhighrate = 100;		/* silo on if dhchars > dhhighrate */
8016190Skarels int	dhlowrate = 75;			/* silo off if dhrate < dhlowrate */
8116190Skarels static short timerstarted;
822479Swnj int	dhstart(), ttrstrt();
8313Sbill 
842479Swnj /*
852479Swnj  * The clist space is mapped by the driver onto each UNIBUS.
862479Swnj  * The UBACVT macro converts a clist space address for unibus uban
872479Swnj  * into an i/o space address for the DMA routine.
882479Swnj  */
8916062Skarels int	dh_ubinfo[NUBA];		/* info about allocated unibus map */
9016062Skarels int	cbase[NUBA];			/* base address in unibus map */
912479Swnj #define	UBACVT(x, uban)		(cbase[uban] + ((x)-(char *)cfree))
9213Sbill 
932456Swnj /*
942456Swnj  * Routine for configuration to force a dh to interrupt.
952456Swnj  * Set to transmit at 9600 baud, and cause a transmitter interrupt.
962456Swnj  */
972468Swnj /*ARGSUSED*/
982605Swnj dhprobe(reg)
992395Swnj 	caddr_t reg;
1002395Swnj {
1012468Swnj 	register int br, cvec;		/* these are ``value-result'' */
1022479Swnj 	register struct dhdevice *dhaddr = (struct dhdevice *)reg;
1032395Swnj 
1042605Swnj #ifdef lint
1052605Swnj 	br = 0; cvec = br; br = cvec;
1067384Sroot 	if (ndh11 == 0) ndh11 = 1;
1074932Swnj 	dhrint(0); dhxint(0);
1082605Swnj #endif
1092696Swnj #ifndef notdef
1102566Swnj 	dhaddr->un.dhcsr = DH_RIE|DH_MM|DH_RI;
1116380Swnj 	DELAY(1000);
1127384Sroot 	dhaddr->un.dhcsr &= ~DH_RI;
1132566Swnj 	dhaddr->un.dhcsr = 0;
1142566Swnj #else
1152456Swnj 	dhaddr->un.dhcsr = DH_TIE;
1162456Swnj 	DELAY(5);
1172456Swnj 	dhaddr->dhlpr = (B9600 << 10) | (B9600 << 6) | BITS7|PENABLE;
1182421Skre 	dhaddr->dhbcr = -1;
1192456Swnj 	dhaddr->dhcar = 0;
1202421Skre 	dhaddr->dhbar = 1;
1212456Swnj 	DELAY(100000);		/* wait 1/10'th of a sec for interrupt */
1222421Skre 	dhaddr->un.dhcsr = 0;
1232456Swnj 	if (cvec && cvec != 0x200)
1242456Swnj 		cvec -= 4;		/* transmit -> receive */
1252482Swnj #endif
1267408Skre 	return (sizeof (struct dhdevice));
1272395Swnj }
1282395Swnj 
1292456Swnj /*
1302605Swnj  * Routine called to attach a dh.
1312456Swnj  */
1322605Swnj dhattach(ui)
1332974Swnj 	struct uba_device *ui;
1342395Swnj {
1352395Swnj 
1362566Swnj 	dhsoftCAR[ui->ui_unit] = ui->ui_flags;
13726219Skarels 	cbase[ui->ui_ubanum] = -1;
1382395Swnj }
1392395Swnj 
14013Sbill /*
1412479Swnj  * Configuration routine to cause a dm to interrupt.
1422479Swnj  */
1432605Swnj dmprobe(reg)
1442605Swnj 	caddr_t reg;
1452479Swnj {
1462479Swnj 	register int br, vec;			/* value-result */
1472605Swnj 	register struct dmdevice *dmaddr = (struct dmdevice *)reg;
1482479Swnj 
1492605Swnj #ifdef lint
1503101Swnj 	br = 0; vec = br; br = vec;
1516185Ssam 	dmintr(0);
1522605Swnj #endif
1532479Swnj 	dmaddr->dmcsr = DM_DONE|DM_IE;
1542479Swnj 	DELAY(20);
1552479Swnj 	dmaddr->dmcsr = 0;
1562605Swnj 	return (1);
1572479Swnj }
1582479Swnj 
1592605Swnj /*ARGSUSED*/
1602605Swnj dmattach(ui)
1612974Swnj 	struct uba_device *ui;
1622479Swnj {
1632479Swnj 
1642479Swnj 	/* no local state to set up */
1652479Swnj }
1662479Swnj 
1672479Swnj /*
1682468Swnj  * Open a DH11 line, mapping the clist onto the uba if this
1692468Swnj  * is the first dh on this uba.  Turn on this dh if this is
1702468Swnj  * the first use of it.  Also do a dmopen to wait for carrier.
17113Sbill  */
17213Sbill /*ARGSUSED*/
17313Sbill dhopen(dev, flag)
1742395Swnj 	dev_t dev;
17513Sbill {
17613Sbill 	register struct tty *tp;
1772395Swnj 	register int unit, dh;
1782479Swnj 	register struct dhdevice *addr;
1792974Swnj 	register struct uba_device *ui;
18013Sbill 	int s;
18113Sbill 
1822395Swnj 	unit = minor(dev);
1832395Swnj 	dh = unit >> 4;
1848566Sroot 	if (unit >= NDH*16 || (ui = dhinfo[dh])== 0 || ui->ui_alive == 0)
1858566Sroot 		return (ENXIO);
1862395Swnj 	tp = &dh11[unit];
1878566Sroot 	if (tp->t_state&TS_XCLUDE && u.u_uid!=0)
1888566Sroot 		return (EBUSY);
1892479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
19013Sbill 	tp->t_addr = (caddr_t)addr;
19113Sbill 	tp->t_oproc = dhstart;
1925406Swnj 	tp->t_state |= TS_WOPEN;
1932468Swnj 	/*
1942468Swnj 	 * While setting up state for this uba and this dh,
1952468Swnj 	 * block uba resets which can clear the state.
1962468Swnj 	 */
1972468Swnj 	s = spl5();
19826219Skarels 	if (cbase[ui->ui_ubanum] == -1) {
1992395Swnj 		dh_ubinfo[ui->ui_ubanum] =
2002421Skre 		    uballoc(ui->ui_ubanum, (caddr_t)cfree,
20125433Skarels 			nclist*sizeof(struct cblock), 0);
20226219Skarels 		cbase[ui->ui_ubanum] = UBAI_ADDR(dh_ubinfo[ui->ui_ubanum]);
20313Sbill 	}
20416190Skarels 	if (timerstarted == 0) {
20516190Skarels 		timerstarted++;
20616190Skarels 		timeout(dhtimer, (caddr_t) 0, hz);
20716190Skarels 	}
2082456Swnj 	if ((dhact&(1<<dh)) == 0) {
2092456Swnj 		addr->un.dhcsr |= DH_IE;
2102468Swnj 		dhact |= (1<<dh);
21116190Skarels 		addr->dhsilo = 0;
2122456Swnj 	}
21313Sbill 	splx(s);
2142468Swnj 	/*
21527049Skarels 	 * If this is first open, initialize tty state to default.
2162468Swnj 	 */
2175406Swnj 	if ((tp->t_state&TS_ISOPEN) == 0) {
21813Sbill 		ttychars(tp);
21927049Skarels #ifndef PORTSELECTOR
22027049Skarels 		if (tp->t_ispeed == 0) {
22127049Skarels #else
22227049Skarels 			tp->t_state |= TS_HUPCLS;
22327049Skarels #endif PORTSELECTOR
22427049Skarels 			tp->t_ispeed = ISPEED;
22527049Skarels 			tp->t_ospeed = ISPEED;
22627049Skarels 			tp->t_flags = IFLAGS;
22727049Skarels #ifndef PORTSELECTOR
22827049Skarels 		}
22927049Skarels #endif PORTSELECTOR
2302395Swnj 		dhparam(unit);
23113Sbill 	}
2322468Swnj 	/*
2332468Swnj 	 * Wait for carrier, then process line discipline specific open.
2342468Swnj 	 */
23513Sbill 	dmopen(dev);
2368566Sroot 	return ((*linesw[tp->t_line].l_open)(dev, tp));
23713Sbill }
23813Sbill 
23913Sbill /*
2402468Swnj  * Close a DH11 line, turning off the DM11.
24113Sbill  */
24213Sbill /*ARGSUSED*/
24313Sbill dhclose(dev, flag)
2442395Swnj 	dev_t dev;
2452395Swnj 	int flag;
24613Sbill {
24713Sbill 	register struct tty *tp;
2482395Swnj 	register unit;
24913Sbill 
2502395Swnj 	unit = minor(dev);
2512395Swnj 	tp = &dh11[unit];
25213Sbill 	(*linesw[tp->t_line].l_close)(tp);
2532479Swnj 	((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
2545406Swnj 	if (tp->t_state&TS_HUPCLS || (tp->t_state&TS_ISOPEN)==0)
2552479Swnj 		dmctl(unit, DML_OFF, DMSET);
25613Sbill 	ttyclose(tp);
25713Sbill }
25813Sbill 
2597725Sroot dhread(dev, uio)
2602395Swnj 	dev_t dev;
2617725Sroot 	struct uio *uio;
26213Sbill {
2638490Sroot 	register struct tty *tp = &dh11[minor(dev)];
26413Sbill 
2657725Sroot 	return ((*linesw[tp->t_line].l_read)(tp, uio));
26613Sbill }
26713Sbill 
2687831Sroot dhwrite(dev, uio)
2692395Swnj 	dev_t dev;
2707831Sroot 	struct uio *uio;
27113Sbill {
2728490Sroot 	register struct tty *tp = &dh11[minor(dev)];
27313Sbill 
2748490Sroot 	return ((*linesw[tp->t_line].l_write)(tp, uio));
27513Sbill }
27613Sbill 
27713Sbill /*
27813Sbill  * DH11 receiver interrupt.
27913Sbill  */
2802395Swnj dhrint(dh)
2812395Swnj 	int dh;
28213Sbill {
28313Sbill 	register struct tty *tp;
2842395Swnj 	register c;
2852479Swnj 	register struct dhdevice *addr;
286117Sbill 	register struct tty *tp0;
2872974Swnj 	register struct uba_device *ui;
2882924Swnj 	int overrun = 0;
28913Sbill 
2902395Swnj 	ui = dhinfo[dh];
2912479Swnj 	if (ui == 0 || ui->ui_alive == 0)
2922479Swnj 		return;
2932479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
2942468Swnj 	tp0 = &dh11[dh<<4];
2952468Swnj 	/*
2962468Swnj 	 * Loop fetching characters from the silo for this
2972468Swnj 	 * dh until there are no more in the silo.
2982468Swnj 	 */
2992468Swnj 	while ((c = addr->dhrcr) < 0) {
3002468Swnj 		tp = tp0 + ((c>>8)&0xf);
30116190Skarels 		dhchars[dh]++;
3025406Swnj 		if ((tp->t_state&TS_ISOPEN)==0) {
30325394Skarels 			wakeup((caddr_t)&tp->t_rawq);
30425394Skarels #ifdef PORTSELECTOR
30525394Skarels 			if ((tp->t_state&TS_WOPEN) == 0)
3066615Ssam #endif
30725394Skarels 				continue;
30813Sbill 		}
3092468Swnj 		if (c & DH_PE)
31013Sbill 			if ((tp->t_flags&(EVENP|ODDP))==EVENP
31113Sbill 			 || (tp->t_flags&(EVENP|ODDP))==ODDP )
31213Sbill 				continue;
3132924Swnj 		if ((c & DH_DO) && overrun == 0) {
31424840Seric 			log(LOG_WARNING, "dh%d: silo overflow\n", dh);
3152924Swnj 			overrun = 1;
3162924Swnj 		}
3172468Swnj 		if (c & DH_FE)
3182468Swnj 			/*
3192468Swnj 			 * At framing error (break) generate
3202468Swnj 			 * a null (in raw mode, for getty), or a
3212468Swnj 			 * interrupt (in cooked/cbreak mode).
3222468Swnj 			 */
32313Sbill 			if (tp->t_flags&RAW)
3242468Swnj 				c = 0;
32513Sbill 			else
3269549Ssam 				c = tp->t_intrc;
3272730Swnj #if NBK > 0
328139Sbill 		if (tp->t_line == NETLDISC) {
329117Sbill 			c &= 0177;
330168Sbill 			BKINPUT(c, tp);
331117Sbill 		} else
3322730Swnj #endif
3332468Swnj 			(*linesw[tp->t_line].l_rint)(c, tp);
33413Sbill 	}
33513Sbill }
33613Sbill 
33713Sbill /*
3382468Swnj  * Ioctl for DH11.
33913Sbill  */
34013Sbill /*ARGSUSED*/
3417629Ssam dhioctl(dev, cmd, data, flag)
3427629Ssam 	caddr_t data;
34313Sbill {
34413Sbill 	register struct tty *tp;
3458566Sroot 	register int unit = minor(dev);
3468566Sroot 	int error;
34713Sbill 
3482395Swnj 	tp = &dh11[unit];
3498566Sroot 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
3508566Sroot 	if (error >= 0)
3518566Sroot 		return (error);
3528566Sroot 	error = ttioctl(tp, cmd, data, flag);
3538566Sroot 	if (error >= 0) {
35417561Sbloom 		if (cmd == TIOCSETP || cmd == TIOCSETN || cmd == TIOCLBIS ||
35517561Sbloom 		    cmd == TIOCLBIC || cmd == TIOCLSET)
3562395Swnj 			dhparam(unit);
3578566Sroot 		return (error);
3588566Sroot 	}
3598566Sroot 	switch (cmd) {
3607629Ssam 
361168Sbill 	case TIOCSBRK:
3622479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak |= 1<<(unit&017);
363168Sbill 		break;
3647629Ssam 
365168Sbill 	case TIOCCBRK:
3662479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
367168Sbill 		break;
3687629Ssam 
369168Sbill 	case TIOCSDTR:
3702479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIS);
371168Sbill 		break;
3727629Ssam 
373168Sbill 	case TIOCCDTR:
3742479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIC);
375168Sbill 		break;
3767629Ssam 
377168Sbill 	default:
3788566Sroot 		return (ENOTTY);
379168Sbill 	}
3808566Sroot 	return (0);
38113Sbill }
38213Sbill 
38313Sbill /*
38413Sbill  * Set parameters from open or stty into the DH hardware
38513Sbill  * registers.
38613Sbill  */
3872395Swnj dhparam(unit)
3882395Swnj 	register int unit;
38913Sbill {
39013Sbill 	register struct tty *tp;
3912479Swnj 	register struct dhdevice *addr;
3922395Swnj 	register int lpar;
393300Sbill 	int s;
39413Sbill 
3952395Swnj 	tp = &dh11[unit];
3962479Swnj 	addr = (struct dhdevice *)tp->t_addr;
3972468Swnj 	/*
3982468Swnj 	 * Block interrupts so parameters will be set
3992468Swnj 	 * before the line interrupts.
4002468Swnj 	 */
401300Sbill 	s = spl5();
4022468Swnj 	addr->un.dhcsrl = (unit&0xf) | DH_IE;
40313Sbill 	if ((tp->t_ispeed)==0) {
4045406Swnj 		tp->t_state |= TS_HUPCLS;
4052479Swnj 		dmctl(unit, DML_OFF, DMSET);
40628147Skarels 		splx(s);
40713Sbill 		return;
40813Sbill 	}
4092395Swnj 	lpar = ((tp->t_ospeed)<<10) | ((tp->t_ispeed)<<6);
4102468Swnj 	if ((tp->t_ispeed) == B134)
4112395Swnj 		lpar |= BITS6|PENABLE|HDUPLX;
41224269Slepreau 	else if (tp->t_flags & (RAW|LITOUT|PASS8))
4132395Swnj 		lpar |= BITS8;
41413Sbill 	else
4152395Swnj 		lpar |= BITS7|PENABLE;
41613Sbill 	if ((tp->t_flags&EVENP) == 0)
4172395Swnj 		lpar |= OPAR;
4182468Swnj 	if ((tp->t_ospeed) == B110)
4192395Swnj 		lpar |= TWOSB;
4202395Swnj 	addr->dhlpr = lpar;
421300Sbill 	splx(s);
42213Sbill }
42313Sbill 
42413Sbill /*
42513Sbill  * DH11 transmitter interrupt.
42613Sbill  * Restart each line which used to be active but has
42713Sbill  * terminated transmission since the last interrupt.
42813Sbill  */
4292395Swnj dhxint(dh)
4302395Swnj 	int dh;
43113Sbill {
43213Sbill 	register struct tty *tp;
4332479Swnj 	register struct dhdevice *addr;
43413Sbill 	short ttybit, bar, *sbar;
4352974Swnj 	register struct uba_device *ui;
4362468Swnj 	register int unit;
4372605Swnj 	u_short cntr;
43813Sbill 
4392395Swnj 	ui = dhinfo[dh];
4402479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
4412456Swnj 	if (addr->un.dhcsr & DH_NXM) {
4422456Swnj 		addr->un.dhcsr |= DH_CNI;
4432924Swnj 		printf("dh%d: NXM\n", dh);
444105Sbill 	}
4452395Swnj 	sbar = &dhsar[dh];
44613Sbill 	bar = *sbar & ~addr->dhbar;
4472395Swnj 	unit = dh * 16; ttybit = 1;
4482468Swnj 	addr->un.dhcsr &= (short)~DH_TI;
4492468Swnj 	for (; bar; unit++, ttybit <<= 1) {
4502468Swnj 		if (bar & ttybit) {
45113Sbill 			*sbar &= ~ttybit;
45213Sbill 			bar &= ~ttybit;
4532395Swnj 			tp = &dh11[unit];
4545406Swnj 			tp->t_state &= ~TS_BUSY;
4555406Swnj 			if (tp->t_state&TS_FLUSH)
4565406Swnj 				tp->t_state &= ~TS_FLUSH;
457113Sbill 			else {
4582456Swnj 				addr->un.dhcsrl = (unit&017)|DH_IE;
4592468Swnj 				/*
4602468Swnj 				 * Do arithmetic in a short to make up
4612468Swnj 				 * for lost 16&17 bits.
4622468Swnj 				 */
4632605Swnj 				cntr = addr->dhcar -
4642468Swnj 				    UBACVT(tp->t_outq.c_cf, ui->ui_ubanum);
4653101Swnj 				ndflush(&tp->t_outq, (int)cntr);
466113Sbill 			}
467113Sbill 			if (tp->t_line)
46813Sbill 				(*linesw[tp->t_line].l_start)(tp);
469113Sbill 			else
47013Sbill 				dhstart(tp);
47113Sbill 		}
47213Sbill 	}
47313Sbill }
47413Sbill 
47513Sbill /*
47613Sbill  * Start (restart) transmission on the given DH11 line.
47713Sbill  */
47813Sbill dhstart(tp)
4792395Swnj 	register struct tty *tp;
48013Sbill {
4812479Swnj 	register struct dhdevice *addr;
4822468Swnj 	register int car, dh, unit, nch;
4832395Swnj 	int s;
48413Sbill 
4852468Swnj 	unit = minor(tp->t_dev);
4862468Swnj 	dh = unit >> 4;
4872468Swnj 	unit &= 0xf;
4882479Swnj 	addr = (struct dhdevice *)tp->t_addr;
4892468Swnj 
49013Sbill 	/*
4912468Swnj 	 * Must hold interrupts in following code to prevent
4922468Swnj 	 * state of the tp from changing.
49313Sbill 	 */
49413Sbill 	s = spl5();
4952468Swnj 	/*
4962468Swnj 	 * If it's currently active, or delaying, no need to do anything.
4972468Swnj 	 */
4985406Swnj 	if (tp->t_state&(TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
49913Sbill 		goto out;
5002468Swnj 	/*
5012468Swnj 	 * If there are sleepers, and output has drained below low
5022468Swnj 	 * water mark, wake up the sleepers.
5032468Swnj 	 */
5045406Swnj 	if (tp->t_outq.c_cc<=TTLOWAT(tp)) {
5055406Swnj 		if (tp->t_state&TS_ASLEEP) {
5065406Swnj 			tp->t_state &= ~TS_ASLEEP;
5075406Swnj 			wakeup((caddr_t)&tp->t_outq);
5085406Swnj 		}
5095406Swnj 		if (tp->t_wsel) {
5105406Swnj 			selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
5115406Swnj 			tp->t_wsel = 0;
5125406Swnj 			tp->t_state &= ~TS_WCOLL;
5135406Swnj 		}
51413Sbill 	}
5152468Swnj 	/*
5162468Swnj 	 * Now restart transmission unless the output queue is
5172468Swnj 	 * empty.
5182468Swnj 	 */
51913Sbill 	if (tp->t_outq.c_cc == 0)
52013Sbill 		goto out;
5219549Ssam 	if (tp->t_flags & (RAW|LITOUT))
52213Sbill 		nch = ndqb(&tp->t_outq, 0);
5232395Swnj 	else {
52413Sbill 		nch = ndqb(&tp->t_outq, 0200);
5252468Swnj 		/*
5262468Swnj 		 * If first thing on queue is a delay process it.
5272468Swnj 		 */
52813Sbill 		if (nch == 0) {
52913Sbill 			nch = getc(&tp->t_outq);
5302468Swnj 			timeout(ttrstrt, (caddr_t)tp, (nch&0x7f)+6);
5315406Swnj 			tp->t_state |= TS_TIMEOUT;
53213Sbill 			goto out;
53313Sbill 		}
53413Sbill 	}
5352468Swnj 	/*
5362468Swnj 	 * If characters to transmit, restart transmission.
5372468Swnj 	 */
53813Sbill 	if (nch) {
5392468Swnj 		car = UBACVT(tp->t_outq.c_cf, dhinfo[dh]->ui_ubanum);
5402468Swnj 		addr->un.dhcsrl = unit|((car>>12)&0x30)|DH_IE;
5413586Sroot 		/*
5423586Sroot 		 * The following nonsense with short word
5433586Sroot 		 * is to make sure the dhbar |= word below
5443586Sroot 		 * is done with an interlocking bisw2 instruction.
5453586Sroot 		 */
5463586Sroot 		{ short word = 1 << unit;
5473586Sroot 		dhsar[dh] |= word;
5482468Swnj 		addr->dhcar = car;
54913Sbill 		addr->dhbcr = -nch;
5503586Sroot 		addr->dhbar |= word;
5513586Sroot 		}
5525406Swnj 		tp->t_state |= TS_BUSY;
55313Sbill 	}
5542395Swnj out:
55513Sbill 	splx(s);
55613Sbill }
55713Sbill 
55813Sbill /*
5592468Swnj  * Stop output on a line, e.g. for ^S/^Q or output flush.
56013Sbill  */
56113Sbill /*ARGSUSED*/
56213Sbill dhstop(tp, flag)
5632468Swnj 	register struct tty *tp;
56413Sbill {
5652479Swnj 	register struct dhdevice *addr;
5662395Swnj 	register int unit, s;
56713Sbill 
5682479Swnj 	addr = (struct dhdevice *)tp->t_addr;
5692468Swnj 	/*
5702468Swnj 	 * Block input/output interrupts while messing with state.
5712468Swnj 	 */
5722468Swnj 	s = spl5();
5735406Swnj 	if (tp->t_state & TS_BUSY) {
5742468Swnj 		/*
5752468Swnj 		 * Device is transmitting; stop output
5762468Swnj 		 * by selecting the line and setting the byte
5772468Swnj 		 * count to -1.  We will clean up later
5782468Swnj 		 * by examining the address where the dh stopped.
5792468Swnj 		 */
5802395Swnj 		unit = minor(tp->t_dev);
5812456Swnj 		addr->un.dhcsrl = (unit&017) | DH_IE;
5825406Swnj 		if ((tp->t_state&TS_TTSTOP)==0)
5835406Swnj 			tp->t_state |= TS_FLUSH;
584113Sbill 		addr->dhbcr = -1;
585113Sbill 	}
58613Sbill 	splx(s);
58713Sbill }
58813Sbill 
589168Sbill /*
590280Sbill  * Reset state of driver if UBA reset was necessary.
591280Sbill  * Reset the csrl and lpr registers on open lines, and
592280Sbill  * restart transmitters.
593280Sbill  */
5942395Swnj dhreset(uban)
5952468Swnj 	int uban;
596280Sbill {
5972395Swnj 	register int dh, unit;
598280Sbill 	register struct tty *tp;
5992974Swnj 	register struct uba_device *ui;
6002421Skre 	int i;
601280Sbill 
6022395Swnj 	dh = 0;
6032643Swnj 	for (dh = 0; dh < NDH; dh++) {
6042421Skre 		ui = dhinfo[dh];
6052421Skre 		if (ui == 0 || ui->ui_alive == 0 || ui->ui_ubanum != uban)
6062421Skre 			continue;
6072924Swnj 		printf(" dh%d", dh);
60826219Skarels 		if (dh_ubinfo[uban]) {
60925433Skarels 			dh_ubinfo[uban] = uballoc(uban, (caddr_t)cfree,
61025433Skarels 			    nclist*sizeof (struct cblock), 0);
61126219Skarels 			cbase[uban] = UBAI_ADDR(dh_ubinfo[uban]);
61225433Skarels 		}
6132479Swnj 		((struct dhdevice *)ui->ui_addr)->un.dhcsr |= DH_IE;
61416190Skarels 		((struct dhdevice *)ui->ui_addr)->dhsilo = 0;
6152421Skre 		unit = dh * 16;
6162421Skre 		for (i = 0; i < 16; i++) {
6172421Skre 			tp = &dh11[unit];
6185406Swnj 			if (tp->t_state & (TS_ISOPEN|TS_WOPEN)) {
6192421Skre 				dhparam(unit);
6202479Swnj 				dmctl(unit, DML_ON, DMSET);
6215406Swnj 				tp->t_state &= ~TS_BUSY;
6222421Skre 				dhstart(tp);
6232421Skre 			}
6242421Skre 			unit++;
625300Sbill 		}
626300Sbill 	}
62716190Skarels 	dhsilos = 0;
628280Sbill }
6292395Swnj 
63016190Skarels int dhtransitions, dhslowtimers, dhfasttimers;		/*DEBUG*/
6312468Swnj /*
63216190Skarels  * At software clock interrupt time, check status.
63316190Skarels  * Empty all the dh silos that are in use, and decide whether
63416190Skarels  * to turn any silos off or on.
6352468Swnj  */
6362456Swnj dhtimer()
6372456Swnj {
63816190Skarels 	register int dh, s;
63916190Skarels 	static int timercalls;
6402456Swnj 
64116190Skarels 	if (dhsilos) {
64216190Skarels 		dhfasttimers++;		/*DEBUG*/
64316190Skarels 		timercalls++;
64416190Skarels 		s = spl5();
64516190Skarels 		for (dh = 0; dh < NDH; dh++)
64616190Skarels 			if (dhsilos & (1 << dh))
64716190Skarels 				dhrint(dh);
64816190Skarels 		splx(s);
64916190Skarels 	}
65016190Skarels 	if ((dhsilos == 0) || ((timercalls += FASTTIMER) >= hz)) {
65116190Skarels 		dhslowtimers++;		/*DEBUG*/
65216190Skarels 		timercalls = 0;
65316190Skarels 		for (dh = 0; dh < NDH; dh++) {
65416190Skarels 		    ave(dhrate[dh], dhchars[dh], 8);
65516190Skarels 		    if ((dhchars[dh] > dhhighrate) &&
65616190Skarels 		      ((dhsilos & (1 << dh)) == 0)) {
65716190Skarels 			((struct dhdevice *)(dhinfo[dh]->ui_addr))->dhsilo =
65816190Skarels 			    (dhchars[dh] > 500? 32 : 16);
65916190Skarels 			dhsilos |= (1 << dh);
66016190Skarels 			dhtransitions++;		/*DEBUG*/
66116190Skarels 		    } else if ((dhsilos & (1 << dh)) &&
66216190Skarels 		      (dhrate[dh] < dhlowrate)) {
66316190Skarels 			((struct dhdevice *)(dhinfo[dh]->ui_addr))->dhsilo = 0;
66416190Skarels 			dhsilos &= ~(1 << dh);
66516190Skarels 		    }
66616190Skarels 		    dhchars[dh] = 0;
66716190Skarels 		}
66816190Skarels 	}
66916190Skarels 	timeout(dhtimer, (caddr_t) 0, dhsilos? FASTTIMER: hz);
6702456Swnj }
6712456Swnj 
6722468Swnj /*
6732479Swnj  * Turn on the line associated with dh dev.
6742468Swnj  */
6752468Swnj dmopen(dev)
6762468Swnj 	dev_t dev;
6772468Swnj {
6782468Swnj 	register struct tty *tp;
6792468Swnj 	register struct dmdevice *addr;
6802974Swnj 	register struct uba_device *ui;
6812468Swnj 	register int unit;
6822468Swnj 	register int dm;
6833792Swnj 	int s;
6842468Swnj 
6852468Swnj 	unit = minor(dev);
6862479Swnj 	dm = unit >> 4;
6872468Swnj 	tp = &dh11[unit];
6882566Swnj 	unit &= 0xf;
68916942Skarels 	if (dm >= NDH || (ui = dminfo[dm]) == 0 || ui->ui_alive == 0) {
6905406Swnj 		tp->t_state |= TS_CARR_ON;
6912468Swnj 		return;
6922468Swnj 	}
6932468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
6943792Swnj 	s = spl5();
6952479Swnj 	addr->dmcsr &= ~DM_SE;
6962479Swnj 	while (addr->dmcsr & DM_BUSY)
6972468Swnj 		;
6982566Swnj 	addr->dmcsr = unit;
6992479Swnj 	addr->dmlstat = DML_ON;
70016942Skarels 	if ((addr->dmlstat&DML_CAR) || (dhsoftCAR[dm]&(1<<unit)))
7015406Swnj 		tp->t_state |= TS_CARR_ON;
7023792Swnj 	addr->dmcsr = DM_IE|DM_SE;
7035406Swnj 	while ((tp->t_state&TS_CARR_ON)==0)
7042468Swnj 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
7053792Swnj 	splx(s);
7062468Swnj }
7072468Swnj 
7082468Swnj /*
7092468Swnj  * Dump control bits into the DM registers.
7102468Swnj  */
7112468Swnj dmctl(dev, bits, how)
7122468Swnj 	dev_t dev;
7132468Swnj 	int bits, how;
7142468Swnj {
7152974Swnj 	register struct uba_device *ui;
7162468Swnj 	register struct dmdevice *addr;
7172468Swnj 	register int unit, s;
7182468Swnj 	int dm;
7192468Swnj 
7202468Swnj 	unit = minor(dev);
7212468Swnj 	dm = unit >> 4;
7222468Swnj 	if ((ui = dminfo[dm]) == 0 || ui->ui_alive == 0)
7232468Swnj 		return;
7242468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7252468Swnj 	s = spl5();
7262479Swnj 	addr->dmcsr &= ~DM_SE;
7272479Swnj 	while (addr->dmcsr & DM_BUSY)
7282468Swnj 		;
7292468Swnj 	addr->dmcsr = unit & 0xf;
7302468Swnj 	switch(how) {
7312468Swnj 	case DMSET:
7322468Swnj 		addr->dmlstat = bits;
7332468Swnj 		break;
7342468Swnj 	case DMBIS:
7352468Swnj 		addr->dmlstat |= bits;
7362468Swnj 		break;
7372468Swnj 	case DMBIC:
7382468Swnj 		addr->dmlstat &= ~bits;
7392468Swnj 		break;
7402468Swnj 	}
7413792Swnj 	addr->dmcsr = DM_IE|DM_SE;
7422468Swnj 	splx(s);
7432468Swnj }
7442468Swnj 
7452468Swnj /*
7462468Swnj  * DM11 interrupt; deal with carrier transitions.
7472468Swnj  */
7482468Swnj dmintr(dm)
7492468Swnj 	register int dm;
7502468Swnj {
7512974Swnj 	register struct uba_device *ui;
7522468Swnj 	register struct tty *tp;
7532468Swnj 	register struct dmdevice *addr;
75416942Skarels 	int unit;
7552468Swnj 
7562468Swnj 	ui = dminfo[dm];
7572479Swnj 	if (ui == 0)
7582479Swnj 		return;
7592468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7603997Sroot 	if (addr->dmcsr&DM_DONE) {
7613997Sroot 		if (addr->dmcsr&DM_CF) {
76216942Skarels 			unit = addr->dmcsr & 0xf;
76316942Skarels 			tp = &dh11[(dm << 4) + unit];
76425394Skarels 			if (addr->dmlstat & DML_CAR)
76525394Skarels 				(void)(*linesw[tp->t_line].l_modem)(tp, 1);
76625394Skarels 			else if ((dhsoftCAR[dm] & (1<<unit)) == 0 &&
76725394Skarels 			    (*linesw[tp->t_line].l_modem)(tp, 0) == 0)
76825394Skarels 				addr->dmlstat = 0;
7693997Sroot 		}
7703997Sroot 		addr->dmcsr = DM_IE|DM_SE;
7712468Swnj 	}
7722468Swnj }
7732625Swnj #endif
774