xref: /illumos-gate/usr/src/uts/sun/io/zs_async.c (revision b6805bf78d2bbbeeaea8909a05623587b42d58b3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  *	Asynchronous protocol handler for Z8530 chips
28  *	Handles normal UNIX support for terminals & modems
29  */
30 
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/signal.h>
36 #include <sys/kmem.h>
37 #include <sys/termios.h>
38 #include <sys/stropts.h>
39 #include <sys/stream.h>
40 #include <sys/strsun.h>
41 #include <sys/tty.h>
42 #include <sys/ptyvar.h>
43 #include <sys/cred.h>
44 #include <sys/user.h>
45 #include <sys/proc.h>
46 #include <sys/file.h>
47 #include <sys/uio.h>
48 #include <sys/buf.h>
49 #include <sys/mkdev.h>
50 #include <sys/cmn_err.h>
51 #include <sys/strtty.h>
52 #include <sys/consdev.h>
53 #include <sys/zsdev.h>
54 #include <sys/ser_async.h>
55 #include <sys/debug.h>
56 #include <sys/kbio.h>
57 #include <sys/conf.h>
58 #include <sys/ddi.h>
59 #include <sys/sunddi.h>
60 #include <sys/promif.h>
61 #include <sys/policy.h>
62 
63 /*
64  * PPS (Pulse Per Second) support.
65  */
66 extern void ddi_hardpps(struct timeval *, int);
67 static struct ppsclockev ppsclockev;
68 
69 #ifdef PPSCLOCKLED
70 /* XXX Use these to observe PPS latencies and jitter on a scope */
71 #define	LED_ON
72 #define	LED_OFF
73 #else
74 #define	LED_ON
75 #define	LED_OFF
76 #endif
77 
78 #define	ZSA_RCV_SIZE	64
79 #define	ZA_KICK_RCV_COUNT	3
80 #define	ZSA_GRACE_MIN_FLOW_CONTROL	5
81 #define	ZSA_GRACE_MAX_FLOW_CONTROL	20
82 
83 int zsasoftdtr = 0;	/* if nonzero, softcarrier raises dtr at attach */
84 int zsb134_weird = 0;	/* if set, old weird B134 behavior */
85 int g_zsticks = 0;	/* if set, becomes the global zsticks value */
86 int g_nocluster = 0;	/* if set, disables clustering of received data */
87 
88 unsigned int zsa_rstandby = ZSA_MIN_RSTANDBY;
89 unsigned int zsa_rdone = ZSA_RDONE_MIN;
90 unsigned int zsa_grace_flow_control = ZSA_GRACE_MIN_FLOW_CONTROL;
91 
92 
93 #define	NSPEED	18	/* max # of speeds */
94 ushort_t zs_speeds[NSPEED] = {
95 	0,
96 	ZSPEED(50),	/* 1 */
97 	ZSPEED(75),	/* 2 */
98 	ZSPEED(110),	/* 3 */
99 #ifdef lint
100 	ZSPEED(134),	/* 4 */
101 #else
102 	ZSPEED(269/2),			/* XXX - This is sleazy */
103 #endif
104 	ZSPEED(150),	/* 5 */
105 	ZSPEED(200),	/* 6 */
106 	ZSPEED(300),	/* 7 */
107 	ZSPEED(600),	/* 8 */
108 	ZSPEED(1200),	/* 9 */
109 	ZSPEED(1800),	/* 10 */
110 	ZSPEED(2400),	/* 11 */
111 	ZSPEED(4800),	/* 12 */
112 	ZSPEED(9600),	/* 13 */
113 	ZSPEED(19200),	/* 14 */
114 	ZSPEED(38400),	/* 15 */
115 	ZSPEED(57680),	/* 16 */
116 	ZSPEED(76800)	/* 17 */
117 };
118 
119 ushort_t zsticks[NSPEED] = {
120 	3,		/* 0 */
121 	3,		/* 1 */
122 	3,		/* 2 */
123 	3,		/* 3 */
124 	3,		/* 4 */
125 	3,		/* 5 */
126 	3,		/* 6 */
127 	3,		/* 7 */
128 	3,		/* 8 */
129 	3,		/* 9 */
130 	3,		/* 10 */
131 	3,		/* 11 */
132 	3,		/* 12 */
133 	3,		/* 13 */
134 	2,		/* 14 */
135 	1,		/* 15 */
136 	1,		/* 16 */
137 	1		/* 17 */
138 };
139 
140 #define	ztdelay(nsp)	(zsdelay[(nsp)]*(hz/100))
141 
142 ushort_t zsdelay[NSPEED] = {
143 	0,
144 	ZDELAY(50),	/* 1 */
145 	ZDELAY(75),	/* 2 */
146 	ZDELAY(110),    /* 3 */
147 #ifdef lint
148 	ZDELAY(134),    /* 4 */
149 #else
150 	ZDELAY(269/2),
151 #endif
152 	ZDELAY(150),    /* 5 */
153 	ZDELAY(200),    /* 6 */
154 	ZDELAY(300),    /* 7 */
155 	ZDELAY(600),    /* 8 */
156 	ZDELAY(1200),   /* 9 */
157 	ZDELAY(1800),   /* 10 */
158 	ZDELAY(2400),   /* 11 */
159 	ZDELAY(4800),   /* 12 */
160 	ZDELAY(9600),   /* 13 */
161 	ZDELAY(19200),  /* 14 */
162 	ZDELAY(38400),  /* 15 */
163 	ZDELAY(57600),  /* 16 */
164 	ZDELAY(76800)	/* 17 */
165 };
166 
167 ushort_t zslowat[NSPEED] = {
168 	3,		/* 0 */
169 	3,		/* 1 */
170 	3,		/* 2 */
171 	3,		/* 3 */
172 	3,		/* 4 */
173 	3,		/* 5 */
174 	3,		/* 6 */
175 	2,		/* 7 */
176 	2,		/* 8 */
177 	2,		/* 9 */
178 	2,		/* 10 */
179 	1,		/* 11 */
180 	1,		/* 12 */
181 	1,		/* 13 */
182 	1,		/* 14 */
183 	1,		/* 15 */
184 	1,		/* 16 */
185 	1		/* 17 */
186 };
187 
188 ushort_t zshiwat[NSPEED] = {
189 	0,		/* 0 */
190 	1,		/* 1 */
191 	1,		/* 2 */
192 	1,		/* 3 */
193 	1,		/* 4 */
194 	1,		/* 5 */
195 	1,		/* 6 */
196 	1,		/* 7 */
197 	1,		/* 8 */
198 	1,		/* 9 */
199 	1,		/* 10 */
200 	1,		/* 11 */
201 	1,		/* 12 */
202 	3,		/* 13 */
203 	3,		/* 14 */
204 	4,		/* 15 */
205 	4,		/* 16 */
206 	4		/* 17 */
207 };
208 
209 #define	SLAVIO_BUG	/* this workaround required to fix bug 1102778 */
210 
211 #define	SPEED(cflag) \
212 	((cflag) & CBAUDEXT) ? \
213 		(((cflag) & 0x1) + CBAUD + 1) : ((cflag) & CBAUD)
214 
215 /*
216  * Special macros to handle STREAMS operations.
217  * These are required to address memory leakage problems.
218  * WARNING : the macro do NOT call ZSSETSOFT
219  */
220 
221 /*
222  * Should be called holding only the adaptive (zs_excl) mutex.
223  */
224 #define	ZSA_GETBLOCK(zs, allocbcount) \
225 { \
226 	register int n = zsa_rstandby; \
227 	while (--n >= 0 && allocbcount > 0) { \
228 		if (!za->za_rstandby[n]) { \
229 			if ((za->za_rstandby[n] = allocb(ZSA_RCV_SIZE, \
230 			    BPRI_MED)) == NULL) { \
231 				if (za->za_bufcid == 0) { \
232 					za->za_bufcid = bufcall(ZSA_RCV_SIZE, \
233 					    BPRI_MED, \
234 					    zsa_callback, zs); \
235 					break; \
236 				} \
237 			} \
238 			allocbcount--; \
239 		} \
240 	} \
241 	if (za->za_ttycommon.t_cflag & CRTSXOFF) { \
242 		mutex_enter(zs->zs_excl_hi); \
243 		if (!(zs->zs_wreg[5] & ZSWR5_RTS)) { \
244 			register int usedcnt = 0; \
245 			for (n = 0; n < zsa_rstandby; n++) \
246 				if (!za->za_rstandby[n]) \
247 					usedcnt++; \
248 			if ((ushort_t)usedcnt <= \
249 			    zslowat[SPEED(za->za_ttycommon.t_cflag)]) \
250 				SCC_BIS(5, ZSWR5_RTS); \
251 		} \
252 		mutex_exit(zs->zs_excl_hi); \
253 	} \
254 }
255 
256 /*
257  * Should be called holding the spin (zs_excl_hi) mutex.
258  */
259 #define	ZSA_ALLOCB(mp) \
260 { \
261 	register int n = zsa_rstandby; \
262 	while (--n >= 0) { \
263 		if ((mp = za->za_rstandby[n]) != NULL) { \
264 			za->za_rstandby[n] = NULL; \
265 			break; \
266 		} \
267 	} \
268 	if (za->za_ttycommon.t_cflag & CRTSXOFF) { \
269 		if (!mp) { \
270 			if (zs->zs_wreg[5] & ZSWR5_RTS) \
271 				SCC_BIC(5, ZSWR5_RTS); \
272 			cmn_err(CE_WARN, "zs%d: message lost\n", \
273 				UNIT(za->za_dev)); \
274 		} else if (zs->zs_wreg[5] & ZSWR5_RTS) { \
275 			register int usedcnt = 0; \
276 			for (n = 0; n < zsa_rstandby; n++) \
277 				if (!za->za_rstandby[n]) \
278 					usedcnt++; \
279 			if ((ushort_t)usedcnt >= (zsa_rstandby - \
280 			    zshiwat[SPEED(za->za_ttycommon.t_cflag)])) \
281 				SCC_BIC(5, ZSWR5_RTS); \
282 		} \
283 	} \
284 }
285 
286 /*
287  * Should get the spin (zs_excl_hi) mutex.
288  */
289 #define	ZSA_QREPLY(q, mp) \
290 { \
291 	mutex_enter(zs->zs_excl_hi); \
292 	ZSA_PUTQ(mp); \
293 	ZSSETSOFT(zs); \
294 	mutex_exit(zs->zs_excl_hi); \
295 }
296 
297 /*
298  * Should be called holding the spin (zs_excl_hi) mutex.
299  */
300 #define	ZSA_PUTQ(mp) \
301 { \
302 	register int wptr, rptr; \
303 	wptr = za->za_rdone_wptr; \
304 	rptr = za->za_rdone_rptr; \
305 	za->za_rdone[wptr] = mp; \
306 	if ((wptr)+1 == zsa_rdone) { \
307 		za->za_rdone_wptr = wptr = 0; \
308 	} else \
309 		za->za_rdone_wptr = ++wptr; \
310 	if (wptr == rptr) { \
311 		SCC_BIC(1, ZSWR1_INIT); \
312 		cmn_err(CE_WARN, "zs%d disabled: input buffer overflow", \
313 			UNIT(za->za_dev)); \
314 	} \
315 }
316 
317 /*
318  * Should be called holding the spin (zs_excl_hi) mutex.
319  */
320 #define	ZSA_KICK_RCV \
321 { \
322 	register mblk_t *mp = za->za_rcvblk; \
323 	if (mp) { \
324 		if (zs->zs_rd_cur) {	/* M_DATA */ \
325 			mp->b_wptr = zs->zs_rd_cur; \
326 			zs->zs_rd_cur = NULL; \
327 			zs->zs_rd_lim = NULL; \
328 		} \
329 		za->za_rcvblk = NULL; \
330 		ZSA_PUTQ(mp); \
331 		ZSSETSOFT(zs); \
332 	} \
333 }
334 
335 #define	ZSA_SEEQ(mp) \
336 { \
337 		if (za->za_rdone_rptr != za->za_rdone_wptr) { \
338 			mp = za->za_rdone[za->za_rdone_rptr]; \
339 		} else { \
340 			mp = NULL; \
341 		} \
342 }
343 
344 
345 /*
346  * Should be called holding only the adaptive (zs_excl) mutex.
347  */
348 #define	ZSA_GETQ(mp) \
349 { \
350 	if (za->za_rdone_rptr != za->za_rdone_wptr) { \
351 		mp = za->za_rdone[za->za_rdone_rptr]; \
352 		za->za_rdone[za->za_rdone_rptr++] = NULL; \
353 		if (za->za_rdone_rptr == zsa_rdone) \
354 			za->za_rdone_rptr = 0; \
355 	} else { \
356 		mp = NULL; \
357 	} \
358 }
359 
360 /*
361  * Should be called holding only the adaptive (zs_excl) mutex.
362  */
363 #define	ZSA_FLUSHQ \
364 { \
365 	register mblk_t *tmp; \
366 	for (;;) { \
367 		ZSA_GETQ(tmp); \
368 		if (!(tmp)) \
369 			break; \
370 		freemsg(tmp); \
371 	} \
372 }
373 
374 
375 /*
376  * Logging definitions
377  */
378 
379 #ifdef ZSA_DEBUG
380 
381 #ifdef ZS_DEBUG_ALL
382 
383 extern	char	zs_h_log[];
384 extern	int	zs_h_log_n;
385 
386 #define	zsa_h_log_clear
387 
388 #define	zsa_h_log_add(c) \
389 { \
390 	if (zs_h_log_n >= ZS_H_LOG_MAX) \
391 		zs_h_log_n = 0; \
392 	zs_h_log[zs_h_log_n++] = 'A' + zs->zs_unit; \
393 	zs_h_log[zs_h_log_n++] = c; \
394 	zs_h_log[zs_h_log_n] = '\0'; \
395 }
396 
397 #else /* ZS_DEBUG_ALL */
398 
399 #define	ZSA_H_LOG_MAX	0x4000
400 char zsa_h_log[40][ZSA_H_LOG_MAX +10];
401 int zsa_h_log_n[40];
402 
403 #define	zsa_h_log_add(c) \
404 { \
405 	if (zsa_h_log_n[zs->zs_unit] >= ZSA_H_LOG_MAX) \
406 		zsa_h_log_n[zs->zs_unit] = 0; \
407 	zsa_h_log[zs->zs_unit][zsa_h_log_n[zs->zs_unit]++] = c; \
408 	zsa_h_log[zs->zs_unit][zsa_h_log_n[zs->zs_unit]] = '\0'; \
409 }
410 
411 #define	zsa_h_log_clear \
412 { \
413 	register char *p; \
414 	for (p = &zsa_h_log[zs->zs_unit][ZSA_H_LOG_MAX]; \
415 		p >= &zsa_h_log[zs->zs_unit][0]; /* null */) \
416 		*p-- = '\0'; \
417 	zsa_h_log_n[zs->zs_unit] = 0; \
418 }
419 
420 #endif /* ZS_DEBUG_ALL */
421 
422 #define	ZSA_R0_LOG(r0) \
423 { \
424 	if (r0 & ZSRR0_RX_READY) zsa_h_log_add('R'); \
425 	if (r0 & ZSRR0_TIMER) zsa_h_log_add('Z'); \
426 	if (r0 & ZSRR0_TX_READY) zsa_h_log_add('T'); \
427 	if (r0 & ZSRR0_CD) zsa_h_log_add('D'); \
428 	if (r0 & ZSRR0_SYNC) zsa_h_log_add('S'); \
429 	if (r0 & ZSRR0_CTS) zsa_h_log_add('C'); \
430 	if (r0 & ZSRR0_TXUNDER) zsa_h_log_add('U'); \
431 	if (r0 & ZSRR0_BREAK) zsa_h_log_add('B'); \
432 }
433 
434 #else /* ZSA_DEBUG */
435 
436 #define	zsa_h_log_clear
437 #define	zsa_h_log_add(c)
438 #define	 ZSA_R0_LOG(r0)
439 
440 #endif /* ZSA_DEBUG */
441 
442 
443 
444 static int zsa_open(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr);
445 static int zsa_close(queue_t *q, int flag);
446 static void zsa_wput(queue_t *q, mblk_t *mp);
447 static void zsa_rsrv(queue_t *q);
448 
449 static struct module_info asyncm_info = {
450 	0,
451 	"zs",
452 	0,
453 	INFPSZ,
454 	2048,
455 	128
456 };
457 
458 static struct qinit async_rinit = {
459 	putq,
460 	(int (*)())zsa_rsrv,
461 	zsa_open,
462 	zsa_close,
463 	NULL,
464 	&asyncm_info,
465 	NULL
466 };
467 
468 static struct qinit async_winit = {
469 	(int (*)())zsa_wput,
470 	NULL,
471 	NULL,
472 	NULL,
473 	NULL,
474 	&asyncm_info,
475 	NULL
476 };
477 
478 struct streamtab asynctab = {
479 	&async_rinit,
480 	&async_winit,
481 	NULL,
482 	NULL,
483 };
484 
485 /*
486  * The async interrupt entry points.
487  */
488 static void	zsa_txint(struct zscom *zs);
489 static void	zsa_xsint(struct zscom *zs);
490 static void	zsa_rxint(struct zscom *zs);
491 static void	zsa_srint(struct zscom *zs);
492 static int	zsa_softint(struct zscom *zs);
493 static int	zsa_suspend(struct zscom *zs);
494 static int	zsa_resume(struct zscom *zs);
495 
496 static void
497 zsa_null(struct zscom *zs)
498 {
499 	/* LINTED */
500 	register short	c;
501 
502 	SCC_WRITE0(ZSWR0_RESET_TXINT);
503 	SCC_WRITE0(ZSWR0_RESET_STATUS);
504 	c = SCC_READDATA();
505 	ZSDELAY();
506 	SCC_WRITE0(ZSWR0_RESET_ERRORS);
507 }
508 
509 /*ARGSUSED*/
510 static int
511 zsa_null_int(struct zscom *zs)
512 {
513 	return (0);
514 }
515 
516 struct zsops zsops_null_async = {
517 	zsa_null,
518 	zsa_null,
519 	zsa_null,
520 	zsa_null,
521 	zsa_null_int,
522 	zsa_null_int,
523 	zsa_null_int
524 };
525 
526 struct zsops zsops_async = {
527 	zsa_txint,
528 	zsa_xsint,
529 	zsa_rxint,
530 	zsa_srint,
531 	zsa_softint,
532 	zsa_suspend,
533 	zsa_resume
534 };
535 
536 static int	dmtozs(int bits);
537 static int	zstodm(int bits);
538 static void	zsa_restart(void *);
539 static void	zsa_reioctl(void *);
540 static void	zsa_ioctl(struct asyncline *za, queue_t *q, mblk_t *mp);
541 static void	zsa_program(struct asyncline *za, int setibaud);
542 static void	zsa_start(struct zscom *zs);
543 static void 	zsa_kick_rcv(void *);
544 static void 	zsa_callback(void *);
545 static void	zsa_set_za_rcv_flags_mask(struct asyncline *za);
546 int		zsgetspeed(dev_t dev);
547 
548 static boolean_t abort_charseq_recognize(uchar_t ch);
549 
550 /* ARGSUSED */
551 int
552 zsc_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
553     void **result)
554 {
555 	register dev_t dev = (dev_t)arg;
556 	register int unit, error;
557 	register struct zscom *zs;
558 
559 	if ((unit = UNIT(dev)) >= nzs)
560 		return (DDI_FAILURE);
561 
562 	switch (infocmd) {
563 	case DDI_INFO_DEVT2DEVINFO:
564 		zs = &zscom[unit];
565 		*result = zs->zs_dip;
566 		error = DDI_SUCCESS;
567 		break;
568 	case DDI_INFO_DEVT2INSTANCE:
569 		*result = (void *)(uintptr_t)(unit / 2);
570 		error = DDI_SUCCESS;
571 		break;
572 	default:
573 		error = DDI_FAILURE;
574 	}
575 	return (error);
576 }
577 
578 /*
579  * The Asynchronous Driver.
580  */
581 
582 /*
583  * Determine if the zsminor device is in use as either a stdin or stdout
584  * device, so we can be careful about how we initialize the DUART, if
585  * it is, in fact, in use.
586  *
587  * Since this is expensive, we do it once and store away the answers,
588  * since this gets called a number of times per phyical zs device.
589  * Perhaps, this should be in a loadable module, so it can get thrown
590  * away after all the zs devices are attached?
591  */
592 
593 /*
594  * To determine if a given unit is being used by the PROM,
595  * we need to map stdin/stdout devices as known to the PROM
596  * to zs internal minor device numbers:
597  *
598  * PROM (real device)	zs minor	device
599  *
600  * "zs", 0, "a"		 0		ttya
601  * "zs", 0, "b"		 1		ttyb
602  * "zs", 1, "a"		 2		keyboard
603  * "zs", 1, "b"		 3		mouse
604  * "zs", 2, "a"		 4		ttyc
605  * "zs", 2, "b"		 5		ttyd
606  *
607  * The following value mapping lines assume that insource
608  * and outsink map as "screen, a, b, c, d, ...", and that
609  * zs minors are "a, b, kbd, mouse, c, d, ...".
610  */
611 
612 static int zsa_inuse;		/* Strictly for debugging */
613 
614 int
615 zsa_channel_is_active_in_rom(dev_info_t *dev, int zsminor)
616 {
617 	char pathname[OBP_MAXPATHLEN];
618 	char default_pathname[OBP_MAXPATHLEN];
619 	char *stdioname;
620 	char minordata[3];
621 
622 	/*
623 	 * Basically, get my name and compare it to stdio devnames
624 	 * and if we get a match, then the device is in use as either
625 	 * stdin or stdout device (console tip line or keyboard device).
626 	 *
627 	 * We get two forms of the pathname, one complete with the
628 	 * the channel number, and if the channel is 'a', then
629 	 * we also deal with the user's ability to default to
630 	 * channel 'a', by omitting the channel number option.
631 	 * We then compare these pathnames to both the stdin and
632 	 * stdout pathnames. If any of these match, then the channel
633 	 * is in use.
634 	 */
635 
636 	(void) ddi_pathname(dev, pathname);	/* device pathname */
637 	default_pathname[0] = (char)0;	/* default pathname if channel 'a' */
638 	if ((zsminor & 1) == 0)
639 		(void) strcpy(default_pathname, pathname);
640 	minordata[0] = ':';
641 	minordata[1] = (char)('a' + (zsminor & 1));
642 	minordata[2] = (char)0;
643 	(void) strcat(pathname, minordata);
644 
645 	stdioname = prom_stdinpath();
646 	if (strcmp(pathname, stdioname) == 0) {
647 		zsa_inuse |= (1 << zsminor);
648 		return (1);
649 	}
650 	if (strcmp(default_pathname, stdioname) == 0) {
651 		zsa_inuse |= (1 << zsminor);
652 		return (1);
653 	}
654 
655 	stdioname = prom_stdoutpath();
656 	if (strcmp(pathname, stdioname) == 0) {
657 		zsa_inuse |= (1 << zsminor);
658 		return (1);
659 	}
660 	if (strcmp(default_pathname, stdioname) == 0) {
661 		zsa_inuse |= (1 << zsminor);
662 		return (1);
663 	}
664 
665 	return (0);
666 }
667 
668 /*
669  * Initialize zs
670  */
671 void
672 zsa_init(struct zscom *zs)
673 {
674 	/*
675 	 * This routine is called near the end of the zs module's attach
676 	 * process. It initializes the TTY protocol-private data for this
677 	 * channel that needs to be in place before interrupts are enabled.
678 	 */
679 	mutex_enter(zs->zs_excl);
680 	mutex_enter(zs->zs_excl_hi);
681 
682 	/*
683 	 * Raise modem control lines on serial ports associated
684 	 * with the console and (optionally) softcarrier lines.
685 	 * Drop modem control lines on all others so that modems
686 	 * will not answer and portselectors will skip these
687 	 * lines until they are opened by a getty.
688 	 */
689 	if (zsa_channel_is_active_in_rom(zs->zs_dip, zs->zs_unit))
690 		(void) zsmctl(zs, ZS_ON, DMSET);	/* raise dtr */
691 	else if (zsasoftdtr && (zssoftCAR[zs->zs_unit]))
692 		(void) zsmctl(zs, ZS_ON, DMSET);	/* raise dtr */
693 	else
694 		(void) zsmctl(zs, ZS_OFF, DMSET);	/* drop dtr */
695 
696 	if (zsa_rstandby > ZSA_MAX_RSTANDBY)
697 		zsa_rstandby = ZSA_MAX_RSTANDBY;
698 
699 	if (zsa_rdone > ZSA_RDONE_MAX)
700 		zsa_rdone = ZSA_RDONE_MAX;
701 
702 	if (zsa_grace_flow_control > ZSA_GRACE_MAX_FLOW_CONTROL)
703 		zsa_grace_flow_control = ZSA_GRACE_MAX_FLOW_CONTROL;
704 
705 	mutex_exit(zs->zs_excl_hi);
706 	mutex_exit(zs->zs_excl);
707 }
708 
709 
710 /*
711  * Open routine.
712  */
713 /*ARGSUSED*/
714 static int
715 zsa_open(queue_t *rq, dev_t *dev, int flag, int sflag, cred_t *cr)
716 {
717 	register struct zscom *zs;
718 	register struct asyncline *za;
719 	register int	speed, unit;
720 	struct termios *termiosp;
721 	int len;
722 	register int allocbcount = zsa_rstandby;
723 	boolean_t set_zsoptinit = B_FALSE;
724 
725 	unit = UNIT(*dev);
726 	if (unit >= nzs)
727 		return (ENXIO);		/* unit not configured */
728 
729 	/* zscom is allocated by zsattach, and thus cannot be NULL here */
730 	zs = &zscom[unit];
731 	if (zs->zs_ops == NULL) {
732 		return (ENXIO);	 /* device not found by autoconfig */
733 	}
734 
735 	mutex_enter(zs->zs_ocexcl);
736 	mutex_enter(zs->zs_excl);
737 again:
738 	if ((zs->zs_ops != &zsops_null) &&
739 	    (zs->zs_ops != &zsops_async)) {
740 		mutex_exit(zs->zs_excl);
741 		mutex_exit(zs->zs_ocexcl);
742 		return (EBUSY);	 /* another protocol got here first */
743 	}
744 
745 	za = (struct asyncline *)&zs->zs_priv_str;
746 
747 	if (zs->zs_suspended) {
748 		mutex_exit(zs->zs_excl);
749 		mutex_exit(zs->zs_ocexcl);
750 		(void) ddi_dev_is_needed(zs->zs_dip, 0, 1);
751 		mutex_enter(zs->zs_ocexcl);
752 		mutex_enter(zs->zs_excl);
753 	}
754 
755 	/* Mark device as busy (for power management) */
756 	(void) pm_busy_component(zs->zs_dip, unit%2+1);
757 
758 	if (zs->zs_ops == &zsops_null) {
759 		bzero(za, sizeof (zs->zs_priv_str));
760 		za->za_common = zs;
761 		if (zssoftCAR[zs->zs_unit])
762 			za->za_ttycommon.t_flags |= TS_SOFTCAR;
763 		zsopinit(zs, &zsops_async);
764 		set_zsoptinit = B_TRUE;
765 		za->za_rdone_wptr = 0;
766 		za->za_rdone_rptr = 0;
767 	}
768 
769 	zs->zs_priv = (caddr_t)za;
770 
771 	/*
772 	 * Block waiting for carrier to come up,
773 	 * unless this is a no-delay open.
774 	 */
775 	mutex_enter(zs->zs_excl_hi);
776 	if (!(za->za_flags & ZAS_ISOPEN)) {
777 		/*
778 		 * Get the default termios settings (cflag).
779 		 * These are stored as a property in the
780 		 * "options" node.
781 		 */
782 		mutex_exit(zs->zs_excl_hi);
783 		if (ddi_getlongprop(DDI_DEV_T_ANY,
784 		    ddi_root_node(), 0, "ttymodes",
785 		    (caddr_t)&termiosp, &len) == DDI_PROP_SUCCESS &&
786 		    len == sizeof (struct termios)) {
787 
788 			za->za_ttycommon.t_cflag = termiosp->c_cflag;
789 			kmem_free(termiosp, len);
790 		} else {
791 			/*
792 			 * Gack! Whine about it.
793 			 */
794 			cmn_err(CE_WARN,
795 			    "zs: Couldn't get ttymodes property!");
796 		}
797 		mutex_enter(zs->zs_excl_hi);
798 		if ((*dev == rconsdev) || (*dev == kbddev) ||
799 		    (*dev == stdindev)) {
800 			speed = zsgetspeed(*dev);
801 			za->za_ttycommon.t_cflag &= ~(CBAUD);
802 			if (speed > CBAUD) {
803 				za->za_ttycommon.t_cflag |= CBAUDEXT;
804 				za->za_ttycommon.t_cflag |=
805 				    ((speed - CBAUD - 1) & CBAUD);
806 			} else {
807 				za->za_ttycommon.t_cflag &= ~CBAUDEXT;
808 				za->za_ttycommon.t_cflag |= (speed & CBAUD);
809 			}
810 		}
811 		za->za_overrun = 0;
812 		za->za_ttycommon.t_iflag = 0;
813 		za->za_ttycommon.t_iocpending = NULL;
814 		za->za_ttycommon.t_size.ws_row = 0;
815 		za->za_ttycommon.t_size.ws_col = 0;
816 		za->za_ttycommon.t_size.ws_xpixel = 0;
817 		za->za_ttycommon.t_size.ws_ypixel = 0;
818 		za->za_dev = *dev;
819 		za->za_wbufcid = 0;
820 		zsa_program(za, za->za_ttycommon.t_cflag & (CIBAUDEXT|CIBAUD));
821 		zsa_set_za_rcv_flags_mask(za);
822 	} else if ((za->za_ttycommon.t_flags & TS_XCLUDE) &&
823 	    secpolicy_excl_open(cr) != 0) {
824 		mutex_exit(zs->zs_excl_hi);
825 		if (set_zsoptinit && !(za->za_flags & ISOPEN))
826 			zsopinit(zs, &zsops_null);
827 		mutex_exit(zs->zs_excl);
828 		mutex_exit(zs->zs_ocexcl);
829 		return (EBUSY);
830 	} else if ((*dev & OUTLINE) && !(za->za_flags & ZAS_OUT)) {
831 		mutex_exit(zs->zs_excl_hi);
832 		if (set_zsoptinit && !(za->za_flags & ISOPEN))
833 			zsopinit(zs, &zsops_null);
834 		mutex_exit(zs->zs_excl);
835 		mutex_exit(zs->zs_ocexcl);
836 		return (EBUSY);
837 	}
838 
839 	if (*dev & OUTLINE)
840 		za->za_flags |= ZAS_OUT;
841 	(void) zsmctl(zs, ZS_ON, DMSET);
842 
843 	/*
844 	 * Check carrier.
845 	 */
846 	if ((za->za_ttycommon.t_flags & TS_SOFTCAR) ||
847 	    (zsmctl(zs, 0, DMGET) & ZSRR0_CD))
848 		za->za_flags |= ZAS_CARR_ON;
849 	mutex_exit(zs->zs_excl_hi);
850 
851 	/*
852 	 * If FNDELAY and FNONBLOCK are clear, block until carrier up.
853 	 * Quit on interrupt.
854 	 */
855 	if (!(flag & (FNDELAY|FNONBLOCK)) &&
856 	    !(za->za_ttycommon.t_cflag & CLOCAL)) {
857 		if (!(za->za_flags & (ZAS_CARR_ON|ZAS_OUT)) ||
858 		    ((za->za_flags & ZAS_OUT) && !(*dev & OUTLINE))) {
859 			za->za_flags |= ZAS_WOPEN;
860 			mutex_exit(zs->zs_excl);
861 			if (cv_wait_sig(&zs->zs_flags_cv, zs->zs_ocexcl) == 0) {
862 				mutex_enter(zs->zs_excl);
863 				if (zs->zs_suspended) {
864 					mutex_exit(zs->zs_excl);
865 					mutex_exit(zs->zs_ocexcl);
866 					(void) ddi_dev_is_needed(zs->zs_dip,
867 					    0, 1);
868 					mutex_enter(zs->zs_ocexcl);
869 					mutex_enter(zs->zs_excl);
870 				}
871 				za->za_flags &= ~ZAS_WOPEN;
872 				if (set_zsoptinit && !(za->za_flags & ISOPEN))
873 					zsopinit(zs, &zsops_null);
874 				mutex_exit(zs->zs_excl);
875 				mutex_exit(zs->zs_ocexcl);
876 				return (EINTR);
877 			}
878 			mutex_enter(zs->zs_excl);
879 			za->za_flags &= ~ZAS_WOPEN;
880 			if ((zs->zs_ops == &zsops_null) ||
881 			    (zs->zs_ops == &zsops_async))
882 				goto again;
883 			else {
884 				if (set_zsoptinit && !(za->za_flags & ISOPEN))
885 					zsopinit(zs, &zsops_null);
886 				mutex_exit(zs->zs_excl);
887 				mutex_exit(zs->zs_ocexcl);
888 				return (EBUSY);
889 			}
890 		}
891 	} else if ((za->za_flags & ZAS_OUT) && !(*dev & OUTLINE)) {
892 		if (set_zsoptinit && !(za->za_flags & ISOPEN))
893 			zsopinit(zs, &zsops_null);
894 		mutex_exit(zs->zs_excl);
895 		mutex_exit(zs->zs_ocexcl);
896 		return (EBUSY);
897 	}
898 
899 	za->za_ttycommon.t_readq = rq;
900 	za->za_ttycommon.t_writeq = WR(rq);
901 	rq->q_ptr = WR(rq)->q_ptr = (caddr_t)za;
902 
903 	za->za_flags |= ZAS_ISOPEN;
904 	ZSA_GETBLOCK(zs, allocbcount);
905 	qprocson(rq);
906 	mutex_exit(zs->zs_excl);
907 	mutex_exit(zs->zs_ocexcl);
908 	return (0);
909 }
910 
911 static void
912 zs_progress_check(void *arg)
913 {
914 	struct asyncline *za = arg;
915 	struct zscom *zs = za->za_common;
916 	mblk_t *bp;
917 
918 	/*
919 	 * We define "progress" as either waiting on a timed break or delay, or
920 	 * having had at least one transmitter interrupt.  If none of these are
921 	 * true, then just terminate the output and wake up that close thread.
922 	 */
923 	mutex_enter(zs->zs_excl);
924 	if (!(zs->zs_flags & ZS_PROGRESS) &&
925 	    !(za->za_flags & (ZAS_BREAK|ZAS_DELAY))) {
926 		za->za_flags &= ~ZAS_BUSY;
927 		mutex_enter(zs->zs_excl_hi);
928 		za->za_rcv_flags_mask &= ~DO_RETRANSMIT;
929 		zs->zs_wr_cur = NULL;
930 		zs->zs_wr_lim = NULL;
931 		bp = za->za_xmitblk;
932 		za->za_xmitblk = NULL;
933 		mutex_exit(zs->zs_excl_hi);
934 		zs->zs_timer = 0;
935 		mutex_exit(zs->zs_excl);
936 		if (bp != NULL)
937 			freeb(bp);
938 		/*
939 		 * Since this timer is running, we know that we're in exit(2).
940 		 * That means that the user can't possibly be waiting on any
941 		 * valid ioctl(2) completion anymore, and we should just flush
942 		 * everything.
943 		 */
944 		flushq(za->za_ttycommon.t_writeq, FLUSHALL);
945 		cv_broadcast(&zs->zs_flags_cv);
946 	} else {
947 		zs->zs_flags &= ~ZS_PROGRESS;
948 		zs->zs_timer = timeout(zs_progress_check, za,
949 		    drv_usectohz(zs_drain_check));
950 		mutex_exit(zs->zs_excl);
951 	}
952 }
953 
954 /*
955  * Close routine.
956  *
957  * Important locking note: the zs_ocexcl lock is not held at all in this
958  * routine.  This is intentional.  That lock is used to coordinate multiple
959  * simultaneous opens on a stream, and there's no such thing as multiple
960  * simultaneous closes on a stream.
961  */
962 
963 /*ARGSUSED*/
964 static int
965 zsa_close(queue_t *q, int flag)
966 {
967 	struct asyncline *za;
968 	struct zscom *zs;
969 	int i;
970 	mblk_t *bp;
971 	timeout_id_t za_zsa_restart_id, za_kick_rcv_id;
972 	bufcall_id_t za_bufcid, za_wbufcid;
973 	int	 tmp;
974 
975 	za = q->q_ptr;
976 	ASSERT(za != NULL);
977 
978 	zs = za->za_common;
979 
980 	mutex_enter(zs->zs_excl);
981 	zs->zs_flags |= ZS_CLOSING;
982 
983 	/*
984 	 * There are two flavors of break -- timed (M_BREAK or TCSBRK) and
985 	 * untimed (TIOCSBRK).  For the timed case, these are enqueued on our
986 	 * write queue and there's a timer running, so we don't have to worry
987 	 * about them.  For the untimed case, though, the user obviously made a
988 	 * mistake, because these are handled immediately.  We'll terminate the
989 	 * break now and honor his implicit request by discarding the rest of
990 	 * the data.
991 	 */
992 	if (!(za->za_flags & ZAS_BREAK) && (zs->zs_wreg[5] & ZSWR5_BREAK))
993 		goto nodrain;
994 
995 	/*
996 	 * If the user told us not to delay the close ("non-blocking"), then
997 	 * don't bother trying to drain.
998 	 *
999 	 * If the user did M_STOP (ASYNC_STOPPED), there's no hope of ever
1000 	 * getting an M_START (since these messages aren't enqueued), and the
1001 	 * only other way to clear the stop condition is by loss of DCD, which
1002 	 * would discard the queue data.  Thus, we drop the output data if
1003 	 * ASYNC_STOPPED is set.
1004 	 */
1005 	if ((flag & (FNDELAY|FNONBLOCK)) || (za->za_flags & ZAS_STOPPED))
1006 		goto nodrain;
1007 
1008 	/*
1009 	 * If there's any pending output, then we have to try to drain it.
1010 	 * There are two main cases to be handled:
1011 	 *	- called by close(2): need to drain until done or until
1012 	 *	  a signal is received.  No timeout.
1013 	 *	- called by exit(2): need to drain while making progress
1014 	 *	  or until a timeout occurs.  No signals.
1015 	 *
1016 	 * If we can't rely on receiving a signal to get us out of a hung
1017 	 * session, then we have to use a timer.  In this case, we set a timer
1018 	 * to check for progress in sending the output data -- all that we ask
1019 	 * (at each interval) is that there's been some progress made.  Since
1020 	 * the interrupt routine grabs buffers from the write queue, we can't
1021 	 * trust changes in zs_wr_cur.  Instead, we use a progress flag.
1022 	 *
1023 	 * Note that loss of carrier will cause the output queue to be flushed,
1024 	 * and we'll wake up again and finish normally.
1025 	 */
1026 	if (!ddi_can_receive_sig() && zs_drain_check != 0) {
1027 		zs->zs_flags &= ~ZS_PROGRESS;
1028 		zs->zs_timer = timeout(zs_progress_check, za,
1029 		    drv_usectohz(zs_drain_check));
1030 	}
1031 
1032 	while (zs->zs_wr_cur != NULL ||
1033 	    za->za_ttycommon.t_writeq->q_first != NULL ||
1034 	    (za->za_flags & (ZAS_BUSY|ZAS_DELAY|ZAS_BREAK))) {
1035 		if (cv_wait_sig(&zs->zs_flags_cv, zs->zs_excl) == 0)
1036 			break;
1037 	}
1038 
1039 	if (zs->zs_timer != 0) {
1040 		(void) untimeout(zs->zs_timer);
1041 		zs->zs_timer = 0;
1042 	}
1043 
1044 nodrain:
1045 	/*
1046 	 * If break is in progress, stop it.
1047 	 */
1048 	mutex_enter(zs->zs_excl_hi);
1049 	if (zs->zs_wreg[5] & ZSWR5_BREAK) {
1050 		SCC_BIC(5, ZSWR5_BREAK);
1051 		za->za_flags &= ~ZAS_BREAK;
1052 	}
1053 
1054 	za_wbufcid = za->za_wbufcid;
1055 	za_bufcid = za->za_bufcid;
1056 	za_zsa_restart_id = za->za_zsa_restart_id;
1057 	za_kick_rcv_id = za->za_kick_rcv_id;
1058 
1059 	za->za_wbufcid = za->za_bufcid = 0;
1060 	za->za_zsa_restart_id = za->za_kick_rcv_id = 0;
1061 
1062 	/*
1063 	 * If line has HUPCL set or is incompletely opened,
1064 	 * and it is not the console or the keyboard,
1065 	 * fix up the modem lines.
1066 	 */
1067 
1068 	zsopinit(zs, &zsops_null_async);
1069 
1070 	/*
1071 	 * Nobody, zsh or zs can now open this port until
1072 	 * zsopinit(zs, &zsops_null);
1073 	 *
1074 	 */
1075 
1076 	if ((za->za_dev != rconsdev) && (za->za_dev != kbddev) &&
1077 	    (za->za_dev != stdindev) &&
1078 	    (((za->za_flags & (ZAS_WOPEN|ZAS_ISOPEN)) != ZAS_ISOPEN) ||
1079 	    (za->za_ttycommon.t_cflag & HUPCL))) {
1080 		/*
1081 		 * If DTR is being held high by softcarrier,
1082 		 * set up the ZS_ON set; if not, hang up.
1083 		 */
1084 		if (zsasoftdtr && (za->za_ttycommon.t_flags & TS_SOFTCAR))
1085 			(void) zsmctl(zs, ZS_ON, DMSET);
1086 		else
1087 			(void) zsmctl(zs, ZS_OFF, DMSET);
1088 		mutex_exit(zs->zs_excl_hi);
1089 		/*
1090 		 * Don't let an interrupt in the middle of close
1091 		 * bounce us back to the top; just continue
1092 		 * closing as if nothing had happened.
1093 		 */
1094 		tmp = cv_reltimedwait_sig(&zs->zs_flags_cv, zs->zs_excl,
1095 		    drv_usectohz(10000), TR_CLOCK_TICK);
1096 		if (zs->zs_suspended) {
1097 			mutex_exit(zs->zs_excl);
1098 			(void) ddi_dev_is_needed(zs->zs_dip, 0, 1);
1099 			mutex_enter(zs->zs_excl);
1100 		}
1101 		if (tmp == 0)
1102 			goto out;
1103 		mutex_enter(zs->zs_excl_hi);
1104 	}
1105 
1106 	/*
1107 	 * If nobody's now using it, turn off receiver interrupts.
1108 	 */
1109 	if ((za->za_flags & (ZAS_ISOPEN|ZAS_WOPEN)) == 0)
1110 		SCC_BIC(1, ZSWR1_RIE);
1111 	mutex_exit(zs->zs_excl_hi);
1112 
1113 out:
1114 	/*
1115 	 * Clear out device state.
1116 	 */
1117 	ttycommon_close(&za->za_ttycommon);
1118 
1119 	za->za_ttycommon.t_readq = NULL;
1120 	za->za_ttycommon.t_writeq = NULL;
1121 
1122 	mutex_enter(zs->zs_excl_hi);
1123 	za->za_rcv_flags_mask &= ~DO_RETRANSMIT;
1124 	zs->zs_wr_cur = NULL;
1125 	zs->zs_wr_lim = NULL;
1126 	bp = za->za_xmitblk;
1127 	za->za_xmitblk = NULL;
1128 	mutex_exit(zs->zs_excl_hi);
1129 	if (bp)
1130 		freemsg(bp);
1131 
1132 	mutex_enter(zs->zs_excl_hi);
1133 	zs->zs_rd_cur = NULL;
1134 	zs->zs_rd_lim = NULL;
1135 	bp = za->za_rcvblk;
1136 	za->za_rcvblk = NULL;
1137 	mutex_exit(zs->zs_excl_hi);
1138 	if (bp)
1139 		freemsg(bp);
1140 
1141 	for (i = 0; i < zsa_rstandby; i++) {
1142 		mutex_enter(zs->zs_excl_hi);
1143 		bp = za->za_rstandby[i];
1144 		za->za_rstandby[i] = NULL;
1145 		mutex_exit(zs->zs_excl_hi);
1146 		if (bp)
1147 			freemsg(bp);
1148 	}
1149 
1150 	if (za->za_soft_active || za->za_kick_active) {
1151 		zs->zs_flags |= ZS_CLOSED;
1152 		while (za->za_soft_active || za->za_kick_active)
1153 			cv_wait(&zs->zs_flags_cv, zs->zs_excl);
1154 	}
1155 	if (zs->zs_suspended) {
1156 		mutex_exit(zs->zs_excl);
1157 		(void) ddi_dev_is_needed(zs->zs_dip, 0, 1);
1158 		mutex_enter(zs->zs_excl);
1159 	}
1160 
1161 	ZSA_FLUSHQ;
1162 	bzero(za, sizeof (struct asyncline));
1163 	qprocsoff(q);
1164 	mutex_exit(zs->zs_excl);
1165 
1166 	/*
1167 	 * Cancel outstanding "bufcall" request.
1168 	 */
1169 	if (za_wbufcid)
1170 		unbufcall(za_wbufcid);
1171 	if (za_bufcid)
1172 		unbufcall(za_bufcid);
1173 
1174 	/*
1175 	 * Cancel outstanding timeout.
1176 	 */
1177 	if (za_zsa_restart_id)
1178 		(void) untimeout(za_zsa_restart_id);
1179 
1180 	if (za_kick_rcv_id)
1181 		(void) untimeout(za_kick_rcv_id);
1182 
1183 	q->q_ptr = WR(q)->q_ptr = NULL;
1184 	zsopinit(zs, &zsops_null);
1185 	cv_broadcast(&zs->zs_flags_cv);
1186 
1187 	/* Mark device as available for power management */
1188 	(void) pm_idle_component(zs->zs_dip, zs->zs_unit%2+1);
1189 	return (0);
1190 }
1191 
1192 /*
1193  * Put procedure for write queue.
1194  * Respond to M_STOP, M_START, M_IOCTL, and M_FLUSH messages here;
1195  * set the flow control character for M_STOPI and M_STARTI messages;
1196  * queue up M_BREAK, M_DELAY, and M_DATA messages for processing
1197  * by the start routine, and then call the start routine; discard
1198  * everything else. Note that this driver does not incorporate any
1199  * mechanism to negotiate to handle the canonicalization process.
1200  * It expects that these functions are handled in upper module(s),
1201  * as we do in ldterm.
1202  */
1203 static void
1204 zsa_wput(queue_t *q, mblk_t *mp)
1205 {
1206 	register struct asyncline	*za;
1207 	register struct zscom		*zs;
1208 	register struct copyresp	*resp;
1209 	register mblk_t			*bp = NULL;
1210 	int				error;
1211 	struct iocblk			*iocp;
1212 
1213 	za = (struct asyncline *)q->q_ptr;
1214 	zs = za->za_common;
1215 	if (zs->zs_flags & ZS_NEEDSOFT) {
1216 		zs->zs_flags &= ~ZS_NEEDSOFT;
1217 		(void) zsa_softint(zs);
1218 	}
1219 
1220 	switch (mp->b_datap->db_type) {
1221 
1222 	case M_STOP:
1223 		/*
1224 		 * Since we don't do real DMA, we can just let the
1225 		 * chip coast to a stop after applying the brakes.
1226 		 */
1227 		mutex_enter(zs->zs_excl);
1228 		mutex_enter(zs->zs_excl_hi);
1229 		za->za_flags |= ZAS_STOPPED;
1230 		if ((zs->zs_wr_cur) != NULL) {
1231 			za->za_flags &= ~ZAS_BUSY;
1232 			za->za_rcv_flags_mask &= ~DO_RETRANSMIT;
1233 			bp = za->za_xmitblk;
1234 			bp->b_rptr = zs->zs_wr_cur;
1235 			zs->zs_wr_cur = NULL;
1236 			zs->zs_wr_lim = NULL;
1237 			za->za_xmitblk = NULL;
1238 		}
1239 		mutex_exit(zs->zs_excl_hi);
1240 		if (bp)
1241 			(void) putbq(q, bp);
1242 		freemsg(mp);
1243 		mutex_exit(zs->zs_excl);
1244 		break;
1245 
1246 	case M_START:
1247 		mutex_enter(zs->zs_excl);
1248 		if (za->za_flags & ZAS_STOPPED) {
1249 			za->za_flags &= ~ZAS_STOPPED;
1250 			/*
1251 			 * If an output operation is in progress,
1252 			 * resume it. Otherwise, prod the start
1253 			 * routine.
1254 			 */
1255 			zsa_start(zs);
1256 		}
1257 		freemsg(mp);
1258 		mutex_exit(zs->zs_excl);
1259 		break;
1260 
1261 	case M_IOCTL:
1262 		mutex_enter(zs->zs_excl);
1263 		iocp = (struct iocblk *)mp->b_rptr;
1264 
1265 		switch (iocp->ioc_cmd) {
1266 
1267 		case TIOCGPPS:
1268 			/*
1269 			 * Get PPS state.
1270 			 */
1271 			if (mp->b_cont != NULL)
1272 				freemsg(mp->b_cont);
1273 
1274 			mp->b_cont = allocb(sizeof (int), BPRI_HI);
1275 			if (mp->b_cont == NULL) {
1276 				mp->b_datap->db_type = M_IOCNAK;
1277 				iocp->ioc_error = ENOMEM;
1278 				ZSA_QREPLY(q, mp);
1279 				break;
1280 			}
1281 			if (za->za_pps)
1282 				*(int *)mp->b_cont->b_wptr = 1;
1283 			else
1284 				*(int *)mp->b_cont->b_wptr = 0;
1285 			mp->b_cont->b_wptr += sizeof (int);
1286 			mp->b_datap->db_type = M_IOCACK;
1287 			iocp->ioc_count = sizeof (int);
1288 			ZSA_QREPLY(q, mp);
1289 			break;
1290 
1291 		case TIOCSPPS:
1292 			/*
1293 			 * Set PPS state.
1294 			 */
1295 			error = miocpullup(mp, sizeof (int));
1296 			if (error != 0) {
1297 				mp->b_datap->db_type = M_IOCNAK;
1298 				iocp->ioc_error = error;
1299 				ZSA_QREPLY(q, mp);
1300 				break;
1301 			}
1302 
1303 			za->za_pps = (*(int *)mp->b_cont->b_rptr != 0);
1304 			mp->b_datap->db_type = M_IOCACK;
1305 			ZSA_QREPLY(q, mp);
1306 			break;
1307 
1308 		case TIOCGPPSEV:
1309 		{
1310 			/*
1311 			 * Get PPS event data.
1312 			 */
1313 			void *buf;
1314 #ifdef _SYSCALL32_IMPL
1315 			struct ppsclockev32 p32;
1316 #endif
1317 
1318 			if (mp->b_cont != NULL) {
1319 				freemsg(mp->b_cont);
1320 				mp->b_cont = NULL;
1321 			}
1322 			if (za->za_pps == NULL) {
1323 				mp->b_datap->db_type = M_IOCNAK;
1324 				iocp->ioc_error = ENXIO;
1325 				ZSA_QREPLY(q, mp);
1326 				break;
1327 			}
1328 
1329 #ifdef _SYSCALL32_IMPL
1330 			if ((iocp->ioc_flag & IOC_MODELS) != IOC_NATIVE) {
1331 				TIMEVAL_TO_TIMEVAL32(&p32.tv, &ppsclockev.tv);
1332 				p32.serial = ppsclockev.serial;
1333 				buf = &p32;
1334 				iocp->ioc_count = sizeof (struct ppsclockev32);
1335 			} else
1336 #endif
1337 			{
1338 				buf = &ppsclockev;
1339 				iocp->ioc_count = sizeof (struct ppsclockev);
1340 			}
1341 
1342 			if ((bp = allocb(iocp->ioc_count, BPRI_HI)) == NULL) {
1343 				mp->b_datap->db_type = M_IOCNAK;
1344 				iocp->ioc_error = ENOMEM;
1345 				ZSA_QREPLY(q, mp);
1346 				break;
1347 			}
1348 			mp->b_cont = bp;
1349 
1350 			bcopy(buf, bp->b_wptr, iocp->ioc_count);
1351 			bp->b_wptr += iocp->ioc_count;
1352 			mp->b_datap->db_type = M_IOCACK;
1353 			ZSA_QREPLY(q, mp);
1354 			break;
1355 		}
1356 
1357 		case TCSETSW:
1358 		case TCSETSF:
1359 		case TCSETAW:
1360 		case TCSETAF:
1361 		case TCSBRK:
1362 			/*
1363 			 * The changes do not take effect until all
1364 			 * output queued before them is drained.
1365 			 * Put this message on the queue, so that
1366 			 * "zsa_start" will see it when it's done
1367 			 * with the output before it. Poke the
1368 			 * start routine, just in case.
1369 			 */
1370 			(void) putq(q, mp);
1371 			zsa_start(zs);
1372 			break;
1373 
1374 		default:
1375 			/*
1376 			 * Do it now.
1377 			 */
1378 			zsa_ioctl(za, q, mp);
1379 			break;
1380 		}
1381 		mutex_exit(zs->zs_excl);
1382 		break;
1383 
1384 
1385 	case M_IOCDATA:
1386 
1387 		mutex_enter(zs->zs_excl);
1388 		resp = (struct copyresp *)mp->b_rptr;
1389 		if (resp->cp_rval) {
1390 			/*
1391 			 * Just free message on failure.
1392 			 */
1393 			freemsg(mp);
1394 			mutex_exit(zs->zs_excl);
1395 			break;
1396 		}
1397 		switch (resp->cp_cmd) {
1398 
1399 		case TIOCMSET:
1400 			mutex_enter(zs->zs_excl_hi);
1401 			(void) zsmctl(zs, dmtozs(*(int *)mp->b_cont->b_rptr),
1402 			    DMSET);
1403 			mutex_exit(zs->zs_excl_hi);
1404 			mioc2ack(mp, NULL, 0, 0);
1405 			ZSA_QREPLY(q, mp);
1406 			break;
1407 
1408 		case TIOCMBIS:
1409 			mutex_enter(zs->zs_excl_hi);
1410 			(void) zsmctl(zs, dmtozs(*(int *)mp->b_cont->b_rptr),
1411 			    DMBIS);
1412 			mutex_exit(zs->zs_excl_hi);
1413 			mioc2ack(mp, NULL, 0, 0);
1414 			ZSA_QREPLY(q, mp);
1415 			break;
1416 
1417 		case TIOCMBIC:
1418 			mutex_enter(zs->zs_excl_hi);
1419 			(void) zsmctl(zs, dmtozs(*(int *)mp->b_cont->b_rptr),
1420 			    DMBIC);
1421 			mutex_exit(zs->zs_excl_hi);
1422 			mioc2ack(mp, NULL, 0, 0);
1423 			ZSA_QREPLY(q, mp);
1424 			break;
1425 
1426 		case TIOCMGET:
1427 			mioc2ack(mp, NULL, 0, 0);
1428 			ZSA_QREPLY(q, mp);
1429 			break;
1430 
1431 		default:
1432 			freemsg(mp);
1433 
1434 		}
1435 		mutex_exit(zs->zs_excl);
1436 		break;
1437 
1438 
1439 	case M_FLUSH:
1440 		mutex_enter(zs->zs_excl);
1441 		if (*mp->b_rptr & FLUSHW) {
1442 
1443 			/*
1444 			 * Abort any output in progress.
1445 			 */
1446 			if (za->za_flags & ZAS_BUSY) {
1447 				za->za_flags &= ~ZAS_BUSY;
1448 				mutex_enter(zs->zs_excl_hi);
1449 				za->za_rcv_flags_mask &= ~DO_RETRANSMIT;
1450 				zs->zs_wr_cur = NULL;
1451 				zs->zs_wr_lim = NULL;
1452 				bp = za->za_xmitblk;
1453 				za->za_xmitblk = NULL;
1454 				mutex_exit(zs->zs_excl_hi);
1455 				if (bp)
1456 					freemsg(bp);
1457 			}
1458 			/*
1459 			 * Flush our write queue.
1460 			 */
1461 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
1462 			*mp->b_rptr &= ~FLUSHW;	/* it has been flushed */
1463 		}
1464 		if (*mp->b_rptr & FLUSHR) {
1465 			/*
1466 			 * Flush any data in the temporary receive buffer
1467 			 */
1468 			mutex_enter(zs->zs_excl_hi);
1469 			if ((za->za_ttycommon.t_flags & TS_SOFTCAR) ||
1470 			    (SCC_READ0() & ZSRR0_CD)) {
1471 				ZSA_KICK_RCV;
1472 			} else {
1473 				ZSA_KICK_RCV;
1474 				if (!(SCC_READ0() & ZSRR0_RX_READY)) {
1475 					/*
1476 					 * settle time for 1 character shift
1477 					 */
1478 					mutex_exit(zs->zs_excl_hi);
1479 					mutex_exit(zs->zs_excl);
1480 					delay(ztdelay(SPEED(
1481 					    za->za_ttycommon.t_cflag))/3 + 1);
1482 					mutex_enter(zs->zs_excl);
1483 					mutex_enter(zs->zs_excl_hi);
1484 					if (!(SCC_READ0() & ZSRR0_CD))
1485 						ZSA_KICK_RCV;
1486 				}
1487 				while ((SCC_READ0() &
1488 				    (ZSRR0_CD | ZSRR0_RX_READY)) ==
1489 				    ZSRR0_RX_READY) {
1490 					/*
1491 					 * Empty Receiver
1492 					 */
1493 					(void) SCC_READDATA();
1494 				}
1495 			}
1496 			mutex_exit(zs->zs_excl_hi);
1497 			flushq(RD(q), FLUSHDATA);
1498 			ZSA_QREPLY(q, mp);
1499 			/*
1500 			 * give the read queues a crack at it
1501 			 */
1502 		} else
1503 			freemsg(mp);
1504 
1505 		/*
1506 		 * We must make sure we process messages that survive the
1507 		 * write-side flush. Without this call, the close protocol
1508 		 * with ldterm can hang forever.  (ldterm will have sent us a
1509 		 * TCSBRK ioctl that it expects a response to.)
1510 		 */
1511 		zsa_start(zs);
1512 		mutex_exit(zs->zs_excl);
1513 		break;
1514 
1515 	case M_BREAK:
1516 	case M_DELAY:
1517 	case M_DATA:
1518 		mutex_enter(zs->zs_excl);
1519 		/*
1520 		 * Queue the message up to be transmitted,
1521 		 * and poke the start routine.
1522 		 */
1523 		(void) putq(q, mp);
1524 		zsa_start(zs);
1525 		mutex_exit(zs->zs_excl);
1526 		break;
1527 
1528 	case M_STOPI:
1529 		mutex_enter(zs->zs_excl);
1530 		mutex_enter(zs->zs_excl_hi);
1531 		za->za_flowc = za->za_ttycommon.t_stopc;
1532 		if ((zs->zs_wr_cur) != NULL) {
1533 			za->za_rcv_flags_mask &= ~DO_RETRANSMIT;
1534 			bp = za->za_xmitblk;
1535 			bp->b_rptr = zs->zs_wr_cur;
1536 			zs->zs_wr_cur = NULL;
1537 			zs->zs_wr_lim = NULL;
1538 			za->za_xmitblk = NULL;
1539 		}
1540 		mutex_exit(zs->zs_excl_hi);
1541 		if (bp)
1542 			(void) putbq(q, bp);
1543 		else
1544 			zsa_start(zs);		/* poke the start routine */
1545 		freemsg(mp);
1546 		mutex_exit(zs->zs_excl);
1547 		break;
1548 
1549 	case M_STARTI:
1550 		mutex_enter(zs->zs_excl);
1551 		mutex_enter(zs->zs_excl_hi);
1552 		za->za_flowc = za->za_ttycommon.t_startc;
1553 		if ((zs->zs_wr_cur) != NULL) {
1554 			za->za_rcv_flags_mask &= ~DO_RETRANSMIT;
1555 			bp = za->za_xmitblk;
1556 			bp->b_rptr = zs->zs_wr_cur;
1557 			zs->zs_wr_cur = NULL;
1558 			zs->zs_wr_lim = NULL;
1559 			za->za_xmitblk = NULL;
1560 		}
1561 		mutex_exit(zs->zs_excl_hi);
1562 		if (bp)
1563 			(void) putbq(q, bp);
1564 		else
1565 			zsa_start(zs);		/* poke the start routine */
1566 		freemsg(mp);
1567 		mutex_exit(zs->zs_excl);
1568 		break;
1569 
1570 	case M_CTL:
1571 		if (MBLKL(mp) >= sizeof (struct iocblk) &&
1572 		    ((struct iocblk *)mp->b_rptr)->ioc_cmd == MC_POSIXQUERY) {
1573 			((struct iocblk *)mp->b_rptr)->ioc_cmd = MC_HAS_POSIX;
1574 			qreply(q, mp);
1575 		} else {
1576 			/*
1577 			 * These MC_SERVICE type messages are used by upper
1578 			 * modules to tell this driver to send input up
1579 			 * immediately, or that it can wait for normal
1580 			 * processing that may or may not be done. Sun
1581 			 * requires these for the mouse module.
1582 			 */
1583 			mutex_enter(zs->zs_excl);
1584 			switch (*mp->b_rptr) {
1585 
1586 			case MC_SERVICEIMM:
1587 				mutex_enter(zs->zs_excl_hi);
1588 				za->za_flags |= ZAS_SERVICEIMM;
1589 				mutex_exit(zs->zs_excl_hi);
1590 				break;
1591 
1592 			case MC_SERVICEDEF:
1593 				mutex_enter(zs->zs_excl_hi);
1594 				za->za_flags &= ~ZAS_SERVICEIMM;
1595 				mutex_exit(zs->zs_excl_hi);
1596 				break;
1597 			}
1598 			freemsg(mp);
1599 			mutex_exit(zs->zs_excl);
1600 		}
1601 		break;
1602 
1603 	default:
1604 		/*
1605 		 * "No, I don't want a subscription to Chain Store Age,
1606 		 * thank you anyway."
1607 		 */
1608 		freemsg(mp);
1609 		break;
1610 	}
1611 }
1612 
1613 /*
1614  * zs read service procedure
1615  */
1616 static void
1617 zsa_rsrv(queue_t *q)
1618 {
1619 	struct asyncline	*za;
1620 	struct zscom		*zs;
1621 
1622 	if (((za = (struct asyncline *)q->q_ptr) != NULL) &&
1623 	    (za->za_ttycommon.t_cflag & CRTSXOFF)) {
1624 		zs = za->za_common;
1625 		mutex_enter(zs->zs_excl_hi);
1626 		ZSSETSOFT(zs);
1627 		mutex_exit(zs->zs_excl_hi);
1628 	}
1629 }
1630 
1631 /*
1632  * Transmitter interrupt service routine.
1633  * If there's more data to transmit in the current pseudo-DMA block,
1634  * and the transmitter is ready, send the next character if output
1635  * is not stopped or draining.
1636  * Otherwise, queue up a soft interrupt.
1637  */
1638 static void
1639 zsa_txint(struct zscom *zs)
1640 {
1641 	register struct asyncline *za = (struct asyncline *)&zs->zs_priv_str;
1642 	register uchar_t *wr_cur;
1643 	register uchar_t s0;
1644 
1645 	s0 = SCC_READ0();
1646 
1647 	if ((wr_cur = zs->zs_wr_cur) != NULL) {
1648 		if (wr_cur < zs->zs_wr_lim) {
1649 			if ((za->za_ttycommon.t_cflag & CRTSCTS) &&
1650 			    !(s0 & ZSRR0_CTS)) {
1651 				SCC_WRITE0(ZSWR0_RESET_TXINT);
1652 				za->za_rcv_flags_mask |= DO_RETRANSMIT;
1653 				return;
1654 			}
1655 			SCC_WRITEDATA(*wr_cur++);
1656 #ifdef ZSA_DEBUG
1657 			za->za_wr++;
1658 #endif
1659 			zs->zs_wr_cur = wr_cur;
1660 			zs->zs_flags |= ZS_PROGRESS;
1661 			return;
1662 		} else {
1663 			zs->zs_wr_cur = NULL;
1664 			zs->zs_wr_lim = NULL;
1665 			/*
1666 			 * Use the rcv_flags_mask as it is set and
1667 			 * test while holding the zs_excl_hi mutex
1668 			 */
1669 			za->za_rcv_flags_mask |= DO_TRANSMIT;
1670 			SCC_WRITE0(ZSWR0_RESET_TXINT);
1671 			ZSSETSOFT(zs);
1672 			return;
1673 		}
1674 	}
1675 
1676 	if (za->za_flowc != '\0' && (!(za->za_flags & ZAS_DRAINING))) {
1677 		if ((za->za_ttycommon.t_cflag & CRTSCTS) &&
1678 		    !(s0 & ZSRR0_CTS)) {
1679 			SCC_WRITE0(ZSWR0_RESET_TXINT);
1680 			return;
1681 		}
1682 		SCC_WRITEDATA(za->za_flowc);
1683 		za->za_flowc = '\0';
1684 		return;
1685 	}
1686 	SCC_WRITE0(ZSWR0_RESET_TXINT);
1687 	/*
1688 	 * Set DO_TRANSMIT bit so that the soft interrupt can
1689 	 * test it and unset the ZAS_BUSY in za_flags while holding
1690 	 * the mutex zs_excl and zs_excl_hi
1691 	 */
1692 	za->za_rcv_flags_mask |= DO_TRANSMIT;
1693 	ZSSETSOFT(zs);
1694 }
1695 
1696 /*
1697  * External/Status interrupt.
1698  */
1699 static void
1700 zsa_xsint(struct zscom *zs)
1701 {
1702 	register struct asyncline *za = (struct asyncline *)&zs->zs_priv_str;
1703 	register uchar_t s0, x0;
1704 
1705 	s0 = SCC_READ0();
1706 	ZSA_R0_LOG(s0);
1707 	x0 = s0 ^ za->za_rr0;
1708 	za->za_rr0 = s0;
1709 	SCC_WRITE0(ZSWR0_RESET_STATUS);
1710 
1711 	/*
1712 	 * PPS (Pulse Per Second) support.
1713 	 */
1714 	if (za->za_pps && (x0 & ZSRR0_CD) && (s0 & ZSRR0_CD)) {
1715 		/*
1716 		 * This code captures a timestamp at the designated
1717 		 * transition of the PPS signal (CD asserted).  The
1718 		 * code provides a pointer to the timestamp, as well
1719 		 * as the hardware counter value at the capture.
1720 		 *
1721 		 * Note: the kernel has nano based time values while
1722 		 * NTP requires micro based, an in-line fast algorithm
1723 		 * to convert nsec to usec is used here -- see hrt2ts()
1724 		 * in common/os/timers.c for a full description.
1725 		 */
1726 		struct timeval *tvp = &ppsclockev.tv;
1727 		timespec_t ts;
1728 		int nsec, usec;
1729 
1730 		LED_OFF;
1731 		gethrestime(&ts);
1732 		LED_ON;
1733 		nsec = ts.tv_nsec;
1734 		usec = nsec + (nsec >> 2);
1735 		usec = nsec + (usec >> 1);
1736 		usec = nsec + (usec >> 2);
1737 		usec = nsec + (usec >> 4);
1738 		usec = nsec - (usec >> 3);
1739 		usec = nsec + (usec >> 2);
1740 		usec = nsec + (usec >> 3);
1741 		usec = nsec + (usec >> 4);
1742 		usec = nsec + (usec >> 1);
1743 		usec = nsec + (usec >> 6);
1744 		tvp->tv_usec = usec >> 10;
1745 		tvp->tv_sec = ts.tv_sec;
1746 
1747 		++ppsclockev.serial;
1748 
1749 		/*
1750 		 * Because the kernel keeps a high-resolution time, pass the
1751 		 * current highres timestamp in tvp and zero in usec.
1752 		 */
1753 		ddi_hardpps(tvp, 0);
1754 	}
1755 
1756 	ZSA_KICK_RCV;
1757 
1758 	if ((x0 & ZSRR0_BREAK) && (s0 & ZSRR0_BREAK) == 0) {
1759 #ifdef SLAVIO_BUG
1760 		/*
1761 		 * ZSRR0_BREAK turned off.  This means that the break sequence
1762 		 * has completed (i.e., the stop bit finally arrived).
1763 		 */
1764 		if ((s0 & ZSRR0_RX_READY) == 0) {
1765 			/*
1766 			 * SLAVIO will generate a separate STATUS change
1767 			 * interrupt when the break sequence completes.
1768 			 * SCC will combine both, taking the higher priority
1769 			 * one, the receive.  Should still see the ext/stat.
1770 			 * bit in REG3 on SCC.  If no ext/stat, it must be
1771 			 * a SLAVIO.
1772 			 */
1773 			za->za_breakoff = 1;
1774 		} else {
1775 			/*
1776 			 * The NUL character in the receiver is part of the
1777 			 * break sequence; it is discarded.
1778 			 */
1779 			(void) SCC_READDATA(); /* swallow null */
1780 		}
1781 #else /* SLAVIO_BUG */
1782 		/*
1783 		 * ZSRR0_BREAK turned off.  This means that the break sequence
1784 		 * has completed (i.e., the stop bit finally arrived).  The NUL
1785 		 * character in the receiver is part of the break sequence;
1786 		 * it is discarded.
1787 		 */
1788 		(void) SCC_READDATA(); /* swallow null */
1789 #endif /* SLAVIO_BUG */
1790 		SCC_WRITE0(ZSWR0_RESET_ERRORS);
1791 
1792 		/*
1793 		 * Note: this will cause an abort if a break occurs on
1794 		 * the "keyboard device", regardless of whether the
1795 		 * "keyboard device" is a real keyboard or just a
1796 		 * terminal on a serial line. This permits you to
1797 		 * abort a workstation by unplugging the keyboard,
1798 		 * even if the normal abort key sequence isn't working.
1799 		 */
1800 		if ((za->za_dev == kbddev) ||
1801 		    ((za->za_dev == rconsdev) || (za->za_dev == stdindev)) &&
1802 		    (abort_enable != KIOCABORTALTERNATE)) {
1803 			abort_sequence_enter((char *)NULL);
1804 			/*
1805 			 * We just broke into the monitor or debugger,
1806 			 * ignore the break in this case so whatever
1807 			 * random program that was running doesn't get
1808 			 * a SIGINT.
1809 			 */
1810 			return;
1811 		}
1812 		za->za_break = 1;
1813 	}
1814 
1815 	/*
1816 	 * If hardware flow control is enabled, (re)start output
1817 	 * when CTS is reasserted.
1818 	 */
1819 	if ((za->za_ttycommon.t_cflag & CRTSCTS) &&
1820 	    (x0 & ZSRR0_CTS) && (s0 & ZSRR0_CTS) &&
1821 	    (za->za_rcv_flags_mask & DO_RETRANSMIT))
1822 			za->za_rcv_flags_mask |= DO_TRANSMIT;
1823 
1824 	za->za_ext = 1;
1825 	ZSSETSOFT(zs);
1826 }
1827 
1828 /*
1829  * Receive Interrupt
1830  */
1831 static void
1832 zsa_rxint(struct zscom *zs)
1833 {
1834 	register struct asyncline *za = (struct asyncline *)&zs->zs_priv_str;
1835 	register uchar_t c;
1836 	register uchar_t *rd_cur = zs->zs_rd_cur;
1837 	register uchar_t *rd_lim = zs->zs_rd_lim;
1838 	register mblk_t	*bp;
1839 	register uint_t	fm = za->za_rcv_flags_mask;
1840 
1841 
1842 #ifdef ZSA_DEBUG
1843 	za->za_rd++;
1844 #endif
1845 	c = (fm >> 16) & (SCC_READDATA());
1846 
1847 	/*
1848 	 * Check for character break sequence
1849 	 */
1850 	if ((abort_enable == KIOCABORTALTERNATE) && (za->za_dev == rconsdev)) {
1851 		if (abort_charseq_recognize(c))
1852 			abort_sequence_enter((char *)NULL);
1853 	}
1854 
1855 	if (!rd_cur) {
1856 #ifdef SLAVIO_BUG
1857 		/*
1858 		 * SLAVIO generates FE for the start of break and
1859 		 * during break when parity is set.  End of break is
1860 		 * detected when the first character is received.
1861 		 * This character is always garbage and is thrown away.
1862 		 */
1863 		if (za->za_slav_break) {
1864 			za->za_slav_break = 0;
1865 			za->za_rr0 |= ZSRR0_BREAK;
1866 			zsa_xsint(zs);
1867 			return;
1868 		}
1869 #endif /* SLAVIO_BUG */
1870 
1871 		if (c == 0 && (za->za_rr0 & ZSRR0_BREAK)) {
1872 			/*
1873 			 * A break sequence was under way, and a NUL character
1874 			 * was received. Discard the NUL character, as it is
1875 			 * part of the break sequence; if ZSRR0_BREAK turned
1876 			 * off, indicating that the break sequence has com-
1877 			 * pleted, call "zsa_xsint" to properly handle the
1878 			 * error. It would appear that External/Status
1879 			 * interrupts get lost occasionally, so this ensures
1880 			 * that one is delivered.
1881 			 */
1882 			c = SCC_READ0();
1883 			if (!(c & ZSRR0_BREAK))
1884 				zsa_xsint(zs);
1885 			return;
1886 		}
1887 
1888 #ifdef SLAVIO_BUG
1889 		if (c == 0 && za->za_breakoff) {
1890 			/*
1891 			 * A break sequence completed, but SLAVIO generates
1892 			 * the NULL character interrupt late, so we throw the
1893 			 * NULL away now.
1894 			 */
1895 			return;
1896 		}
1897 
1898 		/*
1899 		 * make sure it gets cleared.
1900 		 */
1901 		za->za_breakoff = 0;
1902 #endif /* SLAVIO_BUG */
1903 
1904 		ZSA_KICK_RCV;	/* We can have M_BREAK msg */
1905 		ZSA_ALLOCB(bp);
1906 		if (!bp) {
1907 			za->za_sw_overrun++;
1908 			ZSSETSOFT(zs);
1909 			return;
1910 		}
1911 		za->za_rcvblk = bp;
1912 		zs->zs_rd_cur = rd_cur = bp->b_wptr;
1913 		zs->zs_rd_lim = rd_lim = bp->b_datap->db_lim;
1914 		if (za->za_kick_rcv_id == 0)
1915 			ZSSETSOFT(zs);
1916 	}
1917 	if (c == 0377 && (fm & DO_ESC)) {
1918 		if (rd_lim < rd_cur + 2) {
1919 			ZSA_ALLOCB(bp);
1920 			ZSA_KICK_RCV;
1921 			if (!bp) {
1922 				za->za_sw_overrun++;
1923 				return;
1924 			}
1925 			za->za_rcvblk = bp;
1926 			zs->zs_rd_cur = rd_cur = bp->b_wptr;
1927 			zs->zs_rd_lim = rd_lim = bp->b_datap->db_lim;
1928 		}
1929 		*rd_cur++ = c;
1930 	}
1931 
1932 
1933 	*rd_cur++ = c;
1934 	zs->zs_rd_cur = rd_cur;
1935 
1936 	if (rd_cur == rd_lim) {
1937 		ZSA_KICK_RCV;
1938 	} else if ((fm & DO_STOPC) && (c == (fm & 0xff))) {
1939 		za->za_do_kick_rcv_in_softint = 1;
1940 		ZSSETSOFT(zs);
1941 	}
1942 
1943 	if ((za->za_flags & ZAS_SERVICEIMM) || g_nocluster) {
1944 		/*
1945 		 * Send the data up immediately
1946 		 */
1947 		ZSA_KICK_RCV;
1948 	}
1949 }
1950 
1951 /*
1952  * Special receive condition interrupt handler.
1953  */
1954 static void
1955 zsa_srint(struct zscom *zs)
1956 {
1957 	register struct asyncline *za = (struct asyncline *)&zs->zs_priv_str;
1958 	register short s1;
1959 	register uchar_t c;
1960 	register uchar_t c1;
1961 	register mblk_t *bp = za->za_rcvblk;
1962 	register uchar_t *rd_cur = zs->zs_rd_cur;
1963 
1964 	SCC_READ(1, s1);
1965 	if (s1 & (ZSRR1_FE | ZSRR1_PE | ZSRR1_DO)) {
1966 		c = SCC_READDATA();	/* swallow bad character */
1967 	}
1968 #ifdef SLAVIO_BUG
1969 	/*
1970 	 * SLAVIO does not handle breaks properly when parity is enabled.
1971 	 *
1972 	 * In general, if a null character is received when a framing
1973 	 * error occurs then it is a break condition and not a real
1974 	 * framing error. The null character must be limited to the
1975 	 * number of bits including the parity bit. For example, a 6
1976 	 * bit character with parity would be null if the lower 7 bits
1977 	 * read from the receive fifo were 0. (The higher order bits are
1978 	 * padded with 1 and/or the stop bits.) The only exception to this
1979 	 * general rule would be an 8 bit null character with parity being
1980 	 * a 1 in the parity bit and a framing error. This exception
1981 	 * can be determined by examining the parity error bit in RREG 1.
1982 	 *
1983 	 * A null character, even parity, 8 bits, no parity error,
1984 	 * (0 0000 0000) with framing error is a break condition.
1985 	 *
1986 	 * A null character, even parity, 8 bits, parity error,
1987 	 * (1 0000 0000) with framing error is a framing error.
1988 	 *
1989 	 * A null character, odd parity, 8 bits, parity error
1990 	 * (0 0000 0000) with framing error is a break condition.
1991 	 *
1992 	 * A null character, odd parity, 8 bits, no parity error,
1993 	 * (1 0000 0000) with framing error is a framing error.
1994 	 */
1995 	if (za->za_ttycommon.t_cflag & PARENB) {
1996 		switch (za->za_ttycommon.t_cflag & CSIZE) {
1997 
1998 		case CS5:
1999 			c1 = c & 0x3f;
2000 			break;
2001 
2002 		case CS6:
2003 			c1 = c & 0x7f;
2004 			break;
2005 
2006 		case CS7:
2007 			c1 = c & 0xff;
2008 			break;
2009 
2010 		case CS8:
2011 			if ((za->za_ttycommon.t_cflag & PARODD) &&
2012 			    !(s1 & ZSRR1_PE))
2013 				c1 = 0xff;
2014 			else if (!(za->za_ttycommon.t_cflag & PARODD) &&
2015 			    (s1 & ZSRR1_PE))
2016 				c1 = 0xff;
2017 			else
2018 				c1 = c;
2019 			break;
2020 		}
2021 
2022 		/*
2023 		 * We fake start of break condition.
2024 		 */
2025 		if ((s1 & ZSRR1_FE) && c1 == 0) {
2026 			za->za_slav_break = 1;
2027 			return;
2028 		}
2029 	}
2030 #endif /* SLAVIO_BUG */
2031 
2032 	if (s1 & ZSRR1_PE) {
2033 
2034 		/*
2035 		 * Mark the parity error so zsa_process will
2036 		 * notice it and send it up in an M_BREAK
2037 		 * message; ldterm will do the actual parity error
2038 		 * processing
2039 		 */
2040 
2041 		if (bp && zs->zs_rd_cur) {	/* M_DATA msg */
2042 			ZSA_KICK_RCV;
2043 			bp = NULL;
2044 		}
2045 		if (!bp)
2046 			ZSA_ALLOCB(bp);
2047 		if (!bp) {
2048 			za->za_sw_overrun++;
2049 			ZSSETSOFT(zs);
2050 		} else {
2051 			za->za_rcvblk = bp;
2052 			zs->zs_rd_cur = rd_cur = bp->b_wptr;
2053 			zs->zs_rd_lim = bp->b_datap->db_lim;
2054 			*rd_cur++ = c;
2055 			zs->zs_rd_cur = rd_cur;
2056 			bp->b_datap->db_type = M_BREAK;
2057 			if (bp->b_datap->db_lim <= rd_cur)
2058 				ZSA_KICK_RCV;
2059 			za->za_do_kick_rcv_in_softint = 1;
2060 			ZSSETSOFT(zs);
2061 
2062 		}
2063 	}
2064 	SCC_WRITE0(ZSWR0_RESET_ERRORS);
2065 	if (s1 & ZSRR1_DO) {
2066 		za->za_hw_overrun++;
2067 		ZSSETSOFT(zs);
2068 	}
2069 }
2070 
2071 /*
2072  * Process software interrupts (or poll)
2073  * Crucial points:
2074  * 3.	BUG - breaks are handled "out-of-band" - their relative position
2075  *	among input events is lost, as well as multiple breaks together.
2076  *	This is probably not a problem in practice.
2077  */
2078 static int
2079 zsa_softint(struct zscom *zs)
2080 {
2081 	register struct asyncline *za = (struct asyncline *)&zs->zs_priv_str;
2082 	register uchar_t r0;
2083 	register uchar_t za_kick_active;
2084 	register int	m_error;
2085 	register int	allocbcount = 0;
2086 	register int 	do_ttycommon_qfull = 0;
2087 	boolean_t	hangup = B_FALSE, unhangup = B_FALSE;
2088 	boolean_t	m_break = B_FALSE, wakeup = B_FALSE;
2089 	register queue_t *q;
2090 	register mblk_t	*bp;
2091 	register mblk_t *head = NULL, *tail = NULL;
2092 
2093 	mutex_enter(zs->zs_excl);
2094 	if (zs->zs_suspended || (zs->zs_flags & ZS_CLOSED)) {
2095 		mutex_exit(zs->zs_excl);
2096 		return (0);
2097 	}
2098 	q = za->za_ttycommon.t_readq;
2099 	if (za->za_flags & ZAS_WOPEN && !q) {
2100 		if (za->za_ext) {
2101 			mutex_enter(zs->zs_excl_hi);
2102 			r0 = SCC_READ0();
2103 			za->za_ext = 0;
2104 			mutex_exit(zs->zs_excl_hi);
2105 			/*
2106 			 * carrier up?
2107 			 */
2108 			if ((r0 & ZSRR0_CD) ||
2109 			    (za->za_ttycommon.t_flags & TS_SOFTCAR)) {
2110 				/*
2111 				 * carrier present
2112 				 */
2113 				if ((za->za_flags & ZAS_CARR_ON) == 0) {
2114 					za->za_flags |= ZAS_CARR_ON;
2115 					mutex_exit(zs->zs_excl);
2116 					cv_broadcast(&zs->zs_flags_cv);
2117 					return (0);
2118 				}
2119 			}
2120 		}
2121 		mutex_exit(zs->zs_excl);
2122 		return (0);
2123 	}
2124 	q = za->za_ttycommon.t_readq;
2125 	if (!q) {
2126 		mutex_exit(zs->zs_excl);
2127 		return (0);
2128 	}
2129 
2130 	m_error = za->za_m_error;
2131 	za->za_m_error = 0;
2132 
2133 	if (za->za_do_kick_rcv_in_softint) {
2134 		mutex_enter(zs->zs_excl_hi);
2135 		ZSA_KICK_RCV;
2136 		za->za_do_kick_rcv_in_softint = 0;
2137 		mutex_exit(zs->zs_excl_hi);
2138 	}
2139 
2140 	za_kick_active = za->za_kick_active;
2141 
2142 	while (!za_kick_active) {
2143 		ZSA_SEEQ(bp);
2144 		if (!bp)
2145 			break;
2146 
2147 		allocbcount++;
2148 
2149 		if (bp->b_datap->db_type <= QPCTL) {
2150 			if (!(canputnext(q))) {
2151 				if (za->za_grace_flow_control >=
2152 				    zsa_grace_flow_control) {
2153 					if (za->za_ttycommon.t_cflag &
2154 					    CRTSXOFF) {
2155 						allocbcount--;
2156 						break;
2157 					}
2158 					ZSA_GETQ(bp);
2159 					freemsg(bp);
2160 					do_ttycommon_qfull = 1;
2161 					continue;
2162 				} else
2163 					za->za_grace_flow_control++;
2164 			} else
2165 				za->za_grace_flow_control = 0;
2166 		}
2167 		ZSA_GETQ(bp);
2168 		if (!head) {
2169 			head = bp;
2170 		} else {
2171 			if (!tail)
2172 				tail = head;
2173 			tail->b_next = bp;
2174 			tail = bp;
2175 		}
2176 	}
2177 
2178 	if (allocbcount)
2179 		ZSA_GETBLOCK(zs, allocbcount);
2180 
2181 	if (za->za_ext) {
2182 		mutex_enter(zs->zs_excl_hi);
2183 		r0 = SCC_READ0();
2184 		za->za_ext = 0;
2185 		/*
2186 		 * carrier up?
2187 		 */
2188 		if ((r0 & ZSRR0_CD) ||
2189 		    (za->za_ttycommon.t_flags & TS_SOFTCAR)) {
2190 			/*
2191 			 * carrier present
2192 			 */
2193 			if ((za->za_flags & ZAS_CARR_ON) == 0) {
2194 				za->za_flags |= ZAS_CARR_ON;
2195 				unhangup = B_TRUE;
2196 				wakeup = B_TRUE;
2197 			}
2198 		} else {
2199 			if ((za->za_flags & ZAS_CARR_ON) &&
2200 			    !(za->za_ttycommon.t_cflag & CLOCAL)) {
2201 				/*
2202 				 * Carrier went away.
2203 				 * Drop DTR, abort any output in progress,
2204 				 * indicate that output is not stopped, and
2205 				 * send a hangup notification upstream.
2206 				 */
2207 				(void) zsmctl(zs, ZSWR5_DTR, DMBIC);
2208 				if ((za->za_flags & ZAS_BUSY) &&
2209 				    (zs->zs_wr_cur != NULL)) {
2210 					zs->zs_wr_cur = NULL;
2211 					zs->zs_wr_lim = NULL;
2212 				}
2213 				hangup = B_TRUE;
2214 				wakeup = B_TRUE;
2215 				za->za_flags &= ~(ZAS_STOPPED | ZAS_CARR_ON |
2216 				    ZAS_BUSY);
2217 				za->za_rcv_flags_mask &= ~(DO_TRANSMIT |
2218 				    DO_RETRANSMIT);
2219 			}
2220 		}
2221 		mutex_exit(zs->zs_excl_hi);
2222 		if (hangup && (bp = za->za_xmitblk) != NULL) {
2223 			za->za_xmitblk = NULL;
2224 			freeb(bp);
2225 		}
2226 	}
2227 
2228 	if (za->za_break != 0) {
2229 		mutex_enter(zs->zs_excl_hi);
2230 		r0 = SCC_READ0();
2231 		mutex_exit(zs->zs_excl_hi);
2232 		if ((r0 & ZSRR0_BREAK) == 0) {
2233 			za->za_break = 0;
2234 			m_break = B_TRUE;
2235 		}
2236 	}
2237 
2238 	/*
2239 	 * If a transmission has finished, indicate that it's
2240 	 * finished, and start that line up again.
2241 	 */
2242 
2243 	mutex_enter(zs->zs_excl_hi);
2244 	if (za->za_rcv_flags_mask & DO_TRANSMIT) {
2245 		za->za_rcv_flags_mask &= ~DO_TRANSMIT;
2246 		za->za_flags &= ~ZAS_BUSY;
2247 
2248 		if ((za->za_ttycommon.t_cflag & CRTSCTS) &&
2249 		    (za->za_rcv_flags_mask & DO_RETRANSMIT) &&
2250 		    zs->zs_wr_cur)
2251 			bp = NULL;
2252 		else {
2253 			za->za_rcv_flags_mask &= ~DO_RETRANSMIT;
2254 			bp = za->za_xmitblk;
2255 			za->za_xmitblk = 0;
2256 		}
2257 		mutex_exit(zs->zs_excl_hi);
2258 		if (bp)
2259 			freemsg(bp);
2260 		zsa_start(zs);
2261 		/* if we didn't start anything, then notify waiters */
2262 		if (!(za->za_flags & ZAS_BUSY))
2263 			wakeup = B_TRUE;
2264 	} else {
2265 		mutex_exit(zs->zs_excl_hi);
2266 	}
2267 
2268 
2269 	/*
2270 	 * A note about these overrun bits: all they do is *tell* someone
2271 	 * about an error- They do not track multiple errors. In fact,
2272 	 * you could consider them latched register bits if you like.
2273 	 * We are only interested in printing the error message once for
2274 	 * any cluster of overrun errrors.
2275 	 */
2276 	if ((!za->za_kick_rcv_id) && (zs->zs_rd_cur || za_kick_active)) {
2277 		if (g_zsticks)
2278 			za->za_kick_rcv_id = timeout(zsa_kick_rcv, zs,
2279 			    g_zsticks);
2280 		else
2281 			za->za_kick_rcv_id = timeout(zsa_kick_rcv, zs,
2282 			    zsticks[SPEED(za->za_ttycommon.t_cflag)]);
2283 		za->za_kick_rcv_count = ZA_KICK_RCV_COUNT;
2284 	}
2285 	za->za_soft_active = 1;
2286 	mutex_exit(zs->zs_excl);
2287 
2288 	if (!hangup && do_ttycommon_qfull) {
2289 		ttycommon_qfull(&za->za_ttycommon, q);
2290 		mutex_enter(zs->zs_excl);
2291 		zsa_start(zs);
2292 		mutex_exit(zs->zs_excl);
2293 	}
2294 
2295 	if (za->za_hw_overrun > 10) {
2296 		cmn_err(CE_NOTE, "zs%d: silo overflow\n", UNIT(za->za_dev));
2297 		za->za_hw_overrun = 0;
2298 	}
2299 
2300 	if (za->za_sw_overrun > 10) {
2301 		cmn_err(CE_NOTE, "zs%d:ring buffer overflow\n",
2302 		    UNIT(za->za_dev));
2303 		za->za_sw_overrun = 0;
2304 	}
2305 
2306 	if (unhangup)
2307 		(void) putnextctl(q, M_UNHANGUP);
2308 
2309 	if (m_break)
2310 		(void) putnextctl(q, M_BREAK);
2311 
2312 	while (head) {
2313 		if (!tail) {
2314 			putnext(q, head);
2315 			break;
2316 		}
2317 		bp = head;
2318 		head = head->b_next;
2319 		bp->b_next = NULL;
2320 		putnext(q, bp);
2321 	}
2322 
2323 	if (hangup) {
2324 		int flushflag;
2325 
2326 		/*
2327 		 * If we're in the midst of close, then flush everything.  Don't
2328 		 * leave stale ioctls lying about.
2329 		 */
2330 		flushflag = (zs->zs_flags & ZS_CLOSING) ? FLUSHALL : FLUSHDATA;
2331 		flushq(za->za_ttycommon.t_writeq, flushflag);
2332 		(void) putnextctl(q, M_HANGUP);
2333 	}
2334 
2335 	if (m_error)
2336 		(void) putnextctl1(q, M_ERROR, m_error);
2337 
2338 	za->za_soft_active = 0;
2339 
2340 	if (wakeup || (zs->zs_flags & ZS_CLOSED))
2341 		cv_broadcast(&zs->zs_flags_cv);
2342 
2343 	return (0);
2344 }
2345 
2346 /*
2347  * Start output on a line, unless it's busy, frozen, or otherwise.
2348  */
2349 static void
2350 zsa_start(struct zscom *zs)
2351 {
2352 	register struct asyncline *za = (struct asyncline *)&zs->zs_priv_str;
2353 	register int cc;
2354 	register queue_t *q;
2355 	register mblk_t *bp;
2356 	uchar_t *rptr, *wptr;
2357 
2358 	/*
2359 	 * If the chip is busy (i.e., we're waiting for a break timeout
2360 	 * to expire, or for the current transmission to finish, or for
2361 	 * output to finish draining from chip), don't grab anything new.
2362 	 */
2363 	if ((za->za_flags & (ZAS_BREAK|ZAS_BUSY|ZAS_DRAINING)) ||
2364 	    zs->zs_suspended)
2365 		return;
2366 
2367 	if (za->za_ttycommon.t_cflag & CRTSCTS) {
2368 		mutex_enter(zs->zs_excl_hi);
2369 		if (za->za_rcv_flags_mask & DO_RETRANSMIT) {
2370 			rptr = zs->zs_wr_cur;
2371 			wptr = zs->zs_wr_lim;
2372 			goto zsa_start_retransmit;
2373 
2374 		}
2375 		mutex_exit(zs->zs_excl_hi);
2376 	}
2377 
2378 	/*
2379 	 * If we have a flow-control character to transmit, do it now.
2380 	 */
2381 	if (za->za_flowc != '\0') {
2382 		mutex_enter(zs->zs_excl_hi);
2383 		if (za->za_ttycommon.t_cflag & CRTSCTS) {
2384 			if ((SCC_READ0() & (ZSRR0_CTS|ZSRR0_TX_READY)) !=
2385 			    (ZSRR0_CTS|ZSRR0_TX_READY)) {
2386 				mutex_exit(zs->zs_excl_hi);
2387 				return;
2388 			}
2389 		} else if (!(SCC_READ0() & ZSRR0_TX_READY)) {
2390 			mutex_exit(zs->zs_excl_hi);
2391 			return;
2392 		}
2393 
2394 		ZSDELAY();
2395 		SCC_WRITEDATA(za->za_flowc);
2396 		za->za_flowc = '\0';
2397 		mutex_exit(zs->zs_excl_hi);
2398 		return;
2399 	}
2400 
2401 	/*
2402 	 * If we're waiting for a delay timeout to expire, don't grab
2403 	 * anything new.
2404 	 */
2405 	if (za->za_flags & ZAS_DELAY)
2406 		return;
2407 
2408 	if ((q = za->za_ttycommon.t_writeq) == NULL)
2409 		return;	/* not attached to a stream */
2410 
2411 zsa_start_again:
2412 	for (;;) {
2413 		if ((bp = getq(q)) == NULL)
2414 			return;	/* no data to transmit */
2415 
2416 		/*
2417 		 * We have a message block to work on.
2418 		 * Check whether it's a break, a delay, or an ioctl (the latter
2419 		 * occurs if the ioctl in question was waiting for the output
2420 		 * to drain). If it's one of those, process it immediately.
2421 		 */
2422 		switch (bp->b_datap->db_type) {
2423 
2424 		case M_BREAK:
2425 			/*
2426 			 * Set the break bit, and arrange for "zsa_restart"
2427 			 * to be called in 1/4 second; it will turn the
2428 			 * break bit off, and call "zsa_start" to grab
2429 			 * the next message.
2430 			 */
2431 			mutex_enter(zs->zs_excl_hi);
2432 			SCC_BIS(5, ZSWR5_BREAK);
2433 			mutex_exit(zs->zs_excl_hi);
2434 			if (!za->za_zsa_restart_id) {
2435 				za->za_zsa_restart_id =
2436 				    timeout(zsa_restart, zs, hz/4);
2437 			}
2438 			za->za_flags |= ZAS_BREAK;
2439 			freemsg(bp);
2440 			return;	/* wait for this to finish */
2441 
2442 		case M_DELAY:
2443 			/*
2444 			 * Arrange for "zsa_restart" to be called when the
2445 			 * delay expires; it will turn MTS_DELAY off,
2446 			 * and call "zsa_start" to grab the next message.
2447 			 */
2448 			if (! za->za_zsa_restart_id) {
2449 				za->za_zsa_restart_id = timeout(zsa_restart,
2450 				    zs,
2451 				    (int)(*(unsigned char *)bp->b_rptr + 6));
2452 			}
2453 			za->za_flags |= ZAS_DELAY;
2454 			freemsg(bp);
2455 			return;	/* wait for this to finish */
2456 
2457 		case M_IOCTL:
2458 			/*
2459 			 * This ioctl was waiting for the output ahead of
2460 			 * it to drain; obviously, it has. Do it, and
2461 			 * then grab the next message after it.
2462 			 */
2463 			zsa_ioctl(za, q, bp);
2464 			continue;
2465 		default: /* M_DATA */
2466 			goto zsa_start_transmit;
2467 		}
2468 
2469 	}
2470 zsa_start_transmit:
2471 	/*
2472 	 * We have data to transmit. If output is stopped, put
2473 	 * it back and try again later.
2474 	 */
2475 	if (za->za_flags & ZAS_STOPPED) {
2476 		(void) putbq(q, bp);
2477 		return;
2478 	}
2479 
2480 	za->za_xmitblk = bp;
2481 	rptr = bp->b_rptr;
2482 	wptr = bp->b_wptr;
2483 	cc = wptr - rptr;
2484 	bp = bp->b_cont;
2485 	if (bp != NULL) {
2486 		za->za_xmitblk->b_cont = NULL;
2487 		(void) putbq(q, bp);	/* not done with this message yet */
2488 	}
2489 
2490 	if (rptr >= wptr) {
2491 		freeb(za->za_xmitblk);
2492 		za->za_xmitblk = NULL;
2493 		goto zsa_start_again;
2494 	}
2495 
2496 	/*
2497 	 * In 5-bit mode, the high order bits are used
2498 	 * to indicate character sizes less than five,
2499 	 * so we need to explicitly mask before transmitting
2500 	 */
2501 	if ((za->za_ttycommon.t_cflag & CSIZE) == CS5) {
2502 		register unsigned char *p = rptr;
2503 		register int cnt = cc;
2504 
2505 		while (cnt--)
2506 			*p++ &= (unsigned char) 0x1f;
2507 	}
2508 
2509 	/*
2510 	 * Set up this block for pseudo-DMA.
2511 	 */
2512 
2513 	mutex_enter(zs->zs_excl_hi);
2514 	zs->zs_wr_cur = rptr;
2515 	zs->zs_wr_lim = wptr;
2516 
2517 zsa_start_retransmit:
2518 	za->za_rcv_flags_mask &= ~DO_TRANSMIT;
2519 	if (za->za_ttycommon.t_cflag & CRTSCTS) {
2520 		if ((SCC_READ0() & (ZSRR0_CTS|ZSRR0_TX_READY)) !=
2521 		    (ZSRR0_CTS|ZSRR0_TX_READY)) {
2522 			za->za_rcv_flags_mask |= DO_RETRANSMIT;
2523 			za->za_flags |= ZAS_BUSY;
2524 			mutex_exit(zs->zs_excl_hi);
2525 			return;
2526 		}
2527 		za->za_rcv_flags_mask &= ~DO_RETRANSMIT;
2528 	} else {
2529 		if (!(SCC_READ0() & ZSRR0_TX_READY)) {
2530 			za->za_flags |= ZAS_BUSY;
2531 			mutex_exit(zs->zs_excl_hi);
2532 			return;
2533 		}
2534 	}
2535 	/*
2536 	 * If the transmitter is ready, shove the first
2537 	 * character out.
2538 	 */
2539 	ZSDELAY();
2540 	SCC_WRITEDATA(*rptr++);
2541 #ifdef ZSA_DEBUG
2542 	za->za_wr++;
2543 #endif
2544 	zs->zs_wr_cur = rptr;
2545 	za->za_flags |= ZAS_BUSY;
2546 	zs->zs_flags |= ZS_PROGRESS;
2547 	mutex_exit(zs->zs_excl_hi);
2548 }
2549 
2550 /*
2551  * Restart output on a line after a delay or break timer expired.
2552  */
2553 static void
2554 zsa_restart(void *arg)
2555 {
2556 	struct zscom *zs = arg;
2557 	struct asyncline *za = (struct asyncline *)&zs->zs_priv_str;
2558 
2559 	/*
2560 	 * If break timer expired, turn off the break bit.
2561 	 */
2562 	mutex_enter(zs->zs_excl);
2563 	if (!za->za_zsa_restart_id) {
2564 		mutex_exit(zs->zs_excl);
2565 		return;
2566 	}
2567 	za->za_zsa_restart_id = 0;
2568 	if (za->za_flags & ZAS_BREAK) {
2569 		mutex_enter(zs->zs_excl_hi);
2570 		SCC_BIC(5, ZSWR5_BREAK);
2571 		mutex_exit(zs->zs_excl_hi);
2572 	}
2573 	za->za_flags &= ~(ZAS_DELAY|ZAS_BREAK);
2574 	if (za->za_ttycommon.t_writeq != NULL)
2575 		zsa_start(zs);
2576 	mutex_exit(zs->zs_excl);
2577 	cv_broadcast(&zs->zs_flags_cv);
2578 }
2579 
2580 /*
2581  * See if the receiver has any data after zs_tick delay
2582  */
2583 static void
2584 zsa_kick_rcv(void *arg)
2585 {
2586 	struct zscom *zs = arg;
2587 	struct asyncline *za = (struct asyncline *)&zs->zs_priv_str;
2588 	queue_t *q;
2589 	int	tmp;
2590 	mblk_t	*mp;
2591 	uchar_t za_soft_active, za_kick_active;
2592 	int	allocbcount = 0;
2593 	int do_ttycommon_qfull = 0;
2594 	mblk_t *head = NULL, *tail = NULL;
2595 
2596 	mutex_enter(zs->zs_excl);
2597 	if (za->za_kick_rcv_id == 0 || (zs->zs_flags & ZS_CLOSED)) {
2598 		mutex_exit(zs->zs_excl);
2599 		return;
2600 	}
2601 	za_soft_active = za->za_soft_active;
2602 	za_kick_active = za->za_kick_active;
2603 	q = za->za_ttycommon.t_readq;
2604 	if (!q) {
2605 		mutex_exit(zs->zs_excl);
2606 		return;
2607 	}
2608 	mutex_enter(zs->zs_excl_hi);
2609 	if (zs->zs_rd_cur) {
2610 		ZSA_KICK_RCV;
2611 		za->za_kick_rcv_count = tmp = ZA_KICK_RCV_COUNT;
2612 	} else
2613 		tmp = --za->za_kick_rcv_count;
2614 	if (tmp > 0 || za_soft_active || za_kick_active) {
2615 		mutex_exit(zs->zs_excl_hi);
2616 		if (g_zsticks)
2617 			za->za_kick_rcv_id = timeout(zsa_kick_rcv,
2618 			    zs, g_zsticks);
2619 		else
2620 			za->za_kick_rcv_id = timeout(zsa_kick_rcv,
2621 			    zs, zsticks[SPEED(za->za_ttycommon.t_cflag)]);
2622 		if (za_soft_active || za_kick_active) {
2623 			mutex_exit(zs->zs_excl);
2624 			return;
2625 		}
2626 	} else {
2627 		za->za_kick_rcv_id = 0;
2628 		mutex_exit(zs->zs_excl_hi);
2629 	}
2630 
2631 
2632 	for (;;) {
2633 		ZSA_SEEQ(mp);
2634 		if (!mp)
2635 			break;
2636 
2637 		allocbcount++;
2638 
2639 		if (mp->b_datap->db_type <= QPCTL) {
2640 			if (!(canputnext(q))) {
2641 				if (za->za_grace_flow_control >=
2642 				    zsa_grace_flow_control) {
2643 					if (za->za_ttycommon.t_cflag &
2644 					    CRTSXOFF) {
2645 						allocbcount--;
2646 						break;
2647 					}
2648 					ZSA_GETQ(mp);
2649 					freemsg(mp);
2650 					do_ttycommon_qfull = 1;
2651 					continue;
2652 				} else
2653 					za->za_grace_flow_control++;
2654 			} else
2655 				za->za_grace_flow_control = 0;
2656 		}
2657 		ZSA_GETQ(mp);
2658 		if (!head) {
2659 			head = mp;
2660 		} else {
2661 			if (!tail)
2662 				tail = head;
2663 			tail->b_next = mp;
2664 			tail = mp;
2665 		}
2666 	}
2667 
2668 	if (allocbcount)
2669 		ZSA_GETBLOCK(zs, allocbcount);
2670 
2671 	za->za_kick_active = 1;
2672 	mutex_exit(zs->zs_excl);
2673 
2674 	if (do_ttycommon_qfull) {
2675 		ttycommon_qfull(&za->za_ttycommon, q);
2676 		mutex_enter(zs->zs_excl);
2677 		zsa_start(zs);
2678 		mutex_exit(zs->zs_excl);
2679 	}
2680 
2681 	while (head) {
2682 		if (!tail) {
2683 			putnext(q, head);
2684 			break;
2685 		}
2686 		mp = head;
2687 		head = head->b_next;
2688 		mp->b_next = NULL;
2689 		putnext(q, mp);
2690 
2691 	}
2692 	za->za_kick_active = 0;
2693 
2694 	if (zs->zs_flags & ZS_CLOSED)
2695 		cv_broadcast(&zs->zs_flags_cv);
2696 }
2697 
2698 /*
2699  * Retry an "ioctl", now that "bufcall" claims we may be able to allocate
2700  * the buffer we need.
2701  */
2702 static void
2703 zsa_reioctl(void *arg)
2704 {
2705 	struct asyncline *za = arg;
2706 	struct zscom *zs = za->za_common;
2707 	queue_t *q;
2708 	mblk_t	 *mp;
2709 
2710 	/*
2711 	 * The bufcall is no longer pending.
2712 	 */
2713 	mutex_enter(zs->zs_excl);
2714 	if (!za->za_wbufcid) {
2715 		mutex_exit(zs->zs_excl);
2716 		return;
2717 	}
2718 	za->za_wbufcid = 0;
2719 	if ((q = za->za_ttycommon.t_writeq) == NULL) {
2720 		mutex_exit(zs->zs_excl);
2721 		return;
2722 	}
2723 	if ((mp = za->za_ttycommon.t_iocpending) != NULL) {
2724 		/*
2725 		 * not pending any more
2726 		 */
2727 		za->za_ttycommon.t_iocpending = NULL;
2728 		zsa_ioctl(za, q, mp);
2729 	}
2730 	mutex_exit(zs->zs_excl);
2731 }
2732 
2733 /*
2734  * Process an "ioctl" message sent down to us.
2735  * Note that we don't need to get any locks until we are ready to access
2736  * the hardware. Nothing we access until then is going to be altered
2737  * outside of the STREAMS framework, so we should be safe.
2738  */
2739 static void
2740 zsa_ioctl(struct asyncline *za, queue_t *wq, mblk_t *mp)
2741 {
2742 	register struct zscom *zs = za->za_common;
2743 	register struct iocblk *iocp;
2744 	register unsigned datasize;
2745 	int error;
2746 	register mblk_t *tmp;
2747 
2748 	if (za->za_ttycommon.t_iocpending != NULL) {
2749 		/*
2750 		 * We were holding an "ioctl" response pending the
2751 		 * availability of an "mblk" to hold data to be passed up;
2752 		 * another "ioctl" came through, which means that "ioctl"
2753 		 * must have timed out or been aborted.
2754 		 */
2755 		freemsg(za->za_ttycommon.t_iocpending);
2756 		za->za_ttycommon.t_iocpending = NULL;
2757 	}
2758 
2759 	iocp = (struct iocblk *)mp->b_rptr;
2760 
2761 	/*
2762 	 * The only way in which "ttycommon_ioctl" can fail is if the "ioctl"
2763 	 * requires a response containing data to be returned to the user,
2764 	 * and no mblk could be allocated for the data.
2765 	 * No such "ioctl" alters our state. Thus, we always go ahead and
2766 	 * do any state-changes the "ioctl" calls for. If we couldn't allocate
2767 	 * the data, "ttycommon_ioctl" has stashed the "ioctl" away safely, so
2768 	 * we just call "bufcall" to request that we be called back when we
2769 	 * stand a better chance of allocating the data.
2770 	 */
2771 	mutex_exit(zs->zs_excl);
2772 	datasize = ttycommon_ioctl(&za->za_ttycommon, wq, mp, &error);
2773 	mutex_enter(zs->zs_excl);
2774 	if (za->za_ttycommon.t_flags & TS_SOFTCAR)
2775 		zssoftCAR[zs->zs_unit] = 1;
2776 	else
2777 		zssoftCAR[zs->zs_unit] = 0;
2778 	if (datasize != 0) {
2779 		if (za->za_wbufcid)
2780 			unbufcall(za->za_wbufcid);
2781 		za->za_wbufcid = bufcall(datasize, BPRI_HI, zsa_reioctl, za);
2782 		return;
2783 	}
2784 
2785 
2786 	if (error == 0) {
2787 		/*
2788 		 * "ttycommon_ioctl" did most of the work; we just use the
2789 		 * data it set up.
2790 		 */
2791 		switch (iocp->ioc_cmd) {
2792 
2793 		case TCSETS:
2794 		case TCSETSW:
2795 		case TCSETSF:
2796 		case TCSETA:
2797 		case TCSETAW:
2798 		case TCSETAF:
2799 			mutex_enter(zs->zs_excl_hi);
2800 			zsa_program(za, 1);
2801 			zsa_set_za_rcv_flags_mask(za);
2802 			mutex_exit(zs->zs_excl_hi);
2803 			break;
2804 		}
2805 	} else if (error < 0) {
2806 		/*
2807 		 * "ttycommon_ioctl" didn't do anything; we process it here.
2808 		 */
2809 		error = 0;
2810 
2811 		switch (iocp->ioc_cmd) {
2812 
2813 		case TCSBRK:
2814 			error = miocpullup(mp, sizeof (int));
2815 			if (error != 0)
2816 				break;
2817 
2818 			if (*(int *)mp->b_cont->b_rptr == 0) {
2819 				/*
2820 				 * The delay ensures that a 3 byte transmit
2821 				 * fifo is empty.
2822 				 */
2823 				mutex_exit(zs->zs_excl);
2824 				delay(ztdelay(SPEED(za->za_ttycommon.t_cflag)));
2825 				mutex_enter(zs->zs_excl);
2826 
2827 				/*
2828 				 * Set the break bit, and arrange for
2829 				 * "zsa_restart" to be called in 1/4 second;
2830 				 * it will turn the break bit off, and call
2831 				 * "zsa_start" to grab the next message.
2832 				 */
2833 				mutex_enter(zs->zs_excl_hi);
2834 				SCC_BIS(5, ZSWR5_BREAK);
2835 				if (!za->za_zsa_restart_id) {
2836 					mutex_exit(zs->zs_excl_hi);
2837 					za->za_zsa_restart_id =
2838 					    timeout(zsa_restart, zs, hz / 4);
2839 					mutex_enter(zs->zs_excl_hi);
2840 				}
2841 				za->za_flags |= ZAS_BREAK;
2842 				mutex_exit(zs->zs_excl_hi);
2843 			}
2844 			break;
2845 
2846 		case TIOCSBRK:
2847 			mutex_enter(zs->zs_excl_hi);
2848 			SCC_BIS(5, ZSWR5_BREAK);
2849 			mutex_exit(zs->zs_excl_hi);
2850 			mioc2ack(mp, NULL, 0, 0);
2851 			break;
2852 
2853 		case TIOCCBRK:
2854 			mutex_enter(zs->zs_excl_hi);
2855 			SCC_BIC(5, ZSWR5_BREAK);
2856 			mutex_exit(zs->zs_excl_hi);
2857 			mioc2ack(mp, NULL, 0, 0);
2858 			break;
2859 
2860 		case TIOCMSET:
2861 		case TIOCMBIS:
2862 		case TIOCMBIC: {
2863 			int mlines;
2864 
2865 			if (iocp->ioc_count == TRANSPARENT) {
2866 				mcopyin(mp, NULL, sizeof (int), NULL);
2867 				break;
2868 			}
2869 
2870 			error = miocpullup(mp, sizeof (int));
2871 			if (error != 0)
2872 				break;
2873 
2874 			mlines = *(int *)mp->b_cont->b_rptr;
2875 
2876 			mutex_enter(zs->zs_excl_hi);
2877 			switch (iocp->ioc_cmd) {
2878 			case TIOCMSET:
2879 				(void) zsmctl(zs, dmtozs(mlines), DMSET);
2880 				break;
2881 			case TIOCMBIS:
2882 				(void) zsmctl(zs, dmtozs(mlines), DMBIS);
2883 				break;
2884 			case TIOCMBIC:
2885 				(void) zsmctl(zs, dmtozs(mlines), DMBIC);
2886 				break;
2887 			}
2888 			mutex_exit(zs->zs_excl_hi);
2889 
2890 			mioc2ack(mp, NULL, 0, 0);
2891 			break;
2892 		}
2893 
2894 		case TIOCMGET:
2895 			tmp = allocb(sizeof (int), BPRI_MED);
2896 			if (tmp == NULL) {
2897 				error = EAGAIN;
2898 				break;
2899 			}
2900 			if (iocp->ioc_count != TRANSPARENT)
2901 				mioc2ack(mp, tmp, sizeof (int), 0);
2902 			else
2903 				mcopyout(mp, NULL, sizeof (int), NULL, tmp);
2904 
2905 			mutex_enter(zs->zs_excl_hi);
2906 			*(int *)mp->b_cont->b_rptr =
2907 			    zstodm(zsmctl(zs, 0, DMGET));
2908 			mutex_exit(zs->zs_excl_hi);
2909 			/*
2910 			 * qreply done below
2911 			 */
2912 			break;
2913 
2914 		default:
2915 			/*
2916 			 * If we don't understand it, it's an error. NAK it.
2917 			 */
2918 			error = EINVAL;
2919 			break;
2920 		}
2921 	}
2922 
2923 	if (error != 0) {
2924 		iocp->ioc_error = error;
2925 		mp->b_datap->db_type = M_IOCNAK;
2926 	}
2927 
2928 	ZSA_QREPLY(wq, mp);
2929 }
2930 
2931 
2932 static int
2933 dmtozs(int bits)
2934 {
2935 	register int b = 0;
2936 
2937 	if (bits & TIOCM_CAR)
2938 		b |= ZSRR0_CD;
2939 	if (bits & TIOCM_CTS)
2940 		b |= ZSRR0_CTS;
2941 	if (bits & TIOCM_RTS)
2942 		b |= ZSWR5_RTS;
2943 	if (bits & TIOCM_DTR)
2944 		b |= ZSWR5_DTR;
2945 	return (b);
2946 }
2947 
2948 static int
2949 zstodm(int bits)
2950 {
2951 	register int b;
2952 
2953 	b = 0;
2954 	if (bits & ZSRR0_CD)
2955 		b |= TIOCM_CAR;
2956 	if (bits & ZSRR0_CTS)
2957 		b |= TIOCM_CTS;
2958 	if (bits & ZSWR5_RTS)
2959 		b |= TIOCM_RTS;
2960 	if (bits & ZSWR5_DTR)
2961 		b |= TIOCM_DTR;
2962 	return (b);
2963 }
2964 
2965 /*
2966  * Assemble registers and flags necessary to program the port to our liking.
2967  * For async operation, most of this is based on the values of
2968  * the "c_iflag" and "c_cflag" fields supplied to us.
2969  */
2970 static void
2971 zsa_program(struct asyncline *za, int setibaud)
2972 {
2973 	register struct zscom *zs = za->za_common;
2974 	register struct zs_prog *zspp;
2975 	register int wr3, wr4, wr5, wr15, speed, baudrate, flags = 0;
2976 
2977 	if ((baudrate = SPEED(za->za_ttycommon.t_cflag)) == 0) {
2978 		/*
2979 		 * Hang up line.
2980 		 */
2981 		(void) zsmctl(zs, ZS_OFF, DMSET);
2982 		return;
2983 	}
2984 
2985 	/*
2986 	 * set input speed same as output, as split speed not supported
2987 	 */
2988 	if (setibaud) {
2989 		za->za_ttycommon.t_cflag &= ~(CIBAUD);
2990 		if (baudrate > CBAUD) {
2991 			za->za_ttycommon.t_cflag |= CIBAUDEXT;
2992 			za->za_ttycommon.t_cflag |=
2993 			    (((baudrate - CBAUD - 1) << IBSHIFT) & CIBAUD);
2994 		} else {
2995 			za->za_ttycommon.t_cflag &= ~CIBAUDEXT;
2996 			za->za_ttycommon.t_cflag |=
2997 			    ((baudrate << IBSHIFT) & CIBAUD);
2998 		}
2999 	}
3000 
3001 	/*
3002 	 * Do not allow the console/keyboard device to have its receiver
3003 	 * disabled; doing that would mean you couldn't type an abort
3004 	 * sequence.
3005 	 */
3006 	if ((za->za_dev == rconsdev) || (za->za_dev == kbddev) ||
3007 	    (za->za_dev == stdindev) || (za->za_ttycommon.t_cflag & CREAD))
3008 		wr3 = ZSWR3_RX_ENABLE;
3009 	else
3010 		wr3 = 0;
3011 	wr4 = ZSWR4_X16_CLK;
3012 	wr5 = (zs->zs_wreg[5] & (ZSWR5_RTS|ZSWR5_DTR)) | ZSWR5_TX_ENABLE;
3013 
3014 	if (zsb134_weird && baudrate == B134) {	/* what a joke! */
3015 		/*
3016 		 * XXX - should B134 set all this crap in the compatibility
3017 		 * module, leaving this stuff fairly clean?
3018 		 */
3019 		flags |= ZSP_PARITY_SPECIAL;
3020 		wr3 |= ZSWR3_RX_6;
3021 		wr4 |= ZSWR4_PARITY_ENABLE | ZSWR4_PARITY_EVEN;
3022 		wr4 |= ZSWR4_1_5_STOP;
3023 		wr5 |= ZSWR5_TX_6;
3024 	} else {
3025 
3026 		switch (za->za_ttycommon.t_cflag & CSIZE) {
3027 
3028 		case CS5:
3029 			wr3 |= ZSWR3_RX_5;
3030 			wr5 |= ZSWR5_TX_5;
3031 			break;
3032 
3033 		case CS6:
3034 			wr3 |= ZSWR3_RX_6;
3035 			wr5 |= ZSWR5_TX_6;
3036 			break;
3037 
3038 		case CS7:
3039 			wr3 |= ZSWR3_RX_7;
3040 			wr5 |= ZSWR5_TX_7;
3041 			break;
3042 
3043 		case CS8:
3044 			wr3 |= ZSWR3_RX_8;
3045 			wr5 |= ZSWR5_TX_8;
3046 			break;
3047 		}
3048 
3049 		if (za->za_ttycommon.t_cflag & PARENB) {
3050 			/*
3051 			 * The PARITY_SPECIAL bit causes a special rx
3052 			 * interrupt on parity errors. Turn it on if
3053 			 * we're checking the parity of characters.
3054 			 */
3055 			if (za->za_ttycommon.t_iflag & INPCK)
3056 				flags |= ZSP_PARITY_SPECIAL;
3057 			wr4 |= ZSWR4_PARITY_ENABLE;
3058 			if (!(za->za_ttycommon.t_cflag & PARODD))
3059 				wr4 |= ZSWR4_PARITY_EVEN;
3060 		}
3061 		wr4 |= (za->za_ttycommon.t_cflag & CSTOPB) ?
3062 		    ZSWR4_2_STOP : ZSWR4_1_STOP;
3063 	}
3064 
3065 #if 0
3066 	/*
3067 	 * The AUTO_CD_CTS flag enables the hardware flow control feature of
3068 	 * the 8530, which allows the state of CTS and DCD to control the
3069 	 * enabling of the transmitter and receiver, respectively. The
3070 	 * receiver and transmitter still must have their enable bits set in
3071 	 * WR3 and WR5, respectively, for CTS and DCD to be monitored this way.
3072 	 * Hardware flow control can thus be implemented with no help from
3073 	 * software.
3074 	 */
3075 	if (za->za_ttycommon.t_cflag & CRTSCTS)
3076 		wr3 |= ZSWR3_AUTO_CD_CTS;
3077 #endif
3078 	if (za->za_ttycommon.t_cflag & CRTSCTS)
3079 		wr15 = ZSR15_BREAK | ZSR15_TX_UNDER | ZSR15_CD | ZSR15_CTS;
3080 	else
3081 		wr15 = ZSR15_BREAK | ZSR15_TX_UNDER | ZSR15_CD;
3082 
3083 	speed = zs->zs_wreg[12] + (zs->zs_wreg[13] << 8);
3084 
3085 	/*
3086 	 * Here we assemble a set of changes to be passed to zs_program.
3087 	 * Note: Write Register 15 must be set to enable BREAK and UNDERrun
3088 	 * interrupts.  It must also enable CD interrupts which, although
3089 	 * not processed by the hardware interrupt handler, will be processed
3090 	 * by zsa_process, indirectly resulting in a SIGHUP being delivered
3091 	 * to the controlling process if CD drops.  CTS interrupts must NOT
3092 	 * be enabled.  We don't use them at all, and they will hang IPC/IPX
3093 	 * systems at boot time if synchronous modems that supply transmit
3094 	 * clock are attached to any of their serial ports.
3095 	 */
3096 	if (((zs->zs_wreg[1] & ZSWR1_PARITY_SPECIAL) &&
3097 	    !(flags & ZSP_PARITY_SPECIAL)) ||
3098 	    (!(zs->zs_wreg[1] & ZSWR1_PARITY_SPECIAL) &&
3099 	    (flags & ZSP_PARITY_SPECIAL)) ||
3100 	    wr3 != zs->zs_wreg[3] || wr4 != zs->zs_wreg[4] ||
3101 	    wr5 != zs->zs_wreg[5] || wr15 != zs->zs_wreg[15] ||
3102 	    speed != zs_speeds[baudrate]) {
3103 
3104 		za->za_flags |= ZAS_DRAINING;
3105 		zspp = &zs_prog[zs->zs_unit];
3106 		zspp->zs = zs;
3107 		zspp->flags = (uchar_t)flags;
3108 		zspp->wr4 = (uchar_t)wr4;
3109 		zspp->wr11 = (uchar_t)(ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD);
3110 
3111 		speed = zs_speeds[baudrate];
3112 		zspp->wr12 = (uchar_t)(speed & 0xff);
3113 		zspp->wr13 = (uchar_t)((speed >> 8) & 0xff);
3114 		zspp->wr3 = (uchar_t)wr3;
3115 		zspp->wr5 = (uchar_t)wr5;
3116 		zspp->wr15 = (uchar_t)wr15;
3117 
3118 		zs_program(zspp);
3119 		za->za_flags &= ~ZAS_DRAINING;
3120 	}
3121 }
3122 
3123 /*
3124  * Get the current speed of the console and turn it into something
3125  * UNIX knows about - used to preserve console speed when UNIX comes up.
3126  */
3127 int
3128 zsgetspeed(dev_t dev)
3129 {
3130 	register struct zscom *zs;
3131 	register int uspeed, zspeed;
3132 	register uchar_t rr;
3133 
3134 	zs = &zscom[UNIT(dev)];
3135 	SCC_READ(12, zspeed);
3136 	SCC_READ(13, rr);
3137 	zspeed |= rr << 8;
3138 	for (uspeed = 0; uspeed < NSPEED; uspeed++)
3139 		if (zs_speeds[uspeed] == zspeed)
3140 			return (uspeed);
3141 	/*
3142 	 * 9600 baud if we can't figure it out
3143 	 */
3144 	return (ISPEED);
3145 }
3146 
3147 /*
3148  * callback routine when enough memory is available.
3149  */
3150 static void
3151 zsa_callback(void *arg)
3152 {
3153 	struct zscom *zs = arg;
3154 	struct asyncline *za = (struct asyncline *)&zs->zs_priv_str;
3155 	int allocbcount = zsa_rstandby;
3156 
3157 	mutex_enter(zs->zs_excl);
3158 	if (za->za_bufcid) {
3159 		za->za_bufcid = 0;
3160 		ZSA_GETBLOCK(zs, allocbcount);
3161 	}
3162 	mutex_exit(zs->zs_excl);
3163 }
3164 
3165 /*
3166  * Set the receiver flags
3167  */
3168 static void
3169 zsa_set_za_rcv_flags_mask(struct asyncline *za)
3170 {
3171 	register uint_t mask;
3172 
3173 	za->za_rcv_flags_mask &= ~0xFF;
3174 	switch (za->za_ttycommon.t_cflag & CSIZE) {
3175 	case CS5:
3176 		mask = 0x1f;
3177 		break;
3178 	case CS6:
3179 		mask = 0x3f;
3180 		break;
3181 	case CS7:
3182 		mask = 0x7f;
3183 		break;
3184 	default:
3185 		mask = 0xff;
3186 	}
3187 
3188 	za->za_rcv_flags_mask &= ~(0xFF << 16);
3189 	za->za_rcv_flags_mask |=  mask << 16;
3190 
3191 	if ((za->za_ttycommon.t_iflag & PARMRK) &&
3192 	    !(za->za_ttycommon.t_iflag & (IGNPAR|ISTRIP))) {
3193 		za->za_rcv_flags_mask |= DO_ESC;
3194 	} else
3195 		za->za_rcv_flags_mask &= ~DO_ESC;
3196 	if (za->za_ttycommon.t_iflag & IXON) {
3197 		za->za_rcv_flags_mask |= DO_STOPC;
3198 		za->za_rcv_flags_mask &= ~0xFF;
3199 		za->za_rcv_flags_mask |= za->za_ttycommon.t_stopc;
3200 	} else
3201 		za->za_rcv_flags_mask &= ~DO_STOPC;
3202 }
3203 
3204 static int
3205 zsa_suspend(struct zscom *zs)
3206 {
3207 	struct asyncline	*za;
3208 	queue_t			*q;
3209 	mblk_t			*bp = NULL;
3210 	timeout_id_t		restart_id, kick_rcv_id;
3211 	struct zs_prog		*zspp;
3212 
3213 	za = (struct asyncline *)&zs->zs_priv_str;
3214 	mutex_enter(zs->zs_excl);
3215 	if (zs->zs_suspended) {
3216 		mutex_exit(zs->zs_excl);
3217 		return (DDI_SUCCESS);
3218 	}
3219 	zs->zs_suspended = 1;
3220 
3221 	/*
3222 	 * Turn off interrupts and get any bytes in receiver
3223 	 */
3224 	mutex_enter(zs->zs_excl_hi);
3225 	SCC_BIC(1, ZSWR1_INIT);
3226 	ZSA_KICK_RCV;
3227 	restart_id = za->za_zsa_restart_id;
3228 	za->za_zsa_restart_id = 0;
3229 	kick_rcv_id = za->za_kick_rcv_id;
3230 	za->za_kick_rcv_id = 0;
3231 	mutex_exit(zs->zs_excl_hi);
3232 	mutex_exit(zs->zs_excl);
3233 
3234 	/*
3235 	 * Cancel any timeouts
3236 	 */
3237 	if (restart_id)
3238 		(void) untimeout(restart_id);
3239 	if (kick_rcv_id)
3240 		(void) untimeout(kick_rcv_id);
3241 
3242 	/*
3243 	 * Since we have turned off interrupts, zsa_txint will not be called
3244 	 * and no new chars will given to the chip. We just wait for the
3245 	 * current character(s) to drain.
3246 	 */
3247 	delay(ztdelay(za->za_ttycommon.t_cflag & CBAUD));
3248 
3249 	/*
3250 	 * Return remains of partially sent message to queue
3251 	 */
3252 	mutex_enter(zs->zs_excl);
3253 	if ((q = za->za_ttycommon.t_writeq) != NULL) {
3254 		mutex_enter(zs->zs_excl_hi);
3255 		if ((zs->zs_wr_cur) != NULL) {
3256 			za->za_flags &= ~ZAS_BUSY;
3257 			za->za_rcv_flags_mask &= ~DO_RETRANSMIT;
3258 			bp = za->za_xmitblk;
3259 			bp->b_rptr = zs->zs_wr_cur;
3260 			zs->zs_wr_cur = NULL;
3261 			zs->zs_wr_lim = NULL;
3262 			za->za_xmitblk = NULL;
3263 		}
3264 		mutex_exit(zs->zs_excl_hi);
3265 		if (bp)
3266 			(void) putbq(q, bp);
3267 	}
3268 
3269 	/*
3270 	 * Stop any breaks in progress.
3271 	 */
3272 	mutex_enter(zs->zs_excl_hi);
3273 	if (zs->zs_wreg[5] & ZSWR5_BREAK) {
3274 		SCC_BIC(5, ZSWR5_BREAK);
3275 		za->za_flags &= ~ZAS_BREAK;
3276 	}
3277 
3278 	/*
3279 	 * Now get a copy of current registers setting.
3280 	 */
3281 	zspp = &zs_prog[zs->zs_unit];
3282 	zspp->zs = zs;
3283 	zspp->flags = 0;
3284 	zspp->wr3 = zs->zs_wreg[3];
3285 	zspp->wr4 = zs->zs_wreg[4];
3286 	zspp->wr5 = zs->zs_wreg[5];
3287 	zspp->wr11 = zs->zs_wreg[11];
3288 	zspp->wr12 = zs->zs_wreg[12];
3289 	zspp->wr13 = zs->zs_wreg[13];
3290 	zspp->wr15 = zs->zs_wreg[15];
3291 	mutex_exit(zs->zs_excl_hi);
3292 	mutex_exit(zs->zs_excl);
3293 	/*
3294 	 * We do this on the off chance that zsa_close is waiting on a timed
3295 	 * break to complete and nothing else.
3296 	 */
3297 	cv_broadcast(&zs->zs_flags_cv);
3298 	return (DDI_SUCCESS);
3299 }
3300 
3301 static int
3302 zsa_resume(struct zscom *zs)
3303 {
3304 	register struct asyncline *za;
3305 	struct zs_prog	*zspp;
3306 
3307 	za = (struct asyncline *)&zs->zs_priv_str;
3308 	mutex_enter(zs->zs_excl);
3309 	if (!(zs->zs_suspended)) {
3310 		mutex_exit(zs->zs_excl);
3311 		return (DDI_SUCCESS);
3312 	}
3313 
3314 	/*
3315 	 * Restore H/W state
3316 	 */
3317 	mutex_enter(zs->zs_excl_hi);
3318 	zspp = &zs_prog[zs->zs_unit];
3319 	zs_program(zspp);
3320 
3321 	/*
3322 	 * Enable all interrupts for this chip and delay to let chip settle
3323 	 */
3324 	SCC_WRITE(9, ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT);
3325 	DELAY(4000);
3326 
3327 	/*
3328 	 * Restart receiving and transmitting
3329 	 */
3330 	zs->zs_suspended = 0;
3331 	za->za_rcv_flags_mask |= DO_TRANSMIT;
3332 	za->za_ext = 1;
3333 	ZSSETSOFT(zs);
3334 	mutex_exit(zs->zs_excl_hi);
3335 	mutex_exit(zs->zs_excl);
3336 
3337 	return (DDI_SUCCESS);
3338 }
3339 
3340 #ifdef ZSA_DEBUG
3341 static void
3342 zsa_print_info(struct zscom *zs)
3343 {
3344 	register struct asyncline *za = (struct asyncline *)&zs->zs_priv_str;
3345 	register queue_t *q = za->za_ttycommon.t_writeq;
3346 
3347 	printf(" next q=%s\n", (RD(q))->q_next->q_qinfo->qi_minfo->mi_idname);
3348 	printf("unit=%d\n", zs->zs_unit);
3349 	printf("tflag:\n");
3350 	if (za->za_ttycommon.t_flags & TS_SOFTCAR) printf(" t_fl:TS_SOFTCAR");
3351 	if (za->za_ttycommon.t_flags & TS_XCLUDE) printf(" t_fl:TS_XCLUDE");
3352 	if (za->za_ttycommon.t_iflag & IGNBRK) printf(" t_ifl:IGNBRK");
3353 	if (za->za_ttycommon.t_iflag & BRKINT) printf(" t_ifl:BRKINT");
3354 	if (za->za_ttycommon.t_iflag & IGNPAR) printf(" t_ifl:IGNPAR");
3355 	if (za->za_ttycommon.t_iflag & PARMRK) printf(" t_ifl:PARMRK");
3356 	if (za->za_ttycommon.t_iflag & INPCK) printf(" t_ifl:INPCK");
3357 	if (za->za_ttycommon.t_iflag & ISTRIP) printf(" t_ifl:ISTRIP");
3358 	if (za->za_ttycommon.t_iflag & INLCR) printf(" t_ifl:INLCR");
3359 	if (za->za_ttycommon.t_iflag & IGNCR) printf(" t_ifl:IGNCR");
3360 	if (za->za_ttycommon.t_iflag & ICRNL) printf(" t_ifl:ICRNL");
3361 	if (za->za_ttycommon.t_iflag & IUCLC) printf(" t_ifl:IUCLC");
3362 	if (za->za_ttycommon.t_iflag & IXON) printf(" t_ifl:IXON");
3363 	if (za->za_ttycommon.t_iflag & IXOFF) printf(" t_ifl:IXOFF");
3364 
3365 	printf("\n");
3366 
3367 
3368 	if (za->za_ttycommon.t_cflag & CSIZE == CS5) printf(" t_cfl:CS5");
3369 	if (za->za_ttycommon.t_cflag & CSIZE == CS6) printf(" t_cfl:CS6");
3370 	if (za->za_ttycommon.t_cflag & CSIZE == CS7) printf(" t_cfl:CS7");
3371 	if (za->za_ttycommon.t_cflag & CSIZE == CS8) printf(" t_cfl:CS8");
3372 	if (za->za_ttycommon.t_cflag & CSTOPB) printf(" t_cfl:CSTOPB");
3373 	if (za->za_ttycommon.t_cflag & CREAD) printf(" t_cfl:CREAD");
3374 	if (za->za_ttycommon.t_cflag & PARENB) printf(" t_cfl:PARENB");
3375 	if (za->za_ttycommon.t_cflag & PARODD) printf(" t_cfl:PARODD");
3376 	if (za->za_ttycommon.t_cflag & HUPCL) printf(" t_cfl:HUPCL");
3377 	if (za->za_ttycommon.t_cflag & CLOCAL) printf(" t_cfl:CLOCAL");
3378 	printf(" t_stopc=%x", za->za_ttycommon.t_stopc);
3379 	printf("\n");
3380 }
3381 #endif
3382 
3383 /*
3384  * Check for abort character sequence
3385  */
3386 static boolean_t
3387 abort_charseq_recognize(uchar_t ch)
3388 {
3389 	static int state = 0;
3390 #define	CNTRL(c) ((c)&037)
3391 	static char sequence[] = { '\r', '~', CNTRL('b') };
3392 
3393 	if (ch == sequence[state]) {
3394 		if (++state >= sizeof (sequence)) {
3395 			state = 0;
3396 			return (B_TRUE);
3397 		}
3398 	} else {
3399 		state = (ch == sequence[0]) ? 1 : 0;
3400 	}
3401 	return (B_FALSE);
3402 }
3403