bad block handling
[unix-history] / usr / src / sys / vax / uba / dh.c
CommitLineData
896962b1 1/* dh.c 4.51 82/10/10 */
a18f326f 2
66b4fb09 3#include "dh.h"
0916e0d1 4#if NDH > 0
a18f326f 5/*
d4638843 6 * DH-11/DM-11 driver
a18f326f 7 */
e2c4935e 8#include "bk.h"
a18f326f
BJ
9#include "../h/param.h"
10#include "../h/conf.h"
11#include "../h/dir.h"
12#include "../h/user.h"
155d9ff0 13#include "../h/proc.h"
a18f326f
BJ
14#include "../h/tty.h"
15#include "../h/map.h"
16#include "../h/pte.h"
3f3a34c3 17#include "../h/buf.h"
7e286c72 18#include "../h/vm.h"
896962b1
BJ
19
20#include "../vaxuba/ubareg.h"
21#include "../vaxuba/ubavar.h"
22
038bbe6b 23#include "../h/bk.h"
a5cc519e 24#include "../h/clist.h"
7e00c42b 25#include "../h/file.h"
740e4029 26#include "../h/uio.h"
a18f326f 27
7e00c42b 28/*
d4638843
BJ
29 * Definition of the driver for the auto-configuration program.
30 * There is one definition for the dh and one for the dm.
7e00c42b 31 */
71236e46 32int dhprobe(), dhattach(), dhrint(), dhxint();
b09915c5 33struct uba_device *dhinfo[NDH];
3f3a34c3 34u_short dhstd[] = { 0 };
3f3a34c3 35struct uba_driver dhdriver =
71236e46 36 { dhprobe, 0, dhattach, 0, dhstd, "dh", dhinfo };
3f3a34c3 37
71236e46 38int dmprobe(), dmattach(), dmintr();
b09915c5 39struct uba_device *dminfo[NDH];
d4638843
BJ
40u_short dmstd[] = { 0 };
41struct uba_driver dmdriver =
71236e46 42 { dmprobe, 0, dmattach, 0, dmstd, "dm", dminfo };
a18f326f 43
d4638843
BJ
44struct dhdevice
45{
46 union {
47 short dhcsr; /* control-status register */
48 char dhcsrl; /* low byte for line select */
49 } un;
50 short dhrcr; /* receive character register */
51 short dhlpr; /* line parameter register */
52 u_short dhcar; /* current address register */
53 short dhbcr; /* byte count register */
54 u_short dhbar; /* buffer active register */
55 short dhbreak; /* break control register */
56 short dhsilo; /* silo status register */
57};
a18f326f 58
df07bd9e
SL
59#ifndef PORTSELECTOR
60#define ISPEED B300
61#define IFLAGS (EVENP|ODDP|ECHO)
62#else
63#define ISPEED B4800
64#define IFLAGS (EVENP|ODDP)
65#endif
66
88d5b764
BJ
67/* Bits in dhcsr */
68#define DH_TI 0100000 /* transmit interrupt */
69#define DH_SI 0040000 /* storage interrupt */
70#define DH_TIE 0020000 /* transmit interrupt enable */
71#define DH_SIE 0010000 /* storage interrupt enable */
72#define DH_MC 0004000 /* master clear */
73#define DH_NXM 0002000 /* non-existant memory */
74#define DH_MM 0001000 /* maintenance mode */
75#define DH_CNI 0000400 /* clear non-existant memory interrupt */
76#define DH_RI 0000200 /* receiver interrupt */
77#define DH_RIE 0000100 /* receiver interrupt enable */
78
d4638843
BJ
79/* Bits in dhlpr */
80#define BITS6 01
81#define BITS7 02
82#define BITS8 03
83#define TWOSB 04
84#define PENABLE 020
85/* DEC manuals incorrectly say this bit causes generation of even parity. */
86#define OPAR 040
87#define HDUPLX 040000
88
88d5b764
BJ
89#define DH_IE (DH_TIE|DH_SIE|DH_RIE)
90
91/* Bits in dhrcr */
d4638843
BJ
92#define DH_PE 0010000 /* parity error */
93#define DH_FE 0020000 /* framing error */
94#define DH_DO 0040000 /* data overrun */
95
96struct dmdevice
97{
98 short dmcsr; /* control status register */
99 short dmlstat; /* line status register */
100 short dmpad1[2];
101};
102
103/* bits in dm csr */
104#define DM_RF 0100000 /* ring flag */
105#define DM_CF 0040000 /* carrier flag */
106#define DM_CTS 0020000 /* clear to send */
107#define DM_SRF 0010000 /* secondary receive flag */
108#define DM_CS 0004000 /* clear scan */
109#define DM_CM 0002000 /* clear multiplexor */
110#define DM_MM 0001000 /* maintenance mode */
111#define DM_STP 0000400 /* step */
112#define DM_DONE 0000200 /* scanner is done */
113#define DM_IE 0000100 /* interrupt enable */
114#define DM_SE 0000040 /* scan enable */
115#define DM_BUSY 0000020 /* scan busy */
116
117/* bits in dm lsr */
118#define DML_RNG 0000200 /* ring */
119#define DML_CAR 0000100 /* carrier detect */
120#define DML_CTS 0000040 /* clear to send */
121#define DML_SR 0000020 /* secondary receive */
122#define DML_ST 0000010 /* secondary transmit */
123#define DML_RTS 0000004 /* request to send */
124#define DML_DTR 0000002 /* data terminal ready */
125#define DML_LE 0000001 /* line enable */
126
1d6c2d43 127#define DML_ON (DML_DTR|DML_RTS|DML_LE)
d4638843 128#define DML_OFF (DML_LE)
a18f326f 129
a18f326f 130/*
d4638843 131 * Local variables for the driver
a18f326f 132 */
0916e0d1
BJ
133short dhsar[NDH]; /* software copy of last bar */
134short dhsoftCAR[NDH];
a18f326f 135
0916e0d1
BJ
136struct tty dh11[NDH*16];
137int ndh11 = NDH*16;
d4638843
BJ
138int dhact; /* mask of active dh's */
139int dhstart(), ttrstrt();
a18f326f 140
d4638843
BJ
141/*
142 * The clist space is mapped by the driver onto each UNIBUS.
143 * The UBACVT macro converts a clist space address for unibus uban
144 * into an i/o space address for the DMA routine.
145 */
146int dh_ubinfo[MAXNUBA]; /* info about allocated unibus map */
147int cbase[MAXNUBA]; /* base address in unibus map */
148#define UBACVT(x, uban) (cbase[uban] + ((x)-(char *)cfree))
a18f326f 149
88d5b764
BJ
150/*
151 * Routine for configuration to force a dh to interrupt.
152 * Set to transmit at 9600 baud, and cause a transmitter interrupt.
153 */
7e00c42b 154/*ARGSUSED*/
71236e46 155dhprobe(reg)
3f3a34c3
BJ
156 caddr_t reg;
157{
7e00c42b 158 register int br, cvec; /* these are ``value-result'' */
d4638843 159 register struct dhdevice *dhaddr = (struct dhdevice *)reg;
5aa9d5ea 160
71236e46
BJ
161#ifdef lint
162 br = 0; cvec = br; br = cvec;
fde2e6c9 163 if (ndh11 == 0) ndh11 = 1;
89b8a44c 164 dhrint(0); dhxint(0);
71236e46 165#endif
52ab9b2b 166#ifndef notdef
7e286c72 167 dhaddr->un.dhcsr = DH_RIE|DH_MM|DH_RI;
5e7ab705 168 DELAY(1000);
fde2e6c9 169 dhaddr->un.dhcsr &= ~DH_RI;
7e286c72
BJ
170 dhaddr->un.dhcsr = 0;
171#else
88d5b764
BJ
172 dhaddr->un.dhcsr = DH_TIE;
173 DELAY(5);
174 dhaddr->dhlpr = (B9600 << 10) | (B9600 << 6) | BITS7|PENABLE;
5aa9d5ea 175 dhaddr->dhbcr = -1;
5aa9d5ea 176 dhaddr->dhcar = 0;
88d5b764
BJ
177 dhaddr->dhbar = 1;
178 DELAY(100000); /* wait 1/10'th of a sec for interrupt */
5aa9d5ea 179 dhaddr->un.dhcsr = 0;
88d5b764
BJ
180 if (cvec && cvec != 0x200)
181 cvec -= 4; /* transmit -> receive */
4afc81c0 182#endif
9c0adba0 183 return (sizeof (struct dhdevice));
3f3a34c3
BJ
184}
185
88d5b764 186/*
71236e46 187 * Routine called to attach a dh.
88d5b764 188 */
71236e46 189dhattach(ui)
b09915c5 190 struct uba_device *ui;
3f3a34c3
BJ
191{
192
7e286c72 193 dhsoftCAR[ui->ui_unit] = ui->ui_flags;
3f3a34c3
BJ
194}
195
d4638843
BJ
196/*
197 * Configuration routine to cause a dm to interrupt.
198 */
71236e46
BJ
199dmprobe(reg)
200 caddr_t reg;
d4638843
BJ
201{
202 register int br, vec; /* value-result */
71236e46 203 register struct dmdevice *dmaddr = (struct dmdevice *)reg;
d4638843 204
71236e46 205#ifdef lint
a0eab615 206 br = 0; vec = br; br = vec;
155d9ff0 207 dmintr(0);
71236e46 208#endif
d4638843
BJ
209 dmaddr->dmcsr = DM_DONE|DM_IE;
210 DELAY(20);
211 dmaddr->dmcsr = 0;
71236e46 212 return (1);
d4638843
BJ
213}
214
71236e46
BJ
215/*ARGSUSED*/
216dmattach(ui)
b09915c5 217 struct uba_device *ui;
d4638843
BJ
218{
219
220 /* no local state to set up */
221}
222
a18f326f 223/*
7e00c42b
BJ
224 * Open a DH11 line, mapping the clist onto the uba if this
225 * is the first dh on this uba. Turn on this dh if this is
226 * the first use of it. Also do a dmopen to wait for carrier.
a18f326f
BJ
227 */
228/*ARGSUSED*/
229dhopen(dev, flag)
3f3a34c3 230 dev_t dev;
a18f326f
BJ
231{
232 register struct tty *tp;
3f3a34c3 233 register int unit, dh;
d4638843 234 register struct dhdevice *addr;
b09915c5 235 register struct uba_device *ui;
a18f326f
BJ
236 int s;
237
3f3a34c3
BJ
238 unit = minor(dev);
239 dh = unit >> 4;
0916e0d1 240 if (unit >= NDH*16 || (ui = dhinfo[dh])== 0 || ui->ui_alive == 0) {
a18f326f
BJ
241 u.u_error = ENXIO;
242 return;
243 }
3f3a34c3 244 tp = &dh11[unit];
941944c9 245 if (tp->t_state&TS_XCLUDE && u.u_uid!=0) {
7e00c42b
BJ
246 u.u_error = EBUSY;
247 return;
248 }
d4638843 249 addr = (struct dhdevice *)ui->ui_addr;
a18f326f
BJ
250 tp->t_addr = (caddr_t)addr;
251 tp->t_oproc = dhstart;
941944c9 252 tp->t_state |= TS_WOPEN;
7e00c42b
BJ
253 /*
254 * While setting up state for this uba and this dh,
255 * block uba resets which can clear the state.
256 */
257 s = spl5();
5aa9d5ea 258 if (dh_ubinfo[ui->ui_ubanum] == 0) {
d319892b 259 /* 512+ is a kludge to try to get around a hardware problem */
3f3a34c3 260 dh_ubinfo[ui->ui_ubanum] =
5aa9d5ea 261 uballoc(ui->ui_ubanum, (caddr_t)cfree,
4c05b581 262 512+nclist*sizeof(struct cblock), 0);
88d5b764
BJ
263 cbase[ui->ui_ubanum] = dh_ubinfo[ui->ui_ubanum]&0x3ffff;
264 }
265 if ((dhact&(1<<dh)) == 0) {
266 addr->un.dhcsr |= DH_IE;
88d5b764 267 dhact |= (1<<dh);
7e00c42b 268 addr->dhsilo = 16;
a18f326f
BJ
269 }
270 splx(s);
7e00c42b
BJ
271 /*
272 * If this is first open, initialze tty state to default.
273 */
941944c9 274 if ((tp->t_state&TS_ISOPEN) == 0) {
a18f326f 275 ttychars(tp);
df07bd9e 276#ifndef PORTSELECTOR
87f51a66 277 if (tp->t_ispeed == 0) {
df07bd9e
SL
278#endif
279 tp->t_ispeed = ISPEED;
280 tp->t_ospeed = ISPEED;
281 tp->t_flags = IFLAGS;
282#ifndef PORTSELECTOR
87f51a66 283 }
df07bd9e 284#endif
3f3a34c3 285 dhparam(unit);
a18f326f 286 }
7e00c42b
BJ
287 /*
288 * Wait for carrier, then process line discipline specific open.
289 */
a18f326f 290 dmopen(dev);
3f3a34c3 291 (*linesw[tp->t_line].l_open)(dev, tp);
a18f326f
BJ
292}
293
294/*
7e00c42b 295 * Close a DH11 line, turning off the DM11.
a18f326f
BJ
296 */
297/*ARGSUSED*/
298dhclose(dev, flag)
3f3a34c3
BJ
299 dev_t dev;
300 int flag;
a18f326f
BJ
301{
302 register struct tty *tp;
3f3a34c3 303 register unit;
a18f326f 304
3f3a34c3
BJ
305 unit = minor(dev);
306 tp = &dh11[unit];
a18f326f 307 (*linesw[tp->t_line].l_close)(tp);
d4638843 308 ((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
941944c9 309 if (tp->t_state&TS_HUPCLS || (tp->t_state&TS_ISOPEN)==0)
d4638843 310 dmctl(unit, DML_OFF, DMSET);
a18f326f
BJ
311 ttyclose(tp);
312}
313
740e4029 314dhread(dev, uio)
3f3a34c3 315 dev_t dev;
740e4029 316 struct uio *uio;
a18f326f 317{
3f3a34c3 318 register struct tty *tp;
a18f326f 319
3f3a34c3 320 tp = &dh11[minor(dev)];
740e4029 321 return ((*linesw[tp->t_line].l_read)(tp, uio));
a18f326f
BJ
322}
323
406ddcbe 324dhwrite(dev, uio)
3f3a34c3 325 dev_t dev;
406ddcbe 326 struct uio *uio;
a18f326f 327{
3f3a34c3 328 register struct tty *tp;
a18f326f 329
3f3a34c3 330 tp = &dh11[minor(dev)];
406ddcbe 331 (*linesw[tp->t_line].l_write)(tp, uio);
a18f326f
BJ
332}
333
334/*
335 * DH11 receiver interrupt.
336 */
3f3a34c3
BJ
337dhrint(dh)
338 int dh;
a18f326f
BJ
339{
340 register struct tty *tp;
3f3a34c3 341 register c;
d4638843 342 register struct dhdevice *addr;
0e239190 343 register struct tty *tp0;
b09915c5 344 register struct uba_device *ui;
b19fe459 345 int overrun = 0;
a18f326f 346
3f3a34c3 347 ui = dhinfo[dh];
d4638843
BJ
348 if (ui == 0 || ui->ui_alive == 0)
349 return;
350 addr = (struct dhdevice *)ui->ui_addr;
7e00c42b
BJ
351 tp0 = &dh11[dh<<4];
352 /*
353 * Loop fetching characters from the silo for this
354 * dh until there are no more in the silo.
355 */
356 while ((c = addr->dhrcr) < 0) {
357 tp = tp0 + ((c>>8)&0xf);
df07bd9e 358#ifndef PORTSELECTOR
941944c9 359 if ((tp->t_state&TS_ISOPEN)==0) {
df07bd9e
SL
360#else
361 if ((tp->t_state&(TS_ISOPEN|TS_WOPEN))==0) {
362#endif
a18f326f
BJ
363 wakeup((caddr_t)tp);
364 continue;
365 }
7e00c42b 366 if (c & DH_PE)
a18f326f
BJ
367 if ((tp->t_flags&(EVENP|ODDP))==EVENP
368 || (tp->t_flags&(EVENP|ODDP))==ODDP )
369 continue;
b19fe459
BJ
370 if ((c & DH_DO) && overrun == 0) {
371 printf("dh%d: silo overflow\n", dh);
372 overrun = 1;
373 }
7e00c42b
BJ
374 if (c & DH_FE)
375 /*
376 * At framing error (break) generate
377 * a null (in raw mode, for getty), or a
378 * interrupt (in cooked/cbreak mode).
379 */
a18f326f 380 if (tp->t_flags&RAW)
7e00c42b 381 c = 0;
a18f326f 382 else
1c17c385 383 c = tun.t_intrc;
e2c4935e 384#if NBK > 0
5c6adb3e 385 if (tp->t_line == NETLDISC) {
0e239190 386 c &= 0177;
87f51a66 387 BKINPUT(c, tp);
0e239190 388 } else
e2c4935e 389#endif
7e00c42b 390 (*linesw[tp->t_line].l_rint)(c, tp);
a18f326f
BJ
391 }
392}
393
394/*
7e00c42b 395 * Ioctl for DH11.
a18f326f
BJ
396 */
397/*ARGSUSED*/
942f05a9
SL
398dhioctl(dev, cmd, data, flag)
399 caddr_t data;
a18f326f
BJ
400{
401 register struct tty *tp;
3f3a34c3 402 register unit = minor(dev);
a18f326f 403
3f3a34c3 404 tp = &dh11[unit];
942f05a9 405 cmd = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
7e00c42b 406 if (cmd == 0)
038bbe6b 407 return;
942f05a9
SL
408 if (ttioctl(tp, cmd, data, flag)) {
409 if (cmd == TIOCSETP || cmd == TIOCSETN)
3f3a34c3 410 dhparam(unit);
87f51a66 411 } else switch(cmd) {
942f05a9 412
87f51a66 413 case TIOCSBRK:
d4638843 414 ((struct dhdevice *)(tp->t_addr))->dhbreak |= 1<<(unit&017);
87f51a66 415 break;
942f05a9 416
87f51a66 417 case TIOCCBRK:
d4638843 418 ((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
87f51a66 419 break;
942f05a9 420
87f51a66 421 case TIOCSDTR:
d4638843 422 dmctl(unit, DML_DTR|DML_RTS, DMBIS);
87f51a66 423 break;
942f05a9 424
87f51a66 425 case TIOCCDTR:
d4638843 426 dmctl(unit, DML_DTR|DML_RTS, DMBIC);
87f51a66 427 break;
942f05a9 428
87f51a66 429 default:
a18f326f 430 u.u_error = ENOTTY;
87f51a66 431 }
a18f326f
BJ
432}
433
434/*
435 * Set parameters from open or stty into the DH hardware
436 * registers.
437 */
3f3a34c3
BJ
438dhparam(unit)
439 register int unit;
a18f326f
BJ
440{
441 register struct tty *tp;
d4638843 442 register struct dhdevice *addr;
3f3a34c3 443 register int lpar;
0072a3c2 444 int s;
a18f326f 445
3f3a34c3 446 tp = &dh11[unit];
d4638843 447 addr = (struct dhdevice *)tp->t_addr;
7e00c42b
BJ
448 /*
449 * Block interrupts so parameters will be set
450 * before the line interrupts.
451 */
0072a3c2 452 s = spl5();
7e00c42b 453 addr->un.dhcsrl = (unit&0xf) | DH_IE;
a18f326f 454 if ((tp->t_ispeed)==0) {
941944c9 455 tp->t_state |= TS_HUPCLS;
d4638843 456 dmctl(unit, DML_OFF, DMSET);
a18f326f
BJ
457 return;
458 }
3f3a34c3 459 lpar = ((tp->t_ospeed)<<10) | ((tp->t_ispeed)<<6);
7e00c42b 460 if ((tp->t_ispeed) == B134)
3f3a34c3 461 lpar |= BITS6|PENABLE|HDUPLX;
131b2e17 462 else if ((tp->t_flags&RAW) || (tp->t_local&LLITOUT))
3f3a34c3 463 lpar |= BITS8;
a18f326f 464 else
3f3a34c3 465 lpar |= BITS7|PENABLE;
a18f326f 466 if ((tp->t_flags&EVENP) == 0)
3f3a34c3 467 lpar |= OPAR;
7e00c42b 468 if ((tp->t_ospeed) == B110)
3f3a34c3
BJ
469 lpar |= TWOSB;
470 addr->dhlpr = lpar;
0072a3c2 471 splx(s);
a18f326f
BJ
472}
473
474/*
475 * DH11 transmitter interrupt.
476 * Restart each line which used to be active but has
477 * terminated transmission since the last interrupt.
478 */
3f3a34c3
BJ
479dhxint(dh)
480 int dh;
a18f326f
BJ
481{
482 register struct tty *tp;
d4638843 483 register struct dhdevice *addr;
a18f326f 484 short ttybit, bar, *sbar;
b09915c5 485 register struct uba_device *ui;
7e00c42b 486 register int unit;
71236e46 487 u_short cntr;
a18f326f 488
3f3a34c3 489 ui = dhinfo[dh];
d4638843 490 addr = (struct dhdevice *)ui->ui_addr;
88d5b764
BJ
491 if (addr->un.dhcsr & DH_NXM) {
492 addr->un.dhcsr |= DH_CNI;
b19fe459 493 printf("dh%d: NXM\n", dh);
b4ec79ea 494 }
3f3a34c3 495 sbar = &dhsar[dh];
a18f326f 496 bar = *sbar & ~addr->dhbar;
3f3a34c3 497 unit = dh * 16; ttybit = 1;
7e00c42b
BJ
498 addr->un.dhcsr &= (short)~DH_TI;
499 for (; bar; unit++, ttybit <<= 1) {
500 if (bar & ttybit) {
a18f326f
BJ
501 *sbar &= ~ttybit;
502 bar &= ~ttybit;
3f3a34c3 503 tp = &dh11[unit];
941944c9
BJ
504 tp->t_state &= ~TS_BUSY;
505 if (tp->t_state&TS_FLUSH)
506 tp->t_state &= ~TS_FLUSH;
038bbe6b 507 else {
88d5b764 508 addr->un.dhcsrl = (unit&017)|DH_IE;
7e00c42b
BJ
509 /*
510 * Do arithmetic in a short to make up
511 * for lost 16&17 bits.
512 */
71236e46 513 cntr = addr->dhcar -
7e00c42b 514 UBACVT(tp->t_outq.c_cf, ui->ui_ubanum);
a0eab615 515 ndflush(&tp->t_outq, (int)cntr);
a18f326f 516 }
038bbe6b
BJ
517 if (tp->t_line)
518 (*linesw[tp->t_line].l_start)(tp);
519 else
520 dhstart(tp);
a18f326f
BJ
521 }
522 }
523}
524
525/*
526 * Start (restart) transmission on the given DH11 line.
527 */
528dhstart(tp)
3f3a34c3 529 register struct tty *tp;
a18f326f 530{
d4638843 531 register struct dhdevice *addr;
7e00c42b 532 register int car, dh, unit, nch;
3f3a34c3 533 int s;
a18f326f 534
3f3a34c3
BJ
535 unit = minor(tp->t_dev);
536 dh = unit >> 4;
7e00c42b 537 unit &= 0xf;
d4638843 538 addr = (struct dhdevice *)tp->t_addr;
7e00c42b
BJ
539
540 /*
541 * Must hold interrupts in following code to prevent
542 * state of the tp from changing.
543 */
544 s = spl5();
545 /*
546 * If it's currently active, or delaying, no need to do anything.
547 */
941944c9 548 if (tp->t_state&(TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
a18f326f 549 goto out;
7e00c42b
BJ
550 /*
551 * If there are sleepers, and output has drained below low
552 * water mark, wake up the sleepers.
553 */
941944c9
BJ
554 if (tp->t_outq.c_cc<=TTLOWAT(tp)) {
555 if (tp->t_state&TS_ASLEEP) {
556 tp->t_state &= ~TS_ASLEEP;
557 wakeup((caddr_t)&tp->t_outq);
558 }
559 if (tp->t_wsel) {
560 selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
561 tp->t_wsel = 0;
562 tp->t_state &= ~TS_WCOLL;
563 }
a18f326f 564 }
7e00c42b
BJ
565 /*
566 * Now restart transmission unless the output queue is
567 * empty.
568 */
a18f326f
BJ
569 if (tp->t_outq.c_cc == 0)
570 goto out;
6e3ea160 571 if (tp->t_flags&RAW || tp->t_local&LLITOUT)
a18f326f 572 nch = ndqb(&tp->t_outq, 0);
3f3a34c3 573 else {
a18f326f 574 nch = ndqb(&tp->t_outq, 0200);
7e00c42b
BJ
575 /*
576 * If first thing on queue is a delay process it.
577 */
a18f326f
BJ
578 if (nch == 0) {
579 nch = getc(&tp->t_outq);
7e00c42b 580 timeout(ttrstrt, (caddr_t)tp, (nch&0x7f)+6);
941944c9 581 tp->t_state |= TS_TIMEOUT;
a18f326f
BJ
582 goto out;
583 }
584 }
7e00c42b
BJ
585 /*
586 * If characters to transmit, restart transmission.
587 */
a18f326f 588 if (nch) {
7e00c42b
BJ
589 car = UBACVT(tp->t_outq.c_cf, dhinfo[dh]->ui_ubanum);
590 addr->un.dhcsrl = unit|((car>>12)&0x30)|DH_IE;
a1b41f3d
BJ
591 /*
592 * The following nonsense with short word
593 * is to make sure the dhbar |= word below
594 * is done with an interlocking bisw2 instruction.
595 */
596 { short word = 1 << unit;
597 dhsar[dh] |= word;
7e00c42b 598 addr->dhcar = car;
a18f326f 599 addr->dhbcr = -nch;
a1b41f3d
BJ
600 addr->dhbar |= word;
601 }
941944c9 602 tp->t_state |= TS_BUSY;
a18f326f 603 }
3f3a34c3 604out:
a18f326f
BJ
605 splx(s);
606}
607
a18f326f 608/*
7e00c42b 609 * Stop output on a line, e.g. for ^S/^Q or output flush.
a18f326f
BJ
610 */
611/*ARGSUSED*/
612dhstop(tp, flag)
7e00c42b 613 register struct tty *tp;
a18f326f 614{
d4638843 615 register struct dhdevice *addr;
3f3a34c3 616 register int unit, s;
a18f326f 617
d4638843 618 addr = (struct dhdevice *)tp->t_addr;
7e00c42b
BJ
619 /*
620 * Block input/output interrupts while messing with state.
621 */
622 s = spl5();
941944c9 623 if (tp->t_state & TS_BUSY) {
7e00c42b
BJ
624 /*
625 * Device is transmitting; stop output
626 * by selecting the line and setting the byte
627 * count to -1. We will clean up later
628 * by examining the address where the dh stopped.
629 */
3f3a34c3 630 unit = minor(tp->t_dev);
88d5b764 631 addr->un.dhcsrl = (unit&017) | DH_IE;
941944c9
BJ
632 if ((tp->t_state&TS_TTSTOP)==0)
633 tp->t_state |= TS_FLUSH;
038bbe6b
BJ
634 addr->dhbcr = -1;
635 }
a18f326f
BJ
636 splx(s);
637}
638
5c30d566
BJ
639/*
640 * Reset state of driver if UBA reset was necessary.
641 * Reset the csrl and lpr registers on open lines, and
642 * restart transmitters.
643 */
3f3a34c3 644dhreset(uban)
7e00c42b 645 int uban;
5c30d566 646{
3f3a34c3 647 register int dh, unit;
5c30d566 648 register struct tty *tp;
b09915c5 649 register struct uba_device *ui;
5aa9d5ea 650 int i;
5c30d566 651
5aa9d5ea
RE
652 if (dh_ubinfo[uban] == 0)
653 return;
5aa9d5ea
RE
654 ubarelse(uban, &dh_ubinfo[uban]);
655 dh_ubinfo[uban] = uballoc(uban, (caddr_t)cfree,
4c05b581 656 512+nclist*sizeof (struct cblock), 0);
5aa9d5ea 657 cbase[uban] = dh_ubinfo[uban]&0x3ffff;
3f3a34c3 658 dh = 0;
0916e0d1 659 for (dh = 0; dh < NDH; dh++) {
5aa9d5ea
RE
660 ui = dhinfo[dh];
661 if (ui == 0 || ui->ui_alive == 0 || ui->ui_ubanum != uban)
662 continue;
b19fe459 663 printf(" dh%d", dh);
d4638843 664 ((struct dhdevice *)ui->ui_addr)->un.dhcsr |= DH_IE;
d4638843 665 ((struct dhdevice *)ui->ui_addr)->dhsilo = 16;
5aa9d5ea
RE
666 unit = dh * 16;
667 for (i = 0; i < 16; i++) {
668 tp = &dh11[unit];
941944c9 669 if (tp->t_state & (TS_ISOPEN|TS_WOPEN)) {
5aa9d5ea 670 dhparam(unit);
d4638843 671 dmctl(unit, DML_ON, DMSET);
941944c9 672 tp->t_state &= ~TS_BUSY;
5aa9d5ea
RE
673 dhstart(tp);
674 }
675 unit++;
0072a3c2
BJ
676 }
677 }
678 dhtimer();
5c30d566 679}
3f3a34c3 680
7e00c42b
BJ
681/*
682 * At software clock interrupt time or after a UNIBUS reset
683 * empty all the dh silos.
684 */
88d5b764
BJ
685dhtimer()
686{
687 register int dh;
aa890753 688 register int s = spl5();
88d5b764 689
0916e0d1 690 for (dh = 0; dh < NDH; dh++)
88d5b764 691 dhrint(dh);
aa890753 692 splx(s);
88d5b764
BJ
693}
694
7e00c42b 695/*
d4638843 696 * Turn on the line associated with dh dev.
7e00c42b
BJ
697 */
698dmopen(dev)
699 dev_t dev;
700{
701 register struct tty *tp;
702 register struct dmdevice *addr;
b09915c5 703 register struct uba_device *ui;
7e00c42b
BJ
704 register int unit;
705 register int dm;
1d6c2d43 706 int s;
7e00c42b
BJ
707
708 unit = minor(dev);
d4638843 709 dm = unit >> 4;
7e00c42b 710 tp = &dh11[unit];
7e286c72 711 unit &= 0xf;
0916e0d1 712 if (dm >= NDH || (ui = dminfo[dm]) == 0 || ui->ui_alive == 0 ||
7e286c72 713 (dhsoftCAR[dm]&(1<<unit))) {
941944c9 714 tp->t_state |= TS_CARR_ON;
7e00c42b
BJ
715 return;
716 }
717 addr = (struct dmdevice *)ui->ui_addr;
1d6c2d43 718 s = spl5();
d4638843
BJ
719 addr->dmcsr &= ~DM_SE;
720 while (addr->dmcsr & DM_BUSY)
7e00c42b 721 ;
7e286c72 722 addr->dmcsr = unit;
d4638843
BJ
723 addr->dmlstat = DML_ON;
724 if (addr->dmlstat&DML_CAR)
941944c9 725 tp->t_state |= TS_CARR_ON;
1d6c2d43 726 addr->dmcsr = DM_IE|DM_SE;
941944c9 727 while ((tp->t_state&TS_CARR_ON)==0)
7e00c42b 728 sleep((caddr_t)&tp->t_rawq, TTIPRI);
1d6c2d43 729 splx(s);
7e00c42b
BJ
730}
731
732/*
733 * Dump control bits into the DM registers.
734 */
735dmctl(dev, bits, how)
736 dev_t dev;
737 int bits, how;
738{
b09915c5 739 register struct uba_device *ui;
7e00c42b
BJ
740 register struct dmdevice *addr;
741 register int unit, s;
742 int dm;
743
744 unit = minor(dev);
745 dm = unit >> 4;
746 if ((ui = dminfo[dm]) == 0 || ui->ui_alive == 0)
747 return;
748 addr = (struct dmdevice *)ui->ui_addr;
749 s = spl5();
d4638843
BJ
750 addr->dmcsr &= ~DM_SE;
751 while (addr->dmcsr & DM_BUSY)
7e00c42b
BJ
752 ;
753 addr->dmcsr = unit & 0xf;
754 switch(how) {
755 case DMSET:
756 addr->dmlstat = bits;
757 break;
758 case DMBIS:
759 addr->dmlstat |= bits;
760 break;
761 case DMBIC:
762 addr->dmlstat &= ~bits;
763 break;
764 }
1d6c2d43 765 addr->dmcsr = DM_IE|DM_SE;
7e00c42b
BJ
766 splx(s);
767}
768
769/*
770 * DM11 interrupt; deal with carrier transitions.
771 */
772dmintr(dm)
773 register int dm;
774{
b09915c5 775 register struct uba_device *ui;
7e00c42b
BJ
776 register struct tty *tp;
777 register struct dmdevice *addr;
778
779 ui = dminfo[dm];
d4638843
BJ
780 if (ui == 0)
781 return;
7e00c42b 782 addr = (struct dmdevice *)ui->ui_addr;
658d2f56
BJ
783 if (addr->dmcsr&DM_DONE) {
784 if (addr->dmcsr&DM_CF) {
785 tp = &dh11[(dm<<4)+(addr->dmcsr&0xf)];
786 wakeup((caddr_t)&tp->t_rawq);
941944c9 787 if ((tp->t_state&TS_WOPEN)==0 &&
658d2f56
BJ
788 (tp->t_local&LMDMBUF)) {
789 if (addr->dmlstat & DML_CAR) {
941944c9 790 tp->t_state &= ~TS_TTSTOP;
658d2f56 791 ttstart(tp);
941944c9
BJ
792 } else if ((tp->t_state&TS_TTSTOP) == 0) {
793 tp->t_state |= TS_TTSTOP;
658d2f56
BJ
794 dhstop(tp, 0);
795 }
796 } else if ((addr->dmlstat&DML_CAR)==0) {
941944c9 797 if ((tp->t_state&TS_WOPEN)==0 &&
658d2f56
BJ
798 (tp->t_local&LNOHANG)==0) {
799 gsignal(tp->t_pgrp, SIGHUP);
800 gsignal(tp->t_pgrp, SIGCONT);
801 addr->dmlstat = 0;
802 flushtty(tp, FREAD|FWRITE);
803 }
941944c9 804 tp->t_state &= ~TS_CARR_ON;
658d2f56 805 } else
941944c9 806 tp->t_state |= TS_CARR_ON;
658d2f56
BJ
807 }
808 addr->dmcsr = DM_IE|DM_SE;
7e00c42b
BJ
809 }
810}
4569bb70 811#endif